]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
BRs for tar.xz source
[packages/kernel.git] / kernel-aufs5.patch
1 SPDX-License-Identifier: GPL-2.0
2 aufs5.x-rcN kbuild patch
3
4 diff --git a/fs/Kconfig b/fs/Kconfig
5 index f08fbbfafd9a0..1c597cddc351c 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -267,6 +267,7 @@ source "fs/sysv/Kconfig"
9  source "fs/ufs/Kconfig"
10  source "fs/erofs/Kconfig"
11  source "fs/vboxsf/Kconfig"
12 +source "fs/aufs/Kconfig"
13  
14  endif # MISC_FILESYSTEMS
15  
16 diff --git a/fs/Makefile b/fs/Makefile
17 index 2ce5112b02c86..d9b4a51985538 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -136,3 +136,4 @@ obj-$(CONFIG_EFIVAR_FS)             += efivarfs/
21  obj-$(CONFIG_EROFS_FS)         += erofs/
22  obj-$(CONFIG_VBOXSF_FS)                += vboxsf/
23  obj-$(CONFIG_ZONEFS_FS)                += zonefs/
24 +obj-$(CONFIG_AUFS_FS)           += aufs/
25 SPDX-License-Identifier: GPL-2.0
26 aufs5.x-rcN base patch
27
28 diff --git a/MAINTAINERS b/MAINTAINERS
29 index 50659d76976b7..80f67140e4f70 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -2946,6 +2946,19 @@ F:       include/linux/audit.h
33  F:     include/uapi/linux/audit.h
34  F:     kernel/audit*
35  
36 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
37 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
38 +L:     aufs-users@lists.sourceforge.net (members only)
39 +L:     linux-unionfs@vger.kernel.org
40 +W:     http://aufs.sourceforge.net
41 +T:     git://github.com/sfjro/aufs4-linux.git
42 +S:     Supported
43 +F:     Documentation/filesystems/aufs/
44 +F:     Documentation/ABI/testing/debugfs-aufs
45 +F:     Documentation/ABI/testing/sysfs-aufs
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY DISPLAY DRIVERS
50  M:     Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
51  S:     Maintained
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index da693e6a834e5..0479902a0cd50 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -753,6 +753,24 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
57         return error;
58  }
59  
60 +/*
61 + * for AUFS
62 + * no get/put for file.
63 + */
64 +struct file *loop_backing_file(struct super_block *sb)
65 +{
66 +       struct file *ret;
67 +       struct loop_device *l;
68 +
69 +       ret = NULL;
70 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
71 +               l = sb->s_bdev->bd_disk->private_data;
72 +               ret = l->lo_backing_file;
73 +       }
74 +       return ret;
75 +}
76 +EXPORT_SYMBOL_GPL(loop_backing_file);
77 +
78  /* loop sysfs attributes */
79  
80  static ssize_t loop_attr_show(struct device *dev, char *page,
81 diff --git a/fs/dcache.c b/fs/dcache.c
82 index b280e07e162b1..046000653e4d8 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1266,7 +1266,7 @@ enum d_walk_ret {
86   *
87   * The @enter() callbacks are called with d_lock held.
88   */
89 -static void d_walk(struct dentry *parent, void *data,
90 +void d_walk(struct dentry *parent, void *data,
91                    enum d_walk_ret (*enter)(void *, struct dentry *))
92  {
93         struct dentry *this_parent;
94 diff --git a/fs/fcntl.c b/fs/fcntl.c
95 index 2e4c0fa2074b0..0b28a37f7e505 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -32,7 +32,7 @@
99  
100  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
101  
102 -static int setfl(int fd, struct file * filp, unsigned long arg)
103 +int setfl(int fd, struct file *filp, unsigned long arg)
104  {
105         struct inode * inode = file_inode(filp);
106         int error = 0;
107 @@ -63,6 +63,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
108  
109         if (filp->f_op->check_flags)
110                 error = filp->f_op->check_flags(arg);
111 +       if (!error && filp->f_op->setfl)
112 +               error = filp->f_op->setfl(filp, arg);
113         if (error)
114                 return error;
115  
116 diff --git a/fs/inode.c b/fs/inode.c
117 index 93d9252a00ab4..7700c5e4783b9 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1688,7 +1688,7 @@ EXPORT_SYMBOL(generic_update_time);
121   * This does the actual work of updating an inodes time or version.  Must have
122   * had called mnt_want_write() before calling this.
123   */
124 -static int update_time(struct inode *inode, struct timespec64 *time, int flags)
125 +int update_time(struct inode *inode, struct timespec64 *time, int flags)
126  {
127         if (inode->i_op->update_time)
128                 return inode->i_op->update_time(inode, time, flags);
129 diff --git a/fs/namespace.c b/fs/namespace.c
130 index a28e4db075ede..12e725e34c6ca 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -776,6 +776,12 @@ static inline int check_mnt(struct mount *mnt)
134         return mnt->mnt_ns == current->nsproxy->mnt_ns;
135  }
136  
137 +/* for aufs, CONFIG_AUFS_BR_FUSE */
138 +int is_current_mnt_ns(struct vfsmount *mnt)
139 +{
140 +       return check_mnt(real_mount(mnt));
141 +}
142 +
143  /*
144   * vfsmount lock must be held for write
145   */
146 diff --git a/fs/splice.c b/fs/splice.c
147 index 4e53efbd621db..9f230ee854e20 100644
148 --- a/fs/splice.c
149 +++ b/fs/splice.c
150 @@ -849,8 +849,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
151  /*
152   * Attempt to initiate a splice from pipe to file.
153   */
154 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155 -                          loff_t *ppos, size_t len, unsigned int flags)
156 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
157 +                   loff_t *ppos, size_t len, unsigned int flags)
158  {
159         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
160                                 loff_t *, size_t, unsigned int);
161 @@ -866,9 +866,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
162  /*
163   * Attempt to initiate a splice from a file to a pipe.
164   */
165 -static long do_splice_to(struct file *in, loff_t *ppos,
166 -                        struct pipe_inode_info *pipe, size_t len,
167 -                        unsigned int flags)
168 +long do_splice_to(struct file *in, loff_t *ppos,
169 +                 struct pipe_inode_info *pipe, size_t len,
170 +                 unsigned int flags)
171  {
172         ssize_t (*splice_read)(struct file *, loff_t *,
173                                struct pipe_inode_info *, size_t, unsigned int);
174 diff --git a/fs/sync.c b/fs/sync.c
175 index 4d1ff010bc5af..457f4e4a5cc1f 100644
176 --- a/fs/sync.c
177 +++ b/fs/sync.c
178 @@ -28,7 +28,7 @@
179   * wait == 1 case since in that case write_inode() functions do
180   * sync_dirty_buffer() and thus effectively write one block at a time.
181   */
182 -static int __sync_filesystem(struct super_block *sb, int wait)
183 +int __sync_filesystem(struct super_block *sb, int wait)
184  {
185         if (wait)
186                 sync_inodes_sb(sb);
187 diff --git a/include/linux/fs.h b/include/linux/fs.h
188 index 45cc10cdf6ddd..916efd7e612b3 100644
189 --- a/include/linux/fs.h
190 +++ b/include/linux/fs.h
191 @@ -1370,6 +1370,7 @@
192  /* can be called from interrupts */
193  extern void kill_fasync(struct fasync_struct **, int, int);
194  
195 +extern int setfl(int fd, struct file *filp, unsigned long arg);
196  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
197  extern int f_setown(struct file *filp, unsigned long arg, int force);
198  extern void f_delown(struct file *filp);
199 @@ -1858,6 +1859,7 @@
200         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
201         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
202         int (*check_flags)(int);
203 +       int (*setfl)(struct file *, unsigned long);
204         int (*flock) (struct file *, int, struct file_lock *);
205         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
206         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
207 @@ -2362,6 +2370,7 @@
208  extern void ihold(struct inode * inode);
209  extern void iput(struct inode *);
210  extern int generic_update_time(struct inode *, struct timespec64 *, int);
211 +extern int update_time(struct inode *, struct timespec64 *, int);
212  
213  /* /sys/fs */
214  extern struct kobject *fs_kobj;
215 @@ -2549,6 +2558,7 @@
216  }
217  
218  void emergency_thaw_all(void);
219 +extern int __sync_filesystem(struct super_block *, int);
220  extern int sync_filesystem(struct super_block *);
221  extern const struct file_operations def_blk_fops;
222  extern const struct file_operations def_chr_fops;
223 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
224 index 206774ac69460..e5eda5226fbc7 100644
225 --- a/include/linux/lockdep.h
226 +++ b/include/linux/lockdep.h
227 @@ -376,6 +376,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
228         return lock->key == key;
229  }
230  
231 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
232 +
233  /*
234   * Acquire a lock.
235   *
236 @@ -521,6 +523,7 @@ struct lockdep_map { };
237  
238  #define lockdep_depth(tsk)     (0)
239  
240 +#define lockdep_is_held(lock)                  (1)
241  #define lockdep_is_held_type(l, r)             (1)
242  
243  #define lockdep_assert_held(l)                 do { (void)(l); } while (0)
244 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
245 index 35942084cd40d..24f5fd1a789de 100644
246 --- a/include/linux/mnt_namespace.h
247 +++ b/include/linux/mnt_namespace.h
248 @@ -7,12 +7,15 @@
249  struct fs_struct;
250  struct user_namespace;
251  struct ns_common;
252 +struct vfsmount;
253  
254  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
255                 struct user_namespace *, struct fs_struct *);
256  extern void put_mnt_ns(struct mnt_namespace *ns);
257  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
258  
259 +extern int is_current_mnt_ns(struct vfsmount *mnt);
260 +
261  extern const struct file_operations proc_mounts_operations;
262  extern const struct file_operations proc_mountinfo_operations;
263  extern const struct file_operations proc_mountstats_operations;
264 diff --git a/include/linux/splice.h b/include/linux/splice.h
265 index ebbbfea48aa0b..d68d574250283 100644
266 --- a/include/linux/splice.h
267 +++ b/include/linux/splice.h
268 @@ -90,4 +90,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
269  
270  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
271  extern const struct pipe_buf_operations default_pipe_buf_ops;
272 +
273 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
274 +                          loff_t *ppos, size_t len, unsigned int flags);
275 +extern long do_splice_to(struct file *in, loff_t *ppos,
276 +                        struct pipe_inode_info *pipe, size_t len,
277 +                        unsigned int flags);
278  #endif
279 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
280 index ac10db66cc63f..837a3358e77ca 100644
281 --- a/kernel/locking/lockdep.c
282 +++ b/kernel/locking/lockdep.c
283 @@ -169,7 +169,7 @@ static
284  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
285  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
286  
287 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
288 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
289  {
290         unsigned int class_idx = hlock->class_idx;
291  
292 @@ -190,6 +190,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
293          */
294         return lock_classes + class_idx;
295  }
296 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
297  
298  #ifdef CONFIG_LOCK_STAT
299  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
300 SPDX-License-Identifier: GPL-2.0
301 aufs5.x-rcN mmap patch
302
303 diff --git a/fs/proc/base.c b/fs/proc/base.c
304 index eb2255e95f62b..92f6062423c0c 100644
305 --- a/fs/proc/base.c
306 +++ b/fs/proc/base.c
307 @@ -2162,7 +2162,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
308         rc = -ENOENT;
309         vma = find_exact_vma(mm, vm_start, vm_end);
310         if (vma && vma->vm_file) {
311 -               *path = vma->vm_file->f_path;
312 +               *path = vma_pr_or_file(vma)->f_path;
313                 path_get(path);
314                 rc = 0;
315         }
316 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
317 index 14c2badb8fd93..65afe5287e43e 100644
318 --- a/fs/proc/nommu.c
319 +++ b/fs/proc/nommu.c
320 @@ -41,7 +41,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
321         file = region->vm_file;
322  
323         if (file) {
324 -               struct inode *inode = file_inode(region->vm_file);
325 +               struct inode *inode;
326 +
327 +               file = vmr_pr_or_file(region);
328 +               inode = file_inode(file);
329                 dev = inode->i_sb->s_dev;
330                 ino = inode->i_ino;
331         }
332 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
333 index 8d382d4ec0672..93a768f754c91 100644
334 --- a/fs/proc/task_mmu.c
335 +++ b/fs/proc/task_mmu.c
336 @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
337         const char *name = NULL;
338  
339         if (file) {
340 -               struct inode *inode = file_inode(vma->vm_file);
341 +               struct inode *inode;
342 +
343 +               file = vma_pr_or_file(vma);
344 +               inode = file_inode(file);
345                 dev = inode->i_sb->s_dev;
346                 ino = inode->i_ino;
347                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
348 @@ -1787,7 +1790,7 @@ static int show_numa_map(struct seq_file *m, void *v)
349         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
350         struct vm_area_struct *vma = v;
351         struct numa_maps *md = &numa_priv->md;
352 -       struct file *file = vma->vm_file;
353 +       struct file *file = vma_pr_or_file(vma);
354         struct mm_struct *mm = vma->vm_mm;
355         struct mempolicy *pol;
356         char buffer[64];
357 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
358 index 7907e6419e572..d17209cf52bc3 100644
359 --- a/fs/proc/task_nommu.c
360 +++ b/fs/proc/task_nommu.c
361 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
362         file = vma->vm_file;
363  
364         if (file) {
365 -               struct inode *inode = file_inode(vma->vm_file);
366 +               struct inode *inode;
367 +
368 +               file = vma_pr_or_file(vma);
369 +               inode = file_inode(file);
370                 dev = inode->i_sb->s_dev;
371                 ino = inode->i_ino;
372                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
373 diff --git a/include/linux/mm.h b/include/linux/mm.h
374 index f3fe7371855ce..a5b9751a5b331 100644
375 --- a/include/linux/mm.h
376 +++ b/include/linux/mm.h
377 @@ -1688,6 +1688,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
378         unmap_mapping_range(mapping, holebegin, holelen, 0);
379  }
380  
381 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
382 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
383 +                                     int);
384 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
385 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
386 +
387 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
388 +                                                               __LINE__)
389 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
390 +                                                         __LINE__)
391 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
392 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
393 +
394 +#ifndef CONFIG_MMU
395 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
396 +extern void vmr_do_fput(struct vm_region *, const char[], int);
397 +
398 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
399 +                                                         __LINE__)
400 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
401 +#endif /* !CONFIG_MMU */
402 +
403  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
404                 void *buf, int len, unsigned int gup_flags);
405  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
406 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
407 index 4aba6c0c2ba80..974802987f69d 100644
408 --- a/include/linux/mm_types.h
409 +++ b/include/linux/mm_types.h
410 @@ -272,6 +272,7 @@ struct vm_region {
411         unsigned long   vm_top;         /* region allocated to here */
412         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
413         struct file     *vm_file;       /* the backing file or NULL */
414 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
415  
416         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
417         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
418 @@ -351,6 +352,7 @@ struct vm_area_struct {
419         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
420                                            units */
421         struct file * vm_file;          /* File we map to (can be NULL). */
422 +       struct file *vm_prfile;         /* shadow of vm_file */
423         void * vm_private_data;         /* was vm_pte (shared mem) */
424  
425  #ifdef CONFIG_SWAP
426 diff --git a/kernel/fork.c b/kernel/fork.c
427 index 48ed22774efaa..d7439388c1c52 100644
428 --- a/kernel/fork.c
429 +++ b/kernel/fork.c
430 @@ -568,7 +568,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
431                         struct inode *inode = file_inode(file);
432                         struct address_space *mapping = file->f_mapping;
433  
434 -                       get_file(file);
435 +                       vma_get_file(tmp);
436                         if (tmp->vm_flags & VM_DENYWRITE)
437                                 atomic_dec(&inode->i_writecount);
438                         i_mmap_lock_write(mapping);
439 diff --git a/mm/Makefile b/mm/Makefile
440 index fccd3756b25f0..51485ceec650a 100644
441 --- a/mm/Makefile
442 +++ b/mm/Makefile
443 @@ -44,7 +44,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
444                            mm_init.o mmu_context.o percpu.o slab_common.o \
445                            compaction.o vmacache.o \
446                            interval_tree.o list_lru.o workingset.o \
447 -                          debug.o gup.o $(mmu-y)
448 +                          prfile.o debug.o gup.o $(mmu-y)
449  
450  # Give 'page_alloc' its own module-parameter namespace
451  page-alloc-y := page_alloc.o
452 diff --git a/mm/filemap.c b/mm/filemap.c
453 index 23a051a7ef0fb..e2fe9132feb21 100644
454 --- a/mm/filemap.c
455 +++ b/mm/filemap.c
456 @@ -2660,7 +2660,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
457         vm_fault_t ret = VM_FAULT_LOCKED;
458  
459         sb_start_pagefault(inode->i_sb);
460 -       file_update_time(vmf->vma->vm_file);
461 +       vma_file_update_time(vmf->vma);
462         lock_page(page);
463         if (page->mapping != inode->i_mapping) {
464                 unlock_page(page);
465 diff --git a/mm/mmap.c b/mm/mmap.c
466 index f609e9ec4a253..d554b349c653c 100644
467 --- a/mm/mmap.c
468 +++ b/mm/mmap.c
469 @@ -179,7 +179,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
470         if (vma->vm_ops && vma->vm_ops->close)
471                 vma->vm_ops->close(vma);
472         if (vma->vm_file)
473 -               fput(vma->vm_file);
474 +               vma_fput(vma);
475         mpol_put(vma_policy(vma));
476         vm_area_free(vma);
477         return next;
478 @@ -910,7 +910,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
479         if (remove_next) {
480                 if (file) {
481                         uprobe_munmap(next, next->vm_start, next->vm_end);
482 -                       fput(file);
483 +                       vma_fput(vma);
484                 }
485                 if (next->anon_vma)
486                         anon_vma_merge(vma, next);
487 @@ -1831,8 +1831,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
488         return addr;
489  
490  unmap_and_free_vma:
491 +       vma_fput(vma);
492         vma->vm_file = NULL;
493 -       fput(file);
494  
495         /* Undo any partial mapping done by a device driver. */
496         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
497 @@ -2683,7 +2683,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
498                 goto out_free_mpol;
499  
500         if (new->vm_file)
501 -               get_file(new->vm_file);
502 +               vma_get_file(new);
503  
504         if (new->vm_ops && new->vm_ops->open)
505                 new->vm_ops->open(new);
506 @@ -2702,7 +2702,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
507         if (new->vm_ops && new->vm_ops->close)
508                 new->vm_ops->close(new);
509         if (new->vm_file)
510 -               fput(new->vm_file);
511 +               vma_fput(new);
512         unlink_anon_vmas(new);
513   out_free_mpol:
514         mpol_put(vma_policy(new));
515 @@ -2894,7 +2894,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
516         struct vm_area_struct *vma;
517         unsigned long populate = 0;
518         unsigned long ret = -EINVAL;
519 -       struct file *file;
520 +       struct file *file, *prfile;
521  
522         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
523                      current->comm, current->pid);
524 @@ -2969,10 +2969,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
525                 }
526         }
527  
528 -       file = get_file(vma->vm_file);
529 +       vma_get_file(vma);
530 +       file = vma->vm_file;
531 +       prfile = vma->vm_prfile;
532         ret = do_mmap(vma->vm_file, start, size,
533                         prot, flags, pgoff, &populate, NULL);
534 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
535 +               struct vm_area_struct *new_vma;
536 +
537 +               new_vma = find_vma(mm, ret);
538 +               if (!new_vma->vm_prfile)
539 +                       new_vma->vm_prfile = prfile;
540 +               if (new_vma != vma)
541 +                       get_file(prfile);
542 +       }
543 +       /*
544 +        * two fput()s instead of vma_fput(vma),
545 +        * coz vma may not be available anymore.
546 +        */
547         fput(file);
548 +       if (prfile)
549 +               fput(prfile);
550  out:
551         up_write(&mm->mmap_sem);
552         if (populate)
553 @@ -3263,7 +3280,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
554                 if (anon_vma_clone(new_vma, vma))
555                         goto out_free_mempol;
556                 if (new_vma->vm_file)
557 -                       get_file(new_vma->vm_file);
558 +                       vma_get_file(new_vma);
559                 if (new_vma->vm_ops && new_vma->vm_ops->open)
560                         new_vma->vm_ops->open(new_vma);
561                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
562 diff --git a/mm/nommu.c b/mm/nommu.c
563 index 318df4e236c99..7f051e86ea1dd 100644
564 --- a/mm/nommu.c
565 +++ b/mm/nommu.c
566 @@ -567,7 +567,7 @@ static void __put_nommu_region(struct vm_region *region)
567                 up_write(&nommu_region_sem);
568  
569                 if (region->vm_file)
570 -                       fput(region->vm_file);
571 +                       vmr_fput(region);
572  
573                 /* IO memory and memory shared directly out of the pagecache
574                  * from ramfs/tmpfs mustn't be released here */
575 @@ -699,7 +699,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
576         if (vma->vm_ops && vma->vm_ops->close)
577                 vma->vm_ops->close(vma);
578         if (vma->vm_file)
579 -               fput(vma->vm_file);
580 +               vma_fput(vma);
581         put_nommu_region(vma->vm_region);
582         vm_area_free(vma);
583  }
584 @@ -1222,7 +1222,7 @@ unsigned long do_mmap(struct file *file,
585                                         goto error_just_free;
586                                 }
587                         }
588 -                       fput(region->vm_file);
589 +                       vmr_fput(region);
590                         kmem_cache_free(vm_region_jar, region);
591                         region = pregion;
592                         result = start;
593 @@ -1299,10 +1299,10 @@ unsigned long do_mmap(struct file *file,
594         up_write(&nommu_region_sem);
595  error:
596         if (region->vm_file)
597 -               fput(region->vm_file);
598 +               vmr_fput(region);
599         kmem_cache_free(vm_region_jar, region);
600         if (vma->vm_file)
601 -               fput(vma->vm_file);
602 +               vma_fput(vma);
603         vm_area_free(vma);
604         return ret;
605  
606 diff --git a/mm/prfile.c b/mm/prfile.c
607 new file mode 100644
608 index 0000000000000..00d51187c3250
609 --- /dev/null
610 +++ b/mm/prfile.c
611 @@ -0,0 +1,86 @@
612 +// SPDX-License-Identifier: GPL-2.0
613 +/*
614 + * Mainly for aufs which mmap(2) different file and wants to print different
615 + * path in /proc/PID/maps.
616 + * Call these functions via macros defined in linux/mm.h.
617 + *
618 + * See Documentation/filesystems/aufs/design/06mmap.txt
619 + *
620 + * Copyright (c) 2014-2020 Junjro R. Okajima
621 + * Copyright (c) 2014 Ian Campbell
622 + */
623 +
624 +#include <linux/mm.h>
625 +#include <linux/file.h>
626 +#include <linux/fs.h>
627 +
628 +/* #define PRFILE_TRACE */
629 +static inline void prfile_trace(struct file *f, struct file *pr,
630 +                             const char func[], int line, const char func2[])
631 +{
632 +#ifdef PRFILE_TRACE
633 +       if (pr)
634 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
635 +#endif
636 +}
637 +
638 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
639 +                            int line)
640 +{
641 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
642 +
643 +       prfile_trace(f, pr, func, line, __func__);
644 +       file_update_time(f);
645 +       if (f && pr)
646 +               file_update_time(pr);
647 +}
648 +
649 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
650 +                              int line)
651 +{
652 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
653 +
654 +       prfile_trace(f, pr, func, line, __func__);
655 +       return (f && pr) ? pr : f;
656 +}
657 +
658 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
659 +{
660 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
661 +
662 +       prfile_trace(f, pr, func, line, __func__);
663 +       get_file(f);
664 +       if (f && pr)
665 +               get_file(pr);
666 +}
667 +
668 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
669 +{
670 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
671 +
672 +       prfile_trace(f, pr, func, line, __func__);
673 +       fput(f);
674 +       if (f && pr)
675 +               fput(pr);
676 +}
677 +
678 +#ifndef CONFIG_MMU
679 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
680 +                              int line)
681 +{
682 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
683 +
684 +       prfile_trace(f, pr, func, line, __func__);
685 +       return (f && pr) ? pr : f;
686 +}
687 +
688 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
689 +{
690 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
691 +
692 +       prfile_trace(f, pr, func, line, __func__);
693 +       fput(f);
694 +       if (f && pr)
695 +               fput(pr);
696 +}
697 +#endif /* !CONFIG_MMU */
698 SPDX-License-Identifier: GPL-2.0
699 aufs5.x-rcN standalone patch
700
701 diff --git a/fs/dcache.c b/fs/dcache.c
702 index 046000653e4d8..15aa871d1b450 100644
703 --- a/fs/dcache.c
704 +++ b/fs/dcache.c
705 @@ -1371,6 +1371,7 @@ void d_walk(struct dentry *parent, void *data,
706         seq = 1;
707         goto again;
708  }
709 +EXPORT_SYMBOL_GPL(d_walk);
710  
711  struct check_mount {
712         struct vfsmount *mnt;
713 @@ -2916,6 +2917,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
714  
715         write_sequnlock(&rename_lock);
716  }
717 +EXPORT_SYMBOL_GPL(d_exchange);
718  
719  /**
720   * d_ancestor - search for an ancestor
721 diff --git a/fs/exec.c b/fs/exec.c
722 index 2c465119affcc..239bcb8906e78 100644
723 --- a/fs/exec.c
724 +++ b/fs/exec.c
725 @@ -109,6 +109,7 @@ bool path_noexec(const struct path *path)
726         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
727                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
728  }
729 +EXPORT_SYMBOL_GPL(path_noexec);
730  
731  #ifdef CONFIG_USELIB
732  /*
733 diff --git a/fs/fcntl.c b/fs/fcntl.c
734 index 0b28a37f7e505..f2c90a416b751 100644
735 --- a/fs/fcntl.c
736 +++ b/fs/fcntl.c
737 @@ -85,6 +85,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
738   out:
739         return error;
740  }
741 +EXPORT_SYMBOL_GPL(setfl);
742  
743  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
744                       int force)
745 diff --git a/fs/file_table.c b/fs/file_table.c
746 index 30d55c9a1744a..34b9bbf4c5566 100644
747 --- a/fs/file_table.c
748 +++ b/fs/file_table.c
749 @@ -162,6 +162,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
750         }
751         return ERR_PTR(-ENFILE);
752  }
753 +EXPORT_SYMBOL_GPL(alloc_empty_file);
754  
755  /*
756   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
757 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
758  }
759  
760  EXPORT_SYMBOL(fput);
761 +EXPORT_SYMBOL_GPL(__fput_sync);
762  
763  void __init files_init(void)
764  {
765 diff --git a/fs/inode.c b/fs/inode.c
766 index 7700c5e4783b9..6284b41fcf3f0 100644
767 --- a/fs/inode.c
768 +++ b/fs/inode.c
769 @@ -1694,6 +1694,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
770                 return inode->i_op->update_time(inode, time, flags);
771         return generic_update_time(inode, time, flags);
772  }
773 +EXPORT_SYMBOL_GPL(update_time);
774  
775  /**
776   *     touch_atime     -       update the access time
777 diff --git a/fs/namespace.c b/fs/namespace.c
778 index 12e725e34c6ca..fa17b9d5926ba 100644
779 --- a/fs/namespace.c
780 +++ b/fs/namespace.c
781 @@ -431,6 +431,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
782         mnt_dec_writers(real_mount(mnt));
783         preempt_enable();
784  }
785 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
786  
787  /**
788   * mnt_drop_write - give up write access to a mount
789 @@ -781,6 +782,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
790  {
791         return check_mnt(real_mount(mnt));
792  }
793 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
794  
795  /*
796   * vfsmount lock must be held for write
797 @@ -1903,6 +1905,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
798         }
799         return 0;
800  }
801 +EXPORT_SYMBOL_GPL(iterate_mounts);
802  
803  static void lock_mnt_tree(struct mount *mnt)
804  {
805 diff --git a/fs/notify/group.c b/fs/notify/group.c
806 index 133f723aca070..0b9f7f6d8390f 100644
807 --- a/fs/notify/group.c
808 +++ b/fs/notify/group.c
809 @@ -99,6 +99,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
810  {
811         refcount_inc(&group->refcnt);
812  }
813 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
814  
815  /*
816   * Drop a reference to a group.  Free it if it's through.
817 diff --git a/fs/open.c b/fs/open.c
818 index 719b320ede52b..f88ce55c1c998 100644
819 --- a/fs/open.c
820 +++ b/fs/open.c
821 @@ -65,6 +65,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
822         inode_unlock(dentry->d_inode);
823         return ret;
824  }
825 +EXPORT_SYMBOL_GPL(do_truncate);
826  
827  long vfs_truncate(const struct path *path, loff_t length)
828  {
829 diff --git a/fs/read_write.c b/fs/read_write.c
830 index 5d684d7d29207..6f61c0b6aa664 100644
831 --- a/fs/read_write.c
832 +++ b/fs/read_write.c
833 @@ -469,6 +469,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
834  
835         return ret;
836  }
837 +EXPORT_SYMBOL_GPL(vfs_read);
838  
839  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
840  {
841 @@ -589,6 +592,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
842  
843         return ret;
844  }
845 +EXPORT_SYMBOL_GPL(vfs_write);
846  
847  /* file_ppos returns &file->f_pos or NULL if file is stream */
848  static inline loff_t *file_ppos(struct file *file)
849 diff --git a/fs/splice.c b/fs/splice.c
850 index 9f230ee854e20..a6bd05004433c 100644
851 --- a/fs/splice.c
852 +++ b/fs/splice.c
853 @@ -862,6 +862,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
854  
855         return splice_write(pipe, out, ppos, len, flags);
856  }
857 +EXPORT_SYMBOL_GPL(do_splice_from);
858  
859  /*
860   * Attempt to initiate a splice from a file to a pipe.
861 @@ -891,6 +892,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
862  
863         return splice_read(in, ppos, pipe, len, flags);
864  }
865 +EXPORT_SYMBOL_GPL(do_splice_to);
866  
867  /**
868   * splice_direct_to_actor - splices data directly between two non-pipes
869 diff --git a/fs/sync.c b/fs/sync.c
870 index 457f4e4a5cc1f..67c66358f3fe4 100644
871 --- a/fs/sync.c
872 +++ b/fs/sync.c
873 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
874                 sb->s_op->sync_fs(sb, wait);
875         return __sync_blockdev(sb->s_bdev, wait);
876  }
877 +EXPORT_SYMBOL_GPL(__sync_filesystem);
878  
879  /*
880   * Write out and wait upon all dirty data associated with this
881 diff --git a/fs/xattr.c b/fs/xattr.c
882 index 91608d9bfc6aa..02d19ab3ba540 100644
883 --- a/fs/xattr.c
884 +++ b/fs/xattr.c
885 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
886         *xattr_value = value;
887         return error;
888  }
889 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
890  
891  ssize_t
892  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
893 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
894 index 837a3358e77ca..715ba9c1b91ae 100644
895 --- a/kernel/locking/lockdep.c
896 +++ b/kernel/locking/lockdep.c
897 @@ -190,6 +190,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
898          */
899         return lock_classes + class_idx;
900  }
901 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
902  #define hlock_class(hlock) lockdep_hlock_class(hlock)
903  
904  #ifdef CONFIG_LOCK_STAT
905 diff --git a/kernel/task_work.c b/kernel/task_work.c
906 index 825f28259a19a..b77593b29c1a5 100644
907 --- a/kernel/task_work.c
908 +++ b/kernel/task_work.c
909 @@ -126,3 +126,4 @@ void task_work_run(void)
910                 } while (work);
911         }
912  }
913 +EXPORT_SYMBOL_GPL(task_work_run);
914 diff --git a/security/security.c b/security/security.c
915 index 51de970fbb1ed..b21f1ffd90051 100644
916 --- a/security/security.c
917 +++ b/security/security.c
918 @@ -1087,6 +1087,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
919                 return 0;
920         return call_int_hook(path_rmdir, 0, dir, dentry);
921  }
922 +EXPORT_SYMBOL_GPL(security_path_rmdir);
923  
924  int security_path_unlink(const struct path *dir, struct dentry *dentry)
925  {
926 @@ -1103,6 +1104,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
927                 return 0;
928         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
929  }
930 +EXPORT_SYMBOL_GPL(security_path_symlink);
931  
932  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
933                        struct dentry *new_dentry)
934 @@ -1111,6 +1113,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
935                 return 0;
936         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
937  }
938 +EXPORT_SYMBOL_GPL(security_path_link);
939  
940  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
941                          const struct path *new_dir, struct dentry *new_dentry,
942 @@ -1138,6 +1141,7 @@ int security_path_truncate(const struct path *path)
943                 return 0;
944         return call_int_hook(path_truncate, 0, path);
945  }
946 +EXPORT_SYMBOL_GPL(security_path_truncate);
947  
948  int security_path_chmod(const struct path *path, umode_t mode)
949  {
950 @@ -1145,6 +1149,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
951                 return 0;
952         return call_int_hook(path_chmod, 0, path, mode);
953  }
954 +EXPORT_SYMBOL_GPL(security_path_chmod);
955  
956  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
957  {
958 @@ -1152,6 +1157,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
959                 return 0;
960         return call_int_hook(path_chown, 0, path, uid, gid);
961  }
962 +EXPORT_SYMBOL_GPL(security_path_chown);
963  
964  int security_path_chroot(const struct path *path)
965  {
966 @@ -1252,6 +1258,7 @@ int security_inode_permission(struct inode *inode, int mask)
967                 return 0;
968         return call_int_hook(inode_permission, 0, inode, mask);
969  }
970 +EXPORT_SYMBOL_GPL(security_inode_permission);
971  
972  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
973  {
974 @@ -1429,6 +1436,7 @@ int security_file_permission(struct file *file, int mask)
975  
976         return fsnotify_perm(file, mask);
977  }
978 +EXPORT_SYMBOL_GPL(security_file_permission);
979  
980  int security_file_alloc(struct file *file)
981  {
982 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
983 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
984 +++ linux/Documentation/ABI/testing/debugfs-aufs        2019-07-11 15:42:14.455570938 +0200
985 @@ -0,0 +1,55 @@
986 +What:          /debug/aufs/si_<id>/
987 +Date:          March 2009
988 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
989 +Description:
990 +               Under /debug/aufs, a directory named si_<id> is created
991 +               per aufs mount, where <id> is a unique id generated
992 +               internally.
993 +
994 +What:          /debug/aufs/si_<id>/plink
995 +Date:          Apr 2013
996 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
997 +Description:
998 +               It has three lines and shows the information about the
999 +               pseudo-link. The first line is a single number
1000 +               representing a number of buckets. The second line is a
1001 +               number of pseudo-links per buckets (separated by a
1002 +               blank). The last line is a single number representing a
1003 +               total number of psedo-links.
1004 +               When the aufs mount option 'noplink' is specified, it
1005 +               will show "1\n0\n0\n".
1006 +
1007 +What:          /debug/aufs/si_<id>/xib
1008 +Date:          March 2009
1009 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1010 +Description:
1011 +               It shows the consumed blocks by xib (External Inode Number
1012 +               Bitmap), its block size and file size.
1013 +               When the aufs mount option 'noxino' is specified, it
1014 +               will be empty. About XINO files, see the aufs manual.
1015 +
1016 +What:          /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
1017 +Date:          March 2009
1018 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1019 +Description:
1020 +               It shows the consumed blocks by xino (External Inode Number
1021 +               Translation Table), its link count, block size and file
1022 +               size.
1023 +               Due to the file size limit, there may exist multiple
1024 +               xino files per branch.  In this case, "-N" is added to
1025 +               the filename and it corresponds to the index of the
1026 +               internal xino array.  "-0" is omitted.
1027 +               When the aufs mount option 'noxino' is specified, Those
1028 +               entries won't exist.  About XINO files, see the aufs
1029 +               manual.
1030 +
1031 +What:          /debug/aufs/si_<id>/xigen
1032 +Date:          March 2009
1033 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1034 +Description:
1035 +               It shows the consumed blocks by xigen (External Inode
1036 +               Generation Table), its block size and file size.
1037 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1038 +               be created.
1039 +               When the aufs mount option 'noxino' is specified, it
1040 +               will be empty. About XINO files, see the aufs manual.
1041 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1042 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1043 +++ linux/Documentation/ABI/testing/sysfs-aufs  2019-07-11 15:42:14.455570938 +0200
1044 @@ -0,0 +1,31 @@
1045 +What:          /sys/fs/aufs/si_<id>/
1046 +Date:          March 2009
1047 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1048 +Description:
1049 +               Under /sys/fs/aufs, a directory named si_<id> is created
1050 +               per aufs mount, where <id> is a unique id generated
1051 +               internally.
1052 +
1053 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1054 +Date:          March 2009
1055 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1056 +Description:
1057 +               It shows the abolute path of a member directory (which
1058 +               is called branch) in aufs, and its permission.
1059 +
1060 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1061 +Date:          July 2013
1062 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1063 +Description:
1064 +               It shows the id of a member directory (which is called
1065 +               branch) in aufs.
1066 +
1067 +What:          /sys/fs/aufs/si_<id>/xi_path
1068 +Date:          March 2009
1069 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1070 +Description:
1071 +               It shows the abolute path of XINO (External Inode Number
1072 +               Bitmap, Translation Table and Generation Table) file
1073 +               even if it is the default path.
1074 +               When the aufs mount option 'noxino' is specified, it
1075 +               will be empty. About XINO files, see the aufs manual.
1076 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1077 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1078 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2020-01-27 10:57:18.162204582 +0100
1079 @@ -0,0 +1,171 @@
1080 +
1081 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1082 +# 
1083 +# This program is free software; you can redistribute it and/or modify
1084 +# it under the terms of the GNU General Public License as published by
1085 +# the Free Software Foundation; either version 2 of the License, or
1086 +# (at your option) any later version.
1087 +# 
1088 +# This program is distributed in the hope that it will be useful,
1089 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1090 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1091 +# GNU General Public License for more details.
1092 +# 
1093 +# You should have received a copy of the GNU General Public License
1094 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1095 +
1096 +Introduction
1097 +----------------------------------------
1098 +
1099 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1100 +1. abbrev. for "advanced multi-layered unification filesystem".
1101 +2. abbrev. for "another unionfs".
1102 +3. abbrev. for "auf das" in German which means "on the" in English.
1103 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1104 +   But "Filesystem aufs Filesystem" is hard to understand.
1105 +4. abbrev. for "African Urban Fashion Show".
1106 +
1107 +AUFS is a filesystem with features:
1108 +- multi layered stackable unification filesystem, the member directory
1109 +  is called as a branch.
1110 +- branch permission and attribute, 'readonly', 'real-readonly',
1111 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1112 +  combination.
1113 +- internal "file copy-on-write".
1114 +- logical deletion, whiteout.
1115 +- dynamic branch manipulation, adding, deleting and changing permission.
1116 +- allow bypassing aufs, user's direct branch access.
1117 +- external inode number translation table and bitmap which maintains the
1118 +  persistent aufs inode number.
1119 +- seekable directory, including NFS readdir.
1120 +- file mapping, mmap and sharing pages.
1121 +- pseudo-link, hardlink over branches.
1122 +- loopback mounted filesystem as a branch.
1123 +- several policies to select one among multiple writable branches.
1124 +- revert a single systemcall when an error occurs in aufs.
1125 +- and more...
1126 +
1127 +
1128 +Multi Layered Stackable Unification Filesystem
1129 +----------------------------------------------------------------------
1130 +Most people already knows what it is.
1131 +It is a filesystem which unifies several directories and provides a
1132 +merged single directory. When users access a file, the access will be
1133 +passed/re-directed/converted (sorry, I am not sure which English word is
1134 +correct) to the real file on the member filesystem. The member
1135 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1136 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1137 +readonly branch is handled by creating 'whiteout' on the upper writable
1138 +branch.
1139 +
1140 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1141 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1142 +different approaches to implement the merged-view.
1143 +The former tries putting it into VFS, and the latter implements as a
1144 +separate filesystem.
1145 +(If I misunderstand about these implementations, please let me know and
1146 +I shall correct it. Because it is a long time ago when I read their
1147 +source files last time).
1148 +
1149 +UnionMount's approach will be able to small, but may be hard to share
1150 +branches between several UnionMount since the whiteout in it is
1151 +implemented in the inode on branch filesystem and always
1152 +shared. According to Bharata's post, readdir does not seems to be
1153 +finished yet.
1154 +There are several missing features known in this implementations such as
1155 +- for users, the inode number may change silently. eg. copy-up.
1156 +- link(2) may break by copy-up.
1157 +- read(2) may get an obsoleted filedata (fstat(2) too).
1158 +- fcntl(F_SETLK) may be broken by copy-up.
1159 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1160 +  open(O_RDWR).
1161 +
1162 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1163 +merged into mainline. This is another implementation of UnionMount as a
1164 +separated filesystem. All the limitations and known problems which
1165 +UnionMount are equally inherited to "overlay" filesystem.
1166 +
1167 +Unionfs has a longer history. When I started implementing a stackable
1168 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1169 +inode, dentry and file objects and they have an array pointing lower
1170 +same kind objects. After contributing many patches for Unionfs, I
1171 +re-started my project AUFS (Jun 2006).
1172 +
1173 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1174 +implemented my own ideas, approaches and enhancements and it became
1175 +totally different one.
1176 +
1177 +Comparing DM snapshot and fs based implementation
1178 +- the number of bytes to be copied between devices is much smaller.
1179 +- the type of filesystem must be one and only.
1180 +- the fs must be writable, no readonly fs, even for the lower original
1181 +  device. so the compression fs will not be usable. but if we use
1182 +  loopback mount, we may address this issue.
1183 +  for instance,
1184 +       mount /cdrom/squashfs.img /sq
1185 +       losetup /sq/ext2.img
1186 +       losetup /somewhere/cow
1187 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1188 +- it will be difficult (or needs more operations) to extract the
1189 +  difference between the original device and COW.
1190 +- DM snapshot-merge may help a lot when users try merging. in the
1191 +  fs-layer union, users will use rsync(1).
1192 +
1193 +You may want to read my old paper "Filesystems in LiveCD"
1194 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1195 +
1196 +
1197 +Several characters/aspects/persona of aufs
1198 +----------------------------------------------------------------------
1199 +
1200 +Aufs has several characters, aspects or persona.
1201 +1. a filesystem, callee of VFS helper
1202 +2. sub-VFS, caller of VFS helper for branches
1203 +3. a virtual filesystem which maintains persistent inode number
1204 +4. reader/writer of files on branches such like an application
1205 +
1206 +1. Callee of VFS Helper
1207 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1208 +unlink(2) from an application reaches sys_unlink() kernel function and
1209 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1210 +calls filesystem specific unlink operation. Actually aufs implements the
1211 +unlink operation but it behaves like a redirector.
1212 +
1213 +2. Caller of VFS Helper for Branches
1214 +aufs_unlink() passes the unlink request to the branch filesystem as if
1215 +it were called from VFS. So the called unlink operation of the branch
1216 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1217 +every necessary pre/post operation for the branch filesystem.
1218 +- acquire the lock for the parent dir on a branch
1219 +- lookup in a branch
1220 +- revalidate dentry on a branch
1221 +- mnt_want_write() for a branch
1222 +- vfs_unlink() for a branch
1223 +- mnt_drop_write() for a branch
1224 +- release the lock on a branch
1225 +
1226 +3. Persistent Inode Number
1227 +One of the most important issue for a filesystem is to maintain inode
1228 +numbers. This is particularly important to support exporting a
1229 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1230 +backend block device for its own. But some storage is necessary to
1231 +keep and maintain the inode numbers. It may be a large space and may not
1232 +suit to keep in memory. Aufs rents some space from its first writable
1233 +branch filesystem (by default) and creates file(s) on it. These files
1234 +are created by aufs internally and removed soon (currently) keeping
1235 +opened.
1236 +Note: Because these files are removed, they are totally gone after
1237 +      unmounting aufs. It means the inode numbers are not persistent
1238 +      across unmount or reboot. I have a plan to make them really
1239 +      persistent which will be important for aufs on NFS server.
1240 +
1241 +4. Read/Write Files Internally (copy-on-write)
1242 +Because a branch can be readonly, when you write a file on it, aufs will
1243 +"copy-up" it to the upper writable branch internally. And then write the
1244 +originally requested thing to the file. Generally kernel doesn't
1245 +open/read/write file actively. In aufs, even a single write may cause a
1246 +internal "file copy". This behaviour is very similar to cp(1) command.
1247 +
1248 +Some people may think it is better to pass such work to user space
1249 +helper, instead of doing in kernel space. Actually I am still thinking
1250 +about it. But currently I have implemented it in kernel space.
1251 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1252 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1253 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2020-01-27 10:57:18.162204582 +0100
1254 @@ -0,0 +1,258 @@
1255 +
1256 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1257 +# 
1258 +# This program is free software; you can redistribute it and/or modify
1259 +# it under the terms of the GNU General Public License as published by
1260 +# the Free Software Foundation; either version 2 of the License, or
1261 +# (at your option) any later version.
1262 +# 
1263 +# This program is distributed in the hope that it will be useful,
1264 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1265 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1266 +# GNU General Public License for more details.
1267 +# 
1268 +# You should have received a copy of the GNU General Public License
1269 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1270 +
1271 +Basic Aufs Internal Structure
1272 +
1273 +Superblock/Inode/Dentry/File Objects
1274 +----------------------------------------------------------------------
1275 +As like an ordinary filesystem, aufs has its own
1276 +superblock/inode/dentry/file objects. All these objects have a
1277 +dynamically allocated array and store the same kind of pointers to the
1278 +lower filesystem, branch.
1279 +For example, when you build a union with one readwrite branch and one
1280 +readonly, mounted /au, /rw and /ro respectively.
1281 +- /au = /rw + /ro
1282 +- /ro/fileA exists but /rw/fileA
1283 +
1284 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1285 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1286 +- [0] = NULL (because /rw/fileA doesn't exist)
1287 +- [1] = /ro/fileA
1288 +
1289 +This style of an array is essentially same to the aufs
1290 +superblock/inode/dentry/file objects.
1291 +
1292 +Because aufs supports manipulating branches, ie. add/delete/change
1293 +branches dynamically, these objects has its own generation. When
1294 +branches are changed, the generation in aufs superblock is
1295 +incremented. And a generation in other object are compared when it is
1296 +accessed. When a generation in other objects are obsoleted, aufs
1297 +refreshes the internal array.
1298 +
1299 +
1300 +Superblock
1301 +----------------------------------------------------------------------
1302 +Additionally aufs superblock has some data for policies to select one
1303 +among multiple writable branches, XIB files, pseudo-links and kobject.
1304 +See below in detail.
1305 +About the policies which supports copy-down a directory, see
1306 +wbr_policy.txt too.
1307 +
1308 +
1309 +Branch and XINO(External Inode Number Translation Table)
1310 +----------------------------------------------------------------------
1311 +Every branch has its own xino (external inode number translation table)
1312 +file. The xino file is created and unlinked by aufs internally. When two
1313 +members of a union exist on the same filesystem, they share the single
1314 +xino file.
1315 +The struct of a xino file is simple, just a sequence of aufs inode
1316 +numbers which is indexed by the lower inode number.
1317 +In the above sample, assume the inode number of /ro/fileA is i111 and
1318 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1319 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1320 +
1321 +When the inode numbers are not contiguous, the xino file will be sparse
1322 +which has a hole in it and doesn't consume as much disk space as it
1323 +might appear. If your branch filesystem consumes disk space for such
1324 +holes, then you should specify 'xino=' option at mounting aufs.
1325 +
1326 +Aufs has a mount option to free the disk blocks for such holes in XINO
1327 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1328 +meet a problem of disk shortage due to XINO files, then you should try
1329 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1330 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1331 +the holes in XINO files.
1332 +
1333 +Also a writable branch has three kinds of "whiteout bases". All these
1334 +are existed when the branch is joined to aufs, and their names are
1335 +whiteout-ed doubly, so that users will never see their names in aufs
1336 +hierarchy.
1337 +1. a regular file which will be hardlinked to all whiteouts.
1338 +2. a directory to store a pseudo-link.
1339 +3. a directory to store an "orphan"-ed file temporary.
1340 +
1341 +1. Whiteout Base
1342 +   When you remove a file on a readonly branch, aufs handles it as a
1343 +   logical deletion and creates a whiteout on the upper writable branch
1344 +   as a hardlink of this file in order not to consume inode on the
1345 +   writable branch.
1346 +2. Pseudo-link Dir
1347 +   See below, Pseudo-link.
1348 +3. Step-Parent Dir
1349 +   When "fileC" exists on the lower readonly branch only and it is
1350 +   opened and removed with its parent dir, and then user writes
1351 +   something into it, then aufs copies-up fileC to this
1352 +   directory. Because there is no other dir to store fileC. After
1353 +   creating a file under this dir, the file is unlinked.
1354 +
1355 +Because aufs supports manipulating branches, ie. add/delete/change
1356 +dynamically, a branch has its own id. When the branch order changes,
1357 +aufs finds the new index by searching the branch id.
1358 +
1359 +
1360 +Pseudo-link
1361 +----------------------------------------------------------------------
1362 +Assume "fileA" exists on the lower readonly branch only and it is
1363 +hardlinked to "fileB" on the branch. When you write something to fileA,
1364 +aufs copies-up it to the upper writable branch. Additionally aufs
1365 +creates a hardlink under the Pseudo-link Directory of the writable
1366 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1367 +simple list. If fileB is read after unlinking fileA, aufs returns
1368 +filedata from the pseudo-link instead of the lower readonly
1369 +branch. Because the pseudo-link is based upon the inode, to keep the
1370 +inode number by xino (see above) is essentially necessary.
1371 +
1372 +All the hardlinks under the Pseudo-link Directory of the writable branch
1373 +should be restored in a proper location later. Aufs provides a utility
1374 +to do this. The userspace helpers executed at remounting and unmounting
1375 +aufs by default.
1376 +During this utility is running, it puts aufs into the pseudo-link
1377 +maintenance mode. In this mode, only the process which began the
1378 +maintenance mode (and its child processes) is allowed to operate in
1379 +aufs. Some other processes which are not related to the pseudo-link will
1380 +be allowed to run too, but the rest have to return an error or wait
1381 +until the maintenance mode ends. If a process already acquires an inode
1382 +mutex (in VFS), it has to return an error.
1383 +
1384 +
1385 +XIB(external inode number bitmap)
1386 +----------------------------------------------------------------------
1387 +Addition to the xino file per a branch, aufs has an external inode number
1388 +bitmap in a superblock object. It is also an internal file such like a
1389 +xino file.
1390 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1391 +not.
1392 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1393 +
1394 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1395 +reduce the number of consumed disk blocks for these files.
1396 +
1397 +
1398 +Virtual or Vertical Dir, and Readdir in Userspace
1399 +----------------------------------------------------------------------
1400 +In order to support multiple layers (branches), aufs readdir operation
1401 +constructs a virtual dir block on memory. For readdir, aufs calls
1402 +vfs_readdir() internally for each dir on branches, merges their entries
1403 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1404 +object. So the file object has its entry list until it is closed. The
1405 +entry list will be updated when the file position is zero and becomes
1406 +obsoleted. This decision is made in aufs automatically.
1407 +
1408 +The dynamically allocated memory block for the name of entries has a
1409 +unit of 512 bytes (by default) and stores the names contiguously (no
1410 +padding). Another block for each entry is handled by kmem_cache too.
1411 +During building dir blocks, aufs creates hash list and judging whether
1412 +the entry is whiteouted by its upper branch or already listed.
1413 +The merged result is cached in the corresponding inode object and
1414 +maintained by a customizable life-time option.
1415 +
1416 +Some people may call it can be a security hole or invite DoS attack
1417 +since the opened and once readdir-ed dir (file object) holds its entry
1418 +list and becomes a pressure for system memory. But I'd say it is similar
1419 +to files under /proc or /sys. The virtual files in them also holds a
1420 +memory page (generally) while they are opened. When an idea to reduce
1421 +memory for them is introduced, it will be applied to aufs too.
1422 +For those who really hate this situation, I've developed readdir(3)
1423 +library which operates this merging in userspace. You just need to set
1424 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1425 +kernel space for readdir(3).
1426 +
1427 +
1428 +Workqueue
1429 +----------------------------------------------------------------------
1430 +Aufs sometimes requires privilege access to a branch. For instance,
1431 +in copy-up/down operation. When a user process is going to make changes
1432 +to a file which exists in the lower readonly branch only, and the mode
1433 +of one of ancestor directories may not be writable by a user
1434 +process. Here aufs copy-up the file with its ancestors and they may
1435 +require privilege to set its owner/group/mode/etc.
1436 +This is a typical case of a application character of aufs (see
1437 +Introduction).
1438 +
1439 +Aufs uses workqueue synchronously for this case. It creates its own
1440 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1441 +passes the request to call mkdir or write (for example), and wait for
1442 +its completion. This approach solves a problem of a signal handler
1443 +simply.
1444 +If aufs didn't adopt the workqueue and changed the privilege of the
1445 +process, then the process may receive the unexpected SIGXFSZ or other
1446 +signals.
1447 +
1448 +Also aufs uses the system global workqueue ("events" kernel thread) too
1449 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1450 +whiteout base and etc. This is unrelated to a privilege.
1451 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1452 +superblock at the beginning, at the same time waits for the completion
1453 +of all queued asynchronous tasks.
1454 +
1455 +
1456 +Whiteout
1457 +----------------------------------------------------------------------
1458 +The whiteout in aufs is very similar to Unionfs's. That is represented
1459 +by its filename. UnionMount takes an approach of a file mode, but I am
1460 +afraid several utilities (find(1) or something) will have to support it.
1461 +
1462 +Basically the whiteout represents "logical deletion" which stops aufs to
1463 +lookup further, but also it represents "dir is opaque" which also stop
1464 +further lookup.
1465 +
1466 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1467 +In order to make several functions in a single systemcall to be
1468 +revertible, aufs adopts an approach to rename a directory to a temporary
1469 +unique whiteouted name.
1470 +For example, in rename(2) dir where the target dir already existed, aufs
1471 +renames the target dir to a temporary unique whiteouted name before the
1472 +actual rename on a branch, and then handles other actions (make it opaque,
1473 +update the attributes, etc). If an error happens in these actions, aufs
1474 +simply renames the whiteouted name back and returns an error. If all are
1475 +succeeded, aufs registers a function to remove the whiteouted unique
1476 +temporary name completely and asynchronously to the system global
1477 +workqueue.
1478 +
1479 +
1480 +Copy-up
1481 +----------------------------------------------------------------------
1482 +It is a well-known feature or concept.
1483 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1484 +internally and makes change to the new file on the upper writable branch.
1485 +When the trigger systemcall does not update the timestamps of the parent
1486 +dir, aufs reverts it after copy-up.
1487 +
1488 +
1489 +Move-down (aufs3.9 and later)
1490 +----------------------------------------------------------------------
1491 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1492 +the lower readonly branch to the upper writable branch when a user
1493 +changes something about the file.
1494 +"Move-down" is an opposite action of copy-up. Basically this action is
1495 +ran manually instead of automatically and internally.
1496 +For desgin and implementation, aufs has to consider these issues.
1497 +- whiteout for the file may exist on the lower branch.
1498 +- ancestor directories may not exist on the lower branch.
1499 +- diropq for the ancestor directories may exist on the upper branch.
1500 +- free space on the lower branch will reduce.
1501 +- another access to the file may happen during moving-down, including
1502 +  UDBA (see "Revalidate Dentry and UDBA").
1503 +- the file should not be hard-linked nor pseudo-linked. they should be
1504 +  handled by auplink utility later.
1505 +
1506 +Sometimes users want to move-down a file from the upper writable branch
1507 +to the lower readonly or writable branch. For instance,
1508 +- the free space of the upper writable branch is going to run out.
1509 +- create a new intermediate branch between the upper and lower branch.
1510 +- etc.
1511 +
1512 +For this purpose, use "aumvdown" command in aufs-util.git.
1513 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1514 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1515 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2020-01-27 10:57:18.162204582 +0100
1516 @@ -0,0 +1,85 @@
1517 +
1518 +# Copyright (C) 2015-2020 Junjiro R. Okajima
1519 +# 
1520 +# This program is free software; you can redistribute it and/or modify
1521 +# it under the terms of the GNU General Public License as published by
1522 +# the Free Software Foundation; either version 2 of the License, or
1523 +# (at your option) any later version.
1524 +# 
1525 +# This program is distributed in the hope that it will be useful,
1526 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1527 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1528 +# GNU General Public License for more details.
1529 +# 
1530 +# You should have received a copy of the GNU General Public License
1531 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1532 +
1533 +Support for a branch who has its ->atomic_open()
1534 +----------------------------------------------------------------------
1535 +The filesystems who implement its ->atomic_open() are not majority. For
1536 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1537 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1538 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1539 +sure whether all filesystems who have ->atomic_open() behave like this,
1540 +but NFSv4 surely returns the error.
1541 +
1542 +In order to support ->atomic_open() for aufs, there are a few
1543 +approaches.
1544 +
1545 +A. Introduce aufs_atomic_open()
1546 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1547 +     branch fs.
1548 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1549 +   an aufs user Pip Cet's approach
1550 +   - calls aufs_create(), VFS finish_open() and notify_change().
1551 +   - pass fake-mode to finish_open(), and then correct the mode by
1552 +     notify_change().
1553 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1554 +   - no aufs_atomic_open().
1555 +   - aufs_lookup() registers the TID to an aufs internal object.
1556 +   - aufs_create() does nothing when the matching TID is registered, but
1557 +     registers the mode.
1558 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1559 +     TID is registered.
1560 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1561 +   credential
1562 +   - no aufs_atomic_open().
1563 +   - aufs_create() registers the TID to an internal object. this info
1564 +     represents "this process created this file just now."
1565 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1566 +     registered TID and re-try open() with superuser's credential.
1567 +
1568 +Pros and cons for each approach.
1569 +
1570 +A.
1571 +   - straightforward but highly depends upon VFS internal.
1572 +   - the atomic behavaiour is kept.
1573 +   - some of parameters such as nameidata are hard to reproduce for
1574 +     branch fs.
1575 +   - large overhead.
1576 +B.
1577 +   - easy to implement.
1578 +   - the atomic behavaiour is lost.
1579 +C.
1580 +   - the atomic behavaiour is kept.
1581 +   - dirty and tricky.
1582 +   - VFS checks whether the file is created correctly after calling
1583 +     ->create(), which means this approach doesn't work.
1584 +D.
1585 +   - easy to implement.
1586 +   - the atomic behavaiour is lost.
1587 +   - to open a file with superuser's credential and give it to a user
1588 +     process is a bad idea, since the file object keeps the credential
1589 +     in it. It may affect LSM or something. This approach doesn't work
1590 +     either.
1591 +
1592 +The approach A is ideal, but it hard to implement. So here is a
1593 +variation of A, which is to be implemented.
1594 +
1595 +A-1. Introduce aufs_atomic_open()
1596 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1597 +       vfs_create() and finish_open().
1598 +     - the demerit is that the several checks after branch fs
1599 +       ->atomic_open() are lost. in the ordinary case, the checks are
1600 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1601 +       be implemented in aufs, but not all I am afraid.
1602 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1603 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1604 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2020-01-27 10:57:18.165538015 +0100
1605 @@ -0,0 +1,113 @@
1606 +
1607 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1608 +# 
1609 +# This program is free software; you can redistribute it and/or modify
1610 +# it under the terms of the GNU General Public License as published by
1611 +# the Free Software Foundation; either version 2 of the License, or
1612 +# (at your option) any later version.
1613 +# 
1614 +# This program is distributed in the hope that it will be useful,
1615 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1616 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1617 +# GNU General Public License for more details.
1618 +# 
1619 +# You should have received a copy of the GNU General Public License
1620 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1621 +
1622 +Lookup in a Branch
1623 +----------------------------------------------------------------------
1624 +Since aufs has a character of sub-VFS (see Introduction), it operates
1625 +lookup for branches as VFS does. It may be a heavy work. But almost all
1626 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1627 +directly connected to its parent. Digging down the directory hierarchy
1628 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1629 +aufs calls it.
1630 +
1631 +When a branch is a remote filesystem, aufs basically relies upon its
1632 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1633 +them.
1634 +For d_revalidate, aufs implements three levels of revalidate tests. See
1635 +"Revalidate Dentry and UDBA" in detail.
1636 +
1637 +
1638 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1639 +----------------------------------------------------------------------
1640 +Let's try case study.
1641 +- aufs has two branches, upper readwrite and lower readonly.
1642 +  /au = /rw + /ro
1643 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1644 +- user invoked "chmod a+rx /au/dirA"
1645 +- the internal copy-up is activated and "/rw/dirA" is created and its
1646 +  permission bits are set to world readable.
1647 +- then "/au/dirA" becomes world readable?
1648 +
1649 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1650 +or it may be a natively readonly filesystem. If aufs respects the lower
1651 +branch, it should not respond readdir request from other users. But user
1652 +allowed it by chmod. Should really aufs rejects showing the entries
1653 +under /ro/dirA?
1654 +
1655 +To be honest, I don't have a good solution for this case. So aufs
1656 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1657 +users.
1658 +When dirperm1 is specified, aufs checks only the highest one for the
1659 +directory permission, and shows the entries. Otherwise, as usual, checks
1660 +every dir existing on all branches and rejects the request.
1661 +
1662 +As a side effect, dirperm1 option improves the performance of aufs
1663 +because the number of permission check is reduced when the number of
1664 +branch is many.
1665 +
1666 +
1667 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1668 +----------------------------------------------------------------------
1669 +Generally VFS helpers re-validate a dentry as a part of lookup.
1670 +0. digging down the directory hierarchy.
1671 +1. lock the parent dir by its i_mutex.
1672 +2. lookup the final (child) entry.
1673 +3. revalidate it.
1674 +4. call the actual operation (create, unlink, etc.)
1675 +5. unlock the parent dir
1676 +
1677 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1678 +called. Actually aufs implements it and checks the dentry on a branch is
1679 +still valid.
1680 +But it is not enough. Because aufs has to release the lock for the
1681 +parent dir on a branch at the end of ->lookup() (step 2) and
1682 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1683 +held by VFS.
1684 +If the file on a branch is changed directly, eg. bypassing aufs, after
1685 +aufs released the lock, then the subsequent operation may cause
1686 +something unpleasant result.
1687 +
1688 +This situation is a result of VFS architecture, ->lookup() and
1689 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1690 +design from VFS's point of view. It is just not suitable for sub-VFS
1691 +character in aufs.
1692 +
1693 +Aufs supports such case by three level of revalidation which is
1694 +selectable by user.
1695 +1. Simple Revalidate
1696 +   Addition to the native flow in VFS's, confirm the child-parent
1697 +   relationship on the branch just after locking the parent dir on the
1698 +   branch in the "actual operation" (step 4). When this validation
1699 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1700 +   checks the validation of the dentry on branches.
1701 +2. Monitor Changes Internally by Inotify/Fsnotify
1702 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1703 +   the dentry on the branch, and returns EBUSY if it finds different
1704 +   dentry.
1705 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1706 +   during it is in cache. When the event is notified, aufs registers a
1707 +   function to kernel 'events' thread by schedule_work(). And the
1708 +   function sets some special status to the cached aufs dentry and inode
1709 +   private data. If they are not cached, then aufs has nothing to
1710 +   do. When the same file is accessed through aufs (step 0-3) later,
1711 +   aufs will detect the status and refresh all necessary data.
1712 +   In this mode, aufs has to ignore the event which is fired by aufs
1713 +   itself.
1714 +3. No Extra Validation
1715 +   This is the simplest test and doesn't add any additional revalidation
1716 +   test, and skip the revalidation in step 4. It is useful and improves
1717 +   aufs performance when system surely hide the aufs branches from user,
1718 +   by over-mounting something (or another method).
1719 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1720 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1721 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2020-01-27 10:57:18.165538015 +0100
1722 @@ -0,0 +1,74 @@
1723 +
1724 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1725 +# 
1726 +# This program is free software; you can redistribute it and/or modify
1727 +# it under the terms of the GNU General Public License as published by
1728 +# the Free Software Foundation; either version 2 of the License, or
1729 +# (at your option) any later version.
1730 +# 
1731 +# This program is distributed in the hope that it will be useful,
1732 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1733 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1734 +# GNU General Public License for more details.
1735 +# 
1736 +# You should have received a copy of the GNU General Public License
1737 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1738 +
1739 +Branch Manipulation
1740 +
1741 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1742 +and changing its permission/attribute, there are a lot of works to do.
1743 +
1744 +
1745 +Add a Branch
1746 +----------------------------------------------------------------------
1747 +o Confirm the adding dir exists outside of aufs, including loopback
1748 +  mount, and its various attributes.
1749 +o Initialize the xino file and whiteout bases if necessary.
1750 +  See struct.txt.
1751 +
1752 +o Check the owner/group/mode of the directory
1753 +  When the owner/group/mode of the adding directory differs from the
1754 +  existing branch, aufs issues a warning because it may impose a
1755 +  security risk.
1756 +  For example, when a upper writable branch has a world writable empty
1757 +  top directory, a malicious user can create any files on the writable
1758 +  branch directly, like copy-up and modify manually. If something like
1759 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1760 +  writable branch, and the writable branch is world-writable, then a
1761 +  malicious guy may create /etc/passwd on the writable branch directly
1762 +  and the infected file will be valid in aufs.
1763 +  I am afraid it can be a security issue, but aufs can do nothing except
1764 +  producing a warning.
1765 +
1766 +
1767 +Delete a Branch
1768 +----------------------------------------------------------------------
1769 +o Confirm the deleting branch is not busy
1770 +  To be general, there is one merit to adopt "remount" interface to
1771 +  manipulate branches. It is to discard caches. At deleting a branch,
1772 +  aufs checks the still cached (and connected) dentries and inodes. If
1773 +  there are any, then they are all in-use. An inode without its
1774 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1775 +
1776 +  For the cached one, aufs checks whether the same named entry exists on
1777 +  other branches.
1778 +  If the cached one is a directory, because aufs provides a merged view
1779 +  to users, as long as one dir is left on any branch aufs can show the
1780 +  dir to users. In this case, the branch can be removed from aufs.
1781 +  Otherwise aufs rejects deleting the branch.
1782 +
1783 +  If any file on the deleting branch is opened by aufs, then aufs
1784 +  rejects deleting.
1785 +
1786 +
1787 +Modify the Permission of a Branch
1788 +----------------------------------------------------------------------
1789 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1790 +  See struct.txt.
1791 +
1792 +o rw --> ro: Confirm the modifying branch is not busy
1793 +  Aufs rejects the request if any of these conditions are true.
1794 +  - a file on the branch is mmap-ed.
1795 +  - a regular file on the branch is opened for write and there is no
1796 +    same named entry on the upper branch.
1797 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1798 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1799 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2020-01-27 10:57:18.165538015 +0100
1800 @@ -0,0 +1,64 @@
1801 +
1802 +# Copyright (C) 2005-2020 Junjiro R. Okajima
1803 +# 
1804 +# This program is free software; you can redistribute it and/or modify
1805 +# it under the terms of the GNU General Public License as published by
1806 +# the Free Software Foundation; either version 2 of the License, or
1807 +# (at your option) any later version.
1808 +# 
1809 +# This program is distributed in the hope that it will be useful,
1810 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1811 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1812 +# GNU General Public License for more details.
1813 +# 
1814 +# You should have received a copy of the GNU General Public License
1815 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1816 +
1817 +Policies to Select One among Multiple Writable Branches
1818 +----------------------------------------------------------------------
1819 +When the number of writable branch is more than one, aufs has to decide
1820 +the target branch for file creation or copy-up. By default, the highest
1821 +writable branch which has the parent (or ancestor) dir of the target
1822 +file is chosen (top-down-parent policy).
1823 +By user's request, aufs implements some other policies to select the
1824 +writable branch, for file creation several policies, round-robin,
1825 +most-free-space, and other policies. For copy-up, top-down-parent,
1826 +bottom-up-parent, bottom-up and others.
1827 +
1828 +As expected, the round-robin policy selects the branch in circular. When
1829 +you have two writable branches and creates 10 new files, 5 files will be
1830 +created for each branch. mkdir(2) systemcall is an exception. When you
1831 +create 10 new directories, all will be created on the same branch.
1832 +And the most-free-space policy selects the one which has most free
1833 +space among the writable branches. The amount of free space will be
1834 +checked by aufs internally, and users can specify its time interval.
1835 +
1836 +The policies for copy-up is more simple,
1837 +top-down-parent is equivalent to the same named on in create policy,
1838 +bottom-up-parent selects the writable branch where the parent dir
1839 +exists and the nearest upper one from the copyup-source,
1840 +bottom-up selects the nearest upper writable branch from the
1841 +copyup-source, regardless the existence of the parent dir.
1842 +
1843 +There are some rules or exceptions to apply these policies.
1844 +- If there is a readonly branch above the policy-selected branch and
1845 +  the parent dir is marked as opaque (a variation of whiteout), or the
1846 +  target (creating) file is whiteout-ed on the upper readonly branch,
1847 +  then the result of the policy is ignored and the target file will be
1848 +  created on the nearest upper writable branch than the readonly branch.
1849 +- If there is a writable branch above the policy-selected branch and
1850 +  the parent dir is marked as opaque or the target file is whiteouted
1851 +  on the branch, then the result of the policy is ignored and the target
1852 +  file will be created on the highest one among the upper writable
1853 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1854 +  it as usual.
1855 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1856 +  They try selecting the branch where the source exists as possible
1857 +  since copyup a large file will take long time. If it can't be,
1858 +  ie. the branch where the source exists is readonly, then they will
1859 +  follow the copyup policy.
1860 +- There is an exception for rename(2) when the target exists.
1861 +  If the rename target exists, aufs compares the index of the branches
1862 +  where the source and the target exists and selects the higher
1863 +  one. If the selected branch is readonly, then aufs follows the
1864 +  copyup policy.
1865 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1866 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1867 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2019-07-11 15:42:14.458904362 +0200
1868 @@ -0,0 +1,31 @@
1869 +
1870 +// to view this graph, run dot(1) command in GRAPHVIZ.
1871 +
1872 +digraph G {
1873 +node [shape=box];
1874 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1875 +
1876 +node [shape=oval];
1877 +
1878 +aufs_rename -> whinfo [label="store/remove"];
1879 +
1880 +node [shape=oval];
1881 +inode_list [label="h_inum list in branch\ncache"];
1882 +
1883 +node [shape=box];
1884 +whinode [label="h_inum list file"];
1885 +
1886 +node [shape=oval];
1887 +brmgmt [label="br_add/del/mod/umount"];
1888 +
1889 +brmgmt -> inode_list [label="create/remove"];
1890 +brmgmt -> whinode [label="load/store"];
1891 +
1892 +inode_list -> whinode [style=dashed,dir=both];
1893 +
1894 +aufs_rename -> inode_list [label="add/del"];
1895 +
1896 +aufs_lookup -> inode_list [label="search"];
1897 +
1898 +aufs_lookup -> whinfo [label="load/remove"];
1899 +}
1900 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1901 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1902 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2020-01-27 10:57:18.165538015 +0100
1903 @@ -0,0 +1,102 @@
1904 +
1905 +# Copyright (C) 2017-2020 Junjiro R. Okajima
1906 +#
1907 +# This program is free software; you can redistribute it and/or modify
1908 +# it under the terms of the GNU General Public License as published by
1909 +# the Free Software Foundation; either version 2 of the License, or
1910 +# (at your option) any later version.
1911 +#
1912 +# This program is distributed in the hope that it will be useful,
1913 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1914 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1915 +# GNU General Public License for more details.
1916 +#
1917 +# You should have received a copy of the GNU General Public License
1918 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1919 +
1920 +Special handling for renaming a directory (DIRREN)
1921 +----------------------------------------------------------------------
1922 +First, let's assume we have a simple usecase.
1923 +
1924 +- /u = /rw + /ro
1925 +- /rw/dirA exists
1926 +- /ro/dirA and /ro/dirA/file exist too
1927 +- there is no dirB on both branches
1928 +- a user issues rename("dirA", "dirB")
1929 +
1930 +Now, what should aufs behave against this rename(2)?
1931 +There are a few possible cases.
1932 +
1933 +A. returns EROFS.
1934 +   since dirA exists on a readonly branch which cannot be renamed.
1935 +B. returns EXDEV.
1936 +   it is possible to copy-up dirA (only the dir itself), but the child
1937 +   entries ("file" in this case) should not be. it must be a bad
1938 +   approach to copy-up recursively.
1939 +C. returns a success.
1940 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1941 +   is a violation of aufs' policy.
1942 +D. construct an extra information which indicates that /ro/dirA should
1943 +   be handled as the name of dirB.
1944 +   overlayfs has a similar feature called REDIRECT.
1945 +
1946 +Until now, aufs implements the case B only which returns EXDEV, and
1947 +expects the userspace application behaves like mv(1) which tries
1948 +issueing rename(2) recursively.
1949 +
1950 +A new aufs feature called DIRREN is introduced which implements the case
1951 +D. There are several "extra information" added.
1952 +
1953 +1. detailed info per renamed directory
1954 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1955 +2. the inode-number list of directories on a branch
1956 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1957 +
1958 +The filename of "detailed info per directory" represents the lower
1959 +branch, and its format is
1960 +- a type of the branch id
1961 +  one of these.
1962 +  + uuid (not implemented yet)
1963 +  + fsid
1964 +  + dev
1965 +- the inode-number of the branch root dir
1966 +
1967 +And it contains these info in a single regular file.
1968 +- magic number
1969 +- branch's inode-number of the logically renamed dir
1970 +- the name of the before-renamed dir
1971 +
1972 +The "detailed info per directory" file is created in aufs rename(2), and
1973 +loaded in any lookup.
1974 +The info is considered in lookup for the matching case only. Here
1975 +"matching" means that the root of branch (in the info filename) is same
1976 +to the current looking-up branch. After looking-up the before-renamed
1977 +name, the inode-number is compared. And the matched dentry is used.
1978 +
1979 +The "inode-number list of directories" is a regular file which contains
1980 +simply the inode-numbers on the branch. The file is created or updated
1981 +in removing the branch, and loaded in adding the branch. Its lifetime is
1982 +equal to the branch.
1983 +The list is refered in lookup, and when the current target inode is
1984 +found in the list, the aufs tries loading the "detailed info per
1985 +directory" and get the changed and valid name of the dir.
1986 +
1987 +Theoretically these "extra informaiton" may be able to be put into XATTR
1988 +in the dir inode. But aufs doesn't choose this way because
1989 +1. XATTR may not be supported by the branch (or its configuration)
1990 +2. XATTR may have its size limit.
1991 +3. XATTR may be less easy to convert than a regular file, when the
1992 +   format of the info is changed in the future.
1993 +At the same time, I agree that the regular file approach is much slower
1994 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1995 +better approach.
1996 +
1997 +This DIRREN feature is enabled by aufs configuration, and is activated
1998 +by a new mount option.
1999 +
2000 +For the more complicated case, there is a work with UDBA option, which
2001 +is to dected the direct access to the branches (by-passing aufs) and to
2002 +maintain the cashes in aufs. Since a single cached aufs dentry may
2003 +contains two names, before- and after-rename, the name comparision in
2004 +UDBA handler may not work correctly. In this case, the behaviour will be
2005 +equivalen to udba=reval case.
2006 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
2007 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
2008 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2020-01-27 10:57:18.165538015 +0100
2009 @@ -0,0 +1,120 @@
2010 +
2011 +# Copyright (C) 2011-2020 Junjiro R. Okajima
2012 +# 
2013 +# This program is free software; you can redistribute it and/or modify
2014 +# it under the terms of the GNU General Public License as published by
2015 +# the Free Software Foundation; either version 2 of the License, or
2016 +# (at your option) any later version.
2017 +# 
2018 +# This program is distributed in the hope that it will be useful,
2019 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2020 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2021 +# GNU General Public License for more details.
2022 +# 
2023 +# You should have received a copy of the GNU General Public License
2024 +# along with this program; if not, write to the Free Software
2025 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2026 +
2027 +
2028 +File-based Hierarchical Storage Management (FHSM)
2029 +----------------------------------------------------------------------
2030 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2031 +storage world. Aufs provides this feature as file-based with multiple
2032 +writable branches, based upon the principle of "Colder, the Lower".
2033 +Here the word "colder" means that the less used files, and "lower" means
2034 +that the position in the order of the stacked branches vertically.
2035 +These multiple writable branches are prioritized, ie. the topmost one
2036 +should be the fastest drive and be used heavily.
2037 +
2038 +o Characters in aufs FHSM story
2039 +- aufs itself and a new branch attribute.
2040 +- a new ioctl interface to move-down and to establish a connection with
2041 +  the daemon ("move-down" is a converse of "copy-up").
2042 +- userspace tool and daemon.
2043 +
2044 +The userspace daemon establishes a connection with aufs and waits for
2045 +the notification. The notified information is very similar to struct
2046 +statfs containing the number of consumed blocks and inodes.
2047 +When the consumed blocks/inodes of a branch exceeds the user-specified
2048 +upper watermark, the daemon activates its move-down process until the
2049 +consumed blocks/inodes reaches the user-specified lower watermark.
2050 +
2051 +The actual move-down is done by aufs based upon the request from
2052 +user-space since we need to maintain the inode number and the internal
2053 +pointer arrays in aufs.
2054 +
2055 +Currently aufs FHSM handles the regular files only. Additionally they
2056 +must not be hard-linked nor pseudo-linked.
2057 +
2058 +
2059 +o Cowork of aufs and the user-space daemon
2060 +  During the userspace daemon established the connection, aufs sends a
2061 +  small notification to it whenever aufs writes something into the
2062 +  writable branch. But it may cost high since aufs issues statfs(2)
2063 +  internally. So user can specify a new option to cache the
2064 +  info. Actually the notification is controlled by these factors.
2065 +  + the specified cache time.
2066 +  + classified as "force" by aufs internally.
2067 +  Until the specified time expires, aufs doesn't send the info
2068 +  except the forced cases. When aufs decide forcing, the info is always
2069 +  notified to userspace.
2070 +  For example, the number of free inodes is generally large enough and
2071 +  the shortage of it happens rarely. So aufs doesn't force the
2072 +  notification when creating a new file, directory and others. This is
2073 +  the typical case which aufs doesn't force.
2074 +  When aufs writes the actual filedata and the files consumes any of new
2075 +  blocks, the aufs forces notifying.
2076 +
2077 +
2078 +o Interfaces in aufs
2079 +- New branch attribute.
2080 +  + fhsm
2081 +    Specifies that the branch is managed by FHSM feature. In other word,
2082 +    participant in the FHSM.
2083 +    When nofhsm is set to the branch, it will not be the source/target
2084 +    branch of the move-down operation. This attribute is set
2085 +    independently from coo and moo attributes, and if you want full
2086 +    FHSM, you should specify them as well.
2087 +- New mount option.
2088 +  + fhsm_sec
2089 +    Specifies a second to suppress many less important info to be
2090 +    notified.
2091 +- New ioctl.
2092 +  + AUFS_CTL_FHSM_FD
2093 +    create a new file descriptor which userspace can read the notification
2094 +    (a subset of struct statfs) from aufs.
2095 +- Module parameter 'brs'
2096 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2097 +  be set.
2098 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2099 +  When there are two or more branches with fhsm attributes,
2100 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2101 +  terminates it. As a result of remounting and branch-manipulation, the
2102 +  number of branches with fhsm attribute can be one. In this case,
2103 +  /sbin/mount.aufs will terminate the user-space daemon.
2104 +
2105 +
2106 +Finally the operation is done as these steps in kernel-space.
2107 +- make sure that,
2108 +  + no one else is using the file.
2109 +  + the file is not hard-linked.
2110 +  + the file is not pseudo-linked.
2111 +  + the file is a regular file.
2112 +  + the parent dir is not opaqued.
2113 +- find the target writable branch.
2114 +- make sure the file is not whiteout-ed by the upper (than the target)
2115 +  branch.
2116 +- make the parent dir on the target branch.
2117 +- mutex lock the inode on the branch.
2118 +- unlink the whiteout on the target branch (if exists).
2119 +- lookup and create the whiteout-ed temporary name on the target branch.
2120 +- copy the file as the whiteout-ed temporary name on the target branch.
2121 +- rename the whiteout-ed temporary name to the original name.
2122 +- unlink the file on the source branch.
2123 +- maintain the internal pointer array and the external inode number
2124 +  table (XINO).
2125 +- maintain the timestamps and other attributes of the parent dir and the
2126 +  file.
2127 +
2128 +And of course, in every step, an error may happen. So the operation
2129 +should restore the original file state after an error happens.
2130 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2131 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2132 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2020-01-27 10:57:18.165538015 +0100
2133 @@ -0,0 +1,72 @@
2134 +
2135 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2136 +# 
2137 +# This program is free software; you can redistribute it and/or modify
2138 +# it under the terms of the GNU General Public License as published by
2139 +# the Free Software Foundation; either version 2 of the License, or
2140 +# (at your option) any later version.
2141 +# 
2142 +# This program is distributed in the hope that it will be useful,
2143 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2144 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2145 +# GNU General Public License for more details.
2146 +# 
2147 +# You should have received a copy of the GNU General Public License
2148 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2149 +
2150 +mmap(2) -- File Memory Mapping
2151 +----------------------------------------------------------------------
2152 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2153 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2154 +->mmap().
2155 +This approach is simple and good, but there is one problem.
2156 +Under /proc, several entries show the mmapped files by its path (with
2157 +device and inode number), and the printed path will be the path on the
2158 +branch fs's instead of virtual aufs's.
2159 +This is not a problem in most cases, but some utilities lsof(1) (and its
2160 +user) may expect the path on aufs.
2161 +
2162 +To address this issue, aufs adds a new member called vm_prfile in struct
2163 +vm_area_struct (and struct vm_region). The original vm_file points to
2164 +the file on the branch fs in order to handle everything correctly as
2165 +usual. The new vm_prfile points to a virtual file in aufs, and the
2166 +show-functions in procfs refers to vm_prfile if it is set.
2167 +Also we need to maintain several other places where touching vm_file
2168 +such like
2169 +- fork()/clone() copies vma and the reference count of vm_file is
2170 +  incremented.
2171 +- merging vma maintains the ref count too.
2172 +
2173 +This is not a good approach. It just fakes the printed path. But it
2174 +leaves all behaviour around f_mapping unchanged. This is surely an
2175 +advantage.
2176 +Actually aufs had adopted another complicated approach which calls
2177 +generic_file_mmap() and handles struct vm_operations_struct. In this
2178 +approach, aufs met a hard problem and I could not solve it without
2179 +switching the approach.
2180 +
2181 +There may be one more another approach which is
2182 +- bind-mount the branch-root onto the aufs-root internally
2183 +- grab the new vfsmount (ie. struct mount)
2184 +- lazy-umount the branch-root internally
2185 +- in open(2) the aufs-file, open the branch-file with the hidden
2186 +  vfsmount (instead of the original branch's vfsmount)
2187 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2188 +  but it may be possible from userspace by the mount helper.
2189 +
2190 +Adding the internal hidden vfsmount and using it in opening a file, the
2191 +file path under /proc will be printed correctly. This approach looks
2192 +smarter, but is not possible I am afraid.
2193 +- aufs-root may be bind-mount later. when it happens, another hidden
2194 +  vfsmount will be required.
2195 +- it is hard to get the chance to bind-mount and lazy-umount
2196 +  + in kernel-space, FS can have vfsmount in open(2) via
2197 +    file->f_path, and aufs can know its vfsmount. But several locks are
2198 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2199 +    here, then it may cause a deadlock.
2200 +  + in user-space, bind-mount doesn't invoke the mount helper.
2201 +- since /proc shows dev and ino, aufs has to give vma these info. it
2202 +  means a new member vm_prinode will be necessary. this is essentially
2203 +  equivalent to vm_prfile described above.
2204 +
2205 +I have to give up this "looks-smater" approach.
2206 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2207 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2208 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2020-01-27 10:57:18.165538015 +0100
2209 @@ -0,0 +1,96 @@
2210 +
2211 +# Copyright (C) 2014-2020 Junjiro R. Okajima
2212 +#
2213 +# This program is free software; you can redistribute it and/or modify
2214 +# it under the terms of the GNU General Public License as published by
2215 +# the Free Software Foundation; either version 2 of the License, or
2216 +# (at your option) any later version.
2217 +#
2218 +# This program is distributed in the hope that it will be useful,
2219 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2220 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2221 +# GNU General Public License for more details.
2222 +#
2223 +# You should have received a copy of the GNU General Public License
2224 +# along with this program; if not, write to the Free Software
2225 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2226 +
2227 +
2228 +Listing XATTR/EA and getting the value
2229 +----------------------------------------------------------------------
2230 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2231 +shows the values from the topmost existing file. This behaviour is good
2232 +for the non-dir entries since the bahaviour exactly matches the shown
2233 +information. But for the directories, aufs considers all the same named
2234 +entries on the lower branches. Which means, if one of the lower entry
2235 +rejects readdir call, then aufs returns an error even if the topmost
2236 +entry allows it. This behaviour is necessary to respect the branch fs's
2237 +security, but can make users confused since the user-visible standard
2238 +attributes don't match the behaviour.
2239 +To address this issue, aufs has a mount option called dirperm1 which
2240 +checks the permission for the topmost entry only, and ignores the lower
2241 +entry's permission.
2242 +
2243 +A similar issue can happen around XATTR.
2244 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2245 +always set. Otherwise these very unpleasant situation would happen.
2246 +- listxattr(2) may return the duplicated entries.
2247 +- users may not be able to remove or reset the XATTR forever,
2248 +
2249 +
2250 +XATTR/EA support in the internal (copy,move)-(up,down)
2251 +----------------------------------------------------------------------
2252 +Generally the extended attributes of inode are categorized as these.
2253 +- "security" for LSM and capability.
2254 +- "system" for posix ACL, 'acl' mount option is required for the branch
2255 +  fs generally.
2256 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2257 +- "user" for userspace, 'user_xattr' mount option is required for the
2258 +  branch fs generally.
2259 +
2260 +Moreover there are some other categories. Aufs handles these rather
2261 +unpopular categories as the ordinary ones, ie. there is no special
2262 +condition nor exception.
2263 +
2264 +In copy-up, the support for XATTR on the dst branch may differ from the
2265 +src branch. In this case, the copy-up operation will get an error and
2266 +the original user operation which triggered the copy-up will fail. It
2267 +can happen that even all copy-up will fail.
2268 +When both of src and dst branches support XATTR and if an error occurs
2269 +during copying XATTR, then the copy-up should fail obviously. That is a
2270 +good reason and aufs should return an error to userspace. But when only
2271 +the src branch support that XATTR, aufs should not return an error.
2272 +For example, the src branch supports ACL but the dst branch doesn't
2273 +because the dst branch may natively un-support it or temporary
2274 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2275 +may NOT return an error even if the XATTR is not supported. It is
2276 +totally up to the branch fs.
2277 +
2278 +Anyway when the aufs internal copy-up gets an error from the dst branch
2279 +fs, then aufs tries removing the just copied entry and returns the error
2280 +to the userspace. The worst case of this situation will be all copy-up
2281 +will fail.
2282 +
2283 +For the copy-up operation, there two basic approaches.
2284 +- copy the specified XATTR only (by category above), and return the
2285 +  error unconditionally if it happens.
2286 +- copy all XATTR, and ignore the error on the specified category only.
2287 +
2288 +In order to support XATTR and to implement the correct behaviour, aufs
2289 +chooses the latter approach and introduces some new branch attributes,
2290 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2291 +They correspond to the XATTR namespaces (see above). Additionally, to be
2292 +convenient, "icex" is also provided which means all "icex*" attributes
2293 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2294 +
2295 +The meaning of these attributes is to ignore the error from setting
2296 +XATTR on that branch.
2297 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2298 +error from the dst branch according to the specified attributes.
2299 +
2300 +Some XATTR may have its default value. The default value may come from
2301 +the parent dir or the environment. If the default value is set at the
2302 +file creating-time, it will be overwritten by copy-up.
2303 +Some contradiction may happen I am afraid.
2304 +Do we need another attribute to stop copying XATTR? I am unsure. For
2305 +now, aufs implements the branch attributes to ignore the error.
2306 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2307 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2308 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2020-01-27 10:57:18.165538015 +0100
2309 @@ -0,0 +1,58 @@
2310 +
2311 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2312 +# 
2313 +# This program is free software; you can redistribute it and/or modify
2314 +# it under the terms of the GNU General Public License as published by
2315 +# the Free Software Foundation; either version 2 of the License, or
2316 +# (at your option) any later version.
2317 +# 
2318 +# This program is distributed in the hope that it will be useful,
2319 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2320 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2321 +# GNU General Public License for more details.
2322 +# 
2323 +# You should have received a copy of the GNU General Public License
2324 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2325 +
2326 +Export Aufs via NFS
2327 +----------------------------------------------------------------------
2328 +Here is an approach.
2329 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2330 +  generation.
2331 +- iget_locked(): initialize aufs inode generation for a new inode, and
2332 +  store it in xigen file.
2333 +- destroy_inode(): increment aufs inode generation and store it in xigen
2334 +  file. it is necessary even if it is not unlinked, because any data of
2335 +  inode may be changed by UDBA.
2336 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2337 +  build file handle by
2338 +  + branch id (4 bytes)
2339 +  + superblock generation (4 bytes)
2340 +  + inode number (4 or 8 bytes)
2341 +  + parent dir inode number (4 or 8 bytes)
2342 +  + inode generation (4 bytes))
2343 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2344 +    bytes)
2345 +  + file handle for a branch (by exportfs_encode_fh())
2346 +- fh_to_dentry():
2347 +  + find the index of a branch from its id in handle, and check it is
2348 +    still exist in aufs.
2349 +  + 1st level: get the inode number from handle and search it in cache.
2350 +  + 2nd level: if not found in cache, get the parent inode number from
2351 +    the handle and search it in cache. and then open the found parent
2352 +    dir, find the matching inode number by vfs_readdir() and get its
2353 +    name, and call lookup_one_len() for the target dentry.
2354 +  + 3rd level: if the parent dir is not cached, call
2355 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2356 +    build a pathname of it, convert it a pathname in aufs, call
2357 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2358 +    the 2nd level.
2359 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2360 +    for every branch, but not itself. to get this, (currently) aufs
2361 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2362 +    idea, but I didn't get other approach.
2363 +  + test the generation of the gotten inode.
2364 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2365 +  convert it into ESTALE for NFSD.
2366 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2367 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2368 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2369 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2370 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2020-01-27 10:57:18.165538015 +0100
2371 @@ -0,0 +1,52 @@
2372 +
2373 +# Copyright (C) 2005-2020 Junjiro R. Okajima
2374 +# 
2375 +# This program is free software; you can redistribute it and/or modify
2376 +# it under the terms of the GNU General Public License as published by
2377 +# the Free Software Foundation; either version 2 of the License, or
2378 +# (at your option) any later version.
2379 +# 
2380 +# This program is distributed in the hope that it will be useful,
2381 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2382 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2383 +# GNU General Public License for more details.
2384 +# 
2385 +# You should have received a copy of the GNU General Public License
2386 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2387 +
2388 +Show Whiteout Mode (shwh)
2389 +----------------------------------------------------------------------
2390 +Generally aufs hides the name of whiteouts. But in some cases, to show
2391 +them is very useful for users. For instance, creating a new middle layer
2392 +(branch) by merging existing layers.
2393 +
2394 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2395 +When you have three branches,
2396 +- Bottom: 'system', squashfs (underlying base system), read-only
2397 +- Middle: 'mods', squashfs, read-only
2398 +- Top: 'overlay', ram (tmpfs), read-write
2399 +
2400 +The top layer is loaded at boot time and saved at shutdown, to preserve
2401 +the changes made to the system during the session.
2402 +When larger changes have been made, or smaller changes have accumulated,
2403 +the size of the saved top layer data grows. At this point, it would be
2404 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2405 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2406 +restoring save and load speed.
2407 +
2408 +This merging is simplified by the use of another aufs mount, of just the
2409 +two overlay branches using the 'shwh' option.
2410 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2411 +       aufs /livesys/merge_union
2412 +
2413 +A merged view of these two branches is then available at
2414 +/livesys/merge_union, and the new feature is that the whiteouts are
2415 +visible!
2416 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2417 +writing to all branches. Also the default mode for all branches is 'ro'.
2418 +It is now possible to save the combined contents of the two overlay
2419 +branches to a new squashfs, e.g.:
2420 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2421 +
2422 +This new squashfs archive can be stored on the boot device and the
2423 +initramfs will use it to replace the old one at the next boot.
2424 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2425 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2426 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2020-01-27 10:57:18.165538015 +0100
2427 @@ -0,0 +1,47 @@
2428 +
2429 +# Copyright (C) 2010-2020 Junjiro R. Okajima
2430 +#
2431 +# This program is free software; you can redistribute it and/or modify
2432 +# it under the terms of the GNU General Public License as published by
2433 +# the Free Software Foundation; either version 2 of the License, or
2434 +# (at your option) any later version.
2435 +#
2436 +# This program is distributed in the hope that it will be useful,
2437 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2438 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2439 +# GNU General Public License for more details.
2440 +#
2441 +# You should have received a copy of the GNU General Public License
2442 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2443 +
2444 +Dynamically customizable FS operations
2445 +----------------------------------------------------------------------
2446 +Generally FS operations (struct inode_operations, struct
2447 +address_space_operations, struct file_operations, etc.) are defined as
2448 +"static const", but it never means that FS have only one set of
2449 +operation. Some FS have multiple sets of them. For instance, ext2 has
2450 +three sets, one for XIP, for NOBH, and for normal.
2451 +Since aufs overrides and redirects these operations, sometimes aufs has
2452 +to change its behaviour according to the branch FS type. More importantly
2453 +VFS acts differently if a function (member in the struct) is set or
2454 +not. It means aufs should have several sets of operations and select one
2455 +among them according to the branch FS definition.
2456 +
2457 +In order to solve this problem and not to affect the behaviour of VFS,
2458 +aufs defines these operations dynamically. For instance, aufs defines
2459 +dummy direct_IO function for struct address_space_operations, but it may
2460 +not be set to the address_space_operations actually. When the branch FS
2461 +doesn't have it, aufs doesn't set it to its address_space_operations
2462 +while the function definition itself is still alive. So the behaviour
2463 +itself will not change, and it will return an error when direct_IO is
2464 +not set.
2465 +
2466 +The lifetime of these dynamically generated operation object is
2467 +maintained by aufs branch object. When the branch is removed from aufs,
2468 +the reference counter of the object is decremented. When it reaches
2469 +zero, the dynamically generated operation object will be freed.
2470 +
2471 +This approach is designed to support AIO (io_submit), Direct I/O and
2472 +XIP (DAX) mainly.
2473 +Currently this approach is applied to address_space_operations for
2474 +regular files only.
2475 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2476 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2477 +++ linux/Documentation/filesystems/aufs/README 2020-01-23 09:59:36.748660743 +0100
2478 @@ -0,0 +1,401 @@
2479 +
2480 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2481 +http://aufs.sf.net
2482 +Junjiro R. Okajima
2483 +
2484 +
2485 +0. Introduction
2486 +----------------------------------------
2487 +In the early days, aufs was entirely re-designed and re-implemented
2488 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2489 +improvements and implementations, it became totally different from
2490 +Unionfs while keeping the basic features.
2491 +Later, Unionfs Version 2.x series began taking some of the same
2492 +approaches to aufs1's.
2493 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2494 +University and his team.
2495 +
2496 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2497 +support,
2498 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2499 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2500 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2501 +  or aufs1 from CVS on SourceForge.
2502 +
2503 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2504 +      According to Christoph Hellwig, linux rejects all union-type
2505 +      filesystems but UnionMount.
2506 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2507 +
2508 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2509 +    UnionMount, and he pointed out an issue around a directory mutex
2510 +    lock and aufs addressed it. But it is still unsure whether aufs will
2511 +    be merged (or any other union solution).
2512 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2513 +
2514 +
2515 +1. Features
2516 +----------------------------------------
2517 +- unite several directories into a single virtual filesystem. The member
2518 +  directory is called as a branch.
2519 +- you can specify the permission flags to the branch, which are 'readonly',
2520 +  'readwrite' and 'whiteout-able.'
2521 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2522 +  readonly branch are modifiable logically.
2523 +- dynamic branch manipulation, add, del.
2524 +- etc...
2525 +
2526 +Also there are many enhancements in aufs, such as:
2527 +- test only the highest one for the directory permission (dirperm1)
2528 +- copyup on open (coo=)
2529 +- 'move' policy for copy-up between two writable branches, after
2530 +  checking free space.
2531 +- xattr, acl
2532 +- readdir(3) in userspace.
2533 +- keep inode number by external inode number table
2534 +- keep the timestamps of file/dir in internal copyup operation
2535 +- seekable directory, supporting NFS readdir.
2536 +- whiteout is hardlinked in order to reduce the consumption of inodes
2537 +  on branch
2538 +- do not copyup, nor create a whiteout when it is unnecessary
2539 +- revert a single systemcall when an error occurs in aufs
2540 +- remount interface instead of ioctl
2541 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2542 +- loopback mounted filesystem as a branch
2543 +- kernel thread for removing the dir who has a plenty of whiteouts
2544 +- support copyup sparse file (a file which has a 'hole' in it)
2545 +- default permission flags for branches
2546 +- selectable permission flags for ro branch, whether whiteout can
2547 +  exist or not
2548 +- export via NFS.
2549 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2550 +- support multiple writable branches, some policies to select one
2551 +  among multiple writable branches.
2552 +- a new semantics for link(2) and rename(2) to support multiple
2553 +  writable branches.
2554 +- no glibc changes are required.
2555 +- pseudo hardlink (hardlink over branches)
2556 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2557 +  including NFS or remote filesystem branch.
2558 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2559 +- and more...
2560 +
2561 +Currently these features are dropped temporary from aufs5.
2562 +See design/08plan.txt in detail.
2563 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2564 +  (robr)
2565 +- statistics of aufs thread (/sys/fs/aufs/stat)
2566 +
2567 +Features or just an idea in the future (see also design/*.txt),
2568 +- reorder the branch index without del/re-add.
2569 +- permanent xino files for NFSD
2570 +- an option for refreshing the opened files after add/del branches
2571 +- light version, without branch manipulation. (unnecessary?)
2572 +- copyup in userspace
2573 +- inotify in userspace
2574 +- readv/writev
2575 +
2576 +
2577 +2. Download
2578 +----------------------------------------
2579 +There are three GIT trees for aufs5, aufs5-linux.git,
2580 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2581 +"aufs-util.git."
2582 +While the aufs-util is always necessary, you need either of aufs5-linux
2583 +or aufs5-standalone.
2584 +
2585 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2586 +git://git.kernel.org/.../torvalds/linux.git.
2587 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2588 +build aufs5 as an external kernel module.
2589 +Several extra patches are not included in this tree. Only
2590 +aufs5-standalone tree contains them. They are described in the later
2591 +section "Configuration and Compilation."
2592 +
2593 +On the other hand, the aufs5-standalone tree has only aufs source files
2594 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2595 +But you need to apply all aufs patches manually.
2596 +
2597 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2598 +represents the linux kernel version, "linux-5.x". For instance,
2599 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2600 +"aufs5.x-rcN" branch.
2601 +
2602 +o aufs5-linux tree
2603 +$ git clone --reference /your/linux/git/tree \
2604 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2605 +- if you don't have linux GIT tree, then remove "--reference ..."
2606 +$ cd aufs5-linux.git
2607 +$ git checkout origin/aufs5.0
2608 +
2609 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2610 +leave the patch-work to GIT.
2611 +$ cd /your/linux/git/tree
2612 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2613 +$ git fetch aufs5
2614 +$ git checkout -b my5.0 v5.0
2615 +$ (add your local change...)
2616 +$ git pull aufs5 aufs5.0
2617 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2618 +- you may need to solve some conflicts between your_changes and
2619 +  aufs5.0. in this case, git-rerere is recommended so that you can
2620 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2621 +  later in the future.
2622 +
2623 +o aufs5-standalone tree
2624 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2625 +$ cd aufs5-standalone.git
2626 +$ git checkout origin/aufs5.0
2627 +
2628 +o aufs-util tree
2629 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2630 +- note that the public aufs-util.git is on SourceForge instead of
2631 +  GitHUB.
2632 +$ cd aufs-util.git
2633 +$ git checkout origin/aufs5.0
2634 +
2635 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2636 +The minor version number, 'x' in '5.x', of aufs may not always
2637 +follow the minor version number of the kernel.
2638 +Because changes in the kernel that cause the use of a new
2639 +minor version number do not always require changes to aufs-util.
2640 +
2641 +Since aufs-util has its own minor version number, you may not be
2642 +able to find a GIT branch in aufs-util for your kernel's
2643 +exact minor version number.
2644 +In this case, you should git-checkout the branch for the
2645 +nearest lower number.
2646 +
2647 +For (an unreleased) example:
2648 +If you are using "linux-5.10" and the "aufs5.10" branch
2649 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2650 +or something numerically smaller is the branch for your kernel.
2651 +
2652 +Also you can view all branches by
2653 +       $ git branch -a
2654 +
2655 +
2656 +3. Configuration and Compilation
2657 +----------------------------------------
2658 +Make sure you have git-checkout'ed the correct branch.
2659 +
2660 +For aufs5-linux tree,
2661 +- enable CONFIG_AUFS_FS.
2662 +- set other aufs configurations if necessary.
2663 +
2664 +For aufs5-standalone tree,
2665 +There are several ways to build.
2666 +
2667 +1.
2668 +- apply ./aufs5-kbuild.patch to your kernel source files.
2669 +- apply ./aufs5-base.patch too.
2670 +- apply ./aufs5-mmap.patch too.
2671 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2672 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2673 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2674 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2675 +- enable CONFIG_AUFS_FS, you can select either
2676 +  =m or =y.
2677 +- and build your kernel as usual.
2678 +- install the built kernel.
2679 +- install the header files too by "make headers_install" to the
2680 +  directory where you specify. By default, it is $PWD/usr.
2681 +  "make help" shows a brief note for headers_install.
2682 +- and reboot your system.
2683 +
2684 +2.
2685 +- module only (CONFIG_AUFS_FS=m).
2686 +- apply ./aufs5-base.patch to your kernel source files.
2687 +- apply ./aufs5-mmap.patch too.
2688 +- apply ./aufs5-standalone.patch too.
2689 +- build your kernel, don't forget "make headers_install", and reboot.
2690 +- edit ./config.mk and set other aufs configurations if necessary.
2691 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2692 +  every aufs configurations.
2693 +- build the module by simple "make".
2694 +- you can specify ${KDIR} make variable which points to your kernel
2695 +  source tree.
2696 +- install the files
2697 +  + run "make install" to install the aufs module, or copy the built
2698 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2699 +  + run "make install_headers" (instead of headers_install) to install
2700 +    the modified aufs header file (you can specify DESTDIR which is
2701 +    available in aufs standalone version's Makefile only), or copy
2702 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2703 +    you like manually. By default, the target directory is $PWD/usr.
2704 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2705 +  kernel source tree.
2706 +
2707 +Note: The header file aufs_type.h is necessary to build aufs-util
2708 +      as well as "make headers_install" in the kernel source tree.
2709 +      headers_install is subject to be forgotten, but it is essentially
2710 +      necessary, not only for building aufs-util.
2711 +      You may not meet problems without headers_install in some older
2712 +      version though.
2713 +
2714 +And then,
2715 +- read README in aufs-util, build and install it
2716 +- note that your distribution may contain an obsoleted version of
2717 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2718 +  utilities, make sure that your compiler refers the correct aufs header
2719 +  file which is built by "make headers_install."
2720 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2721 +  then run "make install_ulib" too. And refer to the aufs manual in
2722 +  detail.
2723 +
2724 +There several other patches in aufs5-standalone.git. They are all
2725 +optional. When you meet some problems, they will help you.
2726 +- aufs5-loopback.patch
2727 +  Supports a nested loopback mount in a branch-fs. This patch is
2728 +  unnecessary until aufs produces a message like "you may want to try
2729 +  another patch for loopback file".
2730 +- proc_mounts.patch
2731 +  When there are many mountpoints and many mount(2)/umount(2) are
2732 +  running, then /proc/mounts may not show the all mountpoints.  This
2733 +  patch makes /proc/mounts always show the full mountpoints list.
2734 +  If you don't want to apply this patch and meet such problem, then you
2735 +  need to increase the value of 'ProcMounts_Times' make-variable in
2736 +  aufs-util.git as a second best solution.
2737 +- vfs-ino.patch
2738 +  Modifies a system global kernel internal function get_next_ino() in
2739 +  order to stop assigning 0 for an inode-number. Not directly related to
2740 +  aufs, but recommended generally.
2741 +- tmpfs-idr.patch
2742 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2743 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2744 +  duplication of inode number, which is important for backup tools and
2745 +  other utilities. When you find aufs XINO files for tmpfs branch
2746 +  growing too much, try this patch.
2747 +- lockdep-debug.patch
2748 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2749 +  also a caller of VFS functions for branch filesystems, subclassing of
2750 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2751 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2752 +  need to apply this debug patch to expand several constant values.
2753 +  If you don't know what LOCKDEP is, then you don't have apply this
2754 +  patch.
2755 +
2756 +
2757 +4. Usage
2758 +----------------------------------------
2759 +At first, make sure aufs-util are installed, and please read the aufs
2760 +manual, aufs.5 in aufs-util.git tree.
2761 +$ man -l aufs.5
2762 +
2763 +And then,
2764 +$ mkdir /tmp/rw /tmp/aufs
2765 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2766 +
2767 +Here is another example. The result is equivalent.
2768 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2769 +  Or
2770 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2771 +# mount -o remount,append:${HOME} /tmp/aufs
2772 +
2773 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2774 +you modify a file under /tmp/aufs, the one on your home directory is
2775 +not affected, instead the same named file will be newly created under
2776 +/tmp/rw. And all of your modification to a file will be applied to
2777 +the one under /tmp/rw. This is called the file based Copy on Write
2778 +(COW) method.
2779 +Aufs mount options are described in aufs.5.
2780 +If you run chroot or something and make your aufs as a root directory,
2781 +then you need to customize the shutdown script. See the aufs manual in
2782 +detail.
2783 +
2784 +Additionally, there are some sample usages of aufs which are a
2785 +diskless system with network booting, and LiveCD over NFS.
2786 +See sample dir in CVS tree on SourceForge.
2787 +
2788 +
2789 +5. Contact
2790 +----------------------------------------
2791 +When you have any problems or strange behaviour in aufs, please let me
2792 +know with:
2793 +- /proc/mounts (instead of the output of mount(8))
2794 +- /sys/module/aufs/*
2795 +- /sys/fs/aufs/* (if you have them)
2796 +- /debug/aufs/* (if you have them)
2797 +- linux kernel version
2798 +  if your kernel is not plain, for example modified by distributor,
2799 +  the url where i can download its source is necessary too.
2800 +- aufs version which was printed at loading the module or booting the
2801 +  system, instead of the date you downloaded.
2802 +- configuration (define/undefine CONFIG_AUFS_xxx)
2803 +- kernel configuration or /proc/config.gz (if you have it)
2804 +- LSM (linux security module, if you are using)
2805 +- behaviour which you think to be incorrect
2806 +- actual operation, reproducible one is better
2807 +- mailto: aufs-users at lists.sourceforge.net
2808 +
2809 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2810 +and Feature Requests) on SourceForge. Please join and write to
2811 +aufs-users ML.
2812 +
2813 +
2814 +6. Acknowledgements
2815 +----------------------------------------
2816 +Thanks to everyone who have tried and are using aufs, whoever
2817 +have reported a bug or any feedback.
2818 +
2819 +Especially donators:
2820 +Tomas Matejicek(slax.org) made a donation (much more than once).
2821 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2822 +       scripts) is making "doubling" donations.
2823 +       Unfortunately I cannot list all of the donators, but I really
2824 +       appreciate.
2825 +       It ends Aug 2010, but the ordinary donation URL is still available.
2826 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2827 +Dai Itasaka made a donation (2007/8).
2828 +Chuck Smith made a donation (2008/4, 10 and 12).
2829 +Henk Schoneveld made a donation (2008/9).
2830 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2831 +Francois Dupoux made a donation (2008/11).
2832 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2833 +       aufs2 GIT tree (2009/2).
2834 +William Grant made a donation (2009/3).
2835 +Patrick Lane made a donation (2009/4).
2836 +The Mail Archive (mail-archive.com) made donations (2009/5).
2837 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2838 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2839 +Pavel Pronskiy made a donation (2011/2).
2840 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2841 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2842 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2843 +11).
2844 +Sam Liddicott made a donation (2011/9).
2845 +Era Scarecrow made a donation (2013/4).
2846 +Bor Ratajc made a donation (2013/4).
2847 +Alessandro Gorreta made a donation (2013/4).
2848 +POIRETTE Marc made a donation (2013/4).
2849 +Alessandro Gorreta made a donation (2013/4).
2850 +lauri kasvandik made a donation (2013/5).
2851 +"pemasu from Finland" made a donation (2013/7).
2852 +The Parted Magic Project made a donation (2013/9 and 11).
2853 +Pavel Barta made a donation (2013/10).
2854 +Nikolay Pertsev made a donation (2014/5).
2855 +James B made a donation (2014/7 and 2015/7).
2856 +Stefano Di Biase made a donation (2014/8).
2857 +Daniel Epellei made a donation (2015/1).
2858 +OmegaPhil made a donation (2016/1, 2018/4).
2859 +Tomasz Szewczyk made a donation (2016/4).
2860 +James Burry made a donation (2016/12).
2861 +Carsten Rose made a donation (2018/9).
2862 +Porteus Kiosk made a donation (2018/10).
2863 +
2864 +Thank you very much.
2865 +Donations are always, including future donations, very important and
2866 +helpful for me to keep on developing aufs.
2867 +
2868 +
2869 +7.
2870 +----------------------------------------
2871 +If you are an experienced user, no explanation is needed. Aufs is
2872 +just a linux filesystem.
2873 +
2874 +
2875 +Enjoy!
2876 +
2877 +# Local variables: ;
2878 +# mode: text;
2879 +# End: ;
2880 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2881 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2882 +++ linux/fs/aufs/aufs.h        2020-01-27 10:57:18.165538015 +0100
2883 @@ -0,0 +1,62 @@
2884 +/* SPDX-License-Identifier: GPL-2.0 */
2885 +/*
2886 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2887 + *
2888 + * This program, aufs is free software; you can redistribute it and/or modify
2889 + * it under the terms of the GNU General Public License as published by
2890 + * the Free Software Foundation; either version 2 of the License, or
2891 + * (at your option) any later version.
2892 + *
2893 + * This program is distributed in the hope that it will be useful,
2894 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2895 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2896 + * GNU General Public License for more details.
2897 + *
2898 + * You should have received a copy of the GNU General Public License
2899 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2900 + */
2901 +
2902 +/*
2903 + * all header files
2904 + */
2905 +
2906 +#ifndef __AUFS_H__
2907 +#define __AUFS_H__
2908 +
2909 +#ifdef __KERNEL__
2910 +
2911 +#define AuStub(type, name, body, ...) \
2912 +       static inline type name(__VA_ARGS__) { body; }
2913 +
2914 +#define AuStubVoid(name, ...) \
2915 +       AuStub(void, name, , __VA_ARGS__)
2916 +#define AuStubInt0(name, ...) \
2917 +       AuStub(int, name, return 0, __VA_ARGS__)
2918 +
2919 +#include "debug.h"
2920 +
2921 +#include "branch.h"
2922 +#include "cpup.h"
2923 +#include "dcsub.h"
2924 +#include "dbgaufs.h"
2925 +#include "dentry.h"
2926 +#include "dir.h"
2927 +#include "dirren.h"
2928 +#include "dynop.h"
2929 +#include "file.h"
2930 +#include "fstype.h"
2931 +#include "hbl.h"
2932 +#include "inode.h"
2933 +#include "lcnt.h"
2934 +#include "loop.h"
2935 +#include "module.h"
2936 +#include "opts.h"
2937 +#include "rwsem.h"
2938 +#include "super.h"
2939 +#include "sysaufs.h"
2940 +#include "vfsub.h"
2941 +#include "whout.h"
2942 +#include "wkq.h"
2943 +
2944 +#endif /* __KERNEL__ */
2945 +#endif /* __AUFS_H__ */
2946 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2947 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2948 +++ linux/fs/aufs/branch.c      2020-08-03 09:14:46.095748745 +0200
2949 @@ -0,0 +1,1427 @@
2950 +// SPDX-License-Identifier: GPL-2.0
2951 +/*
2952 + * Copyright (C) 2005-2020 Junjiro R. Okajima
2953 + *
2954 + * This program, aufs is free software; you can redistribute it and/or modify
2955 + * it under the terms of the GNU General Public License as published by
2956 + * the Free Software Foundation; either version 2 of the License, or
2957 + * (at your option) any later version.
2958 + *
2959 + * This program is distributed in the hope that it will be useful,
2960 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2961 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2962 + * GNU General Public License for more details.
2963 + *
2964 + * You should have received a copy of the GNU General Public License
2965 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2966 + */
2967 +
2968 +/*
2969 + * branch management
2970 + */
2971 +
2972 +#include <linux/compat.h>
2973 +#include <linux/statfs.h>
2974 +#include "aufs.h"
2975 +
2976 +/*
2977 + * free a single branch
2978 + */
2979 +static void au_br_do_free(struct au_branch *br)
2980 +{
2981 +       int i;
2982 +       struct au_wbr *wbr;
2983 +       struct au_dykey **key;
2984 +
2985 +       au_hnotify_fin_br(br);
2986 +       /* always, regardless the mount option */
2987 +       au_dr_hino_free(&br->br_dirren);
2988 +       au_xino_put(br);
2989 +
2990 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2991 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2992 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2993 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2994 +
2995 +       wbr = br->br_wbr;
2996 +       if (wbr) {
2997 +               for (i = 0; i < AuBrWh_Last; i++)
2998 +                       dput(wbr->wbr_wh[i]);
2999 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
3000 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
3001 +       }
3002 +
3003 +       if (br->br_fhsm) {
3004 +               au_br_fhsm_fin(br->br_fhsm);
3005 +               au_kfree_try_rcu(br->br_fhsm);
3006 +       }
3007 +
3008 +       key = br->br_dykey;
3009 +       for (i = 0; i < AuBrDynOp; i++, key++)
3010 +               if (*key)
3011 +                       au_dy_put(*key);
3012 +               else
3013 +                       break;
3014 +
3015 +       /* recursive lock, s_umount of branch's */
3016 +       /* synchronize_rcu(); */ /* why? */
3017 +       lockdep_off();
3018 +       path_put(&br->br_path);
3019 +       lockdep_on();
3020 +       au_kfree_rcu(wbr);
3021 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3022 +       au_lcnt_wait_for_fin(&br->br_count);
3023 +       /* I don't know why, but percpu_refcount requires this */
3024 +       /* synchronize_rcu(); */
3025 +       au_kfree_rcu(br);
3026 +}
3027 +
3028 +/*
3029 + * frees all branches
3030 + */
3031 +void au_br_free(struct au_sbinfo *sbinfo)
3032 +{
3033 +       aufs_bindex_t bmax;
3034 +       struct au_branch **br;
3035 +
3036 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3037 +
3038 +       bmax = sbinfo->si_bbot + 1;
3039 +       br = sbinfo->si_branch;
3040 +       while (bmax--)
3041 +               au_br_do_free(*br++);
3042 +}
3043 +
3044 +/*
3045 + * find the index of a branch which is specified by @br_id.
3046 + */
3047 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3048 +{
3049 +       aufs_bindex_t bindex, bbot;
3050 +
3051 +       bbot = au_sbbot(sb);
3052 +       for (bindex = 0; bindex <= bbot; bindex++)
3053 +               if (au_sbr_id(sb, bindex) == br_id)
3054 +                       return bindex;
3055 +       return -1;
3056 +}
3057 +
3058 +/* ---------------------------------------------------------------------- */
3059 +
3060 +/*
3061 + * add a branch
3062 + */
3063 +
3064 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3065 +                       struct dentry *h_root)
3066 +{
3067 +       if (unlikely(h_adding == h_root
3068 +                    || au_test_loopback_overlap(sb, h_adding)))
3069 +               return 1;
3070 +       if (h_adding->d_sb != h_root->d_sb)
3071 +               return 0;
3072 +       return au_test_subdir(h_adding, h_root)
3073 +               || au_test_subdir(h_root, h_adding);
3074 +}
3075 +
3076 +/*
3077 + * returns a newly allocated branch. @new_nbranch is a number of branches
3078 + * after adding a branch.
3079 + */
3080 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3081 +                                    int perm)
3082 +{
3083 +       struct au_branch *add_branch;
3084 +       struct dentry *root;
3085 +       struct inode *inode;
3086 +       int err;
3087 +
3088 +       err = -ENOMEM;
3089 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3090 +       if (unlikely(!add_branch))
3091 +               goto out;
3092 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3093 +       if (unlikely(!add_branch->br_xino))
3094 +               goto out_br;
3095 +       err = au_hnotify_init_br(add_branch, perm);
3096 +       if (unlikely(err))
3097 +               goto out_xino;
3098 +
3099 +       if (au_br_writable(perm)) {
3100 +               /* may be freed separately at changing the branch permission */
3101 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3102 +                                            GFP_NOFS);
3103 +               if (unlikely(!add_branch->br_wbr))
3104 +                       goto out_hnotify;
3105 +       }
3106 +
3107 +       if (au_br_fhsm(perm)) {
3108 +               err = au_fhsm_br_alloc(add_branch);
3109 +               if (unlikely(err))
3110 +                       goto out_wbr;
3111 +       }
3112 +
3113 +       root = sb->s_root;
3114 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3115 +       if (!err)
3116 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3117 +       if (!err) {
3118 +               inode = d_inode(root);
3119 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3120 +                                       /*may_shrink*/0);
3121 +       }
3122 +       if (!err)
3123 +               return add_branch; /* success */
3124 +
3125 +out_wbr:
3126 +       au_kfree_rcu(add_branch->br_wbr);
3127 +out_hnotify:
3128 +       au_hnotify_fin_br(add_branch);
3129 +out_xino:
3130 +       au_xino_put(add_branch);
3131 +out_br:
3132 +       au_kfree_rcu(add_branch);
3133 +out:
3134 +       return ERR_PTR(err);
3135 +}
3136 +
3137 +/*
3138 + * test if the branch permission is legal or not.
3139 + */
3140 +static int test_br(struct inode *inode, int brperm, char *path)
3141 +{
3142 +       int err;
3143 +
3144 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3145 +       if (!err)
3146 +               goto out;
3147 +
3148 +       err = -EINVAL;
3149 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3150 +
3151 +out:
3152 +       return err;
3153 +}
3154 +
3155 +/*
3156 + * returns:
3157 + * 0: success, the caller will add it
3158 + * plus: success, it is already unified, the caller should ignore it
3159 + * minus: error
3160 + */
3161 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3162 +{
3163 +       int err;
3164 +       aufs_bindex_t bbot, bindex;
3165 +       struct dentry *root, *h_dentry;
3166 +       struct inode *inode, *h_inode;
3167 +
3168 +       root = sb->s_root;
3169 +       bbot = au_sbbot(sb);
3170 +       if (unlikely(bbot >= 0
3171 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3172 +               err = 1;
3173 +               if (!remount) {
3174 +                       err = -EINVAL;
3175 +                       pr_err("%s duplicated\n", add->pathname);
3176 +               }
3177 +               goto out;
3178 +       }
3179 +
3180 +       err = -ENOSPC; /* -E2BIG; */
3181 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3182 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3183 +               pr_err("number of branches exceeded %s\n", add->pathname);
3184 +               goto out;
3185 +       }
3186 +
3187 +       err = -EDOM;
3188 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3189 +               pr_err("bad index %d\n", add->bindex);
3190 +               goto out;
3191 +       }
3192 +
3193 +       inode = d_inode(add->path.dentry);
3194 +       err = -ENOENT;
3195 +       if (unlikely(!inode->i_nlink)) {
3196 +               pr_err("no existence %s\n", add->pathname);
3197 +               goto out;
3198 +       }
3199 +
3200 +       err = -EINVAL;
3201 +       if (unlikely(inode->i_sb == sb)) {
3202 +               pr_err("%s must be outside\n", add->pathname);
3203 +               goto out;
3204 +       }
3205 +
3206 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3207 +               pr_err("unsupported filesystem, %s (%s)\n",
3208 +                      add->pathname, au_sbtype(inode->i_sb));
3209 +               goto out;
3210 +       }
3211 +
3212 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3213 +               pr_err("already stacked, %s (%s)\n",
3214 +                      add->pathname, au_sbtype(inode->i_sb));
3215 +               goto out;
3216 +       }
3217 +
3218 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3219 +       if (unlikely(err))
3220 +               goto out;
3221 +
3222 +       if (bbot < 0)
3223 +               return 0; /* success */
3224 +
3225 +       err = -EINVAL;
3226 +       for (bindex = 0; bindex <= bbot; bindex++)
3227 +               if (unlikely(test_overlap(sb, add->path.dentry,
3228 +                                         au_h_dptr(root, bindex)))) {
3229 +                       pr_err("%s is overlapped\n", add->pathname);
3230 +                       goto out;
3231 +               }
3232 +
3233 +       err = 0;
3234 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3235 +               h_dentry = au_h_dptr(root, 0);
3236 +               h_inode = d_inode(h_dentry);
3237 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3238 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3239 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3240 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3241 +                               add->pathname,
3242 +                               i_uid_read(inode), i_gid_read(inode),
3243 +                               (inode->i_mode & S_IALLUGO),
3244 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3245 +                               (h_inode->i_mode & S_IALLUGO));
3246 +       }
3247 +
3248 +out:
3249 +       return err;
3250 +}
3251 +
3252 +/*
3253 + * initialize or clean the whiteouts for an adding branch
3254 + */
3255 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3256 +                        int new_perm)
3257 +{
3258 +       int err, old_perm;
3259 +       aufs_bindex_t bindex;
3260 +       struct inode *h_inode;
3261 +       struct au_wbr *wbr;
3262 +       struct au_hinode *hdir;
3263 +       struct dentry *h_dentry;
3264 +
3265 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3266 +       if (unlikely(err))
3267 +               goto out;
3268 +
3269 +       wbr = br->br_wbr;
3270 +       old_perm = br->br_perm;
3271 +       br->br_perm = new_perm;
3272 +       hdir = NULL;
3273 +       h_inode = NULL;
3274 +       bindex = au_br_index(sb, br->br_id);
3275 +       if (0 <= bindex) {
3276 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3277 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3278 +       } else {
3279 +               h_dentry = au_br_dentry(br);
3280 +               h_inode = d_inode(h_dentry);
3281 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3282 +       }
3283 +       if (!wbr)
3284 +               err = au_wh_init(br, sb);
3285 +       else {
3286 +               wbr_wh_write_lock(wbr);
3287 +               err = au_wh_init(br, sb);
3288 +               wbr_wh_write_unlock(wbr);
3289 +       }
3290 +       if (hdir)
3291 +               au_hn_inode_unlock(hdir);
3292 +       else
3293 +               inode_unlock(h_inode);
3294 +       vfsub_mnt_drop_write(au_br_mnt(br));
3295 +       br->br_perm = old_perm;
3296 +
3297 +       if (!err && wbr && !au_br_writable(new_perm)) {
3298 +               au_kfree_rcu(wbr);
3299 +               br->br_wbr = NULL;
3300 +       }
3301 +
3302 +out:
3303 +       return err;
3304 +}
3305 +
3306 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3307 +                      int perm)
3308 +{
3309 +       int err;
3310 +       struct kstatfs kst;
3311 +       struct au_wbr *wbr;
3312 +
3313 +       wbr = br->br_wbr;
3314 +       au_rw_init(&wbr->wbr_wh_rwsem);
3315 +       atomic_set(&wbr->wbr_wh_running, 0);
3316 +
3317 +       /*
3318 +        * a limit for rmdir/rename a dir
3319 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3320 +        */
3321 +       err = vfs_statfs(&br->br_path, &kst);
3322 +       if (unlikely(err))
3323 +               goto out;
3324 +       err = -EINVAL;
3325 +       if (kst.f_namelen >= NAME_MAX)
3326 +               err = au_br_init_wh(sb, br, perm);
3327 +       else
3328 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3329 +                      au_br_dentry(br),
3330 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3331 +
3332 +out:
3333 +       return err;
3334 +}
3335 +
3336 +/* initialize a new branch */
3337 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3338 +                     struct au_opt_add *add)
3339 +{
3340 +       int err;
3341 +       struct au_branch *brbase;
3342 +       struct file *xf;
3343 +       struct inode *h_inode;
3344 +
3345 +       err = 0;
3346 +       br->br_perm = add->perm;
3347 +       br->br_path = add->path; /* set first, path_get() later */
3348 +       spin_lock_init(&br->br_dykey_lock);
3349 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3350 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3351 +       br->br_id = au_new_br_id(sb);
3352 +       AuDebugOn(br->br_id < 0);
3353 +
3354 +       /* always, regardless the given option */
3355 +       err = au_dr_br_init(sb, br, &add->path);
3356 +       if (unlikely(err))
3357 +               goto out_err;
3358 +
3359 +       if (au_br_writable(add->perm)) {
3360 +               err = au_wbr_init(br, sb, add->perm);
3361 +               if (unlikely(err))
3362 +                       goto out_err;
3363 +       }
3364 +
3365 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3366 +               brbase = au_sbr(sb, 0);
3367 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3368 +               AuDebugOn(!xf);
3369 +               h_inode = d_inode(add->path.dentry);
3370 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3371 +               if (unlikely(err)) {
3372 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3373 +                       goto out_err;
3374 +               }
3375 +       }
3376 +
3377 +       sysaufs_br_init(br);
3378 +       path_get(&br->br_path);
3379 +       goto out; /* success */
3380 +
3381 +out_err:
3382 +       memset(&br->br_path, 0, sizeof(br->br_path));
3383 +out:
3384 +       return err;
3385 +}
3386 +
3387 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3388 +                            struct au_branch *br, aufs_bindex_t bbot,
3389 +                            aufs_bindex_t amount)
3390 +{
3391 +       struct au_branch **brp;
3392 +
3393 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3394 +
3395 +       brp = sbinfo->si_branch + bindex;
3396 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3397 +       *brp = br;
3398 +       sbinfo->si_bbot++;
3399 +       if (unlikely(bbot < 0))
3400 +               sbinfo->si_bbot = 0;
3401 +}
3402 +
3403 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3404 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3405 +{
3406 +       struct au_hdentry *hdp;
3407 +
3408 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3409 +
3410 +       hdp = au_hdentry(dinfo, bindex);
3411 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3412 +       au_h_dentry_init(hdp);
3413 +       dinfo->di_bbot++;
3414 +       if (unlikely(bbot < 0))
3415 +               dinfo->di_btop = 0;
3416 +}
3417 +
3418 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3419 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3420 +{
3421 +       struct au_hinode *hip;
3422 +
3423 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3424 +
3425 +       hip = au_hinode(iinfo, bindex);
3426 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3427 +       au_hinode_init(hip);
3428 +       iinfo->ii_bbot++;
3429 +       if (unlikely(bbot < 0))
3430 +               iinfo->ii_btop = 0;
3431 +}
3432 +
3433 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3434 +                        aufs_bindex_t bindex)
3435 +{
3436 +       struct dentry *root, *h_dentry;
3437 +       struct inode *root_inode, *h_inode;
3438 +       aufs_bindex_t bbot, amount;
3439 +
3440 +       root = sb->s_root;
3441 +       root_inode = d_inode(root);
3442 +       bbot = au_sbbot(sb);
3443 +       amount = bbot + 1 - bindex;
3444 +       h_dentry = au_br_dentry(br);
3445 +       au_sbilist_lock();
3446 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3447 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3448 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3449 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3450 +       h_inode = d_inode(h_dentry);
3451 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3452 +       au_sbilist_unlock();
3453 +}
3454 +
3455 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3456 +{
3457 +       int err;
3458 +       aufs_bindex_t bbot, add_bindex;
3459 +       struct dentry *root, *h_dentry;
3460 +       struct inode *root_inode;
3461 +       struct au_branch *add_branch;
3462 +
3463 +       root = sb->s_root;
3464 +       root_inode = d_inode(root);
3465 +       IMustLock(root_inode);
3466 +       IiMustWriteLock(root_inode);
3467 +       err = test_add(sb, add, remount);
3468 +       if (unlikely(err < 0))
3469 +               goto out;
3470 +       if (err) {
3471 +               err = 0;
3472 +               goto out; /* success */
3473 +       }
3474 +
3475 +       bbot = au_sbbot(sb);
3476 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3477 +       err = PTR_ERR(add_branch);
3478 +       if (IS_ERR(add_branch))
3479 +               goto out;
3480 +
3481 +       err = au_br_init(add_branch, sb, add);
3482 +       if (unlikely(err)) {
3483 +               au_br_do_free(add_branch);
3484 +               goto out;
3485 +       }
3486 +
3487 +       add_bindex = add->bindex;
3488 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3489 +       au_br_do_add(sb, add_branch, add_bindex);
3490 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3491 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3492 +
3493 +       h_dentry = add->path.dentry;
3494 +       if (!add_bindex) {
3495 +               au_cpup_attr_all(root_inode, /*force*/1);
3496 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3497 +       } else
3498 +               au_add_nlink(root_inode, d_inode(h_dentry));
3499 +
3500 +out:
3501 +       return err;
3502 +}
3503 +
3504 +/* ---------------------------------------------------------------------- */
3505 +
3506 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3507 +                                      unsigned long long max __maybe_unused,
3508 +                                      void *arg)
3509 +{
3510 +       unsigned long long n;
3511 +       struct file **p, *f;
3512 +       struct hlist_bl_head *files;
3513 +       struct hlist_bl_node *pos;
3514 +       struct au_finfo *finfo;
3515 +
3516 +       n = 0;
3517 +       p = a;
3518 +       files = &au_sbi(sb)->si_files;
3519 +       hlist_bl_lock(files);
3520 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3521 +               f = finfo->fi_file;
3522 +               if (file_count(f)
3523 +                   && !special_file(file_inode(f)->i_mode)) {
3524 +                       get_file(f);
3525 +                       *p++ = f;
3526 +                       n++;
3527 +                       AuDebugOn(n > max);
3528 +               }
3529 +       }
3530 +       hlist_bl_unlock(files);
3531 +
3532 +       return n;
3533 +}
3534 +
3535 +static struct file **au_farray_alloc(struct super_block *sb,
3536 +                                    unsigned long long *max)
3537 +{
3538 +       struct au_sbinfo *sbi;
3539 +
3540 +       sbi = au_sbi(sb);
3541 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3542 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3543 +}
3544 +
3545 +static void au_farray_free(struct file **a, unsigned long long max)
3546 +{
3547 +       unsigned long long ull;
3548 +
3549 +       for (ull = 0; ull < max; ull++)
3550 +               if (a[ull])
3551 +                       fput(a[ull]);
3552 +       kvfree(a);
3553 +}
3554 +
3555 +/* ---------------------------------------------------------------------- */
3556 +
3557 +/*
3558 + * delete a branch
3559 + */
3560 +
3561 +/* to show the line number, do not make it inlined function */
3562 +#define AuVerbose(do_info, fmt, ...) do { \
3563 +       if (do_info) \
3564 +               pr_info(fmt, ##__VA_ARGS__); \
3565 +} while (0)
3566 +
3567 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3568 +                        aufs_bindex_t bbot)
3569 +{
3570 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3571 +}
3572 +
3573 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3574 +                        aufs_bindex_t bbot)
3575 +{
3576 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3577 +}
3578 +
3579 +/*
3580 + * test if the branch is deletable or not.
3581 + */
3582 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3583 +                           unsigned int sigen, const unsigned int verbose)
3584 +{
3585 +       int err, i, j, ndentry;
3586 +       aufs_bindex_t btop, bbot;
3587 +       struct au_dcsub_pages dpages;
3588 +       struct au_dpage *dpage;
3589 +       struct dentry *d;
3590 +
3591 +       err = au_dpages_init(&dpages, GFP_NOFS);
3592 +       if (unlikely(err))
3593 +               goto out;
3594 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3595 +       if (unlikely(err))
3596 +               goto out_dpages;
3597 +
3598 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3599 +               dpage = dpages.dpages + i;
3600 +               ndentry = dpage->ndentry;
3601 +               for (j = 0; !err && j < ndentry; j++) {
3602 +                       d = dpage->dentries[j];
3603 +                       AuDebugOn(au_dcount(d) <= 0);
3604 +                       if (!au_digen_test(d, sigen)) {
3605 +                               di_read_lock_child(d, AuLock_IR);
3606 +                               if (unlikely(au_dbrange_test(d))) {
3607 +                                       di_read_unlock(d, AuLock_IR);
3608 +                                       continue;
3609 +                               }
3610 +                       } else {
3611 +                               di_write_lock_child(d);
3612 +                               if (unlikely(au_dbrange_test(d))) {
3613 +                                       di_write_unlock(d);
3614 +                                       continue;
3615 +                               }
3616 +                               err = au_reval_dpath(d, sigen);
3617 +                               if (!err)
3618 +                                       di_downgrade_lock(d, AuLock_IR);
3619 +                               else {
3620 +                                       di_write_unlock(d);
3621 +                                       break;
3622 +                               }
3623 +                       }
3624 +
3625 +                       /* AuDbgDentry(d); */
3626 +                       btop = au_dbtop(d);
3627 +                       bbot = au_dbbot(d);
3628 +                       if (btop <= bindex
3629 +                           && bindex <= bbot
3630 +                           && au_h_dptr(d, bindex)
3631 +                           && au_test_dbusy(d, btop, bbot)) {
3632 +                               err = -EBUSY;
3633 +                               AuVerbose(verbose, "busy %pd\n", d);
3634 +                               AuDbgDentry(d);
3635 +                       }
3636 +                       di_read_unlock(d, AuLock_IR);
3637 +               }
3638 +       }
3639 +
3640 +out_dpages:
3641 +       au_dpages_free(&dpages);
3642 +out:
3643 +       return err;
3644 +}
3645 +
3646 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3647 +                          unsigned int sigen, const unsigned int verbose)
3648 +{
3649 +       int err;
3650 +       unsigned long long max, ull;
3651 +       struct inode *i, **array;
3652 +       aufs_bindex_t btop, bbot;
3653 +
3654 +       array = au_iarray_alloc(sb, &max);
3655 +       err = PTR_ERR(array);
3656 +       if (IS_ERR(array))
3657 +               goto out;
3658 +
3659 +       err = 0;
3660 +       AuDbg("b%d\n", bindex);
3661 +       for (ull = 0; !err && ull < max; ull++) {
3662 +               i = array[ull];
3663 +               if (unlikely(!i))
3664 +                       break;
3665 +               if (i->i_ino == AUFS_ROOT_INO)
3666 +                       continue;
3667 +
3668 +               /* AuDbgInode(i); */
3669 +               if (au_iigen(i, NULL) == sigen)
3670 +                       ii_read_lock_child(i);
3671 +               else {
3672 +                       ii_write_lock_child(i);
3673 +                       err = au_refresh_hinode_self(i);
3674 +                       au_iigen_dec(i);
3675 +                       if (!err)
3676 +                               ii_downgrade_lock(i);
3677 +                       else {
3678 +                               ii_write_unlock(i);
3679 +                               break;
3680 +                       }
3681 +               }
3682 +
3683 +               btop = au_ibtop(i);
3684 +               bbot = au_ibbot(i);
3685 +               if (btop <= bindex
3686 +                   && bindex <= bbot
3687 +                   && au_h_iptr(i, bindex)
3688 +                   && au_test_ibusy(i, btop, bbot)) {
3689 +                       err = -EBUSY;
3690 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3691 +                       AuDbgInode(i);
3692 +               }
3693 +               ii_read_unlock(i);
3694 +       }
3695 +       au_iarray_free(array, max);
3696 +
3697 +out:
3698 +       return err;
3699 +}
3700 +
3701 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3702 +                             const unsigned int verbose)
3703 +{
3704 +       int err;
3705 +       unsigned int sigen;
3706 +
3707 +       sigen = au_sigen(root->d_sb);
3708 +       DiMustNoWaiters(root);
3709 +       IiMustNoWaiters(d_inode(root));
3710 +       di_write_unlock(root);
3711 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3712 +       if (!err)
3713 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3714 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3715 +
3716 +       return err;
3717 +}
3718 +
3719 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3720 +                        struct file **to_free, int *idx)
3721 +{
3722 +       int err;
3723 +       unsigned char matched, root;
3724 +       aufs_bindex_t bindex, bbot;
3725 +       struct au_fidir *fidir;
3726 +       struct au_hfile *hfile;
3727 +
3728 +       err = 0;
3729 +       root = IS_ROOT(file->f_path.dentry);
3730 +       if (root) {
3731 +               get_file(file);
3732 +               to_free[*idx] = file;
3733 +               (*idx)++;
3734 +               goto out;
3735 +       }
3736 +
3737 +       matched = 0;
3738 +       fidir = au_fi(file)->fi_hdir;
3739 +       AuDebugOn(!fidir);
3740 +       bbot = au_fbbot_dir(file);
3741 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3742 +               hfile = fidir->fd_hfile + bindex;
3743 +               if (!hfile->hf_file)
3744 +                       continue;
3745 +
3746 +               if (hfile->hf_br->br_id == br_id) {
3747 +                       matched = 1;
3748 +                       break;
3749 +               }
3750 +       }
3751 +       if (matched)
3752 +               err = -EBUSY;
3753 +
3754 +out:
3755 +       return err;
3756 +}
3757 +
3758 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3759 +                         struct file **to_free, int opened)
3760 +{
3761 +       int err, idx;
3762 +       unsigned long long ull, max;
3763 +       aufs_bindex_t btop;
3764 +       struct file *file, **array;
3765 +       struct dentry *root;
3766 +       struct au_hfile *hfile;
3767 +
3768 +       array = au_farray_alloc(sb, &max);
3769 +       err = PTR_ERR(array);
3770 +       if (IS_ERR(array))
3771 +               goto out;
3772 +
3773 +       err = 0;
3774 +       idx = 0;
3775 +       root = sb->s_root;
3776 +       di_write_unlock(root);
3777 +       for (ull = 0; ull < max; ull++) {
3778 +               file = array[ull];
3779 +               if (unlikely(!file))
3780 +                       break;
3781 +
3782 +               /* AuDbg("%pD\n", file); */
3783 +               fi_read_lock(file);
3784 +               btop = au_fbtop(file);
3785 +               if (!d_is_dir(file->f_path.dentry)) {
3786 +                       hfile = &au_fi(file)->fi_htop;
3787 +                       if (hfile->hf_br->br_id == br_id)
3788 +                               err = -EBUSY;
3789 +               } else
3790 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3791 +               fi_read_unlock(file);
3792 +               if (unlikely(err))
3793 +                       break;
3794 +       }
3795 +       di_write_lock_child(root);
3796 +       au_farray_free(array, max);
3797 +       AuDebugOn(idx > opened);
3798 +
3799 +out:
3800 +       return err;
3801 +}
3802 +
3803 +static void br_del_file(struct file **to_free, unsigned long long opened,
3804 +                       aufs_bindex_t br_id)
3805 +{
3806 +       unsigned long long ull;
3807 +       aufs_bindex_t bindex, btop, bbot, bfound;
3808 +       struct file *file;
3809 +       struct au_fidir *fidir;
3810 +       struct au_hfile *hfile;
3811 +
3812 +       for (ull = 0; ull < opened; ull++) {
3813 +               file = to_free[ull];
3814 +               if (unlikely(!file))
3815 +                       break;
3816 +
3817 +               /* AuDbg("%pD\n", file); */
3818 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3819 +               bfound = -1;
3820 +               fidir = au_fi(file)->fi_hdir;
3821 +               AuDebugOn(!fidir);
3822 +               fi_write_lock(file);
3823 +               btop = au_fbtop(file);
3824 +               bbot = au_fbbot_dir(file);
3825 +               for (bindex = btop; bindex <= bbot; bindex++) {
3826 +                       hfile = fidir->fd_hfile + bindex;
3827 +                       if (!hfile->hf_file)
3828 +                               continue;
3829 +
3830 +                       if (hfile->hf_br->br_id == br_id) {
3831 +                               bfound = bindex;
3832 +                               break;
3833 +                       }
3834 +               }
3835 +               AuDebugOn(bfound < 0);
3836 +               au_set_h_fptr(file, bfound, NULL);
3837 +               if (bfound == btop) {
3838 +                       for (btop++; btop <= bbot; btop++)
3839 +                               if (au_hf_dir(file, btop)) {
3840 +                                       au_set_fbtop(file, btop);
3841 +                                       break;
3842 +                               }
3843 +               }
3844 +               fi_write_unlock(file);
3845 +       }
3846 +}
3847 +
3848 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3849 +                            const aufs_bindex_t bindex,
3850 +                            const aufs_bindex_t bbot)
3851 +{
3852 +       struct au_branch **brp, **p;
3853 +
3854 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3855 +
3856 +       brp = sbinfo->si_branch + bindex;
3857 +       if (bindex < bbot)
3858 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3859 +       sbinfo->si_branch[0 + bbot] = NULL;
3860 +       sbinfo->si_bbot--;
3861 +
3862 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3863 +                       /*may_shrink*/1);
3864 +       if (p)
3865 +               sbinfo->si_branch = p;
3866 +       /* harmless error */
3867 +}
3868 +
3869 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3870 +                            const aufs_bindex_t bbot)
3871 +{
3872 +       struct au_hdentry *hdp, *p;
3873 +
3874 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3875 +
3876 +       hdp = au_hdentry(dinfo, bindex);
3877 +       if (bindex < bbot)
3878 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3879 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3880 +       dinfo->di_bbot--;
3881 +
3882 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3883 +                       /*may_shrink*/1);
3884 +       if (p)
3885 +               dinfo->di_hdentry = p;
3886 +       /* harmless error */
3887 +}
3888 +
3889 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3890 +                            const aufs_bindex_t bbot)
3891 +{
3892 +       struct au_hinode *hip, *p;
3893 +
3894 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3895 +
3896 +       hip = au_hinode(iinfo, bindex);
3897 +       if (bindex < bbot)
3898 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3899 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3900 +       iinfo->ii_bbot--;
3901 +
3902 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3903 +                       /*may_shrink*/1);
3904 +       if (p)
3905 +               iinfo->ii_hinode = p;
3906 +       /* harmless error */
3907 +}
3908 +
3909 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3910 +                        struct au_branch *br)
3911 +{
3912 +       aufs_bindex_t bbot;
3913 +       struct au_sbinfo *sbinfo;
3914 +       struct dentry *root, *h_root;
3915 +       struct inode *inode, *h_inode;
3916 +       struct au_hinode *hinode;
3917 +
3918 +       SiMustWriteLock(sb);
3919 +
3920 +       root = sb->s_root;
3921 +       inode = d_inode(root);
3922 +       sbinfo = au_sbi(sb);
3923 +       bbot = sbinfo->si_bbot;
3924 +
3925 +       h_root = au_h_dptr(root, bindex);
3926 +       hinode = au_hi(inode, bindex);
3927 +       h_inode = au_igrab(hinode->hi_inode);
3928 +       au_hiput(hinode);
3929 +
3930 +       au_sbilist_lock();
3931 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3932 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3933 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3934 +       au_sbilist_unlock();
3935 +
3936 +       /* ignore an error */
3937 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3938 +
3939 +       dput(h_root);
3940 +       iput(h_inode);
3941 +       au_br_do_free(br);
3942 +}
3943 +
3944 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3945 +                                  unsigned long long max, void *arg)
3946 +{
3947 +       return max;
3948 +}
3949 +
3950 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3951 +{
3952 +       int err, rerr, i;
3953 +       unsigned long long opened;
3954 +       unsigned int mnt_flags;
3955 +       aufs_bindex_t bindex, bbot, br_id;
3956 +       unsigned char do_wh, verbose;
3957 +       struct au_branch *br;
3958 +       struct au_wbr *wbr;
3959 +       struct dentry *root;
3960 +       struct file **to_free;
3961 +
3962 +       err = 0;
3963 +       opened = 0;
3964 +       to_free = NULL;
3965 +       root = sb->s_root;
3966 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3967 +       if (bindex < 0) {
3968 +               if (remount)
3969 +                       goto out; /* success */
3970 +               err = -ENOENT;
3971 +               pr_err("%s no such branch\n", del->pathname);
3972 +               goto out;
3973 +       }
3974 +       AuDbg("bindex b%d\n", bindex);
3975 +
3976 +       err = -EBUSY;
3977 +       mnt_flags = au_mntflags(sb);
3978 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3979 +       bbot = au_sbbot(sb);
3980 +       if (unlikely(!bbot)) {
3981 +               AuVerbose(verbose, "no more branches left\n");
3982 +               goto out;
3983 +       }
3984 +
3985 +       br = au_sbr(sb, bindex);
3986 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3987 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3988 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3989 +               goto out;
3990 +       }
3991 +
3992 +       br_id = br->br_id;
3993 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3994 +       if (unlikely(opened)) {
3995 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3996 +               err = PTR_ERR(to_free);
3997 +               if (IS_ERR(to_free))
3998 +                       goto out;
3999 +
4000 +               err = test_file_busy(sb, br_id, to_free, opened);
4001 +               if (unlikely(err)) {
4002 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
4003 +                       goto out;
4004 +               }
4005 +       }
4006 +
4007 +       wbr = br->br_wbr;
4008 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
4009 +       if (do_wh) {
4010 +               /* instead of WbrWhMustWriteLock(wbr) */
4011 +               SiMustWriteLock(sb);
4012 +               for (i = 0; i < AuBrWh_Last; i++) {
4013 +                       dput(wbr->wbr_wh[i]);
4014 +                       wbr->wbr_wh[i] = NULL;
4015 +               }
4016 +       }
4017 +
4018 +       err = test_children_busy(root, bindex, verbose);
4019 +       if (unlikely(err)) {
4020 +               if (do_wh)
4021 +                       goto out_wh;
4022 +               goto out;
4023 +       }
4024 +
4025 +       err = 0;
4026 +       if (to_free) {
4027 +               /*
4028 +                * now we confirmed the branch is deletable.
4029 +                * let's free the remaining opened dirs on the branch.
4030 +                */
4031 +               di_write_unlock(root);
4032 +               br_del_file(to_free, opened, br_id);
4033 +               di_write_lock_child(root);
4034 +       }
4035 +
4036 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4037 +       dbgaufs_xino_del(br);           /* remove one */
4038 +       au_br_do_del(sb, bindex, br);
4039 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4040 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4041 +
4042 +       if (!bindex) {
4043 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4044 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4045 +       } else
4046 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4047 +       if (au_opt_test(mnt_flags, PLINK))
4048 +               au_plink_half_refresh(sb, br_id);
4049 +
4050 +       goto out; /* success */
4051 +
4052 +out_wh:
4053 +       /* revert */
4054 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4055 +       if (rerr)
4056 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4057 +                       del->pathname, rerr);
4058 +out:
4059 +       if (to_free)
4060 +               au_farray_free(to_free, opened);
4061 +       return err;
4062 +}
4063 +
4064 +/* ---------------------------------------------------------------------- */
4065 +
4066 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4067 +{
4068 +       int err;
4069 +       aufs_bindex_t btop, bbot;
4070 +       struct aufs_ibusy ibusy;
4071 +       struct inode *inode, *h_inode;
4072 +
4073 +       err = -EPERM;
4074 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4075 +               goto out;
4076 +
4077 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4078 +       if (!err)
4079 +               /* VERIFY_WRITE */
4080 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4081 +       if (unlikely(err)) {
4082 +               err = -EFAULT;
4083 +               AuTraceErr(err);
4084 +               goto out;
4085 +       }
4086 +
4087 +       err = -EINVAL;
4088 +       si_read_lock(sb, AuLock_FLUSH);
4089 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4090 +               goto out_unlock;
4091 +
4092 +       err = 0;
4093 +       ibusy.h_ino = 0; /* invalid */
4094 +       inode = ilookup(sb, ibusy.ino);
4095 +       if (!inode
4096 +           || inode->i_ino == AUFS_ROOT_INO
4097 +           || au_is_bad_inode(inode))
4098 +               goto out_unlock;
4099 +
4100 +       ii_read_lock_child(inode);
4101 +       btop = au_ibtop(inode);
4102 +       bbot = au_ibbot(inode);
4103 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4104 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4105 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4106 +                       ibusy.h_ino = h_inode->i_ino;
4107 +       }
4108 +       ii_read_unlock(inode);
4109 +       iput(inode);
4110 +
4111 +out_unlock:
4112 +       si_read_unlock(sb);
4113 +       if (!err) {
4114 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4115 +               if (unlikely(err)) {
4116 +                       err = -EFAULT;
4117 +                       AuTraceErr(err);
4118 +               }
4119 +       }
4120 +out:
4121 +       return err;
4122 +}
4123 +
4124 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4125 +{
4126 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4127 +}
4128 +
4129 +#ifdef CONFIG_COMPAT
4130 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4131 +{
4132 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4133 +}
4134 +#endif
4135 +
4136 +/* ---------------------------------------------------------------------- */
4137 +
4138 +/*
4139 + * change a branch permission
4140 + */
4141 +
4142 +static void au_warn_ima(void)
4143 +{
4144 +#ifdef CONFIG_IMA
4145 +       /* since it doesn't support mark_files_ro() */
4146 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4147 +#endif
4148 +}
4149 +
4150 +static int do_need_sigen_inc(int a, int b)
4151 +{
4152 +       return au_br_whable(a) && !au_br_whable(b);
4153 +}
4154 +
4155 +static int need_sigen_inc(int old, int new)
4156 +{
4157 +       return do_need_sigen_inc(old, new)
4158 +               || do_need_sigen_inc(new, old);
4159 +}
4160 +
4161 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4162 +{
4163 +       int err, do_warn;
4164 +       unsigned int mnt_flags;
4165 +       unsigned long long ull, max;
4166 +       aufs_bindex_t br_id;
4167 +       unsigned char verbose, writer;
4168 +       struct file *file, *hf, **array;
4169 +       struct au_hfile *hfile;
4170 +       struct inode *h_inode;
4171 +
4172 +       mnt_flags = au_mntflags(sb);
4173 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4174 +
4175 +       array = au_farray_alloc(sb, &max);
4176 +       err = PTR_ERR(array);
4177 +       if (IS_ERR(array))
4178 +               goto out;
4179 +
4180 +       do_warn = 0;
4181 +       br_id = au_sbr_id(sb, bindex);
4182 +       for (ull = 0; ull < max; ull++) {
4183 +               file = array[ull];
4184 +               if (unlikely(!file))
4185 +                       break;
4186 +
4187 +               /* AuDbg("%pD\n", file); */
4188 +               fi_read_lock(file);
4189 +               if (unlikely(au_test_mmapped(file))) {
4190 +                       err = -EBUSY;
4191 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4192 +                       AuDbgFile(file);
4193 +                       FiMustNoWaiters(file);
4194 +                       fi_read_unlock(file);
4195 +                       goto out_array;
4196 +               }
4197 +
4198 +               hfile = &au_fi(file)->fi_htop;
4199 +               hf = hfile->hf_file;
4200 +               if (!d_is_reg(file->f_path.dentry)
4201 +                   || !(file->f_mode & FMODE_WRITE)
4202 +                   || hfile->hf_br->br_id != br_id
4203 +                   || !(hf->f_mode & FMODE_WRITE))
4204 +                       array[ull] = NULL;
4205 +               else {
4206 +                       do_warn = 1;
4207 +                       get_file(file);
4208 +               }
4209 +
4210 +               FiMustNoWaiters(file);
4211 +               fi_read_unlock(file);
4212 +               fput(file);
4213 +       }
4214 +
4215 +       err = 0;
4216 +       if (do_warn)
4217 +               au_warn_ima();
4218 +
4219 +       for (ull = 0; ull < max; ull++) {
4220 +               file = array[ull];
4221 +               if (!file)
4222 +                       continue;
4223 +
4224 +               /* todo: already flushed? */
4225 +               /*
4226 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4227 +                * approach which resets f_mode and calls mnt_drop_write() and
4228 +                * file_release_write() for each file, because the branch
4229 +                * attribute in aufs world is totally different from the native
4230 +                * fs rw/ro mode.
4231 +               */
4232 +               /* fi_read_lock(file); */
4233 +               hfile = &au_fi(file)->fi_htop;
4234 +               hf = hfile->hf_file;
4235 +               /* fi_read_unlock(file); */
4236 +               spin_lock(&hf->f_lock);
4237 +               writer = !!(hf->f_mode & FMODE_WRITER);
4238 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4239 +               spin_unlock(&hf->f_lock);
4240 +               if (writer) {
4241 +                       h_inode = file_inode(hf);
4242 +                       if (hf->f_mode & FMODE_READ)
4243 +                               i_readcount_inc(h_inode);
4244 +                       put_write_access(h_inode);
4245 +                       __mnt_drop_write(hf->f_path.mnt);
4246 +               }
4247 +       }
4248 +
4249 +out_array:
4250 +       au_farray_free(array, max);
4251 +out:
4252 +       AuTraceErr(err);
4253 +       return err;
4254 +}
4255 +
4256 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4257 +             int *do_refresh)
4258 +{
4259 +       int err, rerr;
4260 +       aufs_bindex_t bindex;
4261 +       struct dentry *root;
4262 +       struct au_branch *br;
4263 +       struct au_br_fhsm *bf;
4264 +
4265 +       root = sb->s_root;
4266 +       bindex = au_find_dbindex(root, mod->h_root);
4267 +       if (bindex < 0) {
4268 +               if (remount)
4269 +                       return 0; /* success */
4270 +               err = -ENOENT;
4271 +               pr_err("%s no such branch\n", mod->path);
4272 +               goto out;
4273 +       }
4274 +       AuDbg("bindex b%d\n", bindex);
4275 +
4276 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4277 +       if (unlikely(err))
4278 +               goto out;
4279 +
4280 +       br = au_sbr(sb, bindex);
4281 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4282 +       if (br->br_perm == mod->perm)
4283 +               return 0; /* success */
4284 +
4285 +       /* pre-allocate for non-fhsm --> fhsm */
4286 +       bf = NULL;
4287 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4288 +               err = au_fhsm_br_alloc(br);
4289 +               if (unlikely(err))
4290 +                       goto out;
4291 +               bf = br->br_fhsm;
4292 +               br->br_fhsm = NULL;
4293 +       }
4294 +
4295 +       if (au_br_writable(br->br_perm)) {
4296 +               /* remove whiteout base */
4297 +               err = au_br_init_wh(sb, br, mod->perm);
4298 +               if (unlikely(err))
4299 +                       goto out_bf;
4300 +
4301 +               if (!au_br_writable(mod->perm)) {
4302 +                       /* rw --> ro, file might be mmapped */
4303 +                       DiMustNoWaiters(root);
4304 +                       IiMustNoWaiters(d_inode(root));
4305 +                       di_write_unlock(root);
4306 +                       err = au_br_mod_files_ro(sb, bindex);
4307 +                       /* aufs_write_lock() calls ..._child() */
4308 +                       di_write_lock_child(root);
4309 +
4310 +                       if (unlikely(err)) {
4311 +                               rerr = -ENOMEM;
4312 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4313 +                                                    GFP_NOFS);
4314 +                               if (br->br_wbr)
4315 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4316 +                               if (unlikely(rerr)) {
4317 +                                       AuIOErr("nested error %d (%d)\n",
4318 +                                               rerr, err);
4319 +                                       br->br_perm = mod->perm;
4320 +                               }
4321 +                       }
4322 +               }
4323 +       } else if (au_br_writable(mod->perm)) {
4324 +               /* ro --> rw */
4325 +               err = -ENOMEM;
4326 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4327 +               if (br->br_wbr) {
4328 +                       err = au_wbr_init(br, sb, mod->perm);
4329 +                       if (unlikely(err)) {
4330 +                               au_kfree_rcu(br->br_wbr);
4331 +                               br->br_wbr = NULL;
4332 +                       }
4333 +               }
4334 +       }
4335 +       if (unlikely(err))
4336 +               goto out_bf;
4337 +
4338 +       if (au_br_fhsm(br->br_perm)) {
4339 +               if (!au_br_fhsm(mod->perm)) {
4340 +                       /* fhsm --> non-fhsm */
4341 +                       au_br_fhsm_fin(br->br_fhsm);
4342 +                       au_kfree_rcu(br->br_fhsm);
4343 +                       br->br_fhsm = NULL;
4344 +               }
4345 +       } else if (au_br_fhsm(mod->perm))
4346 +               /* non-fhsm --> fhsm */
4347 +               br->br_fhsm = bf;
4348 +
4349 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4350 +       br->br_perm = mod->perm;
4351 +       goto out; /* success */
4352 +
4353 +out_bf:
4354 +       au_kfree_try_rcu(bf);
4355 +out:
4356 +       AuTraceErr(err);
4357 +       return err;
4358 +}
4359 +
4360 +/* ---------------------------------------------------------------------- */
4361 +
4362 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4363 +{
4364 +       int err;
4365 +       struct kstatfs kstfs;
4366 +
4367 +       err = vfs_statfs(&br->br_path, &kstfs);
4368 +       if (!err) {
4369 +               stfs->f_blocks = kstfs.f_blocks;
4370 +               stfs->f_bavail = kstfs.f_bavail;
4371 +               stfs->f_files = kstfs.f_files;
4372 +               stfs->f_ffree = kstfs.f_ffree;
4373 +       }
4374 +
4375 +       return err;
4376 +}
4377 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4378 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4379 +++ linux/fs/aufs/branch.h      2020-12-15 14:10:58.911356294 +0100
4380 @@ -0,0 +1,364 @@
4381 +/* SPDX-License-Identifier: GPL-2.0 */
4382 +/*
4383 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4384 + *
4385 + * This program, aufs is free software; you can redistribute it and/or modify
4386 + * it under the terms of the GNU General Public License as published by
4387 + * the Free Software Foundation; either version 2 of the License, or
4388 + * (at your option) any later version.
4389 + *
4390 + * This program is distributed in the hope that it will be useful,
4391 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4392 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4393 + * GNU General Public License for more details.
4394 + *
4395 + * You should have received a copy of the GNU General Public License
4396 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4397 + */
4398 +
4399 +/*
4400 + * branch filesystems and xino for them
4401 + */
4402 +
4403 +#ifndef __AUFS_BRANCH_H__
4404 +#define __AUFS_BRANCH_H__
4405 +
4406 +#ifdef __KERNEL__
4407 +
4408 +#include <linux/mount.h>
4409 +#include "dirren.h"
4410 +#include "dynop.h"
4411 +#include "lcnt.h"
4412 +#include "rwsem.h"
4413 +#include "super.h"
4414 +
4415 +/* ---------------------------------------------------------------------- */
4416 +
4417 +/* a xino file */
4418 +struct au_xino {
4419 +       struct file             **xi_file;
4420 +       unsigned int            xi_nfile;
4421 +
4422 +       struct {
4423 +               spinlock_t              spin;
4424 +               ino_t                   *array;
4425 +               int                     total;
4426 +               /* reserved for future use */
4427 +               /* unsigned long        *bitmap; */
4428 +               wait_queue_head_t       wqh;
4429 +       } xi_nondir;
4430 +
4431 +       struct mutex            xi_mtx; /* protects xi_file array */
4432 +       struct hlist_bl_head    xi_writing;
4433 +
4434 +       atomic_t                xi_truncating;
4435 +
4436 +       struct kref             xi_kref;
4437 +};
4438 +
4439 +/* File-based Hierarchical Storage Management */
4440 +struct au_br_fhsm {
4441 +#ifdef CONFIG_AUFS_FHSM
4442 +       struct mutex            bf_lock;
4443 +       unsigned long           bf_jiffy;
4444 +       struct aufs_stfs        bf_stfs;
4445 +       int                     bf_readable;
4446 +#endif
4447 +};
4448 +
4449 +/* members for writable branch only */
4450 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4451 +struct au_wbr {
4452 +       struct au_rwsem         wbr_wh_rwsem;
4453 +       struct dentry           *wbr_wh[AuBrWh_Last];
4454 +       atomic_t                wbr_wh_running;
4455 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4456 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4457 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4458 +
4459 +       /* mfs mode */
4460 +       unsigned long long      wbr_bytes;
4461 +};
4462 +
4463 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4464 +#define AuBrDynOp (AuDyLast * 4)
4465 +
4466 +#ifdef CONFIG_AUFS_HFSNOTIFY
4467 +/* support for asynchronous destruction */
4468 +struct au_br_hfsnotify {
4469 +       struct fsnotify_group   *hfsn_group;
4470 +};
4471 +#endif
4472 +
4473 +/* sysfs entries */
4474 +struct au_brsysfs {
4475 +       char                    name[16];
4476 +       struct attribute        attr;
4477 +};
4478 +
4479 +enum {
4480 +       AuBrSysfs_BR,
4481 +       AuBrSysfs_BRID,
4482 +       AuBrSysfs_Last
4483 +};
4484 +
4485 +/* protected by superblock rwsem */
4486 +struct au_branch {
4487 +       struct au_xino          *br_xino;
4488 +
4489 +       aufs_bindex_t           br_id;
4490 +
4491 +       int                     br_perm;
4492 +       struct path             br_path;
4493 +       spinlock_t              br_dykey_lock;
4494 +       struct au_dykey         *br_dykey[AuBrDynOp];
4495 +       au_lcnt_t               br_nfiles;      /* opened files */
4496 +       au_lcnt_t               br_count;       /* in-use for other */
4497 +
4498 +       struct au_wbr           *br_wbr;
4499 +       struct au_br_fhsm       *br_fhsm;
4500 +
4501 +#ifdef CONFIG_AUFS_HFSNOTIFY
4502 +       struct au_br_hfsnotify  *br_hfsn;
4503 +#endif
4504 +
4505 +#ifdef CONFIG_SYSFS
4506 +       /* entries under sysfs per mount-point */
4507 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4508 +#endif
4509 +
4510 +#ifdef CONFIG_DEBUG_FS
4511 +       struct dentry            *br_dbgaufs; /* xino */
4512 +#endif
4513 +
4514 +       struct au_dr_br         br_dirren;
4515 +};
4516 +
4517 +/* ---------------------------------------------------------------------- */
4518 +
4519 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4520 +{
4521 +       return br->br_path.mnt;
4522 +}
4523 +
4524 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4525 +{
4526 +       return br->br_path.dentry;
4527 +}
4528 +
4529 +static inline struct super_block *au_br_sb(struct au_branch *br)
4530 +{
4531 +       return au_br_mnt(br)->mnt_sb;
4532 +}
4533 +
4534 +static inline int au_br_rdonly(struct au_branch *br)
4535 +{
4536 +       return (sb_rdonly(au_br_sb(br))
4537 +               || !au_br_writable(br->br_perm))
4538 +               ? -EROFS : 0;
4539 +}
4540 +
4541 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4542 +{
4543 +#ifdef CONFIG_AUFS_HNOTIFY
4544 +       return !(brperm & AuBrPerm_RR);
4545 +#else
4546 +       return 0;
4547 +#endif
4548 +}
4549 +
4550 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4551 +{
4552 +       int err, exec_flag;
4553 +
4554 +       err = 0;
4555 +       exec_flag = oflag & __FMODE_EXEC;
4556 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4557 +               err = -EACCES;
4558 +
4559 +       return err;
4560 +}
4561 +
4562 +static inline void au_xino_get(struct au_branch *br)
4563 +{
4564 +       struct au_xino *xi;
4565 +
4566 +       xi = br->br_xino;
4567 +       if (xi)
4568 +               kref_get(&xi->xi_kref);
4569 +}
4570 +
4571 +static inline int au_xino_count(struct au_branch *br)
4572 +{
4573 +       int v;
4574 +       struct au_xino *xi;
4575 +
4576 +       v = 0;
4577 +       xi = br->br_xino;
4578 +       if (xi)
4579 +               v = kref_read(&xi->xi_kref);
4580 +
4581 +       return v;
4582 +}
4583 +
4584 +/* ---------------------------------------------------------------------- */
4585 +
4586 +/* branch.c */
4587 +struct au_sbinfo;
4588 +void au_br_free(struct au_sbinfo *sinfo);
4589 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4590 +struct au_opt_add;
4591 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4592 +struct au_opt_del;
4593 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4594 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4595 +#ifdef CONFIG_COMPAT
4596 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4597 +#endif
4598 +struct au_opt_mod;
4599 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4600 +             int *do_refresh);
4601 +struct aufs_stfs;
4602 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4603 +
4604 +/* xino.c */
4605 +static const loff_t au_loff_max = LLONG_MAX;
4606 +
4607 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4608 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4609 +                           int wbrtop);
4610 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4611 +                            struct file *copy_src);
4612 +struct au_xi_new {
4613 +       struct au_xino *xi;     /* switch between xino and xigen */
4614 +       int idx;
4615 +       struct path *base;
4616 +       struct file *copy_src;
4617 +};
4618 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4619 +
4620 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4621 +                ino_t *ino);
4622 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4623 +                 ino_t ino);
4624 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4625 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4626 +
4627 +int au_xib_trunc(struct super_block *sb);
4628 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4629 +
4630 +struct au_xino *au_xino_alloc(unsigned int nfile);
4631 +int au_xino_put(struct au_branch *br);
4632 +struct file *au_xino_file1(struct au_xino *xi);
4633 +
4634 +struct au_opt_xino;
4635 +void au_xino_clr(struct super_block *sb);
4636 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4637 +struct file *au_xino_def(struct super_block *sb);
4638 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4639 +                   struct path *base);
4640 +
4641 +ino_t au_xino_new_ino(struct super_block *sb);
4642 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4643 +
4644 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4645 +                      ino_t h_ino, int idx);
4646 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4647 +                     int *idx);
4648 +
4649 +int au_xino_path(struct seq_file *seq, struct file *file);
4650 +
4651 +/* ---------------------------------------------------------------------- */
4652 +
4653 +/* @idx is signed to accept -1 meaning the first file */
4654 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4655 +{
4656 +       struct file *file;
4657 +
4658 +       file = NULL;
4659 +       if (!xi)
4660 +               goto out;
4661 +
4662 +       if (idx >= 0) {
4663 +               if (idx < xi->xi_nfile)
4664 +                       file = xi->xi_file[idx];
4665 +       } else
4666 +               file = au_xino_file1(xi);
4667 +
4668 +out:
4669 +       return file;
4670 +}
4671 +
4672 +/* ---------------------------------------------------------------------- */
4673 +
4674 +/* Superblock to branch */
4675 +static inline
4676 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4677 +{
4678 +       return au_sbr(sb, bindex)->br_id;
4679 +}
4680 +
4681 +static inline
4682 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4683 +{
4684 +       return au_br_mnt(au_sbr(sb, bindex));
4685 +}
4686 +
4687 +static inline
4688 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4689 +{
4690 +       return au_br_sb(au_sbr(sb, bindex));
4691 +}
4692 +
4693 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4694 +{
4695 +       return au_sbr(sb, bindex)->br_perm;
4696 +}
4697 +
4698 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4699 +{
4700 +       return au_br_whable(au_sbr_perm(sb, bindex));
4701 +}
4702 +
4703 +/* ---------------------------------------------------------------------- */
4704 +
4705 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4706 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4707 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4708 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4709 +/*
4710 +#define wbr_wh_read_trylock_nested(wbr) \
4711 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4712 +#define wbr_wh_write_trylock_nested(wbr) \
4713 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4714 +*/
4715 +
4716 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4717 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4718 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4719 +
4720 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4721 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4722 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4723 +
4724 +/* ---------------------------------------------------------------------- */
4725 +
4726 +#ifdef CONFIG_AUFS_FHSM
4727 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4728 +{
4729 +       mutex_init(&brfhsm->bf_lock);
4730 +       brfhsm->bf_jiffy = 0;
4731 +       brfhsm->bf_readable = 0;
4732 +}
4733 +
4734 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4735 +{
4736 +       mutex_destroy(&brfhsm->bf_lock);
4737 +}
4738 +#else
4739 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4740 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4741 +#endif
4742 +
4743 +#endif /* __KERNEL__ */
4744 +#endif /* __AUFS_BRANCH_H__ */
4745 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4746 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4747 +++ linux/fs/aufs/conf.mk       2019-07-11 15:42:14.462237786 +0200
4748 @@ -0,0 +1,40 @@
4749 +# SPDX-License-Identifier: GPL-2.0
4750 +
4751 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4752 +
4753 +define AuConf
4754 +ifdef ${1}
4755 +AuConfStr += ${1}=${${1}}
4756 +endif
4757 +endef
4758 +
4759 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4760 +       SBILIST \
4761 +       HNOTIFY HFSNOTIFY \
4762 +       EXPORT INO_T_64 \
4763 +       XATTR \
4764 +       FHSM \
4765 +       RDU \
4766 +       DIRREN \
4767 +       SHWH \
4768 +       BR_RAMFS \
4769 +       BR_FUSE POLL \
4770 +       BR_HFSPLUS \
4771 +       BDEV_LOOP \
4772 +       DEBUG MAGIC_SYSRQ
4773 +$(foreach i, ${AuConfAll}, \
4774 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4775 +
4776 +AuConfName = ${obj}/conf.str
4777 +${AuConfName}.tmp: FORCE
4778 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4779 +${AuConfName}: ${AuConfName}.tmp
4780 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4781 +       echo '  GEN    ' $@; \
4782 +       cp -p $< $@; \
4783 +       }
4784 +FORCE:
4785 +clean-files += ${AuConfName} ${AuConfName}.tmp
4786 +${obj}/sysfs.o: ${AuConfName}
4787 +
4788 +-include ${srctree}/${src}/conf_priv.mk
4789 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4790 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4791 +++ linux/fs/aufs/cpup.c        2020-12-15 14:10:58.911356294 +0100
4792 @@ -0,0 +1,1445 @@
4793 +// SPDX-License-Identifier: GPL-2.0
4794 +/*
4795 + * Copyright (C) 2005-2020 Junjiro R. Okajima
4796 + *
4797 + * This program, aufs is free software; you can redistribute it and/or modify
4798 + * it under the terms of the GNU General Public License as published by
4799 + * the Free Software Foundation; either version 2 of the License, or
4800 + * (at your option) any later version.
4801 + *
4802 + * This program is distributed in the hope that it will be useful,
4803 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4804 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4805 + * GNU General Public License for more details.
4806 + *
4807 + * You should have received a copy of the GNU General Public License
4808 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4809 + */
4810 +
4811 +/*
4812 + * copy-up functions, see wbr_policy.c for copy-down
4813 + */
4814 +
4815 +#include <linux/fs_stack.h>
4816 +#include <linux/mm.h>
4817 +#include <linux/task_work.h>
4818 +#include "aufs.h"
4819 +
4820 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4821 +{
4822 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4823 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4824 +
4825 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4826 +
4827 +       dst->i_flags |= iflags & ~mask;
4828 +       if (au_test_fs_notime(dst->i_sb))
4829 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4830 +}
4831 +
4832 +void au_cpup_attr_timesizes(struct inode *inode)
4833 +{
4834 +       struct inode *h_inode;
4835 +
4836 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4837 +       fsstack_copy_attr_times(inode, h_inode);
4838 +       fsstack_copy_inode_size(inode, h_inode);
4839 +}
4840 +
4841 +void au_cpup_attr_nlink(struct inode *inode, int force)
4842 +{
4843 +       struct inode *h_inode;
4844 +       struct super_block *sb;
4845 +       aufs_bindex_t bindex, bbot;
4846 +
4847 +       sb = inode->i_sb;
4848 +       bindex = au_ibtop(inode);
4849 +       h_inode = au_h_iptr(inode, bindex);
4850 +       if (!force
4851 +           && !S_ISDIR(h_inode->i_mode)
4852 +           && au_opt_test(au_mntflags(sb), PLINK)
4853 +           && au_plink_test(inode))
4854 +               return;
4855 +
4856 +       /*
4857 +        * 0 can happen in revalidating.
4858 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4859 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4860 +        * case.
4861 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4862 +        *       the incorrect link count.
4863 +        */
4864 +       set_nlink(inode, h_inode->i_nlink);
4865 +
4866 +       /*
4867 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4868 +        * it may includes whplink directory.
4869 +        */
4870 +       if (S_ISDIR(h_inode->i_mode)) {
4871 +               bbot = au_ibbot(inode);
4872 +               for (bindex++; bindex <= bbot; bindex++) {
4873 +                       h_inode = au_h_iptr(inode, bindex);
4874 +                       if (h_inode)
4875 +                               au_add_nlink(inode, h_inode);
4876 +               }
4877 +       }
4878 +}
4879 +
4880 +void au_cpup_attr_changeable(struct inode *inode)
4881 +{
4882 +       struct inode *h_inode;
4883 +
4884 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4885 +       inode->i_mode = h_inode->i_mode;
4886 +       inode->i_uid = h_inode->i_uid;
4887 +       inode->i_gid = h_inode->i_gid;
4888 +       au_cpup_attr_timesizes(inode);
4889 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4890 +}
4891 +
4892 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4893 +{
4894 +       struct au_iinfo *iinfo = au_ii(inode);
4895 +
4896 +       IiMustWriteLock(inode);
4897 +
4898 +       iinfo->ii_higen = h_inode->i_generation;
4899 +       iinfo->ii_hsb1 = h_inode->i_sb;
4900 +}
4901 +
4902 +void au_cpup_attr_all(struct inode *inode, int force)
4903 +{
4904 +       struct inode *h_inode;
4905 +
4906 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4907 +       au_cpup_attr_changeable(inode);
4908 +       if (inode->i_nlink > 0)
4909 +               au_cpup_attr_nlink(inode, force);
4910 +       inode->i_rdev = h_inode->i_rdev;
4911 +       inode->i_blkbits = h_inode->i_blkbits;
4912 +       au_cpup_igen(inode, h_inode);
4913 +}
4914 +
4915 +/* ---------------------------------------------------------------------- */
4916 +
4917 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4918 +
4919 +/* keep the timestamps of the parent dir when cpup */
4920 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4921 +                   struct path *h_path)
4922 +{
4923 +       struct inode *h_inode;
4924 +
4925 +       dt->dt_dentry = dentry;
4926 +       dt->dt_h_path = *h_path;
4927 +       h_inode = d_inode(h_path->dentry);
4928 +       dt->dt_atime = h_inode->i_atime;
4929 +       dt->dt_mtime = h_inode->i_mtime;
4930 +       /* smp_mb(); */
4931 +}
4932 +
4933 +void au_dtime_revert(struct au_dtime *dt)
4934 +{
4935 +       struct iattr attr;
4936 +       int err;
4937 +
4938 +       attr.ia_atime = dt->dt_atime;
4939 +       attr.ia_mtime = dt->dt_mtime;
4940 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4941 +               | ATTR_ATIME | ATTR_ATIME_SET;
4942 +
4943 +       /* no delegation since this is a directory */
4944 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4945 +       if (unlikely(err))
4946 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4947 +}
4948 +
4949 +/* ---------------------------------------------------------------------- */
4950 +
4951 +/* internal use only */
4952 +struct au_cpup_reg_attr {
4953 +       int             valid;
4954 +       struct kstat    st;
4955 +       unsigned int    iflags; /* inode->i_flags */
4956 +};
4957 +
4958 +static noinline_for_stack
4959 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4960 +              struct au_cpup_reg_attr *h_src_attr)
4961 +{
4962 +       int err, sbits, icex;
4963 +       unsigned int mnt_flags;
4964 +       unsigned char verbose;
4965 +       struct iattr ia;
4966 +       struct path h_path;
4967 +       struct inode *h_isrc, *h_idst;
4968 +       struct kstat *h_st;
4969 +       struct au_branch *br;
4970 +
4971 +       h_path.dentry = au_h_dptr(dst, bindex);
4972 +       h_idst = d_inode(h_path.dentry);
4973 +       br = au_sbr(dst->d_sb, bindex);
4974 +       h_path.mnt = au_br_mnt(br);
4975 +       h_isrc = d_inode(h_src);
4976 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4977 +               | ATTR_ATIME | ATTR_MTIME
4978 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4979 +       if (h_src_attr && h_src_attr->valid) {
4980 +               h_st = &h_src_attr->st;
4981 +               ia.ia_uid = h_st->uid;
4982 +               ia.ia_gid = h_st->gid;
4983 +               ia.ia_atime = h_st->atime;
4984 +               ia.ia_mtime = h_st->mtime;
4985 +               if (h_idst->i_mode != h_st->mode
4986 +                   && !S_ISLNK(h_idst->i_mode)) {
4987 +                       ia.ia_valid |= ATTR_MODE;
4988 +                       ia.ia_mode = h_st->mode;
4989 +               }
4990 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4991 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4992 +       } else {
4993 +               ia.ia_uid = h_isrc->i_uid;
4994 +               ia.ia_gid = h_isrc->i_gid;
4995 +               ia.ia_atime = h_isrc->i_atime;
4996 +               ia.ia_mtime = h_isrc->i_mtime;
4997 +               if (h_idst->i_mode != h_isrc->i_mode
4998 +                   && !S_ISLNK(h_idst->i_mode)) {
4999 +                       ia.ia_valid |= ATTR_MODE;
5000 +                       ia.ia_mode = h_isrc->i_mode;
5001 +               }
5002 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5003 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5004 +       }
5005 +       /* no delegation since it is just created */
5006 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5007 +
5008 +       /* is this nfs only? */
5009 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5010 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5011 +               ia.ia_mode = h_isrc->i_mode;
5012 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5013 +       }
5014 +
5015 +       icex = br->br_perm & AuBrAttr_ICEX;
5016 +       if (!err) {
5017 +               mnt_flags = au_mntflags(dst->d_sb);
5018 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5019 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
5020 +       }
5021 +
5022 +       return err;
5023 +}
5024 +
5025 +/* ---------------------------------------------------------------------- */
5026 +
5027 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5028 +                          char *buf, unsigned long blksize)
5029 +{
5030 +       int err;
5031 +       size_t sz, rbytes, wbytes;
5032 +       unsigned char all_zero;
5033 +       char *p, *zp;
5034 +       struct inode *h_inode;
5035 +       /* reduce stack usage */
5036 +       struct iattr *ia;
5037 +
5038 +       zp = page_address(ZERO_PAGE(0));
5039 +       if (unlikely(!zp))
5040 +               return -ENOMEM; /* possible? */
5041 +
5042 +       err = 0;
5043 +       all_zero = 0;
5044 +       while (len) {
5045 +               AuDbg("len %lld\n", len);
5046 +               sz = blksize;
5047 +               if (len < blksize)
5048 +                       sz = len;
5049 +
5050 +               rbytes = 0;
5051 +               /* todo: signal_pending? */
5052 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5053 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5054 +                       err = rbytes;
5055 +               }
5056 +               if (unlikely(err < 0))
5057 +                       break;
5058 +
5059 +               all_zero = 0;
5060 +               if (len >= rbytes && rbytes == blksize)
5061 +                       all_zero = !memcmp(buf, zp, rbytes);
5062 +               if (!all_zero) {
5063 +                       wbytes = rbytes;
5064 +                       p = buf;
5065 +                       while (wbytes) {
5066 +                               size_t b;
5067 +
5068 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5069 +                               err = b;
5070 +                               /* todo: signal_pending? */
5071 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5072 +                                       continue;
5073 +                               if (unlikely(err < 0))
5074 +                                       break;
5075 +                               wbytes -= b;
5076 +                               p += b;
5077 +                       }
5078 +                       if (unlikely(err < 0))
5079 +                               break;
5080 +               } else {
5081 +                       loff_t res;
5082 +
5083 +                       AuLabel(hole);
5084 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5085 +                       err = res;
5086 +                       if (unlikely(res < 0))
5087 +                               break;
5088 +               }
5089 +               len -= rbytes;
5090 +               err = 0;
5091 +       }
5092 +
5093 +       /* the last block may be a hole */
5094 +       if (!err && all_zero) {
5095 +               AuLabel(last hole);
5096 +
5097 +               err = 1;
5098 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5099 +                       /* nfs requires this step to make last hole */
5100 +                       /* is this only nfs? */
5101 +                       do {
5102 +                               /* todo: signal_pending? */
5103 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5104 +                       } while (err == -EAGAIN || err == -EINTR);
5105 +                       if (err == 1)
5106 +                               dst->f_pos--;
5107 +               }
5108 +
5109 +               if (err == 1) {
5110 +                       ia = (void *)buf;
5111 +                       ia->ia_size = dst->f_pos;
5112 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5113 +                       ia->ia_file = dst;
5114 +                       h_inode = file_inode(dst);
5115 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5116 +                       /* no delegation since it is just created */
5117 +                       err = vfsub_notify_change(&dst->f_path, ia,
5118 +                                                 /*delegated*/NULL);
5119 +                       inode_unlock(h_inode);
5120 +               }
5121 +       }
5122 +
5123 +       return err;
5124 +}
5125 +
5126 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5127 +{
5128 +       int err;
5129 +       unsigned long blksize;
5130 +       unsigned char do_kfree;
5131 +       char *buf;
5132 +       struct super_block *h_sb;
5133 +
5134 +       err = -ENOMEM;
5135 +       h_sb = file_inode(dst)->i_sb;
5136 +       blksize = h_sb->s_blocksize;
5137 +       if (!blksize || PAGE_SIZE < blksize)
5138 +               blksize = PAGE_SIZE;
5139 +       AuDbg("blksize %lu\n", blksize);
5140 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5141 +       if (do_kfree)
5142 +               buf = kmalloc(blksize, GFP_NOFS);
5143 +       else
5144 +               buf = (void *)__get_free_page(GFP_NOFS);
5145 +       if (unlikely(!buf))
5146 +               goto out;
5147 +
5148 +       if (len > (1 << 22))
5149 +               AuDbg("copying a large file %lld\n", (long long)len);
5150 +
5151 +       src->f_pos = 0;
5152 +       dst->f_pos = 0;
5153 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5154 +       if (do_kfree) {
5155 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5156 +               au_kfree_do_rcu(buf);
5157 +       } else
5158 +               free_page((unsigned long)buf);
5159 +
5160 +out:
5161 +       return err;
5162 +}
5163 +
5164 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5165 +{
5166 +       int err;
5167 +       struct super_block *h_src_sb;
5168 +       struct inode *h_src_inode;
5169 +
5170 +       h_src_inode = file_inode(src);
5171 +       h_src_sb = h_src_inode->i_sb;
5172 +
5173 +       /* XFS acquires inode_lock */
5174 +       if (!au_test_xfs(h_src_sb))
5175 +               err = au_copy_file(dst, src, len);
5176 +       else {
5177 +               inode_unlock_shared(h_src_inode);
5178 +               err = au_copy_file(dst, src, len);
5179 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5180 +       }
5181 +
5182 +       return err;
5183 +}
5184 +
5185 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5186 +{
5187 +       int err;
5188 +       loff_t lo;
5189 +       struct super_block *h_src_sb;
5190 +       struct inode *h_src_inode;
5191 +
5192 +       h_src_inode = file_inode(src);
5193 +       h_src_sb = h_src_inode->i_sb;
5194 +       if (h_src_sb != file_inode(dst)->i_sb
5195 +           || !dst->f_op->remap_file_range) {
5196 +               err = au_do_copy(dst, src, len);
5197 +               goto out;
5198 +       }
5199 +
5200 +       if (!au_test_nfs(h_src_sb)) {
5201 +               inode_unlock_shared(h_src_inode);
5202 +               lo = vfsub_clone_file_range(src, dst, len);
5203 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5204 +       } else
5205 +               lo = vfsub_clone_file_range(src, dst, len);
5206 +       if (lo == len) {
5207 +               err = 0;
5208 +               goto out; /* success */
5209 +       } else if (lo >= 0)
5210 +               /* todo: possible? */
5211 +               /* paritially succeeded */
5212 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5213 +       else if (lo != -EOPNOTSUPP) {
5214 +               /* older XFS has a condition in cloning */
5215 +               err = lo;
5216 +               goto out;
5217 +       }
5218 +
5219 +       /* the backend fs on NFS may not support cloning */
5220 +       err = au_do_copy(dst, src, len);
5221 +
5222 +out:
5223 +       AuTraceErr(err);
5224 +       return err;
5225 +}
5226 +
5227 +/*
5228 + * to support a sparse file which is opened with O_APPEND,
5229 + * we need to close the file.
5230 + */
5231 +static int au_cp_regular(struct au_cp_generic *cpg)
5232 +{
5233 +       int err, i;
5234 +       enum { SRC, DST };
5235 +       struct {
5236 +               aufs_bindex_t bindex;
5237 +               unsigned int flags;
5238 +               struct dentry *dentry;
5239 +               int force_wr;
5240 +               struct file *file;
5241 +       } *f, file[] = {
5242 +               {
5243 +                       .bindex = cpg->bsrc,
5244 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5245 +               },
5246 +               {
5247 +                       .bindex = cpg->bdst,
5248 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5249 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5250 +               }
5251 +       };
5252 +       struct au_branch *br;
5253 +       struct super_block *sb, *h_src_sb;
5254 +       struct inode *h_src_inode;
5255 +       struct task_struct *tsk = current;
5256 +
5257 +       /* bsrc branch can be ro/rw. */
5258 +       sb = cpg->dentry->d_sb;
5259 +       f = file;
5260 +       for (i = 0; i < 2; i++, f++) {
5261 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5262 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5263 +                                   /*file*/NULL, f->force_wr);
5264 +               if (IS_ERR(f->file)) {
5265 +                       err = PTR_ERR(f->file);
5266 +                       if (i == SRC)
5267 +                               goto out;
5268 +                       else
5269 +                               goto out_src;
5270 +               }
5271 +       }
5272 +
5273 +       /* try stopping to update while we copyup */
5274 +       h_src_inode = d_inode(file[SRC].dentry);
5275 +       h_src_sb = h_src_inode->i_sb;
5276 +       if (!au_test_nfs(h_src_sb))
5277 +               IMustLock(h_src_inode);
5278 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5279 +
5280 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5281 +       if (tsk->flags & PF_KTHREAD)
5282 +               __fput_sync(file[DST].file);
5283 +       else {
5284 +               /* it happened actually */
5285 +               fput(file[DST].file);
5286 +               /*
5287 +                * too bad.
5288 +                * we have to call both since we don't know which place the file
5289 +                * was added to.
5290 +                */
5291 +               task_work_run();
5292 +               flush_delayed_fput();
5293 +       }
5294 +       br = au_sbr(sb, file[DST].bindex);
5295 +       au_lcnt_dec(&br->br_nfiles);
5296 +
5297 +out_src:
5298 +       fput(file[SRC].file);
5299 +       br = au_sbr(sb, file[SRC].bindex);
5300 +       au_lcnt_dec(&br->br_nfiles);
5301 +out:
5302 +       return err;
5303 +}
5304 +
5305 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5306 +                             struct au_cpup_reg_attr *h_src_attr)
5307 +{
5308 +       int err, rerr;
5309 +       loff_t l;
5310 +       struct path h_path;
5311 +       struct inode *h_src_inode, *h_dst_inode;
5312 +
5313 +       err = 0;
5314 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5315 +       l = i_size_read(h_src_inode);
5316 +       if (cpg->len == -1 || l < cpg->len)
5317 +               cpg->len = l;
5318 +       if (cpg->len) {
5319 +               /* try stopping to update while we are referencing */
5320 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5321 +               au_pin_hdir_unlock(cpg->pin);
5322 +
5323 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5324 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5325 +               h_src_attr->iflags = h_src_inode->i_flags;
5326 +               if (!au_test_nfs(h_src_inode->i_sb))
5327 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5328 +               else {
5329 +                       inode_unlock_shared(h_src_inode);
5330 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5331 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5332 +               }
5333 +               if (unlikely(err)) {
5334 +                       inode_unlock_shared(h_src_inode);
5335 +                       goto out;
5336 +               }
5337 +               h_src_attr->valid = 1;
5338 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5339 +                       err = au_cp_regular(cpg);
5340 +                       inode_unlock_shared(h_src_inode);
5341 +               } else {
5342 +                       inode_unlock_shared(h_src_inode);
5343 +                       err = au_cp_regular(cpg);
5344 +               }
5345 +               rerr = au_pin_hdir_relock(cpg->pin);
5346 +               if (!err && rerr)
5347 +                       err = rerr;
5348 +       }
5349 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5350 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5351 +               h_dst_inode = d_inode(h_path.dentry);
5352 +               spin_lock(&h_dst_inode->i_lock);
5353 +               h_dst_inode->i_state |= I_LINKABLE;
5354 +               spin_unlock(&h_dst_inode->i_lock);
5355 +       }
5356 +
5357 +out:
5358 +       return err;
5359 +}
5360 +
5361 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5362 +                             struct inode *h_dir)
5363 +{
5364 +       int err;
5365 +       DEFINE_DELAYED_CALL(done);
5366 +       const char *sym;
5367 +
5368 +       sym = vfs_get_link(h_src, &done);
5369 +       err = PTR_ERR(sym);
5370 +       if (IS_ERR(sym))
5371 +               goto out;
5372 +
5373 +       err = vfsub_symlink(h_dir, h_path, sym);
5374 +
5375 +out:
5376 +       do_delayed_call(&done);
5377 +       return err;
5378 +}
5379 +
5380 +/*
5381 + * regardless 'acl' option, reset all ACL.
5382 + * All ACL will be copied up later from the original entry on the lower branch.
5383 + */
5384 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5385 +{
5386 +       int err;
5387 +       struct dentry *h_dentry;
5388 +       struct inode *h_inode;
5389 +
5390 +       h_dentry = h_path->dentry;
5391 +       h_inode = d_inode(h_dentry);
5392 +       /* forget_all_cached_acls(h_inode)); */
5393 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5394 +       AuTraceErr(err);
5395 +       if (err == -EOPNOTSUPP)
5396 +               err = 0;
5397 +       if (!err)
5398 +               err = vfsub_acl_chmod(h_inode, mode);
5399 +
5400 +       AuTraceErr(err);
5401 +       return err;
5402 +}
5403 +
5404 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5405 +                         struct inode *h_dir, struct path *h_path)
5406 +{
5407 +       int err;
5408 +       struct inode *dir, *inode;
5409 +
5410 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5411 +       AuTraceErr(err);
5412 +       if (err == -EOPNOTSUPP)
5413 +               err = 0;
5414 +       if (unlikely(err))
5415 +               goto out;
5416 +
5417 +       /*
5418 +        * strange behaviour from the users view,
5419 +        * particularly setattr case
5420 +        */
5421 +       dir = d_inode(dst_parent);
5422 +       if (au_ibtop(dir) == cpg->bdst)
5423 +               au_cpup_attr_nlink(dir, /*force*/1);
5424 +       inode = d_inode(cpg->dentry);
5425 +       au_cpup_attr_nlink(inode, /*force*/1);
5426 +
5427 +out:
5428 +       return err;
5429 +}
5430 +
5431 +static noinline_for_stack
5432 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5433 +              struct au_cpup_reg_attr *h_src_attr)
5434 +{
5435 +       int err;
5436 +       umode_t mode;
5437 +       unsigned int mnt_flags;
5438 +       unsigned char isdir, isreg, force;
5439 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5440 +       struct au_dtime dt;
5441 +       struct path h_path;
5442 +       struct dentry *h_src, *h_dst, *h_parent;
5443 +       struct inode *h_inode, *h_dir;
5444 +       struct super_block *sb;
5445 +
5446 +       /* bsrc branch can be ro/rw. */
5447 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5448 +       h_inode = d_inode(h_src);
5449 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5450 +
5451 +       /* try stopping to be referenced while we are creating */
5452 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5453 +       if (au_ftest_cpup(cpg->flags, RENAME))
5454 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5455 +                                 AUFS_WH_PFX_LEN));
5456 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5457 +       h_dir = d_inode(h_parent);
5458 +       IMustLock(h_dir);
5459 +       AuDebugOn(h_parent != h_dst->d_parent);
5460 +
5461 +       sb = cpg->dentry->d_sb;
5462 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5463 +       if (do_dt) {
5464 +               h_path.dentry = h_parent;
5465 +               au_dtime_store(&dt, dst_parent, &h_path);
5466 +       }
5467 +       h_path.dentry = h_dst;
5468 +
5469 +       isreg = 0;
5470 +       isdir = 0;
5471 +       mode = h_inode->i_mode;
5472 +       switch (mode & S_IFMT) {
5473 +       case S_IFREG:
5474 +               isreg = 1;
5475 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5476 +               if (!err)
5477 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5478 +               break;
5479 +       case S_IFDIR:
5480 +               isdir = 1;
5481 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5482 +               if (!err)
5483 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5484 +               break;
5485 +       case S_IFLNK:
5486 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5487 +               break;
5488 +       case S_IFCHR:
5489 +       case S_IFBLK:
5490 +               AuDebugOn(!capable(CAP_MKNOD));
5491 +               /*FALLTHROUGH*/
5492 +       case S_IFIFO:
5493 +       case S_IFSOCK:
5494 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5495 +               break;
5496 +       default:
5497 +               AuIOErr("Unknown inode type 0%o\n", mode);
5498 +               err = -EIO;
5499 +       }
5500 +       if (!err)
5501 +               err = au_reset_acl(h_dir, &h_path, mode);
5502 +
5503 +       mnt_flags = au_mntflags(sb);
5504 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5505 +           && !isdir
5506 +           && au_opt_test(mnt_flags, XINO)
5507 +           && (h_inode->i_nlink == 1
5508 +               || (h_inode->i_state & I_LINKABLE))
5509 +           /* todo: unnecessary? */
5510 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5511 +           && cpg->bdst < cpg->bsrc
5512 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5513 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5514 +               /* ignore this error */
5515 +
5516 +       if (!err) {
5517 +               force = 0;
5518 +               if (isreg) {
5519 +                       force = !!cpg->len;
5520 +                       if (cpg->len == -1)
5521 +                               force = !!i_size_read(h_inode);
5522 +               }
5523 +               au_fhsm_wrote(sb, cpg->bdst, force);
5524 +       }
5525 +
5526 +       if (do_dt)
5527 +               au_dtime_revert(&dt);
5528 +       return err;
5529 +}
5530 +
5531 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5532 +{
5533 +       int err;
5534 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5535 +       struct inode *h_dir;
5536 +       aufs_bindex_t bdst;
5537 +
5538 +       dentry = cpg->dentry;
5539 +       bdst = cpg->bdst;
5540 +       h_dentry = au_h_dptr(dentry, bdst);
5541 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5542 +               dget(h_dentry);
5543 +               au_set_h_dptr(dentry, bdst, NULL);
5544 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5545 +               if (!err)
5546 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5547 +               au_set_h_dptr(dentry, bdst, h_dentry);
5548 +       } else {
5549 +               err = 0;
5550 +               parent = dget_parent(dentry);
5551 +               h_parent = au_h_dptr(parent, bdst);
5552 +               dput(parent);
5553 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5554 +               if (IS_ERR(h_path->dentry))
5555 +                       err = PTR_ERR(h_path->dentry);
5556 +       }
5557 +       if (unlikely(err))
5558 +               goto out;
5559 +
5560 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5561 +       h_dir = d_inode(h_parent);
5562 +       IMustLock(h_dir);
5563 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5564 +       /* no delegation since it is just created */
5565 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5566 +                          /*flags*/0);
5567 +       dput(h_path->dentry);
5568 +
5569 +out:
5570 +       return err;
5571 +}
5572 +
5573 +/*
5574 + * copyup the @dentry from @bsrc to @bdst.
5575 + * the caller must set the both of lower dentries.
5576 + * @len is for truncating when it is -1 copyup the entire file.
5577 + * in link/rename cases, @dst_parent may be different from the real one.
5578 + * basic->bsrc can be larger than basic->bdst.
5579 + * aufs doesn't touch the credential so
5580 + * security_inode_copy_up{,_xattr}() are unnecessary.
5581 + */
5582 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5583 +{
5584 +       int err, rerr;
5585 +       aufs_bindex_t old_ibtop;
5586 +       unsigned char isdir, plink;
5587 +       struct dentry *h_src, *h_dst, *h_parent;
5588 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5589 +       struct super_block *sb;
5590 +       struct au_branch *br;
5591 +       /* to reduce stack size */
5592 +       struct {
5593 +               struct au_dtime dt;
5594 +               struct path h_path;
5595 +               struct au_cpup_reg_attr h_src_attr;
5596 +       } *a;
5597 +
5598 +       err = -ENOMEM;
5599 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5600 +       if (unlikely(!a))
5601 +               goto out;
5602 +       a->h_src_attr.valid = 0;
5603 +
5604 +       sb = cpg->dentry->d_sb;
5605 +       br = au_sbr(sb, cpg->bdst);
5606 +       a->h_path.mnt = au_br_mnt(br);
5607 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5608 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5609 +       h_dir = d_inode(h_parent);
5610 +       IMustLock(h_dir);
5611 +
5612 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5613 +       inode = d_inode(cpg->dentry);
5614 +
5615 +       if (!dst_parent)
5616 +               dst_parent = dget_parent(cpg->dentry);
5617 +       else
5618 +               dget(dst_parent);
5619 +
5620 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5621 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5622 +       if (dst_inode) {
5623 +               if (unlikely(!plink)) {
5624 +                       err = -EIO;
5625 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5626 +                               "but plink is disabled\n",
5627 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5628 +                       goto out_parent;
5629 +               }
5630 +
5631 +               if (dst_inode->i_nlink) {
5632 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5633 +
5634 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5635 +                       err = PTR_ERR(h_src);
5636 +                       if (IS_ERR(h_src))
5637 +                               goto out_parent;
5638 +                       if (unlikely(d_is_negative(h_src))) {
5639 +                               err = -EIO;
5640 +                               AuIOErr("i%lu exists on b%d "
5641 +                                       "but not pseudo-linked\n",
5642 +                                       inode->i_ino, cpg->bdst);
5643 +                               dput(h_src);
5644 +                               goto out_parent;
5645 +                       }
5646 +
5647 +                       if (do_dt) {
5648 +                               a->h_path.dentry = h_parent;
5649 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5650 +                       }
5651 +
5652 +                       a->h_path.dentry = h_dst;
5653 +                       delegated = NULL;
5654 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5655 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5656 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5657 +                       if (do_dt)
5658 +                               au_dtime_revert(&a->dt);
5659 +                       if (unlikely(err == -EWOULDBLOCK)) {
5660 +                               pr_warn("cannot retry for NFSv4 delegation"
5661 +                                       " for an internal link\n");
5662 +                               iput(delegated);
5663 +                       }
5664 +                       dput(h_src);
5665 +                       goto out_parent;
5666 +               } else
5667 +                       /* todo: cpup_wh_file? */
5668 +                       /* udba work */
5669 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5670 +       }
5671 +
5672 +       isdir = S_ISDIR(inode->i_mode);
5673 +       old_ibtop = au_ibtop(inode);
5674 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5675 +       if (unlikely(err))
5676 +               goto out_rev;
5677 +       dst_inode = d_inode(h_dst);
5678 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5679 +       /* todo: necessary? */
5680 +       /* au_pin_hdir_unlock(cpg->pin); */
5681 +
5682 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5683 +       if (unlikely(err)) {
5684 +               /* todo: necessary? */
5685 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5686 +               inode_unlock(dst_inode);
5687 +               goto out_rev;
5688 +       }
5689 +
5690 +       if (cpg->bdst < old_ibtop) {
5691 +               if (S_ISREG(inode->i_mode)) {
5692 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5693 +                       if (unlikely(err)) {
5694 +                               /* ignore an error */
5695 +                               /* au_pin_hdir_relock(cpg->pin); */
5696 +                               inode_unlock(dst_inode);
5697 +                               goto out_rev;
5698 +                       }
5699 +               }
5700 +               au_set_ibtop(inode, cpg->bdst);
5701 +       } else
5702 +               au_set_ibbot(inode, cpg->bdst);
5703 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5704 +                     au_hi_flags(inode, isdir));
5705 +
5706 +       /* todo: necessary? */
5707 +       /* err = au_pin_hdir_relock(cpg->pin); */
5708 +       inode_unlock(dst_inode);
5709 +       if (unlikely(err))
5710 +               goto out_rev;
5711 +
5712 +       src_inode = d_inode(h_src);
5713 +       if (!isdir
5714 +           && (src_inode->i_nlink > 1
5715 +               || src_inode->i_state & I_LINKABLE)
5716 +           && plink)
5717 +               au_plink_append(inode, cpg->bdst, h_dst);
5718 +
5719 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5720 +               a->h_path.dentry = h_dst;
5721 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5722 +       }
5723 +       if (!err)
5724 +               goto out_parent; /* success */
5725 +
5726 +       /* revert */
5727 +out_rev:
5728 +       a->h_path.dentry = h_parent;
5729 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5730 +       a->h_path.dentry = h_dst;
5731 +       rerr = 0;
5732 +       if (d_is_positive(h_dst)) {
5733 +               if (!isdir) {
5734 +                       /* no delegation since it is just created */
5735 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5736 +                                           /*delegated*/NULL, /*force*/0);
5737 +               } else
5738 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5739 +       }
5740 +       au_dtime_revert(&a->dt);
5741 +       if (rerr) {
5742 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5743 +               err = -EIO;
5744 +       }
5745 +out_parent:
5746 +       dput(dst_parent);
5747 +       au_kfree_rcu(a);
5748 +out:
5749 +       return err;
5750 +}
5751 +
5752 +#if 0 /* reserved */
5753 +struct au_cpup_single_args {
5754 +       int *errp;
5755 +       struct au_cp_generic *cpg;
5756 +       struct dentry *dst_parent;
5757 +};
5758 +
5759 +static void au_call_cpup_single(void *args)
5760 +{
5761 +       struct au_cpup_single_args *a = args;
5762 +
5763 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5764 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5765 +       au_pin_hdir_release(a->cpg->pin);
5766 +}
5767 +#endif
5768 +
5769 +/*
5770 + * prevent SIGXFSZ in copy-up.
5771 + * testing CAP_MKNOD is for generic fs,
5772 + * but CAP_FSETID is for xfs only, currently.
5773 + */
5774 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5775 +{
5776 +       int do_sio;
5777 +       struct super_block *sb;
5778 +       struct inode *h_dir;
5779 +
5780 +       do_sio = 0;
5781 +       sb = au_pinned_parent(pin)->d_sb;
5782 +       if (!au_wkq_test()
5783 +           && (!au_sbi(sb)->si_plink_maint_pid
5784 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5785 +               switch (mode & S_IFMT) {
5786 +               case S_IFREG:
5787 +                       /* no condition about RLIMIT_FSIZE and the file size */
5788 +                       do_sio = 1;
5789 +                       break;
5790 +               case S_IFCHR:
5791 +               case S_IFBLK:
5792 +                       do_sio = !capable(CAP_MKNOD);
5793 +                       break;
5794 +               }
5795 +               if (!do_sio)
5796 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5797 +                                 && !capable(CAP_FSETID));
5798 +               /* this workaround may be removed in the future */
5799 +               if (!do_sio) {
5800 +                       h_dir = au_pinned_h_dir(pin);
5801 +                       do_sio = h_dir->i_mode & S_ISVTX;
5802 +               }
5803 +       }
5804 +
5805 +       return do_sio;
5806 +}
5807 +
5808 +#if 0 /* reserved */
5809 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5810 +{
5811 +       int err, wkq_err;
5812 +       struct dentry *h_dentry;
5813 +
5814 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5815 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5816 +               err = au_cpup_single(cpg, dst_parent);
5817 +       else {
5818 +               struct au_cpup_single_args args = {
5819 +                       .errp           = &err,
5820 +                       .cpg            = cpg,
5821 +                       .dst_parent     = dst_parent
5822 +               };
5823 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5824 +               if (unlikely(wkq_err))
5825 +                       err = wkq_err;
5826 +       }
5827 +
5828 +       return err;
5829 +}
5830 +#endif
5831 +
5832 +/*
5833 + * copyup the @dentry from the first active lower branch to @bdst,
5834 + * using au_cpup_single().
5835 + */
5836 +static int au_cpup_simple(struct au_cp_generic *cpg)
5837 +{
5838 +       int err;
5839 +       unsigned int flags_orig;
5840 +       struct dentry *dentry;
5841 +
5842 +       AuDebugOn(cpg->bsrc < 0);
5843 +
5844 +       dentry = cpg->dentry;
5845 +       DiMustWriteLock(dentry);
5846 +
5847 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5848 +       if (!err) {
5849 +               flags_orig = cpg->flags;
5850 +               au_fset_cpup(cpg->flags, RENAME);
5851 +               err = au_cpup_single(cpg, NULL);
5852 +               cpg->flags = flags_orig;
5853 +               if (!err)
5854 +                       return 0; /* success */
5855 +
5856 +               /* revert */
5857 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5858 +               au_set_dbtop(dentry, cpg->bsrc);
5859 +       }
5860 +
5861 +       return err;
5862 +}
5863 +
5864 +struct au_cpup_simple_args {
5865 +       int *errp;
5866 +       struct au_cp_generic *cpg;
5867 +};
5868 +
5869 +static void au_call_cpup_simple(void *args)
5870 +{
5871 +       struct au_cpup_simple_args *a = args;
5872 +
5873 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5874 +       *a->errp = au_cpup_simple(a->cpg);
5875 +       au_pin_hdir_release(a->cpg->pin);
5876 +}
5877 +
5878 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5879 +{
5880 +       int err, wkq_err;
5881 +       struct dentry *dentry, *parent;
5882 +       struct file *h_file;
5883 +       struct inode *h_dir;
5884 +
5885 +       dentry = cpg->dentry;
5886 +       h_file = NULL;
5887 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5888 +               AuDebugOn(cpg->bsrc < 0);
5889 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5890 +               err = PTR_ERR(h_file);
5891 +               if (IS_ERR(h_file))
5892 +                       goto out;
5893 +       }
5894 +
5895 +       parent = dget_parent(dentry);
5896 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5897 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5898 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5899 +               err = au_cpup_simple(cpg);
5900 +       else {
5901 +               struct au_cpup_simple_args args = {
5902 +                       .errp           = &err,
5903 +                       .cpg            = cpg
5904 +               };
5905 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5906 +               if (unlikely(wkq_err))
5907 +                       err = wkq_err;
5908 +       }
5909 +
5910 +       dput(parent);
5911 +       if (h_file)
5912 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5913 +
5914 +out:
5915 +       return err;
5916 +}
5917 +
5918 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5919 +{
5920 +       aufs_bindex_t bsrc, bbot;
5921 +       struct dentry *dentry, *h_dentry;
5922 +
5923 +       if (cpg->bsrc < 0) {
5924 +               dentry = cpg->dentry;
5925 +               bbot = au_dbbot(dentry);
5926 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5927 +                       h_dentry = au_h_dptr(dentry, bsrc);
5928 +                       if (h_dentry) {
5929 +                               AuDebugOn(d_is_negative(h_dentry));
5930 +                               break;
5931 +                       }
5932 +               }
5933 +               AuDebugOn(bsrc > bbot);
5934 +               cpg->bsrc = bsrc;
5935 +       }
5936 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5937 +       return au_do_sio_cpup_simple(cpg);
5938 +}
5939 +
5940 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5941 +{
5942 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5943 +       return au_do_sio_cpup_simple(cpg);
5944 +}
5945 +
5946 +/* ---------------------------------------------------------------------- */
5947 +
5948 +/*
5949 + * copyup the deleted file for writing.
5950 + */
5951 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5952 +                        struct file *file)
5953 +{
5954 +       int err;
5955 +       unsigned int flags_orig;
5956 +       aufs_bindex_t bsrc_orig;
5957 +       struct au_dinfo *dinfo;
5958 +       struct {
5959 +               struct au_hdentry *hd;
5960 +               struct dentry *h_dentry;
5961 +       } hdst, hsrc;
5962 +
5963 +       dinfo = au_di(cpg->dentry);
5964 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5965 +
5966 +       bsrc_orig = cpg->bsrc;
5967 +       cpg->bsrc = dinfo->di_btop;
5968 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5969 +       hdst.h_dentry = hdst.hd->hd_dentry;
5970 +       hdst.hd->hd_dentry = wh_dentry;
5971 +       dinfo->di_btop = cpg->bdst;
5972 +
5973 +       hsrc.h_dentry = NULL;
5974 +       if (file) {
5975 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5976 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5977 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5978 +       }
5979 +       flags_orig = cpg->flags;
5980 +       cpg->flags = !AuCpup_DTIME;
5981 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5982 +       cpg->flags = flags_orig;
5983 +       if (file) {
5984 +               if (!err)
5985 +                       err = au_reopen_nondir(file);
5986 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5987 +       }
5988 +       hdst.hd->hd_dentry = hdst.h_dentry;
5989 +       dinfo->di_btop = cpg->bsrc;
5990 +       cpg->bsrc = bsrc_orig;
5991 +
5992 +       return err;
5993 +}
5994 +
5995 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5996 +{
5997 +       int err;
5998 +       aufs_bindex_t bdst;
5999 +       struct au_dtime dt;
6000 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6001 +       struct au_branch *br;
6002 +       struct path h_path;
6003 +
6004 +       dentry = cpg->dentry;
6005 +       bdst = cpg->bdst;
6006 +       br = au_sbr(dentry->d_sb, bdst);
6007 +       parent = dget_parent(dentry);
6008 +       h_parent = au_h_dptr(parent, bdst);
6009 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6010 +       err = PTR_ERR(wh_dentry);
6011 +       if (IS_ERR(wh_dentry))
6012 +               goto out;
6013 +
6014 +       h_path.dentry = h_parent;
6015 +       h_path.mnt = au_br_mnt(br);
6016 +       au_dtime_store(&dt, parent, &h_path);
6017 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6018 +       if (unlikely(err))
6019 +               goto out_wh;
6020 +
6021 +       dget(wh_dentry);
6022 +       h_path.dentry = wh_dentry;
6023 +       if (!d_is_dir(wh_dentry)) {
6024 +               /* no delegation since it is just created */
6025 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6026 +                                  /*delegated*/NULL, /*force*/0);
6027 +       } else
6028 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6029 +       if (unlikely(err)) {
6030 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6031 +                       wh_dentry, err);
6032 +               err = -EIO;
6033 +       }
6034 +       au_dtime_revert(&dt);
6035 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6036 +
6037 +out_wh:
6038 +       dput(wh_dentry);
6039 +out:
6040 +       dput(parent);
6041 +       return err;
6042 +}
6043 +
6044 +struct au_cpup_wh_args {
6045 +       int *errp;
6046 +       struct au_cp_generic *cpg;
6047 +       struct file *file;
6048 +};
6049 +
6050 +static void au_call_cpup_wh(void *args)
6051 +{
6052 +       struct au_cpup_wh_args *a = args;
6053 +
6054 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6055 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6056 +       au_pin_hdir_release(a->cpg->pin);
6057 +}
6058 +
6059 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6060 +{
6061 +       int err, wkq_err;
6062 +       aufs_bindex_t bdst;
6063 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6064 +       struct inode *dir, *h_dir, *h_tmpdir;
6065 +       struct au_wbr *wbr;
6066 +       struct au_pin wh_pin, *pin_orig;
6067 +
6068 +       dentry = cpg->dentry;
6069 +       bdst = cpg->bdst;
6070 +       parent = dget_parent(dentry);
6071 +       dir = d_inode(parent);
6072 +       h_orph = NULL;
6073 +       h_parent = NULL;
6074 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6075 +       h_tmpdir = h_dir;
6076 +       pin_orig = NULL;
6077 +       if (!h_dir->i_nlink) {
6078 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6079 +               h_orph = wbr->wbr_orph;
6080 +
6081 +               h_parent = dget(au_h_dptr(parent, bdst));
6082 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6083 +               h_tmpdir = d_inode(h_orph);
6084 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6085 +
6086 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6087 +               /* todo: au_h_open_pre()? */
6088 +
6089 +               pin_orig = cpg->pin;
6090 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6091 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6092 +               cpg->pin = &wh_pin;
6093 +       }
6094 +
6095 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
6096 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6097 +               err = au_cpup_wh(cpg, file);
6098 +       else {
6099 +               struct au_cpup_wh_args args = {
6100 +                       .errp   = &err,
6101 +                       .cpg    = cpg,
6102 +                       .file   = file
6103 +               };
6104 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6105 +               if (unlikely(wkq_err))
6106 +                       err = wkq_err;
6107 +       }
6108 +
6109 +       if (h_orph) {
6110 +               inode_unlock(h_tmpdir);
6111 +               /* todo: au_h_open_post()? */
6112 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6113 +               au_set_h_dptr(parent, bdst, h_parent);
6114 +               AuDebugOn(!pin_orig);
6115 +               cpg->pin = pin_orig;
6116 +       }
6117 +       iput(h_dir);
6118 +       dput(parent);
6119 +
6120 +       return err;
6121 +}
6122 +
6123 +/* ---------------------------------------------------------------------- */
6124 +
6125 +/*
6126 + * generic routine for both of copy-up and copy-down.
6127 + */
6128 +/* cf. revalidate function in file.c */
6129 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6130 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6131 +                        struct au_pin *pin,
6132 +                        struct dentry *h_parent, void *arg),
6133 +              void *arg)
6134 +{
6135 +       int err;
6136 +       struct au_pin pin;
6137 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6138 +
6139 +       err = 0;
6140 +       parent = dget_parent(dentry);
6141 +       if (IS_ROOT(parent))
6142 +               goto out;
6143 +
6144 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6145 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6146 +
6147 +       /* do not use au_dpage */
6148 +       real_parent = parent;
6149 +       while (1) {
6150 +               dput(parent);
6151 +               parent = dget_parent(dentry);
6152 +               h_parent = au_h_dptr(parent, bdst);
6153 +               if (h_parent)
6154 +                       goto out; /* success */
6155 +
6156 +               /* find top dir which is necessary to cpup */
6157 +               do {
6158 +                       d = parent;
6159 +                       dput(parent);
6160 +                       parent = dget_parent(d);
6161 +                       di_read_lock_parent3(parent, !AuLock_IR);
6162 +                       h_parent = au_h_dptr(parent, bdst);
6163 +                       di_read_unlock(parent, !AuLock_IR);
6164 +               } while (!h_parent);
6165 +
6166 +               if (d != real_parent)
6167 +                       di_write_lock_child3(d);
6168 +
6169 +               /* somebody else might create while we were sleeping */
6170 +               h_dentry = au_h_dptr(d, bdst);
6171 +               if (!h_dentry || d_is_negative(h_dentry)) {
6172 +                       if (h_dentry)
6173 +                               au_update_dbtop(d);
6174 +
6175 +                       au_pin_set_dentry(&pin, d);
6176 +                       err = au_do_pin(&pin);
6177 +                       if (!err) {
6178 +                               err = cp(d, bdst, &pin, h_parent, arg);
6179 +                               au_unpin(&pin);
6180 +                       }
6181 +               }
6182 +
6183 +               if (d != real_parent)
6184 +                       di_write_unlock(d);
6185 +               if (unlikely(err))
6186 +                       break;
6187 +       }
6188 +
6189 +out:
6190 +       dput(parent);
6191 +       return err;
6192 +}
6193 +
6194 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6195 +                      struct au_pin *pin,
6196 +                      struct dentry *h_parent __maybe_unused,
6197 +                      void *arg __maybe_unused)
6198 +{
6199 +       struct au_cp_generic cpg = {
6200 +               .dentry = dentry,
6201 +               .bdst   = bdst,
6202 +               .bsrc   = -1,
6203 +               .len    = 0,
6204 +               .pin    = pin,
6205 +               .flags  = AuCpup_DTIME
6206 +       };
6207 +       return au_sio_cpup_simple(&cpg);
6208 +}
6209 +
6210 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6211 +{
6212 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6213 +}
6214 +
6215 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6216 +{
6217 +       int err;
6218 +       struct dentry *parent;
6219 +       struct inode *dir;
6220 +
6221 +       parent = dget_parent(dentry);
6222 +       dir = d_inode(parent);
6223 +       err = 0;
6224 +       if (au_h_iptr(dir, bdst))
6225 +               goto out;
6226 +
6227 +       di_read_unlock(parent, AuLock_IR);
6228 +       di_write_lock_parent(parent);
6229 +       /* someone else might change our inode while we were sleeping */
6230 +       if (!au_h_iptr(dir, bdst))
6231 +               err = au_cpup_dirs(dentry, bdst);
6232 +       di_downgrade_lock(parent, AuLock_IR);
6233 +
6234 +out:
6235 +       dput(parent);
6236 +       return err;
6237 +}
6238 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6239 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6240 +++ linux/fs/aufs/cpup.h        2020-01-27 10:57:18.168871450 +0100
6241 @@ -0,0 +1,100 @@
6242 +/* SPDX-License-Identifier: GPL-2.0 */
6243 +/*
6244 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6245 + *
6246 + * This program, aufs is free software; you can redistribute it and/or modify
6247 + * it under the terms of the GNU General Public License as published by
6248 + * the Free Software Foundation; either version 2 of the License, or
6249 + * (at your option) any later version.
6250 + *
6251 + * This program is distributed in the hope that it will be useful,
6252 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6253 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6254 + * GNU General Public License for more details.
6255 + *
6256 + * You should have received a copy of the GNU General Public License
6257 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6258 + */
6259 +
6260 +/*
6261 + * copy-up/down functions
6262 + */
6263 +
6264 +#ifndef __AUFS_CPUP_H__
6265 +#define __AUFS_CPUP_H__
6266 +
6267 +#ifdef __KERNEL__
6268 +
6269 +#include <linux/path.h>
6270 +
6271 +struct inode;
6272 +struct file;
6273 +struct au_pin;
6274 +
6275 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6276 +void au_cpup_attr_timesizes(struct inode *inode);
6277 +void au_cpup_attr_nlink(struct inode *inode, int force);
6278 +void au_cpup_attr_changeable(struct inode *inode);
6279 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6280 +void au_cpup_attr_all(struct inode *inode, int force);
6281 +
6282 +/* ---------------------------------------------------------------------- */
6283 +
6284 +struct au_cp_generic {
6285 +       struct dentry   *dentry;
6286 +       aufs_bindex_t   bdst, bsrc;
6287 +       loff_t          len;
6288 +       struct au_pin   *pin;
6289 +       unsigned int    flags;
6290 +};
6291 +
6292 +/* cpup flags */
6293 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6294 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6295 +                                                  for link(2) */
6296 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6297 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6298 +                                                  cpup */
6299 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6300 +                                                  existing entry */
6301 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6302 +                                                  the branch is marked as RO */
6303 +
6304 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6305 +#undef AuCpup_HOPEN
6306 +#define AuCpup_HOPEN           0
6307 +#endif
6308 +
6309 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6310 +#define au_fset_cpup(flags, name) \
6311 +       do { (flags) |= AuCpup_##name; } while (0)
6312 +#define au_fclr_cpup(flags, name) \
6313 +       do { (flags) &= ~AuCpup_##name; } while (0)
6314 +
6315 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6316 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6317 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6318 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6319 +
6320 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6321 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6322 +                        struct au_pin *pin,
6323 +                        struct dentry *h_parent, void *arg),
6324 +              void *arg);
6325 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6326 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6327 +
6328 +/* ---------------------------------------------------------------------- */
6329 +
6330 +/* keep timestamps when copyup */
6331 +struct au_dtime {
6332 +       struct dentry *dt_dentry;
6333 +       struct path dt_h_path;
6334 +       struct timespec64 dt_atime, dt_mtime;
6335 +};
6336 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6337 +                   struct path *h_path);
6338 +void au_dtime_revert(struct au_dtime *dt);
6339 +
6340 +#endif /* __KERNEL__ */
6341 +#endif /* __AUFS_CPUP_H__ */
6342 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6343 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6344 +++ linux/fs/aufs/dbgaufs.c     2020-01-27 10:57:18.168871450 +0100
6345 @@ -0,0 +1,526 @@
6346 +// SPDX-License-Identifier: GPL-2.0
6347 +/*
6348 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6349 + *
6350 + * This program, aufs is free software; you can redistribute it and/or modify
6351 + * it under the terms of the GNU General Public License as published by
6352 + * the Free Software Foundation; either version 2 of the License, or
6353 + * (at your option) any later version.
6354 + *
6355 + * This program is distributed in the hope that it will be useful,
6356 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6357 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6358 + * GNU General Public License for more details.
6359 + *
6360 + * You should have received a copy of the GNU General Public License
6361 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6362 + */
6363 +
6364 +/*
6365 + * debugfs interface
6366 + */
6367 +
6368 +#include <linux/debugfs.h>
6369 +#include "aufs.h"
6370 +
6371 +#ifndef CONFIG_SYSFS
6372 +#error DEBUG_FS depends upon SYSFS
6373 +#endif
6374 +
6375 +static struct dentry *dbgaufs;
6376 +static const mode_t dbgaufs_mode = 0444;
6377 +
6378 +/* 20 is max digits length of ulong 64 */
6379 +struct dbgaufs_arg {
6380 +       int n;
6381 +       char a[20 * 4];
6382 +};
6383 +
6384 +/*
6385 + * common function for all XINO files
6386 + */
6387 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6388 +                             struct file *file)
6389 +{
6390 +       void *p;
6391 +
6392 +       p = file->private_data;
6393 +       if (p) {
6394 +               /* this is struct dbgaufs_arg */
6395 +               AuDebugOn(!au_kfree_sz_test(p));
6396 +               au_kfree_do_rcu(p);
6397 +       }
6398 +       return 0;
6399 +}
6400 +
6401 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6402 +                          int cnt)
6403 +{
6404 +       int err;
6405 +       struct kstat st;
6406 +       struct dbgaufs_arg *p;
6407 +
6408 +       err = -ENOMEM;
6409 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6410 +       if (unlikely(!p))
6411 +               goto out;
6412 +
6413 +       err = 0;
6414 +       p->n = 0;
6415 +       file->private_data = p;
6416 +       if (!xf)
6417 +               goto out;
6418 +
6419 +       err = vfsub_getattr(&xf->f_path, &st);
6420 +       if (!err) {
6421 +               if (do_fcnt)
6422 +                       p->n = snprintf
6423 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6424 +                                cnt, st.blocks, st.blksize,
6425 +                                (long long)st.size);
6426 +               else
6427 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6428 +                                       st.blocks, st.blksize,
6429 +                                       (long long)st.size);
6430 +               AuDebugOn(p->n >= sizeof(p->a));
6431 +       } else {
6432 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6433 +               err = 0;
6434 +       }
6435 +
6436 +out:
6437 +       return err;
6438 +}
6439 +
6440 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6441 +                              size_t count, loff_t *ppos)
6442 +{
6443 +       struct dbgaufs_arg *p;
6444 +
6445 +       p = file->private_data;
6446 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6447 +}
6448 +
6449 +/* ---------------------------------------------------------------------- */
6450 +
6451 +struct dbgaufs_plink_arg {
6452 +       int n;
6453 +       char a[];
6454 +};
6455 +
6456 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6457 +                                struct file *file)
6458 +{
6459 +       free_page((unsigned long)file->private_data);
6460 +       return 0;
6461 +}
6462 +
6463 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6464 +{
6465 +       int err, i, limit;
6466 +       unsigned long n, sum;
6467 +       struct dbgaufs_plink_arg *p;
6468 +       struct au_sbinfo *sbinfo;
6469 +       struct super_block *sb;
6470 +       struct hlist_bl_head *hbl;
6471 +
6472 +       err = -ENOMEM;
6473 +       p = (void *)get_zeroed_page(GFP_NOFS);
6474 +       if (unlikely(!p))
6475 +               goto out;
6476 +
6477 +       err = -EFBIG;
6478 +       sbinfo = inode->i_private;
6479 +       sb = sbinfo->si_sb;
6480 +       si_noflush_read_lock(sb);
6481 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6482 +               limit = PAGE_SIZE - sizeof(p->n);
6483 +
6484 +               /* the number of buckets */
6485 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6486 +               p->n += n;
6487 +               limit -= n;
6488 +
6489 +               sum = 0;
6490 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6491 +                    i++, hbl++) {
6492 +                       n = au_hbl_count(hbl);
6493 +                       sum += n;
6494 +
6495 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6496 +                       p->n += n;
6497 +                       limit -= n;
6498 +                       if (unlikely(limit <= 0))
6499 +                               goto out_free;
6500 +               }
6501 +               p->a[p->n - 1] = '\n';
6502 +
6503 +               /* the sum of plinks */
6504 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6505 +               p->n += n;
6506 +               limit -= n;
6507 +               if (unlikely(limit <= 0))
6508 +                       goto out_free;
6509 +       } else {
6510 +#define str "1\n0\n0\n"
6511 +               p->n = sizeof(str) - 1;
6512 +               strcpy(p->a, str);
6513 +#undef str
6514 +       }
6515 +       si_read_unlock(sb);
6516 +
6517 +       err = 0;
6518 +       file->private_data = p;
6519 +       goto out; /* success */
6520 +
6521 +out_free:
6522 +       free_page((unsigned long)p);
6523 +out:
6524 +       return err;
6525 +}
6526 +
6527 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6528 +                                 size_t count, loff_t *ppos)
6529 +{
6530 +       struct dbgaufs_plink_arg *p;
6531 +
6532 +       p = file->private_data;
6533 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6534 +}
6535 +
6536 +static const struct file_operations dbgaufs_plink_fop = {
6537 +       .owner          = THIS_MODULE,
6538 +       .open           = dbgaufs_plink_open,
6539 +       .release        = dbgaufs_plink_release,
6540 +       .read           = dbgaufs_plink_read
6541 +};
6542 +
6543 +/* ---------------------------------------------------------------------- */
6544 +
6545 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6546 +{
6547 +       int err;
6548 +       struct au_sbinfo *sbinfo;
6549 +       struct super_block *sb;
6550 +
6551 +       sbinfo = inode->i_private;
6552 +       sb = sbinfo->si_sb;
6553 +       si_noflush_read_lock(sb);
6554 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6555 +       si_read_unlock(sb);
6556 +       return err;
6557 +}
6558 +
6559 +static const struct file_operations dbgaufs_xib_fop = {
6560 +       .owner          = THIS_MODULE,
6561 +       .open           = dbgaufs_xib_open,
6562 +       .release        = dbgaufs_xi_release,
6563 +       .read           = dbgaufs_xi_read
6564 +};
6565 +
6566 +/* ---------------------------------------------------------------------- */
6567 +
6568 +#define DbgaufsXi_PREFIX "xi"
6569 +
6570 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6571 +{
6572 +       int err, idx;
6573 +       long l;
6574 +       aufs_bindex_t bindex;
6575 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6576 +       struct au_sbinfo *sbinfo;
6577 +       struct super_block *sb;
6578 +       struct au_xino *xi;
6579 +       struct file *xf;
6580 +       struct qstr *name;
6581 +       struct au_branch *br;
6582 +
6583 +       err = -ENOENT;
6584 +       name = &file->f_path.dentry->d_name;
6585 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6586 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6587 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6588 +               goto out;
6589 +
6590 +       AuDebugOn(name->len >= sizeof(a));
6591 +       memcpy(a, name->name, name->len);
6592 +       a[name->len] = '\0';
6593 +       p = strchr(a, '-');
6594 +       if (p)
6595 +               *p = '\0';
6596 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6597 +       if (unlikely(err))
6598 +               goto out;
6599 +       bindex = l;
6600 +       idx = 0;
6601 +       if (p) {
6602 +               err = kstrtol(p + 1, 10, &l);
6603 +               if (unlikely(err))
6604 +                       goto out;
6605 +               idx = l;
6606 +       }
6607 +
6608 +       err = -ENOENT;
6609 +       sbinfo = inode->i_private;
6610 +       sb = sbinfo->si_sb;
6611 +       si_noflush_read_lock(sb);
6612 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6613 +               goto out_si;
6614 +       br = au_sbr(sb, bindex);
6615 +       xi = br->br_xino;
6616 +       if (unlikely(idx >= xi->xi_nfile))
6617 +               goto out_si;
6618 +       xf = au_xino_file(xi, idx);
6619 +       if (xf)
6620 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6621 +                                     au_xino_count(br));
6622 +
6623 +out_si:
6624 +       si_read_unlock(sb);
6625 +out:
6626 +       AuTraceErr(err);
6627 +       return err;
6628 +}
6629 +
6630 +static const struct file_operations dbgaufs_xino_fop = {
6631 +       .owner          = THIS_MODULE,
6632 +       .open           = dbgaufs_xino_open,
6633 +       .release        = dbgaufs_xi_release,
6634 +       .read           = dbgaufs_xi_read
6635 +};
6636 +
6637 +void dbgaufs_xino_del(struct au_branch *br)
6638 +{
6639 +       struct dentry *dbgaufs;
6640 +
6641 +       dbgaufs = br->br_dbgaufs;
6642 +       if (!dbgaufs)
6643 +               return;
6644 +
6645 +       br->br_dbgaufs = NULL;
6646 +       /* debugfs acquires the parent i_mutex */
6647 +       lockdep_off();
6648 +       debugfs_remove(dbgaufs);
6649 +       lockdep_on();
6650 +}
6651 +
6652 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6653 +{
6654 +       aufs_bindex_t bbot;
6655 +       struct au_branch *br;
6656 +
6657 +       if (!au_sbi(sb)->si_dbgaufs)
6658 +               return;
6659 +
6660 +       bbot = au_sbbot(sb);
6661 +       for (; bindex <= bbot; bindex++) {
6662 +               br = au_sbr(sb, bindex);
6663 +               dbgaufs_xino_del(br);
6664 +       }
6665 +}
6666 +
6667 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6668 +                             unsigned int idx, struct dentry *parent,
6669 +                             struct au_sbinfo *sbinfo)
6670 +{
6671 +       struct au_branch *br;
6672 +       struct dentry *d;
6673 +       /* "xi" bindex(5) "-" idx(2) NULL */
6674 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6675 +
6676 +       if (!idx)
6677 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6678 +       else
6679 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6680 +                        bindex, idx);
6681 +       br = au_sbr(sb, bindex);
6682 +       if (br->br_dbgaufs) {
6683 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6684 +
6685 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6686 +                       /* debugfs acquires the parent i_mutex */
6687 +                       lockdep_off();
6688 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6689 +                                          name);
6690 +                       lockdep_on();
6691 +                       if (unlikely(!d))
6692 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6693 +                                       parent, name);
6694 +               }
6695 +       } else {
6696 +               lockdep_off();
6697 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6698 +                                                    sbinfo, &dbgaufs_xino_fop);
6699 +               lockdep_on();
6700 +               if (unlikely(!br->br_dbgaufs))
6701 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6702 +                               parent, name);
6703 +       }
6704 +}
6705 +
6706 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6707 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6708 +{
6709 +       struct au_branch *br;
6710 +       struct au_xino *xi;
6711 +       unsigned int u;
6712 +
6713 +       br = au_sbr(sb, bindex);
6714 +       xi = br->br_xino;
6715 +       for (u = 0; u < xi->xi_nfile; u++)
6716 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6717 +}
6718 +
6719 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6720 +{
6721 +       struct au_sbinfo *sbinfo;
6722 +       struct dentry *parent;
6723 +       aufs_bindex_t bbot;
6724 +
6725 +       if (!au_opt_test(au_mntflags(sb), XINO))
6726 +               return;
6727 +
6728 +       sbinfo = au_sbi(sb);
6729 +       parent = sbinfo->si_dbgaufs;
6730 +       if (!parent)
6731 +               return;
6732 +
6733 +       bbot = au_sbbot(sb);
6734 +       if (topdown)
6735 +               for (; bindex <= bbot; bindex++)
6736 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6737 +       else
6738 +               for (; bbot >= bindex; bbot--)
6739 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6740 +}
6741 +
6742 +/* ---------------------------------------------------------------------- */
6743 +
6744 +#ifdef CONFIG_AUFS_EXPORT
6745 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6746 +{
6747 +       int err;
6748 +       struct au_sbinfo *sbinfo;
6749 +       struct super_block *sb;
6750 +
6751 +       sbinfo = inode->i_private;
6752 +       sb = sbinfo->si_sb;
6753 +       si_noflush_read_lock(sb);
6754 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6755 +       si_read_unlock(sb);
6756 +       return err;
6757 +}
6758 +
6759 +static const struct file_operations dbgaufs_xigen_fop = {
6760 +       .owner          = THIS_MODULE,
6761 +       .open           = dbgaufs_xigen_open,
6762 +       .release        = dbgaufs_xi_release,
6763 +       .read           = dbgaufs_xi_read
6764 +};
6765 +
6766 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6767 +{
6768 +       int err;
6769 +
6770 +       /*
6771 +        * This function is a dynamic '__init' function actually,
6772 +        * so the tiny check for si_rwsem is unnecessary.
6773 +        */
6774 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6775 +
6776 +       err = -EIO;
6777 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6778 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6779 +                &dbgaufs_xigen_fop);
6780 +       if (sbinfo->si_dbgaufs_xigen)
6781 +               err = 0;
6782 +
6783 +       return err;
6784 +}
6785 +#else
6786 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6787 +{
6788 +       return 0;
6789 +}
6790 +#endif /* CONFIG_AUFS_EXPORT */
6791 +
6792 +/* ---------------------------------------------------------------------- */
6793 +
6794 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6795 +{
6796 +       /*
6797 +        * This function is a dynamic '__fin' function actually,
6798 +        * so the tiny check for si_rwsem is unnecessary.
6799 +        */
6800 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6801 +
6802 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6803 +       sbinfo->si_dbgaufs = NULL;
6804 +}
6805 +
6806 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6807 +{
6808 +       int err;
6809 +       char name[SysaufsSiNameLen];
6810 +
6811 +       /*
6812 +        * This function is a dynamic '__init' function actually,
6813 +        * so the tiny check for si_rwsem is unnecessary.
6814 +        */
6815 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6816 +
6817 +       err = -ENOENT;
6818 +       if (!dbgaufs) {
6819 +               AuErr1("/debug/aufs is uninitialized\n");
6820 +               goto out;
6821 +       }
6822 +
6823 +       err = -EIO;
6824 +       sysaufs_name(sbinfo, name);
6825 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6826 +       if (unlikely(!sbinfo->si_dbgaufs))
6827 +               goto out;
6828 +
6829 +       /* regardless plink/noplink option */
6830 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6831 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6832 +                &dbgaufs_plink_fop);
6833 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6834 +               goto out_dir;
6835 +
6836 +       /* regardless xino/noxino option */
6837 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6838 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6839 +                &dbgaufs_xib_fop);
6840 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6841 +               goto out_dir;
6842 +
6843 +       err = dbgaufs_xigen_init(sbinfo);
6844 +       if (!err)
6845 +               goto out; /* success */
6846 +
6847 +out_dir:
6848 +       dbgaufs_si_fin(sbinfo);
6849 +out:
6850 +       if (unlikely(err))
6851 +               pr_err("debugfs/aufs failed\n");
6852 +       return err;
6853 +}
6854 +
6855 +/* ---------------------------------------------------------------------- */
6856 +
6857 +void dbgaufs_fin(void)
6858 +{
6859 +       debugfs_remove(dbgaufs);
6860 +}
6861 +
6862 +int __init dbgaufs_init(void)
6863 +{
6864 +       int err;
6865 +
6866 +       err = -EIO;
6867 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6868 +       if (dbgaufs)
6869 +               err = 0;
6870 +       return err;
6871 +}
6872 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6873 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6874 +++ linux/fs/aufs/dbgaufs.h     2020-01-27 10:57:18.168871450 +0100
6875 @@ -0,0 +1,53 @@
6876 +/* SPDX-License-Identifier: GPL-2.0 */
6877 +/*
6878 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6879 + *
6880 + * This program, aufs is free software; you can redistribute it and/or modify
6881 + * it under the terms of the GNU General Public License as published by
6882 + * the Free Software Foundation; either version 2 of the License, or
6883 + * (at your option) any later version.
6884 + *
6885 + * This program is distributed in the hope that it will be useful,
6886 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6887 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6888 + * GNU General Public License for more details.
6889 + *
6890 + * You should have received a copy of the GNU General Public License
6891 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6892 + */
6893 +
6894 +/*
6895 + * debugfs interface
6896 + */
6897 +
6898 +#ifndef __DBGAUFS_H__
6899 +#define __DBGAUFS_H__
6900 +
6901 +#ifdef __KERNEL__
6902 +
6903 +struct super_block;
6904 +struct au_sbinfo;
6905 +struct au_branch;
6906 +
6907 +#ifdef CONFIG_DEBUG_FS
6908 +/* dbgaufs.c */
6909 +void dbgaufs_xino_del(struct au_branch *br);
6910 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6911 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6912 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6913 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6914 +void dbgaufs_fin(void);
6915 +int __init dbgaufs_init(void);
6916 +#else
6917 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6918 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6919 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6920 +          int topdown)
6921 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6922 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6923 +AuStubVoid(dbgaufs_fin, void)
6924 +AuStubInt0(__init dbgaufs_init, void)
6925 +#endif /* CONFIG_DEBUG_FS */
6926 +
6927 +#endif /* __KERNEL__ */
6928 +#endif /* __DBGAUFS_H__ */
6929 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6930 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6931 +++ linux/fs/aufs/dcsub.c       2020-01-27 10:57:18.168871450 +0100
6932 @@ -0,0 +1,225 @@
6933 +// SPDX-License-Identifier: GPL-2.0
6934 +/*
6935 + * Copyright (C) 2005-2020 Junjiro R. Okajima
6936 + *
6937 + * This program, aufs is free software; you can redistribute it and/or modify
6938 + * it under the terms of the GNU General Public License as published by
6939 + * the Free Software Foundation; either version 2 of the License, or
6940 + * (at your option) any later version.
6941 + *
6942 + * This program is distributed in the hope that it will be useful,
6943 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6944 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6945 + * GNU General Public License for more details.
6946 + *
6947 + * You should have received a copy of the GNU General Public License
6948 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6949 + */
6950 +
6951 +/*
6952 + * sub-routines for dentry cache
6953 + */
6954 +
6955 +#include "aufs.h"
6956 +
6957 +static void au_dpage_free(struct au_dpage *dpage)
6958 +{
6959 +       int i;
6960 +       struct dentry **p;
6961 +
6962 +       p = dpage->dentries;
6963 +       for (i = 0; i < dpage->ndentry; i++)
6964 +               dput(*p++);
6965 +       free_page((unsigned long)dpage->dentries);
6966 +}
6967 +
6968 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6969 +{
6970 +       int err;
6971 +       void *p;
6972 +
6973 +       err = -ENOMEM;
6974 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6975 +       if (unlikely(!dpages->dpages))
6976 +               goto out;
6977 +
6978 +       p = (void *)__get_free_page(gfp);
6979 +       if (unlikely(!p))
6980 +               goto out_dpages;
6981 +
6982 +       dpages->dpages[0].ndentry = 0;
6983 +       dpages->dpages[0].dentries = p;
6984 +       dpages->ndpage = 1;
6985 +       return 0; /* success */
6986 +
6987 +out_dpages:
6988 +       au_kfree_try_rcu(dpages->dpages);
6989 +out:
6990 +       return err;
6991 +}
6992 +
6993 +void au_dpages_free(struct au_dcsub_pages *dpages)
6994 +{
6995 +       int i;
6996 +       struct au_dpage *p;
6997 +
6998 +       p = dpages->dpages;
6999 +       for (i = 0; i < dpages->ndpage; i++)
7000 +               au_dpage_free(p++);
7001 +       au_kfree_try_rcu(dpages->dpages);
7002 +}
7003 +
7004 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7005 +                           struct dentry *dentry, gfp_t gfp)
7006 +{
7007 +       int err, sz;
7008 +       struct au_dpage *dpage;
7009 +       void *p;
7010 +
7011 +       dpage = dpages->dpages + dpages->ndpage - 1;
7012 +       sz = PAGE_SIZE / sizeof(dentry);
7013 +       if (unlikely(dpage->ndentry >= sz)) {
7014 +               AuLabel(new dpage);
7015 +               err = -ENOMEM;
7016 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7017 +               p = au_kzrealloc(dpages->dpages, sz,
7018 +                                sz + sizeof(*dpages->dpages), gfp,
7019 +                                /*may_shrink*/0);
7020 +               if (unlikely(!p))
7021 +                       goto out;
7022 +
7023 +               dpages->dpages = p;
7024 +               dpage = dpages->dpages + dpages->ndpage;
7025 +               p = (void *)__get_free_page(gfp);
7026 +               if (unlikely(!p))
7027 +                       goto out;
7028 +
7029 +               dpage->ndentry = 0;
7030 +               dpage->dentries = p;
7031 +               dpages->ndpage++;
7032 +       }
7033 +
7034 +       AuDebugOn(au_dcount(dentry) <= 0);
7035 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7036 +       return 0; /* success */
7037 +
7038 +out:
7039 +       return err;
7040 +}
7041 +
7042 +/* todo: BAD approach */
7043 +/* copied from linux/fs/dcache.c */
7044 +enum d_walk_ret {
7045 +       D_WALK_CONTINUE,
7046 +       D_WALK_QUIT,
7047 +       D_WALK_NORETRY,
7048 +       D_WALK_SKIP,
7049 +};
7050 +
7051 +extern void d_walk(struct dentry *parent, void *data,
7052 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7053 +
7054 +struct ac_dpages_arg {
7055 +       int err;
7056 +       struct au_dcsub_pages *dpages;
7057 +       struct super_block *sb;
7058 +       au_dpages_test test;
7059 +       void *arg;
7060 +};
7061 +
7062 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7063 +{
7064 +       enum d_walk_ret ret;
7065 +       struct ac_dpages_arg *arg = _arg;
7066 +
7067 +       ret = D_WALK_CONTINUE;
7068 +       if (dentry->d_sb == arg->sb
7069 +           && !IS_ROOT(dentry)
7070 +           && au_dcount(dentry) > 0
7071 +           && au_di(dentry)
7072 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7073 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7074 +               if (unlikely(arg->err))
7075 +                       ret = D_WALK_QUIT;
7076 +       }
7077 +
7078 +       return ret;
7079 +}
7080 +
7081 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7082 +                  au_dpages_test test, void *arg)
7083 +{
7084 +       struct ac_dpages_arg args = {
7085 +               .err    = 0,
7086 +               .dpages = dpages,
7087 +               .sb     = root->d_sb,
7088 +               .test   = test,
7089 +               .arg    = arg
7090 +       };
7091 +
7092 +       d_walk(root, &args, au_call_dpages_append);
7093 +
7094 +       return args.err;
7095 +}
7096 +
7097 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7098 +                      int do_include, au_dpages_test test, void *arg)
7099 +{
7100 +       int err;
7101 +
7102 +       err = 0;
7103 +       write_seqlock(&rename_lock);
7104 +       spin_lock(&dentry->d_lock);
7105 +       if (do_include
7106 +           && au_dcount(dentry) > 0
7107 +           && (!test || test(dentry, arg)))
7108 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7109 +       spin_unlock(&dentry->d_lock);
7110 +       if (unlikely(err))
7111 +               goto out;
7112 +
7113 +       /*
7114 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7115 +        * mount
7116 +        */
7117 +       while (!IS_ROOT(dentry)) {
7118 +               dentry = dentry->d_parent; /* rename_lock is locked */
7119 +               spin_lock(&dentry->d_lock);
7120 +               if (au_dcount(dentry) > 0
7121 +                   && (!test || test(dentry, arg)))
7122 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7123 +               spin_unlock(&dentry->d_lock);
7124 +               if (unlikely(err))
7125 +                       break;
7126 +       }
7127 +
7128 +out:
7129 +       write_sequnlock(&rename_lock);
7130 +       return err;
7131 +}
7132 +
7133 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7134 +{
7135 +       return au_di(dentry) && dentry->d_sb == arg;
7136 +}
7137 +
7138 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7139 +                           struct dentry *dentry, int do_include)
7140 +{
7141 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7142 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7143 +}
7144 +
7145 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7146 +{
7147 +       struct path path[2] = {
7148 +               {
7149 +                       .dentry = d1
7150 +               },
7151 +               {
7152 +                       .dentry = d2
7153 +               }
7154 +       };
7155 +
7156 +       return path_is_under(path + 0, path + 1);
7157 +}
7158 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7159 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7160 +++ linux/fs/aufs/dcsub.h       2020-01-27 10:57:18.168871450 +0100
7161 @@ -0,0 +1,137 @@
7162 +/* SPDX-License-Identifier: GPL-2.0 */
7163 +/*
7164 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7165 + *
7166 + * This program, aufs is free software; you can redistribute it and/or modify
7167 + * it under the terms of the GNU General Public License as published by
7168 + * the Free Software Foundation; either version 2 of the License, or
7169 + * (at your option) any later version.
7170 + *
7171 + * This program is distributed in the hope that it will be useful,
7172 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7173 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7174 + * GNU General Public License for more details.
7175 + *
7176 + * You should have received a copy of the GNU General Public License
7177 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7178 + */
7179 +
7180 +/*
7181 + * sub-routines for dentry cache
7182 + */
7183 +
7184 +#ifndef __AUFS_DCSUB_H__
7185 +#define __AUFS_DCSUB_H__
7186 +
7187 +#ifdef __KERNEL__
7188 +
7189 +#include <linux/dcache.h>
7190 +#include <linux/fs.h>
7191 +
7192 +struct au_dpage {
7193 +       int ndentry;
7194 +       struct dentry **dentries;
7195 +};
7196 +
7197 +struct au_dcsub_pages {
7198 +       int ndpage;
7199 +       struct au_dpage *dpages;
7200 +};
7201 +
7202 +/* ---------------------------------------------------------------------- */
7203 +
7204 +/* dcsub.c */
7205 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7206 +void au_dpages_free(struct au_dcsub_pages *dpages);
7207 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7208 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7209 +                  au_dpages_test test, void *arg);
7210 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7211 +                      int do_include, au_dpages_test test, void *arg);
7212 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7213 +                           struct dentry *dentry, int do_include);
7214 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7215 +
7216 +/* ---------------------------------------------------------------------- */
7217 +
7218 +/*
7219 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7220 + * include/linux/dcache.h. Try them (in the future).
7221 + */
7222 +
7223 +static inline int au_d_hashed_positive(struct dentry *d)
7224 +{
7225 +       int err;
7226 +       struct inode *inode = d_inode(d);
7227 +
7228 +       err = 0;
7229 +       if (unlikely(d_unhashed(d)
7230 +                    || d_is_negative(d)
7231 +                    || !inode->i_nlink))
7232 +               err = -ENOENT;
7233 +       return err;
7234 +}
7235 +
7236 +static inline int au_d_linkable(struct dentry *d)
7237 +{
7238 +       int err;
7239 +       struct inode *inode = d_inode(d);
7240 +
7241 +       err = au_d_hashed_positive(d);
7242 +       if (err
7243 +           && d_is_positive(d)
7244 +           && (inode->i_state & I_LINKABLE))
7245 +               err = 0;
7246 +       return err;
7247 +}
7248 +
7249 +static inline int au_d_alive(struct dentry *d)
7250 +{
7251 +       int err;
7252 +       struct inode *inode;
7253 +
7254 +       err = 0;
7255 +       if (!IS_ROOT(d))
7256 +               err = au_d_hashed_positive(d);
7257 +       else {
7258 +               inode = d_inode(d);
7259 +               if (unlikely(d_unlinked(d)
7260 +                            || d_is_negative(d)
7261 +                            || !inode->i_nlink))
7262 +                       err = -ENOENT;
7263 +       }
7264 +       return err;
7265 +}
7266 +
7267 +static inline int au_alive_dir(struct dentry *d)
7268 +{
7269 +       int err;
7270 +
7271 +       err = au_d_alive(d);
7272 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7273 +               err = -ENOENT;
7274 +       return err;
7275 +}
7276 +
7277 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7278 +{
7279 +       return a->len == b->len
7280 +               && !memcmp(a->name, b->name, a->len);
7281 +}
7282 +
7283 +/*
7284 + * by the commit
7285 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7286 + *                     taking d_lock
7287 + * the type of d_lockref.count became int, but the inlined function d_count()
7288 + * still returns unsigned int.
7289 + * I don't know why. Maybe it is for every d_count() users?
7290 + * Anyway au_dcount() lives on.
7291 + */
7292 +static inline int au_dcount(struct dentry *d)
7293 +{
7294 +       return (int)d_count(d);
7295 +}
7296 +
7297 +#endif /* __KERNEL__ */
7298 +#endif /* __AUFS_DCSUB_H__ */
7299 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7300 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7301 +++ linux/fs/aufs/debug.c       2020-01-27 10:57:18.168871450 +0100
7302 @@ -0,0 +1,441 @@
7303 +// SPDX-License-Identifier: GPL-2.0
7304 +/*
7305 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7306 + *
7307 + * This program, aufs is free software; you can redistribute it and/or modify
7308 + * it under the terms of the GNU General Public License as published by
7309 + * the Free Software Foundation; either version 2 of the License, or
7310 + * (at your option) any later version.
7311 + *
7312 + * This program is distributed in the hope that it will be useful,
7313 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7314 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7315 + * GNU General Public License for more details.
7316 + *
7317 + * You should have received a copy of the GNU General Public License
7318 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7319 + */
7320 +
7321 +/*
7322 + * debug print functions
7323 + */
7324 +
7325 +#include <linux/iversion.h>
7326 +#include "aufs.h"
7327 +
7328 +/* Returns 0, or -errno.  arg is in kp->arg. */
7329 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7330 +{
7331 +       int err, n;
7332 +
7333 +       err = kstrtoint(val, 0, &n);
7334 +       if (!err) {
7335 +               if (n > 0)
7336 +                       au_debug_on();
7337 +               else
7338 +                       au_debug_off();
7339 +       }
7340 +       return err;
7341 +}
7342 +
7343 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7344 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7345 +{
7346 +       atomic_t *a;
7347 +
7348 +       a = kp->arg;
7349 +       return sprintf(buffer, "%d", atomic_read(a));
7350 +}
7351 +
7352 +static struct kernel_param_ops param_ops_atomic_t = {
7353 +       .set = param_atomic_t_set,
7354 +       .get = param_atomic_t_get
7355 +       /* void (*free)(void *arg) */
7356 +};
7357 +
7358 +atomic_t aufs_debug = ATOMIC_INIT(0);
7359 +MODULE_PARM_DESC(debug, "debug print");
7360 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7361 +
7362 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7363 +char *au_plevel = KERN_DEBUG;
7364 +#define dpri(fmt, ...) do {                                    \
7365 +       if ((au_plevel                                          \
7366 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7367 +           || au_debug_test())                                 \
7368 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7369 +} while (0)
7370 +
7371 +/* ---------------------------------------------------------------------- */
7372 +
7373 +void au_dpri_whlist(struct au_nhash *whlist)
7374 +{
7375 +       unsigned long ul, n;
7376 +       struct hlist_head *head;
7377 +       struct au_vdir_wh *pos;
7378 +
7379 +       n = whlist->nh_num;
7380 +       head = whlist->nh_head;
7381 +       for (ul = 0; ul < n; ul++) {
7382 +               hlist_for_each_entry(pos, head, wh_hash)
7383 +                       dpri("b%d, %.*s, %d\n",
7384 +                            pos->wh_bindex,
7385 +                            pos->wh_str.len, pos->wh_str.name,
7386 +                            pos->wh_str.len);
7387 +               head++;
7388 +       }
7389 +}
7390 +
7391 +void au_dpri_vdir(struct au_vdir *vdir)
7392 +{
7393 +       unsigned long ul;
7394 +       union au_vdir_deblk_p p;
7395 +       unsigned char *o;
7396 +
7397 +       if (!vdir || IS_ERR(vdir)) {
7398 +               dpri("err %ld\n", PTR_ERR(vdir));
7399 +               return;
7400 +       }
7401 +
7402 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7403 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7404 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7405 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7406 +               p.deblk = vdir->vd_deblk[ul];
7407 +               o = p.deblk;
7408 +               dpri("[%lu]: %p\n", ul, o);
7409 +       }
7410 +}
7411 +
7412 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7413 +                       struct dentry *wh)
7414 +{
7415 +       char *n = NULL;
7416 +       int l = 0;
7417 +
7418 +       if (!inode || IS_ERR(inode)) {
7419 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7420 +               return -1;
7421 +       }
7422 +
7423 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7424 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7425 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7426 +       if (wh) {
7427 +               n = (void *)wh->d_name.name;
7428 +               l = wh->d_name.len;
7429 +       }
7430 +
7431 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7432 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7433 +            bindex, inode,
7434 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7435 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7436 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7437 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7438 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7439 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7440 +            inode->i_generation,
7441 +            l ? ", wh " : "", l, n);
7442 +       return 0;
7443 +}
7444 +
7445 +void au_dpri_inode(struct inode *inode)
7446 +{
7447 +       struct au_iinfo *iinfo;
7448 +       struct au_hinode *hi;
7449 +       aufs_bindex_t bindex;
7450 +       int err, hn;
7451 +
7452 +       err = do_pri_inode(-1, inode, -1, NULL);
7453 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7454 +               return;
7455 +
7456 +       iinfo = au_ii(inode);
7457 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7458 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7459 +       if (iinfo->ii_btop < 0)
7460 +               return;
7461 +       hn = 0;
7462 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7463 +               hi = au_hinode(iinfo, bindex);
7464 +               hn = !!au_hn(hi);
7465 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7466 +       }
7467 +}
7468 +
7469 +void au_dpri_dalias(struct inode *inode)
7470 +{
7471 +       struct dentry *d;
7472 +
7473 +       spin_lock(&inode->i_lock);
7474 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7475 +               au_dpri_dentry(d);
7476 +       spin_unlock(&inode->i_lock);
7477 +}
7478 +
7479 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7480 +{
7481 +       struct dentry *wh = NULL;
7482 +       int hn;
7483 +       struct inode *inode;
7484 +       struct au_iinfo *iinfo;
7485 +       struct au_hinode *hi;
7486 +
7487 +       if (!dentry || IS_ERR(dentry)) {
7488 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7489 +               return -1;
7490 +       }
7491 +       /* do not call dget_parent() here */
7492 +       /* note: access d_xxx without d_lock */
7493 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7494 +            bindex, dentry, dentry,
7495 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7496 +            au_dcount(dentry), dentry->d_flags,
7497 +            d_unhashed(dentry) ? "un" : "");
7498 +       hn = -1;
7499 +       inode = NULL;
7500 +       if (d_is_positive(dentry))
7501 +               inode = d_inode(dentry);
7502 +       if (inode
7503 +           && au_test_aufs(dentry->d_sb)
7504 +           && bindex >= 0
7505 +           && !au_is_bad_inode(inode)) {
7506 +               iinfo = au_ii(inode);
7507 +               hi = au_hinode(iinfo, bindex);
7508 +               hn = !!au_hn(hi);
7509 +               wh = hi->hi_whdentry;
7510 +       }
7511 +       do_pri_inode(bindex, inode, hn, wh);
7512 +       return 0;
7513 +}
7514 +
7515 +void au_dpri_dentry(struct dentry *dentry)
7516 +{
7517 +       struct au_dinfo *dinfo;
7518 +       aufs_bindex_t bindex;
7519 +       int err;
7520 +
7521 +       err = do_pri_dentry(-1, dentry);
7522 +       if (err || !au_test_aufs(dentry->d_sb))
7523 +               return;
7524 +
7525 +       dinfo = au_di(dentry);
7526 +       if (!dinfo)
7527 +               return;
7528 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7529 +            dinfo->di_btop, dinfo->di_bbot,
7530 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7531 +            dinfo->di_tmpfile);
7532 +       if (dinfo->di_btop < 0)
7533 +               return;
7534 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7535 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7536 +}
7537 +
7538 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7539 +{
7540 +       char a[32];
7541 +
7542 +       if (!file || IS_ERR(file)) {
7543 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7544 +               return -1;
7545 +       }
7546 +       a[0] = 0;
7547 +       if (bindex < 0
7548 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7549 +           && au_test_aufs(file->f_path.dentry->d_sb)
7550 +           && au_fi(file))
7551 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7552 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7553 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7554 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7555 +            file->f_version, file->f_pos, a);
7556 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7557 +               do_pri_dentry(bindex, file->f_path.dentry);
7558 +       return 0;
7559 +}
7560 +
7561 +void au_dpri_file(struct file *file)
7562 +{
7563 +       struct au_finfo *finfo;
7564 +       struct au_fidir *fidir;
7565 +       struct au_hfile *hfile;
7566 +       aufs_bindex_t bindex;
7567 +       int err;
7568 +
7569 +       err = do_pri_file(-1, file);
7570 +       if (err
7571 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7572 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7573 +               return;
7574 +
7575 +       finfo = au_fi(file);
7576 +       if (!finfo)
7577 +               return;
7578 +       if (finfo->fi_btop < 0)
7579 +               return;
7580 +       fidir = finfo->fi_hdir;
7581 +       if (!fidir)
7582 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7583 +       else
7584 +               for (bindex = finfo->fi_btop;
7585 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7586 +                    bindex++) {
7587 +                       hfile = fidir->fd_hfile + bindex;
7588 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7589 +               }
7590 +}
7591 +
7592 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7593 +{
7594 +       struct vfsmount *mnt;
7595 +       struct super_block *sb;
7596 +
7597 +       if (!br || IS_ERR(br))
7598 +               goto out;
7599 +       mnt = au_br_mnt(br);
7600 +       if (!mnt || IS_ERR(mnt))
7601 +               goto out;
7602 +       sb = mnt->mnt_sb;
7603 +       if (!sb || IS_ERR(sb))
7604 +               goto out;
7605 +
7606 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7607 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7608 +            "xino %d\n",
7609 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7610 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7611 +            sb->s_flags, sb->s_count,
7612 +            atomic_read(&sb->s_active),
7613 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7614 +       return 0;
7615 +
7616 +out:
7617 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7618 +       return -1;
7619 +}
7620 +
7621 +void au_dpri_sb(struct super_block *sb)
7622 +{
7623 +       struct au_sbinfo *sbinfo;
7624 +       aufs_bindex_t bindex;
7625 +       int err;
7626 +       /* to reduce stack size */
7627 +       struct {
7628 +               struct vfsmount mnt;
7629 +               struct au_branch fake;
7630 +       } *a;
7631 +
7632 +       /* this function can be called from magic sysrq */
7633 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7634 +       if (unlikely(!a)) {
7635 +               dpri("no memory\n");
7636 +               return;
7637 +       }
7638 +
7639 +       a->mnt.mnt_sb = sb;
7640 +       a->fake.br_path.mnt = &a->mnt;
7641 +       err = do_pri_br(-1, &a->fake);
7642 +       au_kfree_rcu(a);
7643 +       dpri("dev 0x%x\n", sb->s_dev);
7644 +       if (err || !au_test_aufs(sb))
7645 +               return;
7646 +
7647 +       sbinfo = au_sbi(sb);
7648 +       if (!sbinfo)
7649 +               return;
7650 +       dpri("nw %d, gen %u, kobj %d\n",
7651 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7652 +            kref_read(&sbinfo->si_kobj.kref));
7653 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7654 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7655 +}
7656 +
7657 +/* ---------------------------------------------------------------------- */
7658 +
7659 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7660 +{
7661 +       struct inode *h_inode, *inode = d_inode(dentry);
7662 +       struct dentry *h_dentry;
7663 +       aufs_bindex_t bindex, bbot, bi;
7664 +
7665 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7666 +               return;
7667 +
7668 +       bbot = au_dbbot(dentry);
7669 +       bi = au_ibbot(inode);
7670 +       if (bi < bbot)
7671 +               bbot = bi;
7672 +       bindex = au_dbtop(dentry);
7673 +       bi = au_ibtop(inode);
7674 +       if (bi > bindex)
7675 +               bindex = bi;
7676 +
7677 +       for (; bindex <= bbot; bindex++) {
7678 +               h_dentry = au_h_dptr(dentry, bindex);
7679 +               if (!h_dentry)
7680 +                       continue;
7681 +               h_inode = au_h_iptr(inode, bindex);
7682 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7683 +                       au_debug_on();
7684 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7685 +                       AuDbgDentry(dentry);
7686 +                       AuDbgInode(inode);
7687 +                       au_debug_off();
7688 +                       BUG();
7689 +               }
7690 +       }
7691 +}
7692 +
7693 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7694 +{
7695 +       int err, i, j;
7696 +       struct au_dcsub_pages dpages;
7697 +       struct au_dpage *dpage;
7698 +       struct dentry **dentries;
7699 +
7700 +       err = au_dpages_init(&dpages, GFP_NOFS);
7701 +       AuDebugOn(err);
7702 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7703 +       AuDebugOn(err);
7704 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7705 +               dpage = dpages.dpages + i;
7706 +               dentries = dpage->dentries;
7707 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7708 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7709 +       }
7710 +       au_dpages_free(&dpages);
7711 +}
7712 +
7713 +void au_dbg_verify_kthread(void)
7714 +{
7715 +       if (au_wkq_test()) {
7716 +               au_dbg_blocked();
7717 +               /*
7718 +                * It may be recursive, but udba=notify between two aufs mounts,
7719 +                * where a single ro branch is shared, is not a problem.
7720 +                */
7721 +               /* WARN_ON(1); */
7722 +       }
7723 +}
7724 +
7725 +/* ---------------------------------------------------------------------- */
7726 +
7727 +int __init au_debug_init(void)
7728 +{
7729 +       aufs_bindex_t bindex;
7730 +       struct au_vdir_destr destr;
7731 +
7732 +       bindex = -1;
7733 +       AuDebugOn(bindex >= 0);
7734 +
7735 +       destr.len = -1;
7736 +       AuDebugOn(destr.len < NAME_MAX);
7737 +
7738 +#ifdef CONFIG_4KSTACKS
7739 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7740 +#endif
7741 +
7742 +       return 0;
7743 +}
7744 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7745 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7746 +++ linux/fs/aufs/debug.h       2020-01-27 10:57:18.168871450 +0100
7747 @@ -0,0 +1,226 @@
7748 +/* SPDX-License-Identifier: GPL-2.0 */
7749 +/*
7750 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7751 + *
7752 + * This program, aufs is free software; you can redistribute it and/or modify
7753 + * it under the terms of the GNU General Public License as published by
7754 + * the Free Software Foundation; either version 2 of the License, or
7755 + * (at your option) any later version.
7756 + *
7757 + * This program is distributed in the hope that it will be useful,
7758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7760 + * GNU General Public License for more details.
7761 + *
7762 + * You should have received a copy of the GNU General Public License
7763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7764 + */
7765 +
7766 +/*
7767 + * debug print functions
7768 + */
7769 +
7770 +#ifndef __AUFS_DEBUG_H__
7771 +#define __AUFS_DEBUG_H__
7772 +
7773 +#ifdef __KERNEL__
7774 +
7775 +#include <linux/atomic.h>
7776 +#include <linux/module.h>
7777 +#include <linux/kallsyms.h>
7778 +#include <linux/sysrq.h>
7779 +
7780 +#ifdef CONFIG_AUFS_DEBUG
7781 +#define AuDebugOn(a)           BUG_ON(a)
7782 +
7783 +/* module parameter */
7784 +extern atomic_t aufs_debug;
7785 +static inline void au_debug_on(void)
7786 +{
7787 +       atomic_inc(&aufs_debug);
7788 +}
7789 +static inline void au_debug_off(void)
7790 +{
7791 +       atomic_dec_if_positive(&aufs_debug);
7792 +}
7793 +
7794 +static inline int au_debug_test(void)
7795 +{
7796 +       return atomic_read(&aufs_debug) > 0;
7797 +}
7798 +#else
7799 +#define AuDebugOn(a)           do {} while (0)
7800 +AuStubVoid(au_debug_on, void)
7801 +AuStubVoid(au_debug_off, void)
7802 +AuStubInt0(au_debug_test, void)
7803 +#endif /* CONFIG_AUFS_DEBUG */
7804 +
7805 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7806 +
7807 +/* ---------------------------------------------------------------------- */
7808 +
7809 +/* debug print */
7810 +
7811 +#define AuDbg(fmt, ...) do { \
7812 +       if (au_debug_test()) \
7813 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7814 +} while (0)
7815 +#define AuLabel(l)             AuDbg(#l "\n")
7816 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7817 +#define AuWarn1(fmt, ...) do { \
7818 +       static unsigned char _c; \
7819 +       if (!_c++) \
7820 +               pr_warn(fmt, ##__VA_ARGS__); \
7821 +} while (0)
7822 +
7823 +#define AuErr1(fmt, ...) do { \
7824 +       static unsigned char _c; \
7825 +       if (!_c++) \
7826 +               pr_err(fmt, ##__VA_ARGS__); \
7827 +} while (0)
7828 +
7829 +#define AuIOErr1(fmt, ...) do { \
7830 +       static unsigned char _c; \
7831 +       if (!_c++) \
7832 +               AuIOErr(fmt, ##__VA_ARGS__); \
7833 +} while (0)
7834 +
7835 +#define AuUnsupportMsg "This operation is not supported." \
7836 +                       " Please report this application to aufs-users ML."
7837 +#define AuUnsupport(fmt, ...) do { \
7838 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7839 +       dump_stack(); \
7840 +} while (0)
7841 +
7842 +#define AuTraceErr(e) do { \
7843 +       if (unlikely((e) < 0)) \
7844 +               AuDbg("err %d\n", (int)(e)); \
7845 +} while (0)
7846 +
7847 +#define AuTraceErrPtr(p) do { \
7848 +       if (IS_ERR(p)) \
7849 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7850 +} while (0)
7851 +
7852 +/* dirty macros for debug print, use with "%.*s" and caution */
7853 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7854 +
7855 +/* ---------------------------------------------------------------------- */
7856 +
7857 +struct dentry;
7858 +#ifdef CONFIG_AUFS_DEBUG
7859 +extern struct mutex au_dbg_mtx;
7860 +extern char *au_plevel;
7861 +struct au_nhash;
7862 +void au_dpri_whlist(struct au_nhash *whlist);
7863 +struct au_vdir;
7864 +void au_dpri_vdir(struct au_vdir *vdir);
7865 +struct inode;
7866 +void au_dpri_inode(struct inode *inode);
7867 +void au_dpri_dalias(struct inode *inode);
7868 +void au_dpri_dentry(struct dentry *dentry);
7869 +struct file;
7870 +void au_dpri_file(struct file *filp);
7871 +struct super_block;
7872 +void au_dpri_sb(struct super_block *sb);
7873 +
7874 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7875 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7876 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7877 +void au_dbg_verify_kthread(void);
7878 +
7879 +int __init au_debug_init(void);
7880 +
7881 +#define AuDbgWhlist(w) do { \
7882 +       mutex_lock(&au_dbg_mtx); \
7883 +       AuDbg(#w "\n"); \
7884 +       au_dpri_whlist(w); \
7885 +       mutex_unlock(&au_dbg_mtx); \
7886 +} while (0)
7887 +
7888 +#define AuDbgVdir(v) do { \
7889 +       mutex_lock(&au_dbg_mtx); \
7890 +       AuDbg(#v "\n"); \
7891 +       au_dpri_vdir(v); \
7892 +       mutex_unlock(&au_dbg_mtx); \
7893 +} while (0)
7894 +
7895 +#define AuDbgInode(i) do { \
7896 +       mutex_lock(&au_dbg_mtx); \
7897 +       AuDbg(#i "\n"); \
7898 +       au_dpri_inode(i); \
7899 +       mutex_unlock(&au_dbg_mtx); \
7900 +} while (0)
7901 +
7902 +#define AuDbgDAlias(i) do { \
7903 +       mutex_lock(&au_dbg_mtx); \
7904 +       AuDbg(#i "\n"); \
7905 +       au_dpri_dalias(i); \
7906 +       mutex_unlock(&au_dbg_mtx); \
7907 +} while (0)
7908 +
7909 +#define AuDbgDentry(d) do { \
7910 +       mutex_lock(&au_dbg_mtx); \
7911 +       AuDbg(#d "\n"); \
7912 +       au_dpri_dentry(d); \
7913 +       mutex_unlock(&au_dbg_mtx); \
7914 +} while (0)
7915 +
7916 +#define AuDbgFile(f) do { \
7917 +       mutex_lock(&au_dbg_mtx); \
7918 +       AuDbg(#f "\n"); \
7919 +       au_dpri_file(f); \
7920 +       mutex_unlock(&au_dbg_mtx); \
7921 +} while (0)
7922 +
7923 +#define AuDbgSb(sb) do { \
7924 +       mutex_lock(&au_dbg_mtx); \
7925 +       AuDbg(#sb "\n"); \
7926 +       au_dpri_sb(sb); \
7927 +       mutex_unlock(&au_dbg_mtx); \
7928 +} while (0)
7929 +
7930 +#define AuDbgSym(addr) do {                            \
7931 +       char sym[KSYM_SYMBOL_LEN];                      \
7932 +       sprint_symbol(sym, (unsigned long)addr);        \
7933 +       AuDbg("%s\n", sym);                             \
7934 +} while (0)
7935 +#else
7936 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7937 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7938 +AuStubVoid(au_dbg_verify_kthread, void)
7939 +AuStubInt0(__init au_debug_init, void)
7940 +
7941 +#define AuDbgWhlist(w)         do {} while (0)
7942 +#define AuDbgVdir(v)           do {} while (0)
7943 +#define AuDbgInode(i)          do {} while (0)
7944 +#define AuDbgDAlias(i)         do {} while (0)
7945 +#define AuDbgDentry(d)         do {} while (0)
7946 +#define AuDbgFile(f)           do {} while (0)
7947 +#define AuDbgSb(sb)            do {} while (0)
7948 +#define AuDbgSym(addr)         do {} while (0)
7949 +#endif /* CONFIG_AUFS_DEBUG */
7950 +
7951 +/* ---------------------------------------------------------------------- */
7952 +
7953 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7954 +int __init au_sysrq_init(void);
7955 +void au_sysrq_fin(void);
7956 +
7957 +#ifdef CONFIG_HW_CONSOLE
7958 +#define au_dbg_blocked() do { \
7959 +       WARN_ON(1); \
7960 +       handle_sysrq('w'); \
7961 +} while (0)
7962 +#else
7963 +AuStubVoid(au_dbg_blocked, void)
7964 +#endif
7965 +
7966 +#else
7967 +AuStubInt0(__init au_sysrq_init, void)
7968 +AuStubVoid(au_sysrq_fin, void)
7969 +AuStubVoid(au_dbg_blocked, void)
7970 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7971 +
7972 +#endif /* __KERNEL__ */
7973 +#endif /* __AUFS_DEBUG_H__ */
7974 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7975 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7976 +++ linux/fs/aufs/dentry.c      2020-01-27 10:57:18.168871450 +0100
7977 @@ -0,0 +1,1154 @@
7978 +// SPDX-License-Identifier: GPL-2.0
7979 +/*
7980 + * Copyright (C) 2005-2020 Junjiro R. Okajima
7981 + *
7982 + * This program, aufs is free software; you can redistribute it and/or modify
7983 + * it under the terms of the GNU General Public License as published by
7984 + * the Free Software Foundation; either version 2 of the License, or
7985 + * (at your option) any later version.
7986 + *
7987 + * This program is distributed in the hope that it will be useful,
7988 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7989 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7990 + * GNU General Public License for more details.
7991 + *
7992 + * You should have received a copy of the GNU General Public License
7993 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7994 + */
7995 +
7996 +/*
7997 + * lookup and dentry operations
7998 + */
7999 +
8000 +#include <linux/iversion.h>
8001 +#include <linux/namei.h>
8002 +#include "aufs.h"
8003 +
8004 +/*
8005 + * returns positive/negative dentry, NULL or an error.
8006 + * NULL means whiteout-ed or not-found.
8007 + */
8008 +static struct dentry*
8009 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8010 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8011 +{
8012 +       struct dentry *h_dentry;
8013 +       struct inode *h_inode;
8014 +       struct au_branch *br;
8015 +       int wh_found, opq;
8016 +       unsigned char wh_able;
8017 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8018 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8019 +                                                         IGNORE_PERM);
8020 +
8021 +       wh_found = 0;
8022 +       br = au_sbr(dentry->d_sb, bindex);
8023 +       wh_able = !!au_br_whable(br->br_perm);
8024 +       if (wh_able)
8025 +               wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
8026 +       h_dentry = ERR_PTR(wh_found);
8027 +       if (!wh_found)
8028 +               goto real_lookup;
8029 +       if (unlikely(wh_found < 0))
8030 +               goto out;
8031 +
8032 +       /* We found a whiteout */
8033 +       /* au_set_dbbot(dentry, bindex); */
8034 +       au_set_dbwh(dentry, bindex);
8035 +       if (!allow_neg)
8036 +               return NULL; /* success */
8037 +
8038 +real_lookup:
8039 +       if (!ignore_perm)
8040 +               h_dentry = vfsub_lkup_one(args->name, h_parent);
8041 +       else
8042 +               h_dentry = au_sio_lkup_one(args->name, h_parent);
8043 +       if (IS_ERR(h_dentry)) {
8044 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8045 +                   && !allow_neg)
8046 +                       h_dentry = NULL;
8047 +               goto out;
8048 +       }
8049 +
8050 +       h_inode = d_inode(h_dentry);
8051 +       if (d_is_negative(h_dentry)) {
8052 +               if (!allow_neg)
8053 +                       goto out_neg;
8054 +       } else if (wh_found
8055 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8056 +               goto out_neg;
8057 +       else if (au_ftest_lkup(args->flags, DIRREN)
8058 +                /* && h_inode */
8059 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8060 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8061 +                     (unsigned long long)h_inode->i_ino);
8062 +               goto out_neg;
8063 +       }
8064 +
8065 +       if (au_dbbot(dentry) <= bindex)
8066 +               au_set_dbbot(dentry, bindex);
8067 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8068 +               au_set_dbtop(dentry, bindex);
8069 +       au_set_h_dptr(dentry, bindex, h_dentry);
8070 +
8071 +       if (!d_is_dir(h_dentry)
8072 +           || !wh_able
8073 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8074 +               goto out; /* success */
8075 +
8076 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8077 +       opq = au_diropq_test(h_dentry);
8078 +       inode_unlock_shared(h_inode);
8079 +       if (opq > 0)
8080 +               au_set_dbdiropq(dentry, bindex);
8081 +       else if (unlikely(opq < 0)) {
8082 +               au_set_h_dptr(dentry, bindex, NULL);
8083 +               h_dentry = ERR_PTR(opq);
8084 +       }
8085 +       goto out;
8086 +
8087 +out_neg:
8088 +       dput(h_dentry);
8089 +       h_dentry = NULL;
8090 +out:
8091 +       return h_dentry;
8092 +}
8093 +
8094 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8095 +{
8096 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8097 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8098 +               return -EPERM;
8099 +       return 0;
8100 +}
8101 +
8102 +/*
8103 + * returns the number of lower positive dentries,
8104 + * otherwise an error.
8105 + * can be called at unlinking with @type is zero.
8106 + */
8107 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8108 +                  unsigned int flags)
8109 +{
8110 +       int npositive, err;
8111 +       aufs_bindex_t bindex, btail, bdiropq;
8112 +       unsigned char isdir, dirperm1, dirren;
8113 +       struct au_do_lookup_args args = {
8114 +               .flags          = flags,
8115 +               .name           = &dentry->d_name
8116 +       };
8117 +       struct dentry *parent;
8118 +       struct super_block *sb;
8119 +
8120 +       sb = dentry->d_sb;
8121 +       err = au_test_shwh(sb, args.name);
8122 +       if (unlikely(err))
8123 +               goto out;
8124 +
8125 +       err = au_wh_name_alloc(&args.whname, args.name);
8126 +       if (unlikely(err))
8127 +               goto out;
8128 +
8129 +       isdir = !!d_is_dir(dentry);
8130 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8131 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8132 +       if (dirren)
8133 +               au_fset_lkup(args.flags, DIRREN);
8134 +
8135 +       npositive = 0;
8136 +       parent = dget_parent(dentry);
8137 +       btail = au_dbtaildir(parent);
8138 +       for (bindex = btop; bindex <= btail; bindex++) {
8139 +               struct dentry *h_parent, *h_dentry;
8140 +               struct inode *h_inode, *h_dir;
8141 +               struct au_branch *br;
8142 +
8143 +               h_dentry = au_h_dptr(dentry, bindex);
8144 +               if (h_dentry) {
8145 +                       if (d_is_positive(h_dentry))
8146 +                               npositive++;
8147 +                       break;
8148 +               }
8149 +               h_parent = au_h_dptr(parent, bindex);
8150 +               if (!h_parent || !d_is_dir(h_parent))
8151 +                       continue;
8152 +
8153 +               if (dirren) {
8154 +                       /* if the inum matches, then use the prepared name */
8155 +                       err = au_dr_lkup_name(&args, bindex);
8156 +                       if (unlikely(err))
8157 +                               goto out_parent;
8158 +               }
8159 +
8160 +               h_dir = d_inode(h_parent);
8161 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8162 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8163 +               inode_unlock_shared(h_dir);
8164 +               err = PTR_ERR(h_dentry);
8165 +               if (IS_ERR(h_dentry))
8166 +                       goto out_parent;
8167 +               if (h_dentry)
8168 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8169 +               if (dirperm1)
8170 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8171 +
8172 +               if (au_dbwh(dentry) == bindex)
8173 +                       break;
8174 +               if (!h_dentry)
8175 +                       continue;
8176 +               if (d_is_negative(h_dentry))
8177 +                       continue;
8178 +               h_inode = d_inode(h_dentry);
8179 +               npositive++;
8180 +               if (!args.type)
8181 +                       args.type = h_inode->i_mode & S_IFMT;
8182 +               if (args.type != S_IFDIR)
8183 +                       break;
8184 +               else if (isdir) {
8185 +                       /* the type of lower may be different */
8186 +                       bdiropq = au_dbdiropq(dentry);
8187 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8188 +                               break;
8189 +               }
8190 +               br = au_sbr(sb, bindex);
8191 +               if (dirren
8192 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8193 +                                          /*add_ent*/NULL)) {
8194 +                       /* prepare next name to lookup */
8195 +                       err = au_dr_lkup(&args, dentry, bindex);
8196 +                       if (unlikely(err))
8197 +                               goto out_parent;
8198 +               }
8199 +       }
8200 +
8201 +       if (npositive) {
8202 +               AuLabel(positive);
8203 +               au_update_dbtop(dentry);
8204 +       }
8205 +       err = npositive;
8206 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8207 +                    && au_dbtop(dentry) < 0)) {
8208 +               err = -EIO;
8209 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8210 +                       dentry, err);
8211 +       }
8212 +
8213 +out_parent:
8214 +       dput(parent);
8215 +       au_kfree_try_rcu(args.whname.name);
8216 +       if (dirren)
8217 +               au_dr_lkup_fin(&args);
8218 +out:
8219 +       return err;
8220 +}
8221 +
8222 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
8223 +{
8224 +       struct dentry *dentry;
8225 +       int wkq_err;
8226 +
8227 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
8228 +               dentry = vfsub_lkup_one(name, parent);
8229 +       else {
8230 +               struct vfsub_lkup_one_args args = {
8231 +                       .errp   = &dentry,
8232 +                       .name   = name,
8233 +                       .parent = parent
8234 +               };
8235 +
8236 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8237 +               if (unlikely(wkq_err))
8238 +                       dentry = ERR_PTR(wkq_err);
8239 +       }
8240 +
8241 +       return dentry;
8242 +}
8243 +
8244 +/*
8245 + * lookup @dentry on @bindex which should be negative.
8246 + */
8247 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8248 +{
8249 +       int err;
8250 +       struct dentry *parent, *h_parent, *h_dentry;
8251 +       struct au_branch *br;
8252 +
8253 +       parent = dget_parent(dentry);
8254 +       h_parent = au_h_dptr(parent, bindex);
8255 +       br = au_sbr(dentry->d_sb, bindex);
8256 +       if (wh)
8257 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
8258 +       else
8259 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
8260 +       err = PTR_ERR(h_dentry);
8261 +       if (IS_ERR(h_dentry))
8262 +               goto out;
8263 +       if (unlikely(d_is_positive(h_dentry))) {
8264 +               err = -EIO;
8265 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8266 +               dput(h_dentry);
8267 +               goto out;
8268 +       }
8269 +
8270 +       err = 0;
8271 +       if (bindex < au_dbtop(dentry))
8272 +               au_set_dbtop(dentry, bindex);
8273 +       if (au_dbbot(dentry) < bindex)
8274 +               au_set_dbbot(dentry, bindex);
8275 +       au_set_h_dptr(dentry, bindex, h_dentry);
8276 +
8277 +out:
8278 +       dput(parent);
8279 +       return err;
8280 +}
8281 +
8282 +/* ---------------------------------------------------------------------- */
8283 +
8284 +/* subset of struct inode */
8285 +struct au_iattr {
8286 +       unsigned long           i_ino;
8287 +       /* unsigned int         i_nlink; */
8288 +       kuid_t                  i_uid;
8289 +       kgid_t                  i_gid;
8290 +       u64                     i_version;
8291 +/*
8292 +       loff_t                  i_size;
8293 +       blkcnt_t                i_blocks;
8294 +*/
8295 +       umode_t                 i_mode;
8296 +};
8297 +
8298 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8299 +{
8300 +       ia->i_ino = h_inode->i_ino;
8301 +       /* ia->i_nlink = h_inode->i_nlink; */
8302 +       ia->i_uid = h_inode->i_uid;
8303 +       ia->i_gid = h_inode->i_gid;
8304 +       ia->i_version = inode_query_iversion(h_inode);
8305 +/*
8306 +       ia->i_size = h_inode->i_size;
8307 +       ia->i_blocks = h_inode->i_blocks;
8308 +*/
8309 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8310 +}
8311 +
8312 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8313 +{
8314 +       return ia->i_ino != h_inode->i_ino
8315 +               /* || ia->i_nlink != h_inode->i_nlink */
8316 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8317 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8318 +               || !inode_eq_iversion(h_inode, ia->i_version)
8319 +/*
8320 +               || ia->i_size != h_inode->i_size
8321 +               || ia->i_blocks != h_inode->i_blocks
8322 +*/
8323 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8324 +}
8325 +
8326 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8327 +                             struct au_branch *br)
8328 +{
8329 +       int err;
8330 +       struct au_iattr ia;
8331 +       struct inode *h_inode;
8332 +       struct dentry *h_d;
8333 +       struct super_block *h_sb;
8334 +
8335 +       err = 0;
8336 +       memset(&ia, -1, sizeof(ia));
8337 +       h_sb = h_dentry->d_sb;
8338 +       h_inode = NULL;
8339 +       if (d_is_positive(h_dentry)) {
8340 +               h_inode = d_inode(h_dentry);
8341 +               au_iattr_save(&ia, h_inode);
8342 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8343 +               /* nfs d_revalidate may return 0 for negative dentry */
8344 +               /* fuse d_revalidate always return 0 for negative dentry */
8345 +               goto out;
8346 +
8347 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8348 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8349 +       err = PTR_ERR(h_d);
8350 +       if (IS_ERR(h_d))
8351 +               goto out;
8352 +
8353 +       err = 0;
8354 +       if (unlikely(h_d != h_dentry
8355 +                    || d_inode(h_d) != h_inode
8356 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8357 +               err = au_busy_or_stale();
8358 +       dput(h_d);
8359 +
8360 +out:
8361 +       AuTraceErr(err);
8362 +       return err;
8363 +}
8364 +
8365 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8366 +               struct dentry *h_parent, struct au_branch *br)
8367 +{
8368 +       int err;
8369 +
8370 +       err = 0;
8371 +       if (udba == AuOpt_UDBA_REVAL
8372 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8373 +               IMustLock(h_dir);
8374 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8375 +       } else if (udba != AuOpt_UDBA_NONE)
8376 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8377 +
8378 +       return err;
8379 +}
8380 +
8381 +/* ---------------------------------------------------------------------- */
8382 +
8383 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8384 +{
8385 +       int err;
8386 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8387 +       struct au_hdentry tmp, *p, *q;
8388 +       struct au_dinfo *dinfo;
8389 +       struct super_block *sb;
8390 +
8391 +       DiMustWriteLock(dentry);
8392 +
8393 +       sb = dentry->d_sb;
8394 +       dinfo = au_di(dentry);
8395 +       bbot = dinfo->di_bbot;
8396 +       bwh = dinfo->di_bwh;
8397 +       bdiropq = dinfo->di_bdiropq;
8398 +       bindex = dinfo->di_btop;
8399 +       p = au_hdentry(dinfo, bindex);
8400 +       for (; bindex <= bbot; bindex++, p++) {
8401 +               if (!p->hd_dentry)
8402 +                       continue;
8403 +
8404 +               new_bindex = au_br_index(sb, p->hd_id);
8405 +               if (new_bindex == bindex)
8406 +                       continue;
8407 +
8408 +               if (dinfo->di_bwh == bindex)
8409 +                       bwh = new_bindex;
8410 +               if (dinfo->di_bdiropq == bindex)
8411 +                       bdiropq = new_bindex;
8412 +               if (new_bindex < 0) {
8413 +                       au_hdput(p);
8414 +                       p->hd_dentry = NULL;
8415 +                       continue;
8416 +               }
8417 +
8418 +               /* swap two lower dentries, and loop again */
8419 +               q = au_hdentry(dinfo, new_bindex);
8420 +               tmp = *q;
8421 +               *q = *p;
8422 +               *p = tmp;
8423 +               if (tmp.hd_dentry) {
8424 +                       bindex--;
8425 +                       p--;
8426 +               }
8427 +       }
8428 +
8429 +       dinfo->di_bwh = -1;
8430 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8431 +               dinfo->di_bwh = bwh;
8432 +
8433 +       dinfo->di_bdiropq = -1;
8434 +       if (bdiropq >= 0
8435 +           && bdiropq <= au_sbbot(sb)
8436 +           && au_sbr_whable(sb, bdiropq))
8437 +               dinfo->di_bdiropq = bdiropq;
8438 +
8439 +       err = -EIO;
8440 +       dinfo->di_btop = -1;
8441 +       dinfo->di_bbot = -1;
8442 +       bbot = au_dbbot(parent);
8443 +       bindex = 0;
8444 +       p = au_hdentry(dinfo, bindex);
8445 +       for (; bindex <= bbot; bindex++, p++)
8446 +               if (p->hd_dentry) {
8447 +                       dinfo->di_btop = bindex;
8448 +                       break;
8449 +               }
8450 +
8451 +       if (dinfo->di_btop >= 0) {
8452 +               bindex = bbot;
8453 +               p = au_hdentry(dinfo, bindex);
8454 +               for (; bindex >= 0; bindex--, p--)
8455 +                       if (p->hd_dentry) {
8456 +                               dinfo->di_bbot = bindex;
8457 +                               err = 0;
8458 +                               break;
8459 +                       }
8460 +       }
8461 +
8462 +       return err;
8463 +}
8464 +
8465 +static void au_do_hide(struct dentry *dentry)
8466 +{
8467 +       struct inode *inode;
8468 +
8469 +       if (d_really_is_positive(dentry)) {
8470 +               inode = d_inode(dentry);
8471 +               if (!d_is_dir(dentry)) {
8472 +                       if (inode->i_nlink && !d_unhashed(dentry))
8473 +                               drop_nlink(inode);
8474 +               } else {
8475 +                       clear_nlink(inode);
8476 +                       /* stop next lookup */
8477 +                       inode->i_flags |= S_DEAD;
8478 +               }
8479 +               smp_mb(); /* necessary? */
8480 +       }
8481 +       d_drop(dentry);
8482 +}
8483 +
8484 +static int au_hide_children(struct dentry *parent)
8485 +{
8486 +       int err, i, j, ndentry;
8487 +       struct au_dcsub_pages dpages;
8488 +       struct au_dpage *dpage;
8489 +       struct dentry *dentry;
8490 +
8491 +       err = au_dpages_init(&dpages, GFP_NOFS);
8492 +       if (unlikely(err))
8493 +               goto out;
8494 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8495 +       if (unlikely(err))
8496 +               goto out_dpages;
8497 +
8498 +       /* in reverse order */
8499 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8500 +               dpage = dpages.dpages + i;
8501 +               ndentry = dpage->ndentry;
8502 +               for (j = ndentry - 1; j >= 0; j--) {
8503 +                       dentry = dpage->dentries[j];
8504 +                       if (dentry != parent)
8505 +                               au_do_hide(dentry);
8506 +               }
8507 +       }
8508 +
8509 +out_dpages:
8510 +       au_dpages_free(&dpages);
8511 +out:
8512 +       return err;
8513 +}
8514 +
8515 +static void au_hide(struct dentry *dentry)
8516 +{
8517 +       int err;
8518 +
8519 +       AuDbgDentry(dentry);
8520 +       if (d_is_dir(dentry)) {
8521 +               /* shrink_dcache_parent(dentry); */
8522 +               err = au_hide_children(dentry);
8523 +               if (unlikely(err))
8524 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8525 +                               dentry, err);
8526 +       }
8527 +       au_do_hide(dentry);
8528 +}
8529 +
8530 +/*
8531 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8532 + *
8533 + * a dirty branch is added
8534 + * - on the top of layers
8535 + * - in the middle of layers
8536 + * - to the bottom of layers
8537 + *
8538 + * on the added branch there exists
8539 + * - a whiteout
8540 + * - a diropq
8541 + * - a same named entry
8542 + *   + exist
8543 + *     * negative --> positive
8544 + *     * positive --> positive
8545 + *      - type is unchanged
8546 + *      - type is changed
8547 + *   + doesn't exist
8548 + *     * negative --> negative
8549 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8550 + * - none
8551 + */
8552 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8553 +                              struct au_dinfo *tmp)
8554 +{
8555 +       int err;
8556 +       aufs_bindex_t bindex, bbot;
8557 +       struct {
8558 +               struct dentry *dentry;
8559 +               struct inode *inode;
8560 +               mode_t mode;
8561 +       } orig_h, tmp_h = {
8562 +               .dentry = NULL
8563 +       };
8564 +       struct au_hdentry *hd;
8565 +       struct inode *inode, *h_inode;
8566 +       struct dentry *h_dentry;
8567 +
8568 +       err = 0;
8569 +       AuDebugOn(dinfo->di_btop < 0);
8570 +       orig_h.mode = 0;
8571 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8572 +       orig_h.inode = NULL;
8573 +       if (d_is_positive(orig_h.dentry)) {
8574 +               orig_h.inode = d_inode(orig_h.dentry);
8575 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8576 +       }
8577 +       if (tmp->di_btop >= 0) {
8578 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8579 +               if (d_is_positive(tmp_h.dentry)) {
8580 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8581 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8582 +               }
8583 +       }
8584 +
8585 +       inode = NULL;
8586 +       if (d_really_is_positive(dentry))
8587 +               inode = d_inode(dentry);
8588 +       if (!orig_h.inode) {
8589 +               AuDbg("negative originally\n");
8590 +               if (inode) {
8591 +                       au_hide(dentry);
8592 +                       goto out;
8593 +               }
8594 +               AuDebugOn(inode);
8595 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8596 +               AuDebugOn(dinfo->di_bdiropq != -1);
8597 +
8598 +               if (!tmp_h.inode) {
8599 +                       AuDbg("negative --> negative\n");
8600 +                       /* should have only one negative lower */
8601 +                       if (tmp->di_btop >= 0
8602 +                           && tmp->di_btop < dinfo->di_btop) {
8603 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8604 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8605 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8606 +                               au_di_cp(dinfo, tmp);
8607 +                               hd = au_hdentry(tmp, tmp->di_btop);
8608 +                               au_set_h_dptr(dentry, tmp->di_btop,
8609 +                                             dget(hd->hd_dentry));
8610 +                       }
8611 +                       au_dbg_verify_dinode(dentry);
8612 +               } else {
8613 +                       AuDbg("negative --> positive\n");
8614 +                       /*
8615 +                        * similar to the behaviour of creating with bypassing
8616 +                        * aufs.
8617 +                        * unhash it in order to force an error in the
8618 +                        * succeeding create operation.
8619 +                        * we should not set S_DEAD here.
8620 +                        */
8621 +                       d_drop(dentry);
8622 +                       /* au_di_swap(tmp, dinfo); */
8623 +                       au_dbg_verify_dinode(dentry);
8624 +               }
8625 +       } else {
8626 +               AuDbg("positive originally\n");
8627 +               /* inode may be NULL */
8628 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8629 +               if (!tmp_h.inode) {
8630 +                       AuDbg("positive --> negative\n");
8631 +                       /* or bypassing aufs */
8632 +                       au_hide(dentry);
8633 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8634 +                               dinfo->di_bwh = tmp->di_bwh;
8635 +                       if (inode)
8636 +                               err = au_refresh_hinode_self(inode);
8637 +                       au_dbg_verify_dinode(dentry);
8638 +               } else if (orig_h.mode == tmp_h.mode) {
8639 +                       AuDbg("positive --> positive, same type\n");
8640 +                       if (!S_ISDIR(orig_h.mode)
8641 +                           && dinfo->di_btop > tmp->di_btop) {
8642 +                               /*
8643 +                                * similar to the behaviour of removing and
8644 +                                * creating.
8645 +                                */
8646 +                               au_hide(dentry);
8647 +                               if (inode)
8648 +                                       err = au_refresh_hinode_self(inode);
8649 +                               au_dbg_verify_dinode(dentry);
8650 +                       } else {
8651 +                               /* fill empty slots */
8652 +                               if (dinfo->di_btop > tmp->di_btop)
8653 +                                       dinfo->di_btop = tmp->di_btop;
8654 +                               if (dinfo->di_bbot < tmp->di_bbot)
8655 +                                       dinfo->di_bbot = tmp->di_bbot;
8656 +                               dinfo->di_bwh = tmp->di_bwh;
8657 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8658 +                               bbot = dinfo->di_bbot;
8659 +                               bindex = tmp->di_btop;
8660 +                               hd = au_hdentry(tmp, bindex);
8661 +                               for (; bindex <= bbot; bindex++, hd++) {
8662 +                                       if (au_h_dptr(dentry, bindex))
8663 +                                               continue;
8664 +                                       h_dentry = hd->hd_dentry;
8665 +                                       if (!h_dentry)
8666 +                                               continue;
8667 +                                       AuDebugOn(d_is_negative(h_dentry));
8668 +                                       h_inode = d_inode(h_dentry);
8669 +                                       AuDebugOn(orig_h.mode
8670 +                                                 != (h_inode->i_mode
8671 +                                                     & S_IFMT));
8672 +                                       au_set_h_dptr(dentry, bindex,
8673 +                                                     dget(h_dentry));
8674 +                               }
8675 +                               if (inode)
8676 +                                       err = au_refresh_hinode(inode, dentry);
8677 +                               au_dbg_verify_dinode(dentry);
8678 +                       }
8679 +               } else {
8680 +                       AuDbg("positive --> positive, different type\n");
8681 +                       /* similar to the behaviour of removing and creating */
8682 +                       au_hide(dentry);
8683 +                       if (inode)
8684 +                               err = au_refresh_hinode_self(inode);
8685 +                       au_dbg_verify_dinode(dentry);
8686 +               }
8687 +       }
8688 +
8689 +out:
8690 +       return err;
8691 +}
8692 +
8693 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8694 +{
8695 +       const struct dentry_operations *dop
8696 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8697 +       static const unsigned int mask
8698 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8699 +
8700 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8701 +
8702 +       if (dentry->d_op == dop)
8703 +               return;
8704 +
8705 +       AuDbg("%pd\n", dentry);
8706 +       spin_lock(&dentry->d_lock);
8707 +       if (dop == &aufs_dop)
8708 +               dentry->d_flags |= mask;
8709 +       else
8710 +               dentry->d_flags &= ~mask;
8711 +       dentry->d_op = dop;
8712 +       spin_unlock(&dentry->d_lock);
8713 +}
8714 +
8715 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8716 +{
8717 +       int err, ebrange, nbr;
8718 +       unsigned int sigen;
8719 +       struct au_dinfo *dinfo, *tmp;
8720 +       struct super_block *sb;
8721 +       struct inode *inode;
8722 +
8723 +       DiMustWriteLock(dentry);
8724 +       AuDebugOn(IS_ROOT(dentry));
8725 +       AuDebugOn(d_really_is_negative(parent));
8726 +
8727 +       sb = dentry->d_sb;
8728 +       sigen = au_sigen(sb);
8729 +       err = au_digen_test(parent, sigen);
8730 +       if (unlikely(err))
8731 +               goto out;
8732 +
8733 +       nbr = au_sbbot(sb) + 1;
8734 +       dinfo = au_di(dentry);
8735 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8736 +       if (unlikely(err))
8737 +               goto out;
8738 +       ebrange = au_dbrange_test(dentry);
8739 +       if (!ebrange)
8740 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8741 +
8742 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8743 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8744 +               if (d_really_is_positive(dentry)) {
8745 +                       inode = d_inode(dentry);
8746 +                       err = au_refresh_hinode_self(inode);
8747 +               }
8748 +               au_dbg_verify_dinode(dentry);
8749 +               if (!err)
8750 +                       goto out_dgen; /* success */
8751 +               goto out;
8752 +       }
8753 +
8754 +       /* temporary dinfo */
8755 +       AuDbgDentry(dentry);
8756 +       err = -ENOMEM;
8757 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8758 +       if (unlikely(!tmp))
8759 +               goto out;
8760 +       au_di_swap(tmp, dinfo);
8761 +       /* returns the number of positive dentries */
8762 +       /*
8763 +        * if current working dir is removed, it returns an error.
8764 +        * but the dentry is legal.
8765 +        */
8766 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8767 +       AuDbgDentry(dentry);
8768 +       au_di_swap(tmp, dinfo);
8769 +       if (err == -ENOENT)
8770 +               err = 0;
8771 +       if (err >= 0) {
8772 +               /* compare/refresh by dinfo */
8773 +               AuDbgDentry(dentry);
8774 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8775 +               au_dbg_verify_dinode(dentry);
8776 +               AuTraceErr(err);
8777 +       }
8778 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8779 +       au_rw_write_unlock(&tmp->di_rwsem);
8780 +       au_di_free(tmp);
8781 +       if (unlikely(err))
8782 +               goto out;
8783 +
8784 +out_dgen:
8785 +       au_update_digen(dentry);
8786 +out:
8787 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8788 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8789 +               AuDbgDentry(dentry);
8790 +       }
8791 +       AuTraceErr(err);
8792 +       return err;
8793 +}
8794 +
8795 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8796 +                          struct dentry *dentry, aufs_bindex_t bindex)
8797 +{
8798 +       int err, valid;
8799 +
8800 +       err = 0;
8801 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8802 +               goto out;
8803 +
8804 +       AuDbg("b%d\n", bindex);
8805 +       /*
8806 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8807 +        * due to whiteout and branch permission.
8808 +        */
8809 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8810 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8811 +       /* it may return tri-state */
8812 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8813 +
8814 +       if (unlikely(valid < 0))
8815 +               err = valid;
8816 +       else if (!valid)
8817 +               err = -EINVAL;
8818 +
8819 +out:
8820 +       AuTraceErr(err);
8821 +       return err;
8822 +}
8823 +
8824 +/* todo: remove this */
8825 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8826 +                         unsigned int flags, int do_udba, int dirren)
8827 +{
8828 +       int err;
8829 +       umode_t mode, h_mode;
8830 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8831 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8832 +       struct inode *h_inode, *h_cached_inode;
8833 +       struct dentry *h_dentry;
8834 +       struct qstr *name, *h_name;
8835 +
8836 +       err = 0;
8837 +       plus = 0;
8838 +       mode = 0;
8839 +       ibs = -1;
8840 +       ibe = -1;
8841 +       unhashed = !!d_unhashed(dentry);
8842 +       is_root = !!IS_ROOT(dentry);
8843 +       name = &dentry->d_name;
8844 +       tmpfile = au_di(dentry)->di_tmpfile;
8845 +
8846 +       /*
8847 +        * Theoretically, REVAL test should be unnecessary in case of
8848 +        * {FS,I}NOTIFY.
8849 +        * But {fs,i}notify doesn't fire some necessary events,
8850 +        *      IN_ATTRIB for atime/nlink/pageio
8851 +        * Let's do REVAL test too.
8852 +        */
8853 +       if (do_udba && inode) {
8854 +               mode = (inode->i_mode & S_IFMT);
8855 +               plus = (inode->i_nlink > 0);
8856 +               ibs = au_ibtop(inode);
8857 +               ibe = au_ibbot(inode);
8858 +       }
8859 +
8860 +       btop = au_dbtop(dentry);
8861 +       btail = btop;
8862 +       if (inode && S_ISDIR(inode->i_mode))
8863 +               btail = au_dbtaildir(dentry);
8864 +       for (bindex = btop; bindex <= btail; bindex++) {
8865 +               h_dentry = au_h_dptr(dentry, bindex);
8866 +               if (!h_dentry)
8867 +                       continue;
8868 +
8869 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8870 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8871 +               spin_lock(&h_dentry->d_lock);
8872 +               h_name = &h_dentry->d_name;
8873 +               if (unlikely(do_udba
8874 +                            && !is_root
8875 +                            && ((!h_nfs
8876 +                                 && (unhashed != !!d_unhashed(h_dentry)
8877 +                                     || (!tmpfile && !dirren
8878 +                                         && !au_qstreq(name, h_name))
8879 +                                         ))
8880 +                                || (h_nfs
8881 +                                    && !(flags & LOOKUP_OPEN)
8882 +                                    && (h_dentry->d_flags
8883 +                                        & DCACHE_NFSFS_RENAMED)))
8884 +                           )) {
8885 +                       int h_unhashed;
8886 +
8887 +                       h_unhashed = d_unhashed(h_dentry);
8888 +                       spin_unlock(&h_dentry->d_lock);
8889 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8890 +                             unhashed, h_unhashed, dentry, h_dentry);
8891 +                       goto err;
8892 +               }
8893 +               spin_unlock(&h_dentry->d_lock);
8894 +
8895 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8896 +               if (unlikely(err))
8897 +                       /* do not goto err, to keep the errno */
8898 +                       break;
8899 +
8900 +               /* todo: plink too? */
8901 +               if (!do_udba)
8902 +                       continue;
8903 +
8904 +               /* UDBA tests */
8905 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8906 +                       goto err;
8907 +
8908 +               h_inode = NULL;
8909 +               if (d_is_positive(h_dentry))
8910 +                       h_inode = d_inode(h_dentry);
8911 +               h_plus = plus;
8912 +               h_mode = mode;
8913 +               h_cached_inode = h_inode;
8914 +               if (h_inode) {
8915 +                       h_mode = (h_inode->i_mode & S_IFMT);
8916 +                       h_plus = (h_inode->i_nlink > 0);
8917 +               }
8918 +               if (inode && ibs <= bindex && bindex <= ibe)
8919 +                       h_cached_inode = au_h_iptr(inode, bindex);
8920 +
8921 +               if (!h_nfs) {
8922 +                       if (unlikely(plus != h_plus && !tmpfile))
8923 +                               goto err;
8924 +               } else {
8925 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8926 +                                    && !is_root
8927 +                                    && !IS_ROOT(h_dentry)
8928 +                                    && unhashed != d_unhashed(h_dentry)))
8929 +                               goto err;
8930 +               }
8931 +               if (unlikely(mode != h_mode
8932 +                            || h_cached_inode != h_inode))
8933 +                       goto err;
8934 +               continue;
8935 +
8936 +err:
8937 +               err = -EINVAL;
8938 +               break;
8939 +       }
8940 +
8941 +       AuTraceErr(err);
8942 +       return err;
8943 +}
8944 +
8945 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8946 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8947 +{
8948 +       int err;
8949 +       struct dentry *parent;
8950 +
8951 +       if (!au_digen_test(dentry, sigen))
8952 +               return 0;
8953 +
8954 +       parent = dget_parent(dentry);
8955 +       di_read_lock_parent(parent, AuLock_IR);
8956 +       AuDebugOn(au_digen_test(parent, sigen));
8957 +       au_dbg_verify_gen(parent, sigen);
8958 +       err = au_refresh_dentry(dentry, parent);
8959 +       di_read_unlock(parent, AuLock_IR);
8960 +       dput(parent);
8961 +       AuTraceErr(err);
8962 +       return err;
8963 +}
8964 +
8965 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8966 +{
8967 +       int err;
8968 +       struct dentry *d, *parent;
8969 +
8970 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8971 +               return simple_reval_dpath(dentry, sigen);
8972 +
8973 +       /* slow loop, keep it simple and stupid */
8974 +       /* cf: au_cpup_dirs() */
8975 +       err = 0;
8976 +       parent = NULL;
8977 +       while (au_digen_test(dentry, sigen)) {
8978 +               d = dentry;
8979 +               while (1) {
8980 +                       dput(parent);
8981 +                       parent = dget_parent(d);
8982 +                       if (!au_digen_test(parent, sigen))
8983 +                               break;
8984 +                       d = parent;
8985 +               }
8986 +
8987 +               if (d != dentry)
8988 +                       di_write_lock_child2(d);
8989 +
8990 +               /* someone might update our dentry while we were sleeping */
8991 +               if (au_digen_test(d, sigen)) {
8992 +                       /*
8993 +                        * todo: consolidate with simple_reval_dpath(),
8994 +                        * do_refresh() and au_reval_for_attr().
8995 +                        */
8996 +                       di_read_lock_parent(parent, AuLock_IR);
8997 +                       err = au_refresh_dentry(d, parent);
8998 +                       di_read_unlock(parent, AuLock_IR);
8999 +               }
9000 +
9001 +               if (d != dentry)
9002 +                       di_write_unlock(d);
9003 +               dput(parent);
9004 +               if (unlikely(err))
9005 +                       break;
9006 +       }
9007 +
9008 +       return err;
9009 +}
9010 +
9011 +/*
9012 + * if valid returns 1, otherwise 0.
9013 + */
9014 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9015 +{
9016 +       int valid, err;
9017 +       unsigned int sigen;
9018 +       unsigned char do_udba, dirren;
9019 +       struct super_block *sb;
9020 +       struct inode *inode;
9021 +
9022 +       /* todo: support rcu-walk? */
9023 +       if (flags & LOOKUP_RCU)
9024 +               return -ECHILD;
9025 +
9026 +       valid = 0;
9027 +       if (unlikely(!au_di(dentry)))
9028 +               goto out;
9029 +
9030 +       valid = 1;
9031 +       sb = dentry->d_sb;
9032 +       /*
9033 +        * todo: very ugly
9034 +        * i_mutex of parent dir may be held,
9035 +        * but we should not return 'invalid' due to busy.
9036 +        */
9037 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9038 +       if (unlikely(err)) {
9039 +               valid = err;
9040 +               AuTraceErr(err);
9041 +               goto out;
9042 +       }
9043 +       inode = NULL;
9044 +       if (d_really_is_positive(dentry))
9045 +               inode = d_inode(dentry);
9046 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9047 +               err = -EINVAL;
9048 +               AuTraceErr(err);
9049 +               goto out_dgrade;
9050 +       }
9051 +       if (unlikely(au_dbrange_test(dentry))) {
9052 +               err = -EINVAL;
9053 +               AuTraceErr(err);
9054 +               goto out_dgrade;
9055 +       }
9056 +
9057 +       sigen = au_sigen(sb);
9058 +       if (au_digen_test(dentry, sigen)) {
9059 +               AuDebugOn(IS_ROOT(dentry));
9060 +               err = au_reval_dpath(dentry, sigen);
9061 +               if (unlikely(err)) {
9062 +                       AuTraceErr(err);
9063 +                       goto out_dgrade;
9064 +               }
9065 +       }
9066 +       di_downgrade_lock(dentry, AuLock_IR);
9067 +
9068 +       err = -EINVAL;
9069 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9070 +           && inode
9071 +           && !(inode->i_state && I_LINKABLE)
9072 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9073 +               AuTraceErr(err);
9074 +               goto out_inval;
9075 +       }
9076 +
9077 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9078 +       if (do_udba && inode) {
9079 +               aufs_bindex_t btop = au_ibtop(inode);
9080 +               struct inode *h_inode;
9081 +
9082 +               if (btop >= 0) {
9083 +                       h_inode = au_h_iptr(inode, btop);
9084 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9085 +                               AuTraceErr(err);
9086 +                               goto out_inval;
9087 +                       }
9088 +               }
9089 +       }
9090 +
9091 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9092 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9093 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9094 +               err = -EIO;
9095 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9096 +                     dentry, err);
9097 +       }
9098 +       goto out_inval;
9099 +
9100 +out_dgrade:
9101 +       di_downgrade_lock(dentry, AuLock_IR);
9102 +out_inval:
9103 +       aufs_read_unlock(dentry, AuLock_IR);
9104 +       AuTraceErr(err);
9105 +       valid = !err;
9106 +out:
9107 +       if (!valid) {
9108 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9109 +               d_drop(dentry);
9110 +       }
9111 +       return valid;
9112 +}
9113 +
9114 +static void aufs_d_release(struct dentry *dentry)
9115 +{
9116 +       if (au_di(dentry)) {
9117 +               au_di_fin(dentry);
9118 +               au_hn_di_reinit(dentry);
9119 +       }
9120 +}
9121 +
9122 +const struct dentry_operations aufs_dop = {
9123 +       .d_revalidate           = aufs_d_revalidate,
9124 +       .d_weak_revalidate      = aufs_d_revalidate,
9125 +       .d_release              = aufs_d_release
9126 +};
9127 +
9128 +/* aufs_dop without d_revalidate */
9129 +const struct dentry_operations aufs_dop_noreval = {
9130 +       .d_release              = aufs_d_release
9131 +};
9132 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9133 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9134 +++ linux/fs/aufs/dentry.h      2020-01-27 10:57:18.168871450 +0100
9135 @@ -0,0 +1,268 @@
9136 +/* SPDX-License-Identifier: GPL-2.0 */
9137 +/*
9138 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9139 + *
9140 + * This program, aufs is free software; you can redistribute it and/or modify
9141 + * it under the terms of the GNU General Public License as published by
9142 + * the Free Software Foundation; either version 2 of the License, or
9143 + * (at your option) any later version.
9144 + *
9145 + * This program is distributed in the hope that it will be useful,
9146 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9147 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9148 + * GNU General Public License for more details.
9149 + *
9150 + * You should have received a copy of the GNU General Public License
9151 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9152 + */
9153 +
9154 +/*
9155 + * lookup and dentry operations
9156 + */
9157 +
9158 +#ifndef __AUFS_DENTRY_H__
9159 +#define __AUFS_DENTRY_H__
9160 +
9161 +#ifdef __KERNEL__
9162 +
9163 +#include <linux/dcache.h>
9164 +#include "dirren.h"
9165 +#include "rwsem.h"
9166 +
9167 +struct au_hdentry {
9168 +       struct dentry           *hd_dentry;
9169 +       aufs_bindex_t           hd_id;
9170 +};
9171 +
9172 +struct au_dinfo {
9173 +       atomic_t                di_generation;
9174 +
9175 +       struct au_rwsem         di_rwsem;
9176 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9177 +       unsigned char           di_tmpfile; /* to allow the different name */
9178 +       struct au_hdentry       *di_hdentry;
9179 +       struct rcu_head         rcu;
9180 +} ____cacheline_aligned_in_smp;
9181 +
9182 +/* ---------------------------------------------------------------------- */
9183 +
9184 +/* flags for au_lkup_dentry() */
9185 +#define AuLkup_ALLOW_NEG       1
9186 +#define AuLkup_IGNORE_PERM     (1 << 1)
9187 +#define AuLkup_DIRREN          (1 << 2)
9188 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9189 +#define au_fset_lkup(flags, name) \
9190 +       do { (flags) |= AuLkup_##name; } while (0)
9191 +#define au_fclr_lkup(flags, name) \
9192 +       do { (flags) &= ~AuLkup_##name; } while (0)
9193 +
9194 +#ifndef CONFIG_AUFS_DIRREN
9195 +#undef AuLkup_DIRREN
9196 +#define AuLkup_DIRREN 0
9197 +#endif
9198 +
9199 +struct au_do_lookup_args {
9200 +       unsigned int            flags;
9201 +       mode_t                  type;
9202 +       struct qstr             whname, *name;
9203 +       struct au_dr_lookup     dirren;
9204 +};
9205 +
9206 +/* ---------------------------------------------------------------------- */
9207 +
9208 +/* dentry.c */
9209 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9210 +struct au_branch;
9211 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
9212 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9213 +               struct dentry *h_parent, struct au_branch *br);
9214 +
9215 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9216 +                  unsigned int flags);
9217 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9218 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9219 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9220 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9221 +
9222 +/* dinfo.c */
9223 +void au_di_init_once(void *_di);
9224 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9225 +void au_di_free(struct au_dinfo *dinfo);
9226 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9227 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9228 +int au_di_init(struct dentry *dentry);
9229 +void au_di_fin(struct dentry *dentry);
9230 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9231 +
9232 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9233 +void di_read_unlock(struct dentry *d, int flags);
9234 +void di_downgrade_lock(struct dentry *d, int flags);
9235 +void di_write_lock(struct dentry *d, unsigned int lsc);
9236 +void di_write_unlock(struct dentry *d);
9237 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9238 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9239 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9240 +
9241 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9242 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9243 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9244 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9245 +
9246 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9247 +                  struct dentry *h_dentry);
9248 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9249 +int au_dbrange_test(struct dentry *dentry);
9250 +void au_update_digen(struct dentry *dentry);
9251 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9252 +void au_update_dbtop(struct dentry *dentry);
9253 +void au_update_dbbot(struct dentry *dentry);
9254 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9255 +
9256 +/* ---------------------------------------------------------------------- */
9257 +
9258 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9259 +{
9260 +       return dentry->d_fsdata;
9261 +}
9262 +
9263 +/* ---------------------------------------------------------------------- */
9264 +
9265 +/* lock subclass for dinfo */
9266 +enum {
9267 +       AuLsc_DI_CHILD,         /* child first */
9268 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9269 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9270 +       AuLsc_DI_PARENT,
9271 +       AuLsc_DI_PARENT2,
9272 +       AuLsc_DI_PARENT3,
9273 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9274 +};
9275 +
9276 +/*
9277 + * di_read_lock_child, di_write_lock_child,
9278 + * di_read_lock_child2, di_write_lock_child2,
9279 + * di_read_lock_child3, di_write_lock_child3,
9280 + * di_read_lock_parent, di_write_lock_parent,
9281 + * di_read_lock_parent2, di_write_lock_parent2,
9282 + * di_read_lock_parent3, di_write_lock_parent3,
9283 + */
9284 +#define AuReadLockFunc(name, lsc) \
9285 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9286 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9287 +
9288 +#define AuWriteLockFunc(name, lsc) \
9289 +static inline void di_write_lock_##name(struct dentry *d) \
9290 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9291 +
9292 +#define AuRWLockFuncs(name, lsc) \
9293 +       AuReadLockFunc(name, lsc) \
9294 +       AuWriteLockFunc(name, lsc)
9295 +
9296 +AuRWLockFuncs(child, CHILD);
9297 +AuRWLockFuncs(child2, CHILD2);
9298 +AuRWLockFuncs(child3, CHILD3);
9299 +AuRWLockFuncs(parent, PARENT);
9300 +AuRWLockFuncs(parent2, PARENT2);
9301 +AuRWLockFuncs(parent3, PARENT3);
9302 +
9303 +#undef AuReadLockFunc
9304 +#undef AuWriteLockFunc
9305 +#undef AuRWLockFuncs
9306 +
9307 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9308 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9309 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9310 +
9311 +/* ---------------------------------------------------------------------- */
9312 +
9313 +/* todo: memory barrier? */
9314 +static inline unsigned int au_digen(struct dentry *d)
9315 +{
9316 +       return atomic_read(&au_di(d)->di_generation);
9317 +}
9318 +
9319 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9320 +{
9321 +       hdentry->hd_dentry = NULL;
9322 +}
9323 +
9324 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9325 +                                           aufs_bindex_t bindex)
9326 +{
9327 +       return di->di_hdentry + bindex;
9328 +}
9329 +
9330 +static inline void au_hdput(struct au_hdentry *hd)
9331 +{
9332 +       if (hd)
9333 +               dput(hd->hd_dentry);
9334 +}
9335 +
9336 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9337 +{
9338 +       DiMustAnyLock(dentry);
9339 +       return au_di(dentry)->di_btop;
9340 +}
9341 +
9342 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9343 +{
9344 +       DiMustAnyLock(dentry);
9345 +       return au_di(dentry)->di_bbot;
9346 +}
9347 +
9348 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9349 +{
9350 +       DiMustAnyLock(dentry);
9351 +       return au_di(dentry)->di_bwh;
9352 +}
9353 +
9354 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9355 +{
9356 +       DiMustAnyLock(dentry);
9357 +       return au_di(dentry)->di_bdiropq;
9358 +}
9359 +
9360 +/* todo: hard/soft set? */
9361 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9362 +{
9363 +       DiMustWriteLock(dentry);
9364 +       au_di(dentry)->di_btop = bindex;
9365 +}
9366 +
9367 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9368 +{
9369 +       DiMustWriteLock(dentry);
9370 +       au_di(dentry)->di_bbot = bindex;
9371 +}
9372 +
9373 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9374 +{
9375 +       DiMustWriteLock(dentry);
9376 +       /* dbwh can be outside of btop - bbot range */
9377 +       au_di(dentry)->di_bwh = bindex;
9378 +}
9379 +
9380 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9381 +{
9382 +       DiMustWriteLock(dentry);
9383 +       au_di(dentry)->di_bdiropq = bindex;
9384 +}
9385 +
9386 +/* ---------------------------------------------------------------------- */
9387 +
9388 +#ifdef CONFIG_AUFS_HNOTIFY
9389 +static inline void au_digen_dec(struct dentry *d)
9390 +{
9391 +       atomic_dec(&au_di(d)->di_generation);
9392 +}
9393 +
9394 +static inline void au_hn_di_reinit(struct dentry *dentry)
9395 +{
9396 +       dentry->d_fsdata = NULL;
9397 +}
9398 +#else
9399 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9400 +#endif /* CONFIG_AUFS_HNOTIFY */
9401 +
9402 +#endif /* __KERNEL__ */
9403 +#endif /* __AUFS_DENTRY_H__ */
9404 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9405 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9406 +++ linux/fs/aufs/dinfo.c       2020-01-27 10:57:18.168871450 +0100
9407 @@ -0,0 +1,554 @@
9408 +// SPDX-License-Identifier: GPL-2.0
9409 +/*
9410 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9411 + *
9412 + * This program, aufs is free software; you can redistribute it and/or modify
9413 + * it under the terms of the GNU General Public License as published by
9414 + * the Free Software Foundation; either version 2 of the License, or
9415 + * (at your option) any later version.
9416 + *
9417 + * This program is distributed in the hope that it will be useful,
9418 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9419 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9420 + * GNU General Public License for more details.
9421 + *
9422 + * You should have received a copy of the GNU General Public License
9423 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9424 + */
9425 +
9426 +/*
9427 + * dentry private data
9428 + */
9429 +
9430 +#include "aufs.h"
9431 +
9432 +void au_di_init_once(void *_dinfo)
9433 +{
9434 +       struct au_dinfo *dinfo = _dinfo;
9435 +
9436 +       au_rw_init(&dinfo->di_rwsem);
9437 +}
9438 +
9439 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9440 +{
9441 +       struct au_dinfo *dinfo;
9442 +       int nbr, i;
9443 +
9444 +       dinfo = au_cache_alloc_dinfo();
9445 +       if (unlikely(!dinfo))
9446 +               goto out;
9447 +
9448 +       nbr = au_sbbot(sb) + 1;
9449 +       if (nbr <= 0)
9450 +               nbr = 1;
9451 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9452 +       if (dinfo->di_hdentry) {
9453 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9454 +               dinfo->di_btop = -1;
9455 +               dinfo->di_bbot = -1;
9456 +               dinfo->di_bwh = -1;
9457 +               dinfo->di_bdiropq = -1;
9458 +               dinfo->di_tmpfile = 0;
9459 +               for (i = 0; i < nbr; i++)
9460 +                       dinfo->di_hdentry[i].hd_id = -1;
9461 +               goto out;
9462 +       }
9463 +
9464 +       au_cache_free_dinfo(dinfo);
9465 +       dinfo = NULL;
9466 +
9467 +out:
9468 +       return dinfo;
9469 +}
9470 +
9471 +void au_di_free(struct au_dinfo *dinfo)
9472 +{
9473 +       struct au_hdentry *p;
9474 +       aufs_bindex_t bbot, bindex;
9475 +
9476 +       /* dentry may not be revalidated */
9477 +       bindex = dinfo->di_btop;
9478 +       if (bindex >= 0) {
9479 +               bbot = dinfo->di_bbot;
9480 +               p = au_hdentry(dinfo, bindex);
9481 +               while (bindex++ <= bbot)
9482 +                       au_hdput(p++);
9483 +       }
9484 +       au_kfree_try_rcu(dinfo->di_hdentry);
9485 +       au_cache_free_dinfo(dinfo);
9486 +}
9487 +
9488 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9489 +{
9490 +       struct au_hdentry *p;
9491 +       aufs_bindex_t bi;
9492 +
9493 +       AuRwMustWriteLock(&a->di_rwsem);
9494 +       AuRwMustWriteLock(&b->di_rwsem);
9495 +
9496 +#define DiSwap(v, name)                                \
9497 +       do {                                    \
9498 +               v = a->di_##name;               \
9499 +               a->di_##name = b->di_##name;    \
9500 +               b->di_##name = v;               \
9501 +       } while (0)
9502 +
9503 +       DiSwap(p, hdentry);
9504 +       DiSwap(bi, btop);
9505 +       DiSwap(bi, bbot);
9506 +       DiSwap(bi, bwh);
9507 +       DiSwap(bi, bdiropq);
9508 +       /* smp_mb(); */
9509 +
9510 +#undef DiSwap
9511 +}
9512 +
9513 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9514 +{
9515 +       AuRwMustWriteLock(&dst->di_rwsem);
9516 +       AuRwMustWriteLock(&src->di_rwsem);
9517 +
9518 +       dst->di_btop = src->di_btop;
9519 +       dst->di_bbot = src->di_bbot;
9520 +       dst->di_bwh = src->di_bwh;
9521 +       dst->di_bdiropq = src->di_bdiropq;
9522 +       /* smp_mb(); */
9523 +}
9524 +
9525 +int au_di_init(struct dentry *dentry)
9526 +{
9527 +       int err;
9528 +       struct super_block *sb;
9529 +       struct au_dinfo *dinfo;
9530 +
9531 +       err = 0;
9532 +       sb = dentry->d_sb;
9533 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9534 +       if (dinfo) {
9535 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9536 +               /* smp_mb(); */ /* atomic_set */
9537 +               dentry->d_fsdata = dinfo;
9538 +       } else
9539 +               err = -ENOMEM;
9540 +
9541 +       return err;
9542 +}
9543 +
9544 +void au_di_fin(struct dentry *dentry)
9545 +{
9546 +       struct au_dinfo *dinfo;
9547 +
9548 +       dinfo = au_di(dentry);
9549 +       AuRwDestroy(&dinfo->di_rwsem);
9550 +       au_di_free(dinfo);
9551 +}
9552 +
9553 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9554 +{
9555 +       int err, sz;
9556 +       struct au_hdentry *hdp;
9557 +
9558 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9559 +
9560 +       err = -ENOMEM;
9561 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9562 +       if (!sz)
9563 +               sz = sizeof(*hdp);
9564 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9565 +                          may_shrink);
9566 +       if (hdp) {
9567 +               dinfo->di_hdentry = hdp;
9568 +               err = 0;
9569 +       }
9570 +
9571 +       return err;
9572 +}
9573 +
9574 +/* ---------------------------------------------------------------------- */
9575 +
9576 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9577 +{
9578 +       switch (lsc) {
9579 +       case AuLsc_DI_CHILD:
9580 +               ii_write_lock_child(inode);
9581 +               break;
9582 +       case AuLsc_DI_CHILD2:
9583 +               ii_write_lock_child2(inode);
9584 +               break;
9585 +       case AuLsc_DI_CHILD3:
9586 +               ii_write_lock_child3(inode);
9587 +               break;
9588 +       case AuLsc_DI_PARENT:
9589 +               ii_write_lock_parent(inode);
9590 +               break;
9591 +       case AuLsc_DI_PARENT2:
9592 +               ii_write_lock_parent2(inode);
9593 +               break;
9594 +       case AuLsc_DI_PARENT3:
9595 +               ii_write_lock_parent3(inode);
9596 +               break;
9597 +       default:
9598 +               BUG();
9599 +       }
9600 +}
9601 +
9602 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9603 +{
9604 +       switch (lsc) {
9605 +       case AuLsc_DI_CHILD:
9606 +               ii_read_lock_child(inode);
9607 +               break;
9608 +       case AuLsc_DI_CHILD2:
9609 +               ii_read_lock_child2(inode);
9610 +               break;
9611 +       case AuLsc_DI_CHILD3:
9612 +               ii_read_lock_child3(inode);
9613 +               break;
9614 +       case AuLsc_DI_PARENT:
9615 +               ii_read_lock_parent(inode);
9616 +               break;
9617 +       case AuLsc_DI_PARENT2:
9618 +               ii_read_lock_parent2(inode);
9619 +               break;
9620 +       case AuLsc_DI_PARENT3:
9621 +               ii_read_lock_parent3(inode);
9622 +               break;
9623 +       default:
9624 +               BUG();
9625 +       }
9626 +}
9627 +
9628 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9629 +{
9630 +       struct inode *inode;
9631 +
9632 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9633 +       if (d_really_is_positive(d)) {
9634 +               inode = d_inode(d);
9635 +               if (au_ftest_lock(flags, IW))
9636 +                       do_ii_write_lock(inode, lsc);
9637 +               else if (au_ftest_lock(flags, IR))
9638 +                       do_ii_read_lock(inode, lsc);
9639 +       }
9640 +}
9641 +
9642 +void di_read_unlock(struct dentry *d, int flags)
9643 +{
9644 +       struct inode *inode;
9645 +
9646 +       if (d_really_is_positive(d)) {
9647 +               inode = d_inode(d);
9648 +               if (au_ftest_lock(flags, IW)) {
9649 +                       au_dbg_verify_dinode(d);
9650 +                       ii_write_unlock(inode);
9651 +               } else if (au_ftest_lock(flags, IR)) {
9652 +                       au_dbg_verify_dinode(d);
9653 +                       ii_read_unlock(inode);
9654 +               }
9655 +       }
9656 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9657 +}
9658 +
9659 +void di_downgrade_lock(struct dentry *d, int flags)
9660 +{
9661 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9662 +               ii_downgrade_lock(d_inode(d));
9663 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9664 +}
9665 +
9666 +void di_write_lock(struct dentry *d, unsigned int lsc)
9667 +{
9668 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9669 +       if (d_really_is_positive(d))
9670 +               do_ii_write_lock(d_inode(d), lsc);
9671 +}
9672 +
9673 +void di_write_unlock(struct dentry *d)
9674 +{
9675 +       au_dbg_verify_dinode(d);
9676 +       if (d_really_is_positive(d))
9677 +               ii_write_unlock(d_inode(d));
9678 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9679 +}
9680 +
9681 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9682 +{
9683 +       AuDebugOn(d1 == d2
9684 +                 || d_inode(d1) == d_inode(d2)
9685 +                 || d1->d_sb != d2->d_sb);
9686 +
9687 +       if ((isdir && au_test_subdir(d1, d2))
9688 +           || d1 < d2) {
9689 +               di_write_lock_child(d1);
9690 +               di_write_lock_child2(d2);
9691 +       } else {
9692 +               di_write_lock_child(d2);
9693 +               di_write_lock_child2(d1);
9694 +       }
9695 +}
9696 +
9697 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9698 +{
9699 +       AuDebugOn(d1 == d2
9700 +                 || d_inode(d1) == d_inode(d2)
9701 +                 || d1->d_sb != d2->d_sb);
9702 +
9703 +       if ((isdir && au_test_subdir(d1, d2))
9704 +           || d1 < d2) {
9705 +               di_write_lock_parent(d1);
9706 +               di_write_lock_parent2(d2);
9707 +       } else {
9708 +               di_write_lock_parent(d2);
9709 +               di_write_lock_parent2(d1);
9710 +       }
9711 +}
9712 +
9713 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9714 +{
9715 +       di_write_unlock(d1);
9716 +       if (d_inode(d1) == d_inode(d2))
9717 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9718 +       else
9719 +               di_write_unlock(d2);
9720 +}
9721 +
9722 +/* ---------------------------------------------------------------------- */
9723 +
9724 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9725 +{
9726 +       struct dentry *d;
9727 +
9728 +       DiMustAnyLock(dentry);
9729 +
9730 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9731 +               return NULL;
9732 +       AuDebugOn(bindex < 0);
9733 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9734 +       AuDebugOn(d && au_dcount(d) <= 0);
9735 +       return d;
9736 +}
9737 +
9738 +/*
9739 + * extended version of au_h_dptr().
9740 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9741 + * error.
9742 + */
9743 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9744 +{
9745 +       struct dentry *h_dentry;
9746 +       struct inode *inode, *h_inode;
9747 +
9748 +       AuDebugOn(d_really_is_negative(dentry));
9749 +
9750 +       h_dentry = NULL;
9751 +       if (au_dbtop(dentry) <= bindex
9752 +           && bindex <= au_dbbot(dentry))
9753 +               h_dentry = au_h_dptr(dentry, bindex);
9754 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9755 +               dget(h_dentry);
9756 +               goto out; /* success */
9757 +       }
9758 +
9759 +       inode = d_inode(dentry);
9760 +       AuDebugOn(bindex < au_ibtop(inode));
9761 +       AuDebugOn(au_ibbot(inode) < bindex);
9762 +       h_inode = au_h_iptr(inode, bindex);
9763 +       h_dentry = d_find_alias(h_inode);
9764 +       if (h_dentry) {
9765 +               if (!IS_ERR(h_dentry)) {
9766 +                       if (!au_d_linkable(h_dentry))
9767 +                               goto out; /* success */
9768 +                       dput(h_dentry);
9769 +               } else
9770 +                       goto out;
9771 +       }
9772 +
9773 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9774 +               h_dentry = au_plink_lkup(inode, bindex);
9775 +               AuDebugOn(!h_dentry);
9776 +               if (!IS_ERR(h_dentry)) {
9777 +                       if (!au_d_hashed_positive(h_dentry))
9778 +                               goto out; /* success */
9779 +                       dput(h_dentry);
9780 +                       h_dentry = NULL;
9781 +               }
9782 +       }
9783 +
9784 +out:
9785 +       AuDbgDentry(h_dentry);
9786 +       return h_dentry;
9787 +}
9788 +
9789 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9790 +{
9791 +       aufs_bindex_t bbot, bwh;
9792 +
9793 +       bbot = au_dbbot(dentry);
9794 +       if (0 <= bbot) {
9795 +               bwh = au_dbwh(dentry);
9796 +               if (!bwh)
9797 +                       return bwh;
9798 +               if (0 < bwh && bwh < bbot)
9799 +                       return bwh - 1;
9800 +       }
9801 +       return bbot;
9802 +}
9803 +
9804 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9805 +{
9806 +       aufs_bindex_t bbot, bopq;
9807 +
9808 +       bbot = au_dbtail(dentry);
9809 +       if (0 <= bbot) {
9810 +               bopq = au_dbdiropq(dentry);
9811 +               if (0 <= bopq && bopq < bbot)
9812 +                       bbot = bopq;
9813 +       }
9814 +       return bbot;
9815 +}
9816 +
9817 +/* ---------------------------------------------------------------------- */
9818 +
9819 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9820 +                  struct dentry *h_dentry)
9821 +{
9822 +       struct au_dinfo *dinfo;
9823 +       struct au_hdentry *hd;
9824 +       struct au_branch *br;
9825 +
9826 +       DiMustWriteLock(dentry);
9827 +
9828 +       dinfo = au_di(dentry);
9829 +       hd = au_hdentry(dinfo, bindex);
9830 +       au_hdput(hd);
9831 +       hd->hd_dentry = h_dentry;
9832 +       if (h_dentry) {
9833 +               br = au_sbr(dentry->d_sb, bindex);
9834 +               hd->hd_id = br->br_id;
9835 +       }
9836 +}
9837 +
9838 +int au_dbrange_test(struct dentry *dentry)
9839 +{
9840 +       int err;
9841 +       aufs_bindex_t btop, bbot;
9842 +
9843 +       err = 0;
9844 +       btop = au_dbtop(dentry);
9845 +       bbot = au_dbbot(dentry);
9846 +       if (btop >= 0)
9847 +               AuDebugOn(bbot < 0 && btop > bbot);
9848 +       else {
9849 +               err = -EIO;
9850 +               AuDebugOn(bbot >= 0);
9851 +       }
9852 +
9853 +       return err;
9854 +}
9855 +
9856 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9857 +{
9858 +       int err;
9859 +
9860 +       err = 0;
9861 +       if (unlikely(au_digen(dentry) != sigen
9862 +                    || au_iigen_test(d_inode(dentry), sigen)))
9863 +               err = -EIO;
9864 +
9865 +       return err;
9866 +}
9867 +
9868 +void au_update_digen(struct dentry *dentry)
9869 +{
9870 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9871 +       /* smp_mb(); */ /* atomic_set */
9872 +}
9873 +
9874 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9875 +{
9876 +       struct au_dinfo *dinfo;
9877 +       struct dentry *h_d;
9878 +       struct au_hdentry *hdp;
9879 +       aufs_bindex_t bindex, bbot;
9880 +
9881 +       DiMustWriteLock(dentry);
9882 +
9883 +       dinfo = au_di(dentry);
9884 +       if (!dinfo || dinfo->di_btop < 0)
9885 +               return;
9886 +
9887 +       if (do_put_zero) {
9888 +               bbot = dinfo->di_bbot;
9889 +               bindex = dinfo->di_btop;
9890 +               hdp = au_hdentry(dinfo, bindex);
9891 +               for (; bindex <= bbot; bindex++, hdp++) {
9892 +                       h_d = hdp->hd_dentry;
9893 +                       if (h_d && d_is_negative(h_d))
9894 +                               au_set_h_dptr(dentry, bindex, NULL);
9895 +               }
9896 +       }
9897 +
9898 +       dinfo->di_btop = 0;
9899 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9900 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9901 +               if (hdp->hd_dentry)
9902 +                       break;
9903 +       if (dinfo->di_btop > dinfo->di_bbot) {
9904 +               dinfo->di_btop = -1;
9905 +               dinfo->di_bbot = -1;
9906 +               return;
9907 +       }
9908 +
9909 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9910 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9911 +               if (hdp->hd_dentry)
9912 +                       break;
9913 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9914 +}
9915 +
9916 +void au_update_dbtop(struct dentry *dentry)
9917 +{
9918 +       aufs_bindex_t bindex, bbot;
9919 +       struct dentry *h_dentry;
9920 +
9921 +       bbot = au_dbbot(dentry);
9922 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9923 +               h_dentry = au_h_dptr(dentry, bindex);
9924 +               if (!h_dentry)
9925 +                       continue;
9926 +               if (d_is_positive(h_dentry)) {
9927 +                       au_set_dbtop(dentry, bindex);
9928 +                       return;
9929 +               }
9930 +               au_set_h_dptr(dentry, bindex, NULL);
9931 +       }
9932 +}
9933 +
9934 +void au_update_dbbot(struct dentry *dentry)
9935 +{
9936 +       aufs_bindex_t bindex, btop;
9937 +       struct dentry *h_dentry;
9938 +
9939 +       btop = au_dbtop(dentry);
9940 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9941 +               h_dentry = au_h_dptr(dentry, bindex);
9942 +               if (!h_dentry)
9943 +                       continue;
9944 +               if (d_is_positive(h_dentry)) {
9945 +                       au_set_dbbot(dentry, bindex);
9946 +                       return;
9947 +               }
9948 +               au_set_h_dptr(dentry, bindex, NULL);
9949 +       }
9950 +}
9951 +
9952 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9953 +{
9954 +       aufs_bindex_t bindex, bbot;
9955 +
9956 +       bbot = au_dbbot(dentry);
9957 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9958 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9959 +                       return bindex;
9960 +       return -1;
9961 +}
9962 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9963 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9964 +++ linux/fs/aufs/dir.c 2020-01-27 10:57:18.168871450 +0100
9965 @@ -0,0 +1,763 @@
9966 +// SPDX-License-Identifier: GPL-2.0
9967 +/*
9968 + * Copyright (C) 2005-2020 Junjiro R. Okajima
9969 + *
9970 + * This program, aufs is free software; you can redistribute it and/or modify
9971 + * it under the terms of the GNU General Public License as published by
9972 + * the Free Software Foundation; either version 2 of the License, or
9973 + * (at your option) any later version.
9974 + *
9975 + * This program is distributed in the hope that it will be useful,
9976 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9977 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9978 + * GNU General Public License for more details.
9979 + *
9980 + * You should have received a copy of the GNU General Public License
9981 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9982 + */
9983 +
9984 +/*
9985 + * directory operations
9986 + */
9987 +
9988 +#include <linux/fs_stack.h>
9989 +#include <linux/iversion.h>
9990 +#include "aufs.h"
9991 +
9992 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9993 +{
9994 +       unsigned int nlink;
9995 +
9996 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9997 +
9998 +       nlink = dir->i_nlink;
9999 +       nlink += h_dir->i_nlink - 2;
10000 +       if (h_dir->i_nlink < 2)
10001 +               nlink += 2;
10002 +       smp_mb(); /* for i_nlink */
10003 +       /* 0 can happen in revaliding */
10004 +       set_nlink(dir, nlink);
10005 +}
10006 +
10007 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10008 +{
10009 +       unsigned int nlink;
10010 +
10011 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10012 +
10013 +       nlink = dir->i_nlink;
10014 +       nlink -= h_dir->i_nlink - 2;
10015 +       if (h_dir->i_nlink < 2)
10016 +               nlink -= 2;
10017 +       smp_mb(); /* for i_nlink */
10018 +       /* nlink == 0 means the branch-fs is broken */
10019 +       set_nlink(dir, nlink);
10020 +}
10021 +
10022 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10023 +{
10024 +       loff_t sz;
10025 +       aufs_bindex_t bindex, bbot;
10026 +       struct file *h_file;
10027 +       struct dentry *h_dentry;
10028 +
10029 +       sz = 0;
10030 +       if (file) {
10031 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10032 +
10033 +               bbot = au_fbbot_dir(file);
10034 +               for (bindex = au_fbtop(file);
10035 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10036 +                    bindex++) {
10037 +                       h_file = au_hf_dir(file, bindex);
10038 +                       if (h_file && file_inode(h_file))
10039 +                               sz += vfsub_f_size_read(h_file);
10040 +               }
10041 +       } else {
10042 +               AuDebugOn(!dentry);
10043 +               AuDebugOn(!d_is_dir(dentry));
10044 +
10045 +               bbot = au_dbtaildir(dentry);
10046 +               for (bindex = au_dbtop(dentry);
10047 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10048 +                    bindex++) {
10049 +                       h_dentry = au_h_dptr(dentry, bindex);
10050 +                       if (h_dentry && d_is_positive(h_dentry))
10051 +                               sz += i_size_read(d_inode(h_dentry));
10052 +               }
10053 +       }
10054 +       if (sz < KMALLOC_MAX_SIZE)
10055 +               sz = roundup_pow_of_two(sz);
10056 +       if (sz > KMALLOC_MAX_SIZE)
10057 +               sz = KMALLOC_MAX_SIZE;
10058 +       else if (sz < NAME_MAX) {
10059 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10060 +               sz = AUFS_RDBLK_DEF;
10061 +       }
10062 +       return sz;
10063 +}
10064 +
10065 +struct au_dir_ts_arg {
10066 +       struct dentry *dentry;
10067 +       aufs_bindex_t brid;
10068 +};
10069 +
10070 +static void au_do_dir_ts(void *arg)
10071 +{
10072 +       struct au_dir_ts_arg *a = arg;
10073 +       struct au_dtime dt;
10074 +       struct path h_path;
10075 +       struct inode *dir, *h_dir;
10076 +       struct super_block *sb;
10077 +       struct au_branch *br;
10078 +       struct au_hinode *hdir;
10079 +       int err;
10080 +       aufs_bindex_t btop, bindex;
10081 +
10082 +       sb = a->dentry->d_sb;
10083 +       if (d_really_is_negative(a->dentry))
10084 +               goto out;
10085 +       /* no dir->i_mutex lock */
10086 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10087 +
10088 +       dir = d_inode(a->dentry);
10089 +       btop = au_ibtop(dir);
10090 +       bindex = au_br_index(sb, a->brid);
10091 +       if (bindex < btop)
10092 +               goto out_unlock;
10093 +
10094 +       br = au_sbr(sb, bindex);
10095 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10096 +       if (!h_path.dentry)
10097 +               goto out_unlock;
10098 +       h_path.mnt = au_br_mnt(br);
10099 +       au_dtime_store(&dt, a->dentry, &h_path);
10100 +
10101 +       br = au_sbr(sb, btop);
10102 +       if (!au_br_writable(br->br_perm))
10103 +               goto out_unlock;
10104 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10105 +       h_path.mnt = au_br_mnt(br);
10106 +       err = vfsub_mnt_want_write(h_path.mnt);
10107 +       if (err)
10108 +               goto out_unlock;
10109 +       hdir = au_hi(dir, btop);
10110 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10111 +       h_dir = au_h_iptr(dir, btop);
10112 +       if (h_dir->i_nlink
10113 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10114 +               dt.dt_h_path = h_path;
10115 +               au_dtime_revert(&dt);
10116 +       }
10117 +       au_hn_inode_unlock(hdir);
10118 +       vfsub_mnt_drop_write(h_path.mnt);
10119 +       au_cpup_attr_timesizes(dir);
10120 +
10121 +out_unlock:
10122 +       aufs_read_unlock(a->dentry, AuLock_DW);
10123 +out:
10124 +       dput(a->dentry);
10125 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10126 +       au_kfree_try_rcu(arg);
10127 +}
10128 +
10129 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10130 +{
10131 +       int perm, wkq_err;
10132 +       aufs_bindex_t btop;
10133 +       struct au_dir_ts_arg *arg;
10134 +       struct dentry *dentry;
10135 +       struct super_block *sb;
10136 +
10137 +       IMustLock(dir);
10138 +
10139 +       dentry = d_find_any_alias(dir);
10140 +       AuDebugOn(!dentry);
10141 +       sb = dentry->d_sb;
10142 +       btop = au_ibtop(dir);
10143 +       if (btop == bindex) {
10144 +               au_cpup_attr_timesizes(dir);
10145 +               goto out;
10146 +       }
10147 +
10148 +       perm = au_sbr_perm(sb, btop);
10149 +       if (!au_br_writable(perm))
10150 +               goto out;
10151 +
10152 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10153 +       if (!arg)
10154 +               goto out;
10155 +
10156 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10157 +       arg->brid = au_sbr_id(sb, bindex);
10158 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10159 +       if (unlikely(wkq_err)) {
10160 +               pr_err("wkq %d\n", wkq_err);
10161 +               dput(dentry);
10162 +               au_kfree_try_rcu(arg);
10163 +       }
10164 +
10165 +out:
10166 +       dput(dentry);
10167 +}
10168 +
10169 +/* ---------------------------------------------------------------------- */
10170 +
10171 +static int reopen_dir(struct file *file)
10172 +{
10173 +       int err;
10174 +       unsigned int flags;
10175 +       aufs_bindex_t bindex, btail, btop;
10176 +       struct dentry *dentry, *h_dentry;
10177 +       struct file *h_file;
10178 +
10179 +       /* open all lower dirs */
10180 +       dentry = file->f_path.dentry;
10181 +       btop = au_dbtop(dentry);
10182 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10183 +               au_set_h_fptr(file, bindex, NULL);
10184 +       au_set_fbtop(file, btop);
10185 +
10186 +       btail = au_dbtaildir(dentry);
10187 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10188 +               au_set_h_fptr(file, bindex, NULL);
10189 +       au_set_fbbot_dir(file, btail);
10190 +
10191 +       flags = vfsub_file_flags(file);
10192 +       for (bindex = btop; bindex <= btail; bindex++) {
10193 +               h_dentry = au_h_dptr(dentry, bindex);
10194 +               if (!h_dentry)
10195 +                       continue;
10196 +               h_file = au_hf_dir(file, bindex);
10197 +               if (h_file)
10198 +                       continue;
10199 +
10200 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10201 +               err = PTR_ERR(h_file);
10202 +               if (IS_ERR(h_file))
10203 +                       goto out; /* close all? */
10204 +               au_set_h_fptr(file, bindex, h_file);
10205 +       }
10206 +       au_update_figen(file);
10207 +       /* todo: necessary? */
10208 +       /* file->f_ra = h_file->f_ra; */
10209 +       err = 0;
10210 +
10211 +out:
10212 +       return err;
10213 +}
10214 +
10215 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10216 +{
10217 +       int err;
10218 +       aufs_bindex_t bindex, btail;
10219 +       struct dentry *dentry, *h_dentry;
10220 +       struct vfsmount *mnt;
10221 +
10222 +       FiMustWriteLock(file);
10223 +       AuDebugOn(h_file);
10224 +
10225 +       err = 0;
10226 +       mnt = file->f_path.mnt;
10227 +       dentry = file->f_path.dentry;
10228 +       file->f_version = inode_query_iversion(d_inode(dentry));
10229 +       bindex = au_dbtop(dentry);
10230 +       au_set_fbtop(file, bindex);
10231 +       btail = au_dbtaildir(dentry);
10232 +       au_set_fbbot_dir(file, btail);
10233 +       for (; !err && bindex <= btail; bindex++) {
10234 +               h_dentry = au_h_dptr(dentry, bindex);
10235 +               if (!h_dentry)
10236 +                       continue;
10237 +
10238 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10239 +               if (unlikely(err))
10240 +                       break;
10241 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10242 +               if (IS_ERR(h_file)) {
10243 +                       err = PTR_ERR(h_file);
10244 +                       break;
10245 +               }
10246 +               au_set_h_fptr(file, bindex, h_file);
10247 +       }
10248 +       au_update_figen(file);
10249 +       /* todo: necessary? */
10250 +       /* file->f_ra = h_file->f_ra; */
10251 +       if (!err)
10252 +               return 0; /* success */
10253 +
10254 +       /* close all */
10255 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10256 +               au_set_h_fptr(file, bindex, NULL);
10257 +       au_set_fbtop(file, -1);
10258 +       au_set_fbbot_dir(file, -1);
10259 +
10260 +       return err;
10261 +}
10262 +
10263 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10264 +                        struct file *file)
10265 +{
10266 +       int err;
10267 +       struct super_block *sb;
10268 +       struct au_fidir *fidir;
10269 +
10270 +       err = -ENOMEM;
10271 +       sb = file->f_path.dentry->d_sb;
10272 +       si_read_lock(sb, AuLock_FLUSH);
10273 +       fidir = au_fidir_alloc(sb);
10274 +       if (fidir) {
10275 +               struct au_do_open_args args = {
10276 +                       .open   = do_open_dir,
10277 +                       .fidir  = fidir
10278 +               };
10279 +               err = au_do_open(file, &args);
10280 +               if (unlikely(err))
10281 +                       au_kfree_rcu(fidir);
10282 +       }
10283 +       si_read_unlock(sb);
10284 +       return err;
10285 +}
10286 +
10287 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10288 +                           struct file *file)
10289 +{
10290 +       struct au_vdir *vdir_cache;
10291 +       struct au_finfo *finfo;
10292 +       struct au_fidir *fidir;
10293 +       struct au_hfile *hf;
10294 +       aufs_bindex_t bindex, bbot;
10295 +
10296 +       finfo = au_fi(file);
10297 +       fidir = finfo->fi_hdir;
10298 +       if (fidir) {
10299 +               au_hbl_del(&finfo->fi_hlist,
10300 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10301 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10302 +               if (vdir_cache)
10303 +                       au_vdir_free(vdir_cache);
10304 +
10305 +               bindex = finfo->fi_btop;
10306 +               if (bindex >= 0) {
10307 +                       hf = fidir->fd_hfile + bindex;
10308 +                       /*
10309 +                        * calls fput() instead of filp_close(),
10310 +                        * since no dnotify or lock for the lower file.
10311 +                        */
10312 +                       bbot = fidir->fd_bbot;
10313 +                       for (; bindex <= bbot; bindex++, hf++)
10314 +                               if (hf->hf_file)
10315 +                                       au_hfput(hf, /*execed*/0);
10316 +               }
10317 +               au_kfree_rcu(fidir);
10318 +               finfo->fi_hdir = NULL;
10319 +       }
10320 +       au_finfo_fin(file);
10321 +       return 0;
10322 +}
10323 +
10324 +/* ---------------------------------------------------------------------- */
10325 +
10326 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10327 +{
10328 +       int err;
10329 +       aufs_bindex_t bindex, bbot;
10330 +       struct file *h_file;
10331 +
10332 +       err = 0;
10333 +       bbot = au_fbbot_dir(file);
10334 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10335 +               h_file = au_hf_dir(file, bindex);
10336 +               if (h_file)
10337 +                       err = vfsub_flush(h_file, id);
10338 +       }
10339 +       return err;
10340 +}
10341 +
10342 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10343 +{
10344 +       return au_do_flush(file, id, au_do_flush_dir);
10345 +}
10346 +
10347 +/* ---------------------------------------------------------------------- */
10348 +
10349 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10350 +{
10351 +       int err;
10352 +       aufs_bindex_t bbot, bindex;
10353 +       struct inode *inode;
10354 +       struct super_block *sb;
10355 +
10356 +       err = 0;
10357 +       sb = dentry->d_sb;
10358 +       inode = d_inode(dentry);
10359 +       IMustLock(inode);
10360 +       bbot = au_dbbot(dentry);
10361 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10362 +               struct path h_path;
10363 +
10364 +               if (au_test_ro(sb, bindex, inode))
10365 +                       continue;
10366 +               h_path.dentry = au_h_dptr(dentry, bindex);
10367 +               if (!h_path.dentry)
10368 +                       continue;
10369 +
10370 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10371 +               err = vfsub_fsync(NULL, &h_path, datasync);
10372 +       }
10373 +
10374 +       return err;
10375 +}
10376 +
10377 +static int au_do_fsync_dir(struct file *file, int datasync)
10378 +{
10379 +       int err;
10380 +       aufs_bindex_t bbot, bindex;
10381 +       struct file *h_file;
10382 +       struct super_block *sb;
10383 +       struct inode *inode;
10384 +
10385 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10386 +       if (unlikely(err))
10387 +               goto out;
10388 +
10389 +       inode = file_inode(file);
10390 +       sb = inode->i_sb;
10391 +       bbot = au_fbbot_dir(file);
10392 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10393 +               h_file = au_hf_dir(file, bindex);
10394 +               if (!h_file || au_test_ro(sb, bindex, inode))
10395 +                       continue;
10396 +
10397 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10398 +       }
10399 +
10400 +out:
10401 +       return err;
10402 +}
10403 +
10404 +/*
10405 + * @file may be NULL
10406 + */
10407 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10408 +                         int datasync)
10409 +{
10410 +       int err;
10411 +       struct dentry *dentry;
10412 +       struct inode *inode;
10413 +       struct super_block *sb;
10414 +
10415 +       err = 0;
10416 +       dentry = file->f_path.dentry;
10417 +       inode = d_inode(dentry);
10418 +       inode_lock(inode);
10419 +       sb = dentry->d_sb;
10420 +       si_noflush_read_lock(sb);
10421 +       if (file)
10422 +               err = au_do_fsync_dir(file, datasync);
10423 +       else {
10424 +               di_write_lock_child(dentry);
10425 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10426 +       }
10427 +       au_cpup_attr_timesizes(inode);
10428 +       di_write_unlock(dentry);
10429 +       if (file)
10430 +               fi_write_unlock(file);
10431 +
10432 +       si_read_unlock(sb);
10433 +       inode_unlock(inode);
10434 +       return err;
10435 +}
10436 +
10437 +/* ---------------------------------------------------------------------- */
10438 +
10439 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10440 +{
10441 +       int err;
10442 +       struct dentry *dentry;
10443 +       struct inode *inode, *h_inode;
10444 +       struct super_block *sb;
10445 +
10446 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10447 +
10448 +       dentry = file->f_path.dentry;
10449 +       inode = d_inode(dentry);
10450 +       IMustLock(inode);
10451 +
10452 +       sb = dentry->d_sb;
10453 +       si_read_lock(sb, AuLock_FLUSH);
10454 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10455 +       if (unlikely(err))
10456 +               goto out;
10457 +       err = au_alive_dir(dentry);
10458 +       if (!err)
10459 +               err = au_vdir_init(file);
10460 +       di_downgrade_lock(dentry, AuLock_IR);
10461 +       if (unlikely(err))
10462 +               goto out_unlock;
10463 +
10464 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10465 +       if (!au_test_nfsd()) {
10466 +               err = au_vdir_fill_de(file, ctx);
10467 +               fsstack_copy_attr_atime(inode, h_inode);
10468 +       } else {
10469 +               /*
10470 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10471 +                * encode_fh() and others.
10472 +                */
10473 +               atomic_inc(&h_inode->i_count);
10474 +               di_read_unlock(dentry, AuLock_IR);
10475 +               si_read_unlock(sb);
10476 +               err = au_vdir_fill_de(file, ctx);
10477 +               fsstack_copy_attr_atime(inode, h_inode);
10478 +               fi_write_unlock(file);
10479 +               iput(h_inode);
10480 +
10481 +               AuTraceErr(err);
10482 +               return err;
10483 +       }
10484 +
10485 +out_unlock:
10486 +       di_read_unlock(dentry, AuLock_IR);
10487 +       fi_write_unlock(file);
10488 +out:
10489 +       si_read_unlock(sb);
10490 +       return err;
10491 +}
10492 +
10493 +/* ---------------------------------------------------------------------- */
10494 +
10495 +#define AuTestEmpty_WHONLY     1
10496 +#define AuTestEmpty_CALLED     (1 << 1)
10497 +#define AuTestEmpty_SHWH       (1 << 2)
10498 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10499 +#define au_fset_testempty(flags, name) \
10500 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10501 +#define au_fclr_testempty(flags, name) \
10502 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10503 +
10504 +#ifndef CONFIG_AUFS_SHWH
10505 +#undef AuTestEmpty_SHWH
10506 +#define AuTestEmpty_SHWH       0
10507 +#endif
10508 +
10509 +struct test_empty_arg {
10510 +       struct dir_context ctx;
10511 +       struct au_nhash *whlist;
10512 +       unsigned int flags;
10513 +       int err;
10514 +       aufs_bindex_t bindex;
10515 +};
10516 +
10517 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10518 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10519 +                        unsigned int d_type)
10520 +{
10521 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10522 +                                                 ctx);
10523 +       char *name = (void *)__name;
10524 +
10525 +       arg->err = 0;
10526 +       au_fset_testempty(arg->flags, CALLED);
10527 +       /* smp_mb(); */
10528 +       if (name[0] == '.'
10529 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10530 +               goto out; /* success */
10531 +
10532 +       if (namelen <= AUFS_WH_PFX_LEN
10533 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10534 +               if (au_ftest_testempty(arg->flags, WHONLY)
10535 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10536 +                       arg->err = -ENOTEMPTY;
10537 +               goto out;
10538 +       }
10539 +
10540 +       name += AUFS_WH_PFX_LEN;
10541 +       namelen -= AUFS_WH_PFX_LEN;
10542 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10543 +               arg->err = au_nhash_append_wh
10544 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10545 +                        au_ftest_testempty(arg->flags, SHWH));
10546 +
10547 +out:
10548 +       /* smp_mb(); */
10549 +       AuTraceErr(arg->err);
10550 +       return arg->err;
10551 +}
10552 +
10553 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10554 +{
10555 +       int err;
10556 +       struct file *h_file;
10557 +       struct au_branch *br;
10558 +
10559 +       h_file = au_h_open(dentry, arg->bindex,
10560 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10561 +                          /*file*/NULL, /*force_wr*/0);
10562 +       err = PTR_ERR(h_file);
10563 +       if (IS_ERR(h_file))
10564 +               goto out;
10565 +
10566 +       err = 0;
10567 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10568 +           && !file_inode(h_file)->i_nlink)
10569 +               goto out_put;
10570 +
10571 +       do {
10572 +               arg->err = 0;
10573 +               au_fclr_testempty(arg->flags, CALLED);
10574 +               /* smp_mb(); */
10575 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10576 +               if (err >= 0)
10577 +                       err = arg->err;
10578 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10579 +
10580 +out_put:
10581 +       fput(h_file);
10582 +       br = au_sbr(dentry->d_sb, arg->bindex);
10583 +       au_lcnt_dec(&br->br_nfiles);
10584 +out:
10585 +       return err;
10586 +}
10587 +
10588 +struct do_test_empty_args {
10589 +       int *errp;
10590 +       struct dentry *dentry;
10591 +       struct test_empty_arg *arg;
10592 +};
10593 +
10594 +static void call_do_test_empty(void *args)
10595 +{
10596 +       struct do_test_empty_args *a = args;
10597 +       *a->errp = do_test_empty(a->dentry, a->arg);
10598 +}
10599 +
10600 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10601 +{
10602 +       int err, wkq_err;
10603 +       struct dentry *h_dentry;
10604 +       struct inode *h_inode;
10605 +
10606 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10607 +       h_inode = d_inode(h_dentry);
10608 +       /* todo: i_mode changes anytime? */
10609 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10610 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10611 +       inode_unlock_shared(h_inode);
10612 +       if (!err)
10613 +               err = do_test_empty(dentry, arg);
10614 +       else {
10615 +               struct do_test_empty_args args = {
10616 +                       .errp   = &err,
10617 +                       .dentry = dentry,
10618 +                       .arg    = arg
10619 +               };
10620 +               unsigned int flags = arg->flags;
10621 +
10622 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10623 +               if (unlikely(wkq_err))
10624 +                       err = wkq_err;
10625 +               arg->flags = flags;
10626 +       }
10627 +
10628 +       return err;
10629 +}
10630 +
10631 +int au_test_empty_lower(struct dentry *dentry)
10632 +{
10633 +       int err;
10634 +       unsigned int rdhash;
10635 +       aufs_bindex_t bindex, btop, btail;
10636 +       struct au_nhash whlist;
10637 +       struct test_empty_arg arg = {
10638 +               .ctx = {
10639 +                       .actor = test_empty_cb
10640 +               }
10641 +       };
10642 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10643 +
10644 +       SiMustAnyLock(dentry->d_sb);
10645 +
10646 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10647 +       if (!rdhash)
10648 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10649 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10650 +       if (unlikely(err))
10651 +               goto out;
10652 +
10653 +       arg.flags = 0;
10654 +       arg.whlist = &whlist;
10655 +       btop = au_dbtop(dentry);
10656 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10657 +               au_fset_testempty(arg.flags, SHWH);
10658 +       test_empty = do_test_empty;
10659 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10660 +               test_empty = sio_test_empty;
10661 +       arg.bindex = btop;
10662 +       err = test_empty(dentry, &arg);
10663 +       if (unlikely(err))
10664 +               goto out_whlist;
10665 +
10666 +       au_fset_testempty(arg.flags, WHONLY);
10667 +       btail = au_dbtaildir(dentry);
10668 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10669 +               struct dentry *h_dentry;
10670 +
10671 +               h_dentry = au_h_dptr(dentry, bindex);
10672 +               if (h_dentry && d_is_positive(h_dentry)) {
10673 +                       arg.bindex = bindex;
10674 +                       err = test_empty(dentry, &arg);
10675 +               }
10676 +       }
10677 +
10678 +out_whlist:
10679 +       au_nhash_wh_free(&whlist);
10680 +out:
10681 +       return err;
10682 +}
10683 +
10684 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10685 +{
10686 +       int err;
10687 +       struct test_empty_arg arg = {
10688 +               .ctx = {
10689 +                       .actor = test_empty_cb
10690 +               }
10691 +       };
10692 +       aufs_bindex_t bindex, btail;
10693 +
10694 +       err = 0;
10695 +       arg.whlist = whlist;
10696 +       arg.flags = AuTestEmpty_WHONLY;
10697 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10698 +               au_fset_testempty(arg.flags, SHWH);
10699 +       btail = au_dbtaildir(dentry);
10700 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10701 +               struct dentry *h_dentry;
10702 +
10703 +               h_dentry = au_h_dptr(dentry, bindex);
10704 +               if (h_dentry && d_is_positive(h_dentry)) {
10705 +                       arg.bindex = bindex;
10706 +                       err = sio_test_empty(dentry, &arg);
10707 +               }
10708 +       }
10709 +
10710 +       return err;
10711 +}
10712 +
10713 +/* ---------------------------------------------------------------------- */
10714 +
10715 +const struct file_operations aufs_dir_fop = {
10716 +       .owner          = THIS_MODULE,
10717 +       .llseek         = default_llseek,
10718 +       .read           = generic_read_dir,
10719 +       .iterate_shared = aufs_iterate_shared,
10720 +       .unlocked_ioctl = aufs_ioctl_dir,
10721 +#ifdef CONFIG_COMPAT
10722 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10723 +#endif
10724 +       .open           = aufs_open_dir,
10725 +       .release        = aufs_release_dir,
10726 +       .flush          = aufs_flush_dir,
10727 +       .fsync          = aufs_fsync_dir
10728 +};
10729 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10730 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10731 +++ linux/fs/aufs/dir.h 2020-08-03 09:14:46.095748745 +0200
10732 @@ -0,0 +1,134 @@
10733 +/* SPDX-License-Identifier: GPL-2.0 */
10734 +/*
10735 + * Copyright (C) 2005-2020 Junjiro R. Okajima
10736 + *
10737 + * This program, aufs is free software; you can redistribute it and/or modify
10738 + * it under the terms of the GNU General Public License as published by
10739 + * the Free Software Foundation; either version 2 of the License, or
10740 + * (at your option) any later version.
10741 + *
10742 + * This program is distributed in the hope that it will be useful,
10743 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10744 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10745 + * GNU General Public License for more details.
10746 + *
10747 + * You should have received a copy of the GNU General Public License
10748 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10749 + */
10750 +
10751 +/*
10752 + * directory operations
10753 + */
10754 +
10755 +#ifndef __AUFS_DIR_H__
10756 +#define __AUFS_DIR_H__
10757 +
10758 +#ifdef __KERNEL__
10759 +
10760 +#include <linux/fs.h>
10761 +
10762 +/* ---------------------------------------------------------------------- */
10763 +
10764 +/* need to be faster and smaller */
10765 +
10766 +struct au_nhash {
10767 +       unsigned int            nh_num;
10768 +       struct hlist_head       *nh_head;
10769 +};
10770 +
10771 +struct au_vdir_destr {
10772 +       unsigned char   len;
10773 +       unsigned char   name[];
10774 +} __packed;
10775 +
10776 +struct au_vdir_dehstr {
10777 +       struct hlist_node       hash;
10778 +       struct au_vdir_destr    *str;
10779 +       struct rcu_head         rcu;
10780 +} ____cacheline_aligned_in_smp;
10781 +
10782 +struct au_vdir_de {
10783 +       ino_t                   de_ino;
10784 +       unsigned char           de_type;
10785 +       /* caution: packed */
10786 +       struct au_vdir_destr    de_str;
10787 +} __packed;
10788 +
10789 +struct au_vdir_wh {
10790 +       struct hlist_node       wh_hash;
10791 +#ifdef CONFIG_AUFS_SHWH
10792 +       ino_t                   wh_ino;
10793 +       aufs_bindex_t           wh_bindex;
10794 +       unsigned char           wh_type;
10795 +#else
10796 +       aufs_bindex_t           wh_bindex;
10797 +#endif
10798 +       /* caution: packed */
10799 +       struct au_vdir_destr    wh_str;
10800 +} __packed;
10801 +
10802 +union au_vdir_deblk_p {
10803 +       unsigned char           *deblk;
10804 +       struct au_vdir_de       *de;
10805 +};
10806 +
10807 +struct au_vdir {
10808 +       unsigned char   **vd_deblk;
10809 +       unsigned long   vd_nblk;
10810 +       struct {
10811 +               unsigned long           ul;
10812 +               union au_vdir_deblk_p   p;
10813 +       } vd_last;
10814 +
10815 +       u64             vd_version;
10816 +       unsigned int    vd_deblk_sz;
10817 +       unsigned long   vd_jiffy;
10818 +       struct rcu_head rcu;
10819 +} ____cacheline_aligned_in_smp;
10820 +
10821 +/* ---------------------------------------------------------------------- */
10822 +
10823 +/* dir.c */
10824 +extern const struct file_operations aufs_dir_fop;
10825 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10826 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10827 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10828 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10829 +int au_test_empty_lower(struct dentry *dentry);
10830 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10831 +
10832 +/* vdir.c */
10833 +unsigned int au_rdhash_est(loff_t sz);
10834 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10835 +void au_nhash_wh_free(struct au_nhash *whlist);
10836 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10837 +                           int limit);
10838 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10839 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10840 +                      unsigned int d_type, aufs_bindex_t bindex,
10841 +                      unsigned char shwh);
10842 +void au_vdir_free(struct au_vdir *vdir);
10843 +int au_vdir_init(struct file *file);
10844 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10845 +
10846 +/* ioctl.c */
10847 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10848 +
10849 +#ifdef CONFIG_AUFS_RDU
10850 +/* rdu.c */
10851 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10852 +#ifdef CONFIG_COMPAT
10853 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10854 +                        unsigned long arg);
10855 +#endif
10856 +#else
10857 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10858 +       unsigned int cmd, unsigned long arg)
10859 +#ifdef CONFIG_COMPAT
10860 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10861 +       unsigned int cmd, unsigned long arg)
10862 +#endif
10863 +#endif
10864 +
10865 +#endif /* __KERNEL__ */
10866 +#endif /* __AUFS_DIR_H__ */
10867 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10868 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10869 +++ linux/fs/aufs/dirren.c      2020-08-03 09:14:46.095748745 +0200
10870 @@ -0,0 +1,1316 @@
10871 +// SPDX-License-Identifier: GPL-2.0
10872 +/*
10873 + * Copyright (C) 2017-2020 Junjiro R. Okajima
10874 + *
10875 + * This program, aufs is free software; you can redistribute it and/or modify
10876 + * it under the terms of the GNU General Public License as published by
10877 + * the Free Software Foundation; either version 2 of the License, or
10878 + * (at your option) any later version.
10879 + *
10880 + * This program is distributed in the hope that it will be useful,
10881 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10882 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10883 + * GNU General Public License for more details.
10884 + *
10885 + * You should have received a copy of the GNU General Public License
10886 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10887 + */
10888 +
10889 +/*
10890 + * special handling in renaming a directory
10891 + * in order to support looking-up the before-renamed name on the lower readonly
10892 + * branches
10893 + */
10894 +
10895 +#include <linux/byteorder/generic.h>
10896 +#include "aufs.h"
10897 +
10898 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10899 +{
10900 +       int idx;
10901 +
10902 +       idx = au_dr_ihash(ent->dr_h_ino);
10903 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10904 +}
10905 +
10906 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10907 +{
10908 +       int ret, i;
10909 +       struct hlist_bl_head *hbl;
10910 +
10911 +       ret = 1;
10912 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10913 +               hbl = dr->dr_h_ino + i;
10914 +               hlist_bl_lock(hbl);
10915 +               ret &= hlist_bl_empty(hbl);
10916 +               hlist_bl_unlock(hbl);
10917 +       }
10918 +
10919 +       return ret;
10920 +}
10921 +
10922 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10923 +{
10924 +       struct au_dr_hino *found, *ent;
10925 +       struct hlist_bl_head *hbl;
10926 +       struct hlist_bl_node *pos;
10927 +       int idx;
10928 +
10929 +       found = NULL;
10930 +       idx = au_dr_ihash(ino);
10931 +       hbl = dr->dr_h_ino + idx;
10932 +       hlist_bl_lock(hbl);
10933 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10934 +               if (ent->dr_h_ino == ino) {
10935 +                       found = ent;
10936 +                       break;
10937 +               }
10938 +       hlist_bl_unlock(hbl);
10939 +
10940 +       return found;
10941 +}
10942 +
10943 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10944 +                       struct au_dr_hino *add_ent)
10945 +{
10946 +       int found, idx;
10947 +       struct hlist_bl_head *hbl;
10948 +       struct hlist_bl_node *pos;
10949 +       struct au_dr_hino *ent;
10950 +
10951 +       found = 0;
10952 +       idx = au_dr_ihash(ino);
10953 +       hbl = dr->dr_h_ino + idx;
10954 +#if 0 /* debug print */
10955 +       {
10956 +               struct hlist_bl_node *tmp;
10957 +
10958 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10959 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10960 +       }
10961 +#endif
10962 +       hlist_bl_lock(hbl);
10963 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10964 +               if (ent->dr_h_ino == ino) {
10965 +                       found = 1;
10966 +                       break;
10967 +               }
10968 +       if (!found && add_ent)
10969 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10970 +       hlist_bl_unlock(hbl);
10971 +
10972 +       if (!found && add_ent)
10973 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10974 +
10975 +       return found;
10976 +}
10977 +
10978 +void au_dr_hino_free(struct au_dr_br *dr)
10979 +{
10980 +       int i;
10981 +       struct hlist_bl_head *hbl;
10982 +       struct hlist_bl_node *pos, *tmp;
10983 +       struct au_dr_hino *ent;
10984 +
10985 +       /* SiMustWriteLock(sb); */
10986 +
10987 +       for (i = 0; i < AuDirren_NHASH; i++) {
10988 +               hbl = dr->dr_h_ino + i;
10989 +               /* no spinlock since sbinfo must be write-locked */
10990 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10991 +                       au_kfree_rcu(ent);
10992 +               INIT_HLIST_BL_HEAD(hbl);
10993 +       }
10994 +}
10995 +
10996 +/* returns the number of inodes or an error */
10997 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10998 +                           struct file *hinofile)
10999 +{
11000 +       int err, i;
11001 +       ssize_t ssz;
11002 +       loff_t pos, oldsize;
11003 +       __be64 u64;
11004 +       struct inode *hinoinode;
11005 +       struct hlist_bl_head *hbl;
11006 +       struct hlist_bl_node *n1, *n2;
11007 +       struct au_dr_hino *ent;
11008 +
11009 +       SiMustWriteLock(sb);
11010 +       AuDebugOn(!au_br_writable(br->br_perm));
11011 +
11012 +       hinoinode = file_inode(hinofile);
11013 +       oldsize = i_size_read(hinoinode);
11014 +
11015 +       err = 0;
11016 +       pos = 0;
11017 +       hbl = br->br_dirren.dr_h_ino;
11018 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11019 +               /* no bit-lock since sbinfo must be write-locked */
11020 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11021 +                       AuDbg("hi%llu, %pD2\n",
11022 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11023 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11024 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11025 +                       if (ssz == sizeof(u64))
11026 +                               continue;
11027 +
11028 +                       /* write error */
11029 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11030 +                       err = -ENOSPC;
11031 +                       if (ssz < 0)
11032 +                               err = ssz;
11033 +                       break;
11034 +               }
11035 +       }
11036 +       /* regardless the error */
11037 +       if (pos < oldsize) {
11038 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11039 +               AuTraceErr(err);
11040 +       }
11041 +
11042 +       AuTraceErr(err);
11043 +       return err;
11044 +}
11045 +
11046 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11047 +{
11048 +       int err, hidx;
11049 +       ssize_t ssz;
11050 +       size_t sz, n;
11051 +       loff_t pos;
11052 +       uint64_t u64;
11053 +       struct au_dr_hino *ent;
11054 +       struct inode *hinoinode;
11055 +       struct hlist_bl_head *hbl;
11056 +
11057 +       err = 0;
11058 +       pos = 0;
11059 +       hbl = dr->dr_h_ino;
11060 +       hinoinode = file_inode(hinofile);
11061 +       sz = i_size_read(hinoinode);
11062 +       AuDebugOn(sz % sizeof(u64));
11063 +       n = sz / sizeof(u64);
11064 +       while (n--) {
11065 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11066 +               if (unlikely(ssz != sizeof(u64))) {
11067 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11068 +                       err = -EINVAL;
11069 +                       if (ssz < 0)
11070 +                               err = ssz;
11071 +                       goto out_free;
11072 +               }
11073 +
11074 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11075 +               if (!ent) {
11076 +                       err = -ENOMEM;
11077 +                       AuTraceErr(err);
11078 +                       goto out_free;
11079 +               }
11080 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11081 +               AuDbg("hi%llu, %pD2\n",
11082 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11083 +               hidx = au_dr_ihash(ent->dr_h_ino);
11084 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11085 +       }
11086 +       goto out; /* success */
11087 +
11088 +out_free:
11089 +       au_dr_hino_free(dr);
11090 +out:
11091 +       AuTraceErr(err);
11092 +       return err;
11093 +}
11094 +
11095 +/*
11096 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11097 + * @path is a switch to distinguish load and store.
11098 + */
11099 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11100 +                     struct au_branch *br, const struct path *path)
11101 +{
11102 +       int err, flags;
11103 +       unsigned char load, suspend;
11104 +       struct file *hinofile;
11105 +       struct au_hinode *hdir;
11106 +       struct inode *dir, *delegated;
11107 +       struct path hinopath;
11108 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11109 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11110 +
11111 +       AuDebugOn(bindex < 0 && !br);
11112 +       AuDebugOn(bindex >= 0 && br);
11113 +
11114 +       err = -EINVAL;
11115 +       suspend = !br;
11116 +       if (suspend)
11117 +               br = au_sbr(sb, bindex);
11118 +       load = !!path;
11119 +       if (!load) {
11120 +               path = &br->br_path;
11121 +               AuDebugOn(!au_br_writable(br->br_perm));
11122 +               if (unlikely(!au_br_writable(br->br_perm)))
11123 +                       goto out;
11124 +       }
11125 +
11126 +       hdir = NULL;
11127 +       if (suspend) {
11128 +               dir = d_inode(sb->s_root);
11129 +               hdir = au_hinode(au_ii(dir), bindex);
11130 +               dir = hdir->hi_inode;
11131 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11132 +       } else {
11133 +               dir = d_inode(path->dentry);
11134 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11135 +       }
11136 +       hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
11137 +       err = PTR_ERR(hinopath.dentry);
11138 +       if (IS_ERR(hinopath.dentry))
11139 +               goto out_unlock;
11140 +
11141 +       err = 0;
11142 +       flags = O_RDONLY;
11143 +       if (load) {
11144 +               if (d_is_negative(hinopath.dentry))
11145 +                       goto out_dput; /* success */
11146 +       } else {
11147 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11148 +                       if (d_is_positive(hinopath.dentry)) {
11149 +                               delegated = NULL;
11150 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11151 +                                                  /*force*/0);
11152 +                               AuTraceErr(err);
11153 +                               if (unlikely(err))
11154 +                                       pr_err("ignored err %d, %pd2\n",
11155 +                                              err, hinopath.dentry);
11156 +                               if (unlikely(err == -EWOULDBLOCK))
11157 +                                       iput(delegated);
11158 +                               err = 0;
11159 +                       }
11160 +                       goto out_dput;
11161 +               } else if (!d_is_positive(hinopath.dentry)) {
11162 +                       err = vfsub_create(dir, &hinopath, 0600,
11163 +                                          /*want_excl*/false);
11164 +                       AuTraceErr(err);
11165 +                       if (unlikely(err))
11166 +                               goto out_dput;
11167 +               }
11168 +               flags = O_WRONLY;
11169 +       }
11170 +       hinopath.mnt = path->mnt;
11171 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11172 +       if (suspend)
11173 +               au_hn_inode_unlock(hdir);
11174 +       else
11175 +               inode_unlock(dir);
11176 +       dput(hinopath.dentry);
11177 +       AuTraceErrPtr(hinofile);
11178 +       if (IS_ERR(hinofile)) {
11179 +               err = PTR_ERR(hinofile);
11180 +               goto out;
11181 +       }
11182 +
11183 +       if (load)
11184 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11185 +       else
11186 +               err = au_dr_hino_store(sb, br, hinofile);
11187 +       fput(hinofile);
11188 +       goto out;
11189 +
11190 +out_dput:
11191 +       dput(hinopath.dentry);
11192 +out_unlock:
11193 +       if (suspend)
11194 +               au_hn_inode_unlock(hdir);
11195 +       else
11196 +               inode_unlock(dir);
11197 +out:
11198 +       AuTraceErr(err);
11199 +       return err;
11200 +}
11201 +
11202 +/* ---------------------------------------------------------------------- */
11203 +
11204 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11205 +{
11206 +       int err;
11207 +       struct kstatfs kstfs;
11208 +       dev_t dev;
11209 +       struct dentry *dentry;
11210 +       struct super_block *sb;
11211 +
11212 +       err = vfs_statfs((void *)path, &kstfs);
11213 +       AuTraceErr(err);
11214 +       if (unlikely(err))
11215 +               goto out;
11216 +
11217 +       /* todo: support for UUID */
11218 +
11219 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11220 +               brid->type = AuBrid_FSID;
11221 +               brid->fsid = kstfs.f_fsid;
11222 +       } else {
11223 +               dentry = path->dentry;
11224 +               sb = dentry->d_sb;
11225 +               dev = sb->s_dev;
11226 +               if (dev) {
11227 +                       brid->type = AuBrid_DEV;
11228 +                       brid->dev = dev;
11229 +               }
11230 +       }
11231 +
11232 +out:
11233 +       return err;
11234 +}
11235 +
11236 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11237 +                 const struct path *path)
11238 +{
11239 +       int err, i;
11240 +       struct au_dr_br *dr;
11241 +       struct hlist_bl_head *hbl;
11242 +
11243 +       dr = &br->br_dirren;
11244 +       hbl = dr->dr_h_ino;
11245 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11246 +               INIT_HLIST_BL_HEAD(hbl);
11247 +
11248 +       err = au_dr_brid_init(&dr->dr_brid, path);
11249 +       if (unlikely(err))
11250 +               goto out;
11251 +
11252 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11253 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11254 +
11255 +out:
11256 +       AuTraceErr(err);
11257 +       return err;
11258 +}
11259 +
11260 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11261 +{
11262 +       int err;
11263 +
11264 +       err = 0;
11265 +       if (au_br_writable(br->br_perm))
11266 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11267 +       if (!err)
11268 +               au_dr_hino_free(&br->br_dirren);
11269 +
11270 +       return err;
11271 +}
11272 +
11273 +/* ---------------------------------------------------------------------- */
11274 +
11275 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11276 +                      char *buf, size_t sz)
11277 +{
11278 +       int err;
11279 +       unsigned int major, minor;
11280 +       char *p;
11281 +
11282 +       p = buf;
11283 +       err = snprintf(p, sz, "%d_", brid->type);
11284 +       AuDebugOn(err > sz);
11285 +       p += err;
11286 +       sz -= err;
11287 +       switch (brid->type) {
11288 +       case AuBrid_Unset:
11289 +               return -EINVAL;
11290 +       case AuBrid_UUID:
11291 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11292 +               break;
11293 +       case AuBrid_FSID:
11294 +               err = snprintf(p, sz, "%08x-%08x",
11295 +                              brid->fsid.val[0], brid->fsid.val[1]);
11296 +               break;
11297 +       case AuBrid_DEV:
11298 +               major = MAJOR(brid->dev);
11299 +               minor = MINOR(brid->dev);
11300 +               if (major <= 0xff && minor <= 0xff)
11301 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11302 +               else
11303 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11304 +               break;
11305 +       }
11306 +       AuDebugOn(err > sz);
11307 +       p += err;
11308 +       sz -= err;
11309 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11310 +       AuDebugOn(err > sz);
11311 +       p += err;
11312 +       sz -= err;
11313 +
11314 +       return p - buf;
11315 +}
11316 +
11317 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11318 +{
11319 +       int rlen;
11320 +       struct dentry *br_dentry;
11321 +       struct inode *br_inode;
11322 +
11323 +       br_dentry = au_br_dentry(br);
11324 +       br_inode = d_inode(br_dentry);
11325 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11326 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11327 +       AuDebugOn(rlen > len);
11328 +
11329 +       return rlen;
11330 +}
11331 +
11332 +/* ---------------------------------------------------------------------- */
11333 +
11334 +/*
11335 + * from the given @h_dentry, construct drinfo at @*fdata.
11336 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11337 + * @allocated.
11338 + */
11339 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11340 +                              struct dentry *h_dentry,
11341 +                              unsigned char *allocated)
11342 +{
11343 +       int err, v;
11344 +       struct au_drinfo_fdata *f, *p;
11345 +       struct au_drinfo *drinfo;
11346 +       struct inode *h_inode;
11347 +       struct qstr *qname;
11348 +
11349 +       err = 0;
11350 +       f = *fdata;
11351 +       h_inode = d_inode(h_dentry);
11352 +       qname = &h_dentry->d_name;
11353 +       drinfo = &f->drinfo;
11354 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11355 +       drinfo->oldnamelen = qname->len;
11356 +       if (*allocated < sizeof(*f) + qname->len) {
11357 +               v = roundup_pow_of_two(*allocated + qname->len);
11358 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11359 +               if (unlikely(!p)) {
11360 +                       err = -ENOMEM;
11361 +                       AuTraceErr(err);
11362 +                       goto out;
11363 +               }
11364 +               f = p;
11365 +               *fdata = f;
11366 +               *allocated = v;
11367 +               drinfo = &f->drinfo;
11368 +       }
11369 +       memcpy(drinfo->oldname, qname->name, qname->len);
11370 +       AuDbg("i%llu, %.*s\n",
11371 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11372 +             drinfo->oldname);
11373 +
11374 +out:
11375 +       AuTraceErr(err);
11376 +       return err;
11377 +}
11378 +
11379 +/* callers have to free the return value */
11380 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11381 +{
11382 +       struct au_drinfo *ret, *drinfo;
11383 +       struct au_drinfo_fdata fdata;
11384 +       int len;
11385 +       loff_t pos;
11386 +       ssize_t ssz;
11387 +
11388 +       ret = ERR_PTR(-EIO);
11389 +       pos = 0;
11390 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11391 +       if (unlikely(ssz != sizeof(fdata))) {
11392 +               AuIOErr("ssz %zd, %u, %pD2\n",
11393 +                       ssz, (unsigned int)sizeof(fdata), file);
11394 +               goto out;
11395 +       }
11396 +
11397 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11398 +       switch (fdata.magic) {
11399 +       case AUFS_DRINFO_MAGIC_V1:
11400 +               break;
11401 +       default:
11402 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11403 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11404 +               goto out;
11405 +       }
11406 +
11407 +       drinfo = &fdata.drinfo;
11408 +       len = drinfo->oldnamelen;
11409 +       if (!len) {
11410 +               AuIOErr("broken drinfo %pD2\n", file);
11411 +               goto out;
11412 +       }
11413 +
11414 +       ret = NULL;
11415 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11416 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11417 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11418 +                     (unsigned long long)drinfo->ino,
11419 +                     (unsigned long long)h_ino, file);
11420 +               goto out; /* success */
11421 +       }
11422 +
11423 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11424 +       if (unlikely(!ret)) {
11425 +               ret = ERR_PTR(-ENOMEM);
11426 +               AuTraceErrPtr(ret);
11427 +               goto out;
11428 +       }
11429 +
11430 +       *ret = *drinfo;
11431 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11432 +       if (unlikely(ssz != len)) {
11433 +               au_kfree_rcu(ret);
11434 +               ret = ERR_PTR(-EIO);
11435 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11436 +               goto out;
11437 +       }
11438 +
11439 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11440 +
11441 +out:
11442 +       return ret;
11443 +}
11444 +
11445 +/* ---------------------------------------------------------------------- */
11446 +
11447 +/* in order to be revertible */
11448 +struct au_drinfo_rev_elm {
11449 +       int                     created;
11450 +       struct dentry           *info_dentry;
11451 +       struct au_drinfo        *info_last;
11452 +};
11453 +
11454 +struct au_drinfo_rev {
11455 +       unsigned char                   already;
11456 +       aufs_bindex_t                   nelm;
11457 +       struct au_drinfo_rev_elm        elm[];
11458 +};
11459 +
11460 +/* todo: isn't it too large? */
11461 +struct au_drinfo_store {
11462 +       struct path h_ppath;
11463 +       struct dentry *h_dentry;
11464 +       struct au_drinfo_fdata *fdata;
11465 +       char *infoname;                 /* inside of whname, just after PFX */
11466 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11467 +       aufs_bindex_t btgt, btail;
11468 +       unsigned char no_sio,
11469 +               allocated,              /* current size of *fdata */
11470 +               infonamelen,            /* room size for p */
11471 +               whnamelen,              /* length of the generated name */
11472 +               renameback;             /* renamed back */
11473 +};
11474 +
11475 +/* on rename(2) error, the caller should revert it using @elm */
11476 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11477 +                             struct au_drinfo_rev_elm *elm)
11478 +{
11479 +       int err, len;
11480 +       ssize_t ssz;
11481 +       loff_t pos;
11482 +       struct path infopath = {
11483 +               .mnt = w->h_ppath.mnt
11484 +       };
11485 +       struct inode *h_dir, *h_inode, *delegated;
11486 +       struct file *infofile;
11487 +       struct qstr *qname;
11488 +
11489 +       AuDebugOn(elm
11490 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11491 +
11492 +       infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
11493 +                                              w->whnamelen);
11494 +       AuTraceErrPtr(infopath.dentry);
11495 +       if (IS_ERR(infopath.dentry)) {
11496 +               err = PTR_ERR(infopath.dentry);
11497 +               goto out;
11498 +       }
11499 +
11500 +       err = 0;
11501 +       h_dir = d_inode(w->h_ppath.dentry);
11502 +       if (elm && d_is_negative(infopath.dentry)) {
11503 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11504 +               AuTraceErr(err);
11505 +               if (unlikely(err))
11506 +                       goto out_dput;
11507 +               elm->created = 1;
11508 +               elm->info_dentry = dget(infopath.dentry);
11509 +       }
11510 +
11511 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11512 +       AuTraceErrPtr(infofile);
11513 +       if (IS_ERR(infofile)) {
11514 +               err = PTR_ERR(infofile);
11515 +               goto out_dput;
11516 +       }
11517 +
11518 +       h_inode = d_inode(infopath.dentry);
11519 +       if (elm && i_size_read(h_inode)) {
11520 +               h_inode = d_inode(w->h_dentry);
11521 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11522 +               AuTraceErrPtr(elm->info_last);
11523 +               if (IS_ERR(elm->info_last)) {
11524 +                       err = PTR_ERR(elm->info_last);
11525 +                       elm->info_last = NULL;
11526 +                       AuDebugOn(elm->info_dentry);
11527 +                       goto out_fput;
11528 +               }
11529 +       }
11530 +
11531 +       if (elm && w->renameback) {
11532 +               delegated = NULL;
11533 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11534 +               AuTraceErr(err);
11535 +               if (unlikely(err == -EWOULDBLOCK))
11536 +                       iput(delegated);
11537 +               goto out_fput;
11538 +       }
11539 +
11540 +       pos = 0;
11541 +       qname = &w->h_dentry->d_name;
11542 +       len = sizeof(*w->fdata) + qname->len;
11543 +       if (!elm)
11544 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11545 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11546 +       if (ssz == len) {
11547 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11548 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11549 +               goto out_fput; /* success */
11550 +       } else {
11551 +               err = -EIO;
11552 +               if (ssz < 0)
11553 +                       err = ssz;
11554 +               /* the caller should revert it using @elm */
11555 +       }
11556 +
11557 +out_fput:
11558 +       fput(infofile);
11559 +out_dput:
11560 +       dput(infopath.dentry);
11561 +out:
11562 +       AuTraceErr(err);
11563 +       return err;
11564 +}
11565 +
11566 +struct au_call_drinfo_do_store_args {
11567 +       int *errp;
11568 +       struct au_drinfo_store *w;
11569 +       struct au_drinfo_rev_elm *elm;
11570 +};
11571 +
11572 +static void au_call_drinfo_do_store(void *args)
11573 +{
11574 +       struct au_call_drinfo_do_store_args *a = args;
11575 +
11576 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11577 +}
11578 +
11579 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11580 +                              struct au_drinfo_rev_elm *elm)
11581 +{
11582 +       int err, wkq_err;
11583 +
11584 +       if (w->no_sio)
11585 +               err = au_drinfo_do_store(w, elm);
11586 +       else {
11587 +               struct au_call_drinfo_do_store_args a = {
11588 +                       .errp   = &err,
11589 +                       .w      = w,
11590 +                       .elm    = elm
11591 +               };
11592 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11593 +               if (unlikely(wkq_err))
11594 +                       err = wkq_err;
11595 +       }
11596 +       AuTraceErr(err);
11597 +
11598 +       return err;
11599 +}
11600 +
11601 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11602 +                                    aufs_bindex_t btgt)
11603 +{
11604 +       int err;
11605 +
11606 +       memset(w, 0, sizeof(*w));
11607 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11608 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11609 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11610 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11611 +       w->btgt = btgt;
11612 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11613 +
11614 +       err = -ENOMEM;
11615 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11616 +       if (unlikely(!w->fdata)) {
11617 +               AuTraceErr(err);
11618 +               goto out;
11619 +       }
11620 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11621 +       err = 0;
11622 +
11623 +out:
11624 +       return err;
11625 +}
11626 +
11627 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11628 +{
11629 +       au_kfree_rcu(w->fdata);
11630 +}
11631 +
11632 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11633 +                               struct au_drinfo_store *w)
11634 +{
11635 +       struct au_drinfo_rev_elm *elm;
11636 +       struct inode *h_dir, *delegated;
11637 +       int err, nelm;
11638 +       struct path infopath = {
11639 +               .mnt = w->h_ppath.mnt
11640 +       };
11641 +
11642 +       h_dir = d_inode(w->h_ppath.dentry);
11643 +       IMustLock(h_dir);
11644 +
11645 +       err = 0;
11646 +       elm = rev->elm;
11647 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11648 +               AuDebugOn(elm->created && elm->info_last);
11649 +               if (elm->created) {
11650 +                       AuDbg("here\n");
11651 +                       delegated = NULL;
11652 +                       infopath.dentry = elm->info_dentry;
11653 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11654 +                                          !w->no_sio);
11655 +                       AuTraceErr(err);
11656 +                       if (unlikely(err == -EWOULDBLOCK))
11657 +                               iput(delegated);
11658 +                       dput(elm->info_dentry);
11659 +               } else if (elm->info_last) {
11660 +                       AuDbg("here\n");
11661 +                       w->fdata->drinfo = *elm->info_last;
11662 +                       memcpy(w->fdata->drinfo.oldname,
11663 +                              elm->info_last->oldname,
11664 +                              elm->info_last->oldnamelen);
11665 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11666 +                       au_kfree_rcu(elm->info_last);
11667 +               }
11668 +               if (unlikely(err))
11669 +                       AuIOErr("%d, %s\n", err, w->whname);
11670 +               /* go on even if err */
11671 +       }
11672 +}
11673 +
11674 +/* caller has to call au_dr_rename_fin() later */
11675 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11676 +                          struct qstr *dst_name, void *_rev)
11677 +{
11678 +       int err, sz, nelm;
11679 +       aufs_bindex_t bindex, btail;
11680 +       struct au_drinfo_store work;
11681 +       struct au_drinfo_rev *rev, **p;
11682 +       struct au_drinfo_rev_elm *elm;
11683 +       struct super_block *sb;
11684 +       struct au_branch *br;
11685 +       struct au_hinode *hdir;
11686 +
11687 +       err = au_drinfo_store_work_init(&work, btgt);
11688 +       AuTraceErr(err);
11689 +       if (unlikely(err))
11690 +               goto out;
11691 +
11692 +       err = -ENOMEM;
11693 +       btail = au_dbtaildir(dentry);
11694 +       nelm = btail - btgt;
11695 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11696 +       rev = kcalloc(1, sz, GFP_NOFS);
11697 +       if (unlikely(!rev)) {
11698 +               AuTraceErr(err);
11699 +               goto out_args;
11700 +       }
11701 +       rev->nelm = nelm;
11702 +       elm = rev->elm;
11703 +       p = _rev;
11704 +       *p = rev;
11705 +
11706 +       err = 0;
11707 +       sb = dentry->d_sb;
11708 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11709 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11710 +       hdir = au_hi(d_inode(dentry), btgt);
11711 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11712 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11713 +               work.h_dentry = au_h_dptr(dentry, bindex);
11714 +               if (!work.h_dentry)
11715 +                       continue;
11716 +
11717 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11718 +                                         &work.allocated);
11719 +               AuTraceErr(err);
11720 +               if (unlikely(err))
11721 +                       break;
11722 +
11723 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11724 +               br = au_sbr(sb, bindex);
11725 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11726 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11727 +                                                work.infonamelen);
11728 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11729 +                     work.whnamelen, work.whname,
11730 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11731 +                     work.fdata->drinfo.oldnamelen,
11732 +                     work.fdata->drinfo.oldname);
11733 +
11734 +               err = au_drinfo_store_sio(&work, elm);
11735 +               AuTraceErr(err);
11736 +               if (unlikely(err))
11737 +                       break;
11738 +       }
11739 +       if (unlikely(err)) {
11740 +               /* revert all drinfo */
11741 +               au_drinfo_store_rev(rev, &work);
11742 +               au_kfree_try_rcu(rev);
11743 +               *p = NULL;
11744 +       }
11745 +       au_hn_inode_unlock(hdir);
11746 +
11747 +out_args:
11748 +       au_drinfo_store_work_fin(&work);
11749 +out:
11750 +       return err;
11751 +}
11752 +
11753 +/* ---------------------------------------------------------------------- */
11754 +
11755 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11756 +                struct qstr *dst_name, void *_rev)
11757 +{
11758 +       int err, already;
11759 +       ino_t ino;
11760 +       struct super_block *sb;
11761 +       struct au_branch *br;
11762 +       struct au_dr_br *dr;
11763 +       struct dentry *h_dentry;
11764 +       struct inode *h_inode;
11765 +       struct au_dr_hino *ent;
11766 +       struct au_drinfo_rev *rev, **p;
11767 +
11768 +       AuDbg("bindex %d\n", bindex);
11769 +
11770 +       err = -ENOMEM;
11771 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11772 +       if (unlikely(!ent))
11773 +               goto out;
11774 +
11775 +       sb = src->d_sb;
11776 +       br = au_sbr(sb, bindex);
11777 +       dr = &br->br_dirren;
11778 +       h_dentry = au_h_dptr(src, bindex);
11779 +       h_inode = d_inode(h_dentry);
11780 +       ino = h_inode->i_ino;
11781 +       ent->dr_h_ino = ino;
11782 +       already = au_dr_hino_test_add(dr, ino, ent);
11783 +       AuDbg("b%d, hi%llu, already %d\n",
11784 +             bindex, (unsigned long long)ino, already);
11785 +
11786 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11787 +       AuTraceErr(err);
11788 +       if (!err) {
11789 +               p = _rev;
11790 +               rev = *p;
11791 +               rev->already = already;
11792 +               goto out; /* success */
11793 +       }
11794 +
11795 +       /* revert */
11796 +       if (!already)
11797 +               au_dr_hino_del(dr, ent);
11798 +       au_kfree_rcu(ent);
11799 +
11800 +out:
11801 +       AuTraceErr(err);
11802 +       return err;
11803 +}
11804 +
11805 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11806 +{
11807 +       struct au_drinfo_rev *rev;
11808 +       struct au_drinfo_rev_elm *elm;
11809 +       int nelm;
11810 +
11811 +       rev = _rev;
11812 +       elm = rev->elm;
11813 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11814 +               dput(elm->info_dentry);
11815 +               au_kfree_rcu(elm->info_last);
11816 +       }
11817 +       au_kfree_try_rcu(rev);
11818 +}
11819 +
11820 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11821 +{
11822 +       int err;
11823 +       struct au_drinfo_store work;
11824 +       struct au_drinfo_rev *rev = _rev;
11825 +       struct super_block *sb;
11826 +       struct au_branch *br;
11827 +       struct inode *h_inode;
11828 +       struct au_dr_br *dr;
11829 +       struct au_dr_hino *ent;
11830 +
11831 +       err = au_drinfo_store_work_init(&work, btgt);
11832 +       if (unlikely(err))
11833 +               goto out;
11834 +
11835 +       sb = src->d_sb;
11836 +       br = au_sbr(sb, btgt);
11837 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11838 +       work.h_ppath.mnt = au_br_mnt(br);
11839 +       au_drinfo_store_rev(rev, &work);
11840 +       au_drinfo_store_work_fin(&work);
11841 +       if (rev->already)
11842 +               goto out;
11843 +
11844 +       dr = &br->br_dirren;
11845 +       h_inode = d_inode(work.h_ppath.dentry);
11846 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11847 +       BUG_ON(!ent);
11848 +       au_dr_hino_del(dr, ent);
11849 +       au_kfree_rcu(ent);
11850 +
11851 +out:
11852 +       au_kfree_try_rcu(rev);
11853 +       if (unlikely(err))
11854 +               pr_err("failed to remove dirren info\n");
11855 +}
11856 +
11857 +/* ---------------------------------------------------------------------- */
11858 +
11859 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11860 +                                          char *whname, int whnamelen,
11861 +                                          struct dentry **info_dentry)
11862 +{
11863 +       struct au_drinfo *drinfo;
11864 +       struct file *f;
11865 +       struct inode *h_dir;
11866 +       struct path infopath;
11867 +       int unlocked;
11868 +
11869 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11870 +
11871 +       *info_dentry = NULL;
11872 +       drinfo = NULL;
11873 +       unlocked = 0;
11874 +       h_dir = d_inode(h_ppath->dentry);
11875 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11876 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
11877 +                                              whnamelen);
11878 +       if (IS_ERR(infopath.dentry)) {
11879 +               drinfo = (void *)infopath.dentry;
11880 +               goto out;
11881 +       }
11882 +
11883 +       if (d_is_negative(infopath.dentry))
11884 +               goto out_dput; /* success */
11885 +
11886 +       infopath.mnt = h_ppath->mnt;
11887 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11888 +       inode_unlock_shared(h_dir);
11889 +       unlocked = 1;
11890 +       if (IS_ERR(f)) {
11891 +               drinfo = (void *)f;
11892 +               goto out_dput;
11893 +       }
11894 +
11895 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11896 +       if (IS_ERR_OR_NULL(drinfo))
11897 +               goto out_fput;
11898 +
11899 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11900 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11901 +
11902 +out_fput:
11903 +       fput(f);
11904 +out_dput:
11905 +       dput(infopath.dentry);
11906 +out:
11907 +       if (!unlocked)
11908 +               inode_unlock_shared(h_dir);
11909 +       AuTraceErrPtr(drinfo);
11910 +       return drinfo;
11911 +}
11912 +
11913 +struct au_drinfo_do_load_args {
11914 +       struct au_drinfo **drinfop;
11915 +       struct path *h_ppath;
11916 +       char *whname;
11917 +       int whnamelen;
11918 +       struct dentry **info_dentry;
11919 +};
11920 +
11921 +static void au_call_drinfo_do_load(void *args)
11922 +{
11923 +       struct au_drinfo_do_load_args *a = args;
11924 +
11925 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11926 +                                       a->info_dentry);
11927 +}
11928 +
11929 +struct au_drinfo_load {
11930 +       struct path h_ppath;
11931 +       struct qstr *qname;
11932 +       unsigned char no_sio;
11933 +
11934 +       aufs_bindex_t ninfo;
11935 +       struct au_drinfo **drinfo;
11936 +};
11937 +
11938 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11939 +                         struct au_branch *br)
11940 +{
11941 +       int err, wkq_err, whnamelen, e;
11942 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11943 +               = AUFS_WH_DR_INFO_PFX;
11944 +       struct au_drinfo *drinfo;
11945 +       struct qstr oldname;
11946 +       struct inode *h_dir, *delegated;
11947 +       struct dentry *info_dentry;
11948 +       struct path infopath;
11949 +
11950 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11951 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11952 +                                   sizeof(whname) - whnamelen);
11953 +       if (w->no_sio)
11954 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11955 +                                          &info_dentry);
11956 +       else {
11957 +               struct au_drinfo_do_load_args args = {
11958 +                       .drinfop        = &drinfo,
11959 +                       .h_ppath        = &w->h_ppath,
11960 +                       .whname         = whname,
11961 +                       .whnamelen      = whnamelen,
11962 +                       .info_dentry    = &info_dentry
11963 +               };
11964 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11965 +               if (unlikely(wkq_err))
11966 +                       drinfo = ERR_PTR(wkq_err);
11967 +       }
11968 +       err = PTR_ERR(drinfo);
11969 +       if (IS_ERR_OR_NULL(drinfo))
11970 +               goto out;
11971 +
11972 +       err = 0;
11973 +       oldname.len = drinfo->oldnamelen;
11974 +       oldname.name = drinfo->oldname;
11975 +       if (au_qstreq(w->qname, &oldname)) {
11976 +               /* the name is renamed back */
11977 +               au_kfree_rcu(drinfo);
11978 +               drinfo = NULL;
11979 +
11980 +               infopath.dentry = info_dentry;
11981 +               infopath.mnt = w->h_ppath.mnt;
11982 +               h_dir = d_inode(w->h_ppath.dentry);
11983 +               delegated = NULL;
11984 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11985 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11986 +               inode_unlock(h_dir);
11987 +               if (unlikely(e))
11988 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11989 +               if (unlikely(e == -EWOULDBLOCK))
11990 +                       iput(delegated);
11991 +       }
11992 +       au_kfree_rcu(w->drinfo[bindex]);
11993 +       w->drinfo[bindex] = drinfo;
11994 +       dput(info_dentry);
11995 +
11996 +out:
11997 +       AuTraceErr(err);
11998 +       return err;
11999 +}
12000 +
12001 +/* ---------------------------------------------------------------------- */
12002 +
12003 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12004 +{
12005 +       struct au_drinfo **p = drinfo;
12006 +
12007 +       while (n-- > 0)
12008 +               au_kfree_rcu(*drinfo++);
12009 +       au_kfree_try_rcu(p);
12010 +}
12011 +
12012 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12013 +              aufs_bindex_t btgt)
12014 +{
12015 +       int err, ninfo;
12016 +       struct au_drinfo_load w;
12017 +       aufs_bindex_t bindex, bbot;
12018 +       struct au_branch *br;
12019 +       struct inode *h_dir;
12020 +       struct au_dr_hino *ent;
12021 +       struct super_block *sb;
12022 +
12023 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12024 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12025 +             AuLNPair(&lkup->whname), btgt);
12026 +
12027 +       sb = dentry->d_sb;
12028 +       bbot = au_sbbot(sb);
12029 +       w.ninfo = bbot + 1;
12030 +       if (!lkup->dirren.drinfo) {
12031 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12032 +                                             sizeof(*lkup->dirren.drinfo),
12033 +                                             GFP_NOFS);
12034 +               if (unlikely(!lkup->dirren.drinfo)) {
12035 +                       err = -ENOMEM;
12036 +                       goto out;
12037 +               }
12038 +               lkup->dirren.ninfo = w.ninfo;
12039 +       }
12040 +       w.drinfo = lkup->dirren.drinfo;
12041 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12042 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12043 +       AuDebugOn(!w.h_ppath.dentry);
12044 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12045 +       w.qname = &dentry->d_name;
12046 +
12047 +       ninfo = 0;
12048 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12049 +               br = au_sbr(sb, bindex);
12050 +               err = au_drinfo_load(&w, bindex, br);
12051 +               if (unlikely(err))
12052 +                       goto out_free;
12053 +               if (w.drinfo[bindex])
12054 +                       ninfo++;
12055 +       }
12056 +       if (!ninfo) {
12057 +               br = au_sbr(sb, btgt);
12058 +               h_dir = d_inode(w.h_ppath.dentry);
12059 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12060 +               AuDebugOn(!ent);
12061 +               au_dr_hino_del(&br->br_dirren, ent);
12062 +               au_kfree_rcu(ent);
12063 +       }
12064 +       goto out; /* success */
12065 +
12066 +out_free:
12067 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12068 +       lkup->dirren.ninfo = 0;
12069 +       lkup->dirren.drinfo = NULL;
12070 +out:
12071 +       AuTraceErr(err);
12072 +       return err;
12073 +}
12074 +
12075 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12076 +{
12077 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12078 +}
12079 +
12080 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12081 +{
12082 +       int err;
12083 +       struct au_drinfo *drinfo;
12084 +
12085 +       err = 0;
12086 +       if (!lkup->dirren.drinfo)
12087 +               goto out;
12088 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12089 +       drinfo = lkup->dirren.drinfo[btgt];
12090 +       if (!drinfo)
12091 +               goto out;
12092 +
12093 +       au_kfree_try_rcu(lkup->whname.name);
12094 +       lkup->whname.name = NULL;
12095 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12096 +       lkup->dirren.dr_name.name = drinfo->oldname;
12097 +       lkup->name = &lkup->dirren.dr_name;
12098 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12099 +       if (!err)
12100 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12101 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12102 +                     btgt);
12103 +
12104 +out:
12105 +       AuTraceErr(err);
12106 +       return err;
12107 +}
12108 +
12109 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12110 +                    ino_t h_ino)
12111 +{
12112 +       int match;
12113 +       struct au_drinfo *drinfo;
12114 +
12115 +       match = 1;
12116 +       if (!lkup->dirren.drinfo)
12117 +               goto out;
12118 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12119 +       drinfo = lkup->dirren.drinfo[bindex];
12120 +       if (!drinfo)
12121 +               goto out;
12122 +
12123 +       match = (drinfo->ino == h_ino);
12124 +       AuDbg("match %d\n", match);
12125 +
12126 +out:
12127 +       return match;
12128 +}
12129 +
12130 +/* ---------------------------------------------------------------------- */
12131 +
12132 +int au_dr_opt_set(struct super_block *sb)
12133 +{
12134 +       int err;
12135 +       aufs_bindex_t bindex, bbot;
12136 +       struct au_branch *br;
12137 +
12138 +       err = 0;
12139 +       bbot = au_sbbot(sb);
12140 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12141 +               br = au_sbr(sb, bindex);
12142 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12143 +       }
12144 +
12145 +       return err;
12146 +}
12147 +
12148 +int au_dr_opt_flush(struct super_block *sb)
12149 +{
12150 +       int err;
12151 +       aufs_bindex_t bindex, bbot;
12152 +       struct au_branch *br;
12153 +
12154 +       err = 0;
12155 +       bbot = au_sbbot(sb);
12156 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12157 +               br = au_sbr(sb, bindex);
12158 +               if (au_br_writable(br->br_perm))
12159 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12160 +       }
12161 +
12162 +       return err;
12163 +}
12164 +
12165 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12166 +{
12167 +       int err;
12168 +       aufs_bindex_t bindex, bbot;
12169 +       struct au_branch *br;
12170 +
12171 +       err = 0;
12172 +       if (!no_flush) {
12173 +               err = au_dr_opt_flush(sb);
12174 +               if (unlikely(err))
12175 +                       goto out;
12176 +       }
12177 +
12178 +       bbot = au_sbbot(sb);
12179 +       for (bindex = 0; bindex <= bbot; bindex++) {
12180 +               br = au_sbr(sb, bindex);
12181 +               au_dr_hino_free(&br->br_dirren);
12182 +       }
12183 +
12184 +out:
12185 +       return err;
12186 +}
12187 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12188 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12189 +++ linux/fs/aufs/dirren.h      2020-01-27 10:57:18.168871450 +0100
12190 @@ -0,0 +1,140 @@
12191 +/* SPDX-License-Identifier: GPL-2.0 */
12192 +/*
12193 + * Copyright (C) 2017-2020 Junjiro R. Okajima
12194 + *
12195 + * This program, aufs is free software; you can redistribute it and/or modify
12196 + * it under the terms of the GNU General Public License as published by
12197 + * the Free Software Foundation; either version 2 of the License, or
12198 + * (at your option) any later version.
12199 + *
12200 + * This program is distributed in the hope that it will be useful,
12201 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12202 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12203 + * GNU General Public License for more details.
12204 + *
12205 + * You should have received a copy of the GNU General Public License
12206 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12207 + */
12208 +
12209 +/*
12210 + * renamed dir info
12211 + */
12212 +
12213 +#ifndef __AUFS_DIRREN_H__
12214 +#define __AUFS_DIRREN_H__
12215 +
12216 +#ifdef __KERNEL__
12217 +
12218 +#include <linux/dcache.h>
12219 +#include <linux/statfs.h>
12220 +#include <linux/uuid.h>
12221 +#include "hbl.h"
12222 +
12223 +#define AuDirren_NHASH 100
12224 +
12225 +#ifdef CONFIG_AUFS_DIRREN
12226 +enum au_brid_type {
12227 +       AuBrid_Unset,
12228 +       AuBrid_UUID,
12229 +       AuBrid_FSID,
12230 +       AuBrid_DEV
12231 +};
12232 +
12233 +struct au_dr_brid {
12234 +       enum au_brid_type       type;
12235 +       union {
12236 +               uuid_t  uuid;   /* unimplemented yet */
12237 +               fsid_t  fsid;
12238 +               dev_t   dev;
12239 +       };
12240 +};
12241 +
12242 +/* 20 is the max digits length of ulong 64 */
12243 +/* brid-type "_" uuid "_" inum */
12244 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12245 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12246 +
12247 +struct au_dr_hino {
12248 +       struct hlist_bl_node    dr_hnode;
12249 +       ino_t                   dr_h_ino;
12250 +};
12251 +
12252 +struct au_dr_br {
12253 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12254 +       struct au_dr_brid       dr_brid;
12255 +};
12256 +
12257 +struct au_dr_lookup {
12258 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12259 +       struct qstr             dr_name; /* subset of dr_info */
12260 +       aufs_bindex_t           ninfo;
12261 +       struct au_drinfo        **drinfo;
12262 +};
12263 +#else
12264 +struct au_dr_hino;
12265 +/* empty */
12266 +struct au_dr_br { };
12267 +struct au_dr_lookup { };
12268 +#endif
12269 +
12270 +/* ---------------------------------------------------------------------- */
12271 +
12272 +struct au_branch;
12273 +struct au_do_lookup_args;
12274 +struct au_hinode;
12275 +#ifdef CONFIG_AUFS_DIRREN
12276 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12277 +                       struct au_dr_hino *add_ent);
12278 +void au_dr_hino_free(struct au_dr_br *dr);
12279 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12280 +                 const struct path *path);
12281 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12282 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12283 +                struct qstr *dst_name, void *_rev);
12284 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12285 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12286 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12287 +              aufs_bindex_t bindex);
12288 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12289 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12290 +                    ino_t h_ino);
12291 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12292 +int au_dr_opt_set(struct super_block *sb);
12293 +int au_dr_opt_flush(struct super_block *sb);
12294 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12295 +#else
12296 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12297 +          struct au_dr_hino *add_ent);
12298 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12299 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12300 +          const struct path *path);
12301 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12302 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12303 +          struct qstr *dst_name, void *_rev);
12304 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12305 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12306 +          void *rev);
12307 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12308 +          aufs_bindex_t bindex);
12309 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12310 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12311 +          aufs_bindex_t bindex, ino_t h_ino);
12312 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12313 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12314 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12315 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12316 +#endif
12317 +
12318 +/* ---------------------------------------------------------------------- */
12319 +
12320 +#ifdef CONFIG_AUFS_DIRREN
12321 +static inline int au_dr_ihash(ino_t h_ino)
12322 +{
12323 +       return h_ino % AuDirren_NHASH;
12324 +}
12325 +#else
12326 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12327 +#endif
12328 +
12329 +#endif /* __KERNEL__ */
12330 +#endif /* __AUFS_DIRREN_H__ */
12331 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12332 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12333 +++ linux/fs/aufs/dynop.c       2020-12-15 14:10:58.914689728 +0100
12334 @@ -0,0 +1,368 @@
12335 +// SPDX-License-Identifier: GPL-2.0
12336 +/*
12337 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12338 + *
12339 + * This program, aufs is free software; you can redistribute it and/or modify
12340 + * it under the terms of the GNU General Public License as published by
12341 + * the Free Software Foundation; either version 2 of the License, or
12342 + * (at your option) any later version.
12343 + *
12344 + * This program is distributed in the hope that it will be useful,
12345 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12346 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12347 + * GNU General Public License for more details.
12348 + *
12349 + * You should have received a copy of the GNU General Public License
12350 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12351 + */
12352 +
12353 +/*
12354 + * dynamically customizable operations for regular files
12355 + */
12356 +
12357 +#include "aufs.h"
12358 +
12359 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12360 +
12361 +/*
12362 + * How large will these lists be?
12363 + * Usually just a few elements, 20-30 at most for each, I guess.
12364 + */
12365 +static struct hlist_bl_head dynop[AuDyLast];
12366 +
12367 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12368 +                                    const void *h_op)
12369 +{
12370 +       struct au_dykey *key, *tmp;
12371 +       struct hlist_bl_node *pos;
12372 +
12373 +       key = NULL;
12374 +       hlist_bl_lock(hbl);
12375 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12376 +               if (tmp->dk_op.dy_hop == h_op) {
12377 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12378 +                               key = tmp;
12379 +                       break;
12380 +               }
12381 +       hlist_bl_unlock(hbl);
12382 +
12383 +       return key;
12384 +}
12385 +
12386 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12387 +{
12388 +       struct au_dykey **k, *found;
12389 +       const void *h_op = key->dk_op.dy_hop;
12390 +       int i;
12391 +
12392 +       found = NULL;
12393 +       k = br->br_dykey;
12394 +       for (i = 0; i < AuBrDynOp; i++)
12395 +               if (k[i]) {
12396 +                       if (k[i]->dk_op.dy_hop == h_op) {
12397 +                               found = k[i];
12398 +                               break;
12399 +                       }
12400 +               } else
12401 +                       break;
12402 +       if (!found) {
12403 +               spin_lock(&br->br_dykey_lock);
12404 +               for (; i < AuBrDynOp; i++)
12405 +                       if (k[i]) {
12406 +                               if (k[i]->dk_op.dy_hop == h_op) {
12407 +                                       found = k[i];
12408 +                                       break;
12409 +                               }
12410 +                       } else {
12411 +                               k[i] = key;
12412 +                               break;
12413 +                       }
12414 +               spin_unlock(&br->br_dykey_lock);
12415 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12416 +       }
12417 +
12418 +       return found;
12419 +}
12420 +
12421 +/* kref_get() if @key is already added */
12422 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12423 +{
12424 +       struct au_dykey *tmp, *found;
12425 +       struct hlist_bl_node *pos;
12426 +       const void *h_op = key->dk_op.dy_hop;
12427 +
12428 +       found = NULL;
12429 +       hlist_bl_lock(hbl);
12430 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12431 +               if (tmp->dk_op.dy_hop == h_op) {
12432 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12433 +                               found = tmp;
12434 +                       break;
12435 +               }
12436 +       if (!found)
12437 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12438 +       hlist_bl_unlock(hbl);
12439 +
12440 +       if (!found)
12441 +               DyPrSym(key);
12442 +       return found;
12443 +}
12444 +
12445 +static void dy_free_rcu(struct rcu_head *rcu)
12446 +{
12447 +       struct au_dykey *key;
12448 +
12449 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12450 +       DyPrSym(key);
12451 +       kfree(key);
12452 +}
12453 +
12454 +static void dy_free(struct kref *kref)
12455 +{
12456 +       struct au_dykey *key;
12457 +       struct hlist_bl_head *hbl;
12458 +
12459 +       key = container_of(kref, struct au_dykey, dk_kref);
12460 +       hbl = dynop + key->dk_op.dy_type;
12461 +       au_hbl_del(&key->dk_hnode, hbl);
12462 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12463 +}
12464 +
12465 +void au_dy_put(struct au_dykey *key)
12466 +{
12467 +       kref_put(&key->dk_kref, dy_free);
12468 +}
12469 +
12470 +/* ---------------------------------------------------------------------- */
12471 +
12472 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12473 +
12474 +#ifdef CONFIG_AUFS_DEBUG
12475 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12476 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12477 +#else
12478 +#define DyDbgDeclare(cnt)      do {} while (0)
12479 +#define DyDbgInc(cnt)          do {} while (0)
12480 +#endif
12481 +
12482 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12483 +       DyDbgInc(cnt);                                                  \
12484 +       if (h_op->func) {                                               \
12485 +               if (src.func)                                           \
12486 +                       dst.func = src.func;                            \
12487 +               else                                                    \
12488 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12489 +       }                                                               \
12490 +} while (0)
12491 +
12492 +#define DySetForce(func, dst, src) do {                \
12493 +       AuDebugOn(!src.func);                   \
12494 +       DyDbgInc(cnt);                          \
12495 +       dst.func = src.func;                    \
12496 +} while (0)
12497 +
12498 +#define DySetAop(func) \
12499 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12500 +#define DySetAopForce(func) \
12501 +       DySetForce(func, dyaop->da_op, aufs_aop)
12502 +
12503 +static void dy_aop(struct au_dykey *key, const void *h_op,
12504 +                  struct super_block *h_sb __maybe_unused)
12505 +{
12506 +       struct au_dyaop *dyaop = (void *)key;
12507 +       const struct address_space_operations *h_aop = h_op;
12508 +       DyDbgDeclare(cnt);
12509 +
12510 +       AuDbg("%s\n", au_sbtype(h_sb));
12511 +
12512 +       DySetAop(writepage);
12513 +       DySetAopForce(readpage);        /* force */
12514 +       DySetAop(writepages);
12515 +       DySetAop(set_page_dirty);
12516 +       DySetAop(readpages);
12517 +       DySetAop(readahead);
12518 +       DySetAop(write_begin);
12519 +       DySetAop(write_end);
12520 +       DySetAop(bmap);
12521 +       DySetAop(invalidatepage);
12522 +       DySetAop(releasepage);
12523 +       DySetAop(freepage);
12524 +       /* this one will be changed according to an aufs mount option */
12525 +       DySetAop(direct_IO);
12526 +       DySetAop(migratepage);
12527 +       DySetAop(isolate_page);
12528 +       DySetAop(putback_page);
12529 +       DySetAop(launder_page);
12530 +       DySetAop(is_partially_uptodate);
12531 +       DySetAop(is_dirty_writeback);
12532 +       DySetAop(error_remove_page);
12533 +       DySetAop(swap_activate);
12534 +       DySetAop(swap_deactivate);
12535 +
12536 +       DyDbgSize(cnt, *h_aop);
12537 +}
12538 +
12539 +/* ---------------------------------------------------------------------- */
12540 +
12541 +static void dy_bug(struct kref *kref)
12542 +{
12543 +       BUG();
12544 +}
12545 +
12546 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12547 +{
12548 +       struct au_dykey *key, *old;
12549 +       struct hlist_bl_head *hbl;
12550 +       struct op {
12551 +               unsigned int sz;
12552 +               void (*set)(struct au_dykey *key, const void *h_op,
12553 +                           struct super_block *h_sb __maybe_unused);
12554 +       };
12555 +       static const struct op a[] = {
12556 +               [AuDy_AOP] = {
12557 +                       .sz     = sizeof(struct au_dyaop),
12558 +                       .set    = dy_aop
12559 +               }
12560 +       };
12561 +       const struct op *p;
12562 +
12563 +       hbl = dynop + op->dy_type;
12564 +       key = dy_gfind_get(hbl, op->dy_hop);
12565 +       if (key)
12566 +               goto out_add; /* success */
12567 +
12568 +       p = a + op->dy_type;
12569 +       key = kzalloc(p->sz, GFP_NOFS);
12570 +       if (unlikely(!key)) {
12571 +               key = ERR_PTR(-ENOMEM);
12572 +               goto out;
12573 +       }
12574 +
12575 +       key->dk_op.dy_hop = op->dy_hop;
12576 +       kref_init(&key->dk_kref);
12577 +       p->set(key, op->dy_hop, au_br_sb(br));
12578 +       old = dy_gadd(hbl, key);
12579 +       if (old) {
12580 +               au_kfree_rcu(key);
12581 +               key = old;
12582 +       }
12583 +
12584 +out_add:
12585 +       old = dy_bradd(br, key);
12586 +       if (old)
12587 +               /* its ref-count should never be zero here */
12588 +               kref_put(&key->dk_kref, dy_bug);
12589 +out:
12590 +       return key;
12591 +}
12592 +
12593 +/* ---------------------------------------------------------------------- */
12594 +/*
12595 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12596 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12597 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12598 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12599 + * See the aufs manual in detail.
12600 + */
12601 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12602 +{
12603 +       if (!do_dx)
12604 +               dyaop->da_op.direct_IO = NULL;
12605 +       else
12606 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12607 +}
12608 +
12609 +static struct au_dyaop *dy_aget(struct au_branch *br,
12610 +                               const struct address_space_operations *h_aop,
12611 +                               int do_dx)
12612 +{
12613 +       struct au_dyaop *dyaop;
12614 +       struct au_dynop op;
12615 +
12616 +       op.dy_type = AuDy_AOP;
12617 +       op.dy_haop = h_aop;
12618 +       dyaop = (void *)dy_get(&op, br);
12619 +       if (IS_ERR(dyaop))
12620 +               goto out;
12621 +       dy_adx(dyaop, do_dx);
12622 +
12623 +out:
12624 +       return dyaop;
12625 +}
12626 +
12627 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12628 +               struct inode *h_inode)
12629 +{
12630 +       int err, do_dx;
12631 +       struct super_block *sb;
12632 +       struct au_branch *br;
12633 +       struct au_dyaop *dyaop;
12634 +
12635 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12636 +       IiMustWriteLock(inode);
12637 +
12638 +       sb = inode->i_sb;
12639 +       br = au_sbr(sb, bindex);
12640 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12641 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12642 +       err = PTR_ERR(dyaop);
12643 +       if (IS_ERR(dyaop))
12644 +               /* unnecessary to call dy_fput() */
12645 +               goto out;
12646 +
12647 +       err = 0;
12648 +       inode->i_mapping->a_ops = &dyaop->da_op;
12649 +
12650 +out:
12651 +       return err;
12652 +}
12653 +
12654 +/*
12655 + * Is it safe to replace a_ops during the inode/file is in operation?
12656 + * Yes, I hope so.
12657 + */
12658 +int au_dy_irefresh(struct inode *inode)
12659 +{
12660 +       int err;
12661 +       aufs_bindex_t btop;
12662 +       struct inode *h_inode;
12663 +
12664 +       err = 0;
12665 +       if (S_ISREG(inode->i_mode)) {
12666 +               btop = au_ibtop(inode);
12667 +               h_inode = au_h_iptr(inode, btop);
12668 +               err = au_dy_iaop(inode, btop, h_inode);
12669 +       }
12670 +       return err;
12671 +}
12672 +
12673 +void au_dy_arefresh(int do_dx)
12674 +{
12675 +       struct hlist_bl_head *hbl;
12676 +       struct hlist_bl_node *pos;
12677 +       struct au_dykey *key;
12678 +
12679 +       hbl = dynop + AuDy_AOP;
12680 +       hlist_bl_lock(hbl);
12681 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12682 +               dy_adx((void *)key, do_dx);
12683 +       hlist_bl_unlock(hbl);
12684 +}
12685 +
12686 +/* ---------------------------------------------------------------------- */
12687 +
12688 +void __init au_dy_init(void)
12689 +{
12690 +       int i;
12691 +
12692 +       for (i = 0; i < AuDyLast; i++)
12693 +               INIT_HLIST_BL_HEAD(dynop + i);
12694 +}
12695 +
12696 +void au_dy_fin(void)
12697 +{
12698 +       int i;
12699 +
12700 +       for (i = 0; i < AuDyLast; i++)
12701 +               WARN_ON(!hlist_bl_empty(dynop + i));
12702 +}
12703 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12704 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12705 +++ linux/fs/aufs/dynop.h       2020-01-27 10:57:18.168871450 +0100
12706 @@ -0,0 +1,77 @@
12707 +/* SPDX-License-Identifier: GPL-2.0 */
12708 +/*
12709 + * Copyright (C) 2010-2020 Junjiro R. Okajima
12710 + *
12711 + * This program, aufs is free software; you can redistribute it and/or modify
12712 + * it under the terms of the GNU General Public License as published by
12713 + * the Free Software Foundation; either version 2 of the License, or
12714 + * (at your option) any later version.
12715 + *
12716 + * This program is distributed in the hope that it will be useful,
12717 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12718 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12719 + * GNU General Public License for more details.
12720 + *
12721 + * You should have received a copy of the GNU General Public License
12722 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12723 + */
12724 +
12725 +/*
12726 + * dynamically customizable operations (for regular files only)
12727 + */
12728 +
12729 +#ifndef __AUFS_DYNOP_H__
12730 +#define __AUFS_DYNOP_H__
12731 +
12732 +#ifdef __KERNEL__
12733 +
12734 +#include <linux/fs.h>
12735 +#include <linux/kref.h>
12736 +
12737 +enum {AuDy_AOP, AuDyLast};
12738 +
12739 +struct au_dynop {
12740 +       int                                             dy_type;
12741 +       union {
12742 +               const void                              *dy_hop;
12743 +               const struct address_space_operations   *dy_haop;
12744 +       };
12745 +};
12746 +
12747 +struct au_dykey {
12748 +       union {
12749 +               struct hlist_bl_node    dk_hnode;
12750 +               struct rcu_head         dk_rcu;
12751 +       };
12752 +       struct au_dynop         dk_op;
12753 +
12754 +       /*
12755 +        * during I am in the branch local array, kref is gotten. when the
12756 +        * branch is removed, kref is put.
12757 +        */
12758 +       struct kref             dk_kref;
12759 +};
12760 +
12761 +/* stop unioning since their sizes are very different from each other */
12762 +struct au_dyaop {
12763 +       struct au_dykey                 da_key;
12764 +       struct address_space_operations da_op; /* not const */
12765 +};
12766 +/* make sure that 'struct au_dykey *' can be any type */
12767 +static_assert(!offsetof(struct au_dyaop, da_key));
12768 +
12769 +/* ---------------------------------------------------------------------- */
12770 +
12771 +/* dynop.c */
12772 +struct au_branch;
12773 +void au_dy_put(struct au_dykey *key);
12774 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12775 +               struct inode *h_inode);
12776 +int au_dy_irefresh(struct inode *inode);
12777 +void au_dy_arefresh(int do_dio);
12778 +
12779 +void __init au_dy_init(void);
12780 +void au_dy_fin(void);
12781 +
12782 +#endif /* __KERNEL__ */
12783 +#endif /* __AUFS_DYNOP_H__ */
12784 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12785 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12786 +++ linux/fs/aufs/export.c      2020-12-15 14:10:58.914689728 +0100
12787 @@ -0,0 +1,837 @@
12788 +// SPDX-License-Identifier: GPL-2.0
12789 +/*
12790 + * Copyright (C) 2005-2020 Junjiro R. Okajima
12791 + *
12792 + * This program, aufs is free software; you can redistribute it and/or modify
12793 + * it under the terms of the GNU General Public License as published by
12794 + * the Free Software Foundation; either version 2 of the License, or
12795 + * (at your option) any later version.
12796 + *
12797 + * This program is distributed in the hope that it will be useful,
12798 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12799 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12800 + * GNU General Public License for more details.
12801 + *
12802 + * You should have received a copy of the GNU General Public License
12803 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12804 + */
12805 +
12806 +/*
12807 + * export via nfs
12808 + */
12809 +
12810 +#include <linux/exportfs.h>
12811 +#include <linux/fs_struct.h>
12812 +#include <linux/namei.h>
12813 +#include <linux/nsproxy.h>
12814 +#include <linux/random.h>
12815 +#include <linux/writeback.h>
12816 +#include "aufs.h"
12817 +
12818 +union conv {
12819 +#ifdef CONFIG_AUFS_INO_T_64
12820 +       __u32 a[2];
12821 +#else
12822 +       __u32 a[1];
12823 +#endif
12824 +       ino_t ino;
12825 +};
12826 +
12827 +static ino_t decode_ino(__u32 *a)
12828 +{
12829 +       union conv u;
12830 +
12831 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12832 +       u.a[0] = a[0];
12833 +#ifdef CONFIG_AUFS_INO_T_64
12834 +       u.a[1] = a[1];
12835 +#endif
12836 +       return u.ino;
12837 +}
12838 +
12839 +static void encode_ino(__u32 *a, ino_t ino)
12840 +{
12841 +       union conv u;
12842 +
12843 +       u.ino = ino;
12844 +       a[0] = u.a[0];
12845 +#ifdef CONFIG_AUFS_INO_T_64
12846 +       a[1] = u.a[1];
12847 +#endif
12848 +}
12849 +
12850 +/* NFS file handle */
12851 +enum {
12852 +       Fh_br_id,
12853 +       Fh_sigen,
12854 +#ifdef CONFIG_AUFS_INO_T_64
12855 +       /* support 64bit inode number */
12856 +       Fh_ino1,
12857 +       Fh_ino2,
12858 +       Fh_dir_ino1,
12859 +       Fh_dir_ino2,
12860 +#else
12861 +       Fh_ino1,
12862 +       Fh_dir_ino1,
12863 +#endif
12864 +       Fh_igen,
12865 +       Fh_h_type,
12866 +       Fh_tail,
12867 +
12868 +       Fh_ino = Fh_ino1,
12869 +       Fh_dir_ino = Fh_dir_ino1
12870 +};
12871 +
12872 +static int au_test_anon(struct dentry *dentry)
12873 +{
12874 +       /* note: read d_flags without d_lock */
12875 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12876 +}
12877 +
12878 +int au_test_nfsd(void)
12879 +{
12880 +       int ret;
12881 +       struct task_struct *tsk = current;
12882 +       char comm[sizeof(tsk->comm)];
12883 +
12884 +       ret = 0;
12885 +       if (tsk->flags & PF_KTHREAD) {
12886 +               get_task_comm(comm, tsk);
12887 +               ret = !strcmp(comm, "nfsd");
12888 +       }
12889 +
12890 +       return ret;
12891 +}
12892 +
12893 +/* ---------------------------------------------------------------------- */
12894 +/* inode generation external table */
12895 +
12896 +void au_xigen_inc(struct inode *inode)
12897 +{
12898 +       loff_t pos;
12899 +       ssize_t sz;
12900 +       __u32 igen;
12901 +       struct super_block *sb;
12902 +       struct au_sbinfo *sbinfo;
12903 +
12904 +       sb = inode->i_sb;
12905 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12906 +
12907 +       sbinfo = au_sbi(sb);
12908 +       pos = inode->i_ino;
12909 +       pos *= sizeof(igen);
12910 +       igen = inode->i_generation + 1;
12911 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12912 +       if (sz == sizeof(igen))
12913 +               return; /* success */
12914 +
12915 +       if (unlikely(sz >= 0))
12916 +               AuIOErr("xigen error (%zd)\n", sz);
12917 +}
12918 +
12919 +int au_xigen_new(struct inode *inode)
12920 +{
12921 +       int err;
12922 +       loff_t pos;
12923 +       ssize_t sz;
12924 +       struct super_block *sb;
12925 +       struct au_sbinfo *sbinfo;
12926 +       struct file *file;
12927 +
12928 +       err = 0;
12929 +       /* todo: dirty, at mount time */
12930 +       if (inode->i_ino == AUFS_ROOT_INO)
12931 +               goto out;
12932 +       sb = inode->i_sb;
12933 +       SiMustAnyLock(sb);
12934 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12935 +               goto out;
12936 +
12937 +       err = -EFBIG;
12938 +       pos = inode->i_ino;
12939 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12940 +               AuIOErr1("too large i%lld\n", pos);
12941 +               goto out;
12942 +       }
12943 +       pos *= sizeof(inode->i_generation);
12944 +
12945 +       err = 0;
12946 +       sbinfo = au_sbi(sb);
12947 +       file = sbinfo->si_xigen;
12948 +       BUG_ON(!file);
12949 +
12950 +       if (vfsub_f_size_read(file)
12951 +           < pos + sizeof(inode->i_generation)) {
12952 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12953 +               sz = xino_fwrite(file, &inode->i_generation,
12954 +                                sizeof(inode->i_generation), &pos);
12955 +       } else
12956 +               sz = xino_fread(file, &inode->i_generation,
12957 +                               sizeof(inode->i_generation), &pos);
12958 +       if (sz == sizeof(inode->i_generation))
12959 +               goto out; /* success */
12960 +
12961 +       err = sz;
12962 +       if (unlikely(sz >= 0)) {
12963 +               err = -EIO;
12964 +               AuIOErr("xigen error (%zd)\n", sz);
12965 +       }
12966 +
12967 +out:
12968 +       return err;
12969 +}
12970 +
12971 +int au_xigen_set(struct super_block *sb, struct path *path)
12972 +{
12973 +       int err;
12974 +       struct au_sbinfo *sbinfo;
12975 +       struct file *file;
12976 +
12977 +       SiMustWriteLock(sb);
12978 +
12979 +       sbinfo = au_sbi(sb);
12980 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12981 +       err = PTR_ERR(file);
12982 +       if (IS_ERR(file))
12983 +               goto out;
12984 +       err = 0;
12985 +       if (sbinfo->si_xigen)
12986 +               fput(sbinfo->si_xigen);
12987 +       sbinfo->si_xigen = file;
12988 +
12989 +out:
12990 +       AuTraceErr(err);
12991 +       return err;
12992 +}
12993 +
12994 +void au_xigen_clr(struct super_block *sb)
12995 +{
12996 +       struct au_sbinfo *sbinfo;
12997 +
12998 +       SiMustWriteLock(sb);
12999 +
13000 +       sbinfo = au_sbi(sb);
13001 +       if (sbinfo->si_xigen) {
13002 +               fput(sbinfo->si_xigen);
13003 +               sbinfo->si_xigen = NULL;
13004 +       }
13005 +}
13006 +
13007 +/* ---------------------------------------------------------------------- */
13008 +
13009 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13010 +                                   ino_t dir_ino)
13011 +{
13012 +       struct dentry *dentry, *d;
13013 +       struct inode *inode;
13014 +       unsigned int sigen;
13015 +
13016 +       dentry = NULL;
13017 +       inode = ilookup(sb, ino);
13018 +       if (!inode)
13019 +               goto out;
13020 +
13021 +       dentry = ERR_PTR(-ESTALE);
13022 +       sigen = au_sigen(sb);
13023 +       if (unlikely(au_is_bad_inode(inode)
13024 +                    || IS_DEADDIR(inode)
13025 +                    || sigen != au_iigen(inode, NULL)))
13026 +               goto out_iput;
13027 +
13028 +       dentry = NULL;
13029 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13030 +               dentry = d_find_alias(inode);
13031 +       else {
13032 +               spin_lock(&inode->i_lock);
13033 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13034 +                       spin_lock(&d->d_lock);
13035 +                       if (!au_test_anon(d)
13036 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13037 +                               dentry = dget_dlock(d);
13038 +                               spin_unlock(&d->d_lock);
13039 +                               break;
13040 +                       }
13041 +                       spin_unlock(&d->d_lock);
13042 +               }
13043 +               spin_unlock(&inode->i_lock);
13044 +       }
13045 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13046 +               /* need to refresh */
13047 +               dput(dentry);
13048 +               dentry = NULL;
13049 +       }
13050 +
13051 +out_iput:
13052 +       iput(inode);
13053 +out:
13054 +       AuTraceErrPtr(dentry);
13055 +       return dentry;
13056 +}
13057 +
13058 +/* ---------------------------------------------------------------------- */
13059 +
13060 +/* todo: dirty? */
13061 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13062 +
13063 +struct au_compare_mnt_args {
13064 +       /* input */
13065 +       struct super_block *sb;
13066 +
13067 +       /* output */
13068 +       struct vfsmount *mnt;
13069 +};
13070 +
13071 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13072 +{
13073 +       struct au_compare_mnt_args *a = arg;
13074 +
13075 +       if (mnt->mnt_sb != a->sb)
13076 +               return 0;
13077 +       a->mnt = mntget(mnt);
13078 +       return 1;
13079 +}
13080 +
13081 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13082 +{
13083 +       int err;
13084 +       struct path root;
13085 +       struct au_compare_mnt_args args = {
13086 +               .sb = sb
13087 +       };
13088 +
13089 +       get_fs_root(current->fs, &root);
13090 +       rcu_read_lock();
13091 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13092 +       rcu_read_unlock();
13093 +       path_put(&root);
13094 +       AuDebugOn(!err);
13095 +       AuDebugOn(!args.mnt);
13096 +       return args.mnt;
13097 +}
13098 +
13099 +struct au_nfsd_si_lock {
13100 +       unsigned int sigen;
13101 +       aufs_bindex_t bindex, br_id;
13102 +       unsigned char force_lock;
13103 +};
13104 +
13105 +static int si_nfsd_read_lock(struct super_block *sb,
13106 +                            struct au_nfsd_si_lock *nsi_lock)
13107 +{
13108 +       int err;
13109 +       aufs_bindex_t bindex;
13110 +
13111 +       si_read_lock(sb, AuLock_FLUSH);
13112 +
13113 +       /* branch id may be wrapped around */
13114 +       err = 0;
13115 +       bindex = au_br_index(sb, nsi_lock->br_id);
13116 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13117 +               goto out; /* success */
13118 +
13119 +       err = -ESTALE;
13120 +       bindex = -1;
13121 +       if (!nsi_lock->force_lock)
13122 +               si_read_unlock(sb);
13123 +
13124 +out:
13125 +       nsi_lock->bindex = bindex;
13126 +       return err;
13127 +}
13128 +
13129 +struct find_name_by_ino {
13130 +       struct dir_context ctx;
13131 +       int called, found;
13132 +       ino_t ino;
13133 +       char *name;
13134 +       int namelen;
13135 +};
13136 +
13137 +static int
13138 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13139 +                loff_t offset, u64 ino, unsigned int d_type)
13140 +{
13141 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13142 +                                                 ctx);
13143 +
13144 +       a->called++;
13145 +       if (a->ino != ino)
13146 +               return 0;
13147 +
13148 +       memcpy(a->name, name, namelen);
13149 +       a->namelen = namelen;
13150 +       a->found = 1;
13151 +       return 1;
13152 +}
13153 +
13154 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13155 +                                    struct au_nfsd_si_lock *nsi_lock)
13156 +{
13157 +       struct dentry *dentry, *parent;
13158 +       struct file *file;
13159 +       struct inode *dir;
13160 +       struct find_name_by_ino arg = {
13161 +               .ctx = {
13162 +                       .actor = find_name_by_ino
13163 +               }
13164 +       };
13165 +       int err;
13166 +
13167 +       parent = path->dentry;
13168 +       if (nsi_lock)
13169 +               si_read_unlock(parent->d_sb);
13170 +       file = vfsub_dentry_open(path, au_dir_roflags);
13171 +       dentry = (void *)file;
13172 +       if (IS_ERR(file))
13173 +               goto out;
13174 +
13175 +       dentry = ERR_PTR(-ENOMEM);
13176 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13177 +       if (unlikely(!arg.name))
13178 +               goto out_file;
13179 +       arg.ino = ino;
13180 +       arg.found = 0;
13181 +       do {
13182 +               arg.called = 0;
13183 +               /* smp_mb(); */
13184 +               err = vfsub_iterate_dir(file, &arg.ctx);
13185 +       } while (!err && !arg.found && arg.called);
13186 +       dentry = ERR_PTR(err);
13187 +       if (unlikely(err))
13188 +               goto out_name;
13189 +       /* instead of ENOENT */
13190 +       dentry = ERR_PTR(-ESTALE);
13191 +       if (!arg.found)
13192 +               goto out_name;
13193 +
13194 +       /* do not call vfsub_lkup_one() */
13195 +       dir = d_inode(parent);
13196 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
13197 +       AuTraceErrPtr(dentry);
13198 +       if (IS_ERR(dentry))
13199 +               goto out_name;
13200 +       AuDebugOn(au_test_anon(dentry));
13201 +       if (unlikely(d_really_is_negative(dentry))) {
13202 +               dput(dentry);
13203 +               dentry = ERR_PTR(-ENOENT);
13204 +       }
13205 +
13206 +out_name:
13207 +       free_page((unsigned long)arg.name);
13208 +out_file:
13209 +       fput(file);
13210 +out:
13211 +       if (unlikely(nsi_lock
13212 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13213 +               if (!IS_ERR(dentry)) {
13214 +                       dput(dentry);
13215 +                       dentry = ERR_PTR(-ESTALE);
13216 +               }
13217 +       AuTraceErrPtr(dentry);
13218 +       return dentry;
13219 +}
13220 +
13221 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13222 +                                       ino_t dir_ino,
13223 +                                       struct au_nfsd_si_lock *nsi_lock)
13224 +{
13225 +       struct dentry *dentry;
13226 +       struct path path;
13227 +
13228 +       if (dir_ino != AUFS_ROOT_INO) {
13229 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13230 +               dentry = path.dentry;
13231 +               if (!path.dentry || IS_ERR(path.dentry))
13232 +                       goto out;
13233 +               AuDebugOn(au_test_anon(path.dentry));
13234 +       } else
13235 +               path.dentry = dget(sb->s_root);
13236 +
13237 +       path.mnt = au_mnt_get(sb);
13238 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13239 +       path_put(&path);
13240 +
13241 +out:
13242 +       AuTraceErrPtr(dentry);
13243 +       return dentry;
13244 +}
13245 +
13246 +/* ---------------------------------------------------------------------- */
13247 +
13248 +static int h_acceptable(void *expv, struct dentry *dentry)
13249 +{
13250 +       return 1;
13251 +}
13252 +
13253 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13254 +                          char *buf, int len, struct super_block *sb)
13255 +{
13256 +       char *p;
13257 +       int n;
13258 +       struct path path;
13259 +
13260 +       p = d_path(h_rootpath, buf, len);
13261 +       if (IS_ERR(p))
13262 +               goto out;
13263 +       n = strlen(p);
13264 +
13265 +       path.mnt = h_rootpath->mnt;
13266 +       path.dentry = h_parent;
13267 +       p = d_path(&path, buf, len);
13268 +       if (IS_ERR(p))
13269 +               goto out;
13270 +       if (n != 1)
13271 +               p += n;
13272 +
13273 +       path.mnt = au_mnt_get(sb);
13274 +       path.dentry = sb->s_root;
13275 +       p = d_path(&path, buf, len - strlen(p));
13276 +       mntput(path.mnt);
13277 +       if (IS_ERR(p))
13278 +               goto out;
13279 +       if (n != 1)
13280 +               p[strlen(p)] = '/';
13281 +
13282 +out:
13283 +       AuTraceErrPtr(p);
13284 +       return p;
13285 +}
13286 +
13287 +static
13288 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13289 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13290 +{
13291 +       struct dentry *dentry, *h_parent, *root;
13292 +       struct super_block *h_sb;
13293 +       char *pathname, *p;
13294 +       struct vfsmount *h_mnt;
13295 +       struct au_branch *br;
13296 +       int err;
13297 +       struct path path;
13298 +
13299 +       br = au_sbr(sb, nsi_lock->bindex);
13300 +       h_mnt = au_br_mnt(br);
13301 +       h_sb = h_mnt->mnt_sb;
13302 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13303 +       lockdep_off();
13304 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13305 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13306 +                                     h_acceptable, /*context*/NULL);
13307 +       lockdep_on();
13308 +       dentry = h_parent;
13309 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13310 +               AuWarn1("%s decode_fh failed, %ld\n",
13311 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13312 +               goto out;
13313 +       }
13314 +       dentry = NULL;
13315 +       if (unlikely(au_test_anon(h_parent))) {
13316 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13317 +                       au_sbtype(h_sb));
13318 +               goto out_h_parent;
13319 +       }
13320 +
13321 +       dentry = ERR_PTR(-ENOMEM);
13322 +       pathname = (void *)__get_free_page(GFP_NOFS);
13323 +       if (unlikely(!pathname))
13324 +               goto out_h_parent;
13325 +
13326 +       root = sb->s_root;
13327 +       path.mnt = h_mnt;
13328 +       di_read_lock_parent(root, !AuLock_IR);
13329 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13330 +       di_read_unlock(root, !AuLock_IR);
13331 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13332 +       dentry = (void *)p;
13333 +       if (IS_ERR(p))
13334 +               goto out_pathname;
13335 +
13336 +       si_read_unlock(sb);
13337 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13338 +       dentry = ERR_PTR(err);
13339 +       if (unlikely(err))
13340 +               goto out_relock;
13341 +
13342 +       dentry = ERR_PTR(-ENOENT);
13343 +       AuDebugOn(au_test_anon(path.dentry));
13344 +       if (unlikely(d_really_is_negative(path.dentry)))
13345 +               goto out_path;
13346 +
13347 +       if (ino != d_inode(path.dentry)->i_ino)
13348 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13349 +       else
13350 +               dentry = dget(path.dentry);
13351 +
13352 +out_path:
13353 +       path_put(&path);
13354 +out_relock:
13355 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13356 +               if (!IS_ERR(dentry)) {
13357 +                       dput(dentry);
13358 +                       dentry = ERR_PTR(-ESTALE);
13359 +               }
13360 +out_pathname:
13361 +       free_page((unsigned long)pathname);
13362 +out_h_parent:
13363 +       dput(h_parent);
13364 +out:
13365 +       AuTraceErrPtr(dentry);
13366 +       return dentry;
13367 +}
13368 +
13369 +/* ---------------------------------------------------------------------- */
13370 +
13371 +static struct dentry *
13372 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13373 +                 int fh_type)
13374 +{
13375 +       struct dentry *dentry;
13376 +       __u32 *fh = fid->raw;
13377 +       struct au_branch *br;
13378 +       ino_t ino, dir_ino;
13379 +       struct au_nfsd_si_lock nsi_lock = {
13380 +               .force_lock     = 0
13381 +       };
13382 +
13383 +       dentry = ERR_PTR(-ESTALE);
13384 +       /* it should never happen, but the file handle is unreliable */
13385 +       if (unlikely(fh_len < Fh_tail))
13386 +               goto out;
13387 +       nsi_lock.sigen = fh[Fh_sigen];
13388 +       nsi_lock.br_id = fh[Fh_br_id];
13389 +
13390 +       /* branch id may be wrapped around */
13391 +       br = NULL;
13392 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13393 +               goto out;
13394 +       nsi_lock.force_lock = 1;
13395 +
13396 +       /* is this inode still cached? */
13397 +       ino = decode_ino(fh + Fh_ino);
13398 +       /* it should never happen */
13399 +       if (unlikely(ino == AUFS_ROOT_INO))
13400 +               goto out_unlock;
13401 +
13402 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13403 +       dentry = decode_by_ino(sb, ino, dir_ino);
13404 +       if (IS_ERR(dentry))
13405 +               goto out_unlock;
13406 +       if (dentry)
13407 +               goto accept;
13408 +
13409 +       /* is the parent dir cached? */
13410 +       br = au_sbr(sb, nsi_lock.bindex);
13411 +       au_lcnt_inc(&br->br_nfiles);
13412 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13413 +       if (IS_ERR(dentry))
13414 +               goto out_unlock;
13415 +       if (dentry)
13416 +               goto accept;
13417 +
13418 +       /* lookup path */
13419 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13420 +       if (IS_ERR(dentry))
13421 +               goto out_unlock;
13422 +       if (unlikely(!dentry))
13423 +               /* todo?: make it ESTALE */
13424 +               goto out_unlock;
13425 +
13426 +accept:
13427 +       if (!au_digen_test(dentry, au_sigen(sb))
13428 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13429 +               goto out_unlock; /* success */
13430 +
13431 +       dput(dentry);
13432 +       dentry = ERR_PTR(-ESTALE);
13433 +out_unlock:
13434 +       if (br)
13435 +               au_lcnt_dec(&br->br_nfiles);
13436 +       si_read_unlock(sb);
13437 +out:
13438 +       AuTraceErrPtr(dentry);
13439 +       return dentry;
13440 +}
13441 +
13442 +#if 0 /* reserved for future use */
13443 +/* support subtreecheck option */
13444 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13445 +                                       int fh_len, int fh_type)
13446 +{
13447 +       struct dentry *parent;
13448 +       __u32 *fh = fid->raw;
13449 +       ino_t dir_ino;
13450 +
13451 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13452 +       parent = decode_by_ino(sb, dir_ino, 0);
13453 +       if (IS_ERR(parent))
13454 +               goto out;
13455 +       if (!parent)
13456 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13457 +                                       dir_ino, fh, fh_len);
13458 +
13459 +out:
13460 +       AuTraceErrPtr(parent);
13461 +       return parent;
13462 +}
13463 +#endif
13464 +
13465 +/* ---------------------------------------------------------------------- */
13466 +
13467 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13468 +                         struct inode *dir)
13469 +{
13470 +       int err;
13471 +       aufs_bindex_t bindex;
13472 +       struct super_block *sb, *h_sb;
13473 +       struct dentry *dentry, *parent, *h_parent;
13474 +       struct inode *h_dir;
13475 +       struct au_branch *br;
13476 +
13477 +       err = -ENOSPC;
13478 +       if (unlikely(*max_len <= Fh_tail)) {
13479 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13480 +               goto out;
13481 +       }
13482 +
13483 +       err = FILEID_ROOT;
13484 +       if (inode->i_ino == AUFS_ROOT_INO) {
13485 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13486 +               goto out;
13487 +       }
13488 +
13489 +       h_parent = NULL;
13490 +       sb = inode->i_sb;
13491 +       err = si_read_lock(sb, AuLock_FLUSH);
13492 +       if (unlikely(err))
13493 +               goto out;
13494 +
13495 +#ifdef CONFIG_AUFS_DEBUG
13496 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13497 +               AuWarn1("NFS-exporting requires xino\n");
13498 +#endif
13499 +       err = -EIO;
13500 +       parent = NULL;
13501 +       ii_read_lock_child(inode);
13502 +       bindex = au_ibtop(inode);
13503 +       if (!dir) {
13504 +               dentry = d_find_any_alias(inode);
13505 +               if (unlikely(!dentry))
13506 +                       goto out_unlock;
13507 +               AuDebugOn(au_test_anon(dentry));
13508 +               parent = dget_parent(dentry);
13509 +               dput(dentry);
13510 +               if (unlikely(!parent))
13511 +                       goto out_unlock;
13512 +               if (d_really_is_positive(parent))
13513 +                       dir = d_inode(parent);
13514 +       }
13515 +
13516 +       ii_read_lock_parent(dir);
13517 +       h_dir = au_h_iptr(dir, bindex);
13518 +       ii_read_unlock(dir);
13519 +       if (unlikely(!h_dir))
13520 +               goto out_parent;
13521 +       h_parent = d_find_any_alias(h_dir);
13522 +       if (unlikely(!h_parent))
13523 +               goto out_hparent;
13524 +
13525 +       err = -EPERM;
13526 +       br = au_sbr(sb, bindex);
13527 +       h_sb = au_br_sb(br);
13528 +       if (unlikely(!h_sb->s_export_op)) {
13529 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13530 +               goto out_hparent;
13531 +       }
13532 +
13533 +       fh[Fh_br_id] = br->br_id;
13534 +       fh[Fh_sigen] = au_sigen(sb);
13535 +       encode_ino(fh + Fh_ino, inode->i_ino);
13536 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13537 +       fh[Fh_igen] = inode->i_generation;
13538 +
13539 +       *max_len -= Fh_tail;
13540 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13541 +                                          max_len,
13542 +                                          /*connectable or subtreecheck*/0);
13543 +       err = fh[Fh_h_type];
13544 +       *max_len += Fh_tail;
13545 +       /* todo: macros? */
13546 +       if (err != FILEID_INVALID)
13547 +               err = 99;
13548 +       else
13549 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13550 +
13551 +out_hparent:
13552 +       dput(h_parent);
13553 +out_parent:
13554 +       dput(parent);
13555 +out_unlock:
13556 +       ii_read_unlock(inode);
13557 +       si_read_unlock(sb);
13558 +out:
13559 +       if (unlikely(err < 0))
13560 +               err = FILEID_INVALID;
13561 +       return err;
13562 +}
13563 +
13564 +/* ---------------------------------------------------------------------- */
13565 +
13566 +static int aufs_commit_metadata(struct inode *inode)
13567 +{
13568 +       int err;
13569 +       aufs_bindex_t bindex;
13570 +       struct super_block *sb;
13571 +       struct inode *h_inode;
13572 +       int (*f)(struct inode *inode);
13573 +
13574 +       sb = inode->i_sb;
13575 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13576 +       ii_write_lock_child(inode);
13577 +       bindex = au_ibtop(inode);
13578 +       AuDebugOn(bindex < 0);
13579 +       h_inode = au_h_iptr(inode, bindex);
13580 +
13581 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13582 +       if (f)
13583 +               err = f(h_inode);
13584 +       else {
13585 +               struct writeback_control wbc = {
13586 +                       .sync_mode      = WB_SYNC_ALL,
13587 +                       .nr_to_write    = 0 /* metadata only */
13588 +               };
13589 +
13590 +               err = sync_inode(h_inode, &wbc);
13591 +       }
13592 +
13593 +       au_cpup_attr_timesizes(inode);
13594 +       ii_write_unlock(inode);
13595 +       si_read_unlock(sb);
13596 +       return err;
13597 +}
13598 +
13599 +/* ---------------------------------------------------------------------- */
13600 +
13601 +static struct export_operations aufs_export_op = {
13602 +       .fh_to_dentry           = aufs_fh_to_dentry,
13603 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13604 +       .encode_fh              = aufs_encode_fh,
13605 +       .commit_metadata        = aufs_commit_metadata
13606 +};
13607 +
13608 +void au_export_init(struct super_block *sb)
13609 +{
13610 +       struct au_sbinfo *sbinfo;
13611 +       __u32 u;
13612 +
13613 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13614 +                        && IS_MODULE(CONFIG_EXPORTFS),
13615 +                        AUFS_NAME ": unsupported configuration "
13616 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13617 +
13618 +       sb->s_export_op = &aufs_export_op;
13619 +       sbinfo = au_sbi(sb);
13620 +       sbinfo->si_xigen = NULL;
13621 +       get_random_bytes(&u, sizeof(u));
13622 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13623 +       atomic_set(&sbinfo->si_xigen_next, u);
13624 +}
13625 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13626 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13627 +++ linux/fs/aufs/fhsm.c        2020-01-27 10:57:18.172204883 +0100
13628 @@ -0,0 +1,427 @@
13629 +// SPDX-License-Identifier: GPL-2.0
13630 +/*
13631 + * Copyright (C) 2011-2020 Junjiro R. Okajima
13632 + *
13633 + * This program, aufs is free software; you can redistribute it and/or modify
13634 + * it under the terms of the GNU General Public License as published by
13635 + * the Free Software Foundation; either version 2 of the License, or
13636 + * (at your option) any later version.
13637 + *
13638 + * This program is distributed in the hope that it will be useful,
13639 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13640 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13641 + * GNU General Public License for more details.
13642 + *
13643 + * You should have received a copy of the GNU General Public License
13644 + * along with this program; if not, write to the Free Software
13645 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13646 + */
13647 +
13648 +/*
13649 + * File-based Hierarchy Storage Management
13650 + */
13651 +
13652 +#include <linux/anon_inodes.h>
13653 +#include <linux/poll.h>
13654 +#include <linux/seq_file.h>
13655 +#include <linux/statfs.h>
13656 +#include "aufs.h"
13657 +
13658 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13659 +{
13660 +       struct au_sbinfo *sbinfo;
13661 +       struct au_fhsm *fhsm;
13662 +
13663 +       SiMustAnyLock(sb);
13664 +
13665 +       sbinfo = au_sbi(sb);
13666 +       fhsm = &sbinfo->si_fhsm;
13667 +       AuDebugOn(!fhsm);
13668 +       return fhsm->fhsm_bottom;
13669 +}
13670 +
13671 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13672 +{
13673 +       struct au_sbinfo *sbinfo;
13674 +       struct au_fhsm *fhsm;
13675 +
13676 +       SiMustWriteLock(sb);
13677 +
13678 +       sbinfo = au_sbi(sb);
13679 +       fhsm = &sbinfo->si_fhsm;
13680 +       AuDebugOn(!fhsm);
13681 +       fhsm->fhsm_bottom = bindex;
13682 +}
13683 +
13684 +/* ---------------------------------------------------------------------- */
13685 +
13686 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13687 +{
13688 +       struct au_br_fhsm *bf;
13689 +
13690 +       bf = br->br_fhsm;
13691 +       MtxMustLock(&bf->bf_lock);
13692 +
13693 +       return !bf->bf_readable
13694 +               || time_after(jiffies,
13695 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13696 +}
13697 +
13698 +/* ---------------------------------------------------------------------- */
13699 +
13700 +static void au_fhsm_notify(struct super_block *sb, int val)
13701 +{
13702 +       struct au_sbinfo *sbinfo;
13703 +       struct au_fhsm *fhsm;
13704 +
13705 +       SiMustAnyLock(sb);
13706 +
13707 +       sbinfo = au_sbi(sb);
13708 +       fhsm = &sbinfo->si_fhsm;
13709 +       if (au_fhsm_pid(fhsm)
13710 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13711 +               atomic_set(&fhsm->fhsm_readable, val);
13712 +               if (val)
13713 +                       wake_up(&fhsm->fhsm_wqh);
13714 +       }
13715 +}
13716 +
13717 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13718 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13719 +{
13720 +       int err;
13721 +       struct au_branch *br;
13722 +       struct au_br_fhsm *bf;
13723 +
13724 +       br = au_sbr(sb, bindex);
13725 +       AuDebugOn(au_br_rdonly(br));
13726 +       bf = br->br_fhsm;
13727 +       AuDebugOn(!bf);
13728 +
13729 +       if (do_lock)
13730 +               mutex_lock(&bf->bf_lock);
13731 +       else
13732 +               MtxMustLock(&bf->bf_lock);
13733 +
13734 +       /* sb->s_root for NFS is unreliable */
13735 +       err = au_br_stfs(br, &bf->bf_stfs);
13736 +       if (unlikely(err)) {
13737 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13738 +               goto out;
13739 +       }
13740 +
13741 +       bf->bf_jiffy = jiffies;
13742 +       bf->bf_readable = 1;
13743 +       if (do_notify)
13744 +               au_fhsm_notify(sb, /*val*/1);
13745 +       if (rstfs)
13746 +               *rstfs = bf->bf_stfs;
13747 +
13748 +out:
13749 +       if (do_lock)
13750 +               mutex_unlock(&bf->bf_lock);
13751 +       au_fhsm_notify(sb, /*val*/1);
13752 +
13753 +       return err;
13754 +}
13755 +
13756 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13757 +{
13758 +       int err;
13759 +       struct au_sbinfo *sbinfo;
13760 +       struct au_fhsm *fhsm;
13761 +       struct au_branch *br;
13762 +       struct au_br_fhsm *bf;
13763 +
13764 +       AuDbg("b%d, force %d\n", bindex, force);
13765 +       SiMustAnyLock(sb);
13766 +
13767 +       sbinfo = au_sbi(sb);
13768 +       fhsm = &sbinfo->si_fhsm;
13769 +       if (!au_ftest_si(sbinfo, FHSM)
13770 +           || fhsm->fhsm_bottom == bindex)
13771 +               return;
13772 +
13773 +       br = au_sbr(sb, bindex);
13774 +       bf = br->br_fhsm;
13775 +       AuDebugOn(!bf);
13776 +       mutex_lock(&bf->bf_lock);
13777 +       if (force
13778 +           || au_fhsm_pid(fhsm)
13779 +           || au_fhsm_test_jiffy(sbinfo, br))
13780 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13781 +                                 /*do_notify*/1);
13782 +       mutex_unlock(&bf->bf_lock);
13783 +}
13784 +
13785 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13786 +{
13787 +       aufs_bindex_t bindex, bbot;
13788 +       struct au_branch *br;
13789 +
13790 +       /* exclude the bottom */
13791 +       bbot = au_fhsm_bottom(sb);
13792 +       for (bindex = 0; bindex < bbot; bindex++) {
13793 +               br = au_sbr(sb, bindex);
13794 +               if (au_br_fhsm(br->br_perm))
13795 +                       au_fhsm_wrote(sb, bindex, force);
13796 +       }
13797 +}
13798 +
13799 +/* ---------------------------------------------------------------------- */
13800 +
13801 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13802 +{
13803 +       __poll_t mask;
13804 +       struct au_sbinfo *sbinfo;
13805 +       struct au_fhsm *fhsm;
13806 +
13807 +       mask = 0;
13808 +       sbinfo = file->private_data;
13809 +       fhsm = &sbinfo->si_fhsm;
13810 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13811 +       if (atomic_read(&fhsm->fhsm_readable))
13812 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13813 +
13814 +       if (!mask)
13815 +               AuDbg("mask 0x%x\n", mask);
13816 +       return mask;
13817 +}
13818 +
13819 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13820 +                             struct aufs_stfs *stfs, __s16 brid)
13821 +{
13822 +       int err;
13823 +
13824 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13825 +       if (!err)
13826 +               err = __put_user(brid, &stbr->brid);
13827 +       if (unlikely(err))
13828 +               err = -EFAULT;
13829 +
13830 +       return err;
13831 +}
13832 +
13833 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13834 +                              struct aufs_stbr __user *stbr, size_t count)
13835 +{
13836 +       ssize_t err;
13837 +       int nstbr;
13838 +       aufs_bindex_t bindex, bbot;
13839 +       struct au_branch *br;
13840 +       struct au_br_fhsm *bf;
13841 +
13842 +       /* except the bottom branch */
13843 +       err = 0;
13844 +       nstbr = 0;
13845 +       bbot = au_fhsm_bottom(sb);
13846 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13847 +               br = au_sbr(sb, bindex);
13848 +               if (!au_br_fhsm(br->br_perm))
13849 +                       continue;
13850 +
13851 +               bf = br->br_fhsm;
13852 +               mutex_lock(&bf->bf_lock);
13853 +               if (bf->bf_readable) {
13854 +                       err = -EFAULT;
13855 +                       if (count >= sizeof(*stbr))
13856 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13857 +                                                         br->br_id);
13858 +                       if (!err) {
13859 +                               bf->bf_readable = 0;
13860 +                               count -= sizeof(*stbr);
13861 +                               nstbr++;
13862 +                       }
13863 +               }
13864 +               mutex_unlock(&bf->bf_lock);
13865 +       }
13866 +       if (!err)
13867 +               err = sizeof(*stbr) * nstbr;
13868 +
13869 +       return err;
13870 +}
13871 +
13872 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13873 +                          loff_t *pos)
13874 +{
13875 +       ssize_t err;
13876 +       int readable;
13877 +       aufs_bindex_t nfhsm, bindex, bbot;
13878 +       struct au_sbinfo *sbinfo;
13879 +       struct au_fhsm *fhsm;
13880 +       struct au_branch *br;
13881 +       struct super_block *sb;
13882 +
13883 +       err = 0;
13884 +       sbinfo = file->private_data;
13885 +       fhsm = &sbinfo->si_fhsm;
13886 +need_data:
13887 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13888 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13889 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13890 +                       err = -EAGAIN;
13891 +               else
13892 +                       err = wait_event_interruptible_locked_irq
13893 +                               (fhsm->fhsm_wqh,
13894 +                                atomic_read(&fhsm->fhsm_readable));
13895 +       }
13896 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13897 +       if (unlikely(err))
13898 +               goto out;
13899 +
13900 +       /* sb may already be dead */
13901 +       au_rw_read_lock(&sbinfo->si_rwsem);
13902 +       readable = atomic_read(&fhsm->fhsm_readable);
13903 +       if (readable > 0) {
13904 +               sb = sbinfo->si_sb;
13905 +               AuDebugOn(!sb);
13906 +               /* exclude the bottom branch */
13907 +               nfhsm = 0;
13908 +               bbot = au_fhsm_bottom(sb);
13909 +               for (bindex = 0; bindex < bbot; bindex++) {
13910 +                       br = au_sbr(sb, bindex);
13911 +                       if (au_br_fhsm(br->br_perm))
13912 +                               nfhsm++;
13913 +               }
13914 +               err = -EMSGSIZE;
13915 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13916 +                       atomic_set(&fhsm->fhsm_readable, 0);
13917 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13918 +                                            count);
13919 +               }
13920 +       }
13921 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13922 +       if (!readable)
13923 +               goto need_data;
13924 +
13925 +out:
13926 +       return err;
13927 +}
13928 +
13929 +static int au_fhsm_release(struct inode *inode, struct file *file)
13930 +{
13931 +       struct au_sbinfo *sbinfo;
13932 +       struct au_fhsm *fhsm;
13933 +
13934 +       /* sb may already be dead */
13935 +       sbinfo = file->private_data;
13936 +       fhsm = &sbinfo->si_fhsm;
13937 +       spin_lock(&fhsm->fhsm_spin);
13938 +       fhsm->fhsm_pid = 0;
13939 +       spin_unlock(&fhsm->fhsm_spin);
13940 +       kobject_put(&sbinfo->si_kobj);
13941 +
13942 +       return 0;
13943 +}
13944 +
13945 +static const struct file_operations au_fhsm_fops = {
13946 +       .owner          = THIS_MODULE,
13947 +       .llseek         = noop_llseek,
13948 +       .read           = au_fhsm_read,
13949 +       .poll           = au_fhsm_poll,
13950 +       .release        = au_fhsm_release
13951 +};
13952 +
13953 +int au_fhsm_fd(struct super_block *sb, int oflags)
13954 +{
13955 +       int err, fd;
13956 +       struct au_sbinfo *sbinfo;
13957 +       struct au_fhsm *fhsm;
13958 +
13959 +       err = -EPERM;
13960 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13961 +               goto out;
13962 +
13963 +       err = -EINVAL;
13964 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13965 +               goto out;
13966 +
13967 +       err = 0;
13968 +       sbinfo = au_sbi(sb);
13969 +       fhsm = &sbinfo->si_fhsm;
13970 +       spin_lock(&fhsm->fhsm_spin);
13971 +       if (!fhsm->fhsm_pid)
13972 +               fhsm->fhsm_pid = current->pid;
13973 +       else
13974 +               err = -EBUSY;
13975 +       spin_unlock(&fhsm->fhsm_spin);
13976 +       if (unlikely(err))
13977 +               goto out;
13978 +
13979 +       oflags |= O_RDONLY;
13980 +       /* oflags |= FMODE_NONOTIFY; */
13981 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13982 +       err = fd;
13983 +       if (unlikely(fd < 0))
13984 +               goto out_pid;
13985 +
13986 +       /* succeed regardless 'fhsm' status */
13987 +       kobject_get(&sbinfo->si_kobj);
13988 +       si_noflush_read_lock(sb);
13989 +       if (au_ftest_si(sbinfo, FHSM))
13990 +               au_fhsm_wrote_all(sb, /*force*/0);
13991 +       si_read_unlock(sb);
13992 +       goto out; /* success */
13993 +
13994 +out_pid:
13995 +       spin_lock(&fhsm->fhsm_spin);
13996 +       fhsm->fhsm_pid = 0;
13997 +       spin_unlock(&fhsm->fhsm_spin);
13998 +out:
13999 +       AuTraceErr(err);
14000 +       return err;
14001 +}
14002 +
14003 +/* ---------------------------------------------------------------------- */
14004 +
14005 +int au_fhsm_br_alloc(struct au_branch *br)
14006 +{
14007 +       int err;
14008 +
14009 +       err = 0;
14010 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14011 +       if (br->br_fhsm)
14012 +               au_br_fhsm_init(br->br_fhsm);
14013 +       else
14014 +               err = -ENOMEM;
14015 +
14016 +       return err;
14017 +}
14018 +
14019 +/* ---------------------------------------------------------------------- */
14020 +
14021 +void au_fhsm_fin(struct super_block *sb)
14022 +{
14023 +       au_fhsm_notify(sb, /*val*/-1);
14024 +}
14025 +
14026 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14027 +{
14028 +       struct au_fhsm *fhsm;
14029 +
14030 +       fhsm = &sbinfo->si_fhsm;
14031 +       spin_lock_init(&fhsm->fhsm_spin);
14032 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14033 +       atomic_set(&fhsm->fhsm_readable, 0);
14034 +       fhsm->fhsm_expire
14035 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14036 +       fhsm->fhsm_bottom = -1;
14037 +}
14038 +
14039 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14040 +{
14041 +       sbinfo->si_fhsm.fhsm_expire
14042 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14043 +}
14044 +
14045 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14046 +{
14047 +       unsigned int u;
14048 +
14049 +       if (!au_ftest_si(sbinfo, FHSM))
14050 +               return;
14051 +
14052 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14053 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14054 +               seq_printf(seq, ",fhsm_sec=%u", u);
14055 +}
14056 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14057 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14058 +++ linux/fs/aufs/file.c        2020-12-15 14:10:58.918023162 +0100
14059 @@ -0,0 +1,867 @@
14060 +// SPDX-License-Identifier: GPL-2.0
14061 +/*
14062 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14063 + *
14064 + * This program, aufs is free software; you can redistribute it and/or modify
14065 + * it under the terms of the GNU General Public License as published by
14066 + * the Free Software Foundation; either version 2 of the License, or
14067 + * (at your option) any later version.
14068 + *
14069 + * This program is distributed in the hope that it will be useful,
14070 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14071 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14072 + * GNU General Public License for more details.
14073 + *
14074 + * You should have received a copy of the GNU General Public License
14075 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14076 + */
14077 +
14078 +/*
14079 + * handling file/dir, and address_space operation
14080 + */
14081 +
14082 +#ifdef CONFIG_AUFS_DEBUG
14083 +#include <linux/migrate.h>
14084 +#endif
14085 +#include <linux/pagemap.h>
14086 +#include "aufs.h"
14087 +
14088 +/* drop flags for writing */
14089 +unsigned int au_file_roflags(unsigned int flags)
14090 +{
14091 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14092 +       flags |= O_RDONLY | O_NOATIME;
14093 +       return flags;
14094 +}
14095 +
14096 +/* common functions to regular file and dir */
14097 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14098 +                      struct file *file, int force_wr)
14099 +{
14100 +       struct file *h_file;
14101 +       struct dentry *h_dentry;
14102 +       struct inode *h_inode;
14103 +       struct super_block *sb;
14104 +       struct au_branch *br;
14105 +       struct path h_path;
14106 +       int err;
14107 +
14108 +       /* a race condition can happen between open and unlink/rmdir */
14109 +       h_file = ERR_PTR(-ENOENT);
14110 +       h_dentry = au_h_dptr(dentry, bindex);
14111 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14112 +               goto out;
14113 +       h_inode = d_inode(h_dentry);
14114 +       spin_lock(&h_dentry->d_lock);
14115 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14116 +               /* || !d_inode(dentry)->i_nlink */
14117 +               ;
14118 +       spin_unlock(&h_dentry->d_lock);
14119 +       if (unlikely(err))
14120 +               goto out;
14121 +
14122 +       sb = dentry->d_sb;
14123 +       br = au_sbr(sb, bindex);
14124 +       err = au_br_test_oflag(flags, br);
14125 +       h_file = ERR_PTR(err);
14126 +       if (unlikely(err))
14127 +               goto out;
14128 +
14129 +       /* drop flags for writing */
14130 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14131 +               if (force_wr && !(flags & O_WRONLY))
14132 +                       force_wr = 0;
14133 +               flags = au_file_roflags(flags);
14134 +               if (force_wr) {
14135 +                       h_file = ERR_PTR(-EROFS);
14136 +                       flags = au_file_roflags(flags);
14137 +                       if (unlikely(vfsub_native_ro(h_inode)
14138 +                                    || IS_APPEND(h_inode)))
14139 +                               goto out;
14140 +                       flags &= ~O_ACCMODE;
14141 +                       flags |= O_WRONLY;
14142 +               }
14143 +       }
14144 +       flags &= ~O_CREAT;
14145 +       au_lcnt_inc(&br->br_nfiles);
14146 +       h_path.dentry = h_dentry;
14147 +       h_path.mnt = au_br_mnt(br);
14148 +       h_file = vfsub_dentry_open(&h_path, flags);
14149 +       if (IS_ERR(h_file))
14150 +               goto out_br;
14151 +
14152 +       if (flags & __FMODE_EXEC) {
14153 +               err = deny_write_access(h_file);
14154 +               if (unlikely(err)) {
14155 +                       fput(h_file);
14156 +                       h_file = ERR_PTR(err);
14157 +                       goto out_br;
14158 +               }
14159 +       }
14160 +       fsnotify_open(h_file);
14161 +       goto out; /* success */
14162 +
14163 +out_br:
14164 +       au_lcnt_dec(&br->br_nfiles);
14165 +out:
14166 +       return h_file;
14167 +}
14168 +
14169 +static int au_cmoo(struct dentry *dentry)
14170 +{
14171 +       int err, cmoo, matched;
14172 +       unsigned int udba;
14173 +       struct path h_path;
14174 +       struct au_pin pin;
14175 +       struct au_cp_generic cpg = {
14176 +               .dentry = dentry,
14177 +               .bdst   = -1,
14178 +               .bsrc   = -1,
14179 +               .len    = -1,
14180 +               .pin    = &pin,
14181 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14182 +       };
14183 +       struct inode *delegated;
14184 +       struct super_block *sb;
14185 +       struct au_sbinfo *sbinfo;
14186 +       struct au_fhsm *fhsm;
14187 +       pid_t pid;
14188 +       struct au_branch *br;
14189 +       struct dentry *parent;
14190 +       struct au_hinode *hdir;
14191 +
14192 +       DiMustWriteLock(dentry);
14193 +       IiMustWriteLock(d_inode(dentry));
14194 +
14195 +       err = 0;
14196 +       if (IS_ROOT(dentry))
14197 +               goto out;
14198 +       cpg.bsrc = au_dbtop(dentry);
14199 +       if (!cpg.bsrc)
14200 +               goto out;
14201 +
14202 +       sb = dentry->d_sb;
14203 +       sbinfo = au_sbi(sb);
14204 +       fhsm = &sbinfo->si_fhsm;
14205 +       pid = au_fhsm_pid(fhsm);
14206 +       rcu_read_lock();
14207 +       matched = (pid
14208 +                  && (current->pid == pid
14209 +                      || rcu_dereference(current->real_parent)->pid == pid));
14210 +       rcu_read_unlock();
14211 +       if (matched)
14212 +               goto out;
14213 +
14214 +       br = au_sbr(sb, cpg.bsrc);
14215 +       cmoo = au_br_cmoo(br->br_perm);
14216 +       if (!cmoo)
14217 +               goto out;
14218 +       if (!d_is_reg(dentry))
14219 +               cmoo &= AuBrAttr_COO_ALL;
14220 +       if (!cmoo)
14221 +               goto out;
14222 +
14223 +       parent = dget_parent(dentry);
14224 +       di_write_lock_parent(parent);
14225 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14226 +       cpg.bdst = err;
14227 +       if (unlikely(err < 0)) {
14228 +               err = 0;        /* there is no upper writable branch */
14229 +               goto out_dgrade;
14230 +       }
14231 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14232 +
14233 +       /* do not respect the coo attrib for the target branch */
14234 +       err = au_cpup_dirs(dentry, cpg.bdst);
14235 +       if (unlikely(err))
14236 +               goto out_dgrade;
14237 +
14238 +       di_downgrade_lock(parent, AuLock_IR);
14239 +       udba = au_opt_udba(sb);
14240 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14241 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14242 +       if (unlikely(err))
14243 +               goto out_parent;
14244 +
14245 +       err = au_sio_cpup_simple(&cpg);
14246 +       au_unpin(&pin);
14247 +       if (unlikely(err))
14248 +               goto out_parent;
14249 +       if (!(cmoo & AuBrWAttr_MOO))
14250 +               goto out_parent; /* success */
14251 +
14252 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14253 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14254 +       if (unlikely(err))
14255 +               goto out_parent;
14256 +
14257 +       h_path.mnt = au_br_mnt(br);
14258 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14259 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14260 +       delegated = NULL;
14261 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14262 +       au_unpin(&pin);
14263 +       /* todo: keep h_dentry or not? */
14264 +       if (unlikely(err == -EWOULDBLOCK)) {
14265 +               pr_warn("cannot retry for NFSv4 delegation"
14266 +                       " for an internal unlink\n");
14267 +               iput(delegated);
14268 +       }
14269 +       if (unlikely(err)) {
14270 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14271 +                      dentry, err);
14272 +               err = 0;
14273 +       }
14274 +       goto out_parent; /* success */
14275 +
14276 +out_dgrade:
14277 +       di_downgrade_lock(parent, AuLock_IR);
14278 +out_parent:
14279 +       di_read_unlock(parent, AuLock_IR);
14280 +       dput(parent);
14281 +out:
14282 +       AuTraceErr(err);
14283 +       return err;
14284 +}
14285 +
14286 +int au_do_open(struct file *file, struct au_do_open_args *args)
14287 +{
14288 +       int err, aopen = args->aopen;
14289 +       struct dentry *dentry;
14290 +       struct au_finfo *finfo;
14291 +
14292 +       if (!aopen)
14293 +               err = au_finfo_init(file, args->fidir);
14294 +       else {
14295 +               lockdep_off();
14296 +               err = au_finfo_init(file, args->fidir);
14297 +               lockdep_on();
14298 +       }
14299 +       if (unlikely(err))
14300 +               goto out;
14301 +
14302 +       dentry = file->f_path.dentry;
14303 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14304 +       di_write_lock_child(dentry);
14305 +       err = au_cmoo(dentry);
14306 +       di_downgrade_lock(dentry, AuLock_IR);
14307 +       if (!err) {
14308 +               if (!aopen)
14309 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14310 +               else {
14311 +                       lockdep_off();
14312 +                       err = args->open(file, vfsub_file_flags(file),
14313 +                                        args->h_file);
14314 +                       lockdep_on();
14315 +               }
14316 +       }
14317 +       di_read_unlock(dentry, AuLock_IR);
14318 +
14319 +       finfo = au_fi(file);
14320 +       if (!err) {
14321 +               finfo->fi_file = file;
14322 +               au_hbl_add(&finfo->fi_hlist,
14323 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14324 +       }
14325 +       if (!aopen)
14326 +               fi_write_unlock(file);
14327 +       else {
14328 +               lockdep_off();
14329 +               fi_write_unlock(file);
14330 +               lockdep_on();
14331 +       }
14332 +       if (unlikely(err)) {
14333 +               finfo->fi_hdir = NULL;
14334 +               au_finfo_fin(file);
14335 +       }
14336 +
14337 +out:
14338 +       AuTraceErr(err);
14339 +       return err;
14340 +}
14341 +
14342 +int au_reopen_nondir(struct file *file)
14343 +{
14344 +       int err;
14345 +       aufs_bindex_t btop;
14346 +       struct dentry *dentry;
14347 +       struct au_branch *br;
14348 +       struct file *h_file, *h_file_tmp;
14349 +
14350 +       dentry = file->f_path.dentry;
14351 +       btop = au_dbtop(dentry);
14352 +       br = au_sbr(dentry->d_sb, btop);
14353 +       h_file_tmp = NULL;
14354 +       if (au_fbtop(file) == btop) {
14355 +               h_file = au_hf_top(file);
14356 +               if (file->f_mode == h_file->f_mode)
14357 +                       return 0; /* success */
14358 +               h_file_tmp = h_file;
14359 +               get_file(h_file_tmp);
14360 +               au_lcnt_inc(&br->br_nfiles);
14361 +               au_set_h_fptr(file, btop, NULL);
14362 +       }
14363 +       AuDebugOn(au_fi(file)->fi_hdir);
14364 +       /*
14365 +        * it can happen
14366 +        * file exists on both of rw and ro
14367 +        * open --> dbtop and fbtop are both 0
14368 +        * prepend a branch as rw, "rw" become ro
14369 +        * remove rw/file
14370 +        * delete the top branch, "rw" becomes rw again
14371 +        *      --> dbtop is 1, fbtop is still 0
14372 +        * write --> fbtop is 0 but dbtop is 1
14373 +        */
14374 +       /* AuDebugOn(au_fbtop(file) < btop); */
14375 +
14376 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14377 +                          file, /*force_wr*/0);
14378 +       err = PTR_ERR(h_file);
14379 +       if (IS_ERR(h_file)) {
14380 +               if (h_file_tmp) {
14381 +                       /* revert */
14382 +                       au_set_h_fptr(file, btop, h_file_tmp);
14383 +                       h_file_tmp = NULL;
14384 +               }
14385 +               goto out; /* todo: close all? */
14386 +       }
14387 +
14388 +       err = 0;
14389 +       au_set_fbtop(file, btop);
14390 +       au_set_h_fptr(file, btop, h_file);
14391 +       au_update_figen(file);
14392 +       /* todo: necessary? */
14393 +       /* file->f_ra = h_file->f_ra; */
14394 +
14395 +out:
14396 +       if (h_file_tmp) {
14397 +               fput(h_file_tmp);
14398 +               au_lcnt_dec(&br->br_nfiles);
14399 +       }
14400 +       return err;
14401 +}
14402 +
14403 +/* ---------------------------------------------------------------------- */
14404 +
14405 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14406 +                       struct dentry *hi_wh)
14407 +{
14408 +       int err;
14409 +       aufs_bindex_t btop;
14410 +       struct au_dinfo *dinfo;
14411 +       struct dentry *h_dentry;
14412 +       struct au_hdentry *hdp;
14413 +
14414 +       dinfo = au_di(file->f_path.dentry);
14415 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14416 +
14417 +       btop = dinfo->di_btop;
14418 +       dinfo->di_btop = btgt;
14419 +       hdp = au_hdentry(dinfo, btgt);
14420 +       h_dentry = hdp->hd_dentry;
14421 +       hdp->hd_dentry = hi_wh;
14422 +       err = au_reopen_nondir(file);
14423 +       hdp->hd_dentry = h_dentry;
14424 +       dinfo->di_btop = btop;
14425 +
14426 +       return err;
14427 +}
14428 +
14429 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14430 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14431 +{
14432 +       int err;
14433 +       struct inode *inode, *h_inode;
14434 +       struct dentry *h_dentry, *hi_wh;
14435 +       struct au_cp_generic cpg = {
14436 +               .dentry = file->f_path.dentry,
14437 +               .bdst   = bcpup,
14438 +               .bsrc   = -1,
14439 +               .len    = len,
14440 +               .pin    = pin
14441 +       };
14442 +
14443 +       au_update_dbtop(cpg.dentry);
14444 +       inode = d_inode(cpg.dentry);
14445 +       h_inode = NULL;
14446 +       if (au_dbtop(cpg.dentry) <= bcpup
14447 +           && au_dbbot(cpg.dentry) >= bcpup) {
14448 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14449 +               if (h_dentry && d_is_positive(h_dentry))
14450 +                       h_inode = d_inode(h_dentry);
14451 +       }
14452 +       hi_wh = au_hi_wh(inode, bcpup);
14453 +       if (!hi_wh && !h_inode)
14454 +               err = au_sio_cpup_wh(&cpg, file);
14455 +       else
14456 +               /* already copied-up after unlink */
14457 +               err = au_reopen_wh(file, bcpup, hi_wh);
14458 +
14459 +       if (!err
14460 +           && (inode->i_nlink > 1
14461 +               || (inode->i_state & I_LINKABLE))
14462 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14463 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14464 +
14465 +       return err;
14466 +}
14467 +
14468 +/*
14469 + * prepare the @file for writing.
14470 + */
14471 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14472 +{
14473 +       int err;
14474 +       aufs_bindex_t dbtop;
14475 +       struct dentry *parent;
14476 +       struct inode *inode;
14477 +       struct super_block *sb;
14478 +       struct file *h_file;
14479 +       struct au_cp_generic cpg = {
14480 +               .dentry = file->f_path.dentry,
14481 +               .bdst   = -1,
14482 +               .bsrc   = -1,
14483 +               .len    = len,
14484 +               .pin    = pin,
14485 +               .flags  = AuCpup_DTIME
14486 +       };
14487 +
14488 +       sb = cpg.dentry->d_sb;
14489 +       inode = d_inode(cpg.dentry);
14490 +       cpg.bsrc = au_fbtop(file);
14491 +       err = au_test_ro(sb, cpg.bsrc, inode);
14492 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14493 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14494 +                            /*flags*/0);
14495 +               goto out;
14496 +       }
14497 +
14498 +       /* need to cpup or reopen */
14499 +       parent = dget_parent(cpg.dentry);
14500 +       di_write_lock_parent(parent);
14501 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14502 +       cpg.bdst = err;
14503 +       if (unlikely(err < 0))
14504 +               goto out_dgrade;
14505 +       err = 0;
14506 +
14507 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14508 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14509 +               if (unlikely(err))
14510 +                       goto out_dgrade;
14511 +       }
14512 +
14513 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14514 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14515 +       if (unlikely(err))
14516 +               goto out_dgrade;
14517 +
14518 +       dbtop = au_dbtop(cpg.dentry);
14519 +       if (dbtop <= cpg.bdst)
14520 +               cpg.bsrc = cpg.bdst;
14521 +
14522 +       if (dbtop <= cpg.bdst           /* just reopen */
14523 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14524 +               ) {
14525 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14526 +               if (IS_ERR(h_file))
14527 +                       err = PTR_ERR(h_file);
14528 +               else {
14529 +                       di_downgrade_lock(parent, AuLock_IR);
14530 +                       if (dbtop > cpg.bdst)
14531 +                               err = au_sio_cpup_simple(&cpg);
14532 +                       if (!err)
14533 +                               err = au_reopen_nondir(file);
14534 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14535 +               }
14536 +       } else {                        /* copyup as wh and reopen */
14537 +               /*
14538 +                * since writable hfsplus branch is not supported,
14539 +                * h_open_pre/post() are unnecessary.
14540 +                */
14541 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14542 +               di_downgrade_lock(parent, AuLock_IR);
14543 +       }
14544 +
14545 +       if (!err) {
14546 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14547 +               goto out_dput; /* success */
14548 +       }
14549 +       au_unpin(pin);
14550 +       goto out_unlock;
14551 +
14552 +out_dgrade:
14553 +       di_downgrade_lock(parent, AuLock_IR);
14554 +out_unlock:
14555 +       di_read_unlock(parent, AuLock_IR);
14556 +out_dput:
14557 +       dput(parent);
14558 +out:
14559 +       return err;
14560 +}
14561 +
14562 +/* ---------------------------------------------------------------------- */
14563 +
14564 +int au_do_flush(struct file *file, fl_owner_t id,
14565 +               int (*flush)(struct file *file, fl_owner_t id))
14566 +{
14567 +       int err;
14568 +       struct super_block *sb;
14569 +       struct inode *inode;
14570 +
14571 +       inode = file_inode(file);
14572 +       sb = inode->i_sb;
14573 +       si_noflush_read_lock(sb);
14574 +       fi_read_lock(file);
14575 +       ii_read_lock_child(inode);
14576 +
14577 +       err = flush(file, id);
14578 +       au_cpup_attr_timesizes(inode);
14579 +
14580 +       ii_read_unlock(inode);
14581 +       fi_read_unlock(file);
14582 +       si_read_unlock(sb);
14583 +       return err;
14584 +}
14585 +
14586 +/* ---------------------------------------------------------------------- */
14587 +
14588 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14589 +{
14590 +       int err;
14591 +       struct au_pin pin;
14592 +       struct au_finfo *finfo;
14593 +       struct dentry *parent, *hi_wh;
14594 +       struct inode *inode;
14595 +       struct super_block *sb;
14596 +       struct au_cp_generic cpg = {
14597 +               .dentry = file->f_path.dentry,
14598 +               .bdst   = -1,
14599 +               .bsrc   = -1,
14600 +               .len    = -1,
14601 +               .pin    = &pin,
14602 +               .flags  = AuCpup_DTIME
14603 +       };
14604 +
14605 +       FiMustWriteLock(file);
14606 +
14607 +       err = 0;
14608 +       finfo = au_fi(file);
14609 +       sb = cpg.dentry->d_sb;
14610 +       inode = d_inode(cpg.dentry);
14611 +       cpg.bdst = au_ibtop(inode);
14612 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14613 +               goto out;
14614 +
14615 +       parent = dget_parent(cpg.dentry);
14616 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14617 +               di_read_lock_parent(parent, !AuLock_IR);
14618 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14619 +               cpg.bdst = err;
14620 +               di_read_unlock(parent, !AuLock_IR);
14621 +               if (unlikely(err < 0))
14622 +                       goto out_parent;
14623 +               err = 0;
14624 +       }
14625 +
14626 +       di_read_lock_parent(parent, AuLock_IR);
14627 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14628 +       if (!S_ISDIR(inode->i_mode)
14629 +           && au_opt_test(au_mntflags(sb), PLINK)
14630 +           && au_plink_test(inode)
14631 +           && !d_unhashed(cpg.dentry)
14632 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14633 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14634 +               if (unlikely(err))
14635 +                       goto out_unlock;
14636 +
14637 +               /* always superio. */
14638 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14639 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14640 +               if (!err) {
14641 +                       err = au_sio_cpup_simple(&cpg);
14642 +                       au_unpin(&pin);
14643 +               }
14644 +       } else if (hi_wh) {
14645 +               /* already copied-up after unlink */
14646 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14647 +               *need_reopen = 0;
14648 +       }
14649 +
14650 +out_unlock:
14651 +       di_read_unlock(parent, AuLock_IR);
14652 +out_parent:
14653 +       dput(parent);
14654 +out:
14655 +       return err;
14656 +}
14657 +
14658 +static void au_do_refresh_dir(struct file *file)
14659 +{
14660 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14661 +       struct au_hfile *p, tmp, *q;
14662 +       struct au_finfo *finfo;
14663 +       struct super_block *sb;
14664 +       struct au_fidir *fidir;
14665 +
14666 +       FiMustWriteLock(file);
14667 +
14668 +       sb = file->f_path.dentry->d_sb;
14669 +       finfo = au_fi(file);
14670 +       fidir = finfo->fi_hdir;
14671 +       AuDebugOn(!fidir);
14672 +       p = fidir->fd_hfile + finfo->fi_btop;
14673 +       brid = p->hf_br->br_id;
14674 +       bbot = fidir->fd_bbot;
14675 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14676 +               if (!p->hf_file)
14677 +                       continue;
14678 +
14679 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14680 +               if (new_bindex == bindex)
14681 +                       continue;
14682 +               if (new_bindex < 0) {
14683 +                       au_set_h_fptr(file, bindex, NULL);
14684 +                       continue;
14685 +               }
14686 +
14687 +               /* swap two lower inode, and loop again */
14688 +               q = fidir->fd_hfile + new_bindex;
14689 +               tmp = *q;
14690 +               *q = *p;
14691 +               *p = tmp;
14692 +               if (tmp.hf_file) {
14693 +                       bindex--;
14694 +                       p--;
14695 +               }
14696 +       }
14697 +
14698 +       p = fidir->fd_hfile;
14699 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14700 +               bbot = au_sbbot(sb);
14701 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14702 +                    finfo->fi_btop++, p++)
14703 +                       if (p->hf_file) {
14704 +                               if (file_inode(p->hf_file))
14705 +                                       break;
14706 +                               au_hfput(p, /*execed*/0);
14707 +                       }
14708 +       } else {
14709 +               bbot = au_br_index(sb, brid);
14710 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14711 +                    finfo->fi_btop++, p++)
14712 +                       if (p->hf_file)
14713 +                               au_hfput(p, /*execed*/0);
14714 +               bbot = au_sbbot(sb);
14715 +       }
14716 +
14717 +       p = fidir->fd_hfile + bbot;
14718 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14719 +            fidir->fd_bbot--, p--)
14720 +               if (p->hf_file) {
14721 +                       if (file_inode(p->hf_file))
14722 +                               break;
14723 +                       au_hfput(p, /*execed*/0);
14724 +               }
14725 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14726 +}
14727 +
14728 +/*
14729 + * after branch manipulating, refresh the file.
14730 + */
14731 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14732 +{
14733 +       int err, need_reopen, nbr;
14734 +       aufs_bindex_t bbot, bindex;
14735 +       struct dentry *dentry;
14736 +       struct super_block *sb;
14737 +       struct au_finfo *finfo;
14738 +       struct au_hfile *hfile;
14739 +
14740 +       dentry = file->f_path.dentry;
14741 +       sb = dentry->d_sb;
14742 +       nbr = au_sbbot(sb) + 1;
14743 +       finfo = au_fi(file);
14744 +       if (!finfo->fi_hdir) {
14745 +               hfile = &finfo->fi_htop;
14746 +               AuDebugOn(!hfile->hf_file);
14747 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14748 +               AuDebugOn(bindex < 0);
14749 +               if (bindex != finfo->fi_btop)
14750 +                       au_set_fbtop(file, bindex);
14751 +       } else {
14752 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14753 +               if (unlikely(err))
14754 +                       goto out;
14755 +               au_do_refresh_dir(file);
14756 +       }
14757 +
14758 +       err = 0;
14759 +       need_reopen = 1;
14760 +       if (!au_test_mmapped(file))
14761 +               err = au_file_refresh_by_inode(file, &need_reopen);
14762 +       if (finfo->fi_hdir)
14763 +               /* harmless if err */
14764 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14765 +       if (!err && need_reopen && !d_unlinked(dentry))
14766 +               err = reopen(file);
14767 +       if (!err) {
14768 +               au_update_figen(file);
14769 +               goto out; /* success */
14770 +       }
14771 +
14772 +       /* error, close all lower files */
14773 +       if (finfo->fi_hdir) {
14774 +               bbot = au_fbbot_dir(file);
14775 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14776 +                       au_set_h_fptr(file, bindex, NULL);
14777 +       }
14778 +
14779 +out:
14780 +       return err;
14781 +}
14782 +
14783 +/* common function to regular file and dir */
14784 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14785 +                         int wlock, unsigned int fi_lsc)
14786 +{
14787 +       int err;
14788 +       unsigned int sigen, figen;
14789 +       aufs_bindex_t btop;
14790 +       unsigned char pseudo_link;
14791 +       struct dentry *dentry;
14792 +       struct inode *inode;
14793 +
14794 +       err = 0;
14795 +       dentry = file->f_path.dentry;
14796 +       inode = d_inode(dentry);
14797 +       sigen = au_sigen(dentry->d_sb);
14798 +       fi_write_lock_nested(file, fi_lsc);
14799 +       figen = au_figen(file);
14800 +       if (!fi_lsc)
14801 +               di_write_lock_child(dentry);
14802 +       else
14803 +               di_write_lock_child2(dentry);
14804 +       btop = au_dbtop(dentry);
14805 +       pseudo_link = (btop != au_ibtop(inode));
14806 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14807 +               if (!wlock) {
14808 +                       di_downgrade_lock(dentry, AuLock_IR);
14809 +                       fi_downgrade_lock(file);
14810 +               }
14811 +               goto out; /* success */
14812 +       }
14813 +
14814 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14815 +       if (au_digen_test(dentry, sigen)) {
14816 +               err = au_reval_dpath(dentry, sigen);
14817 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14818 +       }
14819 +
14820 +       if (!err)
14821 +               err = refresh_file(file, reopen);
14822 +       if (!err) {
14823 +               if (!wlock) {
14824 +                       di_downgrade_lock(dentry, AuLock_IR);
14825 +                       fi_downgrade_lock(file);
14826 +               }
14827 +       } else {
14828 +               di_write_unlock(dentry);
14829 +               fi_write_unlock(file);
14830 +       }
14831 +
14832 +out:
14833 +       return err;
14834 +}
14835 +
14836 +/* ---------------------------------------------------------------------- */
14837 +
14838 +/* cf. aufs_nopage() */
14839 +/* for madvise(2) */
14840 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14841 +{
14842 +       unlock_page(page);
14843 +       return 0;
14844 +}
14845 +
14846 +/* it will never be called, but necessary to support O_DIRECT */
14847 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14848 +{ BUG(); return 0; }
14849 +
14850 +/* they will never be called. */
14851 +#ifdef CONFIG_AUFS_DEBUG
14852 +/*
14853 +void aufs_readahead(struct readahead_control *)
14854 +{ AuUnsupport(); }
14855 +*/
14856 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14857 +                           loff_t pos, unsigned len, unsigned flags,
14858 +                           struct page **pagep, void **fsdata)
14859 +{ AuUnsupport(); return 0; }
14860 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14861 +                         loff_t pos, unsigned len, unsigned copied,
14862 +                         struct page *page, void *fsdata)
14863 +{ AuUnsupport(); return 0; }
14864 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14865 +{ AuUnsupport(); return 0; }
14866 +
14867 +static int aufs_set_page_dirty(struct page *page)
14868 +{ AuUnsupport(); return 0; }
14869 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14870 +                               unsigned int length)
14871 +{ AuUnsupport(); }
14872 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14873 +{ AuUnsupport(); return 0; }
14874 +#if 0 /* called by memory compaction regardless file */
14875 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14876 +                           struct page *page, enum migrate_mode mode)
14877 +{ AuUnsupport(); return 0; }
14878 +#endif
14879 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14880 +{ AuUnsupport(); return true; }
14881 +static void aufs_putback_page(struct page *page)
14882 +{ AuUnsupport(); }
14883 +static int aufs_launder_page(struct page *page)
14884 +{ AuUnsupport(); return 0; }
14885 +static int aufs_is_partially_uptodate(struct page *page,
14886 +                                     unsigned long from,
14887 +                                     unsigned long count)
14888 +{ AuUnsupport(); return 0; }
14889 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14890 +                                   bool *writeback)
14891 +{ AuUnsupport(); }
14892 +static int aufs_error_remove_page(struct address_space *mapping,
14893 +                                 struct page *page)
14894 +{ AuUnsupport(); return 0; }
14895 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14896 +                             sector_t *span)
14897 +{ AuUnsupport(); return 0; }
14898 +static void aufs_swap_deactivate(struct file *file)
14899 +{ AuUnsupport(); }
14900 +#endif /* CONFIG_AUFS_DEBUG */
14901 +
14902 +const struct address_space_operations aufs_aop = {
14903 +       .readpage               = aufs_readpage,
14904 +       .direct_IO              = aufs_direct_IO,
14905 +#ifdef CONFIG_AUFS_DEBUG
14906 +       .writepage              = aufs_writepage,
14907 +       /* no writepages, because of writepage */
14908 +       .set_page_dirty         = aufs_set_page_dirty,
14909 +       /* no readpages, because of readpage */
14910 +       .write_begin            = aufs_write_begin,
14911 +       .write_end              = aufs_write_end,
14912 +       /* no bmap, no block device */
14913 +       .invalidatepage         = aufs_invalidatepage,
14914 +       .releasepage            = aufs_releasepage,
14915 +       /* is fallback_migrate_page ok? */
14916 +       /* .migratepage         = aufs_migratepage, */
14917 +       .isolate_page           = aufs_isolate_page,
14918 +       .putback_page           = aufs_putback_page,
14919 +       .launder_page           = aufs_launder_page,
14920 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14921 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14922 +       .error_remove_page      = aufs_error_remove_page,
14923 +       .swap_activate          = aufs_swap_activate,
14924 +       .swap_deactivate        = aufs_swap_deactivate
14925 +#endif /* CONFIG_AUFS_DEBUG */
14926 +};
14927 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14928 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14929 +++ linux/fs/aufs/file.h        2020-01-27 10:57:18.172204883 +0100
14930 @@ -0,0 +1,342 @@
14931 +/* SPDX-License-Identifier: GPL-2.0 */
14932 +/*
14933 + * Copyright (C) 2005-2020 Junjiro R. Okajima
14934 + *
14935 + * This program, aufs is free software; you can redistribute it and/or modify
14936 + * it under the terms of the GNU General Public License as published by
14937 + * the Free Software Foundation; either version 2 of the License, or
14938 + * (at your option) any later version.
14939 + *
14940 + * This program is distributed in the hope that it will be useful,
14941 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14942 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14943 + * GNU General Public License for more details.
14944 + *
14945 + * You should have received a copy of the GNU General Public License
14946 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14947 + */
14948 +
14949 +/*
14950 + * file operations
14951 + */
14952 +
14953 +#ifndef __AUFS_FILE_H__
14954 +#define __AUFS_FILE_H__
14955 +
14956 +#ifdef __KERNEL__
14957 +
14958 +#include <linux/file.h>
14959 +#include <linux/fs.h>
14960 +#include <linux/mm_types.h>
14961 +#include <linux/poll.h>
14962 +#include "rwsem.h"
14963 +
14964 +struct au_branch;
14965 +struct au_hfile {
14966 +       struct file             *hf_file;
14967 +       struct au_branch        *hf_br;
14968 +};
14969 +
14970 +struct au_vdir;
14971 +struct au_fidir {
14972 +       aufs_bindex_t           fd_bbot;
14973 +       aufs_bindex_t           fd_nent;
14974 +       struct au_vdir          *fd_vdir_cache;
14975 +       struct au_hfile         fd_hfile[];
14976 +};
14977 +
14978 +static inline int au_fidir_sz(int nent)
14979 +{
14980 +       AuDebugOn(nent < 0);
14981 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14982 +}
14983 +
14984 +struct au_finfo {
14985 +       atomic_t                fi_generation;
14986 +
14987 +       struct au_rwsem         fi_rwsem;
14988 +       aufs_bindex_t           fi_btop;
14989 +
14990 +       /* do not union them */
14991 +       struct {                                /* for non-dir */
14992 +               struct au_hfile                 fi_htop;
14993 +               atomic_t                        fi_mmapped;
14994 +       };
14995 +       struct au_fidir         *fi_hdir;       /* for dir only */
14996 +
14997 +       struct hlist_bl_node    fi_hlist;
14998 +       struct file             *fi_file;       /* very ugly */
14999 +       struct rcu_head         rcu;
15000 +} ____cacheline_aligned_in_smp;
15001 +
15002 +/* ---------------------------------------------------------------------- */
15003 +
15004 +/* file.c */
15005 +extern const struct address_space_operations aufs_aop;
15006 +unsigned int au_file_roflags(unsigned int flags);
15007 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15008 +                      struct file *file, int force_wr);
15009 +struct au_do_open_args {
15010 +       int             aopen;
15011 +       int             (*open)(struct file *file, int flags,
15012 +                               struct file *h_file);
15013 +       struct au_fidir *fidir;
15014 +       struct file     *h_file;
15015 +};
15016 +int au_do_open(struct file *file, struct au_do_open_args *args);
15017 +int au_reopen_nondir(struct file *file);
15018 +struct au_pin;
15019 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15020 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15021 +                         int wlock, unsigned int fi_lsc);
15022 +int au_do_flush(struct file *file, fl_owner_t id,
15023 +               int (*flush)(struct file *file, fl_owner_t id));
15024 +
15025 +/* poll.c */
15026 +#ifdef CONFIG_AUFS_POLL
15027 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15028 +#endif
15029 +
15030 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15031 +/* hfsplus.c */
15032 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15033 +                          int force_wr);
15034 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15035 +                   struct file *h_file);
15036 +#else
15037 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15038 +       aufs_bindex_t bindex, int force_wr)
15039 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15040 +          struct file *h_file);
15041 +#endif
15042 +
15043 +/* f_op.c */
15044 +extern const struct file_operations aufs_file_fop;
15045 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15046 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15047 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15048 +
15049 +/* finfo.c */
15050 +void au_hfput(struct au_hfile *hf, int execed);
15051 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15052 +                  struct file *h_file);
15053 +
15054 +void au_update_figen(struct file *file);
15055 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15056 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15057 +
15058 +void au_fi_init_once(void *_fi);
15059 +void au_finfo_fin(struct file *file);
15060 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15061 +
15062 +/* ioctl.c */
15063 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15064 +#ifdef CONFIG_COMPAT
15065 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15066 +                          unsigned long arg);
15067 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15068 +                             unsigned long arg);
15069 +#endif
15070 +
15071 +/* ---------------------------------------------------------------------- */
15072 +
15073 +static inline struct au_finfo *au_fi(struct file *file)
15074 +{
15075 +       return file->private_data;
15076 +}
15077 +
15078 +/* ---------------------------------------------------------------------- */
15079 +
15080 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15081 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15082 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15083 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15084 +/*
15085 +#define fi_read_trylock_nested(f) \
15086 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15087 +#define fi_write_trylock_nested(f) \
15088 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15089 +*/
15090 +
15091 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15092 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15093 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15094 +
15095 +/* lock subclass for finfo */
15096 +enum {
15097 +       AuLsc_FI_1,
15098 +       AuLsc_FI_2
15099 +};
15100 +
15101 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15102 +{
15103 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15104 +}
15105 +
15106 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15107 +{
15108 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15109 +}
15110 +
15111 +/*
15112 + * fi_read_lock_1, fi_write_lock_1,
15113 + * fi_read_lock_2, fi_write_lock_2
15114 + */
15115 +#define AuReadLockFunc(name) \
15116 +static inline void fi_read_lock_##name(struct file *f) \
15117 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15118 +
15119 +#define AuWriteLockFunc(name) \
15120 +static inline void fi_write_lock_##name(struct file *f) \
15121 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15122 +
15123 +#define AuRWLockFuncs(name) \
15124 +       AuReadLockFunc(name) \
15125 +       AuWriteLockFunc(name)
15126 +
15127 +AuRWLockFuncs(1);
15128 +AuRWLockFuncs(2);
15129 +
15130 +#undef AuReadLockFunc
15131 +#undef AuWriteLockFunc
15132 +#undef AuRWLockFuncs
15133 +
15134 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15135 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15136 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15137 +
15138 +/* ---------------------------------------------------------------------- */
15139 +
15140 +/* todo: hard/soft set? */
15141 +static inline aufs_bindex_t au_fbtop(struct file *file)
15142 +{
15143 +       FiMustAnyLock(file);
15144 +       return au_fi(file)->fi_btop;
15145 +}
15146 +
15147 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15148 +{
15149 +       FiMustAnyLock(file);
15150 +       AuDebugOn(!au_fi(file)->fi_hdir);
15151 +       return au_fi(file)->fi_hdir->fd_bbot;
15152 +}
15153 +
15154 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15155 +{
15156 +       FiMustAnyLock(file);
15157 +       AuDebugOn(!au_fi(file)->fi_hdir);
15158 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15159 +}
15160 +
15161 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15162 +{
15163 +       FiMustWriteLock(file);
15164 +       au_fi(file)->fi_btop = bindex;
15165 +}
15166 +
15167 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15168 +{
15169 +       FiMustWriteLock(file);
15170 +       AuDebugOn(!au_fi(file)->fi_hdir);
15171 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15172 +}
15173 +
15174 +static inline void au_set_fvdir_cache(struct file *file,
15175 +                                     struct au_vdir *vdir_cache)
15176 +{
15177 +       FiMustWriteLock(file);
15178 +       AuDebugOn(!au_fi(file)->fi_hdir);
15179 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15180 +}
15181 +
15182 +static inline struct file *au_hf_top(struct file *file)
15183 +{
15184 +       FiMustAnyLock(file);
15185 +       AuDebugOn(au_fi(file)->fi_hdir);
15186 +       return au_fi(file)->fi_htop.hf_file;
15187 +}
15188 +
15189 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15190 +{
15191 +       FiMustAnyLock(file);
15192 +       AuDebugOn(!au_fi(file)->fi_hdir);
15193 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15194 +}
15195 +
15196 +/* todo: memory barrier? */
15197 +static inline unsigned int au_figen(struct file *f)
15198 +{
15199 +       return atomic_read(&au_fi(f)->fi_generation);
15200 +}
15201 +
15202 +static inline void au_set_mmapped(struct file *f)
15203 +{
15204 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15205 +               return;
15206 +       pr_warn("fi_mmapped wrapped around\n");
15207 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15208 +               ;
15209 +}
15210 +
15211 +static inline void au_unset_mmapped(struct file *f)
15212 +{
15213 +       atomic_dec(&au_fi(f)->fi_mmapped);
15214 +}
15215 +
15216 +static inline int au_test_mmapped(struct file *f)
15217 +{
15218 +       return atomic_read(&au_fi(f)->fi_mmapped);
15219 +}
15220 +
15221 +/* customize vma->vm_file */
15222 +
15223 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15224 +                                      struct file *file)
15225 +{
15226 +       struct file *f;
15227 +
15228 +       f = vma->vm_file;
15229 +       get_file(file);
15230 +       vma->vm_file = file;
15231 +       fput(f);
15232 +}
15233 +
15234 +#ifdef CONFIG_MMU
15235 +#define AuDbgVmRegion(file, vma) do {} while (0)
15236 +
15237 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15238 +                                   struct file *file)
15239 +{
15240 +       au_do_vm_file_reset(vma, file);
15241 +}
15242 +#else
15243 +#define AuDbgVmRegion(file, vma) \
15244 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15245 +
15246 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15247 +                                   struct file *file)
15248 +{
15249 +       struct file *f;
15250 +
15251 +       au_do_vm_file_reset(vma, file);
15252 +       f = vma->vm_region->vm_file;
15253 +       get_file(file);
15254 +       vma->vm_region->vm_file = file;
15255 +       fput(f);
15256 +}
15257 +#endif /* CONFIG_MMU */
15258 +
15259 +/* handle vma->vm_prfile */
15260 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15261 +                                   struct file *file)
15262 +{
15263 +       get_file(file);
15264 +       vma->vm_prfile = file;
15265 +#ifndef CONFIG_MMU
15266 +       get_file(file);
15267 +       vma->vm_region->vm_prfile = file;
15268 +#endif
15269 +}
15270 +
15271 +#endif /* __KERNEL__ */
15272 +#endif /* __AUFS_FILE_H__ */
15273 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15274 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15275 +++ linux/fs/aufs/finfo.c       2020-01-27 10:57:18.172204883 +0100
15276 @@ -0,0 +1,149 @@
15277 +// SPDX-License-Identifier: GPL-2.0
15278 +/*
15279 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15280 + *
15281 + * This program, aufs is free software; you can redistribute it and/or modify
15282 + * it under the terms of the GNU General Public License as published by
15283 + * the Free Software Foundation; either version 2 of the License, or
15284 + * (at your option) any later version.
15285 + *
15286 + * This program is distributed in the hope that it will be useful,
15287 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15288 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15289 + * GNU General Public License for more details.
15290 + *
15291 + * You should have received a copy of the GNU General Public License
15292 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15293 + */
15294 +
15295 +/*
15296 + * file private data
15297 + */
15298 +
15299 +#include "aufs.h"
15300 +
15301 +void au_hfput(struct au_hfile *hf, int execed)
15302 +{
15303 +       if (execed)
15304 +               allow_write_access(hf->hf_file);
15305 +       fput(hf->hf_file);
15306 +       hf->hf_file = NULL;
15307 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15308 +       hf->hf_br = NULL;
15309 +}
15310 +
15311 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15312 +{
15313 +       struct au_finfo *finfo = au_fi(file);
15314 +       struct au_hfile *hf;
15315 +       struct au_fidir *fidir;
15316 +
15317 +       fidir = finfo->fi_hdir;
15318 +       if (!fidir) {
15319 +               AuDebugOn(finfo->fi_btop != bindex);
15320 +               hf = &finfo->fi_htop;
15321 +       } else
15322 +               hf = fidir->fd_hfile + bindex;
15323 +
15324 +       if (hf && hf->hf_file)
15325 +               au_hfput(hf, vfsub_file_execed(file));
15326 +       if (val) {
15327 +               FiMustWriteLock(file);
15328 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15329 +               hf->hf_file = val;
15330 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15331 +       }
15332 +}
15333 +
15334 +void au_update_figen(struct file *file)
15335 +{
15336 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15337 +       /* smp_mb(); */ /* atomic_set */
15338 +}
15339 +
15340 +/* ---------------------------------------------------------------------- */
15341 +
15342 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15343 +{
15344 +       struct au_fidir *fidir;
15345 +       int nbr;
15346 +
15347 +       nbr = au_sbbot(sb) + 1;
15348 +       if (nbr < 2)
15349 +               nbr = 2; /* initial allocate for 2 branches */
15350 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15351 +       if (fidir) {
15352 +               fidir->fd_bbot = -1;
15353 +               fidir->fd_nent = nbr;
15354 +       }
15355 +
15356 +       return fidir;
15357 +}
15358 +
15359 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15360 +{
15361 +       int err;
15362 +       struct au_fidir *fidir, *p;
15363 +
15364 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15365 +       fidir = finfo->fi_hdir;
15366 +       AuDebugOn(!fidir);
15367 +
15368 +       err = -ENOMEM;
15369 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15370 +                        GFP_NOFS, may_shrink);
15371 +       if (p) {
15372 +               p->fd_nent = nbr;
15373 +               finfo->fi_hdir = p;
15374 +               err = 0;
15375 +       }
15376 +
15377 +       return err;
15378 +}
15379 +
15380 +/* ---------------------------------------------------------------------- */
15381 +
15382 +void au_finfo_fin(struct file *file)
15383 +{
15384 +       struct au_finfo *finfo;
15385 +
15386 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15387 +
15388 +       finfo = au_fi(file);
15389 +       AuDebugOn(finfo->fi_hdir);
15390 +       AuRwDestroy(&finfo->fi_rwsem);
15391 +       au_cache_free_finfo(finfo);
15392 +}
15393 +
15394 +void au_fi_init_once(void *_finfo)
15395 +{
15396 +       struct au_finfo *finfo = _finfo;
15397 +
15398 +       au_rw_init(&finfo->fi_rwsem);
15399 +}
15400 +
15401 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15402 +{
15403 +       int err;
15404 +       struct au_finfo *finfo;
15405 +       struct dentry *dentry;
15406 +
15407 +       err = -ENOMEM;
15408 +       dentry = file->f_path.dentry;
15409 +       finfo = au_cache_alloc_finfo();
15410 +       if (unlikely(!finfo))
15411 +               goto out;
15412 +
15413 +       err = 0;
15414 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15415 +       au_rw_write_lock(&finfo->fi_rwsem);
15416 +       finfo->fi_btop = -1;
15417 +       finfo->fi_hdir = fidir;
15418 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15419 +       /* smp_mb(); */ /* atomic_set */
15420 +
15421 +       file->private_data = finfo;
15422 +
15423 +out:
15424 +       return err;
15425 +}
15426 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15427 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15428 +++ linux/fs/aufs/f_op.c        2020-12-15 14:10:58.914689728 +0100
15429 @@ -0,0 +1,762 @@
15430 +// SPDX-License-Identifier: GPL-2.0
15431 +/*
15432 + * Copyright (C) 2005-2020 Junjiro R. Okajima
15433 + *
15434 + * This program, aufs is free software; you can redistribute it and/or modify
15435 + * it under the terms of the GNU General Public License as published by
15436 + * the Free Software Foundation; either version 2 of the License, or
15437 + * (at your option) any later version.
15438 + *
15439 + * This program is distributed in the hope that it will be useful,
15440 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15441 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15442 + * GNU General Public License for more details.
15443 + *
15444 + * You should have received a copy of the GNU General Public License
15445 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15446 + */
15447 +
15448 +/*
15449 + * file and vm operations
15450 + */
15451 +
15452 +#include <linux/aio.h>
15453 +#include <linux/fs_stack.h>
15454 +#include <linux/mman.h>
15455 +#include <linux/security.h>
15456 +#include "aufs.h"
15457 +
15458 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15459 +{
15460 +       int err;
15461 +       aufs_bindex_t bindex;
15462 +       struct dentry *dentry, *h_dentry;
15463 +       struct au_finfo *finfo;
15464 +       struct inode *h_inode;
15465 +
15466 +       FiMustWriteLock(file);
15467 +
15468 +       err = 0;
15469 +       dentry = file->f_path.dentry;
15470 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15471 +       finfo = au_fi(file);
15472 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15473 +       atomic_set(&finfo->fi_mmapped, 0);
15474 +       bindex = au_dbtop(dentry);
15475 +       if (!h_file) {
15476 +               h_dentry = au_h_dptr(dentry, bindex);
15477 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15478 +               if (unlikely(err))
15479 +                       goto out;
15480 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15481 +               if (IS_ERR(h_file)) {
15482 +                       err = PTR_ERR(h_file);
15483 +                       goto out;
15484 +               }
15485 +       } else {
15486 +               h_dentry = h_file->f_path.dentry;
15487 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15488 +               if (unlikely(err))
15489 +                       goto out;
15490 +               /* br ref is already inc-ed */
15491 +       }
15492 +
15493 +       if ((flags & __O_TMPFILE)
15494 +           && !(flags & O_EXCL)) {
15495 +               h_inode = file_inode(h_file);
15496 +               spin_lock(&h_inode->i_lock);
15497 +               h_inode->i_state |= I_LINKABLE;
15498 +               spin_unlock(&h_inode->i_lock);
15499 +       }
15500 +       au_set_fbtop(file, bindex);
15501 +       au_set_h_fptr(file, bindex, h_file);
15502 +       au_update_figen(file);
15503 +       /* todo: necessary? */
15504 +       /* file->f_ra = h_file->f_ra; */
15505 +
15506 +out:
15507 +       return err;
15508 +}
15509 +
15510 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15511 +                           struct file *file)
15512 +{
15513 +       int err;
15514 +       struct super_block *sb;
15515 +       struct au_do_open_args args = {
15516 +               .open   = au_do_open_nondir
15517 +       };
15518 +
15519 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15520 +             file, vfsub_file_flags(file), file->f_mode);
15521 +
15522 +       sb = file->f_path.dentry->d_sb;
15523 +       si_read_lock(sb, AuLock_FLUSH);
15524 +       err = au_do_open(file, &args);
15525 +       si_read_unlock(sb);
15526 +       return err;
15527 +}
15528 +
15529 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15530 +{
15531 +       struct au_finfo *finfo;
15532 +       aufs_bindex_t bindex;
15533 +
15534 +       finfo = au_fi(file);
15535 +       au_hbl_del(&finfo->fi_hlist,
15536 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15537 +       bindex = finfo->fi_btop;
15538 +       if (bindex >= 0)
15539 +               au_set_h_fptr(file, bindex, NULL);
15540 +
15541 +       au_finfo_fin(file);
15542 +       return 0;
15543 +}
15544 +
15545 +/* ---------------------------------------------------------------------- */
15546 +
15547 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15548 +{
15549 +       int err;
15550 +       struct file *h_file;
15551 +
15552 +       err = 0;
15553 +       h_file = au_hf_top(file);
15554 +       if (h_file)
15555 +               err = vfsub_flush(h_file, id);
15556 +       return err;
15557 +}
15558 +
15559 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15560 +{
15561 +       return au_do_flush(file, id, au_do_flush_nondir);
15562 +}
15563 +
15564 +/* ---------------------------------------------------------------------- */
15565 +/*
15566 + * read and write functions acquire [fdi]_rwsem once, but release before
15567 + * mmap_sem. This is because to stop a race condition between mmap(2).
15568 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15569 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15570 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15571 + */
15572 +
15573 +/* Callers should call au_read_post() or fput() in the end */
15574 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15575 +{
15576 +       struct file *h_file;
15577 +       int err;
15578 +
15579 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15580 +       if (!err) {
15581 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15582 +               h_file = au_hf_top(file);
15583 +               get_file(h_file);
15584 +               if (!keep_fi)
15585 +                       fi_read_unlock(file);
15586 +       } else
15587 +               h_file = ERR_PTR(err);
15588 +
15589 +       return h_file;
15590 +}
15591 +
15592 +static void au_read_post(struct inode *inode, struct file *h_file)
15593 +{
15594 +       /* update without lock, I don't think it a problem */
15595 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15596 +       fput(h_file);
15597 +}
15598 +
15599 +struct au_write_pre {
15600 +       /* input */
15601 +       unsigned int lsc;
15602 +
15603 +       /* output */
15604 +       blkcnt_t blks;
15605 +       aufs_bindex_t btop;
15606 +};
15607 +
15608 +/*
15609 + * return with iinfo is write-locked
15610 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15611 + * end
15612 + */
15613 +static struct file *au_write_pre(struct file *file, int do_ready,
15614 +                                struct au_write_pre *wpre)
15615 +{
15616 +       struct file *h_file;
15617 +       struct dentry *dentry;
15618 +       int err;
15619 +       unsigned int lsc;
15620 +       struct au_pin pin;
15621 +
15622 +       lsc = 0;
15623 +       if (wpre)
15624 +               lsc = wpre->lsc;
15625 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15626 +       h_file = ERR_PTR(err);
15627 +       if (unlikely(err))
15628 +               goto out;
15629 +
15630 +       dentry = file->f_path.dentry;
15631 +       if (do_ready) {
15632 +               err = au_ready_to_write(file, -1, &pin);
15633 +               if (unlikely(err)) {
15634 +                       h_file = ERR_PTR(err);
15635 +                       di_write_unlock(dentry);
15636 +                       goto out_fi;
15637 +               }
15638 +       }
15639 +
15640 +       di_downgrade_lock(dentry, /*flags*/0);
15641 +       if (wpre)
15642 +               wpre->btop = au_fbtop(file);
15643 +       h_file = au_hf_top(file);
15644 +       get_file(h_file);
15645 +       if (wpre)
15646 +               wpre->blks = file_inode(h_file)->i_blocks;
15647 +       if (do_ready)
15648 +               au_unpin(&pin);
15649 +       di_read_unlock(dentry, /*flags*/0);
15650 +
15651 +out_fi:
15652 +       fi_write_unlock(file);
15653 +out:
15654 +       return h_file;
15655 +}
15656 +
15657 +static void au_write_post(struct inode *inode, struct file *h_file,
15658 +                         struct au_write_pre *wpre, ssize_t written)
15659 +{
15660 +       struct inode *h_inode;
15661 +
15662 +       au_cpup_attr_timesizes(inode);
15663 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15664 +       h_inode = file_inode(h_file);
15665 +       inode->i_mode = h_inode->i_mode;
15666 +       ii_write_unlock(inode);
15667 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15668 +       if (written > 0)
15669 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15670 +                             /*force*/h_inode->i_blocks > wpre->blks);
15671 +       fput(h_file);
15672 +}
15673 +
15674 +/*
15675 + * todo: very ugly
15676 + * it locks both of i_mutex and si_rwsem for read in safe.
15677 + * if the plink maintenance mode continues forever (that is the problem),
15678 + * may loop forever.
15679 + */
15680 +static void au_mtx_and_read_lock(struct inode *inode)
15681 +{
15682 +       int err;
15683 +       struct super_block *sb = inode->i_sb;
15684 +
15685 +       while (1) {
15686 +               inode_lock(inode);
15687 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15688 +               if (!err)
15689 +                       break;
15690 +               inode_unlock(inode);
15691 +               si_read_lock(sb, AuLock_NOPLMW);
15692 +               si_read_unlock(sb);
15693 +       }
15694 +}
15695 +
15696 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15697 +                         struct iov_iter *iov_iter)
15698 +{
15699 +       ssize_t err;
15700 +       struct file *file;
15701 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15702 +
15703 +       err = security_file_permission(h_file, rw);
15704 +       if (unlikely(err))
15705 +               goto out;
15706 +
15707 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15708 +       iter = NULL;
15709 +       if (rw == MAY_READ)
15710 +               iter = h_file->f_op->read_iter;
15711 +       else if (rw == MAY_WRITE)
15712 +               iter = h_file->f_op->write_iter;
15713 +
15714 +       file = kio->ki_filp;
15715 +       kio->ki_filp = h_file;
15716 +       if (iter) {
15717 +               lockdep_off();
15718 +               err = iter(kio, iov_iter);
15719 +               lockdep_on();
15720 +       } else
15721 +               /* currently there is no such fs */
15722 +               WARN_ON_ONCE(1);
15723 +       kio->ki_filp = file;
15724 +
15725 +out:
15726 +       return err;
15727 +}
15728 +
15729 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15730 +{
15731 +       ssize_t err;
15732 +       struct file *file, *h_file;
15733 +       struct inode *inode;
15734 +       struct super_block *sb;
15735 +
15736 +       file = kio->ki_filp;
15737 +       inode = file_inode(file);
15738 +       sb = inode->i_sb;
15739 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15740 +
15741 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15742 +       err = PTR_ERR(h_file);
15743 +       if (IS_ERR(h_file))
15744 +               goto out;
15745 +
15746 +       if (au_test_loopback_kthread()) {
15747 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15748 +               if (file->f_mapping != h_file->f_mapping) {
15749 +                       file->f_mapping = h_file->f_mapping;
15750 +                       smp_mb(); /* unnecessary? */
15751 +               }
15752 +       }
15753 +       fi_read_unlock(file);
15754 +
15755 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15756 +       /* todo: necessary? */
15757 +       /* file->f_ra = h_file->f_ra; */
15758 +       au_read_post(inode, h_file);
15759 +
15760 +out:
15761 +       si_read_unlock(sb);
15762 +       return err;
15763 +}
15764 +
15765 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15766 +{
15767 +       ssize_t err;
15768 +       struct au_write_pre wpre;
15769 +       struct inode *inode;
15770 +       struct file *file, *h_file;
15771 +
15772 +       file = kio->ki_filp;
15773 +       inode = file_inode(file);
15774 +       au_mtx_and_read_lock(inode);
15775 +
15776 +       wpre.lsc = 0;
15777 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15778 +       err = PTR_ERR(h_file);
15779 +       if (IS_ERR(h_file))
15780 +               goto out;
15781 +
15782 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15783 +       au_write_post(inode, h_file, &wpre, err);
15784 +
15785 +out:
15786 +       si_read_unlock(inode->i_sb);
15787 +       inode_unlock(inode);
15788 +       return err;
15789 +}
15790 +
15791 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15792 +                               struct pipe_inode_info *pipe, size_t len,
15793 +                               unsigned int flags)
15794 +{
15795 +       ssize_t err;
15796 +       struct file *h_file;
15797 +       struct inode *inode;
15798 +       struct super_block *sb;
15799 +
15800 +       inode = file_inode(file);
15801 +       sb = inode->i_sb;
15802 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15803 +
15804 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15805 +       err = PTR_ERR(h_file);
15806 +       if (IS_ERR(h_file))
15807 +               goto out;
15808 +
15809 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15810 +       /* todo: necessary? */
15811 +       /* file->f_ra = h_file->f_ra; */
15812 +       au_read_post(inode, h_file);
15813 +
15814 +out:
15815 +       si_read_unlock(sb);
15816 +       return err;
15817 +}
15818 +
15819 +static ssize_t
15820 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15821 +                 size_t len, unsigned int flags)
15822 +{
15823 +       ssize_t err;
15824 +       struct au_write_pre wpre;
15825 +       struct inode *inode;
15826 +       struct file *h_file;
15827 +
15828 +       inode = file_inode(file);
15829 +       au_mtx_and_read_lock(inode);
15830 +
15831 +       wpre.lsc = 0;
15832 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15833 +       err = PTR_ERR(h_file);
15834 +       if (IS_ERR(h_file))
15835 +               goto out;
15836 +
15837 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15838 +       au_write_post(inode, h_file, &wpre, err);
15839 +
15840 +out:
15841 +       si_read_unlock(inode->i_sb);
15842 +       inode_unlock(inode);
15843 +       return err;
15844 +}
15845 +
15846 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15847 +                          loff_t len)
15848 +{
15849 +       long err;
15850 +       struct au_write_pre wpre;
15851 +       struct inode *inode;
15852 +       struct file *h_file;
15853 +
15854 +       inode = file_inode(file);
15855 +       au_mtx_and_read_lock(inode);
15856 +
15857 +       wpre.lsc = 0;
15858 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15859 +       err = PTR_ERR(h_file);
15860 +       if (IS_ERR(h_file))
15861 +               goto out;
15862 +
15863 +       lockdep_off();
15864 +       err = vfs_fallocate(h_file, mode, offset, len);
15865 +       lockdep_on();
15866 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15867 +
15868 +out:
15869 +       si_read_unlock(inode->i_sb);
15870 +       inode_unlock(inode);
15871 +       return err;
15872 +}
15873 +
15874 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15875 +                                   struct file *dst, loff_t dst_pos,
15876 +                                   size_t len, unsigned int flags)
15877 +{
15878 +       ssize_t err;
15879 +       struct au_write_pre wpre;
15880 +       enum { SRC, DST };
15881 +       struct {
15882 +               struct inode *inode;
15883 +               struct file *h_file;
15884 +               struct super_block *h_sb;
15885 +       } a[2];
15886 +#define a_src  a[SRC]
15887 +#define a_dst  a[DST]
15888 +
15889 +       err = -EINVAL;
15890 +       a_src.inode = file_inode(src);
15891 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15892 +               goto out;
15893 +       a_dst.inode = file_inode(dst);
15894 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15895 +               goto out;
15896 +
15897 +       au_mtx_and_read_lock(a_dst.inode);
15898 +       /*
15899 +        * in order to match the order in di_write_lock2_{child,parent}(),
15900 +        * use f_path.dentry for this comparison.
15901 +        */
15902 +       if (src->f_path.dentry < dst->f_path.dentry) {
15903 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15904 +               err = PTR_ERR(a_src.h_file);
15905 +               if (IS_ERR(a_src.h_file))
15906 +                       goto out_si;
15907 +
15908 +               wpre.lsc = AuLsc_FI_2;
15909 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15910 +               err = PTR_ERR(a_dst.h_file);
15911 +               if (IS_ERR(a_dst.h_file)) {
15912 +                       au_read_post(a_src.inode, a_src.h_file);
15913 +                       goto out_si;
15914 +               }
15915 +       } else {
15916 +               wpre.lsc = AuLsc_FI_1;
15917 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15918 +               err = PTR_ERR(a_dst.h_file);
15919 +               if (IS_ERR(a_dst.h_file))
15920 +                       goto out_si;
15921 +
15922 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15923 +               err = PTR_ERR(a_src.h_file);
15924 +               if (IS_ERR(a_src.h_file)) {
15925 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15926 +                                     /*written*/0);
15927 +                       goto out_si;
15928 +               }
15929 +       }
15930 +
15931 +       err = -EXDEV;
15932 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15933 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15934 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15935 +               AuDbgFile(src);
15936 +               AuDbgFile(dst);
15937 +               goto out_file;
15938 +       }
15939 +
15940 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15941 +                                   dst_pos, len, flags);
15942 +
15943 +out_file:
15944 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15945 +       fi_read_unlock(src);
15946 +       au_read_post(a_src.inode, a_src.h_file);
15947 +out_si:
15948 +       si_read_unlock(a_dst.inode->i_sb);
15949 +       inode_unlock(a_dst.inode);
15950 +out:
15951 +       return err;
15952 +#undef a_src
15953 +#undef a_dst
15954 +}
15955 +
15956 +/* ---------------------------------------------------------------------- */
15957 +
15958 +/*
15959 + * The locking order around current->mmap_sem.
15960 + * - in most and regular cases
15961 + *   file I/O syscall -- aufs_read() or something
15962 + *     -- si_rwsem for read -- mmap_sem
15963 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15964 + * - in mmap case
15965 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15966 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15967 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15968 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15969 + * It means that when aufs acquires si_rwsem for write, the process should never
15970 + * acquire mmap_sem.
15971 + *
15972 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15973 + * problem either since any directory is not able to be mmap-ed.
15974 + * The similar scenario is applied to aufs_readlink() too.
15975 + */
15976 +
15977 +#if 0 /* stop calling security_file_mmap() */
15978 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15979 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15980 +
15981 +static unsigned long au_arch_prot_conv(unsigned long flags)
15982 +{
15983 +       /* currently ppc64 only */
15984 +#ifdef CONFIG_PPC64
15985 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15986 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15987 +       return AuConv_VM_PROT(flags, SAO);
15988 +#else
15989 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15990 +       return 0;
15991 +#endif
15992 +}
15993 +
15994 +static unsigned long au_prot_conv(unsigned long flags)
15995 +{
15996 +       return AuConv_VM_PROT(flags, READ)
15997 +               | AuConv_VM_PROT(flags, WRITE)
15998 +               | AuConv_VM_PROT(flags, EXEC)
15999 +               | au_arch_prot_conv(flags);
16000 +}
16001 +
16002 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16003 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16004 +
16005 +static unsigned long au_flag_conv(unsigned long flags)
16006 +{
16007 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16008 +               | AuConv_VM_MAP(flags, DENYWRITE)
16009 +               | AuConv_VM_MAP(flags, LOCKED);
16010 +}
16011 +#endif
16012 +
16013 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16014 +{
16015 +       int err;
16016 +       const unsigned char wlock
16017 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16018 +       struct super_block *sb;
16019 +       struct file *h_file;
16020 +       struct inode *inode;
16021 +
16022 +       AuDbgVmRegion(file, vma);
16023 +
16024 +       inode = file_inode(file);
16025 +       sb = inode->i_sb;
16026 +       lockdep_off();
16027 +       si_read_lock(sb, AuLock_NOPLMW);
16028 +
16029 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16030 +       lockdep_on();
16031 +       err = PTR_ERR(h_file);
16032 +       if (IS_ERR(h_file))
16033 +               goto out;
16034 +
16035 +       err = 0;
16036 +       au_set_mmapped(file);
16037 +       au_vm_file_reset(vma, h_file);
16038 +       /*
16039 +        * we cannot call security_mmap_file() here since it may acquire
16040 +        * mmap_sem or i_mutex.
16041 +        *
16042 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16043 +        *                       au_flag_conv(vma->vm_flags));
16044 +        */
16045 +       if (!err)
16046 +               err = call_mmap(h_file, vma);
16047 +       if (!err) {
16048 +               au_vm_prfile_set(vma, file);
16049 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16050 +               goto out_fput; /* success */
16051 +       }
16052 +       au_unset_mmapped(file);
16053 +       au_vm_file_reset(vma, file);
16054 +
16055 +out_fput:
16056 +       lockdep_off();
16057 +       ii_write_unlock(inode);
16058 +       lockdep_on();
16059 +       fput(h_file);
16060 +out:
16061 +       lockdep_off();
16062 +       si_read_unlock(sb);
16063 +       lockdep_on();
16064 +       AuTraceErr(err);
16065 +       return err;
16066 +}
16067 +
16068 +/* ---------------------------------------------------------------------- */
16069 +
16070 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16071 +                            int datasync)
16072 +{
16073 +       int err;
16074 +       struct au_write_pre wpre;
16075 +       struct inode *inode;
16076 +       struct file *h_file;
16077 +
16078 +       err = 0; /* -EBADF; */ /* posix? */
16079 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16080 +               goto out;
16081 +
16082 +       inode = file_inode(file);
16083 +       au_mtx_and_read_lock(inode);
16084 +
16085 +       wpre.lsc = 0;
16086 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16087 +       err = PTR_ERR(h_file);
16088 +       if (IS_ERR(h_file))
16089 +               goto out_unlock;
16090 +
16091 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16092 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16093 +
16094 +out_unlock:
16095 +       si_read_unlock(inode->i_sb);
16096 +       inode_unlock(inode);
16097 +out:
16098 +       return err;
16099 +}
16100 +
16101 +static int aufs_fasync(int fd, struct file *file, int flag)
16102 +{
16103 +       int err;
16104 +       struct file *h_file;
16105 +       struct super_block *sb;
16106 +
16107 +       sb = file->f_path.dentry->d_sb;
16108 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16109 +
16110 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16111 +       err = PTR_ERR(h_file);
16112 +       if (IS_ERR(h_file))
16113 +               goto out;
16114 +
16115 +       if (h_file->f_op->fasync)
16116 +               err = h_file->f_op->fasync(fd, h_file, flag);
16117 +       fput(h_file); /* instead of au_read_post() */
16118 +
16119 +out:
16120 +       si_read_unlock(sb);
16121 +       return err;
16122 +}
16123 +
16124 +static int aufs_setfl(struct file *file, unsigned long arg)
16125 +{
16126 +       int err;
16127 +       struct file *h_file;
16128 +       struct super_block *sb;
16129 +
16130 +       sb = file->f_path.dentry->d_sb;
16131 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16132 +
16133 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16134 +       err = PTR_ERR(h_file);
16135 +       if (IS_ERR(h_file))
16136 +               goto out;
16137 +
16138 +       /* stop calling h_file->fasync */
16139 +       arg |= vfsub_file_flags(file) & FASYNC;
16140 +       err = setfl(/*unused fd*/-1, h_file, arg);
16141 +       fput(h_file); /* instead of au_read_post() */
16142 +
16143 +out:
16144 +       si_read_unlock(sb);
16145 +       return err;
16146 +}
16147 +
16148 +/* ---------------------------------------------------------------------- */
16149 +
16150 +/* no one supports this operation, currently */
16151 +#if 0 /* reserved for future use */
16152 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16153 +                            size_t len, loff_t *pos, int more)
16154 +{
16155 +}
16156 +#endif
16157 +
16158 +/* ---------------------------------------------------------------------- */
16159 +
16160 +const struct file_operations aufs_file_fop = {
16161 +       .owner          = THIS_MODULE,
16162 +
16163 +       .llseek         = default_llseek,
16164 +
16165 +       .read_iter      = aufs_read_iter,
16166 +       .write_iter     = aufs_write_iter,
16167 +
16168 +#ifdef CONFIG_AUFS_POLL
16169 +       .poll           = aufs_poll,
16170 +#endif
16171 +       .unlocked_ioctl = aufs_ioctl_nondir,
16172 +#ifdef CONFIG_COMPAT
16173 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16174 +#endif
16175 +       .mmap           = aufs_mmap,
16176 +       .open           = aufs_open_nondir,
16177 +       .flush          = aufs_flush_nondir,
16178 +       .release        = aufs_release_nondir,
16179 +       .fsync          = aufs_fsync_nondir,
16180 +       .fasync         = aufs_fasync,
16181 +       /* .sendpage    = aufs_sendpage, */
16182 +       .setfl          = aufs_setfl,
16183 +       .splice_write   = aufs_splice_write,
16184 +       .splice_read    = aufs_splice_read,
16185 +#if 0 /* reserved for future use */
16186 +       .aio_splice_write = aufs_aio_splice_write,
16187 +       .aio_splice_read  = aufs_aio_splice_read,
16188 +#endif
16189 +       .fallocate      = aufs_fallocate,
16190 +       .copy_file_range = aufs_copy_file_range
16191 +};
16192 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16193 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16194 +++ linux/fs/aufs/fstype.h      2020-01-27 10:57:18.172204883 +0100
16195 @@ -0,0 +1,401 @@
16196 +/* SPDX-License-Identifier: GPL-2.0 */
16197 +/*
16198 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16199 + *
16200 + * This program, aufs is free software; you can redistribute it and/or modify
16201 + * it under the terms of the GNU General Public License as published by
16202 + * the Free Software Foundation; either version 2 of the License, or
16203 + * (at your option) any later version.
16204 + *
16205 + * This program is distributed in the hope that it will be useful,
16206 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16207 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16208 + * GNU General Public License for more details.
16209 + *
16210 + * You should have received a copy of the GNU General Public License
16211 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16212 + */
16213 +
16214 +/*
16215 + * judging filesystem type
16216 + */
16217 +
16218 +#ifndef __AUFS_FSTYPE_H__
16219 +#define __AUFS_FSTYPE_H__
16220 +
16221 +#ifdef __KERNEL__
16222 +
16223 +#include <linux/fs.h>
16224 +#include <linux/magic.h>
16225 +#include <linux/nfs_fs.h>
16226 +#include <linux/romfs_fs.h>
16227 +
16228 +static inline int au_test_aufs(struct super_block *sb)
16229 +{
16230 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16231 +}
16232 +
16233 +static inline const char *au_sbtype(struct super_block *sb)
16234 +{
16235 +       return sb->s_type->name;
16236 +}
16237 +
16238 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16239 +{
16240 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16241 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16242 +#else
16243 +       return 0;
16244 +#endif
16245 +}
16246 +
16247 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16248 +{
16249 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16250 +       return sb->s_magic == ROMFS_MAGIC;
16251 +#else
16252 +       return 0;
16253 +#endif
16254 +}
16255 +
16256 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16257 +{
16258 +#if IS_ENABLED(CONFIG_CRAMFS)
16259 +       return sb->s_magic == CRAMFS_MAGIC;
16260 +#endif
16261 +       return 0;
16262 +}
16263 +
16264 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16265 +{
16266 +#if IS_ENABLED(CONFIG_NFS_FS)
16267 +       return sb->s_magic == NFS_SUPER_MAGIC;
16268 +#else
16269 +       return 0;
16270 +#endif
16271 +}
16272 +
16273 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16274 +{
16275 +#if IS_ENABLED(CONFIG_FUSE_FS)
16276 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16277 +#else
16278 +       return 0;
16279 +#endif
16280 +}
16281 +
16282 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16283 +{
16284 +#if IS_ENABLED(CONFIG_XFS_FS)
16285 +       return sb->s_magic == XFS_SB_MAGIC;
16286 +#else
16287 +       return 0;
16288 +#endif
16289 +}
16290 +
16291 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16292 +{
16293 +#ifdef CONFIG_TMPFS
16294 +       return sb->s_magic == TMPFS_MAGIC;
16295 +#else
16296 +       return 0;
16297 +#endif
16298 +}
16299 +
16300 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16301 +{
16302 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16303 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16304 +#else
16305 +       return 0;
16306 +#endif
16307 +}
16308 +
16309 +static inline int au_test_ramfs(struct super_block *sb)
16310 +{
16311 +       return sb->s_magic == RAMFS_MAGIC;
16312 +}
16313 +
16314 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16315 +{
16316 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16317 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16318 +#else
16319 +       return 0;
16320 +#endif
16321 +}
16322 +
16323 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16324 +{
16325 +#ifdef CONFIG_PROC_FS
16326 +       return sb->s_magic == PROC_SUPER_MAGIC;
16327 +#else
16328 +       return 0;
16329 +#endif
16330 +}
16331 +
16332 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16333 +{
16334 +#ifdef CONFIG_SYSFS
16335 +       return sb->s_magic == SYSFS_MAGIC;
16336 +#else
16337 +       return 0;
16338 +#endif
16339 +}
16340 +
16341 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16342 +{
16343 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16344 +       return sb->s_magic == CONFIGFS_MAGIC;
16345 +#else
16346 +       return 0;
16347 +#endif
16348 +}
16349 +
16350 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16351 +{
16352 +#if IS_ENABLED(CONFIG_MINIX_FS)
16353 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16354 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16355 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16356 +               || sb->s_magic == MINIX_SUPER_MAGIC
16357 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16358 +#else
16359 +       return 0;
16360 +#endif
16361 +}
16362 +
16363 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16364 +{
16365 +#if IS_ENABLED(CONFIG_FAT_FS)
16366 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16367 +#else
16368 +       return 0;
16369 +#endif
16370 +}
16371 +
16372 +static inline int au_test_msdos(struct super_block *sb)
16373 +{
16374 +       return au_test_fat(sb);
16375 +}
16376 +
16377 +static inline int au_test_vfat(struct super_block *sb)
16378 +{
16379 +       return au_test_fat(sb);
16380 +}
16381 +
16382 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16383 +{
16384 +#ifdef CONFIG_SECURITYFS
16385 +       return sb->s_magic == SECURITYFS_MAGIC;
16386 +#else
16387 +       return 0;
16388 +#endif
16389 +}
16390 +
16391 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16392 +{
16393 +#if IS_ENABLED(CONFIG_SQUASHFS)
16394 +       return sb->s_magic == SQUASHFS_MAGIC;
16395 +#else
16396 +       return 0;
16397 +#endif
16398 +}
16399 +
16400 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16401 +{
16402 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16403 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16404 +#else
16405 +       return 0;
16406 +#endif
16407 +}
16408 +
16409 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16410 +{
16411 +#if IS_ENABLED(CONFIG_XENFS)
16412 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16413 +#else
16414 +       return 0;
16415 +#endif
16416 +}
16417 +
16418 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16419 +{
16420 +#ifdef CONFIG_DEBUG_FS
16421 +       return sb->s_magic == DEBUGFS_MAGIC;
16422 +#else
16423 +       return 0;
16424 +#endif
16425 +}
16426 +
16427 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16428 +{
16429 +#if IS_ENABLED(CONFIG_NILFS)
16430 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16431 +#else
16432 +       return 0;
16433 +#endif
16434 +}
16435 +
16436 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16437 +{
16438 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16439 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16440 +#else
16441 +       return 0;
16442 +#endif
16443 +}
16444 +
16445 +/* ---------------------------------------------------------------------- */
16446 +/*
16447 + * they can't be an aufs branch.
16448 + */
16449 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16450 +{
16451 +       return
16452 +#ifndef CONFIG_AUFS_BR_RAMFS
16453 +               au_test_ramfs(sb) ||
16454 +#endif
16455 +               au_test_procfs(sb)
16456 +               || au_test_sysfs(sb)
16457 +               || au_test_configfs(sb)
16458 +               || au_test_debugfs(sb)
16459 +               || au_test_securityfs(sb)
16460 +               || au_test_xenfs(sb)
16461 +               || au_test_ecryptfs(sb)
16462 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16463 +               || au_test_aufs(sb); /* will be supported in next version */
16464 +}
16465 +
16466 +static inline int au_test_fs_remote(struct super_block *sb)
16467 +{
16468 +       return !au_test_tmpfs(sb)
16469 +#ifdef CONFIG_AUFS_BR_RAMFS
16470 +               && !au_test_ramfs(sb)
16471 +#endif
16472 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16473 +}
16474 +
16475 +/* ---------------------------------------------------------------------- */
16476 +
16477 +/*
16478 + * Note: these functions (below) are created after reading ->getattr() in all
16479 + * filesystems under linux/fs. it means we have to do so in every update...
16480 + */
16481 +
16482 +/*
16483 + * some filesystems require getattr to refresh the inode attributes before
16484 + * referencing.
16485 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16486 + * and leave the work for d_revalidate()
16487 + */
16488 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16489 +{
16490 +       return au_test_nfs(sb)
16491 +               || au_test_fuse(sb)
16492 +               /* || au_test_btrfs(sb) */      /* untested */
16493 +               ;
16494 +}
16495 +
16496 +/*
16497 + * filesystems which don't maintain i_size or i_blocks.
16498 + */
16499 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16500 +{
16501 +       return au_test_xfs(sb)
16502 +               || au_test_btrfs(sb)
16503 +               || au_test_ubifs(sb)
16504 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16505 +               /* || au_test_minix(sb) */      /* untested */
16506 +               ;
16507 +}
16508 +
16509 +/*
16510 + * filesystems which don't store the correct value in some of their inode
16511 + * attributes.
16512 + */
16513 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16514 +{
16515 +       return au_test_fs_bad_iattr_size(sb)
16516 +               || au_test_fat(sb)
16517 +               || au_test_msdos(sb)
16518 +               || au_test_vfat(sb);
16519 +}
16520 +
16521 +/* they don't check i_nlink in link(2) */
16522 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16523 +{
16524 +       return au_test_tmpfs(sb)
16525 +#ifdef CONFIG_AUFS_BR_RAMFS
16526 +               || au_test_ramfs(sb)
16527 +#endif
16528 +               || au_test_ubifs(sb)
16529 +               || au_test_hfsplus(sb);
16530 +}
16531 +
16532 +/*
16533 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16534 + */
16535 +static inline int au_test_fs_notime(struct super_block *sb)
16536 +{
16537 +       return au_test_nfs(sb)
16538 +               || au_test_fuse(sb)
16539 +               || au_test_ubifs(sb)
16540 +               ;
16541 +}
16542 +
16543 +/* temporary support for i#1 in cramfs */
16544 +static inline int au_test_fs_unique_ino(struct inode *inode)
16545 +{
16546 +       if (au_test_cramfs(inode->i_sb))
16547 +               return inode->i_ino != 1;
16548 +       return 1;
16549 +}
16550 +
16551 +/* ---------------------------------------------------------------------- */
16552 +
16553 +/*
16554 + * the filesystem where the xino files placed must support i/o after unlink and
16555 + * maintain i_size and i_blocks.
16556 + */
16557 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16558 +{
16559 +       return au_test_fs_remote(sb)
16560 +               || au_test_fs_bad_iattr_size(sb)
16561 +               /* don't want unnecessary work for xino */
16562 +               || au_test_aufs(sb)
16563 +               || au_test_ecryptfs(sb)
16564 +               || au_test_nilfs(sb);
16565 +}
16566 +
16567 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16568 +{
16569 +       return au_test_tmpfs(sb)
16570 +               || au_test_ramfs(sb);
16571 +}
16572 +
16573 +/*
16574 + * test if the @sb is real-readonly.
16575 + */
16576 +static inline int au_test_fs_rr(struct super_block *sb)
16577 +{
16578 +       return au_test_squashfs(sb)
16579 +               || au_test_iso9660(sb)
16580 +               || au_test_cramfs(sb)
16581 +               || au_test_romfs(sb);
16582 +}
16583 +
16584 +/*
16585 + * test if the @inode is nfs with 'noacl' option
16586 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16587 + */
16588 +static inline int au_test_nfs_noacl(struct inode *inode)
16589 +{
16590 +       return au_test_nfs(inode->i_sb)
16591 +               /* && IS_POSIXACL(inode) */
16592 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16593 +}
16594 +
16595 +#endif /* __KERNEL__ */
16596 +#endif /* __AUFS_FSTYPE_H__ */
16597 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16598 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16599 +++ linux/fs/aufs/hbl.h 2020-01-27 10:57:18.172204883 +0100
16600 @@ -0,0 +1,65 @@
16601 +/* SPDX-License-Identifier: GPL-2.0 */
16602 +/*
16603 + * Copyright (C) 2017-2020 Junjiro R. Okajima
16604 + *
16605 + * This program, aufs is free software; you can redistribute it and/or modify
16606 + * it under the terms of the GNU General Public License as published by
16607 + * the Free Software Foundation; either version 2 of the License, or
16608 + * (at your option) any later version.
16609 + *
16610 + * This program is distributed in the hope that it will be useful,
16611 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16612 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16613 + * GNU General Public License for more details.
16614 + *
16615 + * You should have received a copy of the GNU General Public License
16616 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16617 + */
16618 +
16619 +/*
16620 + * helpers for hlist_bl.h
16621 + */
16622 +
16623 +#ifndef __AUFS_HBL_H__
16624 +#define __AUFS_HBL_H__
16625 +
16626 +#ifdef __KERNEL__
16627 +
16628 +#include <linux/list_bl.h>
16629 +
16630 +static inline void au_hbl_add(struct hlist_bl_node *node,
16631 +                             struct hlist_bl_head *hbl)
16632 +{
16633 +       hlist_bl_lock(hbl);
16634 +       hlist_bl_add_head(node, hbl);
16635 +       hlist_bl_unlock(hbl);
16636 +}
16637 +
16638 +static inline void au_hbl_del(struct hlist_bl_node *node,
16639 +                             struct hlist_bl_head *hbl)
16640 +{
16641 +       hlist_bl_lock(hbl);
16642 +       hlist_bl_del(node);
16643 +       hlist_bl_unlock(hbl);
16644 +}
16645 +
16646 +#define au_hbl_for_each(pos, head)                                     \
16647 +       for (pos = hlist_bl_first(head);                                \
16648 +            pos;                                                       \
16649 +            pos = pos->next)
16650 +
16651 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16652 +{
16653 +       unsigned long cnt;
16654 +       struct hlist_bl_node *pos;
16655 +
16656 +       cnt = 0;
16657 +       hlist_bl_lock(hbl);
16658 +       au_hbl_for_each(pos, hbl)
16659 +               cnt++;
16660 +       hlist_bl_unlock(hbl);
16661 +       return cnt;
16662 +}
16663 +
16664 +#endif /* __KERNEL__ */
16665 +#endif /* __AUFS_HBL_H__ */
16666 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16667 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16668 +++ linux/fs/aufs/hfsnotify.c   2020-12-15 14:10:58.918023162 +0100
16669 @@ -0,0 +1,288 @@
16670 +// SPDX-License-Identifier: GPL-2.0
16671 +/*
16672 + * Copyright (C) 2005-2020 Junjiro R. Okajima
16673 + *
16674 + * This program, aufs is free software; you can redistribute it and/or modify
16675 + * it under the terms of the GNU General Public License as published by
16676 + * the Free Software Foundation; either version 2 of the License, or
16677 + * (at your option) any later version.
16678 + *
16679 + * This program is distributed in the hope that it will be useful,
16680 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16681 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16682 + * GNU General Public License for more details.
16683 + *
16684 + * You should have received a copy of the GNU General Public License
16685 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16686 + */
16687 +
16688 +/*
16689 + * fsnotify for the lower directories
16690 + */
16691 +
16692 +#include "aufs.h"
16693 +
16694 +/* FS_IN_IGNORED is unnecessary */
16695 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16696 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16697 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16698 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16699 +
16700 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16701 +{
16702 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16703 +                                            hn_mark);
16704 +       /* AuDbg("here\n"); */
16705 +       au_cache_free_hnotify(hn);
16706 +       smp_mb__before_atomic(); /* for atomic64_dec */
16707 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16708 +               wake_up(&au_hfsn_wq);
16709 +}
16710 +
16711 +static int au_hfsn_alloc(struct au_hinode *hinode)
16712 +{
16713 +       int err;
16714 +       struct au_hnotify *hn;
16715 +       struct super_block *sb;
16716 +       struct au_branch *br;
16717 +       struct fsnotify_mark *mark;
16718 +       aufs_bindex_t bindex;
16719 +
16720 +       hn = hinode->hi_notify;
16721 +       sb = hn->hn_aufs_inode->i_sb;
16722 +       bindex = au_br_index(sb, hinode->hi_id);
16723 +       br = au_sbr(sb, bindex);
16724 +       AuDebugOn(!br->br_hfsn);
16725 +
16726 +       mark = &hn->hn_mark;
16727 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16728 +       mark->mask = AuHfsnMask;
16729 +       /*
16730 +        * by udba rename or rmdir, aufs assign a new inode to the known
16731 +        * h_inode, so specify 1 to allow dups.
16732 +        */
16733 +       lockdep_off();
16734 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16735 +       lockdep_on();
16736 +
16737 +       return err;
16738 +}
16739 +
16740 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16741 +{
16742 +       struct fsnotify_mark *mark;
16743 +       unsigned long long ull;
16744 +       struct fsnotify_group *group;
16745 +
16746 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16747 +       BUG_ON(!ull);
16748 +
16749 +       mark = &hn->hn_mark;
16750 +       spin_lock(&mark->lock);
16751 +       group = mark->group;
16752 +       fsnotify_get_group(group);
16753 +       spin_unlock(&mark->lock);
16754 +       lockdep_off();
16755 +       fsnotify_destroy_mark(mark, group);
16756 +       fsnotify_put_mark(mark);
16757 +       fsnotify_put_group(group);
16758 +       lockdep_on();
16759 +
16760 +       /* free hn by myself */
16761 +       return 0;
16762 +}
16763 +
16764 +/* ---------------------------------------------------------------------- */
16765 +
16766 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16767 +{
16768 +       struct fsnotify_mark *mark;
16769 +
16770 +       mark = &hinode->hi_notify->hn_mark;
16771 +       spin_lock(&mark->lock);
16772 +       if (do_set) {
16773 +               AuDebugOn(mark->mask & AuHfsnMask);
16774 +               mark->mask |= AuHfsnMask;
16775 +       } else {
16776 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16777 +               mark->mask &= ~AuHfsnMask;
16778 +       }
16779 +       spin_unlock(&mark->lock);
16780 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16781 +}
16782 +
16783 +/* ---------------------------------------------------------------------- */
16784 +
16785 +/* #define AuDbgHnotify */
16786 +#ifdef AuDbgHnotify
16787 +static char *au_hfsn_name(u32 mask)
16788 +{
16789 +#ifdef CONFIG_AUFS_DEBUG
16790 +#define test_ret(flag)                         \
16791 +       do {                                    \
16792 +               if (mask & flag)                \
16793 +                       return #flag;           \
16794 +       } while (0)
16795 +       test_ret(FS_ACCESS);
16796 +       test_ret(FS_MODIFY);
16797 +       test_ret(FS_ATTRIB);
16798 +       test_ret(FS_CLOSE_WRITE);
16799 +       test_ret(FS_CLOSE_NOWRITE);
16800 +       test_ret(FS_OPEN);
16801 +       test_ret(FS_MOVED_FROM);
16802 +       test_ret(FS_MOVED_TO);
16803 +       test_ret(FS_CREATE);
16804 +       test_ret(FS_DELETE);
16805 +       test_ret(FS_DELETE_SELF);
16806 +       test_ret(FS_MOVE_SELF);
16807 +       test_ret(FS_UNMOUNT);
16808 +       test_ret(FS_Q_OVERFLOW);
16809 +       test_ret(FS_IN_IGNORED);
16810 +       test_ret(FS_ISDIR);
16811 +       test_ret(FS_IN_ONESHOT);
16812 +       test_ret(FS_EVENT_ON_CHILD);
16813 +       return "";
16814 +#undef test_ret
16815 +#else
16816 +       return "??";
16817 +#endif
16818 +}
16819 +#endif
16820 +
16821 +/* ---------------------------------------------------------------------- */
16822 +
16823 +static void au_hfsn_free_group(struct fsnotify_group *group)
16824 +{
16825 +       struct au_br_hfsnotify *hfsn = group->private;
16826 +
16827 +       /* AuDbg("here\n"); */
16828 +       au_kfree_try_rcu(hfsn);
16829 +}
16830 +
16831 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16832 +                               u32 mask, const void *data, int data_type,
16833 +                               struct inode *dir,
16834 +                               const struct qstr *file_name, u32 cookie,
16835 +                               struct fsnotify_iter_info *iter_info)
16836 +{
16837 +       int err;
16838 +       struct au_hnotify *hnotify;
16839 +       struct inode *h_dir, *h_inode;
16840 +       struct fsnotify_mark *inode_mark;
16841 +
16842 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16843 +
16844 +       err = 0;
16845 +       /* if FS_UNMOUNT happens, there must be another bug */
16846 +       AuDebugOn(mask & FS_UNMOUNT);
16847 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16848 +               goto out;
16849 +
16850 +       h_dir = dir;
16851 +       h_inode = NULL;
16852 +#ifdef AuDbgHnotify
16853 +       au_debug_on();
16854 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16855 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16856 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16857 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16858 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16859 +               /* WARN_ON(1); */
16860 +       }
16861 +       au_debug_off();
16862 +#endif
16863 +
16864 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16865 +       AuDebugOn(!inode_mark);
16866 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16867 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16868 +
16869 +out:
16870 +       return err;
16871 +}
16872 +
16873 +static struct fsnotify_ops au_hfsn_ops = {
16874 +       .handle_event           = au_hfsn_handle_event,
16875 +       .free_group_priv        = au_hfsn_free_group,
16876 +       .free_mark              = au_hfsn_free_mark
16877 +};
16878 +
16879 +/* ---------------------------------------------------------------------- */
16880 +
16881 +static void au_hfsn_fin_br(struct au_branch *br)
16882 +{
16883 +       struct au_br_hfsnotify *hfsn;
16884 +
16885 +       hfsn = br->br_hfsn;
16886 +       if (hfsn) {
16887 +               lockdep_off();
16888 +               fsnotify_put_group(hfsn->hfsn_group);
16889 +               lockdep_on();
16890 +       }
16891 +}
16892 +
16893 +static int au_hfsn_init_br(struct au_branch *br, int perm)
16894 +{
16895 +       int err;
16896 +       struct fsnotify_group *group;
16897 +       struct au_br_hfsnotify *hfsn;
16898 +
16899 +       err = 0;
16900 +       br->br_hfsn = NULL;
16901 +       if (!au_br_hnotifyable(perm))
16902 +               goto out;
16903 +
16904 +       err = -ENOMEM;
16905 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16906 +       if (unlikely(!hfsn))
16907 +               goto out;
16908 +
16909 +       err = 0;
16910 +       group = fsnotify_alloc_group(&au_hfsn_ops);
16911 +       if (IS_ERR(group)) {
16912 +               err = PTR_ERR(group);
16913 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
16914 +               goto out_hfsn;
16915 +       }
16916 +
16917 +       group->private = hfsn;
16918 +       hfsn->hfsn_group = group;
16919 +       br->br_hfsn = hfsn;
16920 +       goto out; /* success */
16921 +
16922 +out_hfsn:
16923 +       au_kfree_try_rcu(hfsn);
16924 +out:
16925 +       return err;
16926 +}
16927 +
16928 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16929 +{
16930 +       int err;
16931 +
16932 +       err = 0;
16933 +       if (!br->br_hfsn)
16934 +               err = au_hfsn_init_br(br, perm);
16935 +
16936 +       return err;
16937 +}
16938 +
16939 +/* ---------------------------------------------------------------------- */
16940 +
16941 +static void au_hfsn_fin(void)
16942 +{
16943 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16944 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16945 +}
16946 +
16947 +const struct au_hnotify_op au_hnotify_op = {
16948 +       .ctl            = au_hfsn_ctl,
16949 +       .alloc          = au_hfsn_alloc,
16950 +       .free           = au_hfsn_free,
16951 +
16952 +       .fin            = au_hfsn_fin,
16953 +
16954 +       .reset_br       = au_hfsn_reset_br,
16955 +       .fin_br         = au_hfsn_fin_br,
16956 +       .init_br        = au_hfsn_init_br
16957 +};
16958 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16959 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
16960 +++ linux/fs/aufs/hfsplus.c     2020-01-27 10:57:18.172204883 +0100
16961 @@ -0,0 +1,60 @@
16962 +// SPDX-License-Identifier: GPL-2.0
16963 +/*
16964 + * Copyright (C) 2010-2020 Junjiro R. Okajima
16965 + *
16966 + * This program, aufs is free software; you can redistribute it and/or modify
16967 + * it under the terms of the GNU General Public License as published by
16968 + * the Free Software Foundation; either version 2 of the License, or
16969 + * (at your option) any later version.
16970 + *
16971 + * This program is distributed in the hope that it will be useful,
16972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16974 + * GNU General Public License for more details.
16975 + *
16976 + * You should have received a copy of the GNU General Public License
16977 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16978 + */
16979 +
16980 +/*
16981 + * special support for filesystems which acquires an inode mutex
16982 + * at final closing a file, eg, hfsplus.
16983 + *
16984 + * This trick is very simple and stupid, just to open the file before really
16985 + * necessary open to tell hfsplus that this is not the final closing.
16986 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
16987 + * and au_h_open_post() after releasing it.
16988 + */
16989 +
16990 +#include "aufs.h"
16991 +
16992 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
16993 +                          int force_wr)
16994 +{
16995 +       struct file *h_file;
16996 +       struct dentry *h_dentry;
16997 +
16998 +       h_dentry = au_h_dptr(dentry, bindex);
16999 +       AuDebugOn(!h_dentry);
17000 +       AuDebugOn(d_is_negative(h_dentry));
17001 +
17002 +       h_file = NULL;
17003 +       if (au_test_hfsplus(h_dentry->d_sb)
17004 +           && d_is_reg(h_dentry))
17005 +               h_file = au_h_open(dentry, bindex,
17006 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17007 +                                  /*file*/NULL, force_wr);
17008 +       return h_file;
17009 +}
17010 +
17011 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17012 +                   struct file *h_file)
17013 +{
17014 +       struct au_branch *br;
17015 +
17016 +       if (h_file) {
17017 +               fput(h_file);
17018 +               br = au_sbr(dentry->d_sb, bindex);
17019 +               au_lcnt_dec(&br->br_nfiles);
17020 +       }
17021 +}
17022 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17023 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17024 +++ linux/fs/aufs/hnotify.c     2020-01-27 10:57:18.172204883 +0100
17025 @@ -0,0 +1,715 @@
17026 +// SPDX-License-Identifier: GPL-2.0
17027 +/*
17028 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17029 + *
17030 + * This program, aufs is free software; you can redistribute it and/or modify
17031 + * it under the terms of the GNU General Public License as published by
17032 + * the Free Software Foundation; either version 2 of the License, or
17033 + * (at your option) any later version.
17034 + *
17035 + * This program is distributed in the hope that it will be useful,
17036 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17037 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17038 + * GNU General Public License for more details.
17039 + *
17040 + * You should have received a copy of the GNU General Public License
17041 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17042 + */
17043 +
17044 +/*
17045 + * abstraction to notify the direct changes on lower directories
17046 + */
17047 +
17048 +/* #include <linux/iversion.h> */
17049 +#include "aufs.h"
17050 +
17051 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17052 +{
17053 +       int err;
17054 +       struct au_hnotify *hn;
17055 +
17056 +       err = -ENOMEM;
17057 +       hn = au_cache_alloc_hnotify();
17058 +       if (hn) {
17059 +               hn->hn_aufs_inode = inode;
17060 +               hinode->hi_notify = hn;
17061 +               err = au_hnotify_op.alloc(hinode);
17062 +               AuTraceErr(err);
17063 +               if (unlikely(err)) {
17064 +                       hinode->hi_notify = NULL;
17065 +                       au_cache_free_hnotify(hn);
17066 +                       /*
17067 +                        * The upper dir was removed by udba, but the same named
17068 +                        * dir left. In this case, aufs assigns a new inode
17069 +                        * number and set the monitor again.
17070 +                        * For the lower dir, the old monitor is still left.
17071 +                        */
17072 +                       if (err == -EEXIST)
17073 +                               err = 0;
17074 +               }
17075 +       }
17076 +
17077 +       AuTraceErr(err);
17078 +       return err;
17079 +}
17080 +
17081 +void au_hn_free(struct au_hinode *hinode)
17082 +{
17083 +       struct au_hnotify *hn;
17084 +
17085 +       hn = hinode->hi_notify;
17086 +       if (hn) {
17087 +               hinode->hi_notify = NULL;
17088 +               if (au_hnotify_op.free(hinode, hn))
17089 +                       au_cache_free_hnotify(hn);
17090 +       }
17091 +}
17092 +
17093 +/* ---------------------------------------------------------------------- */
17094 +
17095 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17096 +{
17097 +       if (hinode->hi_notify)
17098 +               au_hnotify_op.ctl(hinode, do_set);
17099 +}
17100 +
17101 +void au_hn_reset(struct inode *inode, unsigned int flags)
17102 +{
17103 +       aufs_bindex_t bindex, bbot;
17104 +       struct inode *hi;
17105 +       struct dentry *iwhdentry;
17106 +
17107 +       bbot = au_ibbot(inode);
17108 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17109 +               hi = au_h_iptr(inode, bindex);
17110 +               if (!hi)
17111 +                       continue;
17112 +
17113 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17114 +               iwhdentry = au_hi_wh(inode, bindex);
17115 +               if (iwhdentry)
17116 +                       dget(iwhdentry);
17117 +               au_igrab(hi);
17118 +               au_set_h_iptr(inode, bindex, NULL, 0);
17119 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17120 +                             flags & ~AuHi_XINO);
17121 +               iput(hi);
17122 +               dput(iwhdentry);
17123 +               /* inode_unlock(hi); */
17124 +       }
17125 +}
17126 +
17127 +/* ---------------------------------------------------------------------- */
17128 +
17129 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17130 +{
17131 +       int err;
17132 +       aufs_bindex_t bindex, bbot, bfound, btop;
17133 +       struct inode *h_i;
17134 +
17135 +       err = 0;
17136 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17137 +               pr_warn("branch root dir was changed\n");
17138 +               goto out;
17139 +       }
17140 +
17141 +       bfound = -1;
17142 +       bbot = au_ibbot(inode);
17143 +       btop = au_ibtop(inode);
17144 +#if 0 /* reserved for future use */
17145 +       if (bindex == bbot) {
17146 +               /* keep this ino in rename case */
17147 +               goto out;
17148 +       }
17149 +#endif
17150 +       for (bindex = btop; bindex <= bbot; bindex++)
17151 +               if (au_h_iptr(inode, bindex) == h_inode) {
17152 +                       bfound = bindex;
17153 +                       break;
17154 +               }
17155 +       if (bfound < 0)
17156 +               goto out;
17157 +
17158 +       for (bindex = btop; bindex <= bbot; bindex++) {
17159 +               h_i = au_h_iptr(inode, bindex);
17160 +               if (!h_i)
17161 +                       continue;
17162 +
17163 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17164 +               /* ignore this error */
17165 +               /* bad action? */
17166 +       }
17167 +
17168 +       /* children inode number will be broken */
17169 +
17170 +out:
17171 +       AuTraceErr(err);
17172 +       return err;
17173 +}
17174 +
17175 +static int hn_gen_tree(struct dentry *dentry)
17176 +{
17177 +       int err, i, j, ndentry;
17178 +       struct au_dcsub_pages dpages;
17179 +       struct au_dpage *dpage;
17180 +       struct dentry **dentries;
17181 +
17182 +       err = au_dpages_init(&dpages, GFP_NOFS);
17183 +       if (unlikely(err))
17184 +               goto out;
17185 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17186 +       if (unlikely(err))
17187 +               goto out_dpages;
17188 +
17189 +       for (i = 0; i < dpages.ndpage; i++) {
17190 +               dpage = dpages.dpages + i;
17191 +               dentries = dpage->dentries;
17192 +               ndentry = dpage->ndentry;
17193 +               for (j = 0; j < ndentry; j++) {
17194 +                       struct dentry *d;
17195 +
17196 +                       d = dentries[j];
17197 +                       if (IS_ROOT(d))
17198 +                               continue;
17199 +
17200 +                       au_digen_dec(d);
17201 +                       if (d_really_is_positive(d))
17202 +                               /* todo: reset children xino?
17203 +                                  cached children only? */
17204 +                               au_iigen_dec(d_inode(d));
17205 +               }
17206 +       }
17207 +
17208 +out_dpages:
17209 +       au_dpages_free(&dpages);
17210 +out:
17211 +       return err;
17212 +}
17213 +
17214 +/*
17215 + * return 0 if processed.
17216 + */
17217 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17218 +                          const unsigned int isdir)
17219 +{
17220 +       int err;
17221 +       struct dentry *d;
17222 +       struct qstr *dname;
17223 +
17224 +       err = 1;
17225 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17226 +               pr_warn("branch root dir was changed\n");
17227 +               err = 0;
17228 +               goto out;
17229 +       }
17230 +
17231 +       if (!isdir) {
17232 +               AuDebugOn(!name);
17233 +               au_iigen_dec(inode);
17234 +               spin_lock(&inode->i_lock);
17235 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17236 +                       spin_lock(&d->d_lock);
17237 +                       dname = &d->d_name;
17238 +                       if (dname->len != nlen
17239 +                           && memcmp(dname->name, name, nlen)) {
17240 +                               spin_unlock(&d->d_lock);
17241 +                               continue;
17242 +                       }
17243 +                       err = 0;
17244 +                       au_digen_dec(d);
17245 +                       spin_unlock(&d->d_lock);
17246 +                       break;
17247 +               }
17248 +               spin_unlock(&inode->i_lock);
17249 +       } else {
17250 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17251 +               d = d_find_any_alias(inode);
17252 +               if (!d) {
17253 +                       au_iigen_dec(inode);
17254 +                       goto out;
17255 +               }
17256 +
17257 +               spin_lock(&d->d_lock);
17258 +               dname = &d->d_name;
17259 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17260 +                       spin_unlock(&d->d_lock);
17261 +                       err = hn_gen_tree(d);
17262 +                       spin_lock(&d->d_lock);
17263 +               }
17264 +               spin_unlock(&d->d_lock);
17265 +               dput(d);
17266 +       }
17267 +
17268 +out:
17269 +       AuTraceErr(err);
17270 +       return err;
17271 +}
17272 +
17273 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17274 +{
17275 +       int err;
17276 +
17277 +       if (IS_ROOT(dentry)) {
17278 +               pr_warn("branch root dir was changed\n");
17279 +               return 0;
17280 +       }
17281 +
17282 +       err = 0;
17283 +       if (!isdir) {
17284 +               au_digen_dec(dentry);
17285 +               if (d_really_is_positive(dentry))
17286 +                       au_iigen_dec(d_inode(dentry));
17287 +       } else {
17288 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17289 +               if (d_really_is_positive(dentry))
17290 +                       err = hn_gen_tree(dentry);
17291 +       }
17292 +
17293 +       AuTraceErr(err);
17294 +       return err;
17295 +}
17296 +
17297 +/* ---------------------------------------------------------------------- */
17298 +
17299 +/* hnotify job flags */
17300 +#define AuHnJob_XINO0          1
17301 +#define AuHnJob_GEN            (1 << 1)
17302 +#define AuHnJob_DIRENT         (1 << 2)
17303 +#define AuHnJob_ISDIR          (1 << 3)
17304 +#define AuHnJob_TRYXINO0       (1 << 4)
17305 +#define AuHnJob_MNTPNT         (1 << 5)
17306 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17307 +#define au_fset_hnjob(flags, name) \
17308 +       do { (flags) |= AuHnJob_##name; } while (0)
17309 +#define au_fclr_hnjob(flags, name) \
17310 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17311 +
17312 +enum {
17313 +       AuHn_CHILD,
17314 +       AuHn_PARENT,
17315 +       AuHnLast
17316 +};
17317 +
17318 +struct au_hnotify_args {
17319 +       struct inode *h_dir, *dir, *h_child_inode;
17320 +       u32 mask;
17321 +       unsigned int flags[AuHnLast];
17322 +       unsigned int h_child_nlen;
17323 +       char h_child_name[];
17324 +};
17325 +
17326 +struct hn_job_args {
17327 +       unsigned int flags;
17328 +       struct inode *inode, *h_inode, *dir, *h_dir;
17329 +       struct dentry *dentry;
17330 +       char *h_name;
17331 +       int h_nlen;
17332 +};
17333 +
17334 +static int hn_job(struct hn_job_args *a)
17335 +{
17336 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17337 +       int e;
17338 +
17339 +       /* reset xino */
17340 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17341 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17342 +
17343 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17344 +           && a->inode
17345 +           && a->h_inode) {
17346 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17347 +               if (!a->h_inode->i_nlink
17348 +                   && !(a->h_inode->i_state & I_LINKABLE))
17349 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17350 +               inode_unlock_shared(a->h_inode);
17351 +       }
17352 +
17353 +       /* make the generation obsolete */
17354 +       if (au_ftest_hnjob(a->flags, GEN)) {
17355 +               e = -1;
17356 +               if (a->inode)
17357 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17358 +                                             isdir);
17359 +               if (e && a->dentry)
17360 +                       hn_gen_by_name(a->dentry, isdir);
17361 +               /* ignore this error */
17362 +       }
17363 +
17364 +       /* make dir entries obsolete */
17365 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17366 +               struct au_vdir *vdir;
17367 +
17368 +               vdir = au_ivdir(a->inode);
17369 +               if (vdir)
17370 +                       vdir->vd_jiffy = 0;
17371 +               /* IMustLock(a->inode); */
17372 +               /* inode_inc_iversion(a->inode); */
17373 +       }
17374 +
17375 +       /* can do nothing but warn */
17376 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17377 +           && a->dentry
17378 +           && d_mountpoint(a->dentry))
17379 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17380 +
17381 +       return 0;
17382 +}
17383 +
17384 +/* ---------------------------------------------------------------------- */
17385 +
17386 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17387 +                                          struct inode *dir)
17388 +{
17389 +       struct dentry *dentry, *d, *parent;
17390 +       struct qstr *dname;
17391 +
17392 +       parent = d_find_any_alias(dir);
17393 +       if (!parent)
17394 +               return NULL;
17395 +
17396 +       dentry = NULL;
17397 +       spin_lock(&parent->d_lock);
17398 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17399 +               /* AuDbg("%pd\n", d); */
17400 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17401 +               dname = &d->d_name;
17402 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17403 +                       goto cont_unlock;
17404 +               if (au_di(d))
17405 +                       au_digen_dec(d);
17406 +               else
17407 +                       goto cont_unlock;
17408 +               if (au_dcount(d) > 0) {
17409 +                       dentry = dget_dlock(d);
17410 +                       spin_unlock(&d->d_lock);
17411 +                       break;
17412 +               }
17413 +
17414 +cont_unlock:
17415 +               spin_unlock(&d->d_lock);
17416 +       }
17417 +       spin_unlock(&parent->d_lock);
17418 +       dput(parent);
17419 +
17420 +       if (dentry)
17421 +               di_write_lock_child(dentry);
17422 +
17423 +       return dentry;
17424 +}
17425 +
17426 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17427 +                                        aufs_bindex_t bindex, ino_t h_ino)
17428 +{
17429 +       struct inode *inode;
17430 +       ino_t ino;
17431 +       int err;
17432 +
17433 +       inode = NULL;
17434 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17435 +       if (!err && ino)
17436 +               inode = ilookup(sb, ino);
17437 +       if (!inode)
17438 +               goto out;
17439 +
17440 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17441 +               pr_warn("wrong root branch\n");
17442 +               iput(inode);
17443 +               inode = NULL;
17444 +               goto out;
17445 +       }
17446 +
17447 +       ii_write_lock_child(inode);
17448 +
17449 +out:
17450 +       return inode;
17451 +}
17452 +
17453 +static void au_hn_bh(void *_args)
17454 +{
17455 +       struct au_hnotify_args *a = _args;
17456 +       struct super_block *sb;
17457 +       aufs_bindex_t bindex, bbot, bfound;
17458 +       unsigned char xino, try_iput;
17459 +       int err;
17460 +       struct inode *inode;
17461 +       ino_t h_ino;
17462 +       struct hn_job_args args;
17463 +       struct dentry *dentry;
17464 +       struct au_sbinfo *sbinfo;
17465 +
17466 +       AuDebugOn(!_args);
17467 +       AuDebugOn(!a->h_dir);
17468 +       AuDebugOn(!a->dir);
17469 +       AuDebugOn(!a->mask);
17470 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17471 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17472 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17473 +
17474 +       inode = NULL;
17475 +       dentry = NULL;
17476 +       /*
17477 +        * do not lock a->dir->i_mutex here
17478 +        * because of d_revalidate() may cause a deadlock.
17479 +        */
17480 +       sb = a->dir->i_sb;
17481 +       AuDebugOn(!sb);
17482 +       sbinfo = au_sbi(sb);
17483 +       AuDebugOn(!sbinfo);
17484 +       si_write_lock(sb, AuLock_NOPLMW);
17485 +
17486 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17487 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17488 +               case FS_MOVED_FROM:
17489 +               case FS_MOVED_TO:
17490 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17491 +                               "for the direct rename(2)\n");
17492 +               }
17493 +
17494 +       ii_read_lock_parent(a->dir);
17495 +       bfound = -1;
17496 +       bbot = au_ibbot(a->dir);
17497 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17498 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17499 +                       bfound = bindex;
17500 +                       break;
17501 +               }
17502 +       ii_read_unlock(a->dir);
17503 +       if (unlikely(bfound < 0))
17504 +               goto out;
17505 +
17506 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17507 +       h_ino = 0;
17508 +       if (a->h_child_inode)
17509 +               h_ino = a->h_child_inode->i_ino;
17510 +
17511 +       if (a->h_child_nlen
17512 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17513 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17514 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17515 +                                             a->dir);
17516 +       try_iput = 0;
17517 +       if (dentry && d_really_is_positive(dentry))
17518 +               inode = d_inode(dentry);
17519 +       if (xino && !inode && h_ino
17520 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17521 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17522 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17523 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17524 +               try_iput = 1;
17525 +       }
17526 +
17527 +       args.flags = a->flags[AuHn_CHILD];
17528 +       args.dentry = dentry;
17529 +       args.inode = inode;
17530 +       args.h_inode = a->h_child_inode;
17531 +       args.dir = a->dir;
17532 +       args.h_dir = a->h_dir;
17533 +       args.h_name = a->h_child_name;
17534 +       args.h_nlen = a->h_child_nlen;
17535 +       err = hn_job(&args);
17536 +       if (dentry) {
17537 +               if (au_di(dentry))
17538 +                       di_write_unlock(dentry);
17539 +               dput(dentry);
17540 +       }
17541 +       if (inode && try_iput) {
17542 +               ii_write_unlock(inode);
17543 +               iput(inode);
17544 +       }
17545 +
17546 +       ii_write_lock_parent(a->dir);
17547 +       args.flags = a->flags[AuHn_PARENT];
17548 +       args.dentry = NULL;
17549 +       args.inode = a->dir;
17550 +       args.h_inode = a->h_dir;
17551 +       args.dir = NULL;
17552 +       args.h_dir = NULL;
17553 +       args.h_name = NULL;
17554 +       args.h_nlen = 0;
17555 +       err = hn_job(&args);
17556 +       ii_write_unlock(a->dir);
17557 +
17558 +out:
17559 +       iput(a->h_child_inode);
17560 +       iput(a->h_dir);
17561 +       iput(a->dir);
17562 +       si_write_unlock(sb);
17563 +       au_nwt_done(&sbinfo->si_nowait);
17564 +       au_kfree_rcu(a);
17565 +}
17566 +
17567 +/* ---------------------------------------------------------------------- */
17568 +
17569 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17570 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17571 +{
17572 +       int err, len;
17573 +       unsigned int flags[AuHnLast], f;
17574 +       unsigned char isdir, isroot, wh;
17575 +       struct inode *dir;
17576 +       struct au_hnotify_args *args;
17577 +       char *p, *h_child_name;
17578 +
17579 +       err = 0;
17580 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17581 +       dir = igrab(hnotify->hn_aufs_inode);
17582 +       if (!dir)
17583 +               goto out;
17584 +
17585 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17586 +       wh = 0;
17587 +       h_child_name = (void *)h_child_qstr->name;
17588 +       len = h_child_qstr->len;
17589 +       if (h_child_name) {
17590 +               if (len > AUFS_WH_PFX_LEN
17591 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17592 +                       h_child_name += AUFS_WH_PFX_LEN;
17593 +                       len -= AUFS_WH_PFX_LEN;
17594 +                       wh = 1;
17595 +               }
17596 +       }
17597 +
17598 +       isdir = 0;
17599 +       if (h_child_inode)
17600 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17601 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17602 +       flags[AuHn_CHILD] = 0;
17603 +       if (isdir)
17604 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17605 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17606 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17607 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17608 +       case FS_MOVED_FROM:
17609 +       case FS_MOVED_TO:
17610 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17611 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17612 +               /*FALLTHROUGH*/
17613 +       case FS_CREATE:
17614 +               AuDebugOn(!h_child_name);
17615 +               break;
17616 +
17617 +       case FS_DELETE:
17618 +               /*
17619 +                * aufs never be able to get this child inode.
17620 +                * revalidation should be in d_revalidate()
17621 +                * by checking i_nlink, i_generation or d_unhashed().
17622 +                */
17623 +               AuDebugOn(!h_child_name);
17624 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17625 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17626 +               break;
17627 +
17628 +       default:
17629 +               AuDebugOn(1);
17630 +       }
17631 +
17632 +       if (wh)
17633 +               h_child_inode = NULL;
17634 +
17635 +       err = -ENOMEM;
17636 +       /* iput() and kfree() will be called in au_hnotify() */
17637 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17638 +       if (unlikely(!args)) {
17639 +               AuErr1("no memory\n");
17640 +               iput(dir);
17641 +               goto out;
17642 +       }
17643 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17644 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17645 +       args->mask = mask;
17646 +       args->dir = dir;
17647 +       args->h_dir = igrab(h_dir);
17648 +       if (h_child_inode)
17649 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17650 +       args->h_child_inode = h_child_inode;
17651 +       args->h_child_nlen = len;
17652 +       if (len) {
17653 +               p = (void *)args;
17654 +               p += sizeof(*args);
17655 +               memcpy(p, h_child_name, len);
17656 +               p[len] = 0;
17657 +       }
17658 +
17659 +       /* NFS fires the event for silly-renamed one from kworker */
17660 +       f = 0;
17661 +       if (!dir->i_nlink
17662 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17663 +               f = AuWkq_NEST;
17664 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17665 +       if (unlikely(err)) {
17666 +               pr_err("wkq %d\n", err);
17667 +               iput(args->h_child_inode);
17668 +               iput(args->h_dir);
17669 +               iput(args->dir);
17670 +               au_kfree_rcu(args);
17671 +       }
17672 +
17673 +out:
17674 +       return err;
17675 +}
17676 +
17677 +/* ---------------------------------------------------------------------- */
17678 +
17679 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17680 +{
17681 +       int err;
17682 +
17683 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17684 +
17685 +       err = 0;
17686 +       if (au_hnotify_op.reset_br)
17687 +               err = au_hnotify_op.reset_br(udba, br, perm);
17688 +
17689 +       return err;
17690 +}
17691 +
17692 +int au_hnotify_init_br(struct au_branch *br, int perm)
17693 +{
17694 +       int err;
17695 +
17696 +       err = 0;
17697 +       if (au_hnotify_op.init_br)
17698 +               err = au_hnotify_op.init_br(br, perm);
17699 +
17700 +       return err;
17701 +}
17702 +
17703 +void au_hnotify_fin_br(struct au_branch *br)
17704 +{
17705 +       if (au_hnotify_op.fin_br)
17706 +               au_hnotify_op.fin_br(br);
17707 +}
17708 +
17709 +static void au_hn_destroy_cache(void)
17710 +{
17711 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17712 +       au_cache[AuCache_HNOTIFY] = NULL;
17713 +}
17714 +
17715 +int __init au_hnotify_init(void)
17716 +{
17717 +       int err;
17718 +
17719 +       err = -ENOMEM;
17720 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17721 +       if (au_cache[AuCache_HNOTIFY]) {
17722 +               err = 0;
17723 +               if (au_hnotify_op.init)
17724 +                       err = au_hnotify_op.init();
17725 +               if (unlikely(err))
17726 +                       au_hn_destroy_cache();
17727 +       }
17728 +       AuTraceErr(err);
17729 +       return err;
17730 +}
17731 +
17732 +void au_hnotify_fin(void)
17733 +{
17734 +       if (au_hnotify_op.fin)
17735 +               au_hnotify_op.fin();
17736 +
17737 +       /* cf. au_cache_fin() */
17738 +       if (au_cache[AuCache_HNOTIFY])
17739 +               au_hn_destroy_cache();
17740 +}
17741 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17742 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17743 +++ linux/fs/aufs/iinfo.c       2020-01-27 10:57:18.175538316 +0100
17744 @@ -0,0 +1,286 @@
17745 +// SPDX-License-Identifier: GPL-2.0
17746 +/*
17747 + * Copyright (C) 2005-2020 Junjiro R. Okajima
17748 + *
17749 + * This program, aufs is free software; you can redistribute it and/or modify
17750 + * it under the terms of the GNU General Public License as published by
17751 + * the Free Software Foundation; either version 2 of the License, or
17752 + * (at your option) any later version.
17753 + *
17754 + * This program is distributed in the hope that it will be useful,
17755 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17756 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17757 + * GNU General Public License for more details.
17758 + *
17759 + * You should have received a copy of the GNU General Public License
17760 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17761 + */
17762 +
17763 +/*
17764 + * inode private data
17765 + */
17766 +
17767 +#include "aufs.h"
17768 +
17769 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17770 +{
17771 +       struct inode *h_inode;
17772 +       struct au_hinode *hinode;
17773 +
17774 +       IiMustAnyLock(inode);
17775 +
17776 +       hinode = au_hinode(au_ii(inode), bindex);
17777 +       h_inode = hinode->hi_inode;
17778 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17779 +       return h_inode;
17780 +}
17781 +
17782 +/* todo: hard/soft set? */
17783 +void au_hiput(struct au_hinode *hinode)
17784 +{
17785 +       au_hn_free(hinode);
17786 +       dput(hinode->hi_whdentry);
17787 +       iput(hinode->hi_inode);
17788 +}
17789 +
17790 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17791 +{
17792 +       unsigned int flags;
17793 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17794 +
17795 +       flags = 0;
17796 +       if (au_opt_test(mnt_flags, XINO))
17797 +               au_fset_hi(flags, XINO);
17798 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17799 +               au_fset_hi(flags, HNOTIFY);
17800 +       return flags;
17801 +}
17802 +
17803 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17804 +                  struct inode *h_inode, unsigned int flags)
17805 +{
17806 +       struct au_hinode *hinode;
17807 +       struct inode *hi;
17808 +       struct au_iinfo *iinfo = au_ii(inode);
17809 +
17810 +       IiMustWriteLock(inode);
17811 +
17812 +       hinode = au_hinode(iinfo, bindex);
17813 +       hi = hinode->hi_inode;
17814 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17815 +
17816 +       if (hi)
17817 +               au_hiput(hinode);
17818 +       hinode->hi_inode = h_inode;
17819 +       if (h_inode) {
17820 +               int err;
17821 +               struct super_block *sb = inode->i_sb;
17822 +               struct au_branch *br;
17823 +
17824 +               AuDebugOn(inode->i_mode
17825 +                         && (h_inode->i_mode & S_IFMT)
17826 +                         != (inode->i_mode & S_IFMT));
17827 +               if (bindex == iinfo->ii_btop)
17828 +                       au_cpup_igen(inode, h_inode);
17829 +               br = au_sbr(sb, bindex);
17830 +               hinode->hi_id = br->br_id;
17831 +               if (au_ftest_hi(flags, XINO)) {
17832 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17833 +                                           inode->i_ino);
17834 +                       if (unlikely(err))
17835 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17836 +               }
17837 +
17838 +               if (au_ftest_hi(flags, HNOTIFY)
17839 +                   && au_br_hnotifyable(br->br_perm)) {
17840 +                       err = au_hn_alloc(hinode, inode);
17841 +                       if (unlikely(err))
17842 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17843 +               }
17844 +       }
17845 +}
17846 +
17847 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17848 +                 struct dentry *h_wh)
17849 +{
17850 +       struct au_hinode *hinode;
17851 +
17852 +       IiMustWriteLock(inode);
17853 +
17854 +       hinode = au_hinode(au_ii(inode), bindex);
17855 +       AuDebugOn(hinode->hi_whdentry);
17856 +       hinode->hi_whdentry = h_wh;
17857 +}
17858 +
17859 +void au_update_iigen(struct inode *inode, int half)
17860 +{
17861 +       struct au_iinfo *iinfo;
17862 +       struct au_iigen *iigen;
17863 +       unsigned int sigen;
17864 +
17865 +       sigen = au_sigen(inode->i_sb);
17866 +       iinfo = au_ii(inode);
17867 +       iigen = &iinfo->ii_generation;
17868 +       spin_lock(&iigen->ig_spin);
17869 +       iigen->ig_generation = sigen;
17870 +       if (half)
17871 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17872 +       else
17873 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17874 +       spin_unlock(&iigen->ig_spin);
17875 +}
17876 +
17877 +/* it may be called at remount time, too */
17878 +void au_update_ibrange(struct inode *inode, int do_put_zero)
17879 +{
17880 +       struct au_iinfo *iinfo;
17881 +       aufs_bindex_t bindex, bbot;
17882 +
17883 +       AuDebugOn(au_is_bad_inode(inode));
17884 +       IiMustWriteLock(inode);
17885 +
17886 +       iinfo = au_ii(inode);
17887 +       if (do_put_zero && iinfo->ii_btop >= 0) {
17888 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17889 +                    bindex++) {
17890 +                       struct inode *h_i;
17891 +
17892 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
17893 +                       if (h_i
17894 +                           && !h_i->i_nlink
17895 +                           && !(h_i->i_state & I_LINKABLE))
17896 +                               au_set_h_iptr(inode, bindex, NULL, 0);
17897 +               }
17898 +       }
17899 +
17900 +       iinfo->ii_btop = -1;
17901 +       iinfo->ii_bbot = -1;
17902 +       bbot = au_sbbot(inode->i_sb);
17903 +       for (bindex = 0; bindex <= bbot; bindex++)
17904 +               if (au_hinode(iinfo, bindex)->hi_inode) {
17905 +                       iinfo->ii_btop = bindex;
17906 +                       break;
17907 +               }
17908 +       if (iinfo->ii_btop >= 0)
17909 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17910 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
17911 +                               iinfo->ii_bbot = bindex;
17912 +                               break;
17913 +                       }
17914 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17915 +}
17916 +
17917 +/* ---------------------------------------------------------------------- */
17918 +
17919 +void au_icntnr_init_once(void *_c)
17920 +{
17921 +       struct au_icntnr *c = _c;
17922 +       struct au_iinfo *iinfo = &c->iinfo;
17923 +
17924 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
17925 +       au_rw_init(&iinfo->ii_rwsem);
17926 +       inode_init_once(&c->vfs_inode);
17927 +}
17928 +
17929 +void au_hinode_init(struct au_hinode *hinode)
17930 +{
17931 +       hinode->hi_inode = NULL;
17932 +       hinode->hi_id = -1;
17933 +       au_hn_init(hinode);
17934 +       hinode->hi_whdentry = NULL;
17935 +}
17936 +
17937 +int au_iinfo_init(struct inode *inode)
17938 +{
17939 +       struct au_iinfo *iinfo;
17940 +       struct super_block *sb;
17941 +       struct au_hinode *hi;
17942 +       int nbr, i;
17943 +
17944 +       sb = inode->i_sb;
17945 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17946 +       nbr = au_sbbot(sb) + 1;
17947 +       if (unlikely(nbr <= 0))
17948 +               nbr = 1;
17949 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17950 +       if (hi) {
17951 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17952 +
17953 +               iinfo->ii_hinode = hi;
17954 +               for (i = 0; i < nbr; i++, hi++)
17955 +                       au_hinode_init(hi);
17956 +
17957 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
17958 +               iinfo->ii_btop = -1;
17959 +               iinfo->ii_bbot = -1;
17960 +               iinfo->ii_vdir = NULL;
17961 +               return 0;
17962 +       }
17963 +       return -ENOMEM;
17964 +}
17965 +
17966 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17967 +{
17968 +       int err, i;
17969 +       struct au_hinode *hip;
17970 +
17971 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
17972 +
17973 +       err = -ENOMEM;
17974 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
17975 +                         may_shrink);
17976 +       if (hip) {
17977 +               iinfo->ii_hinode = hip;
17978 +               i = iinfo->ii_bbot + 1;
17979 +               hip += i;
17980 +               for (; i < nbr; i++, hip++)
17981 +                       au_hinode_init(hip);
17982 +               err = 0;
17983 +       }
17984 +
17985 +       return err;
17986 +}
17987 +
17988 +void au_iinfo_fin(struct inode *inode)
17989 +{
17990 +       struct au_iinfo *iinfo;
17991 +       struct au_hinode *hi;
17992 +       struct super_block *sb;
17993 +       aufs_bindex_t bindex, bbot;
17994 +       const unsigned char unlinked = !inode->i_nlink;
17995 +
17996 +       AuDebugOn(au_is_bad_inode(inode));
17997 +
17998 +       sb = inode->i_sb;
17999 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18000 +       if (si_pid_test(sb))
18001 +               au_xino_delete_inode(inode, unlinked);
18002 +       else {
18003 +               /*
18004 +                * it is safe to hide the dependency between sbinfo and
18005 +                * sb->s_umount.
18006 +                */
18007 +               lockdep_off();
18008 +               si_noflush_read_lock(sb);
18009 +               au_xino_delete_inode(inode, unlinked);
18010 +               si_read_unlock(sb);
18011 +               lockdep_on();
18012 +       }
18013 +
18014 +       iinfo = au_ii(inode);
18015 +       if (iinfo->ii_vdir)
18016 +               au_vdir_free(iinfo->ii_vdir);
18017 +
18018 +       bindex = iinfo->ii_btop;
18019 +       if (bindex >= 0) {
18020 +               hi = au_hinode(iinfo, bindex);
18021 +               bbot = iinfo->ii_bbot;
18022 +               while (bindex++ <= bbot) {
18023 +                       if (hi->hi_inode)
18024 +                               au_hiput(hi);
18025 +                       hi++;
18026 +               }
18027 +       }
18028 +       au_kfree_rcu(iinfo->ii_hinode);
18029 +       AuRwDestroy(&iinfo->ii_rwsem);
18030 +}
18031 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18032 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18033 +++ linux/fs/aufs/inode.c       2020-01-27 10:57:18.175538316 +0100
18034 @@ -0,0 +1,529 @@
18035 +// SPDX-License-Identifier: GPL-2.0
18036 +/*
18037 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18038 + *
18039 + * This program, aufs is free software; you can redistribute it and/or modify
18040 + * it under the terms of the GNU General Public License as published by
18041 + * the Free Software Foundation; either version 2 of the License, or
18042 + * (at your option) any later version.
18043 + *
18044 + * This program is distributed in the hope that it will be useful,
18045 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18046 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18047 + * GNU General Public License for more details.
18048 + *
18049 + * You should have received a copy of the GNU General Public License
18050 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18051 + */
18052 +
18053 +/*
18054 + * inode functions
18055 + */
18056 +
18057 +#include <linux/iversion.h>
18058 +#include "aufs.h"
18059 +
18060 +struct inode *au_igrab(struct inode *inode)
18061 +{
18062 +       if (inode) {
18063 +               AuDebugOn(!atomic_read(&inode->i_count));
18064 +               ihold(inode);
18065 +       }
18066 +       return inode;
18067 +}
18068 +
18069 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18070 +{
18071 +       au_cpup_attr_all(inode, /*force*/0);
18072 +       au_update_iigen(inode, /*half*/1);
18073 +       if (do_version)
18074 +               inode_inc_iversion(inode);
18075 +}
18076 +
18077 +static int au_ii_refresh(struct inode *inode, int *update)
18078 +{
18079 +       int err, e, nbr;
18080 +       umode_t type;
18081 +       aufs_bindex_t bindex, new_bindex;
18082 +       struct super_block *sb;
18083 +       struct au_iinfo *iinfo;
18084 +       struct au_hinode *p, *q, tmp;
18085 +
18086 +       AuDebugOn(au_is_bad_inode(inode));
18087 +       IiMustWriteLock(inode);
18088 +
18089 +       *update = 0;
18090 +       sb = inode->i_sb;
18091 +       nbr = au_sbbot(sb) + 1;
18092 +       type = inode->i_mode & S_IFMT;
18093 +       iinfo = au_ii(inode);
18094 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18095 +       if (unlikely(err))
18096 +               goto out;
18097 +
18098 +       AuDebugOn(iinfo->ii_btop < 0);
18099 +       p = au_hinode(iinfo, iinfo->ii_btop);
18100 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18101 +            bindex++, p++) {
18102 +               if (!p->hi_inode)
18103 +                       continue;
18104 +
18105 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18106 +               new_bindex = au_br_index(sb, p->hi_id);
18107 +               if (new_bindex == bindex)
18108 +                       continue;
18109 +
18110 +               if (new_bindex < 0) {
18111 +                       *update = 1;
18112 +                       au_hiput(p);
18113 +                       p->hi_inode = NULL;
18114 +                       continue;
18115 +               }
18116 +
18117 +               if (new_bindex < iinfo->ii_btop)
18118 +                       iinfo->ii_btop = new_bindex;
18119 +               if (iinfo->ii_bbot < new_bindex)
18120 +                       iinfo->ii_bbot = new_bindex;
18121 +               /* swap two lower inode, and loop again */
18122 +               q = au_hinode(iinfo, new_bindex);
18123 +               tmp = *q;
18124 +               *q = *p;
18125 +               *p = tmp;
18126 +               if (tmp.hi_inode) {
18127 +                       bindex--;
18128 +                       p--;
18129 +               }
18130 +       }
18131 +       au_update_ibrange(inode, /*do_put_zero*/0);
18132 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18133 +       e = au_dy_irefresh(inode);
18134 +       if (unlikely(e && !err))
18135 +               err = e;
18136 +
18137 +out:
18138 +       AuTraceErr(err);
18139 +       return err;
18140 +}
18141 +
18142 +void au_refresh_iop(struct inode *inode, int force_getattr)
18143 +{
18144 +       int type;
18145 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18146 +       const struct inode_operations *iop
18147 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18148 +
18149 +       if (inode->i_op == iop)
18150 +               return;
18151 +
18152 +       switch (inode->i_mode & S_IFMT) {
18153 +       case S_IFDIR:
18154 +               type = AuIop_DIR;
18155 +               break;
18156 +       case S_IFLNK:
18157 +               type = AuIop_SYMLINK;
18158 +               break;
18159 +       default:
18160 +               type = AuIop_OTHER;
18161 +               break;
18162 +       }
18163 +
18164 +       inode->i_op = iop + type;
18165 +       /* unnecessary smp_wmb() */
18166 +}
18167 +
18168 +int au_refresh_hinode_self(struct inode *inode)
18169 +{
18170 +       int err, update;
18171 +
18172 +       err = au_ii_refresh(inode, &update);
18173 +       if (!err)
18174 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18175 +
18176 +       AuTraceErr(err);
18177 +       return err;
18178 +}
18179 +
18180 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18181 +{
18182 +       int err, e, update;
18183 +       unsigned int flags;
18184 +       umode_t mode;
18185 +       aufs_bindex_t bindex, bbot;
18186 +       unsigned char isdir;
18187 +       struct au_hinode *p;
18188 +       struct au_iinfo *iinfo;
18189 +
18190 +       err = au_ii_refresh(inode, &update);
18191 +       if (unlikely(err))
18192 +               goto out;
18193 +
18194 +       update = 0;
18195 +       iinfo = au_ii(inode);
18196 +       p = au_hinode(iinfo, iinfo->ii_btop);
18197 +       mode = (inode->i_mode & S_IFMT);
18198 +       isdir = S_ISDIR(mode);
18199 +       flags = au_hi_flags(inode, isdir);
18200 +       bbot = au_dbbot(dentry);
18201 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18202 +               struct inode *h_i, *h_inode;
18203 +               struct dentry *h_d;
18204 +
18205 +               h_d = au_h_dptr(dentry, bindex);
18206 +               if (!h_d || d_is_negative(h_d))
18207 +                       continue;
18208 +
18209 +               h_inode = d_inode(h_d);
18210 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18211 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18212 +                       h_i = au_h_iptr(inode, bindex);
18213 +                       if (h_i) {
18214 +                               if (h_i == h_inode)
18215 +                                       continue;
18216 +                               err = -EIO;
18217 +                               break;
18218 +                       }
18219 +               }
18220 +               if (bindex < iinfo->ii_btop)
18221 +                       iinfo->ii_btop = bindex;
18222 +               if (iinfo->ii_bbot < bindex)
18223 +                       iinfo->ii_bbot = bindex;
18224 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18225 +               update = 1;
18226 +       }
18227 +       au_update_ibrange(inode, /*do_put_zero*/0);
18228 +       e = au_dy_irefresh(inode);
18229 +       if (unlikely(e && !err))
18230 +               err = e;
18231 +       if (!err)
18232 +               au_refresh_hinode_attr(inode, update && isdir);
18233 +
18234 +out:
18235 +       AuTraceErr(err);
18236 +       return err;
18237 +}
18238 +
18239 +static int set_inode(struct inode *inode, struct dentry *dentry)
18240 +{
18241 +       int err;
18242 +       unsigned int flags;
18243 +       umode_t mode;
18244 +       aufs_bindex_t bindex, btop, btail;
18245 +       unsigned char isdir;
18246 +       struct dentry *h_dentry;
18247 +       struct inode *h_inode;
18248 +       struct au_iinfo *iinfo;
18249 +       const struct inode_operations *iop;
18250 +
18251 +       IiMustWriteLock(inode);
18252 +
18253 +       err = 0;
18254 +       isdir = 0;
18255 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18256 +       btop = au_dbtop(dentry);
18257 +       h_dentry = au_h_dptr(dentry, btop);
18258 +       h_inode = d_inode(h_dentry);
18259 +       mode = h_inode->i_mode;
18260 +       switch (mode & S_IFMT) {
18261 +       case S_IFREG:
18262 +               btail = au_dbtail(dentry);
18263 +               inode->i_op = iop + AuIop_OTHER;
18264 +               inode->i_fop = &aufs_file_fop;
18265 +               err = au_dy_iaop(inode, btop, h_inode);
18266 +               if (unlikely(err))
18267 +                       goto out;
18268 +               break;
18269 +       case S_IFDIR:
18270 +               isdir = 1;
18271 +               btail = au_dbtaildir(dentry);
18272 +               inode->i_op = iop + AuIop_DIR;
18273 +               inode->i_fop = &aufs_dir_fop;
18274 +               break;
18275 +       case S_IFLNK:
18276 +               btail = au_dbtail(dentry);
18277 +               inode->i_op = iop + AuIop_SYMLINK;
18278 +               break;
18279 +       case S_IFBLK:
18280 +       case S_IFCHR:
18281 +       case S_IFIFO:
18282 +       case S_IFSOCK:
18283 +               btail = au_dbtail(dentry);
18284 +               inode->i_op = iop + AuIop_OTHER;
18285 +               init_special_inode(inode, mode, h_inode->i_rdev);
18286 +               break;
18287 +       default:
18288 +               AuIOErr("Unknown file type 0%o\n", mode);
18289 +               err = -EIO;
18290 +               goto out;
18291 +       }
18292 +
18293 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18294 +       flags = au_hi_flags(inode, isdir);
18295 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18296 +           && au_ftest_hi(flags, HNOTIFY)
18297 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18298 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18299 +               au_fclr_hi(flags, HNOTIFY);
18300 +       iinfo = au_ii(inode);
18301 +       iinfo->ii_btop = btop;
18302 +       iinfo->ii_bbot = btail;
18303 +       for (bindex = btop; bindex <= btail; bindex++) {
18304 +               h_dentry = au_h_dptr(dentry, bindex);
18305 +               if (h_dentry)
18306 +                       au_set_h_iptr(inode, bindex,
18307 +                                     au_igrab(d_inode(h_dentry)), flags);
18308 +       }
18309 +       au_cpup_attr_all(inode, /*force*/1);
18310 +       /*
18311 +        * to force calling aufs_get_acl() every time,
18312 +        * do not call cache_no_acl() for aufs inode.
18313 +        */
18314 +
18315 +out:
18316 +       return err;
18317 +}
18318 +
18319 +/*
18320 + * successful returns with iinfo write_locked
18321 + * minus: errno
18322 + * zero: success, matched
18323 + * plus: no error, but unmatched
18324 + */
18325 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18326 +{
18327 +       int err;
18328 +       unsigned int gen, igflags;
18329 +       aufs_bindex_t bindex, bbot;
18330 +       struct inode *h_inode, *h_dinode;
18331 +       struct dentry *h_dentry;
18332 +
18333 +       /*
18334 +        * before this function, if aufs got any iinfo lock, it must be only
18335 +        * one, the parent dir.
18336 +        * it can happen by UDBA and the obsoleted inode number.
18337 +        */
18338 +       err = -EIO;
18339 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18340 +               goto out;
18341 +
18342 +       err = 1;
18343 +       ii_write_lock_new_child(inode);
18344 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18345 +       h_dinode = d_inode(h_dentry);
18346 +       bbot = au_ibbot(inode);
18347 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18348 +               h_inode = au_h_iptr(inode, bindex);
18349 +               if (!h_inode || h_inode != h_dinode)
18350 +                       continue;
18351 +
18352 +               err = 0;
18353 +               gen = au_iigen(inode, &igflags);
18354 +               if (gen == au_digen(dentry)
18355 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18356 +                       break;
18357 +
18358 +               /* fully refresh inode using dentry */
18359 +               err = au_refresh_hinode(inode, dentry);
18360 +               if (!err)
18361 +                       au_update_iigen(inode, /*half*/0);
18362 +               break;
18363 +       }
18364 +
18365 +       if (unlikely(err))
18366 +               ii_write_unlock(inode);
18367 +out:
18368 +       return err;
18369 +}
18370 +
18371 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18372 +          unsigned int d_type, ino_t *ino)
18373 +{
18374 +       int err, idx;
18375 +       const int isnondir = d_type != DT_DIR;
18376 +
18377 +       /* prevent hardlinked inode number from race condition */
18378 +       if (isnondir) {
18379 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18380 +               if (unlikely(err))
18381 +                       goto out;
18382 +       }
18383 +
18384 +       err = au_xino_read(sb, bindex, h_ino, ino);
18385 +       if (unlikely(err))
18386 +               goto out_xinondir;
18387 +
18388 +       if (!*ino) {
18389 +               err = -EIO;
18390 +               *ino = au_xino_new_ino(sb);
18391 +               if (unlikely(!*ino))
18392 +                       goto out_xinondir;
18393 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18394 +               if (unlikely(err))
18395 +                       goto out_xinondir;
18396 +       }
18397 +
18398 +out_xinondir:
18399 +       if (isnondir && idx >= 0)
18400 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18401 +out:
18402 +       return err;
18403 +}
18404 +
18405 +/* successful returns with iinfo write_locked */
18406 +/* todo: return with unlocked? */
18407 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18408 +{
18409 +       struct inode *inode, *h_inode;
18410 +       struct dentry *h_dentry;
18411 +       struct super_block *sb;
18412 +       ino_t h_ino, ino;
18413 +       int err, idx, hlinked;
18414 +       aufs_bindex_t btop;
18415 +
18416 +       sb = dentry->d_sb;
18417 +       btop = au_dbtop(dentry);
18418 +       h_dentry = au_h_dptr(dentry, btop);
18419 +       h_inode = d_inode(h_dentry);
18420 +       h_ino = h_inode->i_ino;
18421 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18422 +
18423 +new_ino:
18424 +       /*
18425 +        * stop 'race'-ing between hardlinks under different
18426 +        * parents.
18427 +        */
18428 +       if (hlinked) {
18429 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18430 +               inode = ERR_PTR(err);
18431 +               if (unlikely(err))
18432 +                       goto out;
18433 +       }
18434 +
18435 +       err = au_xino_read(sb, btop, h_ino, &ino);
18436 +       inode = ERR_PTR(err);
18437 +       if (unlikely(err))
18438 +               goto out_xinondir;
18439 +
18440 +       if (!ino) {
18441 +               ino = au_xino_new_ino(sb);
18442 +               if (unlikely(!ino)) {
18443 +                       inode = ERR_PTR(-EIO);
18444 +                       goto out_xinondir;
18445 +               }
18446 +       }
18447 +
18448 +       AuDbg("i%lu\n", (unsigned long)ino);
18449 +       inode = au_iget_locked(sb, ino);
18450 +       err = PTR_ERR(inode);
18451 +       if (IS_ERR(inode))
18452 +               goto out_xinondir;
18453 +
18454 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18455 +       if (inode->i_state & I_NEW) {
18456 +               ii_write_lock_new_child(inode);
18457 +               err = set_inode(inode, dentry);
18458 +               if (!err) {
18459 +                       unlock_new_inode(inode);
18460 +                       goto out_xinondir; /* success */
18461 +               }
18462 +
18463 +               /*
18464 +                * iget_failed() calls iput(), but we need to call
18465 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18466 +                * i_count.
18467 +                */
18468 +               atomic_inc(&inode->i_count);
18469 +               iget_failed(inode);
18470 +               ii_write_unlock(inode);
18471 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18472 +               /* ignore this error */
18473 +               goto out_iput;
18474 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18475 +               /*
18476 +                * horrible race condition between lookup, readdir and copyup
18477 +                * (or something).
18478 +                */
18479 +               if (hlinked && idx >= 0)
18480 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18481 +               err = reval_inode(inode, dentry);
18482 +               if (unlikely(err < 0)) {
18483 +                       hlinked = 0;
18484 +                       goto out_iput;
18485 +               }
18486 +               if (!err)
18487 +                       goto out; /* success */
18488 +               else if (hlinked && idx >= 0) {
18489 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18490 +                       if (unlikely(err)) {
18491 +                               iput(inode);
18492 +                               inode = ERR_PTR(err);
18493 +                               goto out;
18494 +                       }
18495 +               }
18496 +       }
18497 +
18498 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18499 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18500 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18501 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18502 +                       (unsigned long)h_ino, (unsigned long)ino);
18503 +       ino = 0;
18504 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18505 +       if (!err) {
18506 +               iput(inode);
18507 +               if (hlinked && idx >= 0)
18508 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18509 +               goto new_ino;
18510 +       }
18511 +
18512 +out_iput:
18513 +       iput(inode);
18514 +       inode = ERR_PTR(err);
18515 +out_xinondir:
18516 +       if (hlinked && idx >= 0)
18517 +               au_xinondir_leave(sb, btop, h_ino, idx);
18518 +out:
18519 +       return inode;
18520 +}
18521 +
18522 +/* ---------------------------------------------------------------------- */
18523 +
18524 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18525 +              struct inode *inode)
18526 +{
18527 +       int err;
18528 +       struct inode *hi;
18529 +
18530 +       err = au_br_rdonly(au_sbr(sb, bindex));
18531 +
18532 +       /* pseudo-link after flushed may happen out of bounds */
18533 +       if (!err
18534 +           && inode
18535 +           && au_ibtop(inode) <= bindex
18536 +           && bindex <= au_ibbot(inode)) {
18537 +               /*
18538 +                * permission check is unnecessary since vfsub routine
18539 +                * will be called later
18540 +                */
18541 +               hi = au_h_iptr(inode, bindex);
18542 +               if (hi)
18543 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18544 +       }
18545 +
18546 +       return err;
18547 +}
18548 +
18549 +int au_test_h_perm(struct inode *h_inode, int mask)
18550 +{
18551 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18552 +               return 0;
18553 +       return inode_permission(h_inode, mask);
18554 +}
18555 +
18556 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
18557 +{
18558 +       if (au_test_nfs(h_inode->i_sb)
18559 +           && (mask & MAY_WRITE)
18560 +           && S_ISDIR(h_inode->i_mode))
18561 +               mask |= MAY_READ; /* force permission check */
18562 +       return au_test_h_perm(h_inode, mask);
18563 +}
18564 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18565 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18566 +++ linux/fs/aufs/inode.h       2020-08-03 09:14:46.095748745 +0200
18567 @@ -0,0 +1,698 @@
18568 +/* SPDX-License-Identifier: GPL-2.0 */
18569 +/*
18570 + * Copyright (C) 2005-2020 Junjiro R. Okajima
18571 + *
18572 + * This program, aufs is free software; you can redistribute it and/or modify
18573 + * it under the terms of the GNU General Public License as published by
18574 + * the Free Software Foundation; either version 2 of the License, or
18575 + * (at your option) any later version.
18576 + *
18577 + * This program is distributed in the hope that it will be useful,
18578 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18579 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18580 + * GNU General Public License for more details.
18581 + *
18582 + * You should have received a copy of the GNU General Public License
18583 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18584 + */
18585 +
18586 +/*
18587 + * inode operations
18588 + */
18589 +
18590 +#ifndef __AUFS_INODE_H__
18591 +#define __AUFS_INODE_H__
18592 +
18593 +#ifdef __KERNEL__
18594 +
18595 +#include <linux/fsnotify.h>
18596 +#include "rwsem.h"
18597 +
18598 +struct vfsmount;
18599 +
18600 +struct au_hnotify {
18601 +#ifdef CONFIG_AUFS_HNOTIFY
18602 +#ifdef CONFIG_AUFS_HFSNOTIFY
18603 +       /* never use fsnotify_add_vfsmount_mark() */
18604 +       struct fsnotify_mark            hn_mark;
18605 +#endif
18606 +       struct inode            *hn_aufs_inode; /* no get/put */
18607 +       struct rcu_head         rcu;
18608 +#endif
18609 +} ____cacheline_aligned_in_smp;
18610 +
18611 +struct au_hinode {
18612 +       struct inode            *hi_inode;
18613 +       aufs_bindex_t           hi_id;
18614 +#ifdef CONFIG_AUFS_HNOTIFY
18615 +       struct au_hnotify       *hi_notify;
18616 +#endif
18617 +
18618 +       /* reference to the copied-up whiteout with get/put */
18619 +       struct dentry           *hi_whdentry;
18620 +};
18621 +
18622 +/* ig_flags */
18623 +#define AuIG_HALF_REFRESHED            1
18624 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18625 +#define au_ig_fset(flags, name) \
18626 +       do { (flags) |= AuIG_##name; } while (0)
18627 +#define au_ig_fclr(flags, name) \
18628 +       do { (flags) &= ~AuIG_##name; } while (0)
18629 +
18630 +struct au_iigen {
18631 +       spinlock_t      ig_spin;
18632 +       __u32           ig_generation, ig_flags;
18633 +};
18634 +
18635 +struct au_vdir;
18636 +struct au_iinfo {
18637 +       struct au_iigen         ii_generation;
18638 +       struct super_block      *ii_hsb1;       /* no get/put */
18639 +
18640 +       struct au_rwsem         ii_rwsem;
18641 +       aufs_bindex_t           ii_btop, ii_bbot;
18642 +       __u32                   ii_higen;
18643 +       struct au_hinode        *ii_hinode;
18644 +       struct au_vdir          *ii_vdir;
18645 +};
18646 +
18647 +struct au_icntnr {
18648 +       struct au_iinfo         iinfo;
18649 +       struct inode            vfs_inode;
18650 +       struct hlist_bl_node    plink;
18651 +       struct rcu_head         rcu;
18652 +} ____cacheline_aligned_in_smp;
18653 +
18654 +/* au_pin flags */
18655 +#define AuPin_DI_LOCKED                1
18656 +#define AuPin_MNT_WRITE                (1 << 1)
18657 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18658 +#define au_fset_pin(flags, name) \
18659 +       do { (flags) |= AuPin_##name; } while (0)
18660 +#define au_fclr_pin(flags, name) \
18661 +       do { (flags) &= ~AuPin_##name; } while (0)
18662 +
18663 +struct au_pin {
18664 +       /* input */
18665 +       struct dentry *dentry;
18666 +       unsigned int udba;
18667 +       unsigned char lsc_di, lsc_hi, flags;
18668 +       aufs_bindex_t bindex;
18669 +
18670 +       /* output */
18671 +       struct dentry *parent;
18672 +       struct au_hinode *hdir;
18673 +       struct vfsmount *h_mnt;
18674 +
18675 +       /* temporary unlock/relock for copyup */
18676 +       struct dentry *h_dentry, *h_parent;
18677 +       struct au_branch *br;
18678 +       struct task_struct *task;
18679 +};
18680 +
18681 +void au_pin_hdir_unlock(struct au_pin *p);
18682 +int au_pin_hdir_lock(struct au_pin *p);
18683 +int au_pin_hdir_relock(struct au_pin *p);
18684 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18685 +void au_pin_hdir_release(struct au_pin *p);
18686 +
18687 +/* ---------------------------------------------------------------------- */
18688 +
18689 +static inline struct au_iinfo *au_ii(struct inode *inode)
18690 +{
18691 +       BUG_ON(is_bad_inode(inode));
18692 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18693 +}
18694 +
18695 +/* ---------------------------------------------------------------------- */
18696 +
18697 +/* inode.c */
18698 +struct inode *au_igrab(struct inode *inode);
18699 +void au_refresh_iop(struct inode *inode, int force_getattr);
18700 +int au_refresh_hinode_self(struct inode *inode);
18701 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18702 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18703 +          unsigned int d_type, ino_t *ino);
18704 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18705 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18706 +              struct inode *inode);
18707 +int au_test_h_perm(struct inode *h_inode, int mask);
18708 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
18709 +
18710 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18711 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18712 +{
18713 +#ifdef CONFIG_AUFS_SHWH
18714 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18715 +#else
18716 +       return 0;
18717 +#endif
18718 +}
18719 +
18720 +/* i_op.c */
18721 +enum {
18722 +       AuIop_SYMLINK,
18723 +       AuIop_DIR,
18724 +       AuIop_OTHER,
18725 +       AuIop_Last
18726 +};
18727 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18728 +       aufs_iop_nogetattr[AuIop_Last];
18729 +
18730 +/* au_wr_dir flags */
18731 +#define AuWrDir_ADD_ENTRY      1
18732 +#define AuWrDir_ISDIR          (1 << 1)
18733 +#define AuWrDir_TMPFILE                (1 << 2)
18734 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18735 +#define au_fset_wrdir(flags, name) \
18736 +       do { (flags) |= AuWrDir_##name; } while (0)
18737 +#define au_fclr_wrdir(flags, name) \
18738 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18739 +
18740 +struct au_wr_dir_args {
18741 +       aufs_bindex_t force_btgt;
18742 +       unsigned char flags;
18743 +};
18744 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18745 +             struct au_wr_dir_args *args);
18746 +
18747 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18748 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18749 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18750 +                unsigned int udba, unsigned char flags);
18751 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18752 +          unsigned int udba, unsigned char flags) __must_check;
18753 +int au_do_pin(struct au_pin *pin) __must_check;
18754 +void au_unpin(struct au_pin *pin);
18755 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18756 +
18757 +#define AuIcpup_DID_CPUP       1
18758 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18759 +#define au_fset_icpup(flags, name) \
18760 +       do { (flags) |= AuIcpup_##name; } while (0)
18761 +#define au_fclr_icpup(flags, name) \
18762 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18763 +
18764 +struct au_icpup_args {
18765 +       unsigned char flags;
18766 +       unsigned char pin_flags;
18767 +       aufs_bindex_t btgt;
18768 +       unsigned int udba;
18769 +       struct au_pin pin;
18770 +       struct path h_path;
18771 +       struct inode *h_inode;
18772 +};
18773 +
18774 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18775 +                    struct au_icpup_args *a);
18776 +
18777 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18778 +                     struct path *h_path, int locked);
18779 +
18780 +/* i_op_add.c */
18781 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18782 +              struct dentry *h_parent, int isdir);
18783 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
18784 +              dev_t dev);
18785 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
18786 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
18787 +               bool want_excl);
18788 +struct vfsub_aopen_args;
18789 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18790 +                      struct vfsub_aopen_args *args);
18791 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
18792 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18793 +             struct dentry *dentry);
18794 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
18795 +
18796 +/* i_op_del.c */
18797 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18798 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18799 +              struct dentry *h_parent, int isdir);
18800 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18801 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18802 +
18803 +/* i_op_ren.c */
18804 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18805 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
18806 +               struct inode *dir, struct dentry *dentry,
18807 +               unsigned int flags);
18808 +
18809 +/* iinfo.c */
18810 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18811 +void au_hiput(struct au_hinode *hinode);
18812 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18813 +                 struct dentry *h_wh);
18814 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18815 +
18816 +/* hinode flags */
18817 +#define AuHi_XINO      1
18818 +#define AuHi_HNOTIFY   (1 << 1)
18819 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18820 +#define au_fset_hi(flags, name) \
18821 +       do { (flags) |= AuHi_##name; } while (0)
18822 +#define au_fclr_hi(flags, name) \
18823 +       do { (flags) &= ~AuHi_##name; } while (0)
18824 +
18825 +#ifndef CONFIG_AUFS_HNOTIFY
18826 +#undef AuHi_HNOTIFY
18827 +#define AuHi_HNOTIFY   0
18828 +#endif
18829 +
18830 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18831 +                  struct inode *h_inode, unsigned int flags);
18832 +
18833 +void au_update_iigen(struct inode *inode, int half);
18834 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18835 +
18836 +void au_icntnr_init_once(void *_c);
18837 +void au_hinode_init(struct au_hinode *hinode);
18838 +int au_iinfo_init(struct inode *inode);
18839 +void au_iinfo_fin(struct inode *inode);
18840 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18841 +
18842 +#ifdef CONFIG_PROC_FS
18843 +/* plink.c */
18844 +int au_plink_maint(struct super_block *sb, int flags);
18845 +struct au_sbinfo;
18846 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18847 +int au_plink_maint_enter(struct super_block *sb);
18848 +#ifdef CONFIG_AUFS_DEBUG
18849 +void au_plink_list(struct super_block *sb);
18850 +#else
18851 +AuStubVoid(au_plink_list, struct super_block *sb)
18852 +#endif
18853 +int au_plink_test(struct inode *inode);
18854 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18855 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18856 +                    struct dentry *h_dentry);
18857 +void au_plink_put(struct super_block *sb, int verbose);
18858 +void au_plink_clean(struct super_block *sb, int verbose);
18859 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18860 +#else
18861 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18862 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18863 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18864 +AuStubVoid(au_plink_list, struct super_block *sb);
18865 +AuStubInt0(au_plink_test, struct inode *inode);
18866 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18867 +       struct inode *inode, aufs_bindex_t bindex);
18868 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18869 +          struct dentry *h_dentry);
18870 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18871 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18872 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18873 +#endif /* CONFIG_PROC_FS */
18874 +
18875 +#ifdef CONFIG_AUFS_XATTR
18876 +/* xattr.c */
18877 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
18878 +                 unsigned int verbose);
18879 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18880 +void au_xattr_init(struct super_block *sb);
18881 +#else
18882 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
18883 +          int ignore_flags, unsigned int verbose);
18884 +AuStubVoid(au_xattr_init, struct super_block *sb);
18885 +#endif
18886 +
18887 +#ifdef CONFIG_FS_POSIX_ACL
18888 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
18889 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
18890 +#endif
18891 +
18892 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18893 +enum {
18894 +       AU_XATTR_SET,
18895 +       AU_ACL_SET
18896 +};
18897 +
18898 +struct au_sxattr {
18899 +       int type;
18900 +       union {
18901 +               struct {
18902 +                       const char      *name;
18903 +                       const void      *value;
18904 +                       size_t          size;
18905 +                       int             flags;
18906 +               } set;
18907 +               struct {
18908 +                       struct posix_acl *acl;
18909 +                       int             type;
18910 +               } acl_set;
18911 +       } u;
18912 +};
18913 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18914 +                 struct au_sxattr *arg);
18915 +#endif
18916 +
18917 +/* ---------------------------------------------------------------------- */
18918 +
18919 +/* lock subclass for iinfo */
18920 +enum {
18921 +       AuLsc_II_CHILD,         /* child first */
18922 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
18923 +       AuLsc_II_CHILD3,        /* copyup dirs */
18924 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
18925 +       AuLsc_II_PARENT2,
18926 +       AuLsc_II_PARENT3,       /* copyup dirs */
18927 +       AuLsc_II_NEW_CHILD
18928 +};
18929 +
18930 +/*
18931 + * ii_read_lock_child, ii_write_lock_child,
18932 + * ii_read_lock_child2, ii_write_lock_child2,
18933 + * ii_read_lock_child3, ii_write_lock_child3,
18934 + * ii_read_lock_parent, ii_write_lock_parent,
18935 + * ii_read_lock_parent2, ii_write_lock_parent2,
18936 + * ii_read_lock_parent3, ii_write_lock_parent3,
18937 + * ii_read_lock_new_child, ii_write_lock_new_child,
18938 + */
18939 +#define AuReadLockFunc(name, lsc) \
18940 +static inline void ii_read_lock_##name(struct inode *i) \
18941 +{ \
18942 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18943 +}
18944 +
18945 +#define AuWriteLockFunc(name, lsc) \
18946 +static inline void ii_write_lock_##name(struct inode *i) \
18947 +{ \
18948 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18949 +}
18950 +
18951 +#define AuRWLockFuncs(name, lsc) \
18952 +       AuReadLockFunc(name, lsc) \
18953 +       AuWriteLockFunc(name, lsc)
18954 +
18955 +AuRWLockFuncs(child, CHILD);
18956 +AuRWLockFuncs(child2, CHILD2);
18957 +AuRWLockFuncs(child3, CHILD3);
18958 +AuRWLockFuncs(parent, PARENT);
18959 +AuRWLockFuncs(parent2, PARENT2);
18960 +AuRWLockFuncs(parent3, PARENT3);
18961 +AuRWLockFuncs(new_child, NEW_CHILD);
18962 +
18963 +#undef AuReadLockFunc
18964 +#undef AuWriteLockFunc
18965 +#undef AuRWLockFuncs
18966 +
18967 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
18968 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
18969 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
18970 +
18971 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
18972 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
18973 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
18974 +
18975 +/* ---------------------------------------------------------------------- */
18976 +
18977 +static inline void au_icntnr_init(struct au_icntnr *c)
18978 +{
18979 +#ifdef CONFIG_AUFS_DEBUG
18980 +       c->vfs_inode.i_mode = 0;
18981 +#endif
18982 +}
18983 +
18984 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
18985 +{
18986 +       unsigned int gen;
18987 +       struct au_iinfo *iinfo;
18988 +       struct au_iigen *iigen;
18989 +
18990 +       iinfo = au_ii(inode);
18991 +       iigen = &iinfo->ii_generation;
18992 +       spin_lock(&iigen->ig_spin);
18993 +       if (igflags)
18994 +               *igflags = iigen->ig_flags;
18995 +       gen = iigen->ig_generation;
18996 +       spin_unlock(&iigen->ig_spin);
18997 +
18998 +       return gen;
18999 +}
19000 +
19001 +/* tiny test for inode number */
19002 +/* tmpfs generation is too rough */
19003 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19004 +{
19005 +       struct au_iinfo *iinfo;
19006 +
19007 +       iinfo = au_ii(inode);
19008 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19009 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19010 +                && iinfo->ii_higen == h_inode->i_generation);
19011 +}
19012 +
19013 +static inline void au_iigen_dec(struct inode *inode)
19014 +{
19015 +       struct au_iinfo *iinfo;
19016 +       struct au_iigen *iigen;
19017 +
19018 +       iinfo = au_ii(inode);
19019 +       iigen = &iinfo->ii_generation;
19020 +       spin_lock(&iigen->ig_spin);
19021 +       iigen->ig_generation--;
19022 +       spin_unlock(&iigen->ig_spin);
19023 +}
19024 +
19025 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19026 +{
19027 +       int err;
19028 +
19029 +       err = 0;
19030 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19031 +               err = -EIO;
19032 +
19033 +       return err;
19034 +}
19035 +
19036 +/* ---------------------------------------------------------------------- */
19037 +
19038 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19039 +                                         aufs_bindex_t bindex)
19040 +{
19041 +       return iinfo->ii_hinode + bindex;
19042 +}
19043 +
19044 +static inline int au_is_bad_inode(struct inode *inode)
19045 +{
19046 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19047 +}
19048 +
19049 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19050 +                                       aufs_bindex_t bindex)
19051 +{
19052 +       IiMustAnyLock(inode);
19053 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19054 +}
19055 +
19056 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19057 +{
19058 +       IiMustAnyLock(inode);
19059 +       return au_ii(inode)->ii_btop;
19060 +}
19061 +
19062 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19063 +{
19064 +       IiMustAnyLock(inode);
19065 +       return au_ii(inode)->ii_bbot;
19066 +}
19067 +
19068 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19069 +{
19070 +       IiMustAnyLock(inode);
19071 +       return au_ii(inode)->ii_vdir;
19072 +}
19073 +
19074 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19075 +{
19076 +       IiMustAnyLock(inode);
19077 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19078 +}
19079 +
19080 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19081 +{
19082 +       IiMustWriteLock(inode);
19083 +       au_ii(inode)->ii_btop = bindex;
19084 +}
19085 +
19086 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19087 +{
19088 +       IiMustWriteLock(inode);
19089 +       au_ii(inode)->ii_bbot = bindex;
19090 +}
19091 +
19092 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19093 +{
19094 +       IiMustWriteLock(inode);
19095 +       au_ii(inode)->ii_vdir = vdir;
19096 +}
19097 +
19098 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19099 +{
19100 +       IiMustAnyLock(inode);
19101 +       return au_hinode(au_ii(inode), bindex);
19102 +}
19103 +
19104 +/* ---------------------------------------------------------------------- */
19105 +
19106 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19107 +{
19108 +       if (pin)
19109 +               return pin->parent;
19110 +       return NULL;
19111 +}
19112 +
19113 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19114 +{
19115 +       if (pin && pin->hdir)
19116 +               return pin->hdir->hi_inode;
19117 +       return NULL;
19118 +}
19119 +
19120 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19121 +{
19122 +       if (pin)
19123 +               return pin->hdir;
19124 +       return NULL;
19125 +}
19126 +
19127 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19128 +{
19129 +       if (pin)
19130 +               pin->dentry = dentry;
19131 +}
19132 +
19133 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19134 +                                          unsigned char lflag)
19135 +{
19136 +       if (pin) {
19137 +               if (lflag)
19138 +                       au_fset_pin(pin->flags, DI_LOCKED);
19139 +               else
19140 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19141 +       }
19142 +}
19143 +
19144 +#if 0 /* reserved */
19145 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19146 +{
19147 +       if (pin) {
19148 +               dput(pin->parent);
19149 +               pin->parent = dget(parent);
19150 +       }
19151 +}
19152 +#endif
19153 +
19154 +/* ---------------------------------------------------------------------- */
19155 +
19156 +struct au_branch;
19157 +#ifdef CONFIG_AUFS_HNOTIFY
19158 +struct au_hnotify_op {
19159 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19160 +       int (*alloc)(struct au_hinode *hinode);
19161 +
19162 +       /*
19163 +        * if it returns true, the the caller should free hinode->hi_notify,
19164 +        * otherwise ->free() frees it.
19165 +        */
19166 +       int (*free)(struct au_hinode *hinode,
19167 +                   struct au_hnotify *hn) __must_check;
19168 +
19169 +       void (*fin)(void);
19170 +       int (*init)(void);
19171 +
19172 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19173 +       void (*fin_br)(struct au_branch *br);
19174 +       int (*init_br)(struct au_branch *br, int perm);
19175 +};
19176 +
19177 +/* hnotify.c */
19178 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19179 +void au_hn_free(struct au_hinode *hinode);
19180 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19181 +void au_hn_reset(struct inode *inode, unsigned int flags);
19182 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19183 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19184 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19185 +int au_hnotify_init_br(struct au_branch *br, int perm);
19186 +void au_hnotify_fin_br(struct au_branch *br);
19187 +int __init au_hnotify_init(void);
19188 +void au_hnotify_fin(void);
19189 +
19190 +/* hfsnotify.c */
19191 +extern const struct au_hnotify_op au_hnotify_op;
19192 +
19193 +static inline
19194 +void au_hn_init(struct au_hinode *hinode)
19195 +{
19196 +       hinode->hi_notify = NULL;
19197 +}
19198 +
19199 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19200 +{
19201 +       return hinode->hi_notify;
19202 +}
19203 +
19204 +#else
19205 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19206 +       struct au_hinode *hinode __maybe_unused,
19207 +       struct inode *inode __maybe_unused)
19208 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19209 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19210 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19211 +          int do_set __maybe_unused)
19212 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19213 +          unsigned int flags __maybe_unused)
19214 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19215 +          struct au_branch *br __maybe_unused,
19216 +          int perm __maybe_unused)
19217 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19218 +          int perm __maybe_unused)
19219 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19220 +AuStubInt0(__init au_hnotify_init, void)
19221 +AuStubVoid(au_hnotify_fin, void)
19222 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19223 +#endif /* CONFIG_AUFS_HNOTIFY */
19224 +
19225 +static inline void au_hn_suspend(struct au_hinode *hdir)
19226 +{
19227 +       au_hn_ctl(hdir, /*do_set*/0);
19228 +}
19229 +
19230 +static inline void au_hn_resume(struct au_hinode *hdir)
19231 +{
19232 +       au_hn_ctl(hdir, /*do_set*/1);
19233 +}
19234 +
19235 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19236 +{
19237 +       inode_lock(hdir->hi_inode);
19238 +       au_hn_suspend(hdir);
19239 +}
19240 +
19241 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19242 +                                         unsigned int sc __maybe_unused)
19243 +{
19244 +       inode_lock_nested(hdir->hi_inode, sc);
19245 +       au_hn_suspend(hdir);
19246 +}
19247 +
19248 +#if 0 /* unused */
19249 +#include "vfsub.h"
19250 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19251 +                                                 unsigned int sc)
19252 +{
19253 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19254 +       au_hn_suspend(hdir);
19255 +}
19256 +#endif
19257 +
19258 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19259 +{
19260 +       au_hn_resume(hdir);
19261 +       inode_unlock(hdir->hi_inode);
19262 +}
19263 +
19264 +#endif /* __KERNEL__ */
19265 +#endif /* __AUFS_INODE_H__ */
19266 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19267 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19268 +++ linux/fs/aufs/ioctl.c       2020-01-27 10:57:18.175538316 +0100
19269 @@ -0,0 +1,220 @@
19270 +// SPDX-License-Identifier: GPL-2.0
19271 +/*
19272 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19273 + *
19274 + * This program, aufs is free software; you can redistribute it and/or modify
19275 + * it under the terms of the GNU General Public License as published by
19276 + * the Free Software Foundation; either version 2 of the License, or
19277 + * (at your option) any later version.
19278 + *
19279 + * This program is distributed in the hope that it will be useful,
19280 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19281 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19282 + * GNU General Public License for more details.
19283 + *
19284 + * You should have received a copy of the GNU General Public License
19285 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19286 + */
19287 +
19288 +/*
19289 + * ioctl
19290 + * plink-management and readdir in userspace.
19291 + * assist the pathconf(3) wrapper library.
19292 + * move-down
19293 + * File-based Hierarchical Storage Management.
19294 + */
19295 +
19296 +#include <linux/compat.h>
19297 +#include <linux/file.h>
19298 +#include "aufs.h"
19299 +
19300 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19301 +{
19302 +       int err, fd;
19303 +       aufs_bindex_t wbi, bindex, bbot;
19304 +       struct file *h_file;
19305 +       struct super_block *sb;
19306 +       struct dentry *root;
19307 +       struct au_branch *br;
19308 +       struct aufs_wbr_fd wbrfd = {
19309 +               .oflags = au_dir_roflags,
19310 +               .brid   = -1
19311 +       };
19312 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19313 +               | O_NOATIME | O_CLOEXEC;
19314 +
19315 +       AuDebugOn(wbrfd.oflags & ~valid);
19316 +
19317 +       if (arg) {
19318 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19319 +               if (unlikely(err)) {
19320 +                       err = -EFAULT;
19321 +                       goto out;
19322 +               }
19323 +
19324 +               err = -EINVAL;
19325 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19326 +               wbrfd.oflags |= au_dir_roflags;
19327 +               AuDbg("0%o\n", wbrfd.oflags);
19328 +               if (unlikely(wbrfd.oflags & ~valid))
19329 +                       goto out;
19330 +       }
19331 +
19332 +       fd = get_unused_fd_flags(0);
19333 +       err = fd;
19334 +       if (unlikely(fd < 0))
19335 +               goto out;
19336 +
19337 +       h_file = ERR_PTR(-EINVAL);
19338 +       wbi = 0;
19339 +       br = NULL;
19340 +       sb = path->dentry->d_sb;
19341 +       root = sb->s_root;
19342 +       aufs_read_lock(root, AuLock_IR);
19343 +       bbot = au_sbbot(sb);
19344 +       if (wbrfd.brid >= 0) {
19345 +               wbi = au_br_index(sb, wbrfd.brid);
19346 +               if (unlikely(wbi < 0 || wbi > bbot))
19347 +                       goto out_unlock;
19348 +       }
19349 +
19350 +       h_file = ERR_PTR(-ENOENT);
19351 +       br = au_sbr(sb, wbi);
19352 +       if (!au_br_writable(br->br_perm)) {
19353 +               if (arg)
19354 +                       goto out_unlock;
19355 +
19356 +               bindex = wbi + 1;
19357 +               wbi = -1;
19358 +               for (; bindex <= bbot; bindex++) {
19359 +                       br = au_sbr(sb, bindex);
19360 +                       if (au_br_writable(br->br_perm)) {
19361 +                               wbi = bindex;
19362 +                               br = au_sbr(sb, wbi);
19363 +                               break;
19364 +                       }
19365 +               }
19366 +       }
19367 +       AuDbg("wbi %d\n", wbi);
19368 +       if (wbi >= 0)
19369 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19370 +                                  /*force_wr*/0);
19371 +
19372 +out_unlock:
19373 +       aufs_read_unlock(root, AuLock_IR);
19374 +       err = PTR_ERR(h_file);
19375 +       if (IS_ERR(h_file))
19376 +               goto out_fd;
19377 +
19378 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19379 +       fd_install(fd, h_file);
19380 +       err = fd;
19381 +       goto out; /* success */
19382 +
19383 +out_fd:
19384 +       put_unused_fd(fd);
19385 +out:
19386 +       AuTraceErr(err);
19387 +       return err;
19388 +}
19389 +
19390 +/* ---------------------------------------------------------------------- */
19391 +
19392 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19393 +{
19394 +       long err;
19395 +       struct dentry *dentry;
19396 +
19397 +       switch (cmd) {
19398 +       case AUFS_CTL_RDU:
19399 +       case AUFS_CTL_RDU_INO:
19400 +               err = au_rdu_ioctl(file, cmd, arg);
19401 +               break;
19402 +
19403 +       case AUFS_CTL_WBR_FD:
19404 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19405 +               break;
19406 +
19407 +       case AUFS_CTL_IBUSY:
19408 +               err = au_ibusy_ioctl(file, arg);
19409 +               break;
19410 +
19411 +       case AUFS_CTL_BRINFO:
19412 +               err = au_brinfo_ioctl(file, arg);
19413 +               break;
19414 +
19415 +       case AUFS_CTL_FHSM_FD:
19416 +               dentry = file->f_path.dentry;
19417 +               if (IS_ROOT(dentry))
19418 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19419 +               else
19420 +                       err = -ENOTTY;
19421 +               break;
19422 +
19423 +       default:
19424 +               /* do not call the lower */
19425 +               AuDbg("0x%x\n", cmd);
19426 +               err = -ENOTTY;
19427 +       }
19428 +
19429 +       AuTraceErr(err);
19430 +       return err;
19431 +}
19432 +
19433 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19434 +{
19435 +       long err;
19436 +
19437 +       switch (cmd) {
19438 +       case AUFS_CTL_MVDOWN:
19439 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19440 +               break;
19441 +
19442 +       case AUFS_CTL_WBR_FD:
19443 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19444 +               break;
19445 +
19446 +       default:
19447 +               /* do not call the lower */
19448 +               AuDbg("0x%x\n", cmd);
19449 +               err = -ENOTTY;
19450 +       }
19451 +
19452 +       AuTraceErr(err);
19453 +       return err;
19454 +}
19455 +
19456 +#ifdef CONFIG_COMPAT
19457 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19458 +                          unsigned long arg)
19459 +{
19460 +       long err;
19461 +
19462 +       switch (cmd) {
19463 +       case AUFS_CTL_RDU:
19464 +       case AUFS_CTL_RDU_INO:
19465 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19466 +               break;
19467 +
19468 +       case AUFS_CTL_IBUSY:
19469 +               err = au_ibusy_compat_ioctl(file, arg);
19470 +               break;
19471 +
19472 +       case AUFS_CTL_BRINFO:
19473 +               err = au_brinfo_compat_ioctl(file, arg);
19474 +               break;
19475 +
19476 +       default:
19477 +               err = aufs_ioctl_dir(file, cmd, arg);
19478 +       }
19479 +
19480 +       AuTraceErr(err);
19481 +       return err;
19482 +}
19483 +
19484 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19485 +                             unsigned long arg)
19486 +{
19487 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19488 +}
19489 +#endif
19490 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19491 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19492 +++ linux/fs/aufs/i_op_add.c    2020-01-27 10:57:18.172204883 +0100
19493 @@ -0,0 +1,936 @@
19494 +// SPDX-License-Identifier: GPL-2.0
19495 +/*
19496 + * Copyright (C) 2005-2020 Junjiro R. Okajima
19497 + *
19498 + * This program, aufs is free software; you can redistribute it and/or modify
19499 + * it under the terms of the GNU General Public License as published by
19500 + * the Free Software Foundation; either version 2 of the License, or
19501 + * (at your option) any later version.
19502 + *
19503 + * This program is distributed in the hope that it will be useful,
19504 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19505 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19506 + * GNU General Public License for more details.
19507 + *
19508 + * You should have received a copy of the GNU General Public License
19509 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19510 + */
19511 +
19512 +/*
19513 + * inode operations (add entry)
19514 + */
19515 +
19516 +#include <linux/iversion.h>
19517 +#include "aufs.h"
19518 +
19519 +/*
19520 + * final procedure of adding a new entry, except link(2).
19521 + * remove whiteout, instantiate, copyup the parent dir's times and size
19522 + * and update version.
19523 + * if it failed, re-create the removed whiteout.
19524 + */
19525 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19526 +                 struct dentry *wh_dentry, struct dentry *dentry)
19527 +{
19528 +       int err, rerr;
19529 +       aufs_bindex_t bwh;
19530 +       struct path h_path;
19531 +       struct super_block *sb;
19532 +       struct inode *inode, *h_dir;
19533 +       struct dentry *wh;
19534 +
19535 +       bwh = -1;
19536 +       sb = dir->i_sb;
19537 +       if (wh_dentry) {
19538 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19539 +               IMustLock(h_dir);
19540 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19541 +               bwh = au_dbwh(dentry);
19542 +               h_path.dentry = wh_dentry;
19543 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19544 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19545 +                                         dentry);
19546 +               if (unlikely(err))
19547 +                       goto out;
19548 +       }
19549 +
19550 +       inode = au_new_inode(dentry, /*must_new*/1);
19551 +       if (!IS_ERR(inode)) {
19552 +               d_instantiate(dentry, inode);
19553 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19554 +               IMustLock(dir);
19555 +               au_dir_ts(dir, bindex);
19556 +               inode_inc_iversion(dir);
19557 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19558 +               return 0; /* success */
19559 +       }
19560 +
19561 +       err = PTR_ERR(inode);
19562 +       if (!wh_dentry)
19563 +               goto out;
19564 +
19565 +       /* revert */
19566 +       /* dir inode is locked */
19567 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19568 +       rerr = PTR_ERR(wh);
19569 +       if (IS_ERR(wh)) {
19570 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19571 +                       dentry, err, rerr);
19572 +               err = -EIO;
19573 +       } else
19574 +               dput(wh);
19575 +
19576 +out:
19577 +       return err;
19578 +}
19579 +
19580 +static int au_d_may_add(struct dentry *dentry)
19581 +{
19582 +       int err;
19583 +
19584 +       err = 0;
19585 +       if (unlikely(d_unhashed(dentry)))
19586 +               err = -ENOENT;
19587 +       if (unlikely(d_really_is_positive(dentry)))
19588 +               err = -EEXIST;
19589 +       return err;
19590 +}
19591 +
19592 +/*
19593 + * simple tests for the adding inode operations.
19594 + * following the checks in vfs, plus the parent-child relationship.
19595 + */
19596 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19597 +              struct dentry *h_parent, int isdir)
19598 +{
19599 +       int err;
19600 +       umode_t h_mode;
19601 +       struct dentry *h_dentry;
19602 +       struct inode *h_inode;
19603 +
19604 +       err = -ENAMETOOLONG;
19605 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19606 +               goto out;
19607 +
19608 +       h_dentry = au_h_dptr(dentry, bindex);
19609 +       if (d_really_is_negative(dentry)) {
19610 +               err = -EEXIST;
19611 +               if (unlikely(d_is_positive(h_dentry)))
19612 +                       goto out;
19613 +       } else {
19614 +               /* rename(2) case */
19615 +               err = -EIO;
19616 +               if (unlikely(d_is_negative(h_dentry)))
19617 +                       goto out;
19618 +               h_inode = d_inode(h_dentry);
19619 +               if (unlikely(!h_inode->i_nlink))
19620 +                       goto out;
19621 +
19622 +               h_mode = h_inode->i_mode;
19623 +               if (!isdir) {
19624 +                       err = -EISDIR;
19625 +                       if (unlikely(S_ISDIR(h_mode)))
19626 +                               goto out;
19627 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19628 +                       err = -ENOTDIR;
19629 +                       goto out;
19630 +               }
19631 +       }
19632 +
19633 +       err = 0;
19634 +       /* expected parent dir is locked */
19635 +       if (unlikely(h_parent != h_dentry->d_parent))
19636 +               err = -EIO;
19637 +
19638 +out:
19639 +       AuTraceErr(err);
19640 +       return err;
19641 +}
19642 +
19643 +/*
19644 + * initial procedure of adding a new entry.
19645 + * prepare writable branch and the parent dir, lock it,
19646 + * and lookup whiteout for the new entry.
19647 + */
19648 +static struct dentry*
19649 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19650 +                 struct dentry *src_dentry, struct au_pin *pin,
19651 +                 struct au_wr_dir_args *wr_dir_args)
19652 +{
19653 +       struct dentry *wh_dentry, *h_parent;
19654 +       struct super_block *sb;
19655 +       struct au_branch *br;
19656 +       int err;
19657 +       unsigned int udba;
19658 +       aufs_bindex_t bcpup;
19659 +
19660 +       AuDbg("%pd\n", dentry);
19661 +
19662 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19663 +       bcpup = err;
19664 +       wh_dentry = ERR_PTR(err);
19665 +       if (unlikely(err < 0))
19666 +               goto out;
19667 +
19668 +       sb = dentry->d_sb;
19669 +       udba = au_opt_udba(sb);
19670 +       err = au_pin(pin, dentry, bcpup, udba,
19671 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19672 +       wh_dentry = ERR_PTR(err);
19673 +       if (unlikely(err))
19674 +               goto out;
19675 +
19676 +       h_parent = au_pinned_h_parent(pin);
19677 +       if (udba != AuOpt_UDBA_NONE
19678 +           && au_dbtop(dentry) == bcpup)
19679 +               err = au_may_add(dentry, bcpup, h_parent,
19680 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19681 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19682 +               err = -ENAMETOOLONG;
19683 +       wh_dentry = ERR_PTR(err);
19684 +       if (unlikely(err))
19685 +               goto out_unpin;
19686 +
19687 +       br = au_sbr(sb, bcpup);
19688 +       if (dt) {
19689 +               struct path tmp = {
19690 +                       .dentry = h_parent,
19691 +                       .mnt    = au_br_mnt(br)
19692 +               };
19693 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19694 +       }
19695 +
19696 +       wh_dentry = NULL;
19697 +       if (bcpup != au_dbwh(dentry))
19698 +               goto out; /* success */
19699 +
19700 +       /*
19701 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19702 +        * would not be able to removed in the future. So we don't allow such
19703 +        * name here and we don't handle ENAMETOOLONG differently here.
19704 +        */
19705 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19706 +
19707 +out_unpin:
19708 +       if (IS_ERR(wh_dentry))
19709 +               au_unpin(pin);
19710 +out:
19711 +       return wh_dentry;
19712 +}
19713 +
19714 +/* ---------------------------------------------------------------------- */
19715 +
19716 +enum { Mknod, Symlink, Creat };
19717 +struct simple_arg {
19718 +       int type;
19719 +       union {
19720 +               struct {
19721 +                       umode_t                 mode;
19722 +                       bool                    want_excl;
19723 +                       bool                    try_aopen;
19724 +                       struct vfsub_aopen_args *aopen;
19725 +               } c;
19726 +               struct {
19727 +                       const char *symname;
19728 +               } s;
19729 +               struct {
19730 +                       umode_t mode;
19731 +                       dev_t dev;
19732 +               } m;
19733 +       } u;
19734 +};
19735 +
19736 +static int add_simple(struct inode *dir, struct dentry *dentry,
19737 +                     struct simple_arg *arg)
19738 +{
19739 +       int err, rerr;
19740 +       aufs_bindex_t btop;
19741 +       unsigned char created;
19742 +       const unsigned char try_aopen
19743 +               = (arg->type == Creat && arg->u.c.try_aopen);
19744 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19745 +       struct dentry *wh_dentry, *parent;
19746 +       struct inode *h_dir;
19747 +       struct super_block *sb;
19748 +       struct au_branch *br;
19749 +       /* to reduce stack size */
19750 +       struct {
19751 +               struct au_dtime dt;
19752 +               struct au_pin pin;
19753 +               struct path h_path;
19754 +               struct au_wr_dir_args wr_dir_args;
19755 +       } *a;
19756 +
19757 +       AuDbg("%pd\n", dentry);
19758 +       IMustLock(dir);
19759 +
19760 +       err = -ENOMEM;
19761 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19762 +       if (unlikely(!a))
19763 +               goto out;
19764 +       a->wr_dir_args.force_btgt = -1;
19765 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19766 +
19767 +       parent = dentry->d_parent; /* dir inode is locked */
19768 +       if (!try_aopen) {
19769 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19770 +               if (unlikely(err))
19771 +                       goto out_free;
19772 +       }
19773 +       err = au_d_may_add(dentry);
19774 +       if (unlikely(err))
19775 +               goto out_unlock;
19776 +       if (!try_aopen)
19777 +               di_write_lock_parent(parent);
19778 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19779 +                                     &a->pin, &a->wr_dir_args);
19780 +       err = PTR_ERR(wh_dentry);
19781 +       if (IS_ERR(wh_dentry))
19782 +               goto out_parent;
19783 +
19784 +       btop = au_dbtop(dentry);
19785 +       sb = dentry->d_sb;
19786 +       br = au_sbr(sb, btop);
19787 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19788 +       a->h_path.mnt = au_br_mnt(br);
19789 +       h_dir = au_pinned_h_dir(&a->pin);
19790 +       switch (arg->type) {
19791 +       case Creat:
19792 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19793 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19794 +                                          arg->u.c.want_excl);
19795 +                       created = !err;
19796 +                       if (!err && try_aopen)
19797 +                               aopen->file->f_mode |= FMODE_CREATED;
19798 +               } else {
19799 +                       aopen->br = br;
19800 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19801 +                       AuDbg("err %d\n", err);
19802 +                       AuDbgFile(aopen->file);
19803 +                       created = err >= 0
19804 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19805 +               }
19806 +               break;
19807 +       case Symlink:
19808 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19809 +               created = !err;
19810 +               break;
19811 +       case Mknod:
19812 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19813 +                                 arg->u.m.dev);
19814 +               created = !err;
19815 +               break;
19816 +       default:
19817 +               BUG();
19818 +       }
19819 +       if (unlikely(err < 0))
19820 +               goto out_unpin;
19821 +
19822 +       err = epilog(dir, btop, wh_dentry, dentry);
19823 +       if (!err)
19824 +               goto out_unpin; /* success */
19825 +
19826 +       /* revert */
19827 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19828 +               /* no delegation since it is just created */
19829 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19830 +                                   /*force*/0);
19831 +               if (rerr) {
19832 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19833 +                               dentry, err, rerr);
19834 +                       err = -EIO;
19835 +               }
19836 +               au_dtime_revert(&a->dt);
19837 +       }
19838 +       if (try_aopen && h_dir->i_op->atomic_open
19839 +           && (aopen->file->f_mode & FMODE_OPENED))
19840 +               /* aopen->file is still opened */
19841 +               au_lcnt_dec(&aopen->br->br_nfiles);
19842 +
19843 +out_unpin:
19844 +       au_unpin(&a->pin);
19845 +       dput(wh_dentry);
19846 +out_parent:
19847 +       if (!try_aopen)
19848 +               di_write_unlock(parent);
19849 +out_unlock:
19850 +       if (unlikely(err)) {
19851 +               au_update_dbtop(dentry);
19852 +               d_drop(dentry);
19853 +       }
19854 +       if (!try_aopen)
19855 +               aufs_read_unlock(dentry, AuLock_DW);
19856 +out_free:
19857 +       au_kfree_rcu(a);
19858 +out:
19859 +       return err;
19860 +}
19861 +
19862 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
19863 +              dev_t dev)
19864 +{
19865 +       struct simple_arg arg = {
19866 +               .type = Mknod,
19867 +               .u.m = {
19868 +                       .mode   = mode,
19869 +                       .dev    = dev
19870 +               }
19871 +       };
19872 +       return add_simple(dir, dentry, &arg);
19873 +}
19874 +
19875 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
19876 +{
19877 +       struct simple_arg arg = {
19878 +               .type = Symlink,
19879 +               .u.s.symname = symname
19880 +       };
19881 +       return add_simple(dir, dentry, &arg);
19882 +}
19883 +
19884 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
19885 +               bool want_excl)
19886 +{
19887 +       struct simple_arg arg = {
19888 +               .type = Creat,
19889 +               .u.c = {
19890 +                       .mode           = mode,
19891 +                       .want_excl      = want_excl
19892 +               }
19893 +       };
19894 +       return add_simple(dir, dentry, &arg);
19895 +}
19896 +
19897 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19898 +                      struct vfsub_aopen_args *aopen_args)
19899 +{
19900 +       struct simple_arg arg = {
19901 +               .type = Creat,
19902 +               .u.c = {
19903 +                       .mode           = aopen_args->create_mode,
19904 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
19905 +                       .try_aopen      = true,
19906 +                       .aopen          = aopen_args
19907 +               }
19908 +       };
19909 +       return add_simple(dir, dentry, &arg);
19910 +}
19911 +
19912 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
19913 +{
19914 +       int err;
19915 +       aufs_bindex_t bindex;
19916 +       struct super_block *sb;
19917 +       struct dentry *parent, *h_parent, *h_dentry;
19918 +       struct inode *h_dir, *inode;
19919 +       struct vfsmount *h_mnt;
19920 +       struct au_wr_dir_args wr_dir_args = {
19921 +               .force_btgt     = -1,
19922 +               .flags          = AuWrDir_TMPFILE
19923 +       };
19924 +
19925 +       /* copy-up may happen */
19926 +       inode_lock(dir);
19927 +
19928 +       sb = dir->i_sb;
19929 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19930 +       if (unlikely(err))
19931 +               goto out;
19932 +
19933 +       err = au_di_init(dentry);
19934 +       if (unlikely(err))
19935 +               goto out_si;
19936 +
19937 +       err = -EBUSY;
19938 +       parent = d_find_any_alias(dir);
19939 +       AuDebugOn(!parent);
19940 +       di_write_lock_parent(parent);
19941 +       if (unlikely(d_inode(parent) != dir))
19942 +               goto out_parent;
19943 +
19944 +       err = au_digen_test(parent, au_sigen(sb));
19945 +       if (unlikely(err))
19946 +               goto out_parent;
19947 +
19948 +       bindex = au_dbtop(parent);
19949 +       au_set_dbtop(dentry, bindex);
19950 +       au_set_dbbot(dentry, bindex);
19951 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19952 +       bindex = err;
19953 +       if (unlikely(err < 0))
19954 +               goto out_parent;
19955 +
19956 +       err = -EOPNOTSUPP;
19957 +       h_dir = au_h_iptr(dir, bindex);
19958 +       if (unlikely(!h_dir->i_op->tmpfile))
19959 +               goto out_parent;
19960 +
19961 +       h_mnt = au_sbr_mnt(sb, bindex);
19962 +       err = vfsub_mnt_want_write(h_mnt);
19963 +       if (unlikely(err))
19964 +               goto out_parent;
19965 +
19966 +       h_parent = au_h_dptr(parent, bindex);
19967 +       h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
19968 +       if (IS_ERR(h_dentry)) {
19969 +               err = PTR_ERR(h_dentry);
19970 +               goto out_mnt;
19971 +       }
19972 +
19973 +       au_set_dbtop(dentry, bindex);
19974 +       au_set_dbbot(dentry, bindex);
19975 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
19976 +       inode = au_new_inode(dentry, /*must_new*/1);
19977 +       if (IS_ERR(inode)) {
19978 +               err = PTR_ERR(inode);
19979 +               au_set_h_dptr(dentry, bindex, NULL);
19980 +               au_set_dbtop(dentry, -1);
19981 +               au_set_dbbot(dentry, -1);
19982 +       } else {
19983 +               if (!inode->i_nlink)
19984 +                       set_nlink(inode, 1);
19985 +               d_tmpfile(dentry, inode);
19986 +               au_di(dentry)->di_tmpfile = 1;
19987 +
19988 +               /* update without i_mutex */
19989 +               if (au_ibtop(dir) == au_dbtop(dentry))
19990 +                       au_cpup_attr_timesizes(dir);
19991 +       }
19992 +       dput(h_dentry);
19993 +
19994 +out_mnt:
19995 +       vfsub_mnt_drop_write(h_mnt);
19996 +out_parent:
19997 +       di_write_unlock(parent);
19998 +       dput(parent);
19999 +       di_write_unlock(dentry);
20000 +       if (unlikely(err)) {
20001 +               au_di_fin(dentry);
20002 +               dentry->d_fsdata = NULL;
20003 +       }
20004 +out_si:
20005 +       si_read_unlock(sb);
20006 +out:
20007 +       inode_unlock(dir);
20008 +       return err;
20009 +}
20010 +
20011 +/* ---------------------------------------------------------------------- */
20012 +
20013 +struct au_link_args {
20014 +       aufs_bindex_t bdst, bsrc;
20015 +       struct au_pin pin;
20016 +       struct path h_path;
20017 +       struct dentry *src_parent, *parent;
20018 +};
20019 +
20020 +static int au_cpup_before_link(struct dentry *src_dentry,
20021 +                              struct au_link_args *a)
20022 +{
20023 +       int err;
20024 +       struct dentry *h_src_dentry;
20025 +       struct au_cp_generic cpg = {
20026 +               .dentry = src_dentry,
20027 +               .bdst   = a->bdst,
20028 +               .bsrc   = a->bsrc,
20029 +               .len    = -1,
20030 +               .pin    = &a->pin,
20031 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20032 +       };
20033 +
20034 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20035 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20036 +       if (unlikely(err))
20037 +               goto out;
20038 +
20039 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20040 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20041 +                    au_opt_udba(src_dentry->d_sb),
20042 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20043 +       if (unlikely(err))
20044 +               goto out;
20045 +
20046 +       err = au_sio_cpup_simple(&cpg);
20047 +       au_unpin(&a->pin);
20048 +
20049 +out:
20050 +       di_read_unlock(a->src_parent, AuLock_IR);
20051 +       return err;
20052 +}
20053 +
20054 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20055 +                          struct au_link_args *a)
20056 +{
20057 +       int err;
20058 +       unsigned char plink;
20059 +       aufs_bindex_t bbot;
20060 +       struct dentry *h_src_dentry;
20061 +       struct inode *h_inode, *inode, *delegated;
20062 +       struct super_block *sb;
20063 +       struct file *h_file;
20064 +
20065 +       plink = 0;
20066 +       h_inode = NULL;
20067 +       sb = src_dentry->d_sb;
20068 +       inode = d_inode(src_dentry);
20069 +       if (au_ibtop(inode) <= a->bdst)
20070 +               h_inode = au_h_iptr(inode, a->bdst);
20071 +       if (!h_inode || !h_inode->i_nlink) {
20072 +               /* copyup src_dentry as the name of dentry. */
20073 +               bbot = au_dbbot(dentry);
20074 +               if (bbot < a->bsrc)
20075 +                       au_set_dbbot(dentry, a->bsrc);
20076 +               au_set_h_dptr(dentry, a->bsrc,
20077 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20078 +               dget(a->h_path.dentry);
20079 +               au_set_h_dptr(dentry, a->bdst, NULL);
20080 +               AuDbg("temporary d_inode...\n");
20081 +               spin_lock(&dentry->d_lock);
20082 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20083 +               spin_unlock(&dentry->d_lock);
20084 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20085 +               if (IS_ERR(h_file))
20086 +                       err = PTR_ERR(h_file);
20087 +               else {
20088 +                       struct au_cp_generic cpg = {
20089 +                               .dentry = dentry,
20090 +                               .bdst   = a->bdst,
20091 +                               .bsrc   = -1,
20092 +                               .len    = -1,
20093 +                               .pin    = &a->pin,
20094 +                               .flags  = AuCpup_KEEPLINO
20095 +                       };
20096 +                       err = au_sio_cpup_simple(&cpg);
20097 +                       au_h_open_post(dentry, a->bsrc, h_file);
20098 +                       if (!err) {
20099 +                               dput(a->h_path.dentry);
20100 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20101 +                       } else
20102 +                               au_set_h_dptr(dentry, a->bdst,
20103 +                                             a->h_path.dentry);
20104 +               }
20105 +               spin_lock(&dentry->d_lock);
20106 +               dentry->d_inode = NULL; /* restore */
20107 +               spin_unlock(&dentry->d_lock);
20108 +               AuDbg("temporary d_inode...done\n");
20109 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20110 +               au_set_dbbot(dentry, bbot);
20111 +       } else {
20112 +               /* the inode of src_dentry already exists on a.bdst branch */
20113 +               h_src_dentry = d_find_alias(h_inode);
20114 +               if (!h_src_dentry && au_plink_test(inode)) {
20115 +                       plink = 1;
20116 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20117 +                       err = PTR_ERR(h_src_dentry);
20118 +                       if (IS_ERR(h_src_dentry))
20119 +                               goto out;
20120 +
20121 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20122 +                               dput(h_src_dentry);
20123 +                               h_src_dentry = NULL;
20124 +                       }
20125 +
20126 +               }
20127 +               if (h_src_dentry) {
20128 +                       delegated = NULL;
20129 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20130 +                                        &a->h_path, &delegated);
20131 +                       if (unlikely(err == -EWOULDBLOCK)) {
20132 +                               pr_warn("cannot retry for NFSv4 delegation"
20133 +                                       " for an internal link\n");
20134 +                               iput(delegated);
20135 +                       }
20136 +                       dput(h_src_dentry);
20137 +               } else {
20138 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20139 +                               h_inode->i_ino, a->bdst);
20140 +                       err = -EIO;
20141 +               }
20142 +       }
20143 +
20144 +       if (!err && !plink)
20145 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20146 +
20147 +out:
20148 +       AuTraceErr(err);
20149 +       return err;
20150 +}
20151 +
20152 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20153 +             struct dentry *dentry)
20154 +{
20155 +       int err, rerr;
20156 +       struct au_dtime dt;
20157 +       struct au_link_args *a;
20158 +       struct dentry *wh_dentry, *h_src_dentry;
20159 +       struct inode *inode, *delegated;
20160 +       struct super_block *sb;
20161 +       struct au_wr_dir_args wr_dir_args = {
20162 +               /* .force_btgt  = -1, */
20163 +               .flags          = AuWrDir_ADD_ENTRY
20164 +       };
20165 +
20166 +       IMustLock(dir);
20167 +       inode = d_inode(src_dentry);
20168 +       IMustLock(inode);
20169 +
20170 +       err = -ENOMEM;
20171 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20172 +       if (unlikely(!a))
20173 +               goto out;
20174 +
20175 +       a->parent = dentry->d_parent; /* dir inode is locked */
20176 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20177 +                                       AuLock_NOPLM | AuLock_GEN);
20178 +       if (unlikely(err))
20179 +               goto out_kfree;
20180 +       err = au_d_linkable(src_dentry);
20181 +       if (unlikely(err))
20182 +               goto out_unlock;
20183 +       err = au_d_may_add(dentry);
20184 +       if (unlikely(err))
20185 +               goto out_unlock;
20186 +
20187 +       a->src_parent = dget_parent(src_dentry);
20188 +       wr_dir_args.force_btgt = au_ibtop(inode);
20189 +
20190 +       di_write_lock_parent(a->parent);
20191 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20192 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20193 +                                     &wr_dir_args);
20194 +       err = PTR_ERR(wh_dentry);
20195 +       if (IS_ERR(wh_dentry))
20196 +               goto out_parent;
20197 +
20198 +       err = 0;
20199 +       sb = dentry->d_sb;
20200 +       a->bdst = au_dbtop(dentry);
20201 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20202 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20203 +       a->bsrc = au_ibtop(inode);
20204 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20205 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20206 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20207 +       if (!h_src_dentry) {
20208 +               a->bsrc = au_dbtop(src_dentry);
20209 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20210 +               AuDebugOn(!h_src_dentry);
20211 +       } else if (IS_ERR(h_src_dentry)) {
20212 +               err = PTR_ERR(h_src_dentry);
20213 +               goto out_parent;
20214 +       }
20215 +
20216 +       /*
20217 +        * aufs doesn't touch the credential so
20218 +        * security_dentry_create_files_as() is unnecessary.
20219 +        */
20220 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20221 +               if (a->bdst < a->bsrc
20222 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20223 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20224 +               else {
20225 +                       delegated = NULL;
20226 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20227 +                                        &a->h_path, &delegated);
20228 +                       if (unlikely(err == -EWOULDBLOCK)) {
20229 +                               pr_warn("cannot retry for NFSv4 delegation"
20230 +                                       " for an internal link\n");
20231 +                               iput(delegated);
20232 +                       }
20233 +               }
20234 +               dput(h_src_dentry);
20235 +       } else {
20236 +               /*
20237 +                * copyup src_dentry to the branch we process,
20238 +                * and then link(2) to it.
20239 +                */
20240 +               dput(h_src_dentry);
20241 +               if (a->bdst < a->bsrc
20242 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20243 +                       au_unpin(&a->pin);
20244 +                       di_write_unlock(a->parent);
20245 +                       err = au_cpup_before_link(src_dentry, a);
20246 +                       di_write_lock_parent(a->parent);
20247 +                       if (!err)
20248 +                               err = au_pin(&a->pin, dentry, a->bdst,
20249 +                                            au_opt_udba(sb),
20250 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20251 +                       if (unlikely(err))
20252 +                               goto out_wh;
20253 +               }
20254 +               if (!err) {
20255 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20256 +                       err = -ENOENT;
20257 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20258 +                               delegated = NULL;
20259 +                               err = vfsub_link(h_src_dentry,
20260 +                                                au_pinned_h_dir(&a->pin),
20261 +                                                &a->h_path, &delegated);
20262 +                               if (unlikely(err == -EWOULDBLOCK)) {
20263 +                                       pr_warn("cannot retry"
20264 +                                               " for NFSv4 delegation"
20265 +                                               " for an internal link\n");
20266 +                                       iput(delegated);
20267 +                               }
20268 +                       }
20269 +               }
20270 +       }
20271 +       if (unlikely(err))
20272 +               goto out_unpin;
20273 +
20274 +       if (wh_dentry) {
20275 +               a->h_path.dentry = wh_dentry;
20276 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20277 +                                         dentry);
20278 +               if (unlikely(err))
20279 +                       goto out_revert;
20280 +       }
20281 +
20282 +       au_dir_ts(dir, a->bdst);
20283 +       inode_inc_iversion(dir);
20284 +       inc_nlink(inode);
20285 +       inode->i_ctime = dir->i_ctime;
20286 +       d_instantiate(dentry, au_igrab(inode));
20287 +       if (d_unhashed(a->h_path.dentry))
20288 +               /* some filesystem calls d_drop() */
20289 +               d_drop(dentry);
20290 +       /* some filesystems consume an inode even hardlink */
20291 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20292 +       goto out_unpin; /* success */
20293 +
20294 +out_revert:
20295 +       /* no delegation since it is just created */
20296 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20297 +                           /*delegated*/NULL, /*force*/0);
20298 +       if (unlikely(rerr)) {
20299 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20300 +               err = -EIO;
20301 +       }
20302 +       au_dtime_revert(&dt);
20303 +out_unpin:
20304 +       au_unpin(&a->pin);
20305 +out_wh:
20306 +       dput(wh_dentry);
20307 +out_parent:
20308 +       di_write_unlock(a->parent);
20309 +       dput(a->src_parent);
20310 +out_unlock:
20311 +       if (unlikely(err)) {
20312 +               au_update_dbtop(dentry);
20313 +               d_drop(dentry);
20314 +       }
20315 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20316 +out_kfree:
20317 +       au_kfree_rcu(a);
20318 +out:
20319 +       AuTraceErr(err);
20320 +       return err;
20321 +}
20322 +
20323 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
20324 +{
20325 +       int err, rerr;
20326 +       aufs_bindex_t bindex;
20327 +       unsigned char diropq;
20328 +       struct path h_path;
20329 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20330 +       struct inode *h_inode;
20331 +       struct super_block *sb;
20332 +       struct {
20333 +               struct au_pin pin;
20334 +               struct au_dtime dt;
20335 +       } *a; /* reduce the stack usage */
20336 +       struct au_wr_dir_args wr_dir_args = {
20337 +               .force_btgt     = -1,
20338 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20339 +       };
20340 +
20341 +       IMustLock(dir);
20342 +
20343 +       err = -ENOMEM;
20344 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20345 +       if (unlikely(!a))
20346 +               goto out;
20347 +
20348 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20349 +       if (unlikely(err))
20350 +               goto out_free;
20351 +       err = au_d_may_add(dentry);
20352 +       if (unlikely(err))
20353 +               goto out_unlock;
20354 +
20355 +       parent = dentry->d_parent; /* dir inode is locked */
20356 +       di_write_lock_parent(parent);
20357 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20358 +                                     &a->pin, &wr_dir_args);
20359 +       err = PTR_ERR(wh_dentry);
20360 +       if (IS_ERR(wh_dentry))
20361 +               goto out_parent;
20362 +
20363 +       sb = dentry->d_sb;
20364 +       bindex = au_dbtop(dentry);
20365 +       h_path.dentry = au_h_dptr(dentry, bindex);
20366 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20367 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20368 +       if (unlikely(err))
20369 +               goto out_unpin;
20370 +
20371 +       /* make the dir opaque */
20372 +       diropq = 0;
20373 +       h_inode = d_inode(h_path.dentry);
20374 +       if (wh_dentry
20375 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20376 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20377 +               opq_dentry = au_diropq_create(dentry, bindex);
20378 +               inode_unlock(h_inode);
20379 +               err = PTR_ERR(opq_dentry);
20380 +               if (IS_ERR(opq_dentry))
20381 +                       goto out_dir;
20382 +               dput(opq_dentry);
20383 +               diropq = 1;
20384 +       }
20385 +
20386 +       err = epilog(dir, bindex, wh_dentry, dentry);
20387 +       if (!err) {
20388 +               inc_nlink(dir);
20389 +               goto out_unpin; /* success */
20390 +       }
20391 +
20392 +       /* revert */
20393 +       if (diropq) {
20394 +               AuLabel(revert opq);
20395 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20396 +               rerr = au_diropq_remove(dentry, bindex);
20397 +               inode_unlock(h_inode);
20398 +               if (rerr) {
20399 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20400 +                               dentry, err, rerr);
20401 +                       err = -EIO;
20402 +               }
20403 +       }
20404 +
20405 +out_dir:
20406 +       AuLabel(revert dir);
20407 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20408 +       if (rerr) {
20409 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20410 +                       dentry, err, rerr);
20411 +               err = -EIO;
20412 +       }
20413 +       au_dtime_revert(&a->dt);
20414 +out_unpin:
20415 +       au_unpin(&a->pin);
20416 +       dput(wh_dentry);
20417 +out_parent:
20418 +       di_write_unlock(parent);
20419 +out_unlock:
20420 +       if (unlikely(err)) {
20421 +               au_update_dbtop(dentry);
20422 +               d_drop(dentry);
20423 +       }
20424 +       aufs_read_unlock(dentry, AuLock_DW);
20425 +out_free:
20426 +       au_kfree_rcu(a);
20427 +out:
20428 +       return err;
20429 +}
20430 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20431 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20432 +++ linux/fs/aufs/i_op.c        2020-08-03 09:14:46.095748745 +0200
20433 @@ -0,0 +1,1502 @@
20434 +// SPDX-License-Identifier: GPL-2.0
20435 +/*
20436 + * Copyright (C) 2005-2020 Junjiro R. Okajima
20437 + *
20438 + * This program, aufs is free software; you can redistribute it and/or modify
20439 + * it under the terms of the GNU General Public License as published by
20440 + * the Free Software Foundation; either version 2 of the License, or
20441 + * (at your option) any later version.
20442 + *
20443 + * This program is distributed in the hope that it will be useful,
20444 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20445 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20446 + * GNU General Public License for more details.
20447 + *
20448 + * You should have received a copy of the GNU General Public License
20449 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20450 + */
20451 +
20452 +/*
20453 + * inode operations (except add/del/rename)
20454 + */
20455 +
20456 +#include <linux/device_cgroup.h>
20457 +#include <linux/fs_stack.h>
20458 +#include <linux/iversion.h>
20459 +#include <linux/namei.h>
20460 +#include <linux/security.h>
20461 +#include "aufs.h"
20462 +
20463 +static int h_permission(struct inode *h_inode, int mask,
20464 +                       struct path *h_path, int brperm)
20465 +{
20466 +       int err;
20467 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20468 +
20469 +       err = -EPERM;
20470 +       if (write_mask && IS_IMMUTABLE(h_inode))
20471 +               goto out;
20472 +
20473 +       err = -EACCES;
20474 +       if (((mask & MAY_EXEC)
20475 +            && S_ISREG(h_inode->i_mode)
20476 +            && (path_noexec(h_path)
20477 +                || !(h_inode->i_mode & 0111))))
20478 +               goto out;
20479 +
20480 +       /*
20481 +        * - skip the lower fs test in the case of write to ro branch.
20482 +        * - nfs dir permission write check is optimized, but a policy for
20483 +        *   link/rename requires a real check.
20484 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20485 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20486 +        */
20487 +       if ((write_mask && !au_br_writable(brperm))
20488 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20489 +               && write_mask && !(mask & MAY_READ))
20490 +           || !h_inode->i_op->permission) {
20491 +               /* AuLabel(generic_permission); */
20492 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20493 +               err = generic_permission(h_inode, mask);
20494 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20495 +                       err = h_inode->i_op->permission(h_inode, mask);
20496 +               AuTraceErr(err);
20497 +       } else {
20498 +               /* AuLabel(h_inode->permission); */
20499 +               err = h_inode->i_op->permission(h_inode, mask);
20500 +               AuTraceErr(err);
20501 +       }
20502 +
20503 +       if (!err)
20504 +               err = devcgroup_inode_permission(h_inode, mask);
20505 +       if (!err)
20506 +               err = security_inode_permission(h_inode, mask);
20507 +
20508 +out:
20509 +       return err;
20510 +}
20511 +
20512 +static int aufs_permission(struct inode *inode, int mask)
20513 +{
20514 +       int err;
20515 +       aufs_bindex_t bindex, bbot;
20516 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20517 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20518 +       struct inode *h_inode;
20519 +       struct super_block *sb;
20520 +       struct au_branch *br;
20521 +
20522 +       /* todo: support rcu-walk? */
20523 +       if (mask & MAY_NOT_BLOCK)
20524 +               return -ECHILD;
20525 +
20526 +       sb = inode->i_sb;
20527 +       si_read_lock(sb, AuLock_FLUSH);
20528 +       ii_read_lock_child(inode);
20529 +#if 0 /* reserved for future use */
20530 +       /*
20531 +        * This test may be rather 'too much' since the test is essentially done
20532 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20533 +        * generation doesn't match to the superblock's here.  But it isn't a
20534 +        * big deal I suppose.
20535 +        */
20536 +       err = au_iigen_test(inode, au_sigen(sb));
20537 +       if (unlikely(err))
20538 +               goto out;
20539 +#endif
20540 +
20541 +       if (!isdir
20542 +           || write_mask
20543 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20544 +               err = au_busy_or_stale();
20545 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20546 +               if (unlikely(!h_inode
20547 +                            || (h_inode->i_mode & S_IFMT)
20548 +                            != (inode->i_mode & S_IFMT)))
20549 +                       goto out;
20550 +
20551 +               err = 0;
20552 +               bindex = au_ibtop(inode);
20553 +               br = au_sbr(sb, bindex);
20554 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20555 +               if (write_mask
20556 +                   && !err
20557 +                   && !special_file(h_inode->i_mode)) {
20558 +                       /* test whether the upper writable branch exists */
20559 +                       err = -EROFS;
20560 +                       for (; bindex >= 0; bindex--)
20561 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20562 +                                       err = 0;
20563 +                                       break;
20564 +                               }
20565 +               }
20566 +               goto out;
20567 +       }
20568 +
20569 +       /* non-write to dir */
20570 +       err = 0;
20571 +       bbot = au_ibbot(inode);
20572 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20573 +               h_inode = au_h_iptr(inode, bindex);
20574 +               if (h_inode) {
20575 +                       err = au_busy_or_stale();
20576 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20577 +                               break;
20578 +
20579 +                       br = au_sbr(sb, bindex);
20580 +                       err = h_permission(h_inode, mask, &br->br_path,
20581 +                                          br->br_perm);
20582 +               }
20583 +       }
20584 +
20585 +out:
20586 +       ii_read_unlock(inode);
20587 +       si_read_unlock(sb);
20588 +       return err;
20589 +}
20590 +
20591 +/* ---------------------------------------------------------------------- */
20592 +
20593 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20594 +                                 unsigned int flags)
20595 +{
20596 +       struct dentry *ret, *parent;
20597 +       struct inode *inode;
20598 +       struct super_block *sb;
20599 +       int err, npositive;
20600 +
20601 +       IMustLock(dir);
20602 +
20603 +       /* todo: support rcu-walk? */
20604 +       ret = ERR_PTR(-ECHILD);
20605 +       if (flags & LOOKUP_RCU)
20606 +               goto out;
20607 +
20608 +       ret = ERR_PTR(-ENAMETOOLONG);
20609 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20610 +               goto out;
20611 +
20612 +       sb = dir->i_sb;
20613 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20614 +       ret = ERR_PTR(err);
20615 +       if (unlikely(err))
20616 +               goto out;
20617 +
20618 +       err = au_di_init(dentry);
20619 +       ret = ERR_PTR(err);
20620 +       if (unlikely(err))
20621 +               goto out_si;
20622 +
20623 +       inode = NULL;
20624 +       npositive = 0; /* suppress a warning */
20625 +       parent = dentry->d_parent; /* dir inode is locked */
20626 +       di_read_lock_parent(parent, AuLock_IR);
20627 +       err = au_alive_dir(parent);
20628 +       if (!err)
20629 +               err = au_digen_test(parent, au_sigen(sb));
20630 +       if (!err) {
20631 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20632 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20633 +                                          AuLkup_ALLOW_NEG);
20634 +               err = npositive;
20635 +       }
20636 +       di_read_unlock(parent, AuLock_IR);
20637 +       ret = ERR_PTR(err);
20638 +       if (unlikely(err < 0))
20639 +               goto out_unlock;
20640 +
20641 +       if (npositive) {
20642 +               inode = au_new_inode(dentry, /*must_new*/0);
20643 +               if (IS_ERR(inode)) {
20644 +                       ret = (void *)inode;
20645 +                       inode = NULL;
20646 +                       goto out_unlock;
20647 +               }
20648 +       }
20649 +
20650 +       if (inode)
20651 +               atomic_inc(&inode->i_count);
20652 +       ret = d_splice_alias(inode, dentry);
20653 +#if 0 /* reserved for future use */
20654 +       if (unlikely(d_need_lookup(dentry))) {
20655 +               spin_lock(&dentry->d_lock);
20656 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20657 +               spin_unlock(&dentry->d_lock);
20658 +       } else
20659 +#endif
20660 +       if (inode) {
20661 +               if (!IS_ERR(ret)) {
20662 +                       iput(inode);
20663 +                       if (ret && ret != dentry)
20664 +                               ii_write_unlock(inode);
20665 +               } else {
20666 +                       ii_write_unlock(inode);
20667 +                       iput(inode);
20668 +                       inode = NULL;
20669 +               }
20670 +       }
20671 +
20672 +out_unlock:
20673 +       di_write_unlock(dentry);
20674 +out_si:
20675 +       si_read_unlock(sb);
20676 +out:
20677 +       return ret;
20678 +}
20679 +
20680 +/* ---------------------------------------------------------------------- */
20681 +
20682 +/*
20683 + * very dirty and complicated aufs ->atomic_open().
20684 + * aufs_atomic_open()
20685 + * + au_aopen_or_create()
20686 + *   + add_simple()
20687 + *     + vfsub_atomic_open()
20688 + *       + branch fs ->atomic_open()
20689 + *        may call the actual 'open' for h_file
20690 + *       + inc br_nfiles only if opened
20691 + * + au_aopen_no_open() or au_aopen_do_open()
20692 + *
20693 + * au_aopen_do_open()
20694 + * + finish_open()
20695 + *   + au_do_aopen()
20696 + *     + au_do_open() the body of all 'open'
20697 + *       + au_do_open_nondir()
20698 + *        set the passed h_file
20699 + *
20700 + * au_aopen_no_open()
20701 + * + finish_no_open()
20702 + */
20703 +
20704 +struct aopen_node {
20705 +       struct hlist_bl_node hblist;
20706 +       struct file *file, *h_file;
20707 +};
20708 +
20709 +static int au_do_aopen(struct inode *inode, struct file *file)
20710 +{
20711 +       struct hlist_bl_head *aopen;
20712 +       struct hlist_bl_node *pos;
20713 +       struct aopen_node *node;
20714 +       struct au_do_open_args args = {
20715 +               .aopen  = 1,
20716 +               .open   = au_do_open_nondir
20717 +       };
20718 +
20719 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20720 +       hlist_bl_lock(aopen);
20721 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20722 +               if (node->file == file) {
20723 +                       args.h_file = node->h_file;
20724 +                       break;
20725 +               }
20726 +       hlist_bl_unlock(aopen);
20727 +       /* AuDebugOn(!args.h_file); */
20728 +
20729 +       return au_do_open(file, &args);
20730 +}
20731 +
20732 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20733 +                           struct aopen_node *aopen_node)
20734 +{
20735 +       int err;
20736 +       struct hlist_bl_head *aopen;
20737 +
20738 +       AuLabel(here);
20739 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20740 +       au_hbl_add(&aopen_node->hblist, aopen);
20741 +       err = finish_open(file, dentry, au_do_aopen);
20742 +       au_hbl_del(&aopen_node->hblist, aopen);
20743 +       /* AuDbgFile(file); */
20744 +       AuDbg("%pd%s%s\n", dentry,
20745 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20746 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20747 +
20748 +       AuTraceErr(err);
20749 +       return err;
20750 +}
20751 +
20752 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20753 +{
20754 +       int err;
20755 +
20756 +       AuLabel(here);
20757 +       dget(dentry);
20758 +       err = finish_no_open(file, dentry);
20759 +
20760 +       AuTraceErr(err);
20761 +       return err;
20762 +}
20763 +
20764 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20765 +                           struct file *file, unsigned int open_flag,
20766 +                           umode_t create_mode)
20767 +{
20768 +       int err, did_open;
20769 +       unsigned int lkup_flags;
20770 +       aufs_bindex_t bindex;
20771 +       struct super_block *sb;
20772 +       struct dentry *parent, *d;
20773 +       struct vfsub_aopen_args args = {
20774 +               .open_flag      = open_flag,
20775 +               .create_mode    = create_mode
20776 +       };
20777 +       struct aopen_node aopen_node = {
20778 +               .file   = file
20779 +       };
20780 +
20781 +       IMustLock(dir);
20782 +       AuDbg("open_flag 0%o\n", open_flag);
20783 +       AuDbgDentry(dentry);
20784 +
20785 +       err = 0;
20786 +       if (!au_di(dentry)) {
20787 +               lkup_flags = LOOKUP_OPEN;
20788 +               if (open_flag & O_CREAT)
20789 +                       lkup_flags |= LOOKUP_CREATE;
20790 +               d = aufs_lookup(dir, dentry, lkup_flags);
20791 +               if (IS_ERR(d)) {
20792 +                       err = PTR_ERR(d);
20793 +                       AuTraceErr(err);
20794 +                       goto out;
20795 +               } else if (d) {
20796 +                       /*
20797 +                        * obsoleted dentry found.
20798 +                        * another error will be returned later.
20799 +                        */
20800 +                       d_drop(d);
20801 +                       AuDbgDentry(d);
20802 +                       dput(d);
20803 +               }
20804 +               AuDbgDentry(dentry);
20805 +       }
20806 +
20807 +       if (d_is_positive(dentry)
20808 +           || d_unhashed(dentry)
20809 +           || d_unlinked(dentry)
20810 +           || !(open_flag & O_CREAT)) {
20811 +               err = au_aopen_no_open(file, dentry);
20812 +               goto out; /* success */
20813 +       }
20814 +
20815 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20816 +       if (unlikely(err))
20817 +               goto out;
20818 +
20819 +       sb = dentry->d_sb;
20820 +       parent = dentry->d_parent;      /* dir is locked */
20821 +       di_write_lock_parent(parent);
20822 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20823 +       if (unlikely(err < 0))
20824 +               goto out_parent;
20825 +
20826 +       AuDbgDentry(dentry);
20827 +       if (d_is_positive(dentry)) {
20828 +               err = au_aopen_no_open(file, dentry);
20829 +               goto out_parent; /* success */
20830 +       }
20831 +
20832 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20833 +       err = PTR_ERR(args.file);
20834 +       if (IS_ERR(args.file))
20835 +               goto out_parent;
20836 +
20837 +       bindex = au_dbtop(dentry);
20838 +       err = au_aopen_or_create(dir, dentry, &args);
20839 +       AuTraceErr(err);
20840 +       AuDbgFile(args.file);
20841 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20842 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20843 +       if (!did_open) {
20844 +               fput(args.file);
20845 +               args.file = NULL;
20846 +       }
20847 +       di_write_unlock(parent);
20848 +       di_write_unlock(dentry);
20849 +       if (unlikely(err < 0)) {
20850 +               if (args.file)
20851 +                       fput(args.file);
20852 +               goto out_sb;
20853 +       }
20854 +
20855 +       if (!did_open)
20856 +               err = au_aopen_no_open(file, dentry);
20857 +       else {
20858 +               aopen_node.h_file = args.file;
20859 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20860 +       }
20861 +       if (unlikely(err < 0)) {
20862 +               if (args.file)
20863 +                       fput(args.file);
20864 +               if (did_open)
20865 +                       au_lcnt_dec(&args.br->br_nfiles);
20866 +       }
20867 +       goto out_sb; /* success */
20868 +
20869 +out_parent:
20870 +       di_write_unlock(parent);
20871 +       di_write_unlock(dentry);
20872 +out_sb:
20873 +       si_read_unlock(sb);
20874 +out:
20875 +       AuTraceErr(err);
20876 +       AuDbgFile(file);
20877 +       return err;
20878 +}
20879 +
20880 +
20881 +/* ---------------------------------------------------------------------- */
20882 +
20883 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20884 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
20885 +                         aufs_bindex_t btop)
20886 +{
20887 +       int err;
20888 +       struct dentry *h_parent;
20889 +       struct inode *h_dir;
20890 +
20891 +       if (add_entry)
20892 +               IMustLock(d_inode(parent));
20893 +       else
20894 +               di_write_lock_parent(parent);
20895 +
20896 +       err = 0;
20897 +       if (!au_h_dptr(parent, bcpup)) {
20898 +               if (btop > bcpup)
20899 +                       err = au_cpup_dirs(dentry, bcpup);
20900 +               else if (btop < bcpup)
20901 +                       err = au_cpdown_dirs(dentry, bcpup);
20902 +               else
20903 +                       BUG();
20904 +       }
20905 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20906 +               h_parent = au_h_dptr(parent, bcpup);
20907 +               h_dir = d_inode(h_parent);
20908 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20909 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20910 +               /* todo: no unlock here */
20911 +               inode_unlock_shared(h_dir);
20912 +
20913 +               AuDbg("bcpup %d\n", bcpup);
20914 +               if (!err) {
20915 +                       if (d_really_is_negative(dentry))
20916 +                               au_set_h_dptr(dentry, btop, NULL);
20917 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
20918 +               }
20919 +       }
20920 +
20921 +       if (!add_entry)
20922 +               di_write_unlock(parent);
20923 +       if (!err)
20924 +               err = bcpup; /* success */
20925 +
20926 +       AuTraceErr(err);
20927 +       return err;
20928 +}
20929 +
20930 +/*
20931 + * decide the branch and the parent dir where we will create a new entry.
20932 + * returns new bindex or an error.
20933 + * copyup the parent dir if needed.
20934 + */
20935 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20936 +             struct au_wr_dir_args *args)
20937 +{
20938 +       int err;
20939 +       unsigned int flags;
20940 +       aufs_bindex_t bcpup, btop, src_btop;
20941 +       const unsigned char add_entry
20942 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
20943 +               | au_ftest_wrdir(args->flags, TMPFILE);
20944 +       struct super_block *sb;
20945 +       struct dentry *parent;
20946 +       struct au_sbinfo *sbinfo;
20947 +
20948 +       sb = dentry->d_sb;
20949 +       sbinfo = au_sbi(sb);
20950 +       parent = dget_parent(dentry);
20951 +       btop = au_dbtop(dentry);
20952 +       bcpup = btop;
20953 +       if (args->force_btgt < 0) {
20954 +               if (src_dentry) {
20955 +                       src_btop = au_dbtop(src_dentry);
20956 +                       if (src_btop < btop)
20957 +                               bcpup = src_btop;
20958 +               } else if (add_entry) {
20959 +                       flags = 0;
20960 +                       if (au_ftest_wrdir(args->flags, ISDIR))
20961 +                               au_fset_wbr(flags, DIR);
20962 +                       err = AuWbrCreate(sbinfo, dentry, flags);
20963 +                       bcpup = err;
20964 +               }
20965 +
20966 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
20967 +                       if (add_entry)
20968 +                               err = AuWbrCopyup(sbinfo, dentry);
20969 +                       else {
20970 +                               if (!IS_ROOT(dentry)) {
20971 +                                       di_read_lock_parent(parent, !AuLock_IR);
20972 +                                       err = AuWbrCopyup(sbinfo, dentry);
20973 +                                       di_read_unlock(parent, !AuLock_IR);
20974 +                               } else
20975 +                                       err = AuWbrCopyup(sbinfo, dentry);
20976 +                       }
20977 +                       bcpup = err;
20978 +                       if (unlikely(err < 0))
20979 +                               goto out;
20980 +               }
20981 +       } else {
20982 +               bcpup = args->force_btgt;
20983 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
20984 +       }
20985 +
20986 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
20987 +       err = bcpup;
20988 +       if (bcpup == btop)
20989 +               goto out; /* success */
20990 +
20991 +       /* copyup the new parent into the branch we process */
20992 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
20993 +       if (err >= 0) {
20994 +               if (d_really_is_negative(dentry)) {
20995 +                       au_set_h_dptr(dentry, btop, NULL);
20996 +                       au_set_dbtop(dentry, bcpup);
20997 +                       au_set_dbbot(dentry, bcpup);
20998 +               }
20999 +               AuDebugOn(add_entry
21000 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21001 +                         && !au_h_dptr(dentry, bcpup));
21002 +       }
21003 +
21004 +out:
21005 +       dput(parent);
21006 +       return err;
21007 +}
21008 +
21009 +/* ---------------------------------------------------------------------- */
21010 +
21011 +void au_pin_hdir_unlock(struct au_pin *p)
21012 +{
21013 +       if (p->hdir)
21014 +               au_hn_inode_unlock(p->hdir);
21015 +}
21016 +
21017 +int au_pin_hdir_lock(struct au_pin *p)
21018 +{
21019 +       int err;
21020 +
21021 +       err = 0;
21022 +       if (!p->hdir)
21023 +               goto out;
21024 +
21025 +       /* even if an error happens later, keep this lock */
21026 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21027 +
21028 +       err = -EBUSY;
21029 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21030 +               goto out;
21031 +
21032 +       err = 0;
21033 +       if (p->h_dentry)
21034 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21035 +                                 p->h_parent, p->br);
21036 +
21037 +out:
21038 +       return err;
21039 +}
21040 +
21041 +int au_pin_hdir_relock(struct au_pin *p)
21042 +{
21043 +       int err, i;
21044 +       struct inode *h_i;
21045 +       struct dentry *h_d[] = {
21046 +               p->h_dentry,
21047 +               p->h_parent
21048 +       };
21049 +
21050 +       err = au_pin_hdir_lock(p);
21051 +       if (unlikely(err))
21052 +               goto out;
21053 +
21054 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21055 +               if (!h_d[i])
21056 +                       continue;
21057 +               if (d_is_positive(h_d[i])) {
21058 +                       h_i = d_inode(h_d[i]);
21059 +                       err = !h_i->i_nlink;
21060 +               }
21061 +       }
21062 +
21063 +out:
21064 +       return err;
21065 +}
21066 +
21067 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21068 +{
21069 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21070 +}
21071 +
21072 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21073 +{
21074 +       if (p->hdir) {
21075 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21076 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21077 +               au_pin_hdir_set_owner(p, current);
21078 +       }
21079 +}
21080 +
21081 +void au_pin_hdir_release(struct au_pin *p)
21082 +{
21083 +       if (p->hdir) {
21084 +               au_pin_hdir_set_owner(p, p->task);
21085 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21086 +       }
21087 +}
21088 +
21089 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21090 +{
21091 +       if (pin && pin->parent)
21092 +               return au_h_dptr(pin->parent, pin->bindex);
21093 +       return NULL;
21094 +}
21095 +
21096 +void au_unpin(struct au_pin *p)
21097 +{
21098 +       if (p->hdir)
21099 +               au_pin_hdir_unlock(p);
21100 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21101 +               vfsub_mnt_drop_write(p->h_mnt);
21102 +       if (!p->hdir)
21103 +               return;
21104 +
21105 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21106 +               di_read_unlock(p->parent, AuLock_IR);
21107 +       iput(p->hdir->hi_inode);
21108 +       dput(p->parent);
21109 +       p->parent = NULL;
21110 +       p->hdir = NULL;
21111 +       p->h_mnt = NULL;
21112 +       /* do not clear p->task */
21113 +}
21114 +
21115 +int au_do_pin(struct au_pin *p)
21116 +{
21117 +       int err;
21118 +       struct super_block *sb;
21119 +       struct inode *h_dir;
21120 +
21121 +       err = 0;
21122 +       sb = p->dentry->d_sb;
21123 +       p->br = au_sbr(sb, p->bindex);
21124 +       if (IS_ROOT(p->dentry)) {
21125 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21126 +                       p->h_mnt = au_br_mnt(p->br);
21127 +                       err = vfsub_mnt_want_write(p->h_mnt);
21128 +                       if (unlikely(err)) {
21129 +                               au_fclr_pin(p->flags, MNT_WRITE);
21130 +                               goto out_err;
21131 +                       }
21132 +               }
21133 +               goto out;
21134 +       }
21135 +
21136 +       p->h_dentry = NULL;
21137 +       if (p->bindex <= au_dbbot(p->dentry))
21138 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21139 +
21140 +       p->parent = dget_parent(p->dentry);
21141 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21142 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21143 +
21144 +       h_dir = NULL;
21145 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21146 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21147 +       if (p->hdir)
21148 +               h_dir = p->hdir->hi_inode;
21149 +
21150 +       /*
21151 +        * udba case, or
21152 +        * if DI_LOCKED is not set, then p->parent may be different
21153 +        * and h_parent can be NULL.
21154 +        */
21155 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21156 +               err = -EBUSY;
21157 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21158 +                       di_read_unlock(p->parent, AuLock_IR);
21159 +               dput(p->parent);
21160 +               p->parent = NULL;
21161 +               goto out_err;
21162 +       }
21163 +
21164 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21165 +               p->h_mnt = au_br_mnt(p->br);
21166 +               err = vfsub_mnt_want_write(p->h_mnt);
21167 +               if (unlikely(err)) {
21168 +                       au_fclr_pin(p->flags, MNT_WRITE);
21169 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21170 +                               di_read_unlock(p->parent, AuLock_IR);
21171 +                       dput(p->parent);
21172 +                       p->parent = NULL;
21173 +                       goto out_err;
21174 +               }
21175 +       }
21176 +
21177 +       au_igrab(h_dir);
21178 +       err = au_pin_hdir_lock(p);
21179 +       if (!err)
21180 +               goto out; /* success */
21181 +
21182 +       au_unpin(p);
21183 +
21184 +out_err:
21185 +       pr_err("err %d\n", err);
21186 +       err = au_busy_or_stale();
21187 +out:
21188 +       return err;
21189 +}
21190 +
21191 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21192 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21193 +                unsigned int udba, unsigned char flags)
21194 +{
21195 +       p->dentry = dentry;
21196 +       p->udba = udba;
21197 +       p->lsc_di = lsc_di;
21198 +       p->lsc_hi = lsc_hi;
21199 +       p->flags = flags;
21200 +       p->bindex = bindex;
21201 +
21202 +       p->parent = NULL;
21203 +       p->hdir = NULL;
21204 +       p->h_mnt = NULL;
21205 +
21206 +       p->h_dentry = NULL;
21207 +       p->h_parent = NULL;
21208 +       p->br = NULL;
21209 +       p->task = current;
21210 +}
21211 +
21212 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21213 +          unsigned int udba, unsigned char flags)
21214 +{
21215 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21216 +                   udba, flags);
21217 +       return au_do_pin(pin);
21218 +}
21219 +
21220 +/* ---------------------------------------------------------------------- */
21221 +
21222 +/*
21223 + * ->setattr() and ->getattr() are called in various cases.
21224 + * chmod, stat: dentry is revalidated.
21225 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21226 + *               unhashed.
21227 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21228 + */
21229 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21230 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21231 +{
21232 +       int err;
21233 +       struct dentry *parent;
21234 +
21235 +       err = 0;
21236 +       if (au_digen_test(dentry, sigen)) {
21237 +               parent = dget_parent(dentry);
21238 +               di_read_lock_parent(parent, AuLock_IR);
21239 +               err = au_refresh_dentry(dentry, parent);
21240 +               di_read_unlock(parent, AuLock_IR);
21241 +               dput(parent);
21242 +       }
21243 +
21244 +       AuTraceErr(err);
21245 +       return err;
21246 +}
21247 +
21248 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21249 +                    struct au_icpup_args *a)
21250 +{
21251 +       int err;
21252 +       loff_t sz;
21253 +       aufs_bindex_t btop, ibtop;
21254 +       struct dentry *hi_wh, *parent;
21255 +       struct inode *inode;
21256 +       struct au_wr_dir_args wr_dir_args = {
21257 +               .force_btgt     = -1,
21258 +               .flags          = 0
21259 +       };
21260 +
21261 +       if (d_is_dir(dentry))
21262 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21263 +       /* plink or hi_wh() case */
21264 +       btop = au_dbtop(dentry);
21265 +       inode = d_inode(dentry);
21266 +       ibtop = au_ibtop(inode);
21267 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21268 +               wr_dir_args.force_btgt = ibtop;
21269 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21270 +       if (unlikely(err < 0))
21271 +               goto out;
21272 +       a->btgt = err;
21273 +       if (err != btop)
21274 +               au_fset_icpup(a->flags, DID_CPUP);
21275 +
21276 +       err = 0;
21277 +       a->pin_flags = AuPin_MNT_WRITE;
21278 +       parent = NULL;
21279 +       if (!IS_ROOT(dentry)) {
21280 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21281 +               parent = dget_parent(dentry);
21282 +               di_write_lock_parent(parent);
21283 +       }
21284 +
21285 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21286 +       if (unlikely(err))
21287 +               goto out_parent;
21288 +
21289 +       sz = -1;
21290 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21291 +       a->h_inode = d_inode(a->h_path.dentry);
21292 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21293 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21294 +               if (ia->ia_size < i_size_read(a->h_inode))
21295 +                       sz = ia->ia_size;
21296 +               inode_unlock_shared(a->h_inode);
21297 +       }
21298 +
21299 +       hi_wh = NULL;
21300 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21301 +               hi_wh = au_hi_wh(inode, a->btgt);
21302 +               if (!hi_wh) {
21303 +                       struct au_cp_generic cpg = {
21304 +                               .dentry = dentry,
21305 +                               .bdst   = a->btgt,
21306 +                               .bsrc   = -1,
21307 +                               .len    = sz,
21308 +                               .pin    = &a->pin
21309 +                       };
21310 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21311 +                       if (unlikely(err))
21312 +                               goto out_unlock;
21313 +                       hi_wh = au_hi_wh(inode, a->btgt);
21314 +                       /* todo: revalidate hi_wh? */
21315 +               }
21316 +       }
21317 +
21318 +       if (parent) {
21319 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21320 +               di_downgrade_lock(parent, AuLock_IR);
21321 +               dput(parent);
21322 +               parent = NULL;
21323 +       }
21324 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21325 +               goto out; /* success */
21326 +
21327 +       if (!d_unhashed(dentry)) {
21328 +               struct au_cp_generic cpg = {
21329 +                       .dentry = dentry,
21330 +                       .bdst   = a->btgt,
21331 +                       .bsrc   = btop,
21332 +                       .len    = sz,
21333 +                       .pin    = &a->pin,
21334 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21335 +               };
21336 +               err = au_sio_cpup_simple(&cpg);
21337 +               if (!err)
21338 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21339 +       } else if (!hi_wh)
21340 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21341 +       else
21342 +               a->h_path.dentry = hi_wh; /* do not dget here */
21343 +
21344 +out_unlock:
21345 +       a->h_inode = d_inode(a->h_path.dentry);
21346 +       if (!err)
21347 +               goto out; /* success */
21348 +       au_unpin(&a->pin);
21349 +out_parent:
21350 +       if (parent) {
21351 +               di_write_unlock(parent);
21352 +               dput(parent);
21353 +       }
21354 +out:
21355 +       if (!err)
21356 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21357 +       return err;
21358 +}
21359 +
21360 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
21361 +{
21362 +       int err;
21363 +       struct inode *inode, *delegated;
21364 +       struct super_block *sb;
21365 +       struct file *file;
21366 +       struct au_icpup_args *a;
21367 +
21368 +       inode = d_inode(dentry);
21369 +       IMustLock(inode);
21370 +
21371 +       err = setattr_prepare(dentry, ia);
21372 +       if (unlikely(err))
21373 +               goto out;
21374 +
21375 +       err = -ENOMEM;
21376 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21377 +       if (unlikely(!a))
21378 +               goto out;
21379 +
21380 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21381 +               ia->ia_valid &= ~ATTR_MODE;
21382 +
21383 +       file = NULL;
21384 +       sb = dentry->d_sb;
21385 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21386 +       if (unlikely(err))
21387 +               goto out_kfree;
21388 +
21389 +       if (ia->ia_valid & ATTR_FILE) {
21390 +               /* currently ftruncate(2) only */
21391 +               AuDebugOn(!d_is_reg(dentry));
21392 +               file = ia->ia_file;
21393 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21394 +                                           /*fi_lsc*/0);
21395 +               if (unlikely(err))
21396 +                       goto out_si;
21397 +               ia->ia_file = au_hf_top(file);
21398 +               a->udba = AuOpt_UDBA_NONE;
21399 +       } else {
21400 +               /* fchmod() doesn't pass ia_file */
21401 +               a->udba = au_opt_udba(sb);
21402 +               di_write_lock_child(dentry);
21403 +               /* no d_unlinked(), to set UDBA_NONE for root */
21404 +               if (d_unhashed(dentry))
21405 +                       a->udba = AuOpt_UDBA_NONE;
21406 +               if (a->udba != AuOpt_UDBA_NONE) {
21407 +                       AuDebugOn(IS_ROOT(dentry));
21408 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21409 +                       if (unlikely(err))
21410 +                               goto out_dentry;
21411 +               }
21412 +       }
21413 +
21414 +       err = au_pin_and_icpup(dentry, ia, a);
21415 +       if (unlikely(err < 0))
21416 +               goto out_dentry;
21417 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21418 +               ia->ia_file = NULL;
21419 +               ia->ia_valid &= ~ATTR_FILE;
21420 +       }
21421 +
21422 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21423 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21424 +           == (ATTR_MODE | ATTR_CTIME)) {
21425 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21426 +               if (unlikely(err))
21427 +                       goto out_unlock;
21428 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21429 +                  && (ia->ia_valid & ATTR_CTIME)) {
21430 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21431 +               if (unlikely(err))
21432 +                       goto out_unlock;
21433 +       }
21434 +
21435 +       if (ia->ia_valid & ATTR_SIZE) {
21436 +               struct file *f;
21437 +
21438 +               if (ia->ia_size < i_size_read(inode))
21439 +                       /* unmap only */
21440 +                       truncate_setsize(inode, ia->ia_size);
21441 +
21442 +               f = NULL;
21443 +               if (ia->ia_valid & ATTR_FILE)
21444 +                       f = ia->ia_file;
21445 +               inode_unlock(a->h_inode);
21446 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21447 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21448 +       } else {
21449 +               delegated = NULL;
21450 +               while (1) {
21451 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21452 +                       if (delegated) {
21453 +                               err = break_deleg_wait(&delegated);
21454 +                               if (!err)
21455 +                                       continue;
21456 +                       }
21457 +                       break;
21458 +               }
21459 +       }
21460 +       /*
21461 +        * regardless aufs 'acl' option setting.
21462 +        * why don't all acl-aware fs call this func from their ->setattr()?
21463 +        */
21464 +       if (!err && (ia->ia_valid & ATTR_MODE))
21465 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
21466 +       if (!err)
21467 +               au_cpup_attr_changeable(inode);
21468 +
21469 +out_unlock:
21470 +       inode_unlock(a->h_inode);
21471 +       au_unpin(&a->pin);
21472 +       if (unlikely(err))
21473 +               au_update_dbtop(dentry);
21474 +out_dentry:
21475 +       di_write_unlock(dentry);
21476 +       if (file) {
21477 +               fi_write_unlock(file);
21478 +               ia->ia_file = file;
21479 +               ia->ia_valid |= ATTR_FILE;
21480 +       }
21481 +out_si:
21482 +       si_read_unlock(sb);
21483 +out_kfree:
21484 +       au_kfree_rcu(a);
21485 +out:
21486 +       AuTraceErr(err);
21487 +       return err;
21488 +}
21489 +
21490 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21491 +static int au_h_path_to_set_attr(struct dentry *dentry,
21492 +                                struct au_icpup_args *a, struct path *h_path)
21493 +{
21494 +       int err;
21495 +       struct super_block *sb;
21496 +
21497 +       sb = dentry->d_sb;
21498 +       a->udba = au_opt_udba(sb);
21499 +       /* no d_unlinked(), to set UDBA_NONE for root */
21500 +       if (d_unhashed(dentry))
21501 +               a->udba = AuOpt_UDBA_NONE;
21502 +       if (a->udba != AuOpt_UDBA_NONE) {
21503 +               AuDebugOn(IS_ROOT(dentry));
21504 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21505 +               if (unlikely(err))
21506 +                       goto out;
21507 +       }
21508 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21509 +       if (unlikely(err < 0))
21510 +               goto out;
21511 +
21512 +       h_path->dentry = a->h_path.dentry;
21513 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21514 +
21515 +out:
21516 +       return err;
21517 +}
21518 +
21519 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21520 +                 struct au_sxattr *arg)
21521 +{
21522 +       int err;
21523 +       struct path h_path;
21524 +       struct super_block *sb;
21525 +       struct au_icpup_args *a;
21526 +       struct inode *h_inode;
21527 +
21528 +       IMustLock(inode);
21529 +
21530 +       err = -ENOMEM;
21531 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21532 +       if (unlikely(!a))
21533 +               goto out;
21534 +
21535 +       sb = dentry->d_sb;
21536 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21537 +       if (unlikely(err))
21538 +               goto out_kfree;
21539 +
21540 +       h_path.dentry = NULL;   /* silence gcc */
21541 +       di_write_lock_child(dentry);
21542 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21543 +       if (unlikely(err))
21544 +               goto out_di;
21545 +
21546 +       inode_unlock(a->h_inode);
21547 +       switch (arg->type) {
21548 +       case AU_XATTR_SET:
21549 +               AuDebugOn(d_is_negative(h_path.dentry));
21550 +               err = vfsub_setxattr(h_path.dentry,
21551 +                                    arg->u.set.name, arg->u.set.value,
21552 +                                    arg->u.set.size, arg->u.set.flags);
21553 +               break;
21554 +       case AU_ACL_SET:
21555 +               err = -EOPNOTSUPP;
21556 +               h_inode = d_inode(h_path.dentry);
21557 +               if (h_inode->i_op->set_acl)
21558 +                       /* this will call posix_acl_update_mode */
21559 +                       err = h_inode->i_op->set_acl(h_inode,
21560 +                                                    arg->u.acl_set.acl,
21561 +                                                    arg->u.acl_set.type);
21562 +               break;
21563 +       }
21564 +       if (!err)
21565 +               au_cpup_attr_timesizes(inode);
21566 +
21567 +       au_unpin(&a->pin);
21568 +       if (unlikely(err))
21569 +               au_update_dbtop(dentry);
21570 +
21571 +out_di:
21572 +       di_write_unlock(dentry);
21573 +       si_read_unlock(sb);
21574 +out_kfree:
21575 +       au_kfree_rcu(a);
21576 +out:
21577 +       AuTraceErr(err);
21578 +       return err;
21579 +}
21580 +#endif
21581 +
21582 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21583 +                            unsigned int nlink)
21584 +{
21585 +       unsigned int n;
21586 +
21587 +       inode->i_mode = st->mode;
21588 +       /* don't i_[ug]id_write() here */
21589 +       inode->i_uid = st->uid;
21590 +       inode->i_gid = st->gid;
21591 +       inode->i_atime = st->atime;
21592 +       inode->i_mtime = st->mtime;
21593 +       inode->i_ctime = st->ctime;
21594 +
21595 +       au_cpup_attr_nlink(inode, /*force*/0);
21596 +       if (S_ISDIR(inode->i_mode)) {
21597 +               n = inode->i_nlink;
21598 +               n -= nlink;
21599 +               n += st->nlink;
21600 +               smp_mb(); /* for i_nlink */
21601 +               /* 0 can happen */
21602 +               set_nlink(inode, n);
21603 +       }
21604 +
21605 +       spin_lock(&inode->i_lock);
21606 +       inode->i_blocks = st->blocks;
21607 +       i_size_write(inode, st->size);
21608 +       spin_unlock(&inode->i_lock);
21609 +}
21610 +
21611 +/*
21612 + * common routine for aufs_getattr() and au_getxattr().
21613 + * returns zero or negative (an error).
21614 + * @dentry will be read-locked in success.
21615 + */
21616 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21617 +                     struct path *h_path, int locked)
21618 +{
21619 +       int err;
21620 +       unsigned int mnt_flags, sigen;
21621 +       unsigned char udba_none;
21622 +       aufs_bindex_t bindex;
21623 +       struct super_block *sb, *h_sb;
21624 +
21625 +       h_path->mnt = NULL;
21626 +       h_path->dentry = NULL;
21627 +
21628 +       err = 0;
21629 +       sb = dentry->d_sb;
21630 +       mnt_flags = au_mntflags(sb);
21631 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21632 +
21633 +       if (unlikely(locked))
21634 +               goto body; /* skip locking dinfo */
21635 +
21636 +       /* support fstat(2) */
21637 +       if (!d_unlinked(dentry) && !udba_none) {
21638 +               sigen = au_sigen(sb);
21639 +               err = au_digen_test(dentry, sigen);
21640 +               if (!err) {
21641 +                       di_read_lock_child(dentry, AuLock_IR);
21642 +                       err = au_dbrange_test(dentry);
21643 +                       if (unlikely(err)) {
21644 +                               di_read_unlock(dentry, AuLock_IR);
21645 +                               goto out;
21646 +                       }
21647 +               } else {
21648 +                       AuDebugOn(IS_ROOT(dentry));
21649 +                       di_write_lock_child(dentry);
21650 +                       err = au_dbrange_test(dentry);
21651 +                       if (!err)
21652 +                               err = au_reval_for_attr(dentry, sigen);
21653 +                       if (!err)
21654 +                               di_downgrade_lock(dentry, AuLock_IR);
21655 +                       else {
21656 +                               di_write_unlock(dentry);
21657 +                               goto out;
21658 +                       }
21659 +               }
21660 +       } else
21661 +               di_read_lock_child(dentry, AuLock_IR);
21662 +
21663 +body:
21664 +       if (!inode) {
21665 +               inode = d_inode(dentry);
21666 +               if (unlikely(!inode))
21667 +                       goto out;
21668 +       }
21669 +       bindex = au_ibtop(inode);
21670 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21671 +       h_sb = h_path->mnt->mnt_sb;
21672 +       if (!force
21673 +           && !au_test_fs_bad_iattr(h_sb)
21674 +           && udba_none)
21675 +               goto out; /* success */
21676 +
21677 +       if (au_dbtop(dentry) == bindex)
21678 +               h_path->dentry = au_h_dptr(dentry, bindex);
21679 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21680 +               h_path->dentry = au_plink_lkup(inode, bindex);
21681 +               if (IS_ERR(h_path->dentry))
21682 +                       /* pretending success */
21683 +                       h_path->dentry = NULL;
21684 +               else
21685 +                       dput(h_path->dentry);
21686 +       }
21687 +
21688 +out:
21689 +       return err;
21690 +}
21691 +
21692 +static int aufs_getattr(const struct path *path, struct kstat *st,
21693 +                       u32 request, unsigned int query)
21694 +{
21695 +       int err;
21696 +       unsigned char positive;
21697 +       struct path h_path;
21698 +       struct dentry *dentry;
21699 +       struct inode *inode;
21700 +       struct super_block *sb;
21701 +
21702 +       dentry = path->dentry;
21703 +       inode = d_inode(dentry);
21704 +       sb = dentry->d_sb;
21705 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21706 +       if (unlikely(err))
21707 +               goto out;
21708 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21709 +                               /*locked*/0);
21710 +       if (unlikely(err))
21711 +               goto out_si;
21712 +       if (unlikely(!h_path.dentry))
21713 +               /* illegally overlapped or something */
21714 +               goto out_fill; /* pretending success */
21715 +
21716 +       positive = d_is_positive(h_path.dentry);
21717 +       if (positive)
21718 +               /* no vfsub version */
21719 +               err = vfs_getattr(&h_path, st, request, query);
21720 +       if (!err) {
21721 +               if (positive)
21722 +                       au_refresh_iattr(inode, st,
21723 +                                        d_inode(h_path.dentry)->i_nlink);
21724 +               goto out_fill; /* success */
21725 +       }
21726 +       AuTraceErr(err);
21727 +       goto out_di;
21728 +
21729 +out_fill:
21730 +       generic_fillattr(inode, st);
21731 +out_di:
21732 +       di_read_unlock(dentry, AuLock_IR);
21733 +out_si:
21734 +       si_read_unlock(sb);
21735 +out:
21736 +       AuTraceErr(err);
21737 +       return err;
21738 +}
21739 +
21740 +/* ---------------------------------------------------------------------- */
21741 +
21742 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21743 +                                struct delayed_call *done)
21744 +{
21745 +       const char *ret;
21746 +       struct dentry *h_dentry;
21747 +       struct inode *h_inode;
21748 +       int err;
21749 +       aufs_bindex_t bindex;
21750 +
21751 +       ret = NULL; /* suppress a warning */
21752 +       err = -ECHILD;
21753 +       if (!dentry)
21754 +               goto out;
21755 +
21756 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21757 +       if (unlikely(err))
21758 +               goto out;
21759 +
21760 +       err = au_d_hashed_positive(dentry);
21761 +       if (unlikely(err))
21762 +               goto out_unlock;
21763 +
21764 +       err = -EINVAL;
21765 +       inode = d_inode(dentry);
21766 +       bindex = au_ibtop(inode);
21767 +       h_inode = au_h_iptr(inode, bindex);
21768 +       if (unlikely(!h_inode->i_op->get_link))
21769 +               goto out_unlock;
21770 +
21771 +       err = -EBUSY;
21772 +       h_dentry = NULL;
21773 +       if (au_dbtop(dentry) <= bindex) {
21774 +               h_dentry = au_h_dptr(dentry, bindex);
21775 +               if (h_dentry)
21776 +                       dget(h_dentry);
21777 +       }
21778 +       if (!h_dentry) {
21779 +               h_dentry = d_find_any_alias(h_inode);
21780 +               if (IS_ERR(h_dentry)) {
21781 +                       err = PTR_ERR(h_dentry);
21782 +                       goto out_unlock;
21783 +               }
21784 +       }
21785 +       if (unlikely(!h_dentry))
21786 +               goto out_unlock;
21787 +
21788 +       err = 0;
21789 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21790 +       AuDbgDentry(h_dentry);
21791 +       ret = vfs_get_link(h_dentry, done);
21792 +       dput(h_dentry);
21793 +       if (IS_ERR(ret))
21794 +               err = PTR_ERR(ret);
21795 +
21796 +out_unlock:
21797 +       aufs_read_unlock(dentry, AuLock_IR);
21798 +out:
21799 +       if (unlikely(err))
21800 +               ret = ERR_PTR(err);
21801 +       AuTraceErrPtr(ret);
21802 +       return ret;
21803 +}
21804 +
21805 +/* ---------------------------------------------------------------------- */
21806 +
21807 +static int au_is_special(struct inode *inode)
21808 +{
21809 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21810 +}
21811 +
21812 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21813 +                           int flags)
21814 +{
21815 +       int err;
21816 +       aufs_bindex_t bindex;
21817 +       struct super_block *sb;
21818 +       struct inode *h_inode;
21819 +       struct vfsmount *h_mnt;
21820 +
21821 +       sb = inode->i_sb;
21822 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21823 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21824 +
21825 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21826 +       lockdep_off();
21827 +       si_read_lock(sb, AuLock_FLUSH);
21828 +       ii_write_lock_child(inode);
21829 +
21830 +       err = 0;
21831 +       bindex = au_ibtop(inode);
21832 +       h_inode = au_h_iptr(inode, bindex);
21833 +       if (!au_test_ro(sb, bindex, inode)) {
21834 +               h_mnt = au_sbr_mnt(sb, bindex);
21835 +               err = vfsub_mnt_want_write(h_mnt);
21836 +               if (!err) {
21837 +                       err = vfsub_update_time(h_inode, ts, flags);
21838 +                       vfsub_mnt_drop_write(h_mnt);
21839 +               }
21840 +       } else if (au_is_special(h_inode)) {
21841 +               /*
21842 +                * Never copy-up here.
21843 +                * These special files may already be opened and used for
21844 +                * communicating. If we copied it up, then the communication
21845 +                * would be corrupted.
21846 +                */
21847 +               AuWarn1("timestamps for i%lu are ignored "
21848 +                       "since it is on readonly branch (hi%lu).\n",
21849 +                       inode->i_ino, h_inode->i_ino);
21850 +       } else if (flags & ~S_ATIME) {
21851 +               err = -EIO;
21852 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21853 +               AuDebugOn(1);
21854 +       }
21855 +
21856 +       if (!err)
21857 +               au_cpup_attr_timesizes(inode);
21858 +       ii_write_unlock(inode);
21859 +       si_read_unlock(sb);
21860 +       lockdep_on();
21861 +
21862 +       if (!err && (flags & S_VERSION))
21863 +               inode_inc_iversion(inode);
21864 +
21865 +       return err;
21866 +}
21867 +
21868 +/* ---------------------------------------------------------------------- */
21869 +
21870 +/* no getattr version will be set by module.c:aufs_init() */
21871 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21872 +       aufs_iop[] = {
21873 +       [AuIop_SYMLINK] = {
21874 +               .permission     = aufs_permission,
21875 +#ifdef CONFIG_FS_POSIX_ACL
21876 +               .get_acl        = aufs_get_acl,
21877 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
21878 +#endif
21879 +
21880 +               .setattr        = aufs_setattr,
21881 +               .getattr        = aufs_getattr,
21882 +
21883 +#ifdef CONFIG_AUFS_XATTR
21884 +               .listxattr      = aufs_listxattr,
21885 +#endif
21886 +
21887 +               .get_link       = aufs_get_link,
21888 +
21889 +               /* .update_time = aufs_update_time */
21890 +       },
21891 +       [AuIop_DIR] = {
21892 +               .create         = aufs_create,
21893 +               .lookup         = aufs_lookup,
21894 +               .link           = aufs_link,
21895 +               .unlink         = aufs_unlink,
21896 +               .symlink        = aufs_symlink,
21897 +               .mkdir          = aufs_mkdir,
21898 +               .rmdir          = aufs_rmdir,
21899 +               .mknod          = aufs_mknod,
21900 +               .rename         = aufs_rename,
21901 +
21902 +               .permission     = aufs_permission,
21903 +#ifdef CONFIG_FS_POSIX_ACL
21904 +               .get_acl        = aufs_get_acl,
21905 +               .set_acl        = aufs_set_acl,
21906 +#endif
21907 +
21908 +               .setattr        = aufs_setattr,
21909 +               .getattr        = aufs_getattr,
21910 +
21911 +#ifdef CONFIG_AUFS_XATTR
21912 +               .listxattr      = aufs_listxattr,
21913 +#endif
21914 +
21915 +               .update_time    = aufs_update_time,
21916 +               .atomic_open    = aufs_atomic_open,
21917 +               .tmpfile        = aufs_tmpfile
21918 +       },
21919 +       [AuIop_OTHER] = {
21920 +               .permission     = aufs_permission,
21921 +#ifdef CONFIG_FS_POSIX_ACL
21922 +               .get_acl        = aufs_get_acl,
21923 +               .set_acl        = aufs_set_acl,
21924 +#endif
21925 +
21926 +               .setattr        = aufs_setattr,
21927 +               .getattr        = aufs_getattr,
21928 +
21929 +#ifdef CONFIG_AUFS_XATTR
21930 +               .listxattr      = aufs_listxattr,
21931 +#endif
21932 +
21933 +               .update_time    = aufs_update_time
21934 +       }
21935 +};
21936 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21937 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21938 +++ linux/fs/aufs/i_op_del.c    2020-01-27 10:57:18.172204883 +0100
21939 @@ -0,0 +1,513 @@
21940 +// SPDX-License-Identifier: GPL-2.0
21941 +/*
21942 + * Copyright (C) 2005-2020 Junjiro R. Okajima
21943 + *
21944 + * This program, aufs is free software; you can redistribute it and/or modify
21945 + * it under the terms of the GNU General Public License as published by
21946 + * the Free Software Foundation; either version 2 of the License, or
21947 + * (at your option) any later version.
21948 + *
21949 + * This program is distributed in the hope that it will be useful,
21950 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21951 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21952 + * GNU General Public License for more details.
21953 + *
21954 + * You should have received a copy of the GNU General Public License
21955 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21956 + */
21957 +
21958 +/*
21959 + * inode operations (del entry)
21960 + */
21961 +
21962 +#include <linux/iversion.h>
21963 +#include "aufs.h"
21964 +
21965 +/*
21966 + * decide if a new whiteout for @dentry is necessary or not.
21967 + * when it is necessary, prepare the parent dir for the upper branch whose
21968 + * branch index is @bcpup for creation. the actual creation of the whiteout will
21969 + * be done by caller.
21970 + * return value:
21971 + * 0: wh is unnecessary
21972 + * plus: wh is necessary
21973 + * minus: error
21974 + */
21975 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
21976 +{
21977 +       int need_wh, err;
21978 +       aufs_bindex_t btop;
21979 +       struct super_block *sb;
21980 +
21981 +       sb = dentry->d_sb;
21982 +       btop = au_dbtop(dentry);
21983 +       if (*bcpup < 0) {
21984 +               *bcpup = btop;
21985 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
21986 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
21987 +                       *bcpup = err;
21988 +                       if (unlikely(err < 0))
21989 +                               goto out;
21990 +               }
21991 +       } else
21992 +               AuDebugOn(btop < *bcpup
21993 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
21994 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
21995 +
21996 +       if (*bcpup != btop) {
21997 +               err = au_cpup_dirs(dentry, *bcpup);
21998 +               if (unlikely(err))
21999 +                       goto out;
22000 +               need_wh = 1;
22001 +       } else {
22002 +               struct au_dinfo *dinfo, *tmp;
22003 +
22004 +               need_wh = -ENOMEM;
22005 +               dinfo = au_di(dentry);
22006 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22007 +               if (tmp) {
22008 +                       au_di_cp(tmp, dinfo);
22009 +                       au_di_swap(tmp, dinfo);
22010 +                       /* returns the number of positive dentries */
22011 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22012 +                                                /* AuLkup_IGNORE_PERM */ 0);
22013 +                       au_di_swap(tmp, dinfo);
22014 +                       au_rw_write_unlock(&tmp->di_rwsem);
22015 +                       au_di_free(tmp);
22016 +               }
22017 +       }
22018 +       AuDbg("need_wh %d\n", need_wh);
22019 +       err = need_wh;
22020 +
22021 +out:
22022 +       return err;
22023 +}
22024 +
22025 +/*
22026 + * simple tests for the del-entry operations.
22027 + * following the checks in vfs, plus the parent-child relationship.
22028 + */
22029 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22030 +              struct dentry *h_parent, int isdir)
22031 +{
22032 +       int err;
22033 +       umode_t h_mode;
22034 +       struct dentry *h_dentry, *h_latest;
22035 +       struct inode *h_inode;
22036 +
22037 +       h_dentry = au_h_dptr(dentry, bindex);
22038 +       if (d_really_is_positive(dentry)) {
22039 +               err = -ENOENT;
22040 +               if (unlikely(d_is_negative(h_dentry)))
22041 +                       goto out;
22042 +               h_inode = d_inode(h_dentry);
22043 +               if (unlikely(!h_inode->i_nlink))
22044 +                       goto out;
22045 +
22046 +               h_mode = h_inode->i_mode;
22047 +               if (!isdir) {
22048 +                       err = -EISDIR;
22049 +                       if (unlikely(S_ISDIR(h_mode)))
22050 +                               goto out;
22051 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22052 +                       err = -ENOTDIR;
22053 +                       goto out;
22054 +               }
22055 +       } else {
22056 +               /* rename(2) case */
22057 +               err = -EIO;
22058 +               if (unlikely(d_is_positive(h_dentry)))
22059 +                       goto out;
22060 +       }
22061 +
22062 +       err = -ENOENT;
22063 +       /* expected parent dir is locked */
22064 +       if (unlikely(h_parent != h_dentry->d_parent))
22065 +               goto out;
22066 +       err = 0;
22067 +
22068 +       /*
22069 +        * rmdir a dir may break the consistency on some filesystem.
22070 +        * let's try heavy test.
22071 +        */
22072 +       err = -EACCES;
22073 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
22074 +                    && au_test_h_perm(d_inode(h_parent),
22075 +                                      MAY_EXEC | MAY_WRITE)))
22076 +               goto out;
22077 +
22078 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
22079 +       err = -EIO;
22080 +       if (IS_ERR(h_latest))
22081 +               goto out;
22082 +       if (h_latest == h_dentry)
22083 +               err = 0;
22084 +       dput(h_latest);
22085 +
22086 +out:
22087 +       return err;
22088 +}
22089 +
22090 +/*
22091 + * decide the branch where we operate for @dentry. the branch index will be set
22092 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22093 + * dir for reverting.
22094 + * when a new whiteout is necessary, create it.
22095 + */
22096 +static struct dentry*
22097 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22098 +                   struct au_dtime *dt, struct au_pin *pin)
22099 +{
22100 +       struct dentry *wh_dentry;
22101 +       struct super_block *sb;
22102 +       struct path h_path;
22103 +       int err, need_wh;
22104 +       unsigned int udba;
22105 +       aufs_bindex_t bcpup;
22106 +
22107 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22108 +       wh_dentry = ERR_PTR(need_wh);
22109 +       if (unlikely(need_wh < 0))
22110 +               goto out;
22111 +
22112 +       sb = dentry->d_sb;
22113 +       udba = au_opt_udba(sb);
22114 +       bcpup = *rbcpup;
22115 +       err = au_pin(pin, dentry, bcpup, udba,
22116 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22117 +       wh_dentry = ERR_PTR(err);
22118 +       if (unlikely(err))
22119 +               goto out;
22120 +
22121 +       h_path.dentry = au_pinned_h_parent(pin);
22122 +       if (udba != AuOpt_UDBA_NONE
22123 +           && au_dbtop(dentry) == bcpup) {
22124 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22125 +               wh_dentry = ERR_PTR(err);
22126 +               if (unlikely(err))
22127 +                       goto out_unpin;
22128 +       }
22129 +
22130 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22131 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22132 +       wh_dentry = NULL;
22133 +       if (!need_wh)
22134 +               goto out; /* success, no need to create whiteout */
22135 +
22136 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22137 +       if (IS_ERR(wh_dentry))
22138 +               goto out_unpin;
22139 +
22140 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22141 +       goto out; /* success */
22142 +
22143 +out_unpin:
22144 +       au_unpin(pin);
22145 +out:
22146 +       return wh_dentry;
22147 +}
22148 +
22149 +/*
22150 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22151 + * in order to be revertible and save time for removing many child whiteouts
22152 + * under the dir.
22153 + * returns 1 when there are too many child whiteout and caller should remove
22154 + * them asynchronously. returns 0 when the number of children is enough small to
22155 + * remove now or the branch fs is a remote fs.
22156 + * otherwise return an error.
22157 + */
22158 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22159 +                          struct au_nhash *whlist, struct inode *dir)
22160 +{
22161 +       int rmdir_later, err, dirwh;
22162 +       struct dentry *h_dentry;
22163 +       struct super_block *sb;
22164 +       struct inode *inode;
22165 +
22166 +       sb = dentry->d_sb;
22167 +       SiMustAnyLock(sb);
22168 +       h_dentry = au_h_dptr(dentry, bindex);
22169 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22170 +       if (unlikely(err))
22171 +               goto out;
22172 +
22173 +       /* stop monitoring */
22174 +       inode = d_inode(dentry);
22175 +       au_hn_free(au_hi(inode, bindex));
22176 +
22177 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22178 +               dirwh = au_sbi(sb)->si_dirwh;
22179 +               rmdir_later = (dirwh <= 1);
22180 +               if (!rmdir_later)
22181 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22182 +                                                             dirwh);
22183 +               if (rmdir_later)
22184 +                       return rmdir_later;
22185 +       }
22186 +
22187 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22188 +       if (unlikely(err)) {
22189 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22190 +                       h_dentry, bindex, err);
22191 +               err = 0;
22192 +       }
22193 +
22194 +out:
22195 +       AuTraceErr(err);
22196 +       return err;
22197 +}
22198 +
22199 +/*
22200 + * final procedure for deleting a entry.
22201 + * maintain dentry and iattr.
22202 + */
22203 +static void epilog(struct inode *dir, struct dentry *dentry,
22204 +                  aufs_bindex_t bindex)
22205 +{
22206 +       struct inode *inode;
22207 +
22208 +       inode = d_inode(dentry);
22209 +       d_drop(dentry);
22210 +       inode->i_ctime = dir->i_ctime;
22211 +
22212 +       au_dir_ts(dir, bindex);
22213 +       inode_inc_iversion(dir);
22214 +}
22215 +
22216 +/*
22217 + * when an error happened, remove the created whiteout and revert everything.
22218 + */
22219 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22220 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22221 +                    struct dentry *dentry, struct au_dtime *dt)
22222 +{
22223 +       int rerr;
22224 +       struct path h_path = {
22225 +               .dentry = wh_dentry,
22226 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22227 +       };
22228 +
22229 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22230 +       if (!rerr) {
22231 +               au_set_dbwh(dentry, bwh);
22232 +               au_dtime_revert(dt);
22233 +               return 0;
22234 +       }
22235 +
22236 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22237 +       return -EIO;
22238 +}
22239 +
22240 +/* ---------------------------------------------------------------------- */
22241 +
22242 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22243 +{
22244 +       int err;
22245 +       aufs_bindex_t bwh, bindex, btop;
22246 +       struct inode *inode, *h_dir, *delegated;
22247 +       struct dentry *parent, *wh_dentry;
22248 +       /* to reduce stack size */
22249 +       struct {
22250 +               struct au_dtime dt;
22251 +               struct au_pin pin;
22252 +               struct path h_path;
22253 +       } *a;
22254 +
22255 +       IMustLock(dir);
22256 +
22257 +       err = -ENOMEM;
22258 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22259 +       if (unlikely(!a))
22260 +               goto out;
22261 +
22262 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22263 +       if (unlikely(err))
22264 +               goto out_free;
22265 +       err = au_d_hashed_positive(dentry);
22266 +       if (unlikely(err))
22267 +               goto out_unlock;
22268 +       inode = d_inode(dentry);
22269 +       IMustLock(inode);
22270 +       err = -EISDIR;
22271 +       if (unlikely(d_is_dir(dentry)))
22272 +               goto out_unlock; /* possible? */
22273 +
22274 +       btop = au_dbtop(dentry);
22275 +       bwh = au_dbwh(dentry);
22276 +       bindex = -1;
22277 +       parent = dentry->d_parent; /* dir inode is locked */
22278 +       di_write_lock_parent(parent);
22279 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22280 +                                       &a->pin);
22281 +       err = PTR_ERR(wh_dentry);
22282 +       if (IS_ERR(wh_dentry))
22283 +               goto out_parent;
22284 +
22285 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22286 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22287 +       dget(a->h_path.dentry);
22288 +       if (bindex == btop) {
22289 +               h_dir = au_pinned_h_dir(&a->pin);
22290 +               delegated = NULL;
22291 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22292 +               if (unlikely(err == -EWOULDBLOCK)) {
22293 +                       pr_warn("cannot retry for NFSv4 delegation"
22294 +                               " for an internal unlink\n");
22295 +                       iput(delegated);
22296 +               }
22297 +       } else {
22298 +               /* dir inode is locked */
22299 +               h_dir = d_inode(wh_dentry->d_parent);
22300 +               IMustLock(h_dir);
22301 +               err = 0;
22302 +       }
22303 +
22304 +       if (!err) {
22305 +               vfsub_drop_nlink(inode);
22306 +               epilog(dir, dentry, bindex);
22307 +
22308 +               /* update target timestamps */
22309 +               if (bindex == btop) {
22310 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22311 +                       /*ignore*/
22312 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22313 +               } else
22314 +                       /* todo: this timestamp may be reverted later */
22315 +                       inode->i_ctime = h_dir->i_ctime;
22316 +               goto out_unpin; /* success */
22317 +       }
22318 +
22319 +       /* revert */
22320 +       if (wh_dentry) {
22321 +               int rerr;
22322 +
22323 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22324 +                                &a->dt);
22325 +               if (rerr)
22326 +                       err = rerr;
22327 +       }
22328 +
22329 +out_unpin:
22330 +       au_unpin(&a->pin);
22331 +       dput(wh_dentry);
22332 +       dput(a->h_path.dentry);
22333 +out_parent:
22334 +       di_write_unlock(parent);
22335 +out_unlock:
22336 +       aufs_read_unlock(dentry, AuLock_DW);
22337 +out_free:
22338 +       au_kfree_rcu(a);
22339 +out:
22340 +       return err;
22341 +}
22342 +
22343 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22344 +{
22345 +       int err, rmdir_later;
22346 +       aufs_bindex_t bwh, bindex, btop;
22347 +       struct inode *inode;
22348 +       struct dentry *parent, *wh_dentry, *h_dentry;
22349 +       struct au_whtmp_rmdir *args;
22350 +       /* to reduce stack size */
22351 +       struct {
22352 +               struct au_dtime dt;
22353 +               struct au_pin pin;
22354 +       } *a;
22355 +
22356 +       IMustLock(dir);
22357 +
22358 +       err = -ENOMEM;
22359 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22360 +       if (unlikely(!a))
22361 +               goto out;
22362 +
22363 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22364 +       if (unlikely(err))
22365 +               goto out_free;
22366 +       err = au_alive_dir(dentry);
22367 +       if (unlikely(err))
22368 +               goto out_unlock;
22369 +       inode = d_inode(dentry);
22370 +       IMustLock(inode);
22371 +       err = -ENOTDIR;
22372 +       if (unlikely(!d_is_dir(dentry)))
22373 +               goto out_unlock; /* possible? */
22374 +
22375 +       err = -ENOMEM;
22376 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22377 +       if (unlikely(!args))
22378 +               goto out_unlock;
22379 +
22380 +       parent = dentry->d_parent; /* dir inode is locked */
22381 +       di_write_lock_parent(parent);
22382 +       err = au_test_empty(dentry, &args->whlist);
22383 +       if (unlikely(err))
22384 +               goto out_parent;
22385 +
22386 +       btop = au_dbtop(dentry);
22387 +       bwh = au_dbwh(dentry);
22388 +       bindex = -1;
22389 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22390 +                                       &a->pin);
22391 +       err = PTR_ERR(wh_dentry);
22392 +       if (IS_ERR(wh_dentry))
22393 +               goto out_parent;
22394 +
22395 +       h_dentry = au_h_dptr(dentry, btop);
22396 +       dget(h_dentry);
22397 +       rmdir_later = 0;
22398 +       if (bindex == btop) {
22399 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22400 +               if (err > 0) {
22401 +                       rmdir_later = err;
22402 +                       err = 0;
22403 +               }
22404 +       } else {
22405 +               /* stop monitoring */
22406 +               au_hn_free(au_hi(inode, btop));
22407 +
22408 +               /* dir inode is locked */
22409 +               IMustLock(d_inode(wh_dentry->d_parent));
22410 +               err = 0;
22411 +       }
22412 +
22413 +       if (!err) {
22414 +               vfsub_dead_dir(inode);
22415 +               au_set_dbdiropq(dentry, -1);
22416 +               epilog(dir, dentry, bindex);
22417 +
22418 +               if (rmdir_later) {
22419 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22420 +                       args = NULL;
22421 +               }
22422 +
22423 +               goto out_unpin; /* success */
22424 +       }
22425 +
22426 +       /* revert */
22427 +       AuLabel(revert);
22428 +       if (wh_dentry) {
22429 +               int rerr;
22430 +
22431 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22432 +                                &a->dt);
22433 +               if (rerr)
22434 +                       err = rerr;
22435 +       }
22436 +
22437 +out_unpin:
22438 +       au_unpin(&a->pin);
22439 +       dput(wh_dentry);
22440 +       dput(h_dentry);
22441 +out_parent:
22442 +       di_write_unlock(parent);
22443 +       if (args)
22444 +               au_whtmp_rmdir_free(args);
22445 +out_unlock:
22446 +       aufs_read_unlock(dentry, AuLock_DW);
22447 +out_free:
22448 +       au_kfree_rcu(a);
22449 +out:
22450 +       AuTraceErr(err);
22451 +       return err;
22452 +}
22453 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22454 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22455 +++ linux/fs/aufs/i_op_ren.c    2020-01-27 10:57:18.172204883 +0100
22456 @@ -0,0 +1,1250 @@
22457 +// SPDX-License-Identifier: GPL-2.0
22458 +/*
22459 + * Copyright (C) 2005-2020 Junjiro R. Okajima
22460 + *
22461 + * This program, aufs is free software; you can redistribute it and/or modify
22462 + * it under the terms of the GNU General Public License as published by
22463 + * the Free Software Foundation; either version 2 of the License, or
22464 + * (at your option) any later version.
22465 + *
22466 + * This program is distributed in the hope that it will be useful,
22467 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22468 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22469 + * GNU General Public License for more details.
22470 + *
22471 + * You should have received a copy of the GNU General Public License
22472 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22473 + */
22474 +
22475 +/*
22476 + * inode operation (rename entry)
22477 + * todo: this is crazy monster
22478 + */
22479 +
22480 +#include <linux/iversion.h>
22481 +#include "aufs.h"
22482 +
22483 +enum { AuSRC, AuDST, AuSrcDst };
22484 +enum { AuPARENT, AuCHILD, AuParentChild };
22485 +
22486 +#define AuRen_ISDIR_SRC                1
22487 +#define AuRen_ISDIR_DST                (1 << 1)
22488 +#define AuRen_ISSAMEDIR                (1 << 2)
22489 +#define AuRen_WHSRC            (1 << 3)
22490 +#define AuRen_WHDST            (1 << 4)
22491 +#define AuRen_MNT_WRITE                (1 << 5)
22492 +#define AuRen_DT_DSTDIR                (1 << 6)
22493 +#define AuRen_DIROPQ_SRC       (1 << 7)
22494 +#define AuRen_DIROPQ_DST       (1 << 8)
22495 +#define AuRen_DIRREN           (1 << 9)
22496 +#define AuRen_DROPPED_SRC      (1 << 10)
22497 +#define AuRen_DROPPED_DST      (1 << 11)
22498 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22499 +#define au_fset_ren(flags, name) \
22500 +       do { (flags) |= AuRen_##name; } while (0)
22501 +#define au_fclr_ren(flags, name) \
22502 +       do { (flags) &= ~AuRen_##name; } while (0)
22503 +
22504 +#ifndef CONFIG_AUFS_DIRREN
22505 +#undef AuRen_DIRREN
22506 +#define AuRen_DIRREN           0
22507 +#endif
22508 +
22509 +struct au_ren_args {
22510 +       struct {
22511 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22512 +                       *wh_dentry;
22513 +               struct inode *dir, *inode;
22514 +               struct au_hinode *hdir, *hinode;
22515 +               struct au_dtime dt[AuParentChild];
22516 +               aufs_bindex_t btop, bdiropq;
22517 +       } sd[AuSrcDst];
22518 +
22519 +#define src_dentry     sd[AuSRC].dentry
22520 +#define src_dir                sd[AuSRC].dir
22521 +#define src_inode      sd[AuSRC].inode
22522 +#define src_h_dentry   sd[AuSRC].h_dentry
22523 +#define src_parent     sd[AuSRC].parent
22524 +#define src_h_parent   sd[AuSRC].h_parent
22525 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22526 +#define src_hdir       sd[AuSRC].hdir
22527 +#define src_hinode     sd[AuSRC].hinode
22528 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22529 +#define src_dt         sd[AuSRC].dt
22530 +#define src_btop       sd[AuSRC].btop
22531 +#define src_bdiropq    sd[AuSRC].bdiropq
22532 +
22533 +#define dst_dentry     sd[AuDST].dentry
22534 +#define dst_dir                sd[AuDST].dir
22535 +#define dst_inode      sd[AuDST].inode
22536 +#define dst_h_dentry   sd[AuDST].h_dentry
22537 +#define dst_parent     sd[AuDST].parent
22538 +#define dst_h_parent   sd[AuDST].h_parent
22539 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22540 +#define dst_hdir       sd[AuDST].hdir
22541 +#define dst_hinode     sd[AuDST].hinode
22542 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22543 +#define dst_dt         sd[AuDST].dt
22544 +#define dst_btop       sd[AuDST].btop
22545 +#define dst_bdiropq    sd[AuDST].bdiropq
22546 +
22547 +       struct dentry *h_trap;
22548 +       struct au_branch *br;
22549 +       struct path h_path;
22550 +       struct au_nhash whlist;
22551 +       aufs_bindex_t btgt, src_bwh;
22552 +
22553 +       struct {
22554 +               unsigned short auren_flags;
22555 +               unsigned char flags;    /* syscall parameter */
22556 +               unsigned char exchange;
22557 +       } __packed;
22558 +
22559 +       struct au_whtmp_rmdir *thargs;
22560 +       struct dentry *h_dst;
22561 +       struct au_hinode *h_root;
22562 +};
22563 +
22564 +/* ---------------------------------------------------------------------- */
22565 +
22566 +/*
22567 + * functions for reverting.
22568 + * when an error happened in a single rename systemcall, we should revert
22569 + * everything as if nothing happened.
22570 + * we don't need to revert the copied-up/down the parent dir since they are
22571 + * harmless.
22572 + */
22573 +
22574 +#define RevertFailure(fmt, ...) do { \
22575 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22576 +               ##__VA_ARGS__, err, rerr); \
22577 +       err = -EIO; \
22578 +} while (0)
22579 +
22580 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22581 +{
22582 +       int rerr;
22583 +       struct dentry *d;
22584 +#define src_or_dst(member) a->sd[idx].member
22585 +
22586 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22587 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22588 +       rerr = au_diropq_remove(d, a->btgt);
22589 +       au_hn_inode_unlock(src_or_dst(hinode));
22590 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22591 +       if (rerr)
22592 +               RevertFailure("remove diropq %pd", d);
22593 +
22594 +#undef src_or_dst_
22595 +}
22596 +
22597 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22598 +{
22599 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22600 +               au_ren_do_rev_diropq(err, a, AuSRC);
22601 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22602 +               au_ren_do_rev_diropq(err, a, AuDST);
22603 +}
22604 +
22605 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22606 +{
22607 +       int rerr;
22608 +       struct inode *delegated;
22609 +
22610 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
22611 +                                         a->src_h_parent);
22612 +       rerr = PTR_ERR(a->h_path.dentry);
22613 +       if (IS_ERR(a->h_path.dentry)) {
22614 +               RevertFailure("lkup one %pd", a->src_dentry);
22615 +               return;
22616 +       }
22617 +
22618 +       delegated = NULL;
22619 +       rerr = vfsub_rename(a->dst_h_dir,
22620 +                           au_h_dptr(a->src_dentry, a->btgt),
22621 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22622 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22623 +               pr_warn("cannot retry for NFSv4 delegation"
22624 +                       " for an internal rename\n");
22625 +               iput(delegated);
22626 +       }
22627 +       d_drop(a->h_path.dentry);
22628 +       dput(a->h_path.dentry);
22629 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22630 +       if (rerr)
22631 +               RevertFailure("rename %pd", a->src_dentry);
22632 +}
22633 +
22634 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22635 +{
22636 +       int rerr;
22637 +       struct inode *delegated;
22638 +
22639 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
22640 +                                         a->dst_h_parent);
22641 +       rerr = PTR_ERR(a->h_path.dentry);
22642 +       if (IS_ERR(a->h_path.dentry)) {
22643 +               RevertFailure("lkup one %pd", a->dst_dentry);
22644 +               return;
22645 +       }
22646 +       if (d_is_positive(a->h_path.dentry)) {
22647 +               d_drop(a->h_path.dentry);
22648 +               dput(a->h_path.dentry);
22649 +               return;
22650 +       }
22651 +
22652 +       delegated = NULL;
22653 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22654 +                           &delegated, a->flags);
22655 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22656 +               pr_warn("cannot retry for NFSv4 delegation"
22657 +                       " for an internal rename\n");
22658 +               iput(delegated);
22659 +       }
22660 +       d_drop(a->h_path.dentry);
22661 +       dput(a->h_path.dentry);
22662 +       if (!rerr)
22663 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22664 +       else
22665 +               RevertFailure("rename %pd", a->h_dst);
22666 +}
22667 +
22668 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22669 +{
22670 +       int rerr;
22671 +
22672 +       a->h_path.dentry = a->src_wh_dentry;
22673 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22674 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22675 +       if (rerr)
22676 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22677 +}
22678 +#undef RevertFailure
22679 +
22680 +/* ---------------------------------------------------------------------- */
22681 +
22682 +/*
22683 + * when we have to copyup the renaming entry, do it with the rename-target name
22684 + * in order to minimize the cost (the later actual rename is unnecessary).
22685 + * otherwise rename it on the target branch.
22686 + */
22687 +static int au_ren_or_cpup(struct au_ren_args *a)
22688 +{
22689 +       int err;
22690 +       struct dentry *d;
22691 +       struct inode *delegated;
22692 +
22693 +       d = a->src_dentry;
22694 +       if (au_dbtop(d) == a->btgt) {
22695 +               a->h_path.dentry = a->dst_h_dentry;
22696 +               AuDebugOn(au_dbtop(d) != a->btgt);
22697 +               delegated = NULL;
22698 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22699 +                                  a->dst_h_dir, &a->h_path, &delegated,
22700 +                                  a->flags);
22701 +               if (unlikely(err == -EWOULDBLOCK)) {
22702 +                       pr_warn("cannot retry for NFSv4 delegation"
22703 +                               " for an internal rename\n");
22704 +                       iput(delegated);
22705 +               }
22706 +       } else
22707 +               BUG();
22708 +
22709 +       if (!err && a->h_dst)
22710 +               /* it will be set to dinfo later */
22711 +               dget(a->h_dst);
22712 +
22713 +       return err;
22714 +}
22715 +
22716 +/* cf. aufs_rmdir() */
22717 +static int au_ren_del_whtmp(struct au_ren_args *a)
22718 +{
22719 +       int err;
22720 +       struct inode *dir;
22721 +
22722 +       dir = a->dst_dir;
22723 +       SiMustAnyLock(dir->i_sb);
22724 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22725 +                                    au_sbi(dir->i_sb)->si_dirwh)
22726 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22727 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22728 +               if (unlikely(err))
22729 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22730 +                               "ignored.\n", a->h_dst, err);
22731 +       } else {
22732 +               au_nhash_wh_free(&a->thargs->whlist);
22733 +               a->thargs->whlist = a->whlist;
22734 +               a->whlist.nh_num = 0;
22735 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22736 +               dput(a->h_dst);
22737 +               a->thargs = NULL;
22738 +       }
22739 +
22740 +       return 0;
22741 +}
22742 +
22743 +/* make it 'opaque' dir. */
22744 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22745 +{
22746 +       int err;
22747 +       struct dentry *d, *diropq;
22748 +#define src_or_dst(member) a->sd[idx].member
22749 +
22750 +       err = 0;
22751 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22752 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22753 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22754 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22755 +       diropq = au_diropq_create(d, a->btgt);
22756 +       au_hn_inode_unlock(src_or_dst(hinode));
22757 +       if (IS_ERR(diropq))
22758 +               err = PTR_ERR(diropq);
22759 +       else
22760 +               dput(diropq);
22761 +
22762 +#undef src_or_dst_
22763 +       return err;
22764 +}
22765 +
22766 +static int au_ren_diropq(struct au_ren_args *a)
22767 +{
22768 +       int err;
22769 +       unsigned char always;
22770 +       struct dentry *d;
22771 +
22772 +       err = 0;
22773 +       d = a->dst_dentry; /* already renamed on the branch */
22774 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22775 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22776 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22777 +           && a->btgt != au_dbdiropq(a->src_dentry)
22778 +           && (a->dst_wh_dentry
22779 +               || a->btgt <= au_dbdiropq(d)
22780 +               /* hide the lower to keep xino */
22781 +               /* the lowers may not be a dir, but we hide them anyway */
22782 +               || a->btgt < au_dbbot(d)
22783 +               || always)) {
22784 +               AuDbg("here\n");
22785 +               err = au_ren_do_diropq(a, AuSRC);
22786 +               if (unlikely(err))
22787 +                       goto out;
22788 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22789 +       }
22790 +       if (!a->exchange)
22791 +               goto out; /* success */
22792 +
22793 +       d = a->src_dentry; /* already renamed on the branch */
22794 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22795 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22796 +           && (a->btgt < au_dbdiropq(d)
22797 +               || a->btgt < au_dbbot(d)
22798 +               || always)) {
22799 +               AuDbgDentry(a->src_dentry);
22800 +               AuDbgDentry(a->dst_dentry);
22801 +               err = au_ren_do_diropq(a, AuDST);
22802 +               if (unlikely(err))
22803 +                       goto out_rev_src;
22804 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22805 +       }
22806 +       goto out; /* success */
22807 +
22808 +out_rev_src:
22809 +       AuDbg("err %d, reverting src\n", err);
22810 +       au_ren_rev_diropq(err, a);
22811 +out:
22812 +       return err;
22813 +}
22814 +
22815 +static int do_rename(struct au_ren_args *a)
22816 +{
22817 +       int err;
22818 +       struct dentry *d, *h_d;
22819 +
22820 +       if (!a->exchange) {
22821 +               /* prepare workqueue args for asynchronous rmdir */
22822 +               h_d = a->dst_h_dentry;
22823 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22824 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22825 +                   && d_is_positive(h_d)) {
22826 +                       err = -ENOMEM;
22827 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22828 +                                                        GFP_NOFS);
22829 +                       if (unlikely(!a->thargs))
22830 +                               goto out;
22831 +                       a->h_dst = dget(h_d);
22832 +               }
22833 +
22834 +               /* create whiteout for src_dentry */
22835 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22836 +                       a->src_bwh = au_dbwh(a->src_dentry);
22837 +                       AuDebugOn(a->src_bwh >= 0);
22838 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22839 +                                                       a->src_h_parent);
22840 +                       err = PTR_ERR(a->src_wh_dentry);
22841 +                       if (IS_ERR(a->src_wh_dentry))
22842 +                               goto out_thargs;
22843 +               }
22844 +
22845 +               /* lookup whiteout for dentry */
22846 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22847 +                       h_d = au_wh_lkup(a->dst_h_parent,
22848 +                                        &a->dst_dentry->d_name, a->br);
22849 +                       err = PTR_ERR(h_d);
22850 +                       if (IS_ERR(h_d))
22851 +                               goto out_whsrc;
22852 +                       if (d_is_negative(h_d))
22853 +                               dput(h_d);
22854 +                       else
22855 +                               a->dst_wh_dentry = h_d;
22856 +               }
22857 +
22858 +               /* rename dentry to tmpwh */
22859 +               if (a->thargs) {
22860 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22861 +                       if (unlikely(err))
22862 +                               goto out_whdst;
22863 +
22864 +                       d = a->dst_dentry;
22865 +                       au_set_h_dptr(d, a->btgt, NULL);
22866 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22867 +                       if (unlikely(err))
22868 +                               goto out_whtmp;
22869 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22870 +               }
22871 +       }
22872 +
22873 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22874 +#if 0 /* debugging */
22875 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22876 +              && d_is_positive(a->dst_h_dentry)
22877 +              && a->src_btop != a->btgt);
22878 +#endif
22879 +
22880 +       /* rename by vfs_rename or cpup */
22881 +       err = au_ren_or_cpup(a);
22882 +       if (unlikely(err))
22883 +               /* leave the copied-up one */
22884 +               goto out_whtmp;
22885 +
22886 +       /* make dir opaque */
22887 +       err = au_ren_diropq(a);
22888 +       if (unlikely(err))
22889 +               goto out_rename;
22890 +
22891 +       /* update target timestamps */
22892 +       if (a->exchange) {
22893 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22894 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22895 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22896 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22897 +       }
22898 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22899 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22900 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22901 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22902 +
22903 +       if (!a->exchange) {
22904 +               /* remove whiteout for dentry */
22905 +               if (a->dst_wh_dentry) {
22906 +                       a->h_path.dentry = a->dst_wh_dentry;
22907 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22908 +                                                 a->dst_dentry);
22909 +                       if (unlikely(err))
22910 +                               goto out_diropq;
22911 +               }
22912 +
22913 +               /* remove whtmp */
22914 +               if (a->thargs)
22915 +                       au_ren_del_whtmp(a); /* ignore this error */
22916 +
22917 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22918 +       }
22919 +       err = 0;
22920 +       goto out_success;
22921 +
22922 +out_diropq:
22923 +       au_ren_rev_diropq(err, a);
22924 +out_rename:
22925 +       au_ren_rev_rename(err, a);
22926 +       dput(a->h_dst);
22927 +out_whtmp:
22928 +       if (a->thargs)
22929 +               au_ren_rev_whtmp(err, a);
22930 +out_whdst:
22931 +       dput(a->dst_wh_dentry);
22932 +       a->dst_wh_dentry = NULL;
22933 +out_whsrc:
22934 +       if (a->src_wh_dentry)
22935 +               au_ren_rev_whsrc(err, a);
22936 +out_success:
22937 +       dput(a->src_wh_dentry);
22938 +       dput(a->dst_wh_dentry);
22939 +out_thargs:
22940 +       if (a->thargs) {
22941 +               dput(a->h_dst);
22942 +               au_whtmp_rmdir_free(a->thargs);
22943 +               a->thargs = NULL;
22944 +       }
22945 +out:
22946 +       return err;
22947 +}
22948 +
22949 +/* ---------------------------------------------------------------------- */
22950 +
22951 +/*
22952 + * test if @dentry dir can be rename destination or not.
22953 + * success means, it is a logically empty dir.
22954 + */
22955 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
22956 +{
22957 +       return au_test_empty(dentry, whlist);
22958 +}
22959 +
22960 +/*
22961 + * test if @a->src_dentry dir can be rename source or not.
22962 + * if it can, return 0.
22963 + * success means,
22964 + * - it is a logically empty dir.
22965 + * - or, it exists on writable branch and has no children including whiteouts
22966 + *   on the lower branch unless DIRREN is on.
22967 + */
22968 +static int may_rename_srcdir(struct au_ren_args *a)
22969 +{
22970 +       int err;
22971 +       unsigned int rdhash;
22972 +       aufs_bindex_t btop, btgt;
22973 +       struct dentry *dentry;
22974 +       struct super_block *sb;
22975 +       struct au_sbinfo *sbinfo;
22976 +
22977 +       dentry = a->src_dentry;
22978 +       sb = dentry->d_sb;
22979 +       sbinfo = au_sbi(sb);
22980 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
22981 +               au_fset_ren(a->auren_flags, DIRREN);
22982 +
22983 +       btgt = a->btgt;
22984 +       btop = au_dbtop(dentry);
22985 +       if (btop != btgt) {
22986 +               struct au_nhash whlist;
22987 +
22988 +               SiMustAnyLock(sb);
22989 +               rdhash = sbinfo->si_rdhash;
22990 +               if (!rdhash)
22991 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
22992 +                                                          dentry));
22993 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
22994 +               if (unlikely(err))
22995 +                       goto out;
22996 +               err = au_test_empty(dentry, &whlist);
22997 +               au_nhash_wh_free(&whlist);
22998 +               goto out;
22999 +       }
23000 +
23001 +       if (btop == au_dbtaildir(dentry))
23002 +               return 0; /* success */
23003 +
23004 +       err = au_test_empty_lower(dentry);
23005 +
23006 +out:
23007 +       if (err == -ENOTEMPTY) {
23008 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23009 +                       err = 0;
23010 +               } else {
23011 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23012 +                               "branches, is not supported\n");
23013 +                       err = -EXDEV;
23014 +               }
23015 +       }
23016 +       return err;
23017 +}
23018 +
23019 +/* side effect: sets whlist and h_dentry */
23020 +static int au_ren_may_dir(struct au_ren_args *a)
23021 +{
23022 +       int err;
23023 +       unsigned int rdhash;
23024 +       struct dentry *d;
23025 +
23026 +       d = a->dst_dentry;
23027 +       SiMustAnyLock(d->d_sb);
23028 +
23029 +       err = 0;
23030 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23031 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23032 +               if (!rdhash)
23033 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23034 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23035 +               if (unlikely(err))
23036 +                       goto out;
23037 +
23038 +               if (!a->exchange) {
23039 +                       au_set_dbtop(d, a->dst_btop);
23040 +                       err = may_rename_dstdir(d, &a->whlist);
23041 +                       au_set_dbtop(d, a->btgt);
23042 +               } else
23043 +                       err = may_rename_srcdir(a);
23044 +       }
23045 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23046 +       if (unlikely(err))
23047 +               goto out;
23048 +
23049 +       d = a->src_dentry;
23050 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23051 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23052 +               err = may_rename_srcdir(a);
23053 +               if (unlikely(err)) {
23054 +                       au_nhash_wh_free(&a->whlist);
23055 +                       a->whlist.nh_num = 0;
23056 +               }
23057 +       }
23058 +out:
23059 +       return err;
23060 +}
23061 +
23062 +/* ---------------------------------------------------------------------- */
23063 +
23064 +/*
23065 + * simple tests for rename.
23066 + * following the checks in vfs, plus the parent-child relationship.
23067 + */
23068 +static int au_may_ren(struct au_ren_args *a)
23069 +{
23070 +       int err, isdir;
23071 +       struct inode *h_inode;
23072 +
23073 +       if (a->src_btop == a->btgt) {
23074 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23075 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23076 +               if (unlikely(err))
23077 +                       goto out;
23078 +               err = -EINVAL;
23079 +               if (unlikely(a->src_h_dentry == a->h_trap))
23080 +                       goto out;
23081 +       }
23082 +
23083 +       err = 0;
23084 +       if (a->dst_btop != a->btgt)
23085 +               goto out;
23086 +
23087 +       err = -ENOTEMPTY;
23088 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23089 +               goto out;
23090 +
23091 +       err = -EIO;
23092 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23093 +       if (d_really_is_negative(a->dst_dentry)) {
23094 +               if (d_is_negative(a->dst_h_dentry))
23095 +                       err = au_may_add(a->dst_dentry, a->btgt,
23096 +                                        a->dst_h_parent, isdir);
23097 +       } else {
23098 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23099 +                       goto out;
23100 +               h_inode = d_inode(a->dst_h_dentry);
23101 +               if (h_inode->i_nlink)
23102 +                       err = au_may_del(a->dst_dentry, a->btgt,
23103 +                                        a->dst_h_parent, isdir);
23104 +       }
23105 +
23106 +out:
23107 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23108 +               err = -EIO;
23109 +       AuTraceErr(err);
23110 +       return err;
23111 +}
23112 +
23113 +/* ---------------------------------------------------------------------- */
23114 +
23115 +/*
23116 + * locking order
23117 + * (VFS)
23118 + * - src_dir and dir by lock_rename()
23119 + * - inode if exists
23120 + * (aufs)
23121 + * - lock all
23122 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23123 + *     + si_read_lock
23124 + *     + di_write_lock2_child()
23125 + *       + di_write_lock_child()
23126 + *        + ii_write_lock_child()
23127 + *       + di_write_lock_child2()
23128 + *        + ii_write_lock_child2()
23129 + *     + src_parent and parent
23130 + *       + di_write_lock_parent()
23131 + *        + ii_write_lock_parent()
23132 + *       + di_write_lock_parent2()
23133 + *        + ii_write_lock_parent2()
23134 + *   + lower src_dir and dir by vfsub_lock_rename()
23135 + *   + verify the every relationships between child and parent. if any
23136 + *     of them failed, unlock all and return -EBUSY.
23137 + */
23138 +static void au_ren_unlock(struct au_ren_args *a)
23139 +{
23140 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23141 +                           a->dst_h_parent, a->dst_hdir);
23142 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23143 +           && a->h_root)
23144 +               au_hn_inode_unlock(a->h_root);
23145 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23146 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23147 +}
23148 +
23149 +static int au_ren_lock(struct au_ren_args *a)
23150 +{
23151 +       int err;
23152 +       unsigned int udba;
23153 +
23154 +       err = 0;
23155 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23156 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23157 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23158 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23159 +
23160 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23161 +       if (unlikely(err))
23162 +               goto out;
23163 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23164 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23165 +               struct dentry *root;
23166 +               struct inode *dir;
23167 +
23168 +               /*
23169 +                * sbinfo is already locked, so this ii_read_lock is
23170 +                * unnecessary. but our debugging feature checks it.
23171 +                */
23172 +               root = a->src_inode->i_sb->s_root;
23173 +               if (root != a->src_parent && root != a->dst_parent) {
23174 +                       dir = d_inode(root);
23175 +                       ii_read_lock_parent3(dir);
23176 +                       a->h_root = au_hi(dir, a->btgt);
23177 +                       ii_read_unlock(dir);
23178 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23179 +               }
23180 +       }
23181 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23182 +                                     a->dst_h_parent, a->dst_hdir);
23183 +       udba = au_opt_udba(a->src_dentry->d_sb);
23184 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23185 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23186 +               err = au_busy_or_stale();
23187 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23188 +               err = au_h_verify(a->src_h_dentry, udba,
23189 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23190 +                                 a->br);
23191 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23192 +               err = au_h_verify(a->dst_h_dentry, udba,
23193 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23194 +                                 a->br);
23195 +       if (!err)
23196 +               goto out; /* success */
23197 +
23198 +       err = au_busy_or_stale();
23199 +       au_ren_unlock(a);
23200 +
23201 +out:
23202 +       return err;
23203 +}
23204 +
23205 +/* ---------------------------------------------------------------------- */
23206 +
23207 +static void au_ren_refresh_dir(struct au_ren_args *a)
23208 +{
23209 +       struct inode *dir;
23210 +
23211 +       dir = a->dst_dir;
23212 +       inode_inc_iversion(dir);
23213 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23214 +               /* is this updating defined in POSIX? */
23215 +               au_cpup_attr_timesizes(a->src_inode);
23216 +               au_cpup_attr_nlink(dir, /*force*/1);
23217 +       }
23218 +       au_dir_ts(dir, a->btgt);
23219 +
23220 +       if (a->exchange) {
23221 +               dir = a->src_dir;
23222 +               inode_inc_iversion(dir);
23223 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23224 +                       /* is this updating defined in POSIX? */
23225 +                       au_cpup_attr_timesizes(a->dst_inode);
23226 +                       au_cpup_attr_nlink(dir, /*force*/1);
23227 +               }
23228 +               au_dir_ts(dir, a->btgt);
23229 +       }
23230 +
23231 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23232 +               return;
23233 +
23234 +       dir = a->src_dir;
23235 +       inode_inc_iversion(dir);
23236 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23237 +               au_cpup_attr_nlink(dir, /*force*/1);
23238 +       au_dir_ts(dir, a->btgt);
23239 +}
23240 +
23241 +static void au_ren_refresh(struct au_ren_args *a)
23242 +{
23243 +       aufs_bindex_t bbot, bindex;
23244 +       struct dentry *d, *h_d;
23245 +       struct inode *i, *h_i;
23246 +       struct super_block *sb;
23247 +
23248 +       d = a->dst_dentry;
23249 +       d_drop(d);
23250 +       if (a->h_dst)
23251 +               /* already dget-ed by au_ren_or_cpup() */
23252 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23253 +
23254 +       i = a->dst_inode;
23255 +       if (i) {
23256 +               if (!a->exchange) {
23257 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23258 +                               vfsub_drop_nlink(i);
23259 +                       else {
23260 +                               vfsub_dead_dir(i);
23261 +                               au_cpup_attr_timesizes(i);
23262 +                       }
23263 +                       au_update_dbrange(d, /*do_put_zero*/1);
23264 +               } else
23265 +                       au_cpup_attr_nlink(i, /*force*/1);
23266 +       } else {
23267 +               bbot = a->btgt;
23268 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23269 +                       au_set_h_dptr(d, bindex, NULL);
23270 +               bbot = au_dbbot(d);
23271 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23272 +                       au_set_h_dptr(d, bindex, NULL);
23273 +               au_update_dbrange(d, /*do_put_zero*/0);
23274 +       }
23275 +
23276 +       if (a->exchange
23277 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23278 +               d_drop(a->src_dentry);
23279 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23280 +                       au_set_dbwh(a->src_dentry, -1);
23281 +               return;
23282 +       }
23283 +
23284 +       d = a->src_dentry;
23285 +       au_set_dbwh(d, -1);
23286 +       bbot = au_dbbot(d);
23287 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23288 +               h_d = au_h_dptr(d, bindex);
23289 +               if (h_d)
23290 +                       au_set_h_dptr(d, bindex, NULL);
23291 +       }
23292 +       au_set_dbbot(d, a->btgt);
23293 +
23294 +       sb = d->d_sb;
23295 +       i = a->src_inode;
23296 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23297 +               return; /* success */
23298 +
23299 +       bbot = au_ibbot(i);
23300 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23301 +               h_i = au_h_iptr(i, bindex);
23302 +               if (h_i) {
23303 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23304 +                       /* ignore this error */
23305 +                       au_set_h_iptr(i, bindex, NULL, 0);
23306 +               }
23307 +       }
23308 +       au_set_ibbot(i, a->btgt);
23309 +}
23310 +
23311 +/* ---------------------------------------------------------------------- */
23312 +
23313 +/* mainly for link(2) and rename(2) */
23314 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23315 +{
23316 +       aufs_bindex_t bdiropq, bwh;
23317 +       struct dentry *parent;
23318 +       struct au_branch *br;
23319 +
23320 +       parent = dentry->d_parent;
23321 +       IMustLock(d_inode(parent)); /* dir is locked */
23322 +
23323 +       bdiropq = au_dbdiropq(parent);
23324 +       bwh = au_dbwh(dentry);
23325 +       br = au_sbr(dentry->d_sb, btgt);
23326 +       if (au_br_rdonly(br)
23327 +           || (0 <= bdiropq && bdiropq < btgt)
23328 +           || (0 <= bwh && bwh < btgt))
23329 +               btgt = -1;
23330 +
23331 +       AuDbg("btgt %d\n", btgt);
23332 +       return btgt;
23333 +}
23334 +
23335 +/* sets src_btop, dst_btop and btgt */
23336 +static int au_ren_wbr(struct au_ren_args *a)
23337 +{
23338 +       int err;
23339 +       struct au_wr_dir_args wr_dir_args = {
23340 +               /* .force_btgt  = -1, */
23341 +               .flags          = AuWrDir_ADD_ENTRY
23342 +       };
23343 +
23344 +       a->src_btop = au_dbtop(a->src_dentry);
23345 +       a->dst_btop = au_dbtop(a->dst_dentry);
23346 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23347 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23348 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23349 +       wr_dir_args.force_btgt = a->src_btop;
23350 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23351 +               wr_dir_args.force_btgt = a->dst_btop;
23352 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23353 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23354 +       a->btgt = err;
23355 +       if (a->exchange)
23356 +               au_update_dbtop(a->dst_dentry);
23357 +
23358 +       return err;
23359 +}
23360 +
23361 +static void au_ren_dt(struct au_ren_args *a)
23362 +{
23363 +       a->h_path.dentry = a->src_h_parent;
23364 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23365 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23366 +               a->h_path.dentry = a->dst_h_parent;
23367 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23368 +       }
23369 +
23370 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23371 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23372 +           && !a->exchange)
23373 +               return;
23374 +
23375 +       a->h_path.dentry = a->src_h_dentry;
23376 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23377 +       if (d_is_positive(a->dst_h_dentry)) {
23378 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23379 +               a->h_path.dentry = a->dst_h_dentry;
23380 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23381 +       }
23382 +}
23383 +
23384 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23385 +{
23386 +       struct dentry *h_d;
23387 +       struct inode *h_inode;
23388 +
23389 +       au_dtime_revert(a->src_dt + AuPARENT);
23390 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23391 +               au_dtime_revert(a->dst_dt + AuPARENT);
23392 +
23393 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23394 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23395 +               h_inode = d_inode(h_d);
23396 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23397 +               au_dtime_revert(a->src_dt + AuCHILD);
23398 +               inode_unlock(h_inode);
23399 +
23400 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23401 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23402 +                       h_inode = d_inode(h_d);
23403 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23404 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23405 +                       inode_unlock(h_inode);
23406 +               }
23407 +       }
23408 +}
23409 +
23410 +/* ---------------------------------------------------------------------- */
23411 +
23412 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
23413 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23414 +               unsigned int _flags)
23415 +{
23416 +       int err, lock_flags;
23417 +       void *rev;
23418 +       /* reduce stack space */
23419 +       struct au_ren_args *a;
23420 +       struct au_pin pin;
23421 +
23422 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23423 +       IMustLock(_src_dir);
23424 +       IMustLock(_dst_dir);
23425 +
23426 +       err = -EINVAL;
23427 +       if (unlikely(_flags & RENAME_WHITEOUT))
23428 +               goto out;
23429 +
23430 +       err = -ENOMEM;
23431 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23432 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23433 +       if (unlikely(!a))
23434 +               goto out;
23435 +
23436 +       a->flags = _flags;
23437 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23438 +                    && RENAME_EXCHANGE > U8_MAX);
23439 +       a->exchange = _flags & RENAME_EXCHANGE;
23440 +       a->src_dir = _src_dir;
23441 +       a->src_dentry = _src_dentry;
23442 +       a->src_inode = NULL;
23443 +       if (d_really_is_positive(a->src_dentry))
23444 +               a->src_inode = d_inode(a->src_dentry);
23445 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23446 +       a->dst_dir = _dst_dir;
23447 +       a->dst_dentry = _dst_dentry;
23448 +       a->dst_inode = NULL;
23449 +       if (d_really_is_positive(a->dst_dentry))
23450 +               a->dst_inode = d_inode(a->dst_dentry);
23451 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23452 +       if (a->dst_inode) {
23453 +               /*
23454 +                * if EXCHANGE && src is non-dir && dst is dir,
23455 +                * dst is not locked.
23456 +                */
23457 +               /* IMustLock(a->dst_inode); */
23458 +               au_igrab(a->dst_inode);
23459 +       }
23460 +
23461 +       err = -ENOTDIR;
23462 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23463 +       if (d_is_dir(a->src_dentry)) {
23464 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23465 +               if (unlikely(!a->exchange
23466 +                            && d_really_is_positive(a->dst_dentry)
23467 +                            && !d_is_dir(a->dst_dentry)))
23468 +                       goto out_free;
23469 +               lock_flags |= AuLock_DIRS;
23470 +       }
23471 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23472 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23473 +               if (unlikely(!a->exchange
23474 +                            && d_really_is_positive(a->src_dentry)
23475 +                            && !d_is_dir(a->src_dentry)))
23476 +                       goto out_free;
23477 +               lock_flags |= AuLock_DIRS;
23478 +       }
23479 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23480 +                                       lock_flags);
23481 +       if (unlikely(err))
23482 +               goto out_free;
23483 +
23484 +       err = au_d_hashed_positive(a->src_dentry);
23485 +       if (unlikely(err))
23486 +               goto out_unlock;
23487 +       err = -ENOENT;
23488 +       if (a->dst_inode) {
23489 +               /*
23490 +                * If it is a dir, VFS unhash it before this
23491 +                * function. It means we cannot rely upon d_unhashed().
23492 +                */
23493 +               if (unlikely(!a->dst_inode->i_nlink))
23494 +                       goto out_unlock;
23495 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23496 +                       err = au_d_hashed_positive(a->dst_dentry);
23497 +                       if (unlikely(err && !a->exchange))
23498 +                               goto out_unlock;
23499 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23500 +                       goto out_unlock;
23501 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23502 +               goto out_unlock;
23503 +
23504 +       /*
23505 +        * is it possible?
23506 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23507 +        * there may exist a problem somewhere else.
23508 +        */
23509 +       err = -EINVAL;
23510 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23511 +               goto out_unlock;
23512 +
23513 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23514 +       di_write_lock_parent(a->dst_parent);
23515 +
23516 +       /* which branch we process */
23517 +       err = au_ren_wbr(a);
23518 +       if (unlikely(err < 0))
23519 +               goto out_parent;
23520 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23521 +       a->h_path.mnt = au_br_mnt(a->br);
23522 +
23523 +       /* are they available to be renamed */
23524 +       err = au_ren_may_dir(a);
23525 +       if (unlikely(err))
23526 +               goto out_children;
23527 +
23528 +       /* prepare the writable parent dir on the same branch */
23529 +       if (a->dst_btop == a->btgt) {
23530 +               au_fset_ren(a->auren_flags, WHDST);
23531 +       } else {
23532 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23533 +               if (unlikely(err))
23534 +                       goto out_children;
23535 +       }
23536 +
23537 +       err = 0;
23538 +       if (!a->exchange) {
23539 +               if (a->src_dir != a->dst_dir) {
23540 +                       /*
23541 +                        * this temporary unlock is safe,
23542 +                        * because both dir->i_mutex are locked.
23543 +                        */
23544 +                       di_write_unlock(a->dst_parent);
23545 +                       di_write_lock_parent(a->src_parent);
23546 +                       err = au_wr_dir_need_wh(a->src_dentry,
23547 +                                               au_ftest_ren(a->auren_flags,
23548 +                                                            ISDIR_SRC),
23549 +                                               &a->btgt);
23550 +                       di_write_unlock(a->src_parent);
23551 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23552 +                                             /*isdir*/1);
23553 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23554 +               } else
23555 +                       err = au_wr_dir_need_wh(a->src_dentry,
23556 +                                               au_ftest_ren(a->auren_flags,
23557 +                                                            ISDIR_SRC),
23558 +                                               &a->btgt);
23559 +       }
23560 +       if (unlikely(err < 0))
23561 +               goto out_children;
23562 +       if (err)
23563 +               au_fset_ren(a->auren_flags, WHSRC);
23564 +
23565 +       /* cpup src */
23566 +       if (a->src_btop != a->btgt) {
23567 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23568 +                            au_opt_udba(a->src_dentry->d_sb),
23569 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23570 +               if (!err) {
23571 +                       struct au_cp_generic cpg = {
23572 +                               .dentry = a->src_dentry,
23573 +                               .bdst   = a->btgt,
23574 +                               .bsrc   = a->src_btop,
23575 +                               .len    = -1,
23576 +                               .pin    = &pin,
23577 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23578 +                       };
23579 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23580 +                       err = au_sio_cpup_simple(&cpg);
23581 +                       au_unpin(&pin);
23582 +               }
23583 +               if (unlikely(err))
23584 +                       goto out_children;
23585 +               a->src_btop = a->btgt;
23586 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23587 +               if (!a->exchange)
23588 +                       au_fset_ren(a->auren_flags, WHSRC);
23589 +       }
23590 +
23591 +       /* cpup dst */
23592 +       if (a->exchange && a->dst_inode
23593 +           && a->dst_btop != a->btgt) {
23594 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23595 +                            au_opt_udba(a->dst_dentry->d_sb),
23596 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23597 +               if (!err) {
23598 +                       struct au_cp_generic cpg = {
23599 +                               .dentry = a->dst_dentry,
23600 +                               .bdst   = a->btgt,
23601 +                               .bsrc   = a->dst_btop,
23602 +                               .len    = -1,
23603 +                               .pin    = &pin,
23604 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23605 +                       };
23606 +                       err = au_sio_cpup_simple(&cpg);
23607 +                       au_unpin(&pin);
23608 +               }
23609 +               if (unlikely(err))
23610 +                       goto out_children;
23611 +               a->dst_btop = a->btgt;
23612 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23613 +       }
23614 +
23615 +       /* lock them all */
23616 +       err = au_ren_lock(a);
23617 +       if (unlikely(err))
23618 +               /* leave the copied-up one */
23619 +               goto out_children;
23620 +
23621 +       if (!a->exchange) {
23622 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23623 +                       err = au_may_ren(a);
23624 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23625 +                       err = -ENAMETOOLONG;
23626 +               if (unlikely(err))
23627 +                       goto out_hdir;
23628 +       }
23629 +
23630 +       /* store timestamps to be revertible */
23631 +       au_ren_dt(a);
23632 +
23633 +       /* store dirren info */
23634 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23635 +               err = au_dr_rename(a->src_dentry, a->btgt,
23636 +                                  &a->dst_dentry->d_name, &rev);
23637 +               AuTraceErr(err);
23638 +               if (unlikely(err))
23639 +                       goto out_dt;
23640 +       }
23641 +
23642 +       /* here we go */
23643 +       err = do_rename(a);
23644 +       if (unlikely(err))
23645 +               goto out_dirren;
23646 +
23647 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23648 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23649 +
23650 +       /* update dir attributes */
23651 +       au_ren_refresh_dir(a);
23652 +
23653 +       /* dput/iput all lower dentries */
23654 +       au_ren_refresh(a);
23655 +
23656 +       goto out_hdir; /* success */
23657 +
23658 +out_dirren:
23659 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23660 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23661 +out_dt:
23662 +       au_ren_rev_dt(err, a);
23663 +out_hdir:
23664 +       au_ren_unlock(a);
23665 +out_children:
23666 +       au_nhash_wh_free(&a->whlist);
23667 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23668 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23669 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23670 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23671 +       }
23672 +out_parent:
23673 +       if (!err) {
23674 +               if (d_unhashed(a->src_dentry))
23675 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23676 +               if (d_unhashed(a->dst_dentry))
23677 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23678 +               if (!a->exchange)
23679 +                       d_move(a->src_dentry, a->dst_dentry);
23680 +               else {
23681 +                       d_exchange(a->src_dentry, a->dst_dentry);
23682 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23683 +                               d_drop(a->dst_dentry);
23684 +               }
23685 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23686 +                       d_drop(a->src_dentry);
23687 +       } else {
23688 +               au_update_dbtop(a->dst_dentry);
23689 +               if (!a->dst_inode)
23690 +                       d_drop(a->dst_dentry);
23691 +       }
23692 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23693 +               di_write_unlock(a->dst_parent);
23694 +       else
23695 +               di_write_unlock2(a->src_parent, a->dst_parent);
23696 +out_unlock:
23697 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23698 +out_free:
23699 +       iput(a->dst_inode);
23700 +       if (a->thargs)
23701 +               au_whtmp_rmdir_free(a->thargs);
23702 +       au_kfree_rcu(a);
23703 +out:
23704 +       AuTraceErr(err);
23705 +       return err;
23706 +}
23707 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23708 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23709 +++ linux/fs/aufs/Kconfig       2019-07-11 15:42:14.458904362 +0200
23710 @@ -0,0 +1,199 @@
23711 +# SPDX-License-Identifier: GPL-2.0
23712 +config AUFS_FS
23713 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23714 +       help
23715 +       Aufs is a stackable unification filesystem such as Unionfs,
23716 +       which unifies several directories and provides a merged single
23717 +       directory.
23718 +       In the early days, aufs was entirely re-designed and
23719 +       re-implemented Unionfs Version 1.x series. Introducing many
23720 +       original ideas, approaches and improvements, it becomes totally
23721 +       different from Unionfs while keeping the basic features.
23722 +
23723 +if AUFS_FS
23724 +choice
23725 +       prompt "Maximum number of branches"
23726 +       default AUFS_BRANCH_MAX_127
23727 +       help
23728 +       Specifies the maximum number of branches (or member directories)
23729 +       in a single aufs. The larger value consumes more system
23730 +       resources and has a minor impact to performance.
23731 +config AUFS_BRANCH_MAX_127
23732 +       bool "127"
23733 +       help
23734 +       Specifies the maximum number of branches (or member directories)
23735 +       in a single aufs. The larger value consumes more system
23736 +       resources and has a minor impact to performance.
23737 +config AUFS_BRANCH_MAX_511
23738 +       bool "511"
23739 +       help
23740 +       Specifies the maximum number of branches (or member directories)
23741 +       in a single aufs. The larger value consumes more system
23742 +       resources and has a minor impact to performance.
23743 +config AUFS_BRANCH_MAX_1023
23744 +       bool "1023"
23745 +       help
23746 +       Specifies the maximum number of branches (or member directories)
23747 +       in a single aufs. The larger value consumes more system
23748 +       resources and has a minor impact to performance.
23749 +config AUFS_BRANCH_MAX_32767
23750 +       bool "32767"
23751 +       help
23752 +       Specifies the maximum number of branches (or member directories)
23753 +       in a single aufs. The larger value consumes more system
23754 +       resources and has a minor impact to performance.
23755 +endchoice
23756 +
23757 +config AUFS_SBILIST
23758 +       bool
23759 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23760 +       default y
23761 +       help
23762 +       Automatic configuration for internal use.
23763 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23764 +
23765 +config AUFS_HNOTIFY
23766 +       bool "Detect direct branch access (bypassing aufs)"
23767 +       help
23768 +       If you want to modify files on branches directly, eg. bypassing aufs,
23769 +       and want aufs to detect the changes of them fully, then enable this
23770 +       option and use 'udba=notify' mount option.
23771 +       Currently there is only one available configuration, "fsnotify".
23772 +       It will have a negative impact to the performance.
23773 +       See detail in aufs.5.
23774 +
23775 +choice
23776 +       prompt "method" if AUFS_HNOTIFY
23777 +       default AUFS_HFSNOTIFY
23778 +config AUFS_HFSNOTIFY
23779 +       bool "fsnotify"
23780 +       select FSNOTIFY
23781 +endchoice
23782 +
23783 +config AUFS_EXPORT
23784 +       bool "NFS-exportable aufs"
23785 +       depends on EXPORTFS
23786 +       help
23787 +       If you want to export your mounted aufs via NFS, then enable this
23788 +       option. There are several requirements for this configuration.
23789 +       See detail in aufs.5.
23790 +
23791 +config AUFS_INO_T_64
23792 +       bool
23793 +       depends on AUFS_EXPORT
23794 +       depends on 64BIT && !(ALPHA || S390)
23795 +       default y
23796 +       help
23797 +       Automatic configuration for internal use.
23798 +       /* typedef unsigned long/int __kernel_ino_t */
23799 +       /* alpha and s390x are int */
23800 +
23801 +config AUFS_XATTR
23802 +       bool "support for XATTR/EA (including Security Labels)"
23803 +       help
23804 +       If your branch fs supports XATTR/EA and you want to make them
23805 +       available in aufs too, then enable this opsion and specify the
23806 +       branch attributes for EA.
23807 +       See detail in aufs.5.
23808 +
23809 +config AUFS_FHSM
23810 +       bool "File-based Hierarchical Storage Management"
23811 +       help
23812 +       Hierarchical Storage Management (or HSM) is a well-known feature
23813 +       in the storage world. Aufs provides this feature as file-based.
23814 +       with multiple branches.
23815 +       These multiple branches are prioritized, ie. the topmost one
23816 +       should be the fastest drive and be used heavily.
23817 +
23818 +config AUFS_RDU
23819 +       bool "Readdir in userspace"
23820 +       help
23821 +       Aufs has two methods to provide a merged view for a directory,
23822 +       by a user-space library and by kernel-space natively. The latter
23823 +       is always enabled but sometimes large and slow.
23824 +       If you enable this option, install the library in aufs2-util
23825 +       package, and set some environment variables for your readdir(3),
23826 +       then the work will be handled in user-space which generally
23827 +       shows better performance in most cases.
23828 +       See detail in aufs.5.
23829 +
23830 +config AUFS_DIRREN
23831 +       bool "Workaround for rename(2)-ing a directory"
23832 +       help
23833 +       By default, aufs returns EXDEV error in renameing a dir who has
23834 +       his child on the lower branch, since it is a bad idea to issue
23835 +       rename(2) internally for every lower branch. But user may not
23836 +       accept this behaviour. So here is a workaround to allow such
23837 +       rename(2) and store some extra infromation on the writable
23838 +       branch. Obviously this costs high (and I don't like it).
23839 +       To use this feature, you need to enable this configuration AND
23840 +       to specify the mount option `dirren.'
23841 +       See details in aufs.5 and the design documents.
23842 +
23843 +config AUFS_SHWH
23844 +       bool "Show whiteouts"
23845 +       help
23846 +       If you want to make the whiteouts in aufs visible, then enable
23847 +       this option and specify 'shwh' mount option. Although it may
23848 +       sounds like philosophy or something, but in technically it
23849 +       simply shows the name of whiteout with keeping its behaviour.
23850 +
23851 +config AUFS_BR_RAMFS
23852 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23853 +       help
23854 +       If you want to use ramfs as an aufs branch fs, then enable this
23855 +       option. Generally tmpfs is recommended.
23856 +       Aufs prohibited them to be a branch fs by default, because
23857 +       initramfs becomes unusable after switch_root or something
23858 +       generally. If you sets initramfs as an aufs branch and boot your
23859 +       system by switch_root, you will meet a problem easily since the
23860 +       files in initramfs may be inaccessible.
23861 +       Unless you are going to use ramfs as an aufs branch fs without
23862 +       switch_root or something, leave it N.
23863 +
23864 +config AUFS_BR_FUSE
23865 +       bool "Fuse fs as an aufs branch"
23866 +       depends on FUSE_FS
23867 +       select AUFS_POLL
23868 +       help
23869 +       If you want to use fuse-based userspace filesystem as an aufs
23870 +       branch fs, then enable this option.
23871 +       It implements the internal poll(2) operation which is
23872 +       implemented by fuse only (curretnly).
23873 +
23874 +config AUFS_POLL
23875 +       bool
23876 +       help
23877 +       Automatic configuration for internal use.
23878 +
23879 +config AUFS_BR_HFSPLUS
23880 +       bool "Hfsplus as an aufs branch"
23881 +       depends on HFSPLUS_FS
23882 +       default y
23883 +       help
23884 +       If you want to use hfsplus fs as an aufs branch fs, then enable
23885 +       this option. This option introduces a small overhead at
23886 +       copying-up a file on hfsplus.
23887 +
23888 +config AUFS_BDEV_LOOP
23889 +       bool
23890 +       depends on BLK_DEV_LOOP
23891 +       default y
23892 +       help
23893 +       Automatic configuration for internal use.
23894 +       Convert =[ym] into =y.
23895 +
23896 +config AUFS_DEBUG
23897 +       bool "Debug aufs"
23898 +       help
23899 +       Enable this to compile aufs internal debug code.
23900 +       It will have a negative impact to the performance.
23901 +
23902 +config AUFS_MAGIC_SYSRQ
23903 +       bool
23904 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
23905 +       default y
23906 +       help
23907 +       Automatic configuration for internal use.
23908 +       When aufs supports Magic SysRq, enabled automatically.
23909 +endif
23910 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23911 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
23912 +++ linux/fs/aufs/lcnt.h        2020-01-27 10:57:18.175538316 +0100
23913 @@ -0,0 +1,186 @@
23914 +/* SPDX-License-Identifier: GPL-2.0 */
23915 +/*
23916 + * Copyright (C) 2018-2020 Junjiro R. Okajima
23917 + *
23918 + * This program, aufs is free software; you can redistribute it and/or modify
23919 + * it under the terms of the GNU General Public License as published by
23920 + * the Free Software Foundation; either version 2 of the License, or
23921 + * (at your option) any later version.
23922 + *
23923 + * This program is distributed in the hope that it will be useful,
23924 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23925 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23926 + * GNU General Public License for more details.
23927 + *
23928 + * You should have received a copy of the GNU General Public License
23929 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23930 + */
23931 +
23932 +/*
23933 + * simple long counter wrapper
23934 + */
23935 +
23936 +#ifndef __AUFS_LCNT_H__
23937 +#define __AUFS_LCNT_H__
23938 +
23939 +#ifdef __KERNEL__
23940 +
23941 +#include "debug.h"
23942 +
23943 +#define AuLCntATOMIC   1
23944 +#define AuLCntPCPUCNT  2
23945 +/*
23946 + * why does percpu_refcount require extra synchronize_rcu()s in
23947 + * au_br_do_free()
23948 + */
23949 +#define AuLCntPCPUREF  3
23950 +
23951 +/* #define AuLCntChosen        AuLCntATOMIC */
23952 +#define AuLCntChosen   AuLCntPCPUCNT
23953 +/* #define AuLCntChosen        AuLCntPCPUREF */
23954 +
23955 +#if AuLCntChosen == AuLCntATOMIC
23956 +#include <linux/atomic.h>
23957 +
23958 +typedef atomic_long_t au_lcnt_t;
23959 +
23960 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23961 +{
23962 +       atomic_long_set(cnt, 0);
23963 +       return 0;
23964 +}
23965 +
23966 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
23967 +{
23968 +       /* empty */
23969 +}
23970 +
23971 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
23972 +                              int do_sync __maybe_unused)
23973 +{
23974 +       /* empty */
23975 +}
23976 +
23977 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
23978 +{
23979 +       atomic_long_inc(cnt);
23980 +}
23981 +
23982 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
23983 +{
23984 +       atomic_long_dec(cnt);
23985 +}
23986 +
23987 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
23988 +{
23989 +       return atomic_long_read(cnt);
23990 +}
23991 +#endif
23992 +
23993 +#if AuLCntChosen == AuLCntPCPUCNT
23994 +#include <linux/percpu_counter.h>
23995 +
23996 +typedef struct percpu_counter au_lcnt_t;
23997 +
23998 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
23999 +{
24000 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24001 +}
24002 +
24003 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24004 +{
24005 +       /* empty */
24006 +}
24007 +
24008 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24009 +{
24010 +       percpu_counter_destroy(cnt);
24011 +}
24012 +
24013 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24014 +{
24015 +       percpu_counter_inc(cnt);
24016 +}
24017 +
24018 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24019 +{
24020 +       percpu_counter_dec(cnt);
24021 +}
24022 +
24023 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24024 +{
24025 +       s64 n;
24026 +
24027 +       n = percpu_counter_sum(cnt);
24028 +       BUG_ON(n < 0);
24029 +       if (LONG_MAX != LLONG_MAX
24030 +           && n > LONG_MAX)
24031 +               AuWarn1("%s\n", "wrap-around");
24032 +
24033 +       return n;
24034 +}
24035 +#endif
24036 +
24037 +#if AuLCntChosen == AuLCntPCPUREF
24038 +#include <linux/percpu-refcount.h>
24039 +
24040 +typedef struct percpu_ref au_lcnt_t;
24041 +
24042 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24043 +{
24044 +       if (!release)
24045 +               release = percpu_ref_exit;
24046 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24047 +}
24048 +
24049 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24050 +{
24051 +       synchronize_rcu();
24052 +}
24053 +
24054 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24055 +{
24056 +       percpu_ref_kill(cnt);
24057 +       if (do_sync)
24058 +               au_lcnt_wait_for_fin(cnt);
24059 +}
24060 +
24061 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24062 +{
24063 +       percpu_ref_get(cnt);
24064 +}
24065 +
24066 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24067 +{
24068 +       percpu_ref_put(cnt);
24069 +}
24070 +
24071 +/*
24072 + * avoid calling this func as possible.
24073 + */
24074 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24075 +{
24076 +       long l;
24077 +
24078 +       percpu_ref_switch_to_atomic_sync(cnt);
24079 +       l = atomic_long_read(&cnt->count);
24080 +       if (do_rev)
24081 +               percpu_ref_switch_to_percpu(cnt);
24082 +
24083 +       /* percpu_ref is initialized by 1 instead of 0 */
24084 +       return l - 1;
24085 +}
24086 +#endif
24087 +
24088 +#ifdef CONFIG_AUFS_DEBUG
24089 +#define AuLCntZero(val) do {                   \
24090 +       long l = val;                           \
24091 +       if (l)                                  \
24092 +               AuDbg("%s = %ld\n", #val, l);   \
24093 +} while (0)
24094 +#else
24095 +#define AuLCntZero(val)                do {} while (0)
24096 +#endif
24097 +
24098 +#endif /* __KERNEL__ */
24099 +#endif /* __AUFS_LCNT_H__ */
24100 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24101 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24102 +++ linux/fs/aufs/loop.c        2020-01-27 10:57:18.175538316 +0100
24103 @@ -0,0 +1,148 @@
24104 +// SPDX-License-Identifier: GPL-2.0
24105 +/*
24106 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24107 + *
24108 + * This program, aufs is free software; you can redistribute it and/or modify
24109 + * it under the terms of the GNU General Public License as published by
24110 + * the Free Software Foundation; either version 2 of the License, or
24111 + * (at your option) any later version.
24112 + *
24113 + * This program is distributed in the hope that it will be useful,
24114 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24115 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24116 + * GNU General Public License for more details.
24117 + *
24118 + * You should have received a copy of the GNU General Public License
24119 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24120 + */
24121 +
24122 +/*
24123 + * support for loopback block device as a branch
24124 + */
24125 +
24126 +#include "aufs.h"
24127 +
24128 +/* added into drivers/block/loop.c */
24129 +static struct file *(*backing_file_func)(struct super_block *sb);
24130 +
24131 +/*
24132 + * test if two lower dentries have overlapping branches.
24133 + */
24134 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24135 +{
24136 +       struct super_block *h_sb;
24137 +       struct file *backing_file;
24138 +
24139 +       if (unlikely(!backing_file_func)) {
24140 +               /* don't load "loop" module here */
24141 +               backing_file_func = symbol_get(loop_backing_file);
24142 +               if (unlikely(!backing_file_func))
24143 +                       /* "loop" module is not loaded */
24144 +                       return 0;
24145 +       }
24146 +
24147 +       h_sb = h_adding->d_sb;
24148 +       backing_file = backing_file_func(h_sb);
24149 +       if (!backing_file)
24150 +               return 0;
24151 +
24152 +       h_adding = backing_file->f_path.dentry;
24153 +       /*
24154 +        * h_adding can be local NFS.
24155 +        * in this case aufs cannot detect the loop.
24156 +        */
24157 +       if (unlikely(h_adding->d_sb == sb))
24158 +               return 1;
24159 +       return !!au_test_subdir(h_adding, sb->s_root);
24160 +}
24161 +
24162 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24163 +int au_test_loopback_kthread(void)
24164 +{
24165 +       int ret;
24166 +       struct task_struct *tsk = current;
24167 +       char c, comm[sizeof(tsk->comm)];
24168 +
24169 +       ret = 0;
24170 +       if (tsk->flags & PF_KTHREAD) {
24171 +               get_task_comm(comm, tsk);
24172 +               c = comm[4];
24173 +               ret = ('0' <= c && c <= '9'
24174 +                      && !strncmp(comm, "loop", 4));
24175 +       }
24176 +
24177 +       return ret;
24178 +}
24179 +
24180 +/* ---------------------------------------------------------------------- */
24181 +
24182 +#define au_warn_loopback_step  16
24183 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24184 +static unsigned long *au_warn_loopback_array;
24185 +
24186 +void au_warn_loopback(struct super_block *h_sb)
24187 +{
24188 +       int i, new_nelem;
24189 +       unsigned long *a, magic;
24190 +       static DEFINE_SPINLOCK(spin);
24191 +
24192 +       magic = h_sb->s_magic;
24193 +       spin_lock(&spin);
24194 +       a = au_warn_loopback_array;
24195 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24196 +               if (a[i] == magic) {
24197 +                       spin_unlock(&spin);
24198 +                       return;
24199 +               }
24200 +
24201 +       /* h_sb is new to us, print it */
24202 +       if (i < au_warn_loopback_nelem) {
24203 +               a[i] = magic;
24204 +               goto pr;
24205 +       }
24206 +
24207 +       /* expand the array */
24208 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24209 +       a = au_kzrealloc(au_warn_loopback_array,
24210 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24211 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24212 +                        /*may_shrink*/0);
24213 +       if (a) {
24214 +               au_warn_loopback_nelem = new_nelem;
24215 +               au_warn_loopback_array = a;
24216 +               a[i] = magic;
24217 +               goto pr;
24218 +       }
24219 +
24220 +       spin_unlock(&spin);
24221 +       AuWarn1("realloc failed, ignored\n");
24222 +       return;
24223 +
24224 +pr:
24225 +       spin_unlock(&spin);
24226 +       pr_warn("you may want to try another patch for loopback file "
24227 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24228 +}
24229 +
24230 +int au_loopback_init(void)
24231 +{
24232 +       int err;
24233 +       struct super_block *sb __maybe_unused;
24234 +
24235 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24236 +
24237 +       err = 0;
24238 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24239 +                                        sizeof(unsigned long), GFP_NOFS);
24240 +       if (unlikely(!au_warn_loopback_array))
24241 +               err = -ENOMEM;
24242 +
24243 +       return err;
24244 +}
24245 +
24246 +void au_loopback_fin(void)
24247 +{
24248 +       if (backing_file_func)
24249 +               symbol_put(loop_backing_file);
24250 +       au_kfree_try_rcu(au_warn_loopback_array);
24251 +}
24252 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24253 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24254 +++ linux/fs/aufs/loop.h        2020-01-27 10:57:18.175538316 +0100
24255 @@ -0,0 +1,55 @@
24256 +/* SPDX-License-Identifier: GPL-2.0 */
24257 +/*
24258 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24259 + *
24260 + * This program, aufs is free software; you can redistribute it and/or modify
24261 + * it under the terms of the GNU General Public License as published by
24262 + * the Free Software Foundation; either version 2 of the License, or
24263 + * (at your option) any later version.
24264 + *
24265 + * This program is distributed in the hope that it will be useful,
24266 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24267 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24268 + * GNU General Public License for more details.
24269 + *
24270 + * You should have received a copy of the GNU General Public License
24271 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24272 + */
24273 +
24274 +/*
24275 + * support for loopback mount as a branch
24276 + */
24277 +
24278 +#ifndef __AUFS_LOOP_H__
24279 +#define __AUFS_LOOP_H__
24280 +
24281 +#ifdef __KERNEL__
24282 +
24283 +struct dentry;
24284 +struct super_block;
24285 +
24286 +#ifdef CONFIG_AUFS_BDEV_LOOP
24287 +/* drivers/block/loop.c */
24288 +struct file *loop_backing_file(struct super_block *sb);
24289 +
24290 +/* loop.c */
24291 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24292 +int au_test_loopback_kthread(void);
24293 +void au_warn_loopback(struct super_block *h_sb);
24294 +
24295 +int au_loopback_init(void);
24296 +void au_loopback_fin(void);
24297 +#else
24298 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24299 +
24300 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24301 +          struct dentry *h_adding)
24302 +AuStubInt0(au_test_loopback_kthread, void)
24303 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24304 +
24305 +AuStubInt0(au_loopback_init, void)
24306 +AuStubVoid(au_loopback_fin, void)
24307 +#endif /* BLK_DEV_LOOP */
24308 +
24309 +#endif /* __KERNEL__ */
24310 +#endif /* __AUFS_LOOP_H__ */
24311 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24312 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24313 +++ linux/fs/aufs/magic.mk      2019-07-11 15:42:14.468904634 +0200
24314 @@ -0,0 +1,31 @@
24315 +# SPDX-License-Identifier: GPL-2.0
24316 +
24317 +# defined in ${srctree}/fs/fuse/inode.c
24318 +# tristate
24319 +ifdef CONFIG_FUSE_FS
24320 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24321 +endif
24322 +
24323 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24324 +# tristate
24325 +ifdef CONFIG_XFS_FS
24326 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24327 +endif
24328 +
24329 +# defined in ${srctree}/fs/configfs/mount.c
24330 +# tristate
24331 +ifdef CONFIG_CONFIGFS_FS
24332 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24333 +endif
24334 +
24335 +# defined in ${srctree}/fs/ubifs/ubifs.h
24336 +# tristate
24337 +ifdef CONFIG_UBIFS_FS
24338 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24339 +endif
24340 +
24341 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24342 +# tristate
24343 +ifdef CONFIG_HFSPLUS_FS
24344 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24345 +endif
24346 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24347 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24348 +++ linux/fs/aufs/Makefile      2019-07-11 15:42:14.462237786 +0200
24349 @@ -0,0 +1,46 @@
24350 +# SPDX-License-Identifier: GPL-2.0
24351 +
24352 +include ${src}/magic.mk
24353 +ifeq (${CONFIG_AUFS_FS},m)
24354 +include ${src}/conf.mk
24355 +endif
24356 +-include ${src}/priv_def.mk
24357 +
24358 +# cf. include/linux/kernel.h
24359 +# enable pr_debug
24360 +ccflags-y += -DDEBUG
24361 +# sparse requires the full pathname
24362 +ifdef M
24363 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24364 +else
24365 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24366 +endif
24367 +
24368 +obj-$(CONFIG_AUFS_FS) += aufs.o
24369 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24370 +       wkq.o vfsub.o dcsub.o \
24371 +       cpup.o whout.o wbr_policy.o \
24372 +       dinfo.o dentry.o \
24373 +       dynop.o \
24374 +       finfo.o file.o f_op.o \
24375 +       dir.o vdir.o \
24376 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24377 +       mvdown.o ioctl.o
24378 +
24379 +# all are boolean
24380 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24381 +aufs-$(CONFIG_SYSFS) += sysfs.o
24382 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24383 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24384 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24385 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24386 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24387 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24388 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24389 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24390 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24391 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24392 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24393 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24394 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24395 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24396 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24397 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24398 +++ linux/fs/aufs/module.c      2020-01-27 10:57:18.175538316 +0100
24399 @@ -0,0 +1,273 @@
24400 +// SPDX-License-Identifier: GPL-2.0
24401 +/*
24402 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24403 + *
24404 + * This program, aufs is free software; you can redistribute it and/or modify
24405 + * it under the terms of the GNU General Public License as published by
24406 + * the Free Software Foundation; either version 2 of the License, or
24407 + * (at your option) any later version.
24408 + *
24409 + * This program is distributed in the hope that it will be useful,
24410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24412 + * GNU General Public License for more details.
24413 + *
24414 + * You should have received a copy of the GNU General Public License
24415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24416 + */
24417 +
24418 +/*
24419 + * module global variables and operations
24420 + */
24421 +
24422 +#include <linux/module.h>
24423 +#include <linux/seq_file.h>
24424 +#include "aufs.h"
24425 +
24426 +/* shrinkable realloc */
24427 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24428 +{
24429 +       size_t sz;
24430 +       int diff;
24431 +
24432 +       sz = 0;
24433 +       diff = -1;
24434 +       if (p) {
24435 +#if 0 /* unused */
24436 +               if (!new_sz) {
24437 +                       au_kfree_rcu(p);
24438 +                       p = NULL;
24439 +                       goto out;
24440 +               }
24441 +#else
24442 +               AuDebugOn(!new_sz);
24443 +#endif
24444 +               sz = ksize(p);
24445 +               diff = au_kmidx_sub(sz, new_sz);
24446 +       }
24447 +       if (sz && !diff)
24448 +               goto out;
24449 +
24450 +       if (sz < new_sz)
24451 +               /* expand or SLOB */
24452 +               p = krealloc(p, new_sz, gfp);
24453 +       else if (new_sz < sz && may_shrink) {
24454 +               /* shrink */
24455 +               void *q;
24456 +
24457 +               q = kmalloc(new_sz, gfp);
24458 +               if (q) {
24459 +                       if (p) {
24460 +                               memcpy(q, p, new_sz);
24461 +                               au_kfree_try_rcu(p);
24462 +                       }
24463 +                       p = q;
24464 +               } else
24465 +                       p = NULL;
24466 +       }
24467 +
24468 +out:
24469 +       return p;
24470 +}
24471 +
24472 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24473 +                  int may_shrink)
24474 +{
24475 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24476 +       if (p && new_sz > nused)
24477 +               memset(p + nused, 0, new_sz - nused);
24478 +       return p;
24479 +}
24480 +
24481 +/* ---------------------------------------------------------------------- */
24482 +/*
24483 + * aufs caches
24484 + */
24485 +struct kmem_cache *au_cache[AuCache_Last];
24486 +
24487 +static void au_cache_fin(void)
24488 +{
24489 +       int i;
24490 +
24491 +       /*
24492 +        * Make sure all delayed rcu free inodes are flushed before we
24493 +        * destroy cache.
24494 +        */
24495 +       rcu_barrier();
24496 +
24497 +       /* excluding AuCache_HNOTIFY */
24498 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24499 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24500 +               kmem_cache_destroy(au_cache[i]);
24501 +               au_cache[i] = NULL;
24502 +       }
24503 +}
24504 +
24505 +static int __init au_cache_init(void)
24506 +{
24507 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24508 +       if (au_cache[AuCache_DINFO])
24509 +               /* SLAB_DESTROY_BY_RCU */
24510 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24511 +                                                      au_icntnr_init_once);
24512 +       if (au_cache[AuCache_ICNTNR])
24513 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24514 +                                                     au_fi_init_once);
24515 +       if (au_cache[AuCache_FINFO])
24516 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24517 +       if (au_cache[AuCache_VDIR])
24518 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24519 +       if (au_cache[AuCache_DEHSTR])
24520 +               return 0;
24521 +
24522 +       au_cache_fin();
24523 +       return -ENOMEM;
24524 +}
24525 +
24526 +/* ---------------------------------------------------------------------- */
24527 +
24528 +int au_dir_roflags;
24529 +
24530 +#ifdef CONFIG_AUFS_SBILIST
24531 +/*
24532 + * iterate_supers_type() doesn't protect us from
24533 + * remounting (branch management)
24534 + */
24535 +struct hlist_bl_head au_sbilist;
24536 +#endif
24537 +
24538 +/*
24539 + * functions for module interface.
24540 + */
24541 +MODULE_LICENSE("GPL");
24542 +/* MODULE_LICENSE("GPL v2"); */
24543 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24544 +MODULE_DESCRIPTION(AUFS_NAME
24545 +       " -- Advanced multi layered unification filesystem");
24546 +MODULE_VERSION(AUFS_VERSION);
24547 +MODULE_ALIAS_FS(AUFS_NAME);
24548 +
24549 +/* this module parameter has no meaning when SYSFS is disabled */
24550 +int sysaufs_brs = 1;
24551 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24552 +module_param_named(brs, sysaufs_brs, int, 0444);
24553 +
24554 +/* this module parameter has no meaning when USER_NS is disabled */
24555 +bool au_userns;
24556 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24557 +module_param_named(allow_userns, au_userns, bool, 0444);
24558 +
24559 +/* ---------------------------------------------------------------------- */
24560 +
24561 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24562 +
24563 +int au_seq_path(struct seq_file *seq, struct path *path)
24564 +{
24565 +       int err;
24566 +
24567 +       err = seq_path(seq, path, au_esc_chars);
24568 +       if (err >= 0)
24569 +               err = 0;
24570 +       else
24571 +               err = -ENOMEM;
24572 +
24573 +       return err;
24574 +}
24575 +
24576 +/* ---------------------------------------------------------------------- */
24577 +
24578 +static int __init aufs_init(void)
24579 +{
24580 +       int err, i;
24581 +       char *p;
24582 +
24583 +       p = au_esc_chars;
24584 +       for (i = 1; i <= ' '; i++)
24585 +               *p++ = i;
24586 +       *p++ = '\\';
24587 +       *p++ = '\x7f';
24588 +       *p = 0;
24589 +
24590 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24591 +
24592 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24593 +       for (i = 0; i < AuIop_Last; i++)
24594 +               aufs_iop_nogetattr[i].getattr = NULL;
24595 +
24596 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24597 +
24598 +       au_sbilist_init();
24599 +       sysaufs_brs_init();
24600 +       au_debug_init();
24601 +       au_dy_init();
24602 +       err = sysaufs_init();
24603 +       if (unlikely(err))
24604 +               goto out;
24605 +       err = dbgaufs_init();
24606 +       if (unlikely(err))
24607 +               goto out_sysaufs;
24608 +       err = au_procfs_init();
24609 +       if (unlikely(err))
24610 +               goto out_dbgaufs;
24611 +       err = au_wkq_init();
24612 +       if (unlikely(err))
24613 +               goto out_procfs;
24614 +       err = au_loopback_init();
24615 +       if (unlikely(err))
24616 +               goto out_wkq;
24617 +       err = au_hnotify_init();
24618 +       if (unlikely(err))
24619 +               goto out_loopback;
24620 +       err = au_sysrq_init();
24621 +       if (unlikely(err))
24622 +               goto out_hin;
24623 +       err = au_cache_init();
24624 +       if (unlikely(err))
24625 +               goto out_sysrq;
24626 +
24627 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24628 +       err = register_filesystem(&aufs_fs_type);
24629 +       if (unlikely(err))
24630 +               goto out_cache;
24631 +
24632 +       /* since we define pr_fmt, call printk directly */
24633 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24634 +       goto out; /* success */
24635 +
24636 +out_cache:
24637 +       au_cache_fin();
24638 +out_sysrq:
24639 +       au_sysrq_fin();
24640 +out_hin:
24641 +       au_hnotify_fin();
24642 +out_loopback:
24643 +       au_loopback_fin();
24644 +out_wkq:
24645 +       au_wkq_fin();
24646 +out_procfs:
24647 +       au_procfs_fin();
24648 +out_dbgaufs:
24649 +       dbgaufs_fin();
24650 +out_sysaufs:
24651 +       sysaufs_fin();
24652 +       au_dy_fin();
24653 +out:
24654 +       return err;
24655 +}
24656 +
24657 +static void __exit aufs_exit(void)
24658 +{
24659 +       unregister_filesystem(&aufs_fs_type);
24660 +       au_cache_fin();
24661 +       au_sysrq_fin();
24662 +       au_hnotify_fin();
24663 +       au_loopback_fin();
24664 +       au_wkq_fin();
24665 +       au_procfs_fin();
24666 +       dbgaufs_fin();
24667 +       sysaufs_fin();
24668 +       au_dy_fin();
24669 +}
24670 +
24671 +module_init(aufs_init);
24672 +module_exit(aufs_exit);
24673 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24674 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24675 +++ linux/fs/aufs/module.h      2020-01-27 10:57:18.175538316 +0100
24676 @@ -0,0 +1,166 @@
24677 +/* SPDX-License-Identifier: GPL-2.0 */
24678 +/*
24679 + * Copyright (C) 2005-2020 Junjiro R. Okajima
24680 + *
24681 + * This program, aufs is free software; you can redistribute it and/or modify
24682 + * it under the terms of the GNU General Public License as published by
24683 + * the Free Software Foundation; either version 2 of the License, or
24684 + * (at your option) any later version.
24685 + *
24686 + * This program is distributed in the hope that it will be useful,
24687 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24688 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24689 + * GNU General Public License for more details.
24690 + *
24691 + * You should have received a copy of the GNU General Public License
24692 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24693 + */
24694 +
24695 +/*
24696 + * module initialization and module-global
24697 + */
24698 +
24699 +#ifndef __AUFS_MODULE_H__
24700 +#define __AUFS_MODULE_H__
24701 +
24702 +#ifdef __KERNEL__
24703 +
24704 +#include <linux/slab.h>
24705 +#include "debug.h"
24706 +#include "dentry.h"
24707 +#include "dir.h"
24708 +#include "file.h"
24709 +#include "inode.h"
24710 +
24711 +struct path;
24712 +struct seq_file;
24713 +
24714 +/* module parameters */
24715 +extern int sysaufs_brs;
24716 +extern bool au_userns;
24717 +
24718 +/* ---------------------------------------------------------------------- */
24719 +
24720 +extern int au_dir_roflags;
24721 +
24722 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24723 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24724 +                  int may_shrink);
24725 +
24726 +/*
24727 + * Comparing the size of the object with sizeof(struct rcu_head)
24728 + * case 1: object is always larger
24729 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24730 + * case 2: object is always smaller
24731 + *     --> au_kfree_small()
24732 + * case 3: object can be any size
24733 + *     --> au_kfree_try_rcu()
24734 + */
24735 +
24736 +static inline void au_kfree_do_rcu(const void *p)
24737 +{
24738 +       struct {
24739 +               struct rcu_head rcu;
24740 +       } *a = (void *)p;
24741 +
24742 +       kfree_rcu(a, rcu);
24743 +}
24744 +
24745 +#define au_kfree_rcu(_p) do {                                          \
24746 +               typeof(_p) p = (_p);                                    \
24747 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24748 +               if (p)                                                  \
24749 +                       au_kfree_do_rcu(p);                             \
24750 +       } while (0)
24751 +
24752 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24753 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24754 +
24755 +static inline void au_kfree_try_rcu(const void *p)
24756 +{
24757 +       if (!p)
24758 +               return;
24759 +       if (au_kfree_sz_test(p))
24760 +               au_kfree_do_rcu(p);
24761 +       else
24762 +               kfree(p);
24763 +}
24764 +
24765 +static inline void au_kfree_small(const void *p)
24766 +{
24767 +       if (!p)
24768 +               return;
24769 +       AuDebugOn(au_kfree_sz_test(p));
24770 +       kfree(p);
24771 +}
24772 +
24773 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24774 +{
24775 +#ifndef CONFIG_SLOB
24776 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
24777 +#else
24778 +       return -1; /* SLOB is untested */
24779 +#endif
24780 +}
24781 +
24782 +int au_seq_path(struct seq_file *seq, struct path *path);
24783 +
24784 +#ifdef CONFIG_PROC_FS
24785 +/* procfs.c */
24786 +int __init au_procfs_init(void);
24787 +void au_procfs_fin(void);
24788 +#else
24789 +AuStubInt0(au_procfs_init, void);
24790 +AuStubVoid(au_procfs_fin, void);
24791 +#endif
24792 +
24793 +/* ---------------------------------------------------------------------- */
24794 +
24795 +/* kmem cache */
24796 +enum {
24797 +       AuCache_DINFO,
24798 +       AuCache_ICNTNR,
24799 +       AuCache_FINFO,
24800 +       AuCache_VDIR,
24801 +       AuCache_DEHSTR,
24802 +       AuCache_HNOTIFY, /* must be last */
24803 +       AuCache_Last
24804 +};
24805 +
24806 +extern struct kmem_cache *au_cache[AuCache_Last];
24807 +
24808 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24809 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24810 +#define AuCacheCtor(type, ctor)        \
24811 +       kmem_cache_create(#type, sizeof(struct type), \
24812 +                         __alignof__(struct type), AuCacheFlags, ctor)
24813 +
24814 +#define AuCacheFuncs(name, index)                                      \
24815 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24816 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24817 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24818 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24819 +                                                                       \
24820 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24821 +       { void *p = rcu;                                                \
24822 +               p -= offsetof(struct au_##name, rcu);                   \
24823 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24824 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24825 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24826 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24827 +                                                                       \
24828 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24829 +       { /* au_cache_free_##name##_norcu(p); */                        \
24830 +               au_cache_free_##name##_rcu(p); }
24831 +
24832 +AuCacheFuncs(dinfo, DINFO);
24833 +AuCacheFuncs(icntnr, ICNTNR);
24834 +AuCacheFuncs(finfo, FINFO);
24835 +AuCacheFuncs(vdir, VDIR);
24836 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24837 +#ifdef CONFIG_AUFS_HNOTIFY
24838 +AuCacheFuncs(hnotify, HNOTIFY);
24839 +#endif
24840 +
24841 +#endif /* __KERNEL__ */
24842 +#endif /* __AUFS_MODULE_H__ */
24843 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24844 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24845 +++ linux/fs/aufs/mvdown.c      2020-01-27 10:57:18.175538316 +0100
24846 @@ -0,0 +1,706 @@
24847 +// SPDX-License-Identifier: GPL-2.0
24848 +/*
24849 + * Copyright (C) 2011-2020 Junjiro R. Okajima
24850 + *
24851 + * This program, aufs is free software; you can redistribute it and/or modify
24852 + * it under the terms of the GNU General Public License as published by
24853 + * the Free Software Foundation; either version 2 of the License, or
24854 + * (at your option) any later version.
24855 + *
24856 + * This program is distributed in the hope that it will be useful,
24857 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24858 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24859 + * GNU General Public License for more details.
24860 + *
24861 + * You should have received a copy of the GNU General Public License
24862 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24863 + */
24864 +
24865 +/*
24866 + * move-down, opposite of copy-up
24867 + */
24868 +
24869 +#include "aufs.h"
24870 +
24871 +struct au_mvd_args {
24872 +       struct {
24873 +               struct super_block *h_sb;
24874 +               struct dentry *h_parent;
24875 +               struct au_hinode *hdir;
24876 +               struct inode *h_dir, *h_inode;
24877 +               struct au_pin pin;
24878 +       } info[AUFS_MVDOWN_NARRAY];
24879 +
24880 +       struct aufs_mvdown mvdown;
24881 +       struct dentry *dentry, *parent;
24882 +       struct inode *inode, *dir;
24883 +       struct super_block *sb;
24884 +       aufs_bindex_t bopq, bwh, bfound;
24885 +       unsigned char rename_lock;
24886 +};
24887 +
24888 +#define mvd_errno              mvdown.au_errno
24889 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24890 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24891 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24892 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24893 +
24894 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24895 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24896 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24897 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24898 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24899 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24900 +
24901 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24902 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24903 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24904 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24905 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24906 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24907 +
24908 +#define AU_MVD_PR(flag, ...) do {                      \
24909 +               if (flag)                               \
24910 +                       pr_err(__VA_ARGS__);            \
24911 +       } while (0)
24912 +
24913 +static int find_lower_writable(struct au_mvd_args *a)
24914 +{
24915 +       struct super_block *sb;
24916 +       aufs_bindex_t bindex, bbot;
24917 +       struct au_branch *br;
24918 +
24919 +       sb = a->sb;
24920 +       bindex = a->mvd_bsrc;
24921 +       bbot = au_sbbot(sb);
24922 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24923 +               for (bindex++; bindex <= bbot; bindex++) {
24924 +                       br = au_sbr(sb, bindex);
24925 +                       if (au_br_fhsm(br->br_perm)
24926 +                           && !sb_rdonly(au_br_sb(br)))
24927 +                               return bindex;
24928 +               }
24929 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
24930 +               for (bindex++; bindex <= bbot; bindex++) {
24931 +                       br = au_sbr(sb, bindex);
24932 +                       if (!au_br_rdonly(br))
24933 +                               return bindex;
24934 +               }
24935 +       else
24936 +               for (bindex++; bindex <= bbot; bindex++) {
24937 +                       br = au_sbr(sb, bindex);
24938 +                       if (!sb_rdonly(au_br_sb(br))) {
24939 +                               if (au_br_rdonly(br))
24940 +                                       a->mvdown.flags
24941 +                                               |= AUFS_MVDOWN_ROLOWER_R;
24942 +                               return bindex;
24943 +                       }
24944 +               }
24945 +
24946 +       return -1;
24947 +}
24948 +
24949 +/* make the parent dir on bdst */
24950 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
24951 +{
24952 +       int err;
24953 +
24954 +       err = 0;
24955 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
24956 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
24957 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
24958 +       a->mvd_h_dst_parent = NULL;
24959 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
24960 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24961 +       if (!a->mvd_h_dst_parent) {
24962 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
24963 +               if (unlikely(err)) {
24964 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
24965 +                       goto out;
24966 +               }
24967 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
24968 +       }
24969 +
24970 +out:
24971 +       AuTraceErr(err);
24972 +       return err;
24973 +}
24974 +
24975 +/* lock them all */
24976 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
24977 +{
24978 +       int err;
24979 +       struct dentry *h_trap;
24980 +
24981 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
24982 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
24983 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
24984 +                    au_opt_udba(a->sb),
24985 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24986 +       AuTraceErr(err);
24987 +       if (unlikely(err)) {
24988 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
24989 +               goto out;
24990 +       }
24991 +
24992 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
24993 +               a->rename_lock = 0;
24994 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
24995 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
24996 +                           au_opt_udba(a->sb),
24997 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
24998 +               err = au_do_pin(&a->mvd_pin_src);
24999 +               AuTraceErr(err);
25000 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25001 +               if (unlikely(err)) {
25002 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25003 +                       goto out_dst;
25004 +               }
25005 +               goto out; /* success */
25006 +       }
25007 +
25008 +       a->rename_lock = 1;
25009 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25010 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25011 +                    au_opt_udba(a->sb),
25012 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25013 +       AuTraceErr(err);
25014 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25015 +       if (unlikely(err)) {
25016 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25017 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25018 +               goto out_dst;
25019 +       }
25020 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25021 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25022 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25023 +       if (h_trap) {
25024 +               err = (h_trap != a->mvd_h_src_parent);
25025 +               if (err)
25026 +                       err = (h_trap != a->mvd_h_dst_parent);
25027 +       }
25028 +       BUG_ON(err); /* it should never happen */
25029 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25030 +               err = -EBUSY;
25031 +               AuTraceErr(err);
25032 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25033 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25034 +               au_pin_hdir_lock(&a->mvd_pin_src);
25035 +               au_unpin(&a->mvd_pin_src);
25036 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25037 +               goto out_dst;
25038 +       }
25039 +       goto out; /* success */
25040 +
25041 +out_dst:
25042 +       au_unpin(&a->mvd_pin_dst);
25043 +out:
25044 +       AuTraceErr(err);
25045 +       return err;
25046 +}
25047 +
25048 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25049 +{
25050 +       if (!a->rename_lock)
25051 +               au_unpin(&a->mvd_pin_src);
25052 +       else {
25053 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25054 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25055 +               au_pin_hdir_lock(&a->mvd_pin_src);
25056 +               au_unpin(&a->mvd_pin_src);
25057 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25058 +       }
25059 +       au_unpin(&a->mvd_pin_dst);
25060 +}
25061 +
25062 +/* copy-down the file */
25063 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25064 +{
25065 +       int err;
25066 +       struct au_cp_generic cpg = {
25067 +               .dentry = a->dentry,
25068 +               .bdst   = a->mvd_bdst,
25069 +               .bsrc   = a->mvd_bsrc,
25070 +               .len    = -1,
25071 +               .pin    = &a->mvd_pin_dst,
25072 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25073 +       };
25074 +
25075 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25076 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25077 +               au_fset_cpup(cpg.flags, OVERWRITE);
25078 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25079 +               au_fset_cpup(cpg.flags, RWDST);
25080 +       err = au_sio_cpdown_simple(&cpg);
25081 +       if (unlikely(err))
25082 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25083 +
25084 +       AuTraceErr(err);
25085 +       return err;
25086 +}
25087 +
25088 +/*
25089 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25090 + * were sleeping
25091 + */
25092 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25093 +{
25094 +       int err;
25095 +       struct path h_path;
25096 +       struct au_branch *br;
25097 +       struct inode *delegated;
25098 +
25099 +       br = au_sbr(a->sb, a->mvd_bdst);
25100 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25101 +       err = PTR_ERR(h_path.dentry);
25102 +       if (IS_ERR(h_path.dentry)) {
25103 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25104 +               goto out;
25105 +       }
25106 +
25107 +       err = 0;
25108 +       if (d_is_positive(h_path.dentry)) {
25109 +               h_path.mnt = au_br_mnt(br);
25110 +               delegated = NULL;
25111 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25112 +                                  &delegated, /*force*/0);
25113 +               if (unlikely(err == -EWOULDBLOCK)) {
25114 +                       pr_warn("cannot retry for NFSv4 delegation"
25115 +                               " for an internal unlink\n");
25116 +                       iput(delegated);
25117 +               }
25118 +               if (unlikely(err))
25119 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25120 +       }
25121 +       dput(h_path.dentry);
25122 +
25123 +out:
25124 +       AuTraceErr(err);
25125 +       return err;
25126 +}
25127 +
25128 +/*
25129 + * unlink the topmost h_dentry
25130 + */
25131 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25132 +{
25133 +       int err;
25134 +       struct path h_path;
25135 +       struct inode *delegated;
25136 +
25137 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25138 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25139 +       delegated = NULL;
25140 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25141 +       if (unlikely(err == -EWOULDBLOCK)) {
25142 +               pr_warn("cannot retry for NFSv4 delegation"
25143 +                       " for an internal unlink\n");
25144 +               iput(delegated);
25145 +       }
25146 +       if (unlikely(err))
25147 +               AU_MVD_PR(dmsg, "unlink failed\n");
25148 +
25149 +       AuTraceErr(err);
25150 +       return err;
25151 +}
25152 +
25153 +/* Since mvdown succeeded, we ignore an error of this function */
25154 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25155 +{
25156 +       int err;
25157 +       struct au_branch *br;
25158 +
25159 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25160 +       br = au_sbr(a->sb, a->mvd_bsrc);
25161 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25162 +       if (!err) {
25163 +               br = au_sbr(a->sb, a->mvd_bdst);
25164 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25165 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25166 +       }
25167 +       if (!err)
25168 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25169 +       else
25170 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25171 +}
25172 +
25173 +/*
25174 + * copy-down the file and unlink the bsrc file.
25175 + * - unlink the bdst whout if exist
25176 + * - copy-down the file (with whtmp name and rename)
25177 + * - unlink the bsrc file
25178 + */
25179 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25180 +{
25181 +       int err;
25182 +
25183 +       err = au_do_mkdir(dmsg, a);
25184 +       if (!err)
25185 +               err = au_do_lock(dmsg, a);
25186 +       if (unlikely(err))
25187 +               goto out;
25188 +
25189 +       /*
25190 +        * do not revert the activities we made on bdst since they should be
25191 +        * harmless in aufs.
25192 +        */
25193 +
25194 +       err = au_do_cpdown(dmsg, a);
25195 +       if (!err)
25196 +               err = au_do_unlink_wh(dmsg, a);
25197 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25198 +               err = au_do_unlink(dmsg, a);
25199 +       if (unlikely(err))
25200 +               goto out_unlock;
25201 +
25202 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25203 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25204 +       if (find_lower_writable(a) < 0)
25205 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25206 +
25207 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25208 +               au_do_stfs(dmsg, a);
25209 +
25210 +       /* maintain internal array */
25211 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25212 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25213 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25214 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25215 +               au_set_ibtop(a->inode, a->mvd_bdst);
25216 +       } else {
25217 +               /* hide the lower */
25218 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25219 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25220 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25221 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25222 +       }
25223 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25224 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25225 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25226 +               au_set_ibbot(a->inode, a->mvd_bdst);
25227 +
25228 +out_unlock:
25229 +       au_do_unlock(dmsg, a);
25230 +out:
25231 +       AuTraceErr(err);
25232 +       return err;
25233 +}
25234 +
25235 +/* ---------------------------------------------------------------------- */
25236 +
25237 +/* make sure the file is idle */
25238 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25239 +{
25240 +       int err, plinked;
25241 +
25242 +       err = 0;
25243 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25244 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25245 +           && au_dcount(a->dentry) == 1
25246 +           && atomic_read(&a->inode->i_count) == 1
25247 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25248 +           && (!plinked || !au_plink_test(a->inode))
25249 +           && a->inode->i_nlink == 1)
25250 +               goto out;
25251 +
25252 +       err = -EBUSY;
25253 +       AU_MVD_PR(dmsg,
25254 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25255 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25256 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25257 +                 a->mvd_h_src_inode->i_nlink,
25258 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25259 +
25260 +out:
25261 +       AuTraceErr(err);
25262 +       return err;
25263 +}
25264 +
25265 +/* make sure the parent dir is fine */
25266 +static int au_mvd_args_parent(const unsigned char dmsg,
25267 +                             struct au_mvd_args *a)
25268 +{
25269 +       int err;
25270 +       aufs_bindex_t bindex;
25271 +
25272 +       err = 0;
25273 +       if (unlikely(au_alive_dir(a->parent))) {
25274 +               err = -ENOENT;
25275 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25276 +               goto out;
25277 +       }
25278 +
25279 +       a->bopq = au_dbdiropq(a->parent);
25280 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25281 +       AuDbg("b%d\n", bindex);
25282 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25283 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25284 +               err = -EINVAL;
25285 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25286 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25287 +                         a->bopq, a->mvd_bdst);
25288 +       }
25289 +
25290 +out:
25291 +       AuTraceErr(err);
25292 +       return err;
25293 +}
25294 +
25295 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25296 +                                   struct au_mvd_args *a)
25297 +{
25298 +       int err;
25299 +       struct au_dinfo *dinfo, *tmp;
25300 +
25301 +       /* lookup the next lower positive entry */
25302 +       err = -ENOMEM;
25303 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25304 +       if (unlikely(!tmp))
25305 +               goto out;
25306 +
25307 +       a->bfound = -1;
25308 +       a->bwh = -1;
25309 +       dinfo = au_di(a->dentry);
25310 +       au_di_cp(tmp, dinfo);
25311 +       au_di_swap(tmp, dinfo);
25312 +
25313 +       /* returns the number of positive dentries */
25314 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25315 +                            /* AuLkup_IGNORE_PERM */ 0);
25316 +       if (!err)
25317 +               a->bwh = au_dbwh(a->dentry);
25318 +       else if (err > 0)
25319 +               a->bfound = au_dbtop(a->dentry);
25320 +
25321 +       au_di_swap(tmp, dinfo);
25322 +       au_rw_write_unlock(&tmp->di_rwsem);
25323 +       au_di_free(tmp);
25324 +       if (unlikely(err < 0))
25325 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25326 +
25327 +       /*
25328 +        * here, we have these cases.
25329 +        * bfound == -1
25330 +        *      no positive dentry under bsrc. there are more sub-cases.
25331 +        *      bwh < 0
25332 +        *              there no whiteout, we can safely move-down.
25333 +        *      bwh <= bsrc
25334 +        *              impossible
25335 +        *      bsrc < bwh && bwh < bdst
25336 +        *              there is a whiteout on RO branch. cannot proceed.
25337 +        *      bwh == bdst
25338 +        *              there is a whiteout on the RW target branch. it should
25339 +        *              be removed.
25340 +        *      bdst < bwh
25341 +        *              there is a whiteout somewhere unrelated branch.
25342 +        * -1 < bfound && bfound <= bsrc
25343 +        *      impossible.
25344 +        * bfound < bdst
25345 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25346 +        *      proceed.
25347 +        * bfound == bdst
25348 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25349 +        *      error.
25350 +        * bdst < bfound
25351 +        *      found, after we create the file on bdst, it will be hidden.
25352 +        */
25353 +
25354 +       AuDebugOn(a->bfound == -1
25355 +                 && a->bwh != -1
25356 +                 && a->bwh <= a->mvd_bsrc);
25357 +       AuDebugOn(-1 < a->bfound
25358 +                 && a->bfound <= a->mvd_bsrc);
25359 +
25360 +       err = -EINVAL;
25361 +       if (a->bfound == -1
25362 +           && a->mvd_bsrc < a->bwh
25363 +           && a->bwh != -1
25364 +           && a->bwh < a->mvd_bdst) {
25365 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25366 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25367 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25368 +               goto out;
25369 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25370 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25371 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25372 +                         a->mvd_bdst, a->bfound);
25373 +               goto out;
25374 +       }
25375 +
25376 +       err = 0; /* success */
25377 +
25378 +out:
25379 +       AuTraceErr(err);
25380 +       return err;
25381 +}
25382 +
25383 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25384 +{
25385 +       int err;
25386 +
25387 +       err = 0;
25388 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25389 +           && a->bfound == a->mvd_bdst)
25390 +               err = -EEXIST;
25391 +       AuTraceErr(err);
25392 +       return err;
25393 +}
25394 +
25395 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25396 +{
25397 +       int err;
25398 +       struct au_branch *br;
25399 +
25400 +       err = -EISDIR;
25401 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25402 +               goto out;
25403 +
25404 +       err = -EINVAL;
25405 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25406 +               a->mvd_bsrc = au_ibtop(a->inode);
25407 +       else {
25408 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25409 +               if (unlikely(a->mvd_bsrc < 0
25410 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25411 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25412 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25413 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25414 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25415 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25416 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25417 +                       AU_MVD_PR(dmsg, "no upper\n");
25418 +                       goto out;
25419 +               }
25420 +       }
25421 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25422 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25423 +               AU_MVD_PR(dmsg, "on the bottom\n");
25424 +               goto out;
25425 +       }
25426 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25427 +       br = au_sbr(a->sb, a->mvd_bsrc);
25428 +       err = au_br_rdonly(br);
25429 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25430 +               if (unlikely(err))
25431 +                       goto out;
25432 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25433 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25434 +               if (err)
25435 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25436 +               /* go on */
25437 +       } else
25438 +               goto out;
25439 +
25440 +       err = -EINVAL;
25441 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25442 +               a->mvd_bdst = find_lower_writable(a);
25443 +               if (unlikely(a->mvd_bdst < 0)) {
25444 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25445 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25446 +                       goto out;
25447 +               }
25448 +       } else {
25449 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25450 +               if (unlikely(a->mvd_bdst < 0
25451 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25452 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25453 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25454 +                       goto out;
25455 +               }
25456 +       }
25457 +
25458 +       err = au_mvd_args_busy(dmsg, a);
25459 +       if (!err)
25460 +               err = au_mvd_args_parent(dmsg, a);
25461 +       if (!err)
25462 +               err = au_mvd_args_intermediate(dmsg, a);
25463 +       if (!err)
25464 +               err = au_mvd_args_exist(dmsg, a);
25465 +       if (!err)
25466 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25467 +
25468 +out:
25469 +       AuTraceErr(err);
25470 +       return err;
25471 +}
25472 +
25473 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25474 +{
25475 +       int err, e;
25476 +       unsigned char dmsg;
25477 +       struct au_mvd_args *args;
25478 +       struct inode *inode;
25479 +
25480 +       inode = d_inode(dentry);
25481 +       err = -EPERM;
25482 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25483 +               goto out;
25484 +
25485 +       err = -ENOMEM;
25486 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25487 +       if (unlikely(!args))
25488 +               goto out;
25489 +
25490 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25491 +       if (!err)
25492 +               /* VERIFY_WRITE */
25493 +               err = !access_ok(uarg, sizeof(*uarg));
25494 +       if (unlikely(err)) {
25495 +               err = -EFAULT;
25496 +               AuTraceErr(err);
25497 +               goto out_free;
25498 +       }
25499 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25500 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25501 +       args->mvdown.au_errno = 0;
25502 +       args->dentry = dentry;
25503 +       args->inode = inode;
25504 +       args->sb = dentry->d_sb;
25505 +
25506 +       err = -ENOENT;
25507 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25508 +       args->parent = dget_parent(dentry);
25509 +       args->dir = d_inode(args->parent);
25510 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25511 +       dput(args->parent);
25512 +       if (unlikely(args->parent != dentry->d_parent)) {
25513 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25514 +               goto out_dir;
25515 +       }
25516 +
25517 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25518 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25519 +       if (unlikely(err))
25520 +               goto out_inode;
25521 +
25522 +       di_write_lock_parent(args->parent);
25523 +       err = au_mvd_args(dmsg, args);
25524 +       if (unlikely(err))
25525 +               goto out_parent;
25526 +
25527 +       err = au_do_mvdown(dmsg, args);
25528 +       if (unlikely(err))
25529 +               goto out_parent;
25530 +
25531 +       au_cpup_attr_timesizes(args->dir);
25532 +       au_cpup_attr_timesizes(inode);
25533 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25534 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25535 +       /* au_digen_dec(dentry); */
25536 +
25537 +out_parent:
25538 +       di_write_unlock(args->parent);
25539 +       aufs_read_unlock(dentry, AuLock_DW);
25540 +out_inode:
25541 +       inode_unlock(inode);
25542 +out_dir:
25543 +       inode_unlock(args->dir);
25544 +out_free:
25545 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25546 +       if (unlikely(e))
25547 +               err = -EFAULT;
25548 +       au_kfree_rcu(args);
25549 +out:
25550 +       AuTraceErr(err);
25551 +       return err;
25552 +}
25553 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25554 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25555 +++ linux/fs/aufs/opts.c        2020-01-27 10:57:18.175538316 +0100
25556 @@ -0,0 +1,1880 @@
25557 +// SPDX-License-Identifier: GPL-2.0
25558 +/*
25559 + * Copyright (C) 2005-2020 Junjiro R. Okajima
25560 + *
25561 + * This program, aufs is free software; you can redistribute it and/or modify
25562 + * it under the terms of the GNU General Public License as published by
25563 + * the Free Software Foundation; either version 2 of the License, or
25564 + * (at your option) any later version.
25565 + *
25566 + * This program is distributed in the hope that it will be useful,
25567 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25568 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25569 + * GNU General Public License for more details.
25570 + *
25571 + * You should have received a copy of the GNU General Public License
25572 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25573 + */
25574 +
25575 +/*
25576 + * mount options/flags
25577 + */
25578 +
25579 +#include <linux/namei.h>
25580 +#include <linux/types.h> /* a distribution requires */
25581 +#include <linux/parser.h>
25582 +#include "aufs.h"
25583 +
25584 +/* ---------------------------------------------------------------------- */
25585 +
25586 +enum {
25587 +       Opt_br,
25588 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25589 +       Opt_idel, Opt_imod,
25590 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25591 +       Opt_rdblk_def, Opt_rdhash_def,
25592 +       Opt_xino, Opt_noxino,
25593 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25594 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25595 +       Opt_trunc_xib, Opt_notrunc_xib,
25596 +       Opt_shwh, Opt_noshwh,
25597 +       Opt_plink, Opt_noplink, Opt_list_plink,
25598 +       Opt_udba,
25599 +       Opt_dio, Opt_nodio,
25600 +       Opt_diropq_a, Opt_diropq_w,
25601 +       Opt_warn_perm, Opt_nowarn_perm,
25602 +       Opt_wbr_copyup, Opt_wbr_create,
25603 +       Opt_fhsm_sec,
25604 +       Opt_verbose, Opt_noverbose,
25605 +       Opt_sum, Opt_nosum, Opt_wsum,
25606 +       Opt_dirperm1, Opt_nodirperm1,
25607 +       Opt_dirren, Opt_nodirren,
25608 +       Opt_acl, Opt_noacl,
25609 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25610 +};
25611 +
25612 +static match_table_t options = {
25613 +       {Opt_br, "br=%s"},
25614 +       {Opt_br, "br:%s"},
25615 +
25616 +       {Opt_add, "add=%d:%s"},
25617 +       {Opt_add, "add:%d:%s"},
25618 +       {Opt_add, "ins=%d:%s"},
25619 +       {Opt_add, "ins:%d:%s"},
25620 +       {Opt_append, "append=%s"},
25621 +       {Opt_append, "append:%s"},
25622 +       {Opt_prepend, "prepend=%s"},
25623 +       {Opt_prepend, "prepend:%s"},
25624 +
25625 +       {Opt_del, "del=%s"},
25626 +       {Opt_del, "del:%s"},
25627 +       /* {Opt_idel, "idel:%d"}, */
25628 +       {Opt_mod, "mod=%s"},
25629 +       {Opt_mod, "mod:%s"},
25630 +       /* {Opt_imod, "imod:%d:%s"}, */
25631 +
25632 +       {Opt_dirwh, "dirwh=%d"},
25633 +
25634 +       {Opt_xino, "xino=%s"},
25635 +       {Opt_noxino, "noxino"},
25636 +       {Opt_trunc_xino, "trunc_xino"},
25637 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25638 +       {Opt_notrunc_xino, "notrunc_xino"},
25639 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25640 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25641 +       /* {Opt_zxino, "zxino=%s"}, */
25642 +       {Opt_trunc_xib, "trunc_xib"},
25643 +       {Opt_notrunc_xib, "notrunc_xib"},
25644 +
25645 +#ifdef CONFIG_PROC_FS
25646 +       {Opt_plink, "plink"},
25647 +#else
25648 +       {Opt_ignore_silent, "plink"},
25649 +#endif
25650 +
25651 +       {Opt_noplink, "noplink"},
25652 +
25653 +#ifdef CONFIG_AUFS_DEBUG
25654 +       {Opt_list_plink, "list_plink"},
25655 +#endif
25656 +
25657 +       {Opt_udba, "udba=%s"},
25658 +
25659 +       {Opt_dio, "dio"},
25660 +       {Opt_nodio, "nodio"},
25661 +
25662 +#ifdef CONFIG_AUFS_DIRREN
25663 +       {Opt_dirren, "dirren"},
25664 +       {Opt_nodirren, "nodirren"},
25665 +#else
25666 +       {Opt_ignore, "dirren"},
25667 +       {Opt_ignore_silent, "nodirren"},
25668 +#endif
25669 +
25670 +#ifdef CONFIG_AUFS_FHSM
25671 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25672 +#else
25673 +       {Opt_ignore, "fhsm_sec=%d"},
25674 +#endif
25675 +
25676 +       {Opt_diropq_a, "diropq=always"},
25677 +       {Opt_diropq_a, "diropq=a"},
25678 +       {Opt_diropq_w, "diropq=whiteouted"},
25679 +       {Opt_diropq_w, "diropq=w"},
25680 +
25681 +       {Opt_warn_perm, "warn_perm"},
25682 +       {Opt_nowarn_perm, "nowarn_perm"},
25683 +
25684 +       /* keep them temporary */
25685 +       {Opt_ignore_silent, "nodlgt"},
25686 +       {Opt_ignore, "clean_plink"},
25687 +
25688 +#ifdef CONFIG_AUFS_SHWH
25689 +       {Opt_shwh, "shwh"},
25690 +#endif
25691 +       {Opt_noshwh, "noshwh"},
25692 +
25693 +       {Opt_dirperm1, "dirperm1"},
25694 +       {Opt_nodirperm1, "nodirperm1"},
25695 +
25696 +       {Opt_verbose, "verbose"},
25697 +       {Opt_verbose, "v"},
25698 +       {Opt_noverbose, "noverbose"},
25699 +       {Opt_noverbose, "quiet"},
25700 +       {Opt_noverbose, "q"},
25701 +       {Opt_noverbose, "silent"},
25702 +
25703 +       {Opt_sum, "sum"},
25704 +       {Opt_nosum, "nosum"},
25705 +       {Opt_wsum, "wsum"},
25706 +
25707 +       {Opt_rdcache, "rdcache=%d"},
25708 +       {Opt_rdblk, "rdblk=%d"},
25709 +       {Opt_rdblk_def, "rdblk=def"},
25710 +       {Opt_rdhash, "rdhash=%d"},
25711 +       {Opt_rdhash_def, "rdhash=def"},
25712 +
25713 +       {Opt_wbr_create, "create=%s"},
25714 +       {Opt_wbr_create, "create_policy=%s"},
25715 +       {Opt_wbr_copyup, "cpup=%s"},
25716 +       {Opt_wbr_copyup, "copyup=%s"},
25717 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25718 +
25719 +       /* generic VFS flag */
25720 +#ifdef CONFIG_FS_POSIX_ACL
25721 +       {Opt_acl, "acl"},
25722 +       {Opt_noacl, "noacl"},
25723 +#else
25724 +       {Opt_ignore, "acl"},
25725 +       {Opt_ignore_silent, "noacl"},
25726 +#endif
25727 +
25728 +       /* internal use for the scripts */
25729 +       {Opt_ignore_silent, "si=%s"},
25730 +
25731 +       {Opt_br, "dirs=%s"},
25732 +       {Opt_ignore, "debug=%d"},
25733 +       {Opt_ignore, "delete=whiteout"},
25734 +       {Opt_ignore, "delete=all"},
25735 +       {Opt_ignore, "imap=%s"},
25736 +
25737 +       /* temporary workaround, due to old mount(8)? */
25738 +       {Opt_ignore_silent, "relatime"},
25739 +
25740 +       {Opt_err, NULL}
25741 +};
25742 +
25743 +/* ---------------------------------------------------------------------- */
25744 +
25745 +static const char *au_parser_pattern(int val, match_table_t tbl)
25746 +{
25747 +       struct match_token *p;
25748 +
25749 +       p = tbl;
25750 +       while (p->pattern) {
25751 +               if (p->token == val)
25752 +                       return p->pattern;
25753 +               p++;
25754 +       }
25755 +       BUG();
25756 +       return "??";
25757 +}
25758 +
25759 +static const char *au_optstr(int *val, match_table_t tbl)
25760 +{
25761 +       struct match_token *p;
25762 +       int v;
25763 +
25764 +       v = *val;
25765 +       if (!v)
25766 +               goto out;
25767 +       p = tbl;
25768 +       while (p->pattern) {
25769 +               if (p->token
25770 +                   && (v & p->token) == p->token) {
25771 +                       *val &= ~p->token;
25772 +                       return p->pattern;
25773 +               }
25774 +               p++;
25775 +       }
25776 +
25777 +out:
25778 +       return NULL;
25779 +}
25780 +
25781 +/* ---------------------------------------------------------------------- */
25782 +
25783 +static match_table_t brperm = {
25784 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25785 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25786 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25787 +       {0, NULL}
25788 +};
25789 +
25790 +static match_table_t brattr = {
25791 +       /* general */
25792 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25793 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25794 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25795 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25796 +#ifdef CONFIG_AUFS_FHSM
25797 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25798 +#endif
25799 +#ifdef CONFIG_AUFS_XATTR
25800 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25801 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25802 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25803 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25804 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25805 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25806 +#endif
25807 +
25808 +       /* ro/rr branch */
25809 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25810 +
25811 +       /* rw branch */
25812 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25813 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25814 +
25815 +       {0, NULL}
25816 +};
25817 +
25818 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25819 +{
25820 +       int attr, v;
25821 +       char *p;
25822 +
25823 +       attr = 0;
25824 +       do {
25825 +               p = strchr(str, '+');
25826 +               if (p)
25827 +                       *p = 0;
25828 +               v = match_token(str, table, args);
25829 +               if (v) {
25830 +                       if (v & AuBrAttr_CMOO_Mask)
25831 +                               attr &= ~AuBrAttr_CMOO_Mask;
25832 +                       attr |= v;
25833 +               } else {
25834 +                       if (p)
25835 +                               *p = '+';
25836 +                       pr_warn("ignored branch attribute %s\n", str);
25837 +                       break;
25838 +               }
25839 +               if (p)
25840 +                       str = p + 1;
25841 +       } while (p);
25842 +
25843 +       return attr;
25844 +}
25845 +
25846 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25847 +{
25848 +       int sz;
25849 +       const char *p;
25850 +       char *q;
25851 +
25852 +       q = str->a;
25853 +       *q = 0;
25854 +       p = au_optstr(&perm, brattr);
25855 +       if (p) {
25856 +               sz = strlen(p);
25857 +               memcpy(q, p, sz + 1);
25858 +               q += sz;
25859 +       } else
25860 +               goto out;
25861 +
25862 +       do {
25863 +               p = au_optstr(&perm, brattr);
25864 +               if (p) {
25865 +                       *q++ = '+';
25866 +                       sz = strlen(p);
25867 +                       memcpy(q, p, sz + 1);
25868 +                       q += sz;
25869 +               }
25870 +       } while (p);
25871 +
25872 +out:
25873 +       return q - str->a;
25874 +}
25875 +
25876 +static int noinline_for_stack br_perm_val(char *perm)
25877 +{
25878 +       int val, bad, sz;
25879 +       char *p;
25880 +       substring_t args[MAX_OPT_ARGS];
25881 +       au_br_perm_str_t attr;
25882 +
25883 +       p = strchr(perm, '+');
25884 +       if (p)
25885 +               *p = 0;
25886 +       val = match_token(perm, brperm, args);
25887 +       if (!val) {
25888 +               if (p)
25889 +                       *p = '+';
25890 +               pr_warn("ignored branch permission %s\n", perm);
25891 +               val = AuBrPerm_RO;
25892 +               goto out;
25893 +       }
25894 +       if (!p)
25895 +               goto out;
25896 +
25897 +       val |= br_attr_val(p + 1, brattr, args);
25898 +
25899 +       bad = 0;
25900 +       switch (val & AuBrPerm_Mask) {
25901 +       case AuBrPerm_RO:
25902 +       case AuBrPerm_RR:
25903 +               bad = val & AuBrWAttr_Mask;
25904 +               val &= ~AuBrWAttr_Mask;
25905 +               break;
25906 +       case AuBrPerm_RW:
25907 +               bad = val & AuBrRAttr_Mask;
25908 +               val &= ~AuBrRAttr_Mask;
25909 +               break;
25910 +       }
25911 +
25912 +       /*
25913 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25914 +        * does not treat it as an error, just warning.
25915 +        * this is a tiny guard for the user operation.
25916 +        */
25917 +       if (val & AuBrAttr_UNPIN) {
25918 +               bad |= AuBrAttr_UNPIN;
25919 +               val &= ~AuBrAttr_UNPIN;
25920 +       }
25921 +
25922 +       if (unlikely(bad)) {
25923 +               sz = au_do_optstr_br_attr(&attr, bad);
25924 +               AuDebugOn(!sz);
25925 +               pr_warn("ignored branch attribute %s\n", attr.a);
25926 +       }
25927 +
25928 +out:
25929 +       return val;
25930 +}
25931 +
25932 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
25933 +{
25934 +       au_br_perm_str_t attr;
25935 +       const char *p;
25936 +       char *q;
25937 +       int sz;
25938 +
25939 +       q = str->a;
25940 +       p = au_optstr(&perm, brperm);
25941 +       AuDebugOn(!p || !*p);
25942 +       sz = strlen(p);
25943 +       memcpy(q, p, sz + 1);
25944 +       q += sz;
25945 +
25946 +       sz = au_do_optstr_br_attr(&attr, perm);
25947 +       if (sz) {
25948 +               *q++ = '+';
25949 +               memcpy(q, attr.a, sz + 1);
25950 +       }
25951 +
25952 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
25953 +}
25954 +
25955 +/* ---------------------------------------------------------------------- */
25956 +
25957 +static match_table_t udbalevel = {
25958 +       {AuOpt_UDBA_REVAL, "reval"},
25959 +       {AuOpt_UDBA_NONE, "none"},
25960 +#ifdef CONFIG_AUFS_HNOTIFY
25961 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
25962 +#ifdef CONFIG_AUFS_HFSNOTIFY
25963 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
25964 +#endif
25965 +#endif
25966 +       {-1, NULL}
25967 +};
25968 +
25969 +static int noinline_for_stack udba_val(char *str)
25970 +{
25971 +       substring_t args[MAX_OPT_ARGS];
25972 +
25973 +       return match_token(str, udbalevel, args);
25974 +}
25975 +
25976 +const char *au_optstr_udba(int udba)
25977 +{
25978 +       return au_parser_pattern(udba, udbalevel);
25979 +}
25980 +
25981 +/* ---------------------------------------------------------------------- */
25982 +
25983 +static match_table_t au_wbr_create_policy = {
25984 +       {AuWbrCreate_TDP, "tdp"},
25985 +       {AuWbrCreate_TDP, "top-down-parent"},
25986 +       {AuWbrCreate_RR, "rr"},
25987 +       {AuWbrCreate_RR, "round-robin"},
25988 +       {AuWbrCreate_MFS, "mfs"},
25989 +       {AuWbrCreate_MFS, "most-free-space"},
25990 +       {AuWbrCreate_MFSV, "mfs:%d"},
25991 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
25992 +
25993 +       /* top-down regardless the parent, and then mfs */
25994 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
25995 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
25996 +
25997 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
25998 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
25999 +       {AuWbrCreate_PMFS, "pmfs"},
26000 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26001 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26002 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26003 +
26004 +       {-1, NULL}
26005 +};
26006 +
26007 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26008 +                           struct au_opt_wbr_create *create)
26009 +{
26010 +       int err;
26011 +       unsigned long long ull;
26012 +
26013 +       err = 0;
26014 +       if (!match_u64(arg, &ull))
26015 +               create->mfsrr_watermark = ull;
26016 +       else {
26017 +               pr_err("bad integer in %s\n", str);
26018 +               err = -EINVAL;
26019 +       }
26020 +
26021 +       return err;
26022 +}
26023 +
26024 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26025 +                         struct au_opt_wbr_create *create)
26026 +{
26027 +       int n, err;
26028 +
26029 +       err = 0;
26030 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26031 +               create->mfs_second = n;
26032 +       else {
26033 +               pr_err("bad integer in %s\n", str);
26034 +               err = -EINVAL;
26035 +       }
26036 +
26037 +       return err;
26038 +}
26039 +
26040 +static int noinline_for_stack
26041 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26042 +{
26043 +       int err, e;
26044 +       substring_t args[MAX_OPT_ARGS];
26045 +
26046 +       err = match_token(str, au_wbr_create_policy, args);
26047 +       create->wbr_create = err;
26048 +       switch (err) {
26049 +       case AuWbrCreate_MFSRRV:
26050 +       case AuWbrCreate_TDMFSV:
26051 +       case AuWbrCreate_PMFSRRV:
26052 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26053 +               if (!e)
26054 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26055 +               if (unlikely(e))
26056 +                       err = e;
26057 +               break;
26058 +       case AuWbrCreate_MFSRR:
26059 +       case AuWbrCreate_TDMFS:
26060 +       case AuWbrCreate_PMFSRR:
26061 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26062 +               if (unlikely(e)) {
26063 +                       err = e;
26064 +                       break;
26065 +               }
26066 +               /*FALLTHROUGH*/
26067 +       case AuWbrCreate_MFS:
26068 +       case AuWbrCreate_PMFS:
26069 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26070 +               break;
26071 +       case AuWbrCreate_MFSV:
26072 +       case AuWbrCreate_PMFSV:
26073 +               e = au_wbr_mfs_sec(&args[0], str, create);
26074 +               if (unlikely(e))
26075 +                       err = e;
26076 +               break;
26077 +       }
26078 +
26079 +       return err;
26080 +}
26081 +
26082 +const char *au_optstr_wbr_create(int wbr_create)
26083 +{
26084 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26085 +}
26086 +
26087 +static match_table_t au_wbr_copyup_policy = {
26088 +       {AuWbrCopyup_TDP, "tdp"},
26089 +       {AuWbrCopyup_TDP, "top-down-parent"},
26090 +       {AuWbrCopyup_BUP, "bup"},
26091 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26092 +       {AuWbrCopyup_BU, "bu"},
26093 +       {AuWbrCopyup_BU, "bottom-up"},
26094 +       {-1, NULL}
26095 +};
26096 +
26097 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26098 +{
26099 +       substring_t args[MAX_OPT_ARGS];
26100 +
26101 +       return match_token(str, au_wbr_copyup_policy, args);
26102 +}
26103 +
26104 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26105 +{
26106 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26107 +}
26108 +
26109 +/* ---------------------------------------------------------------------- */
26110 +
26111 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26112 +
26113 +static void dump_opts(struct au_opts *opts)
26114 +{
26115 +#ifdef CONFIG_AUFS_DEBUG
26116 +       /* reduce stack space */
26117 +       union {
26118 +               struct au_opt_add *add;
26119 +               struct au_opt_del *del;
26120 +               struct au_opt_mod *mod;
26121 +               struct au_opt_xino *xino;
26122 +               struct au_opt_xino_itrunc *xino_itrunc;
26123 +               struct au_opt_wbr_create *create;
26124 +       } u;
26125 +       struct au_opt *opt;
26126 +
26127 +       opt = opts->opt;
26128 +       while (opt->type != Opt_tail) {
26129 +               switch (opt->type) {
26130 +               case Opt_add:
26131 +                       u.add = &opt->add;
26132 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26133 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26134 +                                 u.add->path.dentry);
26135 +                       break;
26136 +               case Opt_del:
26137 +               case Opt_idel:
26138 +                       u.del = &opt->del;
26139 +                       AuDbg("del {%s, %p}\n",
26140 +                             u.del->pathname, u.del->h_path.dentry);
26141 +                       break;
26142 +               case Opt_mod:
26143 +               case Opt_imod:
26144 +                       u.mod = &opt->mod;
26145 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26146 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26147 +                       break;
26148 +               case Opt_append:
26149 +                       u.add = &opt->add;
26150 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26151 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26152 +                                 u.add->path.dentry);
26153 +                       break;
26154 +               case Opt_prepend:
26155 +                       u.add = &opt->add;
26156 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26157 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26158 +                                 u.add->path.dentry);
26159 +                       break;
26160 +               case Opt_dirwh:
26161 +                       AuDbg("dirwh %d\n", opt->dirwh);
26162 +                       break;
26163 +               case Opt_rdcache:
26164 +                       AuDbg("rdcache %d\n", opt->rdcache);
26165 +                       break;
26166 +               case Opt_rdblk:
26167 +                       AuDbg("rdblk %u\n", opt->rdblk);
26168 +                       break;
26169 +               case Opt_rdblk_def:
26170 +                       AuDbg("rdblk_def\n");
26171 +                       break;
26172 +               case Opt_rdhash:
26173 +                       AuDbg("rdhash %u\n", opt->rdhash);
26174 +                       break;
26175 +               case Opt_rdhash_def:
26176 +                       AuDbg("rdhash_def\n");
26177 +                       break;
26178 +               case Opt_xino:
26179 +                       u.xino = &opt->xino;
26180 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26181 +                       break;
26182 +               case Opt_trunc_xino:
26183 +                       AuLabel(trunc_xino);
26184 +                       break;
26185 +               case Opt_notrunc_xino:
26186 +                       AuLabel(notrunc_xino);
26187 +                       break;
26188 +               case Opt_trunc_xino_path:
26189 +               case Opt_itrunc_xino:
26190 +                       u.xino_itrunc = &opt->xino_itrunc;
26191 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26192 +                       break;
26193 +               case Opt_noxino:
26194 +                       AuLabel(noxino);
26195 +                       break;
26196 +               case Opt_trunc_xib:
26197 +                       AuLabel(trunc_xib);
26198 +                       break;
26199 +               case Opt_notrunc_xib:
26200 +                       AuLabel(notrunc_xib);
26201 +                       break;
26202 +               case Opt_shwh:
26203 +                       AuLabel(shwh);
26204 +                       break;
26205 +               case Opt_noshwh:
26206 +                       AuLabel(noshwh);
26207 +                       break;
26208 +               case Opt_dirperm1:
26209 +                       AuLabel(dirperm1);
26210 +                       break;
26211 +               case Opt_nodirperm1:
26212 +                       AuLabel(nodirperm1);
26213 +                       break;
26214 +               case Opt_plink:
26215 +                       AuLabel(plink);
26216 +                       break;
26217 +               case Opt_noplink:
26218 +                       AuLabel(noplink);
26219 +                       break;
26220 +               case Opt_list_plink:
26221 +                       AuLabel(list_plink);
26222 +                       break;
26223 +               case Opt_udba:
26224 +                       AuDbg("udba %d, %s\n",
26225 +                                 opt->udba, au_optstr_udba(opt->udba));
26226 +                       break;
26227 +               case Opt_dio:
26228 +                       AuLabel(dio);
26229 +                       break;
26230 +               case Opt_nodio:
26231 +                       AuLabel(nodio);
26232 +                       break;
26233 +               case Opt_diropq_a:
26234 +                       AuLabel(diropq_a);
26235 +                       break;
26236 +               case Opt_diropq_w:
26237 +                       AuLabel(diropq_w);
26238 +                       break;
26239 +               case Opt_warn_perm:
26240 +                       AuLabel(warn_perm);
26241 +                       break;
26242 +               case Opt_nowarn_perm:
26243 +                       AuLabel(nowarn_perm);
26244 +                       break;
26245 +               case Opt_verbose:
26246 +                       AuLabel(verbose);
26247 +                       break;
26248 +               case Opt_noverbose:
26249 +                       AuLabel(noverbose);
26250 +                       break;
26251 +               case Opt_sum:
26252 +                       AuLabel(sum);
26253 +                       break;
26254 +               case Opt_nosum:
26255 +                       AuLabel(nosum);
26256 +                       break;
26257 +               case Opt_wsum:
26258 +                       AuLabel(wsum);
26259 +                       break;
26260 +               case Opt_wbr_create:
26261 +                       u.create = &opt->wbr_create;
26262 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26263 +                                 au_optstr_wbr_create(u.create->wbr_create));
26264 +                       switch (u.create->wbr_create) {
26265 +                       case AuWbrCreate_MFSV:
26266 +                       case AuWbrCreate_PMFSV:
26267 +                               AuDbg("%d sec\n", u.create->mfs_second);
26268 +                               break;
26269 +                       case AuWbrCreate_MFSRR:
26270 +                       case AuWbrCreate_TDMFS:
26271 +                               AuDbg("%llu watermark\n",
26272 +                                         u.create->mfsrr_watermark);
26273 +                               break;
26274 +                       case AuWbrCreate_MFSRRV:
26275 +                       case AuWbrCreate_TDMFSV:
26276 +                       case AuWbrCreate_PMFSRRV:
26277 +                               AuDbg("%llu watermark, %d sec\n",
26278 +                                         u.create->mfsrr_watermark,
26279 +                                         u.create->mfs_second);
26280 +                               break;
26281 +                       }
26282 +                       break;
26283 +               case Opt_wbr_copyup:
26284 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26285 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26286 +                       break;
26287 +               case Opt_fhsm_sec:
26288 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26289 +                       break;
26290 +               case Opt_dirren:
26291 +                       AuLabel(dirren);
26292 +                       break;
26293 +               case Opt_nodirren:
26294 +                       AuLabel(nodirren);
26295 +                       break;
26296 +               case Opt_acl:
26297 +                       AuLabel(acl);
26298 +                       break;
26299 +               case Opt_noacl:
26300 +                       AuLabel(noacl);
26301 +                       break;
26302 +               default:
26303 +                       BUG();
26304 +               }
26305 +               opt++;
26306 +       }
26307 +#endif
26308 +}
26309 +
26310 +void au_opts_free(struct au_opts *opts)
26311 +{
26312 +       struct au_opt *opt;
26313 +
26314 +       opt = opts->opt;
26315 +       while (opt->type != Opt_tail) {
26316 +               switch (opt->type) {
26317 +               case Opt_add:
26318 +               case Opt_append:
26319 +               case Opt_prepend:
26320 +                       path_put(&opt->add.path);
26321 +                       break;
26322 +               case Opt_del:
26323 +               case Opt_idel:
26324 +                       path_put(&opt->del.h_path);
26325 +                       break;
26326 +               case Opt_mod:
26327 +               case Opt_imod:
26328 +                       dput(opt->mod.h_root);
26329 +                       break;
26330 +               case Opt_xino:
26331 +                       fput(opt->xino.file);
26332 +                       break;
26333 +               }
26334 +               opt++;
26335 +       }
26336 +}
26337 +
26338 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26339 +                  aufs_bindex_t bindex)
26340 +{
26341 +       int err;
26342 +       struct au_opt_add *add = &opt->add;
26343 +       char *p;
26344 +
26345 +       add->bindex = bindex;
26346 +       add->perm = AuBrPerm_RO;
26347 +       add->pathname = opt_str;
26348 +       p = strchr(opt_str, '=');
26349 +       if (p) {
26350 +               *p++ = 0;
26351 +               if (*p)
26352 +                       add->perm = br_perm_val(p);
26353 +       }
26354 +
26355 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26356 +       if (!err) {
26357 +               if (!p) {
26358 +                       add->perm = AuBrPerm_RO;
26359 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26360 +                               add->perm = AuBrPerm_RR;
26361 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26362 +                               add->perm = AuBrPerm_RW;
26363 +               }
26364 +               opt->type = Opt_add;
26365 +               goto out;
26366 +       }
26367 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26368 +       err = -EINVAL;
26369 +
26370 +out:
26371 +       return err;
26372 +}
26373 +
26374 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26375 +{
26376 +       int err;
26377 +
26378 +       del->pathname = args[0].from;
26379 +       AuDbg("del path %s\n", del->pathname);
26380 +
26381 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26382 +       if (unlikely(err))
26383 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26384 +
26385 +       return err;
26386 +}
26387 +
26388 +#if 0 /* reserved for future use */
26389 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26390 +                             struct au_opt_del *del, substring_t args[])
26391 +{
26392 +       int err;
26393 +       struct dentry *root;
26394 +
26395 +       err = -EINVAL;
26396 +       root = sb->s_root;
26397 +       aufs_read_lock(root, AuLock_FLUSH);
26398 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26399 +               pr_err("out of bounds, %d\n", bindex);
26400 +               goto out;
26401 +       }
26402 +
26403 +       err = 0;
26404 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26405 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26406 +
26407 +out:
26408 +       aufs_read_unlock(root, !AuLock_IR);
26409 +       return err;
26410 +}
26411 +#endif
26412 +
26413 +static int noinline_for_stack
26414 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26415 +{
26416 +       int err;
26417 +       struct path path;
26418 +       char *p;
26419 +
26420 +       err = -EINVAL;
26421 +       mod->path = args[0].from;
26422 +       p = strchr(mod->path, '=');
26423 +       if (unlikely(!p)) {
26424 +               pr_err("no permission %s\n", args[0].from);
26425 +               goto out;
26426 +       }
26427 +
26428 +       *p++ = 0;
26429 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26430 +       if (unlikely(err)) {
26431 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26432 +               goto out;
26433 +       }
26434 +
26435 +       mod->perm = br_perm_val(p);
26436 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26437 +       mod->h_root = dget(path.dentry);
26438 +       path_put(&path);
26439 +
26440 +out:
26441 +       return err;
26442 +}
26443 +
26444 +#if 0 /* reserved for future use */
26445 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26446 +                             struct au_opt_mod *mod, substring_t args[])
26447 +{
26448 +       int err;
26449 +       struct dentry *root;
26450 +
26451 +       err = -EINVAL;
26452 +       root = sb->s_root;
26453 +       aufs_read_lock(root, AuLock_FLUSH);
26454 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26455 +               pr_err("out of bounds, %d\n", bindex);
26456 +               goto out;
26457 +       }
26458 +
26459 +       err = 0;
26460 +       mod->perm = br_perm_val(args[1].from);
26461 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26462 +             mod->path, mod->perm, args[1].from);
26463 +       mod->h_root = dget(au_h_dptr(root, bindex));
26464 +
26465 +out:
26466 +       aufs_read_unlock(root, !AuLock_IR);
26467 +       return err;
26468 +}
26469 +#endif
26470 +
26471 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26472 +                             substring_t args[])
26473 +{
26474 +       int err;
26475 +       struct file *file;
26476 +
26477 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26478 +       err = PTR_ERR(file);
26479 +       if (IS_ERR(file))
26480 +               goto out;
26481 +
26482 +       err = -EINVAL;
26483 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26484 +               fput(file);
26485 +               pr_err("%s must be outside\n", args[0].from);
26486 +               goto out;
26487 +       }
26488 +
26489 +       err = 0;
26490 +       xino->file = file;
26491 +       xino->path = args[0].from;
26492 +
26493 +out:
26494 +       return err;
26495 +}
26496 +
26497 +static int noinline_for_stack
26498 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26499 +                              struct au_opt_xino_itrunc *xino_itrunc,
26500 +                              substring_t args[])
26501 +{
26502 +       int err;
26503 +       aufs_bindex_t bbot, bindex;
26504 +       struct path path;
26505 +       struct dentry *root;
26506 +
26507 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26508 +       if (unlikely(err)) {
26509 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26510 +               goto out;
26511 +       }
26512 +
26513 +       xino_itrunc->bindex = -1;
26514 +       root = sb->s_root;
26515 +       aufs_read_lock(root, AuLock_FLUSH);
26516 +       bbot = au_sbbot(sb);
26517 +       for (bindex = 0; bindex <= bbot; bindex++) {
26518 +               if (au_h_dptr(root, bindex) == path.dentry) {
26519 +                       xino_itrunc->bindex = bindex;
26520 +                       break;
26521 +               }
26522 +       }
26523 +       aufs_read_unlock(root, !AuLock_IR);
26524 +       path_put(&path);
26525 +
26526 +       if (unlikely(xino_itrunc->bindex < 0)) {
26527 +               pr_err("no such branch %s\n", args[0].from);
26528 +               err = -EINVAL;
26529 +       }
26530 +
26531 +out:
26532 +       return err;
26533 +}
26534 +
26535 +/* called without aufs lock */
26536 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26537 +{
26538 +       int err, n, token;
26539 +       aufs_bindex_t bindex;
26540 +       unsigned char skipped;
26541 +       struct dentry *root;
26542 +       struct au_opt *opt, *opt_tail;
26543 +       char *opt_str;
26544 +       /* reduce the stack space */
26545 +       union {
26546 +               struct au_opt_xino_itrunc *xino_itrunc;
26547 +               struct au_opt_wbr_create *create;
26548 +       } u;
26549 +       struct {
26550 +               substring_t args[MAX_OPT_ARGS];
26551 +       } *a;
26552 +
26553 +       err = -ENOMEM;
26554 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26555 +       if (unlikely(!a))
26556 +               goto out;
26557 +
26558 +       root = sb->s_root;
26559 +       err = 0;
26560 +       bindex = 0;
26561 +       opt = opts->opt;
26562 +       opt_tail = opt + opts->max_opt - 1;
26563 +       opt->type = Opt_tail;
26564 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26565 +               err = -EINVAL;
26566 +               skipped = 0;
26567 +               token = match_token(opt_str, options, a->args);
26568 +               switch (token) {
26569 +               case Opt_br:
26570 +                       err = 0;
26571 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26572 +                              && *opt_str) {
26573 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26574 +                                             bindex++);
26575 +                               if (unlikely(!err && ++opt > opt_tail)) {
26576 +                                       err = -E2BIG;
26577 +                                       break;
26578 +                               }
26579 +                               opt->type = Opt_tail;
26580 +                               skipped = 1;
26581 +                       }
26582 +                       break;
26583 +               case Opt_add:
26584 +                       if (unlikely(match_int(&a->args[0], &n))) {
26585 +                               pr_err("bad integer in %s\n", opt_str);
26586 +                               break;
26587 +                       }
26588 +                       bindex = n;
26589 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26590 +                                     bindex);
26591 +                       if (!err)
26592 +                               opt->type = token;
26593 +                       break;
26594 +               case Opt_append:
26595 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26596 +                                     /*dummy bindex*/1);
26597 +                       if (!err)
26598 +                               opt->type = token;
26599 +                       break;
26600 +               case Opt_prepend:
26601 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26602 +                                     /*bindex*/0);
26603 +                       if (!err)
26604 +                               opt->type = token;
26605 +                       break;
26606 +               case Opt_del:
26607 +                       err = au_opts_parse_del(&opt->del, a->args);
26608 +                       if (!err)
26609 +                               opt->type = token;
26610 +                       break;
26611 +#if 0 /* reserved for future use */
26612 +               case Opt_idel:
26613 +                       del->pathname = "(indexed)";
26614 +                       if (unlikely(match_int(&args[0], &n))) {
26615 +                               pr_err("bad integer in %s\n", opt_str);
26616 +                               break;
26617 +                       }
26618 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26619 +                       if (!err)
26620 +                               opt->type = token;
26621 +                       break;
26622 +#endif
26623 +               case Opt_mod:
26624 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26625 +                       if (!err)
26626 +                               opt->type = token;
26627 +                       break;
26628 +#ifdef IMOD /* reserved for future use */
26629 +               case Opt_imod:
26630 +                       u.mod->path = "(indexed)";
26631 +                       if (unlikely(match_int(&a->args[0], &n))) {
26632 +                               pr_err("bad integer in %s\n", opt_str);
26633 +                               break;
26634 +                       }
26635 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26636 +                       if (!err)
26637 +                               opt->type = token;
26638 +                       break;
26639 +#endif
26640 +               case Opt_xino:
26641 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26642 +                       if (!err)
26643 +                               opt->type = token;
26644 +                       break;
26645 +
26646 +               case Opt_trunc_xino_path:
26647 +                       err = au_opts_parse_xino_itrunc_path
26648 +                               (sb, &opt->xino_itrunc, a->args);
26649 +                       if (!err)
26650 +                               opt->type = token;
26651 +                       break;
26652 +
26653 +               case Opt_itrunc_xino:
26654 +                       u.xino_itrunc = &opt->xino_itrunc;
26655 +                       if (unlikely(match_int(&a->args[0], &n))) {
26656 +                               pr_err("bad integer in %s\n", opt_str);
26657 +                               break;
26658 +                       }
26659 +                       u.xino_itrunc->bindex = n;
26660 +                       aufs_read_lock(root, AuLock_FLUSH);
26661 +                       if (n < 0 || au_sbbot(sb) < n) {
26662 +                               pr_err("out of bounds, %d\n", n);
26663 +                               aufs_read_unlock(root, !AuLock_IR);
26664 +                               break;
26665 +                       }
26666 +                       aufs_read_unlock(root, !AuLock_IR);
26667 +                       err = 0;
26668 +                       opt->type = token;
26669 +                       break;
26670 +
26671 +               case Opt_dirwh:
26672 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26673 +                               break;
26674 +                       err = 0;
26675 +                       opt->type = token;
26676 +                       break;
26677 +
26678 +               case Opt_rdcache:
26679 +                       if (unlikely(match_int(&a->args[0], &n))) {
26680 +                               pr_err("bad integer in %s\n", opt_str);
26681 +                               break;
26682 +                       }
26683 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26684 +                               pr_err("rdcache must be smaller than %d\n",
26685 +                                      AUFS_RDCACHE_MAX);
26686 +                               break;
26687 +                       }
26688 +                       opt->rdcache = n;
26689 +                       err = 0;
26690 +                       opt->type = token;
26691 +                       break;
26692 +               case Opt_rdblk:
26693 +                       if (unlikely(match_int(&a->args[0], &n)
26694 +                                    || n < 0
26695 +                                    || n > KMALLOC_MAX_SIZE)) {
26696 +                               pr_err("bad integer in %s\n", opt_str);
26697 +                               break;
26698 +                       }
26699 +                       if (unlikely(n && n < NAME_MAX)) {
26700 +                               pr_err("rdblk must be larger than %d\n",
26701 +                                      NAME_MAX);
26702 +                               break;
26703 +                       }
26704 +                       opt->rdblk = n;
26705 +                       err = 0;
26706 +                       opt->type = token;
26707 +                       break;
26708 +               case Opt_rdhash:
26709 +                       if (unlikely(match_int(&a->args[0], &n)
26710 +                                    || n < 0
26711 +                                    || n * sizeof(struct hlist_head)
26712 +                                    > KMALLOC_MAX_SIZE)) {
26713 +                               pr_err("bad integer in %s\n", opt_str);
26714 +                               break;
26715 +                       }
26716 +                       opt->rdhash = n;
26717 +                       err = 0;
26718 +                       opt->type = token;
26719 +                       break;
26720 +
26721 +               case Opt_trunc_xino:
26722 +               case Opt_notrunc_xino:
26723 +               case Opt_noxino:
26724 +               case Opt_trunc_xib:
26725 +               case Opt_notrunc_xib:
26726 +               case Opt_shwh:
26727 +               case Opt_noshwh:
26728 +               case Opt_dirperm1:
26729 +               case Opt_nodirperm1:
26730 +               case Opt_plink:
26731 +               case Opt_noplink:
26732 +               case Opt_list_plink:
26733 +               case Opt_dio:
26734 +               case Opt_nodio:
26735 +               case Opt_diropq_a:
26736 +               case Opt_diropq_w:
26737 +               case Opt_warn_perm:
26738 +               case Opt_nowarn_perm:
26739 +               case Opt_verbose:
26740 +               case Opt_noverbose:
26741 +               case Opt_sum:
26742 +               case Opt_nosum:
26743 +               case Opt_wsum:
26744 +               case Opt_rdblk_def:
26745 +               case Opt_rdhash_def:
26746 +               case Opt_dirren:
26747 +               case Opt_nodirren:
26748 +               case Opt_acl:
26749 +               case Opt_noacl:
26750 +                       err = 0;
26751 +                       opt->type = token;
26752 +                       break;
26753 +
26754 +               case Opt_udba:
26755 +                       opt->udba = udba_val(a->args[0].from);
26756 +                       if (opt->udba >= 0) {
26757 +                               err = 0;
26758 +                               opt->type = token;
26759 +                       } else
26760 +                               pr_err("wrong value, %s\n", opt_str);
26761 +                       break;
26762 +
26763 +               case Opt_wbr_create:
26764 +                       u.create = &opt->wbr_create;
26765 +                       u.create->wbr_create
26766 +                               = au_wbr_create_val(a->args[0].from, u.create);
26767 +                       if (u.create->wbr_create >= 0) {
26768 +                               err = 0;
26769 +                               opt->type = token;
26770 +                       } else
26771 +                               pr_err("wrong value, %s\n", opt_str);
26772 +                       break;
26773 +               case Opt_wbr_copyup:
26774 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26775 +                       if (opt->wbr_copyup >= 0) {
26776 +                               err = 0;
26777 +                               opt->type = token;
26778 +                       } else
26779 +                               pr_err("wrong value, %s\n", opt_str);
26780 +                       break;
26781 +
26782 +               case Opt_fhsm_sec:
26783 +                       if (unlikely(match_int(&a->args[0], &n)
26784 +                                    || n < 0)) {
26785 +                               pr_err("bad integer in %s\n", opt_str);
26786 +                               break;
26787 +                       }
26788 +                       if (sysaufs_brs) {
26789 +                               opt->fhsm_second = n;
26790 +                               opt->type = token;
26791 +                       } else
26792 +                               pr_warn("ignored %s\n", opt_str);
26793 +                       err = 0;
26794 +                       break;
26795 +
26796 +               case Opt_ignore:
26797 +                       pr_warn("ignored %s\n", opt_str);
26798 +                       /*FALLTHROUGH*/
26799 +               case Opt_ignore_silent:
26800 +                       skipped = 1;
26801 +                       err = 0;
26802 +                       break;
26803 +               case Opt_err:
26804 +                       pr_err("unknown option %s\n", opt_str);
26805 +                       break;
26806 +               }
26807 +
26808 +               if (!err && !skipped) {
26809 +                       if (unlikely(++opt > opt_tail)) {
26810 +                               err = -E2BIG;
26811 +                               opt--;
26812 +                               opt->type = Opt_tail;
26813 +                               break;
26814 +                       }
26815 +                       opt->type = Opt_tail;
26816 +               }
26817 +       }
26818 +
26819 +       au_kfree_rcu(a);
26820 +       dump_opts(opts);
26821 +       if (unlikely(err))
26822 +               au_opts_free(opts);
26823 +
26824 +out:
26825 +       return err;
26826 +}
26827 +
26828 +static int au_opt_wbr_create(struct super_block *sb,
26829 +                            struct au_opt_wbr_create *create)
26830 +{
26831 +       int err;
26832 +       struct au_sbinfo *sbinfo;
26833 +
26834 +       SiMustWriteLock(sb);
26835 +
26836 +       err = 1; /* handled */
26837 +       sbinfo = au_sbi(sb);
26838 +       if (sbinfo->si_wbr_create_ops->fin) {
26839 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26840 +               if (!err)
26841 +                       err = 1;
26842 +       }
26843 +
26844 +       sbinfo->si_wbr_create = create->wbr_create;
26845 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26846 +       switch (create->wbr_create) {
26847 +       case AuWbrCreate_MFSRRV:
26848 +       case AuWbrCreate_MFSRR:
26849 +       case AuWbrCreate_TDMFS:
26850 +       case AuWbrCreate_TDMFSV:
26851 +       case AuWbrCreate_PMFSRR:
26852 +       case AuWbrCreate_PMFSRRV:
26853 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26854 +               /*FALLTHROUGH*/
26855 +       case AuWbrCreate_MFS:
26856 +       case AuWbrCreate_MFSV:
26857 +       case AuWbrCreate_PMFS:
26858 +       case AuWbrCreate_PMFSV:
26859 +               sbinfo->si_wbr_mfs.mfs_expire
26860 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26861 +               break;
26862 +       }
26863 +
26864 +       if (sbinfo->si_wbr_create_ops->init)
26865 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26866 +
26867 +       return err;
26868 +}
26869 +
26870 +/*
26871 + * returns,
26872 + * plus: processed without an error
26873 + * zero: unprocessed
26874 + */
26875 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26876 +                        struct au_opts *opts)
26877 +{
26878 +       int err;
26879 +       struct au_sbinfo *sbinfo;
26880 +
26881 +       SiMustWriteLock(sb);
26882 +
26883 +       err = 1; /* handled */
26884 +       sbinfo = au_sbi(sb);
26885 +       switch (opt->type) {
26886 +       case Opt_udba:
26887 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26888 +               sbinfo->si_mntflags |= opt->udba;
26889 +               opts->given_udba |= opt->udba;
26890 +               break;
26891 +
26892 +       case Opt_plink:
26893 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26894 +               break;
26895 +       case Opt_noplink:
26896 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26897 +                       au_plink_put(sb, /*verbose*/1);
26898 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26899 +               break;
26900 +       case Opt_list_plink:
26901 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26902 +                       au_plink_list(sb);
26903 +               break;
26904 +
26905 +       case Opt_dio:
26906 +               au_opt_set(sbinfo->si_mntflags, DIO);
26907 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26908 +               break;
26909 +       case Opt_nodio:
26910 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26911 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26912 +               break;
26913 +
26914 +       case Opt_fhsm_sec:
26915 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26916 +               break;
26917 +
26918 +       case Opt_diropq_a:
26919 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26920 +               break;
26921 +       case Opt_diropq_w:
26922 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26923 +               break;
26924 +
26925 +       case Opt_warn_perm:
26926 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26927 +               break;
26928 +       case Opt_nowarn_perm:
26929 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
26930 +               break;
26931 +
26932 +       case Opt_verbose:
26933 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
26934 +               break;
26935 +       case Opt_noverbose:
26936 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
26937 +               break;
26938 +
26939 +       case Opt_sum:
26940 +               au_opt_set(sbinfo->si_mntflags, SUM);
26941 +               break;
26942 +       case Opt_wsum:
26943 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26944 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
26945 +               break;
26946 +       case Opt_nosum:
26947 +               au_opt_clr(sbinfo->si_mntflags, SUM);
26948 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
26949 +               break;
26950 +
26951 +       case Opt_wbr_create:
26952 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
26953 +               break;
26954 +       case Opt_wbr_copyup:
26955 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
26956 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
26957 +               break;
26958 +
26959 +       case Opt_dirwh:
26960 +               sbinfo->si_dirwh = opt->dirwh;
26961 +               break;
26962 +
26963 +       case Opt_rdcache:
26964 +               sbinfo->si_rdcache
26965 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
26966 +               break;
26967 +       case Opt_rdblk:
26968 +               sbinfo->si_rdblk = opt->rdblk;
26969 +               break;
26970 +       case Opt_rdblk_def:
26971 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26972 +               break;
26973 +       case Opt_rdhash:
26974 +               sbinfo->si_rdhash = opt->rdhash;
26975 +               break;
26976 +       case Opt_rdhash_def:
26977 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26978 +               break;
26979 +
26980 +       case Opt_shwh:
26981 +               au_opt_set(sbinfo->si_mntflags, SHWH);
26982 +               break;
26983 +       case Opt_noshwh:
26984 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
26985 +               break;
26986 +
26987 +       case Opt_dirperm1:
26988 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
26989 +               break;
26990 +       case Opt_nodirperm1:
26991 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
26992 +               break;
26993 +
26994 +       case Opt_trunc_xino:
26995 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
26996 +               break;
26997 +       case Opt_notrunc_xino:
26998 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
26999 +               break;
27000 +
27001 +       case Opt_trunc_xino_path:
27002 +       case Opt_itrunc_xino:
27003 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27004 +                                   /*idx_begin*/0);
27005 +               if (!err)
27006 +                       err = 1;
27007 +               break;
27008 +
27009 +       case Opt_trunc_xib:
27010 +               au_fset_opts(opts->flags, TRUNC_XIB);
27011 +               break;
27012 +       case Opt_notrunc_xib:
27013 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27014 +               break;
27015 +
27016 +       case Opt_dirren:
27017 +               err = 1;
27018 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27019 +                       err = au_dr_opt_set(sb);
27020 +                       if (!err)
27021 +                               err = 1;
27022 +               }
27023 +               if (err == 1)
27024 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27025 +               break;
27026 +       case Opt_nodirren:
27027 +               err = 1;
27028 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27029 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27030 +                                                             DR_FLUSHED));
27031 +                       if (!err)
27032 +                               err = 1;
27033 +               }
27034 +               if (err == 1)
27035 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27036 +               break;
27037 +
27038 +       case Opt_acl:
27039 +               sb->s_flags |= SB_POSIXACL;
27040 +               break;
27041 +       case Opt_noacl:
27042 +               sb->s_flags &= ~SB_POSIXACL;
27043 +               break;
27044 +
27045 +       default:
27046 +               err = 0;
27047 +               break;
27048 +       }
27049 +
27050 +       return err;
27051 +}
27052 +
27053 +/*
27054 + * returns tri-state.
27055 + * plus: processed without an error
27056 + * zero: unprocessed
27057 + * minus: error
27058 + */
27059 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27060 +                    struct au_opts *opts)
27061 +{
27062 +       int err, do_refresh;
27063 +
27064 +       err = 0;
27065 +       switch (opt->type) {
27066 +       case Opt_append:
27067 +               opt->add.bindex = au_sbbot(sb) + 1;
27068 +               if (opt->add.bindex < 0)
27069 +                       opt->add.bindex = 0;
27070 +               goto add;
27071 +               /* Always goto add, not fallthrough */
27072 +       case Opt_prepend:
27073 +               opt->add.bindex = 0;
27074 +               /* fallthrough */
27075 +       add: /* indented label */
27076 +       case Opt_add:
27077 +               err = au_br_add(sb, &opt->add,
27078 +                               au_ftest_opts(opts->flags, REMOUNT));
27079 +               if (!err) {
27080 +                       err = 1;
27081 +                       au_fset_opts(opts->flags, REFRESH);
27082 +               }
27083 +               break;
27084 +
27085 +       case Opt_del:
27086 +       case Opt_idel:
27087 +               err = au_br_del(sb, &opt->del,
27088 +                               au_ftest_opts(opts->flags, REMOUNT));
27089 +               if (!err) {
27090 +                       err = 1;
27091 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27092 +                       au_fset_opts(opts->flags, REFRESH);
27093 +               }
27094 +               break;
27095 +
27096 +       case Opt_mod:
27097 +       case Opt_imod:
27098 +               err = au_br_mod(sb, &opt->mod,
27099 +                               au_ftest_opts(opts->flags, REMOUNT),
27100 +                               &do_refresh);
27101 +               if (!err) {
27102 +                       err = 1;
27103 +                       if (do_refresh)
27104 +                               au_fset_opts(opts->flags, REFRESH);
27105 +               }
27106 +               break;
27107 +       }
27108 +       return err;
27109 +}
27110 +
27111 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27112 +                      struct au_opt_xino **opt_xino,
27113 +                      struct au_opts *opts)
27114 +{
27115 +       int err;
27116 +
27117 +       err = 0;
27118 +       switch (opt->type) {
27119 +       case Opt_xino:
27120 +               err = au_xino_set(sb, &opt->xino,
27121 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27122 +               if (unlikely(err))
27123 +                       break;
27124 +
27125 +               *opt_xino = &opt->xino;
27126 +               break;
27127 +
27128 +       case Opt_noxino:
27129 +               au_xino_clr(sb);
27130 +               *opt_xino = (void *)-1;
27131 +               break;
27132 +       }
27133 +
27134 +       return err;
27135 +}
27136 +
27137 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27138 +                  unsigned int pending)
27139 +{
27140 +       int err, fhsm;
27141 +       aufs_bindex_t bindex, bbot;
27142 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27143 +       struct au_branch *br;
27144 +       struct au_wbr *wbr;
27145 +       struct dentry *root, *dentry;
27146 +       struct inode *dir, *h_dir;
27147 +       struct au_sbinfo *sbinfo;
27148 +       struct au_hinode *hdir;
27149 +
27150 +       SiMustAnyLock(sb);
27151 +
27152 +       sbinfo = au_sbi(sb);
27153 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27154 +
27155 +       if (!(sb_flags & SB_RDONLY)) {
27156 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27157 +                       pr_warn("first branch should be rw\n");
27158 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27159 +                       pr_warn_once("shwh should be used with ro\n");
27160 +       }
27161 +
27162 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27163 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27164 +               pr_warn_once("udba=*notify requires xino\n");
27165 +
27166 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27167 +               pr_warn_once("dirperm1 breaks the protection"
27168 +                            " by the permission bits on the lower branch\n");
27169 +
27170 +       err = 0;
27171 +       fhsm = 0;
27172 +       root = sb->s_root;
27173 +       dir = d_inode(root);
27174 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27175 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27176 +                                     UDBA_NONE);
27177 +       bbot = au_sbbot(sb);
27178 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27179 +               skip = 0;
27180 +               h_dir = au_h_iptr(dir, bindex);
27181 +               br = au_sbr(sb, bindex);
27182 +
27183 +               if ((br->br_perm & AuBrAttr_ICEX)
27184 +                   && !h_dir->i_op->listxattr)
27185 +                       br->br_perm &= ~AuBrAttr_ICEX;
27186 +#if 0 /* untested */
27187 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27188 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27189 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27190 +#endif
27191 +
27192 +               do_free = 0;
27193 +               wbr = br->br_wbr;
27194 +               if (wbr)
27195 +                       wbr_wh_read_lock(wbr);
27196 +
27197 +               if (!au_br_writable(br->br_perm)) {
27198 +                       do_free = !!wbr;
27199 +                       skip = (!wbr
27200 +                               || (!wbr->wbr_whbase
27201 +                                   && !wbr->wbr_plink
27202 +                                   && !wbr->wbr_orph));
27203 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27204 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27205 +                       skip = (!wbr || !wbr->wbr_whbase);
27206 +                       if (skip && wbr) {
27207 +                               if (do_plink)
27208 +                                       skip = !!wbr->wbr_plink;
27209 +                               else
27210 +                                       skip = !wbr->wbr_plink;
27211 +                       }
27212 +               } else {
27213 +                       /* skip = (br->br_whbase && br->br_ohph); */
27214 +                       skip = (wbr && wbr->wbr_whbase);
27215 +                       if (skip) {
27216 +                               if (do_plink)
27217 +                                       skip = !!wbr->wbr_plink;
27218 +                               else
27219 +                                       skip = !wbr->wbr_plink;
27220 +                       }
27221 +               }
27222 +               if (wbr)
27223 +                       wbr_wh_read_unlock(wbr);
27224 +
27225 +               if (can_no_dreval) {
27226 +                       dentry = br->br_path.dentry;
27227 +                       spin_lock(&dentry->d_lock);
27228 +                       if (dentry->d_flags &
27229 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27230 +                               can_no_dreval = 0;
27231 +                       spin_unlock(&dentry->d_lock);
27232 +               }
27233 +
27234 +               if (au_br_fhsm(br->br_perm)) {
27235 +                       fhsm++;
27236 +                       AuDebugOn(!br->br_fhsm);
27237 +               }
27238 +
27239 +               if (skip)
27240 +                       continue;
27241 +
27242 +               hdir = au_hi(dir, bindex);
27243 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27244 +               if (wbr)
27245 +                       wbr_wh_write_lock(wbr);
27246 +               err = au_wh_init(br, sb);
27247 +               if (wbr)
27248 +                       wbr_wh_write_unlock(wbr);
27249 +               au_hn_inode_unlock(hdir);
27250 +
27251 +               if (!err && do_free) {
27252 +                       au_kfree_rcu(wbr);
27253 +                       br->br_wbr = NULL;
27254 +               }
27255 +       }
27256 +
27257 +       if (can_no_dreval)
27258 +               au_fset_si(sbinfo, NO_DREVAL);
27259 +       else
27260 +               au_fclr_si(sbinfo, NO_DREVAL);
27261 +
27262 +       if (fhsm >= 2) {
27263 +               au_fset_si(sbinfo, FHSM);
27264 +               for (bindex = bbot; bindex >= 0; bindex--) {
27265 +                       br = au_sbr(sb, bindex);
27266 +                       if (au_br_fhsm(br->br_perm)) {
27267 +                               au_fhsm_set_bottom(sb, bindex);
27268 +                               break;
27269 +                       }
27270 +               }
27271 +       } else {
27272 +               au_fclr_si(sbinfo, FHSM);
27273 +               au_fhsm_set_bottom(sb, -1);
27274 +       }
27275 +
27276 +       return err;
27277 +}
27278 +
27279 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27280 +{
27281 +       int err;
27282 +       unsigned int tmp;
27283 +       aufs_bindex_t bindex, bbot;
27284 +       struct au_opt *opt;
27285 +       struct au_opt_xino *opt_xino, xino;
27286 +       struct au_sbinfo *sbinfo;
27287 +       struct au_branch *br;
27288 +       struct inode *dir;
27289 +
27290 +       SiMustWriteLock(sb);
27291 +
27292 +       err = 0;
27293 +       opt_xino = NULL;
27294 +       opt = opts->opt;
27295 +       while (err >= 0 && opt->type != Opt_tail)
27296 +               err = au_opt_simple(sb, opt++, opts);
27297 +       if (err > 0)
27298 +               err = 0;
27299 +       else if (unlikely(err < 0))
27300 +               goto out;
27301 +
27302 +       /* disable xino and udba temporary */
27303 +       sbinfo = au_sbi(sb);
27304 +       tmp = sbinfo->si_mntflags;
27305 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27306 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27307 +
27308 +       opt = opts->opt;
27309 +       while (err >= 0 && opt->type != Opt_tail)
27310 +               err = au_opt_br(sb, opt++, opts);
27311 +       if (err > 0)
27312 +               err = 0;
27313 +       else if (unlikely(err < 0))
27314 +               goto out;
27315 +
27316 +       bbot = au_sbbot(sb);
27317 +       if (unlikely(bbot < 0)) {
27318 +               err = -EINVAL;
27319 +               pr_err("no branches\n");
27320 +               goto out;
27321 +       }
27322 +
27323 +       if (au_opt_test(tmp, XINO))
27324 +               au_opt_set(sbinfo->si_mntflags, XINO);
27325 +       opt = opts->opt;
27326 +       while (!err && opt->type != Opt_tail)
27327 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27328 +       if (unlikely(err))
27329 +               goto out;
27330 +
27331 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27332 +       if (unlikely(err))
27333 +               goto out;
27334 +
27335 +       /* restore xino */
27336 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27337 +               xino.file = au_xino_def(sb);
27338 +               err = PTR_ERR(xino.file);
27339 +               if (IS_ERR(xino.file))
27340 +                       goto out;
27341 +
27342 +               err = au_xino_set(sb, &xino, /*remount*/0);
27343 +               fput(xino.file);
27344 +               if (unlikely(err))
27345 +                       goto out;
27346 +       }
27347 +
27348 +       /* restore udba */
27349 +       tmp &= AuOptMask_UDBA;
27350 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27351 +       sbinfo->si_mntflags |= tmp;
27352 +       bbot = au_sbbot(sb);
27353 +       for (bindex = 0; bindex <= bbot; bindex++) {
27354 +               br = au_sbr(sb, bindex);
27355 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27356 +               if (unlikely(err))
27357 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27358 +                               bindex, err);
27359 +               /* go on even if err */
27360 +       }
27361 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27362 +               dir = d_inode(sb->s_root);
27363 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27364 +       }
27365 +
27366 +out:
27367 +       return err;
27368 +}
27369 +
27370 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27371 +{
27372 +       int err, rerr;
27373 +       unsigned char no_dreval;
27374 +       struct inode *dir;
27375 +       struct au_opt_xino *opt_xino;
27376 +       struct au_opt *opt;
27377 +       struct au_sbinfo *sbinfo;
27378 +
27379 +       SiMustWriteLock(sb);
27380 +
27381 +       err = au_dr_opt_flush(sb);
27382 +       if (unlikely(err))
27383 +               goto out;
27384 +       au_fset_opts(opts->flags, DR_FLUSHED);
27385 +
27386 +       dir = d_inode(sb->s_root);
27387 +       sbinfo = au_sbi(sb);
27388 +       opt_xino = NULL;
27389 +       opt = opts->opt;
27390 +       while (err >= 0 && opt->type != Opt_tail) {
27391 +               err = au_opt_simple(sb, opt, opts);
27392 +               if (!err)
27393 +                       err = au_opt_br(sb, opt, opts);
27394 +               if (!err)
27395 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27396 +               opt++;
27397 +       }
27398 +       if (err > 0)
27399 +               err = 0;
27400 +       AuTraceErr(err);
27401 +       /* go on even err */
27402 +
27403 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27404 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27405 +       if (unlikely(rerr && !err))
27406 +               err = rerr;
27407 +
27408 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27409 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27410 +
27411 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27412 +               rerr = au_xib_trunc(sb);
27413 +               if (unlikely(rerr && !err))
27414 +                       err = rerr;
27415 +       }
27416 +
27417 +       /* will be handled by the caller */
27418 +       if (!au_ftest_opts(opts->flags, REFRESH)
27419 +           && (opts->given_udba
27420 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27421 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27422 +                   ))
27423 +               au_fset_opts(opts->flags, REFRESH);
27424 +
27425 +       AuDbg("status 0x%x\n", opts->flags);
27426 +
27427 +out:
27428 +       return err;
27429 +}
27430 +
27431 +/* ---------------------------------------------------------------------- */
27432 +
27433 +unsigned int au_opt_udba(struct super_block *sb)
27434 +{
27435 +       return au_mntflags(sb) & AuOptMask_UDBA;
27436 +}
27437 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27438 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27439 +++ linux/fs/aufs/opts.h        2020-01-27 10:57:18.175538316 +0100
27440 @@ -0,0 +1,225 @@
27441 +/* SPDX-License-Identifier: GPL-2.0 */
27442 +/*
27443 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27444 + *
27445 + * This program, aufs is free software; you can redistribute it and/or modify
27446 + * it under the terms of the GNU General Public License as published by
27447 + * the Free Software Foundation; either version 2 of the License, or
27448 + * (at your option) any later version.
27449 + *
27450 + * This program is distributed in the hope that it will be useful,
27451 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27452 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27453 + * GNU General Public License for more details.
27454 + *
27455 + * You should have received a copy of the GNU General Public License
27456 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27457 + */
27458 +
27459 +/*
27460 + * mount options/flags
27461 + */
27462 +
27463 +#ifndef __AUFS_OPTS_H__
27464 +#define __AUFS_OPTS_H__
27465 +
27466 +#ifdef __KERNEL__
27467 +
27468 +#include <linux/path.h>
27469 +
27470 +struct file;
27471 +
27472 +/* ---------------------------------------------------------------------- */
27473 +
27474 +/* mount flags */
27475 +#define AuOpt_XINO             1               /* external inode number bitmap
27476 +                                                  and translation table */
27477 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27478 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27479 +#define AuOpt_UDBA_REVAL       (1 << 3)
27480 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27481 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27482 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27483 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27484 +                                                  bits */
27485 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27486 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27487 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27488 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27489 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27490 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27491 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27492 +
27493 +#ifndef CONFIG_AUFS_HNOTIFY
27494 +#undef AuOpt_UDBA_HNOTIFY
27495 +#define AuOpt_UDBA_HNOTIFY     0
27496 +#endif
27497 +#ifndef CONFIG_AUFS_DIRREN
27498 +#undef AuOpt_DIRREN
27499 +#define AuOpt_DIRREN           0
27500 +#endif
27501 +#ifndef CONFIG_AUFS_SHWH
27502 +#undef AuOpt_SHWH
27503 +#define AuOpt_SHWH             0
27504 +#endif
27505 +
27506 +#define AuOpt_Def      (AuOpt_XINO \
27507 +                        | AuOpt_UDBA_REVAL \
27508 +                        | AuOpt_PLINK \
27509 +                        /* | AuOpt_DIRPERM1 */ \
27510 +                        | AuOpt_WARN_PERM)
27511 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27512 +                        | AuOpt_UDBA_REVAL \
27513 +                        | AuOpt_UDBA_HNOTIFY)
27514 +
27515 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27516 +#define au_opt_set(flags, name) do { \
27517 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27518 +       ((flags) |= AuOpt_##name); \
27519 +} while (0)
27520 +#define au_opt_set_udba(flags, name) do { \
27521 +       (flags) &= ~AuOptMask_UDBA; \
27522 +       ((flags) |= AuOpt_##name); \
27523 +} while (0)
27524 +#define au_opt_clr(flags, name) do { \
27525 +       ((flags) &= ~AuOpt_##name); \
27526 +} while (0)
27527 +
27528 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27529 +{
27530 +#ifdef CONFIG_PROC_FS
27531 +       return mntflags;
27532 +#else
27533 +       return mntflags & ~AuOpt_PLINK;
27534 +#endif
27535 +}
27536 +
27537 +/* ---------------------------------------------------------------------- */
27538 +
27539 +/* policies to select one among multiple writable branches */
27540 +enum {
27541 +       AuWbrCreate_TDP,        /* top down parent */
27542 +       AuWbrCreate_RR,         /* round robin */
27543 +       AuWbrCreate_MFS,        /* most free space */
27544 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27545 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27546 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27547 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27548 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27549 +       AuWbrCreate_PMFS,       /* parent and mfs */
27550 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27551 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27552 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27553 +
27554 +       AuWbrCreate_Def = AuWbrCreate_TDP
27555 +};
27556 +
27557 +enum {
27558 +       AuWbrCopyup_TDP,        /* top down parent */
27559 +       AuWbrCopyup_BUP,        /* bottom up parent */
27560 +       AuWbrCopyup_BU,         /* bottom up */
27561 +
27562 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27563 +};
27564 +
27565 +/* ---------------------------------------------------------------------- */
27566 +
27567 +struct au_opt_add {
27568 +       aufs_bindex_t   bindex;
27569 +       char            *pathname;
27570 +       int             perm;
27571 +       struct path     path;
27572 +};
27573 +
27574 +struct au_opt_del {
27575 +       char            *pathname;
27576 +       struct path     h_path;
27577 +};
27578 +
27579 +struct au_opt_mod {
27580 +       char            *path;
27581 +       int             perm;
27582 +       struct dentry   *h_root;
27583 +};
27584 +
27585 +struct au_opt_xino {
27586 +       char            *path;
27587 +       struct file     *file;
27588 +};
27589 +
27590 +struct au_opt_xino_itrunc {
27591 +       aufs_bindex_t   bindex;
27592 +};
27593 +
27594 +struct au_opt_wbr_create {
27595 +       int                     wbr_create;
27596 +       int                     mfs_second;
27597 +       unsigned long long      mfsrr_watermark;
27598 +};
27599 +
27600 +struct au_opt {
27601 +       int type;
27602 +       union {
27603 +               struct au_opt_xino      xino;
27604 +               struct au_opt_xino_itrunc xino_itrunc;
27605 +               struct au_opt_add       add;
27606 +               struct au_opt_del       del;
27607 +               struct au_opt_mod       mod;
27608 +               int                     dirwh;
27609 +               int                     rdcache;
27610 +               unsigned int            rdblk;
27611 +               unsigned int            rdhash;
27612 +               int                     udba;
27613 +               struct au_opt_wbr_create wbr_create;
27614 +               int                     wbr_copyup;
27615 +               unsigned int            fhsm_second;
27616 +       };
27617 +};
27618 +
27619 +/* opts flags */
27620 +#define AuOpts_REMOUNT         1
27621 +#define AuOpts_REFRESH         (1 << 1)
27622 +#define AuOpts_TRUNC_XIB       (1 << 2)
27623 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27624 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27625 +#define AuOpts_DR_FLUSHED      (1 << 5)
27626 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27627 +#define au_fset_opts(flags, name) \
27628 +       do { (flags) |= AuOpts_##name; } while (0)
27629 +#define au_fclr_opts(flags, name) \
27630 +       do { (flags) &= ~AuOpts_##name; } while (0)
27631 +
27632 +#ifndef CONFIG_AUFS_DIRREN
27633 +#undef AuOpts_DR_FLUSHED
27634 +#define AuOpts_DR_FLUSHED      0
27635 +#endif
27636 +
27637 +struct au_opts {
27638 +       struct au_opt   *opt;
27639 +       int             max_opt;
27640 +
27641 +       unsigned int    given_udba;
27642 +       unsigned int    flags;
27643 +       unsigned long   sb_flags;
27644 +};
27645 +
27646 +/* ---------------------------------------------------------------------- */
27647 +
27648 +/* opts.c */
27649 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27650 +const char *au_optstr_udba(int udba);
27651 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27652 +const char *au_optstr_wbr_create(int wbr_create);
27653 +
27654 +void au_opts_free(struct au_opts *opts);
27655 +struct super_block;
27656 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27657 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27658 +                  unsigned int pending);
27659 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27660 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27661 +
27662 +unsigned int au_opt_udba(struct super_block *sb);
27663 +
27664 +#endif /* __KERNEL__ */
27665 +#endif /* __AUFS_OPTS_H__ */
27666 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27667 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27668 +++ linux/fs/aufs/plink.c       2020-01-27 10:57:18.175538316 +0100
27669 @@ -0,0 +1,516 @@
27670 +// SPDX-License-Identifier: GPL-2.0
27671 +/*
27672 + * Copyright (C) 2005-2020 Junjiro R. Okajima
27673 + *
27674 + * This program, aufs is free software; you can redistribute it and/or modify
27675 + * it under the terms of the GNU General Public License as published by
27676 + * the Free Software Foundation; either version 2 of the License, or
27677 + * (at your option) any later version.
27678 + *
27679 + * This program is distributed in the hope that it will be useful,
27680 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27681 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27682 + * GNU General Public License for more details.
27683 + *
27684 + * You should have received a copy of the GNU General Public License
27685 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27686 + */
27687 +
27688 +/*
27689 + * pseudo-link
27690 + */
27691 +
27692 +#include "aufs.h"
27693 +
27694 +/*
27695 + * the pseudo-link maintenance mode.
27696 + * during a user process maintains the pseudo-links,
27697 + * prohibit adding a new plink and branch manipulation.
27698 + *
27699 + * Flags
27700 + * NOPLM:
27701 + *     For entry functions which will handle plink, and i_mutex is already held
27702 + *     in VFS.
27703 + *     They cannot wait and should return an error at once.
27704 + *     Callers has to check the error.
27705 + * NOPLMW:
27706 + *     For entry functions which will handle plink, but i_mutex is not held
27707 + *     in VFS.
27708 + *     They can wait the plink maintenance mode to finish.
27709 + *
27710 + * They behave like F_SETLK and F_SETLKW.
27711 + * If the caller never handle plink, then both flags are unnecessary.
27712 + */
27713 +
27714 +int au_plink_maint(struct super_block *sb, int flags)
27715 +{
27716 +       int err;
27717 +       pid_t pid, ppid;
27718 +       struct task_struct *parent, *prev;
27719 +       struct au_sbinfo *sbi;
27720 +
27721 +       SiMustAnyLock(sb);
27722 +
27723 +       err = 0;
27724 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27725 +               goto out;
27726 +
27727 +       sbi = au_sbi(sb);
27728 +       pid = sbi->si_plink_maint_pid;
27729 +       if (!pid || pid == current->pid)
27730 +               goto out;
27731 +
27732 +       /* todo: it highly depends upon /sbin/mount.aufs */
27733 +       prev = NULL;
27734 +       parent = current;
27735 +       ppid = 0;
27736 +       rcu_read_lock();
27737 +       while (1) {
27738 +               parent = rcu_dereference(parent->real_parent);
27739 +               if (parent == prev)
27740 +                       break;
27741 +               ppid = task_pid_vnr(parent);
27742 +               if (pid == ppid) {
27743 +                       rcu_read_unlock();
27744 +                       goto out;
27745 +               }
27746 +               prev = parent;
27747 +       }
27748 +       rcu_read_unlock();
27749 +
27750 +       if (au_ftest_lock(flags, NOPLMW)) {
27751 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27752 +               /* AuDebugOn(!lockdep_depth(current)); */
27753 +               while (sbi->si_plink_maint_pid) {
27754 +                       si_read_unlock(sb);
27755 +                       /* gave up wake_up_bit() */
27756 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27757 +
27758 +                       if (au_ftest_lock(flags, FLUSH))
27759 +                               au_nwt_flush(&sbi->si_nowait);
27760 +                       si_noflush_read_lock(sb);
27761 +               }
27762 +       } else if (au_ftest_lock(flags, NOPLM)) {
27763 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27764 +               err = -EAGAIN;
27765 +       }
27766 +
27767 +out:
27768 +       return err;
27769 +}
27770 +
27771 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27772 +{
27773 +       spin_lock(&sbinfo->si_plink_maint_lock);
27774 +       sbinfo->si_plink_maint_pid = 0;
27775 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27776 +       wake_up_all(&sbinfo->si_plink_wq);
27777 +}
27778 +
27779 +int au_plink_maint_enter(struct super_block *sb)
27780 +{
27781 +       int err;
27782 +       struct au_sbinfo *sbinfo;
27783 +
27784 +       err = 0;
27785 +       sbinfo = au_sbi(sb);
27786 +       /* make sure i am the only one in this fs */
27787 +       si_write_lock(sb, AuLock_FLUSH);
27788 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27789 +               spin_lock(&sbinfo->si_plink_maint_lock);
27790 +               if (!sbinfo->si_plink_maint_pid)
27791 +                       sbinfo->si_plink_maint_pid = current->pid;
27792 +               else
27793 +                       err = -EBUSY;
27794 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27795 +       }
27796 +       si_write_unlock(sb);
27797 +
27798 +       return err;
27799 +}
27800 +
27801 +/* ---------------------------------------------------------------------- */
27802 +
27803 +#ifdef CONFIG_AUFS_DEBUG
27804 +void au_plink_list(struct super_block *sb)
27805 +{
27806 +       int i;
27807 +       struct au_sbinfo *sbinfo;
27808 +       struct hlist_bl_head *hbl;
27809 +       struct hlist_bl_node *pos;
27810 +       struct au_icntnr *icntnr;
27811 +
27812 +       SiMustAnyLock(sb);
27813 +
27814 +       sbinfo = au_sbi(sb);
27815 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27816 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27817 +
27818 +       for (i = 0; i < AuPlink_NHASH; i++) {
27819 +               hbl = sbinfo->si_plink + i;
27820 +               hlist_bl_lock(hbl);
27821 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27822 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27823 +               hlist_bl_unlock(hbl);
27824 +       }
27825 +}
27826 +#endif
27827 +
27828 +/* is the inode pseudo-linked? */
27829 +int au_plink_test(struct inode *inode)
27830 +{
27831 +       int found, i;
27832 +       struct au_sbinfo *sbinfo;
27833 +       struct hlist_bl_head *hbl;
27834 +       struct hlist_bl_node *pos;
27835 +       struct au_icntnr *icntnr;
27836 +
27837 +       sbinfo = au_sbi(inode->i_sb);
27838 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27839 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27840 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27841 +
27842 +       found = 0;
27843 +       i = au_plink_hash(inode->i_ino);
27844 +       hbl =  sbinfo->si_plink + i;
27845 +       hlist_bl_lock(hbl);
27846 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27847 +               if (&icntnr->vfs_inode == inode) {
27848 +                       found = 1;
27849 +                       break;
27850 +               }
27851 +       hlist_bl_unlock(hbl);
27852 +       return found;
27853 +}
27854 +
27855 +/* ---------------------------------------------------------------------- */
27856 +
27857 +/*
27858 + * generate a name for plink.
27859 + * the file will be stored under AUFS_WH_PLINKDIR.
27860 + */
27861 +/* 20 is max digits length of ulong 64 */
27862 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27863 +
27864 +static int plink_name(char *name, int len, struct inode *inode,
27865 +                     aufs_bindex_t bindex)
27866 +{
27867 +       int rlen;
27868 +       struct inode *h_inode;
27869 +
27870 +       h_inode = au_h_iptr(inode, bindex);
27871 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27872 +       return rlen;
27873 +}
27874 +
27875 +struct au_do_plink_lkup_args {
27876 +       struct dentry **errp;
27877 +       struct qstr *tgtname;
27878 +       struct dentry *h_parent;
27879 +       struct au_branch *br;
27880 +};
27881 +
27882 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27883 +                                      struct dentry *h_parent,
27884 +                                      struct au_branch *br)
27885 +{
27886 +       struct dentry *h_dentry;
27887 +       struct inode *h_inode;
27888 +
27889 +       h_inode = d_inode(h_parent);
27890 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27891 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
27892 +       inode_unlock_shared(h_inode);
27893 +       return h_dentry;
27894 +}
27895 +
27896 +static void au_call_do_plink_lkup(void *args)
27897 +{
27898 +       struct au_do_plink_lkup_args *a = args;
27899 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
27900 +}
27901 +
27902 +/* lookup the plink-ed @inode under the branch at @bindex */
27903 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27904 +{
27905 +       struct dentry *h_dentry, *h_parent;
27906 +       struct au_branch *br;
27907 +       int wkq_err;
27908 +       char a[PLINK_NAME_LEN];
27909 +       struct qstr tgtname = QSTR_INIT(a, 0);
27910 +
27911 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27912 +
27913 +       br = au_sbr(inode->i_sb, bindex);
27914 +       h_parent = br->br_wbr->wbr_plink;
27915 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27916 +
27917 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27918 +               struct au_do_plink_lkup_args args = {
27919 +                       .errp           = &h_dentry,
27920 +                       .tgtname        = &tgtname,
27921 +                       .h_parent       = h_parent,
27922 +                       .br             = br
27923 +               };
27924 +
27925 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27926 +               if (unlikely(wkq_err))
27927 +                       h_dentry = ERR_PTR(wkq_err);
27928 +       } else
27929 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
27930 +
27931 +       return h_dentry;
27932 +}
27933 +
27934 +/* create a pseudo-link */
27935 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
27936 +                     struct dentry *h_dentry, struct au_branch *br)
27937 +{
27938 +       int err;
27939 +       struct path h_path = {
27940 +               .mnt = au_br_mnt(br)
27941 +       };
27942 +       struct inode *h_dir, *delegated;
27943 +
27944 +       h_dir = d_inode(h_parent);
27945 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
27946 +again:
27947 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
27948 +       err = PTR_ERR(h_path.dentry);
27949 +       if (IS_ERR(h_path.dentry))
27950 +               goto out;
27951 +
27952 +       err = 0;
27953 +       /* wh.plink dir is not monitored */
27954 +       /* todo: is it really safe? */
27955 +       if (d_is_positive(h_path.dentry)
27956 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
27957 +               delegated = NULL;
27958 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
27959 +               if (unlikely(err == -EWOULDBLOCK)) {
27960 +                       pr_warn("cannot retry for NFSv4 delegation"
27961 +                               " for an internal unlink\n");
27962 +                       iput(delegated);
27963 +               }
27964 +               dput(h_path.dentry);
27965 +               h_path.dentry = NULL;
27966 +               if (!err)
27967 +                       goto again;
27968 +       }
27969 +       if (!err && d_is_negative(h_path.dentry)) {
27970 +               delegated = NULL;
27971 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
27972 +               if (unlikely(err == -EWOULDBLOCK)) {
27973 +                       pr_warn("cannot retry for NFSv4 delegation"
27974 +                               " for an internal link\n");
27975 +                       iput(delegated);
27976 +               }
27977 +       }
27978 +       dput(h_path.dentry);
27979 +
27980 +out:
27981 +       inode_unlock(h_dir);
27982 +       return err;
27983 +}
27984 +
27985 +struct do_whplink_args {
27986 +       int *errp;
27987 +       struct qstr *tgt;
27988 +       struct dentry *h_parent;
27989 +       struct dentry *h_dentry;
27990 +       struct au_branch *br;
27991 +};
27992 +
27993 +static void call_do_whplink(void *args)
27994 +{
27995 +       struct do_whplink_args *a = args;
27996 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
27997 +}
27998 +
27999 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28000 +                  aufs_bindex_t bindex, struct au_branch *br)
28001 +{
28002 +       int err, wkq_err;
28003 +       struct au_wbr *wbr;
28004 +       struct dentry *h_parent;
28005 +       char a[PLINK_NAME_LEN];
28006 +       struct qstr tgtname = QSTR_INIT(a, 0);
28007 +
28008 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
28009 +       h_parent = wbr->wbr_plink;
28010 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28011 +
28012 +       /* always superio. */
28013 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28014 +               struct do_whplink_args args = {
28015 +                       .errp           = &err,
28016 +                       .tgt            = &tgtname,
28017 +                       .h_parent       = h_parent,
28018 +                       .h_dentry       = h_dentry,
28019 +                       .br             = br
28020 +               };
28021 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28022 +               if (unlikely(wkq_err))
28023 +                       err = wkq_err;
28024 +       } else
28025 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
28026 +
28027 +       return err;
28028 +}
28029 +
28030 +/*
28031 + * create a new pseudo-link for @h_dentry on @bindex.
28032 + * the linked inode is held in aufs @inode.
28033 + */
28034 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28035 +                    struct dentry *h_dentry)
28036 +{
28037 +       struct super_block *sb;
28038 +       struct au_sbinfo *sbinfo;
28039 +       struct hlist_bl_head *hbl;
28040 +       struct hlist_bl_node *pos;
28041 +       struct au_icntnr *icntnr;
28042 +       int found, err, cnt, i;
28043 +
28044 +       sb = inode->i_sb;
28045 +       sbinfo = au_sbi(sb);
28046 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28047 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28048 +
28049 +       found = au_plink_test(inode);
28050 +       if (found)
28051 +               return;
28052 +
28053 +       i = au_plink_hash(inode->i_ino);
28054 +       hbl = sbinfo->si_plink + i;
28055 +       au_igrab(inode);
28056 +
28057 +       hlist_bl_lock(hbl);
28058 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28059 +               if (&icntnr->vfs_inode == inode) {
28060 +                       found = 1;
28061 +                       break;
28062 +               }
28063 +       }
28064 +       if (!found) {
28065 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28066 +               hlist_bl_add_head(&icntnr->plink, hbl);
28067 +       }
28068 +       hlist_bl_unlock(hbl);
28069 +       if (!found) {
28070 +               cnt = au_hbl_count(hbl);
28071 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28072 +               if (cnt > AUFS_PLINK_WARN)
28073 +                       AuWarn1(msg ", %d\n", cnt);
28074 +#undef msg
28075 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
28076 +               if (unlikely(err)) {
28077 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28078 +                       au_hbl_del(&icntnr->plink, hbl);
28079 +                       iput(&icntnr->vfs_inode);
28080 +               }
28081 +       } else
28082 +               iput(&icntnr->vfs_inode);
28083 +}
28084 +
28085 +/* free all plinks */
28086 +void au_plink_put(struct super_block *sb, int verbose)
28087 +{
28088 +       int i, warned;
28089 +       struct au_sbinfo *sbinfo;
28090 +       struct hlist_bl_head *hbl;
28091 +       struct hlist_bl_node *pos, *tmp;
28092 +       struct au_icntnr *icntnr;
28093 +
28094 +       SiMustWriteLock(sb);
28095 +
28096 +       sbinfo = au_sbi(sb);
28097 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28098 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28099 +
28100 +       /* no spin_lock since sbinfo is write-locked */
28101 +       warned = 0;
28102 +       for (i = 0; i < AuPlink_NHASH; i++) {
28103 +               hbl = sbinfo->si_plink + i;
28104 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28105 +                       pr_warn("pseudo-link is not flushed");
28106 +                       warned = 1;
28107 +               }
28108 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28109 +                       iput(&icntnr->vfs_inode);
28110 +               INIT_HLIST_BL_HEAD(hbl);
28111 +       }
28112 +}
28113 +
28114 +void au_plink_clean(struct super_block *sb, int verbose)
28115 +{
28116 +       struct dentry *root;
28117 +
28118 +       root = sb->s_root;
28119 +       aufs_write_lock(root);
28120 +       if (au_opt_test(au_mntflags(sb), PLINK))
28121 +               au_plink_put(sb, verbose);
28122 +       aufs_write_unlock(root);
28123 +}
28124 +
28125 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28126 +{
28127 +       int do_put;
28128 +       aufs_bindex_t btop, bbot, bindex;
28129 +
28130 +       do_put = 0;
28131 +       btop = au_ibtop(inode);
28132 +       bbot = au_ibbot(inode);
28133 +       if (btop >= 0) {
28134 +               for (bindex = btop; bindex <= bbot; bindex++) {
28135 +                       if (!au_h_iptr(inode, bindex)
28136 +                           || au_ii_br_id(inode, bindex) != br_id)
28137 +                               continue;
28138 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28139 +                       do_put = 1;
28140 +                       break;
28141 +               }
28142 +               if (do_put)
28143 +                       for (bindex = btop; bindex <= bbot; bindex++)
28144 +                               if (au_h_iptr(inode, bindex)) {
28145 +                                       do_put = 0;
28146 +                                       break;
28147 +                               }
28148 +       } else
28149 +               do_put = 1;
28150 +
28151 +       return do_put;
28152 +}
28153 +
28154 +/* free the plinks on a branch specified by @br_id */
28155 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28156 +{
28157 +       struct au_sbinfo *sbinfo;
28158 +       struct hlist_bl_head *hbl;
28159 +       struct hlist_bl_node *pos, *tmp;
28160 +       struct au_icntnr *icntnr;
28161 +       struct inode *inode;
28162 +       int i, do_put;
28163 +
28164 +       SiMustWriteLock(sb);
28165 +
28166 +       sbinfo = au_sbi(sb);
28167 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28168 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28169 +
28170 +       /* no bit_lock since sbinfo is write-locked */
28171 +       for (i = 0; i < AuPlink_NHASH; i++) {
28172 +               hbl = sbinfo->si_plink + i;
28173 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28174 +                       inode = au_igrab(&icntnr->vfs_inode);
28175 +                       ii_write_lock_child(inode);
28176 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28177 +                       if (do_put) {
28178 +                               hlist_bl_del(&icntnr->plink);
28179 +                               iput(inode);
28180 +                       }
28181 +                       ii_write_unlock(inode);
28182 +                       iput(inode);
28183 +               }
28184 +       }
28185 +}
28186 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28187 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28188 +++ linux/fs/aufs/poll.c        2020-01-27 10:57:18.175538316 +0100
28189 @@ -0,0 +1,51 @@
28190 +// SPDX-License-Identifier: GPL-2.0
28191 +/*
28192 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28193 + *
28194 + * This program, aufs is free software; you can redistribute it and/or modify
28195 + * it under the terms of the GNU General Public License as published by
28196 + * the Free Software Foundation; either version 2 of the License, or
28197 + * (at your option) any later version.
28198 + *
28199 + * This program is distributed in the hope that it will be useful,
28200 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28201 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28202 + * GNU General Public License for more details.
28203 + *
28204 + * You should have received a copy of the GNU General Public License
28205 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28206 + */
28207 +
28208 +/*
28209 + * poll operation
28210 + * There is only one filesystem which implements ->poll operation, currently.
28211 + */
28212 +
28213 +#include "aufs.h"
28214 +
28215 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28216 +{
28217 +       __poll_t mask;
28218 +       struct file *h_file;
28219 +       struct super_block *sb;
28220 +
28221 +       /* We should pretend an error happened. */
28222 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28223 +       sb = file->f_path.dentry->d_sb;
28224 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28225 +
28226 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28227 +       if (IS_ERR(h_file)) {
28228 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28229 +               goto out;
28230 +       }
28231 +
28232 +       mask = vfs_poll(h_file, pt);
28233 +       fput(h_file); /* instead of au_read_post() */
28234 +
28235 +out:
28236 +       si_read_unlock(sb);
28237 +       if (mask & EPOLLERR)
28238 +               AuDbg("mask 0x%x\n", mask);
28239 +       return mask;
28240 +}
28241 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28242 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28243 +++ linux/fs/aufs/posix_acl.c   2020-01-27 10:57:18.175538316 +0100
28244 @@ -0,0 +1,105 @@
28245 +// SPDX-License-Identifier: GPL-2.0
28246 +/*
28247 + * Copyright (C) 2014-2020 Junjiro R. Okajima
28248 + *
28249 + * This program, aufs is free software; you can redistribute it and/or modify
28250 + * it under the terms of the GNU General Public License as published by
28251 + * the Free Software Foundation; either version 2 of the License, or
28252 + * (at your option) any later version.
28253 + *
28254 + * This program is distributed in the hope that it will be useful,
28255 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28256 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28257 + * GNU General Public License for more details.
28258 + *
28259 + * You should have received a copy of the GNU General Public License
28260 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28261 + */
28262 +
28263 +/*
28264 + * posix acl operations
28265 + */
28266 +
28267 +#include <linux/fs.h>
28268 +#include "aufs.h"
28269 +
28270 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
28271 +{
28272 +       struct posix_acl *acl;
28273 +       int err;
28274 +       aufs_bindex_t bindex;
28275 +       struct inode *h_inode;
28276 +       struct super_block *sb;
28277 +
28278 +       acl = NULL;
28279 +       sb = inode->i_sb;
28280 +       si_read_lock(sb, AuLock_FLUSH);
28281 +       ii_read_lock_child(inode);
28282 +       if (!(sb->s_flags & SB_POSIXACL))
28283 +               goto out;
28284 +
28285 +       bindex = au_ibtop(inode);
28286 +       h_inode = au_h_iptr(inode, bindex);
28287 +       if (unlikely(!h_inode
28288 +                    || ((h_inode->i_mode & S_IFMT)
28289 +                        != (inode->i_mode & S_IFMT)))) {
28290 +               err = au_busy_or_stale();
28291 +               acl = ERR_PTR(err);
28292 +               goto out;
28293 +       }
28294 +
28295 +       /* always topmost only */
28296 +       acl = get_acl(h_inode, type);
28297 +       if (IS_ERR(acl))
28298 +               forget_cached_acl(inode, type);
28299 +       else
28300 +               set_cached_acl(inode, type, acl);
28301 +
28302 +out:
28303 +       ii_read_unlock(inode);
28304 +       si_read_unlock(sb);
28305 +
28306 +       AuTraceErrPtr(acl);
28307 +       return acl;
28308 +}
28309 +
28310 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
28311 +{
28312 +       int err;
28313 +       ssize_t ssz;
28314 +       struct dentry *dentry;
28315 +       struct au_sxattr arg = {
28316 +               .type = AU_ACL_SET,
28317 +               .u.acl_set = {
28318 +                       .acl    = acl,
28319 +                       .type   = type
28320 +               },
28321 +       };
28322 +
28323 +       IMustLock(inode);
28324 +
28325 +       if (inode->i_ino == AUFS_ROOT_INO)
28326 +               dentry = dget(inode->i_sb->s_root);
28327 +       else {
28328 +               dentry = d_find_alias(inode);
28329 +               if (!dentry)
28330 +                       dentry = d_find_any_alias(inode);
28331 +               if (!dentry) {
28332 +                       pr_warn("cannot handle this inode, "
28333 +                               "please report to aufs-users ML\n");
28334 +                       err = -ENOENT;
28335 +                       goto out;
28336 +               }
28337 +       }
28338 +
28339 +       ssz = au_sxattr(dentry, inode, &arg);
28340 +       /* forget even it if succeeds since the branch might set differently */
28341 +       forget_cached_acl(inode, type);
28342 +       dput(dentry);
28343 +       err = ssz;
28344 +       if (ssz >= 0)
28345 +               err = 0;
28346 +
28347 +out:
28348 +       return err;
28349 +}
28350 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28351 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28352 +++ linux/fs/aufs/procfs.c      2020-08-03 09:14:43.142321747 +0200
28353 @@ -0,0 +1,170 @@
28354 +// SPDX-License-Identifier: GPL-2.0
28355 +/*
28356 + * Copyright (C) 2010-2020 Junjiro R. Okajima
28357 + *
28358 + * This program, aufs is free software; you can redistribute it and/or modify
28359 + * it under the terms of the GNU General Public License as published by
28360 + * the Free Software Foundation; either version 2 of the License, or
28361 + * (at your option) any later version.
28362 + *
28363 + * This program is distributed in the hope that it will be useful,
28364 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28365 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28366 + * GNU General Public License for more details.
28367 + *
28368 + * You should have received a copy of the GNU General Public License
28369 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28370 + */
28371 +
28372 +/*
28373 + * procfs interfaces
28374 + */
28375 +
28376 +#include <linux/proc_fs.h>
28377 +#include "aufs.h"
28378 +
28379 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28380 +{
28381 +       struct au_sbinfo *sbinfo;
28382 +
28383 +       sbinfo = file->private_data;
28384 +       if (sbinfo) {
28385 +               au_plink_maint_leave(sbinfo);
28386 +               kobject_put(&sbinfo->si_kobj);
28387 +       }
28388 +
28389 +       return 0;
28390 +}
28391 +
28392 +static void au_procfs_plm_write_clean(struct file *file)
28393 +{
28394 +       struct au_sbinfo *sbinfo;
28395 +
28396 +       sbinfo = file->private_data;
28397 +       if (sbinfo)
28398 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28399 +}
28400 +
28401 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28402 +{
28403 +       int err;
28404 +       struct super_block *sb;
28405 +       struct au_sbinfo *sbinfo;
28406 +       struct hlist_bl_node *pos;
28407 +
28408 +       err = -EBUSY;
28409 +       if (unlikely(file->private_data))
28410 +               goto out;
28411 +
28412 +       sb = NULL;
28413 +       /* don't use au_sbilist_lock() here */
28414 +       hlist_bl_lock(&au_sbilist);
28415 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28416 +               if (id == sysaufs_si_id(sbinfo)) {
28417 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28418 +                               sb = sbinfo->si_sb;
28419 +                       break;
28420 +               }
28421 +       hlist_bl_unlock(&au_sbilist);
28422 +
28423 +       err = -EINVAL;
28424 +       if (unlikely(!sb))
28425 +               goto out;
28426 +
28427 +       err = au_plink_maint_enter(sb);
28428 +       if (!err)
28429 +               /* keep kobject_get() */
28430 +               file->private_data = sbinfo;
28431 +       else
28432 +               kobject_put(&sbinfo->si_kobj);
28433 +out:
28434 +       return err;
28435 +}
28436 +
28437 +/*
28438 + * Accept a valid "si=xxxx" only.
28439 + * Once it is accepted successfully, accept "clean" too.
28440 + */
28441 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28442 +                                  size_t count, loff_t *ppos)
28443 +{
28444 +       ssize_t err;
28445 +       unsigned long id;
28446 +       /* last newline is allowed */
28447 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28448 +
28449 +       err = -EACCES;
28450 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28451 +               goto out;
28452 +
28453 +       err = -EINVAL;
28454 +       if (unlikely(count > sizeof(buf)))
28455 +               goto out;
28456 +
28457 +       err = copy_from_user(buf, ubuf, count);
28458 +       if (unlikely(err)) {
28459 +               err = -EFAULT;
28460 +               goto out;
28461 +       }
28462 +       buf[count] = 0;
28463 +
28464 +       err = -EINVAL;
28465 +       if (!strcmp("clean", buf)) {
28466 +               au_procfs_plm_write_clean(file);
28467 +               goto out_success;
28468 +       } else if (unlikely(strncmp("si=", buf, 3)))
28469 +               goto out;
28470 +
28471 +       err = kstrtoul(buf + 3, 16, &id);
28472 +       if (unlikely(err))
28473 +               goto out;
28474 +
28475 +       err = au_procfs_plm_write_si(file, id);
28476 +       if (unlikely(err))
28477 +               goto out;
28478 +
28479 +out_success:
28480 +       err = count; /* success */
28481 +out:
28482 +       return err;
28483 +}
28484 +
28485 +static const struct proc_ops au_procfs_plm_op = {
28486 +       .proc_write     = au_procfs_plm_write,
28487 +       .proc_release   = au_procfs_plm_release
28488 +};
28489 +
28490 +/* ---------------------------------------------------------------------- */
28491 +
28492 +static struct proc_dir_entry *au_procfs_dir;
28493 +
28494 +void au_procfs_fin(void)
28495 +{
28496 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28497 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28498 +}
28499 +
28500 +int __init au_procfs_init(void)
28501 +{
28502 +       int err;
28503 +       struct proc_dir_entry *entry;
28504 +
28505 +       err = -ENOMEM;
28506 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28507 +       if (unlikely(!au_procfs_dir))
28508 +               goto out;
28509 +
28510 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28511 +                           au_procfs_dir, &au_procfs_plm_op);
28512 +       if (unlikely(!entry))
28513 +               goto out_dir;
28514 +
28515 +       err = 0;
28516 +       goto out; /* success */
28517 +
28518 +
28519 +out_dir:
28520 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28521 +out:
28522 +       return err;
28523 +}
28524 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28525 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28526 +++ linux/fs/aufs/rdu.c 2020-01-27 10:57:18.178871751 +0100
28527 @@ -0,0 +1,384 @@
28528 +// SPDX-License-Identifier: GPL-2.0
28529 +/*
28530 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28531 + *
28532 + * This program, aufs is free software; you can redistribute it and/or modify
28533 + * it under the terms of the GNU General Public License as published by
28534 + * the Free Software Foundation; either version 2 of the License, or
28535 + * (at your option) any later version.
28536 + *
28537 + * This program is distributed in the hope that it will be useful,
28538 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28539 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28540 + * GNU General Public License for more details.
28541 + *
28542 + * You should have received a copy of the GNU General Public License
28543 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28544 + */
28545 +
28546 +/*
28547 + * readdir in userspace.
28548 + */
28549 +
28550 +#include <linux/compat.h>
28551 +#include <linux/fs_stack.h>
28552 +#include <linux/security.h>
28553 +#include "aufs.h"
28554 +
28555 +/* bits for struct aufs_rdu.flags */
28556 +#define        AuRdu_CALLED    1
28557 +#define        AuRdu_CONT      (1 << 1)
28558 +#define        AuRdu_FULL      (1 << 2)
28559 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28560 +#define au_fset_rdu(flags, name) \
28561 +       do { (flags) |= AuRdu_##name; } while (0)
28562 +#define au_fclr_rdu(flags, name) \
28563 +       do { (flags) &= ~AuRdu_##name; } while (0)
28564 +
28565 +struct au_rdu_arg {
28566 +       struct dir_context              ctx;
28567 +       struct aufs_rdu                 *rdu;
28568 +       union au_rdu_ent_ul             ent;
28569 +       unsigned long                   end;
28570 +
28571 +       struct super_block              *sb;
28572 +       int                             err;
28573 +};
28574 +
28575 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28576 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28577 +{
28578 +       int err, len;
28579 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28580 +       struct aufs_rdu *rdu = arg->rdu;
28581 +       struct au_rdu_ent ent;
28582 +
28583 +       err = 0;
28584 +       arg->err = 0;
28585 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28586 +       len = au_rdu_len(nlen);
28587 +       if (arg->ent.ul + len  < arg->end) {
28588 +               ent.ino = h_ino;
28589 +               ent.bindex = rdu->cookie.bindex;
28590 +               ent.type = d_type;
28591 +               ent.nlen = nlen;
28592 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28593 +                       ent.type = DT_UNKNOWN;
28594 +
28595 +               /* unnecessary to support mmap_sem since this is a dir */
28596 +               err = -EFAULT;
28597 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28598 +                       goto out;
28599 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28600 +                       goto out;
28601 +               /* the terminating NULL */
28602 +               if (__put_user(0, arg->ent.e->name + nlen))
28603 +                       goto out;
28604 +               err = 0;
28605 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28606 +               arg->ent.ul += len;
28607 +               rdu->rent++;
28608 +       } else {
28609 +               err = -EFAULT;
28610 +               au_fset_rdu(rdu->cookie.flags, FULL);
28611 +               rdu->full = 1;
28612 +               rdu->tail = arg->ent;
28613 +       }
28614 +
28615 +out:
28616 +       /* AuTraceErr(err); */
28617 +       return err;
28618 +}
28619 +
28620 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28621 +{
28622 +       int err;
28623 +       loff_t offset;
28624 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28625 +
28626 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28627 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28628 +       err = offset;
28629 +       if (unlikely(offset != cookie->h_pos))
28630 +               goto out;
28631 +
28632 +       err = 0;
28633 +       do {
28634 +               arg->err = 0;
28635 +               au_fclr_rdu(cookie->flags, CALLED);
28636 +               /* smp_mb(); */
28637 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28638 +               if (err >= 0)
28639 +                       err = arg->err;
28640 +       } while (!err
28641 +                && au_ftest_rdu(cookie->flags, CALLED)
28642 +                && !au_ftest_rdu(cookie->flags, FULL));
28643 +       cookie->h_pos = h_file->f_pos;
28644 +
28645 +out:
28646 +       AuTraceErr(err);
28647 +       return err;
28648 +}
28649 +
28650 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28651 +{
28652 +       int err;
28653 +       aufs_bindex_t bbot;
28654 +       struct au_rdu_arg arg = {
28655 +               .ctx = {
28656 +                       .actor = au_rdu_fill
28657 +               }
28658 +       };
28659 +       struct dentry *dentry;
28660 +       struct inode *inode;
28661 +       struct file *h_file;
28662 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28663 +
28664 +       /* VERIFY_WRITE */
28665 +       err = !access_ok(rdu->ent.e, rdu->sz);
28666 +       if (unlikely(err)) {
28667 +               err = -EFAULT;
28668 +               AuTraceErr(err);
28669 +               goto out;
28670 +       }
28671 +       rdu->rent = 0;
28672 +       rdu->tail = rdu->ent;
28673 +       rdu->full = 0;
28674 +       arg.rdu = rdu;
28675 +       arg.ent = rdu->ent;
28676 +       arg.end = arg.ent.ul;
28677 +       arg.end += rdu->sz;
28678 +
28679 +       err = -ENOTDIR;
28680 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28681 +               goto out;
28682 +
28683 +       err = security_file_permission(file, MAY_READ);
28684 +       AuTraceErr(err);
28685 +       if (unlikely(err))
28686 +               goto out;
28687 +
28688 +       dentry = file->f_path.dentry;
28689 +       inode = d_inode(dentry);
28690 +       inode_lock_shared(inode);
28691 +
28692 +       arg.sb = inode->i_sb;
28693 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28694 +       if (unlikely(err))
28695 +               goto out_mtx;
28696 +       err = au_alive_dir(dentry);
28697 +       if (unlikely(err))
28698 +               goto out_si;
28699 +       /* todo: reval? */
28700 +       fi_read_lock(file);
28701 +
28702 +       err = -EAGAIN;
28703 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28704 +                    && cookie->generation != au_figen(file)))
28705 +               goto out_unlock;
28706 +
28707 +       err = 0;
28708 +       if (!rdu->blk) {
28709 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28710 +               if (!rdu->blk)
28711 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28712 +       }
28713 +       bbot = au_fbtop(file);
28714 +       if (cookie->bindex < bbot)
28715 +               cookie->bindex = bbot;
28716 +       bbot = au_fbbot_dir(file);
28717 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28718 +       for (; !err && cookie->bindex <= bbot;
28719 +            cookie->bindex++, cookie->h_pos = 0) {
28720 +               h_file = au_hf_dir(file, cookie->bindex);
28721 +               if (!h_file)
28722 +                       continue;
28723 +
28724 +               au_fclr_rdu(cookie->flags, FULL);
28725 +               err = au_rdu_do(h_file, &arg);
28726 +               AuTraceErr(err);
28727 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28728 +                       break;
28729 +       }
28730 +       AuDbg("rent %llu\n", rdu->rent);
28731 +
28732 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28733 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28734 +               au_fset_rdu(cookie->flags, CONT);
28735 +               cookie->generation = au_figen(file);
28736 +       }
28737 +
28738 +       ii_read_lock_child(inode);
28739 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28740 +       ii_read_unlock(inode);
28741 +
28742 +out_unlock:
28743 +       fi_read_unlock(file);
28744 +out_si:
28745 +       si_read_unlock(arg.sb);
28746 +out_mtx:
28747 +       inode_unlock_shared(inode);
28748 +out:
28749 +       AuTraceErr(err);
28750 +       return err;
28751 +}
28752 +
28753 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28754 +{
28755 +       int err;
28756 +       ino_t ino;
28757 +       unsigned long long nent;
28758 +       union au_rdu_ent_ul *u;
28759 +       struct au_rdu_ent ent;
28760 +       struct super_block *sb;
28761 +
28762 +       err = 0;
28763 +       nent = rdu->nent;
28764 +       u = &rdu->ent;
28765 +       sb = file->f_path.dentry->d_sb;
28766 +       si_read_lock(sb, AuLock_FLUSH);
28767 +       while (nent-- > 0) {
28768 +               /* unnecessary to support mmap_sem since this is a dir */
28769 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28770 +               if (!err)
28771 +                       /* VERIFY_WRITE */
28772 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28773 +               if (unlikely(err)) {
28774 +                       err = -EFAULT;
28775 +                       AuTraceErr(err);
28776 +                       break;
28777 +               }
28778 +
28779 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28780 +               if (!ent.wh)
28781 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28782 +               else
28783 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28784 +                                       &ino);
28785 +               if (unlikely(err)) {
28786 +                       AuTraceErr(err);
28787 +                       break;
28788 +               }
28789 +
28790 +               err = __put_user(ino, &u->e->ino);
28791 +               if (unlikely(err)) {
28792 +                       err = -EFAULT;
28793 +                       AuTraceErr(err);
28794 +                       break;
28795 +               }
28796 +               u->ul += au_rdu_len(ent.nlen);
28797 +       }
28798 +       si_read_unlock(sb);
28799 +
28800 +       return err;
28801 +}
28802 +
28803 +/* ---------------------------------------------------------------------- */
28804 +
28805 +static int au_rdu_verify(struct aufs_rdu *rdu)
28806 +{
28807 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28808 +             "%llu, b%d, 0x%x, g%u}\n",
28809 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28810 +             rdu->blk,
28811 +             rdu->rent, rdu->shwh, rdu->full,
28812 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28813 +             rdu->cookie.generation);
28814 +
28815 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28816 +               return 0;
28817 +
28818 +       AuDbg("%u:%u\n",
28819 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28820 +       return -EINVAL;
28821 +}
28822 +
28823 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28824 +{
28825 +       long err, e;
28826 +       struct aufs_rdu rdu;
28827 +       void __user *p = (void __user *)arg;
28828 +
28829 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28830 +       if (unlikely(err)) {
28831 +               err = -EFAULT;
28832 +               AuTraceErr(err);
28833 +               goto out;
28834 +       }
28835 +       err = au_rdu_verify(&rdu);
28836 +       if (unlikely(err))
28837 +               goto out;
28838 +
28839 +       switch (cmd) {
28840 +       case AUFS_CTL_RDU:
28841 +               err = au_rdu(file, &rdu);
28842 +               if (unlikely(err))
28843 +                       break;
28844 +
28845 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28846 +               if (unlikely(e)) {
28847 +                       err = -EFAULT;
28848 +                       AuTraceErr(err);
28849 +               }
28850 +               break;
28851 +       case AUFS_CTL_RDU_INO:
28852 +               err = au_rdu_ino(file, &rdu);
28853 +               break;
28854 +
28855 +       default:
28856 +               /* err = -ENOTTY; */
28857 +               err = -EINVAL;
28858 +       }
28859 +
28860 +out:
28861 +       AuTraceErr(err);
28862 +       return err;
28863 +}
28864 +
28865 +#ifdef CONFIG_COMPAT
28866 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28867 +{
28868 +       long err, e;
28869 +       struct aufs_rdu rdu;
28870 +       void __user *p = compat_ptr(arg);
28871 +
28872 +       /* todo: get_user()? */
28873 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28874 +       if (unlikely(err)) {
28875 +               err = -EFAULT;
28876 +               AuTraceErr(err);
28877 +               goto out;
28878 +       }
28879 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28880 +       err = au_rdu_verify(&rdu);
28881 +       if (unlikely(err))
28882 +               goto out;
28883 +
28884 +       switch (cmd) {
28885 +       case AUFS_CTL_RDU:
28886 +               err = au_rdu(file, &rdu);
28887 +               if (unlikely(err))
28888 +                       break;
28889 +
28890 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28891 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28892 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28893 +               if (unlikely(e)) {
28894 +                       err = -EFAULT;
28895 +                       AuTraceErr(err);
28896 +               }
28897 +               break;
28898 +       case AUFS_CTL_RDU_INO:
28899 +               err = au_rdu_ino(file, &rdu);
28900 +               break;
28901 +
28902 +       default:
28903 +               /* err = -ENOTTY; */
28904 +               err = -EINVAL;
28905 +       }
28906 +
28907 +out:
28908 +       AuTraceErr(err);
28909 +       return err;
28910 +}
28911 +#endif
28912 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28913 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28914 +++ linux/fs/aufs/rwsem.h       2020-01-27 10:57:18.178871751 +0100
28915 @@ -0,0 +1,73 @@
28916 +/* SPDX-License-Identifier: GPL-2.0 */
28917 +/*
28918 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28919 + *
28920 + * This program, aufs is free software; you can redistribute it and/or modify
28921 + * it under the terms of the GNU General Public License as published by
28922 + * the Free Software Foundation; either version 2 of the License, or
28923 + * (at your option) any later version.
28924 + *
28925 + * This program is distributed in the hope that it will be useful,
28926 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28927 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28928 + * GNU General Public License for more details.
28929 + *
28930 + * You should have received a copy of the GNU General Public License
28931 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28932 + */
28933 +
28934 +/*
28935 + * simple read-write semaphore wrappers
28936 + */
28937 +
28938 +#ifndef __AUFS_RWSEM_H__
28939 +#define __AUFS_RWSEM_H__
28940 +
28941 +#ifdef __KERNEL__
28942 +
28943 +#include "debug.h"
28944 +
28945 +/* in the future, the name 'au_rwsem' will be totally gone */
28946 +#define au_rwsem       rw_semaphore
28947 +
28948 +/* to debug easier, do not make them inlined functions */
28949 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
28950 +/* rwsem_is_locked() is unusable */
28951 +#define AuRwMustReadLock(rw)   AuDebugOn(!lockdep_recursing(current) \
28952 +                                         && debug_locks \
28953 +                                         && !lockdep_is_held_type(rw, 1))
28954 +#define AuRwMustWriteLock(rw)  AuDebugOn(!lockdep_recursing(current) \
28955 +                                         && debug_locks \
28956 +                                         && !lockdep_is_held_type(rw, 0))
28957 +#define AuRwMustAnyLock(rw)    AuDebugOn(!lockdep_recursing(current) \
28958 +                                         && debug_locks \
28959 +                                         && !lockdep_is_held(rw))
28960 +#define AuRwDestroy(rw)                AuDebugOn(!lockdep_recursing(current) \
28961 +                                         && debug_locks \
28962 +                                         && lockdep_is_held(rw))
28963 +
28964 +#define au_rw_init(rw) init_rwsem(rw)
28965 +
28966 +#define au_rw_init_wlock(rw) do {              \
28967 +               au_rw_init(rw);                 \
28968 +               down_write(rw);                 \
28969 +       } while (0)
28970 +
28971 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
28972 +               au_rw_init(rw);                 \
28973 +               down_write_nested(rw, lsc);     \
28974 +       } while (0)
28975 +
28976 +#define au_rw_read_lock(rw)            down_read(rw)
28977 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
28978 +#define au_rw_read_unlock(rw)          up_read(rw)
28979 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
28980 +#define au_rw_write_lock(rw)           down_write(rw)
28981 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
28982 +#define au_rw_write_unlock(rw)         up_write(rw)
28983 +/* why is not _nested version defined? */
28984 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
28985 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
28986 +
28987 +#endif /* __KERNEL__ */
28988 +#endif /* __AUFS_RWSEM_H__ */
28989 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
28990 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
28991 +++ linux/fs/aufs/sbinfo.c      2020-01-27 10:57:18.178871751 +0100
28992 @@ -0,0 +1,314 @@
28993 +// SPDX-License-Identifier: GPL-2.0
28994 +/*
28995 + * Copyright (C) 2005-2020 Junjiro R. Okajima
28996 + *
28997 + * This program, aufs is free software; you can redistribute it and/or modify
28998 + * it under the terms of the GNU General Public License as published by
28999 + * the Free Software Foundation; either version 2 of the License, or
29000 + * (at your option) any later version.
29001 + *
29002 + * This program is distributed in the hope that it will be useful,
29003 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29004 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29005 + * GNU General Public License for more details.
29006 + *
29007 + * You should have received a copy of the GNU General Public License
29008 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29009 + */
29010 +
29011 +/*
29012 + * superblock private data
29013 + */
29014 +
29015 +#include <linux/iversion.h>
29016 +#include "aufs.h"
29017 +
29018 +/*
29019 + * they are necessary regardless sysfs is disabled.
29020 + */
29021 +void au_si_free(struct kobject *kobj)
29022 +{
29023 +       int i;
29024 +       struct au_sbinfo *sbinfo;
29025 +       char *locked __maybe_unused; /* debug only */
29026 +
29027 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29028 +       for (i = 0; i < AuPlink_NHASH; i++)
29029 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29030 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29031 +
29032 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29033 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29034 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29035 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29036 +
29037 +       dbgaufs_si_fin(sbinfo);
29038 +       au_rw_write_lock(&sbinfo->si_rwsem);
29039 +       au_br_free(sbinfo);
29040 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29041 +
29042 +       au_kfree_try_rcu(sbinfo->si_branch);
29043 +       mutex_destroy(&sbinfo->si_xib_mtx);
29044 +       AuRwDestroy(&sbinfo->si_rwsem);
29045 +
29046 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29047 +       /* si_nfiles is waited too */
29048 +       au_kfree_rcu(sbinfo);
29049 +}
29050 +
29051 +int au_si_alloc(struct super_block *sb)
29052 +{
29053 +       int err, i;
29054 +       struct au_sbinfo *sbinfo;
29055 +
29056 +       err = -ENOMEM;
29057 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29058 +       if (unlikely(!sbinfo))
29059 +               goto out;
29060 +
29061 +       /* will be reallocated separately */
29062 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29063 +       if (unlikely(!sbinfo->si_branch))
29064 +               goto out_sbinfo;
29065 +
29066 +       err = sysaufs_si_init(sbinfo);
29067 +       if (!err) {
29068 +               dbgaufs_si_null(sbinfo);
29069 +               err = dbgaufs_si_init(sbinfo);
29070 +               if (unlikely(err))
29071 +                       kobject_put(&sbinfo->si_kobj);
29072 +       }
29073 +       if (unlikely(err))
29074 +               goto out_br;
29075 +
29076 +       au_nwt_init(&sbinfo->si_nowait);
29077 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29078 +
29079 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29080 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29081 +
29082 +       sbinfo->si_bbot = -1;
29083 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29084 +
29085 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29086 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29087 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29088 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29089 +
29090 +       au_fhsm_init(sbinfo);
29091 +
29092 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29093 +
29094 +       sbinfo->si_xino_jiffy = jiffies;
29095 +       sbinfo->si_xino_expire
29096 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29097 +       mutex_init(&sbinfo->si_xib_mtx);
29098 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29099 +
29100 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29101 +
29102 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29103 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29104 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29105 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29106 +
29107 +       for (i = 0; i < AuPlink_NHASH; i++)
29108 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29109 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29110 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29111 +
29112 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29113 +
29114 +       /* with getattr by default */
29115 +       sbinfo->si_iop_array = aufs_iop;
29116 +
29117 +       /* leave other members for sysaufs and si_mnt. */
29118 +       sbinfo->si_sb = sb;
29119 +       sb->s_fs_info = sbinfo;
29120 +       si_pid_set(sb);
29121 +       return 0; /* success */
29122 +
29123 +out_br:
29124 +       au_kfree_try_rcu(sbinfo->si_branch);
29125 +out_sbinfo:
29126 +       au_kfree_rcu(sbinfo);
29127 +out:
29128 +       return err;
29129 +}
29130 +
29131 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29132 +{
29133 +       int err, sz;
29134 +       struct au_branch **brp;
29135 +
29136 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29137 +
29138 +       err = -ENOMEM;
29139 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29140 +       if (unlikely(!sz))
29141 +               sz = sizeof(*brp);
29142 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29143 +                          may_shrink);
29144 +       if (brp) {
29145 +               sbinfo->si_branch = brp;
29146 +               err = 0;
29147 +       }
29148 +
29149 +       return err;
29150 +}
29151 +
29152 +/* ---------------------------------------------------------------------- */
29153 +
29154 +unsigned int au_sigen_inc(struct super_block *sb)
29155 +{
29156 +       unsigned int gen;
29157 +       struct inode *inode;
29158 +
29159 +       SiMustWriteLock(sb);
29160 +
29161 +       gen = ++au_sbi(sb)->si_generation;
29162 +       au_update_digen(sb->s_root);
29163 +       inode = d_inode(sb->s_root);
29164 +       au_update_iigen(inode, /*half*/0);
29165 +       inode_inc_iversion(inode);
29166 +       return gen;
29167 +}
29168 +
29169 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29170 +{
29171 +       aufs_bindex_t br_id;
29172 +       int i;
29173 +       struct au_sbinfo *sbinfo;
29174 +
29175 +       SiMustWriteLock(sb);
29176 +
29177 +       sbinfo = au_sbi(sb);
29178 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29179 +               br_id = ++sbinfo->si_last_br_id;
29180 +               AuDebugOn(br_id < 0);
29181 +               if (br_id && au_br_index(sb, br_id) < 0)
29182 +                       return br_id;
29183 +       }
29184 +
29185 +       return -1;
29186 +}
29187 +
29188 +/* ---------------------------------------------------------------------- */
29189 +
29190 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29191 +int si_read_lock(struct super_block *sb, int flags)
29192 +{
29193 +       int err;
29194 +
29195 +       err = 0;
29196 +       if (au_ftest_lock(flags, FLUSH))
29197 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29198 +
29199 +       si_noflush_read_lock(sb);
29200 +       err = au_plink_maint(sb, flags);
29201 +       if (unlikely(err))
29202 +               si_read_unlock(sb);
29203 +
29204 +       return err;
29205 +}
29206 +
29207 +int si_write_lock(struct super_block *sb, int flags)
29208 +{
29209 +       int err;
29210 +
29211 +       if (au_ftest_lock(flags, FLUSH))
29212 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29213 +
29214 +       si_noflush_write_lock(sb);
29215 +       err = au_plink_maint(sb, flags);
29216 +       if (unlikely(err))
29217 +               si_write_unlock(sb);
29218 +
29219 +       return err;
29220 +}
29221 +
29222 +/* dentry and super_block lock. call at entry point */
29223 +int aufs_read_lock(struct dentry *dentry, int flags)
29224 +{
29225 +       int err;
29226 +       struct super_block *sb;
29227 +
29228 +       sb = dentry->d_sb;
29229 +       err = si_read_lock(sb, flags);
29230 +       if (unlikely(err))
29231 +               goto out;
29232 +
29233 +       if (au_ftest_lock(flags, DW))
29234 +               di_write_lock_child(dentry);
29235 +       else
29236 +               di_read_lock_child(dentry, flags);
29237 +
29238 +       if (au_ftest_lock(flags, GEN)) {
29239 +               err = au_digen_test(dentry, au_sigen(sb));
29240 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29241 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29242 +               else if (!err)
29243 +                       err = au_dbrange_test(dentry);
29244 +               if (unlikely(err))
29245 +                       aufs_read_unlock(dentry, flags);
29246 +       }
29247 +
29248 +out:
29249 +       return err;
29250 +}
29251 +
29252 +void aufs_read_unlock(struct dentry *dentry, int flags)
29253 +{
29254 +       if (au_ftest_lock(flags, DW))
29255 +               di_write_unlock(dentry);
29256 +       else
29257 +               di_read_unlock(dentry, flags);
29258 +       si_read_unlock(dentry->d_sb);
29259 +}
29260 +
29261 +void aufs_write_lock(struct dentry *dentry)
29262 +{
29263 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29264 +       di_write_lock_child(dentry);
29265 +}
29266 +
29267 +void aufs_write_unlock(struct dentry *dentry)
29268 +{
29269 +       di_write_unlock(dentry);
29270 +       si_write_unlock(dentry->d_sb);
29271 +}
29272 +
29273 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29274 +{
29275 +       int err;
29276 +       unsigned int sigen;
29277 +       struct super_block *sb;
29278 +
29279 +       sb = d1->d_sb;
29280 +       err = si_read_lock(sb, flags);
29281 +       if (unlikely(err))
29282 +               goto out;
29283 +
29284 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29285 +
29286 +       if (au_ftest_lock(flags, GEN)) {
29287 +               sigen = au_sigen(sb);
29288 +               err = au_digen_test(d1, sigen);
29289 +               AuDebugOn(!err && au_dbrange_test(d1));
29290 +               if (!err) {
29291 +                       err = au_digen_test(d2, sigen);
29292 +                       AuDebugOn(!err && au_dbrange_test(d2));
29293 +               }
29294 +               if (unlikely(err))
29295 +                       aufs_read_and_write_unlock2(d1, d2);
29296 +       }
29297 +
29298 +out:
29299 +       return err;
29300 +}
29301 +
29302 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29303 +{
29304 +       di_write_unlock2(d1, d2);
29305 +       si_read_unlock(d1->d_sb);
29306 +}
29307 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29308 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29309 +++ linux/fs/aufs/super.c       2020-01-27 10:57:18.178871751 +0100
29310 @@ -0,0 +1,1047 @@
29311 +// SPDX-License-Identifier: GPL-2.0
29312 +/*
29313 + * Copyright (C) 2005-2020 Junjiro R. Okajima
29314 + *
29315 + * This program, aufs is free software; you can redistribute it and/or modify
29316 + * it under the terms of the GNU General Public License as published by
29317 + * the Free Software Foundation; either version 2 of the License, or
29318 + * (at your option) any later version.
29319 + *
29320 + * This program is distributed in the hope that it will be useful,
29321 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29322 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29323 + * GNU General Public License for more details.
29324 + *
29325 + * You should have received a copy of the GNU General Public License
29326 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29327 + */
29328 +
29329 +/*
29330 + * mount and super_block operations
29331 + */
29332 +
29333 +#include <linux/iversion.h>
29334 +#include <linux/mm.h>
29335 +#include <linux/seq_file.h>
29336 +#include <linux/statfs.h>
29337 +#include <linux/vmalloc.h>
29338 +#include "aufs.h"
29339 +
29340 +/*
29341 + * super_operations
29342 + */
29343 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29344 +{
29345 +       struct au_icntnr *c;
29346 +
29347 +       c = au_cache_alloc_icntnr();
29348 +       if (c) {
29349 +               au_icntnr_init(c);
29350 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29351 +               c->iinfo.ii_hinode = NULL;
29352 +               return &c->vfs_inode;
29353 +       }
29354 +       return NULL;
29355 +}
29356 +
29357 +static void aufs_destroy_inode(struct inode *inode)
29358 +{
29359 +       if (!au_is_bad_inode(inode))
29360 +               au_iinfo_fin(inode);
29361 +}
29362 +
29363 +static void aufs_free_inode(struct inode *inode)
29364 +{
29365 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29366 +}
29367 +
29368 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29369 +{
29370 +       struct inode *inode;
29371 +       int err;
29372 +
29373 +       inode = iget_locked(sb, ino);
29374 +       if (unlikely(!inode)) {
29375 +               inode = ERR_PTR(-ENOMEM);
29376 +               goto out;
29377 +       }
29378 +       if (!(inode->i_state & I_NEW))
29379 +               goto out;
29380 +
29381 +       err = au_xigen_new(inode);
29382 +       if (!err)
29383 +               err = au_iinfo_init(inode);
29384 +       if (!err)
29385 +               inode_inc_iversion(inode);
29386 +       else {
29387 +               iget_failed(inode);
29388 +               inode = ERR_PTR(err);
29389 +       }
29390 +
29391 +out:
29392 +       /* never return NULL */
29393 +       AuDebugOn(!inode);
29394 +       AuTraceErrPtr(inode);
29395 +       return inode;
29396 +}
29397 +
29398 +/* lock free root dinfo */
29399 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29400 +{
29401 +       int err;
29402 +       aufs_bindex_t bindex, bbot;
29403 +       struct path path;
29404 +       struct au_hdentry *hdp;
29405 +       struct au_branch *br;
29406 +       au_br_perm_str_t perm;
29407 +
29408 +       err = 0;
29409 +       bbot = au_sbbot(sb);
29410 +       bindex = 0;
29411 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29412 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29413 +               br = au_sbr(sb, bindex);
29414 +               path.mnt = au_br_mnt(br);
29415 +               path.dentry = hdp->hd_dentry;
29416 +               err = au_seq_path(seq, &path);
29417 +               if (!err) {
29418 +                       au_optstr_br_perm(&perm, br->br_perm);
29419 +                       seq_printf(seq, "=%s", perm.a);
29420 +                       if (bindex != bbot)
29421 +                               seq_putc(seq, ':');
29422 +               }
29423 +       }
29424 +       if (unlikely(err || seq_has_overflowed(seq)))
29425 +               err = -E2BIG;
29426 +
29427 +       return err;
29428 +}
29429 +
29430 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29431 +                      const char *append)
29432 +{
29433 +       char *p;
29434 +
29435 +       p = fmt;
29436 +       while (*pat != ':')
29437 +               *p++ = *pat++;
29438 +       *p++ = *pat++;
29439 +       strcpy(p, append);
29440 +       AuDebugOn(strlen(fmt) >= len);
29441 +}
29442 +
29443 +static void au_show_wbr_create(struct seq_file *m, int v,
29444 +                              struct au_sbinfo *sbinfo)
29445 +{
29446 +       const char *pat;
29447 +       char fmt[32];
29448 +       struct au_wbr_mfs *mfs;
29449 +
29450 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29451 +
29452 +       seq_puts(m, ",create=");
29453 +       pat = au_optstr_wbr_create(v);
29454 +       mfs = &sbinfo->si_wbr_mfs;
29455 +       switch (v) {
29456 +       case AuWbrCreate_TDP:
29457 +       case AuWbrCreate_RR:
29458 +       case AuWbrCreate_MFS:
29459 +       case AuWbrCreate_PMFS:
29460 +               seq_puts(m, pat);
29461 +               break;
29462 +       case AuWbrCreate_MFSRR:
29463 +       case AuWbrCreate_TDMFS:
29464 +       case AuWbrCreate_PMFSRR:
29465 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29466 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29467 +               break;
29468 +       case AuWbrCreate_MFSV:
29469 +       case AuWbrCreate_PMFSV:
29470 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29471 +               seq_printf(m, fmt,
29472 +                          jiffies_to_msecs(mfs->mfs_expire)
29473 +                          / MSEC_PER_SEC);
29474 +               break;
29475 +       case AuWbrCreate_MFSRRV:
29476 +       case AuWbrCreate_TDMFSV:
29477 +       case AuWbrCreate_PMFSRRV:
29478 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29479 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29480 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29481 +               break;
29482 +       default:
29483 +               BUG();
29484 +       }
29485 +}
29486 +
29487 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29488 +{
29489 +#ifdef CONFIG_SYSFS
29490 +       return 0;
29491 +#else
29492 +       int err;
29493 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29494 +       aufs_bindex_t bindex, brid;
29495 +       struct qstr *name;
29496 +       struct file *f;
29497 +       struct dentry *d, *h_root;
29498 +       struct au_branch *br;
29499 +
29500 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29501 +
29502 +       err = 0;
29503 +       f = au_sbi(sb)->si_xib;
29504 +       if (!f)
29505 +               goto out;
29506 +
29507 +       /* stop printing the default xino path on the first writable branch */
29508 +       h_root = NULL;
29509 +       bindex = au_xi_root(sb, f->f_path.dentry);
29510 +       if (bindex >= 0) {
29511 +               br = au_sbr_sb(sb, bindex);
29512 +               h_root = au_br_dentry(br);
29513 +       }
29514 +
29515 +       d = f->f_path.dentry;
29516 +       name = &d->d_name;
29517 +       /* safe ->d_parent because the file is unlinked */
29518 +       if (d->d_parent == h_root
29519 +           && name->len == len
29520 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29521 +               goto out;
29522 +
29523 +       seq_puts(seq, ",xino=");
29524 +       err = au_xino_path(seq, f);
29525 +
29526 +out:
29527 +       return err;
29528 +#endif
29529 +}
29530 +
29531 +/* seq_file will re-call me in case of too long string */
29532 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29533 +{
29534 +       int err;
29535 +       unsigned int mnt_flags, v;
29536 +       struct super_block *sb;
29537 +       struct au_sbinfo *sbinfo;
29538 +
29539 +#define AuBool(name, str) do { \
29540 +       v = au_opt_test(mnt_flags, name); \
29541 +       if (v != au_opt_test(AuOpt_Def, name)) \
29542 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29543 +} while (0)
29544 +
29545 +#define AuStr(name, str) do { \
29546 +       v = mnt_flags & AuOptMask_##name; \
29547 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29548 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29549 +} while (0)
29550 +
29551 +#define AuUInt(name, str, val) do { \
29552 +       if (val != AUFS_##name##_DEF) \
29553 +               seq_printf(m, "," #str "=%u", val); \
29554 +} while (0)
29555 +
29556 +       sb = dentry->d_sb;
29557 +       if (sb->s_flags & SB_POSIXACL)
29558 +               seq_puts(m, ",acl");
29559 +#if 0 /* reserved for future use */
29560 +       if (sb->s_flags & SB_I_VERSION)
29561 +               seq_puts(m, ",i_version");
29562 +#endif
29563 +
29564 +       /* lock free root dinfo */
29565 +       si_noflush_read_lock(sb);
29566 +       sbinfo = au_sbi(sb);
29567 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29568 +
29569 +       mnt_flags = au_mntflags(sb);
29570 +       if (au_opt_test(mnt_flags, XINO)) {
29571 +               err = au_show_xino(m, sb);
29572 +               if (unlikely(err))
29573 +                       goto out;
29574 +       } else
29575 +               seq_puts(m, ",noxino");
29576 +
29577 +       AuBool(TRUNC_XINO, trunc_xino);
29578 +       AuStr(UDBA, udba);
29579 +       AuBool(SHWH, shwh);
29580 +       AuBool(PLINK, plink);
29581 +       AuBool(DIO, dio);
29582 +       AuBool(DIRPERM1, dirperm1);
29583 +
29584 +       v = sbinfo->si_wbr_create;
29585 +       if (v != AuWbrCreate_Def)
29586 +               au_show_wbr_create(m, v, sbinfo);
29587 +
29588 +       v = sbinfo->si_wbr_copyup;
29589 +       if (v != AuWbrCopyup_Def)
29590 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29591 +
29592 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29593 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29594 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29595 +
29596 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29597 +
29598 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29599 +       AuUInt(RDCACHE, rdcache, v);
29600 +
29601 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29602 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29603 +
29604 +       au_fhsm_show(m, sbinfo);
29605 +
29606 +       AuBool(DIRREN, dirren);
29607 +       AuBool(SUM, sum);
29608 +       /* AuBool(SUM_W, wsum); */
29609 +       AuBool(WARN_PERM, warn_perm);
29610 +       AuBool(VERBOSE, verbose);
29611 +
29612 +out:
29613 +       /* be sure to print "br:" last */
29614 +       if (!sysaufs_brs) {
29615 +               seq_puts(m, ",br:");
29616 +               au_show_brs(m, sb);
29617 +       }
29618 +       si_read_unlock(sb);
29619 +       return 0;
29620 +
29621 +#undef AuBool
29622 +#undef AuStr
29623 +#undef AuUInt
29624 +}
29625 +
29626 +/* ---------------------------------------------------------------------- */
29627 +
29628 +/* sum mode which returns the summation for statfs(2) */
29629 +
29630 +static u64 au_add_till_max(u64 a, u64 b)
29631 +{
29632 +       u64 old;
29633 +
29634 +       old = a;
29635 +       a += b;
29636 +       if (old <= a)
29637 +               return a;
29638 +       return ULLONG_MAX;
29639 +}
29640 +
29641 +static u64 au_mul_till_max(u64 a, long mul)
29642 +{
29643 +       u64 old;
29644 +
29645 +       old = a;
29646 +       a *= mul;
29647 +       if (old <= a)
29648 +               return a;
29649 +       return ULLONG_MAX;
29650 +}
29651 +
29652 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29653 +{
29654 +       int err;
29655 +       long bsize, factor;
29656 +       u64 blocks, bfree, bavail, files, ffree;
29657 +       aufs_bindex_t bbot, bindex, i;
29658 +       unsigned char shared;
29659 +       struct path h_path;
29660 +       struct super_block *h_sb;
29661 +
29662 +       err = 0;
29663 +       bsize = LONG_MAX;
29664 +       files = 0;
29665 +       ffree = 0;
29666 +       blocks = 0;
29667 +       bfree = 0;
29668 +       bavail = 0;
29669 +       bbot = au_sbbot(sb);
29670 +       for (bindex = 0; bindex <= bbot; bindex++) {
29671 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29672 +               h_sb = h_path.mnt->mnt_sb;
29673 +               shared = 0;
29674 +               for (i = 0; !shared && i < bindex; i++)
29675 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29676 +               if (shared)
29677 +                       continue;
29678 +
29679 +               /* sb->s_root for NFS is unreliable */
29680 +               h_path.dentry = h_path.mnt->mnt_root;
29681 +               err = vfs_statfs(&h_path, buf);
29682 +               if (unlikely(err))
29683 +                       goto out;
29684 +
29685 +               if (bsize > buf->f_bsize) {
29686 +                       /*
29687 +                        * we will reduce bsize, so we have to expand blocks
29688 +                        * etc. to match them again
29689 +                        */
29690 +                       factor = (bsize / buf->f_bsize);
29691 +                       blocks = au_mul_till_max(blocks, factor);
29692 +                       bfree = au_mul_till_max(bfree, factor);
29693 +                       bavail = au_mul_till_max(bavail, factor);
29694 +                       bsize = buf->f_bsize;
29695 +               }
29696 +
29697 +               factor = (buf->f_bsize / bsize);
29698 +               blocks = au_add_till_max(blocks,
29699 +                               au_mul_till_max(buf->f_blocks, factor));
29700 +               bfree = au_add_till_max(bfree,
29701 +                               au_mul_till_max(buf->f_bfree, factor));
29702 +               bavail = au_add_till_max(bavail,
29703 +                               au_mul_till_max(buf->f_bavail, factor));
29704 +               files = au_add_till_max(files, buf->f_files);
29705 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29706 +       }
29707 +
29708 +       buf->f_bsize = bsize;
29709 +       buf->f_blocks = blocks;
29710 +       buf->f_bfree = bfree;
29711 +       buf->f_bavail = bavail;
29712 +       buf->f_files = files;
29713 +       buf->f_ffree = ffree;
29714 +       buf->f_frsize = 0;
29715 +
29716 +out:
29717 +       return err;
29718 +}
29719 +
29720 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29721 +{
29722 +       int err;
29723 +       struct path h_path;
29724 +       struct super_block *sb;
29725 +
29726 +       /* lock free root dinfo */
29727 +       sb = dentry->d_sb;
29728 +       si_noflush_read_lock(sb);
29729 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29730 +               /* sb->s_root for NFS is unreliable */
29731 +               h_path.mnt = au_sbr_mnt(sb, 0);
29732 +               h_path.dentry = h_path.mnt->mnt_root;
29733 +               err = vfs_statfs(&h_path, buf);
29734 +       } else
29735 +               err = au_statfs_sum(sb, buf);
29736 +       si_read_unlock(sb);
29737 +
29738 +       if (!err) {
29739 +               buf->f_type = AUFS_SUPER_MAGIC;
29740 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29741 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29742 +       }
29743 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29744 +
29745 +       return err;
29746 +}
29747 +
29748 +/* ---------------------------------------------------------------------- */
29749 +
29750 +static int aufs_sync_fs(struct super_block *sb, int wait)
29751 +{
29752 +       int err, e;
29753 +       aufs_bindex_t bbot, bindex;
29754 +       struct au_branch *br;
29755 +       struct super_block *h_sb;
29756 +
29757 +       err = 0;
29758 +       si_noflush_read_lock(sb);
29759 +       bbot = au_sbbot(sb);
29760 +       for (bindex = 0; bindex <= bbot; bindex++) {
29761 +               br = au_sbr(sb, bindex);
29762 +               if (!au_br_writable(br->br_perm))
29763 +                       continue;
29764 +
29765 +               h_sb = au_sbr_sb(sb, bindex);
29766 +               e = vfsub_sync_filesystem(h_sb, wait);
29767 +               if (unlikely(e && !err))
29768 +                       err = e;
29769 +               /* go on even if an error happens */
29770 +       }
29771 +       si_read_unlock(sb);
29772 +
29773 +       return err;
29774 +}
29775 +
29776 +/* ---------------------------------------------------------------------- */
29777 +
29778 +/* final actions when unmounting a file system */
29779 +static void aufs_put_super(struct super_block *sb)
29780 +{
29781 +       struct au_sbinfo *sbinfo;
29782 +
29783 +       sbinfo = au_sbi(sb);
29784 +       if (sbinfo)
29785 +               kobject_put(&sbinfo->si_kobj);
29786 +}
29787 +
29788 +/* ---------------------------------------------------------------------- */
29789 +
29790 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29791 +                    struct super_block *sb, void *arg)
29792 +{
29793 +       void *array;
29794 +       unsigned long long n, sz;
29795 +
29796 +       array = NULL;
29797 +       n = 0;
29798 +       if (!*hint)
29799 +               goto out;
29800 +
29801 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29802 +               array = ERR_PTR(-EMFILE);
29803 +               pr_err("hint %llu\n", *hint);
29804 +               goto out;
29805 +       }
29806 +
29807 +       sz = sizeof(array) * *hint;
29808 +       array = kzalloc(sz, GFP_NOFS);
29809 +       if (unlikely(!array))
29810 +               array = vzalloc(sz);
29811 +       if (unlikely(!array)) {
29812 +               array = ERR_PTR(-ENOMEM);
29813 +               goto out;
29814 +       }
29815 +
29816 +       n = cb(sb, array, *hint, arg);
29817 +       AuDebugOn(n > *hint);
29818 +
29819 +out:
29820 +       *hint = n;
29821 +       return array;
29822 +}
29823 +
29824 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29825 +                                      unsigned long long max __maybe_unused,
29826 +                                      void *arg)
29827 +{
29828 +       unsigned long long n;
29829 +       struct inode **p, *inode;
29830 +       struct list_head *head;
29831 +
29832 +       n = 0;
29833 +       p = a;
29834 +       head = arg;
29835 +       spin_lock(&sb->s_inode_list_lock);
29836 +       list_for_each_entry(inode, head, i_sb_list) {
29837 +               if (!au_is_bad_inode(inode)
29838 +                   && au_ii(inode)->ii_btop >= 0) {
29839 +                       spin_lock(&inode->i_lock);
29840 +                       if (atomic_read(&inode->i_count)) {
29841 +                               au_igrab(inode);
29842 +                               *p++ = inode;
29843 +                               n++;
29844 +                               AuDebugOn(n > max);
29845 +                       }
29846 +                       spin_unlock(&inode->i_lock);
29847 +               }
29848 +       }
29849 +       spin_unlock(&sb->s_inode_list_lock);
29850 +
29851 +       return n;
29852 +}
29853 +
29854 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29855 +{
29856 +       struct au_sbinfo *sbi;
29857 +
29858 +       sbi = au_sbi(sb);
29859 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29860 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29861 +}
29862 +
29863 +void au_iarray_free(struct inode **a, unsigned long long max)
29864 +{
29865 +       unsigned long long ull;
29866 +
29867 +       for (ull = 0; ull < max; ull++)
29868 +               iput(a[ull]);
29869 +       kvfree(a);
29870 +}
29871 +
29872 +/* ---------------------------------------------------------------------- */
29873 +
29874 +/*
29875 + * refresh dentry and inode at remount time.
29876 + */
29877 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29878 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29879 +                     struct dentry *parent)
29880 +{
29881 +       int err;
29882 +
29883 +       di_write_lock_child(dentry);
29884 +       di_read_lock_parent(parent, AuLock_IR);
29885 +       err = au_refresh_dentry(dentry, parent);
29886 +       if (!err && dir_flags)
29887 +               au_hn_reset(d_inode(dentry), dir_flags);
29888 +       di_read_unlock(parent, AuLock_IR);
29889 +       di_write_unlock(dentry);
29890 +
29891 +       return err;
29892 +}
29893 +
29894 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29895 +                          struct au_sbinfo *sbinfo,
29896 +                          const unsigned int dir_flags, unsigned int do_idop)
29897 +{
29898 +       int err;
29899 +       struct dentry *parent;
29900 +
29901 +       err = 0;
29902 +       parent = dget_parent(dentry);
29903 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29904 +               if (d_really_is_positive(dentry)) {
29905 +                       if (!d_is_dir(dentry))
29906 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29907 +                                                parent);
29908 +                       else {
29909 +                               err = au_do_refresh(dentry, dir_flags, parent);
29910 +                               if (unlikely(err))
29911 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
29912 +                       }
29913 +               } else
29914 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
29915 +               AuDbgDentry(dentry);
29916 +       }
29917 +       dput(parent);
29918 +
29919 +       if (!err) {
29920 +               if (do_idop)
29921 +                       au_refresh_dop(dentry, /*force_reval*/0);
29922 +       } else
29923 +               au_refresh_dop(dentry, /*force_reval*/1);
29924 +
29925 +       AuTraceErr(err);
29926 +       return err;
29927 +}
29928 +
29929 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
29930 +{
29931 +       int err, i, j, ndentry, e;
29932 +       unsigned int sigen;
29933 +       struct au_dcsub_pages dpages;
29934 +       struct au_dpage *dpage;
29935 +       struct dentry **dentries, *d;
29936 +       struct au_sbinfo *sbinfo;
29937 +       struct dentry *root = sb->s_root;
29938 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
29939 +
29940 +       if (do_idop)
29941 +               au_refresh_dop(root, /*force_reval*/0);
29942 +
29943 +       err = au_dpages_init(&dpages, GFP_NOFS);
29944 +       if (unlikely(err))
29945 +               goto out;
29946 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
29947 +       if (unlikely(err))
29948 +               goto out_dpages;
29949 +
29950 +       sigen = au_sigen(sb);
29951 +       sbinfo = au_sbi(sb);
29952 +       for (i = 0; i < dpages.ndpage; i++) {
29953 +               dpage = dpages.dpages + i;
29954 +               dentries = dpage->dentries;
29955 +               ndentry = dpage->ndentry;
29956 +               for (j = 0; j < ndentry; j++) {
29957 +                       d = dentries[j];
29958 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
29959 +                                           do_idop);
29960 +                       if (unlikely(e && !err))
29961 +                               err = e;
29962 +                       /* go on even err */
29963 +               }
29964 +       }
29965 +
29966 +out_dpages:
29967 +       au_dpages_free(&dpages);
29968 +out:
29969 +       return err;
29970 +}
29971 +
29972 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
29973 +{
29974 +       int err, e;
29975 +       unsigned int sigen;
29976 +       unsigned long long max, ull;
29977 +       struct inode *inode, **array;
29978 +
29979 +       array = au_iarray_alloc(sb, &max);
29980 +       err = PTR_ERR(array);
29981 +       if (IS_ERR(array))
29982 +               goto out;
29983 +
29984 +       err = 0;
29985 +       sigen = au_sigen(sb);
29986 +       for (ull = 0; ull < max; ull++) {
29987 +               inode = array[ull];
29988 +               if (unlikely(!inode))
29989 +                       break;
29990 +
29991 +               e = 0;
29992 +               ii_write_lock_child(inode);
29993 +               if (au_iigen(inode, NULL) != sigen) {
29994 +                       e = au_refresh_hinode_self(inode);
29995 +                       if (unlikely(e)) {
29996 +                               au_refresh_iop(inode, /*force_getattr*/1);
29997 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
29998 +                               if (!err)
29999 +                                       err = e;
30000 +                               /* go on even if err */
30001 +                       }
30002 +               }
30003 +               if (!e && do_idop)
30004 +                       au_refresh_iop(inode, /*force_getattr*/0);
30005 +               ii_write_unlock(inode);
30006 +       }
30007 +
30008 +       au_iarray_free(array, max);
30009 +
30010 +out:
30011 +       return err;
30012 +}
30013 +
30014 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30015 +{
30016 +       int err, e;
30017 +       unsigned int udba;
30018 +       aufs_bindex_t bindex, bbot;
30019 +       struct dentry *root;
30020 +       struct inode *inode;
30021 +       struct au_branch *br;
30022 +       struct au_sbinfo *sbi;
30023 +
30024 +       au_sigen_inc(sb);
30025 +       sbi = au_sbi(sb);
30026 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30027 +
30028 +       root = sb->s_root;
30029 +       DiMustNoWaiters(root);
30030 +       inode = d_inode(root);
30031 +       IiMustNoWaiters(inode);
30032 +
30033 +       udba = au_opt_udba(sb);
30034 +       bbot = au_sbbot(sb);
30035 +       for (bindex = 0; bindex <= bbot; bindex++) {
30036 +               br = au_sbr(sb, bindex);
30037 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30038 +               if (unlikely(err))
30039 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30040 +                               bindex, err);
30041 +               /* go on even if err */
30042 +       }
30043 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30044 +
30045 +       if (do_idop) {
30046 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30047 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30048 +                       sb->s_d_op = &aufs_dop_noreval;
30049 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30050 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30051 +               } else {
30052 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30053 +                       sb->s_d_op = &aufs_dop;
30054 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30055 +                       sbi->si_iop_array = aufs_iop;
30056 +               }
30057 +               pr_info("reset to %ps and %ps\n",
30058 +                       sb->s_d_op, sbi->si_iop_array);
30059 +       }
30060 +
30061 +       di_write_unlock(root);
30062 +       err = au_refresh_d(sb, do_idop);
30063 +       e = au_refresh_i(sb, do_idop);
30064 +       if (unlikely(e && !err))
30065 +               err = e;
30066 +       /* aufs_write_lock() calls ..._child() */
30067 +       di_write_lock_child(root);
30068 +
30069 +       au_cpup_attr_all(inode, /*force*/1);
30070 +
30071 +       if (unlikely(err))
30072 +               AuIOErr("refresh failed, ignored, %d\n", err);
30073 +}
30074 +
30075 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30076 +static int cvt_err(int err)
30077 +{
30078 +       AuTraceErr(err);
30079 +
30080 +       switch (err) {
30081 +       case -ENOENT:
30082 +       case -ENOTDIR:
30083 +       case -EEXIST:
30084 +       case -EIO:
30085 +               err = -EINVAL;
30086 +       }
30087 +       return err;
30088 +}
30089 +
30090 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30091 +{
30092 +       int err, do_dx;
30093 +       unsigned int mntflags;
30094 +       struct au_opts opts = {
30095 +               .opt = NULL
30096 +       };
30097 +       struct dentry *root;
30098 +       struct inode *inode;
30099 +       struct au_sbinfo *sbinfo;
30100 +
30101 +       err = 0;
30102 +       root = sb->s_root;
30103 +       if (!data || !*data) {
30104 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30105 +               if (!err) {
30106 +                       di_write_lock_child(root);
30107 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30108 +                       aufs_write_unlock(root);
30109 +               }
30110 +               goto out;
30111 +       }
30112 +
30113 +       err = -ENOMEM;
30114 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30115 +       if (unlikely(!opts.opt))
30116 +               goto out;
30117 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30118 +       opts.flags = AuOpts_REMOUNT;
30119 +       opts.sb_flags = *flags;
30120 +
30121 +       /* parse it before aufs lock */
30122 +       err = au_opts_parse(sb, data, &opts);
30123 +       if (unlikely(err))
30124 +               goto out_opts;
30125 +
30126 +       sbinfo = au_sbi(sb);
30127 +       inode = d_inode(root);
30128 +       inode_lock(inode);
30129 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30130 +       if (unlikely(err))
30131 +               goto out_mtx;
30132 +       di_write_lock_child(root);
30133 +
30134 +       /* au_opts_remount() may return an error */
30135 +       err = au_opts_remount(sb, &opts);
30136 +       au_opts_free(&opts);
30137 +
30138 +       if (au_ftest_opts(opts.flags, REFRESH))
30139 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30140 +
30141 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30142 +               mntflags = au_mntflags(sb);
30143 +               do_dx = !!au_opt_test(mntflags, DIO);
30144 +               au_dy_arefresh(do_dx);
30145 +       }
30146 +
30147 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30148 +       aufs_write_unlock(root);
30149 +
30150 +out_mtx:
30151 +       inode_unlock(inode);
30152 +out_opts:
30153 +       free_page((unsigned long)opts.opt);
30154 +out:
30155 +       err = cvt_err(err);
30156 +       AuTraceErr(err);
30157 +       return err;
30158 +}
30159 +
30160 +static const struct super_operations aufs_sop = {
30161 +       .alloc_inode    = aufs_alloc_inode,
30162 +       .destroy_inode  = aufs_destroy_inode,
30163 +       .free_inode     = aufs_free_inode,
30164 +       /* always deleting, no clearing */
30165 +       .drop_inode     = generic_delete_inode,
30166 +       .show_options   = aufs_show_options,
30167 +       .statfs         = aufs_statfs,
30168 +       .put_super      = aufs_put_super,
30169 +       .sync_fs        = aufs_sync_fs,
30170 +       .remount_fs     = aufs_remount_fs
30171 +};
30172 +
30173 +/* ---------------------------------------------------------------------- */
30174 +
30175 +static int alloc_root(struct super_block *sb)
30176 +{
30177 +       int err;
30178 +       struct inode *inode;
30179 +       struct dentry *root;
30180 +
30181 +       err = -ENOMEM;
30182 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30183 +       err = PTR_ERR(inode);
30184 +       if (IS_ERR(inode))
30185 +               goto out;
30186 +
30187 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30188 +       inode->i_fop = &aufs_dir_fop;
30189 +       inode->i_mode = S_IFDIR;
30190 +       set_nlink(inode, 2);
30191 +       unlock_new_inode(inode);
30192 +
30193 +       root = d_make_root(inode);
30194 +       if (unlikely(!root))
30195 +               goto out;
30196 +       err = PTR_ERR(root);
30197 +       if (IS_ERR(root))
30198 +               goto out;
30199 +
30200 +       err = au_di_init(root);
30201 +       if (!err) {
30202 +               sb->s_root = root;
30203 +               return 0; /* success */
30204 +       }
30205 +       dput(root);
30206 +
30207 +out:
30208 +       return err;
30209 +}
30210 +
30211 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30212 +                          int silent __maybe_unused)
30213 +{
30214 +       int err;
30215 +       struct au_opts opts = {
30216 +               .opt = NULL
30217 +       };
30218 +       struct au_sbinfo *sbinfo;
30219 +       struct dentry *root;
30220 +       struct inode *inode;
30221 +       char *arg = raw_data;
30222 +
30223 +       if (unlikely(!arg || !*arg)) {
30224 +               err = -EINVAL;
30225 +               pr_err("no arg\n");
30226 +               goto out;
30227 +       }
30228 +
30229 +       err = -ENOMEM;
30230 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30231 +       if (unlikely(!opts.opt))
30232 +               goto out;
30233 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30234 +       opts.sb_flags = sb->s_flags;
30235 +
30236 +       err = au_si_alloc(sb);
30237 +       if (unlikely(err))
30238 +               goto out_opts;
30239 +       sbinfo = au_sbi(sb);
30240 +
30241 +       /* all timestamps always follow the ones on the branch */
30242 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30243 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30244 +       sb->s_op = &aufs_sop;
30245 +       sb->s_d_op = &aufs_dop;
30246 +       sb->s_magic = AUFS_SUPER_MAGIC;
30247 +       sb->s_maxbytes = 0;
30248 +       sb->s_stack_depth = 1;
30249 +       au_export_init(sb);
30250 +       au_xattr_init(sb);
30251 +
30252 +       err = alloc_root(sb);
30253 +       if (unlikely(err)) {
30254 +               si_write_unlock(sb);
30255 +               goto out_info;
30256 +       }
30257 +       root = sb->s_root;
30258 +       inode = d_inode(root);
30259 +
30260 +       /*
30261 +        * actually we can parse options regardless aufs lock here.
30262 +        * but at remount time, parsing must be done before aufs lock.
30263 +        * so we follow the same rule.
30264 +        */
30265 +       ii_write_lock_parent(inode);
30266 +       aufs_write_unlock(root);
30267 +       err = au_opts_parse(sb, arg, &opts);
30268 +       if (unlikely(err))
30269 +               goto out_root;
30270 +
30271 +       /* lock vfs_inode first, then aufs. */
30272 +       inode_lock(inode);
30273 +       aufs_write_lock(root);
30274 +       err = au_opts_mount(sb, &opts);
30275 +       au_opts_free(&opts);
30276 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30277 +               sb->s_d_op = &aufs_dop_noreval;
30278 +               pr_info("%ps\n", sb->s_d_op);
30279 +               au_refresh_dop(root, /*force_reval*/0);
30280 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30281 +               au_refresh_iop(inode, /*force_getattr*/0);
30282 +       }
30283 +       aufs_write_unlock(root);
30284 +       inode_unlock(inode);
30285 +       if (!err)
30286 +               goto out_opts; /* success */
30287 +
30288 +out_root:
30289 +       dput(root);
30290 +       sb->s_root = NULL;
30291 +out_info:
30292 +       kobject_put(&sbinfo->si_kobj);
30293 +       sb->s_fs_info = NULL;
30294 +out_opts:
30295 +       free_page((unsigned long)opts.opt);
30296 +out:
30297 +       AuTraceErr(err);
30298 +       err = cvt_err(err);
30299 +       AuTraceErr(err);
30300 +       return err;
30301 +}
30302 +
30303 +/* ---------------------------------------------------------------------- */
30304 +
30305 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30306 +                                const char *dev_name __maybe_unused,
30307 +                                void *raw_data)
30308 +{
30309 +       struct dentry *root;
30310 +
30311 +       /* all timestamps always follow the ones on the branch */
30312 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30313 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30314 +       if (IS_ERR(root))
30315 +               goto out;
30316 +
30317 +       au_sbilist_add(root->d_sb);
30318 +
30319 +out:
30320 +       return root;
30321 +}
30322 +
30323 +static void aufs_kill_sb(struct super_block *sb)
30324 +{
30325 +       struct au_sbinfo *sbinfo;
30326 +
30327 +       sbinfo = au_sbi(sb);
30328 +       if (sbinfo) {
30329 +               au_sbilist_del(sb);
30330 +               aufs_write_lock(sb->s_root);
30331 +               au_fhsm_fin(sb);
30332 +               if (sbinfo->si_wbr_create_ops->fin)
30333 +                       sbinfo->si_wbr_create_ops->fin(sb);
30334 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30335 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30336 +                       au_remount_refresh(sb, /*do_idop*/0);
30337 +               }
30338 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30339 +                       au_plink_put(sb, /*verbose*/1);
30340 +               au_xino_clr(sb);
30341 +               au_dr_opt_flush(sb);
30342 +               sbinfo->si_sb = NULL;
30343 +               aufs_write_unlock(sb->s_root);
30344 +               au_nwt_flush(&sbinfo->si_nowait);
30345 +       }
30346 +       kill_anon_super(sb);
30347 +}
30348 +
30349 +struct file_system_type aufs_fs_type = {
30350 +       .name           = AUFS_FSTYPE,
30351 +       /* a race between rename and others */
30352 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
30353 +       .mount          = aufs_mount,
30354 +       .kill_sb        = aufs_kill_sb,
30355 +       /* no need to __module_get() and module_put(). */
30356 +       .owner          = THIS_MODULE,
30357 +};
30358 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30359 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30360 +++ linux/fs/aufs/super.h       2020-12-15 14:10:58.918023162 +0100
30361 @@ -0,0 +1,587 @@
30362 +/* SPDX-License-Identifier: GPL-2.0 */
30363 +/*
30364 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30365 + *
30366 + * This program, aufs is free software; you can redistribute it and/or modify
30367 + * it under the terms of the GNU General Public License as published by
30368 + * the Free Software Foundation; either version 2 of the License, or
30369 + * (at your option) any later version.
30370 + *
30371 + * This program is distributed in the hope that it will be useful,
30372 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30373 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30374 + * GNU General Public License for more details.
30375 + *
30376 + * You should have received a copy of the GNU General Public License
30377 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30378 + */
30379 +
30380 +/*
30381 + * super_block operations
30382 + */
30383 +
30384 +#ifndef __AUFS_SUPER_H__
30385 +#define __AUFS_SUPER_H__
30386 +
30387 +#ifdef __KERNEL__
30388 +
30389 +#include <linux/fs.h>
30390 +#include <linux/kobject.h>
30391 +#include "hbl.h"
30392 +#include "lcnt.h"
30393 +#include "rwsem.h"
30394 +#include "wkq.h"
30395 +
30396 +/* policies to select one among multiple writable branches */
30397 +struct au_wbr_copyup_operations {
30398 +       int (*copyup)(struct dentry *dentry);
30399 +};
30400 +
30401 +#define AuWbr_DIR      1               /* target is a dir */
30402 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30403 +
30404 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30405 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30406 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30407 +
30408 +struct au_wbr_create_operations {
30409 +       int (*create)(struct dentry *dentry, unsigned int flags);
30410 +       int (*init)(struct super_block *sb);
30411 +       int (*fin)(struct super_block *sb);
30412 +};
30413 +
30414 +struct au_wbr_mfs {
30415 +       struct mutex    mfs_lock; /* protect this structure */
30416 +       unsigned long   mfs_jiffy;
30417 +       unsigned long   mfs_expire;
30418 +       aufs_bindex_t   mfs_bindex;
30419 +
30420 +       unsigned long long      mfsrr_bytes;
30421 +       unsigned long long      mfsrr_watermark;
30422 +};
30423 +
30424 +#define AuPlink_NHASH 100
30425 +static inline int au_plink_hash(ino_t ino)
30426 +{
30427 +       return ino % AuPlink_NHASH;
30428 +}
30429 +
30430 +/* File-based Hierarchical Storage Management */
30431 +struct au_fhsm {
30432 +#ifdef CONFIG_AUFS_FHSM
30433 +       /* allow only one process who can receive the notification */
30434 +       spinlock_t              fhsm_spin;
30435 +       pid_t                   fhsm_pid;
30436 +       wait_queue_head_t       fhsm_wqh;
30437 +       atomic_t                fhsm_readable;
30438 +
30439 +       /* these are protected by si_rwsem */
30440 +       unsigned long           fhsm_expire;
30441 +       aufs_bindex_t           fhsm_bottom;
30442 +#endif
30443 +};
30444 +
30445 +struct au_branch;
30446 +struct au_sbinfo {
30447 +       /* nowait tasks in the system-wide workqueue */
30448 +       struct au_nowait_tasks  si_nowait;
30449 +
30450 +       /*
30451 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30452 +        * rwsem for au_sbinfo is necessary.
30453 +        */
30454 +       struct au_rwsem         si_rwsem;
30455 +
30456 +       /*
30457 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30458 +        * remount.
30459 +        */
30460 +       au_lcnt_t               si_ninodes, si_nfiles;
30461 +
30462 +       /* branch management */
30463 +       unsigned int            si_generation;
30464 +
30465 +       /* see AuSi_ flags */
30466 +       unsigned char           au_si_status;
30467 +
30468 +       aufs_bindex_t           si_bbot;
30469 +
30470 +       /* dirty trick to keep br_id plus */
30471 +       unsigned int            si_last_br_id :
30472 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30473 +       struct au_branch        **si_branch;
30474 +
30475 +       /* policy to select a writable branch */
30476 +       unsigned char           si_wbr_copyup;
30477 +       unsigned char           si_wbr_create;
30478 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30479 +       struct au_wbr_create_operations *si_wbr_create_ops;
30480 +
30481 +       /* round robin */
30482 +       atomic_t                si_wbr_rr_next;
30483 +
30484 +       /* most free space */
30485 +       struct au_wbr_mfs       si_wbr_mfs;
30486 +
30487 +       /* File-based Hierarchical Storage Management */
30488 +       struct au_fhsm          si_fhsm;
30489 +
30490 +       /* mount flags */
30491 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30492 +       unsigned int            si_mntflags;
30493 +
30494 +       /* external inode number (bitmap and translation table) */
30495 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30496 +
30497 +       struct file             *si_xib;
30498 +       struct mutex            si_xib_mtx; /* protect xib members */
30499 +       unsigned long           *si_xib_buf;
30500 +       unsigned long           si_xib_last_pindex;
30501 +       int                     si_xib_next_bit;
30502 +
30503 +       unsigned long           si_xino_jiffy;
30504 +       unsigned long           si_xino_expire;
30505 +       /* reserved for future use */
30506 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30507 +
30508 +#ifdef CONFIG_AUFS_EXPORT
30509 +       /* i_generation */
30510 +       /* todo: make xigen file an array to support many inode numbers */
30511 +       struct file             *si_xigen;
30512 +       atomic_t                si_xigen_next;
30513 +#endif
30514 +
30515 +       /* dirty trick to support atomic_open */
30516 +       struct hlist_bl_head    si_aopen;
30517 +
30518 +       /* vdir parameters */
30519 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30520 +       unsigned int            si_rdblk;       /* deblk size */
30521 +       unsigned int            si_rdhash;      /* hash size */
30522 +
30523 +       /*
30524 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30525 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30526 +        * future fsck.aufs or kernel thread will remove them later.
30527 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30528 +        */
30529 +       unsigned int            si_dirwh;
30530 +
30531 +       /* pseudo_link list */
30532 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30533 +       wait_queue_head_t       si_plink_wq;
30534 +       spinlock_t              si_plink_maint_lock;
30535 +       pid_t                   si_plink_maint_pid;
30536 +
30537 +       /* file list */
30538 +       struct hlist_bl_head    si_files;
30539 +
30540 +       /* with/without getattr, brother of sb->s_d_op */
30541 +       const struct inode_operations *si_iop_array;
30542 +
30543 +       /*
30544 +        * sysfs and lifetime management.
30545 +        * this is not a small structure and it may be a waste of memory in case
30546 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30547 +        * but using sysfs is majority.
30548 +        */
30549 +       struct kobject          si_kobj;
30550 +#ifdef CONFIG_DEBUG_FS
30551 +       struct dentry            *si_dbgaufs;
30552 +       struct dentry            *si_dbgaufs_plink;
30553 +       struct dentry            *si_dbgaufs_xib;
30554 +#ifdef CONFIG_AUFS_EXPORT
30555 +       struct dentry            *si_dbgaufs_xigen;
30556 +#endif
30557 +#endif
30558 +
30559 +#ifdef CONFIG_AUFS_SBILIST
30560 +       struct hlist_bl_node    si_list;
30561 +#endif
30562 +
30563 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30564 +       struct super_block      *si_sb;
30565 +};
30566 +
30567 +/* sbinfo status flags */
30568 +/*
30569 + * set true when refresh_dirs() failed at remount time.
30570 + * then try refreshing dirs at access time again.
30571 + * if it is false, refreshing dirs at access time is unnecessary
30572 + */
30573 +#define AuSi_FAILED_REFRESH_DIR        1
30574 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30575 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30576 +
30577 +#ifndef CONFIG_AUFS_FHSM
30578 +#undef AuSi_FHSM
30579 +#define AuSi_FHSM              0
30580 +#endif
30581 +
30582 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30583 +                                          unsigned int flag)
30584 +{
30585 +       AuRwMustAnyLock(&sbi->si_rwsem);
30586 +       return sbi->au_si_status & flag;
30587 +}
30588 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30589 +#define au_fset_si(sbinfo, name) do { \
30590 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30591 +       (sbinfo)->au_si_status |= AuSi_##name; \
30592 +} while (0)
30593 +#define au_fclr_si(sbinfo, name) do { \
30594 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30595 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30596 +} while (0)
30597 +
30598 +/* ---------------------------------------------------------------------- */
30599 +
30600 +/* policy to select one among writable branches */
30601 +#define AuWbrCopyup(sbinfo, ...) \
30602 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30603 +#define AuWbrCreate(sbinfo, ...) \
30604 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30605 +
30606 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30607 +#define AuLock_DW              1               /* write-lock dentry */
30608 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30609 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30610 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30611 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30612 +                                               /* except RENAME_EXCHANGE */
30613 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30614 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30615 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30616 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30617 +#define au_fset_lock(flags, name) \
30618 +       do { (flags) |= AuLock_##name; } while (0)
30619 +#define au_fclr_lock(flags, name) \
30620 +       do { (flags) &= ~AuLock_##name; } while (0)
30621 +
30622 +/* ---------------------------------------------------------------------- */
30623 +
30624 +/* super.c */
30625 +extern struct file_system_type aufs_fs_type;
30626 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30627 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30628 +                                          unsigned long long max, void *arg);
30629 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30630 +                    struct super_block *sb, void *arg);
30631 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30632 +void au_iarray_free(struct inode **a, unsigned long long max);
30633 +
30634 +/* sbinfo.c */
30635 +void au_si_free(struct kobject *kobj);
30636 +int au_si_alloc(struct super_block *sb);
30637 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30638 +
30639 +unsigned int au_sigen_inc(struct super_block *sb);
30640 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30641 +
30642 +int si_read_lock(struct super_block *sb, int flags);
30643 +int si_write_lock(struct super_block *sb, int flags);
30644 +int aufs_read_lock(struct dentry *dentry, int flags);
30645 +void aufs_read_unlock(struct dentry *dentry, int flags);
30646 +void aufs_write_lock(struct dentry *dentry);
30647 +void aufs_write_unlock(struct dentry *dentry);
30648 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30649 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30650 +
30651 +/* wbr_policy.c */
30652 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30653 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30654 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30655 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30656 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30657 +
30658 +/* mvdown.c */
30659 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30660 +
30661 +#ifdef CONFIG_AUFS_FHSM
30662 +/* fhsm.c */
30663 +
30664 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30665 +{
30666 +       pid_t pid;
30667 +
30668 +       spin_lock(&fhsm->fhsm_spin);
30669 +       pid = fhsm->fhsm_pid;
30670 +       spin_unlock(&fhsm->fhsm_spin);
30671 +
30672 +       return pid;
30673 +}
30674 +
30675 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30676 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30677 +int au_fhsm_fd(struct super_block *sb, int oflags);
30678 +int au_fhsm_br_alloc(struct au_branch *br);
30679 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30680 +void au_fhsm_fin(struct super_block *sb);
30681 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30682 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30683 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30684 +#else
30685 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30686 +          int force)
30687 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30688 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30689 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30690 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30691 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30692 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30693 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30694 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30695 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30696 +#endif
30697 +
30698 +/* ---------------------------------------------------------------------- */
30699 +
30700 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30701 +{
30702 +       return sb->s_fs_info;
30703 +}
30704 +
30705 +/* ---------------------------------------------------------------------- */
30706 +
30707 +#ifdef CONFIG_AUFS_EXPORT
30708 +int au_test_nfsd(void);
30709 +void au_export_init(struct super_block *sb);
30710 +void au_xigen_inc(struct inode *inode);
30711 +int au_xigen_new(struct inode *inode);
30712 +int au_xigen_set(struct super_block *sb, struct path *path);
30713 +void au_xigen_clr(struct super_block *sb);
30714 +
30715 +static inline int au_busy_or_stale(void)
30716 +{
30717 +       if (!au_test_nfsd())
30718 +               return -EBUSY;
30719 +       return -ESTALE;
30720 +}
30721 +#else
30722 +AuStubInt0(au_test_nfsd, void)
30723 +AuStubVoid(au_export_init, struct super_block *sb)
30724 +AuStubVoid(au_xigen_inc, struct inode *inode)
30725 +AuStubInt0(au_xigen_new, struct inode *inode)
30726 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30727 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30728 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30729 +#endif /* CONFIG_AUFS_EXPORT */
30730 +
30731 +/* ---------------------------------------------------------------------- */
30732 +
30733 +#ifdef CONFIG_AUFS_SBILIST
30734 +/* module.c */
30735 +extern struct hlist_bl_head au_sbilist;
30736 +
30737 +static inline void au_sbilist_init(void)
30738 +{
30739 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30740 +}
30741 +
30742 +static inline void au_sbilist_add(struct super_block *sb)
30743 +{
30744 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30745 +}
30746 +
30747 +static inline void au_sbilist_del(struct super_block *sb)
30748 +{
30749 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30750 +}
30751 +
30752 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30753 +static inline void au_sbilist_lock(void)
30754 +{
30755 +       hlist_bl_lock(&au_sbilist);
30756 +}
30757 +
30758 +static inline void au_sbilist_unlock(void)
30759 +{
30760 +       hlist_bl_unlock(&au_sbilist);
30761 +}
30762 +#define AuGFP_SBILIST  GFP_ATOMIC
30763 +#else
30764 +AuStubVoid(au_sbilist_lock, void)
30765 +AuStubVoid(au_sbilist_unlock, void)
30766 +#define AuGFP_SBILIST  GFP_NOFS
30767 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30768 +#else
30769 +AuStubVoid(au_sbilist_init, void)
30770 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30771 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30772 +AuStubVoid(au_sbilist_lock, void)
30773 +AuStubVoid(au_sbilist_unlock, void)
30774 +#define AuGFP_SBILIST  GFP_NOFS
30775 +#endif
30776 +
30777 +/* ---------------------------------------------------------------------- */
30778 +
30779 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30780 +{
30781 +       /*
30782 +        * This function is a dynamic '__init' function actually,
30783 +        * so the tiny check for si_rwsem is unnecessary.
30784 +        */
30785 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30786 +#ifdef CONFIG_DEBUG_FS
30787 +       sbinfo->si_dbgaufs = NULL;
30788 +       sbinfo->si_dbgaufs_plink = NULL;
30789 +       sbinfo->si_dbgaufs_xib = NULL;
30790 +#ifdef CONFIG_AUFS_EXPORT
30791 +       sbinfo->si_dbgaufs_xigen = NULL;
30792 +#endif
30793 +#endif
30794 +}
30795 +
30796 +/* ---------------------------------------------------------------------- */
30797 +
30798 +/* current->atomic_flags */
30799 +/* this value should never corrupt the ones defined in linux/sched.h */
30800 +#define PFA_AUFS       0x10
30801 +
30802 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30803 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30804 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30805 +
30806 +static inline int si_pid_test(struct super_block *sb)
30807 +{
30808 +       return !!task_test_aufs(current);
30809 +}
30810 +
30811 +static inline void si_pid_clr(struct super_block *sb)
30812 +{
30813 +       AuDebugOn(!task_test_aufs(current));
30814 +       task_clear_aufs(current);
30815 +}
30816 +
30817 +static inline void si_pid_set(struct super_block *sb)
30818 +{
30819 +       AuDebugOn(task_test_aufs(current));
30820 +       task_set_aufs(current);
30821 +}
30822 +
30823 +/* ---------------------------------------------------------------------- */
30824 +
30825 +/* lock superblock. mainly for entry point functions */
30826 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30827 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30828 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30829 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30830 +/*
30831 +#define __si_read_trylock_nested(sb) \
30832 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30833 +#define __si_write_trylock_nested(sb) \
30834 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30835 +*/
30836 +
30837 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30838 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30839 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30840 +
30841 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30842 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30843 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30844 +
30845 +static inline void si_noflush_read_lock(struct super_block *sb)
30846 +{
30847 +       __si_read_lock(sb);
30848 +       si_pid_set(sb);
30849 +}
30850 +
30851 +static inline int si_noflush_read_trylock(struct super_block *sb)
30852 +{
30853 +       int locked;
30854 +
30855 +       locked = __si_read_trylock(sb);
30856 +       if (locked)
30857 +               si_pid_set(sb);
30858 +       return locked;
30859 +}
30860 +
30861 +static inline void si_noflush_write_lock(struct super_block *sb)
30862 +{
30863 +       __si_write_lock(sb);
30864 +       si_pid_set(sb);
30865 +}
30866 +
30867 +static inline int si_noflush_write_trylock(struct super_block *sb)
30868 +{
30869 +       int locked;
30870 +
30871 +       locked = __si_write_trylock(sb);
30872 +       if (locked)
30873 +               si_pid_set(sb);
30874 +       return locked;
30875 +}
30876 +
30877 +#if 0 /* reserved */
30878 +static inline int si_read_trylock(struct super_block *sb, int flags)
30879 +{
30880 +       if (au_ftest_lock(flags, FLUSH))
30881 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30882 +       return si_noflush_read_trylock(sb);
30883 +}
30884 +#endif
30885 +
30886 +static inline void si_read_unlock(struct super_block *sb)
30887 +{
30888 +       si_pid_clr(sb);
30889 +       __si_read_unlock(sb);
30890 +}
30891 +
30892 +#if 0 /* reserved */
30893 +static inline int si_write_trylock(struct super_block *sb, int flags)
30894 +{
30895 +       if (au_ftest_lock(flags, FLUSH))
30896 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30897 +       return si_noflush_write_trylock(sb);
30898 +}
30899 +#endif
30900 +
30901 +static inline void si_write_unlock(struct super_block *sb)
30902 +{
30903 +       si_pid_clr(sb);
30904 +       __si_write_unlock(sb);
30905 +}
30906 +
30907 +#if 0 /* reserved */
30908 +static inline void si_downgrade_lock(struct super_block *sb)
30909 +{
30910 +       __si_downgrade_lock(sb);
30911 +}
30912 +#endif
30913 +
30914 +/* ---------------------------------------------------------------------- */
30915 +
30916 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
30917 +{
30918 +       SiMustAnyLock(sb);
30919 +       return au_sbi(sb)->si_bbot;
30920 +}
30921 +
30922 +static inline unsigned int au_mntflags(struct super_block *sb)
30923 +{
30924 +       SiMustAnyLock(sb);
30925 +       return au_sbi(sb)->si_mntflags;
30926 +}
30927 +
30928 +static inline unsigned int au_sigen(struct super_block *sb)
30929 +{
30930 +       SiMustAnyLock(sb);
30931 +       return au_sbi(sb)->si_generation;
30932 +}
30933 +
30934 +static inline struct au_branch *au_sbr(struct super_block *sb,
30935 +                                      aufs_bindex_t bindex)
30936 +{
30937 +       SiMustAnyLock(sb);
30938 +       return au_sbi(sb)->si_branch[0 + bindex];
30939 +}
30940 +
30941 +static inline loff_t au_xi_maxent(struct super_block *sb)
30942 +{
30943 +       SiMustAnyLock(sb);
30944 +       return au_sbi(sb)->si_ximaxent;
30945 +}
30946 +
30947 +#endif /* __KERNEL__ */
30948 +#endif /* __AUFS_SUPER_H__ */
30949 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
30950 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
30951 +++ linux/fs/aufs/sysaufs.c     2020-01-27 10:57:18.178871751 +0100
30952 @@ -0,0 +1,93 @@
30953 +// SPDX-License-Identifier: GPL-2.0
30954 +/*
30955 + * Copyright (C) 2005-2020 Junjiro R. Okajima
30956 + *
30957 + * This program, aufs is free software; you can redistribute it and/or modify
30958 + * it under the terms of the GNU General Public License as published by
30959 + * the Free Software Foundation; either version 2 of the License, or
30960 + * (at your option) any later version.
30961 + *
30962 + * This program is distributed in the hope that it will be useful,
30963 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30964 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30965 + * GNU General Public License for more details.
30966 + *
30967 + * You should have received a copy of the GNU General Public License
30968 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30969 + */
30970 +
30971 +/*
30972 + * sysfs interface and lifetime management
30973 + * they are necessary regardless sysfs is disabled.
30974 + */
30975 +
30976 +#include <linux/random.h>
30977 +#include "aufs.h"
30978 +
30979 +unsigned long sysaufs_si_mask;
30980 +struct kset *sysaufs_kset;
30981 +
30982 +#define AuSiAttr(_name) { \
30983 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
30984 +       .show   = sysaufs_si_##_name,                           \
30985 +}
30986 +
30987 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
30988 +struct attribute *sysaufs_si_attrs[] = {
30989 +       &sysaufs_si_attr_xi_path.attr,
30990 +       NULL,
30991 +};
30992 +
30993 +static const struct sysfs_ops au_sbi_ops = {
30994 +       .show   = sysaufs_si_show
30995 +};
30996 +
30997 +static struct kobj_type au_sbi_ktype = {
30998 +       .release        = au_si_free,
30999 +       .sysfs_ops      = &au_sbi_ops,
31000 +       .default_attrs  = sysaufs_si_attrs
31001 +};
31002 +
31003 +/* ---------------------------------------------------------------------- */
31004 +
31005 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31006 +{
31007 +       int err;
31008 +
31009 +       sbinfo->si_kobj.kset = sysaufs_kset;
31010 +       /* cf. sysaufs_name() */
31011 +       err = kobject_init_and_add
31012 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31013 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31014 +
31015 +       return err;
31016 +}
31017 +
31018 +void sysaufs_fin(void)
31019 +{
31020 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31021 +       kset_unregister(sysaufs_kset);
31022 +}
31023 +
31024 +int __init sysaufs_init(void)
31025 +{
31026 +       int err;
31027 +
31028 +       do {
31029 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31030 +       } while (!sysaufs_si_mask);
31031 +
31032 +       err = -EINVAL;
31033 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31034 +       if (unlikely(!sysaufs_kset))
31035 +               goto out;
31036 +       err = PTR_ERR(sysaufs_kset);
31037 +       if (IS_ERR(sysaufs_kset))
31038 +               goto out;
31039 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31040 +       if (unlikely(err))
31041 +               kset_unregister(sysaufs_kset);
31042 +
31043 +out:
31044 +       return err;
31045 +}
31046 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31047 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31048 +++ linux/fs/aufs/sysaufs.h     2020-01-27 10:57:18.178871751 +0100
31049 @@ -0,0 +1,102 @@
31050 +/* SPDX-License-Identifier: GPL-2.0 */
31051 +/*
31052 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31053 + *
31054 + * This program, aufs is free software; you can redistribute it and/or modify
31055 + * it under the terms of the GNU General Public License as published by
31056 + * the Free Software Foundation; either version 2 of the License, or
31057 + * (at your option) any later version.
31058 + *
31059 + * This program is distributed in the hope that it will be useful,
31060 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31061 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31062 + * GNU General Public License for more details.
31063 + *
31064 + * You should have received a copy of the GNU General Public License
31065 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31066 + */
31067 +
31068 +/*
31069 + * sysfs interface and mount lifetime management
31070 + */
31071 +
31072 +#ifndef __SYSAUFS_H__
31073 +#define __SYSAUFS_H__
31074 +
31075 +#ifdef __KERNEL__
31076 +
31077 +#include <linux/sysfs.h>
31078 +#include "module.h"
31079 +
31080 +struct super_block;
31081 +struct au_sbinfo;
31082 +
31083 +struct sysaufs_si_attr {
31084 +       struct attribute attr;
31085 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31086 +};
31087 +
31088 +/* ---------------------------------------------------------------------- */
31089 +
31090 +/* sysaufs.c */
31091 +extern unsigned long sysaufs_si_mask;
31092 +extern struct kset *sysaufs_kset;
31093 +extern struct attribute *sysaufs_si_attrs[];
31094 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31095 +int __init sysaufs_init(void);
31096 +void sysaufs_fin(void);
31097 +
31098 +/* ---------------------------------------------------------------------- */
31099 +
31100 +/* some people doesn't like to show a pointer in kernel */
31101 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31102 +{
31103 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31104 +}
31105 +
31106 +#define SysaufsSiNamePrefix    "si_"
31107 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31108 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31109 +{
31110 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31111 +                sysaufs_si_id(sbinfo));
31112 +}
31113 +
31114 +struct au_branch;
31115 +#ifdef CONFIG_SYSFS
31116 +/* sysfs.c */
31117 +extern struct attribute_group *sysaufs_attr_group;
31118 +
31119 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31120 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31121 +                        char *buf);
31122 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31123 +#ifdef CONFIG_COMPAT
31124 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31125 +#endif
31126 +
31127 +void sysaufs_br_init(struct au_branch *br);
31128 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31129 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31130 +
31131 +#define sysaufs_brs_init()     do {} while (0)
31132 +
31133 +#else
31134 +#define sysaufs_attr_group     NULL
31135 +
31136 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31137 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31138 +       struct attribute *attr, char *buf)
31139 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31140 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31141 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31142 +
31143 +static inline void sysaufs_brs_init(void)
31144 +{
31145 +       sysaufs_brs = 0;
31146 +}
31147 +
31148 +#endif /* CONFIG_SYSFS */
31149 +
31150 +#endif /* __KERNEL__ */
31151 +#endif /* __SYSAUFS_H__ */
31152 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31153 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31154 +++ linux/fs/aufs/sysfs.c       2020-01-27 10:57:18.178871751 +0100
31155 @@ -0,0 +1,374 @@
31156 +// SPDX-License-Identifier: GPL-2.0
31157 +/*
31158 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31159 + *
31160 + * This program, aufs is free software; you can redistribute it and/or modify
31161 + * it under the terms of the GNU General Public License as published by
31162 + * the Free Software Foundation; either version 2 of the License, or
31163 + * (at your option) any later version.
31164 + *
31165 + * This program is distributed in the hope that it will be useful,
31166 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31167 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31168 + * GNU General Public License for more details.
31169 + *
31170 + * You should have received a copy of the GNU General Public License
31171 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31172 + */
31173 +
31174 +/*
31175 + * sysfs interface
31176 + */
31177 +
31178 +#include <linux/compat.h>
31179 +#include <linux/seq_file.h>
31180 +#include "aufs.h"
31181 +
31182 +#ifdef CONFIG_AUFS_FS_MODULE
31183 +/* this entry violates the "one line per file" policy of sysfs */
31184 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31185 +                          char *buf)
31186 +{
31187 +       ssize_t err;
31188 +       static char *conf =
31189 +/* this file is generated at compiling */
31190 +#include "conf.str"
31191 +               ;
31192 +
31193 +       err = snprintf(buf, PAGE_SIZE, conf);
31194 +       if (unlikely(err >= PAGE_SIZE))
31195 +               err = -EFBIG;
31196 +       return err;
31197 +}
31198 +
31199 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31200 +#endif
31201 +
31202 +static struct attribute *au_attr[] = {
31203 +#ifdef CONFIG_AUFS_FS_MODULE
31204 +       &au_config_attr.attr,
31205 +#endif
31206 +       NULL,   /* need to NULL terminate the list of attributes */
31207 +};
31208 +
31209 +static struct attribute_group sysaufs_attr_group_body = {
31210 +       .attrs = au_attr
31211 +};
31212 +
31213 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31214 +
31215 +/* ---------------------------------------------------------------------- */
31216 +
31217 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31218 +{
31219 +       int err;
31220 +
31221 +       SiMustAnyLock(sb);
31222 +
31223 +       err = 0;
31224 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31225 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31226 +               seq_putc(seq, '\n');
31227 +       }
31228 +       return err;
31229 +}
31230 +
31231 +/*
31232 + * the lifetime of branch is independent from the entry under sysfs.
31233 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31234 + * unlinked.
31235 + */
31236 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31237 +                        aufs_bindex_t bindex, int idx)
31238 +{
31239 +       int err;
31240 +       struct path path;
31241 +       struct dentry *root;
31242 +       struct au_branch *br;
31243 +       au_br_perm_str_t perm;
31244 +
31245 +       AuDbg("b%d\n", bindex);
31246 +
31247 +       err = 0;
31248 +       root = sb->s_root;
31249 +       di_read_lock_parent(root, !AuLock_IR);
31250 +       br = au_sbr(sb, bindex);
31251 +
31252 +       switch (idx) {
31253 +       case AuBrSysfs_BR:
31254 +               path.mnt = au_br_mnt(br);
31255 +               path.dentry = au_h_dptr(root, bindex);
31256 +               err = au_seq_path(seq, &path);
31257 +               if (!err) {
31258 +                       au_optstr_br_perm(&perm, br->br_perm);
31259 +                       seq_printf(seq, "=%s\n", perm.a);
31260 +               }
31261 +               break;
31262 +       case AuBrSysfs_BRID:
31263 +               seq_printf(seq, "%d\n", br->br_id);
31264 +               break;
31265 +       }
31266 +       di_read_unlock(root, !AuLock_IR);
31267 +       if (unlikely(err || seq_has_overflowed(seq)))
31268 +               err = -E2BIG;
31269 +
31270 +       return err;
31271 +}
31272 +
31273 +/* ---------------------------------------------------------------------- */
31274 +
31275 +static struct seq_file *au_seq(char *p, ssize_t len)
31276 +{
31277 +       struct seq_file *seq;
31278 +
31279 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31280 +       if (seq) {
31281 +               /* mutex_init(&seq.lock); */
31282 +               seq->buf = p;
31283 +               seq->size = len;
31284 +               return seq; /* success */
31285 +       }
31286 +
31287 +       seq = ERR_PTR(-ENOMEM);
31288 +       return seq;
31289 +}
31290 +
31291 +#define SysaufsBr_PREFIX       "br"
31292 +#define SysaufsBrid_PREFIX     "brid"
31293 +
31294 +/* todo: file size may exceed PAGE_SIZE */
31295 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31296 +                       char *buf)
31297 +{
31298 +       ssize_t err;
31299 +       int idx;
31300 +       long l;
31301 +       aufs_bindex_t bbot;
31302 +       struct au_sbinfo *sbinfo;
31303 +       struct super_block *sb;
31304 +       struct seq_file *seq;
31305 +       char *name;
31306 +       struct attribute **cattr;
31307 +
31308 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31309 +       sb = sbinfo->si_sb;
31310 +
31311 +       /*
31312 +        * prevent a race condition between sysfs and aufs.
31313 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31314 +        * prohibits maintaining the sysfs entries.
31315 +        * hew we acquire read lock after sysfs_get_active_two().
31316 +        * on the other hand, the remount process may maintain the sysfs/aufs
31317 +        * entries after acquiring write lock.
31318 +        * it can cause a deadlock.
31319 +        * simply we gave up processing read here.
31320 +        */
31321 +       err = -EBUSY;
31322 +       if (unlikely(!si_noflush_read_trylock(sb)))
31323 +               goto out;
31324 +
31325 +       seq = au_seq(buf, PAGE_SIZE);
31326 +       err = PTR_ERR(seq);
31327 +       if (IS_ERR(seq))
31328 +               goto out_unlock;
31329 +
31330 +       name = (void *)attr->name;
31331 +       cattr = sysaufs_si_attrs;
31332 +       while (*cattr) {
31333 +               if (!strcmp(name, (*cattr)->name)) {
31334 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31335 +                               ->show(seq, sb);
31336 +                       goto out_seq;
31337 +               }
31338 +               cattr++;
31339 +       }
31340 +
31341 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31342 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31343 +               idx = AuBrSysfs_BRID;
31344 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31345 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31346 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31347 +               idx = AuBrSysfs_BR;
31348 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31349 +       } else
31350 +                 BUG();
31351 +
31352 +       err = kstrtol(name, 10, &l);
31353 +       if (!err) {
31354 +               bbot = au_sbbot(sb);
31355 +               if (l <= bbot)
31356 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31357 +               else
31358 +                       err = -ENOENT;
31359 +       }
31360 +
31361 +out_seq:
31362 +       if (!err) {
31363 +               err = seq->count;
31364 +               /* sysfs limit */
31365 +               if (unlikely(err == PAGE_SIZE))
31366 +                       err = -EFBIG;
31367 +       }
31368 +       au_kfree_rcu(seq);
31369 +out_unlock:
31370 +       si_read_unlock(sb);
31371 +out:
31372 +       return err;
31373 +}
31374 +
31375 +/* ---------------------------------------------------------------------- */
31376 +
31377 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31378 +{
31379 +       int err;
31380 +       int16_t brid;
31381 +       aufs_bindex_t bindex, bbot;
31382 +       size_t sz;
31383 +       char *buf;
31384 +       struct seq_file *seq;
31385 +       struct au_branch *br;
31386 +
31387 +       si_read_lock(sb, AuLock_FLUSH);
31388 +       bbot = au_sbbot(sb);
31389 +       err = bbot + 1;
31390 +       if (!arg)
31391 +               goto out;
31392 +
31393 +       err = -ENOMEM;
31394 +       buf = (void *)__get_free_page(GFP_NOFS);
31395 +       if (unlikely(!buf))
31396 +               goto out;
31397 +
31398 +       seq = au_seq(buf, PAGE_SIZE);
31399 +       err = PTR_ERR(seq);
31400 +       if (IS_ERR(seq))
31401 +               goto out_buf;
31402 +
31403 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31404 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31405 +               /* VERIFY_WRITE */
31406 +               err = !access_ok(arg, sizeof(*arg));
31407 +               if (unlikely(err))
31408 +                       break;
31409 +
31410 +               br = au_sbr(sb, bindex);
31411 +               brid = br->br_id;
31412 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31413 +               err = __put_user(brid, &arg->id);
31414 +               if (unlikely(err))
31415 +                       break;
31416 +
31417 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31418 +               err = __put_user(br->br_perm, &arg->perm);
31419 +               if (unlikely(err))
31420 +                       break;
31421 +
31422 +               err = au_seq_path(seq, &br->br_path);
31423 +               if (unlikely(err))
31424 +                       break;
31425 +               seq_putc(seq, '\0');
31426 +               if (!seq_has_overflowed(seq)) {
31427 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31428 +                       seq->count = 0;
31429 +                       if (unlikely(err))
31430 +                               break;
31431 +               } else {
31432 +                       err = -E2BIG;
31433 +                       goto out_seq;
31434 +               }
31435 +       }
31436 +       if (unlikely(err))
31437 +               err = -EFAULT;
31438 +
31439 +out_seq:
31440 +       au_kfree_rcu(seq);
31441 +out_buf:
31442 +       free_page((unsigned long)buf);
31443 +out:
31444 +       si_read_unlock(sb);
31445 +       return err;
31446 +}
31447 +
31448 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31449 +{
31450 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31451 +}
31452 +
31453 +#ifdef CONFIG_COMPAT
31454 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31455 +{
31456 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31457 +}
31458 +#endif
31459 +
31460 +/* ---------------------------------------------------------------------- */
31461 +
31462 +void sysaufs_br_init(struct au_branch *br)
31463 +{
31464 +       int i;
31465 +       struct au_brsysfs *br_sysfs;
31466 +       struct attribute *attr;
31467 +
31468 +       br_sysfs = br->br_sysfs;
31469 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31470 +               attr = &br_sysfs->attr;
31471 +               sysfs_attr_init(attr);
31472 +               attr->name = br_sysfs->name;
31473 +               attr->mode = 0444;
31474 +               br_sysfs++;
31475 +       }
31476 +}
31477 +
31478 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31479 +{
31480 +       struct au_branch *br;
31481 +       struct kobject *kobj;
31482 +       struct au_brsysfs *br_sysfs;
31483 +       int i;
31484 +       aufs_bindex_t bbot;
31485 +
31486 +       if (!sysaufs_brs)
31487 +               return;
31488 +
31489 +       kobj = &au_sbi(sb)->si_kobj;
31490 +       bbot = au_sbbot(sb);
31491 +       for (; bindex <= bbot; bindex++) {
31492 +               br = au_sbr(sb, bindex);
31493 +               br_sysfs = br->br_sysfs;
31494 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31495 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31496 +                       br_sysfs++;
31497 +               }
31498 +       }
31499 +}
31500 +
31501 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31502 +{
31503 +       int err, i;
31504 +       aufs_bindex_t bbot;
31505 +       struct kobject *kobj;
31506 +       struct au_branch *br;
31507 +       struct au_brsysfs *br_sysfs;
31508 +
31509 +       if (!sysaufs_brs)
31510 +               return;
31511 +
31512 +       kobj = &au_sbi(sb)->si_kobj;
31513 +       bbot = au_sbbot(sb);
31514 +       for (; bindex <= bbot; bindex++) {
31515 +               br = au_sbr(sb, bindex);
31516 +               br_sysfs = br->br_sysfs;
31517 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31518 +                        SysaufsBr_PREFIX "%d", bindex);
31519 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31520 +                        SysaufsBrid_PREFIX "%d", bindex);
31521 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31522 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31523 +                       if (unlikely(err))
31524 +                               pr_warn("failed %s under sysfs(%d)\n",
31525 +                                       br_sysfs->name, err);
31526 +                       br_sysfs++;
31527 +               }
31528 +       }
31529 +}
31530 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31531 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31532 +++ linux/fs/aufs/sysrq.c       2020-01-27 10:57:18.178871751 +0100
31533 @@ -0,0 +1,149 @@
31534 +// SPDX-License-Identifier: GPL-2.0
31535 +/*
31536 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31537 + *
31538 + * This program, aufs is free software; you can redistribute it and/or modify
31539 + * it under the terms of the GNU General Public License as published by
31540 + * the Free Software Foundation; either version 2 of the License, or
31541 + * (at your option) any later version.
31542 + *
31543 + * This program is distributed in the hope that it will be useful,
31544 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31545 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31546 + * GNU General Public License for more details.
31547 + *
31548 + * You should have received a copy of the GNU General Public License
31549 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31550 + */
31551 +
31552 +/*
31553 + * magic sysrq handler
31554 + */
31555 +
31556 +/* #include <linux/sysrq.h> */
31557 +#include <linux/writeback.h>
31558 +#include "aufs.h"
31559 +
31560 +/* ---------------------------------------------------------------------- */
31561 +
31562 +static void sysrq_sb(struct super_block *sb)
31563 +{
31564 +       char *plevel;
31565 +       struct au_sbinfo *sbinfo;
31566 +       struct file *file;
31567 +       struct hlist_bl_head *files;
31568 +       struct hlist_bl_node *pos;
31569 +       struct au_finfo *finfo;
31570 +       struct inode *i;
31571 +
31572 +       plevel = au_plevel;
31573 +       au_plevel = KERN_WARNING;
31574 +
31575 +       /* since we define pr_fmt, call printk directly */
31576 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31577 +
31578 +       sbinfo = au_sbi(sb);
31579 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31580 +       pr("superblock\n");
31581 +       au_dpri_sb(sb);
31582 +
31583 +#if 0 /* reserved */
31584 +       do {
31585 +               int err, i, j, ndentry;
31586 +               struct au_dcsub_pages dpages;
31587 +               struct au_dpage *dpage;
31588 +
31589 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31590 +               if (unlikely(err))
31591 +                       break;
31592 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31593 +               if (!err)
31594 +                       for (i = 0; i < dpages.ndpage; i++) {
31595 +                               dpage = dpages.dpages + i;
31596 +                               ndentry = dpage->ndentry;
31597 +                               for (j = 0; j < ndentry; j++)
31598 +                                       au_dpri_dentry(dpage->dentries[j]);
31599 +                       }
31600 +               au_dpages_free(&dpages);
31601 +       } while (0);
31602 +#endif
31603 +
31604 +       pr("isolated inode\n");
31605 +       spin_lock(&sb->s_inode_list_lock);
31606 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31607 +               spin_lock(&i->i_lock);
31608 +               if (hlist_empty(&i->i_dentry))
31609 +                       au_dpri_inode(i);
31610 +               spin_unlock(&i->i_lock);
31611 +       }
31612 +       spin_unlock(&sb->s_inode_list_lock);
31613 +
31614 +       pr("files\n");
31615 +       files = &au_sbi(sb)->si_files;
31616 +       hlist_bl_lock(files);
31617 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31618 +               umode_t mode;
31619 +
31620 +               file = finfo->fi_file;
31621 +               mode = file_inode(file)->i_mode;
31622 +               if (!special_file(mode))
31623 +                       au_dpri_file(file);
31624 +       }
31625 +       hlist_bl_unlock(files);
31626 +       pr("done\n");
31627 +
31628 +#undef pr
31629 +       au_plevel = plevel;
31630 +}
31631 +
31632 +/* ---------------------------------------------------------------------- */
31633 +
31634 +/* module parameter */
31635 +static char *aufs_sysrq_key = "a";
31636 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31637 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31638 +
31639 +static void au_sysrq(int key __maybe_unused)
31640 +{
31641 +       struct au_sbinfo *sbinfo;
31642 +       struct hlist_bl_node *pos;
31643 +
31644 +       lockdep_off();
31645 +       au_sbilist_lock();
31646 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31647 +               sysrq_sb(sbinfo->si_sb);
31648 +       au_sbilist_unlock();
31649 +       lockdep_on();
31650 +}
31651 +
31652 +static struct sysrq_key_op au_sysrq_op = {
31653 +       .handler        = au_sysrq,
31654 +       .help_msg       = "Aufs",
31655 +       .action_msg     = "Aufs",
31656 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31657 +};
31658 +
31659 +/* ---------------------------------------------------------------------- */
31660 +
31661 +int __init au_sysrq_init(void)
31662 +{
31663 +       int err;
31664 +       char key;
31665 +
31666 +       err = -1;
31667 +       key = *aufs_sysrq_key;
31668 +       if ('a' <= key && key <= 'z')
31669 +               err = register_sysrq_key(key, &au_sysrq_op);
31670 +       if (unlikely(err))
31671 +               pr_err("err %d, sysrq=%c\n", err, key);
31672 +       return err;
31673 +}
31674 +
31675 +void au_sysrq_fin(void)
31676 +{
31677 +       int err;
31678 +
31679 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31680 +       if (unlikely(err))
31681 +               pr_err("err %d (ignored)\n", err);
31682 +}
31683 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31684 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31685 +++ linux/fs/aufs/vdir.c        2020-01-27 10:57:18.178871751 +0100
31686 @@ -0,0 +1,896 @@
31687 +// SPDX-License-Identifier: GPL-2.0
31688 +/*
31689 + * Copyright (C) 2005-2020 Junjiro R. Okajima
31690 + *
31691 + * This program, aufs is free software; you can redistribute it and/or modify
31692 + * it under the terms of the GNU General Public License as published by
31693 + * the Free Software Foundation; either version 2 of the License, or
31694 + * (at your option) any later version.
31695 + *
31696 + * This program is distributed in the hope that it will be useful,
31697 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31698 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31699 + * GNU General Public License for more details.
31700 + *
31701 + * You should have received a copy of the GNU General Public License
31702 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31703 + */
31704 +
31705 +/*
31706 + * virtual or vertical directory
31707 + */
31708 +
31709 +#include <linux/iversion.h>
31710 +#include "aufs.h"
31711 +
31712 +static unsigned int calc_size(int nlen)
31713 +{
31714 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31715 +}
31716 +
31717 +static int set_deblk_end(union au_vdir_deblk_p *p,
31718 +                        union au_vdir_deblk_p *deblk_end)
31719 +{
31720 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31721 +               p->de->de_str.len = 0;
31722 +               /* smp_mb(); */
31723 +               return 0;
31724 +       }
31725 +       return -1; /* error */
31726 +}
31727 +
31728 +/* returns true or false */
31729 +static int is_deblk_end(union au_vdir_deblk_p *p,
31730 +                       union au_vdir_deblk_p *deblk_end)
31731 +{
31732 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31733 +               return !p->de->de_str.len;
31734 +       return 1;
31735 +}
31736 +
31737 +static unsigned char *last_deblk(struct au_vdir *vdir)
31738 +{
31739 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31740 +}
31741 +
31742 +/* ---------------------------------------------------------------------- */
31743 +
31744 +/* estimate the appropriate size for name hash table */
31745 +unsigned int au_rdhash_est(loff_t sz)
31746 +{
31747 +       unsigned int n;
31748 +
31749 +       n = UINT_MAX;
31750 +       sz >>= 10;
31751 +       if (sz < n)
31752 +               n = sz;
31753 +       if (sz < AUFS_RDHASH_DEF)
31754 +               n = AUFS_RDHASH_DEF;
31755 +       /* pr_info("n %u\n", n); */
31756 +       return n;
31757 +}
31758 +
31759 +/*
31760 + * the allocated memory has to be freed by
31761 + * au_nhash_wh_free() or au_nhash_de_free().
31762 + */
31763 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31764 +{
31765 +       struct hlist_head *head;
31766 +       unsigned int u;
31767 +       size_t sz;
31768 +
31769 +       sz = sizeof(*nhash->nh_head) * num_hash;
31770 +       head = kmalloc(sz, gfp);
31771 +       if (head) {
31772 +               nhash->nh_num = num_hash;
31773 +               nhash->nh_head = head;
31774 +               for (u = 0; u < num_hash; u++)
31775 +                       INIT_HLIST_HEAD(head++);
31776 +               return 0; /* success */
31777 +       }
31778 +
31779 +       return -ENOMEM;
31780 +}
31781 +
31782 +static void nhash_count(struct hlist_head *head)
31783 +{
31784 +#if 0 /* debugging */
31785 +       unsigned long n;
31786 +       struct hlist_node *pos;
31787 +
31788 +       n = 0;
31789 +       hlist_for_each(pos, head)
31790 +               n++;
31791 +       pr_info("%lu\n", n);
31792 +#endif
31793 +}
31794 +
31795 +static void au_nhash_wh_do_free(struct hlist_head *head)
31796 +{
31797 +       struct au_vdir_wh *pos;
31798 +       struct hlist_node *node;
31799 +
31800 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31801 +               au_kfree_rcu(pos);
31802 +}
31803 +
31804 +static void au_nhash_de_do_free(struct hlist_head *head)
31805 +{
31806 +       struct au_vdir_dehstr *pos;
31807 +       struct hlist_node *node;
31808 +
31809 +       hlist_for_each_entry_safe(pos, node, head, hash)
31810 +               au_cache_free_vdir_dehstr(pos);
31811 +}
31812 +
31813 +static void au_nhash_do_free(struct au_nhash *nhash,
31814 +                            void (*free)(struct hlist_head *head))
31815 +{
31816 +       unsigned int n;
31817 +       struct hlist_head *head;
31818 +
31819 +       n = nhash->nh_num;
31820 +       if (!n)
31821 +               return;
31822 +
31823 +       head = nhash->nh_head;
31824 +       while (n-- > 0) {
31825 +               nhash_count(head);
31826 +               free(head++);
31827 +       }
31828 +       au_kfree_try_rcu(nhash->nh_head);
31829 +}
31830 +
31831 +void au_nhash_wh_free(struct au_nhash *whlist)
31832 +{
31833 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31834 +}
31835 +
31836 +static void au_nhash_de_free(struct au_nhash *delist)
31837 +{
31838 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31839 +}
31840 +
31841 +/* ---------------------------------------------------------------------- */
31842 +
31843 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31844 +                           int limit)
31845 +{
31846 +       int num;
31847 +       unsigned int u, n;
31848 +       struct hlist_head *head;
31849 +       struct au_vdir_wh *pos;
31850 +
31851 +       num = 0;
31852 +       n = whlist->nh_num;
31853 +       head = whlist->nh_head;
31854 +       for (u = 0; u < n; u++, head++)
31855 +               hlist_for_each_entry(pos, head, wh_hash)
31856 +                       if (pos->wh_bindex == btgt && ++num > limit)
31857 +                               return 1;
31858 +       return 0;
31859 +}
31860 +
31861 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31862 +                                      unsigned char *name,
31863 +                                      unsigned int len)
31864 +{
31865 +       unsigned int v;
31866 +       /* const unsigned int magic_bit = 12; */
31867 +
31868 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31869 +
31870 +       v = 0;
31871 +       if (len > 8)
31872 +               len = 8;
31873 +       while (len--)
31874 +               v += *name++;
31875 +       /* v = hash_long(v, magic_bit); */
31876 +       v %= nhash->nh_num;
31877 +       return nhash->nh_head + v;
31878 +}
31879 +
31880 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31881 +                             int nlen)
31882 +{
31883 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31884 +}
31885 +
31886 +/* returns found or not */
31887 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31888 +{
31889 +       struct hlist_head *head;
31890 +       struct au_vdir_wh *pos;
31891 +       struct au_vdir_destr *str;
31892 +
31893 +       head = au_name_hash(whlist, name, nlen);
31894 +       hlist_for_each_entry(pos, head, wh_hash) {
31895 +               str = &pos->wh_str;
31896 +               AuDbg("%.*s\n", str->len, str->name);
31897 +               if (au_nhash_test_name(str, name, nlen))
31898 +                       return 1;
31899 +       }
31900 +       return 0;
31901 +}
31902 +
31903 +/* returns found(true) or not */
31904 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31905 +{
31906 +       struct hlist_head *head;
31907 +       struct au_vdir_dehstr *pos;
31908 +       struct au_vdir_destr *str;
31909 +
31910 +       head = au_name_hash(delist, name, nlen);
31911 +       hlist_for_each_entry(pos, head, hash) {
31912 +               str = pos->str;
31913 +               AuDbg("%.*s\n", str->len, str->name);
31914 +               if (au_nhash_test_name(str, name, nlen))
31915 +                       return 1;
31916 +       }
31917 +       return 0;
31918 +}
31919 +
31920 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
31921 +                           unsigned char d_type)
31922 +{
31923 +#ifdef CONFIG_AUFS_SHWH
31924 +       wh->wh_ino = ino;
31925 +       wh->wh_type = d_type;
31926 +#endif
31927 +}
31928 +
31929 +/* ---------------------------------------------------------------------- */
31930 +
31931 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
31932 +                      unsigned int d_type, aufs_bindex_t bindex,
31933 +                      unsigned char shwh)
31934 +{
31935 +       int err;
31936 +       struct au_vdir_destr *str;
31937 +       struct au_vdir_wh *wh;
31938 +
31939 +       AuDbg("%.*s\n", nlen, name);
31940 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
31941 +
31942 +       err = -ENOMEM;
31943 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
31944 +       if (unlikely(!wh))
31945 +               goto out;
31946 +
31947 +       err = 0;
31948 +       wh->wh_bindex = bindex;
31949 +       if (shwh)
31950 +               au_shwh_init_wh(wh, ino, d_type);
31951 +       str = &wh->wh_str;
31952 +       str->len = nlen;
31953 +       memcpy(str->name, name, nlen);
31954 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
31955 +       /* smp_mb(); */
31956 +
31957 +out:
31958 +       return err;
31959 +}
31960 +
31961 +static int append_deblk(struct au_vdir *vdir)
31962 +{
31963 +       int err;
31964 +       unsigned long ul;
31965 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31966 +       union au_vdir_deblk_p p, deblk_end;
31967 +       unsigned char **o;
31968 +
31969 +       err = -ENOMEM;
31970 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
31971 +                       GFP_NOFS, /*may_shrink*/0);
31972 +       if (unlikely(!o))
31973 +               goto out;
31974 +
31975 +       vdir->vd_deblk = o;
31976 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
31977 +       if (p.deblk) {
31978 +               ul = vdir->vd_nblk++;
31979 +               vdir->vd_deblk[ul] = p.deblk;
31980 +               vdir->vd_last.ul = ul;
31981 +               vdir->vd_last.p.deblk = p.deblk;
31982 +               deblk_end.deblk = p.deblk + deblk_sz;
31983 +               err = set_deblk_end(&p, &deblk_end);
31984 +       }
31985 +
31986 +out:
31987 +       return err;
31988 +}
31989 +
31990 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
31991 +                    unsigned int d_type, struct au_nhash *delist)
31992 +{
31993 +       int err;
31994 +       unsigned int sz;
31995 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
31996 +       union au_vdir_deblk_p p, *room, deblk_end;
31997 +       struct au_vdir_dehstr *dehstr;
31998 +
31999 +       p.deblk = last_deblk(vdir);
32000 +       deblk_end.deblk = p.deblk + deblk_sz;
32001 +       room = &vdir->vd_last.p;
32002 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32003 +                 || !is_deblk_end(room, &deblk_end));
32004 +
32005 +       sz = calc_size(nlen);
32006 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32007 +               err = append_deblk(vdir);
32008 +               if (unlikely(err))
32009 +                       goto out;
32010 +
32011 +               p.deblk = last_deblk(vdir);
32012 +               deblk_end.deblk = p.deblk + deblk_sz;
32013 +               /* smp_mb(); */
32014 +               AuDebugOn(room->deblk != p.deblk);
32015 +       }
32016 +
32017 +       err = -ENOMEM;
32018 +       dehstr = au_cache_alloc_vdir_dehstr();
32019 +       if (unlikely(!dehstr))
32020 +               goto out;
32021 +
32022 +       dehstr->str = &room->de->de_str;
32023 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32024 +       room->de->de_ino = ino;
32025 +       room->de->de_type = d_type;
32026 +       room->de->de_str.len = nlen;
32027 +       memcpy(room->de->de_str.name, name, nlen);
32028 +
32029 +       err = 0;
32030 +       room->deblk += sz;
32031 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32032 +               err = append_deblk(vdir);
32033 +       /* smp_mb(); */
32034 +
32035 +out:
32036 +       return err;
32037 +}
32038 +
32039 +/* ---------------------------------------------------------------------- */
32040 +
32041 +void au_vdir_free(struct au_vdir *vdir)
32042 +{
32043 +       unsigned char **deblk;
32044 +
32045 +       deblk = vdir->vd_deblk;
32046 +       while (vdir->vd_nblk--)
32047 +               au_kfree_try_rcu(*deblk++);
32048 +       au_kfree_try_rcu(vdir->vd_deblk);
32049 +       au_cache_free_vdir(vdir);
32050 +}
32051 +
32052 +static struct au_vdir *alloc_vdir(struct file *file)
32053 +{
32054 +       struct au_vdir *vdir;
32055 +       struct super_block *sb;
32056 +       int err;
32057 +
32058 +       sb = file->f_path.dentry->d_sb;
32059 +       SiMustAnyLock(sb);
32060 +
32061 +       err = -ENOMEM;
32062 +       vdir = au_cache_alloc_vdir();
32063 +       if (unlikely(!vdir))
32064 +               goto out;
32065 +
32066 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32067 +       if (unlikely(!vdir->vd_deblk))
32068 +               goto out_free;
32069 +
32070 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32071 +       if (!vdir->vd_deblk_sz) {
32072 +               /* estimate the appropriate size for deblk */
32073 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32074 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32075 +       }
32076 +       vdir->vd_nblk = 0;
32077 +       vdir->vd_version = 0;
32078 +       vdir->vd_jiffy = 0;
32079 +       err = append_deblk(vdir);
32080 +       if (!err)
32081 +               return vdir; /* success */
32082 +
32083 +       au_kfree_try_rcu(vdir->vd_deblk);
32084 +
32085 +out_free:
32086 +       au_cache_free_vdir(vdir);
32087 +out:
32088 +       vdir = ERR_PTR(err);
32089 +       return vdir;
32090 +}
32091 +
32092 +static int reinit_vdir(struct au_vdir *vdir)
32093 +{
32094 +       int err;
32095 +       union au_vdir_deblk_p p, deblk_end;
32096 +
32097 +       while (vdir->vd_nblk > 1) {
32098 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32099 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32100 +               vdir->vd_nblk--;
32101 +       }
32102 +       p.deblk = vdir->vd_deblk[0];
32103 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32104 +       err = set_deblk_end(&p, &deblk_end);
32105 +       /* keep vd_dblk_sz */
32106 +       vdir->vd_last.ul = 0;
32107 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32108 +       vdir->vd_version = 0;
32109 +       vdir->vd_jiffy = 0;
32110 +       /* smp_mb(); */
32111 +       return err;
32112 +}
32113 +
32114 +/* ---------------------------------------------------------------------- */
32115 +
32116 +#define AuFillVdir_CALLED      1
32117 +#define AuFillVdir_WHABLE      (1 << 1)
32118 +#define AuFillVdir_SHWH                (1 << 2)
32119 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32120 +#define au_fset_fillvdir(flags, name) \
32121 +       do { (flags) |= AuFillVdir_##name; } while (0)
32122 +#define au_fclr_fillvdir(flags, name) \
32123 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32124 +
32125 +#ifndef CONFIG_AUFS_SHWH
32126 +#undef AuFillVdir_SHWH
32127 +#define AuFillVdir_SHWH                0
32128 +#endif
32129 +
32130 +struct fillvdir_arg {
32131 +       struct dir_context      ctx;
32132 +       struct file             *file;
32133 +       struct au_vdir          *vdir;
32134 +       struct au_nhash         delist;
32135 +       struct au_nhash         whlist;
32136 +       aufs_bindex_t           bindex;
32137 +       unsigned int            flags;
32138 +       int                     err;
32139 +};
32140 +
32141 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32142 +                   loff_t offset __maybe_unused, u64 h_ino,
32143 +                   unsigned int d_type)
32144 +{
32145 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32146 +       char *name = (void *)__name;
32147 +       struct super_block *sb;
32148 +       ino_t ino;
32149 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32150 +
32151 +       arg->err = 0;
32152 +       sb = arg->file->f_path.dentry->d_sb;
32153 +       au_fset_fillvdir(arg->flags, CALLED);
32154 +       /* smp_mb(); */
32155 +       if (nlen <= AUFS_WH_PFX_LEN
32156 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32157 +               if (test_known(&arg->delist, name, nlen)
32158 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32159 +                       goto out; /* already exists or whiteouted */
32160 +
32161 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32162 +               if (!arg->err) {
32163 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32164 +                               d_type = DT_UNKNOWN;
32165 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32166 +                                            d_type, &arg->delist);
32167 +               }
32168 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32169 +               name += AUFS_WH_PFX_LEN;
32170 +               nlen -= AUFS_WH_PFX_LEN;
32171 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32172 +                       goto out; /* already whiteouted */
32173 +
32174 +               ino = 0; /* just to suppress a warning */
32175 +               if (shwh)
32176 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32177 +                                            &ino);
32178 +               if (!arg->err) {
32179 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32180 +                               d_type = DT_UNKNOWN;
32181 +                       arg->err = au_nhash_append_wh
32182 +                               (&arg->whlist, name, nlen, ino, d_type,
32183 +                                arg->bindex, shwh);
32184 +               }
32185 +       }
32186 +
32187 +out:
32188 +       if (!arg->err)
32189 +               arg->vdir->vd_jiffy = jiffies;
32190 +       /* smp_mb(); */
32191 +       AuTraceErr(arg->err);
32192 +       return arg->err;
32193 +}
32194 +
32195 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32196 +                         struct au_nhash *whlist, struct au_nhash *delist)
32197 +{
32198 +#ifdef CONFIG_AUFS_SHWH
32199 +       int err;
32200 +       unsigned int nh, u;
32201 +       struct hlist_head *head;
32202 +       struct au_vdir_wh *pos;
32203 +       struct hlist_node *n;
32204 +       char *p, *o;
32205 +       struct au_vdir_destr *destr;
32206 +
32207 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32208 +
32209 +       err = -ENOMEM;
32210 +       o = p = (void *)__get_free_page(GFP_NOFS);
32211 +       if (unlikely(!p))
32212 +               goto out;
32213 +
32214 +       err = 0;
32215 +       nh = whlist->nh_num;
32216 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32217 +       p += AUFS_WH_PFX_LEN;
32218 +       for (u = 0; u < nh; u++) {
32219 +               head = whlist->nh_head + u;
32220 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32221 +                       destr = &pos->wh_str;
32222 +                       memcpy(p, destr->name, destr->len);
32223 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32224 +                                       pos->wh_ino, pos->wh_type, delist);
32225 +                       if (unlikely(err))
32226 +                               break;
32227 +               }
32228 +       }
32229 +
32230 +       free_page((unsigned long)o);
32231 +
32232 +out:
32233 +       AuTraceErr(err);
32234 +       return err;
32235 +#else
32236 +       return 0;
32237 +#endif
32238 +}
32239 +
32240 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32241 +{
32242 +       int err;
32243 +       unsigned int rdhash;
32244 +       loff_t offset;
32245 +       aufs_bindex_t bbot, bindex, btop;
32246 +       unsigned char shwh;
32247 +       struct file *hf, *file;
32248 +       struct super_block *sb;
32249 +
32250 +       file = arg->file;
32251 +       sb = file->f_path.dentry->d_sb;
32252 +       SiMustAnyLock(sb);
32253 +
32254 +       rdhash = au_sbi(sb)->si_rdhash;
32255 +       if (!rdhash)
32256 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32257 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32258 +       if (unlikely(err))
32259 +               goto out;
32260 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32261 +       if (unlikely(err))
32262 +               goto out_delist;
32263 +
32264 +       err = 0;
32265 +       arg->flags = 0;
32266 +       shwh = 0;
32267 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32268 +               shwh = 1;
32269 +               au_fset_fillvdir(arg->flags, SHWH);
32270 +       }
32271 +       btop = au_fbtop(file);
32272 +       bbot = au_fbbot_dir(file);
32273 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32274 +               hf = au_hf_dir(file, bindex);
32275 +               if (!hf)
32276 +                       continue;
32277 +
32278 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32279 +               err = offset;
32280 +               if (unlikely(offset))
32281 +                       break;
32282 +
32283 +               arg->bindex = bindex;
32284 +               au_fclr_fillvdir(arg->flags, WHABLE);
32285 +               if (shwh
32286 +                   || (bindex != bbot
32287 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32288 +                       au_fset_fillvdir(arg->flags, WHABLE);
32289 +               do {
32290 +                       arg->err = 0;
32291 +                       au_fclr_fillvdir(arg->flags, CALLED);
32292 +                       /* smp_mb(); */
32293 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32294 +                       if (err >= 0)
32295 +                               err = arg->err;
32296 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32297 +
32298 +               /*
32299 +                * dir_relax() may be good for concurrency, but aufs should not
32300 +                * use it since it will cause a lockdep problem.
32301 +                */
32302 +       }
32303 +
32304 +       if (!err && shwh)
32305 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32306 +
32307 +       au_nhash_wh_free(&arg->whlist);
32308 +
32309 +out_delist:
32310 +       au_nhash_de_free(&arg->delist);
32311 +out:
32312 +       return err;
32313 +}
32314 +
32315 +static int read_vdir(struct file *file, int may_read)
32316 +{
32317 +       int err;
32318 +       unsigned long expire;
32319 +       unsigned char do_read;
32320 +       struct fillvdir_arg arg = {
32321 +               .ctx = {
32322 +                       .actor = fillvdir
32323 +               }
32324 +       };
32325 +       struct inode *inode;
32326 +       struct au_vdir *vdir, *allocated;
32327 +
32328 +       err = 0;
32329 +       inode = file_inode(file);
32330 +       IMustLock(inode);
32331 +       IiMustWriteLock(inode);
32332 +       SiMustAnyLock(inode->i_sb);
32333 +
32334 +       allocated = NULL;
32335 +       do_read = 0;
32336 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32337 +       vdir = au_ivdir(inode);
32338 +       if (!vdir) {
32339 +               do_read = 1;
32340 +               vdir = alloc_vdir(file);
32341 +               err = PTR_ERR(vdir);
32342 +               if (IS_ERR(vdir))
32343 +                       goto out;
32344 +               err = 0;
32345 +               allocated = vdir;
32346 +       } else if (may_read
32347 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32348 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32349 +               do_read = 1;
32350 +               err = reinit_vdir(vdir);
32351 +               if (unlikely(err))
32352 +                       goto out;
32353 +       }
32354 +
32355 +       if (!do_read)
32356 +               return 0; /* success */
32357 +
32358 +       arg.file = file;
32359 +       arg.vdir = vdir;
32360 +       err = au_do_read_vdir(&arg);
32361 +       if (!err) {
32362 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32363 +               vdir->vd_version = inode_query_iversion(inode);
32364 +               vdir->vd_last.ul = 0;
32365 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32366 +               if (allocated)
32367 +                       au_set_ivdir(inode, allocated);
32368 +       } else if (allocated)
32369 +               au_vdir_free(allocated);
32370 +
32371 +out:
32372 +       return err;
32373 +}
32374 +
32375 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32376 +{
32377 +       int err, rerr;
32378 +       unsigned long ul, n;
32379 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32380 +
32381 +       AuDebugOn(tgt->vd_nblk != 1);
32382 +
32383 +       err = -ENOMEM;
32384 +       if (tgt->vd_nblk < src->vd_nblk) {
32385 +               unsigned char **p;
32386 +
32387 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32388 +                               GFP_NOFS, /*may_shrink*/0);
32389 +               if (unlikely(!p))
32390 +                       goto out;
32391 +               tgt->vd_deblk = p;
32392 +       }
32393 +
32394 +       if (tgt->vd_deblk_sz != deblk_sz) {
32395 +               unsigned char *p;
32396 +
32397 +               tgt->vd_deblk_sz = deblk_sz;
32398 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32399 +                               /*may_shrink*/1);
32400 +               if (unlikely(!p))
32401 +                       goto out;
32402 +               tgt->vd_deblk[0] = p;
32403 +       }
32404 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32405 +       tgt->vd_version = src->vd_version;
32406 +       tgt->vd_jiffy = src->vd_jiffy;
32407 +
32408 +       n = src->vd_nblk;
32409 +       for (ul = 1; ul < n; ul++) {
32410 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32411 +                                           GFP_NOFS);
32412 +               if (unlikely(!tgt->vd_deblk[ul]))
32413 +                       goto out;
32414 +               tgt->vd_nblk++;
32415 +       }
32416 +       tgt->vd_nblk = n;
32417 +       tgt->vd_last.ul = tgt->vd_last.ul;
32418 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32419 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32420 +               - src->vd_deblk[src->vd_last.ul];
32421 +       /* smp_mb(); */
32422 +       return 0; /* success */
32423 +
32424 +out:
32425 +       rerr = reinit_vdir(tgt);
32426 +       BUG_ON(rerr);
32427 +       return err;
32428 +}
32429 +
32430 +int au_vdir_init(struct file *file)
32431 +{
32432 +       int err;
32433 +       struct inode *inode;
32434 +       struct au_vdir *vdir_cache, *allocated;
32435 +
32436 +       /* test file->f_pos here instead of ctx->pos */
32437 +       err = read_vdir(file, !file->f_pos);
32438 +       if (unlikely(err))
32439 +               goto out;
32440 +
32441 +       allocated = NULL;
32442 +       vdir_cache = au_fvdir_cache(file);
32443 +       if (!vdir_cache) {
32444 +               vdir_cache = alloc_vdir(file);
32445 +               err = PTR_ERR(vdir_cache);
32446 +               if (IS_ERR(vdir_cache))
32447 +                       goto out;
32448 +               allocated = vdir_cache;
32449 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32450 +               /* test file->f_pos here instead of ctx->pos */
32451 +               err = reinit_vdir(vdir_cache);
32452 +               if (unlikely(err))
32453 +                       goto out;
32454 +       } else
32455 +               return 0; /* success */
32456 +
32457 +       inode = file_inode(file);
32458 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32459 +       if (!err) {
32460 +               file->f_version = inode_query_iversion(inode);
32461 +               if (allocated)
32462 +                       au_set_fvdir_cache(file, allocated);
32463 +       } else if (allocated)
32464 +               au_vdir_free(allocated);
32465 +
32466 +out:
32467 +       return err;
32468 +}
32469 +
32470 +static loff_t calc_offset(struct au_vdir *vdir)
32471 +{
32472 +       loff_t offset;
32473 +       union au_vdir_deblk_p p;
32474 +
32475 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32476 +       offset = vdir->vd_last.p.deblk - p.deblk;
32477 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32478 +       return offset;
32479 +}
32480 +
32481 +/* returns true or false */
32482 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32483 +{
32484 +       int valid;
32485 +       unsigned int deblk_sz;
32486 +       unsigned long ul, n;
32487 +       loff_t offset;
32488 +       union au_vdir_deblk_p p, deblk_end;
32489 +       struct au_vdir *vdir_cache;
32490 +
32491 +       valid = 1;
32492 +       vdir_cache = au_fvdir_cache(file);
32493 +       offset = calc_offset(vdir_cache);
32494 +       AuDbg("offset %lld\n", offset);
32495 +       if (ctx->pos == offset)
32496 +               goto out;
32497 +
32498 +       vdir_cache->vd_last.ul = 0;
32499 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32500 +       if (!ctx->pos)
32501 +               goto out;
32502 +
32503 +       valid = 0;
32504 +       deblk_sz = vdir_cache->vd_deblk_sz;
32505 +       ul = div64_u64(ctx->pos, deblk_sz);
32506 +       AuDbg("ul %lu\n", ul);
32507 +       if (ul >= vdir_cache->vd_nblk)
32508 +               goto out;
32509 +
32510 +       n = vdir_cache->vd_nblk;
32511 +       for (; ul < n; ul++) {
32512 +               p.deblk = vdir_cache->vd_deblk[ul];
32513 +               deblk_end.deblk = p.deblk + deblk_sz;
32514 +               offset = ul;
32515 +               offset *= deblk_sz;
32516 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32517 +                       unsigned int l;
32518 +
32519 +                       l = calc_size(p.de->de_str.len);
32520 +                       offset += l;
32521 +                       p.deblk += l;
32522 +               }
32523 +               if (!is_deblk_end(&p, &deblk_end)) {
32524 +                       valid = 1;
32525 +                       vdir_cache->vd_last.ul = ul;
32526 +                       vdir_cache->vd_last.p = p;
32527 +                       break;
32528 +               }
32529 +       }
32530 +
32531 +out:
32532 +       /* smp_mb(); */
32533 +       if (!valid)
32534 +               AuDbg("valid %d\n", !valid);
32535 +       return valid;
32536 +}
32537 +
32538 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32539 +{
32540 +       unsigned int l, deblk_sz;
32541 +       union au_vdir_deblk_p deblk_end;
32542 +       struct au_vdir *vdir_cache;
32543 +       struct au_vdir_de *de;
32544 +
32545 +       if (!seek_vdir(file, ctx))
32546 +               return 0;
32547 +
32548 +       vdir_cache = au_fvdir_cache(file);
32549 +       deblk_sz = vdir_cache->vd_deblk_sz;
32550 +       while (1) {
32551 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32552 +               deblk_end.deblk += deblk_sz;
32553 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32554 +                       de = vdir_cache->vd_last.p.de;
32555 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32556 +                             de->de_str.len, de->de_str.name, ctx->pos,
32557 +                             (unsigned long)de->de_ino, de->de_type);
32558 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32559 +                                              de->de_str.len, de->de_ino,
32560 +                                              de->de_type))) {
32561 +                               /* todo: ignore the error caused by udba? */
32562 +                               /* return err; */
32563 +                               return 0;
32564 +                       }
32565 +
32566 +                       l = calc_size(de->de_str.len);
32567 +                       vdir_cache->vd_last.p.deblk += l;
32568 +                       ctx->pos += l;
32569 +               }
32570 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32571 +                       vdir_cache->vd_last.ul++;
32572 +                       vdir_cache->vd_last.p.deblk
32573 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32574 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32575 +                       continue;
32576 +               }
32577 +               break;
32578 +       }
32579 +
32580 +       /* smp_mb(); */
32581 +       return 0;
32582 +}
32583 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32584 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32585 +++ linux/fs/aufs/vfsub.c       2020-12-15 14:10:58.921356596 +0100
32586 @@ -0,0 +1,885 @@
32587 +// SPDX-License-Identifier: GPL-2.0
32588 +/*
32589 + * Copyright (C) 2005-2020 Junjiro R. Okajima
32590 + *
32591 + * This program, aufs is free software; you can redistribute it and/or modify
32592 + * it under the terms of the GNU General Public License as published by
32593 + * the Free Software Foundation; either version 2 of the License, or
32594 + * (at your option) any later version.
32595 + *
32596 + * This program is distributed in the hope that it will be useful,
32597 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32598 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32599 + * GNU General Public License for more details.
32600 + *
32601 + * You should have received a copy of the GNU General Public License
32602 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32603 + */
32604 +
32605 +/*
32606 + * sub-routines for VFS
32607 + */
32608 +
32609 +#include <linux/mnt_namespace.h>
32610 +#include <linux/namei.h>
32611 +#include <linux/nsproxy.h>
32612 +#include <linux/security.h>
32613 +#include <linux/splice.h>
32614 +#include "aufs.h"
32615 +
32616 +#ifdef CONFIG_AUFS_BR_FUSE
32617 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32618 +{
32619 +       if (!au_test_fuse(h_sb) || !au_userns)
32620 +               return 0;
32621 +
32622 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32623 +}
32624 +#endif
32625 +
32626 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32627 +{
32628 +       int err;
32629 +
32630 +       lockdep_off();
32631 +       down_read(&h_sb->s_umount);
32632 +       err = __sync_filesystem(h_sb, wait);
32633 +       up_read(&h_sb->s_umount);
32634 +       lockdep_on();
32635 +
32636 +       return err;
32637 +}
32638 +
32639 +/* ---------------------------------------------------------------------- */
32640 +
32641 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32642 +{
32643 +       int err;
32644 +       struct kstat st;
32645 +       struct super_block *h_sb;
32646 +
32647 +       /* for remote fs, leave work for its getattr or d_revalidate */
32648 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32649 +       /* still some fs may acquire i_mutex. we need to skip them */
32650 +       err = 0;
32651 +       if (!did)
32652 +               did = &err;
32653 +       h_sb = h_path->dentry->d_sb;
32654 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32655 +       if (*did)
32656 +               err = vfsub_getattr(h_path, &st);
32657 +
32658 +       return err;
32659 +}
32660 +
32661 +/* ---------------------------------------------------------------------- */
32662 +
32663 +struct file *vfsub_dentry_open(struct path *path, int flags)
32664 +{
32665 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32666 +                          current_cred());
32667 +}
32668 +
32669 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32670 +{
32671 +       struct file *file;
32672 +
32673 +       lockdep_off();
32674 +       file = filp_open(path,
32675 +                        oflags /* | __FMODE_NONOTIFY */,
32676 +                        mode);
32677 +       lockdep_on();
32678 +       if (IS_ERR(file))
32679 +               goto out;
32680 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32681 +
32682 +out:
32683 +       return file;
32684 +}
32685 +
32686 +/*
32687 + * Ideally this function should call VFS:do_last() in order to keep all its
32688 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32689 + * structure such as nameidata. This is a second (or third) best approach.
32690 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32691 + */
32692 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32693 +                     struct vfsub_aopen_args *args)
32694 +{
32695 +       int err;
32696 +       struct au_branch *br = args->br;
32697 +       struct file *file = args->file;
32698 +       /* copied from linux/fs/namei.c:atomic_open() */
32699 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32700 +
32701 +       IMustLock(dir);
32702 +       AuDebugOn(!dir->i_op->atomic_open);
32703 +
32704 +       err = au_br_test_oflag(args->open_flag, br);
32705 +       if (unlikely(err))
32706 +               goto out;
32707 +
32708 +       au_lcnt_inc(&br->br_nfiles);
32709 +       file->f_path.dentry = DENTRY_NOT_SET;
32710 +       file->f_path.mnt = au_br_mnt(br);
32711 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32712 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32713 +                                    args->create_mode);
32714 +       if (unlikely(err < 0)) {
32715 +               au_lcnt_dec(&br->br_nfiles);
32716 +               goto out;
32717 +       }
32718 +
32719 +       /* temporary workaround for nfsv4 branch */
32720 +       if (au_test_nfs(dir->i_sb))
32721 +               nfs_mark_for_revalidate(dir);
32722 +
32723 +       if (file->f_mode & FMODE_CREATED)
32724 +               fsnotify_create(dir, dentry);
32725 +       if (!(file->f_mode & FMODE_OPENED)) {
32726 +               au_lcnt_dec(&br->br_nfiles);
32727 +               goto out;
32728 +       }
32729 +
32730 +       /* todo: call VFS:may_open() here */
32731 +       /* todo: ima_file_check() too? */
32732 +       if (!err && (args->open_flag & __FMODE_EXEC))
32733 +               err = deny_write_access(file);
32734 +       if (!err)
32735 +               fsnotify_open(file);
32736 +       else
32737 +               au_lcnt_dec(&br->br_nfiles);
32738 +       /* note that the file is created and still opened */
32739 +
32740 +out:
32741 +       return err;
32742 +}
32743 +
32744 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32745 +{
32746 +       int err;
32747 +
32748 +       err = kern_path(name, flags, path);
32749 +       if (!err && d_is_positive(path->dentry))
32750 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32751 +       return err;
32752 +}
32753 +
32754 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32755 +                                            struct dentry *parent, int len)
32756 +{
32757 +       struct path path = {
32758 +               .mnt = NULL
32759 +       };
32760 +
32761 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
32762 +       if (IS_ERR(path.dentry))
32763 +               goto out;
32764 +       if (d_is_positive(path.dentry))
32765 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32766 +
32767 +out:
32768 +       AuTraceErrPtr(path.dentry);
32769 +       return path.dentry;
32770 +}
32771 +
32772 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
32773 +                                   int len)
32774 +{
32775 +       struct path path = {
32776 +               .mnt = NULL
32777 +       };
32778 +
32779 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32780 +       IMustLock(d_inode(parent));
32781 +
32782 +       path.dentry = lookup_one_len(name, parent, len);
32783 +       if (IS_ERR(path.dentry))
32784 +               goto out;
32785 +       if (d_is_positive(path.dentry))
32786 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32787 +
32788 +out:
32789 +       AuTraceErrPtr(path.dentry);
32790 +       return path.dentry;
32791 +}
32792 +
32793 +void vfsub_call_lkup_one(void *args)
32794 +{
32795 +       struct vfsub_lkup_one_args *a = args;
32796 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
32797 +}
32798 +
32799 +/* ---------------------------------------------------------------------- */
32800 +
32801 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32802 +                                struct dentry *d2, struct au_hinode *hdir2)
32803 +{
32804 +       struct dentry *d;
32805 +
32806 +       lockdep_off();
32807 +       d = lock_rename(d1, d2);
32808 +       lockdep_on();
32809 +       au_hn_suspend(hdir1);
32810 +       if (hdir1 != hdir2)
32811 +               au_hn_suspend(hdir2);
32812 +
32813 +       return d;
32814 +}
32815 +
32816 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32817 +                        struct dentry *d2, struct au_hinode *hdir2)
32818 +{
32819 +       au_hn_resume(hdir1);
32820 +       if (hdir1 != hdir2)
32821 +               au_hn_resume(hdir2);
32822 +       lockdep_off();
32823 +       unlock_rename(d1, d2);
32824 +       lockdep_on();
32825 +}
32826 +
32827 +/* ---------------------------------------------------------------------- */
32828 +
32829 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32830 +{
32831 +       int err;
32832 +       struct dentry *d;
32833 +
32834 +       IMustLock(dir);
32835 +
32836 +       d = path->dentry;
32837 +       path->dentry = d->d_parent;
32838 +       err = security_path_mknod(path, d, mode, 0);
32839 +       path->dentry = d;
32840 +       if (unlikely(err))
32841 +               goto out;
32842 +
32843 +       lockdep_off();
32844 +       err = vfs_create(dir, path->dentry, mode, want_excl);
32845 +       lockdep_on();
32846 +       if (!err) {
32847 +               struct path tmp = *path;
32848 +               int did;
32849 +
32850 +               vfsub_update_h_iattr(&tmp, &did);
32851 +               if (did) {
32852 +                       tmp.dentry = path->dentry->d_parent;
32853 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32854 +               }
32855 +               /*ignore*/
32856 +       }
32857 +
32858 +out:
32859 +       return err;
32860 +}
32861 +
32862 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32863 +{
32864 +       int err;
32865 +       struct dentry *d;
32866 +
32867 +       IMustLock(dir);
32868 +
32869 +       d = path->dentry;
32870 +       path->dentry = d->d_parent;
32871 +       err = security_path_symlink(path, d, symname);
32872 +       path->dentry = d;
32873 +       if (unlikely(err))
32874 +               goto out;
32875 +
32876 +       lockdep_off();
32877 +       err = vfs_symlink(dir, path->dentry, symname);
32878 +       lockdep_on();
32879 +       if (!err) {
32880 +               struct path tmp = *path;
32881 +               int did;
32882 +
32883 +               vfsub_update_h_iattr(&tmp, &did);
32884 +               if (did) {
32885 +                       tmp.dentry = path->dentry->d_parent;
32886 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32887 +               }
32888 +               /*ignore*/
32889 +       }
32890 +
32891 +out:
32892 +       return err;
32893 +}
32894 +
32895 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32896 +{
32897 +       int err;
32898 +       struct dentry *d;
32899 +
32900 +       IMustLock(dir);
32901 +
32902 +       d = path->dentry;
32903 +       path->dentry = d->d_parent;
32904 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
32905 +       path->dentry = d;
32906 +       if (unlikely(err))
32907 +               goto out;
32908 +
32909 +       lockdep_off();
32910 +       err = vfs_mknod(dir, path->dentry, mode, dev);
32911 +       lockdep_on();
32912 +       if (!err) {
32913 +               struct path tmp = *path;
32914 +               int did;
32915 +
32916 +               vfsub_update_h_iattr(&tmp, &did);
32917 +               if (did) {
32918 +                       tmp.dentry = path->dentry->d_parent;
32919 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32920 +               }
32921 +               /*ignore*/
32922 +       }
32923 +
32924 +out:
32925 +       return err;
32926 +}
32927 +
32928 +static int au_test_nlink(struct inode *inode)
32929 +{
32930 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
32931 +
32932 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
32933 +           || inode->i_nlink < link_max)
32934 +               return 0;
32935 +       return -EMLINK;
32936 +}
32937 +
32938 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
32939 +              struct inode **delegated_inode)
32940 +{
32941 +       int err;
32942 +       struct dentry *d;
32943 +
32944 +       IMustLock(dir);
32945 +
32946 +       err = au_test_nlink(d_inode(src_dentry));
32947 +       if (unlikely(err))
32948 +               return err;
32949 +
32950 +       /* we don't call may_linkat() */
32951 +       d = path->dentry;
32952 +       path->dentry = d->d_parent;
32953 +       err = security_path_link(src_dentry, path, d);
32954 +       path->dentry = d;
32955 +       if (unlikely(err))
32956 +               goto out;
32957 +
32958 +       lockdep_off();
32959 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
32960 +       lockdep_on();
32961 +       if (!err) {
32962 +               struct path tmp = *path;
32963 +               int did;
32964 +
32965 +               /* fuse has different memory inode for the same inumber */
32966 +               vfsub_update_h_iattr(&tmp, &did);
32967 +               if (did) {
32968 +                       tmp.dentry = path->dentry->d_parent;
32969 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32970 +                       tmp.dentry = src_dentry;
32971 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32972 +               }
32973 +               /*ignore*/
32974 +       }
32975 +
32976 +out:
32977 +       return err;
32978 +}
32979 +
32980 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
32981 +                struct inode *dir, struct path *path,
32982 +                struct inode **delegated_inode, unsigned int flags)
32983 +{
32984 +       int err;
32985 +       struct path tmp = {
32986 +               .mnt    = path->mnt
32987 +       };
32988 +       struct dentry *d;
32989 +
32990 +       IMustLock(dir);
32991 +       IMustLock(src_dir);
32992 +
32993 +       d = path->dentry;
32994 +       path->dentry = d->d_parent;
32995 +       tmp.dentry = src_dentry->d_parent;
32996 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
32997 +       path->dentry = d;
32998 +       if (unlikely(err))
32999 +               goto out;
33000 +
33001 +       lockdep_off();
33002 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
33003 +                        delegated_inode, flags);
33004 +       lockdep_on();
33005 +       if (!err) {
33006 +               int did;
33007 +
33008 +               tmp.dentry = d->d_parent;
33009 +               vfsub_update_h_iattr(&tmp, &did);
33010 +               if (did) {
33011 +                       tmp.dentry = src_dentry;
33012 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33013 +                       tmp.dentry = src_dentry->d_parent;
33014 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33015 +               }
33016 +               /*ignore*/
33017 +       }
33018 +
33019 +out:
33020 +       return err;
33021 +}
33022 +
33023 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33024 +{
33025 +       int err;
33026 +       struct dentry *d;
33027 +
33028 +       IMustLock(dir);
33029 +
33030 +       d = path->dentry;
33031 +       path->dentry = d->d_parent;
33032 +       err = security_path_mkdir(path, d, mode);
33033 +       path->dentry = d;
33034 +       if (unlikely(err))
33035 +               goto out;
33036 +
33037 +       lockdep_off();
33038 +       err = vfs_mkdir(dir, path->dentry, mode);
33039 +       lockdep_on();
33040 +       if (!err) {
33041 +               struct path tmp = *path;
33042 +               int did;
33043 +
33044 +               vfsub_update_h_iattr(&tmp, &did);
33045 +               if (did) {
33046 +                       tmp.dentry = path->dentry->d_parent;
33047 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33048 +               }
33049 +               /*ignore*/
33050 +       }
33051 +
33052 +out:
33053 +       return err;
33054 +}
33055 +
33056 +int vfsub_rmdir(struct inode *dir, struct path *path)
33057 +{
33058 +       int err;
33059 +       struct dentry *d;
33060 +
33061 +       IMustLock(dir);
33062 +
33063 +       d = path->dentry;
33064 +       path->dentry = d->d_parent;
33065 +       err = security_path_rmdir(path, d);
33066 +       path->dentry = d;
33067 +       if (unlikely(err))
33068 +               goto out;
33069 +
33070 +       lockdep_off();
33071 +       err = vfs_rmdir(dir, path->dentry);
33072 +       lockdep_on();
33073 +       if (!err) {
33074 +               struct path tmp = {
33075 +                       .dentry = path->dentry->d_parent,
33076 +                       .mnt    = path->mnt
33077 +               };
33078 +
33079 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33080 +       }
33081 +
33082 +out:
33083 +       return err;
33084 +}
33085 +
33086 +/* ---------------------------------------------------------------------- */
33087 +
33088 +/* todo: support mmap_sem? */
33089 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33090 +                    loff_t *ppos)
33091 +{
33092 +       ssize_t err;
33093 +
33094 +       lockdep_off();
33095 +       err = vfs_read(file, ubuf, count, ppos);
33096 +       lockdep_on();
33097 +       if (err >= 0)
33098 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33099 +       return err;
33100 +}
33101 +
33102 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33103 +                    loff_t *ppos)
33104 +{
33105 +       ssize_t err;
33106 +
33107 +       lockdep_off();
33108 +       err = kernel_read(file, kbuf, count, ppos);
33109 +       lockdep_on();
33110 +       AuTraceErr(err);
33111 +       if (err >= 0)
33112 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33113 +       return err;
33114 +}
33115 +
33116 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33117 +                     loff_t *ppos)
33118 +{
33119 +       ssize_t err;
33120 +
33121 +       lockdep_off();
33122 +       err = vfs_write(file, ubuf, count, ppos);
33123 +       lockdep_on();
33124 +       if (err >= 0)
33125 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33126 +       return err;
33127 +}
33128 +
33129 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33130 +{
33131 +       ssize_t err;
33132 +
33133 +       lockdep_off();
33134 +       err = kernel_write(file, kbuf, count, ppos);
33135 +       lockdep_on();
33136 +       if (err >= 0)
33137 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33138 +       return err;
33139 +}
33140 +
33141 +int vfsub_flush(struct file *file, fl_owner_t id)
33142 +{
33143 +       int err;
33144 +
33145 +       err = 0;
33146 +       if (file->f_op->flush) {
33147 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33148 +                       err = file->f_op->flush(file, id);
33149 +               else {
33150 +                       lockdep_off();
33151 +                       err = file->f_op->flush(file, id);
33152 +                       lockdep_on();
33153 +               }
33154 +               if (!err)
33155 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33156 +               /*ignore*/
33157 +       }
33158 +       return err;
33159 +}
33160 +
33161 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33162 +{
33163 +       int err;
33164 +
33165 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33166 +
33167 +       lockdep_off();
33168 +       err = iterate_dir(file, ctx);
33169 +       lockdep_on();
33170 +       if (err >= 0)
33171 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33172 +
33173 +       return err;
33174 +}
33175 +
33176 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33177 +                    struct pipe_inode_info *pipe, size_t len,
33178 +                    unsigned int flags)
33179 +{
33180 +       long err;
33181 +
33182 +       lockdep_off();
33183 +       err = do_splice_to(in, ppos, pipe, len, flags);
33184 +       lockdep_on();
33185 +       file_accessed(in);
33186 +       if (err >= 0)
33187 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33188 +       return err;
33189 +}
33190 +
33191 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33192 +                      loff_t *ppos, size_t len, unsigned int flags)
33193 +{
33194 +       long err;
33195 +
33196 +       lockdep_off();
33197 +       err = do_splice_from(pipe, out, ppos, len, flags);
33198 +       lockdep_on();
33199 +       if (err >= 0)
33200 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33201 +       return err;
33202 +}
33203 +
33204 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33205 +{
33206 +       int err;
33207 +
33208 +       /* file can be NULL */
33209 +       lockdep_off();
33210 +       err = vfs_fsync(file, datasync);
33211 +       lockdep_on();
33212 +       if (!err) {
33213 +               if (!path) {
33214 +                       AuDebugOn(!file);
33215 +                       path = &file->f_path;
33216 +               }
33217 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33218 +       }
33219 +       return err;
33220 +}
33221 +
33222 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33223 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33224 +               struct file *h_file)
33225 +{
33226 +       int err;
33227 +       struct inode *h_inode;
33228 +       struct super_block *h_sb;
33229 +
33230 +       if (!h_file) {
33231 +               err = vfsub_truncate(h_path, length);
33232 +               goto out;
33233 +       }
33234 +
33235 +       h_inode = d_inode(h_path->dentry);
33236 +       h_sb = h_inode->i_sb;
33237 +       lockdep_off();
33238 +       sb_start_write(h_sb);
33239 +       lockdep_on();
33240 +       err = locks_verify_truncate(h_inode, h_file, length);
33241 +       if (!err)
33242 +               err = security_path_truncate(h_path);
33243 +       if (!err) {
33244 +               lockdep_off();
33245 +               err = do_truncate(h_path->dentry, length, attr, h_file);
33246 +               lockdep_on();
33247 +       }
33248 +       lockdep_off();
33249 +       sb_end_write(h_sb);
33250 +       lockdep_on();
33251 +
33252 +out:
33253 +       return err;
33254 +}
33255 +
33256 +/* ---------------------------------------------------------------------- */
33257 +
33258 +struct au_vfsub_mkdir_args {
33259 +       int *errp;
33260 +       struct inode *dir;
33261 +       struct path *path;
33262 +       int mode;
33263 +};
33264 +
33265 +static void au_call_vfsub_mkdir(void *args)
33266 +{
33267 +       struct au_vfsub_mkdir_args *a = args;
33268 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33269 +}
33270 +
33271 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33272 +{
33273 +       int err, do_sio, wkq_err;
33274 +
33275 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33276 +       if (!do_sio) {
33277 +               lockdep_off();
33278 +               err = vfsub_mkdir(dir, path, mode);
33279 +               lockdep_on();
33280 +       } else {
33281 +               struct au_vfsub_mkdir_args args = {
33282 +                       .errp   = &err,
33283 +                       .dir    = dir,
33284 +                       .path   = path,
33285 +                       .mode   = mode
33286 +               };
33287 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33288 +               if (unlikely(wkq_err))
33289 +                       err = wkq_err;
33290 +       }
33291 +
33292 +       return err;
33293 +}
33294 +
33295 +struct au_vfsub_rmdir_args {
33296 +       int *errp;
33297 +       struct inode *dir;
33298 +       struct path *path;
33299 +};
33300 +
33301 +static void au_call_vfsub_rmdir(void *args)
33302 +{
33303 +       struct au_vfsub_rmdir_args *a = args;
33304 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33305 +}
33306 +
33307 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33308 +{
33309 +       int err, do_sio, wkq_err;
33310 +
33311 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
33312 +       if (!do_sio) {
33313 +               lockdep_off();
33314 +               err = vfsub_rmdir(dir, path);
33315 +               lockdep_on();
33316 +       } else {
33317 +               struct au_vfsub_rmdir_args args = {
33318 +                       .errp   = &err,
33319 +                       .dir    = dir,
33320 +                       .path   = path
33321 +               };
33322 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33323 +               if (unlikely(wkq_err))
33324 +                       err = wkq_err;
33325 +       }
33326 +
33327 +       return err;
33328 +}
33329 +
33330 +/* ---------------------------------------------------------------------- */
33331 +
33332 +struct notify_change_args {
33333 +       int *errp;
33334 +       struct path *path;
33335 +       struct iattr *ia;
33336 +       struct inode **delegated_inode;
33337 +};
33338 +
33339 +static void call_notify_change(void *args)
33340 +{
33341 +       struct notify_change_args *a = args;
33342 +       struct inode *h_inode;
33343 +
33344 +       h_inode = d_inode(a->path->dentry);
33345 +       IMustLock(h_inode);
33346 +
33347 +       *a->errp = -EPERM;
33348 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33349 +               lockdep_off();
33350 +               *a->errp = notify_change(a->path->dentry, a->ia,
33351 +                                        a->delegated_inode);
33352 +               lockdep_on();
33353 +               if (!*a->errp)
33354 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33355 +       }
33356 +       AuTraceErr(*a->errp);
33357 +}
33358 +
33359 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33360 +                       struct inode **delegated_inode)
33361 +{
33362 +       int err;
33363 +       struct notify_change_args args = {
33364 +               .errp                   = &err,
33365 +               .path                   = path,
33366 +               .ia                     = ia,
33367 +               .delegated_inode        = delegated_inode
33368 +       };
33369 +
33370 +       call_notify_change(&args);
33371 +
33372 +       return err;
33373 +}
33374 +
33375 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33376 +                           struct inode **delegated_inode)
33377 +{
33378 +       int err, wkq_err;
33379 +       struct notify_change_args args = {
33380 +               .errp                   = &err,
33381 +               .path                   = path,
33382 +               .ia                     = ia,
33383 +               .delegated_inode        = delegated_inode
33384 +       };
33385 +
33386 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33387 +       if (unlikely(wkq_err))
33388 +               err = wkq_err;
33389 +
33390 +       return err;
33391 +}
33392 +
33393 +/* ---------------------------------------------------------------------- */
33394 +
33395 +struct unlink_args {
33396 +       int *errp;
33397 +       struct inode *dir;
33398 +       struct path *path;
33399 +       struct inode **delegated_inode;
33400 +};
33401 +
33402 +static void call_unlink(void *args)
33403 +{
33404 +       struct unlink_args *a = args;
33405 +       struct dentry *d = a->path->dentry;
33406 +       struct inode *h_inode;
33407 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33408 +                                     && au_dcount(d) == 1);
33409 +
33410 +       IMustLock(a->dir);
33411 +
33412 +       a->path->dentry = d->d_parent;
33413 +       *a->errp = security_path_unlink(a->path, d);
33414 +       a->path->dentry = d;
33415 +       if (unlikely(*a->errp))
33416 +               return;
33417 +
33418 +       if (!stop_sillyrename)
33419 +               dget(d);
33420 +       h_inode = NULL;
33421 +       if (d_is_positive(d)) {
33422 +               h_inode = d_inode(d);
33423 +               ihold(h_inode);
33424 +       }
33425 +
33426 +       lockdep_off();
33427 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
33428 +       lockdep_on();
33429 +       if (!*a->errp) {
33430 +               struct path tmp = {
33431 +                       .dentry = d->d_parent,
33432 +                       .mnt    = a->path->mnt
33433 +               };
33434 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33435 +       }
33436 +
33437 +       if (!stop_sillyrename)
33438 +               dput(d);
33439 +       if (h_inode)
33440 +               iput(h_inode);
33441 +
33442 +       AuTraceErr(*a->errp);
33443 +}
33444 +
33445 +/*
33446 + * @dir: must be locked.
33447 + * @dentry: target dentry.
33448 + */
33449 +int vfsub_unlink(struct inode *dir, struct path *path,
33450 +                struct inode **delegated_inode, int force)
33451 +{
33452 +       int err;
33453 +       struct unlink_args args = {
33454 +               .errp                   = &err,
33455 +               .dir                    = dir,
33456 +               .path                   = path,
33457 +               .delegated_inode        = delegated_inode
33458 +       };
33459 +
33460 +       if (!force)
33461 +               call_unlink(&args);
33462 +       else {
33463 +               int wkq_err;
33464 +
33465 +               wkq_err = au_wkq_wait(call_unlink, &args);
33466 +               if (unlikely(wkq_err))
33467 +                       err = wkq_err;
33468 +       }
33469 +
33470 +       return err;
33471 +}
33472 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33473 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33474 +++ linux/fs/aufs/vfsub.h       2020-01-27 10:57:18.178871751 +0100
33475 @@ -0,0 +1,354 @@
33476 +/* SPDX-License-Identifier: GPL-2.0 */
33477 +/*
33478 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33479 + *
33480 + * This program, aufs is free software; you can redistribute it and/or modify
33481 + * it under the terms of the GNU General Public License as published by
33482 + * the Free Software Foundation; either version 2 of the License, or
33483 + * (at your option) any later version.
33484 + *
33485 + * This program is distributed in the hope that it will be useful,
33486 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33487 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33488 + * GNU General Public License for more details.
33489 + *
33490 + * You should have received a copy of the GNU General Public License
33491 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33492 + */
33493 +
33494 +/*
33495 + * sub-routines for VFS
33496 + */
33497 +
33498 +#ifndef __AUFS_VFSUB_H__
33499 +#define __AUFS_VFSUB_H__
33500 +
33501 +#ifdef __KERNEL__
33502 +
33503 +#include <linux/fs.h>
33504 +#include <linux/mount.h>
33505 +#include <linux/posix_acl.h>
33506 +#include <linux/xattr.h>
33507 +#include "debug.h"
33508 +
33509 +/* copied from linux/fs/internal.h */
33510 +/* todo: BAD approach!! */
33511 +extern void __mnt_drop_write(struct vfsmount *);
33512 +extern struct file *alloc_empty_file(int, const struct cred *);
33513 +
33514 +/* ---------------------------------------------------------------------- */
33515 +
33516 +/* lock subclass for lower inode */
33517 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33518 +/* reduce? gave up. */
33519 +enum {
33520 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33521 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33522 +       AuLsc_I_PARENT2,        /* copyup dirs */
33523 +       AuLsc_I_PARENT3,        /* copyup wh */
33524 +       AuLsc_I_CHILD,
33525 +       AuLsc_I_CHILD2,
33526 +       AuLsc_I_End
33527 +};
33528 +
33529 +/* to debug easier, do not make them inlined functions */
33530 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33531 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33532 +
33533 +/* ---------------------------------------------------------------------- */
33534 +
33535 +static inline void vfsub_drop_nlink(struct inode *inode)
33536 +{
33537 +       AuDebugOn(!inode->i_nlink);
33538 +       drop_nlink(inode);
33539 +}
33540 +
33541 +static inline void vfsub_dead_dir(struct inode *inode)
33542 +{
33543 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33544 +       inode->i_flags |= S_DEAD;
33545 +       clear_nlink(inode);
33546 +}
33547 +
33548 +static inline int vfsub_native_ro(struct inode *inode)
33549 +{
33550 +       return sb_rdonly(inode->i_sb)
33551 +               || IS_RDONLY(inode)
33552 +               /* || IS_APPEND(inode) */
33553 +               || IS_IMMUTABLE(inode);
33554 +}
33555 +
33556 +#ifdef CONFIG_AUFS_BR_FUSE
33557 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33558 +#else
33559 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33560 +#endif
33561 +
33562 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33563 +
33564 +/* ---------------------------------------------------------------------- */
33565 +
33566 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33567 +struct file *vfsub_dentry_open(struct path *path, int flags);
33568 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33569 +struct au_branch;
33570 +struct vfsub_aopen_args {
33571 +       struct file             *file;
33572 +       unsigned int            open_flag;
33573 +       umode_t                 create_mode;
33574 +       struct au_branch        *br;
33575 +};
33576 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33577 +                     struct vfsub_aopen_args *args);
33578 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33579 +
33580 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33581 +                                            struct dentry *parent, int len);
33582 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
33583 +                                   int len);
33584 +
33585 +struct vfsub_lkup_one_args {
33586 +       struct dentry **errp;
33587 +       struct qstr *name;
33588 +       struct dentry *parent;
33589 +};
33590 +
33591 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33592 +                                           struct dentry *parent)
33593 +{
33594 +       return vfsub_lookup_one_len(name->name, parent, name->len);
33595 +}
33596 +
33597 +void vfsub_call_lkup_one(void *args);
33598 +
33599 +/* ---------------------------------------------------------------------- */
33600 +
33601 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33602 +{
33603 +       int err;
33604 +
33605 +       lockdep_off();
33606 +       err = mnt_want_write(mnt);
33607 +       lockdep_on();
33608 +       return err;
33609 +}
33610 +
33611 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33612 +{
33613 +       lockdep_off();
33614 +       mnt_drop_write(mnt);
33615 +       lockdep_on();
33616 +}
33617 +
33618 +#if 0 /* reserved */
33619 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33620 +{
33621 +       lockdep_off();
33622 +       mnt_drop_write_file(file);
33623 +       lockdep_on();
33624 +}
33625 +#endif
33626 +
33627 +/* ---------------------------------------------------------------------- */
33628 +
33629 +struct au_hinode;
33630 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33631 +                                struct dentry *d2, struct au_hinode *hdir2);
33632 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33633 +                        struct dentry *d2, struct au_hinode *hdir2);
33634 +
33635 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33636 +                bool want_excl);
33637 +int vfsub_symlink(struct inode *dir, struct path *path,
33638 +                 const char *symname);
33639 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33640 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33641 +              struct path *path, struct inode **delegated_inode);
33642 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33643 +                struct inode *hdir, struct path *path,
33644 +                struct inode **delegated_inode, unsigned int flags);
33645 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33646 +int vfsub_rmdir(struct inode *dir, struct path *path);
33647 +
33648 +/* ---------------------------------------------------------------------- */
33649 +
33650 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33651 +                    loff_t *ppos);
33652 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33653 +                       loff_t *ppos);
33654 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33655 +                     loff_t *ppos);
33656 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33657 +                     loff_t *ppos);
33658 +int vfsub_flush(struct file *file, fl_owner_t id);
33659 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33660 +
33661 +static inline loff_t vfsub_f_size_read(struct file *file)
33662 +{
33663 +       return i_size_read(file_inode(file));
33664 +}
33665 +
33666 +static inline unsigned int vfsub_file_flags(struct file *file)
33667 +{
33668 +       unsigned int flags;
33669 +
33670 +       spin_lock(&file->f_lock);
33671 +       flags = file->f_flags;
33672 +       spin_unlock(&file->f_lock);
33673 +
33674 +       return flags;
33675 +}
33676 +
33677 +static inline int vfsub_file_execed(struct file *file)
33678 +{
33679 +       /* todo: direct access f_flags */
33680 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33681 +}
33682 +
33683 +#if 0 /* reserved */
33684 +static inline void vfsub_file_accessed(struct file *h_file)
33685 +{
33686 +       file_accessed(h_file);
33687 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33688 +}
33689 +#endif
33690 +
33691 +#if 0 /* reserved */
33692 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33693 +                                    struct dentry *h_dentry)
33694 +{
33695 +       struct path h_path = {
33696 +               .dentry = h_dentry,
33697 +               .mnt    = h_mnt
33698 +       };
33699 +       touch_atime(&h_path);
33700 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33701 +}
33702 +#endif
33703 +
33704 +static inline int vfsub_update_time(struct inode *h_inode,
33705 +                                   struct timespec64 *ts, int flags)
33706 +{
33707 +       return update_time(h_inode, ts, flags);
33708 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33709 +}
33710 +
33711 +#ifdef CONFIG_FS_POSIX_ACL
33712 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
33713 +{
33714 +       int err;
33715 +
33716 +       err = posix_acl_chmod(h_inode, h_mode);
33717 +       if (err == -EOPNOTSUPP)
33718 +               err = 0;
33719 +       return err;
33720 +}
33721 +#else
33722 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
33723 +#endif
33724 +
33725 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33726 +                    struct pipe_inode_info *pipe, size_t len,
33727 +                    unsigned int flags);
33728 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33729 +                      loff_t *ppos, size_t len, unsigned int flags);
33730 +
33731 +static inline long vfsub_truncate(struct path *path, loff_t length)
33732 +{
33733 +       long err;
33734 +
33735 +       lockdep_off();
33736 +       err = vfs_truncate(path, length);
33737 +       lockdep_on();
33738 +       return err;
33739 +}
33740 +
33741 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33742 +               struct file *h_file);
33743 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33744 +
33745 +/*
33746 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33747 + * ioctl.
33748 + */
33749 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33750 +                                           loff_t len)
33751 +{
33752 +       loff_t err;
33753 +
33754 +       lockdep_off();
33755 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33756 +       lockdep_on();
33757 +
33758 +       return err;
33759 +}
33760 +
33761 +/* copy_file_range(2) is a systemcall */
33762 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33763 +                                           struct file *dst, loff_t dst_pos,
33764 +                                           size_t len, unsigned int flags)
33765 +{
33766 +       ssize_t ssz;
33767 +
33768 +       lockdep_off();
33769 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33770 +       lockdep_on();
33771 +
33772 +       return ssz;
33773 +}
33774 +
33775 +/* ---------------------------------------------------------------------- */
33776 +
33777 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33778 +{
33779 +       loff_t err;
33780 +
33781 +       lockdep_off();
33782 +       err = vfs_llseek(file, offset, origin);
33783 +       lockdep_on();
33784 +       return err;
33785 +}
33786 +
33787 +/* ---------------------------------------------------------------------- */
33788 +
33789 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33790 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33791 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33792 +                           struct inode **delegated_inode);
33793 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33794 +                       struct inode **delegated_inode);
33795 +int vfsub_unlink(struct inode *dir, struct path *path,
33796 +                struct inode **delegated_inode, int force);
33797 +
33798 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33799 +{
33800 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33801 +}
33802 +
33803 +/* ---------------------------------------------------------------------- */
33804 +
33805 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
33806 +                                const void *value, size_t size, int flags)
33807 +{
33808 +       int err;
33809 +
33810 +       lockdep_off();
33811 +       err = vfs_setxattr(dentry, name, value, size, flags);
33812 +       lockdep_on();
33813 +
33814 +       return err;
33815 +}
33816 +
33817 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
33818 +{
33819 +       int err;
33820 +
33821 +       lockdep_off();
33822 +       err = vfs_removexattr(dentry, name);
33823 +       lockdep_on();
33824 +
33825 +       return err;
33826 +}
33827 +
33828 +#endif /* __KERNEL__ */
33829 +#endif /* __AUFS_VFSUB_H__ */
33830 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33831 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33832 +++ linux/fs/aufs/wbr_policy.c  2020-01-27 10:57:18.178871751 +0100
33833 @@ -0,0 +1,830 @@
33834 +// SPDX-License-Identifier: GPL-2.0
33835 +/*
33836 + * Copyright (C) 2005-2020 Junjiro R. Okajima
33837 + *
33838 + * This program, aufs is free software; you can redistribute it and/or modify
33839 + * it under the terms of the GNU General Public License as published by
33840 + * the Free Software Foundation; either version 2 of the License, or
33841 + * (at your option) any later version.
33842 + *
33843 + * This program is distributed in the hope that it will be useful,
33844 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33845 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33846 + * GNU General Public License for more details.
33847 + *
33848 + * You should have received a copy of the GNU General Public License
33849 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33850 + */
33851 +
33852 +/*
33853 + * policies for selecting one among multiple writable branches
33854 + */
33855 +
33856 +#include <linux/statfs.h>
33857 +#include "aufs.h"
33858 +
33859 +/* subset of cpup_attr() */
33860 +static noinline_for_stack
33861 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33862 +{
33863 +       int err, sbits;
33864 +       struct iattr ia;
33865 +       struct inode *h_isrc;
33866 +
33867 +       h_isrc = d_inode(h_src);
33868 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33869 +       ia.ia_mode = h_isrc->i_mode;
33870 +       ia.ia_uid = h_isrc->i_uid;
33871 +       ia.ia_gid = h_isrc->i_gid;
33872 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
33873 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
33874 +       /* no delegation since it is just created */
33875 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33876 +
33877 +       /* is this nfs only? */
33878 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
33879 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
33880 +               ia.ia_mode = h_isrc->i_mode;
33881 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
33882 +       }
33883 +
33884 +       return err;
33885 +}
33886 +
33887 +#define AuCpdown_PARENT_OPQ    1
33888 +#define AuCpdown_WHED          (1 << 1)
33889 +#define AuCpdown_MADE_DIR      (1 << 2)
33890 +#define AuCpdown_DIROPQ                (1 << 3)
33891 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
33892 +#define au_fset_cpdown(flags, name) \
33893 +       do { (flags) |= AuCpdown_##name; } while (0)
33894 +#define au_fclr_cpdown(flags, name) \
33895 +       do { (flags) &= ~AuCpdown_##name; } while (0)
33896 +
33897 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
33898 +                            unsigned int *flags)
33899 +{
33900 +       int err;
33901 +       struct dentry *opq_dentry;
33902 +
33903 +       opq_dentry = au_diropq_create(dentry, bdst);
33904 +       err = PTR_ERR(opq_dentry);
33905 +       if (IS_ERR(opq_dentry))
33906 +               goto out;
33907 +       dput(opq_dentry);
33908 +       au_fset_cpdown(*flags, DIROPQ);
33909 +
33910 +out:
33911 +       return err;
33912 +}
33913 +
33914 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
33915 +                           struct inode *dir, aufs_bindex_t bdst)
33916 +{
33917 +       int err;
33918 +       struct path h_path;
33919 +       struct au_branch *br;
33920 +
33921 +       br = au_sbr(dentry->d_sb, bdst);
33922 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
33923 +       err = PTR_ERR(h_path.dentry);
33924 +       if (IS_ERR(h_path.dentry))
33925 +               goto out;
33926 +
33927 +       err = 0;
33928 +       if (d_is_positive(h_path.dentry)) {
33929 +               h_path.mnt = au_br_mnt(br);
33930 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
33931 +                                         dentry);
33932 +       }
33933 +       dput(h_path.dentry);
33934 +
33935 +out:
33936 +       return err;
33937 +}
33938 +
33939 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
33940 +                        struct au_pin *pin,
33941 +                        struct dentry *h_parent, void *arg)
33942 +{
33943 +       int err, rerr;
33944 +       aufs_bindex_t bopq, btop;
33945 +       struct path h_path;
33946 +       struct dentry *parent;
33947 +       struct inode *h_dir, *h_inode, *inode, *dir;
33948 +       unsigned int *flags = arg;
33949 +
33950 +       btop = au_dbtop(dentry);
33951 +       /* dentry is di-locked */
33952 +       parent = dget_parent(dentry);
33953 +       dir = d_inode(parent);
33954 +       h_dir = d_inode(h_parent);
33955 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
33956 +       IMustLock(h_dir);
33957 +
33958 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
33959 +       if (unlikely(err < 0))
33960 +               goto out;
33961 +       h_path.dentry = au_h_dptr(dentry, bdst);
33962 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
33963 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
33964 +       if (unlikely(err))
33965 +               goto out_put;
33966 +       au_fset_cpdown(*flags, MADE_DIR);
33967 +
33968 +       bopq = au_dbdiropq(dentry);
33969 +       au_fclr_cpdown(*flags, WHED);
33970 +       au_fclr_cpdown(*flags, DIROPQ);
33971 +       if (au_dbwh(dentry) == bdst)
33972 +               au_fset_cpdown(*flags, WHED);
33973 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
33974 +               au_fset_cpdown(*flags, PARENT_OPQ);
33975 +       h_inode = d_inode(h_path.dentry);
33976 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
33977 +       if (au_ftest_cpdown(*flags, WHED)) {
33978 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
33979 +               if (unlikely(err)) {
33980 +                       inode_unlock(h_inode);
33981 +                       goto out_dir;
33982 +               }
33983 +       }
33984 +
33985 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
33986 +       inode_unlock(h_inode);
33987 +       if (unlikely(err))
33988 +               goto out_opq;
33989 +
33990 +       if (au_ftest_cpdown(*flags, WHED)) {
33991 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
33992 +               if (unlikely(err))
33993 +                       goto out_opq;
33994 +       }
33995 +
33996 +       inode = d_inode(dentry);
33997 +       if (au_ibbot(inode) < bdst)
33998 +               au_set_ibbot(inode, bdst);
33999 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34000 +                     au_hi_flags(inode, /*isdir*/1));
34001 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34002 +       goto out; /* success */
34003 +
34004 +       /* revert */
34005 +out_opq:
34006 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34007 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34008 +               rerr = au_diropq_remove(dentry, bdst);
34009 +               inode_unlock(h_inode);
34010 +               if (unlikely(rerr)) {
34011 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34012 +                               dentry, bdst, rerr);
34013 +                       err = -EIO;
34014 +                       goto out;
34015 +               }
34016 +       }
34017 +out_dir:
34018 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34019 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34020 +               if (unlikely(rerr)) {
34021 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34022 +                               dentry, bdst, rerr);
34023 +                       err = -EIO;
34024 +               }
34025 +       }
34026 +out_put:
34027 +       au_set_h_dptr(dentry, bdst, NULL);
34028 +       if (au_dbbot(dentry) == bdst)
34029 +               au_update_dbbot(dentry);
34030 +out:
34031 +       dput(parent);
34032 +       return err;
34033 +}
34034 +
34035 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34036 +{
34037 +       int err;
34038 +       unsigned int flags;
34039 +
34040 +       flags = 0;
34041 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34042 +
34043 +       return err;
34044 +}
34045 +
34046 +/* ---------------------------------------------------------------------- */
34047 +
34048 +/* policies for create */
34049 +
34050 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34051 +{
34052 +       int err, i, j, ndentry;
34053 +       aufs_bindex_t bopq;
34054 +       struct au_dcsub_pages dpages;
34055 +       struct au_dpage *dpage;
34056 +       struct dentry **dentries, *parent, *d;
34057 +
34058 +       err = au_dpages_init(&dpages, GFP_NOFS);
34059 +       if (unlikely(err))
34060 +               goto out;
34061 +       parent = dget_parent(dentry);
34062 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34063 +       if (unlikely(err))
34064 +               goto out_free;
34065 +
34066 +       err = bindex;
34067 +       for (i = 0; i < dpages.ndpage; i++) {
34068 +               dpage = dpages.dpages + i;
34069 +               dentries = dpage->dentries;
34070 +               ndentry = dpage->ndentry;
34071 +               for (j = 0; j < ndentry; j++) {
34072 +                       d = dentries[j];
34073 +                       di_read_lock_parent2(d, !AuLock_IR);
34074 +                       bopq = au_dbdiropq(d);
34075 +                       di_read_unlock(d, !AuLock_IR);
34076 +                       if (bopq >= 0 && bopq < err)
34077 +                               err = bopq;
34078 +               }
34079 +       }
34080 +
34081 +out_free:
34082 +       dput(parent);
34083 +       au_dpages_free(&dpages);
34084 +out:
34085 +       return err;
34086 +}
34087 +
34088 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34089 +{
34090 +       for (; bindex >= 0; bindex--)
34091 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34092 +                       return bindex;
34093 +       return -EROFS;
34094 +}
34095 +
34096 +/* top down parent */
34097 +static int au_wbr_create_tdp(struct dentry *dentry,
34098 +                            unsigned int flags __maybe_unused)
34099 +{
34100 +       int err;
34101 +       aufs_bindex_t btop, bindex;
34102 +       struct super_block *sb;
34103 +       struct dentry *parent, *h_parent;
34104 +
34105 +       sb = dentry->d_sb;
34106 +       btop = au_dbtop(dentry);
34107 +       err = btop;
34108 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34109 +               goto out;
34110 +
34111 +       err = -EROFS;
34112 +       parent = dget_parent(dentry);
34113 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34114 +               h_parent = au_h_dptr(parent, bindex);
34115 +               if (!h_parent || d_is_negative(h_parent))
34116 +                       continue;
34117 +
34118 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34119 +                       err = bindex;
34120 +                       break;
34121 +               }
34122 +       }
34123 +       dput(parent);
34124 +
34125 +       /* bottom up here */
34126 +       if (unlikely(err < 0)) {
34127 +               err = au_wbr_bu(sb, btop - 1);
34128 +               if (err >= 0)
34129 +                       err = au_wbr_nonopq(dentry, err);
34130 +       }
34131 +
34132 +out:
34133 +       AuDbg("b%d\n", err);
34134 +       return err;
34135 +}
34136 +
34137 +/* ---------------------------------------------------------------------- */
34138 +
34139 +/* an exception for the policy other than tdp */
34140 +static int au_wbr_create_exp(struct dentry *dentry)
34141 +{
34142 +       int err;
34143 +       aufs_bindex_t bwh, bdiropq;
34144 +       struct dentry *parent;
34145 +
34146 +       err = -1;
34147 +       bwh = au_dbwh(dentry);
34148 +       parent = dget_parent(dentry);
34149 +       bdiropq = au_dbdiropq(parent);
34150 +       if (bwh >= 0) {
34151 +               if (bdiropq >= 0)
34152 +                       err = min(bdiropq, bwh);
34153 +               else
34154 +                       err = bwh;
34155 +               AuDbg("%d\n", err);
34156 +       } else if (bdiropq >= 0) {
34157 +               err = bdiropq;
34158 +               AuDbg("%d\n", err);
34159 +       }
34160 +       dput(parent);
34161 +
34162 +       if (err >= 0)
34163 +               err = au_wbr_nonopq(dentry, err);
34164 +
34165 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34166 +               err = -1;
34167 +
34168 +       AuDbg("%d\n", err);
34169 +       return err;
34170 +}
34171 +
34172 +/* ---------------------------------------------------------------------- */
34173 +
34174 +/* round robin */
34175 +static int au_wbr_create_init_rr(struct super_block *sb)
34176 +{
34177 +       int err;
34178 +
34179 +       err = au_wbr_bu(sb, au_sbbot(sb));
34180 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34181 +       /* smp_mb(); */
34182 +
34183 +       AuDbg("b%d\n", err);
34184 +       return err;
34185 +}
34186 +
34187 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34188 +{
34189 +       int err, nbr;
34190 +       unsigned int u;
34191 +       aufs_bindex_t bindex, bbot;
34192 +       struct super_block *sb;
34193 +       atomic_t *next;
34194 +
34195 +       err = au_wbr_create_exp(dentry);
34196 +       if (err >= 0)
34197 +               goto out;
34198 +
34199 +       sb = dentry->d_sb;
34200 +       next = &au_sbi(sb)->si_wbr_rr_next;
34201 +       bbot = au_sbbot(sb);
34202 +       nbr = bbot + 1;
34203 +       for (bindex = 0; bindex <= bbot; bindex++) {
34204 +               if (!au_ftest_wbr(flags, DIR)) {
34205 +                       err = atomic_dec_return(next) + 1;
34206 +                       /* modulo for 0 is meaningless */
34207 +                       if (unlikely(!err))
34208 +                               err = atomic_dec_return(next) + 1;
34209 +               } else
34210 +                       err = atomic_read(next);
34211 +               AuDbg("%d\n", err);
34212 +               u = err;
34213 +               err = u % nbr;
34214 +               AuDbg("%d\n", err);
34215 +               if (!au_br_rdonly(au_sbr(sb, err)))
34216 +                       break;
34217 +               err = -EROFS;
34218 +       }
34219 +
34220 +       if (err >= 0)
34221 +               err = au_wbr_nonopq(dentry, err);
34222 +
34223 +out:
34224 +       AuDbg("%d\n", err);
34225 +       return err;
34226 +}
34227 +
34228 +/* ---------------------------------------------------------------------- */
34229 +
34230 +/* most free space */
34231 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34232 +{
34233 +       struct super_block *sb;
34234 +       struct au_branch *br;
34235 +       struct au_wbr_mfs *mfs;
34236 +       struct dentry *h_parent;
34237 +       aufs_bindex_t bindex, bbot;
34238 +       int err;
34239 +       unsigned long long b, bavail;
34240 +       struct path h_path;
34241 +       /* reduce the stack usage */
34242 +       struct kstatfs *st;
34243 +
34244 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34245 +       if (unlikely(!st)) {
34246 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34247 +               return;
34248 +       }
34249 +
34250 +       bavail = 0;
34251 +       sb = dentry->d_sb;
34252 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34253 +       MtxMustLock(&mfs->mfs_lock);
34254 +       mfs->mfs_bindex = -EROFS;
34255 +       mfs->mfsrr_bytes = 0;
34256 +       if (!parent) {
34257 +               bindex = 0;
34258 +               bbot = au_sbbot(sb);
34259 +       } else {
34260 +               bindex = au_dbtop(parent);
34261 +               bbot = au_dbtaildir(parent);
34262 +       }
34263 +
34264 +       for (; bindex <= bbot; bindex++) {
34265 +               if (parent) {
34266 +                       h_parent = au_h_dptr(parent, bindex);
34267 +                       if (!h_parent || d_is_negative(h_parent))
34268 +                               continue;
34269 +               }
34270 +               br = au_sbr(sb, bindex);
34271 +               if (au_br_rdonly(br))
34272 +                       continue;
34273 +
34274 +               /* sb->s_root for NFS is unreliable */
34275 +               h_path.mnt = au_br_mnt(br);
34276 +               h_path.dentry = h_path.mnt->mnt_root;
34277 +               err = vfs_statfs(&h_path, st);
34278 +               if (unlikely(err)) {
34279 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34280 +                       continue;
34281 +               }
34282 +
34283 +               /* when the available size is equal, select the lower one */
34284 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34285 +                            || sizeof(b) < sizeof(st->f_bsize));
34286 +               b = st->f_bavail * st->f_bsize;
34287 +               br->br_wbr->wbr_bytes = b;
34288 +               if (b >= bavail) {
34289 +                       bavail = b;
34290 +                       mfs->mfs_bindex = bindex;
34291 +                       mfs->mfs_jiffy = jiffies;
34292 +               }
34293 +       }
34294 +
34295 +       mfs->mfsrr_bytes = bavail;
34296 +       AuDbg("b%d\n", mfs->mfs_bindex);
34297 +       au_kfree_rcu(st);
34298 +}
34299 +
34300 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34301 +{
34302 +       int err;
34303 +       struct dentry *parent;
34304 +       struct super_block *sb;
34305 +       struct au_wbr_mfs *mfs;
34306 +
34307 +       err = au_wbr_create_exp(dentry);
34308 +       if (err >= 0)
34309 +               goto out;
34310 +
34311 +       sb = dentry->d_sb;
34312 +       parent = NULL;
34313 +       if (au_ftest_wbr(flags, PARENT))
34314 +               parent = dget_parent(dentry);
34315 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34316 +       mutex_lock(&mfs->mfs_lock);
34317 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34318 +           || mfs->mfs_bindex < 0
34319 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34320 +               au_mfs(dentry, parent);
34321 +       mutex_unlock(&mfs->mfs_lock);
34322 +       err = mfs->mfs_bindex;
34323 +       dput(parent);
34324 +
34325 +       if (err >= 0)
34326 +               err = au_wbr_nonopq(dentry, err);
34327 +
34328 +out:
34329 +       AuDbg("b%d\n", err);
34330 +       return err;
34331 +}
34332 +
34333 +static int au_wbr_create_init_mfs(struct super_block *sb)
34334 +{
34335 +       struct au_wbr_mfs *mfs;
34336 +
34337 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34338 +       mutex_init(&mfs->mfs_lock);
34339 +       mfs->mfs_jiffy = 0;
34340 +       mfs->mfs_bindex = -EROFS;
34341 +
34342 +       return 0;
34343 +}
34344 +
34345 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34346 +{
34347 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34348 +       return 0;
34349 +}
34350 +
34351 +/* ---------------------------------------------------------------------- */
34352 +
34353 +/* top down regardless parent, and then mfs */
34354 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34355 +                              unsigned int flags __maybe_unused)
34356 +{
34357 +       int err;
34358 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34359 +       unsigned long long watermark;
34360 +       struct super_block *sb;
34361 +       struct au_wbr_mfs *mfs;
34362 +       struct au_branch *br;
34363 +       struct dentry *parent;
34364 +
34365 +       sb = dentry->d_sb;
34366 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34367 +       mutex_lock(&mfs->mfs_lock);
34368 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34369 +           || mfs->mfs_bindex < 0)
34370 +               au_mfs(dentry, /*parent*/NULL);
34371 +       watermark = mfs->mfsrr_watermark;
34372 +       bmfs = mfs->mfs_bindex;
34373 +       mutex_unlock(&mfs->mfs_lock);
34374 +
34375 +       /* another style of au_wbr_create_exp() */
34376 +       bwh = au_dbwh(dentry);
34377 +       parent = dget_parent(dentry);
34378 +       btail = au_dbtaildir(parent);
34379 +       if (bwh >= 0 && bwh < btail)
34380 +               btail = bwh;
34381 +
34382 +       err = au_wbr_nonopq(dentry, btail);
34383 +       if (unlikely(err < 0))
34384 +               goto out;
34385 +       btail = err;
34386 +       bfound = -1;
34387 +       for (bindex = 0; bindex <= btail; bindex++) {
34388 +               br = au_sbr(sb, bindex);
34389 +               if (au_br_rdonly(br))
34390 +                       continue;
34391 +               if (br->br_wbr->wbr_bytes > watermark) {
34392 +                       bfound = bindex;
34393 +                       break;
34394 +               }
34395 +       }
34396 +       err = bfound;
34397 +       if (err < 0)
34398 +               err = bmfs;
34399 +
34400 +out:
34401 +       dput(parent);
34402 +       AuDbg("b%d\n", err);
34403 +       return err;
34404 +}
34405 +
34406 +/* ---------------------------------------------------------------------- */
34407 +
34408 +/* most free space and then round robin */
34409 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34410 +{
34411 +       int err;
34412 +       struct au_wbr_mfs *mfs;
34413 +
34414 +       err = au_wbr_create_mfs(dentry, flags);
34415 +       if (err >= 0) {
34416 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34417 +               mutex_lock(&mfs->mfs_lock);
34418 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34419 +                       err = au_wbr_create_rr(dentry, flags);
34420 +               mutex_unlock(&mfs->mfs_lock);
34421 +       }
34422 +
34423 +       AuDbg("b%d\n", err);
34424 +       return err;
34425 +}
34426 +
34427 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34428 +{
34429 +       int err;
34430 +
34431 +       au_wbr_create_init_mfs(sb); /* ignore */
34432 +       err = au_wbr_create_init_rr(sb);
34433 +
34434 +       return err;
34435 +}
34436 +
34437 +/* ---------------------------------------------------------------------- */
34438 +
34439 +/* top down parent and most free space */
34440 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34441 +{
34442 +       int err, e2;
34443 +       unsigned long long b;
34444 +       aufs_bindex_t bindex, btop, bbot;
34445 +       struct super_block *sb;
34446 +       struct dentry *parent, *h_parent;
34447 +       struct au_branch *br;
34448 +
34449 +       err = au_wbr_create_tdp(dentry, flags);
34450 +       if (unlikely(err < 0))
34451 +               goto out;
34452 +       parent = dget_parent(dentry);
34453 +       btop = au_dbtop(parent);
34454 +       bbot = au_dbtaildir(parent);
34455 +       if (btop == bbot)
34456 +               goto out_parent; /* success */
34457 +
34458 +       e2 = au_wbr_create_mfs(dentry, flags);
34459 +       if (e2 < 0)
34460 +               goto out_parent; /* success */
34461 +
34462 +       /* when the available size is equal, select upper one */
34463 +       sb = dentry->d_sb;
34464 +       br = au_sbr(sb, err);
34465 +       b = br->br_wbr->wbr_bytes;
34466 +       AuDbg("b%d, %llu\n", err, b);
34467 +
34468 +       for (bindex = btop; bindex <= bbot; bindex++) {
34469 +               h_parent = au_h_dptr(parent, bindex);
34470 +               if (!h_parent || d_is_negative(h_parent))
34471 +                       continue;
34472 +
34473 +               br = au_sbr(sb, bindex);
34474 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34475 +                       b = br->br_wbr->wbr_bytes;
34476 +                       err = bindex;
34477 +                       AuDbg("b%d, %llu\n", err, b);
34478 +               }
34479 +       }
34480 +
34481 +       if (err >= 0)
34482 +               err = au_wbr_nonopq(dentry, err);
34483 +
34484 +out_parent:
34485 +       dput(parent);
34486 +out:
34487 +       AuDbg("b%d\n", err);
34488 +       return err;
34489 +}
34490 +
34491 +/* ---------------------------------------------------------------------- */
34492 +
34493 +/*
34494 + * - top down parent
34495 + * - most free space with parent
34496 + * - most free space round-robin regardless parent
34497 + */
34498 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34499 +{
34500 +       int err;
34501 +       unsigned long long watermark;
34502 +       struct super_block *sb;
34503 +       struct au_branch *br;
34504 +       struct au_wbr_mfs *mfs;
34505 +
34506 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34507 +       if (unlikely(err < 0))
34508 +               goto out;
34509 +
34510 +       sb = dentry->d_sb;
34511 +       br = au_sbr(sb, err);
34512 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34513 +       mutex_lock(&mfs->mfs_lock);
34514 +       watermark = mfs->mfsrr_watermark;
34515 +       mutex_unlock(&mfs->mfs_lock);
34516 +       if (br->br_wbr->wbr_bytes < watermark)
34517 +               /* regardless the parent dir */
34518 +               err = au_wbr_create_mfsrr(dentry, flags);
34519 +
34520 +out:
34521 +       AuDbg("b%d\n", err);
34522 +       return err;
34523 +}
34524 +
34525 +/* ---------------------------------------------------------------------- */
34526 +
34527 +/* policies for copyup */
34528 +
34529 +/* top down parent */
34530 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34531 +{
34532 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34533 +}
34534 +
34535 +/* bottom up parent */
34536 +static int au_wbr_copyup_bup(struct dentry *dentry)
34537 +{
34538 +       int err;
34539 +       aufs_bindex_t bindex, btop;
34540 +       struct dentry *parent, *h_parent;
34541 +       struct super_block *sb;
34542 +
34543 +       err = -EROFS;
34544 +       sb = dentry->d_sb;
34545 +       parent = dget_parent(dentry);
34546 +       btop = au_dbtop(parent);
34547 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34548 +               h_parent = au_h_dptr(parent, bindex);
34549 +               if (!h_parent || d_is_negative(h_parent))
34550 +                       continue;
34551 +
34552 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34553 +                       err = bindex;
34554 +                       break;
34555 +               }
34556 +       }
34557 +       dput(parent);
34558 +
34559 +       /* bottom up here */
34560 +       if (unlikely(err < 0))
34561 +               err = au_wbr_bu(sb, btop - 1);
34562 +
34563 +       AuDbg("b%d\n", err);
34564 +       return err;
34565 +}
34566 +
34567 +/* bottom up */
34568 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34569 +{
34570 +       int err;
34571 +
34572 +       err = au_wbr_bu(dentry->d_sb, btop);
34573 +       AuDbg("b%d\n", err);
34574 +       if (err > btop)
34575 +               err = au_wbr_nonopq(dentry, err);
34576 +
34577 +       AuDbg("b%d\n", err);
34578 +       return err;
34579 +}
34580 +
34581 +static int au_wbr_copyup_bu(struct dentry *dentry)
34582 +{
34583 +       int err;
34584 +       aufs_bindex_t btop;
34585 +
34586 +       btop = au_dbtop(dentry);
34587 +       err = au_wbr_do_copyup_bu(dentry, btop);
34588 +       return err;
34589 +}
34590 +
34591 +/* ---------------------------------------------------------------------- */
34592 +
34593 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34594 +       [AuWbrCopyup_TDP] = {
34595 +               .copyup = au_wbr_copyup_tdp
34596 +       },
34597 +       [AuWbrCopyup_BUP] = {
34598 +               .copyup = au_wbr_copyup_bup
34599 +       },
34600 +       [AuWbrCopyup_BU] = {
34601 +               .copyup = au_wbr_copyup_bu
34602 +       }
34603 +};
34604 +
34605 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34606 +       [AuWbrCreate_TDP] = {
34607 +               .create = au_wbr_create_tdp
34608 +       },
34609 +       [AuWbrCreate_RR] = {
34610 +               .create = au_wbr_create_rr,
34611 +               .init   = au_wbr_create_init_rr
34612 +       },
34613 +       [AuWbrCreate_MFS] = {
34614 +               .create = au_wbr_create_mfs,
34615 +               .init   = au_wbr_create_init_mfs,
34616 +               .fin    = au_wbr_create_fin_mfs
34617 +       },
34618 +       [AuWbrCreate_MFSV] = {
34619 +               .create = au_wbr_create_mfs,
34620 +               .init   = au_wbr_create_init_mfs,
34621 +               .fin    = au_wbr_create_fin_mfs
34622 +       },
34623 +       [AuWbrCreate_MFSRR] = {
34624 +               .create = au_wbr_create_mfsrr,
34625 +               .init   = au_wbr_create_init_mfsrr,
34626 +               .fin    = au_wbr_create_fin_mfs
34627 +       },
34628 +       [AuWbrCreate_MFSRRV] = {
34629 +               .create = au_wbr_create_mfsrr,
34630 +               .init   = au_wbr_create_init_mfsrr,
34631 +               .fin    = au_wbr_create_fin_mfs
34632 +       },
34633 +       [AuWbrCreate_TDMFS] = {
34634 +               .create = au_wbr_create_tdmfs,
34635 +               .init   = au_wbr_create_init_mfs,
34636 +               .fin    = au_wbr_create_fin_mfs
34637 +       },
34638 +       [AuWbrCreate_TDMFSV] = {
34639 +               .create = au_wbr_create_tdmfs,
34640 +               .init   = au_wbr_create_init_mfs,
34641 +               .fin    = au_wbr_create_fin_mfs
34642 +       },
34643 +       [AuWbrCreate_PMFS] = {
34644 +               .create = au_wbr_create_pmfs,
34645 +               .init   = au_wbr_create_init_mfs,
34646 +               .fin    = au_wbr_create_fin_mfs
34647 +       },
34648 +       [AuWbrCreate_PMFSV] = {
34649 +               .create = au_wbr_create_pmfs,
34650 +               .init   = au_wbr_create_init_mfs,
34651 +               .fin    = au_wbr_create_fin_mfs
34652 +       },
34653 +       [AuWbrCreate_PMFSRR] = {
34654 +               .create = au_wbr_create_pmfsrr,
34655 +               .init   = au_wbr_create_init_mfsrr,
34656 +               .fin    = au_wbr_create_fin_mfs
34657 +       },
34658 +       [AuWbrCreate_PMFSRRV] = {
34659 +               .create = au_wbr_create_pmfsrr,
34660 +               .init   = au_wbr_create_init_mfsrr,
34661 +               .fin    = au_wbr_create_fin_mfs
34662 +       }
34663 +};
34664 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34665 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34666 +++ linux/fs/aufs/whout.c       2020-01-27 10:57:18.178871751 +0100
34667 @@ -0,0 +1,1062 @@
34668 +// SPDX-License-Identifier: GPL-2.0
34669 +/*
34670 + * Copyright (C) 2005-2020 Junjiro R. Okajima
34671 + *
34672 + * This program, aufs is free software; you can redistribute it and/or modify
34673 + * it under the terms of the GNU General Public License as published by
34674 + * the Free Software Foundation; either version 2 of the License, or
34675 + * (at your option) any later version.
34676 + *
34677 + * This program is distributed in the hope that it will be useful,
34678 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34679 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34680 + * GNU General Public License for more details.
34681 + *
34682 + * You should have received a copy of the GNU General Public License
34683 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34684 + */
34685 +
34686 +/*
34687 + * whiteout for logical deletion and opaque directory
34688 + */
34689 +
34690 +#include "aufs.h"
34691 +
34692 +#define WH_MASK                        0444
34693 +
34694 +/*
34695 + * If a directory contains this file, then it is opaque.  We start with the
34696 + * .wh. flag so that it is blocked by lookup.
34697 + */
34698 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34699 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34700 +
34701 +/*
34702 + * generate whiteout name, which is NOT terminated by NULL.
34703 + * @name: original d_name.name
34704 + * @len: original d_name.len
34705 + * @wh: whiteout qstr
34706 + * returns zero when succeeds, otherwise error.
34707 + * succeeded value as wh->name should be freed by kfree().
34708 + */
34709 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34710 +{
34711 +       char *p;
34712 +
34713 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34714 +               return -ENAMETOOLONG;
34715 +
34716 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34717 +       p = kmalloc(wh->len, GFP_NOFS);
34718 +       wh->name = p;
34719 +       if (p) {
34720 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34721 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34722 +               /* smp_mb(); */
34723 +               return 0;
34724 +       }
34725 +       return -ENOMEM;
34726 +}
34727 +
34728 +/* ---------------------------------------------------------------------- */
34729 +
34730 +/*
34731 + * test if the @wh_name exists under @h_parent.
34732 + * @try_sio specifies the necessary of super-io.
34733 + */
34734 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
34735 +{
34736 +       int err;
34737 +       struct dentry *wh_dentry;
34738 +
34739 +       if (!try_sio)
34740 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
34741 +       else
34742 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
34743 +       err = PTR_ERR(wh_dentry);
34744 +       if (IS_ERR(wh_dentry)) {
34745 +               if (err == -ENAMETOOLONG)
34746 +                       err = 0;
34747 +               goto out;
34748 +       }
34749 +
34750 +       err = 0;
34751 +       if (d_is_negative(wh_dentry))
34752 +               goto out_wh; /* success */
34753 +
34754 +       err = 1;
34755 +       if (d_is_reg(wh_dentry))
34756 +               goto out_wh; /* success */
34757 +
34758 +       err = -EIO;
34759 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34760 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34761 +
34762 +out_wh:
34763 +       dput(wh_dentry);
34764 +out:
34765 +       return err;
34766 +}
34767 +
34768 +/*
34769 + * test if the @h_dentry sets opaque or not.
34770 + */
34771 +int au_diropq_test(struct dentry *h_dentry)
34772 +{
34773 +       int err;
34774 +       struct inode *h_dir;
34775 +
34776 +       h_dir = d_inode(h_dentry);
34777 +       err = au_wh_test(h_dentry, &diropq_name,
34778 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
34779 +       return err;
34780 +}
34781 +
34782 +/*
34783 + * returns a negative dentry whose name is unique and temporary.
34784 + */
34785 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34786 +                            struct qstr *prefix)
34787 +{
34788 +       struct dentry *dentry;
34789 +       int i;
34790 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34791 +               *name, *p;
34792 +       /* strict atomic_t is unnecessary here */
34793 +       static unsigned short cnt;
34794 +       struct qstr qs;
34795 +
34796 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34797 +
34798 +       name = defname;
34799 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34800 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34801 +               dentry = ERR_PTR(-ENAMETOOLONG);
34802 +               if (unlikely(qs.len > NAME_MAX))
34803 +                       goto out;
34804 +               dentry = ERR_PTR(-ENOMEM);
34805 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34806 +               if (unlikely(!name))
34807 +                       goto out;
34808 +       }
34809 +
34810 +       /* doubly whiteout-ed */
34811 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34812 +       p = name + AUFS_WH_PFX_LEN * 2;
34813 +       memcpy(p, prefix->name, prefix->len);
34814 +       p += prefix->len;
34815 +       *p++ = '.';
34816 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34817 +
34818 +       qs.name = name;
34819 +       for (i = 0; i < 3; i++) {
34820 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34821 +               dentry = au_sio_lkup_one(&qs, h_parent);
34822 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34823 +                       goto out_name;
34824 +               dput(dentry);
34825 +       }
34826 +       /* pr_warn("could not get random name\n"); */
34827 +       dentry = ERR_PTR(-EEXIST);
34828 +       AuDbg("%.*s\n", AuLNPair(&qs));
34829 +       BUG();
34830 +
34831 +out_name:
34832 +       if (name != defname)
34833 +               au_kfree_try_rcu(name);
34834 +out:
34835 +       AuTraceErrPtr(dentry);
34836 +       return dentry;
34837 +}
34838 +
34839 +/*
34840 + * rename the @h_dentry on @br to the whiteouted temporary name.
34841 + */
34842 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34843 +{
34844 +       int err;
34845 +       struct path h_path = {
34846 +               .mnt = au_br_mnt(br)
34847 +       };
34848 +       struct inode *h_dir, *delegated;
34849 +       struct dentry *h_parent;
34850 +
34851 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34852 +       h_dir = d_inode(h_parent);
34853 +       IMustLock(h_dir);
34854 +
34855 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34856 +       err = PTR_ERR(h_path.dentry);
34857 +       if (IS_ERR(h_path.dentry))
34858 +               goto out;
34859 +
34860 +       /* under the same dir, no need to lock_rename() */
34861 +       delegated = NULL;
34862 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34863 +                          /*flags*/0);
34864 +       AuTraceErr(err);
34865 +       if (unlikely(err == -EWOULDBLOCK)) {
34866 +               pr_warn("cannot retry for NFSv4 delegation"
34867 +                       " for an internal rename\n");
34868 +               iput(delegated);
34869 +       }
34870 +       dput(h_path.dentry);
34871 +
34872 +out:
34873 +       AuTraceErr(err);
34874 +       return err;
34875 +}
34876 +
34877 +/* ---------------------------------------------------------------------- */
34878 +/*
34879 + * functions for removing a whiteout
34880 + */
34881 +
34882 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
34883 +{
34884 +       int err, force;
34885 +       struct inode *delegated;
34886 +
34887 +       /*
34888 +        * forces superio when the dir has a sticky bit.
34889 +        * this may be a violation of unix fs semantics.
34890 +        */
34891 +       force = (h_dir->i_mode & S_ISVTX)
34892 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
34893 +       delegated = NULL;
34894 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
34895 +       if (unlikely(err == -EWOULDBLOCK)) {
34896 +               pr_warn("cannot retry for NFSv4 delegation"
34897 +                       " for an internal unlink\n");
34898 +               iput(delegated);
34899 +       }
34900 +       return err;
34901 +}
34902 +
34903 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
34904 +                       struct dentry *dentry)
34905 +{
34906 +       int err;
34907 +
34908 +       err = do_unlink_wh(h_dir, h_path);
34909 +       if (!err && dentry)
34910 +               au_set_dbwh(dentry, -1);
34911 +
34912 +       return err;
34913 +}
34914 +
34915 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
34916 +                         struct au_branch *br)
34917 +{
34918 +       int err;
34919 +       struct path h_path = {
34920 +               .mnt = au_br_mnt(br)
34921 +       };
34922 +
34923 +       err = 0;
34924 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
34925 +       if (IS_ERR(h_path.dentry))
34926 +               err = PTR_ERR(h_path.dentry);
34927 +       else {
34928 +               if (d_is_reg(h_path.dentry))
34929 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
34930 +               dput(h_path.dentry);
34931 +       }
34932 +
34933 +       return err;
34934 +}
34935 +
34936 +/* ---------------------------------------------------------------------- */
34937 +/*
34938 + * initialize/clean whiteout for a branch
34939 + */
34940 +
34941 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
34942 +                       const int isdir)
34943 +{
34944 +       int err;
34945 +       struct inode *delegated;
34946 +
34947 +       if (d_is_negative(whpath->dentry))
34948 +               return;
34949 +
34950 +       if (isdir)
34951 +               err = vfsub_rmdir(h_dir, whpath);
34952 +       else {
34953 +               delegated = NULL;
34954 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
34955 +               if (unlikely(err == -EWOULDBLOCK)) {
34956 +                       pr_warn("cannot retry for NFSv4 delegation"
34957 +                               " for an internal unlink\n");
34958 +                       iput(delegated);
34959 +               }
34960 +       }
34961 +       if (unlikely(err))
34962 +               pr_warn("failed removing %pd (%d), ignored.\n",
34963 +                       whpath->dentry, err);
34964 +}
34965 +
34966 +static int test_linkable(struct dentry *h_root)
34967 +{
34968 +       struct inode *h_dir = d_inode(h_root);
34969 +
34970 +       if (h_dir->i_op->link)
34971 +               return 0;
34972 +
34973 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
34974 +              h_root, au_sbtype(h_root->d_sb));
34975 +       return -ENOSYS; /* the branch doesn't have its ->link() */
34976 +}
34977 +
34978 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
34979 +static int au_whdir(struct inode *h_dir, struct path *path)
34980 +{
34981 +       int err;
34982 +
34983 +       err = -EEXIST;
34984 +       if (d_is_negative(path->dentry)) {
34985 +               int mode = 0700;
34986 +
34987 +               if (au_test_nfs(path->dentry->d_sb))
34988 +                       mode |= 0111;
34989 +               err = vfsub_mkdir(h_dir, path, mode);
34990 +       } else if (d_is_dir(path->dentry))
34991 +               err = 0;
34992 +       else
34993 +               pr_err("unknown %pd exists\n", path->dentry);
34994 +
34995 +       return err;
34996 +}
34997 +
34998 +struct au_wh_base {
34999 +       const struct qstr *name;
35000 +       struct dentry *dentry;
35001 +};
35002 +
35003 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35004 +                         struct path *h_path)
35005 +{
35006 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35007 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35008 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35009 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35010 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35011 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35012 +}
35013 +
35014 +/*
35015 + * returns tri-state,
35016 + * minus: error, caller should print the message
35017 + * zero: success
35018 + * plus: error, caller should NOT print the message
35019 + */
35020 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35021 +                               int do_plink, struct au_wh_base base[],
35022 +                               struct path *h_path)
35023 +{
35024 +       int err;
35025 +       struct inode *h_dir;
35026 +
35027 +       h_dir = d_inode(h_root);
35028 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35029 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35030 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35031 +       if (do_plink) {
35032 +               err = test_linkable(h_root);
35033 +               if (unlikely(err)) {
35034 +                       err = 1;
35035 +                       goto out;
35036 +               }
35037 +
35038 +               err = au_whdir(h_dir, h_path);
35039 +               if (unlikely(err))
35040 +                       goto out;
35041 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35042 +       } else
35043 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35044 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35045 +       err = au_whdir(h_dir, h_path);
35046 +       if (unlikely(err))
35047 +               goto out;
35048 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35049 +
35050 +out:
35051 +       return err;
35052 +}
35053 +
35054 +/*
35055 + * for the moment, aufs supports the branch filesystem which does not support
35056 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35057 + * copyup failed. finally, such filesystem will not be used as the writable
35058 + * branch.
35059 + *
35060 + * returns tri-state, see above.
35061 + */
35062 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35063 +                        int do_plink, struct au_wh_base base[],
35064 +                        struct path *h_path)
35065 +{
35066 +       int err;
35067 +       struct inode *h_dir;
35068 +
35069 +       WbrWhMustWriteLock(wbr);
35070 +
35071 +       err = test_linkable(h_root);
35072 +       if (unlikely(err)) {
35073 +               err = 1;
35074 +               goto out;
35075 +       }
35076 +
35077 +       /*
35078 +        * todo: should this create be done in /sbin/mount.aufs helper?
35079 +        */
35080 +       err = -EEXIST;
35081 +       h_dir = d_inode(h_root);
35082 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35083 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35084 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35085 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35086 +               err = 0;
35087 +       else
35088 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35089 +       if (unlikely(err))
35090 +               goto out;
35091 +
35092 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35093 +       if (do_plink) {
35094 +               err = au_whdir(h_dir, h_path);
35095 +               if (unlikely(err))
35096 +                       goto out;
35097 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35098 +       } else
35099 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35100 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35101 +
35102 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35103 +       err = au_whdir(h_dir, h_path);
35104 +       if (unlikely(err))
35105 +               goto out;
35106 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35107 +
35108 +out:
35109 +       return err;
35110 +}
35111 +
35112 +/*
35113 + * initialize the whiteout base file/dir for @br.
35114 + */
35115 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35116 +{
35117 +       int err, i;
35118 +       const unsigned char do_plink
35119 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35120 +       struct inode *h_dir;
35121 +       struct path path = br->br_path;
35122 +       struct dentry *h_root = path.dentry;
35123 +       struct au_wbr *wbr = br->br_wbr;
35124 +       static const struct qstr base_name[] = {
35125 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35126 +                                         sizeof(AUFS_BASE_NAME) - 1),
35127 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35128 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35129 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35130 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35131 +       };
35132 +       struct au_wh_base base[] = {
35133 +               [AuBrWh_BASE] = {
35134 +                       .name   = base_name + AuBrWh_BASE,
35135 +                       .dentry = NULL
35136 +               },
35137 +               [AuBrWh_PLINK] = {
35138 +                       .name   = base_name + AuBrWh_PLINK,
35139 +                       .dentry = NULL
35140 +               },
35141 +               [AuBrWh_ORPH] = {
35142 +                       .name   = base_name + AuBrWh_ORPH,
35143 +                       .dentry = NULL
35144 +               }
35145 +       };
35146 +
35147 +       if (wbr)
35148 +               WbrWhMustWriteLock(wbr);
35149 +
35150 +       for (i = 0; i < AuBrWh_Last; i++) {
35151 +               /* doubly whiteouted */
35152 +               struct dentry *d;
35153 +
35154 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35155 +               err = PTR_ERR(d);
35156 +               if (IS_ERR(d))
35157 +                       goto out;
35158 +
35159 +               base[i].dentry = d;
35160 +               AuDebugOn(wbr
35161 +                         && wbr->wbr_wh[i]
35162 +                         && wbr->wbr_wh[i] != base[i].dentry);
35163 +       }
35164 +
35165 +       if (wbr)
35166 +               for (i = 0; i < AuBrWh_Last; i++) {
35167 +                       dput(wbr->wbr_wh[i]);
35168 +                       wbr->wbr_wh[i] = NULL;
35169 +               }
35170 +
35171 +       err = 0;
35172 +       if (!au_br_writable(br->br_perm)) {
35173 +               h_dir = d_inode(h_root);
35174 +               au_wh_init_ro(h_dir, base, &path);
35175 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35176 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35177 +               if (err > 0)
35178 +                       goto out;
35179 +               else if (err)
35180 +                       goto out_err;
35181 +       } else {
35182 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35183 +               if (err > 0)
35184 +                       goto out;
35185 +               else if (err)
35186 +                       goto out_err;
35187 +       }
35188 +       goto out; /* success */
35189 +
35190 +out_err:
35191 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35192 +              err, h_root, au_sbtype(h_root->d_sb));
35193 +out:
35194 +       for (i = 0; i < AuBrWh_Last; i++)
35195 +               dput(base[i].dentry);
35196 +       return err;
35197 +}
35198 +
35199 +/* ---------------------------------------------------------------------- */
35200 +/*
35201 + * whiteouts are all hard-linked usually.
35202 + * when its link count reaches a ceiling, we create a new whiteout base
35203 + * asynchronously.
35204 + */
35205 +
35206 +struct reinit_br_wh {
35207 +       struct super_block *sb;
35208 +       struct au_branch *br;
35209 +};
35210 +
35211 +static void reinit_br_wh(void *arg)
35212 +{
35213 +       int err;
35214 +       aufs_bindex_t bindex;
35215 +       struct path h_path;
35216 +       struct reinit_br_wh *a = arg;
35217 +       struct au_wbr *wbr;
35218 +       struct inode *dir, *delegated;
35219 +       struct dentry *h_root;
35220 +       struct au_hinode *hdir;
35221 +
35222 +       err = 0;
35223 +       wbr = a->br->br_wbr;
35224 +       /* big aufs lock */
35225 +       si_noflush_write_lock(a->sb);
35226 +       if (!au_br_writable(a->br->br_perm))
35227 +               goto out;
35228 +       bindex = au_br_index(a->sb, a->br->br_id);
35229 +       if (unlikely(bindex < 0))
35230 +               goto out;
35231 +
35232 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35233 +       dir = d_inode(a->sb->s_root);
35234 +       hdir = au_hi(dir, bindex);
35235 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35236 +       AuDebugOn(h_root != au_br_dentry(a->br));
35237 +
35238 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35239 +       wbr_wh_write_lock(wbr);
35240 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35241 +                         h_root, a->br);
35242 +       if (!err) {
35243 +               h_path.dentry = wbr->wbr_whbase;
35244 +               h_path.mnt = au_br_mnt(a->br);
35245 +               delegated = NULL;
35246 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35247 +                                  /*force*/0);
35248 +               if (unlikely(err == -EWOULDBLOCK)) {
35249 +                       pr_warn("cannot retry for NFSv4 delegation"
35250 +                               " for an internal unlink\n");
35251 +                       iput(delegated);
35252 +               }
35253 +       } else {
35254 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35255 +               err = 0;
35256 +       }
35257 +       dput(wbr->wbr_whbase);
35258 +       wbr->wbr_whbase = NULL;
35259 +       if (!err)
35260 +               err = au_wh_init(a->br, a->sb);
35261 +       wbr_wh_write_unlock(wbr);
35262 +       au_hn_inode_unlock(hdir);
35263 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35264 +       if (!err)
35265 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35266 +
35267 +out:
35268 +       if (wbr)
35269 +               atomic_dec(&wbr->wbr_wh_running);
35270 +       au_lcnt_dec(&a->br->br_count);
35271 +       si_write_unlock(a->sb);
35272 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35273 +       au_kfree_rcu(a);
35274 +       if (unlikely(err))
35275 +               AuIOErr("err %d\n", err);
35276 +}
35277 +
35278 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35279 +{
35280 +       int do_dec, wkq_err;
35281 +       struct reinit_br_wh *arg;
35282 +
35283 +       do_dec = 1;
35284 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35285 +               goto out;
35286 +
35287 +       /* ignore ENOMEM */
35288 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35289 +       if (arg) {
35290 +               /*
35291 +                * dec(wh_running), kfree(arg) and dec(br_count)
35292 +                * in reinit function
35293 +                */
35294 +               arg->sb = sb;
35295 +               arg->br = br;
35296 +               au_lcnt_inc(&br->br_count);
35297 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35298 +               if (unlikely(wkq_err)) {
35299 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35300 +                       au_lcnt_dec(&br->br_count);
35301 +                       au_kfree_rcu(arg);
35302 +               }
35303 +               do_dec = 0;
35304 +       }
35305 +
35306 +out:
35307 +       if (do_dec)
35308 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35309 +}
35310 +
35311 +/* ---------------------------------------------------------------------- */
35312 +
35313 +/*
35314 + * create the whiteout @wh.
35315 + */
35316 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35317 +                            struct dentry *wh)
35318 +{
35319 +       int err;
35320 +       struct path h_path = {
35321 +               .dentry = wh
35322 +       };
35323 +       struct au_branch *br;
35324 +       struct au_wbr *wbr;
35325 +       struct dentry *h_parent;
35326 +       struct inode *h_dir, *delegated;
35327 +
35328 +       h_parent = wh->d_parent; /* dir inode is locked */
35329 +       h_dir = d_inode(h_parent);
35330 +       IMustLock(h_dir);
35331 +
35332 +       br = au_sbr(sb, bindex);
35333 +       h_path.mnt = au_br_mnt(br);
35334 +       wbr = br->br_wbr;
35335 +       wbr_wh_read_lock(wbr);
35336 +       if (wbr->wbr_whbase) {
35337 +               delegated = NULL;
35338 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35339 +               if (unlikely(err == -EWOULDBLOCK)) {
35340 +                       pr_warn("cannot retry for NFSv4 delegation"
35341 +                               " for an internal link\n");
35342 +                       iput(delegated);
35343 +               }
35344 +               if (!err || err != -EMLINK)
35345 +                       goto out;
35346 +
35347 +               /* link count full. re-initialize br_whbase. */
35348 +               kick_reinit_br_wh(sb, br);
35349 +       }
35350 +
35351 +       /* return this error in this context */
35352 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35353 +       if (!err)
35354 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35355 +
35356 +out:
35357 +       wbr_wh_read_unlock(wbr);
35358 +       return err;
35359 +}
35360 +
35361 +/* ---------------------------------------------------------------------- */
35362 +
35363 +/*
35364 + * create or remove the diropq.
35365 + */
35366 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35367 +                               unsigned int flags)
35368 +{
35369 +       struct dentry *opq_dentry, *h_dentry;
35370 +       struct super_block *sb;
35371 +       struct au_branch *br;
35372 +       int err;
35373 +
35374 +       sb = dentry->d_sb;
35375 +       br = au_sbr(sb, bindex);
35376 +       h_dentry = au_h_dptr(dentry, bindex);
35377 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
35378 +       if (IS_ERR(opq_dentry))
35379 +               goto out;
35380 +
35381 +       if (au_ftest_diropq(flags, CREATE)) {
35382 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35383 +               if (!err) {
35384 +                       au_set_dbdiropq(dentry, bindex);
35385 +                       goto out; /* success */
35386 +               }
35387 +       } else {
35388 +               struct path tmp = {
35389 +                       .dentry = opq_dentry,
35390 +                       .mnt    = au_br_mnt(br)
35391 +               };
35392 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
35393 +               if (!err)
35394 +                       au_set_dbdiropq(dentry, -1);
35395 +       }
35396 +       dput(opq_dentry);
35397 +       opq_dentry = ERR_PTR(err);
35398 +
35399 +out:
35400 +       return opq_dentry;
35401 +}
35402 +
35403 +struct do_diropq_args {
35404 +       struct dentry **errp;
35405 +       struct dentry *dentry;
35406 +       aufs_bindex_t bindex;
35407 +       unsigned int flags;
35408 +};
35409 +
35410 +static void call_do_diropq(void *args)
35411 +{
35412 +       struct do_diropq_args *a = args;
35413 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35414 +}
35415 +
35416 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35417 +                            unsigned int flags)
35418 +{
35419 +       struct dentry *diropq, *h_dentry;
35420 +
35421 +       h_dentry = au_h_dptr(dentry, bindex);
35422 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
35423 +               diropq = do_diropq(dentry, bindex, flags);
35424 +       else {
35425 +               int wkq_err;
35426 +               struct do_diropq_args args = {
35427 +                       .errp           = &diropq,
35428 +                       .dentry         = dentry,
35429 +                       .bindex         = bindex,
35430 +                       .flags          = flags
35431 +               };
35432 +
35433 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35434 +               if (unlikely(wkq_err))
35435 +                       diropq = ERR_PTR(wkq_err);
35436 +       }
35437 +
35438 +       return diropq;
35439 +}
35440 +
35441 +/* ---------------------------------------------------------------------- */
35442 +
35443 +/*
35444 + * lookup whiteout dentry.
35445 + * @h_parent: lower parent dentry which must exist and be locked
35446 + * @base_name: name of dentry which will be whiteouted
35447 + * returns dentry for whiteout.
35448 + */
35449 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35450 +                         struct au_branch *br)
35451 +{
35452 +       int err;
35453 +       struct qstr wh_name;
35454 +       struct dentry *wh_dentry;
35455 +
35456 +       err = au_wh_name_alloc(&wh_name, base_name);
35457 +       wh_dentry = ERR_PTR(err);
35458 +       if (!err) {
35459 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
35460 +               au_kfree_try_rcu(wh_name.name);
35461 +       }
35462 +       return wh_dentry;
35463 +}
35464 +
35465 +/*
35466 + * link/create a whiteout for @dentry on @bindex.
35467 + */
35468 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35469 +                           struct dentry *h_parent)
35470 +{
35471 +       struct dentry *wh_dentry;
35472 +       struct super_block *sb;
35473 +       int err;
35474 +
35475 +       sb = dentry->d_sb;
35476 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35477 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35478 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35479 +               if (!err) {
35480 +                       au_set_dbwh(dentry, bindex);
35481 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35482 +               } else {
35483 +                       dput(wh_dentry);
35484 +                       wh_dentry = ERR_PTR(err);
35485 +               }
35486 +       }
35487 +
35488 +       return wh_dentry;
35489 +}
35490 +
35491 +/* ---------------------------------------------------------------------- */
35492 +
35493 +/* Delete all whiteouts in this directory on branch bindex. */
35494 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
35495 +                          aufs_bindex_t bindex, struct au_branch *br)
35496 +{
35497 +       int err;
35498 +       unsigned long ul, n;
35499 +       struct qstr wh_name;
35500 +       char *p;
35501 +       struct hlist_head *head;
35502 +       struct au_vdir_wh *pos;
35503 +       struct au_vdir_destr *str;
35504 +
35505 +       err = -ENOMEM;
35506 +       p = (void *)__get_free_page(GFP_NOFS);
35507 +       wh_name.name = p;
35508 +       if (unlikely(!wh_name.name))
35509 +               goto out;
35510 +
35511 +       err = 0;
35512 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35513 +       p += AUFS_WH_PFX_LEN;
35514 +       n = whlist->nh_num;
35515 +       head = whlist->nh_head;
35516 +       for (ul = 0; !err && ul < n; ul++, head++) {
35517 +               hlist_for_each_entry(pos, head, wh_hash) {
35518 +                       if (pos->wh_bindex != bindex)
35519 +                               continue;
35520 +
35521 +                       str = &pos->wh_str;
35522 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35523 +                               memcpy(p, str->name, str->len);
35524 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35525 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
35526 +                               if (!err)
35527 +                                       continue;
35528 +                               break;
35529 +                       }
35530 +                       AuIOErr("whiteout name too long %.*s\n",
35531 +                               str->len, str->name);
35532 +                       err = -EIO;
35533 +                       break;
35534 +               }
35535 +       }
35536 +       free_page((unsigned long)wh_name.name);
35537 +
35538 +out:
35539 +       return err;
35540 +}
35541 +
35542 +struct del_wh_children_args {
35543 +       int *errp;
35544 +       struct dentry *h_dentry;
35545 +       struct au_nhash *whlist;
35546 +       aufs_bindex_t bindex;
35547 +       struct au_branch *br;
35548 +};
35549 +
35550 +static void call_del_wh_children(void *args)
35551 +{
35552 +       struct del_wh_children_args *a = args;
35553 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
35554 +}
35555 +
35556 +/* ---------------------------------------------------------------------- */
35557 +
35558 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35559 +{
35560 +       struct au_whtmp_rmdir *whtmp;
35561 +       int err;
35562 +       unsigned int rdhash;
35563 +
35564 +       SiMustAnyLock(sb);
35565 +
35566 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35567 +       if (unlikely(!whtmp)) {
35568 +               whtmp = ERR_PTR(-ENOMEM);
35569 +               goto out;
35570 +       }
35571 +
35572 +       /* no estimation for dir size */
35573 +       rdhash = au_sbi(sb)->si_rdhash;
35574 +       if (!rdhash)
35575 +               rdhash = AUFS_RDHASH_DEF;
35576 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35577 +       if (unlikely(err)) {
35578 +               au_kfree_rcu(whtmp);
35579 +               whtmp = ERR_PTR(err);
35580 +       }
35581 +
35582 +out:
35583 +       return whtmp;
35584 +}
35585 +
35586 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35587 +{
35588 +       if (whtmp->br)
35589 +               au_lcnt_dec(&whtmp->br->br_count);
35590 +       dput(whtmp->wh_dentry);
35591 +       iput(whtmp->dir);
35592 +       au_nhash_wh_free(&whtmp->whlist);
35593 +       au_kfree_rcu(whtmp);
35594 +}
35595 +
35596 +/*
35597 + * rmdir the whiteouted temporary named dir @h_dentry.
35598 + * @whlist: whiteouted children.
35599 + */
35600 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35601 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35602 +{
35603 +       int err;
35604 +       unsigned int h_nlink;
35605 +       struct path h_tmp;
35606 +       struct inode *wh_inode, *h_dir;
35607 +       struct au_branch *br;
35608 +
35609 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35610 +       IMustLock(h_dir);
35611 +
35612 +       br = au_sbr(dir->i_sb, bindex);
35613 +       wh_inode = d_inode(wh_dentry);
35614 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35615 +
35616 +       /*
35617 +        * someone else might change some whiteouts while we were sleeping.
35618 +        * it means this whlist may have an obsoleted entry.
35619 +        */
35620 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
35621 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
35622 +       else {
35623 +               int wkq_err;
35624 +               struct del_wh_children_args args = {
35625 +                       .errp           = &err,
35626 +                       .h_dentry       = wh_dentry,
35627 +                       .whlist         = whlist,
35628 +                       .bindex         = bindex,
35629 +                       .br             = br
35630 +               };
35631 +
35632 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35633 +               if (unlikely(wkq_err))
35634 +                       err = wkq_err;
35635 +       }
35636 +       inode_unlock(wh_inode);
35637 +
35638 +       if (!err) {
35639 +               h_tmp.dentry = wh_dentry;
35640 +               h_tmp.mnt = au_br_mnt(br);
35641 +               h_nlink = h_dir->i_nlink;
35642 +               err = vfsub_rmdir(h_dir, &h_tmp);
35643 +               /* some fs doesn't change the parent nlink in some cases */
35644 +               h_nlink -= h_dir->i_nlink;
35645 +       }
35646 +
35647 +       if (!err) {
35648 +               if (au_ibtop(dir) == bindex) {
35649 +                       /* todo: dir->i_mutex is necessary */
35650 +                       au_cpup_attr_timesizes(dir);
35651 +                       if (h_nlink)
35652 +                               vfsub_drop_nlink(dir);
35653 +               }
35654 +               return 0; /* success */
35655 +       }
35656 +
35657 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35658 +       return err;
35659 +}
35660 +
35661 +static void call_rmdir_whtmp(void *args)
35662 +{
35663 +       int err;
35664 +       aufs_bindex_t bindex;
35665 +       struct au_whtmp_rmdir *a = args;
35666 +       struct super_block *sb;
35667 +       struct dentry *h_parent;
35668 +       struct inode *h_dir;
35669 +       struct au_hinode *hdir;
35670 +
35671 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35672 +       /* inode_lock(a->dir); */
35673 +       err = -EROFS;
35674 +       sb = a->dir->i_sb;
35675 +       si_read_lock(sb, !AuLock_FLUSH);
35676 +       if (!au_br_writable(a->br->br_perm))
35677 +               goto out;
35678 +       bindex = au_br_index(sb, a->br->br_id);
35679 +       if (unlikely(bindex < 0))
35680 +               goto out;
35681 +
35682 +       err = -EIO;
35683 +       ii_write_lock_parent(a->dir);
35684 +       h_parent = dget_parent(a->wh_dentry);
35685 +       h_dir = d_inode(h_parent);
35686 +       hdir = au_hi(a->dir, bindex);
35687 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35688 +       if (unlikely(err))
35689 +               goto out_mnt;
35690 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35691 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35692 +                         a->br);
35693 +       if (!err)
35694 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35695 +       au_hn_inode_unlock(hdir);
35696 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35697 +
35698 +out_mnt:
35699 +       dput(h_parent);
35700 +       ii_write_unlock(a->dir);
35701 +out:
35702 +       /* inode_unlock(a->dir); */
35703 +       au_whtmp_rmdir_free(a);
35704 +       si_read_unlock(sb);
35705 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35706 +       if (unlikely(err))
35707 +               AuIOErr("err %d\n", err);
35708 +}
35709 +
35710 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35711 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35712 +{
35713 +       int wkq_err;
35714 +       struct super_block *sb;
35715 +
35716 +       IMustLock(dir);
35717 +
35718 +       /* all post-process will be done in do_rmdir_whtmp(). */
35719 +       sb = dir->i_sb;
35720 +       args->dir = au_igrab(dir);
35721 +       args->br = au_sbr(sb, bindex);
35722 +       au_lcnt_inc(&args->br->br_count);
35723 +       args->wh_dentry = dget(wh_dentry);
35724 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35725 +       if (unlikely(wkq_err)) {
35726 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35727 +               au_whtmp_rmdir_free(args);
35728 +       }
35729 +}
35730 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35731 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35732 +++ linux/fs/aufs/whout.h       2020-01-27 10:57:18.178871751 +0100
35733 @@ -0,0 +1,86 @@
35734 +/* SPDX-License-Identifier: GPL-2.0 */
35735 +/*
35736 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35737 + *
35738 + * This program, aufs is free software; you can redistribute it and/or modify
35739 + * it under the terms of the GNU General Public License as published by
35740 + * the Free Software Foundation; either version 2 of the License, or
35741 + * (at your option) any later version.
35742 + *
35743 + * This program is distributed in the hope that it will be useful,
35744 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35745 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35746 + * GNU General Public License for more details.
35747 + *
35748 + * You should have received a copy of the GNU General Public License
35749 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35750 + */
35751 +
35752 +/*
35753 + * whiteout for logical deletion and opaque directory
35754 + */
35755 +
35756 +#ifndef __AUFS_WHOUT_H__
35757 +#define __AUFS_WHOUT_H__
35758 +
35759 +#ifdef __KERNEL__
35760 +
35761 +#include "dir.h"
35762 +
35763 +/* whout.c */
35764 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35765 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
35766 +int au_diropq_test(struct dentry *h_dentry);
35767 +struct au_branch;
35768 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35769 +                            struct qstr *prefix);
35770 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
35771 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35772 +                       struct dentry *dentry);
35773 +int au_wh_init(struct au_branch *br, struct super_block *sb);
35774 +
35775 +/* diropq flags */
35776 +#define AuDiropq_CREATE        1
35777 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
35778 +#define au_fset_diropq(flags, name) \
35779 +       do { (flags) |= AuDiropq_##name; } while (0)
35780 +#define au_fclr_diropq(flags, name) \
35781 +       do { (flags) &= ~AuDiropq_##name; } while (0)
35782 +
35783 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35784 +                            unsigned int flags);
35785 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35786 +                         struct au_branch *br);
35787 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35788 +                           struct dentry *h_parent);
35789 +
35790 +/* real rmdir for the whiteout-ed dir */
35791 +struct au_whtmp_rmdir {
35792 +       struct inode *dir;
35793 +       struct au_branch *br;
35794 +       struct dentry *wh_dentry;
35795 +       struct au_nhash whlist;
35796 +};
35797 +
35798 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
35799 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
35800 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35801 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
35802 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35803 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
35804 +
35805 +/* ---------------------------------------------------------------------- */
35806 +
35807 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
35808 +                                             aufs_bindex_t bindex)
35809 +{
35810 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
35811 +}
35812 +
35813 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
35814 +{
35815 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
35816 +}
35817 +
35818 +#endif /* __KERNEL__ */
35819 +#endif /* __AUFS_WHOUT_H__ */
35820 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
35821 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
35822 +++ linux/fs/aufs/wkq.c 2020-08-03 09:12:33.764880472 +0200
35823 @@ -0,0 +1,372 @@
35824 +// SPDX-License-Identifier: GPL-2.0
35825 +/*
35826 + * Copyright (C) 2005-2020 Junjiro R. Okajima
35827 + *
35828 + * This program, aufs is free software; you can redistribute it and/or modify
35829 + * it under the terms of the GNU General Public License as published by
35830 + * the Free Software Foundation; either version 2 of the License, or
35831 + * (at your option) any later version.
35832 + *
35833 + * This program is distributed in the hope that it will be useful,
35834 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35835 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35836 + * GNU General Public License for more details.
35837 + *
35838 + * You should have received a copy of the GNU General Public License
35839 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35840 + */
35841 +
35842 +/*
35843 + * workqueue for asynchronous/super-io operations
35844 + * todo: try new credential scheme
35845 + */
35846 +
35847 +#include <linux/module.h>
35848 +#include "aufs.h"
35849 +
35850 +/* internal workqueue named AUFS_WKQ_NAME */
35851 +
35852 +static struct workqueue_struct *au_wkq;
35853 +
35854 +struct au_wkinfo {
35855 +       struct work_struct wk;
35856 +       struct kobject *kobj;
35857 +
35858 +       unsigned int flags; /* see wkq.h */
35859 +
35860 +       au_wkq_func_t func;
35861 +       void *args;
35862 +
35863 +#ifdef CONFIG_LOCKDEP
35864 +       int dont_check;
35865 +       struct held_lock **hlock;
35866 +#endif
35867 +
35868 +       struct completion *comp;
35869 +};
35870 +
35871 +/* ---------------------------------------------------------------------- */
35872 +/*
35873 + * Aufs passes some operations to the workqueue such as the internal copyup.
35874 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
35875 + * job run by workqueue depends upon the locks acquired in the other task.
35876 + * Delegating a small operation to the workqueue, aufs passes its lockdep
35877 + * information too. And the job in the workqueue restores the info in order to
35878 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
35879 + * correctly and expectedly.
35880 + */
35881 +
35882 +#ifndef CONFIG_LOCKDEP
35883 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
35884 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
35885 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
35886 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
35887 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
35888 +#else
35889 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
35890 +{
35891 +       wkinfo->hlock = NULL;
35892 +       wkinfo->dont_check = 0;
35893 +}
35894 +
35895 +/*
35896 + * 1: matched
35897 + * 0: unmatched
35898 + */
35899 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
35900 +{
35901 +       static DEFINE_SPINLOCK(spin);
35902 +       static struct {
35903 +               char *name;
35904 +               struct lock_class_key *key;
35905 +       } a[] = {
35906 +               { .name = "&sbinfo->si_rwsem" },
35907 +               { .name = "&finfo->fi_rwsem" },
35908 +               { .name = "&dinfo->di_rwsem" },
35909 +               { .name = "&iinfo->ii_rwsem" }
35910 +       };
35911 +       static int set;
35912 +       int i;
35913 +
35914 +       /* lockless read from 'set.' see below */
35915 +       if (set == ARRAY_SIZE(a)) {
35916 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35917 +                       if (a[i].key == key)
35918 +                               goto match;
35919 +               goto unmatch;
35920 +       }
35921 +
35922 +       spin_lock(&spin);
35923 +       if (set)
35924 +               for (i = 0; i < ARRAY_SIZE(a); i++)
35925 +                       if (a[i].key == key) {
35926 +                               spin_unlock(&spin);
35927 +                               goto match;
35928 +                       }
35929 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
35930 +               if (a[i].key) {
35931 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
35932 +                               spin_unlock(&spin);
35933 +                               goto match;
35934 +                       } else
35935 +                               continue;
35936 +               }
35937 +               if (strstr(a[i].name, name)) {
35938 +                       /*
35939 +                        * the order of these three lines is important for the
35940 +                        * lockless read above.
35941 +                        */
35942 +                       a[i].key = key;
35943 +                       spin_unlock(&spin);
35944 +                       set++;
35945 +                       /* AuDbg("%d, %s\n", set, name); */
35946 +                       goto match;
35947 +               }
35948 +       }
35949 +       spin_unlock(&spin);
35950 +       goto unmatch;
35951 +
35952 +match:
35953 +       return 1;
35954 +unmatch:
35955 +       return 0;
35956 +}
35957 +
35958 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
35959 +{
35960 +       int err, n;
35961 +       struct task_struct *curr;
35962 +       struct held_lock **hl, *held_locks, *p;
35963 +
35964 +       err = 0;
35965 +       curr = current;
35966 +       wkinfo->dont_check = lockdep_recursing(curr);
35967 +       if (wkinfo->dont_check)
35968 +               goto out;
35969 +       n = curr->lockdep_depth;
35970 +       if (!n)
35971 +               goto out;
35972 +
35973 +       err = -ENOMEM;
35974 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
35975 +       if (unlikely(!wkinfo->hlock))
35976 +               goto out;
35977 +
35978 +       err = 0;
35979 +#if 0 /* left for debugging */
35980 +       if (0 && au_debug_test())
35981 +               lockdep_print_held_locks(curr);
35982 +#endif
35983 +       held_locks = curr->held_locks;
35984 +       hl = wkinfo->hlock;
35985 +       while (n--) {
35986 +               p = held_locks++;
35987 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
35988 +                       *hl++ = p;
35989 +       }
35990 +       *hl = NULL;
35991 +
35992 +out:
35993 +       return err;
35994 +}
35995 +
35996 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
35997 +{
35998 +       au_kfree_try_rcu(wkinfo->hlock);
35999 +}
36000 +
36001 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36002 +{
36003 +       struct held_lock *p, **hl = wkinfo->hlock;
36004 +       int subclass;
36005 +
36006 +       if (wkinfo->dont_check)
36007 +               lockdep_off();
36008 +       if (!hl)
36009 +               return;
36010 +       while ((p = *hl++)) { /* assignment */
36011 +               subclass = lockdep_hlock_class(p)->subclass;
36012 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36013 +               if (p->read)
36014 +                       rwsem_acquire_read(p->instance, subclass, 0,
36015 +                                          /*p->acquire_ip*/_RET_IP_);
36016 +               else
36017 +                       rwsem_acquire(p->instance, subclass, 0,
36018 +                                     /*p->acquire_ip*/_RET_IP_);
36019 +       }
36020 +}
36021 +
36022 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36023 +{
36024 +       struct held_lock *p, **hl = wkinfo->hlock;
36025 +
36026 +       if (wkinfo->dont_check)
36027 +               lockdep_on();
36028 +       if (!hl)
36029 +               return;
36030 +       while ((p = *hl++)) /* assignment */
36031 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36032 +}
36033 +#endif
36034 +
36035 +static void wkq_func(struct work_struct *wk)
36036 +{
36037 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36038 +
36039 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36040 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36041 +
36042 +       au_wkq_lockdep_pre(wkinfo);
36043 +       wkinfo->func(wkinfo->args);
36044 +       au_wkq_lockdep_post(wkinfo);
36045 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36046 +               complete(wkinfo->comp);
36047 +       else {
36048 +               kobject_put(wkinfo->kobj);
36049 +               module_put(THIS_MODULE); /* todo: ?? */
36050 +               au_kfree_rcu(wkinfo);
36051 +       }
36052 +}
36053 +
36054 +/*
36055 + * Since struct completion is large, try allocating it dynamically.
36056 + */
36057 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36058 +
36059 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36060 +{
36061 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36062 +       if (*comp) {
36063 +               init_completion(*comp);
36064 +               wkinfo->comp = *comp;
36065 +               return 0;
36066 +       }
36067 +       return -ENOMEM;
36068 +}
36069 +
36070 +static void au_wkq_comp_free(struct completion *comp)
36071 +{
36072 +       au_kfree_rcu(comp);
36073 +}
36074 +
36075 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36076 +{
36077 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36078 +               if (au_wkq_test()) {
36079 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36080 +                               " due to a dead dir by UDBA,"
36081 +                               " or async xino write?\n");
36082 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36083 +               }
36084 +       } else
36085 +               au_dbg_verify_kthread();
36086 +
36087 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36088 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36089 +               queue_work(au_wkq, &wkinfo->wk);
36090 +       } else {
36091 +               INIT_WORK(&wkinfo->wk, wkq_func);
36092 +               schedule_work(&wkinfo->wk);
36093 +       }
36094 +}
36095 +
36096 +/*
36097 + * Be careful. It is easy to make deadlock happen.
36098 + * processA: lock, wkq and wait
36099 + * processB: wkq and wait, lock in wkq
36100 + * --> deadlock
36101 + */
36102 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36103 +{
36104 +       int err;
36105 +       AuWkqCompDeclare(comp);
36106 +       struct au_wkinfo wkinfo = {
36107 +               .flags  = flags,
36108 +               .func   = func,
36109 +               .args   = args
36110 +       };
36111 +
36112 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36113 +       if (unlikely(err))
36114 +               goto out;
36115 +       err = au_wkq_lockdep_alloc(&wkinfo);
36116 +       if (unlikely(err))
36117 +               goto out_comp;
36118 +       if (!err) {
36119 +               au_wkq_run(&wkinfo);
36120 +               /* no timeout, no interrupt */
36121 +               wait_for_completion(wkinfo.comp);
36122 +       }
36123 +       au_wkq_lockdep_free(&wkinfo);
36124 +
36125 +out_comp:
36126 +       au_wkq_comp_free(comp);
36127 +out:
36128 +       destroy_work_on_stack(&wkinfo.wk);
36129 +       return err;
36130 +}
36131 +
36132 +/*
36133 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36134 + * problem in a concurrent umounting.
36135 + */
36136 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36137 +                 unsigned int flags)
36138 +{
36139 +       int err;
36140 +       struct au_wkinfo *wkinfo;
36141 +
36142 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36143 +
36144 +       /*
36145 +        * wkq_func() must free this wkinfo.
36146 +        * it highly depends upon the implementation of workqueue.
36147 +        */
36148 +       err = 0;
36149 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36150 +       if (wkinfo) {
36151 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36152 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36153 +               wkinfo->func = func;
36154 +               wkinfo->args = args;
36155 +               wkinfo->comp = NULL;
36156 +               au_wkq_lockdep_init(wkinfo);
36157 +               kobject_get(wkinfo->kobj);
36158 +               __module_get(THIS_MODULE); /* todo: ?? */
36159 +
36160 +               au_wkq_run(wkinfo);
36161 +       } else {
36162 +               err = -ENOMEM;
36163 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36164 +       }
36165 +
36166 +       return err;
36167 +}
36168 +
36169 +/* ---------------------------------------------------------------------- */
36170 +
36171 +void au_nwt_init(struct au_nowait_tasks *nwt)
36172 +{
36173 +       atomic_set(&nwt->nw_len, 0);
36174 +       /* smp_mb(); */ /* atomic_set */
36175 +       init_waitqueue_head(&nwt->nw_wq);
36176 +}
36177 +
36178 +void au_wkq_fin(void)
36179 +{
36180 +       destroy_workqueue(au_wkq);
36181 +}
36182 +
36183 +int __init au_wkq_init(void)
36184 +{
36185 +       int err;
36186 +
36187 +       err = 0;
36188 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36189 +       if (IS_ERR(au_wkq))
36190 +               err = PTR_ERR(au_wkq);
36191 +       else if (!au_wkq)
36192 +               err = -ENOMEM;
36193 +
36194 +       return err;
36195 +}
36196 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36197 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36198 +++ linux/fs/aufs/wkq.h 2020-01-27 10:57:18.182205184 +0100
36199 @@ -0,0 +1,89 @@
36200 +/* SPDX-License-Identifier: GPL-2.0 */
36201 +/*
36202 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36203 + *
36204 + * This program, aufs is free software; you can redistribute it and/or modify
36205 + * it under the terms of the GNU General Public License as published by
36206 + * the Free Software Foundation; either version 2 of the License, or
36207 + * (at your option) any later version.
36208 + *
36209 + * This program is distributed in the hope that it will be useful,
36210 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36211 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36212 + * GNU General Public License for more details.
36213 + *
36214 + * You should have received a copy of the GNU General Public License
36215 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36216 + */
36217 +
36218 +/*
36219 + * workqueue for asynchronous/super-io operations
36220 + * todo: try new credentials management scheme
36221 + */
36222 +
36223 +#ifndef __AUFS_WKQ_H__
36224 +#define __AUFS_WKQ_H__
36225 +
36226 +#ifdef __KERNEL__
36227 +
36228 +#include <linux/wait.h>
36229 +
36230 +struct super_block;
36231 +
36232 +/* ---------------------------------------------------------------------- */
36233 +
36234 +/*
36235 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36236 + */
36237 +struct au_nowait_tasks {
36238 +       atomic_t                nw_len;
36239 +       wait_queue_head_t       nw_wq;
36240 +};
36241 +
36242 +/* ---------------------------------------------------------------------- */
36243 +
36244 +typedef void (*au_wkq_func_t)(void *args);
36245 +
36246 +/* wkq flags */
36247 +#define AuWkq_WAIT     1
36248 +#define AuWkq_NEST     (1 << 1)
36249 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36250 +#define au_fset_wkq(flags, name) \
36251 +       do { (flags) |= AuWkq_##name; } while (0)
36252 +#define au_fclr_wkq(flags, name) \
36253 +       do { (flags) &= ~AuWkq_##name; } while (0)
36254 +
36255 +/* wkq.c */
36256 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36257 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36258 +                 unsigned int flags);
36259 +void au_nwt_init(struct au_nowait_tasks *nwt);
36260 +int __init au_wkq_init(void);
36261 +void au_wkq_fin(void);
36262 +
36263 +/* ---------------------------------------------------------------------- */
36264 +
36265 +static inline int au_wkq_test(void)
36266 +{
36267 +       return current->flags & PF_WQ_WORKER;
36268 +}
36269 +
36270 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36271 +{
36272 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36273 +}
36274 +
36275 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36276 +{
36277 +       if (atomic_dec_and_test(&nwt->nw_len))
36278 +               wake_up_all(&nwt->nw_wq);
36279 +}
36280 +
36281 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36282 +{
36283 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36284 +       return 0;
36285 +}
36286 +
36287 +#endif /* __KERNEL__ */
36288 +#endif /* __AUFS_WKQ_H__ */
36289 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36290 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36291 +++ linux/fs/aufs/xattr.c       2020-08-03 09:14:46.095748745 +0200
36292 @@ -0,0 +1,356 @@
36293 +// SPDX-License-Identifier: GPL-2.0
36294 +/*
36295 + * Copyright (C) 2014-2020 Junjiro R. Okajima
36296 + *
36297 + * This program, aufs is free software; you can redistribute it and/or modify
36298 + * it under the terms of the GNU General Public License as published by
36299 + * the Free Software Foundation; either version 2 of the License, or
36300 + * (at your option) any later version.
36301 + *
36302 + * This program is distributed in the hope that it will be useful,
36303 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36304 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36305 + * GNU General Public License for more details.
36306 + *
36307 + * You should have received a copy of the GNU General Public License
36308 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36309 + */
36310 +
36311 +/*
36312 + * handling xattr functions
36313 + */
36314 +
36315 +#include <linux/fs.h>
36316 +#include <linux/posix_acl_xattr.h>
36317 +#include <linux/xattr.h>
36318 +#include "aufs.h"
36319 +
36320 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36321 +{
36322 +       if (!ignore_flags)
36323 +               goto out;
36324 +       switch (err) {
36325 +       case -ENOMEM:
36326 +       case -EDQUOT:
36327 +               goto out;
36328 +       }
36329 +
36330 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36331 +               err = 0;
36332 +               goto out;
36333 +       }
36334 +
36335 +#define cmp(brattr, prefix) do {                                       \
36336 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36337 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36338 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36339 +                               err = 0;                                \
36340 +                       goto out;                                       \
36341 +               }                                                       \
36342 +       } while (0)
36343 +
36344 +       cmp(SEC, SECURITY);
36345 +       cmp(SYS, SYSTEM);
36346 +       cmp(TR, TRUSTED);
36347 +       cmp(USR, USER);
36348 +#undef cmp
36349 +
36350 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36351 +               err = 0;
36352 +
36353 +out:
36354 +       return err;
36355 +}
36356 +
36357 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36358 +
36359 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
36360 +                           char *name, char **buf, unsigned int ignore_flags,
36361 +                           unsigned int verbose)
36362 +{
36363 +       int err;
36364 +       ssize_t ssz;
36365 +       struct inode *h_idst;
36366 +
36367 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
36368 +       err = ssz;
36369 +       if (unlikely(err <= 0)) {
36370 +               if (err == -ENODATA
36371 +                   || (err == -EOPNOTSUPP
36372 +                       && ((ignore_flags & au_xattr_out_of_list)
36373 +                           || (au_test_nfs_noacl(d_inode(h_src))
36374 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36375 +                                   || !strcmp(name,
36376 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36377 +                           ))
36378 +                       err = 0;
36379 +               if (err && (verbose || au_debug_test()))
36380 +                       pr_err("%s, err %d\n", name, err);
36381 +               goto out;
36382 +       }
36383 +
36384 +       /* unlock it temporary */
36385 +       h_idst = d_inode(h_dst);
36386 +       inode_unlock(h_idst);
36387 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
36388 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36389 +       if (unlikely(err)) {
36390 +               if (verbose || au_debug_test())
36391 +                       pr_err("%s, err %d\n", name, err);
36392 +               err = au_xattr_ignore(err, name, ignore_flags);
36393 +       }
36394 +
36395 +out:
36396 +       return err;
36397 +}
36398 +
36399 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
36400 +                 unsigned int verbose)
36401 +{
36402 +       int err, unlocked, acl_access, acl_default;
36403 +       ssize_t ssz;
36404 +       struct inode *h_isrc, *h_idst;
36405 +       char *value, *p, *o, *e;
36406 +
36407 +       /* try stopping to update the source inode while we are referencing */
36408 +       /* there should not be the parent-child relationship between them */
36409 +       h_isrc = d_inode(h_src);
36410 +       h_idst = d_inode(h_dst);
36411 +       inode_unlock(h_idst);
36412 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36413 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36414 +       unlocked = 0;
36415 +
36416 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36417 +       ssz = vfs_listxattr(h_src, NULL, 0);
36418 +       err = ssz;
36419 +       if (unlikely(err < 0)) {
36420 +               AuTraceErr(err);
36421 +               if (err == -ENODATA
36422 +                   || err == -EOPNOTSUPP)
36423 +                       err = 0;        /* ignore */
36424 +               goto out;
36425 +       }
36426 +
36427 +       err = 0;
36428 +       p = NULL;
36429 +       o = NULL;
36430 +       if (ssz) {
36431 +               err = -ENOMEM;
36432 +               p = kmalloc(ssz, GFP_NOFS);
36433 +               o = p;
36434 +               if (unlikely(!p))
36435 +                       goto out;
36436 +               err = vfs_listxattr(h_src, p, ssz);
36437 +       }
36438 +       inode_unlock_shared(h_isrc);
36439 +       unlocked = 1;
36440 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36441 +       if (unlikely(err < 0))
36442 +               goto out_free;
36443 +
36444 +       err = 0;
36445 +       e = p + ssz;
36446 +       value = NULL;
36447 +       acl_access = 0;
36448 +       acl_default = 0;
36449 +       while (!err && p < e) {
36450 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36451 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36452 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36453 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36454 +                                       - 1);
36455 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36456 +                                      verbose);
36457 +               p += strlen(p) + 1;
36458 +       }
36459 +       AuTraceErr(err);
36460 +       ignore_flags |= au_xattr_out_of_list;
36461 +       if (!err && !acl_access) {
36462 +               err = au_do_cpup_xattr(h_dst, h_src,
36463 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36464 +                                      ignore_flags, verbose);
36465 +               AuTraceErr(err);
36466 +       }
36467 +       if (!err && !acl_default) {
36468 +               err = au_do_cpup_xattr(h_dst, h_src,
36469 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36470 +                                      ignore_flags, verbose);
36471 +               AuTraceErr(err);
36472 +       }
36473 +
36474 +       au_kfree_try_rcu(value);
36475 +
36476 +out_free:
36477 +       au_kfree_try_rcu(o);
36478 +out:
36479 +       if (!unlocked)
36480 +               inode_unlock_shared(h_isrc);
36481 +       AuTraceErr(err);
36482 +       return err;
36483 +}
36484 +
36485 +/* ---------------------------------------------------------------------- */
36486 +
36487 +static int au_smack_reentering(struct super_block *sb)
36488 +{
36489 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36490 +       /*
36491 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36492 +        * i_op->getxattr(). ouch.
36493 +        */
36494 +       return si_pid_test(sb);
36495 +#else
36496 +       return 0;
36497 +#endif
36498 +}
36499 +
36500 +enum {
36501 +       AU_XATTR_LIST,
36502 +       AU_XATTR_GET
36503 +};
36504 +
36505 +struct au_lgxattr {
36506 +       int type;
36507 +       union {
36508 +               struct {
36509 +                       char    *list;
36510 +                       size_t  size;
36511 +               } list;
36512 +               struct {
36513 +                       const char      *name;
36514 +                       void            *value;
36515 +                       size_t          size;
36516 +               } get;
36517 +       } u;
36518 +};
36519 +
36520 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36521 +                         struct au_lgxattr *arg)
36522 +{
36523 +       ssize_t err;
36524 +       int reenter;
36525 +       struct path h_path;
36526 +       struct super_block *sb;
36527 +
36528 +       sb = dentry->d_sb;
36529 +       reenter = au_smack_reentering(sb);
36530 +       if (!reenter) {
36531 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36532 +               if (unlikely(err))
36533 +                       goto out;
36534 +       }
36535 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36536 +       if (unlikely(err))
36537 +               goto out_si;
36538 +       if (unlikely(!h_path.dentry))
36539 +               /* illegally overlapped or something */
36540 +               goto out_di; /* pretending success */
36541 +
36542 +       /* always topmost entry only */
36543 +       switch (arg->type) {
36544 +       case AU_XATTR_LIST:
36545 +               err = vfs_listxattr(h_path.dentry,
36546 +                                   arg->u.list.list, arg->u.list.size);
36547 +               break;
36548 +       case AU_XATTR_GET:
36549 +               AuDebugOn(d_is_negative(h_path.dentry));
36550 +               err = vfs_getxattr(h_path.dentry,
36551 +                                  arg->u.get.name, arg->u.get.value,
36552 +                                  arg->u.get.size);
36553 +               break;
36554 +       }
36555 +
36556 +out_di:
36557 +       if (!reenter)
36558 +               di_read_unlock(dentry, AuLock_IR);
36559 +out_si:
36560 +       if (!reenter)
36561 +               si_read_unlock(sb);
36562 +out:
36563 +       AuTraceErr(err);
36564 +       return err;
36565 +}
36566 +
36567 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36568 +{
36569 +       struct au_lgxattr arg = {
36570 +               .type = AU_XATTR_LIST,
36571 +               .u.list = {
36572 +                       .list   = list,
36573 +                       .size   = size
36574 +               },
36575 +       };
36576 +
36577 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36578 +}
36579 +
36580 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36581 +                          const char *name, void *value, size_t size)
36582 +{
36583 +       struct au_lgxattr arg = {
36584 +               .type = AU_XATTR_GET,
36585 +               .u.get = {
36586 +                       .name   = name,
36587 +                       .value  = value,
36588 +                       .size   = size
36589 +               },
36590 +       };
36591 +
36592 +       return au_lgxattr(dentry, inode, &arg);
36593 +}
36594 +
36595 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36596 +                      const char *name, const void *value, size_t size,
36597 +                      int flags)
36598 +{
36599 +       struct au_sxattr arg = {
36600 +               .type = AU_XATTR_SET,
36601 +               .u.set = {
36602 +                       .name   = name,
36603 +                       .value  = value,
36604 +                       .size   = size,
36605 +                       .flags  = flags
36606 +               },
36607 +       };
36608 +
36609 +       return au_sxattr(dentry, inode, &arg);
36610 +}
36611 +
36612 +/* ---------------------------------------------------------------------- */
36613 +
36614 +static int au_xattr_get(const struct xattr_handler *handler,
36615 +                       struct dentry *dentry, struct inode *inode,
36616 +                       const char *name, void *buffer, size_t size)
36617 +{
36618 +       return au_getxattr(dentry, inode, name, buffer, size);
36619 +}
36620 +
36621 +static int au_xattr_set(const struct xattr_handler *handler,
36622 +                       struct dentry *dentry, struct inode *inode,
36623 +                       const char *name, const void *value, size_t size,
36624 +                       int flags)
36625 +{
36626 +       return au_setxattr(dentry, inode, name, value, size, flags);
36627 +}
36628 +
36629 +static const struct xattr_handler au_xattr_handler = {
36630 +       .name   = "",
36631 +       .prefix = "",
36632 +       .get    = au_xattr_get,
36633 +       .set    = au_xattr_set
36634 +};
36635 +
36636 +static const struct xattr_handler *au_xattr_handlers[] = {
36637 +#ifdef CONFIG_FS_POSIX_ACL
36638 +       &posix_acl_access_xattr_handler,
36639 +       &posix_acl_default_xattr_handler,
36640 +#endif
36641 +       &au_xattr_handler, /* must be last */
36642 +       NULL
36643 +};
36644 +
36645 +void au_xattr_init(struct super_block *sb)
36646 +{
36647 +       sb->s_xattr = au_xattr_handlers;
36648 +}
36649 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36650 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36651 +++ linux/fs/aufs/xino.c        2020-12-15 14:10:58.924690030 +0100
36652 @@ -0,0 +1,1925 @@
36653 +// SPDX-License-Identifier: GPL-2.0
36654 +/*
36655 + * Copyright (C) 2005-2020 Junjiro R. Okajima
36656 + *
36657 + * This program, aufs is free software; you can redistribute it and/or modify
36658 + * it under the terms of the GNU General Public License as published by
36659 + * the Free Software Foundation; either version 2 of the License, or
36660 + * (at your option) any later version.
36661 + *
36662 + * This program is distributed in the hope that it will be useful,
36663 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36664 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36665 + * GNU General Public License for more details.
36666 + *
36667 + * You should have received a copy of the GNU General Public License
36668 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36669 + */
36670 +
36671 +/*
36672 + * external inode number translation table and bitmap
36673 + *
36674 + * things to consider
36675 + * - the lifetime
36676 + *   + au_xino object
36677 + *   + XINO files (xino, xib, xigen)
36678 + *   + dynamic debugfs entries (xiN)
36679 + *   + static debugfs entries (xib, xigen)
36680 + *   + static sysfs entry (xi_path)
36681 + * - several entry points to handle them.
36682 + *   + mount(2) without xino option (default)
36683 + *   + mount(2) with xino option
36684 + *   + mount(2) with noxino option
36685 + *   + umount(2)
36686 + *   + remount with add/del branches
36687 + *   + remount with xino/noxino options
36688 + */
36689 +
36690 +#include <linux/seq_file.h>
36691 +#include <linux/statfs.h>
36692 +#include "aufs.h"
36693 +
36694 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36695 +                                    aufs_bindex_t bbot,
36696 +                                    struct super_block *h_sb)
36697 +{
36698 +       /* todo: try binary-search if the branches are many */
36699 +       for (; btop <= bbot; btop++)
36700 +               if (h_sb == au_sbr_sb(sb, btop))
36701 +                       return btop;
36702 +       return -1;
36703 +}
36704 +
36705 +/*
36706 + * find another branch who is on the same filesystem of the specified
36707 + * branch{@btgt}. search until @bbot.
36708 + */
36709 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36710 +                                 aufs_bindex_t bbot)
36711 +{
36712 +       aufs_bindex_t bindex;
36713 +       struct super_block *tgt_sb;
36714 +
36715 +       tgt_sb = au_sbr_sb(sb, btgt);
36716 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36717 +       if (bindex < 0)
36718 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36719 +
36720 +       return bindex;
36721 +}
36722 +
36723 +/* ---------------------------------------------------------------------- */
36724 +
36725 +/*
36726 + * stop unnecessary notify events at creating xino files
36727 + */
36728 +
36729 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36730 +{
36731 +       aufs_bindex_t bfound, bindex, bbot;
36732 +       struct dentry *parent;
36733 +       struct au_branch *br;
36734 +
36735 +       bfound = -1;
36736 +       parent = dentry->d_parent; /* safe d_parent access */
36737 +       bbot = au_sbbot(sb);
36738 +       for (bindex = 0; bindex <= bbot; bindex++) {
36739 +               br = au_sbr(sb, bindex);
36740 +               if (au_br_dentry(br) == parent) {
36741 +                       bfound = bindex;
36742 +                       break;
36743 +               }
36744 +       }
36745 +
36746 +       AuDbg("bfound b%d\n", bfound);
36747 +       return bfound;
36748 +}
36749 +
36750 +struct au_xino_lock_dir {
36751 +       struct au_hinode *hdir;
36752 +       struct dentry *parent;
36753 +       struct inode *dir;
36754 +};
36755 +
36756 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36757 +                                         unsigned int lsc)
36758 +{
36759 +       struct dentry *parent;
36760 +       struct inode *dir;
36761 +
36762 +       parent = dget_parent(dentry);
36763 +       dir = d_inode(parent);
36764 +       inode_lock_nested(dir, lsc);
36765 +#if 0 /* it should not happen */
36766 +       spin_lock(&dentry->d_lock);
36767 +       if (unlikely(dentry->d_parent != parent)) {
36768 +               spin_unlock(&dentry->d_lock);
36769 +               inode_unlock(dir);
36770 +               dput(parent);
36771 +               parent = NULL;
36772 +               goto out;
36773 +       }
36774 +       spin_unlock(&dentry->d_lock);
36775 +
36776 +out:
36777 +#endif
36778 +       return parent;
36779 +}
36780 +
36781 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36782 +                            struct au_xino_lock_dir *ldir)
36783 +{
36784 +       aufs_bindex_t bindex;
36785 +
36786 +       ldir->hdir = NULL;
36787 +       bindex = au_xi_root(sb, xipath->dentry);
36788 +       if (bindex >= 0) {
36789 +               /* rw branch root */
36790 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36791 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36792 +       } else {
36793 +               /* other */
36794 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36795 +                                                  AuLsc_I_PARENT);
36796 +               ldir->dir = d_inode(ldir->parent);
36797 +       }
36798 +}
36799 +
36800 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36801 +{
36802 +       if (ldir->hdir)
36803 +               au_hn_inode_unlock(ldir->hdir);
36804 +       else {
36805 +               inode_unlock(ldir->dir);
36806 +               dput(ldir->parent);
36807 +       }
36808 +}
36809 +
36810 +/* ---------------------------------------------------------------------- */
36811 +
36812 +/*
36813 + * create and set a new xino file
36814 + */
36815 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36816 +                           int wbrtop)
36817 +{
36818 +       struct file *file;
36819 +       struct dentry *h_parent, *d;
36820 +       struct inode *h_dir, *inode;
36821 +       int err;
36822 +       static DEFINE_MUTEX(mtx);
36823 +
36824 +       /*
36825 +        * at mount-time, and the xino file is the default path,
36826 +        * hnotify is disabled so we have no notify events to ignore.
36827 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36828 +        */
36829 +       if (!wbrtop)
36830 +               mutex_lock(&mtx);
36831 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36832 +                              /* | __FMODE_NONOTIFY */,
36833 +                              0666);
36834 +       if (IS_ERR(file)) {
36835 +               if (!wbrtop)
36836 +                       mutex_unlock(&mtx);
36837 +               if (!silent)
36838 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36839 +               return file;
36840 +       }
36841 +
36842 +       /* keep file count */
36843 +       err = 0;
36844 +       d = file->f_path.dentry;
36845 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36846 +       if (!wbrtop)
36847 +               mutex_unlock(&mtx);
36848 +       /* mnt_want_write() is unnecessary here */
36849 +       h_dir = d_inode(h_parent);
36850 +       inode = file_inode(file);
36851 +       /* no delegation since it is just created */
36852 +       if (inode->i_nlink)
36853 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
36854 +                                  /*force*/0);
36855 +       inode_unlock(h_dir);
36856 +       dput(h_parent);
36857 +       if (unlikely(err)) {
36858 +               if (!silent)
36859 +                       pr_err("unlink %s(%d)\n", fpath, err);
36860 +               goto out;
36861 +       }
36862 +
36863 +       err = -EINVAL;
36864 +       if (unlikely(sb == d->d_sb)) {
36865 +               if (!silent)
36866 +                       pr_err("%s must be outside\n", fpath);
36867 +               goto out;
36868 +       }
36869 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
36870 +               if (!silent)
36871 +                       pr_err("xino doesn't support %s(%s)\n",
36872 +                              fpath, au_sbtype(d->d_sb));
36873 +               goto out;
36874 +       }
36875 +       return file; /* success */
36876 +
36877 +out:
36878 +       fput(file);
36879 +       file = ERR_PTR(err);
36880 +       return file;
36881 +}
36882 +
36883 +/*
36884 + * create a new xinofile at the same place/path as @base.
36885 + */
36886 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
36887 +                            struct file *copy_src)
36888 +{
36889 +       struct file *file;
36890 +       struct dentry *dentry, *parent;
36891 +       struct inode *dir, *delegated;
36892 +       struct qstr *name;
36893 +       struct path path;
36894 +       int err, do_unlock;
36895 +       struct au_xino_lock_dir ldir;
36896 +
36897 +       do_unlock = 1;
36898 +       au_xino_lock_dir(sb, base, &ldir);
36899 +       dentry = base->dentry;
36900 +       parent = dentry->d_parent; /* dir inode is locked */
36901 +       dir = d_inode(parent);
36902 +       IMustLock(dir);
36903 +
36904 +       name = &dentry->d_name;
36905 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
36906 +       if (IS_ERR(path.dentry)) {
36907 +               file = (void *)path.dentry;
36908 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
36909 +               goto out;
36910 +       }
36911 +
36912 +       /* no need to mnt_want_write() since we call dentry_open() later */
36913 +       err = vfs_create(dir, path.dentry, 0666, NULL);
36914 +       if (unlikely(err)) {
36915 +               file = ERR_PTR(err);
36916 +               pr_err("%pd create err %d\n", dentry, err);
36917 +               goto out_dput;
36918 +       }
36919 +
36920 +       path.mnt = base->mnt;
36921 +       file = vfsub_dentry_open(&path,
36922 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36923 +                                /* | __FMODE_NONOTIFY */);
36924 +       if (IS_ERR(file)) {
36925 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
36926 +               goto out_dput;
36927 +       }
36928 +
36929 +       delegated = NULL;
36930 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
36931 +       au_xino_unlock_dir(&ldir);
36932 +       do_unlock = 0;
36933 +       if (unlikely(err == -EWOULDBLOCK)) {
36934 +               pr_warn("cannot retry for NFSv4 delegation"
36935 +                       " for an internal unlink\n");
36936 +               iput(delegated);
36937 +       }
36938 +       if (unlikely(err)) {
36939 +               pr_err("%pd unlink err %d\n", dentry, err);
36940 +               goto out_fput;
36941 +       }
36942 +
36943 +       if (copy_src) {
36944 +               /* no one can touch copy_src xino */
36945 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
36946 +               if (unlikely(err)) {
36947 +                       pr_err("%pd copy err %d\n", dentry, err);
36948 +                       goto out_fput;
36949 +               }
36950 +       }
36951 +       goto out_dput; /* success */
36952 +
36953 +out_fput:
36954 +       fput(file);
36955 +       file = ERR_PTR(err);
36956 +out_dput:
36957 +       dput(path.dentry);
36958 +out:
36959 +       if (do_unlock)
36960 +               au_xino_unlock_dir(&ldir);
36961 +       return file;
36962 +}
36963 +
36964 +struct file *au_xino_file1(struct au_xino *xi)
36965 +{
36966 +       struct file *file;
36967 +       unsigned int u, nfile;
36968 +
36969 +       file = NULL;
36970 +       nfile = xi->xi_nfile;
36971 +       for (u = 0; u < nfile; u++) {
36972 +               file = xi->xi_file[u];
36973 +               if (file)
36974 +                       break;
36975 +       }
36976 +
36977 +       return file;
36978 +}
36979 +
36980 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
36981 +{
36982 +       int err;
36983 +       struct file *f;
36984 +       void *p;
36985 +
36986 +       if (file)
36987 +               get_file(file);
36988 +
36989 +       err = 0;
36990 +       f = NULL;
36991 +       if (idx < xi->xi_nfile) {
36992 +               f = xi->xi_file[idx];
36993 +               if (f)
36994 +                       fput(f);
36995 +       } else {
36996 +               p = au_kzrealloc(xi->xi_file,
36997 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
36998 +                                sizeof(*xi->xi_file) * (idx + 1),
36999 +                                GFP_NOFS, /*may_shrink*/0);
37000 +               if (p) {
37001 +                       MtxMustLock(&xi->xi_mtx);
37002 +                       xi->xi_file = p;
37003 +                       xi->xi_nfile = idx + 1;
37004 +               } else {
37005 +                       err = -ENOMEM;
37006 +                       if (file)
37007 +                               fput(file);
37008 +                       goto out;
37009 +               }
37010 +       }
37011 +       xi->xi_file[idx] = file;
37012 +
37013 +out:
37014 +       return err;
37015 +}
37016 +
37017 +/*
37018 + * if @xinew->xi is not set, then create new xigen file.
37019 + */
37020 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37021 +{
37022 +       struct file *file;
37023 +       int err;
37024 +
37025 +       SiMustAnyLock(sb);
37026 +
37027 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37028 +       if (IS_ERR(file)) {
37029 +               err = PTR_ERR(file);
37030 +               pr_err("%s[%d], err %d\n",
37031 +                      xinew->xi ? "xino" : "xigen",
37032 +                      xinew->idx, err);
37033 +               goto out;
37034 +       }
37035 +
37036 +       if (xinew->xi)
37037 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37038 +       else {
37039 +               BUG();
37040 +               /* todo: make xigen file an array */
37041 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37042 +       }
37043 +       fput(file);
37044 +       if (unlikely(err))
37045 +               file = ERR_PTR(err);
37046 +
37047 +out:
37048 +       return file;
37049 +}
37050 +
37051 +/* ---------------------------------------------------------------------- */
37052 +
37053 +/*
37054 + * truncate xino files
37055 + */
37056 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37057 +                           int idx, struct kstatfs *st)
37058 +{
37059 +       int err;
37060 +       blkcnt_t blocks;
37061 +       struct file *file, *new_xino;
37062 +       struct au_xi_new xinew = {
37063 +               .idx = idx
37064 +       };
37065 +
37066 +       err = 0;
37067 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37068 +       file = au_xino_file(xinew.xi, idx);
37069 +       if (!file)
37070 +               goto out;
37071 +
37072 +       xinew.base = &file->f_path;
37073 +       err = vfs_statfs(xinew.base, st);
37074 +       if (unlikely(err)) {
37075 +               AuErr1("statfs err %d, ignored\n", err);
37076 +               err = 0;
37077 +               goto out;
37078 +       }
37079 +
37080 +       blocks = file_inode(file)->i_blocks;
37081 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37082 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37083 +
37084 +       xinew.copy_src = file;
37085 +       new_xino = au_xi_new(sb, &xinew);
37086 +       if (IS_ERR(new_xino)) {
37087 +               err = PTR_ERR(new_xino);
37088 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37089 +               goto out;
37090 +       }
37091 +
37092 +       err = vfs_statfs(&new_xino->f_path, st);
37093 +       if (!err)
37094 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37095 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37096 +                       st->f_bfree, st->f_blocks);
37097 +       else {
37098 +               AuErr1("statfs err %d, ignored\n", err);
37099 +               err = 0;
37100 +       }
37101 +
37102 +out:
37103 +       return err;
37104 +}
37105 +
37106 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37107 +{
37108 +       int err, i;
37109 +       unsigned long jiffy;
37110 +       aufs_bindex_t bbot;
37111 +       struct kstatfs *st;
37112 +       struct au_branch *br;
37113 +       struct au_xino *xi;
37114 +
37115 +       err = -ENOMEM;
37116 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37117 +       if (unlikely(!st))
37118 +               goto out;
37119 +
37120 +       err = -EINVAL;
37121 +       bbot = au_sbbot(sb);
37122 +       if (unlikely(bindex < 0 || bbot < bindex))
37123 +               goto out_st;
37124 +
37125 +       err = 0;
37126 +       jiffy = jiffies;
37127 +       br = au_sbr(sb, bindex);
37128 +       xi = br->br_xino;
37129 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37130 +               err = au_xino_do_trunc(sb, bindex, i, st);
37131 +       if (!err)
37132 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37133 +
37134 +out_st:
37135 +       au_kfree_rcu(st);
37136 +out:
37137 +       return err;
37138 +}
37139 +
37140 +struct xino_do_trunc_args {
37141 +       struct super_block *sb;
37142 +       struct au_branch *br;
37143 +       int idx;
37144 +};
37145 +
37146 +static void xino_do_trunc(void *_args)
37147 +{
37148 +       struct xino_do_trunc_args *args = _args;
37149 +       struct super_block *sb;
37150 +       struct au_branch *br;
37151 +       struct inode *dir;
37152 +       int err, idx;
37153 +       aufs_bindex_t bindex;
37154 +
37155 +       err = 0;
37156 +       sb = args->sb;
37157 +       dir = d_inode(sb->s_root);
37158 +       br = args->br;
37159 +       idx = args->idx;
37160 +
37161 +       si_noflush_write_lock(sb);
37162 +       ii_read_lock_parent(dir);
37163 +       bindex = au_br_index(sb, br->br_id);
37164 +       err = au_xino_trunc(sb, bindex, idx);
37165 +       ii_read_unlock(dir);
37166 +       if (unlikely(err))
37167 +               pr_warn("err b%d, (%d)\n", bindex, err);
37168 +       atomic_dec(&br->br_xino->xi_truncating);
37169 +       au_lcnt_dec(&br->br_count);
37170 +       si_write_unlock(sb);
37171 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37172 +       au_kfree_rcu(args);
37173 +}
37174 +
37175 +/*
37176 + * returns the index in the xi_file array whose corresponding file is necessary
37177 + * to truncate, or -1 which means no need to truncate.
37178 + */
37179 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37180 +{
37181 +       int err;
37182 +       unsigned int u;
37183 +       struct kstatfs st;
37184 +       struct au_sbinfo *sbinfo;
37185 +       struct au_xino *xi;
37186 +       struct file *file;
37187 +
37188 +       /* todo: si_xino_expire and the ratio should be customizable */
37189 +       sbinfo = au_sbi(sb);
37190 +       if (time_before(jiffies,
37191 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37192 +               return -1;
37193 +
37194 +       /* truncation border */
37195 +       xi = br->br_xino;
37196 +       for (u = 0; u < xi->xi_nfile; u++) {
37197 +               file = au_xino_file(xi, u);
37198 +               if (!file)
37199 +                       continue;
37200 +
37201 +               err = vfs_statfs(&file->f_path, &st);
37202 +               if (unlikely(err)) {
37203 +                       AuErr1("statfs err %d, ignored\n", err);
37204 +                       return -1;
37205 +               }
37206 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37207 +                   >= AUFS_XINO_DEF_TRUNC)
37208 +                       return u;
37209 +       }
37210 +
37211 +       return -1;
37212 +}
37213 +
37214 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37215 +{
37216 +       int idx;
37217 +       struct xino_do_trunc_args *args;
37218 +       int wkq_err;
37219 +
37220 +       idx = xino_trunc_test(sb, br);
37221 +       if (idx < 0)
37222 +               return;
37223 +
37224 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37225 +               goto out;
37226 +
37227 +       /* lock and kfree() will be called in trunc_xino() */
37228 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37229 +       if (unlikely(!args)) {
37230 +               AuErr1("no memory\n");
37231 +               goto out;
37232 +       }
37233 +
37234 +       au_lcnt_inc(&br->br_count);
37235 +       args->sb = sb;
37236 +       args->br = br;
37237 +       args->idx = idx;
37238 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37239 +       if (!wkq_err)
37240 +               return; /* success */
37241 +
37242 +       pr_err("wkq %d\n", wkq_err);
37243 +       au_lcnt_dec(&br->br_count);
37244 +       au_kfree_rcu(args);
37245 +
37246 +out:
37247 +       atomic_dec(&br->br_xino->xi_truncating);
37248 +}
37249 +
37250 +/* ---------------------------------------------------------------------- */
37251 +
37252 +struct au_xi_calc {
37253 +       int idx;
37254 +       loff_t pos;
37255 +};
37256 +
37257 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37258 +                      struct au_xi_calc *calc)
37259 +{
37260 +       loff_t maxent;
37261 +
37262 +       maxent = au_xi_maxent(sb);
37263 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37264 +       calc->pos *= sizeof(ino_t);
37265 +}
37266 +
37267 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37268 +                               struct au_xi_calc *calc)
37269 +{
37270 +       int err;
37271 +       struct file *file;
37272 +       struct au_xino *xi = br->br_xino;
37273 +       struct au_xi_new xinew = {
37274 +               .xi = xi
37275 +       };
37276 +
37277 +       SiMustAnyLock(sb);
37278 +
37279 +       err = 0;
37280 +       if (!xi)
37281 +               goto out;
37282 +
37283 +       mutex_lock(&xi->xi_mtx);
37284 +       file = au_xino_file(xi, calc->idx);
37285 +       if (file)
37286 +               goto out_mtx;
37287 +
37288 +       file = au_xino_file(xi, /*idx*/-1);
37289 +       AuDebugOn(!file);
37290 +       xinew.idx = calc->idx;
37291 +       xinew.base = &file->f_path;
37292 +       /* xinew.copy_src = NULL; */
37293 +       file = au_xi_new(sb, &xinew);
37294 +       if (IS_ERR(file))
37295 +               err = PTR_ERR(file);
37296 +
37297 +out_mtx:
37298 +       mutex_unlock(&xi->xi_mtx);
37299 +out:
37300 +       return err;
37301 +}
37302 +
37303 +struct au_xino_do_new_async_args {
37304 +       struct super_block *sb;
37305 +       struct au_branch *br;
37306 +       struct au_xi_calc calc;
37307 +       ino_t ino;
37308 +};
37309 +
37310 +struct au_xi_writing {
37311 +       struct hlist_bl_node node;
37312 +       ino_t h_ino, ino;
37313 +};
37314 +
37315 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37316 +                           ino_t ino);
37317 +
37318 +static void au_xino_call_do_new_async(void *args)
37319 +{
37320 +       struct au_xino_do_new_async_args *a = args;
37321 +       struct au_branch *br;
37322 +       struct super_block *sb;
37323 +       struct au_sbinfo *sbi;
37324 +       struct inode *root;
37325 +       struct file *file;
37326 +       struct au_xi_writing *del, *p;
37327 +       struct hlist_bl_head *hbl;
37328 +       struct hlist_bl_node *pos;
37329 +       int err;
37330 +
37331 +       br = a->br;
37332 +       sb = a->sb;
37333 +       sbi = au_sbi(sb);
37334 +       si_noflush_read_lock(sb);
37335 +       root = d_inode(sb->s_root);
37336 +       ii_read_lock_child(root);
37337 +       err = au_xino_do_new_async(sb, br, &a->calc);
37338 +       if (unlikely(err)) {
37339 +               AuIOErr("err %d\n", err);
37340 +               goto out;
37341 +       }
37342 +
37343 +       file = au_xino_file(br->br_xino, a->calc.idx);
37344 +       AuDebugOn(!file);
37345 +       err = au_xino_do_write(file, &a->calc, a->ino);
37346 +       if (unlikely(err)) {
37347 +               AuIOErr("err %d\n", err);
37348 +               goto out;
37349 +       }
37350 +
37351 +       del = NULL;
37352 +       hbl = &br->br_xino->xi_writing;
37353 +       hlist_bl_lock(hbl);
37354 +       au_hbl_for_each(pos, hbl) {
37355 +               p = container_of(pos, struct au_xi_writing, node);
37356 +               if (p->ino == a->ino) {
37357 +                       del = p;
37358 +                       hlist_bl_del(&p->node);
37359 +                       break;
37360 +               }
37361 +       }
37362 +       hlist_bl_unlock(hbl);
37363 +       au_kfree_rcu(del);
37364 +
37365 +out:
37366 +       au_lcnt_dec(&br->br_count);
37367 +       ii_read_unlock(root);
37368 +       si_read_unlock(sb);
37369 +       au_nwt_done(&sbi->si_nowait);
37370 +       au_kfree_rcu(a);
37371 +}
37372 +
37373 +/*
37374 + * create a new xino file asynchronously
37375 + */
37376 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37377 +                            struct au_xi_calc *calc, ino_t ino)
37378 +{
37379 +       int err;
37380 +       struct au_xino_do_new_async_args *arg;
37381 +
37382 +       err = -ENOMEM;
37383 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37384 +       if (unlikely(!arg))
37385 +               goto out;
37386 +
37387 +       arg->sb = sb;
37388 +       arg->br = br;
37389 +       arg->calc = *calc;
37390 +       arg->ino = ino;
37391 +       au_lcnt_inc(&br->br_count);
37392 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37393 +       if (unlikely(err)) {
37394 +               pr_err("wkq %d\n", err);
37395 +               au_lcnt_dec(&br->br_count);
37396 +               au_kfree_rcu(arg);
37397 +       }
37398 +
37399 +out:
37400 +       return err;
37401 +}
37402 +
37403 +/*
37404 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37405 + * at the position of @h_ino.
37406 + */
37407 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37408 +                ino_t *ino)
37409 +{
37410 +       int err;
37411 +       ssize_t sz;
37412 +       struct au_xi_calc calc;
37413 +       struct au_sbinfo *sbinfo;
37414 +       struct file *file;
37415 +       struct au_xino *xi;
37416 +       struct hlist_bl_head *hbl;
37417 +       struct hlist_bl_node *pos;
37418 +       struct au_xi_writing *p;
37419 +
37420 +       *ino = 0;
37421 +       if (!au_opt_test(au_mntflags(sb), XINO))
37422 +               return 0; /* no xino */
37423 +
37424 +       err = 0;
37425 +       au_xi_calc(sb, h_ino, &calc);
37426 +       xi = au_sbr(sb, bindex)->br_xino;
37427 +       file = au_xino_file(xi, calc.idx);
37428 +       if (!file) {
37429 +               hbl = &xi->xi_writing;
37430 +               hlist_bl_lock(hbl);
37431 +               au_hbl_for_each(pos, hbl) {
37432 +                       p = container_of(pos, struct au_xi_writing, node);
37433 +                       if (p->h_ino == h_ino) {
37434 +                               AuDbg("hi%llu, i%llu, found\n",
37435 +                                     (u64)p->h_ino, (u64)p->ino);
37436 +                               *ino = p->ino;
37437 +                               break;
37438 +                       }
37439 +               }
37440 +               hlist_bl_unlock(hbl);
37441 +               return 0;
37442 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37443 +               return 0; /* no xino */
37444 +
37445 +       sbinfo = au_sbi(sb);
37446 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37447 +       if (sz == sizeof(*ino))
37448 +               return 0; /* success */
37449 +
37450 +       err = sz;
37451 +       if (unlikely(sz >= 0)) {
37452 +               err = -EIO;
37453 +               AuIOErr("xino read error (%zd)\n", sz);
37454 +       }
37455 +       return err;
37456 +}
37457 +
37458 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37459 +                           ino_t ino)
37460 +{
37461 +       ssize_t sz;
37462 +
37463 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37464 +       if (sz == sizeof(ino))
37465 +               return 0; /* success */
37466 +
37467 +       AuIOErr("write failed (%zd)\n", sz);
37468 +       return -EIO;
37469 +}
37470 +
37471 +/*
37472 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37473 + * at the position of @h_ino.
37474 + * even if @ino is zero, it is written to the xinofile and means no entry.
37475 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37476 + * try truncating it.
37477 + */
37478 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37479 +                 ino_t ino)
37480 +{
37481 +       int err;
37482 +       unsigned int mnt_flags;
37483 +       struct au_xi_calc calc;
37484 +       struct file *file;
37485 +       struct au_branch *br;
37486 +       struct au_xino *xi;
37487 +       struct au_xi_writing *p;
37488 +
37489 +       SiMustAnyLock(sb);
37490 +
37491 +       mnt_flags = au_mntflags(sb);
37492 +       if (!au_opt_test(mnt_flags, XINO))
37493 +               return 0;
37494 +
37495 +       au_xi_calc(sb, h_ino, &calc);
37496 +       br = au_sbr(sb, bindex);
37497 +       xi = br->br_xino;
37498 +       file = au_xino_file(xi, calc.idx);
37499 +       if (!file) {
37500 +               /* store the inum pair into the list */
37501 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37502 +               p->h_ino = h_ino;
37503 +               p->ino = ino;
37504 +               au_hbl_add(&p->node, &xi->xi_writing);
37505 +
37506 +               /* create and write a new xino file asynchronously */
37507 +               err = au_xino_new_async(sb, br, &calc, ino);
37508 +               if (!err)
37509 +                       return 0; /* success */
37510 +               goto out;
37511 +       }
37512 +
37513 +       err = au_xino_do_write(file, &calc, ino);
37514 +       if (!err) {
37515 +               br = au_sbr(sb, bindex);
37516 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37517 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37518 +                       xino_try_trunc(sb, br);
37519 +               return 0; /* success */
37520 +       }
37521 +
37522 +out:
37523 +       AuIOErr("write failed (%d)\n", err);
37524 +       return -EIO;
37525 +}
37526 +
37527 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37528 +                             loff_t *pos);
37529 +
37530 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37531 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37532 +{
37533 +       ssize_t err;
37534 +       int i;
37535 +       const int prevent_endless = 10;
37536 +
37537 +       i = 0;
37538 +       do {
37539 +               err = vfsub_read_k(file, kbuf, size, pos);
37540 +               if (err == -EINTR
37541 +                   && !au_wkq_test()
37542 +                   && fatal_signal_pending(current)) {
37543 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37544 +                       BUG_ON(err == -EINTR);
37545 +               }
37546 +       } while (i++ < prevent_endless
37547 +                && (err == -EAGAIN || err == -EINTR));
37548 +
37549 +#if 0 /* reserved for future use */
37550 +       if (err > 0)
37551 +               fsnotify_access(file->f_path.dentry);
37552 +#endif
37553 +
37554 +       return err;
37555 +}
37556 +
37557 +struct xino_fread_args {
37558 +       ssize_t *errp;
37559 +       struct file *file;
37560 +       void *buf;
37561 +       size_t size;
37562 +       loff_t *pos;
37563 +};
37564 +
37565 +static void call_xino_fread(void *args)
37566 +{
37567 +       struct xino_fread_args *a = args;
37568 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37569 +}
37570 +
37571 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37572 +                             loff_t *pos)
37573 +{
37574 +       ssize_t err;
37575 +       int wkq_err;
37576 +       struct xino_fread_args args = {
37577 +               .errp   = &err,
37578 +               .file   = file,
37579 +               .buf    = buf,
37580 +               .size   = size,
37581 +               .pos    = pos
37582 +       };
37583 +
37584 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37585 +       if (unlikely(wkq_err))
37586 +               err = wkq_err;
37587 +
37588 +       return err;
37589 +}
37590 +
37591 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37592 +                              loff_t *pos);
37593 +
37594 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37595 +                             loff_t *pos)
37596 +{
37597 +       ssize_t err;
37598 +       int i;
37599 +       const int prevent_endless = 10;
37600 +
37601 +       i = 0;
37602 +       do {
37603 +               err = vfsub_write_k(file, kbuf, size, pos);
37604 +               if (err == -EINTR
37605 +                   && !au_wkq_test()
37606 +                   && fatal_signal_pending(current)) {
37607 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37608 +                       BUG_ON(err == -EINTR);
37609 +               }
37610 +       } while (i++ < prevent_endless
37611 +                && (err == -EAGAIN || err == -EINTR));
37612 +
37613 +#if 0 /* reserved for future use */
37614 +       if (err > 0)
37615 +               fsnotify_modify(file->f_path.dentry);
37616 +#endif
37617 +
37618 +       return err;
37619 +}
37620 +
37621 +struct do_xino_fwrite_args {
37622 +       ssize_t *errp;
37623 +       struct file *file;
37624 +       void *buf;
37625 +       size_t size;
37626 +       loff_t *pos;
37627 +};
37628 +
37629 +static void call_do_xino_fwrite(void *args)
37630 +{
37631 +       struct do_xino_fwrite_args *a = args;
37632 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37633 +}
37634 +
37635 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37636 +                              loff_t *pos)
37637 +{
37638 +       ssize_t err;
37639 +       int wkq_err;
37640 +       struct do_xino_fwrite_args args = {
37641 +               .errp   = &err,
37642 +               .file   = file,
37643 +               .buf    = buf,
37644 +               .size   = size,
37645 +               .pos    = pos
37646 +       };
37647 +
37648 +       /*
37649 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37650 +        * users should care about quota and real 'filesystem full.'
37651 +        */
37652 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37653 +       if (unlikely(wkq_err))
37654 +               err = wkq_err;
37655 +
37656 +       return err;
37657 +}
37658 +
37659 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37660 +{
37661 +       ssize_t err;
37662 +
37663 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37664 +               lockdep_off();
37665 +               err = do_xino_fwrite(file, buf, size, pos);
37666 +               lockdep_on();
37667 +       } else {
37668 +               lockdep_off();
37669 +               err = xino_fwrite_wkq(file, buf, size, pos);
37670 +               lockdep_on();
37671 +       }
37672 +
37673 +       return err;
37674 +}
37675 +
37676 +/* ---------------------------------------------------------------------- */
37677 +
37678 +/*
37679 + * inode number bitmap
37680 + */
37681 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37682 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37683 +{
37684 +       ino_t ino;
37685 +
37686 +       AuDebugOn(bit < 0 || page_bits <= bit);
37687 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37688 +       return ino;
37689 +}
37690 +
37691 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37692 +{
37693 +       AuDebugOn(ino < AUFS_FIRST_INO);
37694 +       ino -= AUFS_FIRST_INO;
37695 +       *pindex = ino / page_bits;
37696 +       *bit = ino % page_bits;
37697 +}
37698 +
37699 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37700 +{
37701 +       int err;
37702 +       loff_t pos;
37703 +       ssize_t sz;
37704 +       struct au_sbinfo *sbinfo;
37705 +       struct file *xib;
37706 +       unsigned long *p;
37707 +
37708 +       sbinfo = au_sbi(sb);
37709 +       MtxMustLock(&sbinfo->si_xib_mtx);
37710 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37711 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37712 +
37713 +       if (pindex == sbinfo->si_xib_last_pindex)
37714 +               return 0;
37715 +
37716 +       xib = sbinfo->si_xib;
37717 +       p = sbinfo->si_xib_buf;
37718 +       pos = sbinfo->si_xib_last_pindex;
37719 +       pos *= PAGE_SIZE;
37720 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37721 +       if (unlikely(sz != PAGE_SIZE))
37722 +               goto out;
37723 +
37724 +       pos = pindex;
37725 +       pos *= PAGE_SIZE;
37726 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37727 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37728 +       else {
37729 +               memset(p, 0, PAGE_SIZE);
37730 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37731 +       }
37732 +       if (sz == PAGE_SIZE) {
37733 +               sbinfo->si_xib_last_pindex = pindex;
37734 +               return 0; /* success */
37735 +       }
37736 +
37737 +out:
37738 +       AuIOErr1("write failed (%zd)\n", sz);
37739 +       err = sz;
37740 +       if (sz >= 0)
37741 +               err = -EIO;
37742 +       return err;
37743 +}
37744 +
37745 +static void au_xib_clear_bit(struct inode *inode)
37746 +{
37747 +       int err, bit;
37748 +       unsigned long pindex;
37749 +       struct super_block *sb;
37750 +       struct au_sbinfo *sbinfo;
37751 +
37752 +       AuDebugOn(inode->i_nlink);
37753 +
37754 +       sb = inode->i_sb;
37755 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37756 +       AuDebugOn(page_bits <= bit);
37757 +       sbinfo = au_sbi(sb);
37758 +       mutex_lock(&sbinfo->si_xib_mtx);
37759 +       err = xib_pindex(sb, pindex);
37760 +       if (!err) {
37761 +               clear_bit(bit, sbinfo->si_xib_buf);
37762 +               sbinfo->si_xib_next_bit = bit;
37763 +       }
37764 +       mutex_unlock(&sbinfo->si_xib_mtx);
37765 +}
37766 +
37767 +/* ---------------------------------------------------------------------- */
37768 +
37769 +/*
37770 + * truncate a xino bitmap file
37771 + */
37772 +
37773 +/* todo: slow */
37774 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37775 +{
37776 +       int err, bit;
37777 +       ssize_t sz;
37778 +       unsigned long pindex;
37779 +       loff_t pos, pend;
37780 +       struct au_sbinfo *sbinfo;
37781 +       ino_t *ino;
37782 +       unsigned long *p;
37783 +
37784 +       err = 0;
37785 +       sbinfo = au_sbi(sb);
37786 +       MtxMustLock(&sbinfo->si_xib_mtx);
37787 +       p = sbinfo->si_xib_buf;
37788 +       pend = vfsub_f_size_read(file);
37789 +       pos = 0;
37790 +       while (pos < pend) {
37791 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
37792 +               err = sz;
37793 +               if (unlikely(sz <= 0))
37794 +                       goto out;
37795 +
37796 +               err = 0;
37797 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37798 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37799 +                               continue;
37800 +
37801 +                       xib_calc_bit(*ino, &pindex, &bit);
37802 +                       AuDebugOn(page_bits <= bit);
37803 +                       err = xib_pindex(sb, pindex);
37804 +                       if (!err)
37805 +                               set_bit(bit, p);
37806 +                       else
37807 +                               goto out;
37808 +               }
37809 +       }
37810 +
37811 +out:
37812 +       return err;
37813 +}
37814 +
37815 +static int xib_restore(struct super_block *sb)
37816 +{
37817 +       int err, i;
37818 +       unsigned int nfile;
37819 +       aufs_bindex_t bindex, bbot;
37820 +       void *page;
37821 +       struct au_branch *br;
37822 +       struct au_xino *xi;
37823 +       struct file *file;
37824 +
37825 +       err = -ENOMEM;
37826 +       page = (void *)__get_free_page(GFP_NOFS);
37827 +       if (unlikely(!page))
37828 +               goto out;
37829 +
37830 +       err = 0;
37831 +       bbot = au_sbbot(sb);
37832 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37833 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37834 +                       br = au_sbr(sb, bindex);
37835 +                       xi = br->br_xino;
37836 +                       nfile = xi->xi_nfile;
37837 +                       for (i = 0; i < nfile; i++) {
37838 +                               file = au_xino_file(xi, i);
37839 +                               if (file)
37840 +                                       err = do_xib_restore(sb, file, page);
37841 +                       }
37842 +               } else
37843 +                       AuDbg("skip shared b%d\n", bindex);
37844 +       free_page((unsigned long)page);
37845 +
37846 +out:
37847 +       return err;
37848 +}
37849 +
37850 +int au_xib_trunc(struct super_block *sb)
37851 +{
37852 +       int err;
37853 +       ssize_t sz;
37854 +       loff_t pos;
37855 +       struct au_sbinfo *sbinfo;
37856 +       unsigned long *p;
37857 +       struct file *file;
37858 +
37859 +       SiMustWriteLock(sb);
37860 +
37861 +       err = 0;
37862 +       sbinfo = au_sbi(sb);
37863 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
37864 +               goto out;
37865 +
37866 +       file = sbinfo->si_xib;
37867 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
37868 +               goto out;
37869 +
37870 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
37871 +       err = PTR_ERR(file);
37872 +       if (IS_ERR(file))
37873 +               goto out;
37874 +       fput(sbinfo->si_xib);
37875 +       sbinfo->si_xib = file;
37876 +
37877 +       p = sbinfo->si_xib_buf;
37878 +       memset(p, 0, PAGE_SIZE);
37879 +       pos = 0;
37880 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
37881 +       if (unlikely(sz != PAGE_SIZE)) {
37882 +               err = sz;
37883 +               AuIOErr("err %d\n", err);
37884 +               if (sz >= 0)
37885 +                       err = -EIO;
37886 +               goto out;
37887 +       }
37888 +
37889 +       mutex_lock(&sbinfo->si_xib_mtx);
37890 +       /* mnt_want_write() is unnecessary here */
37891 +       err = xib_restore(sb);
37892 +       mutex_unlock(&sbinfo->si_xib_mtx);
37893 +
37894 +out:
37895 +       return err;
37896 +}
37897 +
37898 +/* ---------------------------------------------------------------------- */
37899 +
37900 +struct au_xino *au_xino_alloc(unsigned int nfile)
37901 +{
37902 +       struct au_xino *xi;
37903 +
37904 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
37905 +       if (unlikely(!xi))
37906 +               goto out;
37907 +       xi->xi_nfile = nfile;
37908 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
37909 +       if (unlikely(!xi->xi_file))
37910 +               goto out_free;
37911 +
37912 +       xi->xi_nondir.total = 8; /* initial size */
37913 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
37914 +                                     GFP_NOFS);
37915 +       if (unlikely(!xi->xi_nondir.array))
37916 +               goto out_file;
37917 +
37918 +       spin_lock_init(&xi->xi_nondir.spin);
37919 +       init_waitqueue_head(&xi->xi_nondir.wqh);
37920 +       mutex_init(&xi->xi_mtx);
37921 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
37922 +       atomic_set(&xi->xi_truncating, 0);
37923 +       kref_init(&xi->xi_kref);
37924 +       goto out; /* success */
37925 +
37926 +out_file:
37927 +       au_kfree_try_rcu(xi->xi_file);
37928 +out_free:
37929 +       au_kfree_rcu(xi);
37930 +       xi = NULL;
37931 +out:
37932 +       return xi;
37933 +}
37934 +
37935 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
37936 +{
37937 +       int err;
37938 +       struct au_xino *xi;
37939 +
37940 +       err = 0;
37941 +       xi = au_xino_alloc(idx + 1);
37942 +       if (unlikely(!xi)) {
37943 +               err = -ENOMEM;
37944 +               goto out;
37945 +       }
37946 +
37947 +       if (file)
37948 +               get_file(file);
37949 +       xi->xi_file[idx] = file;
37950 +       AuDebugOn(br->br_xino);
37951 +       br->br_xino = xi;
37952 +
37953 +out:
37954 +       return err;
37955 +}
37956 +
37957 +static void au_xino_release(struct kref *kref)
37958 +{
37959 +       struct au_xino *xi;
37960 +       int i;
37961 +       unsigned long ul;
37962 +       struct hlist_bl_head *hbl;
37963 +       struct hlist_bl_node *pos, *n;
37964 +       struct au_xi_writing *p;
37965 +
37966 +       xi = container_of(kref, struct au_xino, xi_kref);
37967 +       for (i = 0; i < xi->xi_nfile; i++)
37968 +               if (xi->xi_file[i])
37969 +                       fput(xi->xi_file[i]);
37970 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
37971 +               AuDebugOn(xi->xi_nondir.array[i]);
37972 +       mutex_destroy(&xi->xi_mtx);
37973 +       hbl = &xi->xi_writing;
37974 +       ul = au_hbl_count(hbl);
37975 +       if (unlikely(ul)) {
37976 +               pr_warn("xi_writing %lu\n", ul);
37977 +               hlist_bl_lock(hbl);
37978 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
37979 +                       hlist_bl_del(&p->node);
37980 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
37981 +                       kfree(p);
37982 +               }
37983 +               hlist_bl_unlock(hbl);
37984 +       }
37985 +       au_kfree_try_rcu(xi->xi_file);
37986 +       au_kfree_try_rcu(xi->xi_nondir.array);
37987 +       au_kfree_rcu(xi);
37988 +}
37989 +
37990 +int au_xino_put(struct au_branch *br)
37991 +{
37992 +       int ret;
37993 +       struct au_xino *xi;
37994 +
37995 +       ret = 0;
37996 +       xi = br->br_xino;
37997 +       if (xi) {
37998 +               br->br_xino = NULL;
37999 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38000 +       }
38001 +
38002 +       return ret;
38003 +}
38004 +
38005 +/* ---------------------------------------------------------------------- */
38006 +
38007 +/*
38008 + * xino mount option handlers
38009 + */
38010 +
38011 +/* xino bitmap */
38012 +static void xino_clear_xib(struct super_block *sb)
38013 +{
38014 +       struct au_sbinfo *sbinfo;
38015 +
38016 +       SiMustWriteLock(sb);
38017 +
38018 +       sbinfo = au_sbi(sb);
38019 +       if (sbinfo->si_xib)
38020 +               fput(sbinfo->si_xib);
38021 +       sbinfo->si_xib = NULL;
38022 +       if (sbinfo->si_xib_buf)
38023 +               free_page((unsigned long)sbinfo->si_xib_buf);
38024 +       sbinfo->si_xib_buf = NULL;
38025 +}
38026 +
38027 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38028 +{
38029 +       int err;
38030 +       loff_t pos;
38031 +       struct au_sbinfo *sbinfo;
38032 +       struct file *file;
38033 +       struct super_block *xi_sb;
38034 +
38035 +       SiMustWriteLock(sb);
38036 +
38037 +       sbinfo = au_sbi(sb);
38038 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38039 +       err = PTR_ERR(file);
38040 +       if (IS_ERR(file))
38041 +               goto out;
38042 +       if (sbinfo->si_xib)
38043 +               fput(sbinfo->si_xib);
38044 +       sbinfo->si_xib = file;
38045 +       xi_sb = file_inode(file)->i_sb;
38046 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38047 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38048 +               err = -EIO;
38049 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38050 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38051 +               goto out_unset;
38052 +       }
38053 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38054 +
38055 +       err = -ENOMEM;
38056 +       if (!sbinfo->si_xib_buf)
38057 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38058 +       if (unlikely(!sbinfo->si_xib_buf))
38059 +               goto out_unset;
38060 +
38061 +       sbinfo->si_xib_last_pindex = 0;
38062 +       sbinfo->si_xib_next_bit = 0;
38063 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38064 +               pos = 0;
38065 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38066 +               if (unlikely(err != PAGE_SIZE))
38067 +                       goto out_free;
38068 +       }
38069 +       err = 0;
38070 +       goto out; /* success */
38071 +
38072 +out_free:
38073 +       if (sbinfo->si_xib_buf)
38074 +               free_page((unsigned long)sbinfo->si_xib_buf);
38075 +       sbinfo->si_xib_buf = NULL;
38076 +       if (err >= 0)
38077 +               err = -EIO;
38078 +out_unset:
38079 +       fput(sbinfo->si_xib);
38080 +       sbinfo->si_xib = NULL;
38081 +out:
38082 +       AuTraceErr(err);
38083 +       return err;
38084 +}
38085 +
38086 +/* xino for each branch */
38087 +static void xino_clear_br(struct super_block *sb)
38088 +{
38089 +       aufs_bindex_t bindex, bbot;
38090 +       struct au_branch *br;
38091 +
38092 +       bbot = au_sbbot(sb);
38093 +       for (bindex = 0; bindex <= bbot; bindex++) {
38094 +               br = au_sbr(sb, bindex);
38095 +               AuDebugOn(!br);
38096 +               au_xino_put(br);
38097 +       }
38098 +}
38099 +
38100 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38101 +                                 aufs_bindex_t bshared)
38102 +{
38103 +       struct au_branch *brshared;
38104 +
38105 +       brshared = au_sbr(sb, bshared);
38106 +       AuDebugOn(!brshared->br_xino);
38107 +       AuDebugOn(!brshared->br_xino->xi_file);
38108 +       if (br->br_xino != brshared->br_xino) {
38109 +               au_xino_get(brshared);
38110 +               au_xino_put(br);
38111 +               br->br_xino = brshared->br_xino;
38112 +       }
38113 +}
38114 +
38115 +struct au_xino_do_set_br {
38116 +       struct au_branch *br;
38117 +       ino_t h_ino;
38118 +       aufs_bindex_t bshared;
38119 +};
38120 +
38121 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38122 +                            struct au_xino_do_set_br *args)
38123 +{
38124 +       int err;
38125 +       struct au_xi_calc calc;
38126 +       struct file *file;
38127 +       struct au_branch *br;
38128 +       struct au_xi_new xinew = {
38129 +               .base = path
38130 +       };
38131 +
38132 +       br = args->br;
38133 +       xinew.xi = br->br_xino;
38134 +       au_xi_calc(sb, args->h_ino, &calc);
38135 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38136 +       if (args->bshared >= 0)
38137 +               /* shared xino */
38138 +               au_xino_set_br_shared(sb, br, args->bshared);
38139 +       else if (!xinew.xi) {
38140 +               /* new xino */
38141 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38142 +               if (unlikely(err))
38143 +                       goto out;
38144 +       }
38145 +
38146 +       /* force re-creating */
38147 +       xinew.xi = br->br_xino;
38148 +       xinew.idx = calc.idx;
38149 +       mutex_lock(&xinew.xi->xi_mtx);
38150 +       file = au_xi_new(sb, &xinew);
38151 +       mutex_unlock(&xinew.xi->xi_mtx);
38152 +       err = PTR_ERR(file);
38153 +       if (IS_ERR(file))
38154 +               goto out;
38155 +       AuDebugOn(!file);
38156 +
38157 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38158 +       if (unlikely(err))
38159 +               au_xino_put(br);
38160 +
38161 +out:
38162 +       AuTraceErr(err);
38163 +       return err;
38164 +}
38165 +
38166 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38167 +{
38168 +       int err;
38169 +       aufs_bindex_t bindex, bbot;
38170 +       struct au_xino_do_set_br args;
38171 +       struct inode *inode;
38172 +
38173 +       SiMustWriteLock(sb);
38174 +
38175 +       bbot = au_sbbot(sb);
38176 +       inode = d_inode(sb->s_root);
38177 +       for (bindex = 0; bindex <= bbot; bindex++) {
38178 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38179 +               args.br = au_sbr(sb, bindex);
38180 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38181 +               err = au_xino_do_set_br(sb, path, &args);
38182 +               if (unlikely(err))
38183 +                       break;
38184 +       }
38185 +
38186 +       AuTraceErr(err);
38187 +       return err;
38188 +}
38189 +
38190 +void au_xino_clr(struct super_block *sb)
38191 +{
38192 +       struct au_sbinfo *sbinfo;
38193 +
38194 +       au_xigen_clr(sb);
38195 +       xino_clear_xib(sb);
38196 +       xino_clear_br(sb);
38197 +       dbgaufs_brs_del(sb, 0);
38198 +       sbinfo = au_sbi(sb);
38199 +       /* lvalue, do not call au_mntflags() */
38200 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38201 +}
38202 +
38203 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38204 +{
38205 +       int err, skip;
38206 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38207 +       struct qstr *dname, *cur_name;
38208 +       struct file *cur_xino;
38209 +       struct au_sbinfo *sbinfo;
38210 +       struct path *path, *cur_path;
38211 +
38212 +       SiMustWriteLock(sb);
38213 +
38214 +       err = 0;
38215 +       sbinfo = au_sbi(sb);
38216 +       path = &xiopt->file->f_path;
38217 +       dentry = path->dentry;
38218 +       parent = dget_parent(dentry);
38219 +       if (remount) {
38220 +               skip = 0;
38221 +               cur_xino = sbinfo->si_xib;
38222 +               if (cur_xino) {
38223 +                       cur_path = &cur_xino->f_path;
38224 +                       cur_dentry = cur_path->dentry;
38225 +                       cur_parent = dget_parent(cur_dentry);
38226 +                       cur_name = &cur_dentry->d_name;
38227 +                       dname = &dentry->d_name;
38228 +                       skip = (cur_parent == parent
38229 +                               && au_qstreq(dname, cur_name));
38230 +                       dput(cur_parent);
38231 +               }
38232 +               if (skip)
38233 +                       goto out;
38234 +       }
38235 +
38236 +       au_opt_set(sbinfo->si_mntflags, XINO);
38237 +       err = au_xino_set_xib(sb, path);
38238 +       /* si_x{read,write} are set */
38239 +       if (!err)
38240 +               err = au_xigen_set(sb, path);
38241 +       if (!err)
38242 +               err = au_xino_set_br(sb, path);
38243 +       if (!err) {
38244 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38245 +               goto out; /* success */
38246 +       }
38247 +
38248 +       /* reset all */
38249 +       AuIOErr("failed setting xino(%d).\n", err);
38250 +       au_xino_clr(sb);
38251 +
38252 +out:
38253 +       dput(parent);
38254 +       return err;
38255 +}
38256 +
38257 +/*
38258 + * create a xinofile at the default place/path.
38259 + */
38260 +struct file *au_xino_def(struct super_block *sb)
38261 +{
38262 +       struct file *file;
38263 +       char *page, *p;
38264 +       struct au_branch *br;
38265 +       struct super_block *h_sb;
38266 +       struct path path;
38267 +       aufs_bindex_t bbot, bindex, bwr;
38268 +
38269 +       br = NULL;
38270 +       bbot = au_sbbot(sb);
38271 +       bwr = -1;
38272 +       for (bindex = 0; bindex <= bbot; bindex++) {
38273 +               br = au_sbr(sb, bindex);
38274 +               if (au_br_writable(br->br_perm)
38275 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38276 +                       bwr = bindex;
38277 +                       break;
38278 +               }
38279 +       }
38280 +
38281 +       if (bwr >= 0) {
38282 +               file = ERR_PTR(-ENOMEM);
38283 +               page = (void *)__get_free_page(GFP_NOFS);
38284 +               if (unlikely(!page))
38285 +                       goto out;
38286 +               path.mnt = au_br_mnt(br);
38287 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38288 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38289 +               file = (void *)p;
38290 +               if (!IS_ERR(p)) {
38291 +                       strcat(p, "/" AUFS_XINO_FNAME);
38292 +                       AuDbg("%s\n", p);
38293 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38294 +               }
38295 +               free_page((unsigned long)page);
38296 +       } else {
38297 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38298 +                                     /*wbrtop*/0);
38299 +               if (IS_ERR(file))
38300 +                       goto out;
38301 +               h_sb = file->f_path.dentry->d_sb;
38302 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38303 +                       pr_err("xino doesn't support %s(%s)\n",
38304 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38305 +                       fput(file);
38306 +                       file = ERR_PTR(-EINVAL);
38307 +               }
38308 +       }
38309 +
38310 +out:
38311 +       return file;
38312 +}
38313 +
38314 +/* ---------------------------------------------------------------------- */
38315 +
38316 +/*
38317 + * initialize the xinofile for the specified branch @br
38318 + * at the place/path where @base_file indicates.
38319 + * test whether another branch is on the same filesystem or not,
38320 + * if found then share the xinofile with another branch.
38321 + */
38322 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38323 +                   struct path *base)
38324 +{
38325 +       int err;
38326 +       struct au_xino_do_set_br args = {
38327 +               .h_ino  = h_ino,
38328 +               .br     = br
38329 +       };
38330 +
38331 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38332 +                                      au_br_sb(br));
38333 +       err = au_xino_do_set_br(sb, base, &args);
38334 +       if (unlikely(err))
38335 +               au_xino_put(br);
38336 +
38337 +       return err;
38338 +}
38339 +
38340 +/* ---------------------------------------------------------------------- */
38341 +
38342 +/*
38343 + * get an unused inode number from bitmap
38344 + */
38345 +ino_t au_xino_new_ino(struct super_block *sb)
38346 +{
38347 +       ino_t ino;
38348 +       unsigned long *p, pindex, ul, pend;
38349 +       struct au_sbinfo *sbinfo;
38350 +       struct file *file;
38351 +       int free_bit, err;
38352 +
38353 +       if (!au_opt_test(au_mntflags(sb), XINO))
38354 +               return iunique(sb, AUFS_FIRST_INO);
38355 +
38356 +       sbinfo = au_sbi(sb);
38357 +       mutex_lock(&sbinfo->si_xib_mtx);
38358 +       p = sbinfo->si_xib_buf;
38359 +       free_bit = sbinfo->si_xib_next_bit;
38360 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38361 +               goto out; /* success */
38362 +       free_bit = find_first_zero_bit(p, page_bits);
38363 +       if (free_bit < page_bits)
38364 +               goto out; /* success */
38365 +
38366 +       pindex = sbinfo->si_xib_last_pindex;
38367 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38368 +               err = xib_pindex(sb, ul);
38369 +               if (unlikely(err))
38370 +                       goto out_err;
38371 +               free_bit = find_first_zero_bit(p, page_bits);
38372 +               if (free_bit < page_bits)
38373 +                       goto out; /* success */
38374 +       }
38375 +
38376 +       file = sbinfo->si_xib;
38377 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38378 +       for (ul = pindex + 1; ul <= pend; ul++) {
38379 +               err = xib_pindex(sb, ul);
38380 +               if (unlikely(err))
38381 +                       goto out_err;
38382 +               free_bit = find_first_zero_bit(p, page_bits);
38383 +               if (free_bit < page_bits)
38384 +                       goto out; /* success */
38385 +       }
38386 +       BUG();
38387 +
38388 +out:
38389 +       set_bit(free_bit, p);
38390 +       sbinfo->si_xib_next_bit = free_bit + 1;
38391 +       pindex = sbinfo->si_xib_last_pindex;
38392 +       mutex_unlock(&sbinfo->si_xib_mtx);
38393 +       ino = xib_calc_ino(pindex, free_bit);
38394 +       AuDbg("i%lu\n", (unsigned long)ino);
38395 +       return ino;
38396 +out_err:
38397 +       mutex_unlock(&sbinfo->si_xib_mtx);
38398 +       AuDbg("i0\n");
38399 +       return 0;
38400 +}
38401 +
38402 +/* for s_op->delete_inode() */
38403 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38404 +{
38405 +       int err;
38406 +       unsigned int mnt_flags;
38407 +       aufs_bindex_t bindex, bbot, bi;
38408 +       unsigned char try_trunc;
38409 +       struct au_iinfo *iinfo;
38410 +       struct super_block *sb;
38411 +       struct au_hinode *hi;
38412 +       struct inode *h_inode;
38413 +       struct au_branch *br;
38414 +       struct au_xi_calc calc;
38415 +       struct file *file;
38416 +
38417 +       AuDebugOn(au_is_bad_inode(inode));
38418 +
38419 +       sb = inode->i_sb;
38420 +       mnt_flags = au_mntflags(sb);
38421 +       if (!au_opt_test(mnt_flags, XINO)
38422 +           || inode->i_ino == AUFS_ROOT_INO)
38423 +               return;
38424 +
38425 +       if (unlinked) {
38426 +               au_xigen_inc(inode);
38427 +               au_xib_clear_bit(inode);
38428 +       }
38429 +
38430 +       iinfo = au_ii(inode);
38431 +       bindex = iinfo->ii_btop;
38432 +       if (bindex < 0)
38433 +               return;
38434 +
38435 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38436 +       hi = au_hinode(iinfo, bindex);
38437 +       bbot = iinfo->ii_bbot;
38438 +       for (; bindex <= bbot; bindex++, hi++) {
38439 +               h_inode = hi->hi_inode;
38440 +               if (!h_inode
38441 +                   || (!unlinked && h_inode->i_nlink))
38442 +                       continue;
38443 +
38444 +               /* inode may not be revalidated */
38445 +               bi = au_br_index(sb, hi->hi_id);
38446 +               if (bi < 0)
38447 +                       continue;
38448 +
38449 +               br = au_sbr(sb, bi);
38450 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38451 +               file = au_xino_file(br->br_xino, calc.idx);
38452 +               if (IS_ERR_OR_NULL(file))
38453 +                       continue;
38454 +
38455 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38456 +               if (!err && try_trunc
38457 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38458 +                       xino_try_trunc(sb, br);
38459 +       }
38460 +}
38461 +
38462 +/* ---------------------------------------------------------------------- */
38463 +
38464 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38465 +{
38466 +       int found, total, i;
38467 +
38468 +       found = -1;
38469 +       total = xi->xi_nondir.total;
38470 +       for (i = 0; i < total; i++) {
38471 +               if (xi->xi_nondir.array[i] != h_ino)
38472 +                       continue;
38473 +               found = i;
38474 +               break;
38475 +       }
38476 +
38477 +       return found;
38478 +}
38479 +
38480 +static int au_xinondir_expand(struct au_xino *xi)
38481 +{
38482 +       int err, sz;
38483 +       ino_t *p;
38484 +
38485 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38486 +
38487 +       err = -ENOMEM;
38488 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38489 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38490 +               goto out;
38491 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38492 +                        /*may_shrink*/0);
38493 +       if (p) {
38494 +               xi->xi_nondir.array = p;
38495 +               xi->xi_nondir.total <<= 1;
38496 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38497 +               err = 0;
38498 +       }
38499 +
38500 +out:
38501 +       return err;
38502 +}
38503 +
38504 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38505 +                      ino_t h_ino, int idx)
38506 +{
38507 +       struct au_xino *xi;
38508 +
38509 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38510 +       xi = au_sbr(sb, bindex)->br_xino;
38511 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38512 +
38513 +       spin_lock(&xi->xi_nondir.spin);
38514 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38515 +       xi->xi_nondir.array[idx] = 0;
38516 +       spin_unlock(&xi->xi_nondir.spin);
38517 +       wake_up_all(&xi->xi_nondir.wqh);
38518 +}
38519 +
38520 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38521 +                     int *idx)
38522 +{
38523 +       int err, found, empty;
38524 +       struct au_xino *xi;
38525 +
38526 +       err = 0;
38527 +       *idx = -1;
38528 +       if (!au_opt_test(au_mntflags(sb), XINO))
38529 +               goto out; /* no xino */
38530 +
38531 +       xi = au_sbr(sb, bindex)->br_xino;
38532 +
38533 +again:
38534 +       spin_lock(&xi->xi_nondir.spin);
38535 +       found = au_xinondir_find(xi, h_ino);
38536 +       if (found == -1) {
38537 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38538 +               if (empty == -1) {
38539 +                       empty = xi->xi_nondir.total;
38540 +                       err = au_xinondir_expand(xi);
38541 +                       if (unlikely(err))
38542 +                               goto out_unlock;
38543 +               }
38544 +               xi->xi_nondir.array[empty] = h_ino;
38545 +               *idx = empty;
38546 +       } else {
38547 +               spin_unlock(&xi->xi_nondir.spin);
38548 +               wait_event(xi->xi_nondir.wqh,
38549 +                          xi->xi_nondir.array[found] != h_ino);
38550 +               goto again;
38551 +       }
38552 +
38553 +out_unlock:
38554 +       spin_unlock(&xi->xi_nondir.spin);
38555 +out:
38556 +       return err;
38557 +}
38558 +
38559 +/* ---------------------------------------------------------------------- */
38560 +
38561 +int au_xino_path(struct seq_file *seq, struct file *file)
38562 +{
38563 +       int err;
38564 +
38565 +       err = au_seq_path(seq, &file->f_path);
38566 +       if (unlikely(err))
38567 +               goto out;
38568 +
38569 +#define Deleted "\\040(deleted)"
38570 +       seq->count -= sizeof(Deleted) - 1;
38571 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38572 +                        sizeof(Deleted) - 1));
38573 +#undef Deleted
38574 +
38575 +out:
38576 +       return err;
38577 +}
38578 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38579 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38580 +++ linux/include/uapi/linux/aufs_type.h        2020-10-14 08:51:27.819581640 +0200
38581 @@ -0,0 +1,452 @@
38582 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38583 +/*
38584 + * Copyright (C) 2005-2020 Junjiro R. Okajima
38585 + *
38586 + * This program, aufs is free software; you can redistribute it and/or modify
38587 + * it under the terms of the GNU General Public License as published by
38588 + * the Free Software Foundation; either version 2 of the License, or
38589 + * (at your option) any later version.
38590 + *
38591 + * This program is distributed in the hope that it will be useful,
38592 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38593 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38594 + * GNU General Public License for more details.
38595 + *
38596 + * You should have received a copy of the GNU General Public License
38597 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38598 + */
38599 +
38600 +#ifndef __AUFS_TYPE_H__
38601 +#define __AUFS_TYPE_H__
38602 +
38603 +#define AUFS_NAME      "aufs"
38604 +
38605 +#ifdef __KERNEL__
38606 +/*
38607 + * define it before including all other headers.
38608 + * sched.h may use pr_* macros before defining "current", so define the
38609 + * no-current version first, and re-define later.
38610 + */
38611 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38612 +#include <linux/sched.h>
38613 +#undef pr_fmt
38614 +#define pr_fmt(fmt) \
38615 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38616 +               (int)sizeof(current->comm), current->comm, current->pid
38617 +#include <linux/limits.h>
38618 +#else
38619 +#include <stdint.h>
38620 +#include <sys/types.h>
38621 +#include <limits.h>
38622 +#endif /* __KERNEL__ */
38623 +
38624 +#define AUFS_VERSION   "5.x-rcN-20200622"
38625 +
38626 +/* todo? move this to linux-2.6.19/include/magic.h */
38627 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38628 +
38629 +/* ---------------------------------------------------------------------- */
38630 +
38631 +#ifdef __KERNEL__
38632 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38633 +typedef int8_t aufs_bindex_t;
38634 +#define AUFS_BRANCH_MAX 127
38635 +#else
38636 +typedef int16_t aufs_bindex_t;
38637 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38638 +#define AUFS_BRANCH_MAX 511
38639 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38640 +#define AUFS_BRANCH_MAX 1023
38641 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38642 +#define AUFS_BRANCH_MAX 32767
38643 +#endif
38644 +#endif
38645 +
38646 +#ifndef AUFS_BRANCH_MAX
38647 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38648 +#endif
38649 +#endif /* __KERNEL__ */
38650 +
38651 +/* ---------------------------------------------------------------------- */
38652 +
38653 +#define AUFS_FSTYPE            AUFS_NAME
38654 +
38655 +#define AUFS_ROOT_INO          2
38656 +#define AUFS_FIRST_INO         11
38657 +
38658 +#define AUFS_WH_PFX            ".wh."
38659 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38660 +#define AUFS_WH_TMP_LEN                4
38661 +/* a limit for rmdir/rename a dir and copyup */
38662 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38663 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38664 +                               - 1                     /* dot */\
38665 +                               - AUFS_WH_TMP_LEN)      /* hex */
38666 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38667 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38668 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38669 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38670 +#define AUFS_DIRWH_DEF         3
38671 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38672 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38673 +#define AUFS_RDBLK_DEF         512 /* bytes */
38674 +#define AUFS_RDHASH_DEF                32
38675 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38676 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38677 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38678 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38679 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38680 +
38681 +/* pseudo-link maintenace under /proc */
38682 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38683 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38684 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38685 +
38686 +/* dirren, renamed dir */
38687 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38688 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38689 +/* whiteouted doubly */
38690 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38691 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38692 +
38693 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38694 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38695 +
38696 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38697 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38698 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38699 +
38700 +/* doubly whiteouted */
38701 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38702 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38703 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38704 +
38705 +/* branch permissions and attributes */
38706 +#define AUFS_BRPERM_RW         "rw"
38707 +#define AUFS_BRPERM_RO         "ro"
38708 +#define AUFS_BRPERM_RR         "rr"
38709 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38710 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38711 +#define AUFS_BRATTR_FHSM       "fhsm"
38712 +#define AUFS_BRATTR_UNPIN      "unpin"
38713 +#define AUFS_BRATTR_ICEX       "icex"
38714 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38715 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38716 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38717 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38718 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38719 +#define AUFS_BRRATTR_WH                "wh"
38720 +#define AUFS_BRWATTR_NLWH      "nolwh"
38721 +#define AUFS_BRWATTR_MOO       "moo"
38722 +
38723 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38724 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38725 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38726 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38727 +
38728 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38729 +#define AuBrAttr_COO_ALL       (1 << 4)
38730 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38731 +
38732 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38733 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38734 +                                                  branch. meaningless since
38735 +                                                  linux-3.18-rc1 */
38736 +
38737 +/* ignore error in copying XATTR */
38738 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38739 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38740 +#define AuBrAttr_ICEX_TR       (1 << 9)
38741 +#define AuBrAttr_ICEX_USR      (1 << 10)
38742 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38743 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38744 +                                | AuBrAttr_ICEX_SYS    \
38745 +                                | AuBrAttr_ICEX_TR     \
38746 +                                | AuBrAttr_ICEX_USR    \
38747 +                                | AuBrAttr_ICEX_OTH)
38748 +
38749 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38750 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38751 +
38752 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38753 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38754 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38755 +
38756 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38757 +
38758 +/* #warning test userspace */
38759 +#ifdef __KERNEL__
38760 +#ifndef CONFIG_AUFS_FHSM
38761 +#undef AuBrAttr_FHSM
38762 +#define AuBrAttr_FHSM          0
38763 +#endif
38764 +#ifndef CONFIG_AUFS_XATTR
38765 +#undef AuBrAttr_ICEX
38766 +#define AuBrAttr_ICEX          0
38767 +#undef AuBrAttr_ICEX_SEC
38768 +#define AuBrAttr_ICEX_SEC      0
38769 +#undef AuBrAttr_ICEX_SYS
38770 +#define AuBrAttr_ICEX_SYS      0
38771 +#undef AuBrAttr_ICEX_TR
38772 +#define AuBrAttr_ICEX_TR       0
38773 +#undef AuBrAttr_ICEX_USR
38774 +#define AuBrAttr_ICEX_USR      0
38775 +#undef AuBrAttr_ICEX_OTH
38776 +#define AuBrAttr_ICEX_OTH      0
38777 +#endif
38778 +#endif
38779 +
38780 +/* the longest combination */
38781 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38782 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38783 +                              "+" AUFS_BRATTR_COO_REG          \
38784 +                              "+" AUFS_BRATTR_FHSM             \
38785 +                              "+" AUFS_BRATTR_UNPIN            \
38786 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38787 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38788 +                              "+" AUFS_BRATTR_ICEX_USR         \
38789 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38790 +                              "+" AUFS_BRWATTR_NLWH)
38791 +
38792 +typedef struct {
38793 +       char a[AuBrPermStrSz];
38794 +} au_br_perm_str_t;
38795 +
38796 +static inline int au_br_writable(int brperm)
38797 +{
38798 +       return brperm & AuBrPerm_RW;
38799 +}
38800 +
38801 +static inline int au_br_whable(int brperm)
38802 +{
38803 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38804 +}
38805 +
38806 +static inline int au_br_wh_linkable(int brperm)
38807 +{
38808 +       return !(brperm & AuBrWAttr_NoLinkWH);
38809 +}
38810 +
38811 +static inline int au_br_cmoo(int brperm)
38812 +{
38813 +       return brperm & AuBrAttr_CMOO_Mask;
38814 +}
38815 +
38816 +static inline int au_br_fhsm(int brperm)
38817 +{
38818 +       return brperm & AuBrAttr_FHSM;
38819 +}
38820 +
38821 +/* ---------------------------------------------------------------------- */
38822 +
38823 +/* ioctl */
38824 +enum {
38825 +       /* readdir in userspace */
38826 +       AuCtl_RDU,
38827 +       AuCtl_RDU_INO,
38828 +
38829 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38830 +       AuCtl_IBUSY,    /* busy inode */
38831 +       AuCtl_MVDOWN,   /* move-down */
38832 +       AuCtl_BR,       /* info about branches */
38833 +       AuCtl_FHSM_FD   /* connection for fhsm */
38834 +};
38835 +
38836 +/* borrowed from linux/include/linux/kernel.h */
38837 +#ifndef ALIGN
38838 +#ifdef _GNU_SOURCE
38839 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38840 +#else
38841 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
38842 +#endif
38843 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38844 +#endif
38845 +
38846 +/* borrowed from linux/include/linux/compiler-gcc3.h */
38847 +#ifndef __aligned
38848 +#define __aligned(x)                   __attribute__((aligned(x)))
38849 +#endif
38850 +
38851 +#ifdef __KERNEL__
38852 +#ifndef __packed
38853 +#define __packed                       __attribute__((packed))
38854 +#endif
38855 +#endif
38856 +
38857 +struct au_rdu_cookie {
38858 +       uint64_t        h_pos;
38859 +       int16_t         bindex;
38860 +       uint8_t         flags;
38861 +       uint8_t         pad;
38862 +       uint32_t        generation;
38863 +} __aligned(8);
38864 +
38865 +struct au_rdu_ent {
38866 +       uint64_t        ino;
38867 +       int16_t         bindex;
38868 +       uint8_t         type;
38869 +       uint8_t         nlen;
38870 +       uint8_t         wh;
38871 +       char            name[];
38872 +} __aligned(8);
38873 +
38874 +static inline int au_rdu_len(int nlen)
38875 +{
38876 +       /* include the terminating NULL */
38877 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
38878 +                    sizeof(uint64_t));
38879 +}
38880 +
38881 +union au_rdu_ent_ul {
38882 +       struct au_rdu_ent __user        *e;
38883 +       uint64_t                        ul;
38884 +};
38885 +
38886 +enum {
38887 +       AufsCtlRduV_SZ,
38888 +       AufsCtlRduV_End
38889 +};
38890 +
38891 +struct aufs_rdu {
38892 +       /* input */
38893 +       union {
38894 +               uint64_t        sz;     /* AuCtl_RDU */
38895 +               uint64_t        nent;   /* AuCtl_RDU_INO */
38896 +       };
38897 +       union au_rdu_ent_ul     ent;
38898 +       uint16_t                verify[AufsCtlRduV_End];
38899 +
38900 +       /* input/output */
38901 +       uint32_t                blk;
38902 +
38903 +       /* output */
38904 +       union au_rdu_ent_ul     tail;
38905 +       /* number of entries which were added in a single call */
38906 +       uint64_t                rent;
38907 +       uint8_t                 full;
38908 +       uint8_t                 shwh;
38909 +
38910 +       struct au_rdu_cookie    cookie;
38911 +} __aligned(8);
38912 +
38913 +/* ---------------------------------------------------------------------- */
38914 +
38915 +/* dirren. the branch is identified by the filename who contains this */
38916 +struct au_drinfo {
38917 +       uint64_t ino;
38918 +       union {
38919 +               uint8_t oldnamelen;
38920 +               uint64_t _padding;
38921 +       };
38922 +       uint8_t oldname[];
38923 +} __aligned(8);
38924 +
38925 +struct au_drinfo_fdata {
38926 +       uint32_t magic;
38927 +       struct au_drinfo drinfo;
38928 +} __aligned(8);
38929 +
38930 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
38931 +/* future */
38932 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
38933 +
38934 +/* ---------------------------------------------------------------------- */
38935 +
38936 +struct aufs_wbr_fd {
38937 +       uint32_t        oflags;
38938 +       int16_t         brid;
38939 +} __aligned(8);
38940 +
38941 +/* ---------------------------------------------------------------------- */
38942 +
38943 +struct aufs_ibusy {
38944 +       uint64_t        ino, h_ino;
38945 +       int16_t         bindex;
38946 +} __aligned(8);
38947 +
38948 +/* ---------------------------------------------------------------------- */
38949 +
38950 +/* error code for move-down */
38951 +/* the actual message strings are implemented in aufs-util.git */
38952 +enum {
38953 +       EAU_MVDOWN_OPAQUE = 1,
38954 +       EAU_MVDOWN_WHITEOUT,
38955 +       EAU_MVDOWN_UPPER,
38956 +       EAU_MVDOWN_BOTTOM,
38957 +       EAU_MVDOWN_NOUPPER,
38958 +       EAU_MVDOWN_NOLOWERBR,
38959 +       EAU_Last
38960 +};
38961 +
38962 +/* flags for move-down */
38963 +#define AUFS_MVDOWN_DMSG       1
38964 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
38965 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
38966 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
38967 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
38968 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
38969 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
38970 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
38971 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
38972 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
38973 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
38974 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
38975 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
38976 +
38977 +/* index for move-down */
38978 +enum {
38979 +       AUFS_MVDOWN_UPPER,
38980 +       AUFS_MVDOWN_LOWER,
38981 +       AUFS_MVDOWN_NARRAY
38982 +};
38983 +
38984 +/*
38985 + * additional info of move-down
38986 + * number of free blocks and inodes.
38987 + * subset of struct kstatfs, but smaller and always 64bit.
38988 + */
38989 +struct aufs_stfs {
38990 +       uint64_t        f_blocks;
38991 +       uint64_t        f_bavail;
38992 +       uint64_t        f_files;
38993 +       uint64_t        f_ffree;
38994 +};
38995 +
38996 +struct aufs_stbr {
38997 +       int16_t                 brid;   /* optional input */
38998 +       int16_t                 bindex; /* output */
38999 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39000 +} __aligned(8);
39001 +
39002 +struct aufs_mvdown {
39003 +       uint32_t                flags;                  /* input/output */
39004 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39005 +       int8_t                  au_errno;               /* output */
39006 +} __aligned(8);
39007 +
39008 +/* ---------------------------------------------------------------------- */
39009 +
39010 +union aufs_brinfo {
39011 +       /* PATH_MAX may differ between kernel-space and user-space */
39012 +       char    _spacer[4096];
39013 +       struct {
39014 +               int16_t id;
39015 +               int     perm;
39016 +               char    path[];
39017 +       };
39018 +} __aligned(8);
39019 +
39020 +/* ---------------------------------------------------------------------- */
39021 +
39022 +#define AuCtlType              'A'
39023 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39024 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39025 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39026 +                                    struct aufs_wbr_fd)
39027 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39028 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39029 +                                     struct aufs_mvdown)
39030 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39031 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39032 +
39033 +#endif /* __AUFS_TYPE_H__ */
39034 SPDX-License-Identifier: GPL-2.0
39035 aufs5.x-rcN loopback patch
39036
39037 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39038 index 0479902a0cd50..758c8ee4d4e6b 100644
39039 --- a/drivers/block/loop.c
39040 +++ b/drivers/block/loop.c
39041 @@ -638,6 +638,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39042                         lo->use_dio);
39043  }
39044  
39045 +static struct file *loop_real_file(struct file *file)
39046 +{
39047 +       struct file *f = NULL;
39048 +
39049 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39050 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39051 +       return f;
39052 +}
39053 +
39054  static void loop_reread_partitions(struct loop_device *lo,
39055                                    struct block_device *bdev)
39056  {
39057 @@ -693,6 +702,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39058                           unsigned int arg)
39059  {
39060         struct file     *file = NULL, *old_file;
39061 +       struct file     *f, *virt_file = NULL, *old_virt_file;
39062         int             error;
39063         bool            partscan;
39064  
39065 @@ -712,12 +722,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39066         file = fget(arg);
39067         if (!file)
39068                 goto out_err;
39069 +       f = loop_real_file(file);
39070 +       if (f) {
39071 +               virt_file = file;
39072 +               file = f;
39073 +               get_file(file);
39074 +       }
39075  
39076         error = loop_validate_file(file, bdev);
39077         if (error)
39078                 goto out_err;
39079  
39080         old_file = lo->lo_backing_file;
39081 +       old_virt_file = lo->lo_backing_virt_file;
39082  
39083         error = -EINVAL;
39084  
39085 @@ -729,6 +746,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39086         blk_mq_freeze_queue(lo->lo_queue);
39087         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39088         lo->lo_backing_file = file;
39089 +       lo->lo_backing_virt_file = virt_file;
39090         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39091         mapping_set_gfp_mask(file->f_mapping,
39092                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39093 @@ -742,6 +760,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39094          * dependency.
39095          */
39096         fput(old_file);
39097 +       if (old_virt_file)
39098 +               fput(old_virt_file);
39099         if (partscan)
39100                 loop_reread_partitions(lo, bdev);
39101         return 0;
39102 @@ -750,6 +770,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39103         mutex_unlock(&loop_ctl_mutex);
39104         if (file)
39105                 fput(file);
39106 +       if (virt_file)
39107 +               fput(virt_file);
39108         return error;
39109  }
39110  
39111 @@ -973,7 +995,7 @@ static void loop_update_rotational(struct loop_device *lo)
39112  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39113                        struct block_device *bdev, unsigned int arg)
39114  {
39115 -       struct file     *file;
39116 +       struct file     *file, *f, *virt_file = NULL;
39117         struct inode    *inode;
39118         struct address_space *mapping;
39119         struct block_device *claimed_bdev = NULL;
39120 @@ -989,6 +1011,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39121         file = fget(arg);
39122         if (!file)
39123                 goto out;
39124 +       f = loop_real_file(file);
39125 +       if (f) {
39126 +               virt_file = file;
39127 +               file = f;
39128 +               get_file(file);
39129 +       }
39130  
39131         /*
39132          * If we don't hold exclusive handle for the device, upgrade to it
39133 @@ -1079,6 +1079,7 @@ static int loop_configure(struct loop_de
39134         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39135         lo->lo_device = bdev;
39136         lo->lo_backing_file = file;
39137 +       lo->lo_backing_virt_file = virt_file;
39138         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39139         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39140  
39141 @@ -1090,6 +1119,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
39142                 bd_abort_claiming(bdev, claimed_bdev, loop_set_fd);
39143  out_putf:
39144         fput(file);
39145 +       if (virt_file)
39146 +               fput(virt_file);
39147  out:
39148         /* This is safe: open() is still holding a reference. */
39149         module_put(THIS_MODULE);
39150 @@ -1136,6 +1167,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
39151  static int __loop_clr_fd(struct loop_device *lo, bool release)
39152  {
39153         struct file *filp = NULL;
39154 +       struct file *virt_filp = lo->lo_backing_virt_file;
39155         gfp_t gfp = lo->old_gfp_mask;
39156         struct block_device *bdev = lo->lo_device;
39157         int err = 0;
39158 @@ -1159,6 +1191,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39159  
39160         spin_lock_irq(&lo->lo_lock);
39161         lo->lo_backing_file = NULL;
39162 +       lo->lo_backing_virt_file = NULL;
39163         spin_unlock_irq(&lo->lo_lock);
39164  
39165         loop_release_xfer(lo);
39166 @@ -1242,6 +1275,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39167          */
39168         if (filp)
39169                 fput(filp);
39170 +       if (virt_filp)
39171 +               fput(virt_filp);
39172         return err;
39173  }
39174  
39175 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39176 index af75a5ee40944..1d847cb194ff6 100644
39177 --- a/drivers/block/loop.h
39178 +++ b/drivers/block/loop.h
39179 @@ -46,7 +46,7 @@ struct loop_device {
39180         int             (*ioctl)(struct loop_device *, int cmd, 
39181                                  unsigned long arg); 
39182  
39183 -       struct file *   lo_backing_file;
39184 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39185         struct block_device *lo_device;
39186         void            *key_data; 
39187  
39188 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39189 index 6fb4a4ed8cc7f..ba9a959f2db27 100644
39190 --- a/fs/aufs/f_op.c
39191 +++ b/fs/aufs/f_op.c
39192 @@ -359,7 +359,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39193         if (IS_ERR(h_file))
39194                 goto out;
39195  
39196 -       if (au_test_loopback_kthread()) {
39197 +       if (0 && au_test_loopback_kthread()) {
39198                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39199                 if (file->f_mapping != h_file->f_mapping) {
39200                         file->f_mapping = h_file->f_mapping;
39201 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39202 index a8b63acc62045..9d97c3af5686a 100644
39203 --- a/fs/aufs/loop.c
39204 +++ b/fs/aufs/loop.c
39205 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39206                 symbol_put(loop_backing_file);
39207         au_kfree_try_rcu(au_warn_loopback_array);
39208  }
39209 +
39210 +/* ---------------------------------------------------------------------- */
39211 +
39212 +/* support the loopback block device insude aufs */
39213 +
39214 +struct file *aufs_real_loop(struct file *file)
39215 +{
39216 +       struct file *f;
39217 +
39218 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39219 +       fi_read_lock(file);
39220 +       f = au_hf_top(file);
39221 +       fi_read_unlock(file);
39222 +       AuDebugOn(!f);
39223 +       return f;
39224 +}
39225 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39226 index 94f4f80ae33bf..ca1194354aff4 100644
39227 --- a/fs/aufs/loop.h
39228 +++ b/fs/aufs/loop.h
39229 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39230  
39231  int au_loopback_init(void);
39232  void au_loopback_fin(void);
39233 +
39234 +struct file *aufs_real_loop(struct file *file);
39235  #else
39236  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39237  
39238 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39239  
39240  AuStubInt0(au_loopback_init, void)
39241  AuStubVoid(au_loopback_fin, void)
39242 +
39243 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39244  #endif /* BLK_DEV_LOOP */
39245  
39246  #endif /* __KERNEL__ */
39247 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39248 index 589dd01220201..801e0a7faec59 100644
39249 --- a/fs/aufs/super.c
39250 +++ b/fs/aufs/super.c
39251 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39252         .statfs         = aufs_statfs,
39253         .put_super      = aufs_put_super,
39254         .sync_fs        = aufs_sync_fs,
39255 -       .remount_fs     = aufs_remount_fs
39256 +       .remount_fs     = aufs_remount_fs,
39257 +#ifdef CONFIG_AUFS_BDEV_LOOP
39258 +       .real_loop      = aufs_real_loop
39259 +#endif
39260  };
39261  
39262  /* ---------------------------------------------------------------------- */
39263 diff --git a/include/linux/fs.h b/include/linux/fs.h
39264 index 916efd7e612b3..68f536f42b2a0 100644
39265 --- a/include/linux/fs.h
39266 +++ b/include/linux/fs.h
39267 @@ -1985,6 +1985,10 @@ struct super_operations {
39268                                   struct shrink_control *);
39269         long (*free_cached_objects)(struct super_block *,
39270                                     struct shrink_control *);
39271 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39272 +       /* and aufs */
39273 +       struct file *(*real_loop)(struct file *);
39274 +#endif
39275  };
39276  
39277  /*
This page took 3.946005 seconds and 3 git commands to generate.