]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- updated
[packages/kernel.git] / kernel-aufs4.patch
1 aufs4.x-rcN kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index 83eab52..31f16c4 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -248,6 +248,7 @@ source "fs/pstore/Kconfig"
8  source "fs/sysv/Kconfig"
9  source "fs/ufs/Kconfig"
10  source "fs/exofs/Kconfig"
11 +source "fs/aufs/Kconfig"
12  
13  endif # MISC_FILESYSTEMS
14  
15 diff --git a/fs/Makefile b/fs/Makefile
16 index 7bbaca9..a026491 100644
17 --- a/fs/Makefile
18 +++ b/fs/Makefile
19 @@ -128,3 +128,4 @@ obj-y                               += exofs/ # Multiple modules
20  obj-$(CONFIG_CEPH_FS)          += ceph/
21  obj-$(CONFIG_PSTORE)           += pstore/
22  obj-$(CONFIG_EFIVAR_FS)                += efivarfs/
23 +obj-$(CONFIG_AUFS_FS)           += aufs/
24 diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild
25 index f330ba4..67d88cd 100644
26 --- a/include/uapi/linux/Kbuild
27 +++ b/include/uapi/linux/Kbuild
28 @@ -59,6 +59,7 @@ header-y += atmsvc.h
29  header-y += atm_tcp.h
30  header-y += atm_zatm.h
31  header-y += audit.h
32 +header-y += aufs_type.h
33  header-y += auto_fs4.h
34  header-y += auto_fs.h
35  header-y += auxvec.h
36 aufs4.x-rcN base patch
37
38 diff --git a/MAINTAINERS b/MAINTAINERS
39 index 107c10e..dd8df20 100644
40 --- a/MAINTAINERS
41 +++ b/MAINTAINERS
42 @@ -2308,6 +2308,19 @@ F:       include/linux/audit.h
43  F:     include/uapi/linux/audit.h
44  F:     kernel/audit*
45  
46 +AUFS (advanced multi layered unification filesystem) FILESYSTEM
47 +M:     "J. R. Okajima" <hooanon05g@gmail.com>
48 +L:     linux-unionfs@vger.kernel.org
49 +L:     aufs-users@lists.sourceforge.net (members only)
50 +W:     http://aufs.sourceforge.net
51 +T:     git://github.com/sfjro/aufs4-linux.git
52 +S:     Supported
53 +F:     Documentation/filesystems/aufs/
54 +F:     Documentation/ABI/testing/debugfs-aufs
55 +F:     Documentation/ABI/testing/sysfs-aufs
56 +F:     fs/aufs/
57 +F:     include/uapi/linux/aufs_type.h
58 +
59  AUXILIARY DISPLAY DRIVERS
60  M:     Miguel Ojeda Sandonis <miguel.ojeda.sandonis@gmail.com>
61  W:     http://miguelojeda.es/auxdisplay.htm
62 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
63 index f347285..b63f68b 100644
64 --- a/drivers/block/loop.c
65 +++ b/drivers/block/loop.c
66 @@ -701,6 +701,24 @@ static inline int is_loop_device(struct file *file)
67         return i && S_ISBLK(i->i_mode) && MAJOR(i->i_rdev) == LOOP_MAJOR;
68  }
69  
70 +/*
71 + * for AUFS
72 + * no get/put for file.
73 + */
74 +struct file *loop_backing_file(struct super_block *sb)
75 +{
76 +       struct file *ret;
77 +       struct loop_device *l;
78 +
79 +       ret = NULL;
80 +       if (MAJOR(sb->s_dev) == LOOP_MAJOR) {
81 +               l = sb->s_bdev->bd_disk->private_data;
82 +               ret = l->lo_backing_file;
83 +       }
84 +       return ret;
85 +}
86 +EXPORT_SYMBOL_GPL(loop_backing_file);
87 +
88  /* loop sysfs attributes */
89  
90  static ssize_t loop_attr_show(struct device *dev, char *page,
91 diff --git a/fs/dcache.c b/fs/dcache.c
92 index 95d71ed..8ca5f09 100644
93 --- a/fs/dcache.c
94 +++ b/fs/dcache.c
95 @@ -1164,7 +1164,7 @@ enum d_walk_ret {
96   *
97   * The @enter() and @finish() callbacks are called with d_lock held.
98   */
99 -static void d_walk(struct dentry *parent, void *data,
100 +void d_walk(struct dentry *parent, void *data,
101                    enum d_walk_ret (*enter)(void *, struct dentry *),
102                    void (*finish)(void *))
103  {
104 diff --git a/fs/fcntl.c b/fs/fcntl.c
105 index e1c54f2..9f07008 100644
106 --- a/fs/fcntl.c
107 +++ b/fs/fcntl.c
108 @@ -29,7 +29,7 @@
109  
110  #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
111  
112 -static int setfl(int fd, struct file * filp, unsigned long arg)
113 +int setfl(int fd, struct file * filp, unsigned long arg)
114  {
115         struct inode * inode = file_inode(filp);
116         int error = 0;
117 @@ -60,6 +60,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
118  
119         if (filp->f_op->check_flags)
120                 error = filp->f_op->check_flags(arg);
121 +       if (!error && filp->f_op->setfl)
122 +               error = filp->f_op->setfl(filp, arg);
123         if (error)
124                 return error;
125  
126 diff --git a/fs/inode.c b/fs/inode.c
127 index 88110fd..9a9ba3a 100644
128 --- a/fs/inode.c
129 +++ b/fs/inode.c
130 @@ -1642,7 +1642,7 @@ EXPORT_SYMBOL(generic_update_time);
131   * This does the actual work of updating an inodes time or version.  Must have
132   * had called mnt_want_write() before calling this.
133   */
134 -static int update_time(struct inode *inode, struct timespec *time, int flags)
135 +int update_time(struct inode *inode, struct timespec *time, int flags)
136  {
137         int (*update_time)(struct inode *, struct timespec *, int);
138  
139 diff --git a/fs/read_write.c b/fs/read_write.c
140 index 5816d4c..670b365 100644
141 --- a/fs/read_write.c
142 +++ b/fs/read_write.c
143 @@ -515,6 +515,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
144  }
145  EXPORT_SYMBOL(__vfs_write);
146  
147 +vfs_readf_t vfs_readf(struct file *file)
148 +{
149 +       const struct file_operations *fop = file->f_op;
150 +
151 +       if (fop->read)
152 +               return fop->read;
153 +       if (fop->read_iter)
154 +               return new_sync_read;
155 +       return ERR_PTR(-ENOSYS);
156 +}
157 +
158 +vfs_writef_t vfs_writef(struct file *file)
159 +{
160 +       const struct file_operations *fop = file->f_op;
161 +
162 +       if (fop->write)
163 +               return fop->write;
164 +       if (fop->write_iter)
165 +               return new_sync_write;
166 +       return ERR_PTR(-ENOSYS);
167 +}
168 +
169  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
170  {
171         mm_segment_t old_fs;
172 diff --git a/fs/splice.c b/fs/splice.c
173 index 873d831..7899532 100644
174 --- a/fs/splice.c
175 +++ b/fs/splice.c
176 @@ -856,8 +856,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
177  /*
178   * Attempt to initiate a splice from pipe to file.
179   */
180 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
181 -                          loff_t *ppos, size_t len, unsigned int flags)
182 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
183 +                   loff_t *ppos, size_t len, unsigned int flags)
184  {
185         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
186                                 loff_t *, size_t, unsigned int);
187 @@ -873,9 +873,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
188  /*
189   * Attempt to initiate a splice from a file to a pipe.
190   */
191 -static long do_splice_to(struct file *in, loff_t *ppos,
192 -                        struct pipe_inode_info *pipe, size_t len,
193 -                        unsigned int flags)
194 +long do_splice_to(struct file *in, loff_t *ppos,
195 +                 struct pipe_inode_info *pipe, size_t len,
196 +                 unsigned int flags)
197  {
198         ssize_t (*splice_read)(struct file *, loff_t *,
199                                struct pipe_inode_info *, size_t, unsigned int);
200 diff --git a/fs/sync.c b/fs/sync.c
201 index 2a54c1f..7a5fa3f 100644
202 --- a/fs/sync.c
203 +++ b/fs/sync.c
204 @@ -27,7 +27,7 @@
205   * wait == 1 case since in that case write_inode() functions do
206   * sync_dirty_buffer() and thus effectively write one block at a time.
207   */
208 -static int __sync_filesystem(struct super_block *sb, int wait)
209 +int __sync_filesystem(struct super_block *sb, int wait)
210  {
211         if (wait)
212                 sync_inodes_sb(sb);
213 diff --git a/include/linux/file.h b/include/linux/file.h
214 index 61eb82c..e700888 100644
215 --- a/include/linux/file.h
216 +++ b/include/linux/file.h
217 @@ -19,6 +19,7 @@ struct dentry;
218  struct path;
219  extern struct file *alloc_file(const struct path *, fmode_t mode,
220         const struct file_operations *fop);
221 +extern struct file *get_empty_filp(void);
222  
223  static inline void fput_light(struct file *file, int fput_needed)
224  {
225 diff --git a/include/linux/fs.h b/include/linux/fs.h
226 index 2ba0743..d1c583b 100644
227 --- a/include/linux/fs.h
228 +++ b/include/linux/fs.h
229 @@ -1240,6 +1240,7 @@ extern void fasync_free(struct fasync_struct *);
230  /* can be called from interrupts */
231  extern void kill_fasync(struct fasync_struct **, int, int);
232  
233 +extern int setfl(int fd, struct file * filp, unsigned long arg);
234  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
235  extern void f_setown(struct file *filp, unsigned long arg, int force);
236  extern void f_delown(struct file *filp);
237 @@ -1664,6 +1665,7 @@ struct file_operations {
238         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
239         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
240         int (*check_flags)(int);
241 +       int (*setfl)(struct file *, unsigned long);
242         int (*flock) (struct file *, int, struct file_lock *);
243         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
244         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
245 @@ -1717,6 +1719,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
246                               struct iovec *fast_pointer,
247                               struct iovec **ret_pointer);
248  
249 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
250 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
251 +                               loff_t *);
252 +vfs_readf_t vfs_readf(struct file *file);
253 +vfs_writef_t vfs_writef(struct file *file);
254 +
255  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
256  extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
257  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
258 @@ -2108,6 +2116,7 @@ extern int current_umask(void);
259  extern void ihold(struct inode * inode);
260  extern void iput(struct inode *);
261  extern int generic_update_time(struct inode *, struct timespec *, int);
262 +extern int update_time(struct inode *, struct timespec *, int);
263  
264  /* /sys/fs */
265  extern struct kobject *fs_kobj;
266 @@ -2387,6 +2396,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
267         return false;
268  }
269  #endif
270 +extern int __sync_filesystem(struct super_block *, int);
271  extern int sync_filesystem(struct super_block *);
272  extern const struct file_operations def_blk_fops;
273  extern const struct file_operations def_chr_fops;
274 diff --git a/include/linux/splice.h b/include/linux/splice.h
275 index 00a2116..1f0a4a2 100644
276 --- a/include/linux/splice.h
277 +++ b/include/linux/splice.h
278 @@ -86,4 +86,10 @@ extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
279  
280  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
281  extern const struct pipe_buf_operations default_pipe_buf_ops;
282 +
283 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
284 +                          loff_t *ppos, size_t len, unsigned int flags);
285 +extern long do_splice_to(struct file *in, loff_t *ppos,
286 +                        struct pipe_inode_info *pipe, size_t len,
287 +                        unsigned int flags);
288  #endif
289 aufs4.x-rcN mmap patch
290
291 diff --git a/fs/proc/base.c b/fs/proc/base.c
292 index 87c9a9a..a0196f0 100644
293 --- a/fs/proc/base.c
294 +++ b/fs/proc/base.c
295 @@ -1958,7 +1958,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
296         down_read(&mm->mmap_sem);
297         vma = find_exact_vma(mm, vm_start, vm_end);
298         if (vma && vma->vm_file) {
299 -               *path = vma->vm_file->f_path;
300 +               *path = vma_pr_or_file(vma)->f_path;
301                 path_get(path);
302                 rc = 0;
303         }
304 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
305 index 7563437..7c0dc0f 100644
306 --- a/fs/proc/nommu.c
307 +++ b/fs/proc/nommu.c
308 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
309         file = region->vm_file;
310  
311         if (file) {
312 -               struct inode *inode = file_inode(region->vm_file);
313 +               struct inode *inode;
314 +
315 +               file = vmr_pr_or_file(region);
316 +               inode = file_inode(file);
317                 dev = inode->i_sb->s_dev;
318                 ino = inode->i_ino;
319         }
320 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
321 index 8f96a49..0d10404 100644
322 --- a/fs/proc/task_mmu.c
323 +++ b/fs/proc/task_mmu.c
324 @@ -291,7 +291,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
325         const char *name = NULL;
326  
327         if (file) {
328 -               struct inode *inode = file_inode(vma->vm_file);
329 +               struct inode *inode;
330 +
331 +               file = vma_pr_or_file(vma);
332 +               inode = file_inode(file);
333                 dev = inode->i_sb->s_dev;
334                 ino = inode->i_ino;
335                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
336 @@ -1628,7 +1631,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
337         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
338         struct vm_area_struct *vma = v;
339         struct numa_maps *md = &numa_priv->md;
340 -       struct file *file = vma->vm_file;
341 +       struct file *file = vma_pr_or_file(vma);
342         struct mm_struct *mm = vma->vm_mm;
343         struct mm_walk walk = {
344                 .hugetlb_entry = gather_hugetlb_stats,
345 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
346 index 3717562..6a328f1 100644
347 --- a/fs/proc/task_nommu.c
348 +++ b/fs/proc/task_nommu.c
349 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
350         file = vma->vm_file;
351  
352         if (file) {
353 -               struct inode *inode = file_inode(vma->vm_file);
354 +               struct inode *inode;
355 +
356 +               file = vma_pr_or_file(vma);
357 +               inode = file_inode(file);
358                 dev = inode->i_sb->s_dev;
359                 ino = inode->i_ino;
360                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
361 diff --git a/include/linux/mm.h b/include/linux/mm.h
362 index b84615b..3978a35 100644
363 --- a/include/linux/mm.h
364 +++ b/include/linux/mm.h
365 @@ -1257,6 +1257,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
366  }
367  #endif
368  
369 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
370 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
371 +                                     int);
372 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
373 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
374 +
375 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
376 +                                                               __LINE__)
377 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
378 +                                                         __LINE__)
379 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
380 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
381 +
382 +#ifndef CONFIG_MMU
383 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
384 +extern void vmr_do_fput(struct vm_region *, const char[], int);
385 +
386 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
387 +                                                         __LINE__)
388 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
389 +#endif /* !CONFIG_MMU */
390 +
391  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len,
392                 unsigned int gup_flags);
393  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
394 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
395 index 808751d..66fc765 100644
396 --- a/include/linux/mm_types.h
397 +++ b/include/linux/mm_types.h
398 @@ -275,6 +275,7 @@ struct vm_region {
399         unsigned long   vm_top;         /* region allocated to here */
400         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
401         struct file     *vm_file;       /* the backing file or NULL */
402 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
403  
404         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
405         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
406 @@ -349,6 +350,7 @@ struct vm_area_struct {
407         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
408                                            units */
409         struct file * vm_file;          /* File we map to (can be NULL). */
410 +       struct file *vm_prfile;         /* shadow of vm_file */
411         void * vm_private_data;         /* was vm_pte (shared mem) */
412  
413  #ifndef CONFIG_MMU
414 diff --git a/kernel/fork.c b/kernel/fork.c
415 index 11c5c8a..48e7ef7 100644
416 --- a/kernel/fork.c
417 +++ b/kernel/fork.c
418 @@ -626,7 +626,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
419                         struct inode *inode = file_inode(file);
420                         struct address_space *mapping = file->f_mapping;
421  
422 -                       get_file(file);
423 +                       vma_get_file(tmp);
424                         if (tmp->vm_flags & VM_DENYWRITE)
425                                 atomic_dec(&inode->i_writecount);
426                         i_mmap_lock_write(mapping);
427 diff --git a/mm/Makefile b/mm/Makefile
428 index 295bd7a..14fa1c8 100644
429 --- a/mm/Makefile
430 +++ b/mm/Makefile
431 @@ -37,7 +37,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
432                            mm_init.o mmu_context.o percpu.o slab_common.o \
433                            compaction.o vmacache.o \
434                            interval_tree.o list_lru.o workingset.o \
435 -                          debug.o $(mmu-y)
436 +                          prfile.o debug.o $(mmu-y)
437  
438  obj-y += init-mm.o
439  
440 diff --git a/mm/filemap.c b/mm/filemap.c
441 index 3f9afde..bb050b0 100644
442 --- a/mm/filemap.c
443 +++ b/mm/filemap.c
444 @@ -2397,7 +2397,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
445         int ret = VM_FAULT_LOCKED;
446  
447         sb_start_pagefault(inode->i_sb);
448 -       file_update_time(vma->vm_file);
449 +       vma_file_update_time(vma);
450         lock_page(page);
451         if (page->mapping != inode->i_mapping) {
452                 unlock_page(page);
453 diff --git a/mm/mmap.c b/mm/mmap.c
454 index dc4291d..4b3a2aa 100644
455 --- a/mm/mmap.c
456 +++ b/mm/mmap.c
457 @@ -170,7 +170,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
458         if (vma->vm_ops && vma->vm_ops->close)
459                 vma->vm_ops->close(vma);
460         if (vma->vm_file)
461 -               fput(vma->vm_file);
462 +               vma_fput(vma);
463         mpol_put(vma_policy(vma));
464         kmem_cache_free(vm_area_cachep, vma);
465         return next;
466 @@ -879,7 +879,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
467         if (remove_next) {
468                 if (file) {
469                         uprobe_munmap(next, next->vm_start, next->vm_end);
470 -                       fput(file);
471 +                       vma_fput(vma);
472                 }
473                 if (next->anon_vma)
474                         anon_vma_merge(vma, next);
475 @@ -1727,8 +1727,8 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
476         return addr;
477  
478  unmap_and_free_vma:
479 +       vma_fput(vma);
480         vma->vm_file = NULL;
481 -       fput(file);
482  
483         /* Undo any partial mapping done by a device driver. */
484         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
485 @@ -2533,7 +2533,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
486                 goto out_free_mpol;
487  
488         if (new->vm_file)
489 -               get_file(new->vm_file);
490 +               vma_get_file(new);
491  
492         if (new->vm_ops && new->vm_ops->open)
493                 new->vm_ops->open(new);
494 @@ -2552,7 +2552,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
495         if (new->vm_ops && new->vm_ops->close)
496                 new->vm_ops->close(new);
497         if (new->vm_file)
498 -               fput(new->vm_file);
499 +               vma_fput(new);
500         unlink_anon_vmas(new);
501   out_free_mpol:
502         mpol_put(vma_policy(new));
503 @@ -2703,7 +2703,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
504         struct vm_area_struct *vma;
505         unsigned long populate = 0;
506         unsigned long ret = -EINVAL;
507 -       struct file *file;
508 +       struct file *file, *prfile;
509  
510         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
511                      current->comm, current->pid);
512 @@ -2778,10 +2778,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
513                 }
514         }
515  
516 -       file = get_file(vma->vm_file);
517 +       vma_get_file(vma);
518 +       file = vma->vm_file;
519 +       prfile = vma->vm_prfile;
520         ret = do_mmap_pgoff(vma->vm_file, start, size,
521                         prot, flags, pgoff, &populate);
522 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
523 +               struct vm_area_struct *new_vma;
524 +
525 +               new_vma = find_vma(mm, ret);
526 +               if (!new_vma->vm_prfile)
527 +                       new_vma->vm_prfile = prfile;
528 +               if (new_vma != vma)
529 +                       get_file(prfile);
530 +       }
531 +       /*
532 +        * two fput()s instead of vma_fput(vma),
533 +        * coz vma may not be available anymore.
534 +        */
535         fput(file);
536 +       if (prfile)
537 +               fput(prfile);
538  out:
539         up_write(&mm->mmap_sem);
540         if (populate)
541 @@ -3056,7 +3073,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
542                 if (anon_vma_clone(new_vma, vma))
543                         goto out_free_mempol;
544                 if (new_vma->vm_file)
545 -                       get_file(new_vma->vm_file);
546 +                       vma_get_file(new_vma);
547                 if (new_vma->vm_ops && new_vma->vm_ops->open)
548                         new_vma->vm_ops->open(new_vma);
549                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
550 diff --git a/mm/nommu.c b/mm/nommu.c
551 index 24f9f5f..ac0d37a 100644
552 --- a/mm/nommu.c
553 +++ b/mm/nommu.c
554 @@ -636,7 +636,7 @@ static void __put_nommu_region(struct vm_region *region)
555                 up_write(&nommu_region_sem);
556  
557                 if (region->vm_file)
558 -                       fput(region->vm_file);
559 +                       vmr_fput(region);
560  
561                 /* IO memory and memory shared directly out of the pagecache
562                  * from ramfs/tmpfs mustn't be released here */
563 @@ -794,7 +794,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
564         if (vma->vm_ops && vma->vm_ops->close)
565                 vma->vm_ops->close(vma);
566         if (vma->vm_file)
567 -               fput(vma->vm_file);
568 +               vma_fput(vma);
569         put_nommu_region(vma->vm_region);
570         kmem_cache_free(vm_area_cachep, vma);
571  }
572 @@ -1320,7 +1320,7 @@ unsigned long do_mmap(struct file *file,
573                                         goto error_just_free;
574                                 }
575                         }
576 -                       fput(region->vm_file);
577 +                       vmr_fput(region);
578                         kmem_cache_free(vm_region_jar, region);
579                         region = pregion;
580                         result = start;
581 @@ -1395,10 +1395,10 @@ unsigned long do_mmap(struct file *file,
582         up_write(&nommu_region_sem);
583  error:
584         if (region->vm_file)
585 -               fput(region->vm_file);
586 +               vmr_fput(region);
587         kmem_cache_free(vm_region_jar, region);
588         if (vma->vm_file)
589 -               fput(vma->vm_file);
590 +               vma_fput(vma);
591         kmem_cache_free(vm_area_cachep, vma);
592         return ret;
593  
594 diff --git a/mm/prfile.c b/mm/prfile.c
595 new file mode 100644
596 index 0000000..b323b8a
597 --- /dev/null
598 +++ b/mm/prfile.c
599 @@ -0,0 +1,86 @@
600 +/*
601 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
602 + * in /proc/PID/maps.
603 + * Call these functions via macros defined in linux/mm.h.
604 + *
605 + * See Documentation/filesystems/aufs/design/06mmap.txt
606 + *
607 + * Copyright (c) 2014 Junjro R. Okajima
608 + * Copyright (c) 2014 Ian Campbell
609 + */
610 +
611 +#include <linux/mm.h>
612 +#include <linux/file.h>
613 +#include <linux/fs.h>
614 +
615 +/* #define PRFILE_TRACE */
616 +static inline void prfile_trace(struct file *f, struct file *pr,
617 +                             const char func[], int line, const char func2[])
618 +{
619 +#ifdef PRFILE_TRACE
620 +       if (pr)
621 +               pr_info("%s:%d: %s, %s\n", func, line, func2,
622 +                       f ? (char *)f->f_path.dentry->d_name.name : "(null)");
623 +#endif
624 +}
625 +
626 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
627 +                            int line)
628 +{
629 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
630 +
631 +       prfile_trace(f, pr, func, line, __func__);
632 +       file_update_time(f);
633 +       if (f && pr)
634 +               file_update_time(pr);
635 +}
636 +
637 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
638 +                              int line)
639 +{
640 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
641 +
642 +       prfile_trace(f, pr, func, line, __func__);
643 +       return (f && pr) ? pr : f;
644 +}
645 +
646 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
647 +{
648 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
649 +
650 +       prfile_trace(f, pr, func, line, __func__);
651 +       get_file(f);
652 +       if (f && pr)
653 +               get_file(pr);
654 +}
655 +
656 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
657 +{
658 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
659 +
660 +       prfile_trace(f, pr, func, line, __func__);
661 +       fput(f);
662 +       if (f && pr)
663 +               fput(pr);
664 +}
665 +
666 +#ifndef CONFIG_MMU
667 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
668 +                              int line)
669 +{
670 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
671 +
672 +       prfile_trace(f, pr, func, line, __func__);
673 +       return (f && pr) ? pr : f;
674 +}
675 +
676 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
677 +{
678 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
679 +
680 +       prfile_trace(f, pr, func, line, __func__);
681 +       fput(f);
682 +       if (f && pr)
683 +               fput(pr);
684 +}
685 +#endif /* !CONFIG_MMU */
686 aufs4.x-rcN standalone patch
687
688 diff --git a/fs/dcache.c b/fs/dcache.c
689 index 8ca5f09..b1ff5be 100644
690 --- a/fs/dcache.c
691 +++ b/fs/dcache.c
692 @@ -1272,6 +1272,7 @@ void d_walk(struct dentry *parent, void *data,
693         seq = 1;
694         goto again;
695  }
696 +EXPORT_SYMBOL_GPL(d_walk);
697  
698  struct check_mount {
699         struct vfsmount *mnt;
700 @@ -2864,6 +2865,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
701  
702         write_sequnlock(&rename_lock);
703  }
704 +EXPORT_SYMBOL_GPL(d_exchange);
705  
706  /**
707   * d_ancestor - search for an ancestor
708 diff --git a/fs/exec.c b/fs/exec.c
709 index e579466..2566b16 100644
710 --- a/fs/exec.c
711 +++ b/fs/exec.c
712 @@ -104,6 +104,7 @@ bool path_noexec(const struct path *path)
713         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
714                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
715  }
716 +EXPORT_SYMBOL_GPL(path_noexec);
717  
718  #ifdef CONFIG_USELIB
719  /*
720 diff --git a/fs/fcntl.c b/fs/fcntl.c
721 index 9f07008..d60b682 100644
722 --- a/fs/fcntl.c
723 +++ b/fs/fcntl.c
724 @@ -82,6 +82,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
725   out:
726         return error;
727  }
728 +EXPORT_SYMBOL_GPL(setfl);
729  
730  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
731                       int force)
732 diff --git a/fs/file_table.c b/fs/file_table.c
733 index 6d982b5..9a3c6c8 100644
734 --- a/fs/file_table.c
735 +++ b/fs/file_table.c
736 @@ -147,6 +147,7 @@ struct file *get_empty_filp(void)
737         }
738         return ERR_PTR(-ENFILE);
739  }
740 +EXPORT_SYMBOL_GPL(get_empty_filp);
741  
742  /**
743   * alloc_file - allocate and initialize a 'struct file'
744 @@ -258,6 +259,7 @@ void flush_delayed_fput(void)
745  {
746         delayed_fput(NULL);
747  }
748 +EXPORT_SYMBOL_GPL(flush_delayed_fput);
749  
750  static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
751  
752 @@ -300,6 +302,7 @@ void __fput_sync(struct file *file)
753  }
754  
755  EXPORT_SYMBOL(fput);
756 +EXPORT_SYMBOL_GPL(__fput_sync);
757  
758  void put_filp(struct file *file)
759  {
760 @@ -308,6 +311,7 @@ void put_filp(struct file *file)
761                 file_free(file);
762         }
763  }
764 +EXPORT_SYMBOL_GPL(put_filp);
765  
766  void __init files_init(void)
767  { 
768 diff --git a/fs/inode.c b/fs/inode.c
769 index 9a9ba3a..a3a18d8 100644
770 --- a/fs/inode.c
771 +++ b/fs/inode.c
772 @@ -1651,6 +1651,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
773  
774         return update_time(inode, time, flags);
775  }
776 +EXPORT_SYMBOL_GPL(update_time);
777  
778  /**
779   *     touch_atime     -       update the access time
780 diff --git a/fs/namespace.c b/fs/namespace.c
781 index 487ba30..642069d 100644
782 --- a/fs/namespace.c
783 +++ b/fs/namespace.c
784 @@ -462,6 +462,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
785         mnt_dec_writers(real_mount(mnt));
786         preempt_enable();
787  }
788 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
789  
790  /**
791   * mnt_drop_write - give up write access to a mount
792 @@ -1872,6 +1873,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
793         }
794         return 0;
795  }
796 +EXPORT_SYMBOL_GPL(iterate_mounts);
797  
798  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
799  {
800 diff --git a/fs/notify/group.c b/fs/notify/group.c
801 index fbe3cbe..bdfc61e 100644
802 --- a/fs/notify/group.c
803 +++ b/fs/notify/group.c
804 @@ -22,6 +22,7 @@
805  #include <linux/srcu.h>
806  #include <linux/rculist.h>
807  #include <linux/wait.h>
808 +#include <linux/module.h>
809  
810  #include <linux/fsnotify_backend.h>
811  #include "fsnotify.h"
812 @@ -100,6 +101,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
813  {
814         atomic_inc(&group->refcnt);
815  }
816 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
817  
818  /*
819   * Drop a reference to a group.  Free it if it's through.
820 @@ -109,6 +111,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
821         if (atomic_dec_and_test(&group->refcnt))
822                 fsnotify_final_destroy_group(group);
823  }
824 +EXPORT_SYMBOL_GPL(fsnotify_put_group);
825  
826  /*
827   * Create a new fsnotify_group and hold a reference for the group returned.
828 @@ -137,6 +140,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
829  
830         return group;
831  }
832 +EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
833  
834  int fsnotify_fasync(int fd, struct file *file, int on)
835  {
836 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
837 index 6043306..fdb50e4 100644
838 --- a/fs/notify/mark.c
839 +++ b/fs/notify/mark.c
840 @@ -113,6 +113,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
841                 mark->free_mark(mark);
842         }
843  }
844 +EXPORT_SYMBOL_GPL(fsnotify_put_mark);
845  
846  /* Calculate mask of events for a list of marks */
847  u32 fsnotify_recalc_mask(struct hlist_head *head)
848 @@ -230,6 +231,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
849         mutex_unlock(&group->mark_mutex);
850         fsnotify_free_mark(mark);
851  }
852 +EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
853  
854  void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
855  {
856 @@ -415,6 +417,7 @@ int fsnotify_add_mark_locked(struct fsnotify_mark *mark,
857  
858         return ret;
859  }
860 +EXPORT_SYMBOL_GPL(fsnotify_add_mark);
861  
862  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
863                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
864 @@ -521,6 +524,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
865         atomic_set(&mark->refcnt, 1);
866         mark->free_mark = free_mark;
867  }
868 +EXPORT_SYMBOL_GPL(fsnotify_init_mark);
869  
870  /*
871   * Destroy all marks in destroy_list, waits for SRCU period to finish before
872 diff --git a/fs/open.c b/fs/open.c
873 index 9921f70..80d94c0 100644
874 --- a/fs/open.c
875 +++ b/fs/open.c
876 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
877         inode_unlock(dentry->d_inode);
878         return ret;
879  }
880 +EXPORT_SYMBOL_GPL(do_truncate);
881  
882  long vfs_truncate(const struct path *path, loff_t length)
883  {
884 @@ -695,6 +696,7 @@ int open_check_o_direct(struct file *f)
885         }
886         return 0;
887  }
888 +EXPORT_SYMBOL_GPL(open_check_o_direct);
889  
890  static int do_dentry_open(struct file *f,
891                           struct inode *inode,
892 diff --git a/fs/read_write.c b/fs/read_write.c
893 index 670b365..f9f8bb1 100644
894 --- a/fs/read_write.c
895 +++ b/fs/read_write.c
896 @@ -525,6 +525,7 @@ vfs_readf_t vfs_readf(struct file *file)
897                 return new_sync_read;
898         return ERR_PTR(-ENOSYS);
899  }
900 +EXPORT_SYMBOL_GPL(vfs_readf);
901  
902  vfs_writef_t vfs_writef(struct file *file)
903  {
904 @@ -536,6 +537,7 @@ vfs_writef_t vfs_writef(struct file *file)
905                 return new_sync_write;
906         return ERR_PTR(-ENOSYS);
907  }
908 +EXPORT_SYMBOL_GPL(vfs_writef);
909  
910  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
911  {
912 diff --git a/fs/splice.c b/fs/splice.c
913 index 7899532..c0df111 100644
914 --- a/fs/splice.c
915 +++ b/fs/splice.c
916 @@ -869,6 +869,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
917  
918         return splice_write(pipe, out, ppos, len, flags);
919  }
920 +EXPORT_SYMBOL_GPL(do_splice_from);
921  
922  /*
923   * Attempt to initiate a splice from a file to a pipe.
924 @@ -898,6 +899,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
925  
926         return splice_read(in, ppos, pipe, len, flags);
927  }
928 +EXPORT_SYMBOL_GPL(do_splice_to);
929  
930  /**
931   * splice_direct_to_actor - splices data directly between two non-pipes
932 diff --git a/fs/sync.c b/fs/sync.c
933 index 7a5fa3f..c9b9d46 100644
934 --- a/fs/sync.c
935 +++ b/fs/sync.c
936 @@ -38,6 +38,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
937                 sb->s_op->sync_fs(sb, wait);
938         return __sync_blockdev(sb->s_bdev, wait);
939  }
940 +EXPORT_SYMBOL_GPL(__sync_filesystem);
941  
942  /*
943   * Write out and wait upon all dirty data associated with this
944 diff --git a/fs/xattr.c b/fs/xattr.c
945 index 7e3317c..88910fe 100644
946 --- a/fs/xattr.c
947 +++ b/fs/xattr.c
948 @@ -296,6 +296,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
949         *xattr_value = value;
950         return error;
951  }
952 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
953  
954  ssize_t
955  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
956 diff --git a/kernel/task_work.c b/kernel/task_work.c
957 index d513051..e056d54 100644
958 --- a/kernel/task_work.c
959 +++ b/kernel/task_work.c
960 @@ -119,3 +119,4 @@ void task_work_run(void)
961                 } while (work);
962         }
963  }
964 +EXPORT_SYMBOL_GPL(task_work_run);
965 diff --git a/security/commoncap.c b/security/commoncap.c
966 index 8df676f..6b5cc07 100644
967 --- a/security/commoncap.c
968 +++ b/security/commoncap.c
969 @@ -1061,12 +1061,14 @@ int cap_mmap_addr(unsigned long addr)
970         }
971         return ret;
972  }
973 +EXPORT_SYMBOL_GPL(cap_mmap_addr);
974  
975  int cap_mmap_file(struct file *file, unsigned long reqprot,
976                   unsigned long prot, unsigned long flags)
977  {
978         return 0;
979  }
980 +EXPORT_SYMBOL_GPL(cap_mmap_file);
981  
982  #ifdef CONFIG_SECURITY
983  
984 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
985 index 03c1652..f88c84b 100644
986 --- a/security/device_cgroup.c
987 +++ b/security/device_cgroup.c
988 @@ -7,6 +7,7 @@
989  #include <linux/device_cgroup.h>
990  #include <linux/cgroup.h>
991  #include <linux/ctype.h>
992 +#include <linux/export.h>
993  #include <linux/list.h>
994  #include <linux/uaccess.h>
995  #include <linux/seq_file.h>
996 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
997         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
998                         access);
999  }
1000 +EXPORT_SYMBOL_GPL(__devcgroup_inode_permission);
1001  
1002  int devcgroup_inode_mknod(int mode, dev_t dev)
1003  {
1004 diff --git a/security/security.c b/security/security.c
1005 index f825304..8dd441d 100644
1006 --- a/security/security.c
1007 +++ b/security/security.c
1008 @@ -443,6 +443,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
1009                 return 0;
1010         return call_int_hook(path_rmdir, 0, dir, dentry);
1011  }
1012 +EXPORT_SYMBOL_GPL(security_path_rmdir);
1013  
1014  int security_path_unlink(const struct path *dir, struct dentry *dentry)
1015  {
1016 @@ -459,6 +460,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
1017                 return 0;
1018         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
1019  }
1020 +EXPORT_SYMBOL_GPL(security_path_symlink);
1021  
1022  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1023                        struct dentry *new_dentry)
1024 @@ -467,6 +469,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
1025                 return 0;
1026         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
1027  }
1028 +EXPORT_SYMBOL_GPL(security_path_link);
1029  
1030  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1031                          const struct path *new_dir, struct dentry *new_dentry,
1032 @@ -494,6 +497,7 @@ int security_path_truncate(const struct path *path)
1033                 return 0;
1034         return call_int_hook(path_truncate, 0, path);
1035  }
1036 +EXPORT_SYMBOL_GPL(security_path_truncate);
1037  
1038  int security_path_chmod(const struct path *path, umode_t mode)
1039  {
1040 @@ -501,6 +505,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
1041                 return 0;
1042         return call_int_hook(path_chmod, 0, path, mode);
1043  }
1044 +EXPORT_SYMBOL_GPL(security_path_chmod);
1045  
1046  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1047  {
1048 @@ -508,6 +513,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
1049                 return 0;
1050         return call_int_hook(path_chown, 0, path, uid, gid);
1051  }
1052 +EXPORT_SYMBOL_GPL(security_path_chown);
1053  
1054  int security_path_chroot(const struct path *path)
1055  {
1056 @@ -593,6 +599,7 @@ int security_inode_readlink(struct dentry *dentry)
1057                 return 0;
1058         return call_int_hook(inode_readlink, 0, dentry);
1059  }
1060 +EXPORT_SYMBOL_GPL(security_inode_readlink);
1061  
1062  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1063                                bool rcu)
1064 @@ -608,6 +615,7 @@ int security_inode_permission(struct inode *inode, int mask)
1065                 return 0;
1066         return call_int_hook(inode_permission, 0, inode, mask);
1067  }
1068 +EXPORT_SYMBOL_GPL(security_inode_permission);
1069  
1070  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
1071  {
1072 @@ -779,6 +787,7 @@ int security_file_permission(struct file *file, int mask)
1073  
1074         return fsnotify_perm(file, mask);
1075  }
1076 +EXPORT_SYMBOL_GPL(security_file_permission);
1077  
1078  int security_file_alloc(struct file *file)
1079  {
1080 @@ -838,6 +847,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
1081                 return ret;
1082         return ima_file_mmap(file, prot);
1083  }
1084 +EXPORT_SYMBOL_GPL(security_mmap_file);
1085  
1086  int security_mmap_addr(unsigned long addr)
1087  {
1088 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1089 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
1090 +++ linux/Documentation/ABI/testing/debugfs-aufs        2016-10-09 16:55:36.476034536 +0200
1091 @@ -0,0 +1,50 @@
1092 +What:          /debug/aufs/si_<id>/
1093 +Date:          March 2009
1094 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1095 +Description:
1096 +               Under /debug/aufs, a directory named si_<id> is created
1097 +               per aufs mount, where <id> is a unique id generated
1098 +               internally.
1099 +
1100 +What:          /debug/aufs/si_<id>/plink
1101 +Date:          Apr 2013
1102 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1103 +Description:
1104 +               It has three lines and shows the information about the
1105 +               pseudo-link. The first line is a single number
1106 +               representing a number of buckets. The second line is a
1107 +               number of pseudo-links per buckets (separated by a
1108 +               blank). The last line is a single number representing a
1109 +               total number of psedo-links.
1110 +               When the aufs mount option 'noplink' is specified, it
1111 +               will show "1\n0\n0\n".
1112 +
1113 +What:          /debug/aufs/si_<id>/xib
1114 +Date:          March 2009
1115 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1116 +Description:
1117 +               It shows the consumed blocks by xib (External Inode Number
1118 +               Bitmap), its block size and file size.
1119 +               When the aufs mount option 'noxino' is specified, it
1120 +               will be empty. About XINO files, see the aufs manual.
1121 +
1122 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1123 +Date:          March 2009
1124 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1125 +Description:
1126 +               It shows the consumed blocks by xino (External Inode Number
1127 +               Translation Table), its link count, block size and file
1128 +               size.
1129 +               When the aufs mount option 'noxino' is specified, it
1130 +               will be empty. About XINO files, see the aufs manual.
1131 +
1132 +What:          /debug/aufs/si_<id>/xigen
1133 +Date:          March 2009
1134 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1135 +Description:
1136 +               It shows the consumed blocks by xigen (External Inode
1137 +               Generation Table), its block size and file size.
1138 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1139 +               be created.
1140 +               When the aufs mount option 'noxino' is specified, it
1141 +               will be empty. About XINO files, see the aufs manual.
1142 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1143 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1144 +++ linux/Documentation/ABI/testing/sysfs-aufs  2016-10-09 16:55:36.476034536 +0200
1145 @@ -0,0 +1,31 @@
1146 +What:          /sys/fs/aufs/si_<id>/
1147 +Date:          March 2009
1148 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1149 +Description:
1150 +               Under /sys/fs/aufs, a directory named si_<id> is created
1151 +               per aufs mount, where <id> is a unique id generated
1152 +               internally.
1153 +
1154 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1155 +Date:          March 2009
1156 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1157 +Description:
1158 +               It shows the abolute path of a member directory (which
1159 +               is called branch) in aufs, and its permission.
1160 +
1161 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1162 +Date:          July 2013
1163 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1164 +Description:
1165 +               It shows the id of a member directory (which is called
1166 +               branch) in aufs.
1167 +
1168 +What:          /sys/fs/aufs/si_<id>/xi_path
1169 +Date:          March 2009
1170 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1171 +Description:
1172 +               It shows the abolute path of XINO (External Inode Number
1173 +               Bitmap, Translation Table and Generation Table) file
1174 +               even if it is the default path.
1175 +               When the aufs mount option 'noxino' is specified, it
1176 +               will be empty. About XINO files, see the aufs manual.
1177 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1178 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1179 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2017-03-02 14:24:13.850255360 +0100
1180 @@ -0,0 +1,170 @@
1181 +
1182 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1183 +# 
1184 +# This program is free software; you can redistribute it and/or modify
1185 +# it under the terms of the GNU General Public License as published by
1186 +# the Free Software Foundation; either version 2 of the License, or
1187 +# (at your option) any later version.
1188 +# 
1189 +# This program is distributed in the hope that it will be useful,
1190 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1191 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1192 +# GNU General Public License for more details.
1193 +# 
1194 +# You should have received a copy of the GNU General Public License
1195 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1196 +
1197 +Introduction
1198 +----------------------------------------
1199 +
1200 +aufs [ei ju: ef es] | [a u f s]
1201 +1. abbrev. for "advanced multi-layered unification filesystem".
1202 +2. abbrev. for "another unionfs".
1203 +3. abbrev. for "auf das" in German which means "on the" in English.
1204 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1205 +   But "Filesystem aufs Filesystem" is hard to understand.
1206 +
1207 +AUFS is a filesystem with features:
1208 +- multi layered stackable unification filesystem, the member directory
1209 +  is called as a branch.
1210 +- branch permission and attribute, 'readonly', 'real-readonly',
1211 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1212 +  combination.
1213 +- internal "file copy-on-write".
1214 +- logical deletion, whiteout.
1215 +- dynamic branch manipulation, adding, deleting and changing permission.
1216 +- allow bypassing aufs, user's direct branch access.
1217 +- external inode number translation table and bitmap which maintains the
1218 +  persistent aufs inode number.
1219 +- seekable directory, including NFS readdir.
1220 +- file mapping, mmap and sharing pages.
1221 +- pseudo-link, hardlink over branches.
1222 +- loopback mounted filesystem as a branch.
1223 +- several policies to select one among multiple writable branches.
1224 +- revert a single systemcall when an error occurs in aufs.
1225 +- and more...
1226 +
1227 +
1228 +Multi Layered Stackable Unification Filesystem
1229 +----------------------------------------------------------------------
1230 +Most people already knows what it is.
1231 +It is a filesystem which unifies several directories and provides a
1232 +merged single directory. When users access a file, the access will be
1233 +passed/re-directed/converted (sorry, I am not sure which English word is
1234 +correct) to the real file on the member filesystem. The member
1235 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1236 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1237 +readonly branch is handled by creating 'whiteout' on the upper writable
1238 +branch.
1239 +
1240 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1241 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1242 +different approaches to implement the merged-view.
1243 +The former tries putting it into VFS, and the latter implements as a
1244 +separate filesystem.
1245 +(If I misunderstand about these implementations, please let me know and
1246 +I shall correct it. Because it is a long time ago when I read their
1247 +source files last time).
1248 +
1249 +UnionMount's approach will be able to small, but may be hard to share
1250 +branches between several UnionMount since the whiteout in it is
1251 +implemented in the inode on branch filesystem and always
1252 +shared. According to Bharata's post, readdir does not seems to be
1253 +finished yet.
1254 +There are several missing features known in this implementations such as
1255 +- for users, the inode number may change silently. eg. copy-up.
1256 +- link(2) may break by copy-up.
1257 +- read(2) may get an obsoleted filedata (fstat(2) too).
1258 +- fcntl(F_SETLK) may be broken by copy-up.
1259 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1260 +  open(O_RDWR).
1261 +
1262 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1263 +merged into mainline. This is another implementation of UnionMount as a
1264 +separated filesystem. All the limitations and known problems which
1265 +UnionMount are equally inherited to "overlay" filesystem.
1266 +
1267 +Unionfs has a longer history. When I started implementing a stackable
1268 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1269 +inode, dentry and file objects and they have an array pointing lower
1270 +same kind objects. After contributing many patches for Unionfs, I
1271 +re-started my project AUFS (Jun 2006).
1272 +
1273 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1274 +implemented my own ideas, approaches and enhancements and it became
1275 +totally different one.
1276 +
1277 +Comparing DM snapshot and fs based implementation
1278 +- the number of bytes to be copied between devices is much smaller.
1279 +- the type of filesystem must be one and only.
1280 +- the fs must be writable, no readonly fs, even for the lower original
1281 +  device. so the compression fs will not be usable. but if we use
1282 +  loopback mount, we may address this issue.
1283 +  for instance,
1284 +       mount /cdrom/squashfs.img /sq
1285 +       losetup /sq/ext2.img
1286 +       losetup /somewhere/cow
1287 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1288 +- it will be difficult (or needs more operations) to extract the
1289 +  difference between the original device and COW.
1290 +- DM snapshot-merge may help a lot when users try merging. in the
1291 +  fs-layer union, users will use rsync(1).
1292 +
1293 +You may want to read my old paper "Filesystems in LiveCD"
1294 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1295 +
1296 +
1297 +Several characters/aspects/persona of aufs
1298 +----------------------------------------------------------------------
1299 +
1300 +Aufs has several characters, aspects or persona.
1301 +1. a filesystem, callee of VFS helper
1302 +2. sub-VFS, caller of VFS helper for branches
1303 +3. a virtual filesystem which maintains persistent inode number
1304 +4. reader/writer of files on branches such like an application
1305 +
1306 +1. Callee of VFS Helper
1307 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1308 +unlink(2) from an application reaches sys_unlink() kernel function and
1309 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1310 +calls filesystem specific unlink operation. Actually aufs implements the
1311 +unlink operation but it behaves like a redirector.
1312 +
1313 +2. Caller of VFS Helper for Branches
1314 +aufs_unlink() passes the unlink request to the branch filesystem as if
1315 +it were called from VFS. So the called unlink operation of the branch
1316 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1317 +every necessary pre/post operation for the branch filesystem.
1318 +- acquire the lock for the parent dir on a branch
1319 +- lookup in a branch
1320 +- revalidate dentry on a branch
1321 +- mnt_want_write() for a branch
1322 +- vfs_unlink() for a branch
1323 +- mnt_drop_write() for a branch
1324 +- release the lock on a branch
1325 +
1326 +3. Persistent Inode Number
1327 +One of the most important issue for a filesystem is to maintain inode
1328 +numbers. This is particularly important to support exporting a
1329 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1330 +backend block device for its own. But some storage is necessary to
1331 +keep and maintain the inode numbers. It may be a large space and may not
1332 +suit to keep in memory. Aufs rents some space from its first writable
1333 +branch filesystem (by default) and creates file(s) on it. These files
1334 +are created by aufs internally and removed soon (currently) keeping
1335 +opened.
1336 +Note: Because these files are removed, they are totally gone after
1337 +      unmounting aufs. It means the inode numbers are not persistent
1338 +      across unmount or reboot. I have a plan to make them really
1339 +      persistent which will be important for aufs on NFS server.
1340 +
1341 +4. Read/Write Files Internally (copy-on-write)
1342 +Because a branch can be readonly, when you write a file on it, aufs will
1343 +"copy-up" it to the upper writable branch internally. And then write the
1344 +originally requested thing to the file. Generally kernel doesn't
1345 +open/read/write file actively. In aufs, even a single write may cause a
1346 +internal "file copy". This behaviour is very similar to cp(1) command.
1347 +
1348 +Some people may think it is better to pass such work to user space
1349 +helper, instead of doing in kernel space. Actually I am still thinking
1350 +about it. But currently I have implemented it in kernel space.
1351 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1352 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1353 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2017-03-02 14:24:13.850255360 +0100
1354 @@ -0,0 +1,258 @@
1355 +
1356 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1357 +# 
1358 +# This program is free software; you can redistribute it and/or modify
1359 +# it under the terms of the GNU General Public License as published by
1360 +# the Free Software Foundation; either version 2 of the License, or
1361 +# (at your option) any later version.
1362 +# 
1363 +# This program is distributed in the hope that it will be useful,
1364 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1365 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1366 +# GNU General Public License for more details.
1367 +# 
1368 +# You should have received a copy of the GNU General Public License
1369 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1370 +
1371 +Basic Aufs Internal Structure
1372 +
1373 +Superblock/Inode/Dentry/File Objects
1374 +----------------------------------------------------------------------
1375 +As like an ordinary filesystem, aufs has its own
1376 +superblock/inode/dentry/file objects. All these objects have a
1377 +dynamically allocated array and store the same kind of pointers to the
1378 +lower filesystem, branch.
1379 +For example, when you build a union with one readwrite branch and one
1380 +readonly, mounted /au, /rw and /ro respectively.
1381 +- /au = /rw + /ro
1382 +- /ro/fileA exists but /rw/fileA
1383 +
1384 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1385 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1386 +- [0] = NULL (because /rw/fileA doesn't exist)
1387 +- [1] = /ro/fileA
1388 +
1389 +This style of an array is essentially same to the aufs
1390 +superblock/inode/dentry/file objects.
1391 +
1392 +Because aufs supports manipulating branches, ie. add/delete/change
1393 +branches dynamically, these objects has its own generation. When
1394 +branches are changed, the generation in aufs superblock is
1395 +incremented. And a generation in other object are compared when it is
1396 +accessed. When a generation in other objects are obsoleted, aufs
1397 +refreshes the internal array.
1398 +
1399 +
1400 +Superblock
1401 +----------------------------------------------------------------------
1402 +Additionally aufs superblock has some data for policies to select one
1403 +among multiple writable branches, XIB files, pseudo-links and kobject.
1404 +See below in detail.
1405 +About the policies which supports copy-down a directory, see
1406 +wbr_policy.txt too.
1407 +
1408 +
1409 +Branch and XINO(External Inode Number Translation Table)
1410 +----------------------------------------------------------------------
1411 +Every branch has its own xino (external inode number translation table)
1412 +file. The xino file is created and unlinked by aufs internally. When two
1413 +members of a union exist on the same filesystem, they share the single
1414 +xino file.
1415 +The struct of a xino file is simple, just a sequence of aufs inode
1416 +numbers which is indexed by the lower inode number.
1417 +In the above sample, assume the inode number of /ro/fileA is i111 and
1418 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1419 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1420 +
1421 +When the inode numbers are not contiguous, the xino file will be sparse
1422 +which has a hole in it and doesn't consume as much disk space as it
1423 +might appear. If your branch filesystem consumes disk space for such
1424 +holes, then you should specify 'xino=' option at mounting aufs.
1425 +
1426 +Aufs has a mount option to free the disk blocks for such holes in XINO
1427 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1428 +meet a problem of disk shortage due to XINO files, then you should try
1429 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1430 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1431 +the holes in XINO files.
1432 +
1433 +Also a writable branch has three kinds of "whiteout bases". All these
1434 +are existed when the branch is joined to aufs, and their names are
1435 +whiteout-ed doubly, so that users will never see their names in aufs
1436 +hierarchy.
1437 +1. a regular file which will be hardlinked to all whiteouts.
1438 +2. a directory to store a pseudo-link.
1439 +3. a directory to store an "orphan"-ed file temporary.
1440 +
1441 +1. Whiteout Base
1442 +   When you remove a file on a readonly branch, aufs handles it as a
1443 +   logical deletion and creates a whiteout on the upper writable branch
1444 +   as a hardlink of this file in order not to consume inode on the
1445 +   writable branch.
1446 +2. Pseudo-link Dir
1447 +   See below, Pseudo-link.
1448 +3. Step-Parent Dir
1449 +   When "fileC" exists on the lower readonly branch only and it is
1450 +   opened and removed with its parent dir, and then user writes
1451 +   something into it, then aufs copies-up fileC to this
1452 +   directory. Because there is no other dir to store fileC. After
1453 +   creating a file under this dir, the file is unlinked.
1454 +
1455 +Because aufs supports manipulating branches, ie. add/delete/change
1456 +dynamically, a branch has its own id. When the branch order changes,
1457 +aufs finds the new index by searching the branch id.
1458 +
1459 +
1460 +Pseudo-link
1461 +----------------------------------------------------------------------
1462 +Assume "fileA" exists on the lower readonly branch only and it is
1463 +hardlinked to "fileB" on the branch. When you write something to fileA,
1464 +aufs copies-up it to the upper writable branch. Additionally aufs
1465 +creates a hardlink under the Pseudo-link Directory of the writable
1466 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1467 +simple list. If fileB is read after unlinking fileA, aufs returns
1468 +filedata from the pseudo-link instead of the lower readonly
1469 +branch. Because the pseudo-link is based upon the inode, to keep the
1470 +inode number by xino (see above) is essentially necessary.
1471 +
1472 +All the hardlinks under the Pseudo-link Directory of the writable branch
1473 +should be restored in a proper location later. Aufs provides a utility
1474 +to do this. The userspace helpers executed at remounting and unmounting
1475 +aufs by default.
1476 +During this utility is running, it puts aufs into the pseudo-link
1477 +maintenance mode. In this mode, only the process which began the
1478 +maintenance mode (and its child processes) is allowed to operate in
1479 +aufs. Some other processes which are not related to the pseudo-link will
1480 +be allowed to run too, but the rest have to return an error or wait
1481 +until the maintenance mode ends. If a process already acquires an inode
1482 +mutex (in VFS), it has to return an error.
1483 +
1484 +
1485 +XIB(external inode number bitmap)
1486 +----------------------------------------------------------------------
1487 +Addition to the xino file per a branch, aufs has an external inode number
1488 +bitmap in a superblock object. It is also an internal file such like a
1489 +xino file.
1490 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1491 +not.
1492 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1493 +
1494 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1495 +reduce the number of consumed disk blocks for these files.
1496 +
1497 +
1498 +Virtual or Vertical Dir, and Readdir in Userspace
1499 +----------------------------------------------------------------------
1500 +In order to support multiple layers (branches), aufs readdir operation
1501 +constructs a virtual dir block on memory. For readdir, aufs calls
1502 +vfs_readdir() internally for each dir on branches, merges their entries
1503 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1504 +object. So the file object has its entry list until it is closed. The
1505 +entry list will be updated when the file position is zero and becomes
1506 +obsoleted. This decision is made in aufs automatically.
1507 +
1508 +The dynamically allocated memory block for the name of entries has a
1509 +unit of 512 bytes (by default) and stores the names contiguously (no
1510 +padding). Another block for each entry is handled by kmem_cache too.
1511 +During building dir blocks, aufs creates hash list and judging whether
1512 +the entry is whiteouted by its upper branch or already listed.
1513 +The merged result is cached in the corresponding inode object and
1514 +maintained by a customizable life-time option.
1515 +
1516 +Some people may call it can be a security hole or invite DoS attack
1517 +since the opened and once readdir-ed dir (file object) holds its entry
1518 +list and becomes a pressure for system memory. But I'd say it is similar
1519 +to files under /proc or /sys. The virtual files in them also holds a
1520 +memory page (generally) while they are opened. When an idea to reduce
1521 +memory for them is introduced, it will be applied to aufs too.
1522 +For those who really hate this situation, I've developed readdir(3)
1523 +library which operates this merging in userspace. You just need to set
1524 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1525 +kernel space for readdir(3).
1526 +
1527 +
1528 +Workqueue
1529 +----------------------------------------------------------------------
1530 +Aufs sometimes requires privilege access to a branch. For instance,
1531 +in copy-up/down operation. When a user process is going to make changes
1532 +to a file which exists in the lower readonly branch only, and the mode
1533 +of one of ancestor directories may not be writable by a user
1534 +process. Here aufs copy-up the file with its ancestors and they may
1535 +require privilege to set its owner/group/mode/etc.
1536 +This is a typical case of a application character of aufs (see
1537 +Introduction).
1538 +
1539 +Aufs uses workqueue synchronously for this case. It creates its own
1540 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1541 +passes the request to call mkdir or write (for example), and wait for
1542 +its completion. This approach solves a problem of a signal handler
1543 +simply.
1544 +If aufs didn't adopt the workqueue and changed the privilege of the
1545 +process, then the process may receive the unexpected SIGXFSZ or other
1546 +signals.
1547 +
1548 +Also aufs uses the system global workqueue ("events" kernel thread) too
1549 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1550 +whiteout base and etc. This is unrelated to a privilege.
1551 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1552 +superblock at the beginning, at the same time waits for the completion
1553 +of all queued asynchronous tasks.
1554 +
1555 +
1556 +Whiteout
1557 +----------------------------------------------------------------------
1558 +The whiteout in aufs is very similar to Unionfs's. That is represented
1559 +by its filename. UnionMount takes an approach of a file mode, but I am
1560 +afraid several utilities (find(1) or something) will have to support it.
1561 +
1562 +Basically the whiteout represents "logical deletion" which stops aufs to
1563 +lookup further, but also it represents "dir is opaque" which also stop
1564 +further lookup.
1565 +
1566 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1567 +In order to make several functions in a single systemcall to be
1568 +revertible, aufs adopts an approach to rename a directory to a temporary
1569 +unique whiteouted name.
1570 +For example, in rename(2) dir where the target dir already existed, aufs
1571 +renames the target dir to a temporary unique whiteouted name before the
1572 +actual rename on a branch, and then handles other actions (make it opaque,
1573 +update the attributes, etc). If an error happens in these actions, aufs
1574 +simply renames the whiteouted name back and returns an error. If all are
1575 +succeeded, aufs registers a function to remove the whiteouted unique
1576 +temporary name completely and asynchronously to the system global
1577 +workqueue.
1578 +
1579 +
1580 +Copy-up
1581 +----------------------------------------------------------------------
1582 +It is a well-known feature or concept.
1583 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1584 +internally and makes change to the new file on the upper writable branch.
1585 +When the trigger systemcall does not update the timestamps of the parent
1586 +dir, aufs reverts it after copy-up.
1587 +
1588 +
1589 +Move-down (aufs3.9 and later)
1590 +----------------------------------------------------------------------
1591 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1592 +the lower readonly branch to the upper writable branch when a user
1593 +changes something about the file.
1594 +"Move-down" is an opposite action of copy-up. Basically this action is
1595 +ran manually instead of automatically and internally.
1596 +For desgin and implementation, aufs has to consider these issues.
1597 +- whiteout for the file may exist on the lower branch.
1598 +- ancestor directories may not exist on the lower branch.
1599 +- diropq for the ancestor directories may exist on the upper branch.
1600 +- free space on the lower branch will reduce.
1601 +- another access to the file may happen during moving-down, including
1602 +  UDBA (see "Revalidate Dentry and UDBA").
1603 +- the file should not be hard-linked nor pseudo-linked. they should be
1604 +  handled by auplink utility later.
1605 +
1606 +Sometimes users want to move-down a file from the upper writable branch
1607 +to the lower readonly or writable branch. For instance,
1608 +- the free space of the upper writable branch is going to run out.
1609 +- create a new intermediate branch between the upper and lower branch.
1610 +- etc.
1611 +
1612 +For this purpose, use "aumvdown" command in aufs-util.git.
1613 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1614 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1615 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2017-03-02 14:24:13.850255360 +0100
1616 @@ -0,0 +1,85 @@
1617 +
1618 +# Copyright (C) 2015-2017 Junjiro R. Okajima
1619 +# 
1620 +# This program is free software; you can redistribute it and/or modify
1621 +# it under the terms of the GNU General Public License as published by
1622 +# the Free Software Foundation; either version 2 of the License, or
1623 +# (at your option) any later version.
1624 +# 
1625 +# This program is distributed in the hope that it will be useful,
1626 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1627 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1628 +# GNU General Public License for more details.
1629 +# 
1630 +# You should have received a copy of the GNU General Public License
1631 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1632 +
1633 +Support for a branch who has its ->atomic_open()
1634 +----------------------------------------------------------------------
1635 +The filesystems who implement its ->atomic_open() are not majority. For
1636 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1637 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1638 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1639 +sure whether all filesystems who have ->atomic_open() behave like this,
1640 +but NFSv4 surely returns the error.
1641 +
1642 +In order to support ->atomic_open() for aufs, there are a few
1643 +approaches.
1644 +
1645 +A. Introduce aufs_atomic_open()
1646 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1647 +     branch fs.
1648 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1649 +   an aufs user Pip Cet's approach
1650 +   - calls aufs_create(), VFS finish_open() and notify_change().
1651 +   - pass fake-mode to finish_open(), and then correct the mode by
1652 +     notify_change().
1653 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1654 +   - no aufs_atomic_open().
1655 +   - aufs_lookup() registers the TID to an aufs internal object.
1656 +   - aufs_create() does nothing when the matching TID is registered, but
1657 +     registers the mode.
1658 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1659 +     TID is registered.
1660 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1661 +   credential
1662 +   - no aufs_atomic_open().
1663 +   - aufs_create() registers the TID to an internal object. this info
1664 +     represents "this process created this file just now."
1665 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1666 +     registered TID and re-try open() with superuser's credential.
1667 +
1668 +Pros and cons for each approach.
1669 +
1670 +A.
1671 +   - straightforward but highly depends upon VFS internal.
1672 +   - the atomic behavaiour is kept.
1673 +   - some of parameters such as nameidata are hard to reproduce for
1674 +     branch fs.
1675 +   - large overhead.
1676 +B.
1677 +   - easy to implement.
1678 +   - the atomic behavaiour is lost.
1679 +C.
1680 +   - the atomic behavaiour is kept.
1681 +   - dirty and tricky.
1682 +   - VFS checks whether the file is created correctly after calling
1683 +     ->create(), which means this approach doesn't work.
1684 +D.
1685 +   - easy to implement.
1686 +   - the atomic behavaiour is lost.
1687 +   - to open a file with superuser's credential and give it to a user
1688 +     process is a bad idea, since the file object keeps the credential
1689 +     in it. It may affect LSM or something. This approach doesn't work
1690 +     either.
1691 +
1692 +The approach A is ideal, but it hard to implement. So here is a
1693 +variation of A, which is to be implemented.
1694 +
1695 +A-1. Introduce aufs_atomic_open()
1696 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1697 +       vfs_create() and finish_open().
1698 +     - the demerit is that the several checks after branch fs
1699 +       ->atomic_open() are lost. in the ordinary case, the checks are
1700 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1701 +       be implemented in aufs, but not all I am afraid.
1702 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1703 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1704 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2017-03-02 14:24:13.850255360 +0100
1705 @@ -0,0 +1,113 @@
1706 +
1707 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1708 +# 
1709 +# This program is free software; you can redistribute it and/or modify
1710 +# it under the terms of the GNU General Public License as published by
1711 +# the Free Software Foundation; either version 2 of the License, or
1712 +# (at your option) any later version.
1713 +# 
1714 +# This program is distributed in the hope that it will be useful,
1715 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1716 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1717 +# GNU General Public License for more details.
1718 +# 
1719 +# You should have received a copy of the GNU General Public License
1720 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1721 +
1722 +Lookup in a Branch
1723 +----------------------------------------------------------------------
1724 +Since aufs has a character of sub-VFS (see Introduction), it operates
1725 +lookup for branches as VFS does. It may be a heavy work. But almost all
1726 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1727 +directly connected to its parent. Digging down the directory hierarchy
1728 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1729 +aufs calls it.
1730 +
1731 +When a branch is a remote filesystem, aufs basically relies upon its
1732 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1733 +them.
1734 +For d_revalidate, aufs implements three levels of revalidate tests. See
1735 +"Revalidate Dentry and UDBA" in detail.
1736 +
1737 +
1738 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1739 +----------------------------------------------------------------------
1740 +Let's try case study.
1741 +- aufs has two branches, upper readwrite and lower readonly.
1742 +  /au = /rw + /ro
1743 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1744 +- user invoked "chmod a+rx /au/dirA"
1745 +- the internal copy-up is activated and "/rw/dirA" is created and its
1746 +  permission bits are set to world readable.
1747 +- then "/au/dirA" becomes world readable?
1748 +
1749 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1750 +or it may be a natively readonly filesystem. If aufs respects the lower
1751 +branch, it should not respond readdir request from other users. But user
1752 +allowed it by chmod. Should really aufs rejects showing the entries
1753 +under /ro/dirA?
1754 +
1755 +To be honest, I don't have a good solution for this case. So aufs
1756 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1757 +users.
1758 +When dirperm1 is specified, aufs checks only the highest one for the
1759 +directory permission, and shows the entries. Otherwise, as usual, checks
1760 +every dir existing on all branches and rejects the request.
1761 +
1762 +As a side effect, dirperm1 option improves the performance of aufs
1763 +because the number of permission check is reduced when the number of
1764 +branch is many.
1765 +
1766 +
1767 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1768 +----------------------------------------------------------------------
1769 +Generally VFS helpers re-validate a dentry as a part of lookup.
1770 +0. digging down the directory hierarchy.
1771 +1. lock the parent dir by its i_mutex.
1772 +2. lookup the final (child) entry.
1773 +3. revalidate it.
1774 +4. call the actual operation (create, unlink, etc.)
1775 +5. unlock the parent dir
1776 +
1777 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1778 +called. Actually aufs implements it and checks the dentry on a branch is
1779 +still valid.
1780 +But it is not enough. Because aufs has to release the lock for the
1781 +parent dir on a branch at the end of ->lookup() (step 2) and
1782 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1783 +held by VFS.
1784 +If the file on a branch is changed directly, eg. bypassing aufs, after
1785 +aufs released the lock, then the subsequent operation may cause
1786 +something unpleasant result.
1787 +
1788 +This situation is a result of VFS architecture, ->lookup() and
1789 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1790 +design from VFS's point of view. It is just not suitable for sub-VFS
1791 +character in aufs.
1792 +
1793 +Aufs supports such case by three level of revalidation which is
1794 +selectable by user.
1795 +1. Simple Revalidate
1796 +   Addition to the native flow in VFS's, confirm the child-parent
1797 +   relationship on the branch just after locking the parent dir on the
1798 +   branch in the "actual operation" (step 4). When this validation
1799 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1800 +   checks the validation of the dentry on branches.
1801 +2. Monitor Changes Internally by Inotify/Fsnotify
1802 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1803 +   the dentry on the branch, and returns EBUSY if it finds different
1804 +   dentry.
1805 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1806 +   during it is in cache. When the event is notified, aufs registers a
1807 +   function to kernel 'events' thread by schedule_work(). And the
1808 +   function sets some special status to the cached aufs dentry and inode
1809 +   private data. If they are not cached, then aufs has nothing to
1810 +   do. When the same file is accessed through aufs (step 0-3) later,
1811 +   aufs will detect the status and refresh all necessary data.
1812 +   In this mode, aufs has to ignore the event which is fired by aufs
1813 +   itself.
1814 +3. No Extra Validation
1815 +   This is the simplest test and doesn't add any additional revalidation
1816 +   test, and skip the revalidation in step 4. It is useful and improves
1817 +   aufs performance when system surely hide the aufs branches from user,
1818 +   by over-mounting something (or another method).
1819 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1820 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1821 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2017-03-02 14:24:13.850255360 +0100
1822 @@ -0,0 +1,74 @@
1823 +
1824 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1825 +# 
1826 +# This program is free software; you can redistribute it and/or modify
1827 +# it under the terms of the GNU General Public License as published by
1828 +# the Free Software Foundation; either version 2 of the License, or
1829 +# (at your option) any later version.
1830 +# 
1831 +# This program is distributed in the hope that it will be useful,
1832 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1833 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1834 +# GNU General Public License for more details.
1835 +# 
1836 +# You should have received a copy of the GNU General Public License
1837 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1838 +
1839 +Branch Manipulation
1840 +
1841 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1842 +and changing its permission/attribute, there are a lot of works to do.
1843 +
1844 +
1845 +Add a Branch
1846 +----------------------------------------------------------------------
1847 +o Confirm the adding dir exists outside of aufs, including loopback
1848 +  mount, and its various attributes.
1849 +o Initialize the xino file and whiteout bases if necessary.
1850 +  See struct.txt.
1851 +
1852 +o Check the owner/group/mode of the directory
1853 +  When the owner/group/mode of the adding directory differs from the
1854 +  existing branch, aufs issues a warning because it may impose a
1855 +  security risk.
1856 +  For example, when a upper writable branch has a world writable empty
1857 +  top directory, a malicious user can create any files on the writable
1858 +  branch directly, like copy-up and modify manually. If something like
1859 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1860 +  writable branch, and the writable branch is world-writable, then a
1861 +  malicious guy may create /etc/passwd on the writable branch directly
1862 +  and the infected file will be valid in aufs.
1863 +  I am afraid it can be a security issue, but aufs can do nothing except
1864 +  producing a warning.
1865 +
1866 +
1867 +Delete a Branch
1868 +----------------------------------------------------------------------
1869 +o Confirm the deleting branch is not busy
1870 +  To be general, there is one merit to adopt "remount" interface to
1871 +  manipulate branches. It is to discard caches. At deleting a branch,
1872 +  aufs checks the still cached (and connected) dentries and inodes. If
1873 +  there are any, then they are all in-use. An inode without its
1874 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1875 +
1876 +  For the cached one, aufs checks whether the same named entry exists on
1877 +  other branches.
1878 +  If the cached one is a directory, because aufs provides a merged view
1879 +  to users, as long as one dir is left on any branch aufs can show the
1880 +  dir to users. In this case, the branch can be removed from aufs.
1881 +  Otherwise aufs rejects deleting the branch.
1882 +
1883 +  If any file on the deleting branch is opened by aufs, then aufs
1884 +  rejects deleting.
1885 +
1886 +
1887 +Modify the Permission of a Branch
1888 +----------------------------------------------------------------------
1889 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1890 +  See struct.txt.
1891 +
1892 +o rw --> ro: Confirm the modifying branch is not busy
1893 +  Aufs rejects the request if any of these conditions are true.
1894 +  - a file on the branch is mmap-ed.
1895 +  - a regular file on the branch is opened for write and there is no
1896 +    same named entry on the upper branch.
1897 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1898 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1899 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2017-03-02 14:24:13.850255360 +0100
1900 @@ -0,0 +1,64 @@
1901 +
1902 +# Copyright (C) 2005-2017 Junjiro R. Okajima
1903 +# 
1904 +# This program is free software; you can redistribute it and/or modify
1905 +# it under the terms of the GNU General Public License as published by
1906 +# the Free Software Foundation; either version 2 of the License, or
1907 +# (at your option) any later version.
1908 +# 
1909 +# This program is distributed in the hope that it will be useful,
1910 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1911 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1912 +# GNU General Public License for more details.
1913 +# 
1914 +# You should have received a copy of the GNU General Public License
1915 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1916 +
1917 +Policies to Select One among Multiple Writable Branches
1918 +----------------------------------------------------------------------
1919 +When the number of writable branch is more than one, aufs has to decide
1920 +the target branch for file creation or copy-up. By default, the highest
1921 +writable branch which has the parent (or ancestor) dir of the target
1922 +file is chosen (top-down-parent policy).
1923 +By user's request, aufs implements some other policies to select the
1924 +writable branch, for file creation several policies, round-robin,
1925 +most-free-space, and other policies. For copy-up, top-down-parent,
1926 +bottom-up-parent, bottom-up and others.
1927 +
1928 +As expected, the round-robin policy selects the branch in circular. When
1929 +you have two writable branches and creates 10 new files, 5 files will be
1930 +created for each branch. mkdir(2) systemcall is an exception. When you
1931 +create 10 new directories, all will be created on the same branch.
1932 +And the most-free-space policy selects the one which has most free
1933 +space among the writable branches. The amount of free space will be
1934 +checked by aufs internally, and users can specify its time interval.
1935 +
1936 +The policies for copy-up is more simple,
1937 +top-down-parent is equivalent to the same named on in create policy,
1938 +bottom-up-parent selects the writable branch where the parent dir
1939 +exists and the nearest upper one from the copyup-source,
1940 +bottom-up selects the nearest upper writable branch from the
1941 +copyup-source, regardless the existence of the parent dir.
1942 +
1943 +There are some rules or exceptions to apply these policies.
1944 +- If there is a readonly branch above the policy-selected branch and
1945 +  the parent dir is marked as opaque (a variation of whiteout), or the
1946 +  target (creating) file is whiteout-ed on the upper readonly branch,
1947 +  then the result of the policy is ignored and the target file will be
1948 +  created on the nearest upper writable branch than the readonly branch.
1949 +- If there is a writable branch above the policy-selected branch and
1950 +  the parent dir is marked as opaque or the target file is whiteouted
1951 +  on the branch, then the result of the policy is ignored and the target
1952 +  file will be created on the highest one among the upper writable
1953 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1954 +  it as usual.
1955 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1956 +  They try selecting the branch where the source exists as possible
1957 +  since copyup a large file will take long time. If it can't be,
1958 +  ie. the branch where the source exists is readonly, then they will
1959 +  follow the copyup policy.
1960 +- There is an exception for rename(2) when the target exists.
1961 +  If the rename target exists, aufs compares the index of the branches
1962 +  where the source and the target exists and selects the higher
1963 +  one. If the selected branch is readonly, then aufs follows the
1964 +  copyup policy.
1965 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1966 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1967 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2017-03-02 14:24:13.850255360 +0100
1968 @@ -0,0 +1,120 @@
1969 +
1970 +# Copyright (C) 2011-2017 Junjiro R. Okajima
1971 +# 
1972 +# This program is free software; you can redistribute it and/or modify
1973 +# it under the terms of the GNU General Public License as published by
1974 +# the Free Software Foundation; either version 2 of the License, or
1975 +# (at your option) any later version.
1976 +# 
1977 +# This program is distributed in the hope that it will be useful,
1978 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1979 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1980 +# GNU General Public License for more details.
1981 +# 
1982 +# You should have received a copy of the GNU General Public License
1983 +# along with this program; if not, write to the Free Software
1984 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1985 +
1986 +
1987 +File-based Hierarchical Storage Management (FHSM)
1988 +----------------------------------------------------------------------
1989 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1990 +storage world. Aufs provides this feature as file-based with multiple
1991 +writable branches, based upon the principle of "Colder, the Lower".
1992 +Here the word "colder" means that the less used files, and "lower" means
1993 +that the position in the order of the stacked branches vertically.
1994 +These multiple writable branches are prioritized, ie. the topmost one
1995 +should be the fastest drive and be used heavily.
1996 +
1997 +o Characters in aufs FHSM story
1998 +- aufs itself and a new branch attribute.
1999 +- a new ioctl interface to move-down and to establish a connection with
2000 +  the daemon ("move-down" is a converse of "copy-up").
2001 +- userspace tool and daemon.
2002 +
2003 +The userspace daemon establishes a connection with aufs and waits for
2004 +the notification. The notified information is very similar to struct
2005 +statfs containing the number of consumed blocks and inodes.
2006 +When the consumed blocks/inodes of a branch exceeds the user-specified
2007 +upper watermark, the daemon activates its move-down process until the
2008 +consumed blocks/inodes reaches the user-specified lower watermark.
2009 +
2010 +The actual move-down is done by aufs based upon the request from
2011 +user-space since we need to maintain the inode number and the internal
2012 +pointer arrays in aufs.
2013 +
2014 +Currently aufs FHSM handles the regular files only. Additionally they
2015 +must not be hard-linked nor pseudo-linked.
2016 +
2017 +
2018 +o Cowork of aufs and the user-space daemon
2019 +  During the userspace daemon established the connection, aufs sends a
2020 +  small notification to it whenever aufs writes something into the
2021 +  writable branch. But it may cost high since aufs issues statfs(2)
2022 +  internally. So user can specify a new option to cache the
2023 +  info. Actually the notification is controlled by these factors.
2024 +  + the specified cache time.
2025 +  + classified as "force" by aufs internally.
2026 +  Until the specified time expires, aufs doesn't send the info
2027 +  except the forced cases. When aufs decide forcing, the info is always
2028 +  notified to userspace.
2029 +  For example, the number of free inodes is generally large enough and
2030 +  the shortage of it happens rarely. So aufs doesn't force the
2031 +  notification when creating a new file, directory and others. This is
2032 +  the typical case which aufs doesn't force.
2033 +  When aufs writes the actual filedata and the files consumes any of new
2034 +  blocks, the aufs forces notifying.
2035 +
2036 +
2037 +o Interfaces in aufs
2038 +- New branch attribute.
2039 +  + fhsm
2040 +    Specifies that the branch is managed by FHSM feature. In other word,
2041 +    participant in the FHSM.
2042 +    When nofhsm is set to the branch, it will not be the source/target
2043 +    branch of the move-down operation. This attribute is set
2044 +    independently from coo and moo attributes, and if you want full
2045 +    FHSM, you should specify them as well.
2046 +- New mount option.
2047 +  + fhsm_sec
2048 +    Specifies a second to suppress many less important info to be
2049 +    notified.
2050 +- New ioctl.
2051 +  + AUFS_CTL_FHSM_FD
2052 +    create a new file descriptor which userspace can read the notification
2053 +    (a subset of struct statfs) from aufs.
2054 +- Module parameter 'brs'
2055 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2056 +  be set.
2057 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2058 +  When there are two or more branches with fhsm attributes,
2059 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2060 +  terminates it. As a result of remounting and branch-manipulation, the
2061 +  number of branches with fhsm attribute can be one. In this case,
2062 +  /sbin/mount.aufs will terminate the user-space daemon.
2063 +
2064 +
2065 +Finally the operation is done as these steps in kernel-space.
2066 +- make sure that,
2067 +  + no one else is using the file.
2068 +  + the file is not hard-linked.
2069 +  + the file is not pseudo-linked.
2070 +  + the file is a regular file.
2071 +  + the parent dir is not opaqued.
2072 +- find the target writable branch.
2073 +- make sure the file is not whiteout-ed by the upper (than the target)
2074 +  branch.
2075 +- make the parent dir on the target branch.
2076 +- mutex lock the inode on the branch.
2077 +- unlink the whiteout on the target branch (if exists).
2078 +- lookup and create the whiteout-ed temporary name on the target branch.
2079 +- copy the file as the whiteout-ed temporary name on the target branch.
2080 +- rename the whiteout-ed temporary name to the original name.
2081 +- unlink the file on the source branch.
2082 +- maintain the internal pointer array and the external inode number
2083 +  table (XINO).
2084 +- maintain the timestamps and other attributes of the parent dir and the
2085 +  file.
2086 +
2087 +And of course, in every step, an error may happen. So the operation
2088 +should restore the original file state after an error happens.
2089 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2090 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2091 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2017-03-02 14:24:13.853588785 +0100
2092 @@ -0,0 +1,72 @@
2093 +
2094 +# Copyright (C) 2005-2017 Junjiro R. Okajima
2095 +# 
2096 +# This program is free software; you can redistribute it and/or modify
2097 +# it under the terms of the GNU General Public License as published by
2098 +# the Free Software Foundation; either version 2 of the License, or
2099 +# (at your option) any later version.
2100 +# 
2101 +# This program is distributed in the hope that it will be useful,
2102 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2103 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2104 +# GNU General Public License for more details.
2105 +# 
2106 +# You should have received a copy of the GNU General Public License
2107 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2108 +
2109 +mmap(2) -- File Memory Mapping
2110 +----------------------------------------------------------------------
2111 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2112 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2113 +->mmap().
2114 +This approach is simple and good, but there is one problem.
2115 +Under /proc, several entries show the mmapped files by its path (with
2116 +device and inode number), and the printed path will be the path on the
2117 +branch fs's instead of virtual aufs's.
2118 +This is not a problem in most cases, but some utilities lsof(1) (and its
2119 +user) may expect the path on aufs.
2120 +
2121 +To address this issue, aufs adds a new member called vm_prfile in struct
2122 +vm_area_struct (and struct vm_region). The original vm_file points to
2123 +the file on the branch fs in order to handle everything correctly as
2124 +usual. The new vm_prfile points to a virtual file in aufs, and the
2125 +show-functions in procfs refers to vm_prfile if it is set.
2126 +Also we need to maintain several other places where touching vm_file
2127 +such like
2128 +- fork()/clone() copies vma and the reference count of vm_file is
2129 +  incremented.
2130 +- merging vma maintains the ref count too.
2131 +
2132 +This is not a good approach. It just fakes the printed path. But it
2133 +leaves all behaviour around f_mapping unchanged. This is surely an
2134 +advantage.
2135 +Actually aufs had adopted another complicated approach which calls
2136 +generic_file_mmap() and handles struct vm_operations_struct. In this
2137 +approach, aufs met a hard problem and I could not solve it without
2138 +switching the approach.
2139 +
2140 +There may be one more another approach which is
2141 +- bind-mount the branch-root onto the aufs-root internally
2142 +- grab the new vfsmount (ie. struct mount)
2143 +- lazy-umount the branch-root internally
2144 +- in open(2) the aufs-file, open the branch-file with the hidden
2145 +  vfsmount (instead of the original branch's vfsmount)
2146 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2147 +  but it may be possible from userspace by the mount helper.
2148 +
2149 +Adding the internal hidden vfsmount and using it in opening a file, the
2150 +file path under /proc will be printed correctly. This approach looks
2151 +smarter, but is not possible I am afraid.
2152 +- aufs-root may be bind-mount later. when it happens, another hidden
2153 +  vfsmount will be required.
2154 +- it is hard to get the chance to bind-mount and lazy-umount
2155 +  + in kernel-space, FS can have vfsmount in open(2) via
2156 +    file->f_path, and aufs can know its vfsmount. But several locks are
2157 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2158 +    here, then it may cause a deadlock.
2159 +  + in user-space, bind-mount doesn't invoke the mount helper.
2160 +- since /proc shows dev and ino, aufs has to give vma these info. it
2161 +  means a new member vm_prinode will be necessary. this is essentially
2162 +  equivalent to vm_prfile described above.
2163 +
2164 +I have to give up this "looks-smater" approach.
2165 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2166 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2167 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2017-03-02 14:24:13.853588785 +0100
2168 @@ -0,0 +1,96 @@
2169 +
2170 +# Copyright (C) 2014-2017 Junjiro R. Okajima
2171 +#
2172 +# This program is free software; you can redistribute it and/or modify
2173 +# it under the terms of the GNU General Public License as published by
2174 +# the Free Software Foundation; either version 2 of the License, or
2175 +# (at your option) any later version.
2176 +#
2177 +# This program is distributed in the hope that it will be useful,
2178 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2179 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2180 +# GNU General Public License for more details.
2181 +#
2182 +# You should have received a copy of the GNU General Public License
2183 +# along with this program; if not, write to the Free Software
2184 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2185 +
2186 +
2187 +Listing XATTR/EA and getting the value
2188 +----------------------------------------------------------------------
2189 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2190 +shows the values from the topmost existing file. This behaviour is good
2191 +for the non-dir entries since the bahaviour exactly matches the shown
2192 +information. But for the directories, aufs considers all the same named
2193 +entries on the lower branches. Which means, if one of the lower entry
2194 +rejects readdir call, then aufs returns an error even if the topmost
2195 +entry allows it. This behaviour is necessary to respect the branch fs's
2196 +security, but can make users confused since the user-visible standard
2197 +attributes don't match the behaviour.
2198 +To address this issue, aufs has a mount option called dirperm1 which
2199 +checks the permission for the topmost entry only, and ignores the lower
2200 +entry's permission.
2201 +
2202 +A similar issue can happen around XATTR.
2203 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2204 +always set. Otherwise these very unpleasant situation would happen.
2205 +- listxattr(2) may return the duplicated entries.
2206 +- users may not be able to remove or reset the XATTR forever,
2207 +
2208 +
2209 +XATTR/EA support in the internal (copy,move)-(up,down)
2210 +----------------------------------------------------------------------
2211 +Generally the extended attributes of inode are categorized as these.
2212 +- "security" for LSM and capability.
2213 +- "system" for posix ACL, 'acl' mount option is required for the branch
2214 +  fs generally.
2215 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2216 +- "user" for userspace, 'user_xattr' mount option is required for the
2217 +  branch fs generally.
2218 +
2219 +Moreover there are some other categories. Aufs handles these rather
2220 +unpopular categories as the ordinary ones, ie. there is no special
2221 +condition nor exception.
2222 +
2223 +In copy-up, the support for XATTR on the dst branch may differ from the
2224 +src branch. In this case, the copy-up operation will get an error and
2225 +the original user operation which triggered the copy-up will fail. It
2226 +can happen that even all copy-up will fail.
2227 +When both of src and dst branches support XATTR and if an error occurs
2228 +during copying XATTR, then the copy-up should fail obviously. That is a
2229 +good reason and aufs should return an error to userspace. But when only
2230 +the src branch support that XATTR, aufs should not return an error.
2231 +For example, the src branch supports ACL but the dst branch doesn't
2232 +because the dst branch may natively un-support it or temporary
2233 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2234 +may NOT return an error even if the XATTR is not supported. It is
2235 +totally up to the branch fs.
2236 +
2237 +Anyway when the aufs internal copy-up gets an error from the dst branch
2238 +fs, then aufs tries removing the just copied entry and returns the error
2239 +to the userspace. The worst case of this situation will be all copy-up
2240 +will fail.
2241 +
2242 +For the copy-up operation, there two basic approaches.
2243 +- copy the specified XATTR only (by category above), and return the
2244 +  error unconditionally if it happens.
2245 +- copy all XATTR, and ignore the error on the specified category only.
2246 +
2247 +In order to support XATTR and to implement the correct behaviour, aufs
2248 +chooses the latter approach and introduces some new branch attributes,
2249 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2250 +They correspond to the XATTR namespaces (see above). Additionally, to be
2251 +convenient, "icex" is also provided which means all "icex*" attributes
2252 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2253 +
2254 +The meaning of these attributes is to ignore the error from setting
2255 +XATTR on that branch.
2256 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2257 +error from the dst branch according to the specified attributes.
2258 +
2259 +Some XATTR may have its default value. The default value may come from
2260 +the parent dir or the environment. If the default value is set at the
2261 +file creating-time, it will be overwritten by copy-up.
2262 +Some contradiction may happen I am afraid.
2263 +Do we need another attribute to stop copying XATTR? I am unsure. For
2264 +now, aufs implements the branch attributes to ignore the error.
2265 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2266 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2267 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2017-03-02 14:24:13.853588785 +0100
2268 @@ -0,0 +1,58 @@
2269 +
2270 +# Copyright (C) 2005-2017 Junjiro R. Okajima
2271 +# 
2272 +# This program is free software; you can redistribute it and/or modify
2273 +# it under the terms of the GNU General Public License as published by
2274 +# the Free Software Foundation; either version 2 of the License, or
2275 +# (at your option) any later version.
2276 +# 
2277 +# This program is distributed in the hope that it will be useful,
2278 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2279 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2280 +# GNU General Public License for more details.
2281 +# 
2282 +# You should have received a copy of the GNU General Public License
2283 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2284 +
2285 +Export Aufs via NFS
2286 +----------------------------------------------------------------------
2287 +Here is an approach.
2288 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2289 +  generation.
2290 +- iget_locked(): initialize aufs inode generation for a new inode, and
2291 +  store it in xigen file.
2292 +- destroy_inode(): increment aufs inode generation and store it in xigen
2293 +  file. it is necessary even if it is not unlinked, because any data of
2294 +  inode may be changed by UDBA.
2295 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2296 +  build file handle by
2297 +  + branch id (4 bytes)
2298 +  + superblock generation (4 bytes)
2299 +  + inode number (4 or 8 bytes)
2300 +  + parent dir inode number (4 or 8 bytes)
2301 +  + inode generation (4 bytes))
2302 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2303 +    bytes)
2304 +  + file handle for a branch (by exportfs_encode_fh())
2305 +- fh_to_dentry():
2306 +  + find the index of a branch from its id in handle, and check it is
2307 +    still exist in aufs.
2308 +  + 1st level: get the inode number from handle and search it in cache.
2309 +  + 2nd level: if not found in cache, get the parent inode number from
2310 +    the handle and search it in cache. and then open the found parent
2311 +    dir, find the matching inode number by vfs_readdir() and get its
2312 +    name, and call lookup_one_len() for the target dentry.
2313 +  + 3rd level: if the parent dir is not cached, call
2314 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2315 +    build a pathname of it, convert it a pathname in aufs, call
2316 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2317 +    the 2nd level.
2318 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2319 +    for every branch, but not itself. to get this, (currently) aufs
2320 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2321 +    idea, but I didn't get other approach.
2322 +  + test the generation of the gotten inode.
2323 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2324 +  convert it into ESTALE for NFSD.
2325 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2326 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2327 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2328 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2329 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2017-03-02 14:24:13.853588785 +0100
2330 @@ -0,0 +1,52 @@
2331 +
2332 +# Copyright (C) 2005-2017 Junjiro R. Okajima
2333 +# 
2334 +# This program is free software; you can redistribute it and/or modify
2335 +# it under the terms of the GNU General Public License as published by
2336 +# the Free Software Foundation; either version 2 of the License, or
2337 +# (at your option) any later version.
2338 +# 
2339 +# This program is distributed in the hope that it will be useful,
2340 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2341 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2342 +# GNU General Public License for more details.
2343 +# 
2344 +# You should have received a copy of the GNU General Public License
2345 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2346 +
2347 +Show Whiteout Mode (shwh)
2348 +----------------------------------------------------------------------
2349 +Generally aufs hides the name of whiteouts. But in some cases, to show
2350 +them is very useful for users. For instance, creating a new middle layer
2351 +(branch) by merging existing layers.
2352 +
2353 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2354 +When you have three branches,
2355 +- Bottom: 'system', squashfs (underlying base system), read-only
2356 +- Middle: 'mods', squashfs, read-only
2357 +- Top: 'overlay', ram (tmpfs), read-write
2358 +
2359 +The top layer is loaded at boot time and saved at shutdown, to preserve
2360 +the changes made to the system during the session.
2361 +When larger changes have been made, or smaller changes have accumulated,
2362 +the size of the saved top layer data grows. At this point, it would be
2363 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2364 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2365 +restoring save and load speed.
2366 +
2367 +This merging is simplified by the use of another aufs mount, of just the
2368 +two overlay branches using the 'shwh' option.
2369 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2370 +       aufs /livesys/merge_union
2371 +
2372 +A merged view of these two branches is then available at
2373 +/livesys/merge_union, and the new feature is that the whiteouts are
2374 +visible!
2375 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2376 +writing to all branches. Also the default mode for all branches is 'ro'.
2377 +It is now possible to save the combined contents of the two overlay
2378 +branches to a new squashfs, e.g.:
2379 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2380 +
2381 +This new squashfs archive can be stored on the boot device and the
2382 +initramfs will use it to replace the old one at the next boot.
2383 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2384 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2385 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2017-03-02 14:24:13.853588785 +0100
2386 @@ -0,0 +1,47 @@
2387 +
2388 +# Copyright (C) 2010-2017 Junjiro R. Okajima
2389 +#
2390 +# This program is free software; you can redistribute it and/or modify
2391 +# it under the terms of the GNU General Public License as published by
2392 +# the Free Software Foundation; either version 2 of the License, or
2393 +# (at your option) any later version.
2394 +#
2395 +# This program is distributed in the hope that it will be useful,
2396 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2397 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2398 +# GNU General Public License for more details.
2399 +#
2400 +# You should have received a copy of the GNU General Public License
2401 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2402 +
2403 +Dynamically customizable FS operations
2404 +----------------------------------------------------------------------
2405 +Generally FS operations (struct inode_operations, struct
2406 +address_space_operations, struct file_operations, etc.) are defined as
2407 +"static const", but it never means that FS have only one set of
2408 +operation. Some FS have multiple sets of them. For instance, ext2 has
2409 +three sets, one for XIP, for NOBH, and for normal.
2410 +Since aufs overrides and redirects these operations, sometimes aufs has
2411 +to change its behaviour according to the branch FS type. More importantly
2412 +VFS acts differently if a function (member in the struct) is set or
2413 +not. It means aufs should have several sets of operations and select one
2414 +among them according to the branch FS definition.
2415 +
2416 +In order to solve this problem and not to affect the behaviour of VFS,
2417 +aufs defines these operations dynamically. For instance, aufs defines
2418 +dummy direct_IO function for struct address_space_operations, but it may
2419 +not be set to the address_space_operations actually. When the branch FS
2420 +doesn't have it, aufs doesn't set it to its address_space_operations
2421 +while the function definition itself is still alive. So the behaviour
2422 +itself will not change, and it will return an error when direct_IO is
2423 +not set.
2424 +
2425 +The lifetime of these dynamically generated operation object is
2426 +maintained by aufs branch object. When the branch is removed from aufs,
2427 +the reference counter of the object is decremented. When it reaches
2428 +zero, the dynamically generated operation object will be freed.
2429 +
2430 +This approach is designed to support AIO (io_submit), Direct I/O and
2431 +XIP (DAX) mainly.
2432 +Currently this approach is applied to address_space_operations for
2433 +regular files only.
2434 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2435 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2436 +++ linux/Documentation/filesystems/aufs/README 2016-12-17 12:28:17.595211562 +0100
2437 @@ -0,0 +1,393 @@
2438 +
2439 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2440 +http://aufs.sf.net
2441 +Junjiro R. Okajima
2442 +
2443 +
2444 +0. Introduction
2445 +----------------------------------------
2446 +In the early days, aufs was entirely re-designed and re-implemented
2447 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2448 +improvements and implementations, it becomes totally different from
2449 +Unionfs while keeping the basic features.
2450 +Recently, Unionfs Version 2.x series begin taking some of the same
2451 +approaches to aufs1's.
2452 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2453 +University and his team.
2454 +
2455 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2456 +If you want older kernel version support, try aufs2-2.6.git or
2457 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2458 +
2459 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2460 +      According to Christoph Hellwig, linux rejects all union-type
2461 +      filesystems but UnionMount.
2462 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2463 +
2464 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2465 +    UnionMount, and he pointed out an issue around a directory mutex
2466 +    lock and aufs addressed it. But it is still unsure whether aufs will
2467 +    be merged (or any other union solution).
2468 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2469 +
2470 +
2471 +1. Features
2472 +----------------------------------------
2473 +- unite several directories into a single virtual filesystem. The member
2474 +  directory is called as a branch.
2475 +- you can specify the permission flags to the branch, which are 'readonly',
2476 +  'readwrite' and 'whiteout-able.'
2477 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2478 +  readonly branch are modifiable logically.
2479 +- dynamic branch manipulation, add, del.
2480 +- etc...
2481 +
2482 +Also there are many enhancements in aufs, such as:
2483 +- test only the highest one for the directory permission (dirperm1)
2484 +- copyup on open (coo=)
2485 +- 'move' policy for copy-up between two writable branches, after
2486 +  checking free space.
2487 +- xattr, acl
2488 +- readdir(3) in userspace.
2489 +- keep inode number by external inode number table
2490 +- keep the timestamps of file/dir in internal copyup operation
2491 +- seekable directory, supporting NFS readdir.
2492 +- whiteout is hardlinked in order to reduce the consumption of inodes
2493 +  on branch
2494 +- do not copyup, nor create a whiteout when it is unnecessary
2495 +- revert a single systemcall when an error occurs in aufs
2496 +- remount interface instead of ioctl
2497 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2498 +- loopback mounted filesystem as a branch
2499 +- kernel thread for removing the dir who has a plenty of whiteouts
2500 +- support copyup sparse file (a file which has a 'hole' in it)
2501 +- default permission flags for branches
2502 +- selectable permission flags for ro branch, whether whiteout can
2503 +  exist or not
2504 +- export via NFS.
2505 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2506 +- support multiple writable branches, some policies to select one
2507 +  among multiple writable branches.
2508 +- a new semantics for link(2) and rename(2) to support multiple
2509 +  writable branches.
2510 +- no glibc changes are required.
2511 +- pseudo hardlink (hardlink over branches)
2512 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2513 +  including NFS or remote filesystem branch.
2514 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2515 +- and more...
2516 +
2517 +Currently these features are dropped temporary from aufs4.
2518 +See design/08plan.txt in detail.
2519 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2520 +  (robr)
2521 +- statistics of aufs thread (/sys/fs/aufs/stat)
2522 +
2523 +Features or just an idea in the future (see also design/*.txt),
2524 +- reorder the branch index without del/re-add.
2525 +- permanent xino files for NFSD
2526 +- an option for refreshing the opened files after add/del branches
2527 +- light version, without branch manipulation. (unnecessary?)
2528 +- copyup in userspace
2529 +- inotify in userspace
2530 +- readv/writev
2531 +
2532 +
2533 +2. Download
2534 +----------------------------------------
2535 +There are three GIT trees for aufs4, aufs4-linux.git,
2536 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2537 +"aufs-util.git."
2538 +While the aufs-util is always necessary, you need either of aufs4-linux
2539 +or aufs4-standalone.
2540 +
2541 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2542 +git://git.kernel.org/.../torvalds/linux.git.
2543 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2544 +build aufs4 as an external kernel module.
2545 +Several extra patches are not included in this tree. Only
2546 +aufs4-standalone tree contains them. They are described in the later
2547 +section "Configuration and Compilation."
2548 +
2549 +On the other hand, the aufs4-standalone tree has only aufs source files
2550 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2551 +But you need to apply all aufs patches manually.
2552 +
2553 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2554 +represents the linux kernel version, "linux-4.x". For instance,
2555 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2556 +"aufs4.x-rcN" branch.
2557 +
2558 +o aufs4-linux tree
2559 +$ git clone --reference /your/linux/git/tree \
2560 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2561 +- if you don't have linux GIT tree, then remove "--reference ..."
2562 +$ cd aufs4-linux.git
2563 +$ git checkout origin/aufs4.0
2564 +
2565 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2566 +leave the patch-work to GIT.
2567 +$ cd /your/linux/git/tree
2568 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2569 +$ git fetch aufs4
2570 +$ git checkout -b my4.0 v4.0
2571 +$ (add your local change...)
2572 +$ git pull aufs4 aufs4.0
2573 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2574 +- you may need to solve some conflicts between your_changes and
2575 +  aufs4.0. in this case, git-rerere is recommended so that you can
2576 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2577 +  later in the future.
2578 +
2579 +o aufs4-standalone tree
2580 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2581 +$ cd aufs4-standalone.git
2582 +$ git checkout origin/aufs4.0
2583 +
2584 +o aufs-util tree
2585 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2586 +- note that the public aufs-util.git is on SourceForge instead of
2587 +  GitHUB.
2588 +$ cd aufs-util.git
2589 +$ git checkout origin/aufs4.0
2590 +
2591 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2592 +The minor version number, 'x' in '4.x', of aufs may not always
2593 +follow the minor version number of the kernel.
2594 +Because changes in the kernel that cause the use of a new
2595 +minor version number do not always require changes to aufs-util.
2596 +
2597 +Since aufs-util has its own minor version number, you may not be
2598 +able to find a GIT branch in aufs-util for your kernel's
2599 +exact minor version number.
2600 +In this case, you should git-checkout the branch for the
2601 +nearest lower number.
2602 +
2603 +For (an unreleased) example:
2604 +If you are using "linux-4.10" and the "aufs4.10" branch
2605 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2606 +or something numerically smaller is the branch for your kernel.
2607 +
2608 +Also you can view all branches by
2609 +       $ git branch -a
2610 +
2611 +
2612 +3. Configuration and Compilation
2613 +----------------------------------------
2614 +Make sure you have git-checkout'ed the correct branch.
2615 +
2616 +For aufs4-linux tree,
2617 +- enable CONFIG_AUFS_FS.
2618 +- set other aufs configurations if necessary.
2619 +
2620 +For aufs4-standalone tree,
2621 +There are several ways to build.
2622 +
2623 +1.
2624 +- apply ./aufs4-kbuild.patch to your kernel source files.
2625 +- apply ./aufs4-base.patch too.
2626 +- apply ./aufs4-mmap.patch too.
2627 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2628 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2629 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2630 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2631 +- enable CONFIG_AUFS_FS, you can select either
2632 +  =m or =y.
2633 +- and build your kernel as usual.
2634 +- install the built kernel.
2635 +  Note: Since linux-3.9, every filesystem module requires an alias
2636 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2637 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2638 +- install the header files too by "make headers_install" to the
2639 +  directory where you specify. By default, it is $PWD/usr.
2640 +  "make help" shows a brief note for headers_install.
2641 +- and reboot your system.
2642 +
2643 +2.
2644 +- module only (CONFIG_AUFS_FS=m).
2645 +- apply ./aufs4-base.patch to your kernel source files.
2646 +- apply ./aufs4-mmap.patch too.
2647 +- apply ./aufs4-standalone.patch too.
2648 +- build your kernel, don't forget "make headers_install", and reboot.
2649 +- edit ./config.mk and set other aufs configurations if necessary.
2650 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2651 +  every aufs configurations.
2652 +- build the module by simple "make".
2653 +  Note: Since linux-3.9, every filesystem module requires an alias
2654 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2655 +  modules.aliases file.
2656 +- you can specify ${KDIR} make variable which points to your kernel
2657 +  source tree.
2658 +- install the files
2659 +  + run "make install" to install the aufs module, or copy the built
2660 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2661 +  + run "make install_headers" (instead of headers_install) to install
2662 +    the modified aufs header file (you can specify DESTDIR which is
2663 +    available in aufs standalone version's Makefile only), or copy
2664 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2665 +    you like manually. By default, the target directory is $PWD/usr.
2666 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2667 +  kernel source tree.
2668 +
2669 +Note: The header file aufs_type.h is necessary to build aufs-util
2670 +      as well as "make headers_install" in the kernel source tree.
2671 +      headers_install is subject to be forgotten, but it is essentially
2672 +      necessary, not only for building aufs-util.
2673 +      You may not meet problems without headers_install in some older
2674 +      version though.
2675 +
2676 +And then,
2677 +- read README in aufs-util, build and install it
2678 +- note that your distribution may contain an obsoleted version of
2679 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2680 +  utilities, make sure that your compiler refers the correct aufs header
2681 +  file which is built by "make headers_install."
2682 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2683 +  then run "make install_ulib" too. And refer to the aufs manual in
2684 +  detail.
2685 +
2686 +There several other patches in aufs4-standalone.git. They are all
2687 +optional. When you meet some problems, they will help you.
2688 +- aufs4-loopback.patch
2689 +  Supports a nested loopback mount in a branch-fs. This patch is
2690 +  unnecessary until aufs produces a message like "you may want to try
2691 +  another patch for loopback file".
2692 +- vfs-ino.patch
2693 +  Modifies a system global kernel internal function get_next_ino() in
2694 +  order to stop assigning 0 for an inode-number. Not directly related to
2695 +  aufs, but recommended generally.
2696 +- tmpfs-idr.patch
2697 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2698 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2699 +  duplication of inode number, which is important for backup tools and
2700 +  other utilities. When you find aufs XINO files for tmpfs branch
2701 +  growing too much, try this patch.
2702 +- lockdep-debug.patch
2703 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2704 +  also a caller of VFS functions for branch filesystems, subclassing of
2705 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2706 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2707 +  need to apply this debug patch to expand several constant values.
2708 +  If don't know what LOCKDEP, then you don't have apply this patch.
2709 +
2710 +
2711 +4. Usage
2712 +----------------------------------------
2713 +At first, make sure aufs-util are installed, and please read the aufs
2714 +manual, aufs.5 in aufs-util.git tree.
2715 +$ man -l aufs.5
2716 +
2717 +And then,
2718 +$ mkdir /tmp/rw /tmp/aufs
2719 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2720 +
2721 +Here is another example. The result is equivalent.
2722 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2723 +  Or
2724 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2725 +# mount -o remount,append:${HOME} /tmp/aufs
2726 +
2727 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2728 +you modify a file under /tmp/aufs, the one on your home directory is
2729 +not affected, instead the same named file will be newly created under
2730 +/tmp/rw. And all of your modification to a file will be applied to
2731 +the one under /tmp/rw. This is called the file based Copy on Write
2732 +(COW) method.
2733 +Aufs mount options are described in aufs.5.
2734 +If you run chroot or something and make your aufs as a root directory,
2735 +then you need to customize the shutdown script. See the aufs manual in
2736 +detail.
2737 +
2738 +Additionally, there are some sample usages of aufs which are a
2739 +diskless system with network booting, and LiveCD over NFS.
2740 +See sample dir in CVS tree on SourceForge.
2741 +
2742 +
2743 +5. Contact
2744 +----------------------------------------
2745 +When you have any problems or strange behaviour in aufs, please let me
2746 +know with:
2747 +- /proc/mounts (instead of the output of mount(8))
2748 +- /sys/module/aufs/*
2749 +- /sys/fs/aufs/* (if you have them)
2750 +- /debug/aufs/* (if you have them)
2751 +- linux kernel version
2752 +  if your kernel is not plain, for example modified by distributor,
2753 +  the url where i can download its source is necessary too.
2754 +- aufs version which was printed at loading the module or booting the
2755 +  system, instead of the date you downloaded.
2756 +- configuration (define/undefine CONFIG_AUFS_xxx)
2757 +- kernel configuration or /proc/config.gz (if you have it)
2758 +- behaviour which you think to be incorrect
2759 +- actual operation, reproducible one is better
2760 +- mailto: aufs-users at lists.sourceforge.net
2761 +
2762 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2763 +and Feature Requests) on SourceForge. Please join and write to
2764 +aufs-users ML.
2765 +
2766 +
2767 +6. Acknowledgements
2768 +----------------------------------------
2769 +Thanks to everyone who have tried and are using aufs, whoever
2770 +have reported a bug or any feedback.
2771 +
2772 +Especially donators:
2773 +Tomas Matejicek(slax.org) made a donation (much more than once).
2774 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2775 +       scripts) is making "doubling" donations.
2776 +       Unfortunately I cannot list all of the donators, but I really
2777 +       appreciate.
2778 +       It ends Aug 2010, but the ordinary donation URL is still available.
2779 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2780 +Dai Itasaka made a donation (2007/8).
2781 +Chuck Smith made a donation (2008/4, 10 and 12).
2782 +Henk Schoneveld made a donation (2008/9).
2783 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2784 +Francois Dupoux made a donation (2008/11).
2785 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2786 +       aufs2 GIT tree (2009/2).
2787 +William Grant made a donation (2009/3).
2788 +Patrick Lane made a donation (2009/4).
2789 +The Mail Archive (mail-archive.com) made donations (2009/5).
2790 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2791 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2792 +Pavel Pronskiy made a donation (2011/2).
2793 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2794 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2795 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2796 +11).
2797 +Sam Liddicott made a donation (2011/9).
2798 +Era Scarecrow made a donation (2013/4).
2799 +Bor Ratajc made a donation (2013/4).
2800 +Alessandro Gorreta made a donation (2013/4).
2801 +POIRETTE Marc made a donation (2013/4).
2802 +Alessandro Gorreta made a donation (2013/4).
2803 +lauri kasvandik made a donation (2013/5).
2804 +"pemasu from Finland" made a donation (2013/7).
2805 +The Parted Magic Project made a donation (2013/9 and 11).
2806 +Pavel Barta made a donation (2013/10).
2807 +Nikolay Pertsev made a donation (2014/5).
2808 +James B made a donation (2014/7 and 2015/7).
2809 +Stefano Di Biase made a donation (2014/8).
2810 +Daniel Epellei made a donation (2015/1).
2811 +OmegaPhil made a donation (2016/1).
2812 +Tomasz Szewczyk made a donation (2016/4).
2813 +James Burry made a donation (2016/12).
2814 +
2815 +Thank you very much.
2816 +Donations are always, including future donations, very important and
2817 +helpful for me to keep on developing aufs.
2818 +
2819 +
2820 +7.
2821 +----------------------------------------
2822 +If you are an experienced user, no explanation is needed. Aufs is
2823 +just a linux filesystem.
2824 +
2825 +
2826 +Enjoy!
2827 +
2828 +# Local variables: ;
2829 +# mode: text;
2830 +# End: ;
2831 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2832 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2833 +++ linux/fs/aufs/aufs.h        2017-03-02 14:24:13.853588785 +0100
2834 @@ -0,0 +1,59 @@
2835 +/*
2836 + * Copyright (C) 2005-2017 Junjiro R. Okajima
2837 + *
2838 + * This program, aufs is free software; you can redistribute it and/or modify
2839 + * it under the terms of the GNU General Public License as published by
2840 + * the Free Software Foundation; either version 2 of the License, or
2841 + * (at your option) any later version.
2842 + *
2843 + * This program is distributed in the hope that it will be useful,
2844 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2845 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2846 + * GNU General Public License for more details.
2847 + *
2848 + * You should have received a copy of the GNU General Public License
2849 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2850 + */
2851 +
2852 +/*
2853 + * all header files
2854 + */
2855 +
2856 +#ifndef __AUFS_H__
2857 +#define __AUFS_H__
2858 +
2859 +#ifdef __KERNEL__
2860 +
2861 +#define AuStub(type, name, body, ...) \
2862 +       static inline type name(__VA_ARGS__) { body; }
2863 +
2864 +#define AuStubVoid(name, ...) \
2865 +       AuStub(void, name, , __VA_ARGS__)
2866 +#define AuStubInt0(name, ...) \
2867 +       AuStub(int, name, return 0, __VA_ARGS__)
2868 +
2869 +#include "debug.h"
2870 +
2871 +#include "branch.h"
2872 +#include "cpup.h"
2873 +#include "dcsub.h"
2874 +#include "dbgaufs.h"
2875 +#include "dentry.h"
2876 +#include "dir.h"
2877 +#include "dynop.h"
2878 +#include "file.h"
2879 +#include "fstype.h"
2880 +#include "inode.h"
2881 +#include "loop.h"
2882 +#include "module.h"
2883 +#include "opts.h"
2884 +#include "rwsem.h"
2885 +#include "spl.h"
2886 +#include "super.h"
2887 +#include "sysaufs.h"
2888 +#include "vfsub.h"
2889 +#include "whout.h"
2890 +#include "wkq.h"
2891 +
2892 +#endif /* __KERNEL__ */
2893 +#endif /* __AUFS_H__ */
2894 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2895 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2896 +++ linux/fs/aufs/branch.c      2017-03-02 14:24:13.853588785 +0100
2897 @@ -0,0 +1,1412 @@
2898 +/*
2899 + * Copyright (C) 2005-2017 Junjiro R. Okajima
2900 + *
2901 + * This program, aufs is free software; you can redistribute it and/or modify
2902 + * it under the terms of the GNU General Public License as published by
2903 + * the Free Software Foundation; either version 2 of the License, or
2904 + * (at your option) any later version.
2905 + *
2906 + * This program is distributed in the hope that it will be useful,
2907 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2908 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2909 + * GNU General Public License for more details.
2910 + *
2911 + * You should have received a copy of the GNU General Public License
2912 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2913 + */
2914 +
2915 +/*
2916 + * branch management
2917 + */
2918 +
2919 +#include <linux/compat.h>
2920 +#include <linux/statfs.h>
2921 +#include "aufs.h"
2922 +
2923 +/*
2924 + * free a single branch
2925 + */
2926 +static void au_br_do_free(struct au_branch *br)
2927 +{
2928 +       int i;
2929 +       struct au_wbr *wbr;
2930 +       struct au_dykey **key;
2931 +
2932 +       au_hnotify_fin_br(br);
2933 +
2934 +       if (br->br_xino.xi_file)
2935 +               fput(br->br_xino.xi_file);
2936 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2937 +
2938 +       AuDebugOn(au_br_count(br));
2939 +       au_br_count_fin(br);
2940 +
2941 +       wbr = br->br_wbr;
2942 +       if (wbr) {
2943 +               for (i = 0; i < AuBrWh_Last; i++)
2944 +                       dput(wbr->wbr_wh[i]);
2945 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2946 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2947 +       }
2948 +
2949 +       if (br->br_fhsm) {
2950 +               au_br_fhsm_fin(br->br_fhsm);
2951 +               au_delayed_kfree(br->br_fhsm);
2952 +       }
2953 +
2954 +       key = br->br_dykey;
2955 +       for (i = 0; i < AuBrDynOp; i++, key++)
2956 +               if (*key)
2957 +                       au_dy_put(*key);
2958 +               else
2959 +                       break;
2960 +
2961 +       /* recursive lock, s_umount of branch's */
2962 +       lockdep_off();
2963 +       path_put(&br->br_path);
2964 +       lockdep_on();
2965 +       if (wbr)
2966 +               au_delayed_kfree(wbr);
2967 +       au_delayed_kfree(br);
2968 +}
2969 +
2970 +/*
2971 + * frees all branches
2972 + */
2973 +void au_br_free(struct au_sbinfo *sbinfo)
2974 +{
2975 +       aufs_bindex_t bmax;
2976 +       struct au_branch **br;
2977 +
2978 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2979 +
2980 +       bmax = sbinfo->si_bbot + 1;
2981 +       br = sbinfo->si_branch;
2982 +       while (bmax--)
2983 +               au_br_do_free(*br++);
2984 +}
2985 +
2986 +/*
2987 + * find the index of a branch which is specified by @br_id.
2988 + */
2989 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2990 +{
2991 +       aufs_bindex_t bindex, bbot;
2992 +
2993 +       bbot = au_sbbot(sb);
2994 +       for (bindex = 0; bindex <= bbot; bindex++)
2995 +               if (au_sbr_id(sb, bindex) == br_id)
2996 +                       return bindex;
2997 +       return -1;
2998 +}
2999 +
3000 +/* ---------------------------------------------------------------------- */
3001 +
3002 +/*
3003 + * add a branch
3004 + */
3005 +
3006 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3007 +                       struct dentry *h_root)
3008 +{
3009 +       if (unlikely(h_adding == h_root
3010 +                    || au_test_loopback_overlap(sb, h_adding)))
3011 +               return 1;
3012 +       if (h_adding->d_sb != h_root->d_sb)
3013 +               return 0;
3014 +       return au_test_subdir(h_adding, h_root)
3015 +               || au_test_subdir(h_root, h_adding);
3016 +}
3017 +
3018 +/*
3019 + * returns a newly allocated branch. @new_nbranch is a number of branches
3020 + * after adding a branch.
3021 + */
3022 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3023 +                                    int perm)
3024 +{
3025 +       struct au_branch *add_branch;
3026 +       struct dentry *root;
3027 +       struct inode *inode;
3028 +       int err;
3029 +
3030 +       err = -ENOMEM;
3031 +       root = sb->s_root;
3032 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3033 +       if (unlikely(!add_branch))
3034 +               goto out;
3035 +
3036 +       err = au_hnotify_init_br(add_branch, perm);
3037 +       if (unlikely(err))
3038 +               goto out_br;
3039 +
3040 +       if (au_br_writable(perm)) {
3041 +               /* may be freed separately at changing the branch permission */
3042 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3043 +                                            GFP_NOFS);
3044 +               if (unlikely(!add_branch->br_wbr))
3045 +                       goto out_hnotify;
3046 +       }
3047 +
3048 +       if (au_br_fhsm(perm)) {
3049 +               err = au_fhsm_br_alloc(add_branch);
3050 +               if (unlikely(err))
3051 +                       goto out_wbr;
3052 +       }
3053 +
3054 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3055 +       if (!err)
3056 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3057 +       if (!err) {
3058 +               inode = d_inode(root);
3059 +               err = au_hinode_realloc(au_ii(inode), new_nbranch, /*may_shrink*/0);
3060 +       }
3061 +       if (!err)
3062 +               return add_branch; /* success */
3063 +
3064 +out_wbr:
3065 +       if (add_branch->br_wbr)
3066 +               au_delayed_kfree(add_branch->br_wbr);
3067 +out_hnotify:
3068 +       au_hnotify_fin_br(add_branch);
3069 +out_br:
3070 +       au_delayed_kfree(add_branch);
3071 +out:
3072 +       return ERR_PTR(err);
3073 +}
3074 +
3075 +/*
3076 + * test if the branch permission is legal or not.
3077 + */
3078 +static int test_br(struct inode *inode, int brperm, char *path)
3079 +{
3080 +       int err;
3081 +
3082 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3083 +       if (!err)
3084 +               goto out;
3085 +
3086 +       err = -EINVAL;
3087 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3088 +
3089 +out:
3090 +       return err;
3091 +}
3092 +
3093 +/*
3094 + * returns:
3095 + * 0: success, the caller will add it
3096 + * plus: success, it is already unified, the caller should ignore it
3097 + * minus: error
3098 + */
3099 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3100 +{
3101 +       int err;
3102 +       aufs_bindex_t bbot, bindex;
3103 +       struct dentry *root, *h_dentry;
3104 +       struct inode *inode, *h_inode;
3105 +
3106 +       root = sb->s_root;
3107 +       bbot = au_sbbot(sb);
3108 +       if (unlikely(bbot >= 0
3109 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3110 +               err = 1;
3111 +               if (!remount) {
3112 +                       err = -EINVAL;
3113 +                       pr_err("%s duplicated\n", add->pathname);
3114 +               }
3115 +               goto out;
3116 +       }
3117 +
3118 +       err = -ENOSPC; /* -E2BIG; */
3119 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3120 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3121 +               pr_err("number of branches exceeded %s\n", add->pathname);
3122 +               goto out;
3123 +       }
3124 +
3125 +       err = -EDOM;
3126 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3127 +               pr_err("bad index %d\n", add->bindex);
3128 +               goto out;
3129 +       }
3130 +
3131 +       inode = d_inode(add->path.dentry);
3132 +       err = -ENOENT;
3133 +       if (unlikely(!inode->i_nlink)) {
3134 +               pr_err("no existence %s\n", add->pathname);
3135 +               goto out;
3136 +       }
3137 +
3138 +       err = -EINVAL;
3139 +       if (unlikely(inode->i_sb == sb)) {
3140 +               pr_err("%s must be outside\n", add->pathname);
3141 +               goto out;
3142 +       }
3143 +
3144 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3145 +               pr_err("unsupported filesystem, %s (%s)\n",
3146 +                      add->pathname, au_sbtype(inode->i_sb));
3147 +               goto out;
3148 +       }
3149 +
3150 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3151 +               pr_err("already stacked, %s (%s)\n",
3152 +                      add->pathname, au_sbtype(inode->i_sb));
3153 +               goto out;
3154 +       }
3155 +
3156 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3157 +       if (unlikely(err))
3158 +               goto out;
3159 +
3160 +       if (bbot < 0)
3161 +               return 0; /* success */
3162 +
3163 +       err = -EINVAL;
3164 +       for (bindex = 0; bindex <= bbot; bindex++)
3165 +               if (unlikely(test_overlap(sb, add->path.dentry,
3166 +                                         au_h_dptr(root, bindex)))) {
3167 +                       pr_err("%s is overlapped\n", add->pathname);
3168 +                       goto out;
3169 +               }
3170 +
3171 +       err = 0;
3172 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3173 +               h_dentry = au_h_dptr(root, 0);
3174 +               h_inode = d_inode(h_dentry);
3175 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3176 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3177 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3178 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3179 +                               add->pathname,
3180 +                               i_uid_read(inode), i_gid_read(inode),
3181 +                               (inode->i_mode & S_IALLUGO),
3182 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3183 +                               (h_inode->i_mode & S_IALLUGO));
3184 +       }
3185 +
3186 +out:
3187 +       return err;
3188 +}
3189 +
3190 +/*
3191 + * initialize or clean the whiteouts for an adding branch
3192 + */
3193 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3194 +                        int new_perm)
3195 +{
3196 +       int err, old_perm;
3197 +       aufs_bindex_t bindex;
3198 +       struct inode *h_inode;
3199 +       struct au_wbr *wbr;
3200 +       struct au_hinode *hdir;
3201 +       struct dentry *h_dentry;
3202 +
3203 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3204 +       if (unlikely(err))
3205 +               goto out;
3206 +
3207 +       wbr = br->br_wbr;
3208 +       old_perm = br->br_perm;
3209 +       br->br_perm = new_perm;
3210 +       hdir = NULL;
3211 +       h_inode = NULL;
3212 +       bindex = au_br_index(sb, br->br_id);
3213 +       if (0 <= bindex) {
3214 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3215 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3216 +       } else {
3217 +               h_dentry = au_br_dentry(br);
3218 +               h_inode = d_inode(h_dentry);
3219 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3220 +       }
3221 +       if (!wbr)
3222 +               err = au_wh_init(br, sb);
3223 +       else {
3224 +               wbr_wh_write_lock(wbr);
3225 +               err = au_wh_init(br, sb);
3226 +               wbr_wh_write_unlock(wbr);
3227 +       }
3228 +       if (hdir)
3229 +               au_hn_inode_unlock(hdir);
3230 +       else
3231 +               inode_unlock(h_inode);
3232 +       vfsub_mnt_drop_write(au_br_mnt(br));
3233 +       br->br_perm = old_perm;
3234 +
3235 +       if (!err && wbr && !au_br_writable(new_perm)) {
3236 +               au_delayed_kfree(wbr);
3237 +               br->br_wbr = NULL;
3238 +       }
3239 +
3240 +out:
3241 +       return err;
3242 +}
3243 +
3244 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3245 +                      int perm)
3246 +{
3247 +       int err;
3248 +       struct kstatfs kst;
3249 +       struct au_wbr *wbr;
3250 +
3251 +       wbr = br->br_wbr;
3252 +       au_rw_init(&wbr->wbr_wh_rwsem);
3253 +       atomic_set(&wbr->wbr_wh_running, 0);
3254 +
3255 +       /*
3256 +        * a limit for rmdir/rename a dir
3257 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3258 +        */
3259 +       err = vfs_statfs(&br->br_path, &kst);
3260 +       if (unlikely(err))
3261 +               goto out;
3262 +       err = -EINVAL;
3263 +       if (kst.f_namelen >= NAME_MAX)
3264 +               err = au_br_init_wh(sb, br, perm);
3265 +       else
3266 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3267 +                      au_br_dentry(br),
3268 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3269 +
3270 +out:
3271 +       return err;
3272 +}
3273 +
3274 +/* initialize a new branch */
3275 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3276 +                     struct au_opt_add *add)
3277 +{
3278 +       int err;
3279 +       struct inode *h_inode;
3280 +
3281 +       err = 0;
3282 +       mutex_init(&br->br_xino.xi_nondir_mtx);
3283 +       br->br_perm = add->perm;
3284 +       br->br_path = add->path; /* set first, path_get() later */
3285 +       spin_lock_init(&br->br_dykey_lock);
3286 +       au_br_count_init(br);
3287 +       atomic_set(&br->br_xino_running, 0);
3288 +       br->br_id = au_new_br_id(sb);
3289 +       AuDebugOn(br->br_id < 0);
3290 +
3291 +       if (au_br_writable(add->perm)) {
3292 +               err = au_wbr_init(br, sb, add->perm);
3293 +               if (unlikely(err))
3294 +                       goto out_err;
3295 +       }
3296 +
3297 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3298 +               h_inode = d_inode(add->path.dentry);
3299 +               err = au_xino_br(sb, br, h_inode->i_ino,
3300 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3301 +               if (unlikely(err)) {
3302 +                       AuDebugOn(br->br_xino.xi_file);
3303 +                       goto out_err;
3304 +               }
3305 +       }
3306 +
3307 +       sysaufs_br_init(br);
3308 +       path_get(&br->br_path);
3309 +       goto out; /* success */
3310 +
3311 +out_err:
3312 +       memset(&br->br_path, 0, sizeof(br->br_path));
3313 +out:
3314 +       return err;
3315 +}
3316 +
3317 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3318 +                            struct au_branch *br, aufs_bindex_t bbot,
3319 +                            aufs_bindex_t amount)
3320 +{
3321 +       struct au_branch **brp;
3322 +
3323 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3324 +
3325 +       brp = sbinfo->si_branch + bindex;
3326 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3327 +       *brp = br;
3328 +       sbinfo->si_bbot++;
3329 +       if (unlikely(bbot < 0))
3330 +               sbinfo->si_bbot = 0;
3331 +}
3332 +
3333 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3334 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3335 +{
3336 +       struct au_hdentry *hdp;
3337 +
3338 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3339 +
3340 +       hdp = au_hdentry(dinfo, bindex);
3341 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3342 +       au_h_dentry_init(hdp);
3343 +       dinfo->di_bbot++;
3344 +       if (unlikely(bbot < 0))
3345 +               dinfo->di_btop = 0;
3346 +}
3347 +
3348 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3349 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3350 +{
3351 +       struct au_hinode *hip;
3352 +
3353 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3354 +
3355 +       hip = au_hinode(iinfo, bindex);
3356 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3357 +       au_hinode_init(hip);
3358 +       iinfo->ii_bbot++;
3359 +       if (unlikely(bbot < 0))
3360 +               iinfo->ii_btop = 0;
3361 +}
3362 +
3363 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3364 +                        aufs_bindex_t bindex)
3365 +{
3366 +       struct dentry *root, *h_dentry;
3367 +       struct inode *root_inode, *h_inode;
3368 +       aufs_bindex_t bbot, amount;
3369 +
3370 +       root = sb->s_root;
3371 +       root_inode = d_inode(root);
3372 +       bbot = au_sbbot(sb);
3373 +       amount = bbot + 1 - bindex;
3374 +       h_dentry = au_br_dentry(br);
3375 +       au_sbilist_lock();
3376 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3377 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3378 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3379 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3380 +       h_inode = d_inode(h_dentry);
3381 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3382 +       au_sbilist_unlock();
3383 +}
3384 +
3385 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3386 +{
3387 +       int err;
3388 +       aufs_bindex_t bbot, add_bindex;
3389 +       struct dentry *root, *h_dentry;
3390 +       struct inode *root_inode;
3391 +       struct au_branch *add_branch;
3392 +
3393 +       root = sb->s_root;
3394 +       root_inode = d_inode(root);
3395 +       IMustLock(root_inode);
3396 +       IiMustWriteLock(root_inode);
3397 +       err = test_add(sb, add, remount);
3398 +       if (unlikely(err < 0))
3399 +               goto out;
3400 +       if (err) {
3401 +               err = 0;
3402 +               goto out; /* success */
3403 +       }
3404 +
3405 +       bbot = au_sbbot(sb);
3406 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3407 +       err = PTR_ERR(add_branch);
3408 +       if (IS_ERR(add_branch))
3409 +               goto out;
3410 +
3411 +       err = au_br_init(add_branch, sb, add);
3412 +       if (unlikely(err)) {
3413 +               au_br_do_free(add_branch);
3414 +               goto out;
3415 +       }
3416 +
3417 +       add_bindex = add->bindex;
3418 +       if (!remount)
3419 +               au_br_do_add(sb, add_branch, add_bindex);
3420 +       else {
3421 +               sysaufs_brs_del(sb, add_bindex);
3422 +               au_br_do_add(sb, add_branch, add_bindex);
3423 +               sysaufs_brs_add(sb, add_bindex);
3424 +       }
3425 +
3426 +       h_dentry = add->path.dentry;
3427 +       if (!add_bindex) {
3428 +               au_cpup_attr_all(root_inode, /*force*/1);
3429 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3430 +       } else
3431 +               au_add_nlink(root_inode, d_inode(h_dentry));
3432 +
3433 +       /*
3434 +        * this test/set prevents aufs from handling unnecesary notify events
3435 +        * of xino files, in case of re-adding a writable branch which was
3436 +        * once detached from aufs.
3437 +        */
3438 +       if (au_xino_brid(sb) < 0
3439 +           && au_br_writable(add_branch->br_perm)
3440 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3441 +           && add_branch->br_xino.xi_file
3442 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3443 +               au_xino_brid_set(sb, add_branch->br_id);
3444 +
3445 +out:
3446 +       return err;
3447 +}
3448 +
3449 +/* ---------------------------------------------------------------------- */
3450 +
3451 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3452 +                                      unsigned long long max __maybe_unused,
3453 +                                      void *arg)
3454 +{
3455 +       unsigned long long n;
3456 +       struct file **p, *f;
3457 +       struct au_sphlhead *files;
3458 +       struct au_finfo *finfo;
3459 +
3460 +       n = 0;
3461 +       p = a;
3462 +       files = &au_sbi(sb)->si_files;
3463 +       spin_lock(&files->spin);
3464 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3465 +               f = finfo->fi_file;
3466 +               if (file_count(f)
3467 +                   && !special_file(file_inode(f)->i_mode)) {
3468 +                       get_file(f);
3469 +                       *p++ = f;
3470 +                       n++;
3471 +                       AuDebugOn(n > max);
3472 +               }
3473 +       }
3474 +       spin_unlock(&files->spin);
3475 +
3476 +       return n;
3477 +}
3478 +
3479 +static struct file **au_farray_alloc(struct super_block *sb,
3480 +                                    unsigned long long *max)
3481 +{
3482 +       *max = au_nfiles(sb);
3483 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3484 +}
3485 +
3486 +static void au_farray_free(struct file **a, unsigned long long max)
3487 +{
3488 +       unsigned long long ull;
3489 +
3490 +       for (ull = 0; ull < max; ull++)
3491 +               if (a[ull])
3492 +                       fput(a[ull]);
3493 +       kvfree(a);
3494 +}
3495 +
3496 +/* ---------------------------------------------------------------------- */
3497 +
3498 +/*
3499 + * delete a branch
3500 + */
3501 +
3502 +/* to show the line number, do not make it inlined function */
3503 +#define AuVerbose(do_info, fmt, ...) do { \
3504 +       if (do_info) \
3505 +               pr_info(fmt, ##__VA_ARGS__); \
3506 +} while (0)
3507 +
3508 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3509 +                        aufs_bindex_t bbot)
3510 +{
3511 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3512 +}
3513 +
3514 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3515 +                        aufs_bindex_t bbot)
3516 +{
3517 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3518 +}
3519 +
3520 +/*
3521 + * test if the branch is deletable or not.
3522 + */
3523 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3524 +                           unsigned int sigen, const unsigned int verbose)
3525 +{
3526 +       int err, i, j, ndentry;
3527 +       aufs_bindex_t btop, bbot;
3528 +       struct au_dcsub_pages dpages;
3529 +       struct au_dpage *dpage;
3530 +       struct dentry *d;
3531 +
3532 +       err = au_dpages_init(&dpages, GFP_NOFS);
3533 +       if (unlikely(err))
3534 +               goto out;
3535 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3536 +       if (unlikely(err))
3537 +               goto out_dpages;
3538 +
3539 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3540 +               dpage = dpages.dpages + i;
3541 +               ndentry = dpage->ndentry;
3542 +               for (j = 0; !err && j < ndentry; j++) {
3543 +                       d = dpage->dentries[j];
3544 +                       AuDebugOn(au_dcount(d) <= 0);
3545 +                       if (!au_digen_test(d, sigen)) {
3546 +                               di_read_lock_child(d, AuLock_IR);
3547 +                               if (unlikely(au_dbrange_test(d))) {
3548 +                                       di_read_unlock(d, AuLock_IR);
3549 +                                       continue;
3550 +                               }
3551 +                       } else {
3552 +                               di_write_lock_child(d);
3553 +                               if (unlikely(au_dbrange_test(d))) {
3554 +                                       di_write_unlock(d);
3555 +                                       continue;
3556 +                               }
3557 +                               err = au_reval_dpath(d, sigen);
3558 +                               if (!err)
3559 +                                       di_downgrade_lock(d, AuLock_IR);
3560 +                               else {
3561 +                                       di_write_unlock(d);
3562 +                                       break;
3563 +                               }
3564 +                       }
3565 +
3566 +                       /* AuDbgDentry(d); */
3567 +                       btop = au_dbtop(d);
3568 +                       bbot = au_dbbot(d);
3569 +                       if (btop <= bindex
3570 +                           && bindex <= bbot
3571 +                           && au_h_dptr(d, bindex)
3572 +                           && au_test_dbusy(d, btop, bbot)) {
3573 +                               err = -EBUSY;
3574 +                               AuVerbose(verbose, "busy %pd\n", d);
3575 +                               AuDbgDentry(d);
3576 +                       }
3577 +                       di_read_unlock(d, AuLock_IR);
3578 +               }
3579 +       }
3580 +
3581 +out_dpages:
3582 +       au_dpages_free(&dpages);
3583 +out:
3584 +       return err;
3585 +}
3586 +
3587 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3588 +                          unsigned int sigen, const unsigned int verbose)
3589 +{
3590 +       int err;
3591 +       unsigned long long max, ull;
3592 +       struct inode *i, **array;
3593 +       aufs_bindex_t btop, bbot;
3594 +
3595 +       array = au_iarray_alloc(sb, &max);
3596 +       err = PTR_ERR(array);
3597 +       if (IS_ERR(array))
3598 +               goto out;
3599 +
3600 +       err = 0;
3601 +       AuDbg("b%d\n", bindex);
3602 +       for (ull = 0; !err && ull < max; ull++) {
3603 +               i = array[ull];
3604 +               if (unlikely(!i))
3605 +                       break;
3606 +               if (i->i_ino == AUFS_ROOT_INO)
3607 +                       continue;
3608 +
3609 +               /* AuDbgInode(i); */
3610 +               if (au_iigen(i, NULL) == sigen)
3611 +                       ii_read_lock_child(i);
3612 +               else {
3613 +                       ii_write_lock_child(i);
3614 +                       err = au_refresh_hinode_self(i);
3615 +                       au_iigen_dec(i);
3616 +                       if (!err)
3617 +                               ii_downgrade_lock(i);
3618 +                       else {
3619 +                               ii_write_unlock(i);
3620 +                               break;
3621 +                       }
3622 +               }
3623 +
3624 +               btop = au_ibtop(i);
3625 +               bbot = au_ibbot(i);
3626 +               if (btop <= bindex
3627 +                   && bindex <= bbot
3628 +                   && au_h_iptr(i, bindex)
3629 +                   && au_test_ibusy(i, btop, bbot)) {
3630 +                       err = -EBUSY;
3631 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3632 +                       AuDbgInode(i);
3633 +               }
3634 +               ii_read_unlock(i);
3635 +       }
3636 +       au_iarray_free(array, max);
3637 +
3638 +out:
3639 +       return err;
3640 +}
3641 +
3642 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3643 +                             const unsigned int verbose)
3644 +{
3645 +       int err;
3646 +       unsigned int sigen;
3647 +
3648 +       sigen = au_sigen(root->d_sb);
3649 +       DiMustNoWaiters(root);
3650 +       IiMustNoWaiters(d_inode(root));
3651 +       di_write_unlock(root);
3652 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3653 +       if (!err)
3654 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3655 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3656 +
3657 +       return err;
3658 +}
3659 +
3660 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3661 +                        struct file **to_free, int *idx)
3662 +{
3663 +       int err;
3664 +       unsigned char matched, root;
3665 +       aufs_bindex_t bindex, bbot;
3666 +       struct au_fidir *fidir;
3667 +       struct au_hfile *hfile;
3668 +
3669 +       err = 0;
3670 +       root = IS_ROOT(file->f_path.dentry);
3671 +       if (root) {
3672 +               get_file(file);
3673 +               to_free[*idx] = file;
3674 +               (*idx)++;
3675 +               goto out;
3676 +       }
3677 +
3678 +       matched = 0;
3679 +       fidir = au_fi(file)->fi_hdir;
3680 +       AuDebugOn(!fidir);
3681 +       bbot = au_fbbot_dir(file);
3682 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3683 +               hfile = fidir->fd_hfile + bindex;
3684 +               if (!hfile->hf_file)
3685 +                       continue;
3686 +
3687 +               if (hfile->hf_br->br_id == br_id) {
3688 +                       matched = 1;
3689 +                       break;
3690 +               }
3691 +       }
3692 +       if (matched)
3693 +               err = -EBUSY;
3694 +
3695 +out:
3696 +       return err;
3697 +}
3698 +
3699 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3700 +                         struct file **to_free, int opened)
3701 +{
3702 +       int err, idx;
3703 +       unsigned long long ull, max;
3704 +       aufs_bindex_t btop;
3705 +       struct file *file, **array;
3706 +       struct dentry *root;
3707 +       struct au_hfile *hfile;
3708 +
3709 +       array = au_farray_alloc(sb, &max);
3710 +       err = PTR_ERR(array);
3711 +       if (IS_ERR(array))
3712 +               goto out;
3713 +
3714 +       err = 0;
3715 +       idx = 0;
3716 +       root = sb->s_root;
3717 +       di_write_unlock(root);
3718 +       for (ull = 0; ull < max; ull++) {
3719 +               file = array[ull];
3720 +               if (unlikely(!file))
3721 +                       break;
3722 +
3723 +               /* AuDbg("%pD\n", file); */
3724 +               fi_read_lock(file);
3725 +               btop = au_fbtop(file);
3726 +               if (!d_is_dir(file->f_path.dentry)) {
3727 +                       hfile = &au_fi(file)->fi_htop;
3728 +                       if (hfile->hf_br->br_id == br_id)
3729 +                               err = -EBUSY;
3730 +               } else
3731 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3732 +               fi_read_unlock(file);
3733 +               if (unlikely(err))
3734 +                       break;
3735 +       }
3736 +       di_write_lock_child(root);
3737 +       au_farray_free(array, max);
3738 +       AuDebugOn(idx > opened);
3739 +
3740 +out:
3741 +       return err;
3742 +}
3743 +
3744 +static void br_del_file(struct file **to_free, unsigned long long opened,
3745 +                         aufs_bindex_t br_id)
3746 +{
3747 +       unsigned long long ull;
3748 +       aufs_bindex_t bindex, btop, bbot, bfound;
3749 +       struct file *file;
3750 +       struct au_fidir *fidir;
3751 +       struct au_hfile *hfile;
3752 +
3753 +       for (ull = 0; ull < opened; ull++) {
3754 +               file = to_free[ull];
3755 +               if (unlikely(!file))
3756 +                       break;
3757 +
3758 +               /* AuDbg("%pD\n", file); */
3759 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3760 +               bfound = -1;
3761 +               fidir = au_fi(file)->fi_hdir;
3762 +               AuDebugOn(!fidir);
3763 +               fi_write_lock(file);
3764 +               btop = au_fbtop(file);
3765 +               bbot = au_fbbot_dir(file);
3766 +               for (bindex = btop; bindex <= bbot; bindex++) {
3767 +                       hfile = fidir->fd_hfile + bindex;
3768 +                       if (!hfile->hf_file)
3769 +                               continue;
3770 +
3771 +                       if (hfile->hf_br->br_id == br_id) {
3772 +                               bfound = bindex;
3773 +                               break;
3774 +                       }
3775 +               }
3776 +               AuDebugOn(bfound < 0);
3777 +               au_set_h_fptr(file, bfound, NULL);
3778 +               if (bfound == btop) {
3779 +                       for (btop++; btop <= bbot; btop++)
3780 +                               if (au_hf_dir(file, btop)) {
3781 +                                       au_set_fbtop(file, btop);
3782 +                                       break;
3783 +                               }
3784 +               }
3785 +               fi_write_unlock(file);
3786 +       }
3787 +}
3788 +
3789 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3790 +                            const aufs_bindex_t bindex,
3791 +                            const aufs_bindex_t bbot)
3792 +{
3793 +       struct au_branch **brp, **p;
3794 +
3795 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3796 +
3797 +       brp = sbinfo->si_branch + bindex;
3798 +       if (bindex < bbot)
3799 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3800 +       sbinfo->si_branch[0 + bbot] = NULL;
3801 +       sbinfo->si_bbot--;
3802 +
3803 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3804 +                       /*may_shrink*/1);
3805 +       if (p)
3806 +               sbinfo->si_branch = p;
3807 +       /* harmless error */
3808 +}
3809 +
3810 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3811 +                            const aufs_bindex_t bbot)
3812 +{
3813 +       struct au_hdentry *hdp, *p;
3814 +
3815 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3816 +
3817 +       hdp = au_hdentry(dinfo, bindex);
3818 +       if (bindex < bbot)
3819 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3820 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3821 +       dinfo->di_bbot--;
3822 +
3823 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3824 +                       /*may_shrink*/1);
3825 +       if (p)
3826 +               dinfo->di_hdentry = p;
3827 +       /* harmless error */
3828 +}
3829 +
3830 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3831 +                            const aufs_bindex_t bbot)
3832 +{
3833 +       struct au_hinode *hip, *p;
3834 +
3835 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3836 +
3837 +       hip = au_hinode(iinfo, bindex);
3838 +       if (bindex < bbot)
3839 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3840 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3841 +       iinfo->ii_bbot--;
3842 +
3843 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3844 +                       /*may_shrink*/1);
3845 +       if (p)
3846 +               iinfo->ii_hinode = p;
3847 +       /* harmless error */
3848 +}
3849 +
3850 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3851 +                        struct au_branch *br)
3852 +{
3853 +       aufs_bindex_t bbot;
3854 +       struct au_sbinfo *sbinfo;
3855 +       struct dentry *root, *h_root;
3856 +       struct inode *inode, *h_inode;
3857 +       struct au_hinode *hinode;
3858 +
3859 +       SiMustWriteLock(sb);
3860 +
3861 +       root = sb->s_root;
3862 +       inode = d_inode(root);
3863 +       sbinfo = au_sbi(sb);
3864 +       bbot = sbinfo->si_bbot;
3865 +
3866 +       h_root = au_h_dptr(root, bindex);
3867 +       hinode = au_hi(inode, bindex);
3868 +       h_inode = au_igrab(hinode->hi_inode);
3869 +       au_hiput(hinode);
3870 +
3871 +       au_sbilist_lock();
3872 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3873 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3874 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3875 +       au_sbilist_unlock();
3876 +
3877 +       dput(h_root);
3878 +       iput(h_inode);
3879 +       au_br_do_free(br);
3880 +}
3881 +
3882 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3883 +                                  unsigned long long max, void *arg)
3884 +{
3885 +       return max;
3886 +}
3887 +
3888 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3889 +{
3890 +       int err, rerr, i;
3891 +       unsigned long long opened;
3892 +       unsigned int mnt_flags;
3893 +       aufs_bindex_t bindex, bbot, br_id;
3894 +       unsigned char do_wh, verbose;
3895 +       struct au_branch *br;
3896 +       struct au_wbr *wbr;
3897 +       struct dentry *root;
3898 +       struct file **to_free;
3899 +
3900 +       err = 0;
3901 +       opened = 0;
3902 +       to_free = NULL;
3903 +       root = sb->s_root;
3904 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3905 +       if (bindex < 0) {
3906 +               if (remount)
3907 +                       goto out; /* success */
3908 +               err = -ENOENT;
3909 +               pr_err("%s no such branch\n", del->pathname);
3910 +               goto out;
3911 +       }
3912 +       AuDbg("bindex b%d\n", bindex);
3913 +
3914 +       err = -EBUSY;
3915 +       mnt_flags = au_mntflags(sb);
3916 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3917 +       bbot = au_sbbot(sb);
3918 +       if (unlikely(!bbot)) {
3919 +               AuVerbose(verbose, "no more branches left\n");
3920 +               goto out;
3921 +       }
3922 +       br = au_sbr(sb, bindex);
3923 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3924 +
3925 +       br_id = br->br_id;
3926 +       opened = au_br_count(br);
3927 +       if (unlikely(opened)) {
3928 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3929 +               err = PTR_ERR(to_free);
3930 +               if (IS_ERR(to_free))
3931 +                       goto out;
3932 +
3933 +               err = test_file_busy(sb, br_id, to_free, opened);
3934 +               if (unlikely(err)) {
3935 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3936 +                       goto out;
3937 +               }
3938 +       }
3939 +
3940 +       wbr = br->br_wbr;
3941 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3942 +       if (do_wh) {
3943 +               /* instead of WbrWhMustWriteLock(wbr) */
3944 +               SiMustWriteLock(sb);
3945 +               for (i = 0; i < AuBrWh_Last; i++) {
3946 +                       dput(wbr->wbr_wh[i]);
3947 +                       wbr->wbr_wh[i] = NULL;
3948 +               }
3949 +       }
3950 +
3951 +       err = test_children_busy(root, bindex, verbose);
3952 +       if (unlikely(err)) {
3953 +               if (do_wh)
3954 +                       goto out_wh;
3955 +               goto out;
3956 +       }
3957 +
3958 +       err = 0;
3959 +       if (to_free) {
3960 +               /*
3961 +                * now we confirmed the branch is deletable.
3962 +                * let's free the remaining opened dirs on the branch.
3963 +                */
3964 +               di_write_unlock(root);
3965 +               br_del_file(to_free, opened, br_id);
3966 +               di_write_lock_child(root);
3967 +       }
3968 +
3969 +       if (!remount)
3970 +               au_br_do_del(sb, bindex, br);
3971 +       else {
3972 +               sysaufs_brs_del(sb, bindex);
3973 +               au_br_do_del(sb, bindex, br);
3974 +               sysaufs_brs_add(sb, bindex);
3975 +       }
3976 +
3977 +       if (!bindex) {
3978 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3979 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3980 +       } else
3981 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3982 +       if (au_opt_test(mnt_flags, PLINK))
3983 +               au_plink_half_refresh(sb, br_id);
3984 +
3985 +       if (au_xino_brid(sb) == br_id)
3986 +               au_xino_brid_set(sb, -1);
3987 +       goto out; /* success */
3988 +
3989 +out_wh:
3990 +       /* revert */
3991 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3992 +       if (rerr)
3993 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3994 +                       del->pathname, rerr);
3995 +out:
3996 +       if (to_free)
3997 +               au_farray_free(to_free, opened);
3998 +       return err;
3999 +}
4000 +
4001 +/* ---------------------------------------------------------------------- */
4002 +
4003 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4004 +{
4005 +       int err;
4006 +       aufs_bindex_t btop, bbot;
4007 +       struct aufs_ibusy ibusy;
4008 +       struct inode *inode, *h_inode;
4009 +
4010 +       err = -EPERM;
4011 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4012 +               goto out;
4013 +
4014 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4015 +       if (!err)
4016 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
4017 +       if (unlikely(err)) {
4018 +               err = -EFAULT;
4019 +               AuTraceErr(err);
4020 +               goto out;
4021 +       }
4022 +
4023 +       err = -EINVAL;
4024 +       si_read_lock(sb, AuLock_FLUSH);
4025 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4026 +               goto out_unlock;
4027 +
4028 +       err = 0;
4029 +       ibusy.h_ino = 0; /* invalid */
4030 +       inode = ilookup(sb, ibusy.ino);
4031 +       if (!inode
4032 +           || inode->i_ino == AUFS_ROOT_INO
4033 +           || au_is_bad_inode(inode))
4034 +               goto out_unlock;
4035 +
4036 +       ii_read_lock_child(inode);
4037 +       btop = au_ibtop(inode);
4038 +       bbot = au_ibbot(inode);
4039 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4040 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4041 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4042 +                       ibusy.h_ino = h_inode->i_ino;
4043 +       }
4044 +       ii_read_unlock(inode);
4045 +       iput(inode);
4046 +
4047 +out_unlock:
4048 +       si_read_unlock(sb);
4049 +       if (!err) {
4050 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4051 +               if (unlikely(err)) {
4052 +                       err = -EFAULT;
4053 +                       AuTraceErr(err);
4054 +               }
4055 +       }
4056 +out:
4057 +       return err;
4058 +}
4059 +
4060 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4061 +{
4062 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4063 +}
4064 +
4065 +#ifdef CONFIG_COMPAT
4066 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4067 +{
4068 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4069 +}
4070 +#endif
4071 +
4072 +/* ---------------------------------------------------------------------- */
4073 +
4074 +/*
4075 + * change a branch permission
4076 + */
4077 +
4078 +static void au_warn_ima(void)
4079 +{
4080 +#ifdef CONFIG_IMA
4081 +       /* since it doesn't support mark_files_ro() */
4082 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4083 +#endif
4084 +}
4085 +
4086 +static int do_need_sigen_inc(int a, int b)
4087 +{
4088 +       return au_br_whable(a) && !au_br_whable(b);
4089 +}
4090 +
4091 +static int need_sigen_inc(int old, int new)
4092 +{
4093 +       return do_need_sigen_inc(old, new)
4094 +               || do_need_sigen_inc(new, old);
4095 +}
4096 +
4097 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4098 +{
4099 +       int err, do_warn;
4100 +       unsigned int mnt_flags;
4101 +       unsigned long long ull, max;
4102 +       aufs_bindex_t br_id;
4103 +       unsigned char verbose, writer;
4104 +       struct file *file, *hf, **array;
4105 +       struct au_hfile *hfile;
4106 +
4107 +       mnt_flags = au_mntflags(sb);
4108 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4109 +
4110 +       array = au_farray_alloc(sb, &max);
4111 +       err = PTR_ERR(array);
4112 +       if (IS_ERR(array))
4113 +               goto out;
4114 +
4115 +       do_warn = 0;
4116 +       br_id = au_sbr_id(sb, bindex);
4117 +       for (ull = 0; ull < max; ull++) {
4118 +               file = array[ull];
4119 +               if (unlikely(!file))
4120 +                       break;
4121 +
4122 +               /* AuDbg("%pD\n", file); */
4123 +               fi_read_lock(file);
4124 +               if (unlikely(au_test_mmapped(file))) {
4125 +                       err = -EBUSY;
4126 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4127 +                       AuDbgFile(file);
4128 +                       FiMustNoWaiters(file);
4129 +                       fi_read_unlock(file);
4130 +                       goto out_array;
4131 +               }
4132 +
4133 +               hfile = &au_fi(file)->fi_htop;
4134 +               hf = hfile->hf_file;
4135 +               if (!d_is_reg(file->f_path.dentry)
4136 +                   || !(file->f_mode & FMODE_WRITE)
4137 +                   || hfile->hf_br->br_id != br_id
4138 +                   || !(hf->f_mode & FMODE_WRITE))
4139 +                       array[ull] = NULL;
4140 +               else {
4141 +                       do_warn = 1;
4142 +                       get_file(file);
4143 +               }
4144 +
4145 +               FiMustNoWaiters(file);
4146 +               fi_read_unlock(file);
4147 +               fput(file);
4148 +       }
4149 +
4150 +       err = 0;
4151 +       if (do_warn)
4152 +               au_warn_ima();
4153 +
4154 +       for (ull = 0; ull < max; ull++) {
4155 +               file = array[ull];
4156 +               if (!file)
4157 +                       continue;
4158 +
4159 +               /* todo: already flushed? */
4160 +               /*
4161 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4162 +                * approach which resets f_mode and calls mnt_drop_write() and
4163 +                * file_release_write() for each file, because the branch
4164 +                * attribute in aufs world is totally different from the native
4165 +                * fs rw/ro mode.
4166 +               */
4167 +               /* fi_read_lock(file); */
4168 +               hfile = &au_fi(file)->fi_htop;
4169 +               hf = hfile->hf_file;
4170 +               /* fi_read_unlock(file); */
4171 +               spin_lock(&hf->f_lock);
4172 +               writer = !!(hf->f_mode & FMODE_WRITER);
4173 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4174 +               spin_unlock(&hf->f_lock);
4175 +               if (writer) {
4176 +                       put_write_access(file_inode(hf));
4177 +                       __mnt_drop_write(hf->f_path.mnt);
4178 +               }
4179 +       }
4180 +
4181 +out_array:
4182 +       au_farray_free(array, max);
4183 +out:
4184 +       AuTraceErr(err);
4185 +       return err;
4186 +}
4187 +
4188 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4189 +             int *do_refresh)
4190 +{
4191 +       int err, rerr;
4192 +       aufs_bindex_t bindex;
4193 +       struct dentry *root;
4194 +       struct au_branch *br;
4195 +       struct au_br_fhsm *bf;
4196 +
4197 +       root = sb->s_root;
4198 +       bindex = au_find_dbindex(root, mod->h_root);
4199 +       if (bindex < 0) {
4200 +               if (remount)
4201 +                       return 0; /* success */
4202 +               err = -ENOENT;
4203 +               pr_err("%s no such branch\n", mod->path);
4204 +               goto out;
4205 +       }
4206 +       AuDbg("bindex b%d\n", bindex);
4207 +
4208 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4209 +       if (unlikely(err))
4210 +               goto out;
4211 +
4212 +       br = au_sbr(sb, bindex);
4213 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4214 +       if (br->br_perm == mod->perm)
4215 +               return 0; /* success */
4216 +
4217 +       /* pre-allocate for non-fhsm --> fhsm */
4218 +       bf = NULL;
4219 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4220 +               err = au_fhsm_br_alloc(br);
4221 +               if (unlikely(err))
4222 +                       goto out;
4223 +               bf = br->br_fhsm;
4224 +               br->br_fhsm = NULL;
4225 +       }
4226 +
4227 +       if (au_br_writable(br->br_perm)) {
4228 +               /* remove whiteout base */
4229 +               err = au_br_init_wh(sb, br, mod->perm);
4230 +               if (unlikely(err))
4231 +                       goto out_bf;
4232 +
4233 +               if (!au_br_writable(mod->perm)) {
4234 +                       /* rw --> ro, file might be mmapped */
4235 +                       DiMustNoWaiters(root);
4236 +                       IiMustNoWaiters(d_inode(root));
4237 +                       di_write_unlock(root);
4238 +                       err = au_br_mod_files_ro(sb, bindex);
4239 +                       /* aufs_write_lock() calls ..._child() */
4240 +                       di_write_lock_child(root);
4241 +
4242 +                       if (unlikely(err)) {
4243 +                               rerr = -ENOMEM;
4244 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4245 +                                                    GFP_NOFS);
4246 +                               if (br->br_wbr)
4247 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4248 +                               if (unlikely(rerr)) {
4249 +                                       AuIOErr("nested error %d (%d)\n",
4250 +                                               rerr, err);
4251 +                                       br->br_perm = mod->perm;
4252 +                               }
4253 +                       }
4254 +               }
4255 +       } else if (au_br_writable(mod->perm)) {
4256 +               /* ro --> rw */
4257 +               err = -ENOMEM;
4258 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4259 +               if (br->br_wbr) {
4260 +                       err = au_wbr_init(br, sb, mod->perm);
4261 +                       if (unlikely(err)) {
4262 +                               au_delayed_kfree(br->br_wbr);
4263 +                               br->br_wbr = NULL;
4264 +                       }
4265 +               }
4266 +       }
4267 +       if (unlikely(err))
4268 +               goto out_bf;
4269 +
4270 +       if (au_br_fhsm(br->br_perm)) {
4271 +               if (!au_br_fhsm(mod->perm)) {
4272 +                       /* fhsm --> non-fhsm */
4273 +                       au_br_fhsm_fin(br->br_fhsm);
4274 +                       au_delayed_kfree(br->br_fhsm);
4275 +                       br->br_fhsm = NULL;
4276 +               }
4277 +       } else if (au_br_fhsm(mod->perm))
4278 +               /* non-fhsm --> fhsm */
4279 +               br->br_fhsm = bf;
4280 +
4281 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4282 +       br->br_perm = mod->perm;
4283 +       goto out; /* success */
4284 +
4285 +out_bf:
4286 +       if (bf)
4287 +               au_delayed_kfree(bf);
4288 +out:
4289 +       AuTraceErr(err);
4290 +       return err;
4291 +}
4292 +
4293 +/* ---------------------------------------------------------------------- */
4294 +
4295 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4296 +{
4297 +       int err;
4298 +       struct kstatfs kstfs;
4299 +
4300 +       err = vfs_statfs(&br->br_path, &kstfs);
4301 +       if (!err) {
4302 +               stfs->f_blocks = kstfs.f_blocks;
4303 +               stfs->f_bavail = kstfs.f_bavail;
4304 +               stfs->f_files = kstfs.f_files;
4305 +               stfs->f_ffree = kstfs.f_ffree;
4306 +       }
4307 +
4308 +       return err;
4309 +}
4310 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4311 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4312 +++ linux/fs/aufs/branch.h      2017-03-02 14:24:13.853588785 +0100
4313 @@ -0,0 +1,309 @@
4314 +/*
4315 + * Copyright (C) 2005-2017 Junjiro R. Okajima
4316 + *
4317 + * This program, aufs is free software; you can redistribute it and/or modify
4318 + * it under the terms of the GNU General Public License as published by
4319 + * the Free Software Foundation; either version 2 of the License, or
4320 + * (at your option) any later version.
4321 + *
4322 + * This program is distributed in the hope that it will be useful,
4323 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4324 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4325 + * GNU General Public License for more details.
4326 + *
4327 + * You should have received a copy of the GNU General Public License
4328 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4329 + */
4330 +
4331 +/*
4332 + * branch filesystems and xino for them
4333 + */
4334 +
4335 +#ifndef __AUFS_BRANCH_H__
4336 +#define __AUFS_BRANCH_H__
4337 +
4338 +#ifdef __KERNEL__
4339 +
4340 +#include <linux/mount.h>
4341 +#include "dynop.h"
4342 +#include "rwsem.h"
4343 +#include "super.h"
4344 +
4345 +/* ---------------------------------------------------------------------- */
4346 +
4347 +/* a xino file */
4348 +struct au_xino_file {
4349 +       struct file             *xi_file;
4350 +       struct mutex            xi_nondir_mtx;
4351 +
4352 +       /* todo: make xino files an array to support huge inode number */
4353 +
4354 +#ifdef CONFIG_DEBUG_FS
4355 +       struct dentry            *xi_dbgaufs;
4356 +#endif
4357 +};
4358 +
4359 +/* File-based Hierarchical Storage Management */
4360 +struct au_br_fhsm {
4361 +#ifdef CONFIG_AUFS_FHSM
4362 +       struct mutex            bf_lock;
4363 +       unsigned long           bf_jiffy;
4364 +       struct aufs_stfs        bf_stfs;
4365 +       int                     bf_readable;
4366 +#endif
4367 +};
4368 +
4369 +/* members for writable branch only */
4370 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4371 +struct au_wbr {
4372 +       struct au_rwsem         wbr_wh_rwsem;
4373 +       struct dentry           *wbr_wh[AuBrWh_Last];
4374 +       atomic_t                wbr_wh_running;
4375 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4376 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4377 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4378 +
4379 +       /* mfs mode */
4380 +       unsigned long long      wbr_bytes;
4381 +};
4382 +
4383 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4384 +#define AuBrDynOp (AuDyLast * 4)
4385 +
4386 +#ifdef CONFIG_AUFS_HFSNOTIFY
4387 +/* support for asynchronous destruction */
4388 +struct au_br_hfsnotify {
4389 +       struct fsnotify_group   *hfsn_group;
4390 +};
4391 +#endif
4392 +
4393 +/* sysfs entries */
4394 +struct au_brsysfs {
4395 +       char                    name[16];
4396 +       struct attribute        attr;
4397 +};
4398 +
4399 +enum {
4400 +       AuBrSysfs_BR,
4401 +       AuBrSysfs_BRID,
4402 +       AuBrSysfs_Last
4403 +};
4404 +
4405 +/* protected by superblock rwsem */
4406 +struct au_branch {
4407 +       struct au_xino_file     br_xino;
4408 +
4409 +       aufs_bindex_t           br_id;
4410 +
4411 +       int                     br_perm;
4412 +       struct path             br_path;
4413 +       spinlock_t              br_dykey_lock;
4414 +       struct au_dykey         *br_dykey[AuBrDynOp];
4415 +       struct percpu_counter   br_count;
4416 +
4417 +       struct au_wbr           *br_wbr;
4418 +       struct au_br_fhsm       *br_fhsm;
4419 +
4420 +       /* xino truncation */
4421 +       atomic_t                br_xino_running;
4422 +
4423 +#ifdef CONFIG_AUFS_HFSNOTIFY
4424 +       struct au_br_hfsnotify  *br_hfsn;
4425 +#endif
4426 +
4427 +#ifdef CONFIG_SYSFS
4428 +       /* entries under sysfs per mount-point */
4429 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4430 +#endif
4431 +};
4432 +
4433 +/* ---------------------------------------------------------------------- */
4434 +
4435 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4436 +{
4437 +       return br->br_path.mnt;
4438 +}
4439 +
4440 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4441 +{
4442 +       return br->br_path.dentry;
4443 +}
4444 +
4445 +static inline struct super_block *au_br_sb(struct au_branch *br)
4446 +{
4447 +       return au_br_mnt(br)->mnt_sb;
4448 +}
4449 +
4450 +static inline void au_br_get(struct au_branch *br)
4451 +{
4452 +       percpu_counter_inc(&br->br_count);
4453 +}
4454 +
4455 +static inline void au_br_put(struct au_branch *br)
4456 +{
4457 +       percpu_counter_dec(&br->br_count);
4458 +}
4459 +
4460 +static inline s64 au_br_count(struct au_branch *br)
4461 +{
4462 +       return percpu_counter_sum(&br->br_count);
4463 +}
4464 +
4465 +static inline void au_br_count_init(struct au_branch *br)
4466 +{
4467 +       percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4468 +}
4469 +
4470 +static inline void au_br_count_fin(struct au_branch *br)
4471 +{
4472 +       percpu_counter_destroy(&br->br_count);
4473 +}
4474 +
4475 +static inline int au_br_rdonly(struct au_branch *br)
4476 +{
4477 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4478 +               || !au_br_writable(br->br_perm))
4479 +               ? -EROFS : 0;
4480 +}
4481 +
4482 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4483 +{
4484 +#ifdef CONFIG_AUFS_HNOTIFY
4485 +       return !(brperm & AuBrPerm_RR);
4486 +#else
4487 +       return 0;
4488 +#endif
4489 +}
4490 +
4491 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4492 +{
4493 +       int err, exec_flag;
4494 +
4495 +       err = 0;
4496 +       exec_flag = oflag & __FMODE_EXEC;
4497 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4498 +               err = -EACCES;
4499 +
4500 +       return err;
4501 +}
4502 +
4503 +/* ---------------------------------------------------------------------- */
4504 +
4505 +/* branch.c */
4506 +struct au_sbinfo;
4507 +void au_br_free(struct au_sbinfo *sinfo);
4508 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4509 +struct au_opt_add;
4510 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4511 +struct au_opt_del;
4512 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4513 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4514 +#ifdef CONFIG_COMPAT
4515 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4516 +#endif
4517 +struct au_opt_mod;
4518 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4519 +             int *do_refresh);
4520 +struct aufs_stfs;
4521 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4522 +
4523 +/* xino.c */
4524 +static const loff_t au_loff_max = LLONG_MAX;
4525 +
4526 +int au_xib_trunc(struct super_block *sb);
4527 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4528 +                  loff_t *pos);
4529 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4530 +                   size_t size, loff_t *pos);
4531 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4532 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4533 +ino_t au_xino_new_ino(struct super_block *sb);
4534 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4535 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4536 +                 ino_t ino);
4537 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4538 +                ino_t *ino);
4539 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4540 +              struct file *base_file, int do_test);
4541 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4542 +
4543 +struct au_opt_xino;
4544 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4545 +void au_xino_clr(struct super_block *sb);
4546 +struct file *au_xino_def(struct super_block *sb);
4547 +int au_xino_path(struct seq_file *seq, struct file *file);
4548 +
4549 +/* ---------------------------------------------------------------------- */
4550 +
4551 +/* Superblock to branch */
4552 +static inline
4553 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4554 +{
4555 +       return au_sbr(sb, bindex)->br_id;
4556 +}
4557 +
4558 +static inline
4559 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4560 +{
4561 +       return au_br_mnt(au_sbr(sb, bindex));
4562 +}
4563 +
4564 +static inline
4565 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4566 +{
4567 +       return au_br_sb(au_sbr(sb, bindex));
4568 +}
4569 +
4570 +static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4571 +{
4572 +       au_br_get(au_sbr(sb, bindex));
4573 +}
4574 +
4575 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4576 +{
4577 +       au_br_put(au_sbr(sb, bindex));
4578 +}
4579 +
4580 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4581 +{
4582 +       return au_sbr(sb, bindex)->br_perm;
4583 +}
4584 +
4585 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4586 +{
4587 +       return au_br_whable(au_sbr_perm(sb, bindex));
4588 +}
4589 +
4590 +/* ---------------------------------------------------------------------- */
4591 +
4592 +/*
4593 + * wbr_wh_read_lock, wbr_wh_write_lock
4594 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4595 + */
4596 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4597 +
4598 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4599 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4600 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4601 +
4602 +/* ---------------------------------------------------------------------- */
4603 +
4604 +#ifdef CONFIG_AUFS_FHSM
4605 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4606 +{
4607 +       mutex_init(&brfhsm->bf_lock);
4608 +       brfhsm->bf_jiffy = 0;
4609 +       brfhsm->bf_readable = 0;
4610 +}
4611 +
4612 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4613 +{
4614 +       mutex_destroy(&brfhsm->bf_lock);
4615 +}
4616 +#else
4617 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4618 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4619 +#endif
4620 +
4621 +#endif /* __KERNEL__ */
4622 +#endif /* __AUFS_BRANCH_H__ */
4623 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4624 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4625 +++ linux/fs/aufs/conf.mk       2016-10-09 16:55:36.486034798 +0200
4626 @@ -0,0 +1,38 @@
4627 +
4628 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4629 +
4630 +define AuConf
4631 +ifdef ${1}
4632 +AuConfStr += ${1}=${${1}}
4633 +endif
4634 +endef
4635 +
4636 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4637 +       SBILIST \
4638 +       HNOTIFY HFSNOTIFY \
4639 +       EXPORT INO_T_64 \
4640 +       XATTR \
4641 +       FHSM \
4642 +       RDU \
4643 +       SHWH \
4644 +       BR_RAMFS \
4645 +       BR_FUSE POLL \
4646 +       BR_HFSPLUS \
4647 +       BDEV_LOOP \
4648 +       DEBUG MAGIC_SYSRQ
4649 +$(foreach i, ${AuConfAll}, \
4650 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4651 +
4652 +AuConfName = ${obj}/conf.str
4653 +${AuConfName}.tmp: FORCE
4654 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4655 +${AuConfName}: ${AuConfName}.tmp
4656 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4657 +       echo '  GEN    ' $@; \
4658 +       cp -p $< $@; \
4659 +       }
4660 +FORCE:
4661 +clean-files += ${AuConfName} ${AuConfName}.tmp
4662 +${obj}/sysfs.o: ${AuConfName}
4663 +
4664 +-include ${srctree}/${src}/conf_priv.mk
4665 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4666 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4667 +++ linux/fs/aufs/cpup.c        2017-03-02 14:24:13.856922210 +0100
4668 @@ -0,0 +1,1388 @@
4669 +/*
4670 + * Copyright (C) 2005-2017 Junjiro R. Okajima
4671 + *
4672 + * This program, aufs is free software; you can redistribute it and/or modify
4673 + * it under the terms of the GNU General Public License as published by
4674 + * the Free Software Foundation; either version 2 of the License, or
4675 + * (at your option) any later version.
4676 + *
4677 + * This program is distributed in the hope that it will be useful,
4678 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4679 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4680 + * GNU General Public License for more details.
4681 + *
4682 + * You should have received a copy of the GNU General Public License
4683 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4684 + */
4685 +
4686 +/*
4687 + * copy-up functions, see wbr_policy.c for copy-down
4688 + */
4689 +
4690 +#include <linux/fs_stack.h>
4691 +#include <linux/mm.h>
4692 +#include <linux/task_work.h>
4693 +#include "aufs.h"
4694 +
4695 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4696 +{
4697 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4698 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4699 +
4700 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4701 +
4702 +       dst->i_flags |= iflags & ~mask;
4703 +       if (au_test_fs_notime(dst->i_sb))
4704 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4705 +}
4706 +
4707 +void au_cpup_attr_timesizes(struct inode *inode)
4708 +{
4709 +       struct inode *h_inode;
4710 +
4711 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4712 +       fsstack_copy_attr_times(inode, h_inode);
4713 +       fsstack_copy_inode_size(inode, h_inode);
4714 +}
4715 +
4716 +void au_cpup_attr_nlink(struct inode *inode, int force)
4717 +{
4718 +       struct inode *h_inode;
4719 +       struct super_block *sb;
4720 +       aufs_bindex_t bindex, bbot;
4721 +
4722 +       sb = inode->i_sb;
4723 +       bindex = au_ibtop(inode);
4724 +       h_inode = au_h_iptr(inode, bindex);
4725 +       if (!force
4726 +           && !S_ISDIR(h_inode->i_mode)
4727 +           && au_opt_test(au_mntflags(sb), PLINK)
4728 +           && au_plink_test(inode))
4729 +               return;
4730 +
4731 +       /*
4732 +        * 0 can happen in revalidating.
4733 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4734 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4735 +        * case.
4736 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4737 +        *       the incorrect link count.
4738 +        */
4739 +       set_nlink(inode, h_inode->i_nlink);
4740 +
4741 +       /*
4742 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4743 +        * it may includes whplink directory.
4744 +        */
4745 +       if (S_ISDIR(h_inode->i_mode)) {
4746 +               bbot = au_ibbot(inode);
4747 +               for (bindex++; bindex <= bbot; bindex++) {
4748 +                       h_inode = au_h_iptr(inode, bindex);
4749 +                       if (h_inode)
4750 +                               au_add_nlink(inode, h_inode);
4751 +               }
4752 +       }
4753 +}
4754 +
4755 +void au_cpup_attr_changeable(struct inode *inode)
4756 +{
4757 +       struct inode *h_inode;
4758 +
4759 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4760 +       inode->i_mode = h_inode->i_mode;
4761 +       inode->i_uid = h_inode->i_uid;
4762 +       inode->i_gid = h_inode->i_gid;
4763 +       au_cpup_attr_timesizes(inode);
4764 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4765 +}
4766 +
4767 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4768 +{
4769 +       struct au_iinfo *iinfo = au_ii(inode);
4770 +
4771 +       IiMustWriteLock(inode);
4772 +
4773 +       iinfo->ii_higen = h_inode->i_generation;
4774 +       iinfo->ii_hsb1 = h_inode->i_sb;
4775 +}
4776 +
4777 +void au_cpup_attr_all(struct inode *inode, int force)
4778 +{
4779 +       struct inode *h_inode;
4780 +
4781 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4782 +       au_cpup_attr_changeable(inode);
4783 +       if (inode->i_nlink > 0)
4784 +               au_cpup_attr_nlink(inode, force);
4785 +       inode->i_rdev = h_inode->i_rdev;
4786 +       inode->i_blkbits = h_inode->i_blkbits;
4787 +       au_cpup_igen(inode, h_inode);
4788 +}
4789 +
4790 +/* ---------------------------------------------------------------------- */
4791 +
4792 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4793 +
4794 +/* keep the timestamps of the parent dir when cpup */
4795 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4796 +                   struct path *h_path)
4797 +{
4798 +       struct inode *h_inode;
4799 +
4800 +       dt->dt_dentry = dentry;
4801 +       dt->dt_h_path = *h_path;
4802 +       h_inode = d_inode(h_path->dentry);
4803 +       dt->dt_atime = h_inode->i_atime;
4804 +       dt->dt_mtime = h_inode->i_mtime;
4805 +       /* smp_mb(); */
4806 +}
4807 +
4808 +void au_dtime_revert(struct au_dtime *dt)
4809 +{
4810 +       struct iattr attr;
4811 +       int err;
4812 +
4813 +       attr.ia_atime = dt->dt_atime;
4814 +       attr.ia_mtime = dt->dt_mtime;
4815 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4816 +               | ATTR_ATIME | ATTR_ATIME_SET;
4817 +
4818 +       /* no delegation since this is a directory */
4819 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4820 +       if (unlikely(err))
4821 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4822 +}
4823 +
4824 +/* ---------------------------------------------------------------------- */
4825 +
4826 +/* internal use only */
4827 +struct au_cpup_reg_attr {
4828 +       int             valid;
4829 +       struct kstat    st;
4830 +       unsigned int    iflags; /* inode->i_flags */
4831 +};
4832 +
4833 +static noinline_for_stack
4834 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4835 +              struct au_cpup_reg_attr *h_src_attr)
4836 +{
4837 +       int err, sbits, icex;
4838 +       unsigned int mnt_flags;
4839 +       unsigned char verbose;
4840 +       struct iattr ia;
4841 +       struct path h_path;
4842 +       struct inode *h_isrc, *h_idst;
4843 +       struct kstat *h_st;
4844 +       struct au_branch *br;
4845 +
4846 +       h_path.dentry = au_h_dptr(dst, bindex);
4847 +       h_idst = d_inode(h_path.dentry);
4848 +       br = au_sbr(dst->d_sb, bindex);
4849 +       h_path.mnt = au_br_mnt(br);
4850 +       h_isrc = d_inode(h_src);
4851 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4852 +               | ATTR_ATIME | ATTR_MTIME
4853 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4854 +       if (h_src_attr && h_src_attr->valid) {
4855 +               h_st = &h_src_attr->st;
4856 +               ia.ia_uid = h_st->uid;
4857 +               ia.ia_gid = h_st->gid;
4858 +               ia.ia_atime = h_st->atime;
4859 +               ia.ia_mtime = h_st->mtime;
4860 +               if (h_idst->i_mode != h_st->mode
4861 +                   && !S_ISLNK(h_idst->i_mode)) {
4862 +                       ia.ia_valid |= ATTR_MODE;
4863 +                       ia.ia_mode = h_st->mode;
4864 +               }
4865 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4866 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4867 +       } else {
4868 +               ia.ia_uid = h_isrc->i_uid;
4869 +               ia.ia_gid = h_isrc->i_gid;
4870 +               ia.ia_atime = h_isrc->i_atime;
4871 +               ia.ia_mtime = h_isrc->i_mtime;
4872 +               if (h_idst->i_mode != h_isrc->i_mode
4873 +                   && !S_ISLNK(h_idst->i_mode)) {
4874 +                       ia.ia_valid |= ATTR_MODE;
4875 +                       ia.ia_mode = h_isrc->i_mode;
4876 +               }
4877 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4878 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4879 +       }
4880 +       /* no delegation since it is just created */
4881 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4882 +
4883 +       /* is this nfs only? */
4884 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4885 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4886 +               ia.ia_mode = h_isrc->i_mode;
4887 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4888 +       }
4889 +
4890 +       icex = br->br_perm & AuBrAttr_ICEX;
4891 +       if (!err) {
4892 +               mnt_flags = au_mntflags(dst->d_sb);
4893 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4894 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4895 +       }
4896 +
4897 +       return err;
4898 +}
4899 +
4900 +/* ---------------------------------------------------------------------- */
4901 +
4902 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4903 +                          char *buf, unsigned long blksize)
4904 +{
4905 +       int err;
4906 +       size_t sz, rbytes, wbytes;
4907 +       unsigned char all_zero;
4908 +       char *p, *zp;
4909 +       struct inode *h_inode;
4910 +       /* reduce stack usage */
4911 +       struct iattr *ia;
4912 +
4913 +       zp = page_address(ZERO_PAGE(0));
4914 +       if (unlikely(!zp))
4915 +               return -ENOMEM; /* possible? */
4916 +
4917 +       err = 0;
4918 +       all_zero = 0;
4919 +       while (len) {
4920 +               AuDbg("len %lld\n", len);
4921 +               sz = blksize;
4922 +               if (len < blksize)
4923 +                       sz = len;
4924 +
4925 +               rbytes = 0;
4926 +               /* todo: signal_pending? */
4927 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4928 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4929 +                       err = rbytes;
4930 +               }
4931 +               if (unlikely(err < 0))
4932 +                       break;
4933 +
4934 +               all_zero = 0;
4935 +               if (len >= rbytes && rbytes == blksize)
4936 +                       all_zero = !memcmp(buf, zp, rbytes);
4937 +               if (!all_zero) {
4938 +                       wbytes = rbytes;
4939 +                       p = buf;
4940 +                       while (wbytes) {
4941 +                               size_t b;
4942 +
4943 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4944 +                               err = b;
4945 +                               /* todo: signal_pending? */
4946 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4947 +                                       continue;
4948 +                               if (unlikely(err < 0))
4949 +                                       break;
4950 +                               wbytes -= b;
4951 +                               p += b;
4952 +                       }
4953 +                       if (unlikely(err < 0))
4954 +                               break;
4955 +               } else {
4956 +                       loff_t res;
4957 +
4958 +                       AuLabel(hole);
4959 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4960 +                       err = res;
4961 +                       if (unlikely(res < 0))
4962 +                               break;
4963 +               }
4964 +               len -= rbytes;
4965 +               err = 0;
4966 +       }
4967 +
4968 +       /* the last block may be a hole */
4969 +       if (!err && all_zero) {
4970 +               AuLabel(last hole);
4971 +
4972 +               err = 1;
4973 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4974 +                       /* nfs requires this step to make last hole */
4975 +                       /* is this only nfs? */
4976 +                       do {
4977 +                               /* todo: signal_pending? */
4978 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4979 +                       } while (err == -EAGAIN || err == -EINTR);
4980 +                       if (err == 1)
4981 +                               dst->f_pos--;
4982 +               }
4983 +
4984 +               if (err == 1) {
4985 +                       ia = (void *)buf;
4986 +                       ia->ia_size = dst->f_pos;
4987 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4988 +                       ia->ia_file = dst;
4989 +                       h_inode = file_inode(dst);
4990 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
4991 +                       /* no delegation since it is just created */
4992 +                       err = vfsub_notify_change(&dst->f_path, ia,
4993 +                                                 /*delegated*/NULL);
4994 +                       inode_unlock(h_inode);
4995 +               }
4996 +       }
4997 +
4998 +       return err;
4999 +}
5000 +
5001 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5002 +{
5003 +       int err;
5004 +       unsigned long blksize;
5005 +       unsigned char do_kfree;
5006 +       char *buf;
5007 +
5008 +       err = -ENOMEM;
5009 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
5010 +       if (!blksize || PAGE_SIZE < blksize)
5011 +               blksize = PAGE_SIZE;
5012 +       AuDbg("blksize %lu\n", blksize);
5013 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5014 +       if (do_kfree)
5015 +               buf = kmalloc(blksize, GFP_NOFS);
5016 +       else
5017 +               buf = (void *)__get_free_page(GFP_NOFS);
5018 +       if (unlikely(!buf))
5019 +               goto out;
5020 +
5021 +       if (len > (1 << 22))
5022 +               AuDbg("copying a large file %lld\n", (long long)len);
5023 +
5024 +       src->f_pos = 0;
5025 +       dst->f_pos = 0;
5026 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5027 +       if (do_kfree)
5028 +               au_delayed_kfree(buf);
5029 +       else
5030 +               au_delayed_free_page((unsigned long)buf);
5031 +
5032 +out:
5033 +       return err;
5034 +}
5035 +
5036 +/*
5037 + * to support a sparse file which is opened with O_APPEND,
5038 + * we need to close the file.
5039 + */
5040 +static int au_cp_regular(struct au_cp_generic *cpg)
5041 +{
5042 +       int err, i;
5043 +       enum { SRC, DST };
5044 +       struct {
5045 +               aufs_bindex_t bindex;
5046 +               unsigned int flags;
5047 +               struct dentry *dentry;
5048 +               int force_wr;
5049 +               struct file *file;
5050 +               void *label;
5051 +       } *f, file[] = {
5052 +               {
5053 +                       .bindex = cpg->bsrc,
5054 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5055 +                       .label = &&out
5056 +               },
5057 +               {
5058 +                       .bindex = cpg->bdst,
5059 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5060 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5061 +                       .label = &&out_src
5062 +               }
5063 +       };
5064 +       struct super_block *sb;
5065 +       struct inode *h_src_inode;
5066 +       struct task_struct *tsk = current;
5067 +
5068 +       /* bsrc branch can be ro/rw. */
5069 +       sb = cpg->dentry->d_sb;
5070 +       f = file;
5071 +       for (i = 0; i < 2; i++, f++) {
5072 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5073 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5074 +                                   /*file*/NULL, f->force_wr);
5075 +               err = PTR_ERR(f->file);
5076 +               if (IS_ERR(f->file))
5077 +                       goto *f->label;
5078 +       }
5079 +
5080 +       /* try stopping to update while we copyup */
5081 +       h_src_inode = d_inode(file[SRC].dentry);
5082 +       if (!au_test_nfs(h_src_inode->i_sb))
5083 +               IMustLock(h_src_inode);
5084 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
5085 +
5086 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5087 +       if (tsk->flags & PF_KTHREAD)
5088 +               __fput_sync(file[DST].file);
5089 +       else {
5090 +               WARN(1, "%pD\nPlease report this warning to aufs-users ML",
5091 +                    file[DST].file);
5092 +               fput(file[DST].file);
5093 +               /*
5094 +                * too bad.
5095 +                * we have to call both since we don't know which place the file
5096 +                * was added to.
5097 +                */
5098 +               task_work_run();
5099 +               flush_delayed_fput();
5100 +       }
5101 +       au_sbr_put(sb, file[DST].bindex);
5102 +
5103 +out_src:
5104 +       fput(file[SRC].file);
5105 +       au_sbr_put(sb, file[SRC].bindex);
5106 +out:
5107 +       return err;
5108 +}
5109 +
5110 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5111 +                             struct au_cpup_reg_attr *h_src_attr)
5112 +{
5113 +       int err, rerr;
5114 +       loff_t l;
5115 +       struct path h_path;
5116 +       struct inode *h_src_inode, *h_dst_inode;
5117 +
5118 +       err = 0;
5119 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5120 +       l = i_size_read(h_src_inode);
5121 +       if (cpg->len == -1 || l < cpg->len)
5122 +               cpg->len = l;
5123 +       if (cpg->len) {
5124 +               /* try stopping to update while we are referencing */
5125 +               inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5126 +               au_pin_hdir_unlock(cpg->pin);
5127 +
5128 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5129 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5130 +               h_src_attr->iflags = h_src_inode->i_flags;
5131 +               if (!au_test_nfs(h_src_inode->i_sb))
5132 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
5133 +               else {
5134 +                       inode_unlock(h_src_inode);
5135 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
5136 +                       inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5137 +               }
5138 +               if (unlikely(err)) {
5139 +                       inode_unlock(h_src_inode);
5140 +                       goto out;
5141 +               }
5142 +               h_src_attr->valid = 1;
5143 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5144 +                       err = au_cp_regular(cpg);
5145 +                       inode_unlock(h_src_inode);
5146 +               } else {
5147 +                       inode_unlock(h_src_inode);
5148 +                       err = au_cp_regular(cpg);
5149 +               }
5150 +               rerr = au_pin_hdir_relock(cpg->pin);
5151 +               if (!err && rerr)
5152 +                       err = rerr;
5153 +       }
5154 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5155 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5156 +               h_dst_inode = d_inode(h_path.dentry);
5157 +               spin_lock(&h_dst_inode->i_lock);
5158 +               h_dst_inode->i_state |= I_LINKABLE;
5159 +               spin_unlock(&h_dst_inode->i_lock);
5160 +       }
5161 +
5162 +out:
5163 +       return err;
5164 +}
5165 +
5166 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5167 +                             struct inode *h_dir)
5168 +{
5169 +       int err, symlen;
5170 +       mm_segment_t old_fs;
5171 +       union {
5172 +               char *k;
5173 +               char __user *u;
5174 +       } sym;
5175 +
5176 +       err = -ENOMEM;
5177 +       sym.k = (void *)__get_free_page(GFP_NOFS);
5178 +       if (unlikely(!sym.k))
5179 +               goto out;
5180 +
5181 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
5182 +       old_fs = get_fs();
5183 +       set_fs(KERNEL_DS);
5184 +       symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
5185 +       err = symlen;
5186 +       set_fs(old_fs);
5187 +
5188 +       if (symlen > 0) {
5189 +               sym.k[symlen] = 0;
5190 +               err = vfsub_symlink(h_dir, h_path, sym.k);
5191 +       }
5192 +       au_delayed_free_page((unsigned long)sym.k);
5193 +
5194 +out:
5195 +       return err;
5196 +}
5197 +
5198 +/*
5199 + * regardless 'acl' option, reset all ACL.
5200 + * All ACL will be copied up later from the original entry on the lower branch.
5201 + */
5202 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5203 +{
5204 +       int err;
5205 +       struct dentry *h_dentry;
5206 +       struct inode *h_inode;
5207 +
5208 +       h_dentry = h_path->dentry;
5209 +       h_inode = d_inode(h_dentry);
5210 +       /* forget_all_cached_acls(h_inode)); */
5211 +       err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5212 +       AuTraceErr(err);
5213 +       if (err == -EOPNOTSUPP)
5214 +               err = 0;
5215 +       if (!err)
5216 +               err = vfsub_acl_chmod(h_inode, mode);
5217 +
5218 +       AuTraceErr(err);
5219 +       return err;
5220 +}
5221 +
5222 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5223 +                         struct inode *h_dir, struct path *h_path)
5224 +{
5225 +       int err;
5226 +       struct inode *dir, *inode;
5227 +
5228 +       err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5229 +       AuTraceErr(err);
5230 +       if (err == -EOPNOTSUPP)
5231 +               err = 0;
5232 +       if (unlikely(err))
5233 +               goto out;
5234 +
5235 +       /*
5236 +        * strange behaviour from the users view,
5237 +        * particularry setattr case
5238 +        */
5239 +       dir = d_inode(dst_parent);
5240 +       if (au_ibtop(dir) == cpg->bdst)
5241 +               au_cpup_attr_nlink(dir, /*force*/1);
5242 +       inode = d_inode(cpg->dentry);
5243 +       au_cpup_attr_nlink(inode, /*force*/1);
5244 +
5245 +out:
5246 +       return err;
5247 +}
5248 +
5249 +static noinline_for_stack
5250 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5251 +              struct au_cpup_reg_attr *h_src_attr)
5252 +{
5253 +       int err;
5254 +       umode_t mode;
5255 +       unsigned int mnt_flags;
5256 +       unsigned char isdir, isreg, force;
5257 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5258 +       struct au_dtime dt;
5259 +       struct path h_path;
5260 +       struct dentry *h_src, *h_dst, *h_parent;
5261 +       struct inode *h_inode, *h_dir;
5262 +       struct super_block *sb;
5263 +
5264 +       /* bsrc branch can be ro/rw. */
5265 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5266 +       h_inode = d_inode(h_src);
5267 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5268 +
5269 +       /* try stopping to be referenced while we are creating */
5270 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5271 +       if (au_ftest_cpup(cpg->flags, RENAME))
5272 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5273 +                                 AUFS_WH_PFX_LEN));
5274 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5275 +       h_dir = d_inode(h_parent);
5276 +       IMustLock(h_dir);
5277 +       AuDebugOn(h_parent != h_dst->d_parent);
5278 +
5279 +       sb = cpg->dentry->d_sb;
5280 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5281 +       if (do_dt) {
5282 +               h_path.dentry = h_parent;
5283 +               au_dtime_store(&dt, dst_parent, &h_path);
5284 +       }
5285 +       h_path.dentry = h_dst;
5286 +
5287 +       isreg = 0;
5288 +       isdir = 0;
5289 +       mode = h_inode->i_mode;
5290 +       switch (mode & S_IFMT) {
5291 +       case S_IFREG:
5292 +               isreg = 1;
5293 +               err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
5294 +                                  /*want_excl*/true);
5295 +               if (!err)
5296 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5297 +               break;
5298 +       case S_IFDIR:
5299 +               isdir = 1;
5300 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5301 +               if (!err)
5302 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5303 +               break;
5304 +       case S_IFLNK:
5305 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5306 +               break;
5307 +       case S_IFCHR:
5308 +       case S_IFBLK:
5309 +               AuDebugOn(!capable(CAP_MKNOD));
5310 +               /*FALLTHROUGH*/
5311 +       case S_IFIFO:
5312 +       case S_IFSOCK:
5313 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5314 +               break;
5315 +       default:
5316 +               AuIOErr("Unknown inode type 0%o\n", mode);
5317 +               err = -EIO;
5318 +       }
5319 +       if (!err)
5320 +               err = au_reset_acl(h_dir, &h_path, mode);
5321 +
5322 +       mnt_flags = au_mntflags(sb);
5323 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5324 +           && !isdir
5325 +           && au_opt_test(mnt_flags, XINO)
5326 +           && (h_inode->i_nlink == 1
5327 +               || (h_inode->i_state & I_LINKABLE))
5328 +           /* todo: unnecessary? */
5329 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5330 +           && cpg->bdst < cpg->bsrc
5331 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5332 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5333 +               /* ignore this error */
5334 +
5335 +       if (!err) {
5336 +               force = 0;
5337 +               if (isreg) {
5338 +                       force = !!cpg->len;
5339 +                       if (cpg->len == -1)
5340 +                               force = !!i_size_read(h_inode);
5341 +               }
5342 +               au_fhsm_wrote(sb, cpg->bdst, force);
5343 +       }
5344 +
5345 +       if (do_dt)
5346 +               au_dtime_revert(&dt);
5347 +       return err;
5348 +}
5349 +
5350 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5351 +{
5352 +       int err;
5353 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5354 +       struct inode *h_dir;
5355 +       aufs_bindex_t bdst;
5356 +
5357 +       dentry = cpg->dentry;
5358 +       bdst = cpg->bdst;
5359 +       h_dentry = au_h_dptr(dentry, bdst);
5360 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5361 +               dget(h_dentry);
5362 +               au_set_h_dptr(dentry, bdst, NULL);
5363 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5364 +               if (!err)
5365 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5366 +               au_set_h_dptr(dentry, bdst, h_dentry);
5367 +       } else {
5368 +               err = 0;
5369 +               parent = dget_parent(dentry);
5370 +               h_parent = au_h_dptr(parent, bdst);
5371 +               dput(parent);
5372 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5373 +               if (IS_ERR(h_path->dentry))
5374 +                       err = PTR_ERR(h_path->dentry);
5375 +       }
5376 +       if (unlikely(err))
5377 +               goto out;
5378 +
5379 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5380 +       h_dir = d_inode(h_parent);
5381 +       IMustLock(h_dir);
5382 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5383 +       /* no delegation since it is just created */
5384 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5385 +                          /*flags*/0);
5386 +       dput(h_path->dentry);
5387 +
5388 +out:
5389 +       return err;
5390 +}
5391 +
5392 +/*
5393 + * copyup the @dentry from @bsrc to @bdst.
5394 + * the caller must set the both of lower dentries.
5395 + * @len is for truncating when it is -1 copyup the entire file.
5396 + * in link/rename cases, @dst_parent may be different from the real one.
5397 + * basic->bsrc can be larger than basic->bdst.
5398 + * aufs doesn't touch the credential so
5399 + * security_inode_copy_up{,_xattr}() are unnecrssary.
5400 + */
5401 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5402 +{
5403 +       int err, rerr;
5404 +       aufs_bindex_t old_ibtop;
5405 +       unsigned char isdir, plink;
5406 +       struct dentry *h_src, *h_dst, *h_parent;
5407 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5408 +       struct super_block *sb;
5409 +       struct au_branch *br;
5410 +       /* to reuduce stack size */
5411 +       struct {
5412 +               struct au_dtime dt;
5413 +               struct path h_path;
5414 +               struct au_cpup_reg_attr h_src_attr;
5415 +       } *a;
5416 +
5417 +       err = -ENOMEM;
5418 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5419 +       if (unlikely(!a))
5420 +               goto out;
5421 +       a->h_src_attr.valid = 0;
5422 +
5423 +       sb = cpg->dentry->d_sb;
5424 +       br = au_sbr(sb, cpg->bdst);
5425 +       a->h_path.mnt = au_br_mnt(br);
5426 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5427 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5428 +       h_dir = d_inode(h_parent);
5429 +       IMustLock(h_dir);
5430 +
5431 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5432 +       inode = d_inode(cpg->dentry);
5433 +
5434 +       if (!dst_parent)
5435 +               dst_parent = dget_parent(cpg->dentry);
5436 +       else
5437 +               dget(dst_parent);
5438 +
5439 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5440 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5441 +       if (dst_inode) {
5442 +               if (unlikely(!plink)) {
5443 +                       err = -EIO;
5444 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5445 +                               "but plink is disabled\n",
5446 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5447 +                       goto out_parent;
5448 +               }
5449 +
5450 +               if (dst_inode->i_nlink) {
5451 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5452 +
5453 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5454 +                       err = PTR_ERR(h_src);
5455 +                       if (IS_ERR(h_src))
5456 +                               goto out_parent;
5457 +                       if (unlikely(d_is_negative(h_src))) {
5458 +                               err = -EIO;
5459 +                               AuIOErr("i%lu exists on b%d "
5460 +                                       "but not pseudo-linked\n",
5461 +                                       inode->i_ino, cpg->bdst);
5462 +                               dput(h_src);
5463 +                               goto out_parent;
5464 +                       }
5465 +
5466 +                       if (do_dt) {
5467 +                               a->h_path.dentry = h_parent;
5468 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5469 +                       }
5470 +
5471 +                       a->h_path.dentry = h_dst;
5472 +                       delegated = NULL;
5473 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5474 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5475 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5476 +                       if (do_dt)
5477 +                               au_dtime_revert(&a->dt);
5478 +                       if (unlikely(err == -EWOULDBLOCK)) {
5479 +                               pr_warn("cannot retry for NFSv4 delegation"
5480 +                                       " for an internal link\n");
5481 +                               iput(delegated);
5482 +                       }
5483 +                       dput(h_src);
5484 +                       goto out_parent;
5485 +               } else
5486 +                       /* todo: cpup_wh_file? */
5487 +                       /* udba work */
5488 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5489 +       }
5490 +
5491 +       isdir = S_ISDIR(inode->i_mode);
5492 +       old_ibtop = au_ibtop(inode);
5493 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5494 +       if (unlikely(err))
5495 +               goto out_rev;
5496 +       dst_inode = d_inode(h_dst);
5497 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5498 +       /* todo: necessary? */
5499 +       /* au_pin_hdir_unlock(cpg->pin); */
5500 +
5501 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5502 +       if (unlikely(err)) {
5503 +               /* todo: necessary? */
5504 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5505 +               inode_unlock(dst_inode);
5506 +               goto out_rev;
5507 +       }
5508 +
5509 +       if (cpg->bdst < old_ibtop) {
5510 +               if (S_ISREG(inode->i_mode)) {
5511 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5512 +                       if (unlikely(err)) {
5513 +                               /* ignore an error */
5514 +                               /* au_pin_hdir_relock(cpg->pin); */
5515 +                               inode_unlock(dst_inode);
5516 +                               goto out_rev;
5517 +                       }
5518 +               }
5519 +               au_set_ibtop(inode, cpg->bdst);
5520 +       } else
5521 +               au_set_ibbot(inode, cpg->bdst);
5522 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5523 +                     au_hi_flags(inode, isdir));
5524 +
5525 +       /* todo: necessary? */
5526 +       /* err = au_pin_hdir_relock(cpg->pin); */
5527 +       inode_unlock(dst_inode);
5528 +       if (unlikely(err))
5529 +               goto out_rev;
5530 +
5531 +       src_inode = d_inode(h_src);
5532 +       if (!isdir
5533 +           && (src_inode->i_nlink > 1
5534 +               || src_inode->i_state & I_LINKABLE)
5535 +           && plink)
5536 +               au_plink_append(inode, cpg->bdst, h_dst);
5537 +
5538 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5539 +               a->h_path.dentry = h_dst;
5540 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5541 +       }
5542 +       if (!err)
5543 +               goto out_parent; /* success */
5544 +
5545 +       /* revert */
5546 +out_rev:
5547 +       a->h_path.dentry = h_parent;
5548 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5549 +       a->h_path.dentry = h_dst;
5550 +       rerr = 0;
5551 +       if (d_is_positive(h_dst)) {
5552 +               if (!isdir) {
5553 +                       /* no delegation since it is just created */
5554 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5555 +                                           /*delegated*/NULL, /*force*/0);
5556 +               } else
5557 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5558 +       }
5559 +       au_dtime_revert(&a->dt);
5560 +       if (rerr) {
5561 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5562 +               err = -EIO;
5563 +       }
5564 +out_parent:
5565 +       dput(dst_parent);
5566 +       au_delayed_kfree(a);
5567 +out:
5568 +       return err;
5569 +}
5570 +
5571 +#if 0 /* reserved */
5572 +struct au_cpup_single_args {
5573 +       int *errp;
5574 +       struct au_cp_generic *cpg;
5575 +       struct dentry *dst_parent;
5576 +};
5577 +
5578 +static void au_call_cpup_single(void *args)
5579 +{
5580 +       struct au_cpup_single_args *a = args;
5581 +
5582 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5583 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5584 +       au_pin_hdir_release(a->cpg->pin);
5585 +}
5586 +#endif
5587 +
5588 +/*
5589 + * prevent SIGXFSZ in copy-up.
5590 + * testing CAP_MKNOD is for generic fs,
5591 + * but CAP_FSETID is for xfs only, currently.
5592 + */
5593 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5594 +{
5595 +       int do_sio;
5596 +       struct super_block *sb;
5597 +       struct inode *h_dir;
5598 +
5599 +       do_sio = 0;
5600 +       sb = au_pinned_parent(pin)->d_sb;
5601 +       if (!au_wkq_test()
5602 +           && (!au_sbi(sb)->si_plink_maint_pid
5603 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5604 +               switch (mode & S_IFMT) {
5605 +               case S_IFREG:
5606 +                       /* no condition about RLIMIT_FSIZE and the file size */
5607 +                       do_sio = 1;
5608 +                       break;
5609 +               case S_IFCHR:
5610 +               case S_IFBLK:
5611 +                       do_sio = !capable(CAP_MKNOD);
5612 +                       break;
5613 +               }
5614 +               if (!do_sio)
5615 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5616 +                                 && !capable(CAP_FSETID));
5617 +               /* this workaround may be removed in the future */
5618 +               if (!do_sio) {
5619 +                       h_dir = au_pinned_h_dir(pin);
5620 +                       do_sio = h_dir->i_mode & S_ISVTX;
5621 +               }
5622 +       }
5623 +
5624 +       return do_sio;
5625 +}
5626 +
5627 +#if 0 /* reserved */
5628 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5629 +{
5630 +       int err, wkq_err;
5631 +       struct dentry *h_dentry;
5632 +
5633 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5634 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5635 +               err = au_cpup_single(cpg, dst_parent);
5636 +       else {
5637 +               struct au_cpup_single_args args = {
5638 +                       .errp           = &err,
5639 +                       .cpg            = cpg,
5640 +                       .dst_parent     = dst_parent
5641 +               };
5642 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5643 +               if (unlikely(wkq_err))
5644 +                       err = wkq_err;
5645 +       }
5646 +
5647 +       return err;
5648 +}
5649 +#endif
5650 +
5651 +/*
5652 + * copyup the @dentry from the first active lower branch to @bdst,
5653 + * using au_cpup_single().
5654 + */
5655 +static int au_cpup_simple(struct au_cp_generic *cpg)
5656 +{
5657 +       int err;
5658 +       unsigned int flags_orig;
5659 +       struct dentry *dentry;
5660 +
5661 +       AuDebugOn(cpg->bsrc < 0);
5662 +
5663 +       dentry = cpg->dentry;
5664 +       DiMustWriteLock(dentry);
5665 +
5666 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5667 +       if (!err) {
5668 +               flags_orig = cpg->flags;
5669 +               au_fset_cpup(cpg->flags, RENAME);
5670 +               err = au_cpup_single(cpg, NULL);
5671 +               cpg->flags = flags_orig;
5672 +               if (!err)
5673 +                       return 0; /* success */
5674 +
5675 +               /* revert */
5676 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5677 +               au_set_dbtop(dentry, cpg->bsrc);
5678 +       }
5679 +
5680 +       return err;
5681 +}
5682 +
5683 +struct au_cpup_simple_args {
5684 +       int *errp;
5685 +       struct au_cp_generic *cpg;
5686 +};
5687 +
5688 +static void au_call_cpup_simple(void *args)
5689 +{
5690 +       struct au_cpup_simple_args *a = args;
5691 +
5692 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5693 +       *a->errp = au_cpup_simple(a->cpg);
5694 +       au_pin_hdir_release(a->cpg->pin);
5695 +}
5696 +
5697 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5698 +{
5699 +       int err, wkq_err;
5700 +       struct dentry *dentry, *parent;
5701 +       struct file *h_file;
5702 +       struct inode *h_dir;
5703 +
5704 +       dentry = cpg->dentry;
5705 +       h_file = NULL;
5706 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5707 +               AuDebugOn(cpg->bsrc < 0);
5708 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5709 +               err = PTR_ERR(h_file);
5710 +               if (IS_ERR(h_file))
5711 +                       goto out;
5712 +       }
5713 +
5714 +       parent = dget_parent(dentry);
5715 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5716 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5717 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5718 +               err = au_cpup_simple(cpg);
5719 +       else {
5720 +               struct au_cpup_simple_args args = {
5721 +                       .errp           = &err,
5722 +                       .cpg            = cpg
5723 +               };
5724 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5725 +               if (unlikely(wkq_err))
5726 +                       err = wkq_err;
5727 +       }
5728 +
5729 +       dput(parent);
5730 +       if (h_file)
5731 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5732 +
5733 +out:
5734 +       return err;
5735 +}
5736 +
5737 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5738 +{
5739 +       aufs_bindex_t bsrc, bbot;
5740 +       struct dentry *dentry, *h_dentry;
5741 +
5742 +       if (cpg->bsrc < 0) {
5743 +               dentry = cpg->dentry;
5744 +               bbot = au_dbbot(dentry);
5745 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5746 +                       h_dentry = au_h_dptr(dentry, bsrc);
5747 +                       if (h_dentry) {
5748 +                               AuDebugOn(d_is_negative(h_dentry));
5749 +                               break;
5750 +                       }
5751 +               }
5752 +               AuDebugOn(bsrc > bbot);
5753 +               cpg->bsrc = bsrc;
5754 +       }
5755 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5756 +       return au_do_sio_cpup_simple(cpg);
5757 +}
5758 +
5759 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5760 +{
5761 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5762 +       return au_do_sio_cpup_simple(cpg);
5763 +}
5764 +
5765 +/* ---------------------------------------------------------------------- */
5766 +
5767 +/*
5768 + * copyup the deleted file for writing.
5769 + */
5770 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5771 +                        struct file *file)
5772 +{
5773 +       int err;
5774 +       unsigned int flags_orig;
5775 +       aufs_bindex_t bsrc_orig;
5776 +       struct au_dinfo *dinfo;
5777 +       struct {
5778 +               struct au_hdentry *hd;
5779 +               struct dentry *h_dentry;
5780 +       } hdst, hsrc;
5781 +
5782 +       dinfo = au_di(cpg->dentry);
5783 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5784 +
5785 +       bsrc_orig = cpg->bsrc;
5786 +       cpg->bsrc = dinfo->di_btop;
5787 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5788 +       hdst.h_dentry = hdst.hd->hd_dentry;
5789 +       hdst.hd->hd_dentry = wh_dentry;
5790 +       dinfo->di_btop = cpg->bdst;
5791 +
5792 +       hsrc.h_dentry = NULL;
5793 +       if (file) {
5794 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5795 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5796 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5797 +       }
5798 +       flags_orig = cpg->flags;
5799 +       cpg->flags = !AuCpup_DTIME;
5800 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5801 +       cpg->flags = flags_orig;
5802 +       if (file) {
5803 +               if (!err)
5804 +                       err = au_reopen_nondir(file);
5805 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5806 +       }
5807 +       hdst.hd->hd_dentry = hdst.h_dentry;
5808 +       dinfo->di_btop = cpg->bsrc;
5809 +       cpg->bsrc = bsrc_orig;
5810 +
5811 +       return err;
5812 +}
5813 +
5814 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5815 +{
5816 +       int err;
5817 +       aufs_bindex_t bdst;
5818 +       struct au_dtime dt;
5819 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5820 +       struct au_branch *br;
5821 +       struct path h_path;
5822 +
5823 +       dentry = cpg->dentry;
5824 +       bdst = cpg->bdst;
5825 +       br = au_sbr(dentry->d_sb, bdst);
5826 +       parent = dget_parent(dentry);
5827 +       h_parent = au_h_dptr(parent, bdst);
5828 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5829 +       err = PTR_ERR(wh_dentry);
5830 +       if (IS_ERR(wh_dentry))
5831 +               goto out;
5832 +
5833 +       h_path.dentry = h_parent;
5834 +       h_path.mnt = au_br_mnt(br);
5835 +       au_dtime_store(&dt, parent, &h_path);
5836 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5837 +       if (unlikely(err))
5838 +               goto out_wh;
5839 +
5840 +       dget(wh_dentry);
5841 +       h_path.dentry = wh_dentry;
5842 +       if (!d_is_dir(wh_dentry)) {
5843 +               /* no delegation since it is just created */
5844 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5845 +                                  /*delegated*/NULL, /*force*/0);
5846 +       } else
5847 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5848 +       if (unlikely(err)) {
5849 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5850 +                       wh_dentry, err);
5851 +               err = -EIO;
5852 +       }
5853 +       au_dtime_revert(&dt);
5854 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5855 +
5856 +out_wh:
5857 +       dput(wh_dentry);
5858 +out:
5859 +       dput(parent);
5860 +       return err;
5861 +}
5862 +
5863 +struct au_cpup_wh_args {
5864 +       int *errp;
5865 +       struct au_cp_generic *cpg;
5866 +       struct file *file;
5867 +};
5868 +
5869 +static void au_call_cpup_wh(void *args)
5870 +{
5871 +       struct au_cpup_wh_args *a = args;
5872 +
5873 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5874 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5875 +       au_pin_hdir_release(a->cpg->pin);
5876 +}
5877 +
5878 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5879 +{
5880 +       int err, wkq_err;
5881 +       aufs_bindex_t bdst;
5882 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5883 +       struct inode *dir, *h_dir, *h_tmpdir;
5884 +       struct au_wbr *wbr;
5885 +       struct au_pin wh_pin, *pin_orig;
5886 +
5887 +       dentry = cpg->dentry;
5888 +       bdst = cpg->bdst;
5889 +       parent = dget_parent(dentry);
5890 +       dir = d_inode(parent);
5891 +       h_orph = NULL;
5892 +       h_parent = NULL;
5893 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5894 +       h_tmpdir = h_dir;
5895 +       pin_orig = NULL;
5896 +       if (!h_dir->i_nlink) {
5897 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5898 +               h_orph = wbr->wbr_orph;
5899 +
5900 +               h_parent = dget(au_h_dptr(parent, bdst));
5901 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5902 +               h_tmpdir = d_inode(h_orph);
5903 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5904 +
5905 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
5906 +               /* todo: au_h_open_pre()? */
5907 +
5908 +               pin_orig = cpg->pin;
5909 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5910 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5911 +               cpg->pin = &wh_pin;
5912 +       }
5913 +
5914 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5915 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5916 +               err = au_cpup_wh(cpg, file);
5917 +       else {
5918 +               struct au_cpup_wh_args args = {
5919 +                       .errp   = &err,
5920 +                       .cpg    = cpg,
5921 +                       .file   = file
5922 +               };
5923 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5924 +               if (unlikely(wkq_err))
5925 +                       err = wkq_err;
5926 +       }
5927 +
5928 +       if (h_orph) {
5929 +               inode_unlock(h_tmpdir);
5930 +               /* todo: au_h_open_post()? */
5931 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5932 +               au_set_h_dptr(parent, bdst, h_parent);
5933 +               AuDebugOn(!pin_orig);
5934 +               cpg->pin = pin_orig;
5935 +       }
5936 +       iput(h_dir);
5937 +       dput(parent);
5938 +
5939 +       return err;
5940 +}
5941 +
5942 +/* ---------------------------------------------------------------------- */
5943 +
5944 +/*
5945 + * generic routine for both of copy-up and copy-down.
5946 + */
5947 +/* cf. revalidate function in file.c */
5948 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5949 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5950 +                        struct au_pin *pin,
5951 +                        struct dentry *h_parent, void *arg),
5952 +              void *arg)
5953 +{
5954 +       int err;
5955 +       struct au_pin pin;
5956 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
5957 +
5958 +       err = 0;
5959 +       parent = dget_parent(dentry);
5960 +       if (IS_ROOT(parent))
5961 +               goto out;
5962 +
5963 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5964 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5965 +
5966 +       /* do not use au_dpage */
5967 +       real_parent = parent;
5968 +       while (1) {
5969 +               dput(parent);
5970 +               parent = dget_parent(dentry);
5971 +               h_parent = au_h_dptr(parent, bdst);
5972 +               if (h_parent)
5973 +                       goto out; /* success */
5974 +
5975 +               /* find top dir which is necessary to cpup */
5976 +               do {
5977 +                       d = parent;
5978 +                       dput(parent);
5979 +                       parent = dget_parent(d);
5980 +                       di_read_lock_parent3(parent, !AuLock_IR);
5981 +                       h_parent = au_h_dptr(parent, bdst);
5982 +                       di_read_unlock(parent, !AuLock_IR);
5983 +               } while (!h_parent);
5984 +
5985 +               if (d != real_parent)
5986 +                       di_write_lock_child3(d);
5987 +
5988 +               /* somebody else might create while we were sleeping */
5989 +               h_dentry = au_h_dptr(d, bdst);
5990 +               if (!h_dentry || d_is_negative(h_dentry)) {
5991 +                       if (h_dentry)
5992 +                               au_update_dbtop(d);
5993 +
5994 +                       au_pin_set_dentry(&pin, d);
5995 +                       err = au_do_pin(&pin);
5996 +                       if (!err) {
5997 +                               err = cp(d, bdst, &pin, h_parent, arg);
5998 +                               au_unpin(&pin);
5999 +                       }
6000 +               }
6001 +
6002 +               if (d != real_parent)
6003 +                       di_write_unlock(d);
6004 +               if (unlikely(err))
6005 +                       break;
6006 +       }
6007 +
6008 +out:
6009 +       dput(parent);
6010 +       return err;
6011 +}
6012 +
6013 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6014 +                      struct au_pin *pin,
6015 +                      struct dentry *h_parent __maybe_unused,
6016 +                      void *arg __maybe_unused)
6017 +{
6018 +       struct au_cp_generic cpg = {
6019 +               .dentry = dentry,
6020 +               .bdst   = bdst,
6021 +               .bsrc   = -1,
6022 +               .len    = 0,
6023 +               .pin    = pin,
6024 +               .flags  = AuCpup_DTIME
6025 +       };
6026 +       return au_sio_cpup_simple(&cpg);
6027 +}
6028 +
6029 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6030 +{
6031 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6032 +}
6033 +
6034 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6035 +{
6036 +       int err;
6037 +       struct dentry *parent;
6038 +       struct inode *dir;
6039 +
6040 +       parent = dget_parent(dentry);
6041 +       dir = d_inode(parent);
6042 +       err = 0;
6043 +       if (au_h_iptr(dir, bdst))
6044 +               goto out;
6045 +
6046 +       di_read_unlock(parent, AuLock_IR);
6047 +       di_write_lock_parent(parent);
6048 +       /* someone else might change our inode while we were sleeping */
6049 +       if (!au_h_iptr(dir, bdst))
6050 +               err = au_cpup_dirs(dentry, bdst);
6051 +       di_downgrade_lock(parent, AuLock_IR);
6052 +
6053 +out:
6054 +       dput(parent);
6055 +       return err;
6056 +}
6057 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6058 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6059 +++ linux/fs/aufs/cpup.h        2017-03-02 14:24:13.856922210 +0100
6060 @@ -0,0 +1,94 @@
6061 +/*
6062 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6063 + *
6064 + * This program, aufs is free software; you can redistribute it and/or modify
6065 + * it under the terms of the GNU General Public License as published by
6066 + * the Free Software Foundation; either version 2 of the License, or
6067 + * (at your option) any later version.
6068 + *
6069 + * This program is distributed in the hope that it will be useful,
6070 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6071 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6072 + * GNU General Public License for more details.
6073 + *
6074 + * You should have received a copy of the GNU General Public License
6075 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6076 + */
6077 +
6078 +/*
6079 + * copy-up/down functions
6080 + */
6081 +
6082 +#ifndef __AUFS_CPUP_H__
6083 +#define __AUFS_CPUP_H__
6084 +
6085 +#ifdef __KERNEL__
6086 +
6087 +#include <linux/path.h>
6088 +
6089 +struct inode;
6090 +struct file;
6091 +struct au_pin;
6092 +
6093 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6094 +void au_cpup_attr_timesizes(struct inode *inode);
6095 +void au_cpup_attr_nlink(struct inode *inode, int force);
6096 +void au_cpup_attr_changeable(struct inode *inode);
6097 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6098 +void au_cpup_attr_all(struct inode *inode, int force);
6099 +
6100 +/* ---------------------------------------------------------------------- */
6101 +
6102 +struct au_cp_generic {
6103 +       struct dentry   *dentry;
6104 +       aufs_bindex_t   bdst, bsrc;
6105 +       loff_t          len;
6106 +       struct au_pin   *pin;
6107 +       unsigned int    flags;
6108 +};
6109 +
6110 +/* cpup flags */
6111 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6112 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6113 +                                                  for link(2) */
6114 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6115 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6116 +                                                  cpup */
6117 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6118 +                                                  existing entry */
6119 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6120 +                                                  the branch is marked as RO */
6121 +
6122 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6123 +#define au_fset_cpup(flags, name) \
6124 +       do { (flags) |= AuCpup_##name; } while (0)
6125 +#define au_fclr_cpup(flags, name) \
6126 +       do { (flags) &= ~AuCpup_##name; } while (0)
6127 +
6128 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6129 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6130 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6131 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6132 +
6133 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6134 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6135 +                        struct au_pin *pin,
6136 +                        struct dentry *h_parent, void *arg),
6137 +              void *arg);
6138 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6139 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6140 +
6141 +/* ---------------------------------------------------------------------- */
6142 +
6143 +/* keep timestamps when copyup */
6144 +struct au_dtime {
6145 +       struct dentry *dt_dentry;
6146 +       struct path dt_h_path;
6147 +       struct timespec dt_atime, dt_mtime;
6148 +};
6149 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6150 +                   struct path *h_path);
6151 +void au_dtime_revert(struct au_dtime *dt);
6152 +
6153 +#endif /* __KERNEL__ */
6154 +#endif /* __AUFS_CPUP_H__ */
6155 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6156 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6157 +++ linux/fs/aufs/dbgaufs.c     2017-03-02 14:24:13.856922210 +0100
6158 @@ -0,0 +1,438 @@
6159 +/*
6160 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6161 + *
6162 + * This program, aufs is free software; you can redistribute it and/or modify
6163 + * it under the terms of the GNU General Public License as published by
6164 + * the Free Software Foundation; either version 2 of the License, or
6165 + * (at your option) any later version.
6166 + *
6167 + * This program is distributed in the hope that it will be useful,
6168 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6169 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6170 + * GNU General Public License for more details.
6171 + *
6172 + * You should have received a copy of the GNU General Public License
6173 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6174 + */
6175 +
6176 +/*
6177 + * debugfs interface
6178 + */
6179 +
6180 +#include <linux/debugfs.h>
6181 +#include "aufs.h"
6182 +
6183 +#ifndef CONFIG_SYSFS
6184 +#error DEBUG_FS depends upon SYSFS
6185 +#endif
6186 +
6187 +static struct dentry *dbgaufs;
6188 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6189 +
6190 +/* 20 is max digits length of ulong 64 */
6191 +struct dbgaufs_arg {
6192 +       int n;
6193 +       char a[20 * 4];
6194 +};
6195 +
6196 +/*
6197 + * common function for all XINO files
6198 + */
6199 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6200 +                             struct file *file)
6201 +{
6202 +       au_delayed_kfree(file->private_data);
6203 +       return 0;
6204 +}
6205 +
6206 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6207 +{
6208 +       int err;
6209 +       struct kstat st;
6210 +       struct dbgaufs_arg *p;
6211 +
6212 +       err = -ENOMEM;
6213 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6214 +       if (unlikely(!p))
6215 +               goto out;
6216 +
6217 +       err = 0;
6218 +       p->n = 0;
6219 +       file->private_data = p;
6220 +       if (!xf)
6221 +               goto out;
6222 +
6223 +       err = vfs_getattr(&xf->f_path, &st);
6224 +       if (!err) {
6225 +               if (do_fcnt)
6226 +                       p->n = snprintf
6227 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
6228 +                                (long)file_count(xf), st.blocks, st.blksize,
6229 +                                (long long)st.size);
6230 +               else
6231 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
6232 +                                       st.blocks, st.blksize,
6233 +                                       (long long)st.size);
6234 +               AuDebugOn(p->n >= sizeof(p->a));
6235 +       } else {
6236 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6237 +               err = 0;
6238 +       }
6239 +
6240 +out:
6241 +       return err;
6242 +
6243 +}
6244 +
6245 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6246 +                              size_t count, loff_t *ppos)
6247 +{
6248 +       struct dbgaufs_arg *p;
6249 +
6250 +       p = file->private_data;
6251 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6252 +}
6253 +
6254 +/* ---------------------------------------------------------------------- */
6255 +
6256 +struct dbgaufs_plink_arg {
6257 +       int n;
6258 +       char a[];
6259 +};
6260 +
6261 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6262 +                                struct file *file)
6263 +{
6264 +       au_delayed_free_page((unsigned long)file->private_data);
6265 +       return 0;
6266 +}
6267 +
6268 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6269 +{
6270 +       int err, i, limit;
6271 +       unsigned long n, sum;
6272 +       struct dbgaufs_plink_arg *p;
6273 +       struct au_sbinfo *sbinfo;
6274 +       struct super_block *sb;
6275 +       struct au_sphlhead *sphl;
6276 +
6277 +       err = -ENOMEM;
6278 +       p = (void *)get_zeroed_page(GFP_NOFS);
6279 +       if (unlikely(!p))
6280 +               goto out;
6281 +
6282 +       err = -EFBIG;
6283 +       sbinfo = inode->i_private;
6284 +       sb = sbinfo->si_sb;
6285 +       si_noflush_read_lock(sb);
6286 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6287 +               limit = PAGE_SIZE - sizeof(p->n);
6288 +
6289 +               /* the number of buckets */
6290 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6291 +               p->n += n;
6292 +               limit -= n;
6293 +
6294 +               sum = 0;
6295 +               for (i = 0, sphl = sbinfo->si_plink;
6296 +                    i < AuPlink_NHASH;
6297 +                    i++, sphl++) {
6298 +                       n = au_sphl_count(sphl);
6299 +                       sum += n;
6300 +
6301 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6302 +                       p->n += n;
6303 +                       limit -= n;
6304 +                       if (unlikely(limit <= 0))
6305 +                               goto out_free;
6306 +               }
6307 +               p->a[p->n - 1] = '\n';
6308 +
6309 +               /* the sum of plinks */
6310 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6311 +               p->n += n;
6312 +               limit -= n;
6313 +               if (unlikely(limit <= 0))
6314 +                       goto out_free;
6315 +       } else {
6316 +#define str "1\n0\n0\n"
6317 +               p->n = sizeof(str) - 1;
6318 +               strcpy(p->a, str);
6319 +#undef str
6320 +       }
6321 +       si_read_unlock(sb);
6322 +
6323 +       err = 0;
6324 +       file->private_data = p;
6325 +       goto out; /* success */
6326 +
6327 +out_free:
6328 +       au_delayed_free_page((unsigned long)p);
6329 +out:
6330 +       return err;
6331 +}
6332 +
6333 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6334 +                                 size_t count, loff_t *ppos)
6335 +{
6336 +       struct dbgaufs_plink_arg *p;
6337 +
6338 +       p = file->private_data;
6339 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6340 +}
6341 +
6342 +static const struct file_operations dbgaufs_plink_fop = {
6343 +       .owner          = THIS_MODULE,
6344 +       .open           = dbgaufs_plink_open,
6345 +       .release        = dbgaufs_plink_release,
6346 +       .read           = dbgaufs_plink_read
6347 +};
6348 +
6349 +/* ---------------------------------------------------------------------- */
6350 +
6351 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6352 +{
6353 +       int err;
6354 +       struct au_sbinfo *sbinfo;
6355 +       struct super_block *sb;
6356 +
6357 +       sbinfo = inode->i_private;
6358 +       sb = sbinfo->si_sb;
6359 +       si_noflush_read_lock(sb);
6360 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6361 +       si_read_unlock(sb);
6362 +       return err;
6363 +}
6364 +
6365 +static const struct file_operations dbgaufs_xib_fop = {
6366 +       .owner          = THIS_MODULE,
6367 +       .open           = dbgaufs_xib_open,
6368 +       .release        = dbgaufs_xi_release,
6369 +       .read           = dbgaufs_xi_read
6370 +};
6371 +
6372 +/* ---------------------------------------------------------------------- */
6373 +
6374 +#define DbgaufsXi_PREFIX "xi"
6375 +
6376 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6377 +{
6378 +       int err;
6379 +       long l;
6380 +       struct au_sbinfo *sbinfo;
6381 +       struct super_block *sb;
6382 +       struct file *xf;
6383 +       struct qstr *name;
6384 +
6385 +       err = -ENOENT;
6386 +       xf = NULL;
6387 +       name = &file->f_path.dentry->d_name;
6388 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6389 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6390 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6391 +               goto out;
6392 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6393 +       if (unlikely(err))
6394 +               goto out;
6395 +
6396 +       sbinfo = inode->i_private;
6397 +       sb = sbinfo->si_sb;
6398 +       si_noflush_read_lock(sb);
6399 +       if (l <= au_sbbot(sb)) {
6400 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6401 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6402 +       } else
6403 +               err = -ENOENT;
6404 +       si_read_unlock(sb);
6405 +
6406 +out:
6407 +       return err;
6408 +}
6409 +
6410 +static const struct file_operations dbgaufs_xino_fop = {
6411 +       .owner          = THIS_MODULE,
6412 +       .open           = dbgaufs_xino_open,
6413 +       .release        = dbgaufs_xi_release,
6414 +       .read           = dbgaufs_xi_read
6415 +};
6416 +
6417 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6418 +{
6419 +       aufs_bindex_t bbot;
6420 +       struct au_branch *br;
6421 +       struct au_xino_file *xi;
6422 +
6423 +       if (!au_sbi(sb)->si_dbgaufs)
6424 +               return;
6425 +
6426 +       bbot = au_sbbot(sb);
6427 +       for (; bindex <= bbot; bindex++) {
6428 +               br = au_sbr(sb, bindex);
6429 +               xi = &br->br_xino;
6430 +               /* debugfs acquires the parent i_mutex */
6431 +               lockdep_off();
6432 +               debugfs_remove(xi->xi_dbgaufs);
6433 +               lockdep_on();
6434 +               xi->xi_dbgaufs = NULL;
6435 +       }
6436 +}
6437 +
6438 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6439 +{
6440 +       struct au_sbinfo *sbinfo;
6441 +       struct dentry *parent;
6442 +       struct au_branch *br;
6443 +       struct au_xino_file *xi;
6444 +       aufs_bindex_t bbot;
6445 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6446 +
6447 +       sbinfo = au_sbi(sb);
6448 +       parent = sbinfo->si_dbgaufs;
6449 +       if (!parent)
6450 +               return;
6451 +
6452 +       bbot = au_sbbot(sb);
6453 +       for (; bindex <= bbot; bindex++) {
6454 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6455 +               br = au_sbr(sb, bindex);
6456 +               xi = &br->br_xino;
6457 +               AuDebugOn(xi->xi_dbgaufs);
6458 +               /* debugfs acquires the parent i_mutex */
6459 +               lockdep_off();
6460 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6461 +                                                    sbinfo, &dbgaufs_xino_fop);
6462 +               lockdep_on();
6463 +               /* ignore an error */
6464 +               if (unlikely(!xi->xi_dbgaufs))
6465 +                       AuWarn1("failed %s under debugfs\n", name);
6466 +       }
6467 +}
6468 +
6469 +/* ---------------------------------------------------------------------- */
6470 +
6471 +#ifdef CONFIG_AUFS_EXPORT
6472 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6473 +{
6474 +       int err;
6475 +       struct au_sbinfo *sbinfo;
6476 +       struct super_block *sb;
6477 +
6478 +       sbinfo = inode->i_private;
6479 +       sb = sbinfo->si_sb;
6480 +       si_noflush_read_lock(sb);
6481 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6482 +       si_read_unlock(sb);
6483 +       return err;
6484 +}
6485 +
6486 +static const struct file_operations dbgaufs_xigen_fop = {
6487 +       .owner          = THIS_MODULE,
6488 +       .open           = dbgaufs_xigen_open,
6489 +       .release        = dbgaufs_xi_release,
6490 +       .read           = dbgaufs_xi_read
6491 +};
6492 +
6493 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6494 +{
6495 +       int err;
6496 +
6497 +       /*
6498 +        * This function is a dynamic '__init' function actually,
6499 +        * so the tiny check for si_rwsem is unnecessary.
6500 +        */
6501 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6502 +
6503 +       err = -EIO;
6504 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6505 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6506 +                &dbgaufs_xigen_fop);
6507 +       if (sbinfo->si_dbgaufs_xigen)
6508 +               err = 0;
6509 +
6510 +       return err;
6511 +}
6512 +#else
6513 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6514 +{
6515 +       return 0;
6516 +}
6517 +#endif /* CONFIG_AUFS_EXPORT */
6518 +
6519 +/* ---------------------------------------------------------------------- */
6520 +
6521 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6522 +{
6523 +       /*
6524 +        * This function is a dynamic '__fin' function actually,
6525 +        * so the tiny check for si_rwsem is unnecessary.
6526 +        */
6527 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6528 +
6529 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6530 +       sbinfo->si_dbgaufs = NULL;
6531 +       kobject_put(&sbinfo->si_kobj);
6532 +}
6533 +
6534 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6535 +{
6536 +       int err;
6537 +       char name[SysaufsSiNameLen];
6538 +
6539 +       /*
6540 +        * This function is a dynamic '__init' function actually,
6541 +        * so the tiny check for si_rwsem is unnecessary.
6542 +        */
6543 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6544 +
6545 +       err = -ENOENT;
6546 +       if (!dbgaufs) {
6547 +               AuErr1("/debug/aufs is uninitialized\n");
6548 +               goto out;
6549 +       }
6550 +
6551 +       err = -EIO;
6552 +       sysaufs_name(sbinfo, name);
6553 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6554 +       if (unlikely(!sbinfo->si_dbgaufs))
6555 +               goto out;
6556 +       kobject_get(&sbinfo->si_kobj);
6557 +
6558 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6559 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6560 +                &dbgaufs_xib_fop);
6561 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6562 +               goto out_dir;
6563 +
6564 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6565 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6566 +                &dbgaufs_plink_fop);
6567 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6568 +               goto out_dir;
6569 +
6570 +       err = dbgaufs_xigen_init(sbinfo);
6571 +       if (!err)
6572 +               goto out; /* success */
6573 +
6574 +out_dir:
6575 +       dbgaufs_si_fin(sbinfo);
6576 +out:
6577 +       return err;
6578 +}
6579 +
6580 +/* ---------------------------------------------------------------------- */
6581 +
6582 +void dbgaufs_fin(void)
6583 +{
6584 +       debugfs_remove(dbgaufs);
6585 +}
6586 +
6587 +int __init dbgaufs_init(void)
6588 +{
6589 +       int err;
6590 +
6591 +       err = -EIO;
6592 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6593 +       if (dbgaufs)
6594 +               err = 0;
6595 +       return err;
6596 +}
6597 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6598 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6599 +++ linux/fs/aufs/dbgaufs.h     2017-03-02 14:24:13.856922210 +0100
6600 @@ -0,0 +1,48 @@
6601 +/*
6602 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6603 + *
6604 + * This program, aufs is free software; you can redistribute it and/or modify
6605 + * it under the terms of the GNU General Public License as published by
6606 + * the Free Software Foundation; either version 2 of the License, or
6607 + * (at your option) any later version.
6608 + *
6609 + * This program is distributed in the hope that it will be useful,
6610 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6611 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6612 + * GNU General Public License for more details.
6613 + *
6614 + * You should have received a copy of the GNU General Public License
6615 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6616 + */
6617 +
6618 +/*
6619 + * debugfs interface
6620 + */
6621 +
6622 +#ifndef __DBGAUFS_H__
6623 +#define __DBGAUFS_H__
6624 +
6625 +#ifdef __KERNEL__
6626 +
6627 +struct super_block;
6628 +struct au_sbinfo;
6629 +
6630 +#ifdef CONFIG_DEBUG_FS
6631 +/* dbgaufs.c */
6632 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6633 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6634 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6635 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6636 +void dbgaufs_fin(void);
6637 +int __init dbgaufs_init(void);
6638 +#else
6639 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6640 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6641 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6642 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6643 +AuStubVoid(dbgaufs_fin, void)
6644 +AuStubInt0(__init dbgaufs_init, void)
6645 +#endif /* CONFIG_DEBUG_FS */
6646 +
6647 +#endif /* __KERNEL__ */
6648 +#endif /* __DBGAUFS_H__ */
6649 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6650 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6651 +++ linux/fs/aufs/dcsub.c       2017-03-02 14:24:13.856922210 +0100
6652 @@ -0,0 +1,225 @@
6653 +/*
6654 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6655 + *
6656 + * This program, aufs is free software; you can redistribute it and/or modify
6657 + * it under the terms of the GNU General Public License as published by
6658 + * the Free Software Foundation; either version 2 of the License, or
6659 + * (at your option) any later version.
6660 + *
6661 + * This program is distributed in the hope that it will be useful,
6662 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6663 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6664 + * GNU General Public License for more details.
6665 + *
6666 + * You should have received a copy of the GNU General Public License
6667 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6668 + */
6669 +
6670 +/*
6671 + * sub-routines for dentry cache
6672 + */
6673 +
6674 +#include "aufs.h"
6675 +
6676 +static void au_dpage_free(struct au_dpage *dpage)
6677 +{
6678 +       int i;
6679 +       struct dentry **p;
6680 +
6681 +       p = dpage->dentries;
6682 +       for (i = 0; i < dpage->ndentry; i++)
6683 +               dput(*p++);
6684 +       au_delayed_free_page((unsigned long)dpage->dentries);
6685 +}
6686 +
6687 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6688 +{
6689 +       int err;
6690 +       void *p;
6691 +
6692 +       err = -ENOMEM;
6693 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6694 +       if (unlikely(!dpages->dpages))
6695 +               goto out;
6696 +
6697 +       p = (void *)__get_free_page(gfp);
6698 +       if (unlikely(!p))
6699 +               goto out_dpages;
6700 +
6701 +       dpages->dpages[0].ndentry = 0;
6702 +       dpages->dpages[0].dentries = p;
6703 +       dpages->ndpage = 1;
6704 +       return 0; /* success */
6705 +
6706 +out_dpages:
6707 +       au_delayed_kfree(dpages->dpages);
6708 +out:
6709 +       return err;
6710 +}
6711 +
6712 +void au_dpages_free(struct au_dcsub_pages *dpages)
6713 +{
6714 +       int i;
6715 +       struct au_dpage *p;
6716 +
6717 +       p = dpages->dpages;
6718 +       for (i = 0; i < dpages->ndpage; i++)
6719 +               au_dpage_free(p++);
6720 +       au_delayed_kfree(dpages->dpages);
6721 +}
6722 +
6723 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6724 +                           struct dentry *dentry, gfp_t gfp)
6725 +{
6726 +       int err, sz;
6727 +       struct au_dpage *dpage;
6728 +       void *p;
6729 +
6730 +       dpage = dpages->dpages + dpages->ndpage - 1;
6731 +       sz = PAGE_SIZE / sizeof(dentry);
6732 +       if (unlikely(dpage->ndentry >= sz)) {
6733 +               AuLabel(new dpage);
6734 +               err = -ENOMEM;
6735 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6736 +               p = au_kzrealloc(dpages->dpages, sz,
6737 +                                sz + sizeof(*dpages->dpages), gfp,
6738 +                                /*may_shrink*/0);
6739 +               if (unlikely(!p))
6740 +                       goto out;
6741 +
6742 +               dpages->dpages = p;
6743 +               dpage = dpages->dpages + dpages->ndpage;
6744 +               p = (void *)__get_free_page(gfp);
6745 +               if (unlikely(!p))
6746 +                       goto out;
6747 +
6748 +               dpage->ndentry = 0;
6749 +               dpage->dentries = p;
6750 +               dpages->ndpage++;
6751 +       }
6752 +
6753 +       AuDebugOn(au_dcount(dentry) <= 0);
6754 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6755 +       return 0; /* success */
6756 +
6757 +out:
6758 +       return err;
6759 +}
6760 +
6761 +/* todo: BAD approach */
6762 +/* copied from linux/fs/dcache.c */
6763 +enum d_walk_ret {
6764 +       D_WALK_CONTINUE,
6765 +       D_WALK_QUIT,
6766 +       D_WALK_NORETRY,
6767 +       D_WALK_SKIP,
6768 +};
6769 +
6770 +extern void d_walk(struct dentry *parent, void *data,
6771 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6772 +                  void (*finish)(void *));
6773 +
6774 +struct ac_dpages_arg {
6775 +       int err;
6776 +       struct au_dcsub_pages *dpages;
6777 +       struct super_block *sb;
6778 +       au_dpages_test test;
6779 +       void *arg;
6780 +};
6781 +
6782 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6783 +{
6784 +       enum d_walk_ret ret;
6785 +       struct ac_dpages_arg *arg = _arg;
6786 +
6787 +       ret = D_WALK_CONTINUE;
6788 +       if (dentry->d_sb == arg->sb
6789 +           && !IS_ROOT(dentry)
6790 +           && au_dcount(dentry) > 0
6791 +           && au_di(dentry)
6792 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6793 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6794 +               if (unlikely(arg->err))
6795 +                       ret = D_WALK_QUIT;
6796 +       }
6797 +
6798 +       return ret;
6799 +}
6800 +
6801 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6802 +                  au_dpages_test test, void *arg)
6803 +{
6804 +       struct ac_dpages_arg args = {
6805 +               .err    = 0,
6806 +               .dpages = dpages,
6807 +               .sb     = root->d_sb,
6808 +               .test   = test,
6809 +               .arg    = arg
6810 +       };
6811 +
6812 +       d_walk(root, &args, au_call_dpages_append, NULL);
6813 +
6814 +       return args.err;
6815 +}
6816 +
6817 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6818 +                      int do_include, au_dpages_test test, void *arg)
6819 +{
6820 +       int err;
6821 +
6822 +       err = 0;
6823 +       write_seqlock(&rename_lock);
6824 +       spin_lock(&dentry->d_lock);
6825 +       if (do_include
6826 +           && au_dcount(dentry) > 0
6827 +           && (!test || test(dentry, arg)))
6828 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6829 +       spin_unlock(&dentry->d_lock);
6830 +       if (unlikely(err))
6831 +               goto out;
6832 +
6833 +       /*
6834 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6835 +        * mount
6836 +        */
6837 +       while (!IS_ROOT(dentry)) {
6838 +               dentry = dentry->d_parent; /* rename_lock is locked */
6839 +               spin_lock(&dentry->d_lock);
6840 +               if (au_dcount(dentry) > 0
6841 +                   && (!test || test(dentry, arg)))
6842 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6843 +               spin_unlock(&dentry->d_lock);
6844 +               if (unlikely(err))
6845 +                       break;
6846 +       }
6847 +
6848 +out:
6849 +       write_sequnlock(&rename_lock);
6850 +       return err;
6851 +}
6852 +
6853 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6854 +{
6855 +       return au_di(dentry) && dentry->d_sb == arg;
6856 +}
6857 +
6858 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6859 +                           struct dentry *dentry, int do_include)
6860 +{
6861 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6862 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6863 +}
6864 +
6865 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6866 +{
6867 +       struct path path[2] = {
6868 +               {
6869 +                       .dentry = d1
6870 +               },
6871 +               {
6872 +                       .dentry = d2
6873 +               }
6874 +       };
6875 +
6876 +       return path_is_under(path + 0, path + 1);
6877 +}
6878 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6879 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6880 +++ linux/fs/aufs/dcsub.h       2017-03-02 14:24:13.856922210 +0100
6881 @@ -0,0 +1,136 @@
6882 +/*
6883 + * Copyright (C) 2005-2017 Junjiro R. Okajima
6884 + *
6885 + * This program, aufs is free software; you can redistribute it and/or modify
6886 + * it under the terms of the GNU General Public License as published by
6887 + * the Free Software Foundation; either version 2 of the License, or
6888 + * (at your option) any later version.
6889 + *
6890 + * This program is distributed in the hope that it will be useful,
6891 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6892 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6893 + * GNU General Public License for more details.
6894 + *
6895 + * You should have received a copy of the GNU General Public License
6896 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6897 + */
6898 +
6899 +/*
6900 + * sub-routines for dentry cache
6901 + */
6902 +
6903 +#ifndef __AUFS_DCSUB_H__
6904 +#define __AUFS_DCSUB_H__
6905 +
6906 +#ifdef __KERNEL__
6907 +
6908 +#include <linux/dcache.h>
6909 +#include <linux/fs.h>
6910 +
6911 +struct au_dpage {
6912 +       int ndentry;
6913 +       struct dentry **dentries;
6914 +};
6915 +
6916 +struct au_dcsub_pages {
6917 +       int ndpage;
6918 +       struct au_dpage *dpages;
6919 +};
6920 +
6921 +/* ---------------------------------------------------------------------- */
6922 +
6923 +/* dcsub.c */
6924 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6925 +void au_dpages_free(struct au_dcsub_pages *dpages);
6926 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6927 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6928 +                  au_dpages_test test, void *arg);
6929 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6930 +                      int do_include, au_dpages_test test, void *arg);
6931 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6932 +                           struct dentry *dentry, int do_include);
6933 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6934 +
6935 +/* ---------------------------------------------------------------------- */
6936 +
6937 +/*
6938 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6939 + * include/linux/dcache.h. Try them (in the future).
6940 + */
6941 +
6942 +static inline int au_d_hashed_positive(struct dentry *d)
6943 +{
6944 +       int err;
6945 +       struct inode *inode = d_inode(d);
6946 +
6947 +       err = 0;
6948 +       if (unlikely(d_unhashed(d)
6949 +                    || d_is_negative(d)
6950 +                    || !inode->i_nlink))
6951 +               err = -ENOENT;
6952 +       return err;
6953 +}
6954 +
6955 +static inline int au_d_linkable(struct dentry *d)
6956 +{
6957 +       int err;
6958 +       struct inode *inode = d_inode(d);
6959 +
6960 +       err = au_d_hashed_positive(d);
6961 +       if (err
6962 +           && d_is_positive(d)
6963 +           && (inode->i_state & I_LINKABLE))
6964 +               err = 0;
6965 +       return err;
6966 +}
6967 +
6968 +static inline int au_d_alive(struct dentry *d)
6969 +{
6970 +       int err;
6971 +       struct inode *inode;
6972 +
6973 +       err = 0;
6974 +       if (!IS_ROOT(d))
6975 +               err = au_d_hashed_positive(d);
6976 +       else {
6977 +               inode = d_inode(d);
6978 +               if (unlikely(d_unlinked(d)
6979 +                            || d_is_negative(d)
6980 +                            || !inode->i_nlink))
6981 +                       err = -ENOENT;
6982 +       }
6983 +       return err;
6984 +}
6985 +
6986 +static inline int au_alive_dir(struct dentry *d)
6987 +{
6988 +       int err;
6989 +
6990 +       err = au_d_alive(d);
6991 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
6992 +               err = -ENOENT;
6993 +       return err;
6994 +}
6995 +
6996 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6997 +{
6998 +       return a->len == b->len
6999 +               && !memcmp(a->name, b->name, a->len);
7000 +}
7001 +
7002 +/*
7003 + * by the commit
7004 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7005 + *                     taking d_lock
7006 + * the type of d_lockref.count became int, but the inlined function d_count()
7007 + * still returns unsigned int.
7008 + * I don't know why. Maybe it is for every d_count() users?
7009 + * Anyway au_dcount() lives on.
7010 + */
7011 +static inline int au_dcount(struct dentry *d)
7012 +{
7013 +       return (int)d_count(d);
7014 +}
7015 +
7016 +#endif /* __KERNEL__ */
7017 +#endif /* __AUFS_DCSUB_H__ */
7018 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7019 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7020 +++ linux/fs/aufs/debug.c       2017-03-02 14:24:13.856922210 +0100
7021 @@ -0,0 +1,440 @@
7022 +/*
7023 + * Copyright (C) 2005-2017 Junjiro R. Okajima
7024 + *
7025 + * This program, aufs is free software; you can redistribute it and/or modify
7026 + * it under the terms of the GNU General Public License as published by
7027 + * the Free Software Foundation; either version 2 of the License, or
7028 + * (at your option) any later version.
7029 + *
7030 + * This program is distributed in the hope that it will be useful,
7031 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7032 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7033 + * GNU General Public License for more details.
7034 + *
7035 + * You should have received a copy of the GNU General Public License
7036 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7037 + */
7038 +
7039 +/*
7040 + * debug print functions
7041 + */
7042 +
7043 +#include "aufs.h"
7044 +
7045 +/* Returns 0, or -errno.  arg is in kp->arg. */
7046 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7047 +{
7048 +       int err, n;
7049 +
7050 +       err = kstrtoint(val, 0, &n);
7051 +       if (!err) {
7052 +               if (n > 0)
7053 +                       au_debug_on();
7054 +               else
7055 +                       au_debug_off();
7056 +       }
7057 +       return err;
7058 +}
7059 +
7060 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7061 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7062 +{
7063 +       atomic_t *a;
7064 +
7065 +       a = kp->arg;
7066 +       return sprintf(buffer, "%d", atomic_read(a));
7067 +}
7068 +
7069 +static struct kernel_param_ops param_ops_atomic_t = {
7070 +       .set = param_atomic_t_set,
7071 +       .get = param_atomic_t_get
7072 +       /* void (*free)(void *arg) */
7073 +};
7074 +
7075 +atomic_t aufs_debug = ATOMIC_INIT(0);
7076 +MODULE_PARM_DESC(debug, "debug print");
7077 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
7078 +
7079 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7080 +char *au_plevel = KERN_DEBUG;
7081 +#define dpri(fmt, ...) do {                                    \
7082 +       if ((au_plevel                                          \
7083 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7084 +           || au_debug_test())                                 \
7085 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7086 +} while (0)
7087 +
7088 +/* ---------------------------------------------------------------------- */
7089 +
7090 +void au_dpri_whlist(struct au_nhash *whlist)
7091 +{
7092 +       unsigned long ul, n;
7093 +       struct hlist_head *head;
7094 +       struct au_vdir_wh *pos;
7095 +
7096 +       n = whlist->nh_num;
7097 +       head = whlist->nh_head;
7098 +       for (ul = 0; ul < n; ul++) {
7099 +               hlist_for_each_entry(pos, head, wh_hash)
7100 +                       dpri("b%d, %.*s, %d\n",
7101 +                            pos->wh_bindex,
7102 +                            pos->wh_str.len, pos->wh_str.name,
7103 +                            pos->wh_str.len);
7104 +               head++;
7105 +       }
7106 +}
7107 +
7108 +void au_dpri_vdir(struct au_vdir *vdir)
7109 +{
7110 +       unsigned long ul;
7111 +       union au_vdir_deblk_p p;
7112 +       unsigned char *o;
7113 +
7114 +       if (!vdir || IS_ERR(vdir)) {
7115 +               dpri("err %ld\n", PTR_ERR(vdir));
7116 +               return;
7117 +       }
7118 +
7119 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
7120 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7121 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7122 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7123 +               p.deblk = vdir->vd_deblk[ul];
7124 +               o = p.deblk;
7125 +               dpri("[%lu]: %p\n", ul, o);
7126 +       }
7127 +}
7128 +
7129 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7130 +                       struct dentry *wh)
7131 +{
7132 +       char *n = NULL;
7133 +       int l = 0;
7134 +
7135 +       if (!inode || IS_ERR(inode)) {
7136 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7137 +               return -1;
7138 +       }
7139 +
7140 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7141 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7142 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7143 +       if (wh) {
7144 +               n = (void *)wh->d_name.name;
7145 +               l = wh->d_name.len;
7146 +       }
7147 +
7148 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7149 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7150 +            bindex, inode,
7151 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7152 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7153 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7154 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
7155 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7156 +            inode->i_state, inode->i_flags, inode->i_version,
7157 +            inode->i_generation,
7158 +            l ? ", wh " : "", l, n);
7159 +       return 0;
7160 +}
7161 +
7162 +void au_dpri_inode(struct inode *inode)
7163 +{
7164 +       struct au_iinfo *iinfo;
7165 +       struct au_hinode *hi;
7166 +       aufs_bindex_t bindex;
7167 +       int err, hn;
7168 +
7169 +       err = do_pri_inode(-1, inode, -1, NULL);
7170 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7171 +               return;
7172 +
7173 +       iinfo = au_ii(inode);
7174 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7175 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7176 +       if (iinfo->ii_btop < 0)
7177 +               return;
7178 +       hn = 0;
7179 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7180 +               hi = au_hinode(iinfo, bindex);
7181 +               hn = !!au_hn(hi);
7182 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7183 +       }
7184 +}
7185 +
7186 +void au_dpri_dalias(struct inode *inode)
7187 +{
7188 +       struct dentry *d;
7189 +
7190 +       spin_lock(&inode->i_lock);
7191 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7192 +               au_dpri_dentry(d);
7193 +       spin_unlock(&inode->i_lock);
7194 +}
7195 +
7196 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7197 +{
7198 +       struct dentry *wh = NULL;
7199 +       int hn;
7200 +       struct inode *inode;
7201 +       struct au_iinfo *iinfo;
7202 +       struct au_hinode *hi;
7203 +
7204 +       if (!dentry || IS_ERR(dentry)) {
7205 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7206 +               return -1;
7207 +       }
7208 +       /* do not call dget_parent() here */
7209 +       /* note: access d_xxx without d_lock */
7210 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7211 +            bindex, dentry, dentry,
7212 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7213 +            au_dcount(dentry), dentry->d_flags,
7214 +            d_unhashed(dentry) ? "un" : "");
7215 +       hn = -1;
7216 +       inode = NULL;
7217 +       if (d_is_positive(dentry))
7218 +               inode = d_inode(dentry);
7219 +       if (inode
7220 +           && au_test_aufs(dentry->d_sb)
7221 +           && bindex >= 0
7222 +           && !au_is_bad_inode(inode)) {
7223 +               iinfo = au_ii(inode);
7224 +               hi = au_hinode(iinfo, bindex);
7225 +               hn = !!au_hn(hi);
7226 +               wh = hi->hi_whdentry;
7227 +       }
7228 +       do_pri_inode(bindex, inode, hn, wh);
7229 +       return 0;
7230 +}
7231 +
7232 +void au_dpri_dentry(struct dentry *dentry)
7233 +{
7234 +       struct au_dinfo *dinfo;
7235 +       aufs_bindex_t bindex;
7236 +       int err;
7237 +
7238 +       err = do_pri_dentry(-1, dentry);
7239 +       if (err || !au_test_aufs(dentry->d_sb))
7240 +               return;
7241 +
7242 +       dinfo = au_di(dentry);
7243 +       if (!dinfo)
7244 +               return;
7245 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7246 +            dinfo->di_btop, dinfo->di_bbot,
7247 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7248 +            dinfo->di_tmpfile);
7249 +       if (dinfo->di_btop < 0)
7250 +               return;
7251 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7252 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7253 +}
7254 +
7255 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7256 +{
7257 +       char a[32];
7258 +
7259 +       if (!file || IS_ERR(file)) {
7260 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7261 +               return -1;
7262 +       }
7263 +       a[0] = 0;
7264 +       if (bindex < 0
7265 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7266 +           && au_test_aufs(file->f_path.dentry->d_sb)
7267 +           && au_fi(file))
7268 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7269 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7270 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7271 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7272 +            file->f_version, file->f_pos, a);
7273 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7274 +               do_pri_dentry(bindex, file->f_path.dentry);
7275 +       return 0;
7276 +}
7277 +
7278 +void au_dpri_file(struct file *file)
7279 +{
7280 +       struct au_finfo *finfo;
7281 +       struct au_fidir *fidir;
7282 +       struct au_hfile *hfile;
7283 +       aufs_bindex_t bindex;
7284 +       int err;
7285 +
7286 +       err = do_pri_file(-1, file);
7287 +       if (err
7288 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7289 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7290 +               return;
7291 +
7292 +       finfo = au_fi(file);
7293 +       if (!finfo)
7294 +               return;
7295 +       if (finfo->fi_btop < 0)
7296 +               return;
7297 +       fidir = finfo->fi_hdir;
7298 +       if (!fidir)
7299 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7300 +       else
7301 +               for (bindex = finfo->fi_btop;
7302 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7303 +                    bindex++) {
7304 +                       hfile = fidir->fd_hfile + bindex;
7305 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7306 +               }
7307 +}
7308 +
7309 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7310 +{
7311 +       struct vfsmount *mnt;
7312 +       struct super_block *sb;
7313 +
7314 +       if (!br || IS_ERR(br))
7315 +               goto out;
7316 +       mnt = au_br_mnt(br);
7317 +       if (!mnt || IS_ERR(mnt))
7318 +               goto out;
7319 +       sb = mnt->mnt_sb;
7320 +       if (!sb || IS_ERR(sb))
7321 +               goto out;
7322 +
7323 +       dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
7324 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7325 +            "xino %d\n",
7326 +            bindex, br->br_perm, br->br_id, au_br_count(br),
7327 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7328 +            sb->s_flags, sb->s_count,
7329 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7330 +       return 0;
7331 +
7332 +out:
7333 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7334 +       return -1;
7335 +}
7336 +
7337 +void au_dpri_sb(struct super_block *sb)
7338 +{
7339 +       struct au_sbinfo *sbinfo;
7340 +       aufs_bindex_t bindex;
7341 +       int err;
7342 +       /* to reuduce stack size */
7343 +       struct {
7344 +               struct vfsmount mnt;
7345 +               struct au_branch fake;
7346 +       } *a;
7347 +
7348 +       /* this function can be called from magic sysrq */
7349 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7350 +       if (unlikely(!a)) {
7351 +               dpri("no memory\n");
7352 +               return;
7353 +       }
7354 +
7355 +       a->mnt.mnt_sb = sb;
7356 +       a->fake.br_path.mnt = &a->mnt;
7357 +       au_br_count_init(&a->fake);
7358 +       err = do_pri_br(-1, &a->fake);
7359 +       au_br_count_fin(&a->fake);
7360 +       au_delayed_kfree(a);
7361 +       dpri("dev 0x%x\n", sb->s_dev);
7362 +       if (err || !au_test_aufs(sb))
7363 +               return;
7364 +
7365 +       sbinfo = au_sbi(sb);
7366 +       if (!sbinfo)
7367 +               return;
7368 +       dpri("nw %d, gen %u, kobj %d\n",
7369 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7370 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
7371 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7372 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7373 +}
7374 +
7375 +/* ---------------------------------------------------------------------- */
7376 +
7377 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7378 +{
7379 +       struct inode *h_inode, *inode = d_inode(dentry);
7380 +       struct dentry *h_dentry;
7381 +       aufs_bindex_t bindex, bbot, bi;
7382 +
7383 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7384 +               return;
7385 +
7386 +       bbot = au_dbbot(dentry);
7387 +       bi = au_ibbot(inode);
7388 +       if (bi < bbot)
7389 +               bbot = bi;
7390 +       bindex = au_dbtop(dentry);
7391 +       bi = au_ibtop(inode);
7392 +       if (bi > bindex)
7393 +               bindex = bi;
7394 +
7395 +       for (; bindex <= bbot; bindex++) {
7396 +               h_dentry = au_h_dptr(dentry, bindex);
7397 +               if (!h_dentry)
7398 +                       continue;
7399 +               h_inode = au_h_iptr(inode, bindex);
7400 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7401 +                       au_debug_on();
7402 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7403 +                       AuDbgDentry(dentry);
7404 +                       AuDbgInode(inode);
7405 +                       au_debug_off();
7406 +                       BUG();
7407 +               }
7408 +       }
7409 +}
7410 +
7411 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7412 +{
7413 +       int err, i, j;
7414 +       struct au_dcsub_pages dpages;
7415 +       struct au_dpage *dpage;
7416 +       struct dentry **dentries;
7417 +
7418 +       err = au_dpages_init(&dpages, GFP_NOFS);
7419 +       AuDebugOn(err);
7420 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7421 +       AuDebugOn(err);
7422 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7423 +               dpage = dpages.dpages + i;
7424 +               dentries = dpage->dentries;
7425 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7426 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7427 +       }
7428 +       au_dpages_free(&dpages);
7429 +}
7430 +
7431 +void au_dbg_verify_kthread(void)
7432 +{
7433 +       if (au_wkq_test()) {
7434 +               au_dbg_blocked();
7435 +               /*
7436 +                * It may be recursive, but udba=notify between two aufs mounts,
7437 +                * where a single ro branch is shared, is not a problem.
7438 +                */
7439 +               /* WARN_ON(1); */
7440 +       }
7441 +}
7442 +
7443 +/* ---------------------------------------------------------------------- */
7444 +
7445 +int __init au_debug_init(void)
7446 +{
7447 +       aufs_bindex_t bindex;
7448 +       struct au_vdir_destr destr;
7449 +
7450 +       bindex = -1;
7451 +       AuDebugOn(bindex >= 0);
7452 +
7453 +       destr.len = -1;
7454 +       AuDebugOn(destr.len < NAME_MAX);
7455 +
7456 +#ifdef CONFIG_4KSTACKS
7457 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7458 +#endif
7459 +
7460 +       return 0;
7461 +}
7462 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7463 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7464 +++ linux/fs/aufs/debug.h       2017-03-02 14:24:13.856922210 +0100
7465 @@ -0,0 +1,225 @@
7466 +/*
7467 + * Copyright (C) 2005-2017 Junjiro R. Okajima
7468 + *
7469 + * This program, aufs is free software; you can redistribute it and/or modify
7470 + * it under the terms of the GNU General Public License as published by
7471 + * the Free Software Foundation; either version 2 of the License, or
7472 + * (at your option) any later version.
7473 + *
7474 + * This program is distributed in the hope that it will be useful,
7475 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7476 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7477 + * GNU General Public License for more details.
7478 + *
7479 + * You should have received a copy of the GNU General Public License
7480 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7481 + */
7482 +
7483 +/*
7484 + * debug print functions
7485 + */
7486 +
7487 +#ifndef __AUFS_DEBUG_H__
7488 +#define __AUFS_DEBUG_H__
7489 +
7490 +#ifdef __KERNEL__
7491 +
7492 +#include <linux/atomic.h>
7493 +#include <linux/module.h>
7494 +#include <linux/kallsyms.h>
7495 +#include <linux/sysrq.h>
7496 +
7497 +#ifdef CONFIG_AUFS_DEBUG
7498 +#define AuDebugOn(a)           BUG_ON(a)
7499 +
7500 +/* module parameter */
7501 +extern atomic_t aufs_debug;
7502 +static inline void au_debug_on(void)
7503 +{
7504 +       atomic_inc(&aufs_debug);
7505 +}
7506 +static inline void au_debug_off(void)
7507 +{
7508 +       atomic_dec_if_positive(&aufs_debug);
7509 +}
7510 +
7511 +static inline int au_debug_test(void)
7512 +{
7513 +       return atomic_read(&aufs_debug) > 0;
7514 +}
7515 +#else
7516 +#define AuDebugOn(a)           do {} while (0)
7517 +AuStubVoid(au_debug_on, void)
7518 +AuStubVoid(au_debug_off, void)
7519 +AuStubInt0(au_debug_test, void)
7520 +#endif /* CONFIG_AUFS_DEBUG */
7521 +
7522 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7523 +
7524 +/* ---------------------------------------------------------------------- */
7525 +
7526 +/* debug print */
7527 +
7528 +#define AuDbg(fmt, ...) do { \
7529 +       if (au_debug_test()) \
7530 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7531 +} while (0)
7532 +#define AuLabel(l)             AuDbg(#l "\n")
7533 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7534 +#define AuWarn1(fmt, ...) do { \
7535 +       static unsigned char _c; \
7536 +       if (!_c++) \
7537 +               pr_warn(fmt, ##__VA_ARGS__); \
7538 +} while (0)
7539 +
7540 +#define AuErr1(fmt, ...) do { \
7541 +       static unsigned char _c; \
7542 +       if (!_c++) \
7543 +               pr_err(fmt, ##__VA_ARGS__); \
7544 +} while (0)
7545 +
7546 +#define AuIOErr1(fmt, ...) do { \
7547 +       static unsigned char _c; \
7548 +       if (!_c++) \
7549 +               AuIOErr(fmt, ##__VA_ARGS__); \
7550 +} while (0)
7551 +
7552 +#define AuUnsupportMsg "This operation is not supported." \
7553 +                       " Please report this application to aufs-users ML."
7554 +#define AuUnsupport(fmt, ...) do { \
7555 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7556 +       dump_stack(); \
7557 +} while (0)
7558 +
7559 +#define AuTraceErr(e) do { \
7560 +       if (unlikely((e) < 0)) \
7561 +               AuDbg("err %d\n", (int)(e)); \
7562 +} while (0)
7563 +
7564 +#define AuTraceErrPtr(p) do { \
7565 +       if (IS_ERR(p)) \
7566 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7567 +} while (0)
7568 +
7569 +/* dirty macros for debug print, use with "%.*s" and caution */
7570 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7571 +
7572 +/* ---------------------------------------------------------------------- */
7573 +
7574 +struct dentry;
7575 +#ifdef CONFIG_AUFS_DEBUG
7576 +extern struct mutex au_dbg_mtx;
7577 +extern char *au_plevel;
7578 +struct au_nhash;
7579 +void au_dpri_whlist(struct au_nhash *whlist);
7580 +struct au_vdir;
7581 +void au_dpri_vdir(struct au_vdir *vdir);
7582 +struct inode;
7583 +void au_dpri_inode(struct inode *inode);
7584 +void au_dpri_dalias(struct inode *inode);
7585 +void au_dpri_dentry(struct dentry *dentry);
7586 +struct file;
7587 +void au_dpri_file(struct file *filp);
7588 +struct super_block;
7589 +void au_dpri_sb(struct super_block *sb);
7590 +
7591 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7592 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7593 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7594 +void au_dbg_verify_kthread(void);
7595 +
7596 +int __init au_debug_init(void);
7597 +
7598 +#define AuDbgWhlist(w) do { \
7599 +       mutex_lock(&au_dbg_mtx); \
7600 +       AuDbg(#w "\n"); \
7601 +       au_dpri_whlist(w); \
7602 +       mutex_unlock(&au_dbg_mtx); \
7603 +} while (0)
7604 +
7605 +#define AuDbgVdir(v) do { \
7606 +       mutex_lock(&au_dbg_mtx); \
7607 +       AuDbg(#v "\n"); \
7608 +       au_dpri_vdir(v); \
7609 +       mutex_unlock(&au_dbg_mtx); \
7610 +} while (0)
7611 +
7612 +#define AuDbgInode(i) do { \
7613 +       mutex_lock(&au_dbg_mtx); \
7614 +       AuDbg(#i "\n"); \
7615 +       au_dpri_inode(i); \
7616 +       mutex_unlock(&au_dbg_mtx); \
7617 +} while (0)
7618 +
7619 +#define AuDbgDAlias(i) do { \
7620 +       mutex_lock(&au_dbg_mtx); \
7621 +       AuDbg(#i "\n"); \
7622 +       au_dpri_dalias(i); \
7623 +       mutex_unlock(&au_dbg_mtx); \
7624 +} while (0)
7625 +
7626 +#define AuDbgDentry(d) do { \
7627 +       mutex_lock(&au_dbg_mtx); \
7628 +       AuDbg(#d "\n"); \
7629 +       au_dpri_dentry(d); \
7630 +       mutex_unlock(&au_dbg_mtx); \
7631 +} while (0)
7632 +
7633 +#define AuDbgFile(f) do { \
7634 +       mutex_lock(&au_dbg_mtx); \
7635 +       AuDbg(#f "\n"); \
7636 +       au_dpri_file(f); \
7637 +       mutex_unlock(&au_dbg_mtx); \
7638 +} while (0)
7639 +
7640 +#define AuDbgSb(sb) do { \
7641 +       mutex_lock(&au_dbg_mtx); \
7642 +       AuDbg(#sb "\n"); \
7643 +       au_dpri_sb(sb); \
7644 +       mutex_unlock(&au_dbg_mtx); \
7645 +} while (0)
7646 +
7647 +#define AuDbgSym(addr) do {                            \
7648 +       char sym[KSYM_SYMBOL_LEN];                      \
7649 +       sprint_symbol(sym, (unsigned long)addr);        \
7650 +       AuDbg("%s\n", sym);                             \
7651 +} while (0)
7652 +#else
7653 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7654 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7655 +AuStubVoid(au_dbg_verify_kthread, void)
7656 +AuStubInt0(__init au_debug_init, void)
7657 +
7658 +#define AuDbgWhlist(w)         do {} while (0)
7659 +#define AuDbgVdir(v)           do {} while (0)
7660 +#define AuDbgInode(i)          do {} while (0)
7661 +#define AuDbgDAlias(i)         do {} while (0)
7662 +#define AuDbgDentry(d)         do {} while (0)
7663 +#define AuDbgFile(f)           do {} while (0)
7664 +#define AuDbgSb(sb)            do {} while (0)
7665 +#define AuDbgSym(addr)         do {} while (0)
7666 +#endif /* CONFIG_AUFS_DEBUG */
7667 +
7668 +/* ---------------------------------------------------------------------- */
7669 +
7670 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7671 +int __init au_sysrq_init(void);
7672 +void au_sysrq_fin(void);
7673 +
7674 +#ifdef CONFIG_HW_CONSOLE
7675 +#define au_dbg_blocked() do { \
7676 +       WARN_ON(1); \
7677 +       handle_sysrq('w'); \
7678 +} while (0)
7679 +#else
7680 +AuStubVoid(au_dbg_blocked, void)
7681 +#endif
7682 +
7683 +#else
7684 +AuStubInt0(__init au_sysrq_init, void)
7685 +AuStubVoid(au_sysrq_fin, void)
7686 +AuStubVoid(au_dbg_blocked, void)
7687 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7688 +
7689 +#endif /* __KERNEL__ */
7690 +#endif /* __AUFS_DEBUG_H__ */
7691 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7692 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7693 +++ linux/fs/aufs/dentry.c      2017-03-02 14:24:13.856922210 +0100
7694 @@ -0,0 +1,1130 @@
7695 +/*
7696 + * Copyright (C) 2005-2017 Junjiro R. Okajima
7697 + *
7698 + * This program, aufs is free software; you can redistribute it and/or modify
7699 + * it under the terms of the GNU General Public License as published by
7700 + * the Free Software Foundation; either version 2 of the License, or
7701 + * (at your option) any later version.
7702 + *
7703 + * This program is distributed in the hope that it will be useful,
7704 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7705 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7706 + * GNU General Public License for more details.
7707 + *
7708 + * You should have received a copy of the GNU General Public License
7709 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7710 + */
7711 +
7712 +/*
7713 + * lookup and dentry operations
7714 + */
7715 +
7716 +#include <linux/namei.h>
7717 +#include "aufs.h"
7718 +
7719 +struct au_do_lookup_args {
7720 +       unsigned int            flags;
7721 +       mode_t                  type;
7722 +};
7723 +
7724 +/*
7725 + * returns positive/negative dentry, NULL or an error.
7726 + * NULL means whiteout-ed or not-found.
7727 + */
7728 +static struct dentry*
7729 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7730 +            aufs_bindex_t bindex, struct qstr *wh_name,
7731 +            struct au_do_lookup_args *args)
7732 +{
7733 +       struct dentry *h_dentry;
7734 +       struct inode *h_inode;
7735 +       struct au_branch *br;
7736 +       int wh_found, opq;
7737 +       unsigned char wh_able;
7738 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7739 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7740 +                                                         IGNORE_PERM);
7741 +
7742 +       wh_found = 0;
7743 +       br = au_sbr(dentry->d_sb, bindex);
7744 +       wh_able = !!au_br_whable(br->br_perm);
7745 +       if (wh_able)
7746 +               wh_found = au_wh_test(h_parent, wh_name, ignore_perm);
7747 +       h_dentry = ERR_PTR(wh_found);
7748 +       if (!wh_found)
7749 +               goto real_lookup;
7750 +       if (unlikely(wh_found < 0))
7751 +               goto out;
7752 +
7753 +       /* We found a whiteout */
7754 +       /* au_set_dbbot(dentry, bindex); */
7755 +       au_set_dbwh(dentry, bindex);
7756 +       if (!allow_neg)
7757 +               return NULL; /* success */
7758 +
7759 +real_lookup:
7760 +       if (!ignore_perm)
7761 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7762 +       else
7763 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7764 +       if (IS_ERR(h_dentry)) {
7765 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7766 +                   && !allow_neg)
7767 +                       h_dentry = NULL;
7768 +               goto out;
7769 +       }
7770 +
7771 +       h_inode = d_inode(h_dentry);
7772 +       if (d_is_negative(h_dentry)) {
7773 +               if (!allow_neg)
7774 +                       goto out_neg;
7775 +       } else if (wh_found
7776 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7777 +               goto out_neg;
7778 +
7779 +       if (au_dbbot(dentry) <= bindex)
7780 +               au_set_dbbot(dentry, bindex);
7781 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
7782 +               au_set_dbtop(dentry, bindex);
7783 +       au_set_h_dptr(dentry, bindex, h_dentry);
7784 +
7785 +       if (!d_is_dir(h_dentry)
7786 +           || !wh_able
7787 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
7788 +               goto out; /* success */
7789 +
7790 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
7791 +       opq = au_diropq_test(h_dentry);
7792 +       inode_unlock(h_inode);
7793 +       if (opq > 0)
7794 +               au_set_dbdiropq(dentry, bindex);
7795 +       else if (unlikely(opq < 0)) {
7796 +               au_set_h_dptr(dentry, bindex, NULL);
7797 +               h_dentry = ERR_PTR(opq);
7798 +       }
7799 +       goto out;
7800 +
7801 +out_neg:
7802 +       dput(h_dentry);
7803 +       h_dentry = NULL;
7804 +out:
7805 +       return h_dentry;
7806 +}
7807 +
7808 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7809 +{
7810 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7811 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7812 +               return -EPERM;
7813 +       return 0;
7814 +}
7815 +
7816 +/*
7817 + * returns the number of lower positive dentries,
7818 + * otherwise an error.
7819 + * can be called at unlinking with @type is zero.
7820 + */
7821 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
7822 +                  unsigned int flags)
7823 +{
7824 +       int npositive, err;
7825 +       aufs_bindex_t bindex, btail, bdiropq;
7826 +       unsigned char isdir, dirperm1;
7827 +       struct qstr whname;
7828 +       struct au_do_lookup_args args = {
7829 +               .flags          = flags
7830 +       };
7831 +       const struct qstr *name = &dentry->d_name;
7832 +       struct dentry *parent;
7833 +       struct super_block *sb;
7834 +
7835 +       sb = dentry->d_sb;
7836 +       err = au_test_shwh(sb, name);
7837 +       if (unlikely(err))
7838 +               goto out;
7839 +
7840 +       err = au_wh_name_alloc(&whname, name);
7841 +       if (unlikely(err))
7842 +               goto out;
7843 +
7844 +       isdir = !!d_is_dir(dentry);
7845 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7846 +
7847 +       npositive = 0;
7848 +       parent = dget_parent(dentry);
7849 +       btail = au_dbtaildir(parent);
7850 +       for (bindex = btop; bindex <= btail; bindex++) {
7851 +               struct dentry *h_parent, *h_dentry;
7852 +               struct inode *h_inode, *h_dir;
7853 +
7854 +               h_dentry = au_h_dptr(dentry, bindex);
7855 +               if (h_dentry) {
7856 +                       if (d_is_positive(h_dentry))
7857 +                               npositive++;
7858 +                       break;
7859 +               }
7860 +               h_parent = au_h_dptr(parent, bindex);
7861 +               if (!h_parent || !d_is_dir(h_parent))
7862 +                       continue;
7863 +
7864 +               h_dir = d_inode(h_parent);
7865 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
7866 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7867 +                                       &args);
7868 +               inode_unlock(h_dir);
7869 +               err = PTR_ERR(h_dentry);
7870 +               if (IS_ERR(h_dentry))
7871 +                       goto out_parent;
7872 +               if (h_dentry)
7873 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7874 +               if (dirperm1)
7875 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7876 +
7877 +               if (au_dbwh(dentry) == bindex)
7878 +                       break;
7879 +               if (!h_dentry)
7880 +                       continue;
7881 +               if (d_is_negative(h_dentry))
7882 +                       continue;
7883 +               h_inode = d_inode(h_dentry);
7884 +               npositive++;
7885 +               if (!args.type)
7886 +                       args.type = h_inode->i_mode & S_IFMT;
7887 +               if (args.type != S_IFDIR)
7888 +                       break;
7889 +               else if (isdir) {
7890 +                       /* the type of lower may be different */
7891 +                       bdiropq = au_dbdiropq(dentry);
7892 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7893 +                               break;
7894 +               }
7895 +       }
7896 +
7897 +       if (npositive) {
7898 +               AuLabel(positive);
7899 +               au_update_dbtop(dentry);
7900 +       }
7901 +       err = npositive;
7902 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7903 +                    && au_dbtop(dentry) < 0)) {
7904 +               err = -EIO;
7905 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7906 +                       dentry, err);
7907 +       }
7908 +
7909 +out_parent:
7910 +       dput(parent);
7911 +       au_delayed_kfree(whname.name);
7912 +out:
7913 +       return err;
7914 +}
7915 +
7916 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7917 +{
7918 +       struct dentry *dentry;
7919 +       int wkq_err;
7920 +
7921 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
7922 +               dentry = vfsub_lkup_one(name, parent);
7923 +       else {
7924 +               struct vfsub_lkup_one_args args = {
7925 +                       .errp   = &dentry,
7926 +                       .name   = name,
7927 +                       .parent = parent
7928 +               };
7929 +
7930 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7931 +               if (unlikely(wkq_err))
7932 +                       dentry = ERR_PTR(wkq_err);
7933 +       }
7934 +
7935 +       return dentry;
7936 +}
7937 +
7938 +/*
7939 + * lookup @dentry on @bindex which should be negative.
7940 + */
7941 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7942 +{
7943 +       int err;
7944 +       struct dentry *parent, *h_parent, *h_dentry;
7945 +       struct au_branch *br;
7946 +
7947 +       parent = dget_parent(dentry);
7948 +       h_parent = au_h_dptr(parent, bindex);
7949 +       br = au_sbr(dentry->d_sb, bindex);
7950 +       if (wh)
7951 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7952 +       else
7953 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7954 +       err = PTR_ERR(h_dentry);
7955 +       if (IS_ERR(h_dentry))
7956 +               goto out;
7957 +       if (unlikely(d_is_positive(h_dentry))) {
7958 +               err = -EIO;
7959 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7960 +               dput(h_dentry);
7961 +               goto out;
7962 +       }
7963 +
7964 +       err = 0;
7965 +       if (bindex < au_dbtop(dentry))
7966 +               au_set_dbtop(dentry, bindex);
7967 +       if (au_dbbot(dentry) < bindex)
7968 +               au_set_dbbot(dentry, bindex);
7969 +       au_set_h_dptr(dentry, bindex, h_dentry);
7970 +
7971 +out:
7972 +       dput(parent);
7973 +       return err;
7974 +}
7975 +
7976 +/* ---------------------------------------------------------------------- */
7977 +
7978 +/* subset of struct inode */
7979 +struct au_iattr {
7980 +       unsigned long           i_ino;
7981 +       /* unsigned int         i_nlink; */
7982 +       kuid_t                  i_uid;
7983 +       kgid_t                  i_gid;
7984 +       u64                     i_version;
7985 +/*
7986 +       loff_t                  i_size;
7987 +       blkcnt_t                i_blocks;
7988 +*/
7989 +       umode_t                 i_mode;
7990 +};
7991 +
7992 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7993 +{
7994 +       ia->i_ino = h_inode->i_ino;
7995 +       /* ia->i_nlink = h_inode->i_nlink; */
7996 +       ia->i_uid = h_inode->i_uid;
7997 +       ia->i_gid = h_inode->i_gid;
7998 +       ia->i_version = h_inode->i_version;
7999 +/*
8000 +       ia->i_size = h_inode->i_size;
8001 +       ia->i_blocks = h_inode->i_blocks;
8002 +*/
8003 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8004 +}
8005 +
8006 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8007 +{
8008 +       return ia->i_ino != h_inode->i_ino
8009 +               /* || ia->i_nlink != h_inode->i_nlink */
8010 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8011 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8012 +               || ia->i_version != h_inode->i_version
8013 +/*
8014 +               || ia->i_size != h_inode->i_size
8015 +               || ia->i_blocks != h_inode->i_blocks
8016 +*/
8017 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8018 +}
8019 +
8020 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8021 +                             struct au_branch *br)
8022 +{
8023 +       int err;
8024 +       struct au_iattr ia;
8025 +       struct inode *h_inode;
8026 +       struct dentry *h_d;
8027 +       struct super_block *h_sb;
8028 +
8029 +       err = 0;
8030 +       memset(&ia, -1, sizeof(ia));
8031 +       h_sb = h_dentry->d_sb;
8032 +       h_inode = NULL;
8033 +       if (d_is_positive(h_dentry)) {
8034 +               h_inode = d_inode(h_dentry);
8035 +               au_iattr_save(&ia, h_inode);
8036 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8037 +               /* nfs d_revalidate may return 0 for negative dentry */
8038 +               /* fuse d_revalidate always return 0 for negative dentry */
8039 +               goto out;
8040 +
8041 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8042 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
8043 +       err = PTR_ERR(h_d);
8044 +       if (IS_ERR(h_d))
8045 +               goto out;
8046 +
8047 +       err = 0;
8048 +       if (unlikely(h_d != h_dentry
8049 +                    || d_inode(h_d) != h_inode
8050 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8051 +               err = au_busy_or_stale();
8052 +       dput(h_d);
8053 +
8054 +out:
8055 +       AuTraceErr(err);
8056 +       return err;
8057 +}
8058 +
8059 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8060 +               struct dentry *h_parent, struct au_branch *br)
8061 +{
8062 +       int err;
8063 +
8064 +       err = 0;
8065 +       if (udba == AuOpt_UDBA_REVAL
8066 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8067 +               IMustLock(h_dir);
8068 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8069 +       } else if (udba != AuOpt_UDBA_NONE)
8070 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8071 +
8072 +       return err;
8073 +}
8074 +
8075 +/* ---------------------------------------------------------------------- */
8076 +
8077 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8078 +{
8079 +       int err;
8080 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8081 +       struct au_hdentry tmp, *p, *q;
8082 +       struct au_dinfo *dinfo;
8083 +       struct super_block *sb;
8084 +
8085 +       DiMustWriteLock(dentry);
8086 +
8087 +       sb = dentry->d_sb;
8088 +       dinfo = au_di(dentry);
8089 +       bbot = dinfo->di_bbot;
8090 +       bwh = dinfo->di_bwh;
8091 +       bdiropq = dinfo->di_bdiropq;
8092 +       bindex = dinfo->di_btop;
8093 +       p = au_hdentry(dinfo, bindex);
8094 +       for (; bindex <= bbot; bindex++, p++) {
8095 +               if (!p->hd_dentry)
8096 +                       continue;
8097 +
8098 +               new_bindex = au_br_index(sb, p->hd_id);
8099 +               if (new_bindex == bindex)
8100 +                       continue;
8101 +
8102 +               if (dinfo->di_bwh == bindex)
8103 +                       bwh = new_bindex;
8104 +               if (dinfo->di_bdiropq == bindex)
8105 +                       bdiropq = new_bindex;
8106 +               if (new_bindex < 0) {
8107 +                       au_hdput(p);
8108 +                       p->hd_dentry = NULL;
8109 +                       continue;
8110 +               }
8111 +
8112 +               /* swap two lower dentries, and loop again */
8113 +               q = au_hdentry(dinfo, new_bindex);
8114 +               tmp = *q;
8115 +               *q = *p;
8116 +               *p = tmp;
8117 +               if (tmp.hd_dentry) {
8118 +                       bindex--;
8119 +                       p--;
8120 +               }
8121 +       }
8122 +
8123 +       dinfo->di_bwh = -1;
8124 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8125 +               dinfo->di_bwh = bwh;
8126 +
8127 +       dinfo->di_bdiropq = -1;
8128 +       if (bdiropq >= 0
8129 +           && bdiropq <= au_sbbot(sb)
8130 +           && au_sbr_whable(sb, bdiropq))
8131 +               dinfo->di_bdiropq = bdiropq;
8132 +
8133 +       err = -EIO;
8134 +       dinfo->di_btop = -1;
8135 +       dinfo->di_bbot = -1;
8136 +       bbot = au_dbbot(parent);
8137 +       bindex = 0;
8138 +       p = au_hdentry(dinfo, bindex);
8139 +       for (; bindex <= bbot; bindex++, p++)
8140 +               if (p->hd_dentry) {
8141 +                       dinfo->di_btop = bindex;
8142 +                       break;
8143 +               }
8144 +
8145 +       if (dinfo->di_btop >= 0) {
8146 +               bindex = bbot;
8147 +               p = au_hdentry(dinfo, bindex);
8148 +               for (; bindex >= 0; bindex--, p--)
8149 +                       if (p->hd_dentry) {
8150 +                               dinfo->di_bbot = bindex;
8151 +                               err = 0;
8152 +                               break;
8153 +                       }
8154 +       }
8155 +
8156 +       return err;
8157 +}
8158 +
8159 +static void au_do_hide(struct dentry *dentry)
8160 +{
8161 +       struct inode *inode;
8162 +
8163 +       if (d_really_is_positive(dentry)) {
8164 +               inode = d_inode(dentry);
8165 +               if (!d_is_dir(dentry)) {
8166 +                       if (inode->i_nlink && !d_unhashed(dentry))
8167 +                               drop_nlink(inode);
8168 +               } else {
8169 +                       clear_nlink(inode);
8170 +                       /* stop next lookup */
8171 +                       inode->i_flags |= S_DEAD;
8172 +               }
8173 +               smp_mb(); /* necessary? */
8174 +       }
8175 +       d_drop(dentry);
8176 +}
8177 +
8178 +static int au_hide_children(struct dentry *parent)
8179 +{
8180 +       int err, i, j, ndentry;
8181 +       struct au_dcsub_pages dpages;
8182 +       struct au_dpage *dpage;
8183 +       struct dentry *dentry;
8184 +
8185 +       err = au_dpages_init(&dpages, GFP_NOFS);
8186 +       if (unlikely(err))
8187 +               goto out;
8188 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8189 +       if (unlikely(err))
8190 +               goto out_dpages;
8191 +
8192 +       /* in reverse order */
8193 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8194 +               dpage = dpages.dpages + i;
8195 +               ndentry = dpage->ndentry;
8196 +               for (j = ndentry - 1; j >= 0; j--) {
8197 +                       dentry = dpage->dentries[j];
8198 +                       if (dentry != parent)
8199 +                               au_do_hide(dentry);
8200 +               }
8201 +       }
8202 +
8203 +out_dpages:
8204 +       au_dpages_free(&dpages);
8205 +out:
8206 +       return err;
8207 +}
8208 +
8209 +static void au_hide(struct dentry *dentry)
8210 +{
8211 +       int err;
8212 +
8213 +       AuDbgDentry(dentry);
8214 +       if (d_is_dir(dentry)) {
8215 +               /* shrink_dcache_parent(dentry); */
8216 +               err = au_hide_children(dentry);
8217 +               if (unlikely(err))
8218 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8219 +                               dentry, err);
8220 +       }
8221 +       au_do_hide(dentry);
8222 +}
8223 +
8224 +/*
8225 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8226 + *
8227 + * a dirty branch is added
8228 + * - on the top of layers
8229 + * - in the middle of layers
8230 + * - to the bottom of layers
8231 + *
8232 + * on the added branch there exists
8233 + * - a whiteout
8234 + * - a diropq
8235 + * - a same named entry
8236 + *   + exist
8237 + *     * negative --> positive
8238 + *     * positive --> positive
8239 + *      - type is unchanged
8240 + *      - type is changed
8241 + *   + doesn't exist
8242 + *     * negative --> negative
8243 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8244 + * - none
8245 + */
8246 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8247 +                              struct au_dinfo *tmp)
8248 +{
8249 +       int err;
8250 +       aufs_bindex_t bindex, bbot;
8251 +       struct {
8252 +               struct dentry *dentry;
8253 +               struct inode *inode;
8254 +               mode_t mode;
8255 +       } orig_h, tmp_h = {
8256 +               .dentry = NULL
8257 +       };
8258 +       struct au_hdentry *hd;
8259 +       struct inode *inode, *h_inode;
8260 +       struct dentry *h_dentry;
8261 +
8262 +       err = 0;
8263 +       AuDebugOn(dinfo->di_btop < 0);
8264 +       orig_h.mode = 0;
8265 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8266 +       orig_h.inode = NULL;
8267 +       if (d_is_positive(orig_h.dentry)) {
8268 +               orig_h.inode = d_inode(orig_h.dentry);
8269 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8270 +       }
8271 +       if (tmp->di_btop >= 0) {
8272 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8273 +               if (d_is_positive(tmp_h.dentry)) {
8274 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8275 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8276 +               }
8277 +       }
8278 +
8279 +       inode = NULL;
8280 +       if (d_really_is_positive(dentry))
8281 +               inode = d_inode(dentry);
8282 +       if (!orig_h.inode) {
8283 +               AuDbg("nagative originally\n");
8284 +               if (inode) {
8285 +                       au_hide(dentry);
8286 +                       goto out;
8287 +               }
8288 +               AuDebugOn(inode);
8289 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8290 +               AuDebugOn(dinfo->di_bdiropq != -1);
8291 +
8292 +               if (!tmp_h.inode) {
8293 +                       AuDbg("negative --> negative\n");
8294 +                       /* should have only one negative lower */
8295 +                       if (tmp->di_btop >= 0
8296 +                           && tmp->di_btop < dinfo->di_btop) {
8297 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8298 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8299 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8300 +                               au_di_cp(dinfo, tmp);
8301 +                               hd = au_hdentry(tmp, tmp->di_btop);
8302 +                               au_set_h_dptr(dentry, tmp->di_btop,
8303 +                                             dget(hd->hd_dentry));
8304 +                       }
8305 +                       au_dbg_verify_dinode(dentry);
8306 +               } else {
8307 +                       AuDbg("negative --> positive\n");
8308 +                       /*
8309 +                        * similar to the behaviour of creating with bypassing
8310 +                        * aufs.
8311 +                        * unhash it in order to force an error in the
8312 +                        * succeeding create operation.
8313 +                        * we should not set S_DEAD here.
8314 +                        */
8315 +                       d_drop(dentry);
8316 +                       /* au_di_swap(tmp, dinfo); */
8317 +                       au_dbg_verify_dinode(dentry);
8318 +               }
8319 +       } else {
8320 +               AuDbg("positive originally\n");
8321 +               /* inode may be NULL */
8322 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8323 +               if (!tmp_h.inode) {
8324 +                       AuDbg("positive --> negative\n");
8325 +                       /* or bypassing aufs */
8326 +                       au_hide(dentry);
8327 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8328 +                               dinfo->di_bwh = tmp->di_bwh;
8329 +                       if (inode)
8330 +                               err = au_refresh_hinode_self(inode);
8331 +                       au_dbg_verify_dinode(dentry);
8332 +               } else if (orig_h.mode == tmp_h.mode) {
8333 +                       AuDbg("positive --> positive, same type\n");
8334 +                       if (!S_ISDIR(orig_h.mode)
8335 +                           && dinfo->di_btop > tmp->di_btop) {
8336 +                               /*
8337 +                                * similar to the behaviour of removing and
8338 +                                * creating.
8339 +                                */
8340 +                               au_hide(dentry);
8341 +                               if (inode)
8342 +                                       err = au_refresh_hinode_self(inode);
8343 +                               au_dbg_verify_dinode(dentry);
8344 +                       } else {
8345 +                               /* fill empty slots */
8346 +                               if (dinfo->di_btop > tmp->di_btop)
8347 +                                       dinfo->di_btop = tmp->di_btop;
8348 +                               if (dinfo->di_bbot < tmp->di_bbot)
8349 +                                       dinfo->di_bbot = tmp->di_bbot;
8350 +                               dinfo->di_bwh = tmp->di_bwh;
8351 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8352 +                               bbot = dinfo->di_bbot;
8353 +                               bindex = tmp->di_btop;
8354 +                               hd = au_hdentry(tmp, bindex);
8355 +                               for (; bindex <= bbot; bindex++, hd++) {
8356 +                                       if (au_h_dptr(dentry, bindex))
8357 +                                               continue;
8358 +                                       h_dentry = hd->hd_dentry;
8359 +                                       if (!h_dentry)
8360 +                                               continue;
8361 +                                       AuDebugOn(d_is_negative(h_dentry));
8362 +                                       h_inode = d_inode(h_dentry);
8363 +                                       AuDebugOn(orig_h.mode
8364 +                                                 != (h_inode->i_mode
8365 +                                                     & S_IFMT));
8366 +                                       au_set_h_dptr(dentry, bindex,
8367 +                                                     dget(h_dentry));
8368 +                               }
8369 +                               if (inode)
8370 +                                       err = au_refresh_hinode(inode, dentry);
8371 +                               au_dbg_verify_dinode(dentry);
8372 +                       }
8373 +               } else {
8374 +                       AuDbg("positive --> positive, different type\n");
8375 +                       /* similar to the behaviour of removing and creating */
8376 +                       au_hide(dentry);
8377 +                       if (inode)
8378 +                               err = au_refresh_hinode_self(inode);
8379 +                       au_dbg_verify_dinode(dentry);
8380 +               }
8381 +       }
8382 +
8383 +out:
8384 +       return err;
8385 +}
8386 +
8387 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8388 +{
8389 +       const struct dentry_operations *dop
8390 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8391 +       static const unsigned int mask
8392 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8393 +
8394 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8395 +
8396 +       if (dentry->d_op == dop)
8397 +               return;
8398 +
8399 +       AuDbg("%pd\n", dentry);
8400 +       spin_lock(&dentry->d_lock);
8401 +       if (dop == &aufs_dop)
8402 +               dentry->d_flags |= mask;
8403 +       else
8404 +               dentry->d_flags &= ~mask;
8405 +       dentry->d_op = dop;
8406 +       spin_unlock(&dentry->d_lock);
8407 +}
8408 +
8409 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8410 +{
8411 +       int err, ebrange, nbr;
8412 +       unsigned int sigen;
8413 +       struct au_dinfo *dinfo, *tmp;
8414 +       struct super_block *sb;
8415 +       struct inode *inode;
8416 +
8417 +       DiMustWriteLock(dentry);
8418 +       AuDebugOn(IS_ROOT(dentry));
8419 +       AuDebugOn(d_really_is_negative(parent));
8420 +
8421 +       sb = dentry->d_sb;
8422 +       sigen = au_sigen(sb);
8423 +       err = au_digen_test(parent, sigen);
8424 +       if (unlikely(err))
8425 +               goto out;
8426 +
8427 +       nbr = au_sbbot(sb) + 1;
8428 +       dinfo = au_di(dentry);
8429 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8430 +       if (unlikely(err))
8431 +               goto out;
8432 +       ebrange = au_dbrange_test(dentry);
8433 +       if (!ebrange)
8434 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8435 +
8436 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8437 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8438 +               if (d_really_is_positive(dentry)) {
8439 +                       inode = d_inode(dentry);
8440 +                       err = au_refresh_hinode_self(inode);
8441 +               }
8442 +               au_dbg_verify_dinode(dentry);
8443 +               if (!err)
8444 +                       goto out_dgen; /* success */
8445 +               goto out;
8446 +       }
8447 +
8448 +       /* temporary dinfo */
8449 +       AuDbgDentry(dentry);
8450 +       err = -ENOMEM;
8451 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8452 +       if (unlikely(!tmp))
8453 +               goto out;
8454 +       au_di_swap(tmp, dinfo);
8455 +       /* returns the number of positive dentries */
8456 +       /*
8457 +        * if current working dir is removed, it returns an error.
8458 +        * but the dentry is legal.
8459 +        */
8460 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8461 +       AuDbgDentry(dentry);
8462 +       au_di_swap(tmp, dinfo);
8463 +       if (err == -ENOENT)
8464 +               err = 0;
8465 +       if (err >= 0) {
8466 +               /* compare/refresh by dinfo */
8467 +               AuDbgDentry(dentry);
8468 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8469 +               au_dbg_verify_dinode(dentry);
8470 +               AuTraceErr(err);
8471 +       }
8472 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8473 +       au_rw_write_unlock(&tmp->di_rwsem);
8474 +       au_di_free(tmp);
8475 +       if (unlikely(err))
8476 +               goto out;
8477 +
8478 +out_dgen:
8479 +       au_update_digen(dentry);
8480 +out:
8481 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8482 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8483 +               AuDbgDentry(dentry);
8484 +       }
8485 +       AuTraceErr(err);
8486 +       return err;
8487 +}
8488 +
8489 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8490 +                          struct dentry *dentry, aufs_bindex_t bindex)
8491 +{
8492 +       int err, valid;
8493 +
8494 +       err = 0;
8495 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8496 +               goto out;
8497 +
8498 +       AuDbg("b%d\n", bindex);
8499 +       /*
8500 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8501 +        * due to whiteout and branch permission.
8502 +        */
8503 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8504 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8505 +       /* it may return tri-state */
8506 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8507 +
8508 +       if (unlikely(valid < 0))
8509 +               err = valid;
8510 +       else if (!valid)
8511 +               err = -EINVAL;
8512 +
8513 +out:
8514 +       AuTraceErr(err);
8515 +       return err;
8516 +}
8517 +
8518 +/* todo: remove this */
8519 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8520 +                         unsigned int flags, int do_udba)
8521 +{
8522 +       int err;
8523 +       umode_t mode, h_mode;
8524 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8525 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8526 +       struct inode *h_inode, *h_cached_inode;
8527 +       struct dentry *h_dentry;
8528 +       struct qstr *name, *h_name;
8529 +
8530 +       err = 0;
8531 +       plus = 0;
8532 +       mode = 0;
8533 +       ibs = -1;
8534 +       ibe = -1;
8535 +       unhashed = !!d_unhashed(dentry);
8536 +       is_root = !!IS_ROOT(dentry);
8537 +       name = &dentry->d_name;
8538 +       tmpfile = au_di(dentry)->di_tmpfile;
8539 +
8540 +       /*
8541 +        * Theoretically, REVAL test should be unnecessary in case of
8542 +        * {FS,I}NOTIFY.
8543 +        * But {fs,i}notify doesn't fire some necessary events,
8544 +        *      IN_ATTRIB for atime/nlink/pageio
8545 +        * Let's do REVAL test too.
8546 +        */
8547 +       if (do_udba && inode) {
8548 +               mode = (inode->i_mode & S_IFMT);
8549 +               plus = (inode->i_nlink > 0);
8550 +               ibs = au_ibtop(inode);
8551 +               ibe = au_ibbot(inode);
8552 +       }
8553 +
8554 +       btop = au_dbtop(dentry);
8555 +       btail = btop;
8556 +       if (inode && S_ISDIR(inode->i_mode))
8557 +               btail = au_dbtaildir(dentry);
8558 +       for (bindex = btop; bindex <= btail; bindex++) {
8559 +               h_dentry = au_h_dptr(dentry, bindex);
8560 +               if (!h_dentry)
8561 +                       continue;
8562 +
8563 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8564 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8565 +               spin_lock(&h_dentry->d_lock);
8566 +               h_name = &h_dentry->d_name;
8567 +               if (unlikely(do_udba
8568 +                            && !is_root
8569 +                            && ((!h_nfs
8570 +                                 && (unhashed != !!d_unhashed(h_dentry)
8571 +                                     || (!tmpfile
8572 +                                         && !au_qstreq(name, h_name))
8573 +                                         ))
8574 +                                || (h_nfs
8575 +                                    && !(flags & LOOKUP_OPEN)
8576 +                                    && (h_dentry->d_flags
8577 +                                        & DCACHE_NFSFS_RENAMED)))
8578 +                           )) {
8579 +                       int h_unhashed;
8580 +
8581 +                       h_unhashed = d_unhashed(h_dentry);
8582 +                       spin_unlock(&h_dentry->d_lock);
8583 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8584 +                             unhashed, h_unhashed, dentry, h_dentry);
8585 +                       goto err;
8586 +               }
8587 +               spin_unlock(&h_dentry->d_lock);
8588 +
8589 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8590 +               if (unlikely(err))
8591 +                       /* do not goto err, to keep the errno */
8592 +                       break;
8593 +
8594 +               /* todo: plink too? */
8595 +               if (!do_udba)
8596 +                       continue;
8597 +
8598 +               /* UDBA tests */
8599 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8600 +                       goto err;
8601 +
8602 +               h_inode = NULL;
8603 +               if (d_is_positive(h_dentry))
8604 +                       h_inode = d_inode(h_dentry);
8605 +               h_plus = plus;
8606 +               h_mode = mode;
8607 +               h_cached_inode = h_inode;
8608 +               if (h_inode) {
8609 +                       h_mode = (h_inode->i_mode & S_IFMT);
8610 +                       h_plus = (h_inode->i_nlink > 0);
8611 +               }
8612 +               if (inode && ibs <= bindex && bindex <= ibe)
8613 +                       h_cached_inode = au_h_iptr(inode, bindex);
8614 +
8615 +               if (!h_nfs) {
8616 +                       if (unlikely(plus != h_plus && !tmpfile))
8617 +                               goto err;
8618 +               } else {
8619 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8620 +                                    && !is_root
8621 +                                    && !IS_ROOT(h_dentry)
8622 +                                    && unhashed != d_unhashed(h_dentry)))
8623 +                               goto err;
8624 +               }
8625 +               if (unlikely(mode != h_mode
8626 +                            || h_cached_inode != h_inode))
8627 +                       goto err;
8628 +               continue;
8629 +
8630 +err:
8631 +               err = -EINVAL;
8632 +               break;
8633 +       }
8634 +
8635 +       AuTraceErr(err);
8636 +       return err;
8637 +}
8638 +
8639 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8640 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8641 +{
8642 +       int err;
8643 +       struct dentry *parent;
8644 +
8645 +       if (!au_digen_test(dentry, sigen))
8646 +               return 0;
8647 +
8648 +       parent = dget_parent(dentry);
8649 +       di_read_lock_parent(parent, AuLock_IR);
8650 +       AuDebugOn(au_digen_test(parent, sigen));
8651 +       au_dbg_verify_gen(parent, sigen);
8652 +       err = au_refresh_dentry(dentry, parent);
8653 +       di_read_unlock(parent, AuLock_IR);
8654 +       dput(parent);
8655 +       AuTraceErr(err);
8656 +       return err;
8657 +}
8658 +
8659 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8660 +{
8661 +       int err;
8662 +       struct dentry *d, *parent;
8663 +
8664 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8665 +               return simple_reval_dpath(dentry, sigen);
8666 +
8667 +       /* slow loop, keep it simple and stupid */
8668 +       /* cf: au_cpup_dirs() */
8669 +       err = 0;
8670 +       parent = NULL;
8671 +       while (au_digen_test(dentry, sigen)) {
8672 +               d = dentry;
8673 +               while (1) {
8674 +                       dput(parent);
8675 +                       parent = dget_parent(d);
8676 +                       if (!au_digen_test(parent, sigen))
8677 +                               break;
8678 +                       d = parent;
8679 +               }
8680 +
8681 +               if (d != dentry)
8682 +                       di_write_lock_child2(d);
8683 +
8684 +               /* someone might update our dentry while we were sleeping */
8685 +               if (au_digen_test(d, sigen)) {
8686 +                       /*
8687 +                        * todo: consolidate with simple_reval_dpath(),
8688 +                        * do_refresh() and au_reval_for_attr().
8689 +                        */
8690 +                       di_read_lock_parent(parent, AuLock_IR);
8691 +                       err = au_refresh_dentry(d, parent);
8692 +                       di_read_unlock(parent, AuLock_IR);
8693 +               }
8694 +
8695 +               if (d != dentry)
8696 +                       di_write_unlock(d);
8697 +               dput(parent);
8698 +               if (unlikely(err))
8699 +                       break;
8700 +       }
8701 +
8702 +       return err;
8703 +}
8704 +
8705 +/*
8706 + * if valid returns 1, otherwise 0.
8707 + */
8708 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8709 +{
8710 +       int valid, err;
8711 +       unsigned int sigen;
8712 +       unsigned char do_udba;
8713 +       struct super_block *sb;
8714 +       struct inode *inode;
8715 +
8716 +       /* todo: support rcu-walk? */
8717 +       if (flags & LOOKUP_RCU)
8718 +               return -ECHILD;
8719 +
8720 +       valid = 0;
8721 +       if (unlikely(!au_di(dentry)))
8722 +               goto out;
8723 +
8724 +       valid = 1;
8725 +       sb = dentry->d_sb;
8726 +       /*
8727 +        * todo: very ugly
8728 +        * i_mutex of parent dir may be held,
8729 +        * but we should not return 'invalid' due to busy.
8730 +        */
8731 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8732 +       if (unlikely(err)) {
8733 +               valid = err;
8734 +               AuTraceErr(err);
8735 +               goto out;
8736 +       }
8737 +       inode = NULL;
8738 +       if (d_really_is_positive(dentry))
8739 +               inode = d_inode(dentry);
8740 +       if (unlikely(inode && au_is_bad_inode(inode))) {
8741 +               err = -EINVAL;
8742 +               AuTraceErr(err);
8743 +               goto out_dgrade;
8744 +       }
8745 +       if (unlikely(au_dbrange_test(dentry))) {
8746 +               err = -EINVAL;
8747 +               AuTraceErr(err);
8748 +               goto out_dgrade;
8749 +       }
8750 +
8751 +       sigen = au_sigen(sb);
8752 +       if (au_digen_test(dentry, sigen)) {
8753 +               AuDebugOn(IS_ROOT(dentry));
8754 +               err = au_reval_dpath(dentry, sigen);
8755 +               if (unlikely(err)) {
8756 +                       AuTraceErr(err);
8757 +                       goto out_dgrade;
8758 +               }
8759 +       }
8760 +       di_downgrade_lock(dentry, AuLock_IR);
8761 +
8762 +       err = -EINVAL;
8763 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8764 +           && inode
8765 +           && !(inode->i_state && I_LINKABLE)
8766 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8767 +               AuTraceErr(err);
8768 +               goto out_inval;
8769 +       }
8770 +
8771 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8772 +       if (do_udba && inode) {
8773 +               aufs_bindex_t btop = au_ibtop(inode);
8774 +               struct inode *h_inode;
8775 +
8776 +               if (btop >= 0) {
8777 +                       h_inode = au_h_iptr(inode, btop);
8778 +                       if (h_inode && au_test_higen(inode, h_inode)) {
8779 +                               AuTraceErr(err);
8780 +                               goto out_inval;
8781 +                       }
8782 +               }
8783 +       }
8784 +
8785 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8786 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
8787 +               err = -EIO;
8788 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8789 +                     dentry, err);
8790 +       }
8791 +       goto out_inval;
8792 +
8793 +out_dgrade:
8794 +       di_downgrade_lock(dentry, AuLock_IR);
8795 +out_inval:
8796 +       aufs_read_unlock(dentry, AuLock_IR);
8797 +       AuTraceErr(err);
8798 +       valid = !err;
8799 +out:
8800 +       if (!valid) {
8801 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8802 +               d_drop(dentry);
8803 +       }
8804 +       return valid;
8805 +}
8806 +
8807 +static void aufs_d_release(struct dentry *dentry)
8808 +{
8809 +       if (au_di(dentry)) {
8810 +               au_di_fin(dentry);
8811 +               au_hn_di_reinit(dentry);
8812 +       }
8813 +}
8814 +
8815 +const struct dentry_operations aufs_dop = {
8816 +       .d_revalidate           = aufs_d_revalidate,
8817 +       .d_weak_revalidate      = aufs_d_revalidate,
8818 +       .d_release              = aufs_d_release
8819 +};
8820 +
8821 +/* aufs_dop without d_revalidate */
8822 +const struct dentry_operations aufs_dop_noreval = {
8823 +       .d_release              = aufs_d_release
8824 +};
8825 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8826 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8827 +++ linux/fs/aufs/dentry.h      2017-03-02 14:24:13.856922210 +0100
8828 @@ -0,0 +1,255 @@
8829 +/*
8830 + * Copyright (C) 2005-2017 Junjiro R. Okajima
8831 + *
8832 + * This program, aufs is free software; you can redistribute it and/or modify
8833 + * it under the terms of the GNU General Public License as published by
8834 + * the Free Software Foundation; either version 2 of the License, or
8835 + * (at your option) any later version.
8836 + *
8837 + * This program is distributed in the hope that it will be useful,
8838 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8839 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8840 + * GNU General Public License for more details.
8841 + *
8842 + * You should have received a copy of the GNU General Public License
8843 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8844 + */
8845 +
8846 +/*
8847 + * lookup and dentry operations
8848 + */
8849 +
8850 +#ifndef __AUFS_DENTRY_H__
8851 +#define __AUFS_DENTRY_H__
8852 +
8853 +#ifdef __KERNEL__
8854 +
8855 +#include <linux/dcache.h>
8856 +#include "rwsem.h"
8857 +
8858 +struct au_hdentry {
8859 +       struct dentry           *hd_dentry;
8860 +       aufs_bindex_t           hd_id;
8861 +};
8862 +
8863 +struct au_dinfo {
8864 +       atomic_t                di_generation;
8865 +
8866 +       struct au_rwsem         di_rwsem;
8867 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
8868 +       unsigned char           di_tmpfile; /* to allow the different name */
8869 +       union {
8870 +               struct au_hdentry       *di_hdentry;
8871 +               struct llist_node       di_lnode;       /* delayed free */
8872 +       };
8873 +} ____cacheline_aligned_in_smp;
8874 +
8875 +/* ---------------------------------------------------------------------- */
8876 +
8877 +/* flags for au_lkup_dentry() */
8878 +#define AuLkup_ALLOW_NEG       1
8879 +#define AuLkup_IGNORE_PERM     (1 << 1)
8880 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
8881 +#define au_fset_lkup(flags, name) \
8882 +       do { (flags) |= AuLkup_##name; } while (0)
8883 +#define au_fclr_lkup(flags, name) \
8884 +       do { (flags) &= ~AuLkup_##name; } while (0)
8885 +
8886 +/* ---------------------------------------------------------------------- */
8887 +
8888 +/* dentry.c */
8889 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
8890 +struct au_branch;
8891 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8892 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8893 +               struct dentry *h_parent, struct au_branch *br);
8894 +
8895 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8896 +                  unsigned int flags);
8897 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8898 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8899 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8900 +void au_refresh_dop(struct dentry *dentry, int force_reval);
8901 +
8902 +/* dinfo.c */
8903 +void au_di_init_once(void *_di);
8904 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8905 +void au_di_free(struct au_dinfo *dinfo);
8906 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8907 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8908 +int au_di_init(struct dentry *dentry);
8909 +void au_di_fin(struct dentry *dentry);
8910 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
8911 +
8912 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8913 +void di_read_unlock(struct dentry *d, int flags);
8914 +void di_downgrade_lock(struct dentry *d, int flags);
8915 +void di_write_lock(struct dentry *d, unsigned int lsc);
8916 +void di_write_unlock(struct dentry *d);
8917 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8918 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8919 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8920 +
8921 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8922 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8923 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8924 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8925 +
8926 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8927 +                  struct dentry *h_dentry);
8928 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8929 +int au_dbrange_test(struct dentry *dentry);
8930 +void au_update_digen(struct dentry *dentry);
8931 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8932 +void au_update_dbtop(struct dentry *dentry);
8933 +void au_update_dbbot(struct dentry *dentry);
8934 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8935 +
8936 +/* ---------------------------------------------------------------------- */
8937 +
8938 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8939 +{
8940 +       return dentry->d_fsdata;
8941 +}
8942 +
8943 +/* ---------------------------------------------------------------------- */
8944 +
8945 +/* lock subclass for dinfo */
8946 +enum {
8947 +       AuLsc_DI_CHILD,         /* child first */
8948 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8949 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8950 +       AuLsc_DI_PARENT,
8951 +       AuLsc_DI_PARENT2,
8952 +       AuLsc_DI_PARENT3,
8953 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8954 +};
8955 +
8956 +/*
8957 + * di_read_lock_child, di_write_lock_child,
8958 + * di_read_lock_child2, di_write_lock_child2,
8959 + * di_read_lock_child3, di_write_lock_child3,
8960 + * di_read_lock_parent, di_write_lock_parent,
8961 + * di_read_lock_parent2, di_write_lock_parent2,
8962 + * di_read_lock_parent3, di_write_lock_parent3,
8963 + */
8964 +#define AuReadLockFunc(name, lsc) \
8965 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8966 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8967 +
8968 +#define AuWriteLockFunc(name, lsc) \
8969 +static inline void di_write_lock_##name(struct dentry *d) \
8970 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8971 +
8972 +#define AuRWLockFuncs(name, lsc) \
8973 +       AuReadLockFunc(name, lsc) \
8974 +       AuWriteLockFunc(name, lsc)
8975 +
8976 +AuRWLockFuncs(child, CHILD);
8977 +AuRWLockFuncs(child2, CHILD2);
8978 +AuRWLockFuncs(child3, CHILD3);
8979 +AuRWLockFuncs(parent, PARENT);
8980 +AuRWLockFuncs(parent2, PARENT2);
8981 +AuRWLockFuncs(parent3, PARENT3);
8982 +
8983 +#undef AuReadLockFunc
8984 +#undef AuWriteLockFunc
8985 +#undef AuRWLockFuncs
8986 +
8987 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8988 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8989 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8990 +
8991 +/* ---------------------------------------------------------------------- */
8992 +
8993 +/* todo: memory barrier? */
8994 +static inline unsigned int au_digen(struct dentry *d)
8995 +{
8996 +       return atomic_read(&au_di(d)->di_generation);
8997 +}
8998 +
8999 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9000 +{
9001 +       hdentry->hd_dentry = NULL;
9002 +}
9003 +
9004 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9005 +                                           aufs_bindex_t bindex)
9006 +{
9007 +       return di->di_hdentry + bindex;
9008 +}
9009 +
9010 +static inline void au_hdput(struct au_hdentry *hd)
9011 +{
9012 +       if (hd)
9013 +               dput(hd->hd_dentry);
9014 +}
9015 +
9016 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9017 +{
9018 +       DiMustAnyLock(dentry);
9019 +       return au_di(dentry)->di_btop;
9020 +}
9021 +
9022 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9023 +{
9024 +       DiMustAnyLock(dentry);
9025 +       return au_di(dentry)->di_bbot;
9026 +}
9027 +
9028 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9029 +{
9030 +       DiMustAnyLock(dentry);
9031 +       return au_di(dentry)->di_bwh;
9032 +}
9033 +
9034 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9035 +{
9036 +       DiMustAnyLock(dentry);
9037 +       return au_di(dentry)->di_bdiropq;
9038 +}
9039 +
9040 +/* todo: hard/soft set? */
9041 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9042 +{
9043 +       DiMustWriteLock(dentry);
9044 +       au_di(dentry)->di_btop = bindex;
9045 +}
9046 +
9047 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9048 +{
9049 +       DiMustWriteLock(dentry);
9050 +       au_di(dentry)->di_bbot = bindex;
9051 +}
9052 +
9053 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9054 +{
9055 +       DiMustWriteLock(dentry);
9056 +       /* dbwh can be outside of btop - bbot range */
9057 +       au_di(dentry)->di_bwh = bindex;
9058 +}
9059 +
9060 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9061 +{
9062 +       DiMustWriteLock(dentry);
9063 +       au_di(dentry)->di_bdiropq = bindex;
9064 +}
9065 +
9066 +/* ---------------------------------------------------------------------- */
9067 +
9068 +#ifdef CONFIG_AUFS_HNOTIFY
9069 +static inline void au_digen_dec(struct dentry *d)
9070 +{
9071 +       atomic_dec(&au_di(d)->di_generation);
9072 +}
9073 +
9074 +static inline void au_hn_di_reinit(struct dentry *dentry)
9075 +{
9076 +       dentry->d_fsdata = NULL;
9077 +}
9078 +#else
9079 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9080 +#endif /* CONFIG_AUFS_HNOTIFY */
9081 +
9082 +#endif /* __KERNEL__ */
9083 +#endif /* __AUFS_DENTRY_H__ */
9084 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9085 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9086 +++ linux/fs/aufs/dinfo.c       2017-03-02 14:24:13.856922210 +0100
9087 @@ -0,0 +1,553 @@
9088 +/*
9089 + * Copyright (C) 2005-2017 Junjiro R. Okajima
9090 + *
9091 + * This program, aufs is free software; you can redistribute it and/or modify
9092 + * it under the terms of the GNU General Public License as published by
9093 + * the Free Software Foundation; either version 2 of the License, or
9094 + * (at your option) any later version.
9095 + *
9096 + * This program is distributed in the hope that it will be useful,
9097 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9098 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9099 + * GNU General Public License for more details.
9100 + *
9101 + * You should have received a copy of the GNU General Public License
9102 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9103 + */
9104 +
9105 +/*
9106 + * dentry private data
9107 + */
9108 +
9109 +#include "aufs.h"
9110 +
9111 +void au_di_init_once(void *_dinfo)
9112 +{
9113 +       struct au_dinfo *dinfo = _dinfo;
9114 +
9115 +       au_rw_init(&dinfo->di_rwsem);
9116 +}
9117 +
9118 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9119 +{
9120 +       struct au_dinfo *dinfo;
9121 +       int nbr, i;
9122 +
9123 +       dinfo = au_cache_alloc_dinfo();
9124 +       if (unlikely(!dinfo))
9125 +               goto out;
9126 +
9127 +       nbr = au_sbbot(sb) + 1;
9128 +       if (nbr <= 0)
9129 +               nbr = 1;
9130 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9131 +       if (dinfo->di_hdentry) {
9132 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9133 +               dinfo->di_btop = -1;
9134 +               dinfo->di_bbot = -1;
9135 +               dinfo->di_bwh = -1;
9136 +               dinfo->di_bdiropq = -1;
9137 +               dinfo->di_tmpfile = 0;
9138 +               for (i = 0; i < nbr; i++)
9139 +                       dinfo->di_hdentry[i].hd_id = -1;
9140 +               goto out;
9141 +       }
9142 +
9143 +       au_cache_dfree_dinfo(dinfo);
9144 +       dinfo = NULL;
9145 +
9146 +out:
9147 +       return dinfo;
9148 +}
9149 +
9150 +void au_di_free(struct au_dinfo *dinfo)
9151 +{
9152 +       struct au_hdentry *p;
9153 +       aufs_bindex_t bbot, bindex;
9154 +
9155 +       /* dentry may not be revalidated */
9156 +       bindex = dinfo->di_btop;
9157 +       if (bindex >= 0) {
9158 +               bbot = dinfo->di_bbot;
9159 +               p = au_hdentry(dinfo, bindex);
9160 +               while (bindex++ <= bbot)
9161 +                       au_hdput(p++);
9162 +       }
9163 +       au_delayed_kfree(dinfo->di_hdentry);
9164 +       au_cache_dfree_dinfo(dinfo);
9165 +}
9166 +
9167 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9168 +{
9169 +       struct au_hdentry *p;
9170 +       aufs_bindex_t bi;
9171 +
9172 +       AuRwMustWriteLock(&a->di_rwsem);
9173 +       AuRwMustWriteLock(&b->di_rwsem);
9174 +
9175 +#define DiSwap(v, name)                                \
9176 +       do {                                    \
9177 +               v = a->di_##name;               \
9178 +               a->di_##name = b->di_##name;    \
9179 +               b->di_##name = v;               \
9180 +       } while (0)
9181 +
9182 +       DiSwap(p, hdentry);
9183 +       DiSwap(bi, btop);
9184 +       DiSwap(bi, bbot);
9185 +       DiSwap(bi, bwh);
9186 +       DiSwap(bi, bdiropq);
9187 +       /* smp_mb(); */
9188 +
9189 +#undef DiSwap
9190 +}
9191 +
9192 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9193 +{
9194 +       AuRwMustWriteLock(&dst->di_rwsem);
9195 +       AuRwMustWriteLock(&src->di_rwsem);
9196 +
9197 +       dst->di_btop = src->di_btop;
9198 +       dst->di_bbot = src->di_bbot;
9199 +       dst->di_bwh = src->di_bwh;
9200 +       dst->di_bdiropq = src->di_bdiropq;
9201 +       /* smp_mb(); */
9202 +}
9203 +
9204 +int au_di_init(struct dentry *dentry)
9205 +{
9206 +       int err;
9207 +       struct super_block *sb;
9208 +       struct au_dinfo *dinfo;
9209 +
9210 +       err = 0;
9211 +       sb = dentry->d_sb;
9212 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9213 +       if (dinfo) {
9214 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9215 +               /* smp_mb(); */ /* atomic_set */
9216 +               dentry->d_fsdata = dinfo;
9217 +       } else
9218 +               err = -ENOMEM;
9219 +
9220 +       return err;
9221 +}
9222 +
9223 +void au_di_fin(struct dentry *dentry)
9224 +{
9225 +       struct au_dinfo *dinfo;
9226 +
9227 +       dinfo = au_di(dentry);
9228 +       AuRwDestroy(&dinfo->di_rwsem);
9229 +       au_di_free(dinfo);
9230 +}
9231 +
9232 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9233 +{
9234 +       int err, sz;
9235 +       struct au_hdentry *hdp;
9236 +
9237 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9238 +
9239 +       err = -ENOMEM;
9240 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9241 +       if (!sz)
9242 +               sz = sizeof(*hdp);
9243 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9244 +                          may_shrink);
9245 +       if (hdp) {
9246 +               dinfo->di_hdentry = hdp;
9247 +               err = 0;
9248 +       }
9249 +
9250 +       return err;
9251 +}
9252 +
9253 +/* ---------------------------------------------------------------------- */
9254 +
9255 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9256 +{
9257 +       switch (lsc) {
9258 +       case AuLsc_DI_CHILD:
9259 +               ii_write_lock_child(inode);
9260 +               break;
9261 +       case AuLsc_DI_CHILD2:
9262 +               ii_write_lock_child2(inode);
9263 +               break;
9264 +       case AuLsc_DI_CHILD3:
9265 +               ii_write_lock_child3(inode);
9266 +               break;
9267 +       case AuLsc_DI_PARENT:
9268 +               ii_write_lock_parent(inode);
9269 +               break;
9270 +       case AuLsc_DI_PARENT2:
9271 +               ii_write_lock_parent2(inode);
9272 +               break;
9273 +       case AuLsc_DI_PARENT3:
9274 +               ii_write_lock_parent3(inode);
9275 +               break;
9276 +       default:
9277 +               BUG();
9278 +       }
9279 +}
9280 +
9281 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9282 +{
9283 +       switch (lsc) {
9284 +       case AuLsc_DI_CHILD:
9285 +               ii_read_lock_child(inode);
9286 +               break;
9287 +       case AuLsc_DI_CHILD2:
9288 +               ii_read_lock_child2(inode);
9289 +               break;
9290 +       case AuLsc_DI_CHILD3:
9291 +               ii_read_lock_child3(inode);
9292 +               break;
9293 +       case AuLsc_DI_PARENT:
9294 +               ii_read_lock_parent(inode);
9295 +               break;
9296 +       case AuLsc_DI_PARENT2:
9297 +               ii_read_lock_parent2(inode);
9298 +               break;
9299 +       case AuLsc_DI_PARENT3:
9300 +               ii_read_lock_parent3(inode);
9301 +               break;
9302 +       default:
9303 +               BUG();
9304 +       }
9305 +}
9306 +
9307 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9308 +{
9309 +       struct inode *inode;
9310 +
9311 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9312 +       if (d_really_is_positive(d)) {
9313 +               inode = d_inode(d);
9314 +               if (au_ftest_lock(flags, IW))
9315 +                       do_ii_write_lock(inode, lsc);
9316 +               else if (au_ftest_lock(flags, IR))
9317 +                       do_ii_read_lock(inode, lsc);
9318 +       }
9319 +}
9320 +
9321 +void di_read_unlock(struct dentry *d, int flags)
9322 +{
9323 +       struct inode *inode;
9324 +
9325 +       if (d_really_is_positive(d)) {
9326 +               inode = d_inode(d);
9327 +               if (au_ftest_lock(flags, IW)) {
9328 +                       au_dbg_verify_dinode(d);
9329 +                       ii_write_unlock(inode);
9330 +               } else if (au_ftest_lock(flags, IR)) {
9331 +                       au_dbg_verify_dinode(d);
9332 +                       ii_read_unlock(inode);
9333 +               }
9334 +       }
9335 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9336 +}
9337 +
9338 +void di_downgrade_lock(struct dentry *d, int flags)
9339 +{
9340 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9341 +               ii_downgrade_lock(d_inode(d));
9342 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9343 +}
9344 +
9345 +void di_write_lock(struct dentry *d, unsigned int lsc)
9346 +{
9347 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9348 +       if (d_really_is_positive(d))
9349 +               do_ii_write_lock(d_inode(d), lsc);
9350 +}
9351 +
9352 +void di_write_unlock(struct dentry *d)
9353 +{
9354 +       au_dbg_verify_dinode(d);
9355 +       if (d_really_is_positive(d))
9356 +               ii_write_unlock(d_inode(d));
9357 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9358 +}
9359 +
9360 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9361 +{
9362 +       AuDebugOn(d1 == d2
9363 +                 || d_inode(d1) == d_inode(d2)
9364 +                 || d1->d_sb != d2->d_sb);
9365 +
9366 +       if (isdir && au_test_subdir(d1, d2)) {
9367 +               di_write_lock_child(d1);
9368 +               di_write_lock_child2(d2);
9369 +       } else {
9370 +               /* there should be no races */
9371 +               di_write_lock_child(d2);
9372 +               di_write_lock_child2(d1);
9373 +       }
9374 +}
9375 +
9376 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9377 +{
9378 +       AuDebugOn(d1 == d2
9379 +                 || d_inode(d1) == d_inode(d2)
9380 +                 || d1->d_sb != d2->d_sb);
9381 +
9382 +       if (isdir && au_test_subdir(d1, d2)) {
9383 +               di_write_lock_parent(d1);
9384 +               di_write_lock_parent2(d2);
9385 +       } else {
9386 +               /* there should be no races */
9387 +               di_write_lock_parent(d2);
9388 +               di_write_lock_parent2(d1);
9389 +       }
9390 +}
9391 +
9392 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9393 +{
9394 +       di_write_unlock(d1);
9395 +       if (d_inode(d1) == d_inode(d2))
9396 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9397 +       else
9398 +               di_write_unlock(d2);
9399 +}
9400 +
9401 +/* ---------------------------------------------------------------------- */
9402 +
9403 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9404 +{
9405 +       struct dentry *d;
9406 +
9407 +       DiMustAnyLock(dentry);
9408 +
9409 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9410 +               return NULL;
9411 +       AuDebugOn(bindex < 0);
9412 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9413 +       AuDebugOn(d && au_dcount(d) <= 0);
9414 +       return d;
9415 +}
9416 +
9417 +/*
9418 + * extended version of au_h_dptr().
9419 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9420 + * error.
9421 + */
9422 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9423 +{
9424 +       struct dentry *h_dentry;
9425 +       struct inode *inode, *h_inode;
9426 +
9427 +       AuDebugOn(d_really_is_negative(dentry));
9428 +
9429 +       h_dentry = NULL;
9430 +       if (au_dbtop(dentry) <= bindex
9431 +           && bindex <= au_dbbot(dentry))
9432 +               h_dentry = au_h_dptr(dentry, bindex);
9433 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9434 +               dget(h_dentry);
9435 +               goto out; /* success */
9436 +       }
9437 +
9438 +       inode = d_inode(dentry);
9439 +       AuDebugOn(bindex < au_ibtop(inode));
9440 +       AuDebugOn(au_ibbot(inode) < bindex);
9441 +       h_inode = au_h_iptr(inode, bindex);
9442 +       h_dentry = d_find_alias(h_inode);
9443 +       if (h_dentry) {
9444 +               if (!IS_ERR(h_dentry)) {
9445 +                       if (!au_d_linkable(h_dentry))
9446 +                               goto out; /* success */
9447 +                       dput(h_dentry);
9448 +               } else
9449 +                       goto out;
9450 +       }
9451 +
9452 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9453 +               h_dentry = au_plink_lkup(inode, bindex);
9454 +               AuDebugOn(!h_dentry);
9455 +               if (!IS_ERR(h_dentry)) {
9456 +                       if (!au_d_hashed_positive(h_dentry))
9457 +                               goto out; /* success */
9458 +                       dput(h_dentry);
9459 +                       h_dentry = NULL;
9460 +               }
9461 +       }
9462 +
9463 +out:
9464 +       AuDbgDentry(h_dentry);
9465 +       return h_dentry;
9466 +}
9467 +
9468 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9469 +{
9470 +       aufs_bindex_t bbot, bwh;
9471 +
9472 +       bbot = au_dbbot(dentry);
9473 +       if (0 <= bbot) {
9474 +               bwh = au_dbwh(dentry);
9475 +               if (!bwh)
9476 +                       return bwh;
9477 +               if (0 < bwh && bwh < bbot)
9478 +                       return bwh - 1;
9479 +       }
9480 +       return bbot;
9481 +}
9482 +
9483 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9484 +{
9485 +       aufs_bindex_t bbot, bopq;
9486 +
9487 +       bbot = au_dbtail(dentry);
9488 +       if (0 <= bbot) {
9489 +               bopq = au_dbdiropq(dentry);
9490 +               if (0 <= bopq && bopq < bbot)
9491 +                       bbot = bopq;
9492 +       }
9493 +       return bbot;
9494 +}
9495 +
9496 +/* ---------------------------------------------------------------------- */
9497 +
9498 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9499 +                  struct dentry *h_dentry)
9500 +{
9501 +       struct au_dinfo *dinfo;
9502 +       struct au_hdentry *hd;
9503 +       struct au_branch *br;
9504 +
9505 +       DiMustWriteLock(dentry);
9506 +
9507 +       dinfo = au_di(dentry);
9508 +       hd = au_hdentry(dinfo, bindex);
9509 +       au_hdput(hd);
9510 +       hd->hd_dentry = h_dentry;
9511 +       if (h_dentry) {
9512 +               br = au_sbr(dentry->d_sb, bindex);
9513 +               hd->hd_id = br->br_id;
9514 +       }
9515 +}
9516 +
9517 +int au_dbrange_test(struct dentry *dentry)
9518 +{
9519 +       int err;
9520 +       aufs_bindex_t btop, bbot;
9521 +
9522 +       err = 0;
9523 +       btop = au_dbtop(dentry);
9524 +       bbot = au_dbbot(dentry);
9525 +       if (btop >= 0)
9526 +               AuDebugOn(bbot < 0 && btop > bbot);
9527 +       else {
9528 +               err = -EIO;
9529 +               AuDebugOn(bbot >= 0);
9530 +       }
9531 +
9532 +       return err;
9533 +}
9534 +
9535 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9536 +{
9537 +       int err;
9538 +
9539 +       err = 0;
9540 +       if (unlikely(au_digen(dentry) != sigen
9541 +                    || au_iigen_test(d_inode(dentry), sigen)))
9542 +               err = -EIO;
9543 +
9544 +       return err;
9545 +}
9546 +
9547 +void au_update_digen(struct dentry *dentry)
9548 +{
9549 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9550 +       /* smp_mb(); */ /* atomic_set */
9551 +}
9552 +
9553 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9554 +{
9555 +       struct au_dinfo *dinfo;
9556 +       struct dentry *h_d;
9557 +       struct au_hdentry *hdp;
9558 +       aufs_bindex_t bindex, bbot;
9559 +
9560 +       DiMustWriteLock(dentry);
9561 +
9562 +       dinfo = au_di(dentry);
9563 +       if (!dinfo || dinfo->di_btop < 0)
9564 +               return;
9565 +
9566 +       if (do_put_zero) {
9567 +               bbot = dinfo->di_bbot;
9568 +               bindex = dinfo->di_btop;
9569 +               hdp = au_hdentry(dinfo, bindex);
9570 +               for (; bindex <= bbot; bindex++, hdp++) {
9571 +                       h_d = hdp->hd_dentry;
9572 +                       if (h_d && d_is_negative(h_d))
9573 +                               au_set_h_dptr(dentry, bindex, NULL);
9574 +               }
9575 +       }
9576 +
9577 +       dinfo->di_btop = 0;
9578 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9579 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9580 +               if (hdp->hd_dentry)
9581 +                       break;
9582 +       if (dinfo->di_btop > dinfo->di_bbot) {
9583 +               dinfo->di_btop = -1;
9584 +               dinfo->di_bbot = -1;
9585 +               return;
9586 +       }
9587 +
9588 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9589 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9590 +               if (hdp->hd_dentry)
9591 +                       break;
9592 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9593 +}
9594 +
9595 +void au_update_dbtop(struct dentry *dentry)
9596 +{
9597 +       aufs_bindex_t bindex, bbot;
9598 +       struct dentry *h_dentry;
9599 +
9600 +       bbot = au_dbbot(dentry);
9601 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9602 +               h_dentry = au_h_dptr(dentry, bindex);
9603 +               if (!h_dentry)
9604 +                       continue;
9605 +               if (d_is_positive(h_dentry)) {
9606 +                       au_set_dbtop(dentry, bindex);
9607 +                       return;
9608 +               }
9609 +               au_set_h_dptr(dentry, bindex, NULL);
9610 +       }
9611 +}
9612 +
9613 +void au_update_dbbot(struct dentry *dentry)
9614 +{
9615 +       aufs_bindex_t bindex, btop;
9616 +       struct dentry *h_dentry;
9617 +
9618 +       btop = au_dbtop(dentry);
9619 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9620 +               h_dentry = au_h_dptr(dentry, bindex);
9621 +               if (!h_dentry)
9622 +                       continue;
9623 +               if (d_is_positive(h_dentry)) {
9624 +                       au_set_dbbot(dentry, bindex);
9625 +                       return;
9626 +               }
9627 +               au_set_h_dptr(dentry, bindex, NULL);
9628 +       }
9629 +}
9630 +
9631 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9632 +{
9633 +       aufs_bindex_t bindex, bbot;
9634 +
9635 +       bbot = au_dbbot(dentry);
9636 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9637 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9638 +                       return bindex;
9639 +       return -1;
9640 +}
9641 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9642 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9643 +++ linux/fs/aufs/dir.c 2017-03-02 14:24:13.856922210 +0100
9644 @@ -0,0 +1,762 @@
9645 +/*
9646 + * Copyright (C) 2005-2017 Junjiro R. Okajima
9647 + *
9648 + * This program, aufs is free software; you can redistribute it and/or modify
9649 + * it under the terms of the GNU General Public License as published by
9650 + * the Free Software Foundation; either version 2 of the License, or
9651 + * (at your option) any later version.
9652 + *
9653 + * This program is distributed in the hope that it will be useful,
9654 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9655 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9656 + * GNU General Public License for more details.
9657 + *
9658 + * You should have received a copy of the GNU General Public License
9659 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9660 + */
9661 +
9662 +/*
9663 + * directory operations
9664 + */
9665 +
9666 +#include <linux/fs_stack.h>
9667 +#include "aufs.h"
9668 +
9669 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9670 +{
9671 +       unsigned int nlink;
9672 +
9673 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9674 +
9675 +       nlink = dir->i_nlink;
9676 +       nlink += h_dir->i_nlink - 2;
9677 +       if (h_dir->i_nlink < 2)
9678 +               nlink += 2;
9679 +       smp_mb(); /* for i_nlink */
9680 +       /* 0 can happen in revaliding */
9681 +       set_nlink(dir, nlink);
9682 +}
9683 +
9684 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9685 +{
9686 +       unsigned int nlink;
9687 +
9688 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9689 +
9690 +       nlink = dir->i_nlink;
9691 +       nlink -= h_dir->i_nlink - 2;
9692 +       if (h_dir->i_nlink < 2)
9693 +               nlink -= 2;
9694 +       smp_mb(); /* for i_nlink */
9695 +       /* nlink == 0 means the branch-fs is broken */
9696 +       set_nlink(dir, nlink);
9697 +}
9698 +
9699 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9700 +{
9701 +       loff_t sz;
9702 +       aufs_bindex_t bindex, bbot;
9703 +       struct file *h_file;
9704 +       struct dentry *h_dentry;
9705 +
9706 +       sz = 0;
9707 +       if (file) {
9708 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9709 +
9710 +               bbot = au_fbbot_dir(file);
9711 +               for (bindex = au_fbtop(file);
9712 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
9713 +                    bindex++) {
9714 +                       h_file = au_hf_dir(file, bindex);
9715 +                       if (h_file && file_inode(h_file))
9716 +                               sz += vfsub_f_size_read(h_file);
9717 +               }
9718 +       } else {
9719 +               AuDebugOn(!dentry);
9720 +               AuDebugOn(!d_is_dir(dentry));
9721 +
9722 +               bbot = au_dbtaildir(dentry);
9723 +               for (bindex = au_dbtop(dentry);
9724 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
9725 +                    bindex++) {
9726 +                       h_dentry = au_h_dptr(dentry, bindex);
9727 +                       if (h_dentry && d_is_positive(h_dentry))
9728 +                               sz += i_size_read(d_inode(h_dentry));
9729 +               }
9730 +       }
9731 +       if (sz < KMALLOC_MAX_SIZE)
9732 +               sz = roundup_pow_of_two(sz);
9733 +       if (sz > KMALLOC_MAX_SIZE)
9734 +               sz = KMALLOC_MAX_SIZE;
9735 +       else if (sz < NAME_MAX) {
9736 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9737 +               sz = AUFS_RDBLK_DEF;
9738 +       }
9739 +       return sz;
9740 +}
9741 +
9742 +struct au_dir_ts_arg {
9743 +       struct dentry *dentry;
9744 +       aufs_bindex_t brid;
9745 +};
9746 +
9747 +static void au_do_dir_ts(void *arg)
9748 +{
9749 +       struct au_dir_ts_arg *a = arg;
9750 +       struct au_dtime dt;
9751 +       struct path h_path;
9752 +       struct inode *dir, *h_dir;
9753 +       struct super_block *sb;
9754 +       struct au_branch *br;
9755 +       struct au_hinode *hdir;
9756 +       int err;
9757 +       aufs_bindex_t btop, bindex;
9758 +
9759 +       sb = a->dentry->d_sb;
9760 +       if (d_really_is_negative(a->dentry))
9761 +               goto out;
9762 +       /* no dir->i_mutex lock */
9763 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9764 +
9765 +       dir = d_inode(a->dentry);
9766 +       btop = au_ibtop(dir);
9767 +       bindex = au_br_index(sb, a->brid);
9768 +       if (bindex < btop)
9769 +               goto out_unlock;
9770 +
9771 +       br = au_sbr(sb, bindex);
9772 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
9773 +       if (!h_path.dentry)
9774 +               goto out_unlock;
9775 +       h_path.mnt = au_br_mnt(br);
9776 +       au_dtime_store(&dt, a->dentry, &h_path);
9777 +
9778 +       br = au_sbr(sb, btop);
9779 +       if (!au_br_writable(br->br_perm))
9780 +               goto out_unlock;
9781 +       h_path.dentry = au_h_dptr(a->dentry, btop);
9782 +       h_path.mnt = au_br_mnt(br);
9783 +       err = vfsub_mnt_want_write(h_path.mnt);
9784 +       if (err)
9785 +               goto out_unlock;
9786 +       hdir = au_hi(dir, btop);
9787 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
9788 +       h_dir = au_h_iptr(dir, btop);
9789 +       if (h_dir->i_nlink
9790 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9791 +               dt.dt_h_path = h_path;
9792 +               au_dtime_revert(&dt);
9793 +       }
9794 +       au_hn_inode_unlock(hdir);
9795 +       vfsub_mnt_drop_write(h_path.mnt);
9796 +       au_cpup_attr_timesizes(dir);
9797 +
9798 +out_unlock:
9799 +       aufs_read_unlock(a->dentry, AuLock_DW);
9800 +out:
9801 +       dput(a->dentry);
9802 +       au_nwt_done(&au_sbi(sb)->si_nowait);
9803 +       au_delayed_kfree(arg);
9804 +}
9805 +
9806 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9807 +{
9808 +       int perm, wkq_err;
9809 +       aufs_bindex_t btop;
9810 +       struct au_dir_ts_arg *arg;
9811 +       struct dentry *dentry;
9812 +       struct super_block *sb;
9813 +
9814 +       IMustLock(dir);
9815 +
9816 +       dentry = d_find_any_alias(dir);
9817 +       AuDebugOn(!dentry);
9818 +       sb = dentry->d_sb;
9819 +       btop = au_ibtop(dir);
9820 +       if (btop == bindex) {
9821 +               au_cpup_attr_timesizes(dir);
9822 +               goto out;
9823 +       }
9824 +
9825 +       perm = au_sbr_perm(sb, btop);
9826 +       if (!au_br_writable(perm))
9827 +               goto out;
9828 +
9829 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
9830 +       if (!arg)
9831 +               goto out;
9832 +
9833 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9834 +       arg->brid = au_sbr_id(sb, bindex);
9835 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9836 +       if (unlikely(wkq_err)) {
9837 +               pr_err("wkq %d\n", wkq_err);
9838 +               dput(dentry);
9839 +               au_delayed_kfree(arg);
9840 +       }
9841 +
9842 +out:
9843 +       dput(dentry);
9844 +}
9845 +
9846 +/* ---------------------------------------------------------------------- */
9847 +
9848 +static int reopen_dir(struct file *file)
9849 +{
9850 +       int err;
9851 +       unsigned int flags;
9852 +       aufs_bindex_t bindex, btail, btop;
9853 +       struct dentry *dentry, *h_dentry;
9854 +       struct file *h_file;
9855 +
9856 +       /* open all lower dirs */
9857 +       dentry = file->f_path.dentry;
9858 +       btop = au_dbtop(dentry);
9859 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
9860 +               au_set_h_fptr(file, bindex, NULL);
9861 +       au_set_fbtop(file, btop);
9862 +
9863 +       btail = au_dbtaildir(dentry);
9864 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
9865 +               au_set_h_fptr(file, bindex, NULL);
9866 +       au_set_fbbot_dir(file, btail);
9867 +
9868 +       flags = vfsub_file_flags(file);
9869 +       for (bindex = btop; bindex <= btail; bindex++) {
9870 +               h_dentry = au_h_dptr(dentry, bindex);
9871 +               if (!h_dentry)
9872 +                       continue;
9873 +               h_file = au_hf_dir(file, bindex);
9874 +               if (h_file)
9875 +                       continue;
9876 +
9877 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9878 +               err = PTR_ERR(h_file);
9879 +               if (IS_ERR(h_file))
9880 +                       goto out; /* close all? */
9881 +               au_set_h_fptr(file, bindex, h_file);
9882 +       }
9883 +       au_update_figen(file);
9884 +       /* todo: necessary? */
9885 +       /* file->f_ra = h_file->f_ra; */
9886 +       err = 0;
9887 +
9888 +out:
9889 +       return err;
9890 +}
9891 +
9892 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9893 +{
9894 +       int err;
9895 +       aufs_bindex_t bindex, btail;
9896 +       struct dentry *dentry, *h_dentry;
9897 +       struct vfsmount *mnt;
9898 +
9899 +       FiMustWriteLock(file);
9900 +       AuDebugOn(h_file);
9901 +
9902 +       err = 0;
9903 +       mnt = file->f_path.mnt;
9904 +       dentry = file->f_path.dentry;
9905 +       file->f_version = d_inode(dentry)->i_version;
9906 +       bindex = au_dbtop(dentry);
9907 +       au_set_fbtop(file, bindex);
9908 +       btail = au_dbtaildir(dentry);
9909 +       au_set_fbbot_dir(file, btail);
9910 +       for (; !err && bindex <= btail; bindex++) {
9911 +               h_dentry = au_h_dptr(dentry, bindex);
9912 +               if (!h_dentry)
9913 +                       continue;
9914 +
9915 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
9916 +               if (unlikely(err))
9917 +                       break;
9918 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9919 +               if (IS_ERR(h_file)) {
9920 +                       err = PTR_ERR(h_file);
9921 +                       break;
9922 +               }
9923 +               au_set_h_fptr(file, bindex, h_file);
9924 +       }
9925 +       au_update_figen(file);
9926 +       /* todo: necessary? */
9927 +       /* file->f_ra = h_file->f_ra; */
9928 +       if (!err)
9929 +               return 0; /* success */
9930 +
9931 +       /* close all */
9932 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
9933 +               au_set_h_fptr(file, bindex, NULL);
9934 +       au_set_fbtop(file, -1);
9935 +       au_set_fbbot_dir(file, -1);
9936 +
9937 +       return err;
9938 +}
9939 +
9940 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9941 +                        struct file *file)
9942 +{
9943 +       int err;
9944 +       struct super_block *sb;
9945 +       struct au_fidir *fidir;
9946 +
9947 +       err = -ENOMEM;
9948 +       sb = file->f_path.dentry->d_sb;
9949 +       si_read_lock(sb, AuLock_FLUSH);
9950 +       fidir = au_fidir_alloc(sb);
9951 +       if (fidir) {
9952 +               struct au_do_open_args args = {
9953 +                       .open   = do_open_dir,
9954 +                       .fidir  = fidir
9955 +               };
9956 +               err = au_do_open(file, &args);
9957 +               if (unlikely(err))
9958 +                       au_delayed_kfree(fidir);
9959 +       }
9960 +       si_read_unlock(sb);
9961 +       return err;
9962 +}
9963 +
9964 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9965 +                           struct file *file)
9966 +{
9967 +       struct au_vdir *vdir_cache;
9968 +       struct au_finfo *finfo;
9969 +       struct au_fidir *fidir;
9970 +       struct au_hfile *hf;
9971 +       aufs_bindex_t bindex, bbot;
9972 +       int execed, delayed;
9973 +
9974 +       delayed = (current->flags & PF_KTHREAD) || in_interrupt();
9975 +       finfo = au_fi(file);
9976 +       fidir = finfo->fi_hdir;
9977 +       if (fidir) {
9978 +               au_sphl_del(&finfo->fi_hlist,
9979 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
9980 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9981 +               if (vdir_cache)
9982 +                       au_vdir_free(vdir_cache, delayed);
9983 +
9984 +               bindex = finfo->fi_btop;
9985 +               if (bindex >= 0) {
9986 +                       execed = vfsub_file_execed(file);
9987 +                       hf = fidir->fd_hfile + bindex;
9988 +                       /*
9989 +                        * calls fput() instead of filp_close(),
9990 +                        * since no dnotify or lock for the lower file.
9991 +                        */
9992 +                       bbot = fidir->fd_bbot;
9993 +                       for (; bindex <= bbot; bindex++, hf++)
9994 +                               if (hf->hf_file)
9995 +                                       au_hfput(hf, execed);
9996 +               }
9997 +               au_delayed_kfree(fidir);
9998 +               finfo->fi_hdir = NULL;
9999 +       }
10000 +       au_finfo_fin(file, delayed);
10001 +       return 0;
10002 +}
10003 +
10004 +/* ---------------------------------------------------------------------- */
10005 +
10006 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10007 +{
10008 +       int err;
10009 +       aufs_bindex_t bindex, bbot;
10010 +       struct file *h_file;
10011 +
10012 +       err = 0;
10013 +       bbot = au_fbbot_dir(file);
10014 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10015 +               h_file = au_hf_dir(file, bindex);
10016 +               if (h_file)
10017 +                       err = vfsub_flush(h_file, id);
10018 +       }
10019 +       return err;
10020 +}
10021 +
10022 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10023 +{
10024 +       return au_do_flush(file, id, au_do_flush_dir);
10025 +}
10026 +
10027 +/* ---------------------------------------------------------------------- */
10028 +
10029 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10030 +{
10031 +       int err;
10032 +       aufs_bindex_t bbot, bindex;
10033 +       struct inode *inode;
10034 +       struct super_block *sb;
10035 +
10036 +       err = 0;
10037 +       sb = dentry->d_sb;
10038 +       inode = d_inode(dentry);
10039 +       IMustLock(inode);
10040 +       bbot = au_dbbot(dentry);
10041 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10042 +               struct path h_path;
10043 +
10044 +               if (au_test_ro(sb, bindex, inode))
10045 +                       continue;
10046 +               h_path.dentry = au_h_dptr(dentry, bindex);
10047 +               if (!h_path.dentry)
10048 +                       continue;
10049 +
10050 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10051 +               err = vfsub_fsync(NULL, &h_path, datasync);
10052 +       }
10053 +
10054 +       return err;
10055 +}
10056 +
10057 +static int au_do_fsync_dir(struct file *file, int datasync)
10058 +{
10059 +       int err;
10060 +       aufs_bindex_t bbot, bindex;
10061 +       struct file *h_file;
10062 +       struct super_block *sb;
10063 +       struct inode *inode;
10064 +
10065 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
10066 +       if (unlikely(err))
10067 +               goto out;
10068 +
10069 +       inode = file_inode(file);
10070 +       sb = inode->i_sb;
10071 +       bbot = au_fbbot_dir(file);
10072 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10073 +               h_file = au_hf_dir(file, bindex);
10074 +               if (!h_file || au_test_ro(sb, bindex, inode))
10075 +                       continue;
10076 +
10077 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10078 +       }
10079 +
10080 +out:
10081 +       return err;
10082 +}
10083 +
10084 +/*
10085 + * @file may be NULL
10086 + */
10087 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10088 +                         int datasync)
10089 +{
10090 +       int err;
10091 +       struct dentry *dentry;
10092 +       struct inode *inode;
10093 +       struct super_block *sb;
10094 +
10095 +       err = 0;
10096 +       dentry = file->f_path.dentry;
10097 +       inode = d_inode(dentry);
10098 +       inode_lock(inode);
10099 +       sb = dentry->d_sb;
10100 +       si_noflush_read_lock(sb);
10101 +       if (file)
10102 +               err = au_do_fsync_dir(file, datasync);
10103 +       else {
10104 +               di_write_lock_child(dentry);
10105 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10106 +       }
10107 +       au_cpup_attr_timesizes(inode);
10108 +       di_write_unlock(dentry);
10109 +       if (file)
10110 +               fi_write_unlock(file);
10111 +
10112 +       si_read_unlock(sb);
10113 +       inode_unlock(inode);
10114 +       return err;
10115 +}
10116 +
10117 +/* ---------------------------------------------------------------------- */
10118 +
10119 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10120 +{
10121 +       int err;
10122 +       struct dentry *dentry;
10123 +       struct inode *inode, *h_inode;
10124 +       struct super_block *sb;
10125 +
10126 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
10127 +
10128 +       dentry = file->f_path.dentry;
10129 +       inode = d_inode(dentry);
10130 +       IMustLock(inode);
10131 +
10132 +       sb = dentry->d_sb;
10133 +       si_read_lock(sb, AuLock_FLUSH);
10134 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
10135 +       if (unlikely(err))
10136 +               goto out;
10137 +       err = au_alive_dir(dentry);
10138 +       if (!err)
10139 +               err = au_vdir_init(file);
10140 +       di_downgrade_lock(dentry, AuLock_IR);
10141 +       if (unlikely(err))
10142 +               goto out_unlock;
10143 +
10144 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10145 +       if (!au_test_nfsd()) {
10146 +               err = au_vdir_fill_de(file, ctx);
10147 +               fsstack_copy_attr_atime(inode, h_inode);
10148 +       } else {
10149 +               /*
10150 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10151 +                * encode_fh() and others.
10152 +                */
10153 +               atomic_inc(&h_inode->i_count);
10154 +               di_read_unlock(dentry, AuLock_IR);
10155 +               si_read_unlock(sb);
10156 +               err = au_vdir_fill_de(file, ctx);
10157 +               fsstack_copy_attr_atime(inode, h_inode);
10158 +               fi_write_unlock(file);
10159 +               iput(h_inode);
10160 +
10161 +               AuTraceErr(err);
10162 +               return err;
10163 +       }
10164 +
10165 +out_unlock:
10166 +       di_read_unlock(dentry, AuLock_IR);
10167 +       fi_write_unlock(file);
10168 +out:
10169 +       si_read_unlock(sb);
10170 +       return err;
10171 +}
10172 +
10173 +/* ---------------------------------------------------------------------- */
10174 +
10175 +#define AuTestEmpty_WHONLY     1
10176 +#define AuTestEmpty_CALLED     (1 << 1)
10177 +#define AuTestEmpty_SHWH       (1 << 2)
10178 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10179 +#define au_fset_testempty(flags, name) \
10180 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10181 +#define au_fclr_testempty(flags, name) \
10182 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10183 +
10184 +#ifndef CONFIG_AUFS_SHWH
10185 +#undef AuTestEmpty_SHWH
10186 +#define AuTestEmpty_SHWH       0
10187 +#endif
10188 +
10189 +struct test_empty_arg {
10190 +       struct dir_context ctx;
10191 +       struct au_nhash *whlist;
10192 +       unsigned int flags;
10193 +       int err;
10194 +       aufs_bindex_t bindex;
10195 +};
10196 +
10197 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10198 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10199 +                        unsigned int d_type)
10200 +{
10201 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10202 +                                                 ctx);
10203 +       char *name = (void *)__name;
10204 +
10205 +       arg->err = 0;
10206 +       au_fset_testempty(arg->flags, CALLED);
10207 +       /* smp_mb(); */
10208 +       if (name[0] == '.'
10209 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10210 +               goto out; /* success */
10211 +
10212 +       if (namelen <= AUFS_WH_PFX_LEN
10213 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10214 +               if (au_ftest_testempty(arg->flags, WHONLY)
10215 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10216 +                       arg->err = -ENOTEMPTY;
10217 +               goto out;
10218 +       }
10219 +
10220 +       name += AUFS_WH_PFX_LEN;
10221 +       namelen -= AUFS_WH_PFX_LEN;
10222 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10223 +               arg->err = au_nhash_append_wh
10224 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10225 +                        au_ftest_testempty(arg->flags, SHWH));
10226 +
10227 +out:
10228 +       /* smp_mb(); */
10229 +       AuTraceErr(arg->err);
10230 +       return arg->err;
10231 +}
10232 +
10233 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10234 +{
10235 +       int err;
10236 +       struct file *h_file;
10237 +
10238 +       h_file = au_h_open(dentry, arg->bindex,
10239 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10240 +                          /*file*/NULL, /*force_wr*/0);
10241 +       err = PTR_ERR(h_file);
10242 +       if (IS_ERR(h_file))
10243 +               goto out;
10244 +
10245 +       err = 0;
10246 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10247 +           && !file_inode(h_file)->i_nlink)
10248 +               goto out_put;
10249 +
10250 +       do {
10251 +               arg->err = 0;
10252 +               au_fclr_testempty(arg->flags, CALLED);
10253 +               /* smp_mb(); */
10254 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10255 +               if (err >= 0)
10256 +                       err = arg->err;
10257 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10258 +
10259 +out_put:
10260 +       fput(h_file);
10261 +       au_sbr_put(dentry->d_sb, arg->bindex);
10262 +out:
10263 +       return err;
10264 +}
10265 +
10266 +struct do_test_empty_args {
10267 +       int *errp;
10268 +       struct dentry *dentry;
10269 +       struct test_empty_arg *arg;
10270 +};
10271 +
10272 +static void call_do_test_empty(void *args)
10273 +{
10274 +       struct do_test_empty_args *a = args;
10275 +       *a->errp = do_test_empty(a->dentry, a->arg);
10276 +}
10277 +
10278 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10279 +{
10280 +       int err, wkq_err;
10281 +       struct dentry *h_dentry;
10282 +       struct inode *h_inode;
10283 +
10284 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10285 +       h_inode = d_inode(h_dentry);
10286 +       /* todo: i_mode changes anytime? */
10287 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
10288 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
10289 +       inode_unlock(h_inode);
10290 +       if (!err)
10291 +               err = do_test_empty(dentry, arg);
10292 +       else {
10293 +               struct do_test_empty_args args = {
10294 +                       .errp   = &err,
10295 +                       .dentry = dentry,
10296 +                       .arg    = arg
10297 +               };
10298 +               unsigned int flags = arg->flags;
10299 +
10300 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10301 +               if (unlikely(wkq_err))
10302 +                       err = wkq_err;
10303 +               arg->flags = flags;
10304 +       }
10305 +
10306 +       return err;
10307 +}
10308 +
10309 +int au_test_empty_lower(struct dentry *dentry)
10310 +{
10311 +       int err;
10312 +       unsigned int rdhash;
10313 +       aufs_bindex_t bindex, btop, btail;
10314 +       struct au_nhash whlist;
10315 +       struct test_empty_arg arg = {
10316 +               .ctx = {
10317 +                       .actor = test_empty_cb
10318 +               }
10319 +       };
10320 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10321 +
10322 +       SiMustAnyLock(dentry->d_sb);
10323 +
10324 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10325 +       if (!rdhash)
10326 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10327 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10328 +       if (unlikely(err))
10329 +               goto out;
10330 +
10331 +       arg.flags = 0;
10332 +       arg.whlist = &whlist;
10333 +       btop = au_dbtop(dentry);
10334 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10335 +               au_fset_testempty(arg.flags, SHWH);
10336 +       test_empty = do_test_empty;
10337 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10338 +               test_empty = sio_test_empty;
10339 +       arg.bindex = btop;
10340 +       err = test_empty(dentry, &arg);
10341 +       if (unlikely(err))
10342 +               goto out_whlist;
10343 +
10344 +       au_fset_testempty(arg.flags, WHONLY);
10345 +       btail = au_dbtaildir(dentry);
10346 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10347 +               struct dentry *h_dentry;
10348 +
10349 +               h_dentry = au_h_dptr(dentry, bindex);
10350 +               if (h_dentry && d_is_positive(h_dentry)) {
10351 +                       arg.bindex = bindex;
10352 +                       err = test_empty(dentry, &arg);
10353 +               }
10354 +       }
10355 +
10356 +out_whlist:
10357 +       au_nhash_wh_free(&whlist);
10358 +out:
10359 +       return err;
10360 +}
10361 +
10362 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10363 +{
10364 +       int err;
10365 +       struct test_empty_arg arg = {
10366 +               .ctx = {
10367 +                       .actor = test_empty_cb
10368 +               }
10369 +       };
10370 +       aufs_bindex_t bindex, btail;
10371 +
10372 +       err = 0;
10373 +       arg.whlist = whlist;
10374 +       arg.flags = AuTestEmpty_WHONLY;
10375 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10376 +               au_fset_testempty(arg.flags, SHWH);
10377 +       btail = au_dbtaildir(dentry);
10378 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10379 +               struct dentry *h_dentry;
10380 +
10381 +               h_dentry = au_h_dptr(dentry, bindex);
10382 +               if (h_dentry && d_is_positive(h_dentry)) {
10383 +                       arg.bindex = bindex;
10384 +                       err = sio_test_empty(dentry, &arg);
10385 +               }
10386 +       }
10387 +
10388 +       return err;
10389 +}
10390 +
10391 +/* ---------------------------------------------------------------------- */
10392 +
10393 +const struct file_operations aufs_dir_fop = {
10394 +       .owner          = THIS_MODULE,
10395 +       .llseek         = default_llseek,
10396 +       .read           = generic_read_dir,
10397 +       .iterate_shared = aufs_iterate_shared,
10398 +       .unlocked_ioctl = aufs_ioctl_dir,
10399 +#ifdef CONFIG_COMPAT
10400 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10401 +#endif
10402 +       .open           = aufs_open_dir,
10403 +       .release        = aufs_release_dir,
10404 +       .flush          = aufs_flush_dir,
10405 +       .fsync          = aufs_fsync_dir
10406 +};
10407 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10408 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10409 +++ linux/fs/aufs/dir.h 2017-03-02 14:24:13.856922210 +0100
10410 @@ -0,0 +1,137 @@
10411 +/*
10412 + * Copyright (C) 2005-2017 Junjiro R. Okajima
10413 + *
10414 + * This program, aufs is free software; you can redistribute it and/or modify
10415 + * it under the terms of the GNU General Public License as published by
10416 + * the Free Software Foundation; either version 2 of the License, or
10417 + * (at your option) any later version.
10418 + *
10419 + * This program is distributed in the hope that it will be useful,
10420 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10421 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10422 + * GNU General Public License for more details.
10423 + *
10424 + * You should have received a copy of the GNU General Public License
10425 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10426 + */
10427 +
10428 +/*
10429 + * directory operations
10430 + */
10431 +
10432 +#ifndef __AUFS_DIR_H__
10433 +#define __AUFS_DIR_H__
10434 +
10435 +#ifdef __KERNEL__
10436 +
10437 +#include <linux/fs.h>
10438 +
10439 +/* ---------------------------------------------------------------------- */
10440 +
10441 +/* need to be faster and smaller */
10442 +
10443 +struct au_nhash {
10444 +       unsigned int            nh_num;
10445 +       struct hlist_head       *nh_head;
10446 +};
10447 +
10448 +struct au_vdir_destr {
10449 +       unsigned char   len;
10450 +       unsigned char   name[0];
10451 +} __packed;
10452 +
10453 +struct au_vdir_dehstr {
10454 +       struct hlist_node       hash;
10455 +       union {
10456 +               struct au_vdir_destr    *str;
10457 +               struct llist_node       lnode;  /* delayed free */
10458 +       };
10459 +} ____cacheline_aligned_in_smp;
10460 +
10461 +struct au_vdir_de {
10462 +       ino_t                   de_ino;
10463 +       unsigned char           de_type;
10464 +       /* caution: packed */
10465 +       struct au_vdir_destr    de_str;
10466 +} __packed;
10467 +
10468 +struct au_vdir_wh {
10469 +       struct hlist_node       wh_hash;
10470 +#ifdef CONFIG_AUFS_SHWH
10471 +       ino_t                   wh_ino;
10472 +       aufs_bindex_t           wh_bindex;
10473 +       unsigned char           wh_type;
10474 +#else
10475 +       aufs_bindex_t           wh_bindex;
10476 +#endif
10477 +       /* caution: packed */
10478 +       struct au_vdir_destr    wh_str;
10479 +} __packed;
10480 +
10481 +union au_vdir_deblk_p {
10482 +       unsigned char           *deblk;
10483 +       struct au_vdir_de       *de;
10484 +};
10485 +
10486 +struct au_vdir {
10487 +       unsigned char   **vd_deblk;
10488 +       unsigned long   vd_nblk;
10489 +       struct {
10490 +               unsigned long           ul;
10491 +               union au_vdir_deblk_p   p;
10492 +       } vd_last;
10493 +
10494 +       unsigned long   vd_version;
10495 +       unsigned int    vd_deblk_sz;
10496 +       union {
10497 +               unsigned long           vd_jiffy;
10498 +               struct llist_node       vd_lnode;       /* delayed free */
10499 +       };
10500 +} ____cacheline_aligned_in_smp;
10501 +
10502 +/* ---------------------------------------------------------------------- */
10503 +
10504 +/* dir.c */
10505 +extern const struct file_operations aufs_dir_fop;
10506 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10507 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10508 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10509 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10510 +int au_test_empty_lower(struct dentry *dentry);
10511 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10512 +
10513 +/* vdir.c */
10514 +unsigned int au_rdhash_est(loff_t sz);
10515 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10516 +void au_nhash_wh_free(struct au_nhash *whlist);
10517 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10518 +                           int limit);
10519 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10520 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10521 +                      unsigned int d_type, aufs_bindex_t bindex,
10522 +                      unsigned char shwh);
10523 +void au_vdir_free(struct au_vdir *vdir, int atonce);
10524 +int au_vdir_init(struct file *file);
10525 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10526 +
10527 +/* ioctl.c */
10528 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10529 +
10530 +#ifdef CONFIG_AUFS_RDU
10531 +/* rdu.c */
10532 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10533 +#ifdef CONFIG_COMPAT
10534 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10535 +                        unsigned long arg);
10536 +#endif
10537 +#else
10538 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10539 +       unsigned int cmd, unsigned long arg)
10540 +#ifdef CONFIG_COMPAT
10541 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10542 +       unsigned int cmd, unsigned long arg)
10543 +#endif
10544 +#endif
10545 +
10546 +#endif /* __KERNEL__ */
10547 +#endif /* __AUFS_DIR_H__ */
10548 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10549 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
10550 +++ linux/fs/aufs/dynop.c       2017-03-02 14:24:13.860255635 +0100
10551 @@ -0,0 +1,371 @@
10552 +/*
10553 + * Copyright (C) 2010-2017 Junjiro R. Okajima
10554 + *
10555 + * This program, aufs is free software; you can redistribute it and/or modify
10556 + * it under the terms of the GNU General Public License as published by
10557 + * the Free Software Foundation; either version 2 of the License, or
10558 + * (at your option) any later version.
10559 + *
10560 + * This program is distributed in the hope that it will be useful,
10561 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10562 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10563 + * GNU General Public License for more details.
10564 + *
10565 + * You should have received a copy of the GNU General Public License
10566 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10567 + */
10568 +
10569 +/*
10570 + * dynamically customizable operations for regular files
10571 + */
10572 +
10573 +#include "aufs.h"
10574 +
10575 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
10576 +
10577 +/*
10578 + * How large will these lists be?
10579 + * Usually just a few elements, 20-30 at most for each, I guess.
10580 + */
10581 +static struct au_sphlhead dynop[AuDyLast];
10582 +
10583 +static struct au_dykey *dy_gfind_get(struct au_sphlhead *sphl, const void *h_op)
10584 +{
10585 +       struct au_dykey *key, *tmp;
10586 +       struct hlist_head *head;
10587 +
10588 +       key = NULL;
10589 +       head = &sphl->head;
10590 +       rcu_read_lock();
10591 +       hlist_for_each_entry_rcu(tmp, head, dk_hnode)
10592 +               if (tmp->dk_op.dy_hop == h_op) {
10593 +                       key = tmp;
10594 +                       kref_get(&key->dk_kref);
10595 +                       break;
10596 +               }
10597 +       rcu_read_unlock();
10598 +
10599 +       return key;
10600 +}
10601 +
10602 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
10603 +{
10604 +       struct au_dykey **k, *found;
10605 +       const void *h_op = key->dk_op.dy_hop;
10606 +       int i;
10607 +
10608 +       found = NULL;
10609 +       k = br->br_dykey;
10610 +       for (i = 0; i < AuBrDynOp; i++)
10611 +               if (k[i]) {
10612 +                       if (k[i]->dk_op.dy_hop == h_op) {
10613 +                               found = k[i];
10614 +                               break;
10615 +                       }
10616 +               } else
10617 +                       break;
10618 +       if (!found) {
10619 +               spin_lock(&br->br_dykey_lock);
10620 +               for (; i < AuBrDynOp; i++)
10621 +                       if (k[i]) {
10622 +                               if (k[i]->dk_op.dy_hop == h_op) {
10623 +                                       found = k[i];
10624 +                                       break;
10625 +                               }
10626 +                       } else {
10627 +                               k[i] = key;
10628 +                               break;
10629 +                       }
10630 +               spin_unlock(&br->br_dykey_lock);
10631 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10632 +       }
10633 +
10634 +       return found;
10635 +}
10636 +
10637 +/* kref_get() if @key is already added */
10638 +static struct au_dykey *dy_gadd(struct au_sphlhead *sphl, struct au_dykey *key)
10639 +{
10640 +       struct au_dykey *tmp, *found;
10641 +       struct hlist_head *head;
10642 +       const void *h_op = key->dk_op.dy_hop;
10643 +
10644 +       found = NULL;
10645 +       head = &sphl->head;
10646 +       spin_lock(&sphl->spin);
10647 +       hlist_for_each_entry(tmp, head, dk_hnode)
10648 +               if (tmp->dk_op.dy_hop == h_op) {
10649 +                       kref_get(&tmp->dk_kref);
10650 +                       found = tmp;
10651 +                       break;
10652 +               }
10653 +       if (!found)
10654 +               hlist_add_head_rcu(&key->dk_hnode, head);
10655 +       spin_unlock(&sphl->spin);
10656 +
10657 +       if (!found)
10658 +               DyPrSym(key);
10659 +       return found;
10660 +}
10661 +
10662 +static void dy_free_rcu(struct rcu_head *rcu)
10663 +{
10664 +       struct au_dykey *key;
10665 +
10666 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10667 +       DyPrSym(key);
10668 +       kfree(key);     /* not delayed */
10669 +}
10670 +
10671 +static void dy_free(struct kref *kref)
10672 +{
10673 +       struct au_dykey *key;
10674 +       struct au_sphlhead *sphl;
10675 +
10676 +       key = container_of(kref, struct au_dykey, dk_kref);
10677 +       sphl = dynop + key->dk_op.dy_type;
10678 +       au_sphl_del_rcu(&key->dk_hnode, sphl);
10679 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10680 +}
10681 +
10682 +void au_dy_put(struct au_dykey *key)
10683 +{
10684 +       kref_put(&key->dk_kref, dy_free);
10685 +}
10686 +
10687 +/* ---------------------------------------------------------------------- */
10688 +
10689 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10690 +
10691 +#ifdef CONFIG_AUFS_DEBUG
10692 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10693 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10694 +#else
10695 +#define DyDbgDeclare(cnt)      do {} while (0)
10696 +#define DyDbgInc(cnt)          do {} while (0)
10697 +#endif
10698 +
10699 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10700 +       DyDbgInc(cnt);                                                  \
10701 +       if (h_op->func) {                                               \
10702 +               if (src.func)                                           \
10703 +                       dst.func = src.func;                            \
10704 +               else                                                    \
10705 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10706 +       }                                                               \
10707 +} while (0)
10708 +
10709 +#define DySetForce(func, dst, src) do {                \
10710 +       AuDebugOn(!src.func);                   \
10711 +       DyDbgInc(cnt);                          \
10712 +       dst.func = src.func;                    \
10713 +} while (0)
10714 +
10715 +#define DySetAop(func) \
10716 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10717 +#define DySetAopForce(func) \
10718 +       DySetForce(func, dyaop->da_op, aufs_aop)
10719 +
10720 +static void dy_aop(struct au_dykey *key, const void *h_op,
10721 +                  struct super_block *h_sb __maybe_unused)
10722 +{
10723 +       struct au_dyaop *dyaop = (void *)key;
10724 +       const struct address_space_operations *h_aop = h_op;
10725 +       DyDbgDeclare(cnt);
10726 +
10727 +       AuDbg("%s\n", au_sbtype(h_sb));
10728 +
10729 +       DySetAop(writepage);
10730 +       DySetAopForce(readpage);        /* force */
10731 +       DySetAop(writepages);
10732 +       DySetAop(set_page_dirty);
10733 +       DySetAop(readpages);
10734 +       DySetAop(write_begin);
10735 +       DySetAop(write_end);
10736 +       DySetAop(bmap);
10737 +       DySetAop(invalidatepage);
10738 +       DySetAop(releasepage);
10739 +       DySetAop(freepage);
10740 +       /* this one will be changed according to an aufs mount option */
10741 +       DySetAop(direct_IO);
10742 +       DySetAop(migratepage);
10743 +       DySetAop(isolate_page);
10744 +       DySetAop(putback_page);
10745 +       DySetAop(launder_page);
10746 +       DySetAop(is_partially_uptodate);
10747 +       DySetAop(is_dirty_writeback);
10748 +       DySetAop(error_remove_page);
10749 +       DySetAop(swap_activate);
10750 +       DySetAop(swap_deactivate);
10751 +
10752 +       DyDbgSize(cnt, *h_aop);
10753 +}
10754 +
10755 +/* ---------------------------------------------------------------------- */
10756 +
10757 +static void dy_bug(struct kref *kref)
10758 +{
10759 +       BUG();
10760 +}
10761 +
10762 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10763 +{
10764 +       struct au_dykey *key, *old;
10765 +       struct au_sphlhead *sphl;
10766 +       struct op {
10767 +               unsigned int sz;
10768 +               void (*set)(struct au_dykey *key, const void *h_op,
10769 +                           struct super_block *h_sb __maybe_unused);
10770 +       };
10771 +       static const struct op a[] = {
10772 +               [AuDy_AOP] = {
10773 +                       .sz     = sizeof(struct au_dyaop),
10774 +                       .set    = dy_aop
10775 +               }
10776 +       };
10777 +       const struct op *p;
10778 +
10779 +       sphl = dynop + op->dy_type;
10780 +       key = dy_gfind_get(sphl, op->dy_hop);
10781 +       if (key)
10782 +               goto out_add; /* success */
10783 +
10784 +       p = a + op->dy_type;
10785 +       key = kzalloc(p->sz, GFP_NOFS);
10786 +       if (unlikely(!key)) {
10787 +               key = ERR_PTR(-ENOMEM);
10788 +               goto out;
10789 +       }
10790 +
10791 +       key->dk_op.dy_hop = op->dy_hop;
10792 +       kref_init(&key->dk_kref);
10793 +       p->set(key, op->dy_hop, au_br_sb(br));
10794 +       old = dy_gadd(sphl, key);
10795 +       if (old) {
10796 +               au_delayed_kfree(key);
10797 +               key = old;
10798 +       }
10799 +
10800 +out_add:
10801 +       old = dy_bradd(br, key);
10802 +       if (old)
10803 +               /* its ref-count should never be zero here */
10804 +               kref_put(&key->dk_kref, dy_bug);
10805 +out:
10806 +       return key;
10807 +}
10808 +
10809 +/* ---------------------------------------------------------------------- */
10810 +/*
10811 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10812 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10813 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10814 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10815 + * See the aufs manual in detail.
10816 + */
10817 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10818 +{
10819 +       if (!do_dx)
10820 +               dyaop->da_op.direct_IO = NULL;
10821 +       else
10822 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10823 +}
10824 +
10825 +static struct au_dyaop *dy_aget(struct au_branch *br,
10826 +                               const struct address_space_operations *h_aop,
10827 +                               int do_dx)
10828 +{
10829 +       struct au_dyaop *dyaop;
10830 +       struct au_dynop op;
10831 +
10832 +       op.dy_type = AuDy_AOP;
10833 +       op.dy_haop = h_aop;
10834 +       dyaop = (void *)dy_get(&op, br);
10835 +       if (IS_ERR(dyaop))
10836 +               goto out;
10837 +       dy_adx(dyaop, do_dx);
10838 +
10839 +out:
10840 +       return dyaop;
10841 +}
10842 +
10843 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10844 +               struct inode *h_inode)
10845 +{
10846 +       int err, do_dx;
10847 +       struct super_block *sb;
10848 +       struct au_branch *br;
10849 +       struct au_dyaop *dyaop;
10850 +
10851 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10852 +       IiMustWriteLock(inode);
10853 +
10854 +       sb = inode->i_sb;
10855 +       br = au_sbr(sb, bindex);
10856 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10857 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10858 +       err = PTR_ERR(dyaop);
10859 +       if (IS_ERR(dyaop))
10860 +               /* unnecessary to call dy_fput() */
10861 +               goto out;
10862 +
10863 +       err = 0;
10864 +       inode->i_mapping->a_ops = &dyaop->da_op;
10865 +
10866 +out:
10867 +       return err;
10868 +}
10869 +
10870 +/*
10871 + * Is it safe to replace a_ops during the inode/file is in operation?
10872 + * Yes, I hope so.
10873 + */
10874 +int au_dy_irefresh(struct inode *inode)
10875 +{
10876 +       int err;
10877 +       aufs_bindex_t btop;
10878 +       struct inode *h_inode;
10879 +
10880 +       err = 0;
10881 +       if (S_ISREG(inode->i_mode)) {
10882 +               btop = au_ibtop(inode);
10883 +               h_inode = au_h_iptr(inode, btop);
10884 +               err = au_dy_iaop(inode, btop, h_inode);
10885 +       }
10886 +       return err;
10887 +}
10888 +
10889 +void au_dy_arefresh(int do_dx)
10890 +{
10891 +       struct au_sphlhead *sphl;
10892 +       struct hlist_head *head;
10893 +       struct au_dykey *key;
10894 +
10895 +       sphl = dynop + AuDy_AOP;
10896 +       head = &sphl->head;
10897 +       spin_lock(&sphl->spin);
10898 +       hlist_for_each_entry(key, head, dk_hnode)
10899 +               dy_adx((void *)key, do_dx);
10900 +       spin_unlock(&sphl->spin);
10901 +}
10902 +
10903 +/* ---------------------------------------------------------------------- */
10904 +
10905 +void __init au_dy_init(void)
10906 +{
10907 +       int i;
10908 +
10909 +       /* make sure that 'struct au_dykey *' can be any type */
10910 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10911 +
10912 +       for (i = 0; i < AuDyLast; i++)
10913 +               au_sphl_init(dynop + i);
10914 +}
10915 +
10916 +void au_dy_fin(void)
10917 +{
10918 +       int i;
10919 +
10920 +       for (i = 0; i < AuDyLast; i++)
10921 +               WARN_ON(!hlist_empty(&dynop[i].head));
10922 +}
10923 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10924 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10925 +++ linux/fs/aufs/dynop.h       2017-03-02 14:24:13.860255635 +0100
10926 @@ -0,0 +1,74 @@
10927 +/*
10928 + * Copyright (C) 2010-2017 Junjiro R. Okajima
10929 + *
10930 + * This program, aufs is free software; you can redistribute it and/or modify
10931 + * it under the terms of the GNU General Public License as published by
10932 + * the Free Software Foundation; either version 2 of the License, or
10933 + * (at your option) any later version.
10934 + *
10935 + * This program is distributed in the hope that it will be useful,
10936 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10937 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10938 + * GNU General Public License for more details.
10939 + *
10940 + * You should have received a copy of the GNU General Public License
10941 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10942 + */
10943 +
10944 +/*
10945 + * dynamically customizable operations (for regular files only)
10946 + */
10947 +
10948 +#ifndef __AUFS_DYNOP_H__
10949 +#define __AUFS_DYNOP_H__
10950 +
10951 +#ifdef __KERNEL__
10952 +
10953 +#include <linux/fs.h>
10954 +#include <linux/kref.h>
10955 +
10956 +enum {AuDy_AOP, AuDyLast};
10957 +
10958 +struct au_dynop {
10959 +       int                                             dy_type;
10960 +       union {
10961 +               const void                              *dy_hop;
10962 +               const struct address_space_operations   *dy_haop;
10963 +       };
10964 +};
10965 +
10966 +struct au_dykey {
10967 +       union {
10968 +               struct hlist_node       dk_hnode;
10969 +               struct rcu_head         dk_rcu;
10970 +       };
10971 +       struct au_dynop         dk_op;
10972 +
10973 +       /*
10974 +        * during I am in the branch local array, kref is gotten. when the
10975 +        * branch is removed, kref is put.
10976 +        */
10977 +       struct kref             dk_kref;
10978 +};
10979 +
10980 +/* stop unioning since their sizes are very different from each other */
10981 +struct au_dyaop {
10982 +       struct au_dykey                 da_key;
10983 +       struct address_space_operations da_op; /* not const */
10984 +};
10985 +
10986 +/* ---------------------------------------------------------------------- */
10987 +
10988 +/* dynop.c */
10989 +struct au_branch;
10990 +void au_dy_put(struct au_dykey *key);
10991 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10992 +               struct inode *h_inode);
10993 +int au_dy_irefresh(struct inode *inode);
10994 +void au_dy_arefresh(int do_dio);
10995 +
10996 +void __init au_dy_init(void);
10997 +void au_dy_fin(void);
10998 +
10999 +#endif /* __KERNEL__ */
11000 +#endif /* __AUFS_DYNOP_H__ */
11001 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
11002 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
11003 +++ linux/fs/aufs/export.c      2017-03-02 14:24:13.860255635 +0100
11004 @@ -0,0 +1,836 @@
11005 +/*
11006 + * Copyright (C) 2005-2017 Junjiro R. Okajima
11007 + *
11008 + * This program, aufs is free software; you can redistribute it and/or modify
11009 + * it under the terms of the GNU General Public License as published by
11010 + * the Free Software Foundation; either version 2 of the License, or
11011 + * (at your option) any later version.
11012 + *
11013 + * This program is distributed in the hope that it will be useful,
11014 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11015 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11016 + * GNU General Public License for more details.
11017 + *
11018 + * You should have received a copy of the GNU General Public License
11019 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11020 + */
11021 +
11022 +/*
11023 + * export via nfs
11024 + */
11025 +
11026 +#include <linux/exportfs.h>
11027 +#include <linux/fs_struct.h>
11028 +#include <linux/namei.h>
11029 +#include <linux/nsproxy.h>
11030 +#include <linux/random.h>
11031 +#include <linux/writeback.h>
11032 +#include "aufs.h"
11033 +
11034 +union conv {
11035 +#ifdef CONFIG_AUFS_INO_T_64
11036 +       __u32 a[2];
11037 +#else
11038 +       __u32 a[1];
11039 +#endif
11040 +       ino_t ino;
11041 +};
11042 +
11043 +static ino_t decode_ino(__u32 *a)
11044 +{
11045 +       union conv u;
11046 +
11047 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
11048 +       u.a[0] = a[0];
11049 +#ifdef CONFIG_AUFS_INO_T_64
11050 +       u.a[1] = a[1];
11051 +#endif
11052 +       return u.ino;
11053 +}
11054 +
11055 +static void encode_ino(__u32 *a, ino_t ino)
11056 +{
11057 +       union conv u;
11058 +
11059 +       u.ino = ino;
11060 +       a[0] = u.a[0];
11061 +#ifdef CONFIG_AUFS_INO_T_64
11062 +       a[1] = u.a[1];
11063 +#endif
11064 +}
11065 +
11066 +/* NFS file handle */
11067 +enum {
11068 +       Fh_br_id,
11069 +       Fh_sigen,
11070 +#ifdef CONFIG_AUFS_INO_T_64
11071 +       /* support 64bit inode number */
11072 +       Fh_ino1,
11073 +       Fh_ino2,
11074 +       Fh_dir_ino1,
11075 +       Fh_dir_ino2,
11076 +#else
11077 +       Fh_ino1,
11078 +       Fh_dir_ino1,
11079 +#endif
11080 +       Fh_igen,
11081 +       Fh_h_type,
11082 +       Fh_tail,
11083 +
11084 +       Fh_ino = Fh_ino1,
11085 +       Fh_dir_ino = Fh_dir_ino1
11086 +};
11087 +
11088 +static int au_test_anon(struct dentry *dentry)
11089 +{
11090 +       /* note: read d_flags without d_lock */
11091 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
11092 +}
11093 +
11094 +int au_test_nfsd(void)
11095 +{
11096 +       int ret;
11097 +       struct task_struct *tsk = current;
11098 +       char comm[sizeof(tsk->comm)];
11099 +
11100 +       ret = 0;
11101 +       if (tsk->flags & PF_KTHREAD) {
11102 +               get_task_comm(comm, tsk);
11103 +               ret = !strcmp(comm, "nfsd");
11104 +       }
11105 +
11106 +       return ret;
11107 +}
11108 +
11109 +/* ---------------------------------------------------------------------- */
11110 +/* inode generation external table */
11111 +
11112 +void au_xigen_inc(struct inode *inode)
11113 +{
11114 +       loff_t pos;
11115 +       ssize_t sz;
11116 +       __u32 igen;
11117 +       struct super_block *sb;
11118 +       struct au_sbinfo *sbinfo;
11119 +
11120 +       sb = inode->i_sb;
11121 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
11122 +
11123 +       sbinfo = au_sbi(sb);
11124 +       pos = inode->i_ino;
11125 +       pos *= sizeof(igen);
11126 +       igen = inode->i_generation + 1;
11127 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
11128 +                        sizeof(igen), &pos);
11129 +       if (sz == sizeof(igen))
11130 +               return; /* success */
11131 +
11132 +       if (unlikely(sz >= 0))
11133 +               AuIOErr("xigen error (%zd)\n", sz);
11134 +}
11135 +
11136 +int au_xigen_new(struct inode *inode)
11137 +{
11138 +       int err;
11139 +       loff_t pos;
11140 +       ssize_t sz;
11141 +       struct super_block *sb;
11142 +       struct au_sbinfo *sbinfo;
11143 +       struct file *file;
11144 +
11145 +       err = 0;
11146 +       /* todo: dirty, at mount time */
11147 +       if (inode->i_ino == AUFS_ROOT_INO)
11148 +               goto out;
11149 +       sb = inode->i_sb;
11150 +       SiMustAnyLock(sb);
11151 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11152 +               goto out;
11153 +
11154 +       err = -EFBIG;
11155 +       pos = inode->i_ino;
11156 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
11157 +               AuIOErr1("too large i%lld\n", pos);
11158 +               goto out;
11159 +       }
11160 +       pos *= sizeof(inode->i_generation);
11161 +
11162 +       err = 0;
11163 +       sbinfo = au_sbi(sb);
11164 +       file = sbinfo->si_xigen;
11165 +       BUG_ON(!file);
11166 +
11167 +       if (vfsub_f_size_read(file)
11168 +           < pos + sizeof(inode->i_generation)) {
11169 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
11170 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
11171 +                                sizeof(inode->i_generation), &pos);
11172 +       } else
11173 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
11174 +                               sizeof(inode->i_generation), &pos);
11175 +       if (sz == sizeof(inode->i_generation))
11176 +               goto out; /* success */
11177 +
11178 +       err = sz;
11179 +       if (unlikely(sz >= 0)) {
11180 +               err = -EIO;
11181 +               AuIOErr("xigen error (%zd)\n", sz);
11182 +       }
11183 +
11184 +out:
11185 +       return err;
11186 +}
11187 +
11188 +int au_xigen_set(struct super_block *sb, struct file *base)
11189 +{
11190 +       int err;
11191 +       struct au_sbinfo *sbinfo;
11192 +       struct file *file;
11193 +
11194 +       SiMustWriteLock(sb);
11195 +
11196 +       sbinfo = au_sbi(sb);
11197 +       file = au_xino_create2(base, sbinfo->si_xigen);
11198 +       err = PTR_ERR(file);
11199 +       if (IS_ERR(file))
11200 +               goto out;
11201 +       err = 0;
11202 +       if (sbinfo->si_xigen)
11203 +               fput(sbinfo->si_xigen);
11204 +       sbinfo->si_xigen = file;
11205 +
11206 +out:
11207 +       return err;
11208 +}
11209 +
11210 +void au_xigen_clr(struct super_block *sb)
11211 +{
11212 +       struct au_sbinfo *sbinfo;
11213 +
11214 +       SiMustWriteLock(sb);
11215 +
11216 +       sbinfo = au_sbi(sb);
11217 +       if (sbinfo->si_xigen) {
11218 +               fput(sbinfo->si_xigen);
11219 +               sbinfo->si_xigen = NULL;
11220 +       }
11221 +}
11222 +
11223 +/* ---------------------------------------------------------------------- */
11224 +
11225 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
11226 +                                   ino_t dir_ino)
11227 +{
11228 +       struct dentry *dentry, *d;
11229 +       struct inode *inode;
11230 +       unsigned int sigen;
11231 +
11232 +       dentry = NULL;
11233 +       inode = ilookup(sb, ino);
11234 +       if (!inode)
11235 +               goto out;
11236 +
11237 +       dentry = ERR_PTR(-ESTALE);
11238 +       sigen = au_sigen(sb);
11239 +       if (unlikely(au_is_bad_inode(inode)
11240 +                    || IS_DEADDIR(inode)
11241 +                    || sigen != au_iigen(inode, NULL)))
11242 +               goto out_iput;
11243 +
11244 +       dentry = NULL;
11245 +       if (!dir_ino || S_ISDIR(inode->i_mode))
11246 +               dentry = d_find_alias(inode);
11247 +       else {
11248 +               spin_lock(&inode->i_lock);
11249 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
11250 +                       spin_lock(&d->d_lock);
11251 +                       if (!au_test_anon(d)
11252 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
11253 +                               dentry = dget_dlock(d);
11254 +                               spin_unlock(&d->d_lock);
11255 +                               break;
11256 +                       }
11257 +                       spin_unlock(&d->d_lock);
11258 +               }
11259 +               spin_unlock(&inode->i_lock);
11260 +       }
11261 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
11262 +               /* need to refresh */
11263 +               dput(dentry);
11264 +               dentry = NULL;
11265 +       }
11266 +
11267 +out_iput:
11268 +       iput(inode);
11269 +out:
11270 +       AuTraceErrPtr(dentry);
11271 +       return dentry;
11272 +}
11273 +
11274 +/* ---------------------------------------------------------------------- */
11275 +
11276 +/* todo: dirty? */
11277 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
11278 +
11279 +struct au_compare_mnt_args {
11280 +       /* input */
11281 +       struct super_block *sb;
11282 +
11283 +       /* output */
11284 +       struct vfsmount *mnt;
11285 +};
11286 +
11287 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
11288 +{
11289 +       struct au_compare_mnt_args *a = arg;
11290 +
11291 +       if (mnt->mnt_sb != a->sb)
11292 +               return 0;
11293 +       a->mnt = mntget(mnt);
11294 +       return 1;
11295 +}
11296 +
11297 +static struct vfsmount *au_mnt_get(struct super_block *sb)
11298 +{
11299 +       int err;
11300 +       struct path root;
11301 +       struct au_compare_mnt_args args = {
11302 +               .sb = sb
11303 +       };
11304 +
11305 +       get_fs_root(current->fs, &root);
11306 +       rcu_read_lock();
11307 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
11308 +       rcu_read_unlock();
11309 +       path_put(&root);
11310 +       AuDebugOn(!err);
11311 +       AuDebugOn(!args.mnt);
11312 +       return args.mnt;
11313 +}
11314 +
11315 +struct au_nfsd_si_lock {
11316 +       unsigned int sigen;
11317 +       aufs_bindex_t bindex, br_id;
11318 +       unsigned char force_lock;
11319 +};
11320 +
11321 +static int si_nfsd_read_lock(struct super_block *sb,
11322 +                            struct au_nfsd_si_lock *nsi_lock)
11323 +{
11324 +       int err;
11325 +       aufs_bindex_t bindex;
11326 +
11327 +       si_read_lock(sb, AuLock_FLUSH);
11328 +
11329 +       /* branch id may be wrapped around */
11330 +       err = 0;
11331 +       bindex = au_br_index(sb, nsi_lock->br_id);
11332 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11333 +               goto out; /* success */
11334 +
11335 +       err = -ESTALE;
11336 +       bindex = -1;
11337 +       if (!nsi_lock->force_lock)
11338 +               si_read_unlock(sb);
11339 +
11340 +out:
11341 +       nsi_lock->bindex = bindex;
11342 +       return err;
11343 +}
11344 +
11345 +struct find_name_by_ino {
11346 +       struct dir_context ctx;
11347 +       int called, found;
11348 +       ino_t ino;
11349 +       char *name;
11350 +       int namelen;
11351 +};
11352 +
11353 +static int
11354 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11355 +                loff_t offset, u64 ino, unsigned int d_type)
11356 +{
11357 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11358 +                                                 ctx);
11359 +
11360 +       a->called++;
11361 +       if (a->ino != ino)
11362 +               return 0;
11363 +
11364 +       memcpy(a->name, name, namelen);
11365 +       a->namelen = namelen;
11366 +       a->found = 1;
11367 +       return 1;
11368 +}
11369 +
11370 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11371 +                                    struct au_nfsd_si_lock *nsi_lock)
11372 +{
11373 +       struct dentry *dentry, *parent;
11374 +       struct file *file;
11375 +       struct inode *dir;
11376 +       struct find_name_by_ino arg = {
11377 +               .ctx = {
11378 +                       .actor = find_name_by_ino
11379 +               }
11380 +       };
11381 +       int err;
11382 +
11383 +       parent = path->dentry;
11384 +       if (nsi_lock)
11385 +               si_read_unlock(parent->d_sb);
11386 +       file = vfsub_dentry_open(path, au_dir_roflags);
11387 +       dentry = (void *)file;
11388 +       if (IS_ERR(file))
11389 +               goto out;
11390 +
11391 +       dentry = ERR_PTR(-ENOMEM);
11392 +       arg.name = (void *)__get_free_page(GFP_NOFS);
11393 +       if (unlikely(!arg.name))
11394 +               goto out_file;
11395 +       arg.ino = ino;
11396 +       arg.found = 0;
11397 +       do {
11398 +               arg.called = 0;
11399 +               /* smp_mb(); */
11400 +               err = vfsub_iterate_dir(file, &arg.ctx);
11401 +       } while (!err && !arg.found && arg.called);
11402 +       dentry = ERR_PTR(err);
11403 +       if (unlikely(err))
11404 +               goto out_name;
11405 +       /* instead of ENOENT */
11406 +       dentry = ERR_PTR(-ESTALE);
11407 +       if (!arg.found)
11408 +               goto out_name;
11409 +
11410 +       /* do not call vfsub_lkup_one() */
11411 +       dir = d_inode(parent);
11412 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
11413 +       AuTraceErrPtr(dentry);
11414 +       if (IS_ERR(dentry))
11415 +               goto out_name;
11416 +       AuDebugOn(au_test_anon(dentry));
11417 +       if (unlikely(d_really_is_negative(dentry))) {
11418 +               dput(dentry);
11419 +               dentry = ERR_PTR(-ENOENT);
11420 +       }
11421 +
11422 +out_name:
11423 +       au_delayed_free_page((unsigned long)arg.name);
11424 +out_file:
11425 +       fput(file);
11426 +out:
11427 +       if (unlikely(nsi_lock
11428 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11429 +               if (!IS_ERR(dentry)) {
11430 +                       dput(dentry);
11431 +                       dentry = ERR_PTR(-ESTALE);
11432 +               }
11433 +       AuTraceErrPtr(dentry);
11434 +       return dentry;
11435 +}
11436 +
11437 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11438 +                                       ino_t dir_ino,
11439 +                                       struct au_nfsd_si_lock *nsi_lock)
11440 +{
11441 +       struct dentry *dentry;
11442 +       struct path path;
11443 +
11444 +       if (dir_ino != AUFS_ROOT_INO) {
11445 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
11446 +               dentry = path.dentry;
11447 +               if (!path.dentry || IS_ERR(path.dentry))
11448 +                       goto out;
11449 +               AuDebugOn(au_test_anon(path.dentry));
11450 +       } else
11451 +               path.dentry = dget(sb->s_root);
11452 +
11453 +       path.mnt = au_mnt_get(sb);
11454 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11455 +       path_put(&path);
11456 +
11457 +out:
11458 +       AuTraceErrPtr(dentry);
11459 +       return dentry;
11460 +}
11461 +
11462 +/* ---------------------------------------------------------------------- */
11463 +
11464 +static int h_acceptable(void *expv, struct dentry *dentry)
11465 +{
11466 +       return 1;
11467 +}
11468 +
11469 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11470 +                          char *buf, int len, struct super_block *sb)
11471 +{
11472 +       char *p;
11473 +       int n;
11474 +       struct path path;
11475 +
11476 +       p = d_path(h_rootpath, buf, len);
11477 +       if (IS_ERR(p))
11478 +               goto out;
11479 +       n = strlen(p);
11480 +
11481 +       path.mnt = h_rootpath->mnt;
11482 +       path.dentry = h_parent;
11483 +       p = d_path(&path, buf, len);
11484 +       if (IS_ERR(p))
11485 +               goto out;
11486 +       if (n != 1)
11487 +               p += n;
11488 +
11489 +       path.mnt = au_mnt_get(sb);
11490 +       path.dentry = sb->s_root;
11491 +       p = d_path(&path, buf, len - strlen(p));
11492 +       mntput(path.mnt);
11493 +       if (IS_ERR(p))
11494 +               goto out;
11495 +       if (n != 1)
11496 +               p[strlen(p)] = '/';
11497 +
11498 +out:
11499 +       AuTraceErrPtr(p);
11500 +       return p;
11501 +}
11502 +
11503 +static
11504 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11505 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
11506 +{
11507 +       struct dentry *dentry, *h_parent, *root;
11508 +       struct super_block *h_sb;
11509 +       char *pathname, *p;
11510 +       struct vfsmount *h_mnt;
11511 +       struct au_branch *br;
11512 +       int err;
11513 +       struct path path;
11514 +
11515 +       br = au_sbr(sb, nsi_lock->bindex);
11516 +       h_mnt = au_br_mnt(br);
11517 +       h_sb = h_mnt->mnt_sb;
11518 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
11519 +       lockdep_off();
11520 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11521 +                                     fh_len - Fh_tail, fh[Fh_h_type],
11522 +                                     h_acceptable, /*context*/NULL);
11523 +       lockdep_on();
11524 +       dentry = h_parent;
11525 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
11526 +               AuWarn1("%s decode_fh failed, %ld\n",
11527 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
11528 +               goto out;
11529 +       }
11530 +       dentry = NULL;
11531 +       if (unlikely(au_test_anon(h_parent))) {
11532 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
11533 +                       au_sbtype(h_sb));
11534 +               goto out_h_parent;
11535 +       }
11536 +
11537 +       dentry = ERR_PTR(-ENOMEM);
11538 +       pathname = (void *)__get_free_page(GFP_NOFS);
11539 +       if (unlikely(!pathname))
11540 +               goto out_h_parent;
11541 +
11542 +       root = sb->s_root;
11543 +       path.mnt = h_mnt;
11544 +       di_read_lock_parent(root, !AuLock_IR);
11545 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
11546 +       di_read_unlock(root, !AuLock_IR);
11547 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11548 +       dentry = (void *)p;
11549 +       if (IS_ERR(p))
11550 +               goto out_pathname;
11551 +
11552 +       si_read_unlock(sb);
11553 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11554 +       dentry = ERR_PTR(err);
11555 +       if (unlikely(err))
11556 +               goto out_relock;
11557 +
11558 +       dentry = ERR_PTR(-ENOENT);
11559 +       AuDebugOn(au_test_anon(path.dentry));
11560 +       if (unlikely(d_really_is_negative(path.dentry)))
11561 +               goto out_path;
11562 +
11563 +       if (ino != d_inode(path.dentry)->i_ino)
11564 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11565 +       else
11566 +               dentry = dget(path.dentry);
11567 +
11568 +out_path:
11569 +       path_put(&path);
11570 +out_relock:
11571 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11572 +               if (!IS_ERR(dentry)) {
11573 +                       dput(dentry);
11574 +                       dentry = ERR_PTR(-ESTALE);
11575 +               }
11576 +out_pathname:
11577 +       au_delayed_free_page((unsigned long)pathname);
11578 +out_h_parent:
11579 +       dput(h_parent);
11580 +out:
11581 +       AuTraceErrPtr(dentry);
11582 +       return dentry;
11583 +}
11584 +
11585 +/* ---------------------------------------------------------------------- */
11586 +
11587 +static struct dentry *
11588 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11589 +                 int fh_type)
11590 +{
11591 +       struct dentry *dentry;
11592 +       __u32 *fh = fid->raw;
11593 +       struct au_branch *br;
11594 +       ino_t ino, dir_ino;
11595 +       struct au_nfsd_si_lock nsi_lock = {
11596 +               .force_lock     = 0
11597 +       };
11598 +
11599 +       dentry = ERR_PTR(-ESTALE);
11600 +       /* it should never happen, but the file handle is unreliable */
11601 +       if (unlikely(fh_len < Fh_tail))
11602 +               goto out;
11603 +       nsi_lock.sigen = fh[Fh_sigen];
11604 +       nsi_lock.br_id = fh[Fh_br_id];
11605 +
11606 +       /* branch id may be wrapped around */
11607 +       br = NULL;
11608 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11609 +               goto out;
11610 +       nsi_lock.force_lock = 1;
11611 +
11612 +       /* is this inode still cached? */
11613 +       ino = decode_ino(fh + Fh_ino);
11614 +       /* it should never happen */
11615 +       if (unlikely(ino == AUFS_ROOT_INO))
11616 +               goto out_unlock;
11617 +
11618 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11619 +       dentry = decode_by_ino(sb, ino, dir_ino);
11620 +       if (IS_ERR(dentry))
11621 +               goto out_unlock;
11622 +       if (dentry)
11623 +               goto accept;
11624 +
11625 +       /* is the parent dir cached? */
11626 +       br = au_sbr(sb, nsi_lock.bindex);
11627 +       au_br_get(br);
11628 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11629 +       if (IS_ERR(dentry))
11630 +               goto out_unlock;
11631 +       if (dentry)
11632 +               goto accept;
11633 +
11634 +       /* lookup path */
11635 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11636 +       if (IS_ERR(dentry))
11637 +               goto out_unlock;
11638 +       if (unlikely(!dentry))
11639 +               /* todo?: make it ESTALE */
11640 +               goto out_unlock;
11641 +
11642 +accept:
11643 +       if (!au_digen_test(dentry, au_sigen(sb))
11644 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
11645 +               goto out_unlock; /* success */
11646 +
11647 +       dput(dentry);
11648 +       dentry = ERR_PTR(-ESTALE);
11649 +out_unlock:
11650 +       if (br)
11651 +               au_br_put(br);
11652 +       si_read_unlock(sb);
11653 +out:
11654 +       AuTraceErrPtr(dentry);
11655 +       return dentry;
11656 +}
11657 +
11658 +#if 0 /* reserved for future use */
11659 +/* support subtreecheck option */
11660 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11661 +                                       int fh_len, int fh_type)
11662 +{
11663 +       struct dentry *parent;
11664 +       __u32 *fh = fid->raw;
11665 +       ino_t dir_ino;
11666 +
11667 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11668 +       parent = decode_by_ino(sb, dir_ino, 0);
11669 +       if (IS_ERR(parent))
11670 +               goto out;
11671 +       if (!parent)
11672 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11673 +                                       dir_ino, fh, fh_len);
11674 +
11675 +out:
11676 +       AuTraceErrPtr(parent);
11677 +       return parent;
11678 +}
11679 +#endif
11680 +
11681 +/* ---------------------------------------------------------------------- */
11682 +
11683 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11684 +                         struct inode *dir)
11685 +{
11686 +       int err;
11687 +       aufs_bindex_t bindex;
11688 +       struct super_block *sb, *h_sb;
11689 +       struct dentry *dentry, *parent, *h_parent;
11690 +       struct inode *h_dir;
11691 +       struct au_branch *br;
11692 +
11693 +       err = -ENOSPC;
11694 +       if (unlikely(*max_len <= Fh_tail)) {
11695 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11696 +               goto out;
11697 +       }
11698 +
11699 +       err = FILEID_ROOT;
11700 +       if (inode->i_ino == AUFS_ROOT_INO) {
11701 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11702 +               goto out;
11703 +       }
11704 +
11705 +       h_parent = NULL;
11706 +       sb = inode->i_sb;
11707 +       err = si_read_lock(sb, AuLock_FLUSH);
11708 +       if (unlikely(err))
11709 +               goto out;
11710 +
11711 +#ifdef CONFIG_AUFS_DEBUG
11712 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11713 +               AuWarn1("NFS-exporting requires xino\n");
11714 +#endif
11715 +       err = -EIO;
11716 +       parent = NULL;
11717 +       ii_read_lock_child(inode);
11718 +       bindex = au_ibtop(inode);
11719 +       if (!dir) {
11720 +               dentry = d_find_any_alias(inode);
11721 +               if (unlikely(!dentry))
11722 +                       goto out_unlock;
11723 +               AuDebugOn(au_test_anon(dentry));
11724 +               parent = dget_parent(dentry);
11725 +               dput(dentry);
11726 +               if (unlikely(!parent))
11727 +                       goto out_unlock;
11728 +               if (d_really_is_positive(parent))
11729 +                       dir = d_inode(parent);
11730 +       }
11731 +
11732 +       ii_read_lock_parent(dir);
11733 +       h_dir = au_h_iptr(dir, bindex);
11734 +       ii_read_unlock(dir);
11735 +       if (unlikely(!h_dir))
11736 +               goto out_parent;
11737 +       h_parent = d_find_any_alias(h_dir);
11738 +       if (unlikely(!h_parent))
11739 +               goto out_hparent;
11740 +
11741 +       err = -EPERM;
11742 +       br = au_sbr(sb, bindex);
11743 +       h_sb = au_br_sb(br);
11744 +       if (unlikely(!h_sb->s_export_op)) {
11745 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11746 +               goto out_hparent;
11747 +       }
11748 +
11749 +       fh[Fh_br_id] = br->br_id;
11750 +       fh[Fh_sigen] = au_sigen(sb);
11751 +       encode_ino(fh + Fh_ino, inode->i_ino);
11752 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11753 +       fh[Fh_igen] = inode->i_generation;
11754 +
11755 +       *max_len -= Fh_tail;
11756 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11757 +                                          max_len,
11758 +                                          /*connectable or subtreecheck*/0);
11759 +       err = fh[Fh_h_type];
11760 +       *max_len += Fh_tail;
11761 +       /* todo: macros? */
11762 +       if (err != FILEID_INVALID)
11763 +               err = 99;
11764 +       else
11765 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11766 +
11767 +out_hparent:
11768 +       dput(h_parent);
11769 +out_parent:
11770 +       dput(parent);
11771 +out_unlock:
11772 +       ii_read_unlock(inode);
11773 +       si_read_unlock(sb);
11774 +out:
11775 +       if (unlikely(err < 0))
11776 +               err = FILEID_INVALID;
11777 +       return err;
11778 +}
11779 +
11780 +/* ---------------------------------------------------------------------- */
11781 +
11782 +static int aufs_commit_metadata(struct inode *inode)
11783 +{
11784 +       int err;
11785 +       aufs_bindex_t bindex;
11786 +       struct super_block *sb;
11787 +       struct inode *h_inode;
11788 +       int (*f)(struct inode *inode);
11789 +
11790 +       sb = inode->i_sb;
11791 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11792 +       ii_write_lock_child(inode);
11793 +       bindex = au_ibtop(inode);
11794 +       AuDebugOn(bindex < 0);
11795 +       h_inode = au_h_iptr(inode, bindex);
11796 +
11797 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11798 +       if (f)
11799 +               err = f(h_inode);
11800 +       else {
11801 +               struct writeback_control wbc = {
11802 +                       .sync_mode      = WB_SYNC_ALL,
11803 +                       .nr_to_write    = 0 /* metadata only */
11804 +               };
11805 +
11806 +               err = sync_inode(h_inode, &wbc);
11807 +       }
11808 +
11809 +       au_cpup_attr_timesizes(inode);
11810 +       ii_write_unlock(inode);
11811 +       si_read_unlock(sb);
11812 +       return err;
11813 +}
11814 +
11815 +/* ---------------------------------------------------------------------- */
11816 +
11817 +static struct export_operations aufs_export_op = {
11818 +       .fh_to_dentry           = aufs_fh_to_dentry,
11819 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11820 +       .encode_fh              = aufs_encode_fh,
11821 +       .commit_metadata        = aufs_commit_metadata
11822 +};
11823 +
11824 +void au_export_init(struct super_block *sb)
11825 +{
11826 +       struct au_sbinfo *sbinfo;
11827 +       __u32 u;
11828 +
11829 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
11830 +                        && IS_MODULE(CONFIG_EXPORTFS),
11831 +                        AUFS_NAME ": unsupported configuration "
11832 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
11833 +
11834 +       sb->s_export_op = &aufs_export_op;
11835 +       sbinfo = au_sbi(sb);
11836 +       sbinfo->si_xigen = NULL;
11837 +       get_random_bytes(&u, sizeof(u));
11838 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11839 +       atomic_set(&sbinfo->si_xigen_next, u);
11840 +}
11841 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11842 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11843 +++ linux/fs/aufs/fhsm.c        2017-03-02 14:24:13.860255635 +0100
11844 @@ -0,0 +1,426 @@
11845 +/*
11846 + * Copyright (C) 2011-2017 Junjiro R. Okajima
11847 + *
11848 + * This program, aufs is free software; you can redistribute it and/or modify
11849 + * it under the terms of the GNU General Public License as published by
11850 + * the Free Software Foundation; either version 2 of the License, or
11851 + * (at your option) any later version.
11852 + *
11853 + * This program is distributed in the hope that it will be useful,
11854 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11855 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11856 + * GNU General Public License for more details.
11857 + *
11858 + * You should have received a copy of the GNU General Public License
11859 + * along with this program; if not, write to the Free Software
11860 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11861 + */
11862 +
11863 +/*
11864 + * File-based Hierarchy Storage Management
11865 + */
11866 +
11867 +#include <linux/anon_inodes.h>
11868 +#include <linux/poll.h>
11869 +#include <linux/seq_file.h>
11870 +#include <linux/statfs.h>
11871 +#include "aufs.h"
11872 +
11873 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11874 +{
11875 +       struct au_sbinfo *sbinfo;
11876 +       struct au_fhsm *fhsm;
11877 +
11878 +       SiMustAnyLock(sb);
11879 +
11880 +       sbinfo = au_sbi(sb);
11881 +       fhsm = &sbinfo->si_fhsm;
11882 +       AuDebugOn(!fhsm);
11883 +       return fhsm->fhsm_bottom;
11884 +}
11885 +
11886 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11887 +{
11888 +       struct au_sbinfo *sbinfo;
11889 +       struct au_fhsm *fhsm;
11890 +
11891 +       SiMustWriteLock(sb);
11892 +
11893 +       sbinfo = au_sbi(sb);
11894 +       fhsm = &sbinfo->si_fhsm;
11895 +       AuDebugOn(!fhsm);
11896 +       fhsm->fhsm_bottom = bindex;
11897 +}
11898 +
11899 +/* ---------------------------------------------------------------------- */
11900 +
11901 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11902 +{
11903 +       struct au_br_fhsm *bf;
11904 +
11905 +       bf = br->br_fhsm;
11906 +       MtxMustLock(&bf->bf_lock);
11907 +
11908 +       return !bf->bf_readable
11909 +               || time_after(jiffies,
11910 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11911 +}
11912 +
11913 +/* ---------------------------------------------------------------------- */
11914 +
11915 +static void au_fhsm_notify(struct super_block *sb, int val)
11916 +{
11917 +       struct au_sbinfo *sbinfo;
11918 +       struct au_fhsm *fhsm;
11919 +
11920 +       SiMustAnyLock(sb);
11921 +
11922 +       sbinfo = au_sbi(sb);
11923 +       fhsm = &sbinfo->si_fhsm;
11924 +       if (au_fhsm_pid(fhsm)
11925 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11926 +               atomic_set(&fhsm->fhsm_readable, val);
11927 +               if (val)
11928 +                       wake_up(&fhsm->fhsm_wqh);
11929 +       }
11930 +}
11931 +
11932 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11933 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11934 +{
11935 +       int err;
11936 +       struct au_branch *br;
11937 +       struct au_br_fhsm *bf;
11938 +
11939 +       br = au_sbr(sb, bindex);
11940 +       AuDebugOn(au_br_rdonly(br));
11941 +       bf = br->br_fhsm;
11942 +       AuDebugOn(!bf);
11943 +
11944 +       if (do_lock)
11945 +               mutex_lock(&bf->bf_lock);
11946 +       else
11947 +               MtxMustLock(&bf->bf_lock);
11948 +
11949 +       /* sb->s_root for NFS is unreliable */
11950 +       err = au_br_stfs(br, &bf->bf_stfs);
11951 +       if (unlikely(err)) {
11952 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11953 +               goto out;
11954 +       }
11955 +
11956 +       bf->bf_jiffy = jiffies;
11957 +       bf->bf_readable = 1;
11958 +       if (do_notify)
11959 +               au_fhsm_notify(sb, /*val*/1);
11960 +       if (rstfs)
11961 +               *rstfs = bf->bf_stfs;
11962 +
11963 +out:
11964 +       if (do_lock)
11965 +               mutex_unlock(&bf->bf_lock);
11966 +       au_fhsm_notify(sb, /*val*/1);
11967 +
11968 +       return err;
11969 +}
11970 +
11971 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11972 +{
11973 +       int err;
11974 +       struct au_sbinfo *sbinfo;
11975 +       struct au_fhsm *fhsm;
11976 +       struct au_branch *br;
11977 +       struct au_br_fhsm *bf;
11978 +
11979 +       AuDbg("b%d, force %d\n", bindex, force);
11980 +       SiMustAnyLock(sb);
11981 +
11982 +       sbinfo = au_sbi(sb);
11983 +       fhsm = &sbinfo->si_fhsm;
11984 +       if (!au_ftest_si(sbinfo, FHSM)
11985 +           || fhsm->fhsm_bottom == bindex)
11986 +               return;
11987 +
11988 +       br = au_sbr(sb, bindex);
11989 +       bf = br->br_fhsm;
11990 +       AuDebugOn(!bf);
11991 +       mutex_lock(&bf->bf_lock);
11992 +       if (force
11993 +           || au_fhsm_pid(fhsm)
11994 +           || au_fhsm_test_jiffy(sbinfo, br))
11995 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11996 +                                 /*do_notify*/1);
11997 +       mutex_unlock(&bf->bf_lock);
11998 +}
11999 +
12000 +void au_fhsm_wrote_all(struct super_block *sb, int force)
12001 +{
12002 +       aufs_bindex_t bindex, bbot;
12003 +       struct au_branch *br;
12004 +
12005 +       /* exclude the bottom */
12006 +       bbot = au_fhsm_bottom(sb);
12007 +       for (bindex = 0; bindex < bbot; bindex++) {
12008 +               br = au_sbr(sb, bindex);
12009 +               if (au_br_fhsm(br->br_perm))
12010 +                       au_fhsm_wrote(sb, bindex, force);
12011 +       }
12012 +}
12013 +
12014 +/* ---------------------------------------------------------------------- */
12015 +
12016 +static unsigned int au_fhsm_poll(struct file *file,
12017 +                                struct poll_table_struct *wait)
12018 +{
12019 +       unsigned int mask;
12020 +       struct au_sbinfo *sbinfo;
12021 +       struct au_fhsm *fhsm;
12022 +
12023 +       mask = 0;
12024 +       sbinfo = file->private_data;
12025 +       fhsm = &sbinfo->si_fhsm;
12026 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
12027 +       if (atomic_read(&fhsm->fhsm_readable))
12028 +               mask = POLLIN /* | POLLRDNORM */;
12029 +
12030 +       AuTraceErr((int)mask);
12031 +       return mask;
12032 +}
12033 +
12034 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
12035 +                             struct aufs_stfs *stfs, __s16 brid)
12036 +{
12037 +       int err;
12038 +
12039 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
12040 +       if (!err)
12041 +               err = __put_user(brid, &stbr->brid);
12042 +       if (unlikely(err))
12043 +               err = -EFAULT;
12044 +
12045 +       return err;
12046 +}
12047 +
12048 +static ssize_t au_fhsm_do_read(struct super_block *sb,
12049 +                              struct aufs_stbr __user *stbr, size_t count)
12050 +{
12051 +       ssize_t err;
12052 +       int nstbr;
12053 +       aufs_bindex_t bindex, bbot;
12054 +       struct au_branch *br;
12055 +       struct au_br_fhsm *bf;
12056 +
12057 +       /* except the bottom branch */
12058 +       err = 0;
12059 +       nstbr = 0;
12060 +       bbot = au_fhsm_bottom(sb);
12061 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
12062 +               br = au_sbr(sb, bindex);
12063 +               if (!au_br_fhsm(br->br_perm))
12064 +                       continue;
12065 +
12066 +               bf = br->br_fhsm;
12067 +               mutex_lock(&bf->bf_lock);
12068 +               if (bf->bf_readable) {
12069 +                       err = -EFAULT;
12070 +                       if (count >= sizeof(*stbr))
12071 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
12072 +                                                         br->br_id);
12073 +                       if (!err) {
12074 +                               bf->bf_readable = 0;
12075 +                               count -= sizeof(*stbr);
12076 +                               nstbr++;
12077 +                       }
12078 +               }
12079 +               mutex_unlock(&bf->bf_lock);
12080 +       }
12081 +       if (!err)
12082 +               err = sizeof(*stbr) * nstbr;
12083 +
12084 +       return err;
12085 +}
12086 +
12087 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
12088 +                          loff_t *pos)
12089 +{
12090 +       ssize_t err;
12091 +       int readable;
12092 +       aufs_bindex_t nfhsm, bindex, bbot;
12093 +       struct au_sbinfo *sbinfo;
12094 +       struct au_fhsm *fhsm;
12095 +       struct au_branch *br;
12096 +       struct super_block *sb;
12097 +
12098 +       err = 0;
12099 +       sbinfo = file->private_data;
12100 +       fhsm = &sbinfo->si_fhsm;
12101 +need_data:
12102 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
12103 +       if (!atomic_read(&fhsm->fhsm_readable)) {
12104 +               if (vfsub_file_flags(file) & O_NONBLOCK)
12105 +                       err = -EAGAIN;
12106 +               else
12107 +                       err = wait_event_interruptible_locked_irq
12108 +                               (fhsm->fhsm_wqh,
12109 +                                atomic_read(&fhsm->fhsm_readable));
12110 +       }
12111 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
12112 +       if (unlikely(err))
12113 +               goto out;
12114 +
12115 +       /* sb may already be dead */
12116 +       au_rw_read_lock(&sbinfo->si_rwsem);
12117 +       readable = atomic_read(&fhsm->fhsm_readable);
12118 +       if (readable > 0) {
12119 +               sb = sbinfo->si_sb;
12120 +               AuDebugOn(!sb);
12121 +               /* exclude the bottom branch */
12122 +               nfhsm = 0;
12123 +               bbot = au_fhsm_bottom(sb);
12124 +               for (bindex = 0; bindex < bbot; bindex++) {
12125 +                       br = au_sbr(sb, bindex);
12126 +                       if (au_br_fhsm(br->br_perm))
12127 +                               nfhsm++;
12128 +               }
12129 +               err = -EMSGSIZE;
12130 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
12131 +                       atomic_set(&fhsm->fhsm_readable, 0);
12132 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
12133 +                                            count);
12134 +               }
12135 +       }
12136 +       au_rw_read_unlock(&sbinfo->si_rwsem);
12137 +       if (!readable)
12138 +               goto need_data;
12139 +
12140 +out:
12141 +       return err;
12142 +}
12143 +
12144 +static int au_fhsm_release(struct inode *inode, struct file *file)
12145 +{
12146 +       struct au_sbinfo *sbinfo;
12147 +       struct au_fhsm *fhsm;
12148 +
12149 +       /* sb may already be dead */
12150 +       sbinfo = file->private_data;
12151 +       fhsm = &sbinfo->si_fhsm;
12152 +       spin_lock(&fhsm->fhsm_spin);
12153 +       fhsm->fhsm_pid = 0;
12154 +       spin_unlock(&fhsm->fhsm_spin);
12155 +       kobject_put(&sbinfo->si_kobj);
12156 +
12157 +       return 0;
12158 +}
12159 +
12160 +static const struct file_operations au_fhsm_fops = {
12161 +       .owner          = THIS_MODULE,
12162 +       .llseek         = noop_llseek,
12163 +       .read           = au_fhsm_read,
12164 +       .poll           = au_fhsm_poll,
12165 +       .release        = au_fhsm_release
12166 +};
12167 +
12168 +int au_fhsm_fd(struct super_block *sb, int oflags)
12169 +{
12170 +       int err, fd;
12171 +       struct au_sbinfo *sbinfo;
12172 +       struct au_fhsm *fhsm;
12173 +
12174 +       err = -EPERM;
12175 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
12176 +               goto out;
12177 +
12178 +       err = -EINVAL;
12179 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
12180 +               goto out;
12181 +
12182 +       err = 0;
12183 +       sbinfo = au_sbi(sb);
12184 +       fhsm = &sbinfo->si_fhsm;
12185 +       spin_lock(&fhsm->fhsm_spin);
12186 +       if (!fhsm->fhsm_pid)
12187 +               fhsm->fhsm_pid = current->pid;
12188 +       else
12189 +               err = -EBUSY;
12190 +       spin_unlock(&fhsm->fhsm_spin);
12191 +       if (unlikely(err))
12192 +               goto out;
12193 +
12194 +       oflags |= O_RDONLY;
12195 +       /* oflags |= FMODE_NONOTIFY; */
12196 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
12197 +       err = fd;
12198 +       if (unlikely(fd < 0))
12199 +               goto out_pid;
12200 +
12201 +       /* succeed reglardless 'fhsm' status */
12202 +       kobject_get(&sbinfo->si_kobj);
12203 +       si_noflush_read_lock(sb);
12204 +       if (au_ftest_si(sbinfo, FHSM))
12205 +               au_fhsm_wrote_all(sb, /*force*/0);
12206 +       si_read_unlock(sb);
12207 +       goto out; /* success */
12208 +
12209 +out_pid:
12210 +       spin_lock(&fhsm->fhsm_spin);
12211 +       fhsm->fhsm_pid = 0;
12212 +       spin_unlock(&fhsm->fhsm_spin);
12213 +out:
12214 +       AuTraceErr(err);
12215 +       return err;
12216 +}
12217 +
12218 +/* ---------------------------------------------------------------------- */
12219 +
12220 +int au_fhsm_br_alloc(struct au_branch *br)
12221 +{
12222 +       int err;
12223 +
12224 +       err = 0;
12225 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
12226 +       if (br->br_fhsm)
12227 +               au_br_fhsm_init(br->br_fhsm);
12228 +       else
12229 +               err = -ENOMEM;
12230 +
12231 +       return err;
12232 +}
12233 +
12234 +/* ---------------------------------------------------------------------- */
12235 +
12236 +void au_fhsm_fin(struct super_block *sb)
12237 +{
12238 +       au_fhsm_notify(sb, /*val*/-1);
12239 +}
12240 +
12241 +void au_fhsm_init(struct au_sbinfo *sbinfo)
12242 +{
12243 +       struct au_fhsm *fhsm;
12244 +
12245 +       fhsm = &sbinfo->si_fhsm;
12246 +       spin_lock_init(&fhsm->fhsm_spin);
12247 +       init_waitqueue_head(&fhsm->fhsm_wqh);
12248 +       atomic_set(&fhsm->fhsm_readable, 0);
12249 +       fhsm->fhsm_expire
12250 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
12251 +       fhsm->fhsm_bottom = -1;
12252 +}
12253 +
12254 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
12255 +{
12256 +       sbinfo->si_fhsm.fhsm_expire
12257 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
12258 +}
12259 +
12260 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
12261 +{
12262 +       unsigned int u;
12263 +
12264 +       if (!au_ftest_si(sbinfo, FHSM))
12265 +               return;
12266 +
12267 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
12268 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
12269 +               seq_printf(seq, ",fhsm_sec=%u", u);
12270 +}
12271 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
12272 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
12273 +++ linux/fs/aufs/file.c        2017-03-02 14:24:13.860255635 +0100
12274 @@ -0,0 +1,857 @@
12275 +/*
12276 + * Copyright (C) 2005-2017 Junjiro R. Okajima
12277 + *
12278 + * This program, aufs is free software; you can redistribute it and/or modify
12279 + * it under the terms of the GNU General Public License as published by
12280 + * the Free Software Foundation; either version 2 of the License, or
12281 + * (at your option) any later version.
12282 + *
12283 + * This program is distributed in the hope that it will be useful,
12284 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12285 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12286 + * GNU General Public License for more details.
12287 + *
12288 + * You should have received a copy of the GNU General Public License
12289 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12290 + */
12291 +
12292 +/*
12293 + * handling file/dir, and address_space operation
12294 + */
12295 +
12296 +#ifdef CONFIG_AUFS_DEBUG
12297 +#include <linux/migrate.h>
12298 +#endif
12299 +#include <linux/pagemap.h>
12300 +#include "aufs.h"
12301 +
12302 +/* drop flags for writing */
12303 +unsigned int au_file_roflags(unsigned int flags)
12304 +{
12305 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
12306 +       flags |= O_RDONLY | O_NOATIME;
12307 +       return flags;
12308 +}
12309 +
12310 +/* common functions to regular file and dir */
12311 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12312 +                      struct file *file, int force_wr)
12313 +{
12314 +       struct file *h_file;
12315 +       struct dentry *h_dentry;
12316 +       struct inode *h_inode;
12317 +       struct super_block *sb;
12318 +       struct au_branch *br;
12319 +       struct path h_path;
12320 +       int err;
12321 +
12322 +       /* a race condition can happen between open and unlink/rmdir */
12323 +       h_file = ERR_PTR(-ENOENT);
12324 +       h_dentry = au_h_dptr(dentry, bindex);
12325 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
12326 +               goto out;
12327 +       h_inode = d_inode(h_dentry);
12328 +       spin_lock(&h_dentry->d_lock);
12329 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
12330 +               /* || !d_inode(dentry)->i_nlink */
12331 +               ;
12332 +       spin_unlock(&h_dentry->d_lock);
12333 +       if (unlikely(err))
12334 +               goto out;
12335 +
12336 +       sb = dentry->d_sb;
12337 +       br = au_sbr(sb, bindex);
12338 +       err = au_br_test_oflag(flags, br);
12339 +       h_file = ERR_PTR(err);
12340 +       if (unlikely(err))
12341 +               goto out;
12342 +
12343 +       /* drop flags for writing */
12344 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
12345 +               if (force_wr && !(flags & O_WRONLY))
12346 +                       force_wr = 0;
12347 +               flags = au_file_roflags(flags);
12348 +               if (force_wr) {
12349 +                       h_file = ERR_PTR(-EROFS);
12350 +                       flags = au_file_roflags(flags);
12351 +                       if (unlikely(vfsub_native_ro(h_inode)
12352 +                                    || IS_APPEND(h_inode)))
12353 +                               goto out;
12354 +                       flags &= ~O_ACCMODE;
12355 +                       flags |= O_WRONLY;
12356 +               }
12357 +       }
12358 +       flags &= ~O_CREAT;
12359 +       au_br_get(br);
12360 +       h_path.dentry = h_dentry;
12361 +       h_path.mnt = au_br_mnt(br);
12362 +       h_file = vfsub_dentry_open(&h_path, flags);
12363 +       if (IS_ERR(h_file))
12364 +               goto out_br;
12365 +
12366 +       if (flags & __FMODE_EXEC) {
12367 +               err = deny_write_access(h_file);
12368 +               if (unlikely(err)) {
12369 +                       fput(h_file);
12370 +                       h_file = ERR_PTR(err);
12371 +                       goto out_br;
12372 +               }
12373 +       }
12374 +       fsnotify_open(h_file);
12375 +       goto out; /* success */
12376 +
12377 +out_br:
12378 +       au_br_put(br);
12379 +out:
12380 +       return h_file;
12381 +}
12382 +
12383 +static int au_cmoo(struct dentry *dentry)
12384 +{
12385 +       int err, cmoo;
12386 +       unsigned int udba;
12387 +       struct path h_path;
12388 +       struct au_pin pin;
12389 +       struct au_cp_generic cpg = {
12390 +               .dentry = dentry,
12391 +               .bdst   = -1,
12392 +               .bsrc   = -1,
12393 +               .len    = -1,
12394 +               .pin    = &pin,
12395 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
12396 +       };
12397 +       struct inode *delegated;
12398 +       struct super_block *sb;
12399 +       struct au_sbinfo *sbinfo;
12400 +       struct au_fhsm *fhsm;
12401 +       pid_t pid;
12402 +       struct au_branch *br;
12403 +       struct dentry *parent;
12404 +       struct au_hinode *hdir;
12405 +
12406 +       DiMustWriteLock(dentry);
12407 +       IiMustWriteLock(d_inode(dentry));
12408 +
12409 +       err = 0;
12410 +       if (IS_ROOT(dentry))
12411 +               goto out;
12412 +       cpg.bsrc = au_dbtop(dentry);
12413 +       if (!cpg.bsrc)
12414 +               goto out;
12415 +
12416 +       sb = dentry->d_sb;
12417 +       sbinfo = au_sbi(sb);
12418 +       fhsm = &sbinfo->si_fhsm;
12419 +       pid = au_fhsm_pid(fhsm);
12420 +       if (pid
12421 +           && (current->pid == pid
12422 +               || current->real_parent->pid == pid))
12423 +               goto out;
12424 +
12425 +       br = au_sbr(sb, cpg.bsrc);
12426 +       cmoo = au_br_cmoo(br->br_perm);
12427 +       if (!cmoo)
12428 +               goto out;
12429 +       if (!d_is_reg(dentry))
12430 +               cmoo &= AuBrAttr_COO_ALL;
12431 +       if (!cmoo)
12432 +               goto out;
12433 +
12434 +       parent = dget_parent(dentry);
12435 +       di_write_lock_parent(parent);
12436 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12437 +       cpg.bdst = err;
12438 +       if (unlikely(err < 0)) {
12439 +               err = 0;        /* there is no upper writable branch */
12440 +               goto out_dgrade;
12441 +       }
12442 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12443 +
12444 +       /* do not respect the coo attrib for the target branch */
12445 +       err = au_cpup_dirs(dentry, cpg.bdst);
12446 +       if (unlikely(err))
12447 +               goto out_dgrade;
12448 +
12449 +       di_downgrade_lock(parent, AuLock_IR);
12450 +       udba = au_opt_udba(sb);
12451 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
12452 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12453 +       if (unlikely(err))
12454 +               goto out_parent;
12455 +
12456 +       err = au_sio_cpup_simple(&cpg);
12457 +       au_unpin(&pin);
12458 +       if (unlikely(err))
12459 +               goto out_parent;
12460 +       if (!(cmoo & AuBrWAttr_MOO))
12461 +               goto out_parent; /* success */
12462 +
12463 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
12464 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12465 +       if (unlikely(err))
12466 +               goto out_parent;
12467 +
12468 +       h_path.mnt = au_br_mnt(br);
12469 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
12470 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
12471 +       delegated = NULL;
12472 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12473 +       au_unpin(&pin);
12474 +       /* todo: keep h_dentry or not? */
12475 +       if (unlikely(err == -EWOULDBLOCK)) {
12476 +               pr_warn("cannot retry for NFSv4 delegation"
12477 +                       " for an internal unlink\n");
12478 +               iput(delegated);
12479 +       }
12480 +       if (unlikely(err)) {
12481 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
12482 +                      dentry, err);
12483 +               err = 0;
12484 +       }
12485 +       goto out_parent; /* success */
12486 +
12487 +out_dgrade:
12488 +       di_downgrade_lock(parent, AuLock_IR);
12489 +out_parent:
12490 +       di_read_unlock(parent, AuLock_IR);
12491 +       dput(parent);
12492 +out:
12493 +       AuTraceErr(err);
12494 +       return err;
12495 +}
12496 +
12497 +int au_do_open(struct file *file, struct au_do_open_args *args)
12498 +{
12499 +       int err, no_lock = args->no_lock;
12500 +       struct dentry *dentry;
12501 +       struct au_finfo *finfo;
12502 +
12503 +       if (!no_lock)
12504 +               err = au_finfo_init(file, args->fidir);
12505 +       else {
12506 +               lockdep_off();
12507 +               err = au_finfo_init(file, args->fidir);
12508 +               lockdep_on();
12509 +       }
12510 +       if (unlikely(err))
12511 +               goto out;
12512 +
12513 +       dentry = file->f_path.dentry;
12514 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
12515 +       if (!no_lock) {
12516 +               di_write_lock_child(dentry);
12517 +               err = au_cmoo(dentry);
12518 +               di_downgrade_lock(dentry, AuLock_IR);
12519 +               if (!err)
12520 +                       err = args->open(file, vfsub_file_flags(file), NULL);
12521 +               di_read_unlock(dentry, AuLock_IR);
12522 +       } else {
12523 +               err = au_cmoo(dentry);
12524 +               if (!err)
12525 +                       err = args->open(file, vfsub_file_flags(file),
12526 +                                        args->h_file);
12527 +               if (!err && au_fbtop(file) != au_dbtop(dentry))
12528 +                       /*
12529 +                        * cmoo happens after h_file was opened.
12530 +                        * need to refresh file later.
12531 +                        */
12532 +                       atomic_dec(&au_fi(file)->fi_generation);
12533 +       }
12534 +
12535 +       finfo = au_fi(file);
12536 +       if (!err) {
12537 +               finfo->fi_file = file;
12538 +               au_sphl_add(&finfo->fi_hlist,
12539 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
12540 +       }
12541 +       if (!no_lock)
12542 +               fi_write_unlock(file);
12543 +       else {
12544 +               lockdep_off();
12545 +               fi_write_unlock(file);
12546 +               lockdep_on();
12547 +       }
12548 +       if (unlikely(err)) {
12549 +               finfo->fi_hdir = NULL;
12550 +               au_finfo_fin(file, /*atonce*/0);
12551 +       }
12552 +
12553 +out:
12554 +       return err;
12555 +}
12556 +
12557 +int au_reopen_nondir(struct file *file)
12558 +{
12559 +       int err;
12560 +       aufs_bindex_t btop;
12561 +       struct dentry *dentry;
12562 +       struct file *h_file, *h_file_tmp;
12563 +
12564 +       dentry = file->f_path.dentry;
12565 +       btop = au_dbtop(dentry);
12566 +       h_file_tmp = NULL;
12567 +       if (au_fbtop(file) == btop) {
12568 +               h_file = au_hf_top(file);
12569 +               if (file->f_mode == h_file->f_mode)
12570 +                       return 0; /* success */
12571 +               h_file_tmp = h_file;
12572 +               get_file(h_file_tmp);
12573 +               au_set_h_fptr(file, btop, NULL);
12574 +       }
12575 +       AuDebugOn(au_fi(file)->fi_hdir);
12576 +       /*
12577 +        * it can happen
12578 +        * file exists on both of rw and ro
12579 +        * open --> dbtop and fbtop are both 0
12580 +        * prepend a branch as rw, "rw" become ro
12581 +        * remove rw/file
12582 +        * delete the top branch, "rw" becomes rw again
12583 +        *      --> dbtop is 1, fbtop is still 0
12584 +        * write --> fbtop is 0 but dbtop is 1
12585 +        */
12586 +       /* AuDebugOn(au_fbtop(file) < btop); */
12587 +
12588 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
12589 +                          file, /*force_wr*/0);
12590 +       err = PTR_ERR(h_file);
12591 +       if (IS_ERR(h_file)) {
12592 +               if (h_file_tmp) {
12593 +                       au_sbr_get(dentry->d_sb, btop);
12594 +                       au_set_h_fptr(file, btop, h_file_tmp);
12595 +                       h_file_tmp = NULL;
12596 +               }
12597 +               goto out; /* todo: close all? */
12598 +       }
12599 +
12600 +       err = 0;
12601 +       au_set_fbtop(file, btop);
12602 +       au_set_h_fptr(file, btop, h_file);
12603 +       au_update_figen(file);
12604 +       /* todo: necessary? */
12605 +       /* file->f_ra = h_file->f_ra; */
12606 +
12607 +out:
12608 +       if (h_file_tmp)
12609 +               fput(h_file_tmp);
12610 +       return err;
12611 +}
12612 +
12613 +/* ---------------------------------------------------------------------- */
12614 +
12615 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12616 +                       struct dentry *hi_wh)
12617 +{
12618 +       int err;
12619 +       aufs_bindex_t btop;
12620 +       struct au_dinfo *dinfo;
12621 +       struct dentry *h_dentry;
12622 +       struct au_hdentry *hdp;
12623 +
12624 +       dinfo = au_di(file->f_path.dentry);
12625 +       AuRwMustWriteLock(&dinfo->di_rwsem);
12626 +
12627 +       btop = dinfo->di_btop;
12628 +       dinfo->di_btop = btgt;
12629 +       hdp = au_hdentry(dinfo, btgt);
12630 +       h_dentry = hdp->hd_dentry;
12631 +       hdp->hd_dentry = hi_wh;
12632 +       err = au_reopen_nondir(file);
12633 +       hdp->hd_dentry = h_dentry;
12634 +       dinfo->di_btop = btop;
12635 +
12636 +       return err;
12637 +}
12638 +
12639 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12640 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12641 +{
12642 +       int err;
12643 +       struct inode *inode, *h_inode;
12644 +       struct dentry *h_dentry, *hi_wh;
12645 +       struct au_cp_generic cpg = {
12646 +               .dentry = file->f_path.dentry,
12647 +               .bdst   = bcpup,
12648 +               .bsrc   = -1,
12649 +               .len    = len,
12650 +               .pin    = pin
12651 +       };
12652 +
12653 +       au_update_dbtop(cpg.dentry);
12654 +       inode = d_inode(cpg.dentry);
12655 +       h_inode = NULL;
12656 +       if (au_dbtop(cpg.dentry) <= bcpup
12657 +           && au_dbbot(cpg.dentry) >= bcpup) {
12658 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12659 +               if (h_dentry && d_is_positive(h_dentry))
12660 +                       h_inode = d_inode(h_dentry);
12661 +       }
12662 +       hi_wh = au_hi_wh(inode, bcpup);
12663 +       if (!hi_wh && !h_inode)
12664 +               err = au_sio_cpup_wh(&cpg, file);
12665 +       else
12666 +               /* already copied-up after unlink */
12667 +               err = au_reopen_wh(file, bcpup, hi_wh);
12668 +
12669 +       if (!err
12670 +           && (inode->i_nlink > 1
12671 +               || (inode->i_state & I_LINKABLE))
12672 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12673 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12674 +
12675 +       return err;
12676 +}
12677 +
12678 +/*
12679 + * prepare the @file for writing.
12680 + */
12681 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12682 +{
12683 +       int err;
12684 +       aufs_bindex_t dbtop;
12685 +       struct dentry *parent;
12686 +       struct inode *inode;
12687 +       struct super_block *sb;
12688 +       struct file *h_file;
12689 +       struct au_cp_generic cpg = {
12690 +               .dentry = file->f_path.dentry,
12691 +               .bdst   = -1,
12692 +               .bsrc   = -1,
12693 +               .len    = len,
12694 +               .pin    = pin,
12695 +               .flags  = AuCpup_DTIME
12696 +       };
12697 +
12698 +       sb = cpg.dentry->d_sb;
12699 +       inode = d_inode(cpg.dentry);
12700 +       cpg.bsrc = au_fbtop(file);
12701 +       err = au_test_ro(sb, cpg.bsrc, inode);
12702 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12703 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12704 +                            /*flags*/0);
12705 +               goto out;
12706 +       }
12707 +
12708 +       /* need to cpup or reopen */
12709 +       parent = dget_parent(cpg.dentry);
12710 +       di_write_lock_parent(parent);
12711 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12712 +       cpg.bdst = err;
12713 +       if (unlikely(err < 0))
12714 +               goto out_dgrade;
12715 +       err = 0;
12716 +
12717 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12718 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12719 +               if (unlikely(err))
12720 +                       goto out_dgrade;
12721 +       }
12722 +
12723 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12724 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12725 +       if (unlikely(err))
12726 +               goto out_dgrade;
12727 +
12728 +       dbtop = au_dbtop(cpg.dentry);
12729 +       if (dbtop <= cpg.bdst)
12730 +               cpg.bsrc = cpg.bdst;
12731 +
12732 +       if (dbtop <= cpg.bdst           /* just reopen */
12733 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12734 +               ) {
12735 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12736 +               if (IS_ERR(h_file))
12737 +                       err = PTR_ERR(h_file);
12738 +               else {
12739 +                       di_downgrade_lock(parent, AuLock_IR);
12740 +                       if (dbtop > cpg.bdst)
12741 +                               err = au_sio_cpup_simple(&cpg);
12742 +                       if (!err)
12743 +                               err = au_reopen_nondir(file);
12744 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12745 +               }
12746 +       } else {                        /* copyup as wh and reopen */
12747 +               /*
12748 +                * since writable hfsplus branch is not supported,
12749 +                * h_open_pre/post() are unnecessary.
12750 +                */
12751 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12752 +               di_downgrade_lock(parent, AuLock_IR);
12753 +       }
12754 +
12755 +       if (!err) {
12756 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12757 +               goto out_dput; /* success */
12758 +       }
12759 +       au_unpin(pin);
12760 +       goto out_unlock;
12761 +
12762 +out_dgrade:
12763 +       di_downgrade_lock(parent, AuLock_IR);
12764 +out_unlock:
12765 +       di_read_unlock(parent, AuLock_IR);
12766 +out_dput:
12767 +       dput(parent);
12768 +out:
12769 +       return err;
12770 +}
12771 +
12772 +/* ---------------------------------------------------------------------- */
12773 +
12774 +int au_do_flush(struct file *file, fl_owner_t id,
12775 +               int (*flush)(struct file *file, fl_owner_t id))
12776 +{
12777 +       int err;
12778 +       struct super_block *sb;
12779 +       struct inode *inode;
12780 +
12781 +       inode = file_inode(file);
12782 +       sb = inode->i_sb;
12783 +       si_noflush_read_lock(sb);
12784 +       fi_read_lock(file);
12785 +       ii_read_lock_child(inode);
12786 +
12787 +       err = flush(file, id);
12788 +       au_cpup_attr_timesizes(inode);
12789 +
12790 +       ii_read_unlock(inode);
12791 +       fi_read_unlock(file);
12792 +       si_read_unlock(sb);
12793 +       return err;
12794 +}
12795 +
12796 +/* ---------------------------------------------------------------------- */
12797 +
12798 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12799 +{
12800 +       int err;
12801 +       struct au_pin pin;
12802 +       struct au_finfo *finfo;
12803 +       struct dentry *parent, *hi_wh;
12804 +       struct inode *inode;
12805 +       struct super_block *sb;
12806 +       struct au_cp_generic cpg = {
12807 +               .dentry = file->f_path.dentry,
12808 +               .bdst   = -1,
12809 +               .bsrc   = -1,
12810 +               .len    = -1,
12811 +               .pin    = &pin,
12812 +               .flags  = AuCpup_DTIME
12813 +       };
12814 +
12815 +       FiMustWriteLock(file);
12816 +
12817 +       err = 0;
12818 +       finfo = au_fi(file);
12819 +       sb = cpg.dentry->d_sb;
12820 +       inode = d_inode(cpg.dentry);
12821 +       cpg.bdst = au_ibtop(inode);
12822 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12823 +               goto out;
12824 +
12825 +       parent = dget_parent(cpg.dentry);
12826 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12827 +               di_read_lock_parent(parent, !AuLock_IR);
12828 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12829 +               cpg.bdst = err;
12830 +               di_read_unlock(parent, !AuLock_IR);
12831 +               if (unlikely(err < 0))
12832 +                       goto out_parent;
12833 +               err = 0;
12834 +       }
12835 +
12836 +       di_read_lock_parent(parent, AuLock_IR);
12837 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12838 +       if (!S_ISDIR(inode->i_mode)
12839 +           && au_opt_test(au_mntflags(sb), PLINK)
12840 +           && au_plink_test(inode)
12841 +           && !d_unhashed(cpg.dentry)
12842 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
12843 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12844 +               if (unlikely(err))
12845 +                       goto out_unlock;
12846 +
12847 +               /* always superio. */
12848 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12849 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12850 +               if (!err) {
12851 +                       err = au_sio_cpup_simple(&cpg);
12852 +                       au_unpin(&pin);
12853 +               }
12854 +       } else if (hi_wh) {
12855 +               /* already copied-up after unlink */
12856 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12857 +               *need_reopen = 0;
12858 +       }
12859 +
12860 +out_unlock:
12861 +       di_read_unlock(parent, AuLock_IR);
12862 +out_parent:
12863 +       dput(parent);
12864 +out:
12865 +       return err;
12866 +}
12867 +
12868 +static void au_do_refresh_dir(struct file *file)
12869 +{
12870 +       int execed;
12871 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
12872 +       struct au_hfile *p, tmp, *q;
12873 +       struct au_finfo *finfo;
12874 +       struct super_block *sb;
12875 +       struct au_fidir *fidir;
12876 +
12877 +       FiMustWriteLock(file);
12878 +
12879 +       sb = file->f_path.dentry->d_sb;
12880 +       finfo = au_fi(file);
12881 +       fidir = finfo->fi_hdir;
12882 +       AuDebugOn(!fidir);
12883 +       p = fidir->fd_hfile + finfo->fi_btop;
12884 +       brid = p->hf_br->br_id;
12885 +       bbot = fidir->fd_bbot;
12886 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
12887 +               if (!p->hf_file)
12888 +                       continue;
12889 +
12890 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12891 +               if (new_bindex == bindex)
12892 +                       continue;
12893 +               if (new_bindex < 0) {
12894 +                       au_set_h_fptr(file, bindex, NULL);
12895 +                       continue;
12896 +               }
12897 +
12898 +               /* swap two lower inode, and loop again */
12899 +               q = fidir->fd_hfile + new_bindex;
12900 +               tmp = *q;
12901 +               *q = *p;
12902 +               *p = tmp;
12903 +               if (tmp.hf_file) {
12904 +                       bindex--;
12905 +                       p--;
12906 +               }
12907 +       }
12908 +
12909 +       execed = vfsub_file_execed(file);
12910 +       p = fidir->fd_hfile;
12911 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12912 +               bbot = au_sbbot(sb);
12913 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
12914 +                    finfo->fi_btop++, p++)
12915 +                       if (p->hf_file) {
12916 +                               if (file_inode(p->hf_file))
12917 +                                       break;
12918 +                               au_hfput(p, execed);
12919 +                       }
12920 +       } else {
12921 +               bbot = au_br_index(sb, brid);
12922 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
12923 +                    finfo->fi_btop++, p++)
12924 +                       if (p->hf_file)
12925 +                               au_hfput(p, execed);
12926 +               bbot = au_sbbot(sb);
12927 +       }
12928 +
12929 +       p = fidir->fd_hfile + bbot;
12930 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
12931 +            fidir->fd_bbot--, p--)
12932 +               if (p->hf_file) {
12933 +                       if (file_inode(p->hf_file))
12934 +                               break;
12935 +                       au_hfput(p, execed);
12936 +               }
12937 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12938 +}
12939 +
12940 +/*
12941 + * after branch manipulating, refresh the file.
12942 + */
12943 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12944 +{
12945 +       int err, need_reopen, nbr;
12946 +       aufs_bindex_t bbot, bindex;
12947 +       struct dentry *dentry;
12948 +       struct super_block *sb;
12949 +       struct au_finfo *finfo;
12950 +       struct au_hfile *hfile;
12951 +
12952 +       dentry = file->f_path.dentry;
12953 +       sb = dentry->d_sb;
12954 +       nbr = au_sbbot(sb) + 1;
12955 +       finfo = au_fi(file);
12956 +       if (!finfo->fi_hdir) {
12957 +               hfile = &finfo->fi_htop;
12958 +               AuDebugOn(!hfile->hf_file);
12959 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
12960 +               AuDebugOn(bindex < 0);
12961 +               if (bindex != finfo->fi_btop)
12962 +                       au_set_fbtop(file, bindex);
12963 +       } else {
12964 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
12965 +               if (unlikely(err))
12966 +                       goto out;
12967 +               au_do_refresh_dir(file);
12968 +       }
12969 +
12970 +       err = 0;
12971 +       need_reopen = 1;
12972 +       if (!au_test_mmapped(file))
12973 +               err = au_file_refresh_by_inode(file, &need_reopen);
12974 +       if (finfo->fi_hdir)
12975 +               /* harmless if err */
12976 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
12977 +       if (!err && need_reopen && !d_unlinked(dentry))
12978 +               err = reopen(file);
12979 +       if (!err) {
12980 +               au_update_figen(file);
12981 +               goto out; /* success */
12982 +       }
12983 +
12984 +       /* error, close all lower files */
12985 +       if (finfo->fi_hdir) {
12986 +               bbot = au_fbbot_dir(file);
12987 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
12988 +                       au_set_h_fptr(file, bindex, NULL);
12989 +       }
12990 +
12991 +out:
12992 +       return err;
12993 +}
12994 +
12995 +/* common function to regular file and dir */
12996 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12997 +                         int wlock)
12998 +{
12999 +       int err;
13000 +       unsigned int sigen, figen;
13001 +       aufs_bindex_t btop;
13002 +       unsigned char pseudo_link;
13003 +       struct dentry *dentry;
13004 +       struct inode *inode;
13005 +
13006 +       err = 0;
13007 +       dentry = file->f_path.dentry;
13008 +       inode = d_inode(dentry);
13009 +       sigen = au_sigen(dentry->d_sb);
13010 +       fi_write_lock(file);
13011 +       figen = au_figen(file);
13012 +       di_write_lock_child(dentry);
13013 +       btop = au_dbtop(dentry);
13014 +       pseudo_link = (btop != au_ibtop(inode));
13015 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
13016 +               if (!wlock) {
13017 +                       di_downgrade_lock(dentry, AuLock_IR);
13018 +                       fi_downgrade_lock(file);
13019 +               }
13020 +               goto out; /* success */
13021 +       }
13022 +
13023 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
13024 +       if (au_digen_test(dentry, sigen)) {
13025 +               err = au_reval_dpath(dentry, sigen);
13026 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
13027 +       }
13028 +
13029 +       if (!err)
13030 +               err = refresh_file(file, reopen);
13031 +       if (!err) {
13032 +               if (!wlock) {
13033 +                       di_downgrade_lock(dentry, AuLock_IR);
13034 +                       fi_downgrade_lock(file);
13035 +               }
13036 +       } else {
13037 +               di_write_unlock(dentry);
13038 +               fi_write_unlock(file);
13039 +       }
13040 +
13041 +out:
13042 +       return err;
13043 +}
13044 +
13045 +/* ---------------------------------------------------------------------- */
13046 +
13047 +/* cf. aufs_nopage() */
13048 +/* for madvise(2) */
13049 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
13050 +{
13051 +       unlock_page(page);
13052 +       return 0;
13053 +}
13054 +
13055 +/* it will never be called, but necessary to support O_DIRECT */
13056 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
13057 +{ BUG(); return 0; }
13058 +
13059 +/* they will never be called. */
13060 +#ifdef CONFIG_AUFS_DEBUG
13061 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
13062 +                           loff_t pos, unsigned len, unsigned flags,
13063 +                           struct page **pagep, void **fsdata)
13064 +{ AuUnsupport(); return 0; }
13065 +static int aufs_write_end(struct file *file, struct address_space *mapping,
13066 +                         loff_t pos, unsigned len, unsigned copied,
13067 +                         struct page *page, void *fsdata)
13068 +{ AuUnsupport(); return 0; }
13069 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
13070 +{ AuUnsupport(); return 0; }
13071 +
13072 +static int aufs_set_page_dirty(struct page *page)
13073 +{ AuUnsupport(); return 0; }
13074 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
13075 +                               unsigned int length)
13076 +{ AuUnsupport(); }
13077 +static int aufs_releasepage(struct page *page, gfp_t gfp)
13078 +{ AuUnsupport(); return 0; }
13079 +#if 0 /* called by memory compaction regardless file */
13080 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
13081 +                           struct page *page, enum migrate_mode mode)
13082 +{ AuUnsupport(); return 0; }
13083 +#endif
13084 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
13085 +{ AuUnsupport(); return true; }
13086 +static void aufs_putback_page(struct page *page)
13087 +{ AuUnsupport(); }
13088 +static int aufs_launder_page(struct page *page)
13089 +{ AuUnsupport(); return 0; }
13090 +static int aufs_is_partially_uptodate(struct page *page,
13091 +                                     unsigned long from,
13092 +                                     unsigned long count)
13093 +{ AuUnsupport(); return 0; }
13094 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
13095 +                                   bool *writeback)
13096 +{ AuUnsupport(); }
13097 +static int aufs_error_remove_page(struct address_space *mapping,
13098 +                                 struct page *page)
13099 +{ AuUnsupport(); return 0; }
13100 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
13101 +                             sector_t *span)
13102 +{ AuUnsupport(); return 0; }
13103 +static void aufs_swap_deactivate(struct file *file)
13104 +{ AuUnsupport(); }
13105 +#endif /* CONFIG_AUFS_DEBUG */
13106 +
13107 +const struct address_space_operations aufs_aop = {
13108 +       .readpage               = aufs_readpage,
13109 +       .direct_IO              = aufs_direct_IO,
13110 +#ifdef CONFIG_AUFS_DEBUG
13111 +       .writepage              = aufs_writepage,
13112 +       /* no writepages, because of writepage */
13113 +       .set_page_dirty         = aufs_set_page_dirty,
13114 +       /* no readpages, because of readpage */
13115 +       .write_begin            = aufs_write_begin,
13116 +       .write_end              = aufs_write_end,
13117 +       /* no bmap, no block device */
13118 +       .invalidatepage         = aufs_invalidatepage,
13119 +       .releasepage            = aufs_releasepage,
13120 +       /* is fallback_migrate_page ok? */
13121 +       /* .migratepage         = aufs_migratepage, */
13122 +       .isolate_page           = aufs_isolate_page,
13123 +       .putback_page           = aufs_putback_page,
13124 +       .launder_page           = aufs_launder_page,
13125 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
13126 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
13127 +       .error_remove_page      = aufs_error_remove_page,
13128 +       .swap_activate          = aufs_swap_activate,
13129 +       .swap_deactivate        = aufs_swap_deactivate
13130 +#endif /* CONFIG_AUFS_DEBUG */
13131 +};
13132 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
13133 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
13134 +++ linux/fs/aufs/file.h        2017-03-02 14:24:13.860255635 +0100
13135 @@ -0,0 +1,294 @@
13136 +/*
13137 + * Copyright (C) 2005-2017 Junjiro R. Okajima
13138 + *
13139 + * This program, aufs is free software; you can redistribute it and/or modify
13140 + * it under the terms of the GNU General Public License as published by
13141 + * the Free Software Foundation; either version 2 of the License, or
13142 + * (at your option) any later version.
13143 + *
13144 + * This program is distributed in the hope that it will be useful,
13145 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13146 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13147 + * GNU General Public License for more details.
13148 + *
13149 + * You should have received a copy of the GNU General Public License
13150 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13151 + */
13152 +
13153 +/*
13154 + * file operations
13155 + */
13156 +
13157 +#ifndef __AUFS_FILE_H__
13158 +#define __AUFS_FILE_H__
13159 +
13160 +#ifdef __KERNEL__
13161 +
13162 +#include <linux/file.h>
13163 +#include <linux/fs.h>
13164 +#include <linux/poll.h>
13165 +#include "rwsem.h"
13166 +
13167 +struct au_branch;
13168 +struct au_hfile {
13169 +       struct file             *hf_file;
13170 +       struct au_branch        *hf_br;
13171 +};
13172 +
13173 +struct au_vdir;
13174 +struct au_fidir {
13175 +       aufs_bindex_t           fd_bbot;
13176 +       aufs_bindex_t           fd_nent;
13177 +       struct au_vdir          *fd_vdir_cache;
13178 +       struct au_hfile         fd_hfile[];
13179 +};
13180 +
13181 +static inline int au_fidir_sz(int nent)
13182 +{
13183 +       AuDebugOn(nent < 0);
13184 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
13185 +}
13186 +
13187 +struct au_finfo {
13188 +       atomic_t                fi_generation;
13189 +
13190 +       struct au_rwsem         fi_rwsem;
13191 +       aufs_bindex_t           fi_btop;
13192 +
13193 +       /* do not union them */
13194 +       struct {                                /* for non-dir */
13195 +               struct au_hfile                 fi_htop;
13196 +               atomic_t                        fi_mmapped;
13197 +       };
13198 +       struct au_fidir         *fi_hdir;       /* for dir only */
13199 +
13200 +       struct hlist_node       fi_hlist;
13201 +       union {
13202 +               struct file             *fi_file;       /* very ugly */
13203 +               struct llist_node       fi_lnode;       /* delayed free */
13204 +       };
13205 +} ____cacheline_aligned_in_smp;
13206 +
13207 +/* ---------------------------------------------------------------------- */
13208 +
13209 +/* file.c */
13210 +extern const struct address_space_operations aufs_aop;
13211 +unsigned int au_file_roflags(unsigned int flags);
13212 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
13213 +                      struct file *file, int force_wr);
13214 +struct au_do_open_args {
13215 +       int             no_lock;
13216 +       int             (*open)(struct file *file, int flags,
13217 +                               struct file *h_file);
13218 +       struct au_fidir *fidir;
13219 +       struct file     *h_file;
13220 +};
13221 +int au_do_open(struct file *file, struct au_do_open_args *args);
13222 +int au_reopen_nondir(struct file *file);
13223 +struct au_pin;
13224 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
13225 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
13226 +                         int wlock);
13227 +int au_do_flush(struct file *file, fl_owner_t id,
13228 +               int (*flush)(struct file *file, fl_owner_t id));
13229 +
13230 +/* poll.c */
13231 +#ifdef CONFIG_AUFS_POLL
13232 +unsigned int aufs_poll(struct file *file, poll_table *wait);
13233 +#endif
13234 +
13235 +#ifdef CONFIG_AUFS_BR_HFSPLUS
13236 +/* hfsplus.c */
13237 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
13238 +                          int force_wr);
13239 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13240 +                   struct file *h_file);
13241 +#else
13242 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
13243 +       aufs_bindex_t bindex, int force_wr)
13244 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
13245 +          struct file *h_file);
13246 +#endif
13247 +
13248 +/* f_op.c */
13249 +extern const struct file_operations aufs_file_fop;
13250 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
13251 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
13252 +struct file *au_read_pre(struct file *file, int keep_fi);
13253 +
13254 +/* finfo.c */
13255 +void au_hfput(struct au_hfile *hf, int execed);
13256 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
13257 +                  struct file *h_file);
13258 +
13259 +void au_update_figen(struct file *file);
13260 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
13261 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
13262 +
13263 +void au_fi_init_once(void *_fi);
13264 +void au_finfo_fin(struct file *file, int atonce);
13265 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
13266 +
13267 +/* ioctl.c */
13268 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
13269 +#ifdef CONFIG_COMPAT
13270 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
13271 +                          unsigned long arg);
13272 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
13273 +                             unsigned long arg);
13274 +#endif
13275 +
13276 +/* ---------------------------------------------------------------------- */
13277 +
13278 +static inline struct au_finfo *au_fi(struct file *file)
13279 +{
13280 +       return file->private_data;
13281 +}
13282 +
13283 +/* ---------------------------------------------------------------------- */
13284 +
13285 +/*
13286 + * fi_read_lock, fi_write_lock,
13287 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
13288 + */
13289 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
13290 +
13291 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
13292 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
13293 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
13294 +
13295 +/* ---------------------------------------------------------------------- */
13296 +
13297 +/* todo: hard/soft set? */
13298 +static inline aufs_bindex_t au_fbtop(struct file *file)
13299 +{
13300 +       FiMustAnyLock(file);
13301 +       return au_fi(file)->fi_btop;
13302 +}
13303 +
13304 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
13305 +{
13306 +       FiMustAnyLock(file);
13307 +       AuDebugOn(!au_fi(file)->fi_hdir);
13308 +       return au_fi(file)->fi_hdir->fd_bbot;
13309 +}
13310 +
13311 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
13312 +{
13313 +       FiMustAnyLock(file);
13314 +       AuDebugOn(!au_fi(file)->fi_hdir);
13315 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
13316 +}
13317 +
13318 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
13319 +{
13320 +       FiMustWriteLock(file);
13321 +       au_fi(file)->fi_btop = bindex;
13322 +}
13323 +
13324 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
13325 +{
13326 +       FiMustWriteLock(file);
13327 +       AuDebugOn(!au_fi(file)->fi_hdir);
13328 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
13329 +}
13330 +
13331 +static inline void au_set_fvdir_cache(struct file *file,
13332 +                                     struct au_vdir *vdir_cache)
13333 +{
13334 +       FiMustWriteLock(file);
13335 +       AuDebugOn(!au_fi(file)->fi_hdir);
13336 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
13337 +}
13338 +
13339 +static inline struct file *au_hf_top(struct file *file)
13340 +{
13341 +       FiMustAnyLock(file);
13342 +       AuDebugOn(au_fi(file)->fi_hdir);
13343 +       return au_fi(file)->fi_htop.hf_file;
13344 +}
13345 +
13346 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13347 +{
13348 +       FiMustAnyLock(file);
13349 +       AuDebugOn(!au_fi(file)->fi_hdir);
13350 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
13351 +}
13352 +
13353 +/* todo: memory barrier? */
13354 +static inline unsigned int au_figen(struct file *f)
13355 +{
13356 +       return atomic_read(&au_fi(f)->fi_generation);
13357 +}
13358 +
13359 +static inline void au_set_mmapped(struct file *f)
13360 +{
13361 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13362 +               return;
13363 +       pr_warn("fi_mmapped wrapped around\n");
13364 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13365 +               ;
13366 +}
13367 +
13368 +static inline void au_unset_mmapped(struct file *f)
13369 +{
13370 +       atomic_dec(&au_fi(f)->fi_mmapped);
13371 +}
13372 +
13373 +static inline int au_test_mmapped(struct file *f)
13374 +{
13375 +       return atomic_read(&au_fi(f)->fi_mmapped);
13376 +}
13377 +
13378 +/* customize vma->vm_file */
13379 +
13380 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13381 +                                      struct file *file)
13382 +{
13383 +       struct file *f;
13384 +
13385 +       f = vma->vm_file;
13386 +       get_file(file);
13387 +       vma->vm_file = file;
13388 +       fput(f);
13389 +}
13390 +
13391 +#ifdef CONFIG_MMU
13392 +#define AuDbgVmRegion(file, vma) do {} while (0)
13393 +
13394 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13395 +                                   struct file *file)
13396 +{
13397 +       au_do_vm_file_reset(vma, file);
13398 +}
13399 +#else
13400 +#define AuDbgVmRegion(file, vma) \
13401 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13402 +
13403 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13404 +                                   struct file *file)
13405 +{
13406 +       struct file *f;
13407 +
13408 +       au_do_vm_file_reset(vma, file);
13409 +       f = vma->vm_region->vm_file;
13410 +       get_file(file);
13411 +       vma->vm_region->vm_file = file;
13412 +       fput(f);
13413 +}
13414 +#endif /* CONFIG_MMU */
13415 +
13416 +/* handle vma->vm_prfile */
13417 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
13418 +                                   struct file *file)
13419 +{
13420 +       get_file(file);
13421 +       vma->vm_prfile = file;
13422 +#ifndef CONFIG_MMU
13423 +       get_file(file);
13424 +       vma->vm_region->vm_prfile = file;
13425 +#endif
13426 +}
13427 +
13428 +#endif /* __KERNEL__ */
13429 +#endif /* __AUFS_FILE_H__ */
13430 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13431 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
13432 +++ linux/fs/aufs/finfo.c       2017-03-02 14:24:13.860255635 +0100
13433 @@ -0,0 +1,151 @@
13434 +/*
13435 + * Copyright (C) 2005-2017 Junjiro R. Okajima
13436 + *
13437 + * This program, aufs is free software; you can redistribute it and/or modify
13438 + * it under the terms of the GNU General Public License as published by
13439 + * the Free Software Foundation; either version 2 of the License, or
13440 + * (at your option) any later version.
13441 + *
13442 + * This program is distributed in the hope that it will be useful,
13443 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13444 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13445 + * GNU General Public License for more details.
13446 + *
13447 + * You should have received a copy of the GNU General Public License
13448 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13449 + */
13450 +
13451 +/*
13452 + * file private data
13453 + */
13454 +
13455 +#include "aufs.h"
13456 +
13457 +void au_hfput(struct au_hfile *hf, int execed)
13458 +{
13459 +       if (execed)
13460 +               allow_write_access(hf->hf_file);
13461 +       fput(hf->hf_file);
13462 +       hf->hf_file = NULL;
13463 +       au_br_put(hf->hf_br);
13464 +       hf->hf_br = NULL;
13465 +}
13466 +
13467 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13468 +{
13469 +       struct au_finfo *finfo = au_fi(file);
13470 +       struct au_hfile *hf;
13471 +       struct au_fidir *fidir;
13472 +
13473 +       fidir = finfo->fi_hdir;
13474 +       if (!fidir) {
13475 +               AuDebugOn(finfo->fi_btop != bindex);
13476 +               hf = &finfo->fi_htop;
13477 +       } else
13478 +               hf = fidir->fd_hfile + bindex;
13479 +
13480 +       if (hf && hf->hf_file)
13481 +               au_hfput(hf, vfsub_file_execed(file));
13482 +       if (val) {
13483 +               FiMustWriteLock(file);
13484 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
13485 +               hf->hf_file = val;
13486 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
13487 +       }
13488 +}
13489 +
13490 +void au_update_figen(struct file *file)
13491 +{
13492 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
13493 +       /* smp_mb(); */ /* atomic_set */
13494 +}
13495 +
13496 +/* ---------------------------------------------------------------------- */
13497 +
13498 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
13499 +{
13500 +       struct au_fidir *fidir;
13501 +       int nbr;
13502 +
13503 +       nbr = au_sbbot(sb) + 1;
13504 +       if (nbr < 2)
13505 +               nbr = 2; /* initial allocate for 2 branches */
13506 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13507 +       if (fidir) {
13508 +               fidir->fd_bbot = -1;
13509 +               fidir->fd_nent = nbr;
13510 +       }
13511 +
13512 +       return fidir;
13513 +}
13514 +
13515 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
13516 +{
13517 +       int err;
13518 +       struct au_fidir *fidir, *p;
13519 +
13520 +       AuRwMustWriteLock(&finfo->fi_rwsem);
13521 +       fidir = finfo->fi_hdir;
13522 +       AuDebugOn(!fidir);
13523 +
13524 +       err = -ENOMEM;
13525 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13526 +                        GFP_NOFS, may_shrink);
13527 +       if (p) {
13528 +               p->fd_nent = nbr;
13529 +               finfo->fi_hdir = p;
13530 +               err = 0;
13531 +       }
13532 +
13533 +       return err;
13534 +}
13535 +
13536 +/* ---------------------------------------------------------------------- */
13537 +
13538 +void au_finfo_fin(struct file *file, int atonce)
13539 +{
13540 +       struct au_finfo *finfo;
13541 +
13542 +       au_nfiles_dec(file->f_path.dentry->d_sb);
13543 +
13544 +       finfo = au_fi(file);
13545 +       AuDebugOn(finfo->fi_hdir);
13546 +       AuRwDestroy(&finfo->fi_rwsem);
13547 +       if (!atonce)
13548 +               au_cache_dfree_finfo(finfo);
13549 +       else
13550 +               au_cache_free_finfo(finfo);
13551 +}
13552 +
13553 +void au_fi_init_once(void *_finfo)
13554 +{
13555 +       struct au_finfo *finfo = _finfo;
13556 +
13557 +       au_rw_init(&finfo->fi_rwsem);
13558 +}
13559 +
13560 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
13561 +{
13562 +       int err;
13563 +       struct au_finfo *finfo;
13564 +       struct dentry *dentry;
13565 +
13566 +       err = -ENOMEM;
13567 +       dentry = file->f_path.dentry;
13568 +       finfo = au_cache_alloc_finfo();
13569 +       if (unlikely(!finfo))
13570 +               goto out;
13571 +
13572 +       err = 0;
13573 +       au_nfiles_inc(dentry->d_sb);
13574 +       au_rw_write_lock(&finfo->fi_rwsem);
13575 +       finfo->fi_btop = -1;
13576 +       finfo->fi_hdir = fidir;
13577 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
13578 +       /* smp_mb(); */ /* atomic_set */
13579 +
13580 +       file->private_data = finfo;
13581 +
13582 +out:
13583 +       return err;
13584 +}
13585 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13586 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
13587 +++ linux/fs/aufs/f_op.c        2017-03-02 14:24:13.860255635 +0100
13588 @@ -0,0 +1,723 @@
13589 +/*
13590 + * Copyright (C) 2005-2017 Junjiro R. Okajima
13591 + *
13592 + * This program, aufs is free software; you can redistribute it and/or modify
13593 + * it under the terms of the GNU General Public License as published by
13594 + * the Free Software Foundation; either version 2 of the License, or
13595 + * (at your option) any later version.
13596 + *
13597 + * This program is distributed in the hope that it will be useful,
13598 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13599 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13600 + * GNU General Public License for more details.
13601 + *
13602 + * You should have received a copy of the GNU General Public License
13603 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13604 + */
13605 +
13606 +/*
13607 + * file and vm operations
13608 + */
13609 +
13610 +#include <linux/aio.h>
13611 +#include <linux/fs_stack.h>
13612 +#include <linux/mman.h>
13613 +#include <linux/security.h>
13614 +#include "aufs.h"
13615 +
13616 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13617 +{
13618 +       int err;
13619 +       aufs_bindex_t bindex;
13620 +       struct dentry *dentry, *h_dentry;
13621 +       struct au_finfo *finfo;
13622 +       struct inode *h_inode;
13623 +
13624 +       FiMustWriteLock(file);
13625 +
13626 +       err = 0;
13627 +       dentry = file->f_path.dentry;
13628 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
13629 +       finfo = au_fi(file);
13630 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13631 +       atomic_set(&finfo->fi_mmapped, 0);
13632 +       bindex = au_dbtop(dentry);
13633 +       if (!h_file) {
13634 +               h_dentry = au_h_dptr(dentry, bindex);
13635 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13636 +               if (unlikely(err))
13637 +                       goto out;
13638 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13639 +       } else {
13640 +               h_dentry = h_file->f_path.dentry;
13641 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13642 +               if (unlikely(err))
13643 +                       goto out;
13644 +               get_file(h_file);
13645 +       }
13646 +       if (IS_ERR(h_file))
13647 +               err = PTR_ERR(h_file);
13648 +       else {
13649 +               if ((flags & __O_TMPFILE)
13650 +                   && !(flags & O_EXCL)) {
13651 +                       h_inode = file_inode(h_file);
13652 +                       spin_lock(&h_inode->i_lock);
13653 +                       h_inode->i_state |= I_LINKABLE;
13654 +                       spin_unlock(&h_inode->i_lock);
13655 +               }
13656 +               au_set_fbtop(file, bindex);
13657 +               au_set_h_fptr(file, bindex, h_file);
13658 +               au_update_figen(file);
13659 +               /* todo: necessary? */
13660 +               /* file->f_ra = h_file->f_ra; */
13661 +       }
13662 +
13663 +out:
13664 +       return err;
13665 +}
13666 +
13667 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13668 +                           struct file *file)
13669 +{
13670 +       int err;
13671 +       struct super_block *sb;
13672 +       struct au_do_open_args args = {
13673 +               .open   = au_do_open_nondir
13674 +       };
13675 +
13676 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13677 +             file, vfsub_file_flags(file), file->f_mode);
13678 +
13679 +       sb = file->f_path.dentry->d_sb;
13680 +       si_read_lock(sb, AuLock_FLUSH);
13681 +       err = au_do_open(file, &args);
13682 +       si_read_unlock(sb);
13683 +       return err;
13684 +}
13685 +
13686 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13687 +{
13688 +       struct au_finfo *finfo;
13689 +       aufs_bindex_t bindex;
13690 +       int delayed;
13691 +
13692 +       finfo = au_fi(file);
13693 +       au_sphl_del(&finfo->fi_hlist,
13694 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13695 +       bindex = finfo->fi_btop;
13696 +       if (bindex >= 0)
13697 +               au_set_h_fptr(file, bindex, NULL);
13698 +
13699 +       delayed = (current->flags & PF_KTHREAD) || in_interrupt();
13700 +       au_finfo_fin(file, delayed);
13701 +       return 0;
13702 +}
13703 +
13704 +/* ---------------------------------------------------------------------- */
13705 +
13706 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13707 +{
13708 +       int err;
13709 +       struct file *h_file;
13710 +
13711 +       err = 0;
13712 +       h_file = au_hf_top(file);
13713 +       if (h_file)
13714 +               err = vfsub_flush(h_file, id);
13715 +       return err;
13716 +}
13717 +
13718 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13719 +{
13720 +       return au_do_flush(file, id, au_do_flush_nondir);
13721 +}
13722 +
13723 +/* ---------------------------------------------------------------------- */
13724 +/*
13725 + * read and write functions acquire [fdi]_rwsem once, but release before
13726 + * mmap_sem. This is because to stop a race condition between mmap(2).
13727 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13728 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13729 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13730 + */
13731 +
13732 +/* Callers should call au_read_post() or fput() in the end */
13733 +struct file *au_read_pre(struct file *file, int keep_fi)
13734 +{
13735 +       struct file *h_file;
13736 +       int err;
13737 +
13738 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13739 +       if (!err) {
13740 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
13741 +               h_file = au_hf_top(file);
13742 +               get_file(h_file);
13743 +               if (!keep_fi)
13744 +                       fi_read_unlock(file);
13745 +       } else
13746 +               h_file = ERR_PTR(err);
13747 +
13748 +       return h_file;
13749 +}
13750 +
13751 +static void au_read_post(struct inode *inode, struct file *h_file)
13752 +{
13753 +       /* update without lock, I don't think it a problem */
13754 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
13755 +       fput(h_file);
13756 +}
13757 +
13758 +struct au_write_pre {
13759 +       blkcnt_t blks;
13760 +       aufs_bindex_t btop;
13761 +};
13762 +
13763 +/*
13764 + * return with iinfo is write-locked
13765 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13766 + * end
13767 + */
13768 +static struct file *au_write_pre(struct file *file, int do_ready,
13769 +                                struct au_write_pre *wpre)
13770 +{
13771 +       struct file *h_file;
13772 +       struct dentry *dentry;
13773 +       int err;
13774 +       struct au_pin pin;
13775 +
13776 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13777 +       h_file = ERR_PTR(err);
13778 +       if (unlikely(err))
13779 +               goto out;
13780 +
13781 +       dentry = file->f_path.dentry;
13782 +       if (do_ready) {
13783 +               err = au_ready_to_write(file, -1, &pin);
13784 +               if (unlikely(err)) {
13785 +                       h_file = ERR_PTR(err);
13786 +                       di_write_unlock(dentry);
13787 +                       goto out_fi;
13788 +               }
13789 +       }
13790 +
13791 +       di_downgrade_lock(dentry, /*flags*/0);
13792 +       if (wpre)
13793 +               wpre->btop = au_fbtop(file);
13794 +       h_file = au_hf_top(file);
13795 +       get_file(h_file);
13796 +       if (wpre)
13797 +               wpre->blks = file_inode(h_file)->i_blocks;
13798 +       if (do_ready)
13799 +               au_unpin(&pin);
13800 +       di_read_unlock(dentry, /*flags*/0);
13801 +
13802 +out_fi:
13803 +       fi_write_unlock(file);
13804 +out:
13805 +       return h_file;
13806 +}
13807 +
13808 +static void au_write_post(struct inode *inode, struct file *h_file,
13809 +                         struct au_write_pre *wpre, ssize_t written)
13810 +{
13811 +       struct inode *h_inode;
13812 +
13813 +       au_cpup_attr_timesizes(inode);
13814 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
13815 +       h_inode = file_inode(h_file);
13816 +       inode->i_mode = h_inode->i_mode;
13817 +       ii_write_unlock(inode);
13818 +       fput(h_file);
13819 +
13820 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13821 +       if (written > 0)
13822 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
13823 +                             /*force*/h_inode->i_blocks > wpre->blks);
13824 +}
13825 +
13826 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13827 +                        loff_t *ppos)
13828 +{
13829 +       ssize_t err;
13830 +       struct inode *inode;
13831 +       struct file *h_file;
13832 +       struct super_block *sb;
13833 +
13834 +       inode = file_inode(file);
13835 +       sb = inode->i_sb;
13836 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13837 +
13838 +       h_file = au_read_pre(file, /*keep_fi*/0);
13839 +       err = PTR_ERR(h_file);
13840 +       if (IS_ERR(h_file))
13841 +               goto out;
13842 +
13843 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13844 +       err = vfsub_read_u(h_file, buf, count, ppos);
13845 +       /* todo: necessary? */
13846 +       /* file->f_ra = h_file->f_ra; */
13847 +       au_read_post(inode, h_file);
13848 +
13849 +out:
13850 +       si_read_unlock(sb);
13851 +       return err;
13852 +}
13853 +
13854 +/*
13855 + * todo: very ugly
13856 + * it locks both of i_mutex and si_rwsem for read in safe.
13857 + * if the plink maintenance mode continues forever (that is the problem),
13858 + * may loop forever.
13859 + */
13860 +static void au_mtx_and_read_lock(struct inode *inode)
13861 +{
13862 +       int err;
13863 +       struct super_block *sb = inode->i_sb;
13864 +
13865 +       while (1) {
13866 +               inode_lock(inode);
13867 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13868 +               if (!err)
13869 +                       break;
13870 +               inode_unlock(inode);
13871 +               si_read_lock(sb, AuLock_NOPLMW);
13872 +               si_read_unlock(sb);
13873 +       }
13874 +}
13875 +
13876 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13877 +                         size_t count, loff_t *ppos)
13878 +{
13879 +       ssize_t err;
13880 +       struct au_write_pre wpre;
13881 +       struct inode *inode;
13882 +       struct file *h_file;
13883 +       char __user *buf = (char __user *)ubuf;
13884 +
13885 +       inode = file_inode(file);
13886 +       au_mtx_and_read_lock(inode);
13887 +
13888 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13889 +       err = PTR_ERR(h_file);
13890 +       if (IS_ERR(h_file))
13891 +               goto out;
13892 +
13893 +       err = vfsub_write_u(h_file, buf, count, ppos);
13894 +       au_write_post(inode, h_file, &wpre, err);
13895 +
13896 +out:
13897 +       si_read_unlock(inode->i_sb);
13898 +       inode_unlock(inode);
13899 +       return err;
13900 +}
13901 +
13902 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13903 +                         struct iov_iter *iov_iter)
13904 +{
13905 +       ssize_t err;
13906 +       struct file *file;
13907 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13908 +
13909 +       err = security_file_permission(h_file, rw);
13910 +       if (unlikely(err))
13911 +               goto out;
13912 +
13913 +       err = -ENOSYS;
13914 +       iter = NULL;
13915 +       if (rw == MAY_READ)
13916 +               iter = h_file->f_op->read_iter;
13917 +       else if (rw == MAY_WRITE)
13918 +               iter = h_file->f_op->write_iter;
13919 +
13920 +       file = kio->ki_filp;
13921 +       kio->ki_filp = h_file;
13922 +       if (iter) {
13923 +               lockdep_off();
13924 +               err = iter(kio, iov_iter);
13925 +               lockdep_on();
13926 +       } else
13927 +               /* currently there is no such fs */
13928 +               WARN_ON_ONCE(1);
13929 +       kio->ki_filp = file;
13930 +
13931 +out:
13932 +       return err;
13933 +}
13934 +
13935 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13936 +{
13937 +       ssize_t err;
13938 +       struct file *file, *h_file;
13939 +       struct inode *inode;
13940 +       struct super_block *sb;
13941 +
13942 +       file = kio->ki_filp;
13943 +       inode = file_inode(file);
13944 +       sb = inode->i_sb;
13945 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13946 +
13947 +       h_file = au_read_pre(file, /*keep_fi*/1);
13948 +       err = PTR_ERR(h_file);
13949 +       if (IS_ERR(h_file))
13950 +               goto out;
13951 +
13952 +       if (au_test_loopback_kthread()) {
13953 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
13954 +               if (file->f_mapping != h_file->f_mapping) {
13955 +                       file->f_mapping = h_file->f_mapping;
13956 +                       smp_mb(); /* unnecessary? */
13957 +               }
13958 +       }
13959 +       fi_read_unlock(file);
13960 +
13961 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13962 +       /* todo: necessary? */
13963 +       /* file->f_ra = h_file->f_ra; */
13964 +       au_read_post(inode, h_file);
13965 +
13966 +out:
13967 +       si_read_unlock(sb);
13968 +       return err;
13969 +}
13970 +
13971 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13972 +{
13973 +       ssize_t err;
13974 +       struct au_write_pre wpre;
13975 +       struct inode *inode;
13976 +       struct file *file, *h_file;
13977 +
13978 +       file = kio->ki_filp;
13979 +       inode = file_inode(file);
13980 +       au_mtx_and_read_lock(inode);
13981 +
13982 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13983 +       err = PTR_ERR(h_file);
13984 +       if (IS_ERR(h_file))
13985 +               goto out;
13986 +
13987 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13988 +       au_write_post(inode, h_file, &wpre, err);
13989 +
13990 +out:
13991 +       si_read_unlock(inode->i_sb);
13992 +       inode_unlock(inode);
13993 +       return err;
13994 +}
13995 +
13996 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13997 +                               struct pipe_inode_info *pipe, size_t len,
13998 +                               unsigned int flags)
13999 +{
14000 +       ssize_t err;
14001 +       struct file *h_file;
14002 +       struct inode *inode;
14003 +       struct super_block *sb;
14004 +
14005 +       inode = file_inode(file);
14006 +       sb = inode->i_sb;
14007 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14008 +
14009 +       h_file = au_read_pre(file, /*keep_fi*/0);
14010 +       err = PTR_ERR(h_file);
14011 +       if (IS_ERR(h_file))
14012 +               goto out;
14013 +
14014 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
14015 +       /* todo: necessasry? */
14016 +       /* file->f_ra = h_file->f_ra; */
14017 +       au_read_post(inode, h_file);
14018 +
14019 +out:
14020 +       si_read_unlock(sb);
14021 +       return err;
14022 +}
14023 +
14024 +static ssize_t
14025 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
14026 +                 size_t len, unsigned int flags)
14027 +{
14028 +       ssize_t err;
14029 +       struct au_write_pre wpre;
14030 +       struct inode *inode;
14031 +       struct file *h_file;
14032 +
14033 +       inode = file_inode(file);
14034 +       au_mtx_and_read_lock(inode);
14035 +
14036 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14037 +       err = PTR_ERR(h_file);
14038 +       if (IS_ERR(h_file))
14039 +               goto out;
14040 +
14041 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
14042 +       au_write_post(inode, h_file, &wpre, err);
14043 +
14044 +out:
14045 +       si_read_unlock(inode->i_sb);
14046 +       inode_unlock(inode);
14047 +       return err;
14048 +}
14049 +
14050 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
14051 +                          loff_t len)
14052 +{
14053 +       long err;
14054 +       struct au_write_pre wpre;
14055 +       struct inode *inode;
14056 +       struct file *h_file;
14057 +
14058 +       inode = file_inode(file);
14059 +       au_mtx_and_read_lock(inode);
14060 +
14061 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14062 +       err = PTR_ERR(h_file);
14063 +       if (IS_ERR(h_file))
14064 +               goto out;
14065 +
14066 +       lockdep_off();
14067 +       err = vfs_fallocate(h_file, mode, offset, len);
14068 +       lockdep_on();
14069 +       au_write_post(inode, h_file, &wpre, /*written*/1);
14070 +
14071 +out:
14072 +       si_read_unlock(inode->i_sb);
14073 +       inode_unlock(inode);
14074 +       return err;
14075 +}
14076 +
14077 +/* ---------------------------------------------------------------------- */
14078 +
14079 +/*
14080 + * The locking order around current->mmap_sem.
14081 + * - in most and regular cases
14082 + *   file I/O syscall -- aufs_read() or something
14083 + *     -- si_rwsem for read -- mmap_sem
14084 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
14085 + * - in mmap case
14086 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
14087 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
14088 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
14089 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
14090 + * It means that when aufs acquires si_rwsem for write, the process should never
14091 + * acquire mmap_sem.
14092 + *
14093 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
14094 + * problem either since any directory is not able to be mmap-ed.
14095 + * The similar scenario is applied to aufs_readlink() too.
14096 + */
14097 +
14098 +#if 0 /* stop calling security_file_mmap() */
14099 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
14100 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
14101 +
14102 +static unsigned long au_arch_prot_conv(unsigned long flags)
14103 +{
14104 +       /* currently ppc64 only */
14105 +#ifdef CONFIG_PPC64
14106 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
14107 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
14108 +       return AuConv_VM_PROT(flags, SAO);
14109 +#else
14110 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
14111 +       return 0;
14112 +#endif
14113 +}
14114 +
14115 +static unsigned long au_prot_conv(unsigned long flags)
14116 +{
14117 +       return AuConv_VM_PROT(flags, READ)
14118 +               | AuConv_VM_PROT(flags, WRITE)
14119 +               | AuConv_VM_PROT(flags, EXEC)
14120 +               | au_arch_prot_conv(flags);
14121 +}
14122 +
14123 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
14124 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
14125 +
14126 +static unsigned long au_flag_conv(unsigned long flags)
14127 +{
14128 +       return AuConv_VM_MAP(flags, GROWSDOWN)
14129 +               | AuConv_VM_MAP(flags, DENYWRITE)
14130 +               | AuConv_VM_MAP(flags, LOCKED);
14131 +}
14132 +#endif
14133 +
14134 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
14135 +{
14136 +       int err;
14137 +       const unsigned char wlock
14138 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
14139 +       struct super_block *sb;
14140 +       struct file *h_file;
14141 +       struct inode *inode;
14142 +
14143 +       AuDbgVmRegion(file, vma);
14144 +
14145 +       inode = file_inode(file);
14146 +       sb = inode->i_sb;
14147 +       lockdep_off();
14148 +       si_read_lock(sb, AuLock_NOPLMW);
14149 +
14150 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
14151 +       lockdep_on();
14152 +       err = PTR_ERR(h_file);
14153 +       if (IS_ERR(h_file))
14154 +               goto out;
14155 +
14156 +       err = 0;
14157 +       au_set_mmapped(file);
14158 +       au_vm_file_reset(vma, h_file);
14159 +       /*
14160 +        * we cannot call security_mmap_file() here since it may acquire
14161 +        * mmap_sem or i_mutex.
14162 +        *
14163 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
14164 +        *                       au_flag_conv(vma->vm_flags));
14165 +        */
14166 +       if (!err)
14167 +               err = h_file->f_op->mmap(h_file, vma);
14168 +       if (!err) {
14169 +               au_vm_prfile_set(vma, file);
14170 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
14171 +               goto out_fput; /* success */
14172 +       }
14173 +       au_unset_mmapped(file);
14174 +       au_vm_file_reset(vma, file);
14175 +
14176 +out_fput:
14177 +       lockdep_off();
14178 +       ii_write_unlock(inode);
14179 +       lockdep_on();
14180 +       fput(h_file);
14181 +out:
14182 +       lockdep_off();
14183 +       si_read_unlock(sb);
14184 +       lockdep_on();
14185 +       AuTraceErr(err);
14186 +       return err;
14187 +}
14188 +
14189 +/* ---------------------------------------------------------------------- */
14190 +
14191 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
14192 +                            int datasync)
14193 +{
14194 +       int err;
14195 +       struct au_write_pre wpre;
14196 +       struct inode *inode;
14197 +       struct file *h_file;
14198 +
14199 +       err = 0; /* -EBADF; */ /* posix? */
14200 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
14201 +               goto out;
14202 +
14203 +       inode = file_inode(file);
14204 +       au_mtx_and_read_lock(inode);
14205 +
14206 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14207 +       err = PTR_ERR(h_file);
14208 +       if (IS_ERR(h_file))
14209 +               goto out_unlock;
14210 +
14211 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
14212 +       au_write_post(inode, h_file, &wpre, /*written*/0);
14213 +
14214 +out_unlock:
14215 +       si_read_unlock(inode->i_sb);
14216 +       inode_unlock(inode);
14217 +out:
14218 +       return err;
14219 +}
14220 +
14221 +static int aufs_fasync(int fd, struct file *file, int flag)
14222 +{
14223 +       int err;
14224 +       struct file *h_file;
14225 +       struct super_block *sb;
14226 +
14227 +       sb = file->f_path.dentry->d_sb;
14228 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14229 +
14230 +       h_file = au_read_pre(file, /*keep_fi*/0);
14231 +       err = PTR_ERR(h_file);
14232 +       if (IS_ERR(h_file))
14233 +               goto out;
14234 +
14235 +       if (h_file->f_op->fasync)
14236 +               err = h_file->f_op->fasync(fd, h_file, flag);
14237 +       fput(h_file); /* instead of au_read_post() */
14238 +
14239 +out:
14240 +       si_read_unlock(sb);
14241 +       return err;
14242 +}
14243 +
14244 +static int aufs_setfl(struct file *file, unsigned long arg)
14245 +{
14246 +       int err;
14247 +       struct file *h_file;
14248 +       struct super_block *sb;
14249 +
14250 +       sb = file->f_path.dentry->d_sb;
14251 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14252 +
14253 +       h_file = au_read_pre(file, /*keep_fi*/0);
14254 +       err = PTR_ERR(h_file);
14255 +       if (IS_ERR(h_file))
14256 +               goto out;
14257 +
14258 +       arg |= vfsub_file_flags(file) & FASYNC; /* stop calling h_file->fasync */
14259 +       err = setfl(/*unused fd*/-1, h_file, arg);
14260 +       fput(h_file); /* instead of au_read_post() */
14261 +
14262 +out:
14263 +       si_read_unlock(sb);
14264 +       return err;
14265 +}
14266 +
14267 +/* ---------------------------------------------------------------------- */
14268 +
14269 +/* no one supports this operation, currently */
14270 +#if 0
14271 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
14272 +                            size_t len, loff_t *pos, int more)
14273 +{
14274 +}
14275 +#endif
14276 +
14277 +/* ---------------------------------------------------------------------- */
14278 +
14279 +const struct file_operations aufs_file_fop = {
14280 +       .owner          = THIS_MODULE,
14281 +
14282 +       .llseek         = default_llseek,
14283 +
14284 +       .read           = aufs_read,
14285 +       .write          = aufs_write,
14286 +       .read_iter      = aufs_read_iter,
14287 +       .write_iter     = aufs_write_iter,
14288 +
14289 +#ifdef CONFIG_AUFS_POLL
14290 +       .poll           = aufs_poll,
14291 +#endif
14292 +       .unlocked_ioctl = aufs_ioctl_nondir,
14293 +#ifdef CONFIG_COMPAT
14294 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
14295 +#endif
14296 +       .mmap           = aufs_mmap,
14297 +       .open           = aufs_open_nondir,
14298 +       .flush          = aufs_flush_nondir,
14299 +       .release        = aufs_release_nondir,
14300 +       .fsync          = aufs_fsync_nondir,
14301 +       .fasync         = aufs_fasync,
14302 +       /* .sendpage    = aufs_sendpage, */
14303 +       .setfl          = aufs_setfl,
14304 +       .splice_write   = aufs_splice_write,
14305 +       .splice_read    = aufs_splice_read,
14306 +#if 0
14307 +       .aio_splice_write = aufs_aio_splice_write,
14308 +       .aio_splice_read  = aufs_aio_splice_read,
14309 +#endif
14310 +       .fallocate      = aufs_fallocate
14311 +};
14312 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
14313 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
14314 +++ linux/fs/aufs/fstype.h      2017-03-02 14:24:13.860255635 +0100
14315 @@ -0,0 +1,400 @@
14316 +/*
14317 + * Copyright (C) 2005-2017 Junjiro R. Okajima
14318 + *
14319 + * This program, aufs is free software; you can redistribute it and/or modify
14320 + * it under the terms of the GNU General Public License as published by
14321 + * the Free Software Foundation; either version 2 of the License, or
14322 + * (at your option) any later version.
14323 + *
14324 + * This program is distributed in the hope that it will be useful,
14325 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14326 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14327 + * GNU General Public License for more details.
14328 + *
14329 + * You should have received a copy of the GNU General Public License
14330 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14331 + */
14332 +
14333 +/*
14334 + * judging filesystem type
14335 + */
14336 +
14337 +#ifndef __AUFS_FSTYPE_H__
14338 +#define __AUFS_FSTYPE_H__
14339 +
14340 +#ifdef __KERNEL__
14341 +
14342 +#include <linux/fs.h>
14343 +#include <linux/magic.h>
14344 +#include <linux/nfs_fs.h>
14345 +#include <linux/romfs_fs.h>
14346 +
14347 +static inline int au_test_aufs(struct super_block *sb)
14348 +{
14349 +       return sb->s_magic == AUFS_SUPER_MAGIC;
14350 +}
14351 +
14352 +static inline const char *au_sbtype(struct super_block *sb)
14353 +{
14354 +       return sb->s_type->name;
14355 +}
14356 +
14357 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14358 +{
14359 +#if IS_ENABLED(CONFIG_ISO9660_FS)
14360 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
14361 +#else
14362 +       return 0;
14363 +#endif
14364 +}
14365 +
14366 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
14367 +{
14368 +#if IS_ENABLED(CONFIG_ROMFS_FS)
14369 +       return sb->s_magic == ROMFS_MAGIC;
14370 +#else
14371 +       return 0;
14372 +#endif
14373 +}
14374 +
14375 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
14376 +{
14377 +#if IS_ENABLED(CONFIG_CRAMFS)
14378 +       return sb->s_magic == CRAMFS_MAGIC;
14379 +#endif
14380 +       return 0;
14381 +}
14382 +
14383 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14384 +{
14385 +#if IS_ENABLED(CONFIG_NFS_FS)
14386 +       return sb->s_magic == NFS_SUPER_MAGIC;
14387 +#else
14388 +       return 0;
14389 +#endif
14390 +}
14391 +
14392 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
14393 +{
14394 +#if IS_ENABLED(CONFIG_FUSE_FS)
14395 +       return sb->s_magic == FUSE_SUPER_MAGIC;
14396 +#else
14397 +       return 0;
14398 +#endif
14399 +}
14400 +
14401 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
14402 +{
14403 +#if IS_ENABLED(CONFIG_XFS_FS)
14404 +       return sb->s_magic == XFS_SB_MAGIC;
14405 +#else
14406 +       return 0;
14407 +#endif
14408 +}
14409 +
14410 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
14411 +{
14412 +#ifdef CONFIG_TMPFS
14413 +       return sb->s_magic == TMPFS_MAGIC;
14414 +#else
14415 +       return 0;
14416 +#endif
14417 +}
14418 +
14419 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
14420 +{
14421 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
14422 +       return !strcmp(au_sbtype(sb), "ecryptfs");
14423 +#else
14424 +       return 0;
14425 +#endif
14426 +}
14427 +
14428 +static inline int au_test_ramfs(struct super_block *sb)
14429 +{
14430 +       return sb->s_magic == RAMFS_MAGIC;
14431 +}
14432 +
14433 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14434 +{
14435 +#if IS_ENABLED(CONFIG_UBIFS_FS)
14436 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
14437 +#else
14438 +       return 0;
14439 +#endif
14440 +}
14441 +
14442 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14443 +{
14444 +#ifdef CONFIG_PROC_FS
14445 +       return sb->s_magic == PROC_SUPER_MAGIC;
14446 +#else
14447 +       return 0;
14448 +#endif
14449 +}
14450 +
14451 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14452 +{
14453 +#ifdef CONFIG_SYSFS
14454 +       return sb->s_magic == SYSFS_MAGIC;
14455 +#else
14456 +       return 0;
14457 +#endif
14458 +}
14459 +
14460 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14461 +{
14462 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
14463 +       return sb->s_magic == CONFIGFS_MAGIC;
14464 +#else
14465 +       return 0;
14466 +#endif
14467 +}
14468 +
14469 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
14470 +{
14471 +#if IS_ENABLED(CONFIG_MINIX_FS)
14472 +       return sb->s_magic == MINIX3_SUPER_MAGIC
14473 +               || sb->s_magic == MINIX2_SUPER_MAGIC
14474 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
14475 +               || sb->s_magic == MINIX_SUPER_MAGIC
14476 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
14477 +#else
14478 +       return 0;
14479 +#endif
14480 +}
14481 +
14482 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
14483 +{
14484 +#if IS_ENABLED(CONFIG_FAT_FS)
14485 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
14486 +#else
14487 +       return 0;
14488 +#endif
14489 +}
14490 +
14491 +static inline int au_test_msdos(struct super_block *sb)
14492 +{
14493 +       return au_test_fat(sb);
14494 +}
14495 +
14496 +static inline int au_test_vfat(struct super_block *sb)
14497 +{
14498 +       return au_test_fat(sb);
14499 +}
14500 +
14501 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14502 +{
14503 +#ifdef CONFIG_SECURITYFS
14504 +       return sb->s_magic == SECURITYFS_MAGIC;
14505 +#else
14506 +       return 0;
14507 +#endif
14508 +}
14509 +
14510 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14511 +{
14512 +#if IS_ENABLED(CONFIG_SQUASHFS)
14513 +       return sb->s_magic == SQUASHFS_MAGIC;
14514 +#else
14515 +       return 0;
14516 +#endif
14517 +}
14518 +
14519 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14520 +{
14521 +#if IS_ENABLED(CONFIG_BTRFS_FS)
14522 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
14523 +#else
14524 +       return 0;
14525 +#endif
14526 +}
14527 +
14528 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14529 +{
14530 +#if IS_ENABLED(CONFIG_XENFS)
14531 +       return sb->s_magic == XENFS_SUPER_MAGIC;
14532 +#else
14533 +       return 0;
14534 +#endif
14535 +}
14536 +
14537 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14538 +{
14539 +#ifdef CONFIG_DEBUG_FS
14540 +       return sb->s_magic == DEBUGFS_MAGIC;
14541 +#else
14542 +       return 0;
14543 +#endif
14544 +}
14545 +
14546 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14547 +{
14548 +#if IS_ENABLED(CONFIG_NILFS)
14549 +       return sb->s_magic == NILFS_SUPER_MAGIC;
14550 +#else
14551 +       return 0;
14552 +#endif
14553 +}
14554 +
14555 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14556 +{
14557 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
14558 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14559 +#else
14560 +       return 0;
14561 +#endif
14562 +}
14563 +
14564 +/* ---------------------------------------------------------------------- */
14565 +/*
14566 + * they can't be an aufs branch.
14567 + */
14568 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14569 +{
14570 +       return
14571 +#ifndef CONFIG_AUFS_BR_RAMFS
14572 +               au_test_ramfs(sb) ||
14573 +#endif
14574 +               au_test_procfs(sb)
14575 +               || au_test_sysfs(sb)
14576 +               || au_test_configfs(sb)
14577 +               || au_test_debugfs(sb)
14578 +               || au_test_securityfs(sb)
14579 +               || au_test_xenfs(sb)
14580 +               || au_test_ecryptfs(sb)
14581 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14582 +               || au_test_aufs(sb); /* will be supported in next version */
14583 +}
14584 +
14585 +static inline int au_test_fs_remote(struct super_block *sb)
14586 +{
14587 +       return !au_test_tmpfs(sb)
14588 +#ifdef CONFIG_AUFS_BR_RAMFS
14589 +               && !au_test_ramfs(sb)
14590 +#endif
14591 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14592 +}
14593 +
14594 +/* ---------------------------------------------------------------------- */
14595 +
14596 +/*
14597 + * Note: these functions (below) are created after reading ->getattr() in all
14598 + * filesystems under linux/fs. it means we have to do so in every update...
14599 + */
14600 +
14601 +/*
14602 + * some filesystems require getattr to refresh the inode attributes before
14603 + * referencing.
14604 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14605 + * and leave the work for d_revalidate()
14606 + */
14607 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14608 +{
14609 +       return au_test_nfs(sb)
14610 +               || au_test_fuse(sb)
14611 +               /* || au_test_btrfs(sb) */      /* untested */
14612 +               ;
14613 +}
14614 +
14615 +/*
14616 + * filesystems which don't maintain i_size or i_blocks.
14617 + */
14618 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14619 +{
14620 +       return au_test_xfs(sb)
14621 +               || au_test_btrfs(sb)
14622 +               || au_test_ubifs(sb)
14623 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14624 +               /* || au_test_minix(sb) */      /* untested */
14625 +               ;
14626 +}
14627 +
14628 +/*
14629 + * filesystems which don't store the correct value in some of their inode
14630 + * attributes.
14631 + */
14632 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14633 +{
14634 +       return au_test_fs_bad_iattr_size(sb)
14635 +               || au_test_fat(sb)
14636 +               || au_test_msdos(sb)
14637 +               || au_test_vfat(sb);
14638 +}
14639 +
14640 +/* they don't check i_nlink in link(2) */
14641 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14642 +{
14643 +       return au_test_tmpfs(sb)
14644 +#ifdef CONFIG_AUFS_BR_RAMFS
14645 +               || au_test_ramfs(sb)
14646 +#endif
14647 +               || au_test_ubifs(sb)
14648 +               || au_test_hfsplus(sb);
14649 +}
14650 +
14651 +/*
14652 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14653 + */
14654 +static inline int au_test_fs_notime(struct super_block *sb)
14655 +{
14656 +       return au_test_nfs(sb)
14657 +               || au_test_fuse(sb)
14658 +               || au_test_ubifs(sb)
14659 +               ;
14660 +}
14661 +
14662 +/* temporary support for i#1 in cramfs */
14663 +static inline int au_test_fs_unique_ino(struct inode *inode)
14664 +{
14665 +       if (au_test_cramfs(inode->i_sb))
14666 +               return inode->i_ino != 1;
14667 +       return 1;
14668 +}
14669 +
14670 +/* ---------------------------------------------------------------------- */
14671 +
14672 +/*
14673 + * the filesystem where the xino files placed must support i/o after unlink and
14674 + * maintain i_size and i_blocks.
14675 + */
14676 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14677 +{
14678 +       return au_test_fs_remote(sb)
14679 +               || au_test_fs_bad_iattr_size(sb)
14680 +               /* don't want unnecessary work for xino */
14681 +               || au_test_aufs(sb)
14682 +               || au_test_ecryptfs(sb)
14683 +               || au_test_nilfs(sb);
14684 +}
14685 +
14686 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14687 +{
14688 +       return au_test_tmpfs(sb)
14689 +               || au_test_ramfs(sb);
14690 +}
14691 +
14692 +/*
14693 + * test if the @sb is real-readonly.
14694 + */
14695 +static inline int au_test_fs_rr(struct super_block *sb)
14696 +{
14697 +       return au_test_squashfs(sb)
14698 +               || au_test_iso9660(sb)
14699 +               || au_test_cramfs(sb)
14700 +               || au_test_romfs(sb);
14701 +}
14702 +
14703 +/*
14704 + * test if the @inode is nfs with 'noacl' option
14705 + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14706 + */
14707 +static inline int au_test_nfs_noacl(struct inode *inode)
14708 +{
14709 +       return au_test_nfs(inode->i_sb)
14710 +               /* && IS_POSIXACL(inode) */
14711 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
14712 +}
14713 +
14714 +#endif /* __KERNEL__ */
14715 +#endif /* __AUFS_FSTYPE_H__ */
14716 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14717 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14718 +++ linux/fs/aufs/hfsnotify.c   2017-03-02 14:24:13.860255635 +0100
14719 @@ -0,0 +1,287 @@
14720 +/*
14721 + * Copyright (C) 2005-2017 Junjiro R. Okajima
14722 + *
14723 + * This program, aufs is free software; you can redistribute it and/or modify
14724 + * it under the terms of the GNU General Public License as published by
14725 + * the Free Software Foundation; either version 2 of the License, or
14726 + * (at your option) any later version.
14727 + *
14728 + * This program is distributed in the hope that it will be useful,
14729 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14730 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14731 + * GNU General Public License for more details.
14732 + *
14733 + * You should have received a copy of the GNU General Public License
14734 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14735 + */
14736 +
14737 +/*
14738 + * fsnotify for the lower directories
14739 + */
14740 +
14741 +#include "aufs.h"
14742 +
14743 +/* FS_IN_IGNORED is unnecessary */
14744 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14745 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14746 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14747 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14748 +
14749 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14750 +{
14751 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14752 +                                            hn_mark);
14753 +       /* AuDbg("here\n"); */
14754 +       au_cache_dfree_hnotify(hn);
14755 +       smp_mb__before_atomic();
14756 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14757 +               wake_up(&au_hfsn_wq);
14758 +}
14759 +
14760 +static int au_hfsn_alloc(struct au_hinode *hinode)
14761 +{
14762 +       int err;
14763 +       struct au_hnotify *hn;
14764 +       struct super_block *sb;
14765 +       struct au_branch *br;
14766 +       struct fsnotify_mark *mark;
14767 +       aufs_bindex_t bindex;
14768 +
14769 +       hn = hinode->hi_notify;
14770 +       sb = hn->hn_aufs_inode->i_sb;
14771 +       bindex = au_br_index(sb, hinode->hi_id);
14772 +       br = au_sbr(sb, bindex);
14773 +       AuDebugOn(!br->br_hfsn);
14774 +
14775 +       mark = &hn->hn_mark;
14776 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
14777 +       mark->mask = AuHfsnMask;
14778 +       /*
14779 +        * by udba rename or rmdir, aufs assign a new inode to the known
14780 +        * h_inode, so specify 1 to allow dups.
14781 +        */
14782 +       lockdep_off();
14783 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
14784 +                                /*mnt*/NULL, /*allow_dups*/1);
14785 +       lockdep_on();
14786 +
14787 +       return err;
14788 +}
14789 +
14790 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14791 +{
14792 +       struct fsnotify_mark *mark;
14793 +       unsigned long long ull;
14794 +       struct fsnotify_group *group;
14795 +
14796 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14797 +       BUG_ON(!ull);
14798 +
14799 +       mark = &hn->hn_mark;
14800 +       spin_lock(&mark->lock);
14801 +       group = mark->group;
14802 +       fsnotify_get_group(group);
14803 +       spin_unlock(&mark->lock);
14804 +       lockdep_off();
14805 +       fsnotify_destroy_mark(mark, group);
14806 +       fsnotify_put_mark(mark);
14807 +       fsnotify_put_group(group);
14808 +       lockdep_on();
14809 +
14810 +       /* free hn by myself */
14811 +       return 0;
14812 +}
14813 +
14814 +/* ---------------------------------------------------------------------- */
14815 +
14816 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14817 +{
14818 +       struct fsnotify_mark *mark;
14819 +
14820 +       mark = &hinode->hi_notify->hn_mark;
14821 +       spin_lock(&mark->lock);
14822 +       if (do_set) {
14823 +               AuDebugOn(mark->mask & AuHfsnMask);
14824 +               mark->mask |= AuHfsnMask;
14825 +       } else {
14826 +               AuDebugOn(!(mark->mask & AuHfsnMask));
14827 +               mark->mask &= ~AuHfsnMask;
14828 +       }
14829 +       spin_unlock(&mark->lock);
14830 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
14831 +}
14832 +
14833 +/* ---------------------------------------------------------------------- */
14834 +
14835 +/* #define AuDbgHnotify */
14836 +#ifdef AuDbgHnotify
14837 +static char *au_hfsn_name(u32 mask)
14838 +{
14839 +#ifdef CONFIG_AUFS_DEBUG
14840 +#define test_ret(flag)                         \
14841 +       do {                                    \
14842 +               if (mask & flag)                \
14843 +                       return #flag;           \
14844 +       } while (0)
14845 +       test_ret(FS_ACCESS);
14846 +       test_ret(FS_MODIFY);
14847 +       test_ret(FS_ATTRIB);
14848 +       test_ret(FS_CLOSE_WRITE);
14849 +       test_ret(FS_CLOSE_NOWRITE);
14850 +       test_ret(FS_OPEN);
14851 +       test_ret(FS_MOVED_FROM);
14852 +       test_ret(FS_MOVED_TO);
14853 +       test_ret(FS_CREATE);
14854 +       test_ret(FS_DELETE);
14855 +       test_ret(FS_DELETE_SELF);
14856 +       test_ret(FS_MOVE_SELF);
14857 +       test_ret(FS_UNMOUNT);
14858 +       test_ret(FS_Q_OVERFLOW);
14859 +       test_ret(FS_IN_IGNORED);
14860 +       test_ret(FS_ISDIR);
14861 +       test_ret(FS_IN_ONESHOT);
14862 +       test_ret(FS_EVENT_ON_CHILD);
14863 +       return "";
14864 +#undef test_ret
14865 +#else
14866 +       return "??";
14867 +#endif
14868 +}
14869 +#endif
14870 +
14871 +/* ---------------------------------------------------------------------- */
14872 +
14873 +static void au_hfsn_free_group(struct fsnotify_group *group)
14874 +{
14875 +       struct au_br_hfsnotify *hfsn = group->private;
14876 +
14877 +       /* AuDbg("here\n"); */
14878 +       au_delayed_kfree(hfsn);
14879 +}
14880 +
14881 +static int au_hfsn_handle_event(struct fsnotify_group *group,
14882 +                               struct inode *inode,
14883 +                               struct fsnotify_mark *inode_mark,
14884 +                               struct fsnotify_mark *vfsmount_mark,
14885 +                               u32 mask, const void *data, int data_type,
14886 +                               const unsigned char *file_name, u32 cookie)
14887 +{
14888 +       int err;
14889 +       struct au_hnotify *hnotify;
14890 +       struct inode *h_dir, *h_inode;
14891 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
14892 +
14893 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
14894 +
14895 +       err = 0;
14896 +       /* if FS_UNMOUNT happens, there must be another bug */
14897 +       AuDebugOn(mask & FS_UNMOUNT);
14898 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
14899 +               goto out;
14900 +
14901 +       h_dir = inode;
14902 +       h_inode = NULL;
14903 +#ifdef AuDbgHnotify
14904 +       au_debug_on();
14905 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14906 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14907 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14908 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
14909 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14910 +               /* WARN_ON(1); */
14911 +       }
14912 +       au_debug_off();
14913 +#endif
14914 +
14915 +       AuDebugOn(!inode_mark);
14916 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14917 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
14918 +
14919 +out:
14920 +       return err;
14921 +}
14922 +
14923 +static struct fsnotify_ops au_hfsn_ops = {
14924 +       .handle_event           = au_hfsn_handle_event,
14925 +       .free_group_priv        = au_hfsn_free_group
14926 +};
14927 +
14928 +/* ---------------------------------------------------------------------- */
14929 +
14930 +static void au_hfsn_fin_br(struct au_branch *br)
14931 +{
14932 +       struct au_br_hfsnotify *hfsn;
14933 +
14934 +       hfsn = br->br_hfsn;
14935 +       if (hfsn) {
14936 +               lockdep_off();
14937 +               fsnotify_put_group(hfsn->hfsn_group);
14938 +               lockdep_on();
14939 +       }
14940 +}
14941 +
14942 +static int au_hfsn_init_br(struct au_branch *br, int perm)
14943 +{
14944 +       int err;
14945 +       struct fsnotify_group *group;
14946 +       struct au_br_hfsnotify *hfsn;
14947 +
14948 +       err = 0;
14949 +       br->br_hfsn = NULL;
14950 +       if (!au_br_hnotifyable(perm))
14951 +               goto out;
14952 +
14953 +       err = -ENOMEM;
14954 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14955 +       if (unlikely(!hfsn))
14956 +               goto out;
14957 +
14958 +       err = 0;
14959 +       group = fsnotify_alloc_group(&au_hfsn_ops);
14960 +       if (IS_ERR(group)) {
14961 +               err = PTR_ERR(group);
14962 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
14963 +               goto out_hfsn;
14964 +       }
14965 +
14966 +       group->private = hfsn;
14967 +       hfsn->hfsn_group = group;
14968 +       br->br_hfsn = hfsn;
14969 +       goto out; /* success */
14970 +
14971 +out_hfsn:
14972 +       au_delayed_kfree(hfsn);
14973 +out:
14974 +       return err;
14975 +}
14976 +
14977 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14978 +{
14979 +       int err;
14980 +
14981 +       err = 0;
14982 +       if (!br->br_hfsn)
14983 +               err = au_hfsn_init_br(br, perm);
14984 +
14985 +       return err;
14986 +}
14987 +
14988 +/* ---------------------------------------------------------------------- */
14989 +
14990 +static void au_hfsn_fin(void)
14991 +{
14992 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14993 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14994 +}
14995 +
14996 +const struct au_hnotify_op au_hnotify_op = {
14997 +       .ctl            = au_hfsn_ctl,
14998 +       .alloc          = au_hfsn_alloc,
14999 +       .free           = au_hfsn_free,
15000 +
15001 +       .fin            = au_hfsn_fin,
15002 +
15003 +       .reset_br       = au_hfsn_reset_br,
15004 +       .fin_br         = au_hfsn_fin_br,
15005 +       .init_br        = au_hfsn_init_br
15006 +};
15007 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
15008 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
15009 +++ linux/fs/aufs/hfsplus.c     2017-03-02 14:24:13.860255635 +0100
15010 @@ -0,0 +1,56 @@
15011 +/*
15012 + * Copyright (C) 2010-2017 Junjiro R. Okajima
15013 + *
15014 + * This program, aufs is free software; you can redistribute it and/or modify
15015 + * it under the terms of the GNU General Public License as published by
15016 + * the Free Software Foundation; either version 2 of the License, or
15017 + * (at your option) any later version.
15018 + *
15019 + * This program is distributed in the hope that it will be useful,
15020 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15021 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15022 + * GNU General Public License for more details.
15023 + *
15024 + * You should have received a copy of the GNU General Public License
15025 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15026 + */
15027 +
15028 +/*
15029 + * special support for filesystems which aqucires an inode mutex
15030 + * at final closing a file, eg, hfsplus.
15031 + *
15032 + * This trick is very simple and stupid, just to open the file before really
15033 + * neceeary open to tell hfsplus that this is not the final closing.
15034 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
15035 + * and au_h_open_post() after releasing it.
15036 + */
15037 +
15038 +#include "aufs.h"
15039 +
15040 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15041 +                          int force_wr)
15042 +{
15043 +       struct file *h_file;
15044 +       struct dentry *h_dentry;
15045 +
15046 +       h_dentry = au_h_dptr(dentry, bindex);
15047 +       AuDebugOn(!h_dentry);
15048 +       AuDebugOn(d_is_negative(h_dentry));
15049 +
15050 +       h_file = NULL;
15051 +       if (au_test_hfsplus(h_dentry->d_sb)
15052 +           && d_is_reg(h_dentry))
15053 +               h_file = au_h_open(dentry, bindex,
15054 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
15055 +                                  /*file*/NULL, force_wr);
15056 +       return h_file;
15057 +}
15058 +
15059 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15060 +                   struct file *h_file)
15061 +{
15062 +       if (h_file) {
15063 +               fput(h_file);
15064 +               au_sbr_put(dentry->d_sb, bindex);
15065 +       }
15066 +}
15067 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
15068 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
15069 +++ linux/fs/aufs/hnotify.c     2017-03-02 14:24:13.860255635 +0100
15070 @@ -0,0 +1,723 @@
15071 +/*
15072 + * Copyright (C) 2005-2017 Junjiro R. Okajima
15073 + *
15074 + * This program, aufs is free software; you can redistribute it and/or modify
15075 + * it under the terms of the GNU General Public License as published by
15076 + * the Free Software Foundation; either version 2 of the License, or
15077 + * (at your option) any later version.
15078 + *
15079 + * This program is distributed in the hope that it will be useful,
15080 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15081 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15082 + * GNU General Public License for more details.
15083 + *
15084 + * You should have received a copy of the GNU General Public License
15085 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15086 + */
15087 +
15088 +/*
15089 + * abstraction to notify the direct changes on lower directories
15090 + */
15091 +
15092 +#include "aufs.h"
15093 +
15094 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
15095 +{
15096 +       int err;
15097 +       struct au_hnotify *hn;
15098 +
15099 +       err = -ENOMEM;
15100 +       hn = au_cache_alloc_hnotify();
15101 +       if (hn) {
15102 +               hn->hn_aufs_inode = inode;
15103 +               hinode->hi_notify = hn;
15104 +               err = au_hnotify_op.alloc(hinode);
15105 +               AuTraceErr(err);
15106 +               if (unlikely(err)) {
15107 +                       hinode->hi_notify = NULL;
15108 +                       au_cache_dfree_hnotify(hn);
15109 +                       /*
15110 +                        * The upper dir was removed by udba, but the same named
15111 +                        * dir left. In this case, aufs assignes a new inode
15112 +                        * number and set the monitor again.
15113 +                        * For the lower dir, the old monitnor is still left.
15114 +                        */
15115 +                       if (err == -EEXIST)
15116 +                               err = 0;
15117 +               }
15118 +       }
15119 +
15120 +       AuTraceErr(err);
15121 +       return err;
15122 +}
15123 +
15124 +void au_hn_free(struct au_hinode *hinode)
15125 +{
15126 +       struct au_hnotify *hn;
15127 +
15128 +       hn = hinode->hi_notify;
15129 +       if (hn) {
15130 +               hinode->hi_notify = NULL;
15131 +               if (au_hnotify_op.free(hinode, hn))
15132 +                       au_cache_dfree_hnotify(hn);
15133 +       }
15134 +}
15135 +
15136 +/* ---------------------------------------------------------------------- */
15137 +
15138 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
15139 +{
15140 +       if (hinode->hi_notify)
15141 +               au_hnotify_op.ctl(hinode, do_set);
15142 +}
15143 +
15144 +void au_hn_reset(struct inode *inode, unsigned int flags)
15145 +{
15146 +       aufs_bindex_t bindex, bbot;
15147 +       struct inode *hi;
15148 +       struct dentry *iwhdentry;
15149 +
15150 +       bbot = au_ibbot(inode);
15151 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
15152 +               hi = au_h_iptr(inode, bindex);
15153 +               if (!hi)
15154 +                       continue;
15155 +
15156 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
15157 +               iwhdentry = au_hi_wh(inode, bindex);
15158 +               if (iwhdentry)
15159 +                       dget(iwhdentry);
15160 +               au_igrab(hi);
15161 +               au_set_h_iptr(inode, bindex, NULL, 0);
15162 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
15163 +                             flags & ~AuHi_XINO);
15164 +               iput(hi);
15165 +               dput(iwhdentry);
15166 +               /* inode_unlock(hi); */
15167 +       }
15168 +}
15169 +
15170 +/* ---------------------------------------------------------------------- */
15171 +
15172 +static int hn_xino(struct inode *inode, struct inode *h_inode)
15173 +{
15174 +       int err;
15175 +       aufs_bindex_t bindex, bbot, bfound, btop;
15176 +       struct inode *h_i;
15177 +
15178 +       err = 0;
15179 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15180 +               pr_warn("branch root dir was changed\n");
15181 +               goto out;
15182 +       }
15183 +
15184 +       bfound = -1;
15185 +       bbot = au_ibbot(inode);
15186 +       btop = au_ibtop(inode);
15187 +#if 0 /* reserved for future use */
15188 +       if (bindex == bbot) {
15189 +               /* keep this ino in rename case */
15190 +               goto out;
15191 +       }
15192 +#endif
15193 +       for (bindex = btop; bindex <= bbot; bindex++)
15194 +               if (au_h_iptr(inode, bindex) == h_inode) {
15195 +                       bfound = bindex;
15196 +                       break;
15197 +               }
15198 +       if (bfound < 0)
15199 +               goto out;
15200 +
15201 +       for (bindex = btop; bindex <= bbot; bindex++) {
15202 +               h_i = au_h_iptr(inode, bindex);
15203 +               if (!h_i)
15204 +                       continue;
15205 +
15206 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
15207 +               /* ignore this error */
15208 +               /* bad action? */
15209 +       }
15210 +
15211 +       /* children inode number will be broken */
15212 +
15213 +out:
15214 +       AuTraceErr(err);
15215 +       return err;
15216 +}
15217 +
15218 +static int hn_gen_tree(struct dentry *dentry)
15219 +{
15220 +       int err, i, j, ndentry;
15221 +       struct au_dcsub_pages dpages;
15222 +       struct au_dpage *dpage;
15223 +       struct dentry **dentries;
15224 +
15225 +       err = au_dpages_init(&dpages, GFP_NOFS);
15226 +       if (unlikely(err))
15227 +               goto out;
15228 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
15229 +       if (unlikely(err))
15230 +               goto out_dpages;
15231 +
15232 +       for (i = 0; i < dpages.ndpage; i++) {
15233 +               dpage = dpages.dpages + i;
15234 +               dentries = dpage->dentries;
15235 +               ndentry = dpage->ndentry;
15236 +               for (j = 0; j < ndentry; j++) {
15237 +                       struct dentry *d;
15238 +
15239 +                       d = dentries[j];
15240 +                       if (IS_ROOT(d))
15241 +                               continue;
15242 +
15243 +                       au_digen_dec(d);
15244 +                       if (d_really_is_positive(d))
15245 +                               /* todo: reset children xino?
15246 +                                  cached children only? */
15247 +                               au_iigen_dec(d_inode(d));
15248 +               }
15249 +       }
15250 +
15251 +out_dpages:
15252 +       au_dpages_free(&dpages);
15253 +
15254 +#if 0
15255 +       /* discard children */
15256 +       dentry_unhash(dentry);
15257 +       dput(dentry);
15258 +#endif
15259 +out:
15260 +       return err;
15261 +}
15262 +
15263 +/*
15264 + * return 0 if processed.
15265 + */
15266 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
15267 +                          const unsigned int isdir)
15268 +{
15269 +       int err;
15270 +       struct dentry *d;
15271 +       struct qstr *dname;
15272 +
15273 +       err = 1;
15274 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15275 +               pr_warn("branch root dir was changed\n");
15276 +               err = 0;
15277 +               goto out;
15278 +       }
15279 +
15280 +       if (!isdir) {
15281 +               AuDebugOn(!name);
15282 +               au_iigen_dec(inode);
15283 +               spin_lock(&inode->i_lock);
15284 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
15285 +                       spin_lock(&d->d_lock);
15286 +                       dname = &d->d_name;
15287 +                       if (dname->len != nlen
15288 +                           && memcmp(dname->name, name, nlen)) {
15289 +                               spin_unlock(&d->d_lock);
15290 +                               continue;
15291 +                       }
15292 +                       err = 0;
15293 +                       au_digen_dec(d);
15294 +                       spin_unlock(&d->d_lock);
15295 +                       break;
15296 +               }
15297 +               spin_unlock(&inode->i_lock);
15298 +       } else {
15299 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
15300 +               d = d_find_any_alias(inode);
15301 +               if (!d) {
15302 +                       au_iigen_dec(inode);
15303 +                       goto out;
15304 +               }
15305 +
15306 +               spin_lock(&d->d_lock);
15307 +               dname = &d->d_name;
15308 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
15309 +                       spin_unlock(&d->d_lock);
15310 +                       err = hn_gen_tree(d);
15311 +                       spin_lock(&d->d_lock);
15312 +               }
15313 +               spin_unlock(&d->d_lock);
15314 +               dput(d);
15315 +       }
15316 +
15317 +out:
15318 +       AuTraceErr(err);
15319 +       return err;
15320 +}
15321 +
15322 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
15323 +{
15324 +       int err;
15325 +
15326 +       if (IS_ROOT(dentry)) {
15327 +               pr_warn("branch root dir was changed\n");
15328 +               return 0;
15329 +       }
15330 +
15331 +       err = 0;
15332 +       if (!isdir) {
15333 +               au_digen_dec(dentry);
15334 +               if (d_really_is_positive(dentry))
15335 +                       au_iigen_dec(d_inode(dentry));
15336 +       } else {
15337 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
15338 +               if (d_really_is_positive(dentry))
15339 +                       err = hn_gen_tree(dentry);
15340 +       }
15341 +
15342 +       AuTraceErr(err);
15343 +       return err;
15344 +}
15345 +
15346 +/* ---------------------------------------------------------------------- */
15347 +
15348 +/* hnotify job flags */
15349 +#define AuHnJob_XINO0          1
15350 +#define AuHnJob_GEN            (1 << 1)
15351 +#define AuHnJob_DIRENT         (1 << 2)
15352 +#define AuHnJob_ISDIR          (1 << 3)
15353 +#define AuHnJob_TRYXINO0       (1 << 4)
15354 +#define AuHnJob_MNTPNT         (1 << 5)
15355 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
15356 +#define au_fset_hnjob(flags, name) \
15357 +       do { (flags) |= AuHnJob_##name; } while (0)
15358 +#define au_fclr_hnjob(flags, name) \
15359 +       do { (flags) &= ~AuHnJob_##name; } while (0)
15360 +
15361 +enum {
15362 +       AuHn_CHILD,
15363 +       AuHn_PARENT,
15364 +       AuHnLast
15365 +};
15366 +
15367 +struct au_hnotify_args {
15368 +       struct inode *h_dir, *dir, *h_child_inode;
15369 +       u32 mask;
15370 +       unsigned int flags[AuHnLast];
15371 +       unsigned int h_child_nlen;
15372 +       char h_child_name[];
15373 +};
15374 +
15375 +struct hn_job_args {
15376 +       unsigned int flags;
15377 +       struct inode *inode, *h_inode, *dir, *h_dir;
15378 +       struct dentry *dentry;
15379 +       char *h_name;
15380 +       int h_nlen;
15381 +};
15382 +
15383 +static int hn_job(struct hn_job_args *a)
15384 +{
15385 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
15386 +       int e;
15387 +
15388 +       /* reset xino */
15389 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15390 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
15391 +
15392 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
15393 +           && a->inode
15394 +           && a->h_inode) {
15395 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
15396 +               if (!a->h_inode->i_nlink
15397 +                   && !(a->h_inode->i_state & I_LINKABLE))
15398 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
15399 +               inode_unlock(a->h_inode);
15400 +       }
15401 +
15402 +       /* make the generation obsolete */
15403 +       if (au_ftest_hnjob(a->flags, GEN)) {
15404 +               e = -1;
15405 +               if (a->inode)
15406 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
15407 +                                             isdir);
15408 +               if (e && a->dentry)
15409 +                       hn_gen_by_name(a->dentry, isdir);
15410 +               /* ignore this error */
15411 +       }
15412 +
15413 +       /* make dir entries obsolete */
15414 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15415 +               struct au_vdir *vdir;
15416 +
15417 +               vdir = au_ivdir(a->inode);
15418 +               if (vdir)
15419 +                       vdir->vd_jiffy = 0;
15420 +               /* IMustLock(a->inode); */
15421 +               /* a->inode->i_version++; */
15422 +       }
15423 +
15424 +       /* can do nothing but warn */
15425 +       if (au_ftest_hnjob(a->flags, MNTPNT)
15426 +           && a->dentry
15427 +           && d_mountpoint(a->dentry))
15428 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
15429 +
15430 +       return 0;
15431 +}
15432 +
15433 +/* ---------------------------------------------------------------------- */
15434 +
15435 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15436 +                                          struct inode *dir)
15437 +{
15438 +       struct dentry *dentry, *d, *parent;
15439 +       struct qstr *dname;
15440 +
15441 +       parent = d_find_any_alias(dir);
15442 +       if (!parent)
15443 +               return NULL;
15444 +
15445 +       dentry = NULL;
15446 +       spin_lock(&parent->d_lock);
15447 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
15448 +               /* AuDbg("%pd\n", d); */
15449 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
15450 +               dname = &d->d_name;
15451 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
15452 +                       goto cont_unlock;
15453 +               if (au_di(d))
15454 +                       au_digen_dec(d);
15455 +               else
15456 +                       goto cont_unlock;
15457 +               if (au_dcount(d) > 0) {
15458 +                       dentry = dget_dlock(d);
15459 +                       spin_unlock(&d->d_lock);
15460 +                       break;
15461 +               }
15462 +
15463 +cont_unlock:
15464 +               spin_unlock(&d->d_lock);
15465 +       }
15466 +       spin_unlock(&parent->d_lock);
15467 +       dput(parent);
15468 +
15469 +       if (dentry)
15470 +               di_write_lock_child(dentry);
15471 +
15472 +       return dentry;
15473 +}
15474 +
15475 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15476 +                                        aufs_bindex_t bindex, ino_t h_ino)
15477 +{
15478 +       struct inode *inode;
15479 +       ino_t ino;
15480 +       int err;
15481 +
15482 +       inode = NULL;
15483 +       err = au_xino_read(sb, bindex, h_ino, &ino);
15484 +       if (!err && ino)
15485 +               inode = ilookup(sb, ino);
15486 +       if (!inode)
15487 +               goto out;
15488 +
15489 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15490 +               pr_warn("wrong root branch\n");
15491 +               iput(inode);
15492 +               inode = NULL;
15493 +               goto out;
15494 +       }
15495 +
15496 +       ii_write_lock_child(inode);
15497 +
15498 +out:
15499 +       return inode;
15500 +}
15501 +
15502 +static void au_hn_bh(void *_args)
15503 +{
15504 +       struct au_hnotify_args *a = _args;
15505 +       struct super_block *sb;
15506 +       aufs_bindex_t bindex, bbot, bfound;
15507 +       unsigned char xino, try_iput;
15508 +       int err;
15509 +       struct inode *inode;
15510 +       ino_t h_ino;
15511 +       struct hn_job_args args;
15512 +       struct dentry *dentry;
15513 +       struct au_sbinfo *sbinfo;
15514 +
15515 +       AuDebugOn(!_args);
15516 +       AuDebugOn(!a->h_dir);
15517 +       AuDebugOn(!a->dir);
15518 +       AuDebugOn(!a->mask);
15519 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15520 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
15521 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
15522 +
15523 +       inode = NULL;
15524 +       dentry = NULL;
15525 +       /*
15526 +        * do not lock a->dir->i_mutex here
15527 +        * because of d_revalidate() may cause a deadlock.
15528 +        */
15529 +       sb = a->dir->i_sb;
15530 +       AuDebugOn(!sb);
15531 +       sbinfo = au_sbi(sb);
15532 +       AuDebugOn(!sbinfo);
15533 +       si_write_lock(sb, AuLock_NOPLMW);
15534 +
15535 +       ii_read_lock_parent(a->dir);
15536 +       bfound = -1;
15537 +       bbot = au_ibbot(a->dir);
15538 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
15539 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15540 +                       bfound = bindex;
15541 +                       break;
15542 +               }
15543 +       ii_read_unlock(a->dir);
15544 +       if (unlikely(bfound < 0))
15545 +               goto out;
15546 +
15547 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
15548 +       h_ino = 0;
15549 +       if (a->h_child_inode)
15550 +               h_ino = a->h_child_inode->i_ino;
15551 +
15552 +       if (a->h_child_nlen
15553 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15554 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15555 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15556 +                                             a->dir);
15557 +       try_iput = 0;
15558 +       if (dentry && d_really_is_positive(dentry))
15559 +               inode = d_inode(dentry);
15560 +       if (xino && !inode && h_ino
15561 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15562 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15563 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15564 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15565 +               try_iput = 1;
15566 +       }
15567 +
15568 +       args.flags = a->flags[AuHn_CHILD];
15569 +       args.dentry = dentry;
15570 +       args.inode = inode;
15571 +       args.h_inode = a->h_child_inode;
15572 +       args.dir = a->dir;
15573 +       args.h_dir = a->h_dir;
15574 +       args.h_name = a->h_child_name;
15575 +       args.h_nlen = a->h_child_nlen;
15576 +       err = hn_job(&args);
15577 +       if (dentry) {
15578 +               if (au_di(dentry))
15579 +                       di_write_unlock(dentry);
15580 +               dput(dentry);
15581 +       }
15582 +       if (inode && try_iput) {
15583 +               ii_write_unlock(inode);
15584 +               iput(inode);
15585 +       }
15586 +
15587 +       ii_write_lock_parent(a->dir);
15588 +       args.flags = a->flags[AuHn_PARENT];
15589 +       args.dentry = NULL;
15590 +       args.inode = a->dir;
15591 +       args.h_inode = a->h_dir;
15592 +       args.dir = NULL;
15593 +       args.h_dir = NULL;
15594 +       args.h_name = NULL;
15595 +       args.h_nlen = 0;
15596 +       err = hn_job(&args);
15597 +       ii_write_unlock(a->dir);
15598 +
15599 +out:
15600 +       iput(a->h_child_inode);
15601 +       iput(a->h_dir);
15602 +       iput(a->dir);
15603 +       si_write_unlock(sb);
15604 +       au_nwt_done(&sbinfo->si_nowait);
15605 +       au_delayed_kfree(a);
15606 +}
15607 +
15608 +/* ---------------------------------------------------------------------- */
15609 +
15610 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15611 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15612 +{
15613 +       int err, len;
15614 +       unsigned int flags[AuHnLast], f;
15615 +       unsigned char isdir, isroot, wh;
15616 +       struct inode *dir;
15617 +       struct au_hnotify_args *args;
15618 +       char *p, *h_child_name;
15619 +
15620 +       err = 0;
15621 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15622 +       dir = igrab(hnotify->hn_aufs_inode);
15623 +       if (!dir)
15624 +               goto out;
15625 +
15626 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15627 +       wh = 0;
15628 +       h_child_name = (void *)h_child_qstr->name;
15629 +       len = h_child_qstr->len;
15630 +       if (h_child_name) {
15631 +               if (len > AUFS_WH_PFX_LEN
15632 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15633 +                       h_child_name += AUFS_WH_PFX_LEN;
15634 +                       len -= AUFS_WH_PFX_LEN;
15635 +                       wh = 1;
15636 +               }
15637 +       }
15638 +
15639 +       isdir = 0;
15640 +       if (h_child_inode)
15641 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15642 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15643 +       flags[AuHn_CHILD] = 0;
15644 +       if (isdir)
15645 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15646 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15647 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15648 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15649 +       case FS_MOVED_FROM:
15650 +       case FS_MOVED_TO:
15651 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15652 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15653 +               /*FALLTHROUGH*/
15654 +       case FS_CREATE:
15655 +               AuDebugOn(!h_child_name);
15656 +               break;
15657 +
15658 +       case FS_DELETE:
15659 +               /*
15660 +                * aufs never be able to get this child inode.
15661 +                * revalidation should be in d_revalidate()
15662 +                * by checking i_nlink, i_generation or d_unhashed().
15663 +                */
15664 +               AuDebugOn(!h_child_name);
15665 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15666 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15667 +               break;
15668 +
15669 +       default:
15670 +               AuDebugOn(1);
15671 +       }
15672 +
15673 +       if (wh)
15674 +               h_child_inode = NULL;
15675 +
15676 +       err = -ENOMEM;
15677 +       /* iput() and kfree() will be called in au_hnotify() */
15678 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15679 +       if (unlikely(!args)) {
15680 +               AuErr1("no memory\n");
15681 +               iput(dir);
15682 +               goto out;
15683 +       }
15684 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15685 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15686 +       args->mask = mask;
15687 +       args->dir = dir;
15688 +       args->h_dir = igrab(h_dir);
15689 +       if (h_child_inode)
15690 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15691 +       args->h_child_inode = h_child_inode;
15692 +       args->h_child_nlen = len;
15693 +       if (len) {
15694 +               p = (void *)args;
15695 +               p += sizeof(*args);
15696 +               memcpy(p, h_child_name, len);
15697 +               p[len] = 0;
15698 +       }
15699 +
15700 +       /* NFS fires the event for silly-renamed one from kworker */
15701 +       f = 0;
15702 +       if (!dir->i_nlink
15703 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15704 +               f = AuWkq_NEST;
15705 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15706 +       if (unlikely(err)) {
15707 +               pr_err("wkq %d\n", err);
15708 +               iput(args->h_child_inode);
15709 +               iput(args->h_dir);
15710 +               iput(args->dir);
15711 +               au_delayed_kfree(args);
15712 +       }
15713 +
15714 +out:
15715 +       return err;
15716 +}
15717 +
15718 +/* ---------------------------------------------------------------------- */
15719 +
15720 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15721 +{
15722 +       int err;
15723 +
15724 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15725 +
15726 +       err = 0;
15727 +       if (au_hnotify_op.reset_br)
15728 +               err = au_hnotify_op.reset_br(udba, br, perm);
15729 +
15730 +       return err;
15731 +}
15732 +
15733 +int au_hnotify_init_br(struct au_branch *br, int perm)
15734 +{
15735 +       int err;
15736 +
15737 +       err = 0;
15738 +       if (au_hnotify_op.init_br)
15739 +               err = au_hnotify_op.init_br(br, perm);
15740 +
15741 +       return err;
15742 +}
15743 +
15744 +void au_hnotify_fin_br(struct au_branch *br)
15745 +{
15746 +       if (au_hnotify_op.fin_br)
15747 +               au_hnotify_op.fin_br(br);
15748 +}
15749 +
15750 +static void au_hn_destroy_cache(void)
15751 +{
15752 +       struct au_cache *cp;
15753 +
15754 +       flush_delayed_work(&au_dfree.dwork);
15755 +       cp = au_dfree.cache + AuCache_HNOTIFY;
15756 +       AuDebugOn(!llist_empty(&cp->llist));
15757 +       kmem_cache_destroy(cp->cache);
15758 +       cp->cache = NULL;
15759 +}
15760 +
15761 +AU_CACHE_DFREE_FUNC(hnotify, HNOTIFY, hn_lnode);
15762 +
15763 +int __init au_hnotify_init(void)
15764 +{
15765 +       int err;
15766 +       struct au_cache *cp;
15767 +
15768 +       err = -ENOMEM;
15769 +       cp = au_dfree.cache + AuCache_HNOTIFY;
15770 +       cp->cache = AuCache(au_hnotify);
15771 +       if (cp->cache) {
15772 +               err = 0;
15773 +               if (au_hnotify_op.init)
15774 +                       err = au_hnotify_op.init();
15775 +               if (unlikely(err))
15776 +                       au_hn_destroy_cache();
15777 +       }
15778 +       AuTraceErr(err);
15779 +       return err;
15780 +}
15781 +
15782 +void au_hnotify_fin(void)
15783 +{
15784 +       struct au_cache *cp;
15785 +
15786 +       if (au_hnotify_op.fin)
15787 +               au_hnotify_op.fin();
15788 +
15789 +       /* cf. au_cache_fin() */
15790 +       cp = au_dfree.cache + AuCache_HNOTIFY;
15791 +       if (cp->cache)
15792 +               au_hn_destroy_cache();
15793 +}
15794 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15795 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15796 +++ linux/fs/aufs/iinfo.c       2017-03-02 14:24:13.863589061 +0100
15797 @@ -0,0 +1,285 @@
15798 +/*
15799 + * Copyright (C) 2005-2017 Junjiro R. Okajima
15800 + *
15801 + * This program, aufs is free software; you can redistribute it and/or modify
15802 + * it under the terms of the GNU General Public License as published by
15803 + * the Free Software Foundation; either version 2 of the License, or
15804 + * (at your option) any later version.
15805 + *
15806 + * This program is distributed in the hope that it will be useful,
15807 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15808 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15809 + * GNU General Public License for more details.
15810 + *
15811 + * You should have received a copy of the GNU General Public License
15812 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15813 + */
15814 +
15815 +/*
15816 + * inode private data
15817 + */
15818 +
15819 +#include "aufs.h"
15820 +
15821 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15822 +{
15823 +       struct inode *h_inode;
15824 +       struct au_hinode *hinode;
15825 +
15826 +       IiMustAnyLock(inode);
15827 +
15828 +       hinode = au_hinode(au_ii(inode), bindex);
15829 +       h_inode = hinode->hi_inode;
15830 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15831 +       return h_inode;
15832 +}
15833 +
15834 +/* todo: hard/soft set? */
15835 +void au_hiput(struct au_hinode *hinode)
15836 +{
15837 +       au_hn_free(hinode);
15838 +       dput(hinode->hi_whdentry);
15839 +       iput(hinode->hi_inode);
15840 +}
15841 +
15842 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15843 +{
15844 +       unsigned int flags;
15845 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15846 +
15847 +       flags = 0;
15848 +       if (au_opt_test(mnt_flags, XINO))
15849 +               au_fset_hi(flags, XINO);
15850 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15851 +               au_fset_hi(flags, HNOTIFY);
15852 +       return flags;
15853 +}
15854 +
15855 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15856 +                  struct inode *h_inode, unsigned int flags)
15857 +{
15858 +       struct au_hinode *hinode;
15859 +       struct inode *hi;
15860 +       struct au_iinfo *iinfo = au_ii(inode);
15861 +
15862 +       IiMustWriteLock(inode);
15863 +
15864 +       hinode = au_hinode(iinfo, bindex);
15865 +       hi = hinode->hi_inode;
15866 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15867 +
15868 +       if (hi)
15869 +               au_hiput(hinode);
15870 +       hinode->hi_inode = h_inode;
15871 +       if (h_inode) {
15872 +               int err;
15873 +               struct super_block *sb = inode->i_sb;
15874 +               struct au_branch *br;
15875 +
15876 +               AuDebugOn(inode->i_mode
15877 +                         && (h_inode->i_mode & S_IFMT)
15878 +                         != (inode->i_mode & S_IFMT));
15879 +               if (bindex == iinfo->ii_btop)
15880 +                       au_cpup_igen(inode, h_inode);
15881 +               br = au_sbr(sb, bindex);
15882 +               hinode->hi_id = br->br_id;
15883 +               if (au_ftest_hi(flags, XINO)) {
15884 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15885 +                                           inode->i_ino);
15886 +                       if (unlikely(err))
15887 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15888 +               }
15889 +
15890 +               if (au_ftest_hi(flags, HNOTIFY)
15891 +                   && au_br_hnotifyable(br->br_perm)) {
15892 +                       err = au_hn_alloc(hinode, inode);
15893 +                       if (unlikely(err))
15894 +                               AuIOErr1("au_hn_alloc() %d\n", err);
15895 +               }
15896 +       }
15897 +}
15898 +
15899 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15900 +                 struct dentry *h_wh)
15901 +{
15902 +       struct au_hinode *hinode;
15903 +
15904 +       IiMustWriteLock(inode);
15905 +
15906 +       hinode = au_hinode(au_ii(inode), bindex);
15907 +       AuDebugOn(hinode->hi_whdentry);
15908 +       hinode->hi_whdentry = h_wh;
15909 +}
15910 +
15911 +void au_update_iigen(struct inode *inode, int half)
15912 +{
15913 +       struct au_iinfo *iinfo;
15914 +       struct au_iigen *iigen;
15915 +       unsigned int sigen;
15916 +
15917 +       sigen = au_sigen(inode->i_sb);
15918 +       iinfo = au_ii(inode);
15919 +       iigen = &iinfo->ii_generation;
15920 +       spin_lock(&iigen->ig_spin);
15921 +       iigen->ig_generation = sigen;
15922 +       if (half)
15923 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15924 +       else
15925 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
15926 +       spin_unlock(&iigen->ig_spin);
15927 +}
15928 +
15929 +/* it may be called at remount time, too */
15930 +void au_update_ibrange(struct inode *inode, int do_put_zero)
15931 +{
15932 +       struct au_iinfo *iinfo;
15933 +       aufs_bindex_t bindex, bbot;
15934 +
15935 +       AuDebugOn(au_is_bad_inode(inode));
15936 +       IiMustWriteLock(inode);
15937 +
15938 +       iinfo = au_ii(inode);
15939 +       if (do_put_zero && iinfo->ii_btop >= 0) {
15940 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
15941 +                    bindex++) {
15942 +                       struct inode *h_i;
15943 +
15944 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
15945 +                       if (h_i
15946 +                           && !h_i->i_nlink
15947 +                           && !(h_i->i_state & I_LINKABLE))
15948 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15949 +               }
15950 +       }
15951 +
15952 +       iinfo->ii_btop = -1;
15953 +       iinfo->ii_bbot = -1;
15954 +       bbot = au_sbbot(inode->i_sb);
15955 +       for (bindex = 0; bindex <= bbot; bindex++)
15956 +               if (au_hinode(iinfo, bindex)->hi_inode) {
15957 +                       iinfo->ii_btop = bindex;
15958 +                       break;
15959 +               }
15960 +       if (iinfo->ii_btop >= 0)
15961 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
15962 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
15963 +                               iinfo->ii_bbot = bindex;
15964 +                               break;
15965 +                       }
15966 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
15967 +}
15968 +
15969 +/* ---------------------------------------------------------------------- */
15970 +
15971 +void au_icntnr_init_once(void *_c)
15972 +{
15973 +       struct au_icntnr *c = _c;
15974 +       struct au_iinfo *iinfo = &c->iinfo;
15975 +
15976 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
15977 +       au_rw_init(&iinfo->ii_rwsem);
15978 +       inode_init_once(&c->vfs_inode);
15979 +}
15980 +
15981 +void au_hinode_init(struct au_hinode *hinode)
15982 +{
15983 +       hinode->hi_inode = NULL;
15984 +       hinode->hi_id = -1;
15985 +       au_hn_init(hinode);
15986 +       hinode->hi_whdentry = NULL;
15987 +}
15988 +
15989 +int au_iinfo_init(struct inode *inode)
15990 +{
15991 +       struct au_iinfo *iinfo;
15992 +       struct super_block *sb;
15993 +       struct au_hinode *hi;
15994 +       int nbr, i;
15995 +
15996 +       sb = inode->i_sb;
15997 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15998 +       nbr = au_sbbot(sb) + 1;
15999 +       if (unlikely(nbr <= 0))
16000 +               nbr = 1;
16001 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
16002 +       if (hi) {
16003 +               au_ninodes_inc(sb);
16004 +
16005 +               iinfo->ii_hinode = hi;
16006 +               for (i = 0; i < nbr; i++, hi++)
16007 +                       au_hinode_init(hi);
16008 +
16009 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
16010 +               iinfo->ii_btop = -1;
16011 +               iinfo->ii_bbot = -1;
16012 +               iinfo->ii_vdir = NULL;
16013 +               return 0;
16014 +       }
16015 +       return -ENOMEM;
16016 +}
16017 +
16018 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
16019 +{
16020 +       int err, i;
16021 +       struct au_hinode *hip;
16022 +
16023 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
16024 +
16025 +       err = -ENOMEM;
16026 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
16027 +                         may_shrink);
16028 +       if (hip) {
16029 +               iinfo->ii_hinode = hip;
16030 +               i = iinfo->ii_bbot + 1;
16031 +               hip += i;
16032 +               for (; i < nbr; i++, hip++)
16033 +                       au_hinode_init(hip);
16034 +               err = 0;
16035 +       }
16036 +
16037 +       return err;
16038 +}
16039 +
16040 +void au_iinfo_fin(struct inode *inode)
16041 +{
16042 +       struct au_iinfo *iinfo;
16043 +       struct au_hinode *hi;
16044 +       struct super_block *sb;
16045 +       aufs_bindex_t bindex, bbot;
16046 +       const unsigned char unlinked = !inode->i_nlink;
16047 +
16048 +       AuDebugOn(au_is_bad_inode(inode));
16049 +
16050 +       sb = inode->i_sb;
16051 +       au_ninodes_dec(sb);
16052 +       if (si_pid_test(sb))
16053 +               au_xino_delete_inode(inode, unlinked);
16054 +       else {
16055 +               /*
16056 +                * it is safe to hide the dependency between sbinfo and
16057 +                * sb->s_umount.
16058 +                */
16059 +               lockdep_off();
16060 +               si_noflush_read_lock(sb);
16061 +               au_xino_delete_inode(inode, unlinked);
16062 +               si_read_unlock(sb);
16063 +               lockdep_on();
16064 +       }
16065 +
16066 +       iinfo = au_ii(inode);
16067 +       if (iinfo->ii_vdir)
16068 +               au_vdir_free(iinfo->ii_vdir, /*atonce*/0);
16069 +
16070 +       bindex = iinfo->ii_btop;
16071 +       if (bindex >= 0) {
16072 +               hi = au_hinode(iinfo, bindex);
16073 +               bbot = iinfo->ii_bbot;
16074 +               while (bindex++ <= bbot) {
16075 +                       if (hi->hi_inode)
16076 +                               au_hiput(hi);
16077 +                       hi++;
16078 +               }
16079 +       }
16080 +       au_delayed_kfree(iinfo->ii_hinode);
16081 +       AuRwDestroy(&iinfo->ii_rwsem);
16082 +}
16083 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
16084 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
16085 +++ linux/fs/aufs/inode.c       2017-03-02 14:24:13.863589061 +0100
16086 @@ -0,0 +1,519 @@
16087 +/*
16088 + * Copyright (C) 2005-2017 Junjiro R. Okajima
16089 + *
16090 + * This program, aufs is free software; you can redistribute it and/or modify
16091 + * it under the terms of the GNU General Public License as published by
16092 + * the Free Software Foundation; either version 2 of the License, or
16093 + * (at your option) any later version.
16094 + *
16095 + * This program is distributed in the hope that it will be useful,
16096 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16097 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16098 + * GNU General Public License for more details.
16099 + *
16100 + * You should have received a copy of the GNU General Public License
16101 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16102 + */
16103 +
16104 +/*
16105 + * inode functions
16106 + */
16107 +
16108 +#include "aufs.h"
16109 +
16110 +struct inode *au_igrab(struct inode *inode)
16111 +{
16112 +       if (inode) {
16113 +               AuDebugOn(!atomic_read(&inode->i_count));
16114 +               ihold(inode);
16115 +       }
16116 +       return inode;
16117 +}
16118 +
16119 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
16120 +{
16121 +       au_cpup_attr_all(inode, /*force*/0);
16122 +       au_update_iigen(inode, /*half*/1);
16123 +       if (do_version)
16124 +               inode->i_version++;
16125 +}
16126 +
16127 +static int au_ii_refresh(struct inode *inode, int *update)
16128 +{
16129 +       int err, e, nbr;
16130 +       umode_t type;
16131 +       aufs_bindex_t bindex, new_bindex;
16132 +       struct super_block *sb;
16133 +       struct au_iinfo *iinfo;
16134 +       struct au_hinode *p, *q, tmp;
16135 +
16136 +       AuDebugOn(au_is_bad_inode(inode));
16137 +       IiMustWriteLock(inode);
16138 +
16139 +       *update = 0;
16140 +       sb = inode->i_sb;
16141 +       nbr = au_sbbot(sb) + 1;
16142 +       type = inode->i_mode & S_IFMT;
16143 +       iinfo = au_ii(inode);
16144 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
16145 +       if (unlikely(err))
16146 +               goto out;
16147 +
16148 +       AuDebugOn(iinfo->ii_btop < 0);
16149 +       p = au_hinode(iinfo, iinfo->ii_btop);
16150 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
16151 +            bindex++, p++) {
16152 +               if (!p->hi_inode)
16153 +                       continue;
16154 +
16155 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
16156 +               new_bindex = au_br_index(sb, p->hi_id);
16157 +               if (new_bindex == bindex)
16158 +                       continue;
16159 +
16160 +               if (new_bindex < 0) {
16161 +                       *update = 1;
16162 +                       au_hiput(p);
16163 +                       p->hi_inode = NULL;
16164 +                       continue;
16165 +               }
16166 +
16167 +               if (new_bindex < iinfo->ii_btop)
16168 +                       iinfo->ii_btop = new_bindex;
16169 +               if (iinfo->ii_bbot < new_bindex)
16170 +                       iinfo->ii_bbot = new_bindex;
16171 +               /* swap two lower inode, and loop again */
16172 +               q = au_hinode(iinfo, new_bindex);
16173 +               tmp = *q;
16174 +               *q = *p;
16175 +               *p = tmp;
16176 +               if (tmp.hi_inode) {
16177 +                       bindex--;
16178 +                       p--;
16179 +               }
16180 +       }
16181 +       au_update_ibrange(inode, /*do_put_zero*/0);
16182 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
16183 +       e = au_dy_irefresh(inode);
16184 +       if (unlikely(e && !err))
16185 +               err = e;
16186 +
16187 +out:
16188 +       AuTraceErr(err);
16189 +       return err;
16190 +}
16191 +
16192 +void au_refresh_iop(struct inode *inode, int force_getattr)
16193 +{
16194 +       int type;
16195 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
16196 +       const struct inode_operations *iop
16197 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
16198 +
16199 +       if (inode->i_op == iop)
16200 +               return;
16201 +
16202 +       switch (inode->i_mode & S_IFMT) {
16203 +       case S_IFDIR:
16204 +               type = AuIop_DIR;
16205 +               break;
16206 +       case S_IFLNK:
16207 +               type = AuIop_SYMLINK;
16208 +               break;
16209 +       default:
16210 +               type = AuIop_OTHER;
16211 +               break;
16212 +       }
16213 +
16214 +       inode->i_op = iop + type;
16215 +       /* unnecessary smp_wmb() */
16216 +}
16217 +
16218 +int au_refresh_hinode_self(struct inode *inode)
16219 +{
16220 +       int err, update;
16221 +
16222 +       err = au_ii_refresh(inode, &update);
16223 +       if (!err)
16224 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
16225 +
16226 +       AuTraceErr(err);
16227 +       return err;
16228 +}
16229 +
16230 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
16231 +{
16232 +       int err, e, update;
16233 +       unsigned int flags;
16234 +       umode_t mode;
16235 +       aufs_bindex_t bindex, bbot;
16236 +       unsigned char isdir;
16237 +       struct au_hinode *p;
16238 +       struct au_iinfo *iinfo;
16239 +
16240 +       err = au_ii_refresh(inode, &update);
16241 +       if (unlikely(err))
16242 +               goto out;
16243 +
16244 +       update = 0;
16245 +       iinfo = au_ii(inode);
16246 +       p = au_hinode(iinfo, iinfo->ii_btop);
16247 +       mode = (inode->i_mode & S_IFMT);
16248 +       isdir = S_ISDIR(mode);
16249 +       flags = au_hi_flags(inode, isdir);
16250 +       bbot = au_dbbot(dentry);
16251 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
16252 +               struct inode *h_i, *h_inode;
16253 +               struct dentry *h_d;
16254 +
16255 +               h_d = au_h_dptr(dentry, bindex);
16256 +               if (!h_d || d_is_negative(h_d))
16257 +                       continue;
16258 +
16259 +               h_inode = d_inode(h_d);
16260 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
16261 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
16262 +                       h_i = au_h_iptr(inode, bindex);
16263 +                       if (h_i) {
16264 +                               if (h_i == h_inode)
16265 +                                       continue;
16266 +                               err = -EIO;
16267 +                               break;
16268 +                       }
16269 +               }
16270 +               if (bindex < iinfo->ii_btop)
16271 +                       iinfo->ii_btop = bindex;
16272 +               if (iinfo->ii_bbot < bindex)
16273 +                       iinfo->ii_bbot = bindex;
16274 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
16275 +               update = 1;
16276 +       }
16277 +       au_update_ibrange(inode, /*do_put_zero*/0);
16278 +       e = au_dy_irefresh(inode);
16279 +       if (unlikely(e && !err))
16280 +               err = e;
16281 +       if (!err)
16282 +               au_refresh_hinode_attr(inode, update && isdir);
16283 +
16284 +out:
16285 +       AuTraceErr(err);
16286 +       return err;
16287 +}
16288 +
16289 +static int set_inode(struct inode *inode, struct dentry *dentry)
16290 +{
16291 +       int err;
16292 +       unsigned int flags;
16293 +       umode_t mode;
16294 +       aufs_bindex_t bindex, btop, btail;
16295 +       unsigned char isdir;
16296 +       struct dentry *h_dentry;
16297 +       struct inode *h_inode;
16298 +       struct au_iinfo *iinfo;
16299 +       struct inode_operations *iop;
16300 +
16301 +       IiMustWriteLock(inode);
16302 +
16303 +       err = 0;
16304 +       isdir = 0;
16305 +       iop = au_sbi(inode->i_sb)->si_iop_array;
16306 +       btop = au_dbtop(dentry);
16307 +       h_dentry = au_h_dptr(dentry, btop);
16308 +       h_inode = d_inode(h_dentry);
16309 +       mode = h_inode->i_mode;
16310 +       switch (mode & S_IFMT) {
16311 +       case S_IFREG:
16312 +               btail = au_dbtail(dentry);
16313 +               inode->i_op = iop + AuIop_OTHER;
16314 +               inode->i_fop = &aufs_file_fop;
16315 +               err = au_dy_iaop(inode, btop, h_inode);
16316 +               if (unlikely(err))
16317 +                       goto out;
16318 +               break;
16319 +       case S_IFDIR:
16320 +               isdir = 1;
16321 +               btail = au_dbtaildir(dentry);
16322 +               inode->i_op = iop + AuIop_DIR;
16323 +               inode->i_fop = &aufs_dir_fop;
16324 +               break;
16325 +       case S_IFLNK:
16326 +               btail = au_dbtail(dentry);
16327 +               inode->i_op = iop + AuIop_SYMLINK;
16328 +               break;
16329 +       case S_IFBLK:
16330 +       case S_IFCHR:
16331 +       case S_IFIFO:
16332 +       case S_IFSOCK:
16333 +               btail = au_dbtail(dentry);
16334 +               inode->i_op = iop + AuIop_OTHER;
16335 +               init_special_inode(inode, mode, h_inode->i_rdev);
16336 +               break;
16337 +       default:
16338 +               AuIOErr("Unknown file type 0%o\n", mode);
16339 +               err = -EIO;
16340 +               goto out;
16341 +       }
16342 +
16343 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
16344 +       flags = au_hi_flags(inode, isdir);
16345 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
16346 +           && au_ftest_hi(flags, HNOTIFY)
16347 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
16348 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
16349 +               au_fclr_hi(flags, HNOTIFY);
16350 +       iinfo = au_ii(inode);
16351 +       iinfo->ii_btop = btop;
16352 +       iinfo->ii_bbot = btail;
16353 +       for (bindex = btop; bindex <= btail; bindex++) {
16354 +               h_dentry = au_h_dptr(dentry, bindex);
16355 +               if (h_dentry)
16356 +                       au_set_h_iptr(inode, bindex,
16357 +                                     au_igrab(d_inode(h_dentry)), flags);
16358 +       }
16359 +       au_cpup_attr_all(inode, /*force*/1);
16360 +       /*
16361 +        * to force calling aufs_get_acl() every time,
16362 +        * do not call cache_no_acl() for aufs inode.
16363 +        */
16364 +
16365 +out:
16366 +       return err;
16367 +}
16368 +
16369 +/*
16370 + * successful returns with iinfo write_locked
16371 + * minus: errno
16372 + * zero: success, matched
16373 + * plus: no error, but unmatched
16374 + */
16375 +static int reval_inode(struct inode *inode, struct dentry *dentry)
16376 +{
16377 +       int err;
16378 +       unsigned int gen, igflags;
16379 +       aufs_bindex_t bindex, bbot;
16380 +       struct inode *h_inode, *h_dinode;
16381 +       struct dentry *h_dentry;
16382 +
16383 +       /*
16384 +        * before this function, if aufs got any iinfo lock, it must be only
16385 +        * one, the parent dir.
16386 +        * it can happen by UDBA and the obsoleted inode number.
16387 +        */
16388 +       err = -EIO;
16389 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
16390 +               goto out;
16391 +
16392 +       err = 1;
16393 +       ii_write_lock_new_child(inode);
16394 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
16395 +       h_dinode = d_inode(h_dentry);
16396 +       bbot = au_ibbot(inode);
16397 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
16398 +               h_inode = au_h_iptr(inode, bindex);
16399 +               if (!h_inode || h_inode != h_dinode)
16400 +                       continue;
16401 +
16402 +               err = 0;
16403 +               gen = au_iigen(inode, &igflags);
16404 +               if (gen == au_digen(dentry)
16405 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
16406 +                       break;
16407 +
16408 +               /* fully refresh inode using dentry */
16409 +               err = au_refresh_hinode(inode, dentry);
16410 +               if (!err)
16411 +                       au_update_iigen(inode, /*half*/0);
16412 +               break;
16413 +       }
16414 +
16415 +       if (unlikely(err))
16416 +               ii_write_unlock(inode);
16417 +out:
16418 +       return err;
16419 +}
16420 +
16421 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16422 +          unsigned int d_type, ino_t *ino)
16423 +{
16424 +       int err;
16425 +       struct mutex *mtx;
16426 +
16427 +       /* prevent hardlinked inode number from race condition */
16428 +       mtx = NULL;
16429 +       if (d_type != DT_DIR) {
16430 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16431 +               mutex_lock(mtx);
16432 +       }
16433 +       err = au_xino_read(sb, bindex, h_ino, ino);
16434 +       if (unlikely(err))
16435 +               goto out;
16436 +
16437 +       if (!*ino) {
16438 +               err = -EIO;
16439 +               *ino = au_xino_new_ino(sb);
16440 +               if (unlikely(!*ino))
16441 +                       goto out;
16442 +               err = au_xino_write(sb, bindex, h_ino, *ino);
16443 +               if (unlikely(err))
16444 +                       goto out;
16445 +       }
16446 +
16447 +out:
16448 +       if (mtx)
16449 +               mutex_unlock(mtx);
16450 +       return err;
16451 +}
16452 +
16453 +/* successful returns with iinfo write_locked */
16454 +/* todo: return with unlocked? */
16455 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
16456 +{
16457 +       struct inode *inode, *h_inode;
16458 +       struct dentry *h_dentry;
16459 +       struct super_block *sb;
16460 +       struct mutex *mtx;
16461 +       ino_t h_ino, ino;
16462 +       int err;
16463 +       aufs_bindex_t btop;
16464 +
16465 +       sb = dentry->d_sb;
16466 +       btop = au_dbtop(dentry);
16467 +       h_dentry = au_h_dptr(dentry, btop);
16468 +       h_inode = d_inode(h_dentry);
16469 +       h_ino = h_inode->i_ino;
16470 +
16471 +       /*
16472 +        * stop 'race'-ing between hardlinks under different
16473 +        * parents.
16474 +        */
16475 +       mtx = NULL;
16476 +       if (!d_is_dir(h_dentry))
16477 +               mtx = &au_sbr(sb, btop)->br_xino.xi_nondir_mtx;
16478 +
16479 +new_ino:
16480 +       if (mtx)
16481 +               mutex_lock(mtx);
16482 +       err = au_xino_read(sb, btop, h_ino, &ino);
16483 +       inode = ERR_PTR(err);
16484 +       if (unlikely(err))
16485 +               goto out;
16486 +
16487 +       if (!ino) {
16488 +               ino = au_xino_new_ino(sb);
16489 +               if (unlikely(!ino)) {
16490 +                       inode = ERR_PTR(-EIO);
16491 +                       goto out;
16492 +               }
16493 +       }
16494 +
16495 +       AuDbg("i%lu\n", (unsigned long)ino);
16496 +       inode = au_iget_locked(sb, ino);
16497 +       err = PTR_ERR(inode);
16498 +       if (IS_ERR(inode))
16499 +               goto out;
16500 +
16501 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16502 +       if (inode->i_state & I_NEW) {
16503 +               ii_write_lock_new_child(inode);
16504 +               err = set_inode(inode, dentry);
16505 +               if (!err) {
16506 +                       unlock_new_inode(inode);
16507 +                       goto out; /* success */
16508 +               }
16509 +
16510 +               /*
16511 +                * iget_failed() calls iput(), but we need to call
16512 +                * ii_write_unlock() after iget_failed(). so dirty hack for
16513 +                * i_count.
16514 +                */
16515 +               atomic_inc(&inode->i_count);
16516 +               iget_failed(inode);
16517 +               ii_write_unlock(inode);
16518 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
16519 +               /* ignore this error */
16520 +               goto out_iput;
16521 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
16522 +               /*
16523 +                * horrible race condition between lookup, readdir and copyup
16524 +                * (or something).
16525 +                */
16526 +               if (mtx)
16527 +                       mutex_unlock(mtx);
16528 +               err = reval_inode(inode, dentry);
16529 +               if (unlikely(err < 0)) {
16530 +                       mtx = NULL;
16531 +                       goto out_iput;
16532 +               }
16533 +
16534 +               if (!err) {
16535 +                       mtx = NULL;
16536 +                       goto out; /* success */
16537 +               } else if (mtx)
16538 +                       mutex_lock(mtx);
16539 +       }
16540 +
16541 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
16542 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
16543 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
16544 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
16545 +                       (unsigned long)h_ino, (unsigned long)ino);
16546 +       ino = 0;
16547 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
16548 +       if (!err) {
16549 +               iput(inode);
16550 +               if (mtx)
16551 +                       mutex_unlock(mtx);
16552 +               goto new_ino;
16553 +       }
16554 +
16555 +out_iput:
16556 +       iput(inode);
16557 +       inode = ERR_PTR(err);
16558 +out:
16559 +       if (mtx)
16560 +               mutex_unlock(mtx);
16561 +       return inode;
16562 +}
16563 +
16564 +/* ---------------------------------------------------------------------- */
16565 +
16566 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16567 +              struct inode *inode)
16568 +{
16569 +       int err;
16570 +       struct inode *hi;
16571 +
16572 +       err = au_br_rdonly(au_sbr(sb, bindex));
16573 +
16574 +       /* pseudo-link after flushed may happen out of bounds */
16575 +       if (!err
16576 +           && inode
16577 +           && au_ibtop(inode) <= bindex
16578 +           && bindex <= au_ibbot(inode)) {
16579 +               /*
16580 +                * permission check is unnecessary since vfsub routine
16581 +                * will be called later
16582 +                */
16583 +               hi = au_h_iptr(inode, bindex);
16584 +               if (hi)
16585 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
16586 +       }
16587 +
16588 +       return err;
16589 +}
16590 +
16591 +int au_test_h_perm(struct inode *h_inode, int mask)
16592 +{
16593 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
16594 +               return 0;
16595 +       return inode_permission(h_inode, mask);
16596 +}
16597 +
16598 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
16599 +{
16600 +       if (au_test_nfs(h_inode->i_sb)
16601 +           && (mask & MAY_WRITE)
16602 +           && S_ISDIR(h_inode->i_mode))
16603 +               mask |= MAY_READ; /* force permission check */
16604 +       return au_test_h_perm(h_inode, mask);
16605 +}
16606 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16607 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
16608 +++ linux/fs/aufs/inode.h       2017-03-02 14:24:13.863589061 +0100
16609 @@ -0,0 +1,692 @@
16610 +/*
16611 + * Copyright (C) 2005-2017 Junjiro R. Okajima
16612 + *
16613 + * This program, aufs is free software; you can redistribute it and/or modify
16614 + * it under the terms of the GNU General Public License as published by
16615 + * the Free Software Foundation; either version 2 of the License, or
16616 + * (at your option) any later version.
16617 + *
16618 + * This program is distributed in the hope that it will be useful,
16619 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16620 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16621 + * GNU General Public License for more details.
16622 + *
16623 + * You should have received a copy of the GNU General Public License
16624 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16625 + */
16626 +
16627 +/*
16628 + * inode operations
16629 + */
16630 +
16631 +#ifndef __AUFS_INODE_H__
16632 +#define __AUFS_INODE_H__
16633 +
16634 +#ifdef __KERNEL__
16635 +
16636 +#include <linux/fsnotify.h>
16637 +#include "rwsem.h"
16638 +
16639 +struct vfsmount;
16640 +
16641 +struct au_hnotify {
16642 +#ifdef CONFIG_AUFS_HNOTIFY
16643 +#ifdef CONFIG_AUFS_HFSNOTIFY
16644 +       /* never use fsnotify_add_vfsmount_mark() */
16645 +       struct fsnotify_mark            hn_mark;
16646 +#endif
16647 +       union {
16648 +               struct inode            *hn_aufs_inode; /* no get/put */
16649 +               struct llist_node       hn_lnode;       /* delayed free */
16650 +       };
16651 +#endif
16652 +} ____cacheline_aligned_in_smp;
16653 +
16654 +struct au_hinode {
16655 +       struct inode            *hi_inode;
16656 +       aufs_bindex_t           hi_id;
16657 +#ifdef CONFIG_AUFS_HNOTIFY
16658 +       struct au_hnotify       *hi_notify;
16659 +#endif
16660 +
16661 +       /* reference to the copied-up whiteout with get/put */
16662 +       struct dentry           *hi_whdentry;
16663 +};
16664 +
16665 +/* ig_flags */
16666 +#define AuIG_HALF_REFRESHED            1
16667 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16668 +#define au_ig_fset(flags, name) \
16669 +       do { (flags) |= AuIG_##name; } while (0)
16670 +#define au_ig_fclr(flags, name) \
16671 +       do { (flags) &= ~AuIG_##name; } while (0)
16672 +
16673 +struct au_iigen {
16674 +       spinlock_t      ig_spin;
16675 +       __u32           ig_generation, ig_flags;
16676 +};
16677 +
16678 +struct au_vdir;
16679 +struct au_iinfo {
16680 +       struct au_iigen         ii_generation;
16681 +       struct super_block      *ii_hsb1;       /* no get/put */
16682 +
16683 +       struct au_rwsem         ii_rwsem;
16684 +       aufs_bindex_t           ii_btop, ii_bbot;
16685 +       __u32                   ii_higen;
16686 +       struct au_hinode        *ii_hinode;
16687 +       struct au_vdir          *ii_vdir;
16688 +};
16689 +
16690 +struct au_icntnr {
16691 +       struct au_iinfo iinfo;
16692 +       struct inode vfs_inode;
16693 +       union {
16694 +               struct hlist_node       plink;
16695 +               struct llist_node       lnode;  /* delayed free */
16696 +       };
16697 +} ____cacheline_aligned_in_smp;
16698 +
16699 +/* au_pin flags */
16700 +#define AuPin_DI_LOCKED                1
16701 +#define AuPin_MNT_WRITE                (1 << 1)
16702 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16703 +#define au_fset_pin(flags, name) \
16704 +       do { (flags) |= AuPin_##name; } while (0)
16705 +#define au_fclr_pin(flags, name) \
16706 +       do { (flags) &= ~AuPin_##name; } while (0)
16707 +
16708 +struct au_pin {
16709 +       /* input */
16710 +       struct dentry *dentry;
16711 +       unsigned int udba;
16712 +       unsigned char lsc_di, lsc_hi, flags;
16713 +       aufs_bindex_t bindex;
16714 +
16715 +       /* output */
16716 +       struct dentry *parent;
16717 +       struct au_hinode *hdir;
16718 +       struct vfsmount *h_mnt;
16719 +
16720 +       /* temporary unlock/relock for copyup */
16721 +       struct dentry *h_dentry, *h_parent;
16722 +       struct au_branch *br;
16723 +       struct task_struct *task;
16724 +};
16725 +
16726 +void au_pin_hdir_unlock(struct au_pin *p);
16727 +int au_pin_hdir_lock(struct au_pin *p);
16728 +int au_pin_hdir_relock(struct au_pin *p);
16729 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16730 +void au_pin_hdir_release(struct au_pin *p);
16731 +
16732 +/* ---------------------------------------------------------------------- */
16733 +
16734 +static inline struct au_iinfo *au_ii(struct inode *inode)
16735 +{
16736 +       BUG_ON(is_bad_inode(inode));
16737 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16738 +}
16739 +
16740 +/* ---------------------------------------------------------------------- */
16741 +
16742 +/* inode.c */
16743 +struct inode *au_igrab(struct inode *inode);
16744 +void au_refresh_iop(struct inode *inode, int force_getattr);
16745 +int au_refresh_hinode_self(struct inode *inode);
16746 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16747 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16748 +          unsigned int d_type, ino_t *ino);
16749 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16750 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16751 +              struct inode *inode);
16752 +int au_test_h_perm(struct inode *h_inode, int mask);
16753 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16754 +
16755 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16756 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16757 +{
16758 +#ifdef CONFIG_AUFS_SHWH
16759 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16760 +#else
16761 +       return 0;
16762 +#endif
16763 +}
16764 +
16765 +/* i_op.c */
16766 +enum {
16767 +       AuIop_SYMLINK,
16768 +       AuIop_DIR,
16769 +       AuIop_OTHER,
16770 +       AuIop_Last
16771 +};
16772 +extern struct inode_operations aufs_iop[AuIop_Last],
16773 +       aufs_iop_nogetattr[AuIop_Last];
16774 +
16775 +/* au_wr_dir flags */
16776 +#define AuWrDir_ADD_ENTRY      1
16777 +#define AuWrDir_ISDIR          (1 << 1)
16778 +#define AuWrDir_TMPFILE                (1 << 2)
16779 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16780 +#define au_fset_wrdir(flags, name) \
16781 +       do { (flags) |= AuWrDir_##name; } while (0)
16782 +#define au_fclr_wrdir(flags, name) \
16783 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16784 +
16785 +struct au_wr_dir_args {
16786 +       aufs_bindex_t force_btgt;
16787 +       unsigned char flags;
16788 +};
16789 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16790 +             struct au_wr_dir_args *args);
16791 +
16792 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16793 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16794 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16795 +                unsigned int udba, unsigned char flags);
16796 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16797 +          unsigned int udba, unsigned char flags) __must_check;
16798 +int au_do_pin(struct au_pin *pin) __must_check;
16799 +void au_unpin(struct au_pin *pin);
16800 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16801 +
16802 +#define AuIcpup_DID_CPUP       1
16803 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16804 +#define au_fset_icpup(flags, name) \
16805 +       do { (flags) |= AuIcpup_##name; } while (0)
16806 +#define au_fclr_icpup(flags, name) \
16807 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16808 +
16809 +struct au_icpup_args {
16810 +       unsigned char flags;
16811 +       unsigned char pin_flags;
16812 +       aufs_bindex_t btgt;
16813 +       unsigned int udba;
16814 +       struct au_pin pin;
16815 +       struct path h_path;
16816 +       struct inode *h_inode;
16817 +};
16818 +
16819 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16820 +                    struct au_icpup_args *a);
16821 +
16822 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
16823 +                     int locked);
16824 +
16825 +/* i_op_add.c */
16826 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16827 +              struct dentry *h_parent, int isdir);
16828 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16829 +              dev_t dev);
16830 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
16831 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16832 +               bool want_excl);
16833 +struct vfsub_aopen_args;
16834 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16835 +                      struct vfsub_aopen_args *args);
16836 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
16837 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16838 +             struct dentry *dentry);
16839 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
16840 +
16841 +/* i_op_del.c */
16842 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16843 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16844 +              struct dentry *h_parent, int isdir);
16845 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
16846 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
16847 +
16848 +/* i_op_ren.c */
16849 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16850 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16851 +               struct inode *dir, struct dentry *dentry,
16852 +               unsigned int flags);
16853 +
16854 +/* iinfo.c */
16855 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16856 +void au_hiput(struct au_hinode *hinode);
16857 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16858 +                 struct dentry *h_wh);
16859 +unsigned int au_hi_flags(struct inode *inode, int isdir);
16860 +
16861 +/* hinode flags */
16862 +#define AuHi_XINO      1
16863 +#define AuHi_HNOTIFY   (1 << 1)
16864 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
16865 +#define au_fset_hi(flags, name) \
16866 +       do { (flags) |= AuHi_##name; } while (0)
16867 +#define au_fclr_hi(flags, name) \
16868 +       do { (flags) &= ~AuHi_##name; } while (0)
16869 +
16870 +#ifndef CONFIG_AUFS_HNOTIFY
16871 +#undef AuHi_HNOTIFY
16872 +#define AuHi_HNOTIFY   0
16873 +#endif
16874 +
16875 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16876 +                  struct inode *h_inode, unsigned int flags);
16877 +
16878 +void au_update_iigen(struct inode *inode, int half);
16879 +void au_update_ibrange(struct inode *inode, int do_put_zero);
16880 +
16881 +void au_icntnr_init_once(void *_c);
16882 +void au_hinode_init(struct au_hinode *hinode);
16883 +int au_iinfo_init(struct inode *inode);
16884 +void au_iinfo_fin(struct inode *inode);
16885 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
16886 +
16887 +#ifdef CONFIG_PROC_FS
16888 +/* plink.c */
16889 +int au_plink_maint(struct super_block *sb, int flags);
16890 +struct au_sbinfo;
16891 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16892 +int au_plink_maint_enter(struct super_block *sb);
16893 +#ifdef CONFIG_AUFS_DEBUG
16894 +void au_plink_list(struct super_block *sb);
16895 +#else
16896 +AuStubVoid(au_plink_list, struct super_block *sb)
16897 +#endif
16898 +int au_plink_test(struct inode *inode);
16899 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16900 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16901 +                    struct dentry *h_dentry);
16902 +void au_plink_put(struct super_block *sb, int verbose);
16903 +void au_plink_clean(struct super_block *sb, int verbose);
16904 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
16905 +#else
16906 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16907 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16908 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16909 +AuStubVoid(au_plink_list, struct super_block *sb);
16910 +AuStubInt0(au_plink_test, struct inode *inode);
16911 +AuStub(struct dentry *, au_plink_lkup, return NULL,
16912 +       struct inode *inode, aufs_bindex_t bindex);
16913 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16914 +          struct dentry *h_dentry);
16915 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16916 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16917 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16918 +#endif /* CONFIG_PROC_FS */
16919 +
16920 +#ifdef CONFIG_AUFS_XATTR
16921 +/* xattr.c */
16922 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16923 +                 unsigned int verbose);
16924 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
16925 +void au_xattr_init(struct super_block *sb);
16926 +#else
16927 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
16928 +          int ignore_flags, unsigned int verbose);
16929 +AuStubVoid(au_xattr_init, struct super_block *sb);
16930 +#endif
16931 +
16932 +#ifdef CONFIG_FS_POSIX_ACL
16933 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16934 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16935 +#endif
16936 +
16937 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16938 +enum {
16939 +       AU_XATTR_SET,
16940 +       AU_ACL_SET
16941 +};
16942 +
16943 +struct au_sxattr {
16944 +       int type;
16945 +       union {
16946 +               struct {
16947 +                       const char      *name;
16948 +                       const void      *value;
16949 +                       size_t          size;
16950 +                       int             flags;
16951 +               } set;
16952 +               struct {
16953 +                       struct posix_acl *acl;
16954 +                       int             type;
16955 +               } acl_set;
16956 +       } u;
16957 +};
16958 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
16959 +                 struct au_sxattr *arg);
16960 +#endif
16961 +
16962 +/* ---------------------------------------------------------------------- */
16963 +
16964 +/* lock subclass for iinfo */
16965 +enum {
16966 +       AuLsc_II_CHILD,         /* child first */
16967 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
16968 +       AuLsc_II_CHILD3,        /* copyup dirs */
16969 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
16970 +       AuLsc_II_PARENT2,
16971 +       AuLsc_II_PARENT3,       /* copyup dirs */
16972 +       AuLsc_II_NEW_CHILD
16973 +};
16974 +
16975 +/*
16976 + * ii_read_lock_child, ii_write_lock_child,
16977 + * ii_read_lock_child2, ii_write_lock_child2,
16978 + * ii_read_lock_child3, ii_write_lock_child3,
16979 + * ii_read_lock_parent, ii_write_lock_parent,
16980 + * ii_read_lock_parent2, ii_write_lock_parent2,
16981 + * ii_read_lock_parent3, ii_write_lock_parent3,
16982 + * ii_read_lock_new_child, ii_write_lock_new_child,
16983 + */
16984 +#define AuReadLockFunc(name, lsc) \
16985 +static inline void ii_read_lock_##name(struct inode *i) \
16986 +{ \
16987 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16988 +}
16989 +
16990 +#define AuWriteLockFunc(name, lsc) \
16991 +static inline void ii_write_lock_##name(struct inode *i) \
16992 +{ \
16993 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16994 +}
16995 +
16996 +#define AuRWLockFuncs(name, lsc) \
16997 +       AuReadLockFunc(name, lsc) \
16998 +       AuWriteLockFunc(name, lsc)
16999 +
17000 +AuRWLockFuncs(child, CHILD);
17001 +AuRWLockFuncs(child2, CHILD2);
17002 +AuRWLockFuncs(child3, CHILD3);
17003 +AuRWLockFuncs(parent, PARENT);
17004 +AuRWLockFuncs(parent2, PARENT2);
17005 +AuRWLockFuncs(parent3, PARENT3);
17006 +AuRWLockFuncs(new_child, NEW_CHILD);
17007 +
17008 +#undef AuReadLockFunc
17009 +#undef AuWriteLockFunc
17010 +#undef AuRWLockFuncs
17011 +
17012 +/*
17013 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
17014 + */
17015 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
17016 +
17017 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
17018 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
17019 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
17020 +
17021 +/* ---------------------------------------------------------------------- */
17022 +
17023 +static inline void au_icntnr_init(struct au_icntnr *c)
17024 +{
17025 +#ifdef CONFIG_AUFS_DEBUG
17026 +       c->vfs_inode.i_mode = 0;
17027 +#endif
17028 +}
17029 +
17030 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
17031 +{
17032 +       unsigned int gen;
17033 +       struct au_iinfo *iinfo;
17034 +       struct au_iigen *iigen;
17035 +
17036 +       iinfo = au_ii(inode);
17037 +       iigen = &iinfo->ii_generation;
17038 +       spin_lock(&iigen->ig_spin);
17039 +       if (igflags)
17040 +               *igflags = iigen->ig_flags;
17041 +       gen = iigen->ig_generation;
17042 +       spin_unlock(&iigen->ig_spin);
17043 +
17044 +       return gen;
17045 +}
17046 +
17047 +/* tiny test for inode number */
17048 +/* tmpfs generation is too rough */
17049 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
17050 +{
17051 +       struct au_iinfo *iinfo;
17052 +
17053 +       iinfo = au_ii(inode);
17054 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
17055 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
17056 +                && iinfo->ii_higen == h_inode->i_generation);
17057 +}
17058 +
17059 +static inline void au_iigen_dec(struct inode *inode)
17060 +{
17061 +       struct au_iinfo *iinfo;
17062 +       struct au_iigen *iigen;
17063 +
17064 +       iinfo = au_ii(inode);
17065 +       iigen = &iinfo->ii_generation;
17066 +       spin_lock(&iigen->ig_spin);
17067 +       iigen->ig_generation--;
17068 +       spin_unlock(&iigen->ig_spin);
17069 +}
17070 +
17071 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
17072 +{
17073 +       int err;
17074 +
17075 +       err = 0;
17076 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
17077 +               err = -EIO;
17078 +
17079 +       return err;
17080 +}
17081 +
17082 +/* ---------------------------------------------------------------------- */
17083 +
17084 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
17085 +                                         aufs_bindex_t bindex)
17086 +{
17087 +       return iinfo->ii_hinode + bindex;
17088 +}
17089 +
17090 +static inline int au_is_bad_inode(struct inode *inode)
17091 +{
17092 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
17093 +}
17094 +
17095 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
17096 +                                       aufs_bindex_t bindex)
17097 +{
17098 +       IiMustAnyLock(inode);
17099 +       return au_hinode(au_ii(inode), bindex)->hi_id;
17100 +}
17101 +
17102 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
17103 +{
17104 +       IiMustAnyLock(inode);
17105 +       return au_ii(inode)->ii_btop;
17106 +}
17107 +
17108 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
17109 +{
17110 +       IiMustAnyLock(inode);
17111 +       return au_ii(inode)->ii_bbot;
17112 +}
17113 +
17114 +static inline struct au_vdir *au_ivdir(struct inode *inode)
17115 +{
17116 +       IiMustAnyLock(inode);
17117 +       return au_ii(inode)->ii_vdir;
17118 +}
17119 +
17120 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
17121 +{
17122 +       IiMustAnyLock(inode);
17123 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
17124 +}
17125 +
17126 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
17127 +{
17128 +       IiMustWriteLock(inode);
17129 +       au_ii(inode)->ii_btop = bindex;
17130 +}
17131 +
17132 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
17133 +{
17134 +       IiMustWriteLock(inode);
17135 +       au_ii(inode)->ii_bbot = bindex;
17136 +}
17137 +
17138 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
17139 +{
17140 +       IiMustWriteLock(inode);
17141 +       au_ii(inode)->ii_vdir = vdir;
17142 +}
17143 +
17144 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
17145 +{
17146 +       IiMustAnyLock(inode);
17147 +       return au_hinode(au_ii(inode), bindex);
17148 +}
17149 +
17150 +/* ---------------------------------------------------------------------- */
17151 +
17152 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
17153 +{
17154 +       if (pin)
17155 +               return pin->parent;
17156 +       return NULL;
17157 +}
17158 +
17159 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
17160 +{
17161 +       if (pin && pin->hdir)
17162 +               return pin->hdir->hi_inode;
17163 +       return NULL;
17164 +}
17165 +
17166 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
17167 +{
17168 +       if (pin)
17169 +               return pin->hdir;
17170 +       return NULL;
17171 +}
17172 +
17173 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
17174 +{
17175 +       if (pin)
17176 +               pin->dentry = dentry;
17177 +}
17178 +
17179 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
17180 +                                          unsigned char lflag)
17181 +{
17182 +       if (pin) {
17183 +               if (lflag)
17184 +                       au_fset_pin(pin->flags, DI_LOCKED);
17185 +               else
17186 +                       au_fclr_pin(pin->flags, DI_LOCKED);
17187 +       }
17188 +}
17189 +
17190 +#if 0 /* reserved */
17191 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
17192 +{
17193 +       if (pin) {
17194 +               dput(pin->parent);
17195 +               pin->parent = dget(parent);
17196 +       }
17197 +}
17198 +#endif
17199 +
17200 +/* ---------------------------------------------------------------------- */
17201 +
17202 +struct au_branch;
17203 +#ifdef CONFIG_AUFS_HNOTIFY
17204 +struct au_hnotify_op {
17205 +       void (*ctl)(struct au_hinode *hinode, int do_set);
17206 +       int (*alloc)(struct au_hinode *hinode);
17207 +
17208 +       /*
17209 +        * if it returns true, the the caller should free hinode->hi_notify,
17210 +        * otherwise ->free() frees it.
17211 +        */
17212 +       int (*free)(struct au_hinode *hinode,
17213 +                   struct au_hnotify *hn) __must_check;
17214 +
17215 +       void (*fin)(void);
17216 +       int (*init)(void);
17217 +
17218 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
17219 +       void (*fin_br)(struct au_branch *br);
17220 +       int (*init_br)(struct au_branch *br, int perm);
17221 +};
17222 +
17223 +/* hnotify.c */
17224 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
17225 +void au_hn_free(struct au_hinode *hinode);
17226 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
17227 +void au_hn_reset(struct inode *inode, unsigned int flags);
17228 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17229 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
17230 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
17231 +int au_hnotify_init_br(struct au_branch *br, int perm);
17232 +void au_hnotify_fin_br(struct au_branch *br);
17233 +int __init au_hnotify_init(void);
17234 +void au_hnotify_fin(void);
17235 +
17236 +/* hfsnotify.c */
17237 +extern const struct au_hnotify_op au_hnotify_op;
17238 +
17239 +static inline
17240 +void au_hn_init(struct au_hinode *hinode)
17241 +{
17242 +       hinode->hi_notify = NULL;
17243 +}
17244 +
17245 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
17246 +{
17247 +       return hinode->hi_notify;
17248 +}
17249 +
17250 +#else
17251 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
17252 +       struct au_hinode *hinode __maybe_unused,
17253 +       struct inode *inode __maybe_unused)
17254 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
17255 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
17256 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
17257 +          int do_set __maybe_unused)
17258 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
17259 +          unsigned int flags __maybe_unused)
17260 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
17261 +          struct au_branch *br __maybe_unused,
17262 +          int perm __maybe_unused)
17263 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
17264 +          int perm __maybe_unused)
17265 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
17266 +AuStubInt0(__init au_hnotify_init, void)
17267 +AuStubVoid(au_hnotify_fin, void)
17268 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
17269 +#endif /* CONFIG_AUFS_HNOTIFY */
17270 +
17271 +static inline void au_hn_suspend(struct au_hinode *hdir)
17272 +{
17273 +       au_hn_ctl(hdir, /*do_set*/0);
17274 +}
17275 +
17276 +static inline void au_hn_resume(struct au_hinode *hdir)
17277 +{
17278 +       au_hn_ctl(hdir, /*do_set*/1);
17279 +}
17280 +
17281 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
17282 +{
17283 +       inode_lock(hdir->hi_inode);
17284 +       au_hn_suspend(hdir);
17285 +}
17286 +
17287 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
17288 +                                         unsigned int sc __maybe_unused)
17289 +{
17290 +       inode_lock_nested(hdir->hi_inode, sc);
17291 +       au_hn_suspend(hdir);
17292 +}
17293 +
17294 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
17295 +{
17296 +       au_hn_resume(hdir);
17297 +       inode_unlock(hdir->hi_inode);
17298 +}
17299 +
17300 +#endif /* __KERNEL__ */
17301 +#endif /* __AUFS_INODE_H__ */
17302 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
17303 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
17304 +++ linux/fs/aufs/ioctl.c       2017-03-02 14:24:13.863589061 +0100
17305 @@ -0,0 +1,219 @@
17306 +/*
17307 + * Copyright (C) 2005-2017 Junjiro R. Okajima
17308 + *
17309 + * This program, aufs is free software; you can redistribute it and/or modify
17310 + * it under the terms of the GNU General Public License as published by
17311 + * the Free Software Foundation; either version 2 of the License, or
17312 + * (at your option) any later version.
17313 + *
17314 + * This program is distributed in the hope that it will be useful,
17315 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17316 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17317 + * GNU General Public License for more details.
17318 + *
17319 + * You should have received a copy of the GNU General Public License
17320 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17321 + */
17322 +
17323 +/*
17324 + * ioctl
17325 + * plink-management and readdir in userspace.
17326 + * assist the pathconf(3) wrapper library.
17327 + * move-down
17328 + * File-based Hierarchical Storage Management.
17329 + */
17330 +
17331 +#include <linux/compat.h>
17332 +#include <linux/file.h>
17333 +#include "aufs.h"
17334 +
17335 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
17336 +{
17337 +       int err, fd;
17338 +       aufs_bindex_t wbi, bindex, bbot;
17339 +       struct file *h_file;
17340 +       struct super_block *sb;
17341 +       struct dentry *root;
17342 +       struct au_branch *br;
17343 +       struct aufs_wbr_fd wbrfd = {
17344 +               .oflags = au_dir_roflags,
17345 +               .brid   = -1
17346 +       };
17347 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
17348 +               | O_NOATIME | O_CLOEXEC;
17349 +
17350 +       AuDebugOn(wbrfd.oflags & ~valid);
17351 +
17352 +       if (arg) {
17353 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
17354 +               if (unlikely(err)) {
17355 +                       err = -EFAULT;
17356 +                       goto out;
17357 +               }
17358 +
17359 +               err = -EINVAL;
17360 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
17361 +               wbrfd.oflags |= au_dir_roflags;
17362 +               AuDbg("0%o\n", wbrfd.oflags);
17363 +               if (unlikely(wbrfd.oflags & ~valid))
17364 +                       goto out;
17365 +       }
17366 +
17367 +       fd = get_unused_fd_flags(0);
17368 +       err = fd;
17369 +       if (unlikely(fd < 0))
17370 +               goto out;
17371 +
17372 +       h_file = ERR_PTR(-EINVAL);
17373 +       wbi = 0;
17374 +       br = NULL;
17375 +       sb = path->dentry->d_sb;
17376 +       root = sb->s_root;
17377 +       aufs_read_lock(root, AuLock_IR);
17378 +       bbot = au_sbbot(sb);
17379 +       if (wbrfd.brid >= 0) {
17380 +               wbi = au_br_index(sb, wbrfd.brid);
17381 +               if (unlikely(wbi < 0 || wbi > bbot))
17382 +                       goto out_unlock;
17383 +       }
17384 +
17385 +       h_file = ERR_PTR(-ENOENT);
17386 +       br = au_sbr(sb, wbi);
17387 +       if (!au_br_writable(br->br_perm)) {
17388 +               if (arg)
17389 +                       goto out_unlock;
17390 +
17391 +               bindex = wbi + 1;
17392 +               wbi = -1;
17393 +               for (; bindex <= bbot; bindex++) {
17394 +                       br = au_sbr(sb, bindex);
17395 +                       if (au_br_writable(br->br_perm)) {
17396 +                               wbi = bindex;
17397 +                               br = au_sbr(sb, wbi);
17398 +                               break;
17399 +                       }
17400 +               }
17401 +       }
17402 +       AuDbg("wbi %d\n", wbi);
17403 +       if (wbi >= 0)
17404 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17405 +                                  /*force_wr*/0);
17406 +
17407 +out_unlock:
17408 +       aufs_read_unlock(root, AuLock_IR);
17409 +       err = PTR_ERR(h_file);
17410 +       if (IS_ERR(h_file))
17411 +               goto out_fd;
17412 +
17413 +       au_br_put(br); /* cf. au_h_open() */
17414 +       fd_install(fd, h_file);
17415 +       err = fd;
17416 +       goto out; /* success */
17417 +
17418 +out_fd:
17419 +       put_unused_fd(fd);
17420 +out:
17421 +       AuTraceErr(err);
17422 +       return err;
17423 +}
17424 +
17425 +/* ---------------------------------------------------------------------- */
17426 +
17427 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17428 +{
17429 +       long err;
17430 +       struct dentry *dentry;
17431 +
17432 +       switch (cmd) {
17433 +       case AUFS_CTL_RDU:
17434 +       case AUFS_CTL_RDU_INO:
17435 +               err = au_rdu_ioctl(file, cmd, arg);
17436 +               break;
17437 +
17438 +       case AUFS_CTL_WBR_FD:
17439 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17440 +               break;
17441 +
17442 +       case AUFS_CTL_IBUSY:
17443 +               err = au_ibusy_ioctl(file, arg);
17444 +               break;
17445 +
17446 +       case AUFS_CTL_BRINFO:
17447 +               err = au_brinfo_ioctl(file, arg);
17448 +               break;
17449 +
17450 +       case AUFS_CTL_FHSM_FD:
17451 +               dentry = file->f_path.dentry;
17452 +               if (IS_ROOT(dentry))
17453 +                       err = au_fhsm_fd(dentry->d_sb, arg);
17454 +               else
17455 +                       err = -ENOTTY;
17456 +               break;
17457 +
17458 +       default:
17459 +               /* do not call the lower */
17460 +               AuDbg("0x%x\n", cmd);
17461 +               err = -ENOTTY;
17462 +       }
17463 +
17464 +       AuTraceErr(err);
17465 +       return err;
17466 +}
17467 +
17468 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17469 +{
17470 +       long err;
17471 +
17472 +       switch (cmd) {
17473 +       case AUFS_CTL_MVDOWN:
17474 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
17475 +               break;
17476 +
17477 +       case AUFS_CTL_WBR_FD:
17478 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17479 +               break;
17480 +
17481 +       default:
17482 +               /* do not call the lower */
17483 +               AuDbg("0x%x\n", cmd);
17484 +               err = -ENOTTY;
17485 +       }
17486 +
17487 +       AuTraceErr(err);
17488 +       return err;
17489 +}
17490 +
17491 +#ifdef CONFIG_COMPAT
17492 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17493 +                          unsigned long arg)
17494 +{
17495 +       long err;
17496 +
17497 +       switch (cmd) {
17498 +       case AUFS_CTL_RDU:
17499 +       case AUFS_CTL_RDU_INO:
17500 +               err = au_rdu_compat_ioctl(file, cmd, arg);
17501 +               break;
17502 +
17503 +       case AUFS_CTL_IBUSY:
17504 +               err = au_ibusy_compat_ioctl(file, arg);
17505 +               break;
17506 +
17507 +       case AUFS_CTL_BRINFO:
17508 +               err = au_brinfo_compat_ioctl(file, arg);
17509 +               break;
17510 +
17511 +       default:
17512 +               err = aufs_ioctl_dir(file, cmd, arg);
17513 +       }
17514 +
17515 +       AuTraceErr(err);
17516 +       return err;
17517 +}
17518 +
17519 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17520 +                             unsigned long arg)
17521 +{
17522 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17523 +}
17524 +#endif
17525 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17526 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
17527 +++ linux/fs/aufs/i_op_add.c    2017-03-02 14:24:13.863589061 +0100
17528 @@ -0,0 +1,928 @@
17529 +/*
17530 + * Copyright (C) 2005-2017 Junjiro R. Okajima
17531 + *
17532 + * This program, aufs is free software; you can redistribute it and/or modify
17533 + * it under the terms of the GNU General Public License as published by
17534 + * the Free Software Foundation; either version 2 of the License, or
17535 + * (at your option) any later version.
17536 + *
17537 + * This program is distributed in the hope that it will be useful,
17538 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17539 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17540 + * GNU General Public License for more details.
17541 + *
17542 + * You should have received a copy of the GNU General Public License
17543 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17544 + */
17545 +
17546 +/*
17547 + * inode operations (add entry)
17548 + */
17549 +
17550 +#include "aufs.h"
17551 +
17552 +/*
17553 + * final procedure of adding a new entry, except link(2).
17554 + * remove whiteout, instantiate, copyup the parent dir's times and size
17555 + * and update version.
17556 + * if it failed, re-create the removed whiteout.
17557 + */
17558 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
17559 +                 struct dentry *wh_dentry, struct dentry *dentry)
17560 +{
17561 +       int err, rerr;
17562 +       aufs_bindex_t bwh;
17563 +       struct path h_path;
17564 +       struct super_block *sb;
17565 +       struct inode *inode, *h_dir;
17566 +       struct dentry *wh;
17567 +
17568 +       bwh = -1;
17569 +       sb = dir->i_sb;
17570 +       if (wh_dentry) {
17571 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
17572 +               IMustLock(h_dir);
17573 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17574 +               bwh = au_dbwh(dentry);
17575 +               h_path.dentry = wh_dentry;
17576 +               h_path.mnt = au_sbr_mnt(sb, bindex);
17577 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17578 +                                         dentry);
17579 +               if (unlikely(err))
17580 +                       goto out;
17581 +       }
17582 +
17583 +       inode = au_new_inode(dentry, /*must_new*/1);
17584 +       if (!IS_ERR(inode)) {
17585 +               d_instantiate(dentry, inode);
17586 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
17587 +               IMustLock(dir);
17588 +               au_dir_ts(dir, bindex);
17589 +               dir->i_version++;
17590 +               au_fhsm_wrote(sb, bindex, /*force*/0);
17591 +               return 0; /* success */
17592 +       }
17593 +
17594 +       err = PTR_ERR(inode);
17595 +       if (!wh_dentry)
17596 +               goto out;
17597 +
17598 +       /* revert */
17599 +       /* dir inode is locked */
17600 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17601 +       rerr = PTR_ERR(wh);
17602 +       if (IS_ERR(wh)) {
17603 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17604 +                       dentry, err, rerr);
17605 +               err = -EIO;
17606 +       } else
17607 +               dput(wh);
17608 +
17609 +out:
17610 +       return err;
17611 +}
17612 +
17613 +static int au_d_may_add(struct dentry *dentry)
17614 +{
17615 +       int err;
17616 +
17617 +       err = 0;
17618 +       if (unlikely(d_unhashed(dentry)))
17619 +               err = -ENOENT;
17620 +       if (unlikely(d_really_is_positive(dentry)))
17621 +               err = -EEXIST;
17622 +       return err;
17623 +}
17624 +
17625 +/*
17626 + * simple tests for the adding inode operations.
17627 + * following the checks in vfs, plus the parent-child relationship.
17628 + */
17629 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17630 +              struct dentry *h_parent, int isdir)
17631 +{
17632 +       int err;
17633 +       umode_t h_mode;
17634 +       struct dentry *h_dentry;
17635 +       struct inode *h_inode;
17636 +
17637 +       err = -ENAMETOOLONG;
17638 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17639 +               goto out;
17640 +
17641 +       h_dentry = au_h_dptr(dentry, bindex);
17642 +       if (d_really_is_negative(dentry)) {
17643 +               err = -EEXIST;
17644 +               if (unlikely(d_is_positive(h_dentry)))
17645 +                       goto out;
17646 +       } else {
17647 +               /* rename(2) case */
17648 +               err = -EIO;
17649 +               if (unlikely(d_is_negative(h_dentry)))
17650 +                       goto out;
17651 +               h_inode = d_inode(h_dentry);
17652 +               if (unlikely(!h_inode->i_nlink))
17653 +                       goto out;
17654 +
17655 +               h_mode = h_inode->i_mode;
17656 +               if (!isdir) {
17657 +                       err = -EISDIR;
17658 +                       if (unlikely(S_ISDIR(h_mode)))
17659 +                               goto out;
17660 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17661 +                       err = -ENOTDIR;
17662 +                       goto out;
17663 +               }
17664 +       }
17665 +
17666 +       err = 0;
17667 +       /* expected parent dir is locked */
17668 +       if (unlikely(h_parent != h_dentry->d_parent))
17669 +               err = -EIO;
17670 +
17671 +out:
17672 +       AuTraceErr(err);
17673 +       return err;
17674 +}
17675 +
17676 +/*
17677 + * initial procedure of adding a new entry.
17678 + * prepare writable branch and the parent dir, lock it,
17679 + * and lookup whiteout for the new entry.
17680 + */
17681 +static struct dentry*
17682 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17683 +                 struct dentry *src_dentry, struct au_pin *pin,
17684 +                 struct au_wr_dir_args *wr_dir_args)
17685 +{
17686 +       struct dentry *wh_dentry, *h_parent;
17687 +       struct super_block *sb;
17688 +       struct au_branch *br;
17689 +       int err;
17690 +       unsigned int udba;
17691 +       aufs_bindex_t bcpup;
17692 +
17693 +       AuDbg("%pd\n", dentry);
17694 +
17695 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17696 +       bcpup = err;
17697 +       wh_dentry = ERR_PTR(err);
17698 +       if (unlikely(err < 0))
17699 +               goto out;
17700 +
17701 +       sb = dentry->d_sb;
17702 +       udba = au_opt_udba(sb);
17703 +       err = au_pin(pin, dentry, bcpup, udba,
17704 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17705 +       wh_dentry = ERR_PTR(err);
17706 +       if (unlikely(err))
17707 +               goto out;
17708 +
17709 +       h_parent = au_pinned_h_parent(pin);
17710 +       if (udba != AuOpt_UDBA_NONE
17711 +           && au_dbtop(dentry) == bcpup)
17712 +               err = au_may_add(dentry, bcpup, h_parent,
17713 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17714 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17715 +               err = -ENAMETOOLONG;
17716 +       wh_dentry = ERR_PTR(err);
17717 +       if (unlikely(err))
17718 +               goto out_unpin;
17719 +
17720 +       br = au_sbr(sb, bcpup);
17721 +       if (dt) {
17722 +               struct path tmp = {
17723 +                       .dentry = h_parent,
17724 +                       .mnt    = au_br_mnt(br)
17725 +               };
17726 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17727 +       }
17728 +
17729 +       wh_dentry = NULL;
17730 +       if (bcpup != au_dbwh(dentry))
17731 +               goto out; /* success */
17732 +
17733 +       /*
17734 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17735 +        * would not be able to removed in the future. So we don't allow such
17736 +        * name here and we don't handle ENAMETOOLONG differently here.
17737 +        */
17738 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17739 +
17740 +out_unpin:
17741 +       if (IS_ERR(wh_dentry))
17742 +               au_unpin(pin);
17743 +out:
17744 +       return wh_dentry;
17745 +}
17746 +
17747 +/* ---------------------------------------------------------------------- */
17748 +
17749 +enum { Mknod, Symlink, Creat };
17750 +struct simple_arg {
17751 +       int type;
17752 +       union {
17753 +               struct {
17754 +                       umode_t                 mode;
17755 +                       bool                    want_excl;
17756 +                       bool                    try_aopen;
17757 +                       struct vfsub_aopen_args *aopen;
17758 +               } c;
17759 +               struct {
17760 +                       const char *symname;
17761 +               } s;
17762 +               struct {
17763 +                       umode_t mode;
17764 +                       dev_t dev;
17765 +               } m;
17766 +       } u;
17767 +};
17768 +
17769 +static int add_simple(struct inode *dir, struct dentry *dentry,
17770 +                     struct simple_arg *arg)
17771 +{
17772 +       int err, rerr;
17773 +       aufs_bindex_t btop;
17774 +       unsigned char created;
17775 +       const unsigned char try_aopen
17776 +               = (arg->type == Creat && arg->u.c.try_aopen);
17777 +       struct dentry *wh_dentry, *parent;
17778 +       struct inode *h_dir;
17779 +       struct super_block *sb;
17780 +       struct au_branch *br;
17781 +       /* to reuduce stack size */
17782 +       struct {
17783 +               struct au_dtime dt;
17784 +               struct au_pin pin;
17785 +               struct path h_path;
17786 +               struct au_wr_dir_args wr_dir_args;
17787 +       } *a;
17788 +
17789 +       AuDbg("%pd\n", dentry);
17790 +       IMustLock(dir);
17791 +
17792 +       err = -ENOMEM;
17793 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17794 +       if (unlikely(!a))
17795 +               goto out;
17796 +       a->wr_dir_args.force_btgt = -1;
17797 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17798 +
17799 +       parent = dentry->d_parent; /* dir inode is locked */
17800 +       if (!try_aopen) {
17801 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17802 +               if (unlikely(err))
17803 +                       goto out_free;
17804 +       }
17805 +       err = au_d_may_add(dentry);
17806 +       if (unlikely(err))
17807 +               goto out_unlock;
17808 +       if (!try_aopen)
17809 +               di_write_lock_parent(parent);
17810 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17811 +                                     &a->pin, &a->wr_dir_args);
17812 +       err = PTR_ERR(wh_dentry);
17813 +       if (IS_ERR(wh_dentry))
17814 +               goto out_parent;
17815 +
17816 +       btop = au_dbtop(dentry);
17817 +       sb = dentry->d_sb;
17818 +       br = au_sbr(sb, btop);
17819 +       a->h_path.dentry = au_h_dptr(dentry, btop);
17820 +       a->h_path.mnt = au_br_mnt(br);
17821 +       h_dir = au_pinned_h_dir(&a->pin);
17822 +       switch (arg->type) {
17823 +       case Creat:
17824 +               err = 0;
17825 +               if (!try_aopen || !h_dir->i_op->atomic_open)
17826 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17827 +                                          arg->u.c.want_excl);
17828 +               else
17829 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17830 +                                               arg->u.c.aopen, br);
17831 +               break;
17832 +       case Symlink:
17833 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
17834 +               break;
17835 +       case Mknod:
17836 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17837 +                                 arg->u.m.dev);
17838 +               break;
17839 +       default:
17840 +               BUG();
17841 +       }
17842 +       created = !err;
17843 +       if (!err)
17844 +               err = epilog(dir, btop, wh_dentry, dentry);
17845 +
17846 +       /* revert */
17847 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
17848 +               /* no delegation since it is just created */
17849 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17850 +                                   /*force*/0);
17851 +               if (rerr) {
17852 +                       AuIOErr("%pd revert failure(%d, %d)\n",
17853 +                               dentry, err, rerr);
17854 +                       err = -EIO;
17855 +               }
17856 +               au_dtime_revert(&a->dt);
17857 +       }
17858 +
17859 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
17860 +               *arg->u.c.aopen->opened |= FILE_CREATED;
17861 +
17862 +       au_unpin(&a->pin);
17863 +       dput(wh_dentry);
17864 +
17865 +out_parent:
17866 +       if (!try_aopen)
17867 +               di_write_unlock(parent);
17868 +out_unlock:
17869 +       if (unlikely(err)) {
17870 +               au_update_dbtop(dentry);
17871 +               d_drop(dentry);
17872 +       }
17873 +       if (!try_aopen)
17874 +               aufs_read_unlock(dentry, AuLock_DW);
17875 +out_free:
17876 +       au_delayed_kfree(a);
17877 +out:
17878 +       return err;
17879 +}
17880 +
17881 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17882 +              dev_t dev)
17883 +{
17884 +       struct simple_arg arg = {
17885 +               .type = Mknod,
17886 +               .u.m = {
17887 +                       .mode   = mode,
17888 +                       .dev    = dev
17889 +               }
17890 +       };
17891 +       return add_simple(dir, dentry, &arg);
17892 +}
17893 +
17894 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17895 +{
17896 +       struct simple_arg arg = {
17897 +               .type = Symlink,
17898 +               .u.s.symname = symname
17899 +       };
17900 +       return add_simple(dir, dentry, &arg);
17901 +}
17902 +
17903 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17904 +               bool want_excl)
17905 +{
17906 +       struct simple_arg arg = {
17907 +               .type = Creat,
17908 +               .u.c = {
17909 +                       .mode           = mode,
17910 +                       .want_excl      = want_excl
17911 +               }
17912 +       };
17913 +       return add_simple(dir, dentry, &arg);
17914 +}
17915 +
17916 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17917 +                      struct vfsub_aopen_args *aopen_args)
17918 +{
17919 +       struct simple_arg arg = {
17920 +               .type = Creat,
17921 +               .u.c = {
17922 +                       .mode           = aopen_args->create_mode,
17923 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
17924 +                       .try_aopen      = true,
17925 +                       .aopen          = aopen_args
17926 +               }
17927 +       };
17928 +       return add_simple(dir, dentry, &arg);
17929 +}
17930 +
17931 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17932 +{
17933 +       int err;
17934 +       aufs_bindex_t bindex;
17935 +       struct super_block *sb;
17936 +       struct dentry *parent, *h_parent, *h_dentry;
17937 +       struct inode *h_dir, *inode;
17938 +       struct vfsmount *h_mnt;
17939 +       struct au_wr_dir_args wr_dir_args = {
17940 +               .force_btgt     = -1,
17941 +               .flags          = AuWrDir_TMPFILE
17942 +       };
17943 +
17944 +       /* copy-up may happen */
17945 +       inode_lock(dir);
17946 +
17947 +       sb = dir->i_sb;
17948 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17949 +       if (unlikely(err))
17950 +               goto out;
17951 +
17952 +       err = au_di_init(dentry);
17953 +       if (unlikely(err))
17954 +               goto out_si;
17955 +
17956 +       err = -EBUSY;
17957 +       parent = d_find_any_alias(dir);
17958 +       AuDebugOn(!parent);
17959 +       di_write_lock_parent(parent);
17960 +       if (unlikely(d_inode(parent) != dir))
17961 +               goto out_parent;
17962 +
17963 +       err = au_digen_test(parent, au_sigen(sb));
17964 +       if (unlikely(err))
17965 +               goto out_parent;
17966 +
17967 +       bindex = au_dbtop(parent);
17968 +       au_set_dbtop(dentry, bindex);
17969 +       au_set_dbbot(dentry, bindex);
17970 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17971 +       bindex = err;
17972 +       if (unlikely(err < 0))
17973 +               goto out_parent;
17974 +
17975 +       err = -EOPNOTSUPP;
17976 +       h_dir = au_h_iptr(dir, bindex);
17977 +       if (unlikely(!h_dir->i_op->tmpfile))
17978 +               goto out_parent;
17979 +
17980 +       h_mnt = au_sbr_mnt(sb, bindex);
17981 +       err = vfsub_mnt_want_write(h_mnt);
17982 +       if (unlikely(err))
17983 +               goto out_parent;
17984 +
17985 +       h_parent = au_h_dptr(parent, bindex);
17986 +       err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
17987 +       if (unlikely(err))
17988 +               goto out_mnt;
17989 +
17990 +       err = -ENOMEM;
17991 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
17992 +       if (unlikely(!h_dentry))
17993 +               goto out_mnt;
17994 +
17995 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17996 +       if (unlikely(err))
17997 +               goto out_dentry;
17998 +
17999 +       au_set_dbtop(dentry, bindex);
18000 +       au_set_dbbot(dentry, bindex);
18001 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
18002 +       inode = au_new_inode(dentry, /*must_new*/1);
18003 +       if (IS_ERR(inode)) {
18004 +               err = PTR_ERR(inode);
18005 +               au_set_h_dptr(dentry, bindex, NULL);
18006 +               au_set_dbtop(dentry, -1);
18007 +               au_set_dbbot(dentry, -1);
18008 +       } else {
18009 +               if (!inode->i_nlink)
18010 +                       set_nlink(inode, 1);
18011 +               d_tmpfile(dentry, inode);
18012 +               au_di(dentry)->di_tmpfile = 1;
18013 +
18014 +               /* update without i_mutex */
18015 +               if (au_ibtop(dir) == au_dbtop(dentry))
18016 +                       au_cpup_attr_timesizes(dir);
18017 +       }
18018 +
18019 +out_dentry:
18020 +       dput(h_dentry);
18021 +out_mnt:
18022 +       vfsub_mnt_drop_write(h_mnt);
18023 +out_parent:
18024 +       di_write_unlock(parent);
18025 +       dput(parent);
18026 +       di_write_unlock(dentry);
18027 +       if (unlikely(err)) {
18028 +               au_di_fin(dentry);
18029 +               dentry->d_fsdata = NULL;
18030 +       }
18031 +out_si:
18032 +       si_read_unlock(sb);
18033 +out:
18034 +       inode_unlock(dir);
18035 +       return err;
18036 +}
18037 +
18038 +/* ---------------------------------------------------------------------- */
18039 +
18040 +struct au_link_args {
18041 +       aufs_bindex_t bdst, bsrc;
18042 +       struct au_pin pin;
18043 +       struct path h_path;
18044 +       struct dentry *src_parent, *parent;
18045 +};
18046 +
18047 +static int au_cpup_before_link(struct dentry *src_dentry,
18048 +                              struct au_link_args *a)
18049 +{
18050 +       int err;
18051 +       struct dentry *h_src_dentry;
18052 +       struct au_cp_generic cpg = {
18053 +               .dentry = src_dentry,
18054 +               .bdst   = a->bdst,
18055 +               .bsrc   = a->bsrc,
18056 +               .len    = -1,
18057 +               .pin    = &a->pin,
18058 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
18059 +       };
18060 +
18061 +       di_read_lock_parent(a->src_parent, AuLock_IR);
18062 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
18063 +       if (unlikely(err))
18064 +               goto out;
18065 +
18066 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
18067 +       err = au_pin(&a->pin, src_dentry, a->bdst,
18068 +                    au_opt_udba(src_dentry->d_sb),
18069 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18070 +       if (unlikely(err))
18071 +               goto out;
18072 +
18073 +       err = au_sio_cpup_simple(&cpg);
18074 +       au_unpin(&a->pin);
18075 +
18076 +out:
18077 +       di_read_unlock(a->src_parent, AuLock_IR);
18078 +       return err;
18079 +}
18080 +
18081 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
18082 +                          struct au_link_args *a)
18083 +{
18084 +       int err;
18085 +       unsigned char plink;
18086 +       aufs_bindex_t bbot;
18087 +       struct dentry *h_src_dentry;
18088 +       struct inode *h_inode, *inode, *delegated;
18089 +       struct super_block *sb;
18090 +       struct file *h_file;
18091 +
18092 +       plink = 0;
18093 +       h_inode = NULL;
18094 +       sb = src_dentry->d_sb;
18095 +       inode = d_inode(src_dentry);
18096 +       if (au_ibtop(inode) <= a->bdst)
18097 +               h_inode = au_h_iptr(inode, a->bdst);
18098 +       if (!h_inode || !h_inode->i_nlink) {
18099 +               /* copyup src_dentry as the name of dentry. */
18100 +               bbot = au_dbbot(dentry);
18101 +               if (bbot < a->bsrc)
18102 +                       au_set_dbbot(dentry, a->bsrc);
18103 +               au_set_h_dptr(dentry, a->bsrc,
18104 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
18105 +               dget(a->h_path.dentry);
18106 +               au_set_h_dptr(dentry, a->bdst, NULL);
18107 +               AuDbg("temporary d_inode...\n");
18108 +               spin_lock(&dentry->d_lock);
18109 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
18110 +               spin_unlock(&dentry->d_lock);
18111 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
18112 +               if (IS_ERR(h_file))
18113 +                       err = PTR_ERR(h_file);
18114 +               else {
18115 +                       struct au_cp_generic cpg = {
18116 +                               .dentry = dentry,
18117 +                               .bdst   = a->bdst,
18118 +                               .bsrc   = -1,
18119 +                               .len    = -1,
18120 +                               .pin    = &a->pin,
18121 +                               .flags  = AuCpup_KEEPLINO
18122 +                       };
18123 +                       err = au_sio_cpup_simple(&cpg);
18124 +                       au_h_open_post(dentry, a->bsrc, h_file);
18125 +                       if (!err) {
18126 +                               dput(a->h_path.dentry);
18127 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18128 +                       } else
18129 +                               au_set_h_dptr(dentry, a->bdst,
18130 +                                             a->h_path.dentry);
18131 +               }
18132 +               spin_lock(&dentry->d_lock);
18133 +               dentry->d_inode = NULL; /* restore */
18134 +               spin_unlock(&dentry->d_lock);
18135 +               AuDbg("temporary d_inode...done\n");
18136 +               au_set_h_dptr(dentry, a->bsrc, NULL);
18137 +               au_set_dbbot(dentry, bbot);
18138 +       } else {
18139 +               /* the inode of src_dentry already exists on a.bdst branch */
18140 +               h_src_dentry = d_find_alias(h_inode);
18141 +               if (!h_src_dentry && au_plink_test(inode)) {
18142 +                       plink = 1;
18143 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
18144 +                       err = PTR_ERR(h_src_dentry);
18145 +                       if (IS_ERR(h_src_dentry))
18146 +                               goto out;
18147 +
18148 +                       if (unlikely(d_is_negative(h_src_dentry))) {
18149 +                               dput(h_src_dentry);
18150 +                               h_src_dentry = NULL;
18151 +                       }
18152 +
18153 +               }
18154 +               if (h_src_dentry) {
18155 +                       delegated = NULL;
18156 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
18157 +                                        &a->h_path, &delegated);
18158 +                       if (unlikely(err == -EWOULDBLOCK)) {
18159 +                               pr_warn("cannot retry for NFSv4 delegation"
18160 +                                       " for an internal link\n");
18161 +                               iput(delegated);
18162 +                       }
18163 +                       dput(h_src_dentry);
18164 +               } else {
18165 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
18166 +                               h_inode->i_ino, a->bdst);
18167 +                       err = -EIO;
18168 +               }
18169 +       }
18170 +
18171 +       if (!err && !plink)
18172 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
18173 +
18174 +out:
18175 +       AuTraceErr(err);
18176 +       return err;
18177 +}
18178 +
18179 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18180 +             struct dentry *dentry)
18181 +{
18182 +       int err, rerr;
18183 +       struct au_dtime dt;
18184 +       struct au_link_args *a;
18185 +       struct dentry *wh_dentry, *h_src_dentry;
18186 +       struct inode *inode, *delegated;
18187 +       struct super_block *sb;
18188 +       struct au_wr_dir_args wr_dir_args = {
18189 +               /* .force_btgt  = -1, */
18190 +               .flags          = AuWrDir_ADD_ENTRY
18191 +       };
18192 +
18193 +       IMustLock(dir);
18194 +       inode = d_inode(src_dentry);
18195 +       IMustLock(inode);
18196 +
18197 +       err = -ENOMEM;
18198 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18199 +       if (unlikely(!a))
18200 +               goto out;
18201 +
18202 +       a->parent = dentry->d_parent; /* dir inode is locked */
18203 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
18204 +                                       AuLock_NOPLM | AuLock_GEN);
18205 +       if (unlikely(err))
18206 +               goto out_kfree;
18207 +       err = au_d_linkable(src_dentry);
18208 +       if (unlikely(err))
18209 +               goto out_unlock;
18210 +       err = au_d_may_add(dentry);
18211 +       if (unlikely(err))
18212 +               goto out_unlock;
18213 +
18214 +       a->src_parent = dget_parent(src_dentry);
18215 +       wr_dir_args.force_btgt = au_ibtop(inode);
18216 +
18217 +       di_write_lock_parent(a->parent);
18218 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
18219 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
18220 +                                     &wr_dir_args);
18221 +       err = PTR_ERR(wh_dentry);
18222 +       if (IS_ERR(wh_dentry))
18223 +               goto out_parent;
18224 +
18225 +       err = 0;
18226 +       sb = dentry->d_sb;
18227 +       a->bdst = au_dbtop(dentry);
18228 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18229 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
18230 +       a->bsrc = au_ibtop(inode);
18231 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18232 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
18233 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
18234 +       if (!h_src_dentry) {
18235 +               a->bsrc = au_dbtop(src_dentry);
18236 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18237 +               AuDebugOn(!h_src_dentry);
18238 +       } else if (IS_ERR(h_src_dentry)) {
18239 +               err = PTR_ERR(h_src_dentry);
18240 +               goto out_parent;
18241 +       }
18242 +
18243 +       /*
18244 +        * aufs doesn't touch the credential so
18245 +        * security_dentry_create_files_as() is unnecrssary.
18246 +        */
18247 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
18248 +               if (a->bdst < a->bsrc
18249 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
18250 +                       err = au_cpup_or_link(src_dentry, dentry, a);
18251 +               else {
18252 +                       delegated = NULL;
18253 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
18254 +                                        &a->h_path, &delegated);
18255 +                       if (unlikely(err == -EWOULDBLOCK)) {
18256 +                               pr_warn("cannot retry for NFSv4 delegation"
18257 +                                       " for an internal link\n");
18258 +                               iput(delegated);
18259 +                       }
18260 +               }
18261 +               dput(h_src_dentry);
18262 +       } else {
18263 +               /*
18264 +                * copyup src_dentry to the branch we process,
18265 +                * and then link(2) to it.
18266 +                */
18267 +               dput(h_src_dentry);
18268 +               if (a->bdst < a->bsrc
18269 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
18270 +                       au_unpin(&a->pin);
18271 +                       di_write_unlock(a->parent);
18272 +                       err = au_cpup_before_link(src_dentry, a);
18273 +                       di_write_lock_parent(a->parent);
18274 +                       if (!err)
18275 +                               err = au_pin(&a->pin, dentry, a->bdst,
18276 +                                            au_opt_udba(sb),
18277 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18278 +                       if (unlikely(err))
18279 +                               goto out_wh;
18280 +               }
18281 +               if (!err) {
18282 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
18283 +                       err = -ENOENT;
18284 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
18285 +                               delegated = NULL;
18286 +                               err = vfsub_link(h_src_dentry,
18287 +                                                au_pinned_h_dir(&a->pin),
18288 +                                                &a->h_path, &delegated);
18289 +                               if (unlikely(err == -EWOULDBLOCK)) {
18290 +                                       pr_warn("cannot retry"
18291 +                                               " for NFSv4 delegation"
18292 +                                               " for an internal link\n");
18293 +                                       iput(delegated);
18294 +                               }
18295 +                       }
18296 +               }
18297 +       }
18298 +       if (unlikely(err))
18299 +               goto out_unpin;
18300 +
18301 +       if (wh_dentry) {
18302 +               a->h_path.dentry = wh_dentry;
18303 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
18304 +                                         dentry);
18305 +               if (unlikely(err))
18306 +                       goto out_revert;
18307 +       }
18308 +
18309 +       au_dir_ts(dir, a->bdst);
18310 +       dir->i_version++;
18311 +       inc_nlink(inode);
18312 +       inode->i_ctime = dir->i_ctime;
18313 +       d_instantiate(dentry, au_igrab(inode));
18314 +       if (d_unhashed(a->h_path.dentry))
18315 +               /* some filesystem calls d_drop() */
18316 +               d_drop(dentry);
18317 +       /* some filesystems consume an inode even hardlink */
18318 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
18319 +       goto out_unpin; /* success */
18320 +
18321 +out_revert:
18322 +       /* no delegation since it is just created */
18323 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
18324 +                           /*delegated*/NULL, /*force*/0);
18325 +       if (unlikely(rerr)) {
18326 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
18327 +               err = -EIO;
18328 +       }
18329 +       au_dtime_revert(&dt);
18330 +out_unpin:
18331 +       au_unpin(&a->pin);
18332 +out_wh:
18333 +       dput(wh_dentry);
18334 +out_parent:
18335 +       di_write_unlock(a->parent);
18336 +       dput(a->src_parent);
18337 +out_unlock:
18338 +       if (unlikely(err)) {
18339 +               au_update_dbtop(dentry);
18340 +               d_drop(dentry);
18341 +       }
18342 +       aufs_read_and_write_unlock2(dentry, src_dentry);
18343 +out_kfree:
18344 +       au_delayed_kfree(a);
18345 +out:
18346 +       AuTraceErr(err);
18347 +       return err;
18348 +}
18349 +
18350 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
18351 +{
18352 +       int err, rerr;
18353 +       aufs_bindex_t bindex;
18354 +       unsigned char diropq;
18355 +       struct path h_path;
18356 +       struct dentry *wh_dentry, *parent, *opq_dentry;
18357 +       struct inode *h_inode;
18358 +       struct super_block *sb;
18359 +       struct {
18360 +               struct au_pin pin;
18361 +               struct au_dtime dt;
18362 +       } *a; /* reduce the stack usage */
18363 +       struct au_wr_dir_args wr_dir_args = {
18364 +               .force_btgt     = -1,
18365 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
18366 +       };
18367 +
18368 +       IMustLock(dir);
18369 +
18370 +       err = -ENOMEM;
18371 +       a = kmalloc(sizeof(*a), GFP_NOFS);
18372 +       if (unlikely(!a))
18373 +               goto out;
18374 +
18375 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18376 +       if (unlikely(err))
18377 +               goto out_free;
18378 +       err = au_d_may_add(dentry);
18379 +       if (unlikely(err))
18380 +               goto out_unlock;
18381 +
18382 +       parent = dentry->d_parent; /* dir inode is locked */
18383 +       di_write_lock_parent(parent);
18384 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18385 +                                     &a->pin, &wr_dir_args);
18386 +       err = PTR_ERR(wh_dentry);
18387 +       if (IS_ERR(wh_dentry))
18388 +               goto out_parent;
18389 +
18390 +       sb = dentry->d_sb;
18391 +       bindex = au_dbtop(dentry);
18392 +       h_path.dentry = au_h_dptr(dentry, bindex);
18393 +       h_path.mnt = au_sbr_mnt(sb, bindex);
18394 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18395 +       if (unlikely(err))
18396 +               goto out_unpin;
18397 +
18398 +       /* make the dir opaque */
18399 +       diropq = 0;
18400 +       h_inode = d_inode(h_path.dentry);
18401 +       if (wh_dentry
18402 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
18403 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
18404 +               opq_dentry = au_diropq_create(dentry, bindex);
18405 +               inode_unlock(h_inode);
18406 +               err = PTR_ERR(opq_dentry);
18407 +               if (IS_ERR(opq_dentry))
18408 +                       goto out_dir;
18409 +               dput(opq_dentry);
18410 +               diropq = 1;
18411 +       }
18412 +
18413 +       err = epilog(dir, bindex, wh_dentry, dentry);
18414 +       if (!err) {
18415 +               inc_nlink(dir);
18416 +               goto out_unpin; /* success */
18417 +       }
18418 +
18419 +       /* revert */
18420 +       if (diropq) {
18421 +               AuLabel(revert opq);
18422 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
18423 +               rerr = au_diropq_remove(dentry, bindex);
18424 +               inode_unlock(h_inode);
18425 +               if (rerr) {
18426 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18427 +                               dentry, err, rerr);
18428 +                       err = -EIO;
18429 +               }
18430 +       }
18431 +
18432 +out_dir:
18433 +       AuLabel(revert dir);
18434 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18435 +       if (rerr) {
18436 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
18437 +                       dentry, err, rerr);
18438 +               err = -EIO;
18439 +       }
18440 +       au_dtime_revert(&a->dt);
18441 +out_unpin:
18442 +       au_unpin(&a->pin);
18443 +       dput(wh_dentry);
18444 +out_parent:
18445 +       di_write_unlock(parent);
18446 +out_unlock:
18447 +       if (unlikely(err)) {
18448 +               au_update_dbtop(dentry);
18449 +               d_drop(dentry);
18450 +       }
18451 +       aufs_read_unlock(dentry, AuLock_DW);
18452 +out_free:
18453 +       au_delayed_kfree(a);
18454 +out:
18455 +       return err;
18456 +}
18457 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18458 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
18459 +++ linux/fs/aufs/i_op.c        2017-03-02 14:24:13.860255635 +0100
18460 @@ -0,0 +1,1448 @@
18461 +/*
18462 + * Copyright (C) 2005-2017 Junjiro R. Okajima
18463 + *
18464 + * This program, aufs is free software; you can redistribute it and/or modify
18465 + * it under the terms of the GNU General Public License as published by
18466 + * the Free Software Foundation; either version 2 of the License, or
18467 + * (at your option) any later version.
18468 + *
18469 + * This program is distributed in the hope that it will be useful,
18470 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18471 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18472 + * GNU General Public License for more details.
18473 + *
18474 + * You should have received a copy of the GNU General Public License
18475 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18476 + */
18477 +
18478 +/*
18479 + * inode operations (except add/del/rename)
18480 + */
18481 +
18482 +#include <linux/device_cgroup.h>
18483 +#include <linux/fs_stack.h>
18484 +#include <linux/namei.h>
18485 +#include <linux/security.h>
18486 +#include "aufs.h"
18487 +
18488 +static int h_permission(struct inode *h_inode, int mask,
18489 +                       struct path *h_path, int brperm)
18490 +{
18491 +       int err;
18492 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18493 +
18494 +       err = -EPERM;
18495 +       if (write_mask && IS_IMMUTABLE(h_inode))
18496 +               goto out;
18497 +
18498 +       err = -EACCES;
18499 +       if (((mask & MAY_EXEC)
18500 +            && S_ISREG(h_inode->i_mode)
18501 +            && (path_noexec(h_path)
18502 +                || !(h_inode->i_mode & S_IXUGO))))
18503 +               goto out;
18504 +
18505 +       /*
18506 +        * - skip the lower fs test in the case of write to ro branch.
18507 +        * - nfs dir permission write check is optimized, but a policy for
18508 +        *   link/rename requires a real check.
18509 +        * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18510 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
18511 +        */
18512 +       if ((write_mask && !au_br_writable(brperm))
18513 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18514 +               && write_mask && !(mask & MAY_READ))
18515 +           || !h_inode->i_op->permission) {
18516 +               /* AuLabel(generic_permission); */
18517 +               /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
18518 +               err = generic_permission(h_inode, mask);
18519 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18520 +                       err = h_inode->i_op->permission(h_inode, mask);
18521 +               AuTraceErr(err);
18522 +       } else {
18523 +               /* AuLabel(h_inode->permission); */
18524 +               err = h_inode->i_op->permission(h_inode, mask);
18525 +               AuTraceErr(err);
18526 +       }
18527 +
18528 +       if (!err)
18529 +               err = devcgroup_inode_permission(h_inode, mask);
18530 +       if (!err)
18531 +               err = security_inode_permission(h_inode, mask);
18532 +
18533 +#if 0
18534 +       if (!err) {
18535 +               /* todo: do we need to call ima_path_check()? */
18536 +               struct path h_path = {
18537 +                       .dentry =
18538 +                       .mnt    = h_mnt
18539 +               };
18540 +               err = ima_path_check(&h_path,
18541 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18542 +                                    IMA_COUNT_LEAVE);
18543 +       }
18544 +#endif
18545 +
18546 +out:
18547 +       return err;
18548 +}
18549 +
18550 +static int aufs_permission(struct inode *inode, int mask)
18551 +{
18552 +       int err;
18553 +       aufs_bindex_t bindex, bbot;
18554 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18555 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18556 +       struct inode *h_inode;
18557 +       struct super_block *sb;
18558 +       struct au_branch *br;
18559 +
18560 +       /* todo: support rcu-walk? */
18561 +       if (mask & MAY_NOT_BLOCK)
18562 +               return -ECHILD;
18563 +
18564 +       sb = inode->i_sb;
18565 +       si_read_lock(sb, AuLock_FLUSH);
18566 +       ii_read_lock_child(inode);
18567 +#if 0
18568 +       err = au_iigen_test(inode, au_sigen(sb));
18569 +       if (unlikely(err))
18570 +               goto out;
18571 +#endif
18572 +
18573 +       if (!isdir
18574 +           || write_mask
18575 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18576 +               err = au_busy_or_stale();
18577 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
18578 +               if (unlikely(!h_inode
18579 +                            || (h_inode->i_mode & S_IFMT)
18580 +                            != (inode->i_mode & S_IFMT)))
18581 +                       goto out;
18582 +
18583 +               err = 0;
18584 +               bindex = au_ibtop(inode);
18585 +               br = au_sbr(sb, bindex);
18586 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
18587 +               if (write_mask
18588 +                   && !err
18589 +                   && !special_file(h_inode->i_mode)) {
18590 +                       /* test whether the upper writable branch exists */
18591 +                       err = -EROFS;
18592 +                       for (; bindex >= 0; bindex--)
18593 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
18594 +                                       err = 0;
18595 +                                       break;
18596 +                               }
18597 +               }
18598 +               goto out;
18599 +       }
18600 +
18601 +       /* non-write to dir */
18602 +       err = 0;
18603 +       bbot = au_ibbot(inode);
18604 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
18605 +               h_inode = au_h_iptr(inode, bindex);
18606 +               if (h_inode) {
18607 +                       err = au_busy_or_stale();
18608 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
18609 +                               break;
18610 +
18611 +                       br = au_sbr(sb, bindex);
18612 +                       err = h_permission(h_inode, mask, &br->br_path,
18613 +                                          br->br_perm);
18614 +               }
18615 +       }
18616 +
18617 +out:
18618 +       ii_read_unlock(inode);
18619 +       si_read_unlock(sb);
18620 +       return err;
18621 +}
18622 +
18623 +/* ---------------------------------------------------------------------- */
18624 +
18625 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18626 +                                 unsigned int flags)
18627 +{
18628 +       struct dentry *ret, *parent;
18629 +       struct inode *inode;
18630 +       struct super_block *sb;
18631 +       int err, npositive;
18632 +
18633 +       IMustLock(dir);
18634 +
18635 +       /* todo: support rcu-walk? */
18636 +       ret = ERR_PTR(-ECHILD);
18637 +       if (flags & LOOKUP_RCU)
18638 +               goto out;
18639 +
18640 +       ret = ERR_PTR(-ENAMETOOLONG);
18641 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18642 +               goto out;
18643 +
18644 +       sb = dir->i_sb;
18645 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18646 +       ret = ERR_PTR(err);
18647 +       if (unlikely(err))
18648 +               goto out;
18649 +
18650 +       err = au_di_init(dentry);
18651 +       ret = ERR_PTR(err);
18652 +       if (unlikely(err))
18653 +               goto out_si;
18654 +
18655 +       inode = NULL;
18656 +       npositive = 0; /* suppress a warning */
18657 +       parent = dentry->d_parent; /* dir inode is locked */
18658 +       di_read_lock_parent(parent, AuLock_IR);
18659 +       err = au_alive_dir(parent);
18660 +       if (!err)
18661 +               err = au_digen_test(parent, au_sigen(sb));
18662 +       if (!err) {
18663 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
18664 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
18665 +                                          AuLkup_ALLOW_NEG);
18666 +               err = npositive;
18667 +       }
18668 +       di_read_unlock(parent, AuLock_IR);
18669 +       ret = ERR_PTR(err);
18670 +       if (unlikely(err < 0))
18671 +               goto out_unlock;
18672 +
18673 +       if (npositive) {
18674 +               inode = au_new_inode(dentry, /*must_new*/0);
18675 +               if (IS_ERR(inode)) {
18676 +                       ret = (void *)inode;
18677 +                       inode = NULL;
18678 +                       goto out_unlock;
18679 +               }
18680 +       }
18681 +
18682 +       if (inode)
18683 +               atomic_inc(&inode->i_count);
18684 +       ret = d_splice_alias(inode, dentry);
18685 +#if 0
18686 +       if (unlikely(d_need_lookup(dentry))) {
18687 +               spin_lock(&dentry->d_lock);
18688 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18689 +               spin_unlock(&dentry->d_lock);
18690 +       } else
18691 +#endif
18692 +       if (inode) {
18693 +               if (!IS_ERR(ret)) {
18694 +                       iput(inode);
18695 +                       if (ret && ret != dentry)
18696 +                               ii_write_unlock(inode);
18697 +               } else {
18698 +                       ii_write_unlock(inode);
18699 +                       iput(inode);
18700 +                       inode = NULL;
18701 +               }
18702 +       }
18703 +
18704 +out_unlock:
18705 +       di_write_unlock(dentry);
18706 +out_si:
18707 +       si_read_unlock(sb);
18708 +out:
18709 +       return ret;
18710 +}
18711 +
18712 +/* ---------------------------------------------------------------------- */
18713 +
18714 +struct aopen_node {
18715 +       struct hlist_node hlist;
18716 +       struct file *file, *h_file;
18717 +};
18718 +
18719 +static int au_do_aopen(struct inode *inode, struct file *file)
18720 +{
18721 +       struct au_sphlhead *aopen;
18722 +       struct aopen_node *node;
18723 +       struct au_do_open_args args = {
18724 +               .no_lock        = 1,
18725 +               .open           = au_do_open_nondir
18726 +       };
18727 +
18728 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
18729 +       spin_lock(&aopen->spin);
18730 +       hlist_for_each_entry(node, &aopen->head, hlist)
18731 +               if (node->file == file) {
18732 +                       args.h_file = node->h_file;
18733 +                       break;
18734 +               }
18735 +       spin_unlock(&aopen->spin);
18736 +       /* AuDebugOn(!args.h_file); */
18737 +
18738 +       return au_do_open(file, &args);
18739 +}
18740 +
18741 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18742 +                           struct file *file, unsigned int open_flag,
18743 +                           umode_t create_mode, int *opened)
18744 +{
18745 +       int err, h_opened = *opened;
18746 +       unsigned int lkup_flags;
18747 +       struct dentry *parent, *d;
18748 +       struct au_sphlhead *aopen;
18749 +       struct vfsub_aopen_args args = {
18750 +               .open_flag      = open_flag,
18751 +               .create_mode    = create_mode,
18752 +               .opened         = &h_opened
18753 +       };
18754 +       struct aopen_node aopen_node = {
18755 +               .file   = file
18756 +       };
18757 +
18758 +       IMustLock(dir);
18759 +       AuDbg("open_flag 0%o\n", open_flag);
18760 +       AuDbgDentry(dentry);
18761 +
18762 +       err = 0;
18763 +       if (!au_di(dentry)) {
18764 +               lkup_flags = LOOKUP_OPEN;
18765 +               if (open_flag & O_CREAT)
18766 +                       lkup_flags |= LOOKUP_CREATE;
18767 +               d = aufs_lookup(dir, dentry, lkup_flags);
18768 +               if (IS_ERR(d)) {
18769 +                       err = PTR_ERR(d);
18770 +                       AuTraceErr(err);
18771 +                       goto out;
18772 +               } else if (d) {
18773 +                       /*
18774 +                        * obsoleted dentry found.
18775 +                        * another error will be returned later.
18776 +                        */
18777 +                       d_drop(d);
18778 +                       AuDbgDentry(d);
18779 +                       dput(d);
18780 +               }
18781 +               AuDbgDentry(dentry);
18782 +       }
18783 +
18784 +       if (d_is_positive(dentry)
18785 +           || d_unhashed(dentry)
18786 +           || d_unlinked(dentry)
18787 +           || !(open_flag & O_CREAT))
18788 +               goto out_no_open;
18789 +
18790 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18791 +       if (unlikely(err))
18792 +               goto out;
18793 +
18794 +       parent = dentry->d_parent;      /* dir is locked */
18795 +       di_write_lock_parent(parent);
18796 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
18797 +       if (unlikely(err))
18798 +               goto out_unlock;
18799 +
18800 +       AuDbgDentry(dentry);
18801 +       if (d_is_positive(dentry))
18802 +               goto out_unlock;
18803 +
18804 +       args.file = get_empty_filp();
18805 +       err = PTR_ERR(args.file);
18806 +       if (IS_ERR(args.file))
18807 +               goto out_unlock;
18808 +
18809 +       args.file->f_flags = file->f_flags;
18810 +       err = au_aopen_or_create(dir, dentry, &args);
18811 +       AuTraceErr(err);
18812 +       AuDbgFile(args.file);
18813 +       if (unlikely(err < 0)) {
18814 +               if (h_opened & FILE_OPENED)
18815 +                       fput(args.file);
18816 +               else
18817 +                       put_filp(args.file);
18818 +               goto out_unlock;
18819 +       }
18820 +
18821 +       /* some filesystems don't set FILE_CREATED while succeeded? */
18822 +       *opened |= FILE_CREATED;
18823 +       if (h_opened & FILE_OPENED)
18824 +               aopen_node.h_file = args.file;
18825 +       else {
18826 +               put_filp(args.file);
18827 +               args.file = NULL;
18828 +       }
18829 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
18830 +       au_sphl_add(&aopen_node.hlist, aopen);
18831 +       err = finish_open(file, dentry, au_do_aopen, opened);
18832 +       au_sphl_del(&aopen_node.hlist, aopen);
18833 +       AuTraceErr(err);
18834 +       AuDbgFile(file);
18835 +       if (aopen_node.h_file)
18836 +               fput(aopen_node.h_file);
18837 +
18838 +out_unlock:
18839 +       di_write_unlock(parent);
18840 +       aufs_read_unlock(dentry, AuLock_DW);
18841 +       AuDbgDentry(dentry);
18842 +       if (unlikely(err < 0))
18843 +               goto out;
18844 +out_no_open:
18845 +       if (err >= 0 && !(*opened & FILE_CREATED)) {
18846 +               AuLabel(out_no_open);
18847 +               dget(dentry);
18848 +               err = finish_no_open(file, dentry);
18849 +       }
18850 +out:
18851 +       AuDbg("%pd%s%s\n", dentry,
18852 +             (*opened & FILE_CREATED) ? " created" : "",
18853 +             (*opened & FILE_OPENED) ? " opened" : "");
18854 +       AuTraceErr(err);
18855 +       return err;
18856 +}
18857 +
18858 +
18859 +/* ---------------------------------------------------------------------- */
18860 +
18861 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18862 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
18863 +                         aufs_bindex_t btop)
18864 +{
18865 +       int err;
18866 +       struct dentry *h_parent;
18867 +       struct inode *h_dir;
18868 +
18869 +       if (add_entry)
18870 +               IMustLock(d_inode(parent));
18871 +       else
18872 +               di_write_lock_parent(parent);
18873 +
18874 +       err = 0;
18875 +       if (!au_h_dptr(parent, bcpup)) {
18876 +               if (btop > bcpup)
18877 +                       err = au_cpup_dirs(dentry, bcpup);
18878 +               else if (btop < bcpup)
18879 +                       err = au_cpdown_dirs(dentry, bcpup);
18880 +               else
18881 +                       BUG();
18882 +       }
18883 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
18884 +               h_parent = au_h_dptr(parent, bcpup);
18885 +               h_dir = d_inode(h_parent);
18886 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
18887 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
18888 +               /* todo: no unlock here */
18889 +               inode_unlock(h_dir);
18890 +
18891 +               AuDbg("bcpup %d\n", bcpup);
18892 +               if (!err) {
18893 +                       if (d_really_is_negative(dentry))
18894 +                               au_set_h_dptr(dentry, btop, NULL);
18895 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
18896 +               }
18897 +       }
18898 +
18899 +       if (!add_entry)
18900 +               di_write_unlock(parent);
18901 +       if (!err)
18902 +               err = bcpup; /* success */
18903 +
18904 +       AuTraceErr(err);
18905 +       return err;
18906 +}
18907 +
18908 +/*
18909 + * decide the branch and the parent dir where we will create a new entry.
18910 + * returns new bindex or an error.
18911 + * copyup the parent dir if needed.
18912 + */
18913 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18914 +             struct au_wr_dir_args *args)
18915 +{
18916 +       int err;
18917 +       unsigned int flags;
18918 +       aufs_bindex_t bcpup, btop, src_btop;
18919 +       const unsigned char add_entry
18920 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
18921 +               | au_ftest_wrdir(args->flags, TMPFILE);
18922 +       struct super_block *sb;
18923 +       struct dentry *parent;
18924 +       struct au_sbinfo *sbinfo;
18925 +
18926 +       sb = dentry->d_sb;
18927 +       sbinfo = au_sbi(sb);
18928 +       parent = dget_parent(dentry);
18929 +       btop = au_dbtop(dentry);
18930 +       bcpup = btop;
18931 +       if (args->force_btgt < 0) {
18932 +               if (src_dentry) {
18933 +                       src_btop = au_dbtop(src_dentry);
18934 +                       if (src_btop < btop)
18935 +                               bcpup = src_btop;
18936 +               } else if (add_entry) {
18937 +                       flags = 0;
18938 +                       if (au_ftest_wrdir(args->flags, ISDIR))
18939 +                               au_fset_wbr(flags, DIR);
18940 +                       err = AuWbrCreate(sbinfo, dentry, flags);
18941 +                       bcpup = err;
18942 +               }
18943 +
18944 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
18945 +                       if (add_entry)
18946 +                               err = AuWbrCopyup(sbinfo, dentry);
18947 +                       else {
18948 +                               if (!IS_ROOT(dentry)) {
18949 +                                       di_read_lock_parent(parent, !AuLock_IR);
18950 +                                       err = AuWbrCopyup(sbinfo, dentry);
18951 +                                       di_read_unlock(parent, !AuLock_IR);
18952 +                               } else
18953 +                                       err = AuWbrCopyup(sbinfo, dentry);
18954 +                       }
18955 +                       bcpup = err;
18956 +                       if (unlikely(err < 0))
18957 +                               goto out;
18958 +               }
18959 +       } else {
18960 +               bcpup = args->force_btgt;
18961 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
18962 +       }
18963 +
18964 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
18965 +       err = bcpup;
18966 +       if (bcpup == btop)
18967 +               goto out; /* success */
18968 +
18969 +       /* copyup the new parent into the branch we process */
18970 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
18971 +       if (err >= 0) {
18972 +               if (d_really_is_negative(dentry)) {
18973 +                       au_set_h_dptr(dentry, btop, NULL);
18974 +                       au_set_dbtop(dentry, bcpup);
18975 +                       au_set_dbbot(dentry, bcpup);
18976 +               }
18977 +               AuDebugOn(add_entry
18978 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
18979 +                         && !au_h_dptr(dentry, bcpup));
18980 +       }
18981 +
18982 +out:
18983 +       dput(parent);
18984 +       return err;
18985 +}
18986 +
18987 +/* ---------------------------------------------------------------------- */
18988 +
18989 +void au_pin_hdir_unlock(struct au_pin *p)
18990 +{
18991 +       if (p->hdir)
18992 +               au_hn_inode_unlock(p->hdir);
18993 +}
18994 +
18995 +int au_pin_hdir_lock(struct au_pin *p)
18996 +{
18997 +       int err;
18998 +
18999 +       err = 0;
19000 +       if (!p->hdir)
19001 +               goto out;
19002 +
19003 +       /* even if an error happens later, keep this lock */
19004 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
19005 +
19006 +       err = -EBUSY;
19007 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
19008 +               goto out;
19009 +
19010 +       err = 0;
19011 +       if (p->h_dentry)
19012 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
19013 +                                 p->h_parent, p->br);
19014 +
19015 +out:
19016 +       return err;
19017 +}
19018 +
19019 +int au_pin_hdir_relock(struct au_pin *p)
19020 +{
19021 +       int err, i;
19022 +       struct inode *h_i;
19023 +       struct dentry *h_d[] = {
19024 +               p->h_dentry,
19025 +               p->h_parent
19026 +       };
19027 +
19028 +       err = au_pin_hdir_lock(p);
19029 +       if (unlikely(err))
19030 +               goto out;
19031 +
19032 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
19033 +               if (!h_d[i])
19034 +                       continue;
19035 +               if (d_is_positive(h_d[i])) {
19036 +                       h_i = d_inode(h_d[i]);
19037 +                       err = !h_i->i_nlink;
19038 +               }
19039 +       }
19040 +
19041 +out:
19042 +       return err;
19043 +}
19044 +
19045 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
19046 +{
19047 +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
19048 +       p->hdir->hi_inode->i_rwsem.owner = task;
19049 +#endif
19050 +}
19051 +
19052 +void au_pin_hdir_acquire_nest(struct au_pin *p)
19053 +{
19054 +       if (p->hdir) {
19055 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
19056 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
19057 +               au_pin_hdir_set_owner(p, current);
19058 +       }
19059 +}
19060 +
19061 +void au_pin_hdir_release(struct au_pin *p)
19062 +{
19063 +       if (p->hdir) {
19064 +               au_pin_hdir_set_owner(p, p->task);
19065 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
19066 +       }
19067 +}
19068 +
19069 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
19070 +{
19071 +       if (pin && pin->parent)
19072 +               return au_h_dptr(pin->parent, pin->bindex);
19073 +       return NULL;
19074 +}
19075 +
19076 +void au_unpin(struct au_pin *p)
19077 +{
19078 +       if (p->hdir)
19079 +               au_pin_hdir_unlock(p);
19080 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
19081 +               vfsub_mnt_drop_write(p->h_mnt);
19082 +       if (!p->hdir)
19083 +               return;
19084 +
19085 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
19086 +               di_read_unlock(p->parent, AuLock_IR);
19087 +       iput(p->hdir->hi_inode);
19088 +       dput(p->parent);
19089 +       p->parent = NULL;
19090 +       p->hdir = NULL;
19091 +       p->h_mnt = NULL;
19092 +       /* do not clear p->task */
19093 +}
19094 +
19095 +int au_do_pin(struct au_pin *p)
19096 +{
19097 +       int err;
19098 +       struct super_block *sb;
19099 +       struct inode *h_dir;
19100 +
19101 +       err = 0;
19102 +       sb = p->dentry->d_sb;
19103 +       p->br = au_sbr(sb, p->bindex);
19104 +       if (IS_ROOT(p->dentry)) {
19105 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
19106 +                       p->h_mnt = au_br_mnt(p->br);
19107 +                       err = vfsub_mnt_want_write(p->h_mnt);
19108 +                       if (unlikely(err)) {
19109 +                               au_fclr_pin(p->flags, MNT_WRITE);
19110 +                               goto out_err;
19111 +                       }
19112 +               }
19113 +               goto out;
19114 +       }
19115 +
19116 +       p->h_dentry = NULL;
19117 +       if (p->bindex <= au_dbbot(p->dentry))
19118 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
19119 +
19120 +       p->parent = dget_parent(p->dentry);
19121 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
19122 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
19123 +
19124 +       h_dir = NULL;
19125 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
19126 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
19127 +       if (p->hdir)
19128 +               h_dir = p->hdir->hi_inode;
19129 +
19130 +       /*
19131 +        * udba case, or
19132 +        * if DI_LOCKED is not set, then p->parent may be different
19133 +        * and h_parent can be NULL.
19134 +        */
19135 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
19136 +               err = -EBUSY;
19137 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
19138 +                       di_read_unlock(p->parent, AuLock_IR);
19139 +               dput(p->parent);
19140 +               p->parent = NULL;
19141 +               goto out_err;
19142 +       }
19143 +
19144 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
19145 +               p->h_mnt = au_br_mnt(p->br);
19146 +               err = vfsub_mnt_want_write(p->h_mnt);
19147 +               if (unlikely(err)) {
19148 +                       au_fclr_pin(p->flags, MNT_WRITE);
19149 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
19150 +                               di_read_unlock(p->parent, AuLock_IR);
19151 +                       dput(p->parent);
19152 +                       p->parent = NULL;
19153 +                       goto out_err;
19154 +               }
19155 +       }
19156 +
19157 +       au_igrab(h_dir);
19158 +       err = au_pin_hdir_lock(p);
19159 +       if (!err)
19160 +               goto out; /* success */
19161 +
19162 +       au_unpin(p);
19163 +
19164 +out_err:
19165 +       pr_err("err %d\n", err);
19166 +       err = au_busy_or_stale();
19167 +out:
19168 +       return err;
19169 +}
19170 +
19171 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
19172 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19173 +                unsigned int udba, unsigned char flags)
19174 +{
19175 +       p->dentry = dentry;
19176 +       p->udba = udba;
19177 +       p->lsc_di = lsc_di;
19178 +       p->lsc_hi = lsc_hi;
19179 +       p->flags = flags;
19180 +       p->bindex = bindex;
19181 +
19182 +       p->parent = NULL;
19183 +       p->hdir = NULL;
19184 +       p->h_mnt = NULL;
19185 +
19186 +       p->h_dentry = NULL;
19187 +       p->h_parent = NULL;
19188 +       p->br = NULL;
19189 +       p->task = current;
19190 +}
19191 +
19192 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19193 +          unsigned int udba, unsigned char flags)
19194 +{
19195 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
19196 +                   udba, flags);
19197 +       return au_do_pin(pin);
19198 +}
19199 +
19200 +/* ---------------------------------------------------------------------- */
19201 +
19202 +/*
19203 + * ->setattr() and ->getattr() are called in various cases.
19204 + * chmod, stat: dentry is revalidated.
19205 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
19206 + *               unhashed.
19207 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
19208 + */
19209 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
19210 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
19211 +{
19212 +       int err;
19213 +       struct dentry *parent;
19214 +
19215 +       err = 0;
19216 +       if (au_digen_test(dentry, sigen)) {
19217 +               parent = dget_parent(dentry);
19218 +               di_read_lock_parent(parent, AuLock_IR);
19219 +               err = au_refresh_dentry(dentry, parent);
19220 +               di_read_unlock(parent, AuLock_IR);
19221 +               dput(parent);
19222 +       }
19223 +
19224 +       AuTraceErr(err);
19225 +       return err;
19226 +}
19227 +
19228 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19229 +                    struct au_icpup_args *a)
19230 +{
19231 +       int err;
19232 +       loff_t sz;
19233 +       aufs_bindex_t btop, ibtop;
19234 +       struct dentry *hi_wh, *parent;
19235 +       struct inode *inode;
19236 +       struct au_wr_dir_args wr_dir_args = {
19237 +               .force_btgt     = -1,
19238 +               .flags          = 0
19239 +       };
19240 +
19241 +       if (d_is_dir(dentry))
19242 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
19243 +       /* plink or hi_wh() case */
19244 +       btop = au_dbtop(dentry);
19245 +       inode = d_inode(dentry);
19246 +       ibtop = au_ibtop(inode);
19247 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
19248 +               wr_dir_args.force_btgt = ibtop;
19249 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19250 +       if (unlikely(err < 0))
19251 +               goto out;
19252 +       a->btgt = err;
19253 +       if (err != btop)
19254 +               au_fset_icpup(a->flags, DID_CPUP);
19255 +
19256 +       err = 0;
19257 +       a->pin_flags = AuPin_MNT_WRITE;
19258 +       parent = NULL;
19259 +       if (!IS_ROOT(dentry)) {
19260 +               au_fset_pin(a->pin_flags, DI_LOCKED);
19261 +               parent = dget_parent(dentry);
19262 +               di_write_lock_parent(parent);
19263 +       }
19264 +
19265 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
19266 +       if (unlikely(err))
19267 +               goto out_parent;
19268 +
19269 +       sz = -1;
19270 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19271 +       a->h_inode = d_inode(a->h_path.dentry);
19272 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
19273 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19274 +               if (ia->ia_size < i_size_read(a->h_inode))
19275 +                       sz = ia->ia_size;
19276 +               inode_unlock(a->h_inode);
19277 +       }
19278 +
19279 +       hi_wh = NULL;
19280 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
19281 +               hi_wh = au_hi_wh(inode, a->btgt);
19282 +               if (!hi_wh) {
19283 +                       struct au_cp_generic cpg = {
19284 +                               .dentry = dentry,
19285 +                               .bdst   = a->btgt,
19286 +                               .bsrc   = -1,
19287 +                               .len    = sz,
19288 +                               .pin    = &a->pin
19289 +                       };
19290 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
19291 +                       if (unlikely(err))
19292 +                               goto out_unlock;
19293 +                       hi_wh = au_hi_wh(inode, a->btgt);
19294 +                       /* todo: revalidate hi_wh? */
19295 +               }
19296 +       }
19297 +
19298 +       if (parent) {
19299 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
19300 +               di_downgrade_lock(parent, AuLock_IR);
19301 +               dput(parent);
19302 +               parent = NULL;
19303 +       }
19304 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
19305 +               goto out; /* success */
19306 +
19307 +       if (!d_unhashed(dentry)) {
19308 +               struct au_cp_generic cpg = {
19309 +                       .dentry = dentry,
19310 +                       .bdst   = a->btgt,
19311 +                       .bsrc   = btop,
19312 +                       .len    = sz,
19313 +                       .pin    = &a->pin,
19314 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
19315 +               };
19316 +               err = au_sio_cpup_simple(&cpg);
19317 +               if (!err)
19318 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19319 +       } else if (!hi_wh)
19320 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19321 +       else
19322 +               a->h_path.dentry = hi_wh; /* do not dget here */
19323 +
19324 +out_unlock:
19325 +       a->h_inode = d_inode(a->h_path.dentry);
19326 +       if (!err)
19327 +               goto out; /* success */
19328 +       au_unpin(&a->pin);
19329 +out_parent:
19330 +       if (parent) {
19331 +               di_write_unlock(parent);
19332 +               dput(parent);
19333 +       }
19334 +out:
19335 +       if (!err)
19336 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19337 +       return err;
19338 +}
19339 +
19340 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
19341 +{
19342 +       int err;
19343 +       struct inode *inode, *delegated;
19344 +       struct super_block *sb;
19345 +       struct file *file;
19346 +       struct au_icpup_args *a;
19347 +
19348 +       inode = d_inode(dentry);
19349 +       IMustLock(inode);
19350 +
19351 +       err = setattr_prepare(dentry, ia);
19352 +       if (unlikely(err))
19353 +               goto out;
19354 +
19355 +       err = -ENOMEM;
19356 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19357 +       if (unlikely(!a))
19358 +               goto out;
19359 +
19360 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19361 +               ia->ia_valid &= ~ATTR_MODE;
19362 +
19363 +       file = NULL;
19364 +       sb = dentry->d_sb;
19365 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19366 +       if (unlikely(err))
19367 +               goto out_kfree;
19368 +
19369 +       if (ia->ia_valid & ATTR_FILE) {
19370 +               /* currently ftruncate(2) only */
19371 +               AuDebugOn(!d_is_reg(dentry));
19372 +               file = ia->ia_file;
19373 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
19374 +               if (unlikely(err))
19375 +                       goto out_si;
19376 +               ia->ia_file = au_hf_top(file);
19377 +               a->udba = AuOpt_UDBA_NONE;
19378 +       } else {
19379 +               /* fchmod() doesn't pass ia_file */
19380 +               a->udba = au_opt_udba(sb);
19381 +               di_write_lock_child(dentry);
19382 +               /* no d_unlinked(), to set UDBA_NONE for root */
19383 +               if (d_unhashed(dentry))
19384 +                       a->udba = AuOpt_UDBA_NONE;
19385 +               if (a->udba != AuOpt_UDBA_NONE) {
19386 +                       AuDebugOn(IS_ROOT(dentry));
19387 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
19388 +                       if (unlikely(err))
19389 +                               goto out_dentry;
19390 +               }
19391 +       }
19392 +
19393 +       err = au_pin_and_icpup(dentry, ia, a);
19394 +       if (unlikely(err < 0))
19395 +               goto out_dentry;
19396 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
19397 +               ia->ia_file = NULL;
19398 +               ia->ia_valid &= ~ATTR_FILE;
19399 +       }
19400 +
19401 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19402 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19403 +           == (ATTR_MODE | ATTR_CTIME)) {
19404 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
19405 +               if (unlikely(err))
19406 +                       goto out_unlock;
19407 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19408 +                  && (ia->ia_valid & ATTR_CTIME)) {
19409 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19410 +               if (unlikely(err))
19411 +                       goto out_unlock;
19412 +       }
19413 +
19414 +       if (ia->ia_valid & ATTR_SIZE) {
19415 +               struct file *f;
19416 +
19417 +               if (ia->ia_size < i_size_read(inode))
19418 +                       /* unmap only */
19419 +                       truncate_setsize(inode, ia->ia_size);
19420 +
19421 +               f = NULL;
19422 +               if (ia->ia_valid & ATTR_FILE)
19423 +                       f = ia->ia_file;
19424 +               inode_unlock(a->h_inode);
19425 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19426 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
19427 +       } else {
19428 +               delegated = NULL;
19429 +               while (1) {
19430 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
19431 +                       if (delegated) {
19432 +                               err = break_deleg_wait(&delegated);
19433 +                               if (!err)
19434 +                                       continue;
19435 +                       }
19436 +                       break;
19437 +               }
19438 +       }
19439 +       /*
19440 +        * regardless aufs 'acl' option setting.
19441 +        * why don't all acl-aware fs call this func from their ->setattr()?
19442 +        */
19443 +       if (!err && (ia->ia_valid & ATTR_MODE))
19444 +               err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
19445 +       if (!err)
19446 +               au_cpup_attr_changeable(inode);
19447 +
19448 +out_unlock:
19449 +       inode_unlock(a->h_inode);
19450 +       au_unpin(&a->pin);
19451 +       if (unlikely(err))
19452 +               au_update_dbtop(dentry);
19453 +out_dentry:
19454 +       di_write_unlock(dentry);
19455 +       if (file) {
19456 +               fi_write_unlock(file);
19457 +               ia->ia_file = file;
19458 +               ia->ia_valid |= ATTR_FILE;
19459 +       }
19460 +out_si:
19461 +       si_read_unlock(sb);
19462 +out_kfree:
19463 +       au_delayed_kfree(a);
19464 +out:
19465 +       AuTraceErr(err);
19466 +       return err;
19467 +}
19468 +
19469 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19470 +static int au_h_path_to_set_attr(struct dentry *dentry,
19471 +                                struct au_icpup_args *a, struct path *h_path)
19472 +{
19473 +       int err;
19474 +       struct super_block *sb;
19475 +
19476 +       sb = dentry->d_sb;
19477 +       a->udba = au_opt_udba(sb);
19478 +       /* no d_unlinked(), to set UDBA_NONE for root */
19479 +       if (d_unhashed(dentry))
19480 +               a->udba = AuOpt_UDBA_NONE;
19481 +       if (a->udba != AuOpt_UDBA_NONE) {
19482 +               AuDebugOn(IS_ROOT(dentry));
19483 +               err = au_reval_for_attr(dentry, au_sigen(sb));
19484 +               if (unlikely(err))
19485 +                       goto out;
19486 +       }
19487 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19488 +       if (unlikely(err < 0))
19489 +               goto out;
19490 +
19491 +       h_path->dentry = a->h_path.dentry;
19492 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
19493 +
19494 +out:
19495 +       return err;
19496 +}
19497 +
19498 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
19499 +                 struct au_sxattr *arg)
19500 +{
19501 +       int err;
19502 +       struct path h_path;
19503 +       struct super_block *sb;
19504 +       struct au_icpup_args *a;
19505 +       struct inode *h_inode;
19506 +
19507 +       IMustLock(inode);
19508 +
19509 +       err = -ENOMEM;
19510 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19511 +       if (unlikely(!a))
19512 +               goto out;
19513 +
19514 +       sb = dentry->d_sb;
19515 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19516 +       if (unlikely(err))
19517 +               goto out_kfree;
19518 +
19519 +       h_path.dentry = NULL;   /* silence gcc */
19520 +       di_write_lock_child(dentry);
19521 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
19522 +       if (unlikely(err))
19523 +               goto out_di;
19524 +
19525 +       inode_unlock(a->h_inode);
19526 +       switch (arg->type) {
19527 +       case AU_XATTR_SET:
19528 +               AuDebugOn(d_is_negative(h_path.dentry));
19529 +               err = vfsub_setxattr(h_path.dentry,
19530 +                                    arg->u.set.name, arg->u.set.value,
19531 +                                    arg->u.set.size, arg->u.set.flags);
19532 +               break;
19533 +       case AU_ACL_SET:
19534 +               err = -EOPNOTSUPP;
19535 +               h_inode = d_inode(h_path.dentry);
19536 +               if (h_inode->i_op->set_acl)
19537 +                       /* this will call posix_acl_update_mode */
19538 +                       err = h_inode->i_op->set_acl(h_inode,
19539 +                                                    arg->u.acl_set.acl,
19540 +                                                    arg->u.acl_set.type);
19541 +               break;
19542 +       }
19543 +       if (!err)
19544 +               au_cpup_attr_timesizes(inode);
19545 +
19546 +       au_unpin(&a->pin);
19547 +       if (unlikely(err))
19548 +               au_update_dbtop(dentry);
19549 +
19550 +out_di:
19551 +       di_write_unlock(dentry);
19552 +       si_read_unlock(sb);
19553 +out_kfree:
19554 +       au_delayed_kfree(a);
19555 +out:
19556 +       AuTraceErr(err);
19557 +       return err;
19558 +}
19559 +#endif
19560 +
19561 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19562 +                            unsigned int nlink)
19563 +{
19564 +       unsigned int n;
19565 +
19566 +       inode->i_mode = st->mode;
19567 +       /* don't i_[ug]id_write() here */
19568 +       inode->i_uid = st->uid;
19569 +       inode->i_gid = st->gid;
19570 +       inode->i_atime = st->atime;
19571 +       inode->i_mtime = st->mtime;
19572 +       inode->i_ctime = st->ctime;
19573 +
19574 +       au_cpup_attr_nlink(inode, /*force*/0);
19575 +       if (S_ISDIR(inode->i_mode)) {
19576 +               n = inode->i_nlink;
19577 +               n -= nlink;
19578 +               n += st->nlink;
19579 +               smp_mb(); /* for i_nlink */
19580 +               /* 0 can happen */
19581 +               set_nlink(inode, n);
19582 +       }
19583 +
19584 +       spin_lock(&inode->i_lock);
19585 +       inode->i_blocks = st->blocks;
19586 +       i_size_write(inode, st->size);
19587 +       spin_unlock(&inode->i_lock);
19588 +}
19589 +
19590 +/*
19591 + * common routine for aufs_getattr() and au_getxattr().
19592 + * returns zero or negative (an error).
19593 + * @dentry will be read-locked in success.
19594 + */
19595 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
19596 +                     int locked)
19597 +{
19598 +       int err;
19599 +       unsigned int mnt_flags, sigen;
19600 +       unsigned char udba_none;
19601 +       aufs_bindex_t bindex;
19602 +       struct super_block *sb, *h_sb;
19603 +       struct inode *inode;
19604 +
19605 +       h_path->mnt = NULL;
19606 +       h_path->dentry = NULL;
19607 +
19608 +       err = 0;
19609 +       sb = dentry->d_sb;
19610 +       mnt_flags = au_mntflags(sb);
19611 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19612 +
19613 +       if (unlikely(locked))
19614 +               goto body; /* skip locking dinfo */
19615 +
19616 +       /* support fstat(2) */
19617 +       if (!d_unlinked(dentry) && !udba_none) {
19618 +               sigen = au_sigen(sb);
19619 +               err = au_digen_test(dentry, sigen);
19620 +               if (!err) {
19621 +                       di_read_lock_child(dentry, AuLock_IR);
19622 +                       err = au_dbrange_test(dentry);
19623 +                       if (unlikely(err)) {
19624 +                               di_read_unlock(dentry, AuLock_IR);
19625 +                               goto out;
19626 +                       }
19627 +               } else {
19628 +                       AuDebugOn(IS_ROOT(dentry));
19629 +                       di_write_lock_child(dentry);
19630 +                       err = au_dbrange_test(dentry);
19631 +                       if (!err)
19632 +                               err = au_reval_for_attr(dentry, sigen);
19633 +                       if (!err)
19634 +                               di_downgrade_lock(dentry, AuLock_IR);
19635 +                       else {
19636 +                               di_write_unlock(dentry);
19637 +                               goto out;
19638 +                       }
19639 +               }
19640 +       } else
19641 +               di_read_lock_child(dentry, AuLock_IR);
19642 +
19643 +body:
19644 +       inode = d_inode(dentry);
19645 +       bindex = au_ibtop(inode);
19646 +       h_path->mnt = au_sbr_mnt(sb, bindex);
19647 +       h_sb = h_path->mnt->mnt_sb;
19648 +       if (!force
19649 +           && !au_test_fs_bad_iattr(h_sb)
19650 +           && udba_none)
19651 +               goto out; /* success */
19652 +
19653 +       if (au_dbtop(dentry) == bindex)
19654 +               h_path->dentry = au_h_dptr(dentry, bindex);
19655 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19656 +               h_path->dentry = au_plink_lkup(inode, bindex);
19657 +               if (IS_ERR(h_path->dentry))
19658 +                       /* pretending success */
19659 +                       h_path->dentry = NULL;
19660 +               else
19661 +                       dput(h_path->dentry);
19662 +       }
19663 +
19664 +out:
19665 +       return err;
19666 +}
19667 +
19668 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19669 +                       struct dentry *dentry, struct kstat *st)
19670 +{
19671 +       int err;
19672 +       unsigned char positive;
19673 +       struct path h_path;
19674 +       struct inode *inode;
19675 +       struct super_block *sb;
19676 +
19677 +       inode = d_inode(dentry);
19678 +       sb = dentry->d_sb;
19679 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19680 +       if (unlikely(err))
19681 +               goto out;
19682 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
19683 +       if (unlikely(err))
19684 +               goto out_si;
19685 +       if (unlikely(!h_path.dentry))
19686 +               /* illegally overlapped or something */
19687 +               goto out_fill; /* pretending success */
19688 +
19689 +       positive = d_is_positive(h_path.dentry);
19690 +       if (positive)
19691 +               err = vfs_getattr(&h_path, st);
19692 +       if (!err) {
19693 +               if (positive)
19694 +                       au_refresh_iattr(inode, st,
19695 +                                        d_inode(h_path.dentry)->i_nlink);
19696 +               goto out_fill; /* success */
19697 +       }
19698 +       AuTraceErr(err);
19699 +       goto out_di;
19700 +
19701 +out_fill:
19702 +       generic_fillattr(inode, st);
19703 +out_di:
19704 +       di_read_unlock(dentry, AuLock_IR);
19705 +out_si:
19706 +       si_read_unlock(sb);
19707 +out:
19708 +       AuTraceErr(err);
19709 +       return err;
19710 +}
19711 +
19712 +/* ---------------------------------------------------------------------- */
19713 +
19714 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
19715 +                                struct delayed_call *done)
19716 +{
19717 +       const char *ret;
19718 +       struct dentry *h_dentry;
19719 +       struct inode *h_inode;
19720 +       int err;
19721 +       aufs_bindex_t bindex;
19722 +
19723 +       ret = NULL; /* suppress a warning */
19724 +       err = -ECHILD;
19725 +       if (!dentry)
19726 +               goto out;
19727 +
19728 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19729 +       if (unlikely(err))
19730 +               goto out;
19731 +
19732 +       err = au_d_hashed_positive(dentry);
19733 +       if (unlikely(err))
19734 +               goto out_unlock;
19735 +
19736 +       err = -EINVAL;
19737 +       inode = d_inode(dentry);
19738 +       bindex = au_ibtop(inode);
19739 +       h_inode = au_h_iptr(inode, bindex);
19740 +       if (unlikely(!h_inode->i_op->get_link))
19741 +               goto out_unlock;
19742 +
19743 +       err = -EBUSY;
19744 +       h_dentry = NULL;
19745 +       if (au_dbtop(dentry) <= bindex) {
19746 +               h_dentry = au_h_dptr(dentry, bindex);
19747 +               if (h_dentry)
19748 +                       dget(h_dentry);
19749 +       }
19750 +       if (!h_dentry) {
19751 +               h_dentry = d_find_any_alias(h_inode);
19752 +               if (IS_ERR(h_dentry)) {
19753 +                       err = PTR_ERR(h_dentry);
19754 +                       goto out_unlock;
19755 +               }
19756 +       }
19757 +       if (unlikely(!h_dentry))
19758 +               goto out_unlock;
19759 +
19760 +       err = 0;
19761 +       AuDbg("%pf\n", h_inode->i_op->get_link);
19762 +       AuDbgDentry(h_dentry);
19763 +       ret = vfs_get_link(h_dentry, done);
19764 +       dput(h_dentry);
19765 +       if (IS_ERR(ret))
19766 +               err = PTR_ERR(ret);
19767 +
19768 +out_unlock:
19769 +       aufs_read_unlock(dentry, AuLock_IR);
19770 +out:
19771 +       if (unlikely(err))
19772 +               ret = ERR_PTR(err);
19773 +       AuTraceErrPtr(ret);
19774 +       return ret;
19775 +}
19776 +
19777 +/* ---------------------------------------------------------------------- */
19778 +
19779 +static int au_is_special(struct inode *inode)
19780 +{
19781 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
19782 +}
19783 +
19784 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
19785 +{
19786 +       int err;
19787 +       aufs_bindex_t bindex;
19788 +       struct super_block *sb;
19789 +       struct inode *h_inode;
19790 +       struct vfsmount *h_mnt;
19791 +
19792 +       sb = inode->i_sb;
19793 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
19794 +                 "unexpected s_flags 0x%lx", sb->s_flags);
19795 +
19796 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
19797 +       lockdep_off();
19798 +       si_read_lock(sb, AuLock_FLUSH);
19799 +       ii_write_lock_child(inode);
19800 +       lockdep_on();
19801 +
19802 +       err = 0;
19803 +       bindex = au_ibtop(inode);
19804 +       h_inode = au_h_iptr(inode, bindex);
19805 +       if (!au_test_ro(sb, bindex, inode)) {
19806 +               h_mnt = au_sbr_mnt(sb, bindex);
19807 +               err = vfsub_mnt_want_write(h_mnt);
19808 +               if (!err) {
19809 +                       err = vfsub_update_time(h_inode, ts, flags);
19810 +                       vfsub_mnt_drop_write(h_mnt);
19811 +               }
19812 +       } else if (au_is_special(h_inode)) {
19813 +               /*
19814 +                * Never copy-up here.
19815 +                * These special files may already be opened and used for
19816 +                * communicating. If we copied it up, then the communication
19817 +                * would be corrupted.
19818 +                */
19819 +               AuWarn1("timestamps for i%lu are ignored "
19820 +                       "since it is on readonly branch (hi%lu).\n",
19821 +                       inode->i_ino, h_inode->i_ino);
19822 +       } else if (flags & ~S_ATIME) {
19823 +               err = -EIO;
19824 +               AuIOErr1("unexpected flags 0x%x\n", flags);
19825 +               AuDebugOn(1);
19826 +       }
19827 +
19828 +       lockdep_off();
19829 +       if (!err)
19830 +               au_cpup_attr_timesizes(inode);
19831 +       ii_write_unlock(inode);
19832 +       si_read_unlock(sb);
19833 +       lockdep_on();
19834 +
19835 +       if (!err && (flags & S_VERSION))
19836 +               inode_inc_iversion(inode);
19837 +
19838 +       return err;
19839 +}
19840 +
19841 +/* ---------------------------------------------------------------------- */
19842 +
19843 +/* no getattr version will be set by module.c:aufs_init() */
19844 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
19845 +       aufs_iop[] = {
19846 +       [AuIop_SYMLINK] = {
19847 +               .permission     = aufs_permission,
19848 +#ifdef CONFIG_FS_POSIX_ACL
19849 +               .get_acl        = aufs_get_acl,
19850 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
19851 +#endif
19852 +
19853 +               .setattr        = aufs_setattr,
19854 +               .getattr        = aufs_getattr,
19855 +
19856 +#ifdef CONFIG_AUFS_XATTR
19857 +               .listxattr      = aufs_listxattr,
19858 +#endif
19859 +
19860 +               .get_link       = aufs_get_link,
19861 +
19862 +               /* .update_time = aufs_update_time */
19863 +       },
19864 +       [AuIop_DIR] = {
19865 +               .create         = aufs_create,
19866 +               .lookup         = aufs_lookup,
19867 +               .link           = aufs_link,
19868 +               .unlink         = aufs_unlink,
19869 +               .symlink        = aufs_symlink,
19870 +               .mkdir          = aufs_mkdir,
19871 +               .rmdir          = aufs_rmdir,
19872 +               .mknod          = aufs_mknod,
19873 +               .rename         = aufs_rename,
19874 +
19875 +               .permission     = aufs_permission,
19876 +#ifdef CONFIG_FS_POSIX_ACL
19877 +               .get_acl        = aufs_get_acl,
19878 +               .set_acl        = aufs_set_acl,
19879 +#endif
19880 +
19881 +               .setattr        = aufs_setattr,
19882 +               .getattr        = aufs_getattr,
19883 +
19884 +#ifdef CONFIG_AUFS_XATTR
19885 +               .listxattr      = aufs_listxattr,
19886 +#endif
19887 +
19888 +               .update_time    = aufs_update_time,
19889 +               .atomic_open    = aufs_atomic_open,
19890 +               .tmpfile        = aufs_tmpfile
19891 +       },
19892 +       [AuIop_OTHER] = {
19893 +               .permission     = aufs_permission,
19894 +#ifdef CONFIG_FS_POSIX_ACL
19895 +               .get_acl        = aufs_get_acl,
19896 +               .set_acl        = aufs_set_acl,
19897 +#endif
19898 +
19899 +               .setattr        = aufs_setattr,
19900 +               .getattr        = aufs_getattr,
19901 +
19902 +#ifdef CONFIG_AUFS_XATTR
19903 +               .listxattr      = aufs_listxattr,
19904 +#endif
19905 +
19906 +               .update_time    = aufs_update_time
19907 +       }
19908 +};
19909 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19910 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
19911 +++ linux/fs/aufs/i_op_del.c    2017-03-02 14:24:13.863589061 +0100
19912 @@ -0,0 +1,511 @@
19913 +/*
19914 + * Copyright (C) 2005-2017 Junjiro R. Okajima
19915 + *
19916 + * This program, aufs is free software; you can redistribute it and/or modify
19917 + * it under the terms of the GNU General Public License as published by
19918 + * the Free Software Foundation; either version 2 of the License, or
19919 + * (at your option) any later version.
19920 + *
19921 + * This program is distributed in the hope that it will be useful,
19922 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19923 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19924 + * GNU General Public License for more details.
19925 + *
19926 + * You should have received a copy of the GNU General Public License
19927 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19928 + */
19929 +
19930 +/*
19931 + * inode operations (del entry)
19932 + */
19933 +
19934 +#include "aufs.h"
19935 +
19936 +/*
19937 + * decide if a new whiteout for @dentry is necessary or not.
19938 + * when it is necessary, prepare the parent dir for the upper branch whose
19939 + * branch index is @bcpup for creation. the actual creation of the whiteout will
19940 + * be done by caller.
19941 + * return value:
19942 + * 0: wh is unnecessary
19943 + * plus: wh is necessary
19944 + * minus: error
19945 + */
19946 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
19947 +{
19948 +       int need_wh, err;
19949 +       aufs_bindex_t btop;
19950 +       struct super_block *sb;
19951 +
19952 +       sb = dentry->d_sb;
19953 +       btop = au_dbtop(dentry);
19954 +       if (*bcpup < 0) {
19955 +               *bcpup = btop;
19956 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
19957 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
19958 +                       *bcpup = err;
19959 +                       if (unlikely(err < 0))
19960 +                               goto out;
19961 +               }
19962 +       } else
19963 +               AuDebugOn(btop < *bcpup
19964 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
19965 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
19966 +
19967 +       if (*bcpup != btop) {
19968 +               err = au_cpup_dirs(dentry, *bcpup);
19969 +               if (unlikely(err))
19970 +                       goto out;
19971 +               need_wh = 1;
19972 +       } else {
19973 +               struct au_dinfo *dinfo, *tmp;
19974 +
19975 +               need_wh = -ENOMEM;
19976 +               dinfo = au_di(dentry);
19977 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19978 +               if (tmp) {
19979 +                       au_di_cp(tmp, dinfo);
19980 +                       au_di_swap(tmp, dinfo);
19981 +                       /* returns the number of positive dentries */
19982 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
19983 +                                                /* AuLkup_IGNORE_PERM */ 0);
19984 +                       au_di_swap(tmp, dinfo);
19985 +                       au_rw_write_unlock(&tmp->di_rwsem);
19986 +                       au_di_free(tmp);
19987 +               }
19988 +       }
19989 +       AuDbg("need_wh %d\n", need_wh);
19990 +       err = need_wh;
19991 +
19992 +out:
19993 +       return err;
19994 +}
19995 +
19996 +/*
19997 + * simple tests for the del-entry operations.
19998 + * following the checks in vfs, plus the parent-child relationship.
19999 + */
20000 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20001 +              struct dentry *h_parent, int isdir)
20002 +{
20003 +       int err;
20004 +       umode_t h_mode;
20005 +       struct dentry *h_dentry, *h_latest;
20006 +       struct inode *h_inode;
20007 +
20008 +       h_dentry = au_h_dptr(dentry, bindex);
20009 +       if (d_really_is_positive(dentry)) {
20010 +               err = -ENOENT;
20011 +               if (unlikely(d_is_negative(h_dentry)))
20012 +                       goto out;
20013 +               h_inode = d_inode(h_dentry);
20014 +               if (unlikely(!h_inode->i_nlink))
20015 +                       goto out;
20016 +
20017 +               h_mode = h_inode->i_mode;
20018 +               if (!isdir) {
20019 +                       err = -EISDIR;
20020 +                       if (unlikely(S_ISDIR(h_mode)))
20021 +                               goto out;
20022 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20023 +                       err = -ENOTDIR;
20024 +                       goto out;
20025 +               }
20026 +       } else {
20027 +               /* rename(2) case */
20028 +               err = -EIO;
20029 +               if (unlikely(d_is_positive(h_dentry)))
20030 +                       goto out;
20031 +       }
20032 +
20033 +       err = -ENOENT;
20034 +       /* expected parent dir is locked */
20035 +       if (unlikely(h_parent != h_dentry->d_parent))
20036 +               goto out;
20037 +       err = 0;
20038 +
20039 +       /*
20040 +        * rmdir a dir may break the consistency on some filesystem.
20041 +        * let's try heavy test.
20042 +        */
20043 +       err = -EACCES;
20044 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
20045 +                    && au_test_h_perm(d_inode(h_parent),
20046 +                                      MAY_EXEC | MAY_WRITE)))
20047 +               goto out;
20048 +
20049 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
20050 +       err = -EIO;
20051 +       if (IS_ERR(h_latest))
20052 +               goto out;
20053 +       if (h_latest == h_dentry)
20054 +               err = 0;
20055 +       dput(h_latest);
20056 +
20057 +out:
20058 +       return err;
20059 +}
20060 +
20061 +/*
20062 + * decide the branch where we operate for @dentry. the branch index will be set
20063 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
20064 + * dir for reverting.
20065 + * when a new whiteout is necessary, create it.
20066 + */
20067 +static struct dentry*
20068 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
20069 +                   struct au_dtime *dt, struct au_pin *pin)
20070 +{
20071 +       struct dentry *wh_dentry;
20072 +       struct super_block *sb;
20073 +       struct path h_path;
20074 +       int err, need_wh;
20075 +       unsigned int udba;
20076 +       aufs_bindex_t bcpup;
20077 +
20078 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
20079 +       wh_dentry = ERR_PTR(need_wh);
20080 +       if (unlikely(need_wh < 0))
20081 +               goto out;
20082 +
20083 +       sb = dentry->d_sb;
20084 +       udba = au_opt_udba(sb);
20085 +       bcpup = *rbcpup;
20086 +       err = au_pin(pin, dentry, bcpup, udba,
20087 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20088 +       wh_dentry = ERR_PTR(err);
20089 +       if (unlikely(err))
20090 +               goto out;
20091 +
20092 +       h_path.dentry = au_pinned_h_parent(pin);
20093 +       if (udba != AuOpt_UDBA_NONE
20094 +           && au_dbtop(dentry) == bcpup) {
20095 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
20096 +               wh_dentry = ERR_PTR(err);
20097 +               if (unlikely(err))
20098 +                       goto out_unpin;
20099 +       }
20100 +
20101 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
20102 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
20103 +       wh_dentry = NULL;
20104 +       if (!need_wh)
20105 +               goto out; /* success, no need to create whiteout */
20106 +
20107 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
20108 +       if (IS_ERR(wh_dentry))
20109 +               goto out_unpin;
20110 +
20111 +       /* returns with the parent is locked and wh_dentry is dget-ed */
20112 +       goto out; /* success */
20113 +
20114 +out_unpin:
20115 +       au_unpin(pin);
20116 +out:
20117 +       return wh_dentry;
20118 +}
20119 +
20120 +/*
20121 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
20122 + * in order to be revertible and save time for removing many child whiteouts
20123 + * under the dir.
20124 + * returns 1 when there are too many child whiteout and caller should remove
20125 + * them asynchronously. returns 0 when the number of children is enough small to
20126 + * remove now or the branch fs is a remote fs.
20127 + * otherwise return an error.
20128 + */
20129 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
20130 +                          struct au_nhash *whlist, struct inode *dir)
20131 +{
20132 +       int rmdir_later, err, dirwh;
20133 +       struct dentry *h_dentry;
20134 +       struct super_block *sb;
20135 +       struct inode *inode;
20136 +
20137 +       sb = dentry->d_sb;
20138 +       SiMustAnyLock(sb);
20139 +       h_dentry = au_h_dptr(dentry, bindex);
20140 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
20141 +       if (unlikely(err))
20142 +               goto out;
20143 +
20144 +       /* stop monitoring */
20145 +       inode = d_inode(dentry);
20146 +       au_hn_free(au_hi(inode, bindex));
20147 +
20148 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
20149 +               dirwh = au_sbi(sb)->si_dirwh;
20150 +               rmdir_later = (dirwh <= 1);
20151 +               if (!rmdir_later)
20152 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
20153 +                                                             dirwh);
20154 +               if (rmdir_later)
20155 +                       return rmdir_later;
20156 +       }
20157 +
20158 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
20159 +       if (unlikely(err)) {
20160 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
20161 +                       h_dentry, bindex, err);
20162 +               err = 0;
20163 +       }
20164 +
20165 +out:
20166 +       AuTraceErr(err);
20167 +       return err;
20168 +}
20169 +
20170 +/*
20171 + * final procedure for deleting a entry.
20172 + * maintain dentry and iattr.
20173 + */
20174 +static void epilog(struct inode *dir, struct dentry *dentry,
20175 +                  aufs_bindex_t bindex)
20176 +{
20177 +       struct inode *inode;
20178 +
20179 +       inode = d_inode(dentry);
20180 +       d_drop(dentry);
20181 +       inode->i_ctime = dir->i_ctime;
20182 +
20183 +       au_dir_ts(dir, bindex);
20184 +       dir->i_version++;
20185 +}
20186 +
20187 +/*
20188 + * when an error happened, remove the created whiteout and revert everything.
20189 + */
20190 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
20191 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
20192 +                    struct dentry *dentry, struct au_dtime *dt)
20193 +{
20194 +       int rerr;
20195 +       struct path h_path = {
20196 +               .dentry = wh_dentry,
20197 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
20198 +       };
20199 +
20200 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
20201 +       if (!rerr) {
20202 +               au_set_dbwh(dentry, bwh);
20203 +               au_dtime_revert(dt);
20204 +               return 0;
20205 +       }
20206 +
20207 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
20208 +       return -EIO;
20209 +}
20210 +
20211 +/* ---------------------------------------------------------------------- */
20212 +
20213 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
20214 +{
20215 +       int err;
20216 +       aufs_bindex_t bwh, bindex, btop;
20217 +       struct inode *inode, *h_dir, *delegated;
20218 +       struct dentry *parent, *wh_dentry;
20219 +       /* to reuduce stack size */
20220 +       struct {
20221 +               struct au_dtime dt;
20222 +               struct au_pin pin;
20223 +               struct path h_path;
20224 +       } *a;
20225 +
20226 +       IMustLock(dir);
20227 +
20228 +       err = -ENOMEM;
20229 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20230 +       if (unlikely(!a))
20231 +               goto out;
20232 +
20233 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20234 +       if (unlikely(err))
20235 +               goto out_free;
20236 +       err = au_d_hashed_positive(dentry);
20237 +       if (unlikely(err))
20238 +               goto out_unlock;
20239 +       inode = d_inode(dentry);
20240 +       IMustLock(inode);
20241 +       err = -EISDIR;
20242 +       if (unlikely(d_is_dir(dentry)))
20243 +               goto out_unlock; /* possible? */
20244 +
20245 +       btop = au_dbtop(dentry);
20246 +       bwh = au_dbwh(dentry);
20247 +       bindex = -1;
20248 +       parent = dentry->d_parent; /* dir inode is locked */
20249 +       di_write_lock_parent(parent);
20250 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
20251 +                                       &a->pin);
20252 +       err = PTR_ERR(wh_dentry);
20253 +       if (IS_ERR(wh_dentry))
20254 +               goto out_parent;
20255 +
20256 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
20257 +       a->h_path.dentry = au_h_dptr(dentry, btop);
20258 +       dget(a->h_path.dentry);
20259 +       if (bindex == btop) {
20260 +               h_dir = au_pinned_h_dir(&a->pin);
20261 +               delegated = NULL;
20262 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
20263 +               if (unlikely(err == -EWOULDBLOCK)) {
20264 +                       pr_warn("cannot retry for NFSv4 delegation"
20265 +                               " for an internal unlink\n");
20266 +                       iput(delegated);
20267 +               }
20268 +       } else {
20269 +               /* dir inode is locked */
20270 +               h_dir = d_inode(wh_dentry->d_parent);
20271 +               IMustLock(h_dir);
20272 +               err = 0;
20273 +       }
20274 +
20275 +       if (!err) {
20276 +               vfsub_drop_nlink(inode);
20277 +               epilog(dir, dentry, bindex);
20278 +
20279 +               /* update target timestamps */
20280 +               if (bindex == btop) {
20281 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
20282 +                       /*ignore*/
20283 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20284 +               } else
20285 +                       /* todo: this timestamp may be reverted later */
20286 +                       inode->i_ctime = h_dir->i_ctime;
20287 +               goto out_unpin; /* success */
20288 +       }
20289 +
20290 +       /* revert */
20291 +       if (wh_dentry) {
20292 +               int rerr;
20293 +
20294 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20295 +                                &a->dt);
20296 +               if (rerr)
20297 +                       err = rerr;
20298 +       }
20299 +
20300 +out_unpin:
20301 +       au_unpin(&a->pin);
20302 +       dput(wh_dentry);
20303 +       dput(a->h_path.dentry);
20304 +out_parent:
20305 +       di_write_unlock(parent);
20306 +out_unlock:
20307 +       aufs_read_unlock(dentry, AuLock_DW);
20308 +out_free:
20309 +       au_delayed_kfree(a);
20310 +out:
20311 +       return err;
20312 +}
20313 +
20314 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
20315 +{
20316 +       int err, rmdir_later;
20317 +       aufs_bindex_t bwh, bindex, btop;
20318 +       struct inode *inode;
20319 +       struct dentry *parent, *wh_dentry, *h_dentry;
20320 +       struct au_whtmp_rmdir *args;
20321 +       /* to reuduce stack size */
20322 +       struct {
20323 +               struct au_dtime dt;
20324 +               struct au_pin pin;
20325 +       } *a;
20326 +
20327 +       IMustLock(dir);
20328 +
20329 +       err = -ENOMEM;
20330 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20331 +       if (unlikely(!a))
20332 +               goto out;
20333 +
20334 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20335 +       if (unlikely(err))
20336 +               goto out_free;
20337 +       err = au_alive_dir(dentry);
20338 +       if (unlikely(err))
20339 +               goto out_unlock;
20340 +       inode = d_inode(dentry);
20341 +       IMustLock(inode);
20342 +       err = -ENOTDIR;
20343 +       if (unlikely(!d_is_dir(dentry)))
20344 +               goto out_unlock; /* possible? */
20345 +
20346 +       err = -ENOMEM;
20347 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20348 +       if (unlikely(!args))
20349 +               goto out_unlock;
20350 +
20351 +       parent = dentry->d_parent; /* dir inode is locked */
20352 +       di_write_lock_parent(parent);
20353 +       err = au_test_empty(dentry, &args->whlist);
20354 +       if (unlikely(err))
20355 +               goto out_parent;
20356 +
20357 +       btop = au_dbtop(dentry);
20358 +       bwh = au_dbwh(dentry);
20359 +       bindex = -1;
20360 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20361 +                                       &a->pin);
20362 +       err = PTR_ERR(wh_dentry);
20363 +       if (IS_ERR(wh_dentry))
20364 +               goto out_parent;
20365 +
20366 +       h_dentry = au_h_dptr(dentry, btop);
20367 +       dget(h_dentry);
20368 +       rmdir_later = 0;
20369 +       if (bindex == btop) {
20370 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
20371 +               if (err > 0) {
20372 +                       rmdir_later = err;
20373 +                       err = 0;
20374 +               }
20375 +       } else {
20376 +               /* stop monitoring */
20377 +               au_hn_free(au_hi(inode, btop));
20378 +
20379 +               /* dir inode is locked */
20380 +               IMustLock(d_inode(wh_dentry->d_parent));
20381 +               err = 0;
20382 +       }
20383 +
20384 +       if (!err) {
20385 +               vfsub_dead_dir(inode);
20386 +               au_set_dbdiropq(dentry, -1);
20387 +               epilog(dir, dentry, bindex);
20388 +
20389 +               if (rmdir_later) {
20390 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
20391 +                       args = NULL;
20392 +               }
20393 +
20394 +               goto out_unpin; /* success */
20395 +       }
20396 +
20397 +       /* revert */
20398 +       AuLabel(revert);
20399 +       if (wh_dentry) {
20400 +               int rerr;
20401 +
20402 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20403 +                                &a->dt);
20404 +               if (rerr)
20405 +                       err = rerr;
20406 +       }
20407 +
20408 +out_unpin:
20409 +       au_unpin(&a->pin);
20410 +       dput(wh_dentry);
20411 +       dput(h_dentry);
20412 +out_parent:
20413 +       di_write_unlock(parent);
20414 +       if (args)
20415 +               au_whtmp_rmdir_free(args);
20416 +out_unlock:
20417 +       aufs_read_unlock(dentry, AuLock_DW);
20418 +out_free:
20419 +       au_delayed_kfree(a);
20420 +out:
20421 +       AuTraceErr(err);
20422 +       return err;
20423 +}
20424 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20425 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
20426 +++ linux/fs/aufs/i_op_ren.c    2017-03-02 14:24:13.863589061 +0100
20427 @@ -0,0 +1,1165 @@
20428 +/*
20429 + * Copyright (C) 2005-2017 Junjiro R. Okajima
20430 + *
20431 + * This program, aufs is free software; you can redistribute it and/or modify
20432 + * it under the terms of the GNU General Public License as published by
20433 + * the Free Software Foundation; either version 2 of the License, or
20434 + * (at your option) any later version.
20435 + *
20436 + * This program is distributed in the hope that it will be useful,
20437 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20438 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20439 + * GNU General Public License for more details.
20440 + *
20441 + * You should have received a copy of the GNU General Public License
20442 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20443 + */
20444 +
20445 +/*
20446 + * inode operation (rename entry)
20447 + * todo: this is crazy monster
20448 + */
20449 +
20450 +#include "aufs.h"
20451 +
20452 +enum { AuSRC, AuDST, AuSrcDst };
20453 +enum { AuPARENT, AuCHILD, AuParentChild };
20454 +
20455 +#define AuRen_ISDIR_SRC                1
20456 +#define AuRen_ISDIR_DST                (1 << 1)
20457 +#define AuRen_ISSAMEDIR                (1 << 2)
20458 +#define AuRen_WHSRC            (1 << 3)
20459 +#define AuRen_WHDST            (1 << 4)
20460 +#define AuRen_MNT_WRITE                (1 << 5)
20461 +#define AuRen_DT_DSTDIR                (1 << 6)
20462 +#define AuRen_DIROPQ_SRC       (1 << 7)
20463 +#define AuRen_DIROPQ_DST       (1 << 8)
20464 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
20465 +#define au_fset_ren(flags, name) \
20466 +       do { (flags) |= AuRen_##name; } while (0)
20467 +#define au_fclr_ren(flags, name) \
20468 +       do { (flags) &= ~AuRen_##name; } while (0)
20469 +
20470 +struct au_ren_args {
20471 +       struct {
20472 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
20473 +                       *wh_dentry;
20474 +               struct inode *dir, *inode;
20475 +               struct au_hinode *hdir, *hinode;
20476 +               struct au_dtime dt[AuParentChild];
20477 +               aufs_bindex_t btop, bdiropq;
20478 +       } sd[AuSrcDst];
20479 +
20480 +#define src_dentry     sd[AuSRC].dentry
20481 +#define src_dir                sd[AuSRC].dir
20482 +#define src_inode      sd[AuSRC].inode
20483 +#define src_h_dentry   sd[AuSRC].h_dentry
20484 +#define src_parent     sd[AuSRC].parent
20485 +#define src_h_parent   sd[AuSRC].h_parent
20486 +#define src_wh_dentry  sd[AuSRC].wh_dentry
20487 +#define src_hdir       sd[AuSRC].hdir
20488 +#define src_hinode     sd[AuSRC].hinode
20489 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
20490 +#define src_dt         sd[AuSRC].dt
20491 +#define src_btop       sd[AuSRC].btop
20492 +#define src_bdiropq    sd[AuSRC].bdiropq
20493 +
20494 +#define dst_dentry     sd[AuDST].dentry
20495 +#define dst_dir                sd[AuDST].dir
20496 +#define dst_inode      sd[AuDST].inode
20497 +#define dst_h_dentry   sd[AuDST].h_dentry
20498 +#define dst_parent     sd[AuDST].parent
20499 +#define dst_h_parent   sd[AuDST].h_parent
20500 +#define dst_wh_dentry  sd[AuDST].wh_dentry
20501 +#define dst_hdir       sd[AuDST].hdir
20502 +#define dst_hinode     sd[AuDST].hinode
20503 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
20504 +#define dst_dt         sd[AuDST].dt
20505 +#define dst_btop       sd[AuDST].btop
20506 +#define dst_bdiropq    sd[AuDST].bdiropq
20507 +
20508 +       struct dentry *h_trap;
20509 +       struct au_branch *br;
20510 +       struct path h_path;
20511 +       struct au_nhash whlist;
20512 +       aufs_bindex_t btgt, src_bwh;
20513 +
20514 +       struct {
20515 +               unsigned short auren_flags;
20516 +               unsigned char flags;    /* syscall parameter */
20517 +               unsigned char exchange;
20518 +       } __packed;
20519 +
20520 +       struct au_whtmp_rmdir *thargs;
20521 +       struct dentry *h_dst;
20522 +};
20523 +
20524 +/* ---------------------------------------------------------------------- */
20525 +
20526 +/*
20527 + * functions for reverting.
20528 + * when an error happened in a single rename systemcall, we should revert
20529 + * everything as if nothing happened.
20530 + * we don't need to revert the copied-up/down the parent dir since they are
20531 + * harmless.
20532 + */
20533 +
20534 +#define RevertFailure(fmt, ...) do { \
20535 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20536 +               ##__VA_ARGS__, err, rerr); \
20537 +       err = -EIO; \
20538 +} while (0)
20539 +
20540 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
20541 +{
20542 +       int rerr;
20543 +       struct dentry *d;
20544 +#define src_or_dst(member) a->sd[idx].member
20545 +
20546 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
20547 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
20548 +       rerr = au_diropq_remove(d, a->btgt);
20549 +       au_hn_inode_unlock(src_or_dst(hinode));
20550 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
20551 +       if (rerr)
20552 +               RevertFailure("remove diropq %pd", d);
20553 +
20554 +#undef src_or_dst_
20555 +}
20556 +
20557 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
20558 +{
20559 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
20560 +               au_ren_do_rev_diropq(err, a, AuSRC);
20561 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
20562 +               au_ren_do_rev_diropq(err, a, AuDST);
20563 +}
20564 +
20565 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
20566 +{
20567 +       int rerr;
20568 +       struct inode *delegated;
20569 +
20570 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20571 +                                         a->src_h_parent);
20572 +       rerr = PTR_ERR(a->h_path.dentry);
20573 +       if (IS_ERR(a->h_path.dentry)) {
20574 +               RevertFailure("lkup one %pd", a->src_dentry);
20575 +               return;
20576 +       }
20577 +
20578 +       delegated = NULL;
20579 +       rerr = vfsub_rename(a->dst_h_dir,
20580 +                           au_h_dptr(a->src_dentry, a->btgt),
20581 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
20582 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20583 +               pr_warn("cannot retry for NFSv4 delegation"
20584 +                       " for an internal rename\n");
20585 +               iput(delegated);
20586 +       }
20587 +       d_drop(a->h_path.dentry);
20588 +       dput(a->h_path.dentry);
20589 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20590 +       if (rerr)
20591 +               RevertFailure("rename %pd", a->src_dentry);
20592 +}
20593 +
20594 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
20595 +{
20596 +       int rerr;
20597 +       struct inode *delegated;
20598 +
20599 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20600 +                                         a->dst_h_parent);
20601 +       rerr = PTR_ERR(a->h_path.dentry);
20602 +       if (IS_ERR(a->h_path.dentry)) {
20603 +               RevertFailure("lkup one %pd", a->dst_dentry);
20604 +               return;
20605 +       }
20606 +       if (d_is_positive(a->h_path.dentry)) {
20607 +               d_drop(a->h_path.dentry);
20608 +               dput(a->h_path.dentry);
20609 +               return;
20610 +       }
20611 +
20612 +       delegated = NULL;
20613 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20614 +                           &delegated, a->flags);
20615 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20616 +               pr_warn("cannot retry for NFSv4 delegation"
20617 +                       " for an internal rename\n");
20618 +               iput(delegated);
20619 +       }
20620 +       d_drop(a->h_path.dentry);
20621 +       dput(a->h_path.dentry);
20622 +       if (!rerr)
20623 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20624 +       else
20625 +               RevertFailure("rename %pd", a->h_dst);
20626 +}
20627 +
20628 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20629 +{
20630 +       int rerr;
20631 +
20632 +       a->h_path.dentry = a->src_wh_dentry;
20633 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
20634 +       au_set_dbwh(a->src_dentry, a->src_bwh);
20635 +       if (rerr)
20636 +               RevertFailure("unlink %pd", a->src_wh_dentry);
20637 +}
20638 +#undef RevertFailure
20639 +
20640 +/* ---------------------------------------------------------------------- */
20641 +
20642 +/*
20643 + * when we have to copyup the renaming entry, do it with the rename-target name
20644 + * in order to minimize the cost (the later actual rename is unnecessary).
20645 + * otherwise rename it on the target branch.
20646 + */
20647 +static int au_ren_or_cpup(struct au_ren_args *a)
20648 +{
20649 +       int err;
20650 +       struct dentry *d;
20651 +       struct inode *delegated;
20652 +
20653 +       d = a->src_dentry;
20654 +       if (au_dbtop(d) == a->btgt) {
20655 +               a->h_path.dentry = a->dst_h_dentry;
20656 +               AuDebugOn(au_dbtop(d) != a->btgt);
20657 +               delegated = NULL;
20658 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
20659 +                                  a->dst_h_dir, &a->h_path, &delegated,
20660 +                                  a->flags);
20661 +               if (unlikely(err == -EWOULDBLOCK)) {
20662 +                       pr_warn("cannot retry for NFSv4 delegation"
20663 +                               " for an internal rename\n");
20664 +                       iput(delegated);
20665 +               }
20666 +       } else
20667 +               BUG();
20668 +
20669 +       if (!err && a->h_dst)
20670 +               /* it will be set to dinfo later */
20671 +               dget(a->h_dst);
20672 +
20673 +       return err;
20674 +}
20675 +
20676 +/* cf. aufs_rmdir() */
20677 +static int au_ren_del_whtmp(struct au_ren_args *a)
20678 +{
20679 +       int err;
20680 +       struct inode *dir;
20681 +
20682 +       dir = a->dst_dir;
20683 +       SiMustAnyLock(dir->i_sb);
20684 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20685 +                                    au_sbi(dir->i_sb)->si_dirwh)
20686 +           || au_test_fs_remote(a->h_dst->d_sb)) {
20687 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20688 +               if (unlikely(err))
20689 +                       pr_warn("failed removing whtmp dir %pd (%d), "
20690 +                               "ignored.\n", a->h_dst, err);
20691 +       } else {
20692 +               au_nhash_wh_free(&a->thargs->whlist);
20693 +               a->thargs->whlist = a->whlist;
20694 +               a->whlist.nh_num = 0;
20695 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20696 +               dput(a->h_dst);
20697 +               a->thargs = NULL;
20698 +       }
20699 +
20700 +       return 0;
20701 +}
20702 +
20703 +/* make it 'opaque' dir. */
20704 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
20705 +{
20706 +       int err;
20707 +       struct dentry *d, *diropq;
20708 +#define src_or_dst(member) a->sd[idx].member
20709 +
20710 +       err = 0;
20711 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
20712 +       src_or_dst(bdiropq) = au_dbdiropq(d);
20713 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
20714 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
20715 +       diropq = au_diropq_create(d, a->btgt);
20716 +       au_hn_inode_unlock(src_or_dst(hinode));
20717 +       if (IS_ERR(diropq))
20718 +               err = PTR_ERR(diropq);
20719 +       else
20720 +               dput(diropq);
20721 +
20722 +#undef src_or_dst_
20723 +       return err;
20724 +}
20725 +
20726 +static int au_ren_diropq(struct au_ren_args *a)
20727 +{
20728 +       int err;
20729 +       unsigned char always;
20730 +       struct dentry *d;
20731 +
20732 +       err = 0;
20733 +       d = a->dst_dentry; /* already renamed on the branch */
20734 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
20735 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
20736 +           && a->btgt != au_dbdiropq(a->src_dentry)
20737 +           && (a->dst_wh_dentry
20738 +               || a->btgt <= au_dbdiropq(d)
20739 +               /* hide the lower to keep xino */
20740 +               /* the lowers may not be a dir, but we hide them anyway */
20741 +               || a->btgt < au_dbbot(d)
20742 +               || always)) {
20743 +               AuDbg("here\n");
20744 +               err = au_ren_do_diropq(a, AuSRC);
20745 +               if (unlikely(err))
20746 +                       goto out;
20747 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
20748 +       }
20749 +       if (!a->exchange)
20750 +               goto out; /* success */
20751 +
20752 +       d = a->src_dentry; /* already renamed on the branch */
20753 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
20754 +           && a->btgt != au_dbdiropq(a->dst_dentry)
20755 +           && (a->btgt < au_dbdiropq(d)
20756 +               || a->btgt < au_dbbot(d)
20757 +               || always)) {
20758 +               AuDbgDentry(a->src_dentry);
20759 +               AuDbgDentry(a->dst_dentry);
20760 +               err = au_ren_do_diropq(a, AuDST);
20761 +               if (unlikely(err))
20762 +                       goto out_rev_src;
20763 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
20764 +       }
20765 +       goto out; /* success */
20766 +
20767 +out_rev_src:
20768 +       AuDbg("err %d, reverting src\n", err);
20769 +       au_ren_rev_diropq(err, a);
20770 +out:
20771 +       return err;
20772 +}
20773 +
20774 +static int do_rename(struct au_ren_args *a)
20775 +{
20776 +       int err;
20777 +       struct dentry *d, *h_d;
20778 +
20779 +       if (!a->exchange) {
20780 +               /* prepare workqueue args for asynchronous rmdir */
20781 +               h_d = a->dst_h_dentry;
20782 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
20783 +                   && d_is_positive(h_d)) {
20784 +                       err = -ENOMEM;
20785 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
20786 +                                                        GFP_NOFS);
20787 +                       if (unlikely(!a->thargs))
20788 +                               goto out;
20789 +                       a->h_dst = dget(h_d);
20790 +               }
20791 +
20792 +               /* create whiteout for src_dentry */
20793 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
20794 +                       a->src_bwh = au_dbwh(a->src_dentry);
20795 +                       AuDebugOn(a->src_bwh >= 0);
20796 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
20797 +                                                       a->src_h_parent);
20798 +                       err = PTR_ERR(a->src_wh_dentry);
20799 +                       if (IS_ERR(a->src_wh_dentry))
20800 +                               goto out_thargs;
20801 +               }
20802 +
20803 +               /* lookup whiteout for dentry */
20804 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
20805 +                       h_d = au_wh_lkup(a->dst_h_parent,
20806 +                                        &a->dst_dentry->d_name, a->br);
20807 +                       err = PTR_ERR(h_d);
20808 +                       if (IS_ERR(h_d))
20809 +                               goto out_whsrc;
20810 +                       if (d_is_negative(h_d))
20811 +                               dput(h_d);
20812 +                       else
20813 +                               a->dst_wh_dentry = h_d;
20814 +               }
20815 +
20816 +               /* rename dentry to tmpwh */
20817 +               if (a->thargs) {
20818 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
20819 +                       if (unlikely(err))
20820 +                               goto out_whdst;
20821 +
20822 +                       d = a->dst_dentry;
20823 +                       au_set_h_dptr(d, a->btgt, NULL);
20824 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
20825 +                       if (unlikely(err))
20826 +                               goto out_whtmp;
20827 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
20828 +               }
20829 +       }
20830 +
20831 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
20832 +
20833 +       /* rename by vfs_rename or cpup */
20834 +       err = au_ren_or_cpup(a);
20835 +       if (unlikely(err))
20836 +               /* leave the copied-up one */
20837 +               goto out_whtmp;
20838 +
20839 +       /* make dir opaque */
20840 +       err = au_ren_diropq(a);
20841 +       if (unlikely(err))
20842 +               goto out_rename;
20843 +
20844 +       /* update target timestamps */
20845 +       if (a->exchange) {
20846 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
20847 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
20848 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
20849 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20850 +       }
20851 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
20852 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20853 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
20854 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20855 +
20856 +       if (!a->exchange) {
20857 +               /* remove whiteout for dentry */
20858 +               if (a->dst_wh_dentry) {
20859 +                       a->h_path.dentry = a->dst_wh_dentry;
20860 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20861 +                                                 a->dst_dentry);
20862 +                       if (unlikely(err))
20863 +                               goto out_diropq;
20864 +               }
20865 +
20866 +               /* remove whtmp */
20867 +               if (a->thargs)
20868 +                       au_ren_del_whtmp(a); /* ignore this error */
20869 +
20870 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
20871 +       }
20872 +       err = 0;
20873 +       goto out_success;
20874 +
20875 +out_diropq:
20876 +       au_ren_rev_diropq(err, a);
20877 +out_rename:
20878 +       au_ren_rev_rename(err, a);
20879 +       dput(a->h_dst);
20880 +out_whtmp:
20881 +       if (a->thargs)
20882 +               au_ren_rev_whtmp(err, a);
20883 +out_whdst:
20884 +       dput(a->dst_wh_dentry);
20885 +       a->dst_wh_dentry = NULL;
20886 +out_whsrc:
20887 +       if (a->src_wh_dentry)
20888 +               au_ren_rev_whsrc(err, a);
20889 +out_success:
20890 +       dput(a->src_wh_dentry);
20891 +       dput(a->dst_wh_dentry);
20892 +out_thargs:
20893 +       if (a->thargs) {
20894 +               dput(a->h_dst);
20895 +               au_whtmp_rmdir_free(a->thargs);
20896 +               a->thargs = NULL;
20897 +       }
20898 +out:
20899 +       return err;
20900 +}
20901 +
20902 +/* ---------------------------------------------------------------------- */
20903 +
20904 +/*
20905 + * test if @dentry dir can be rename destination or not.
20906 + * success means, it is a logically empty dir.
20907 + */
20908 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
20909 +{
20910 +       return au_test_empty(dentry, whlist);
20911 +}
20912 +
20913 +/*
20914 + * test if @dentry dir can be rename source or not.
20915 + * if it can, return 0 and @children is filled.
20916 + * success means,
20917 + * - it is a logically empty dir.
20918 + * - or, it exists on writable branch and has no children including whiteouts
20919 + *       on the lower branch.
20920 + */
20921 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20922 +{
20923 +       int err;
20924 +       unsigned int rdhash;
20925 +       aufs_bindex_t btop;
20926 +
20927 +       btop = au_dbtop(dentry);
20928 +       if (btop != btgt) {
20929 +               struct au_nhash whlist;
20930 +
20931 +               SiMustAnyLock(dentry->d_sb);
20932 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20933 +               if (!rdhash)
20934 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20935 +                                                          dentry));
20936 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20937 +               if (unlikely(err))
20938 +                       goto out;
20939 +               err = au_test_empty(dentry, &whlist);
20940 +               au_nhash_wh_free(&whlist);
20941 +               goto out;
20942 +       }
20943 +
20944 +       if (btop == au_dbtaildir(dentry))
20945 +               return 0; /* success */
20946 +
20947 +       err = au_test_empty_lower(dentry);
20948 +
20949 +out:
20950 +       if (err == -ENOTEMPTY) {
20951 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
20952 +                       " is not supported\n");
20953 +               err = -EXDEV;
20954 +       }
20955 +       return err;
20956 +}
20957 +
20958 +/* side effect: sets whlist and h_dentry */
20959 +static int au_ren_may_dir(struct au_ren_args *a)
20960 +{
20961 +       int err;
20962 +       unsigned int rdhash;
20963 +       struct dentry *d;
20964 +
20965 +       d = a->dst_dentry;
20966 +       SiMustAnyLock(d->d_sb);
20967 +
20968 +       err = 0;
20969 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
20970 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
20971 +               if (!rdhash)
20972 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20973 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20974 +               if (unlikely(err))
20975 +                       goto out;
20976 +
20977 +               if (!a->exchange) {
20978 +                       au_set_dbtop(d, a->dst_btop);
20979 +                       err = may_rename_dstdir(d, &a->whlist);
20980 +                       au_set_dbtop(d, a->btgt);
20981 +               } else
20982 +                       err = may_rename_srcdir(d, a->btgt);
20983 +       }
20984 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
20985 +       if (unlikely(err))
20986 +               goto out;
20987 +
20988 +       d = a->src_dentry;
20989 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
20990 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
20991 +               err = may_rename_srcdir(d, a->btgt);
20992 +               if (unlikely(err)) {
20993 +                       au_nhash_wh_free(&a->whlist);
20994 +                       a->whlist.nh_num = 0;
20995 +               }
20996 +       }
20997 +out:
20998 +       return err;
20999 +}
21000 +
21001 +/* ---------------------------------------------------------------------- */
21002 +
21003 +/*
21004 + * simple tests for rename.
21005 + * following the checks in vfs, plus the parent-child relationship.
21006 + */
21007 +static int au_may_ren(struct au_ren_args *a)
21008 +{
21009 +       int err, isdir;
21010 +       struct inode *h_inode;
21011 +
21012 +       if (a->src_btop == a->btgt) {
21013 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
21014 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
21015 +               if (unlikely(err))
21016 +                       goto out;
21017 +               err = -EINVAL;
21018 +               if (unlikely(a->src_h_dentry == a->h_trap))
21019 +                       goto out;
21020 +       }
21021 +
21022 +       err = 0;
21023 +       if (a->dst_btop != a->btgt)
21024 +               goto out;
21025 +
21026 +       err = -ENOTEMPTY;
21027 +       if (unlikely(a->dst_h_dentry == a->h_trap))
21028 +               goto out;
21029 +
21030 +       err = -EIO;
21031 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
21032 +       if (d_really_is_negative(a->dst_dentry)) {
21033 +               if (d_is_negative(a->dst_h_dentry))
21034 +                       err = au_may_add(a->dst_dentry, a->btgt,
21035 +                                        a->dst_h_parent, isdir);
21036 +       } else {
21037 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
21038 +                       goto out;
21039 +               h_inode = d_inode(a->dst_h_dentry);
21040 +               if (h_inode->i_nlink)
21041 +                       err = au_may_del(a->dst_dentry, a->btgt,
21042 +                                        a->dst_h_parent, isdir);
21043 +       }
21044 +
21045 +out:
21046 +       if (unlikely(err == -ENOENT || err == -EEXIST))
21047 +               err = -EIO;
21048 +       AuTraceErr(err);
21049 +       return err;
21050 +}
21051 +
21052 +/* ---------------------------------------------------------------------- */
21053 +
21054 +/*
21055 + * locking order
21056 + * (VFS)
21057 + * - src_dir and dir by lock_rename()
21058 + * - inode if exitsts
21059 + * (aufs)
21060 + * - lock all
21061 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
21062 + *     + si_read_lock
21063 + *     + di_write_lock2_child()
21064 + *       + di_write_lock_child()
21065 + *        + ii_write_lock_child()
21066 + *       + di_write_lock_child2()
21067 + *        + ii_write_lock_child2()
21068 + *     + src_parent and parent
21069 + *       + di_write_lock_parent()
21070 + *        + ii_write_lock_parent()
21071 + *       + di_write_lock_parent2()
21072 + *        + ii_write_lock_parent2()
21073 + *   + lower src_dir and dir by vfsub_lock_rename()
21074 + *   + verify the every relationships between child and parent. if any
21075 + *     of them failed, unlock all and return -EBUSY.
21076 + */
21077 +static void au_ren_unlock(struct au_ren_args *a)
21078 +{
21079 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
21080 +                           a->dst_h_parent, a->dst_hdir);
21081 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
21082 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
21083 +}
21084 +
21085 +static int au_ren_lock(struct au_ren_args *a)
21086 +{
21087 +       int err;
21088 +       unsigned int udba;
21089 +
21090 +       err = 0;
21091 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
21092 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
21093 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
21094 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
21095 +
21096 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
21097 +       if (unlikely(err))
21098 +               goto out;
21099 +       au_fset_ren(a->auren_flags, MNT_WRITE);
21100 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
21101 +                                     a->dst_h_parent, a->dst_hdir);
21102 +       udba = au_opt_udba(a->src_dentry->d_sb);
21103 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
21104 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
21105 +               err = au_busy_or_stale();
21106 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
21107 +               err = au_h_verify(a->src_h_dentry, udba,
21108 +                                 d_inode(a->src_h_parent), a->src_h_parent,
21109 +                                 a->br);
21110 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
21111 +               err = au_h_verify(a->dst_h_dentry, udba,
21112 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
21113 +                                 a->br);
21114 +       if (!err)
21115 +               goto out; /* success */
21116 +
21117 +       err = au_busy_or_stale();
21118 +       au_ren_unlock(a);
21119 +
21120 +out:
21121 +       return err;
21122 +}
21123 +
21124 +/* ---------------------------------------------------------------------- */
21125 +
21126 +static void au_ren_refresh_dir(struct au_ren_args *a)
21127 +{
21128 +       struct inode *dir;
21129 +
21130 +       dir = a->dst_dir;
21131 +       dir->i_version++;
21132 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
21133 +               /* is this updating defined in POSIX? */
21134 +               au_cpup_attr_timesizes(a->src_inode);
21135 +               au_cpup_attr_nlink(dir, /*force*/1);
21136 +       }
21137 +       au_dir_ts(dir, a->btgt);
21138 +
21139 +       if (a->exchange) {
21140 +               dir = a->src_dir;
21141 +               dir->i_version++;
21142 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
21143 +                       /* is this updating defined in POSIX? */
21144 +                       au_cpup_attr_timesizes(a->dst_inode);
21145 +                       au_cpup_attr_nlink(dir, /*force*/1);
21146 +               }
21147 +               au_dir_ts(dir, a->btgt);
21148 +       }
21149 +
21150 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
21151 +               return;
21152 +
21153 +       dir = a->src_dir;
21154 +       dir->i_version++;
21155 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
21156 +               au_cpup_attr_nlink(dir, /*force*/1);
21157 +       au_dir_ts(dir, a->btgt);
21158 +}
21159 +
21160 +static void au_ren_refresh(struct au_ren_args *a)
21161 +{
21162 +       aufs_bindex_t bbot, bindex;
21163 +       struct dentry *d, *h_d;
21164 +       struct inode *i, *h_i;
21165 +       struct super_block *sb;
21166 +
21167 +       d = a->dst_dentry;
21168 +       d_drop(d);
21169 +       if (a->h_dst)
21170 +               /* already dget-ed by au_ren_or_cpup() */
21171 +               au_set_h_dptr(d, a->btgt, a->h_dst);
21172 +
21173 +       i = a->dst_inode;
21174 +       if (i) {
21175 +               if (!a->exchange) {
21176 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
21177 +                               vfsub_drop_nlink(i);
21178 +                       else {
21179 +                               vfsub_dead_dir(i);
21180 +                               au_cpup_attr_timesizes(i);
21181 +                       }
21182 +                       au_update_dbrange(d, /*do_put_zero*/1);
21183 +               } else
21184 +                       au_cpup_attr_nlink(i, /*force*/1);
21185 +       } else {
21186 +               bbot = a->btgt;
21187 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
21188 +                       au_set_h_dptr(d, bindex, NULL);
21189 +               bbot = au_dbbot(d);
21190 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
21191 +                       au_set_h_dptr(d, bindex, NULL);
21192 +               au_update_dbrange(d, /*do_put_zero*/0);
21193 +       }
21194 +
21195 +       d = a->src_dentry;
21196 +       if (!a->exchange) {
21197 +               au_set_dbwh(d, -1);
21198 +               bbot = au_dbbot(d);
21199 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
21200 +                       h_d = au_h_dptr(d, bindex);
21201 +                       if (h_d)
21202 +                               au_set_h_dptr(d, bindex, NULL);
21203 +               }
21204 +               au_set_dbbot(d, a->btgt);
21205 +
21206 +               sb = d->d_sb;
21207 +               i = a->src_inode;
21208 +               if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
21209 +                       return; /* success */
21210 +
21211 +               bbot = au_ibbot(i);
21212 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
21213 +                       h_i = au_h_iptr(i, bindex);
21214 +                       if (h_i) {
21215 +                               au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
21216 +                               /* ignore this error */
21217 +                               au_set_h_iptr(i, bindex, NULL, 0);
21218 +                       }
21219 +               }
21220 +               au_set_ibbot(i, a->btgt);
21221 +       }
21222 +               d_drop(a->src_dentry);
21223 +}
21224 +
21225 +/* ---------------------------------------------------------------------- */
21226 +
21227 +/* mainly for link(2) and rename(2) */
21228 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
21229 +{
21230 +       aufs_bindex_t bdiropq, bwh;
21231 +       struct dentry *parent;
21232 +       struct au_branch *br;
21233 +
21234 +       parent = dentry->d_parent;
21235 +       IMustLock(d_inode(parent)); /* dir is locked */
21236 +
21237 +       bdiropq = au_dbdiropq(parent);
21238 +       bwh = au_dbwh(dentry);
21239 +       br = au_sbr(dentry->d_sb, btgt);
21240 +       if (au_br_rdonly(br)
21241 +           || (0 <= bdiropq && bdiropq < btgt)
21242 +           || (0 <= bwh && bwh < btgt))
21243 +               btgt = -1;
21244 +
21245 +       AuDbg("btgt %d\n", btgt);
21246 +       return btgt;
21247 +}
21248 +
21249 +/* sets src_btop, dst_btop and btgt */
21250 +static int au_ren_wbr(struct au_ren_args *a)
21251 +{
21252 +       int err;
21253 +       struct au_wr_dir_args wr_dir_args = {
21254 +               /* .force_btgt  = -1, */
21255 +               .flags          = AuWrDir_ADD_ENTRY
21256 +       };
21257 +
21258 +       a->src_btop = au_dbtop(a->src_dentry);
21259 +       a->dst_btop = au_dbtop(a->dst_dentry);
21260 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
21261 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
21262 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21263 +       wr_dir_args.force_btgt = a->src_btop;
21264 +       if (a->dst_inode && a->dst_btop < a->src_btop)
21265 +               wr_dir_args.force_btgt = a->dst_btop;
21266 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
21267 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
21268 +       a->btgt = err;
21269 +       if (a->exchange)
21270 +               au_update_dbtop(a->dst_dentry);
21271 +
21272 +       return err;
21273 +}
21274 +
21275 +static void au_ren_dt(struct au_ren_args *a)
21276 +{
21277 +       a->h_path.dentry = a->src_h_parent;
21278 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
21279 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
21280 +               a->h_path.dentry = a->dst_h_parent;
21281 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
21282 +       }
21283 +
21284 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
21285 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
21286 +           && !a->exchange)
21287 +               return;
21288 +
21289 +       a->h_path.dentry = a->src_h_dentry;
21290 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
21291 +       if (d_is_positive(a->dst_h_dentry)) {
21292 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
21293 +               a->h_path.dentry = a->dst_h_dentry;
21294 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
21295 +       }
21296 +}
21297 +
21298 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
21299 +{
21300 +       struct dentry *h_d;
21301 +       struct inode *h_inode;
21302 +
21303 +       au_dtime_revert(a->src_dt + AuPARENT);
21304 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
21305 +               au_dtime_revert(a->dst_dt + AuPARENT);
21306 +
21307 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
21308 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
21309 +               h_inode = d_inode(h_d);
21310 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21311 +               au_dtime_revert(a->src_dt + AuCHILD);
21312 +               inode_unlock(h_inode);
21313 +
21314 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
21315 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
21316 +                       h_inode = d_inode(h_d);
21317 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
21318 +                       au_dtime_revert(a->dst_dt + AuCHILD);
21319 +                       inode_unlock(h_inode);
21320 +               }
21321 +       }
21322 +}
21323 +
21324 +/* ---------------------------------------------------------------------- */
21325 +
21326 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
21327 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
21328 +               unsigned int _flags)
21329 +{
21330 +       int err, lock_flags;
21331 +       /* reduce stack space */
21332 +       struct au_ren_args *a;
21333 +       struct au_pin pin;
21334 +
21335 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
21336 +       IMustLock(_src_dir);
21337 +       IMustLock(_dst_dir);
21338 +
21339 +       err = -EINVAL;
21340 +       if (unlikely(_flags & RENAME_WHITEOUT))
21341 +               goto out;
21342 +
21343 +       err = -ENOMEM;
21344 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
21345 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21346 +       if (unlikely(!a))
21347 +               goto out;
21348 +
21349 +       a->flags = _flags;
21350 +       a->exchange = _flags & RENAME_EXCHANGE;
21351 +       a->src_dir = _src_dir;
21352 +       a->src_dentry = _src_dentry;
21353 +       a->src_inode = NULL;
21354 +       if (d_really_is_positive(a->src_dentry))
21355 +               a->src_inode = d_inode(a->src_dentry);
21356 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
21357 +       a->dst_dir = _dst_dir;
21358 +       a->dst_dentry = _dst_dentry;
21359 +       a->dst_inode = NULL;
21360 +       if (d_really_is_positive(a->dst_dentry))
21361 +               a->dst_inode = d_inode(a->dst_dentry);
21362 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
21363 +       if (a->dst_inode) {
21364 +               /*
21365 +                * if EXCHANGE && src is non-dir && dst is dir,
21366 +                * dst is not locked.
21367 +                */
21368 +               /* IMustLock(a->dst_inode); */
21369 +               au_igrab(a->dst_inode);
21370 +       }
21371 +
21372 +       err = -ENOTDIR;
21373 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
21374 +       if (d_is_dir(a->src_dentry)) {
21375 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
21376 +               if (unlikely(!a->exchange
21377 +                            && d_really_is_positive(a->dst_dentry)
21378 +                            && !d_is_dir(a->dst_dentry)))
21379 +                       goto out_free;
21380 +               lock_flags |= AuLock_DIRS;
21381 +       }
21382 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
21383 +               au_fset_ren(a->auren_flags, ISDIR_DST);
21384 +               if (unlikely(!a->exchange
21385 +                            && d_really_is_positive(a->src_dentry)
21386 +                            && !d_is_dir(a->src_dentry)))
21387 +                       goto out_free;
21388 +               lock_flags |= AuLock_DIRS;
21389 +       }
21390 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, lock_flags);
21391 +       if (unlikely(err))
21392 +               goto out_free;
21393 +
21394 +       err = au_d_hashed_positive(a->src_dentry);
21395 +       if (unlikely(err))
21396 +               goto out_unlock;
21397 +       err = -ENOENT;
21398 +       if (a->dst_inode) {
21399 +               /*
21400 +                * If it is a dir, VFS unhash it before this
21401 +                * function. It means we cannot rely upon d_unhashed().
21402 +                */
21403 +               if (unlikely(!a->dst_inode->i_nlink))
21404 +                       goto out_unlock;
21405 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
21406 +                       err = au_d_hashed_positive(a->dst_dentry);
21407 +                       if (unlikely(err && !a->exchange))
21408 +                               goto out_unlock;
21409 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
21410 +                       goto out_unlock;
21411 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
21412 +               goto out_unlock;
21413 +
21414 +       /*
21415 +        * is it possible?
21416 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
21417 +        * there may exist a problem somewhere else.
21418 +        */
21419 +       err = -EINVAL;
21420 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
21421 +               goto out_unlock;
21422 +
21423 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
21424 +       di_write_lock_parent(a->dst_parent);
21425 +
21426 +       /* which branch we process */
21427 +       err = au_ren_wbr(a);
21428 +       if (unlikely(err < 0))
21429 +               goto out_parent;
21430 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
21431 +       a->h_path.mnt = au_br_mnt(a->br);
21432 +
21433 +       /* are they available to be renamed */
21434 +       err = au_ren_may_dir(a);
21435 +       if (unlikely(err))
21436 +               goto out_children;
21437 +
21438 +       /* prepare the writable parent dir on the same branch */
21439 +       if (a->dst_btop == a->btgt) {
21440 +               au_fset_ren(a->auren_flags, WHDST);
21441 +       } else {
21442 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
21443 +               if (unlikely(err))
21444 +                       goto out_children;
21445 +       }
21446 +
21447 +       err = 0;
21448 +       if (!a->exchange) {
21449 +               if (a->src_dir != a->dst_dir) {
21450 +                       /*
21451 +                        * this temporary unlock is safe,
21452 +                        * because both dir->i_mutex are locked.
21453 +                        */
21454 +                       di_write_unlock(a->dst_parent);
21455 +                       di_write_lock_parent(a->src_parent);
21456 +                       err = au_wr_dir_need_wh(a->src_dentry,
21457 +                                               au_ftest_ren(a->auren_flags,
21458 +                                                            ISDIR_SRC),
21459 +                                               &a->btgt);
21460 +                       di_write_unlock(a->src_parent);
21461 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
21462 +                                             /*isdir*/1);
21463 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
21464 +               } else
21465 +                       err = au_wr_dir_need_wh(a->src_dentry,
21466 +                                               au_ftest_ren(a->auren_flags,
21467 +                                                            ISDIR_SRC),
21468 +                                               &a->btgt);
21469 +       }
21470 +       if (unlikely(err < 0))
21471 +               goto out_children;
21472 +       if (err)
21473 +               au_fset_ren(a->auren_flags, WHSRC);
21474 +
21475 +       /* cpup src */
21476 +       if (a->src_btop != a->btgt) {
21477 +               err = au_pin(&pin, a->src_dentry, a->btgt,
21478 +                            au_opt_udba(a->src_dentry->d_sb),
21479 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21480 +               if (!err) {
21481 +                       struct au_cp_generic cpg = {
21482 +                               .dentry = a->src_dentry,
21483 +                               .bdst   = a->btgt,
21484 +                               .bsrc   = a->src_btop,
21485 +                               .len    = -1,
21486 +                               .pin    = &pin,
21487 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21488 +                       };
21489 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
21490 +                       err = au_sio_cpup_simple(&cpg);
21491 +                       au_unpin(&pin);
21492 +               }
21493 +               if (unlikely(err))
21494 +                       goto out_children;
21495 +               a->src_btop = a->btgt;
21496 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21497 +               if (!a->exchange)
21498 +                       au_fset_ren(a->auren_flags, WHSRC);
21499 +       }
21500 +
21501 +       /* cpup dst */
21502 +       if (a->exchange && a->dst_inode
21503 +           && a->dst_btop != a->btgt) {
21504 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
21505 +                            au_opt_udba(a->dst_dentry->d_sb),
21506 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21507 +               if (!err) {
21508 +                       struct au_cp_generic cpg = {
21509 +                               .dentry = a->dst_dentry,
21510 +                               .bdst   = a->btgt,
21511 +                               .bsrc   = a->dst_btop,
21512 +                               .len    = -1,
21513 +                               .pin    = &pin,
21514 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21515 +                       };
21516 +                       err = au_sio_cpup_simple(&cpg);
21517 +                       au_unpin(&pin);
21518 +               }
21519 +               if (unlikely(err))
21520 +                       goto out_children;
21521 +               a->dst_btop = a->btgt;
21522 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
21523 +       }
21524 +
21525 +       /* lock them all */
21526 +       err = au_ren_lock(a);
21527 +       if (unlikely(err))
21528 +               /* leave the copied-up one */
21529 +               goto out_children;
21530 +
21531 +       if (!a->exchange) {
21532 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21533 +                       err = au_may_ren(a);
21534 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21535 +                       err = -ENAMETOOLONG;
21536 +               if (unlikely(err))
21537 +                       goto out_hdir;
21538 +       }
21539 +
21540 +       /* store timestamps to be revertible */
21541 +       au_ren_dt(a);
21542 +
21543 +       /* here we go */
21544 +       err = do_rename(a);
21545 +       if (unlikely(err))
21546 +               goto out_dt;
21547 +
21548 +       /* update dir attributes */
21549 +       au_ren_refresh_dir(a);
21550 +
21551 +       /* dput/iput all lower dentries */
21552 +       au_ren_refresh(a);
21553 +
21554 +       goto out_hdir; /* success */
21555 +
21556 +out_dt:
21557 +       au_ren_rev_dt(err, a);
21558 +out_hdir:
21559 +       au_ren_unlock(a);
21560 +out_children:
21561 +       au_nhash_wh_free(&a->whlist);
21562 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
21563 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
21564 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
21565 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
21566 +       }
21567 +out_parent:
21568 +       if (!err) {
21569 +               if (!a->exchange)
21570 +                       d_move(a->src_dentry, a->dst_dentry);
21571 +               else
21572 +                       d_exchange(a->src_dentry, a->dst_dentry);
21573 +       } else {
21574 +               au_update_dbtop(a->dst_dentry);
21575 +               if (!a->dst_inode)
21576 +                       d_drop(a->dst_dentry);
21577 +       }
21578 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
21579 +               di_write_unlock(a->dst_parent);
21580 +       else
21581 +               di_write_unlock2(a->src_parent, a->dst_parent);
21582 +out_unlock:
21583 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
21584 +out_free:
21585 +       iput(a->dst_inode);
21586 +       if (a->thargs)
21587 +               au_whtmp_rmdir_free(a->thargs);
21588 +       au_delayed_kfree(a);
21589 +out:
21590 +       AuTraceErr(err);
21591 +       return err;
21592 +}
21593 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21594 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
21595 +++ linux/fs/aufs/Kconfig       2016-10-09 16:55:36.482701377 +0200
21596 @@ -0,0 +1,185 @@
21597 +config AUFS_FS
21598 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
21599 +       help
21600 +       Aufs is a stackable unification filesystem such as Unionfs,
21601 +       which unifies several directories and provides a merged single
21602 +       directory.
21603 +       In the early days, aufs was entirely re-designed and
21604 +       re-implemented Unionfs Version 1.x series. Introducing many
21605 +       original ideas, approaches and improvements, it becomes totally
21606 +       different from Unionfs while keeping the basic features.
21607 +
21608 +if AUFS_FS
21609 +choice
21610 +       prompt "Maximum number of branches"
21611 +       default AUFS_BRANCH_MAX_127
21612 +       help
21613 +       Specifies the maximum number of branches (or member directories)
21614 +       in a single aufs. The larger value consumes more system
21615 +       resources and has a minor impact to performance.
21616 +config AUFS_BRANCH_MAX_127
21617 +       bool "127"
21618 +       help
21619 +       Specifies the maximum number of branches (or member directories)
21620 +       in a single aufs. The larger value consumes more system
21621 +       resources and has a minor impact to performance.
21622 +config AUFS_BRANCH_MAX_511
21623 +       bool "511"
21624 +       help
21625 +       Specifies the maximum number of branches (or member directories)
21626 +       in a single aufs. The larger value consumes more system
21627 +       resources and has a minor impact to performance.
21628 +config AUFS_BRANCH_MAX_1023
21629 +       bool "1023"
21630 +       help
21631 +       Specifies the maximum number of branches (or member directories)
21632 +       in a single aufs. The larger value consumes more system
21633 +       resources and has a minor impact to performance.
21634 +config AUFS_BRANCH_MAX_32767
21635 +       bool "32767"
21636 +       help
21637 +       Specifies the maximum number of branches (or member directories)
21638 +       in a single aufs. The larger value consumes more system
21639 +       resources and has a minor impact to performance.
21640 +endchoice
21641 +
21642 +config AUFS_SBILIST
21643 +       bool
21644 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
21645 +       default y
21646 +       help
21647 +       Automatic configuration for internal use.
21648 +       When aufs supports Magic SysRq or /proc, enabled automatically.
21649 +
21650 +config AUFS_HNOTIFY
21651 +       bool "Detect direct branch access (bypassing aufs)"
21652 +       help
21653 +       If you want to modify files on branches directly, eg. bypassing aufs,
21654 +       and want aufs to detect the changes of them fully, then enable this
21655 +       option and use 'udba=notify' mount option.
21656 +       Currently there is only one available configuration, "fsnotify".
21657 +       It will have a negative impact to the performance.
21658 +       See detail in aufs.5.
21659 +
21660 +choice
21661 +       prompt "method" if AUFS_HNOTIFY
21662 +       default AUFS_HFSNOTIFY
21663 +config AUFS_HFSNOTIFY
21664 +       bool "fsnotify"
21665 +       select FSNOTIFY
21666 +endchoice
21667 +
21668 +config AUFS_EXPORT
21669 +       bool "NFS-exportable aufs"
21670 +       depends on EXPORTFS
21671 +       help
21672 +       If you want to export your mounted aufs via NFS, then enable this
21673 +       option. There are several requirements for this configuration.
21674 +       See detail in aufs.5.
21675 +
21676 +config AUFS_INO_T_64
21677 +       bool
21678 +       depends on AUFS_EXPORT
21679 +       depends on 64BIT && !(ALPHA || S390)
21680 +       default y
21681 +       help
21682 +       Automatic configuration for internal use.
21683 +       /* typedef unsigned long/int __kernel_ino_t */
21684 +       /* alpha and s390x are int */
21685 +
21686 +config AUFS_XATTR
21687 +       bool "support for XATTR/EA (including Security Labels)"
21688 +       help
21689 +       If your branch fs supports XATTR/EA and you want to make them
21690 +       available in aufs too, then enable this opsion and specify the
21691 +       branch attributes for EA.
21692 +       See detail in aufs.5.
21693 +
21694 +config AUFS_FHSM
21695 +       bool "File-based Hierarchical Storage Management"
21696 +       help
21697 +       Hierarchical Storage Management (or HSM) is a well-known feature
21698 +       in the storage world. Aufs provides this feature as file-based.
21699 +       with multiple branches.
21700 +       These multiple branches are prioritized, ie. the topmost one
21701 +       should be the fastest drive and be used heavily.
21702 +
21703 +config AUFS_RDU
21704 +       bool "Readdir in userspace"
21705 +       help
21706 +       Aufs has two methods to provide a merged view for a directory,
21707 +       by a user-space library and by kernel-space natively. The latter
21708 +       is always enabled but sometimes large and slow.
21709 +       If you enable this option, install the library in aufs2-util
21710 +       package, and set some environment variables for your readdir(3),
21711 +       then the work will be handled in user-space which generally
21712 +       shows better performance in most cases.
21713 +       See detail in aufs.5.
21714 +
21715 +config AUFS_SHWH
21716 +       bool "Show whiteouts"
21717 +       help
21718 +       If you want to make the whiteouts in aufs visible, then enable
21719 +       this option and specify 'shwh' mount option. Although it may
21720 +       sounds like philosophy or something, but in technically it
21721 +       simply shows the name of whiteout with keeping its behaviour.
21722 +
21723 +config AUFS_BR_RAMFS
21724 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
21725 +       help
21726 +       If you want to use ramfs as an aufs branch fs, then enable this
21727 +       option. Generally tmpfs is recommended.
21728 +       Aufs prohibited them to be a branch fs by default, because
21729 +       initramfs becomes unusable after switch_root or something
21730 +       generally. If you sets initramfs as an aufs branch and boot your
21731 +       system by switch_root, you will meet a problem easily since the
21732 +       files in initramfs may be inaccessible.
21733 +       Unless you are going to use ramfs as an aufs branch fs without
21734 +       switch_root or something, leave it N.
21735 +
21736 +config AUFS_BR_FUSE
21737 +       bool "Fuse fs as an aufs branch"
21738 +       depends on FUSE_FS
21739 +       select AUFS_POLL
21740 +       help
21741 +       If you want to use fuse-based userspace filesystem as an aufs
21742 +       branch fs, then enable this option.
21743 +       It implements the internal poll(2) operation which is
21744 +       implemented by fuse only (curretnly).
21745 +
21746 +config AUFS_POLL
21747 +       bool
21748 +       help
21749 +       Automatic configuration for internal use.
21750 +
21751 +config AUFS_BR_HFSPLUS
21752 +       bool "Hfsplus as an aufs branch"
21753 +       depends on HFSPLUS_FS
21754 +       default y
21755 +       help
21756 +       If you want to use hfsplus fs as an aufs branch fs, then enable
21757 +       this option. This option introduces a small overhead at
21758 +       copying-up a file on hfsplus.
21759 +
21760 +config AUFS_BDEV_LOOP
21761 +       bool
21762 +       depends on BLK_DEV_LOOP
21763 +       default y
21764 +       help
21765 +       Automatic configuration for internal use.
21766 +       Convert =[ym] into =y.
21767 +
21768 +config AUFS_DEBUG
21769 +       bool "Debug aufs"
21770 +       help
21771 +       Enable this to compile aufs internal debug code.
21772 +       It will have a negative impact to the performance.
21773 +
21774 +config AUFS_MAGIC_SYSRQ
21775 +       bool
21776 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
21777 +       default y
21778 +       help
21779 +       Automatic configuration for internal use.
21780 +       When aufs supports Magic SysRq, enabled automatically.
21781 +endif
21782 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21783 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
21784 +++ linux/fs/aufs/loop.c        2017-03-02 14:24:13.863589061 +0100
21785 @@ -0,0 +1,147 @@
21786 +/*
21787 + * Copyright (C) 2005-2017 Junjiro R. Okajima
21788 + *
21789 + * This program, aufs is free software; you can redistribute it and/or modify
21790 + * it under the terms of the GNU General Public License as published by
21791 + * the Free Software Foundation; either version 2 of the License, or
21792 + * (at your option) any later version.
21793 + *
21794 + * This program is distributed in the hope that it will be useful,
21795 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21796 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21797 + * GNU General Public License for more details.
21798 + *
21799 + * You should have received a copy of the GNU General Public License
21800 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21801 + */
21802 +
21803 +/*
21804 + * support for loopback block device as a branch
21805 + */
21806 +
21807 +#include "aufs.h"
21808 +
21809 +/* added into drivers/block/loop.c */
21810 +static struct file *(*backing_file_func)(struct super_block *sb);
21811 +
21812 +/*
21813 + * test if two lower dentries have overlapping branches.
21814 + */
21815 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
21816 +{
21817 +       struct super_block *h_sb;
21818 +       struct file *backing_file;
21819 +
21820 +       if (unlikely(!backing_file_func)) {
21821 +               /* don't load "loop" module here */
21822 +               backing_file_func = symbol_get(loop_backing_file);
21823 +               if (unlikely(!backing_file_func))
21824 +                       /* "loop" module is not loaded */
21825 +                       return 0;
21826 +       }
21827 +
21828 +       h_sb = h_adding->d_sb;
21829 +       backing_file = backing_file_func(h_sb);
21830 +       if (!backing_file)
21831 +               return 0;
21832 +
21833 +       h_adding = backing_file->f_path.dentry;
21834 +       /*
21835 +        * h_adding can be local NFS.
21836 +        * in this case aufs cannot detect the loop.
21837 +        */
21838 +       if (unlikely(h_adding->d_sb == sb))
21839 +               return 1;
21840 +       return !!au_test_subdir(h_adding, sb->s_root);
21841 +}
21842 +
21843 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21844 +int au_test_loopback_kthread(void)
21845 +{
21846 +       int ret;
21847 +       struct task_struct *tsk = current;
21848 +       char c, comm[sizeof(tsk->comm)];
21849 +
21850 +       ret = 0;
21851 +       if (tsk->flags & PF_KTHREAD) {
21852 +               get_task_comm(comm, tsk);
21853 +               c = comm[4];
21854 +               ret = ('0' <= c && c <= '9'
21855 +                      && !strncmp(comm, "loop", 4));
21856 +       }
21857 +
21858 +       return ret;
21859 +}
21860 +
21861 +/* ---------------------------------------------------------------------- */
21862 +
21863 +#define au_warn_loopback_step  16
21864 +static int au_warn_loopback_nelem = au_warn_loopback_step;
21865 +static unsigned long *au_warn_loopback_array;
21866 +
21867 +void au_warn_loopback(struct super_block *h_sb)
21868 +{
21869 +       int i, new_nelem;
21870 +       unsigned long *a, magic;
21871 +       static DEFINE_SPINLOCK(spin);
21872 +
21873 +       magic = h_sb->s_magic;
21874 +       spin_lock(&spin);
21875 +       a = au_warn_loopback_array;
21876 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21877 +               if (a[i] == magic) {
21878 +                       spin_unlock(&spin);
21879 +                       return;
21880 +               }
21881 +
21882 +       /* h_sb is new to us, print it */
21883 +       if (i < au_warn_loopback_nelem) {
21884 +               a[i] = magic;
21885 +               goto pr;
21886 +       }
21887 +
21888 +       /* expand the array */
21889 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21890 +       a = au_kzrealloc(au_warn_loopback_array,
21891 +                        au_warn_loopback_nelem * sizeof(unsigned long),
21892 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
21893 +                        /*may_shrink*/0);
21894 +       if (a) {
21895 +               au_warn_loopback_nelem = new_nelem;
21896 +               au_warn_loopback_array = a;
21897 +               a[i] = magic;
21898 +               goto pr;
21899 +       }
21900 +
21901 +       spin_unlock(&spin);
21902 +       AuWarn1("realloc failed, ignored\n");
21903 +       return;
21904 +
21905 +pr:
21906 +       spin_unlock(&spin);
21907 +       pr_warn("you may want to try another patch for loopback file "
21908 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
21909 +}
21910 +
21911 +int au_loopback_init(void)
21912 +{
21913 +       int err;
21914 +       struct super_block *sb __maybe_unused;
21915 +
21916 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
21917 +
21918 +       err = 0;
21919 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21920 +                                        sizeof(unsigned long), GFP_NOFS);
21921 +       if (unlikely(!au_warn_loopback_array))
21922 +               err = -ENOMEM;
21923 +
21924 +       return err;
21925 +}
21926 +
21927 +void au_loopback_fin(void)
21928 +{
21929 +       if (backing_file_func)
21930 +               symbol_put(loop_backing_file);
21931 +       au_delayed_kfree(au_warn_loopback_array);
21932 +}
21933 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21934 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
21935 +++ linux/fs/aufs/loop.h        2017-03-02 14:24:13.863589061 +0100
21936 @@ -0,0 +1,52 @@
21937 +/*
21938 + * Copyright (C) 2005-2017 Junjiro R. Okajima
21939 + *
21940 + * This program, aufs is free software; you can redistribute it and/or modify
21941 + * it under the terms of the GNU General Public License as published by
21942 + * the Free Software Foundation; either version 2 of the License, or
21943 + * (at your option) any later version.
21944 + *
21945 + * This program is distributed in the hope that it will be useful,
21946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21948 + * GNU General Public License for more details.
21949 + *
21950 + * You should have received a copy of the GNU General Public License
21951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21952 + */
21953 +
21954 +/*
21955 + * support for loopback mount as a branch
21956 + */
21957 +
21958 +#ifndef __AUFS_LOOP_H__
21959 +#define __AUFS_LOOP_H__
21960 +
21961 +#ifdef __KERNEL__
21962 +
21963 +struct dentry;
21964 +struct super_block;
21965 +
21966 +#ifdef CONFIG_AUFS_BDEV_LOOP
21967 +/* drivers/block/loop.c */
21968 +struct file *loop_backing_file(struct super_block *sb);
21969 +
21970 +/* loop.c */
21971 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
21972 +int au_test_loopback_kthread(void);
21973 +void au_warn_loopback(struct super_block *h_sb);
21974 +
21975 +int au_loopback_init(void);
21976 +void au_loopback_fin(void);
21977 +#else
21978 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
21979 +          struct dentry *h_adding)
21980 +AuStubInt0(au_test_loopback_kthread, void)
21981 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21982 +
21983 +AuStubInt0(au_loopback_init, void)
21984 +AuStubVoid(au_loopback_fin, void)
21985 +#endif /* BLK_DEV_LOOP */
21986 +
21987 +#endif /* __KERNEL__ */
21988 +#endif /* __AUFS_LOOP_H__ */
21989 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21990 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
21991 +++ linux/fs/aufs/magic.mk      2016-10-09 16:55:36.492701639 +0200
21992 @@ -0,0 +1,30 @@
21993 +
21994 +# defined in ${srctree}/fs/fuse/inode.c
21995 +# tristate
21996 +ifdef CONFIG_FUSE_FS
21997 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21998 +endif
21999 +
22000 +# defined in ${srctree}/fs/xfs/xfs_sb.h
22001 +# tristate
22002 +ifdef CONFIG_XFS_FS
22003 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
22004 +endif
22005 +
22006 +# defined in ${srctree}/fs/configfs/mount.c
22007 +# tristate
22008 +ifdef CONFIG_CONFIGFS_FS
22009 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
22010 +endif
22011 +
22012 +# defined in ${srctree}/fs/ubifs/ubifs.h
22013 +# tristate
22014 +ifdef CONFIG_UBIFS_FS
22015 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
22016 +endif
22017 +
22018 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
22019 +# tristate
22020 +ifdef CONFIG_HFSPLUS_FS
22021 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
22022 +endif
22023 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
22024 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
22025 +++ linux/fs/aufs/Makefile      2016-10-09 16:55:36.486034798 +0200
22026 @@ -0,0 +1,44 @@
22027 +
22028 +include ${src}/magic.mk
22029 +ifeq (${CONFIG_AUFS_FS},m)
22030 +include ${src}/conf.mk
22031 +endif
22032 +-include ${src}/priv_def.mk
22033 +
22034 +# cf. include/linux/kernel.h
22035 +# enable pr_debug
22036 +ccflags-y += -DDEBUG
22037 +# sparse requires the full pathname
22038 +ifdef M
22039 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
22040 +else
22041 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
22042 +endif
22043 +
22044 +obj-$(CONFIG_AUFS_FS) += aufs.o
22045 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
22046 +       wkq.o vfsub.o dcsub.o \
22047 +       cpup.o whout.o wbr_policy.o \
22048 +       dinfo.o dentry.o \
22049 +       dynop.o \
22050 +       finfo.o file.o f_op.o \
22051 +       dir.o vdir.o \
22052 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
22053 +       mvdown.o ioctl.o
22054 +
22055 +# all are boolean
22056 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
22057 +aufs-$(CONFIG_SYSFS) += sysfs.o
22058 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
22059 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
22060 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
22061 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
22062 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
22063 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
22064 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
22065 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
22066 +aufs-$(CONFIG_AUFS_POLL) += poll.o
22067 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
22068 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
22069 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
22070 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
22071 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
22072 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
22073 +++ linux/fs/aufs/module.c      2017-03-02 14:24:13.863589061 +0100
22074 @@ -0,0 +1,333 @@
22075 +/*
22076 + * Copyright (C) 2005-2017 Junjiro R. Okajima
22077 + *
22078 + * This program, aufs is free software; you can redistribute it and/or modify
22079 + * it under the terms of the GNU General Public License as published by
22080 + * the Free Software Foundation; either version 2 of the License, or
22081 + * (at your option) any later version.
22082 + *
22083 + * This program is distributed in the hope that it will be useful,
22084 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22085 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22086 + * GNU General Public License for more details.
22087 + *
22088 + * You should have received a copy of the GNU General Public License
22089 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22090 + */
22091 +
22092 +/*
22093 + * module global variables and operations
22094 + */
22095 +
22096 +#include <linux/module.h>
22097 +#include <linux/seq_file.h>
22098 +#include "aufs.h"
22099 +
22100 +/* shrinkable realloc */
22101 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
22102 +{
22103 +       size_t sz;
22104 +       int diff;
22105 +
22106 +       sz = 0;
22107 +       diff = -1;
22108 +       if (p) {
22109 +#if 0 /* unused */
22110 +               if (!new_sz) {
22111 +                       au_delayed_kfree(p);
22112 +                       p = NULL;
22113 +                       goto out;
22114 +               }
22115 +#else
22116 +               AuDebugOn(!new_sz);
22117 +#endif
22118 +               sz = ksize(p);
22119 +               diff = au_kmidx_sub(sz, new_sz);
22120 +       }
22121 +       if (sz && !diff)
22122 +               goto out;
22123 +
22124 +       if (sz < new_sz)
22125 +               /* expand or SLOB */
22126 +               p = krealloc(p, new_sz, gfp);
22127 +       else if (new_sz < sz && may_shrink) {
22128 +               /* shrink */
22129 +               void *q;
22130 +
22131 +               q = kmalloc(new_sz, gfp);
22132 +               if (q) {
22133 +                       if (p) {
22134 +                               memcpy(q, p, new_sz);
22135 +                               au_delayed_kfree(p);
22136 +                       }
22137 +                       p = q;
22138 +               } else
22139 +                       p = NULL;
22140 +       }
22141 +
22142 +out:
22143 +       return p;
22144 +}
22145 +
22146 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
22147 +                  int may_shrink)
22148 +{
22149 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
22150 +       if (p && new_sz > nused)
22151 +               memset(p + nused, 0, new_sz - nused);
22152 +       return p;
22153 +}
22154 +
22155 +/* ---------------------------------------------------------------------- */
22156 +/*
22157 + * aufs caches
22158 + */
22159 +
22160 +struct au_dfree au_dfree;
22161 +
22162 +/* delayed free */
22163 +static void au_do_dfree(struct work_struct *work __maybe_unused)
22164 +{
22165 +       struct llist_head *head;
22166 +       struct llist_node *node, *next;
22167 +
22168 +#define AU_CACHE_DFREE_DO_BODY(name, idx, lnode) do {                  \
22169 +               head = &au_dfree.cache[AuCache_##idx].llist;            \
22170 +               node = llist_del_all(head);                             \
22171 +               for (; node; node = next) {                             \
22172 +                       struct au_##name *p                             \
22173 +                               = llist_entry(node, struct au_##name,   \
22174 +                                             lnode);                   \
22175 +                       next = llist_next(node);                        \
22176 +                       au_cache_free_##name(p);                        \
22177 +               }                                                       \
22178 +       } while (0)
22179 +
22180 +       AU_CACHE_DFREE_DO_BODY(dinfo, DINFO, di_lnode);
22181 +       AU_CACHE_DFREE_DO_BODY(icntnr, ICNTNR, lnode);
22182 +       AU_CACHE_DFREE_DO_BODY(finfo, FINFO, fi_lnode);
22183 +       AU_CACHE_DFREE_DO_BODY(vdir, VDIR, vd_lnode);
22184 +       AU_CACHE_DFREE_DO_BODY(vdir_dehstr, DEHSTR, lnode);
22185 +#ifdef CONFIG_AUFS_HNOTIFY
22186 +       AU_CACHE_DFREE_DO_BODY(hnotify, HNOTIFY, hn_lnode);
22187 +#endif
22188 +
22189 +#define AU_DFREE_DO_BODY(llist, func) do {             \
22190 +               node = llist_del_all(llist);            \
22191 +               for (; node; node = next) {             \
22192 +                       next = llist_next(node);        \
22193 +                       func(node);                     \
22194 +               }                                       \
22195 +       } while (0)
22196 +
22197 +       AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_KFREE, kfree);
22198 +       AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_FREE_PAGE, au_free_page);
22199 +
22200 +#undef AU_CACHE_DFREE_DO_BODY
22201 +#undef AU_DFREE_DO_BODY
22202 +}
22203 +
22204 +AU_CACHE_DFREE_FUNC(dinfo, DINFO, di_lnode);
22205 +AU_CACHE_DFREE_FUNC(icntnr, ICNTNR, lnode);
22206 +AU_CACHE_DFREE_FUNC(finfo, FINFO, fi_lnode);
22207 +AU_CACHE_DFREE_FUNC(vdir, VDIR, vd_lnode);
22208 +AU_CACHE_DFREE_FUNC(vdir_dehstr, DEHSTR, lnode);
22209 +
22210 +static void au_cache_fin(void)
22211 +{
22212 +       int i;
22213 +       struct au_cache *cp;
22214 +
22215 +       /*
22216 +        * Make sure all delayed rcu free inodes are flushed before we
22217 +        * destroy cache.
22218 +        */
22219 +       rcu_barrier();
22220 +
22221 +       /* excluding AuCache_HNOTIFY */
22222 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
22223 +       flush_delayed_work(&au_dfree.dwork);
22224 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
22225 +               cp = au_dfree.cache + i;
22226 +               AuDebugOn(!llist_empty(&cp->llist));
22227 +               kmem_cache_destroy(cp->cache);
22228 +               cp->cache = NULL;
22229 +       }
22230 +}
22231 +
22232 +static int __init au_cache_init(void)
22233 +{
22234 +       struct au_cache *cp;
22235 +
22236 +       cp = au_dfree.cache;
22237 +       cp[AuCache_DINFO].cache = AuCacheCtor(au_dinfo, au_di_init_once);
22238 +       if (cp[AuCache_DINFO].cache)
22239 +               /* SLAB_DESTROY_BY_RCU */
22240 +               cp[AuCache_ICNTNR].cache = AuCacheCtor(au_icntnr,
22241 +                                                      au_icntnr_init_once);
22242 +       if (cp[AuCache_ICNTNR].cache)
22243 +               cp[AuCache_FINFO].cache = AuCacheCtor(au_finfo,
22244 +                                                     au_fi_init_once);
22245 +       if (cp[AuCache_FINFO].cache)
22246 +               cp[AuCache_VDIR].cache = AuCache(au_vdir);
22247 +       if (cp[AuCache_VDIR].cache)
22248 +               cp[AuCache_DEHSTR].cache = AuCache(au_vdir_dehstr);
22249 +       if (cp[AuCache_DEHSTR].cache)
22250 +               return 0;
22251 +
22252 +       au_cache_fin();
22253 +       return -ENOMEM;
22254 +}
22255 +
22256 +/* ---------------------------------------------------------------------- */
22257 +
22258 +int au_dir_roflags;
22259 +
22260 +#ifdef CONFIG_AUFS_SBILIST
22261 +/*
22262 + * iterate_supers_type() doesn't protect us from
22263 + * remounting (branch management)
22264 + */
22265 +struct au_sphlhead au_sbilist;
22266 +#endif
22267 +
22268 +/*
22269 + * functions for module interface.
22270 + */
22271 +MODULE_LICENSE("GPL");
22272 +/* MODULE_LICENSE("GPL v2"); */
22273 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
22274 +MODULE_DESCRIPTION(AUFS_NAME
22275 +       " -- Advanced multi layered unification filesystem");
22276 +MODULE_VERSION(AUFS_VERSION);
22277 +MODULE_ALIAS_FS(AUFS_NAME);
22278 +
22279 +/* this module parameter has no meaning when SYSFS is disabled */
22280 +int sysaufs_brs = 1;
22281 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
22282 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
22283 +
22284 +/* this module parameter has no meaning when USER_NS is disabled */
22285 +bool au_userns;
22286 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
22287 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
22288 +
22289 +/* ---------------------------------------------------------------------- */
22290 +
22291 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
22292 +
22293 +int au_seq_path(struct seq_file *seq, struct path *path)
22294 +{
22295 +       int err;
22296 +
22297 +       err = seq_path(seq, path, au_esc_chars);
22298 +       if (err > 0)
22299 +               err = 0;
22300 +       else if (err < 0)
22301 +               err = -ENOMEM;
22302 +
22303 +       return err;
22304 +}
22305 +
22306 +/* ---------------------------------------------------------------------- */
22307 +
22308 +static int __init aufs_init(void)
22309 +{
22310 +       int err, i;
22311 +       char *p;
22312 +       struct au_cache *cp;
22313 +
22314 +       p = au_esc_chars;
22315 +       for (i = 1; i <= ' '; i++)
22316 +               *p++ = i;
22317 +       *p++ = '\\';
22318 +       *p++ = '\x7f';
22319 +       *p = 0;
22320 +
22321 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
22322 +
22323 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
22324 +       for (i = 0; i < AuIop_Last; i++)
22325 +               aufs_iop_nogetattr[i].getattr = NULL;
22326 +
22327 +       /* First, initialize au_dfree */
22328 +       for (i = 0; i < AuCache_Last; i++) {    /* including hnotify */
22329 +               cp = au_dfree.cache + i;
22330 +               cp->cache = NULL;
22331 +               init_llist_head(&cp->llist);
22332 +       }
22333 +       for (i = 0; i < AU_DFREE_Last; i++)
22334 +               init_llist_head(au_dfree.llist + i);
22335 +       INIT_DELAYED_WORK(&au_dfree.dwork, au_do_dfree);
22336 +
22337 +       au_sbilist_init();
22338 +       sysaufs_brs_init();
22339 +       au_debug_init();
22340 +       au_dy_init();
22341 +       err = sysaufs_init();
22342 +       if (unlikely(err))
22343 +               goto out;
22344 +       err = au_procfs_init();
22345 +       if (unlikely(err))
22346 +               goto out_sysaufs;
22347 +       err = au_wkq_init();
22348 +       if (unlikely(err))
22349 +               goto out_procfs;
22350 +       err = au_loopback_init();
22351 +       if (unlikely(err))
22352 +               goto out_wkq;
22353 +       err = au_hnotify_init();
22354 +       if (unlikely(err))
22355 +               goto out_loopback;
22356 +       err = au_sysrq_init();
22357 +       if (unlikely(err))
22358 +               goto out_hin;
22359 +       err = au_cache_init();
22360 +       if (unlikely(err))
22361 +               goto out_sysrq;
22362 +
22363 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
22364 +       err = register_filesystem(&aufs_fs_type);
22365 +       if (unlikely(err))
22366 +               goto out_cache;
22367 +
22368 +       /* since we define pr_fmt, call printk directly */
22369 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
22370 +       goto out; /* success */
22371 +
22372 +out_cache:
22373 +       au_cache_fin();
22374 +out_sysrq:
22375 +       au_sysrq_fin();
22376 +out_hin:
22377 +       au_hnotify_fin();
22378 +out_loopback:
22379 +       au_loopback_fin();
22380 +out_wkq:
22381 +       au_wkq_fin();
22382 +out_procfs:
22383 +       au_procfs_fin();
22384 +out_sysaufs:
22385 +       sysaufs_fin();
22386 +       au_dy_fin();
22387 +       flush_delayed_work(&au_dfree.dwork);
22388 +out:
22389 +       return err;
22390 +}
22391 +
22392 +static void __exit aufs_exit(void)
22393 +{
22394 +       unregister_filesystem(&aufs_fs_type);
22395 +       au_cache_fin();
22396 +       au_sysrq_fin();
22397 +       au_hnotify_fin();
22398 +       au_loopback_fin();
22399 +       au_wkq_fin();
22400 +       au_procfs_fin();
22401 +       sysaufs_fin();
22402 +       au_dy_fin();
22403 +       flush_delayed_work(&au_dfree.dwork);
22404 +}
22405 +
22406 +module_init(aufs_init);
22407 +module_exit(aufs_exit);
22408 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
22409 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
22410 +++ linux/fs/aufs/module.h      2017-03-02 14:24:13.863589061 +0100
22411 @@ -0,0 +1,156 @@
22412 +/*
22413 + * Copyright (C) 2005-2017 Junjiro R. Okajima
22414 + *
22415 + * This program, aufs is free software; you can redistribute it and/or modify
22416 + * it under the terms of the GNU General Public License as published by
22417 + * the Free Software Foundation; either version 2 of the License, or
22418 + * (at your option) any later version.
22419 + *
22420 + * This program is distributed in the hope that it will be useful,
22421 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22422 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22423 + * GNU General Public License for more details.
22424 + *
22425 + * You should have received a copy of the GNU General Public License
22426 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22427 + */
22428 +
22429 +/*
22430 + * module initialization and module-global
22431 + */
22432 +
22433 +#ifndef __AUFS_MODULE_H__
22434 +#define __AUFS_MODULE_H__
22435 +
22436 +#ifdef __KERNEL__
22437 +
22438 +#include <linux/slab.h>
22439 +#include "debug.h"
22440 +
22441 +struct path;
22442 +struct seq_file;
22443 +
22444 +/* module parameters */
22445 +extern int sysaufs_brs;
22446 +extern bool au_userns;
22447 +
22448 +/* ---------------------------------------------------------------------- */
22449 +
22450 +extern int au_dir_roflags;
22451 +
22452 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
22453 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
22454 +                  int may_shrink);
22455 +
22456 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
22457 +{
22458 +#ifndef CONFIG_SLOB
22459 +       return kmalloc_index(sz) - kmalloc_index(new_sz);
22460 +#else
22461 +       return -1; /* SLOB is untested */
22462 +#endif
22463 +}
22464 +
22465 +int au_seq_path(struct seq_file *seq, struct path *path);
22466 +
22467 +#ifdef CONFIG_PROC_FS
22468 +/* procfs.c */
22469 +int __init au_procfs_init(void);
22470 +void au_procfs_fin(void);
22471 +#else
22472 +AuStubInt0(au_procfs_init, void);
22473 +AuStubVoid(au_procfs_fin, void);
22474 +#endif
22475 +
22476 +/* ---------------------------------------------------------------------- */
22477 +
22478 +/* kmem cache and delayed free */
22479 +enum {
22480 +       AuCache_DINFO,
22481 +       AuCache_ICNTNR,
22482 +       AuCache_FINFO,
22483 +       AuCache_VDIR,
22484 +       AuCache_DEHSTR,
22485 +       AuCache_HNOTIFY, /* must be last */
22486 +       AuCache_Last
22487 +};
22488 +
22489 +enum {
22490 +       AU_DFREE_KFREE,
22491 +       AU_DFREE_FREE_PAGE,
22492 +       AU_DFREE_Last
22493 +};
22494 +
22495 +struct au_cache {
22496 +       struct kmem_cache       *cache;
22497 +       struct llist_head       llist;  /* delayed free */
22498 +};
22499 +
22500 +/*
22501 + * in order to reduce the cost of the internal timer, consolidate all the
22502 + * delayed free works into a single delayed_work.
22503 + */
22504 +struct au_dfree {
22505 +       struct au_cache         cache[AuCache_Last];
22506 +       struct llist_head       llist[AU_DFREE_Last];
22507 +       struct delayed_work     dwork;
22508 +};
22509 +
22510 +extern struct au_dfree au_dfree;
22511 +
22512 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
22513 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
22514 +#define AuCacheCtor(type, ctor)        \
22515 +       kmem_cache_create(#type, sizeof(struct type), \
22516 +                         __alignof__(struct type), AuCacheFlags, ctor)
22517 +
22518 +#define AU_DFREE_DELAY         msecs_to_jiffies(10)
22519 +#define AU_DFREE_BODY(lnode, llist) do {                               \
22520 +               if (llist_add(lnode, llist))                            \
22521 +                       schedule_delayed_work(&au_dfree.dwork,          \
22522 +                                             AU_DFREE_DELAY);          \
22523 +       } while (0)
22524 +#define AU_CACHE_DFREE_FUNC(name, idx, lnode)                          \
22525 +       void au_cache_dfree_##name(struct au_##name *p)                 \
22526 +       {                                                               \
22527 +               struct au_cache *cp = au_dfree.cache + AuCache_##idx;   \
22528 +               AU_DFREE_BODY(&p->lnode, &cp->llist);                   \
22529 +       }
22530 +
22531 +#define AuCacheFuncs(name, index) \
22532 +static inline struct au_##name *au_cache_alloc_##name(void) \
22533 +{ return kmem_cache_alloc(au_dfree.cache[AuCache_##index].cache, GFP_NOFS); } \
22534 +static inline void au_cache_free_##name(struct au_##name *p) \
22535 +{ kmem_cache_free(au_dfree.cache[AuCache_##index].cache, p); } \
22536 +void au_cache_dfree_##name(struct au_##name *p)
22537 +
22538 +AuCacheFuncs(dinfo, DINFO);
22539 +AuCacheFuncs(icntnr, ICNTNR);
22540 +AuCacheFuncs(finfo, FINFO);
22541 +AuCacheFuncs(vdir, VDIR);
22542 +AuCacheFuncs(vdir_dehstr, DEHSTR);
22543 +#ifdef CONFIG_AUFS_HNOTIFY
22544 +AuCacheFuncs(hnotify, HNOTIFY);
22545 +#endif
22546 +
22547 +static inline void au_delayed_kfree(const void *p)
22548 +{
22549 +       AuDebugOn(!p);
22550 +       AuDebugOn(ksize(p) < sizeof(struct llist_node));
22551 +
22552 +       AU_DFREE_BODY((void *)p, au_dfree.llist + AU_DFREE_KFREE);
22553 +}
22554 +
22555 +/* cast only */
22556 +static inline void au_free_page(void *p)
22557 +{
22558 +       free_page((unsigned long)p);
22559 +}
22560 +
22561 +static inline void au_delayed_free_page(unsigned long addr)
22562 +{
22563 +       AU_DFREE_BODY((void *)addr, au_dfree.llist + AU_DFREE_FREE_PAGE);
22564 +}
22565 +
22566 +#endif /* __KERNEL__ */
22567 +#endif /* __AUFS_MODULE_H__ */
22568 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
22569 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
22570 +++ linux/fs/aufs/mvdown.c      2017-03-02 14:24:13.863589061 +0100
22571 @@ -0,0 +1,704 @@
22572 +/*
22573 + * Copyright (C) 2011-2017 Junjiro R. Okajima
22574 + *
22575 + * This program, aufs is free software; you can redistribute it and/or modify
22576 + * it under the terms of the GNU General Public License as published by
22577 + * the Free Software Foundation; either version 2 of the License, or
22578 + * (at your option) any later version.
22579 + *
22580 + * This program is distributed in the hope that it will be useful,
22581 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22582 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22583 + * GNU General Public License for more details.
22584 + *
22585 + * You should have received a copy of the GNU General Public License
22586 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22587 + */
22588 +
22589 +/*
22590 + * move-down, opposite of copy-up
22591 + */
22592 +
22593 +#include "aufs.h"
22594 +
22595 +struct au_mvd_args {
22596 +       struct {
22597 +               struct super_block *h_sb;
22598 +               struct dentry *h_parent;
22599 +               struct au_hinode *hdir;
22600 +               struct inode *h_dir, *h_inode;
22601 +               struct au_pin pin;
22602 +       } info[AUFS_MVDOWN_NARRAY];
22603 +
22604 +       struct aufs_mvdown mvdown;
22605 +       struct dentry *dentry, *parent;
22606 +       struct inode *inode, *dir;
22607 +       struct super_block *sb;
22608 +       aufs_bindex_t bopq, bwh, bfound;
22609 +       unsigned char rename_lock;
22610 +};
22611 +
22612 +#define mvd_errno              mvdown.au_errno
22613 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
22614 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
22615 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
22616 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
22617 +
22618 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
22619 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
22620 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
22621 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
22622 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
22623 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
22624 +
22625 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
22626 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
22627 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
22628 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
22629 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
22630 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
22631 +
22632 +#define AU_MVD_PR(flag, ...) do {                      \
22633 +               if (flag)                               \
22634 +                       pr_err(__VA_ARGS__);            \
22635 +       } while (0)
22636 +
22637 +static int find_lower_writable(struct au_mvd_args *a)
22638 +{
22639 +       struct super_block *sb;
22640 +       aufs_bindex_t bindex, bbot;
22641 +       struct au_branch *br;
22642 +
22643 +       sb = a->sb;
22644 +       bindex = a->mvd_bsrc;
22645 +       bbot = au_sbbot(sb);
22646 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
22647 +               for (bindex++; bindex <= bbot; bindex++) {
22648 +                       br = au_sbr(sb, bindex);
22649 +                       if (au_br_fhsm(br->br_perm)
22650 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
22651 +                               return bindex;
22652 +               }
22653 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
22654 +               for (bindex++; bindex <= bbot; bindex++) {
22655 +                       br = au_sbr(sb, bindex);
22656 +                       if (!au_br_rdonly(br))
22657 +                               return bindex;
22658 +               }
22659 +       else
22660 +               for (bindex++; bindex <= bbot; bindex++) {
22661 +                       br = au_sbr(sb, bindex);
22662 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
22663 +                               if (au_br_rdonly(br))
22664 +                                       a->mvdown.flags
22665 +                                               |= AUFS_MVDOWN_ROLOWER_R;
22666 +                               return bindex;
22667 +                       }
22668 +               }
22669 +
22670 +       return -1;
22671 +}
22672 +
22673 +/* make the parent dir on bdst */
22674 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
22675 +{
22676 +       int err;
22677 +
22678 +       err = 0;
22679 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22680 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22681 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22682 +       a->mvd_h_dst_parent = NULL;
22683 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
22684 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22685 +       if (!a->mvd_h_dst_parent) {
22686 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22687 +               if (unlikely(err)) {
22688 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
22689 +                       goto out;
22690 +               }
22691 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22692 +       }
22693 +
22694 +out:
22695 +       AuTraceErr(err);
22696 +       return err;
22697 +}
22698 +
22699 +/* lock them all */
22700 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
22701 +{
22702 +       int err;
22703 +       struct dentry *h_trap;
22704 +
22705 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22706 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
22707 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22708 +                    au_opt_udba(a->sb),
22709 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22710 +       AuTraceErr(err);
22711 +       if (unlikely(err)) {
22712 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
22713 +               goto out;
22714 +       }
22715 +
22716 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22717 +               a->rename_lock = 0;
22718 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22719 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22720 +                           au_opt_udba(a->sb),
22721 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22722 +               err = au_do_pin(&a->mvd_pin_src);
22723 +               AuTraceErr(err);
22724 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22725 +               if (unlikely(err)) {
22726 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
22727 +                       goto out_dst;
22728 +               }
22729 +               goto out; /* success */
22730 +       }
22731 +
22732 +       a->rename_lock = 1;
22733 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
22734 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22735 +                    au_opt_udba(a->sb),
22736 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22737 +       AuTraceErr(err);
22738 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22739 +       if (unlikely(err)) {
22740 +               AU_MVD_PR(dmsg, "pin_src failed\n");
22741 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22742 +               goto out_dst;
22743 +       }
22744 +       au_pin_hdir_unlock(&a->mvd_pin_src);
22745 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22746 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
22747 +       if (h_trap) {
22748 +               err = (h_trap != a->mvd_h_src_parent);
22749 +               if (err)
22750 +                       err = (h_trap != a->mvd_h_dst_parent);
22751 +       }
22752 +       BUG_ON(err); /* it should never happen */
22753 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22754 +               err = -EBUSY;
22755 +               AuTraceErr(err);
22756 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22757 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22758 +               au_pin_hdir_lock(&a->mvd_pin_src);
22759 +               au_unpin(&a->mvd_pin_src);
22760 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22761 +               goto out_dst;
22762 +       }
22763 +       goto out; /* success */
22764 +
22765 +out_dst:
22766 +       au_unpin(&a->mvd_pin_dst);
22767 +out:
22768 +       AuTraceErr(err);
22769 +       return err;
22770 +}
22771 +
22772 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
22773 +{
22774 +       if (!a->rename_lock)
22775 +               au_unpin(&a->mvd_pin_src);
22776 +       else {
22777 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22778 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22779 +               au_pin_hdir_lock(&a->mvd_pin_src);
22780 +               au_unpin(&a->mvd_pin_src);
22781 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22782 +       }
22783 +       au_unpin(&a->mvd_pin_dst);
22784 +}
22785 +
22786 +/* copy-down the file */
22787 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
22788 +{
22789 +       int err;
22790 +       struct au_cp_generic cpg = {
22791 +               .dentry = a->dentry,
22792 +               .bdst   = a->mvd_bdst,
22793 +               .bsrc   = a->mvd_bsrc,
22794 +               .len    = -1,
22795 +               .pin    = &a->mvd_pin_dst,
22796 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
22797 +       };
22798 +
22799 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
22800 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22801 +               au_fset_cpup(cpg.flags, OVERWRITE);
22802 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22803 +               au_fset_cpup(cpg.flags, RWDST);
22804 +       err = au_sio_cpdown_simple(&cpg);
22805 +       if (unlikely(err))
22806 +               AU_MVD_PR(dmsg, "cpdown failed\n");
22807 +
22808 +       AuTraceErr(err);
22809 +       return err;
22810 +}
22811 +
22812 +/*
22813 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
22814 + * were sleeping
22815 + */
22816 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
22817 +{
22818 +       int err;
22819 +       struct path h_path;
22820 +       struct au_branch *br;
22821 +       struct inode *delegated;
22822 +
22823 +       br = au_sbr(a->sb, a->mvd_bdst);
22824 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22825 +       err = PTR_ERR(h_path.dentry);
22826 +       if (IS_ERR(h_path.dentry)) {
22827 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
22828 +               goto out;
22829 +       }
22830 +
22831 +       err = 0;
22832 +       if (d_is_positive(h_path.dentry)) {
22833 +               h_path.mnt = au_br_mnt(br);
22834 +               delegated = NULL;
22835 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
22836 +                                  &delegated, /*force*/0);
22837 +               if (unlikely(err == -EWOULDBLOCK)) {
22838 +                       pr_warn("cannot retry for NFSv4 delegation"
22839 +                               " for an internal unlink\n");
22840 +                       iput(delegated);
22841 +               }
22842 +               if (unlikely(err))
22843 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
22844 +       }
22845 +       dput(h_path.dentry);
22846 +
22847 +out:
22848 +       AuTraceErr(err);
22849 +       return err;
22850 +}
22851 +
22852 +/*
22853 + * unlink the topmost h_dentry
22854 + */
22855 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
22856 +{
22857 +       int err;
22858 +       struct path h_path;
22859 +       struct inode *delegated;
22860 +
22861 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22862 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
22863 +       delegated = NULL;
22864 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22865 +       if (unlikely(err == -EWOULDBLOCK)) {
22866 +               pr_warn("cannot retry for NFSv4 delegation"
22867 +                       " for an internal unlink\n");
22868 +               iput(delegated);
22869 +       }
22870 +       if (unlikely(err))
22871 +               AU_MVD_PR(dmsg, "unlink failed\n");
22872 +
22873 +       AuTraceErr(err);
22874 +       return err;
22875 +}
22876 +
22877 +/* Since mvdown succeeded, we ignore an error of this function */
22878 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22879 +{
22880 +       int err;
22881 +       struct au_branch *br;
22882 +
22883 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22884 +       br = au_sbr(a->sb, a->mvd_bsrc);
22885 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22886 +       if (!err) {
22887 +               br = au_sbr(a->sb, a->mvd_bdst);
22888 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22889 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22890 +       }
22891 +       if (!err)
22892 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22893 +       else
22894 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22895 +}
22896 +
22897 +/*
22898 + * copy-down the file and unlink the bsrc file.
22899 + * - unlink the bdst whout if exist
22900 + * - copy-down the file (with whtmp name and rename)
22901 + * - unlink the bsrc file
22902 + */
22903 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
22904 +{
22905 +       int err;
22906 +
22907 +       err = au_do_mkdir(dmsg, a);
22908 +       if (!err)
22909 +               err = au_do_lock(dmsg, a);
22910 +       if (unlikely(err))
22911 +               goto out;
22912 +
22913 +       /*
22914 +        * do not revert the activities we made on bdst since they should be
22915 +        * harmless in aufs.
22916 +        */
22917 +
22918 +       err = au_do_cpdown(dmsg, a);
22919 +       if (!err)
22920 +               err = au_do_unlink_wh(dmsg, a);
22921 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22922 +               err = au_do_unlink(dmsg, a);
22923 +       if (unlikely(err))
22924 +               goto out_unlock;
22925 +
22926 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
22927 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
22928 +       if (find_lower_writable(a) < 0)
22929 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22930 +
22931 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22932 +               au_do_stfs(dmsg, a);
22933 +
22934 +       /* maintain internal array */
22935 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22936 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
22937 +               au_set_dbtop(a->dentry, a->mvd_bdst);
22938 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
22939 +               au_set_ibtop(a->inode, a->mvd_bdst);
22940 +       } else {
22941 +               /* hide the lower */
22942 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
22943 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
22944 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
22945 +               au_set_ibbot(a->inode, a->mvd_bsrc);
22946 +       }
22947 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
22948 +               au_set_dbbot(a->dentry, a->mvd_bdst);
22949 +       if (au_ibbot(a->inode) < a->mvd_bdst)
22950 +               au_set_ibbot(a->inode, a->mvd_bdst);
22951 +
22952 +out_unlock:
22953 +       au_do_unlock(dmsg, a);
22954 +out:
22955 +       AuTraceErr(err);
22956 +       return err;
22957 +}
22958 +
22959 +/* ---------------------------------------------------------------------- */
22960 +
22961 +/* make sure the file is idle */
22962 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
22963 +{
22964 +       int err, plinked;
22965 +
22966 +       err = 0;
22967 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
22968 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
22969 +           && au_dcount(a->dentry) == 1
22970 +           && atomic_read(&a->inode->i_count) == 1
22971 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
22972 +           && (!plinked || !au_plink_test(a->inode))
22973 +           && a->inode->i_nlink == 1)
22974 +               goto out;
22975 +
22976 +       err = -EBUSY;
22977 +       AU_MVD_PR(dmsg,
22978 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
22979 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
22980 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
22981 +                 a->mvd_h_src_inode->i_nlink,
22982 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
22983 +
22984 +out:
22985 +       AuTraceErr(err);
22986 +       return err;
22987 +}
22988 +
22989 +/* make sure the parent dir is fine */
22990 +static int au_mvd_args_parent(const unsigned char dmsg,
22991 +                             struct au_mvd_args *a)
22992 +{
22993 +       int err;
22994 +       aufs_bindex_t bindex;
22995 +
22996 +       err = 0;
22997 +       if (unlikely(au_alive_dir(a->parent))) {
22998 +               err = -ENOENT;
22999 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
23000 +               goto out;
23001 +       }
23002 +
23003 +       a->bopq = au_dbdiropq(a->parent);
23004 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
23005 +       AuDbg("b%d\n", bindex);
23006 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
23007 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
23008 +               err = -EINVAL;
23009 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
23010 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
23011 +                         a->bopq, a->mvd_bdst);
23012 +       }
23013 +
23014 +out:
23015 +       AuTraceErr(err);
23016 +       return err;
23017 +}
23018 +
23019 +static int au_mvd_args_intermediate(const unsigned char dmsg,
23020 +                                   struct au_mvd_args *a)
23021 +{
23022 +       int err;
23023 +       struct au_dinfo *dinfo, *tmp;
23024 +
23025 +       /* lookup the next lower positive entry */
23026 +       err = -ENOMEM;
23027 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
23028 +       if (unlikely(!tmp))
23029 +               goto out;
23030 +
23031 +       a->bfound = -1;
23032 +       a->bwh = -1;
23033 +       dinfo = au_di(a->dentry);
23034 +       au_di_cp(tmp, dinfo);
23035 +       au_di_swap(tmp, dinfo);
23036 +
23037 +       /* returns the number of positive dentries */
23038 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
23039 +                            /* AuLkup_IGNORE_PERM */ 0);
23040 +       if (!err)
23041 +               a->bwh = au_dbwh(a->dentry);
23042 +       else if (err > 0)
23043 +               a->bfound = au_dbtop(a->dentry);
23044 +
23045 +       au_di_swap(tmp, dinfo);
23046 +       au_rw_write_unlock(&tmp->di_rwsem);
23047 +       au_di_free(tmp);
23048 +       if (unlikely(err < 0))
23049 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
23050 +
23051 +       /*
23052 +        * here, we have these cases.
23053 +        * bfound == -1
23054 +        *      no positive dentry under bsrc. there are more sub-cases.
23055 +        *      bwh < 0
23056 +        *              there no whiteout, we can safely move-down.
23057 +        *      bwh <= bsrc
23058 +        *              impossible
23059 +        *      bsrc < bwh && bwh < bdst
23060 +        *              there is a whiteout on RO branch. cannot proceed.
23061 +        *      bwh == bdst
23062 +        *              there is a whiteout on the RW target branch. it should
23063 +        *              be removed.
23064 +        *      bdst < bwh
23065 +        *              there is a whiteout somewhere unrelated branch.
23066 +        * -1 < bfound && bfound <= bsrc
23067 +        *      impossible.
23068 +        * bfound < bdst
23069 +        *      found, but it is on RO branch between bsrc and bdst. cannot
23070 +        *      proceed.
23071 +        * bfound == bdst
23072 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
23073 +        *      error.
23074 +        * bdst < bfound
23075 +        *      found, after we create the file on bdst, it will be hidden.
23076 +        */
23077 +
23078 +       AuDebugOn(a->bfound == -1
23079 +                 && a->bwh != -1
23080 +                 && a->bwh <= a->mvd_bsrc);
23081 +       AuDebugOn(-1 < a->bfound
23082 +                 && a->bfound <= a->mvd_bsrc);
23083 +
23084 +       err = -EINVAL;
23085 +       if (a->bfound == -1
23086 +           && a->mvd_bsrc < a->bwh
23087 +           && a->bwh != -1
23088 +           && a->bwh < a->mvd_bdst) {
23089 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
23090 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
23091 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
23092 +               goto out;
23093 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
23094 +               a->mvd_errno = EAU_MVDOWN_UPPER;
23095 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
23096 +                         a->mvd_bdst, a->bfound);
23097 +               goto out;
23098 +       }
23099 +
23100 +       err = 0; /* success */
23101 +
23102 +out:
23103 +       AuTraceErr(err);
23104 +       return err;
23105 +}
23106 +
23107 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
23108 +{
23109 +       int err;
23110 +
23111 +       err = 0;
23112 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
23113 +           && a->bfound == a->mvd_bdst)
23114 +               err = -EEXIST;
23115 +       AuTraceErr(err);
23116 +       return err;
23117 +}
23118 +
23119 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
23120 +{
23121 +       int err;
23122 +       struct au_branch *br;
23123 +
23124 +       err = -EISDIR;
23125 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
23126 +               goto out;
23127 +
23128 +       err = -EINVAL;
23129 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
23130 +               a->mvd_bsrc = au_ibtop(a->inode);
23131 +       else {
23132 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
23133 +               if (unlikely(a->mvd_bsrc < 0
23134 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
23135 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
23136 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
23137 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
23138 +                                || au_ibbot(a->inode) < a->mvd_bsrc
23139 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
23140 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
23141 +                       AU_MVD_PR(dmsg, "no upper\n");
23142 +                       goto out;
23143 +               }
23144 +       }
23145 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
23146 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
23147 +               AU_MVD_PR(dmsg, "on the bottom\n");
23148 +               goto out;
23149 +       }
23150 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
23151 +       br = au_sbr(a->sb, a->mvd_bsrc);
23152 +       err = au_br_rdonly(br);
23153 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
23154 +               if (unlikely(err))
23155 +                       goto out;
23156 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
23157 +                    || IS_APPEND(a->mvd_h_src_inode))) {
23158 +               if (err)
23159 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
23160 +               /* go on */
23161 +       } else
23162 +               goto out;
23163 +
23164 +       err = -EINVAL;
23165 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
23166 +               a->mvd_bdst = find_lower_writable(a);
23167 +               if (unlikely(a->mvd_bdst < 0)) {
23168 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
23169 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
23170 +                       goto out;
23171 +               }
23172 +       } else {
23173 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
23174 +               if (unlikely(a->mvd_bdst < 0
23175 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
23176 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
23177 +                       AU_MVD_PR(dmsg, "no lower brid\n");
23178 +                       goto out;
23179 +               }
23180 +       }
23181 +
23182 +       err = au_mvd_args_busy(dmsg, a);
23183 +       if (!err)
23184 +               err = au_mvd_args_parent(dmsg, a);
23185 +       if (!err)
23186 +               err = au_mvd_args_intermediate(dmsg, a);
23187 +       if (!err)
23188 +               err = au_mvd_args_exist(dmsg, a);
23189 +       if (!err)
23190 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
23191 +
23192 +out:
23193 +       AuTraceErr(err);
23194 +       return err;
23195 +}
23196 +
23197 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
23198 +{
23199 +       int err, e;
23200 +       unsigned char dmsg;
23201 +       struct au_mvd_args *args;
23202 +       struct inode *inode;
23203 +
23204 +       inode = d_inode(dentry);
23205 +       err = -EPERM;
23206 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
23207 +               goto out;
23208 +
23209 +       err = -ENOMEM;
23210 +       args = kmalloc(sizeof(*args), GFP_NOFS);
23211 +       if (unlikely(!args))
23212 +               goto out;
23213 +
23214 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
23215 +       if (!err)
23216 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
23217 +       if (unlikely(err)) {
23218 +               err = -EFAULT;
23219 +               AuTraceErr(err);
23220 +               goto out_free;
23221 +       }
23222 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
23223 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
23224 +       args->mvdown.au_errno = 0;
23225 +       args->dentry = dentry;
23226 +       args->inode = inode;
23227 +       args->sb = dentry->d_sb;
23228 +
23229 +       err = -ENOENT;
23230 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
23231 +       args->parent = dget_parent(dentry);
23232 +       args->dir = d_inode(args->parent);
23233 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
23234 +       dput(args->parent);
23235 +       if (unlikely(args->parent != dentry->d_parent)) {
23236 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
23237 +               goto out_dir;
23238 +       }
23239 +
23240 +       inode_lock_nested(inode, I_MUTEX_CHILD);
23241 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
23242 +       if (unlikely(err))
23243 +               goto out_inode;
23244 +
23245 +       di_write_lock_parent(args->parent);
23246 +       err = au_mvd_args(dmsg, args);
23247 +       if (unlikely(err))
23248 +               goto out_parent;
23249 +
23250 +       err = au_do_mvdown(dmsg, args);
23251 +       if (unlikely(err))
23252 +               goto out_parent;
23253 +
23254 +       au_cpup_attr_timesizes(args->dir);
23255 +       au_cpup_attr_timesizes(inode);
23256 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
23257 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
23258 +       /* au_digen_dec(dentry); */
23259 +
23260 +out_parent:
23261 +       di_write_unlock(args->parent);
23262 +       aufs_read_unlock(dentry, AuLock_DW);
23263 +out_inode:
23264 +       inode_unlock(inode);
23265 +out_dir:
23266 +       inode_unlock(args->dir);
23267 +out_free:
23268 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
23269 +       if (unlikely(e))
23270 +               err = -EFAULT;
23271 +       au_delayed_kfree(args);
23272 +out:
23273 +       AuTraceErr(err);
23274 +       return err;
23275 +}
23276 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
23277 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
23278 +++ linux/fs/aufs/opts.c        2017-03-02 14:24:13.863589061 +0100
23279 @@ -0,0 +1,1848 @@
23280 +/*
23281 + * Copyright (C) 2005-2017 Junjiro R. Okajima
23282 + *
23283 + * This program, aufs is free software; you can redistribute it and/or modify
23284 + * it under the terms of the GNU General Public License as published by
23285 + * the Free Software Foundation; either version 2 of the License, or
23286 + * (at your option) any later version.
23287 + *
23288 + * This program is distributed in the hope that it will be useful,
23289 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23290 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23291 + * GNU General Public License for more details.
23292 + *
23293 + * You should have received a copy of the GNU General Public License
23294 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23295 + */
23296 +
23297 +/*
23298 + * mount options/flags
23299 + */
23300 +
23301 +#include <linux/namei.h>
23302 +#include <linux/types.h> /* a distribution requires */
23303 +#include <linux/parser.h>
23304 +#include "aufs.h"
23305 +
23306 +/* ---------------------------------------------------------------------- */
23307 +
23308 +enum {
23309 +       Opt_br,
23310 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
23311 +       Opt_idel, Opt_imod,
23312 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
23313 +       Opt_rdblk_def, Opt_rdhash_def,
23314 +       Opt_xino, Opt_noxino,
23315 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
23316 +       Opt_trunc_xino_path, Opt_itrunc_xino,
23317 +       Opt_trunc_xib, Opt_notrunc_xib,
23318 +       Opt_shwh, Opt_noshwh,
23319 +       Opt_plink, Opt_noplink, Opt_list_plink,
23320 +       Opt_udba,
23321 +       Opt_dio, Opt_nodio,
23322 +       Opt_diropq_a, Opt_diropq_w,
23323 +       Opt_warn_perm, Opt_nowarn_perm,
23324 +       Opt_wbr_copyup, Opt_wbr_create,
23325 +       Opt_fhsm_sec,
23326 +       Opt_verbose, Opt_noverbose,
23327 +       Opt_sum, Opt_nosum, Opt_wsum,
23328 +       Opt_dirperm1, Opt_nodirperm1,
23329 +       Opt_acl, Opt_noacl,
23330 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
23331 +};
23332 +
23333 +static match_table_t options = {
23334 +       {Opt_br, "br=%s"},
23335 +       {Opt_br, "br:%s"},
23336 +
23337 +       {Opt_add, "add=%d:%s"},
23338 +       {Opt_add, "add:%d:%s"},
23339 +       {Opt_add, "ins=%d:%s"},
23340 +       {Opt_add, "ins:%d:%s"},
23341 +       {Opt_append, "append=%s"},
23342 +       {Opt_append, "append:%s"},
23343 +       {Opt_prepend, "prepend=%s"},
23344 +       {Opt_prepend, "prepend:%s"},
23345 +
23346 +       {Opt_del, "del=%s"},
23347 +       {Opt_del, "del:%s"},
23348 +       /* {Opt_idel, "idel:%d"}, */
23349 +       {Opt_mod, "mod=%s"},
23350 +       {Opt_mod, "mod:%s"},
23351 +       /* {Opt_imod, "imod:%d:%s"}, */
23352 +
23353 +       {Opt_dirwh, "dirwh=%d"},
23354 +
23355 +       {Opt_xino, "xino=%s"},
23356 +       {Opt_noxino, "noxino"},
23357 +       {Opt_trunc_xino, "trunc_xino"},
23358 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
23359 +       {Opt_notrunc_xino, "notrunc_xino"},
23360 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
23361 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
23362 +       /* {Opt_zxino, "zxino=%s"}, */
23363 +       {Opt_trunc_xib, "trunc_xib"},
23364 +       {Opt_notrunc_xib, "notrunc_xib"},
23365 +
23366 +#ifdef CONFIG_PROC_FS
23367 +       {Opt_plink, "plink"},
23368 +#else
23369 +       {Opt_ignore_silent, "plink"},
23370 +#endif
23371 +
23372 +       {Opt_noplink, "noplink"},
23373 +
23374 +#ifdef CONFIG_AUFS_DEBUG
23375 +       {Opt_list_plink, "list_plink"},
23376 +#endif
23377 +
23378 +       {Opt_udba, "udba=%s"},
23379 +
23380 +       {Opt_dio, "dio"},
23381 +       {Opt_nodio, "nodio"},
23382 +
23383 +#ifdef CONFIG_AUFS_FHSM
23384 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
23385 +#else
23386 +       {Opt_ignore_silent, "fhsm_sec=%d"},
23387 +#endif
23388 +
23389 +       {Opt_diropq_a, "diropq=always"},
23390 +       {Opt_diropq_a, "diropq=a"},
23391 +       {Opt_diropq_w, "diropq=whiteouted"},
23392 +       {Opt_diropq_w, "diropq=w"},
23393 +
23394 +       {Opt_warn_perm, "warn_perm"},
23395 +       {Opt_nowarn_perm, "nowarn_perm"},
23396 +
23397 +       /* keep them temporary */
23398 +       {Opt_ignore_silent, "nodlgt"},
23399 +       {Opt_ignore_silent, "clean_plink"},
23400 +
23401 +#ifdef CONFIG_AUFS_SHWH
23402 +       {Opt_shwh, "shwh"},
23403 +#endif
23404 +       {Opt_noshwh, "noshwh"},
23405 +
23406 +       {Opt_dirperm1, "dirperm1"},
23407 +       {Opt_nodirperm1, "nodirperm1"},
23408 +
23409 +       {Opt_verbose, "verbose"},
23410 +       {Opt_verbose, "v"},
23411 +       {Opt_noverbose, "noverbose"},
23412 +       {Opt_noverbose, "quiet"},
23413 +       {Opt_noverbose, "q"},
23414 +       {Opt_noverbose, "silent"},
23415 +
23416 +       {Opt_sum, "sum"},
23417 +       {Opt_nosum, "nosum"},
23418 +       {Opt_wsum, "wsum"},
23419 +
23420 +       {Opt_rdcache, "rdcache=%d"},
23421 +       {Opt_rdblk, "rdblk=%d"},
23422 +       {Opt_rdblk_def, "rdblk=def"},
23423 +       {Opt_rdhash, "rdhash=%d"},
23424 +       {Opt_rdhash_def, "rdhash=def"},
23425 +
23426 +       {Opt_wbr_create, "create=%s"},
23427 +       {Opt_wbr_create, "create_policy=%s"},
23428 +       {Opt_wbr_copyup, "cpup=%s"},
23429 +       {Opt_wbr_copyup, "copyup=%s"},
23430 +       {Opt_wbr_copyup, "copyup_policy=%s"},
23431 +
23432 +       /* generic VFS flag */
23433 +#ifdef CONFIG_FS_POSIX_ACL
23434 +       {Opt_acl, "acl"},
23435 +       {Opt_noacl, "noacl"},
23436 +#else
23437 +       {Opt_ignore_silent, "acl"},
23438 +       {Opt_ignore_silent, "noacl"},
23439 +#endif
23440 +
23441 +       /* internal use for the scripts */
23442 +       {Opt_ignore_silent, "si=%s"},
23443 +
23444 +       {Opt_br, "dirs=%s"},
23445 +       {Opt_ignore, "debug=%d"},
23446 +       {Opt_ignore, "delete=whiteout"},
23447 +       {Opt_ignore, "delete=all"},
23448 +       {Opt_ignore, "imap=%s"},
23449 +
23450 +       /* temporary workaround, due to old mount(8)? */
23451 +       {Opt_ignore_silent, "relatime"},
23452 +
23453 +       {Opt_err, NULL}
23454 +};
23455 +
23456 +/* ---------------------------------------------------------------------- */
23457 +
23458 +static const char *au_parser_pattern(int val, match_table_t tbl)
23459 +{
23460 +       struct match_token *p;
23461 +
23462 +       p = tbl;
23463 +       while (p->pattern) {
23464 +               if (p->token == val)
23465 +                       return p->pattern;
23466 +               p++;
23467 +       }
23468 +       BUG();
23469 +       return "??";
23470 +}
23471 +
23472 +static const char *au_optstr(int *val, match_table_t tbl)
23473 +{
23474 +       struct match_token *p;
23475 +       int v;
23476 +
23477 +       v = *val;
23478 +       if (!v)
23479 +               goto out;
23480 +       p = tbl;
23481 +       while (p->pattern) {
23482 +               if (p->token
23483 +                   && (v & p->token) == p->token) {
23484 +                       *val &= ~p->token;
23485 +                       return p->pattern;
23486 +               }
23487 +               p++;
23488 +       }
23489 +
23490 +out:
23491 +       return NULL;
23492 +}
23493 +
23494 +/* ---------------------------------------------------------------------- */
23495 +
23496 +static match_table_t brperm = {
23497 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
23498 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
23499 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
23500 +       {0, NULL}
23501 +};
23502 +
23503 +static match_table_t brattr = {
23504 +       /* general */
23505 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
23506 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
23507 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
23508 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
23509 +#ifdef CONFIG_AUFS_FHSM
23510 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
23511 +#endif
23512 +#ifdef CONFIG_AUFS_XATTR
23513 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
23514 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
23515 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
23516 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
23517 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
23518 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
23519 +#endif
23520 +
23521 +       /* ro/rr branch */
23522 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
23523 +
23524 +       /* rw branch */
23525 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
23526 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
23527 +
23528 +       {0, NULL}
23529 +};
23530 +
23531 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
23532 +{
23533 +       int attr, v;
23534 +       char *p;
23535 +
23536 +       attr = 0;
23537 +       do {
23538 +               p = strchr(str, '+');
23539 +               if (p)
23540 +                       *p = 0;
23541 +               v = match_token(str, table, args);
23542 +               if (v) {
23543 +                       if (v & AuBrAttr_CMOO_Mask)
23544 +                               attr &= ~AuBrAttr_CMOO_Mask;
23545 +                       attr |= v;
23546 +               } else {
23547 +                       if (p)
23548 +                               *p = '+';
23549 +                       pr_warn("ignored branch attribute %s\n", str);
23550 +                       break;
23551 +               }
23552 +               if (p)
23553 +                       str = p + 1;
23554 +       } while (p);
23555 +
23556 +       return attr;
23557 +}
23558 +
23559 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
23560 +{
23561 +       int sz;
23562 +       const char *p;
23563 +       char *q;
23564 +
23565 +       q = str->a;
23566 +       *q = 0;
23567 +       p = au_optstr(&perm, brattr);
23568 +       if (p) {
23569 +               sz = strlen(p);
23570 +               memcpy(q, p, sz + 1);
23571 +               q += sz;
23572 +       } else
23573 +               goto out;
23574 +
23575 +       do {
23576 +               p = au_optstr(&perm, brattr);
23577 +               if (p) {
23578 +                       *q++ = '+';
23579 +                       sz = strlen(p);
23580 +                       memcpy(q, p, sz + 1);
23581 +                       q += sz;
23582 +               }
23583 +       } while (p);
23584 +
23585 +out:
23586 +       return q - str->a;
23587 +}
23588 +
23589 +static int noinline_for_stack br_perm_val(char *perm)
23590 +{
23591 +       int val, bad, sz;
23592 +       char *p;
23593 +       substring_t args[MAX_OPT_ARGS];
23594 +       au_br_perm_str_t attr;
23595 +
23596 +       p = strchr(perm, '+');
23597 +       if (p)
23598 +               *p = 0;
23599 +       val = match_token(perm, brperm, args);
23600 +       if (!val) {
23601 +               if (p)
23602 +                       *p = '+';
23603 +               pr_warn("ignored branch permission %s\n", perm);
23604 +               val = AuBrPerm_RO;
23605 +               goto out;
23606 +       }
23607 +       if (!p)
23608 +               goto out;
23609 +
23610 +       val |= br_attr_val(p + 1, brattr, args);
23611 +
23612 +       bad = 0;
23613 +       switch (val & AuBrPerm_Mask) {
23614 +       case AuBrPerm_RO:
23615 +       case AuBrPerm_RR:
23616 +               bad = val & AuBrWAttr_Mask;
23617 +               val &= ~AuBrWAttr_Mask;
23618 +               break;
23619 +       case AuBrPerm_RW:
23620 +               bad = val & AuBrRAttr_Mask;
23621 +               val &= ~AuBrRAttr_Mask;
23622 +               break;
23623 +       }
23624 +
23625 +       /*
23626 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
23627 +        * does not treat it as an error, just warning.
23628 +        * this is a tiny guard for the user operation.
23629 +        */
23630 +       if (val & AuBrAttr_UNPIN) {
23631 +               bad |= AuBrAttr_UNPIN;
23632 +               val &= ~AuBrAttr_UNPIN;
23633 +       }
23634 +
23635 +       if (unlikely(bad)) {
23636 +               sz = au_do_optstr_br_attr(&attr, bad);
23637 +               AuDebugOn(!sz);
23638 +               pr_warn("ignored branch attribute %s\n", attr.a);
23639 +       }
23640 +
23641 +out:
23642 +       return val;
23643 +}
23644 +
23645 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
23646 +{
23647 +       au_br_perm_str_t attr;
23648 +       const char *p;
23649 +       char *q;
23650 +       int sz;
23651 +
23652 +       q = str->a;
23653 +       p = au_optstr(&perm, brperm);
23654 +       AuDebugOn(!p || !*p);
23655 +       sz = strlen(p);
23656 +       memcpy(q, p, sz + 1);
23657 +       q += sz;
23658 +
23659 +       sz = au_do_optstr_br_attr(&attr, perm);
23660 +       if (sz) {
23661 +               *q++ = '+';
23662 +               memcpy(q, attr.a, sz + 1);
23663 +       }
23664 +
23665 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
23666 +}
23667 +
23668 +/* ---------------------------------------------------------------------- */
23669 +
23670 +static match_table_t udbalevel = {
23671 +       {AuOpt_UDBA_REVAL, "reval"},
23672 +       {AuOpt_UDBA_NONE, "none"},
23673 +#ifdef CONFIG_AUFS_HNOTIFY
23674 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
23675 +#ifdef CONFIG_AUFS_HFSNOTIFY
23676 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
23677 +#endif
23678 +#endif
23679 +       {-1, NULL}
23680 +};
23681 +
23682 +static int noinline_for_stack udba_val(char *str)
23683 +{
23684 +       substring_t args[MAX_OPT_ARGS];
23685 +
23686 +       return match_token(str, udbalevel, args);
23687 +}
23688 +
23689 +const char *au_optstr_udba(int udba)
23690 +{
23691 +       return au_parser_pattern(udba, udbalevel);
23692 +}
23693 +
23694 +/* ---------------------------------------------------------------------- */
23695 +
23696 +static match_table_t au_wbr_create_policy = {
23697 +       {AuWbrCreate_TDP, "tdp"},
23698 +       {AuWbrCreate_TDP, "top-down-parent"},
23699 +       {AuWbrCreate_RR, "rr"},
23700 +       {AuWbrCreate_RR, "round-robin"},
23701 +       {AuWbrCreate_MFS, "mfs"},
23702 +       {AuWbrCreate_MFS, "most-free-space"},
23703 +       {AuWbrCreate_MFSV, "mfs:%d"},
23704 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
23705 +
23706 +       /* top-down regardless the parent, and then mfs */
23707 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
23708 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
23709 +
23710 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
23711 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23712 +       {AuWbrCreate_PMFS, "pmfs"},
23713 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
23714 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23715 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
23716 +
23717 +       {-1, NULL}
23718 +};
23719 +
23720 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23721 +                           struct au_opt_wbr_create *create)
23722 +{
23723 +       int err;
23724 +       unsigned long long ull;
23725 +
23726 +       err = 0;
23727 +       if (!match_u64(arg, &ull))
23728 +               create->mfsrr_watermark = ull;
23729 +       else {
23730 +               pr_err("bad integer in %s\n", str);
23731 +               err = -EINVAL;
23732 +       }
23733 +
23734 +       return err;
23735 +}
23736 +
23737 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
23738 +                         struct au_opt_wbr_create *create)
23739 +{
23740 +       int n, err;
23741 +
23742 +       err = 0;
23743 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
23744 +               create->mfs_second = n;
23745 +       else {
23746 +               pr_err("bad integer in %s\n", str);
23747 +               err = -EINVAL;
23748 +       }
23749 +
23750 +       return err;
23751 +}
23752 +
23753 +static int noinline_for_stack
23754 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
23755 +{
23756 +       int err, e;
23757 +       substring_t args[MAX_OPT_ARGS];
23758 +
23759 +       err = match_token(str, au_wbr_create_policy, args);
23760 +       create->wbr_create = err;
23761 +       switch (err) {
23762 +       case AuWbrCreate_MFSRRV:
23763 +       case AuWbrCreate_TDMFSV:
23764 +       case AuWbrCreate_PMFSRRV:
23765 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23766 +               if (!e)
23767 +                       e = au_wbr_mfs_sec(&args[1], str, create);
23768 +               if (unlikely(e))
23769 +                       err = e;
23770 +               break;
23771 +       case AuWbrCreate_MFSRR:
23772 +       case AuWbrCreate_TDMFS:
23773 +       case AuWbrCreate_PMFSRR:
23774 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23775 +               if (unlikely(e)) {
23776 +                       err = e;
23777 +                       break;
23778 +               }
23779 +               /*FALLTHROUGH*/
23780 +       case AuWbrCreate_MFS:
23781 +       case AuWbrCreate_PMFS:
23782 +               create->mfs_second = AUFS_MFS_DEF_SEC;
23783 +               break;
23784 +       case AuWbrCreate_MFSV:
23785 +       case AuWbrCreate_PMFSV:
23786 +               e = au_wbr_mfs_sec(&args[0], str, create);
23787 +               if (unlikely(e))
23788 +                       err = e;
23789 +               break;
23790 +       }
23791 +
23792 +       return err;
23793 +}
23794 +
23795 +const char *au_optstr_wbr_create(int wbr_create)
23796 +{
23797 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
23798 +}
23799 +
23800 +static match_table_t au_wbr_copyup_policy = {
23801 +       {AuWbrCopyup_TDP, "tdp"},
23802 +       {AuWbrCopyup_TDP, "top-down-parent"},
23803 +       {AuWbrCopyup_BUP, "bup"},
23804 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
23805 +       {AuWbrCopyup_BU, "bu"},
23806 +       {AuWbrCopyup_BU, "bottom-up"},
23807 +       {-1, NULL}
23808 +};
23809 +
23810 +static int noinline_for_stack au_wbr_copyup_val(char *str)
23811 +{
23812 +       substring_t args[MAX_OPT_ARGS];
23813 +
23814 +       return match_token(str, au_wbr_copyup_policy, args);
23815 +}
23816 +
23817 +const char *au_optstr_wbr_copyup(int wbr_copyup)
23818 +{
23819 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
23820 +}
23821 +
23822 +/* ---------------------------------------------------------------------- */
23823 +
23824 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23825 +
23826 +static void dump_opts(struct au_opts *opts)
23827 +{
23828 +#ifdef CONFIG_AUFS_DEBUG
23829 +       /* reduce stack space */
23830 +       union {
23831 +               struct au_opt_add *add;
23832 +               struct au_opt_del *del;
23833 +               struct au_opt_mod *mod;
23834 +               struct au_opt_xino *xino;
23835 +               struct au_opt_xino_itrunc *xino_itrunc;
23836 +               struct au_opt_wbr_create *create;
23837 +       } u;
23838 +       struct au_opt *opt;
23839 +
23840 +       opt = opts->opt;
23841 +       while (opt->type != Opt_tail) {
23842 +               switch (opt->type) {
23843 +               case Opt_add:
23844 +                       u.add = &opt->add;
23845 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
23846 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23847 +                                 u.add->path.dentry);
23848 +                       break;
23849 +               case Opt_del:
23850 +               case Opt_idel:
23851 +                       u.del = &opt->del;
23852 +                       AuDbg("del {%s, %p}\n",
23853 +                             u.del->pathname, u.del->h_path.dentry);
23854 +                       break;
23855 +               case Opt_mod:
23856 +               case Opt_imod:
23857 +                       u.mod = &opt->mod;
23858 +                       AuDbg("mod {%s, 0x%x, %p}\n",
23859 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
23860 +                       break;
23861 +               case Opt_append:
23862 +                       u.add = &opt->add;
23863 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
23864 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23865 +                                 u.add->path.dentry);
23866 +                       break;
23867 +               case Opt_prepend:
23868 +                       u.add = &opt->add;
23869 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23870 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23871 +                                 u.add->path.dentry);
23872 +                       break;
23873 +               case Opt_dirwh:
23874 +                       AuDbg("dirwh %d\n", opt->dirwh);
23875 +                       break;
23876 +               case Opt_rdcache:
23877 +                       AuDbg("rdcache %d\n", opt->rdcache);
23878 +                       break;
23879 +               case Opt_rdblk:
23880 +                       AuDbg("rdblk %u\n", opt->rdblk);
23881 +                       break;
23882 +               case Opt_rdblk_def:
23883 +                       AuDbg("rdblk_def\n");
23884 +                       break;
23885 +               case Opt_rdhash:
23886 +                       AuDbg("rdhash %u\n", opt->rdhash);
23887 +                       break;
23888 +               case Opt_rdhash_def:
23889 +                       AuDbg("rdhash_def\n");
23890 +                       break;
23891 +               case Opt_xino:
23892 +                       u.xino = &opt->xino;
23893 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
23894 +                       break;
23895 +               case Opt_trunc_xino:
23896 +                       AuLabel(trunc_xino);
23897 +                       break;
23898 +               case Opt_notrunc_xino:
23899 +                       AuLabel(notrunc_xino);
23900 +                       break;
23901 +               case Opt_trunc_xino_path:
23902 +               case Opt_itrunc_xino:
23903 +                       u.xino_itrunc = &opt->xino_itrunc;
23904 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23905 +                       break;
23906 +               case Opt_noxino:
23907 +                       AuLabel(noxino);
23908 +                       break;
23909 +               case Opt_trunc_xib:
23910 +                       AuLabel(trunc_xib);
23911 +                       break;
23912 +               case Opt_notrunc_xib:
23913 +                       AuLabel(notrunc_xib);
23914 +                       break;
23915 +               case Opt_shwh:
23916 +                       AuLabel(shwh);
23917 +                       break;
23918 +               case Opt_noshwh:
23919 +                       AuLabel(noshwh);
23920 +                       break;
23921 +               case Opt_dirperm1:
23922 +                       AuLabel(dirperm1);
23923 +                       break;
23924 +               case Opt_nodirperm1:
23925 +                       AuLabel(nodirperm1);
23926 +                       break;
23927 +               case Opt_plink:
23928 +                       AuLabel(plink);
23929 +                       break;
23930 +               case Opt_noplink:
23931 +                       AuLabel(noplink);
23932 +                       break;
23933 +               case Opt_list_plink:
23934 +                       AuLabel(list_plink);
23935 +                       break;
23936 +               case Opt_udba:
23937 +                       AuDbg("udba %d, %s\n",
23938 +                                 opt->udba, au_optstr_udba(opt->udba));
23939 +                       break;
23940 +               case Opt_dio:
23941 +                       AuLabel(dio);
23942 +                       break;
23943 +               case Opt_nodio:
23944 +                       AuLabel(nodio);
23945 +                       break;
23946 +               case Opt_diropq_a:
23947 +                       AuLabel(diropq_a);
23948 +                       break;
23949 +               case Opt_diropq_w:
23950 +                       AuLabel(diropq_w);
23951 +                       break;
23952 +               case Opt_warn_perm:
23953 +                       AuLabel(warn_perm);
23954 +                       break;
23955 +               case Opt_nowarn_perm:
23956 +                       AuLabel(nowarn_perm);
23957 +                       break;
23958 +               case Opt_verbose:
23959 +                       AuLabel(verbose);
23960 +                       break;
23961 +               case Opt_noverbose:
23962 +                       AuLabel(noverbose);
23963 +                       break;
23964 +               case Opt_sum:
23965 +                       AuLabel(sum);
23966 +                       break;
23967 +               case Opt_nosum:
23968 +                       AuLabel(nosum);
23969 +                       break;
23970 +               case Opt_wsum:
23971 +                       AuLabel(wsum);
23972 +                       break;
23973 +               case Opt_wbr_create:
23974 +                       u.create = &opt->wbr_create;
23975 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
23976 +                                 au_optstr_wbr_create(u.create->wbr_create));
23977 +                       switch (u.create->wbr_create) {
23978 +                       case AuWbrCreate_MFSV:
23979 +                       case AuWbrCreate_PMFSV:
23980 +                               AuDbg("%d sec\n", u.create->mfs_second);
23981 +                               break;
23982 +                       case AuWbrCreate_MFSRR:
23983 +                       case AuWbrCreate_TDMFS:
23984 +                               AuDbg("%llu watermark\n",
23985 +                                         u.create->mfsrr_watermark);
23986 +                               break;
23987 +                       case AuWbrCreate_MFSRRV:
23988 +                       case AuWbrCreate_TDMFSV:
23989 +                       case AuWbrCreate_PMFSRRV:
23990 +                               AuDbg("%llu watermark, %d sec\n",
23991 +                                         u.create->mfsrr_watermark,
23992 +                                         u.create->mfs_second);
23993 +                               break;
23994 +                       }
23995 +                       break;
23996 +               case Opt_wbr_copyup:
23997 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23998 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
23999 +                       break;
24000 +               case Opt_fhsm_sec:
24001 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
24002 +                       break;
24003 +               case Opt_acl:
24004 +                       AuLabel(acl);
24005 +                       break;
24006 +               case Opt_noacl:
24007 +                       AuLabel(noacl);
24008 +                       break;
24009 +               default:
24010 +                       BUG();
24011 +               }
24012 +               opt++;
24013 +       }
24014 +#endif
24015 +}
24016 +
24017 +void au_opts_free(struct au_opts *opts)
24018 +{
24019 +       struct au_opt *opt;
24020 +
24021 +       opt = opts->opt;
24022 +       while (opt->type != Opt_tail) {
24023 +               switch (opt->type) {
24024 +               case Opt_add:
24025 +               case Opt_append:
24026 +               case Opt_prepend:
24027 +                       path_put(&opt->add.path);
24028 +                       break;
24029 +               case Opt_del:
24030 +               case Opt_idel:
24031 +                       path_put(&opt->del.h_path);
24032 +                       break;
24033 +               case Opt_mod:
24034 +               case Opt_imod:
24035 +                       dput(opt->mod.h_root);
24036 +                       break;
24037 +               case Opt_xino:
24038 +                       fput(opt->xino.file);
24039 +                       break;
24040 +               }
24041 +               opt++;
24042 +       }
24043 +}
24044 +
24045 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
24046 +                  aufs_bindex_t bindex)
24047 +{
24048 +       int err;
24049 +       struct au_opt_add *add = &opt->add;
24050 +       char *p;
24051 +
24052 +       add->bindex = bindex;
24053 +       add->perm = AuBrPerm_RO;
24054 +       add->pathname = opt_str;
24055 +       p = strchr(opt_str, '=');
24056 +       if (p) {
24057 +               *p++ = 0;
24058 +               if (*p)
24059 +                       add->perm = br_perm_val(p);
24060 +       }
24061 +
24062 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
24063 +       if (!err) {
24064 +               if (!p) {
24065 +                       add->perm = AuBrPerm_RO;
24066 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
24067 +                               add->perm = AuBrPerm_RR;
24068 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
24069 +                               add->perm = AuBrPerm_RW;
24070 +               }
24071 +               opt->type = Opt_add;
24072 +               goto out;
24073 +       }
24074 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
24075 +       err = -EINVAL;
24076 +
24077 +out:
24078 +       return err;
24079 +}
24080 +
24081 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
24082 +{
24083 +       int err;
24084 +
24085 +       del->pathname = args[0].from;
24086 +       AuDbg("del path %s\n", del->pathname);
24087 +
24088 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
24089 +       if (unlikely(err))
24090 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
24091 +
24092 +       return err;
24093 +}
24094 +
24095 +#if 0 /* reserved for future use */
24096 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
24097 +                             struct au_opt_del *del, substring_t args[])
24098 +{
24099 +       int err;
24100 +       struct dentry *root;
24101 +
24102 +       err = -EINVAL;
24103 +       root = sb->s_root;
24104 +       aufs_read_lock(root, AuLock_FLUSH);
24105 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
24106 +               pr_err("out of bounds, %d\n", bindex);
24107 +               goto out;
24108 +       }
24109 +
24110 +       err = 0;
24111 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
24112 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
24113 +
24114 +out:
24115 +       aufs_read_unlock(root, !AuLock_IR);
24116 +       return err;
24117 +}
24118 +#endif
24119 +
24120 +static int noinline_for_stack
24121 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
24122 +{
24123 +       int err;
24124 +       struct path path;
24125 +       char *p;
24126 +
24127 +       err = -EINVAL;
24128 +       mod->path = args[0].from;
24129 +       p = strchr(mod->path, '=');
24130 +       if (unlikely(!p)) {
24131 +               pr_err("no permssion %s\n", args[0].from);
24132 +               goto out;
24133 +       }
24134 +
24135 +       *p++ = 0;
24136 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
24137 +       if (unlikely(err)) {
24138 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
24139 +               goto out;
24140 +       }
24141 +
24142 +       mod->perm = br_perm_val(p);
24143 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
24144 +       mod->h_root = dget(path.dentry);
24145 +       path_put(&path);
24146 +
24147 +out:
24148 +       return err;
24149 +}
24150 +
24151 +#if 0 /* reserved for future use */
24152 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
24153 +                             struct au_opt_mod *mod, substring_t args[])
24154 +{
24155 +       int err;
24156 +       struct dentry *root;
24157 +
24158 +       err = -EINVAL;
24159 +       root = sb->s_root;
24160 +       aufs_read_lock(root, AuLock_FLUSH);
24161 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
24162 +               pr_err("out of bounds, %d\n", bindex);
24163 +               goto out;
24164 +       }
24165 +
24166 +       err = 0;
24167 +       mod->perm = br_perm_val(args[1].from);
24168 +       AuDbg("mod path %s, perm 0x%x, %s\n",
24169 +             mod->path, mod->perm, args[1].from);
24170 +       mod->h_root = dget(au_h_dptr(root, bindex));
24171 +
24172 +out:
24173 +       aufs_read_unlock(root, !AuLock_IR);
24174 +       return err;
24175 +}
24176 +#endif
24177 +
24178 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
24179 +                             substring_t args[])
24180 +{
24181 +       int err;
24182 +       struct file *file;
24183 +
24184 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
24185 +       err = PTR_ERR(file);
24186 +       if (IS_ERR(file))
24187 +               goto out;
24188 +
24189 +       err = -EINVAL;
24190 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
24191 +               fput(file);
24192 +               pr_err("%s must be outside\n", args[0].from);
24193 +               goto out;
24194 +       }
24195 +
24196 +       err = 0;
24197 +       xino->file = file;
24198 +       xino->path = args[0].from;
24199 +
24200 +out:
24201 +       return err;
24202 +}
24203 +
24204 +static int noinline_for_stack
24205 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
24206 +                              struct au_opt_xino_itrunc *xino_itrunc,
24207 +                              substring_t args[])
24208 +{
24209 +       int err;
24210 +       aufs_bindex_t bbot, bindex;
24211 +       struct path path;
24212 +       struct dentry *root;
24213 +
24214 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
24215 +       if (unlikely(err)) {
24216 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
24217 +               goto out;
24218 +       }
24219 +
24220 +       xino_itrunc->bindex = -1;
24221 +       root = sb->s_root;
24222 +       aufs_read_lock(root, AuLock_FLUSH);
24223 +       bbot = au_sbbot(sb);
24224 +       for (bindex = 0; bindex <= bbot; bindex++) {
24225 +               if (au_h_dptr(root, bindex) == path.dentry) {
24226 +                       xino_itrunc->bindex = bindex;
24227 +                       break;
24228 +               }
24229 +       }
24230 +       aufs_read_unlock(root, !AuLock_IR);
24231 +       path_put(&path);
24232 +
24233 +       if (unlikely(xino_itrunc->bindex < 0)) {
24234 +               pr_err("no such branch %s\n", args[0].from);
24235 +               err = -EINVAL;
24236 +       }
24237 +
24238 +out:
24239 +       return err;
24240 +}
24241 +
24242 +/* called without aufs lock */
24243 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
24244 +{
24245 +       int err, n, token;
24246 +       aufs_bindex_t bindex;
24247 +       unsigned char skipped;
24248 +       struct dentry *root;
24249 +       struct au_opt *opt, *opt_tail;
24250 +       char *opt_str;
24251 +       /* reduce the stack space */
24252 +       union {
24253 +               struct au_opt_xino_itrunc *xino_itrunc;
24254 +               struct au_opt_wbr_create *create;
24255 +       } u;
24256 +       struct {
24257 +               substring_t args[MAX_OPT_ARGS];
24258 +       } *a;
24259 +
24260 +       err = -ENOMEM;
24261 +       a = kmalloc(sizeof(*a), GFP_NOFS);
24262 +       if (unlikely(!a))
24263 +               goto out;
24264 +
24265 +       root = sb->s_root;
24266 +       err = 0;
24267 +       bindex = 0;
24268 +       opt = opts->opt;
24269 +       opt_tail = opt + opts->max_opt - 1;
24270 +       opt->type = Opt_tail;
24271 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
24272 +               err = -EINVAL;
24273 +               skipped = 0;
24274 +               token = match_token(opt_str, options, a->args);
24275 +               switch (token) {
24276 +               case Opt_br:
24277 +                       err = 0;
24278 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
24279 +                              && *opt_str) {
24280 +                               err = opt_add(opt, opt_str, opts->sb_flags,
24281 +                                             bindex++);
24282 +                               if (unlikely(!err && ++opt > opt_tail)) {
24283 +                                       err = -E2BIG;
24284 +                                       break;
24285 +                               }
24286 +                               opt->type = Opt_tail;
24287 +                               skipped = 1;
24288 +                       }
24289 +                       break;
24290 +               case Opt_add:
24291 +                       if (unlikely(match_int(&a->args[0], &n))) {
24292 +                               pr_err("bad integer in %s\n", opt_str);
24293 +                               break;
24294 +                       }
24295 +                       bindex = n;
24296 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
24297 +                                     bindex);
24298 +                       if (!err)
24299 +                               opt->type = token;
24300 +                       break;
24301 +               case Opt_append:
24302 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
24303 +                                     /*dummy bindex*/1);
24304 +                       if (!err)
24305 +                               opt->type = token;
24306 +                       break;
24307 +               case Opt_prepend:
24308 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
24309 +                                     /*bindex*/0);
24310 +                       if (!err)
24311 +                               opt->type = token;
24312 +                       break;
24313 +               case Opt_del:
24314 +                       err = au_opts_parse_del(&opt->del, a->args);
24315 +                       if (!err)
24316 +                               opt->type = token;
24317 +                       break;
24318 +#if 0 /* reserved for future use */
24319 +               case Opt_idel:
24320 +                       del->pathname = "(indexed)";
24321 +                       if (unlikely(match_int(&args[0], &n))) {
24322 +                               pr_err("bad integer in %s\n", opt_str);
24323 +                               break;
24324 +                       }
24325 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
24326 +                       if (!err)
24327 +                               opt->type = token;
24328 +                       break;
24329 +#endif
24330 +               case Opt_mod:
24331 +                       err = au_opts_parse_mod(&opt->mod, a->args);
24332 +                       if (!err)
24333 +                               opt->type = token;
24334 +                       break;
24335 +#ifdef IMOD /* reserved for future use */
24336 +               case Opt_imod:
24337 +                       u.mod->path = "(indexed)";
24338 +                       if (unlikely(match_int(&a->args[0], &n))) {
24339 +                               pr_err("bad integer in %s\n", opt_str);
24340 +                               break;
24341 +                       }
24342 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
24343 +                       if (!err)
24344 +                               opt->type = token;
24345 +                       break;
24346 +#endif
24347 +               case Opt_xino:
24348 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
24349 +                       if (!err)
24350 +                               opt->type = token;
24351 +                       break;
24352 +
24353 +               case Opt_trunc_xino_path:
24354 +                       err = au_opts_parse_xino_itrunc_path
24355 +                               (sb, &opt->xino_itrunc, a->args);
24356 +                       if (!err)
24357 +                               opt->type = token;
24358 +                       break;
24359 +
24360 +               case Opt_itrunc_xino:
24361 +                       u.xino_itrunc = &opt->xino_itrunc;
24362 +                       if (unlikely(match_int(&a->args[0], &n))) {
24363 +                               pr_err("bad integer in %s\n", opt_str);
24364 +                               break;
24365 +                       }
24366 +                       u.xino_itrunc->bindex = n;
24367 +                       aufs_read_lock(root, AuLock_FLUSH);
24368 +                       if (n < 0 || au_sbbot(sb) < n) {
24369 +                               pr_err("out of bounds, %d\n", n);
24370 +                               aufs_read_unlock(root, !AuLock_IR);
24371 +                               break;
24372 +                       }
24373 +                       aufs_read_unlock(root, !AuLock_IR);
24374 +                       err = 0;
24375 +                       opt->type = token;
24376 +                       break;
24377 +
24378 +               case Opt_dirwh:
24379 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
24380 +                               break;
24381 +                       err = 0;
24382 +                       opt->type = token;
24383 +                       break;
24384 +
24385 +               case Opt_rdcache:
24386 +                       if (unlikely(match_int(&a->args[0], &n))) {
24387 +                               pr_err("bad integer in %s\n", opt_str);
24388 +                               break;
24389 +                       }
24390 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
24391 +                               pr_err("rdcache must be smaller than %d\n",
24392 +                                      AUFS_RDCACHE_MAX);
24393 +                               break;
24394 +                       }
24395 +                       opt->rdcache = n;
24396 +                       err = 0;
24397 +                       opt->type = token;
24398 +                       break;
24399 +               case Opt_rdblk:
24400 +                       if (unlikely(match_int(&a->args[0], &n)
24401 +                                    || n < 0
24402 +                                    || n > KMALLOC_MAX_SIZE)) {
24403 +                               pr_err("bad integer in %s\n", opt_str);
24404 +                               break;
24405 +                       }
24406 +                       if (unlikely(n && n < NAME_MAX)) {
24407 +                               pr_err("rdblk must be larger than %d\n",
24408 +                                      NAME_MAX);
24409 +                               break;
24410 +                       }
24411 +                       opt->rdblk = n;
24412 +                       err = 0;
24413 +                       opt->type = token;
24414 +                       break;
24415 +               case Opt_rdhash:
24416 +                       if (unlikely(match_int(&a->args[0], &n)
24417 +                                    || n < 0
24418 +                                    || n * sizeof(struct hlist_head)
24419 +                                    > KMALLOC_MAX_SIZE)) {
24420 +                               pr_err("bad integer in %s\n", opt_str);
24421 +                               break;
24422 +                       }
24423 +                       opt->rdhash = n;
24424 +                       err = 0;
24425 +                       opt->type = token;
24426 +                       break;
24427 +
24428 +               case Opt_trunc_xino:
24429 +               case Opt_notrunc_xino:
24430 +               case Opt_noxino:
24431 +               case Opt_trunc_xib:
24432 +               case Opt_notrunc_xib:
24433 +               case Opt_shwh:
24434 +               case Opt_noshwh:
24435 +               case Opt_dirperm1:
24436 +               case Opt_nodirperm1:
24437 +               case Opt_plink:
24438 +               case Opt_noplink:
24439 +               case Opt_list_plink:
24440 +               case Opt_dio:
24441 +               case Opt_nodio:
24442 +               case Opt_diropq_a:
24443 +               case Opt_diropq_w:
24444 +               case Opt_warn_perm:
24445 +               case Opt_nowarn_perm:
24446 +               case Opt_verbose:
24447 +               case Opt_noverbose:
24448 +               case Opt_sum:
24449 +               case Opt_nosum:
24450 +               case Opt_wsum:
24451 +               case Opt_rdblk_def:
24452 +               case Opt_rdhash_def:
24453 +               case Opt_acl:
24454 +               case Opt_noacl:
24455 +                       err = 0;
24456 +                       opt->type = token;
24457 +                       break;
24458 +
24459 +               case Opt_udba:
24460 +                       opt->udba = udba_val(a->args[0].from);
24461 +                       if (opt->udba >= 0) {
24462 +                               err = 0;
24463 +                               opt->type = token;
24464 +                       } else
24465 +                               pr_err("wrong value, %s\n", opt_str);
24466 +                       break;
24467 +
24468 +               case Opt_wbr_create:
24469 +                       u.create = &opt->wbr_create;
24470 +                       u.create->wbr_create
24471 +                               = au_wbr_create_val(a->args[0].from, u.create);
24472 +                       if (u.create->wbr_create >= 0) {
24473 +                               err = 0;
24474 +                               opt->type = token;
24475 +                       } else
24476 +                               pr_err("wrong value, %s\n", opt_str);
24477 +                       break;
24478 +               case Opt_wbr_copyup:
24479 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
24480 +                       if (opt->wbr_copyup >= 0) {
24481 +                               err = 0;
24482 +                               opt->type = token;
24483 +                       } else
24484 +                               pr_err("wrong value, %s\n", opt_str);
24485 +                       break;
24486 +
24487 +               case Opt_fhsm_sec:
24488 +                       if (unlikely(match_int(&a->args[0], &n)
24489 +                                    || n < 0)) {
24490 +                               pr_err("bad integer in %s\n", opt_str);
24491 +                               break;
24492 +                       }
24493 +                       if (sysaufs_brs) {
24494 +                               opt->fhsm_second = n;
24495 +                               opt->type = token;
24496 +                       } else
24497 +                               pr_warn("ignored %s\n", opt_str);
24498 +                       err = 0;
24499 +                       break;
24500 +
24501 +               case Opt_ignore:
24502 +                       pr_warn("ignored %s\n", opt_str);
24503 +                       /*FALLTHROUGH*/
24504 +               case Opt_ignore_silent:
24505 +                       skipped = 1;
24506 +                       err = 0;
24507 +                       break;
24508 +               case Opt_err:
24509 +                       pr_err("unknown option %s\n", opt_str);
24510 +                       break;
24511 +               }
24512 +
24513 +               if (!err && !skipped) {
24514 +                       if (unlikely(++opt > opt_tail)) {
24515 +                               err = -E2BIG;
24516 +                               opt--;
24517 +                               opt->type = Opt_tail;
24518 +                               break;
24519 +                       }
24520 +                       opt->type = Opt_tail;
24521 +               }
24522 +       }
24523 +
24524 +       au_delayed_kfree(a);
24525 +       dump_opts(opts);
24526 +       if (unlikely(err))
24527 +               au_opts_free(opts);
24528 +
24529 +out:
24530 +       return err;
24531 +}
24532 +
24533 +static int au_opt_wbr_create(struct super_block *sb,
24534 +                            struct au_opt_wbr_create *create)
24535 +{
24536 +       int err;
24537 +       struct au_sbinfo *sbinfo;
24538 +
24539 +       SiMustWriteLock(sb);
24540 +
24541 +       err = 1; /* handled */
24542 +       sbinfo = au_sbi(sb);
24543 +       if (sbinfo->si_wbr_create_ops->fin) {
24544 +               err = sbinfo->si_wbr_create_ops->fin(sb);
24545 +               if (!err)
24546 +                       err = 1;
24547 +       }
24548 +
24549 +       sbinfo->si_wbr_create = create->wbr_create;
24550 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
24551 +       switch (create->wbr_create) {
24552 +       case AuWbrCreate_MFSRRV:
24553 +       case AuWbrCreate_MFSRR:
24554 +       case AuWbrCreate_TDMFS:
24555 +       case AuWbrCreate_TDMFSV:
24556 +       case AuWbrCreate_PMFSRR:
24557 +       case AuWbrCreate_PMFSRRV:
24558 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
24559 +               /*FALLTHROUGH*/
24560 +       case AuWbrCreate_MFS:
24561 +       case AuWbrCreate_MFSV:
24562 +       case AuWbrCreate_PMFS:
24563 +       case AuWbrCreate_PMFSV:
24564 +               sbinfo->si_wbr_mfs.mfs_expire
24565 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
24566 +               break;
24567 +       }
24568 +
24569 +       if (sbinfo->si_wbr_create_ops->init)
24570 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
24571 +
24572 +       return err;
24573 +}
24574 +
24575 +/*
24576 + * returns,
24577 + * plus: processed without an error
24578 + * zero: unprocessed
24579 + */
24580 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
24581 +                        struct au_opts *opts)
24582 +{
24583 +       int err;
24584 +       struct au_sbinfo *sbinfo;
24585 +
24586 +       SiMustWriteLock(sb);
24587 +
24588 +       err = 1; /* handled */
24589 +       sbinfo = au_sbi(sb);
24590 +       switch (opt->type) {
24591 +       case Opt_udba:
24592 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24593 +               sbinfo->si_mntflags |= opt->udba;
24594 +               opts->given_udba |= opt->udba;
24595 +               break;
24596 +
24597 +       case Opt_plink:
24598 +               au_opt_set(sbinfo->si_mntflags, PLINK);
24599 +               break;
24600 +       case Opt_noplink:
24601 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24602 +                       au_plink_put(sb, /*verbose*/1);
24603 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
24604 +               break;
24605 +       case Opt_list_plink:
24606 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
24607 +                       au_plink_list(sb);
24608 +               break;
24609 +
24610 +       case Opt_dio:
24611 +               au_opt_set(sbinfo->si_mntflags, DIO);
24612 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24613 +               break;
24614 +       case Opt_nodio:
24615 +               au_opt_clr(sbinfo->si_mntflags, DIO);
24616 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
24617 +               break;
24618 +
24619 +       case Opt_fhsm_sec:
24620 +               au_fhsm_set(sbinfo, opt->fhsm_second);
24621 +               break;
24622 +
24623 +       case Opt_diropq_a:
24624 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24625 +               break;
24626 +       case Opt_diropq_w:
24627 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24628 +               break;
24629 +
24630 +       case Opt_warn_perm:
24631 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
24632 +               break;
24633 +       case Opt_nowarn_perm:
24634 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
24635 +               break;
24636 +
24637 +       case Opt_verbose:
24638 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
24639 +               break;
24640 +       case Opt_noverbose:
24641 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
24642 +               break;
24643 +
24644 +       case Opt_sum:
24645 +               au_opt_set(sbinfo->si_mntflags, SUM);
24646 +               break;
24647 +       case Opt_wsum:
24648 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24649 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
24650 +       case Opt_nosum:
24651 +               au_opt_clr(sbinfo->si_mntflags, SUM);
24652 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
24653 +               break;
24654 +
24655 +       case Opt_wbr_create:
24656 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
24657 +               break;
24658 +       case Opt_wbr_copyup:
24659 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
24660 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
24661 +               break;
24662 +
24663 +       case Opt_dirwh:
24664 +               sbinfo->si_dirwh = opt->dirwh;
24665 +               break;
24666 +
24667 +       case Opt_rdcache:
24668 +               sbinfo->si_rdcache
24669 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
24670 +               break;
24671 +       case Opt_rdblk:
24672 +               sbinfo->si_rdblk = opt->rdblk;
24673 +               break;
24674 +       case Opt_rdblk_def:
24675 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24676 +               break;
24677 +       case Opt_rdhash:
24678 +               sbinfo->si_rdhash = opt->rdhash;
24679 +               break;
24680 +       case Opt_rdhash_def:
24681 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24682 +               break;
24683 +
24684 +       case Opt_shwh:
24685 +               au_opt_set(sbinfo->si_mntflags, SHWH);
24686 +               break;
24687 +       case Opt_noshwh:
24688 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
24689 +               break;
24690 +
24691 +       case Opt_dirperm1:
24692 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24693 +               break;
24694 +       case Opt_nodirperm1:
24695 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24696 +               break;
24697 +
24698 +       case Opt_trunc_xino:
24699 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24700 +               break;
24701 +       case Opt_notrunc_xino:
24702 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24703 +               break;
24704 +
24705 +       case Opt_trunc_xino_path:
24706 +       case Opt_itrunc_xino:
24707 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24708 +               if (!err)
24709 +                       err = 1;
24710 +               break;
24711 +
24712 +       case Opt_trunc_xib:
24713 +               au_fset_opts(opts->flags, TRUNC_XIB);
24714 +               break;
24715 +       case Opt_notrunc_xib:
24716 +               au_fclr_opts(opts->flags, TRUNC_XIB);
24717 +               break;
24718 +
24719 +       case Opt_acl:
24720 +               sb->s_flags |= MS_POSIXACL;
24721 +               break;
24722 +       case Opt_noacl:
24723 +               sb->s_flags &= ~MS_POSIXACL;
24724 +               break;
24725 +
24726 +       default:
24727 +               err = 0;
24728 +               break;
24729 +       }
24730 +
24731 +       return err;
24732 +}
24733 +
24734 +/*
24735 + * returns tri-state.
24736 + * plus: processed without an error
24737 + * zero: unprocessed
24738 + * minus: error
24739 + */
24740 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24741 +                    struct au_opts *opts)
24742 +{
24743 +       int err, do_refresh;
24744 +
24745 +       err = 0;
24746 +       switch (opt->type) {
24747 +       case Opt_append:
24748 +               opt->add.bindex = au_sbbot(sb) + 1;
24749 +               if (opt->add.bindex < 0)
24750 +                       opt->add.bindex = 0;
24751 +               goto add;
24752 +       case Opt_prepend:
24753 +               opt->add.bindex = 0;
24754 +       add: /* indented label */
24755 +       case Opt_add:
24756 +               err = au_br_add(sb, &opt->add,
24757 +                               au_ftest_opts(opts->flags, REMOUNT));
24758 +               if (!err) {
24759 +                       err = 1;
24760 +                       au_fset_opts(opts->flags, REFRESH);
24761 +               }
24762 +               break;
24763 +
24764 +       case Opt_del:
24765 +       case Opt_idel:
24766 +               err = au_br_del(sb, &opt->del,
24767 +                               au_ftest_opts(opts->flags, REMOUNT));
24768 +               if (!err) {
24769 +                       err = 1;
24770 +                       au_fset_opts(opts->flags, TRUNC_XIB);
24771 +                       au_fset_opts(opts->flags, REFRESH);
24772 +               }
24773 +               break;
24774 +
24775 +       case Opt_mod:
24776 +       case Opt_imod:
24777 +               err = au_br_mod(sb, &opt->mod,
24778 +                               au_ftest_opts(opts->flags, REMOUNT),
24779 +                               &do_refresh);
24780 +               if (!err) {
24781 +                       err = 1;
24782 +                       if (do_refresh)
24783 +                               au_fset_opts(opts->flags, REFRESH);
24784 +               }
24785 +               break;
24786 +       }
24787 +
24788 +       return err;
24789 +}
24790 +
24791 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24792 +                      struct au_opt_xino **opt_xino,
24793 +                      struct au_opts *opts)
24794 +{
24795 +       int err;
24796 +       aufs_bindex_t bbot, bindex;
24797 +       struct dentry *root, *parent, *h_root;
24798 +
24799 +       err = 0;
24800 +       switch (opt->type) {
24801 +       case Opt_xino:
24802 +               err = au_xino_set(sb, &opt->xino,
24803 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
24804 +               if (unlikely(err))
24805 +                       break;
24806 +
24807 +               *opt_xino = &opt->xino;
24808 +               au_xino_brid_set(sb, -1);
24809 +
24810 +               /* safe d_parent access */
24811 +               parent = opt->xino.file->f_path.dentry->d_parent;
24812 +               root = sb->s_root;
24813 +               bbot = au_sbbot(sb);
24814 +               for (bindex = 0; bindex <= bbot; bindex++) {
24815 +                       h_root = au_h_dptr(root, bindex);
24816 +                       if (h_root == parent) {
24817 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24818 +                               break;
24819 +                       }
24820 +               }
24821 +               break;
24822 +
24823 +       case Opt_noxino:
24824 +               au_xino_clr(sb);
24825 +               au_xino_brid_set(sb, -1);
24826 +               *opt_xino = (void *)-1;
24827 +               break;
24828 +       }
24829 +
24830 +       return err;
24831 +}
24832 +
24833 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24834 +                  unsigned int pending)
24835 +{
24836 +       int err, fhsm;
24837 +       aufs_bindex_t bindex, bbot;
24838 +       unsigned char do_plink, skip, do_free, can_no_dreval;
24839 +       struct au_branch *br;
24840 +       struct au_wbr *wbr;
24841 +       struct dentry *root, *dentry;
24842 +       struct inode *dir, *h_dir;
24843 +       struct au_sbinfo *sbinfo;
24844 +       struct au_hinode *hdir;
24845 +
24846 +       SiMustAnyLock(sb);
24847 +
24848 +       sbinfo = au_sbi(sb);
24849 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24850 +
24851 +       if (!(sb_flags & MS_RDONLY)) {
24852 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
24853 +                       pr_warn("first branch should be rw\n");
24854 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
24855 +                       pr_warn_once("shwh should be used with ro\n");
24856 +       }
24857 +
24858 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
24859 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
24860 +               pr_warn_once("udba=*notify requires xino\n");
24861 +
24862 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
24863 +               pr_warn_once("dirperm1 breaks the protection"
24864 +                            " by the permission bits on the lower branch\n");
24865 +
24866 +       err = 0;
24867 +       fhsm = 0;
24868 +       root = sb->s_root;
24869 +       dir = d_inode(root);
24870 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
24871 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
24872 +                                     UDBA_NONE);
24873 +       bbot = au_sbbot(sb);
24874 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
24875 +               skip = 0;
24876 +               h_dir = au_h_iptr(dir, bindex);
24877 +               br = au_sbr(sb, bindex);
24878 +
24879 +               if ((br->br_perm & AuBrAttr_ICEX)
24880 +                   && !h_dir->i_op->listxattr)
24881 +                       br->br_perm &= ~AuBrAttr_ICEX;
24882 +#if 0
24883 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
24884 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
24885 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
24886 +#endif
24887 +
24888 +               do_free = 0;
24889 +               wbr = br->br_wbr;
24890 +               if (wbr)
24891 +                       wbr_wh_read_lock(wbr);
24892 +
24893 +               if (!au_br_writable(br->br_perm)) {
24894 +                       do_free = !!wbr;
24895 +                       skip = (!wbr
24896 +                               || (!wbr->wbr_whbase
24897 +                                   && !wbr->wbr_plink
24898 +                                   && !wbr->wbr_orph));
24899 +               } else if (!au_br_wh_linkable(br->br_perm)) {
24900 +                       /* skip = (!br->br_whbase && !br->br_orph); */
24901 +                       skip = (!wbr || !wbr->wbr_whbase);
24902 +                       if (skip && wbr) {
24903 +                               if (do_plink)
24904 +                                       skip = !!wbr->wbr_plink;
24905 +                               else
24906 +                                       skip = !wbr->wbr_plink;
24907 +                       }
24908 +               } else {
24909 +                       /* skip = (br->br_whbase && br->br_ohph); */
24910 +                       skip = (wbr && wbr->wbr_whbase);
24911 +                       if (skip) {
24912 +                               if (do_plink)
24913 +                                       skip = !!wbr->wbr_plink;
24914 +                               else
24915 +                                       skip = !wbr->wbr_plink;
24916 +                       }
24917 +               }
24918 +               if (wbr)
24919 +                       wbr_wh_read_unlock(wbr);
24920 +
24921 +               if (can_no_dreval) {
24922 +                       dentry = br->br_path.dentry;
24923 +                       spin_lock(&dentry->d_lock);
24924 +                       if (dentry->d_flags &
24925 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
24926 +                               can_no_dreval = 0;
24927 +                       spin_unlock(&dentry->d_lock);
24928 +               }
24929 +
24930 +               if (au_br_fhsm(br->br_perm)) {
24931 +                       fhsm++;
24932 +                       AuDebugOn(!br->br_fhsm);
24933 +               }
24934 +
24935 +               if (skip)
24936 +                       continue;
24937 +
24938 +               hdir = au_hi(dir, bindex);
24939 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
24940 +               if (wbr)
24941 +                       wbr_wh_write_lock(wbr);
24942 +               err = au_wh_init(br, sb);
24943 +               if (wbr)
24944 +                       wbr_wh_write_unlock(wbr);
24945 +               au_hn_inode_unlock(hdir);
24946 +
24947 +               if (!err && do_free) {
24948 +                       if (wbr)
24949 +                               au_delayed_kfree(wbr);
24950 +                       br->br_wbr = NULL;
24951 +               }
24952 +       }
24953 +
24954 +       if (can_no_dreval)
24955 +               au_fset_si(sbinfo, NO_DREVAL);
24956 +       else
24957 +               au_fclr_si(sbinfo, NO_DREVAL);
24958 +
24959 +       if (fhsm >= 2) {
24960 +               au_fset_si(sbinfo, FHSM);
24961 +               for (bindex = bbot; bindex >= 0; bindex--) {
24962 +                       br = au_sbr(sb, bindex);
24963 +                       if (au_br_fhsm(br->br_perm)) {
24964 +                               au_fhsm_set_bottom(sb, bindex);
24965 +                               break;
24966 +                       }
24967 +               }
24968 +       } else {
24969 +               au_fclr_si(sbinfo, FHSM);
24970 +               au_fhsm_set_bottom(sb, -1);
24971 +       }
24972 +
24973 +       return err;
24974 +}
24975 +
24976 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24977 +{
24978 +       int err;
24979 +       unsigned int tmp;
24980 +       aufs_bindex_t bindex, bbot;
24981 +       struct au_opt *opt;
24982 +       struct au_opt_xino *opt_xino, xino;
24983 +       struct au_sbinfo *sbinfo;
24984 +       struct au_branch *br;
24985 +       struct inode *dir;
24986 +
24987 +       SiMustWriteLock(sb);
24988 +
24989 +       err = 0;
24990 +       opt_xino = NULL;
24991 +       opt = opts->opt;
24992 +       while (err >= 0 && opt->type != Opt_tail)
24993 +               err = au_opt_simple(sb, opt++, opts);
24994 +       if (err > 0)
24995 +               err = 0;
24996 +       else if (unlikely(err < 0))
24997 +               goto out;
24998 +
24999 +       /* disable xino and udba temporary */
25000 +       sbinfo = au_sbi(sb);
25001 +       tmp = sbinfo->si_mntflags;
25002 +       au_opt_clr(sbinfo->si_mntflags, XINO);
25003 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
25004 +
25005 +       opt = opts->opt;
25006 +       while (err >= 0 && opt->type != Opt_tail)
25007 +               err = au_opt_br(sb, opt++, opts);
25008 +       if (err > 0)
25009 +               err = 0;
25010 +       else if (unlikely(err < 0))
25011 +               goto out;
25012 +
25013 +       bbot = au_sbbot(sb);
25014 +       if (unlikely(bbot < 0)) {
25015 +               err = -EINVAL;
25016 +               pr_err("no branches\n");
25017 +               goto out;
25018 +       }
25019 +
25020 +       if (au_opt_test(tmp, XINO))
25021 +               au_opt_set(sbinfo->si_mntflags, XINO);
25022 +       opt = opts->opt;
25023 +       while (!err && opt->type != Opt_tail)
25024 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
25025 +       if (unlikely(err))
25026 +               goto out;
25027 +
25028 +       err = au_opts_verify(sb, sb->s_flags, tmp);
25029 +       if (unlikely(err))
25030 +               goto out;
25031 +
25032 +       /* restore xino */
25033 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
25034 +               xino.file = au_xino_def(sb);
25035 +               err = PTR_ERR(xino.file);
25036 +               if (IS_ERR(xino.file))
25037 +                       goto out;
25038 +
25039 +               err = au_xino_set(sb, &xino, /*remount*/0);
25040 +               fput(xino.file);
25041 +               if (unlikely(err))
25042 +                       goto out;
25043 +       }
25044 +
25045 +       /* restore udba */
25046 +       tmp &= AuOptMask_UDBA;
25047 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
25048 +       sbinfo->si_mntflags |= tmp;
25049 +       bbot = au_sbbot(sb);
25050 +       for (bindex = 0; bindex <= bbot; bindex++) {
25051 +               br = au_sbr(sb, bindex);
25052 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
25053 +               if (unlikely(err))
25054 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
25055 +                               bindex, err);
25056 +               /* go on even if err */
25057 +       }
25058 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
25059 +               dir = d_inode(sb->s_root);
25060 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
25061 +       }
25062 +
25063 +out:
25064 +       return err;
25065 +}
25066 +
25067 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
25068 +{
25069 +       int err, rerr;
25070 +       unsigned char no_dreval;
25071 +       struct inode *dir;
25072 +       struct au_opt_xino *opt_xino;
25073 +       struct au_opt *opt;
25074 +       struct au_sbinfo *sbinfo;
25075 +
25076 +       SiMustWriteLock(sb);
25077 +
25078 +       err = 0;
25079 +       dir = d_inode(sb->s_root);
25080 +       sbinfo = au_sbi(sb);
25081 +       opt_xino = NULL;
25082 +       opt = opts->opt;
25083 +       while (err >= 0 && opt->type != Opt_tail) {
25084 +               err = au_opt_simple(sb, opt, opts);
25085 +               if (!err)
25086 +                       err = au_opt_br(sb, opt, opts);
25087 +               if (!err)
25088 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
25089 +               opt++;
25090 +       }
25091 +       if (err > 0)
25092 +               err = 0;
25093 +       AuTraceErr(err);
25094 +       /* go on even err */
25095 +
25096 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
25097 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
25098 +       if (unlikely(rerr && !err))
25099 +               err = rerr;
25100 +
25101 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
25102 +               au_fset_opts(opts->flags, REFRESH_IDOP);
25103 +
25104 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
25105 +               rerr = au_xib_trunc(sb);
25106 +               if (unlikely(rerr && !err))
25107 +                       err = rerr;
25108 +       }
25109 +
25110 +       /* will be handled by the caller */
25111 +       if (!au_ftest_opts(opts->flags, REFRESH)
25112 +           && (opts->given_udba
25113 +               || au_opt_test(sbinfo->si_mntflags, XINO)
25114 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
25115 +                   ))
25116 +               au_fset_opts(opts->flags, REFRESH);
25117 +
25118 +       AuDbg("status 0x%x\n", opts->flags);
25119 +       return err;
25120 +}
25121 +
25122 +/* ---------------------------------------------------------------------- */
25123 +
25124 +unsigned int au_opt_udba(struct super_block *sb)
25125 +{
25126 +       return au_mntflags(sb) & AuOptMask_UDBA;
25127 +}
25128 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
25129 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
25130 +++ linux/fs/aufs/opts.h        2017-03-02 14:24:13.863589061 +0100
25131 @@ -0,0 +1,213 @@
25132 +/*
25133 + * Copyright (C) 2005-2017 Junjiro R. Okajima
25134 + *
25135 + * This program, aufs is free software; you can redistribute it and/or modify
25136 + * it under the terms of the GNU General Public License as published by
25137 + * the Free Software Foundation; either version 2 of the License, or
25138 + * (at your option) any later version.
25139 + *
25140 + * This program is distributed in the hope that it will be useful,
25141 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25142 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25143 + * GNU General Public License for more details.
25144 + *
25145 + * You should have received a copy of the GNU General Public License
25146 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25147 + */
25148 +
25149 +/*
25150 + * mount options/flags
25151 + */
25152 +
25153 +#ifndef __AUFS_OPTS_H__
25154 +#define __AUFS_OPTS_H__
25155 +
25156 +#ifdef __KERNEL__
25157 +
25158 +#include <linux/path.h>
25159 +
25160 +struct file;
25161 +struct super_block;
25162 +
25163 +/* ---------------------------------------------------------------------- */
25164 +
25165 +/* mount flags */
25166 +#define AuOpt_XINO             1               /* external inode number bitmap
25167 +                                                  and translation table */
25168 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
25169 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
25170 +#define AuOpt_UDBA_REVAL       (1 << 3)
25171 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
25172 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
25173 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
25174 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
25175 +                                                  bits */
25176 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
25177 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
25178 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
25179 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
25180 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
25181 +#define AuOpt_DIO              (1 << 14)       /* direct io */
25182 +
25183 +#ifndef CONFIG_AUFS_HNOTIFY
25184 +#undef AuOpt_UDBA_HNOTIFY
25185 +#define AuOpt_UDBA_HNOTIFY     0
25186 +#endif
25187 +#ifndef CONFIG_AUFS_SHWH
25188 +#undef AuOpt_SHWH
25189 +#define AuOpt_SHWH             0
25190 +#endif
25191 +
25192 +#define AuOpt_Def      (AuOpt_XINO \
25193 +                        | AuOpt_UDBA_REVAL \
25194 +                        | AuOpt_PLINK \
25195 +                        /* | AuOpt_DIRPERM1 */ \
25196 +                        | AuOpt_WARN_PERM)
25197 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
25198 +                        | AuOpt_UDBA_REVAL \
25199 +                        | AuOpt_UDBA_HNOTIFY)
25200 +
25201 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
25202 +#define au_opt_set(flags, name) do { \
25203 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
25204 +       ((flags) |= AuOpt_##name); \
25205 +} while (0)
25206 +#define au_opt_set_udba(flags, name) do { \
25207 +       (flags) &= ~AuOptMask_UDBA; \
25208 +       ((flags) |= AuOpt_##name); \
25209 +} while (0)
25210 +#define au_opt_clr(flags, name) do { \
25211 +       ((flags) &= ~AuOpt_##name); \
25212 +} while (0)
25213 +
25214 +static inline unsigned int au_opts_plink(unsigned int mntflags)
25215 +{
25216 +#ifdef CONFIG_PROC_FS
25217 +       return mntflags;
25218 +#else
25219 +       return mntflags & ~AuOpt_PLINK;
25220 +#endif
25221 +}
25222 +
25223 +/* ---------------------------------------------------------------------- */
25224 +
25225 +/* policies to select one among multiple writable branches */
25226 +enum {
25227 +       AuWbrCreate_TDP,        /* top down parent */
25228 +       AuWbrCreate_RR,         /* round robin */
25229 +       AuWbrCreate_MFS,        /* most free space */
25230 +       AuWbrCreate_MFSV,       /* mfs with seconds */
25231 +       AuWbrCreate_MFSRR,      /* mfs then rr */
25232 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
25233 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
25234 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
25235 +       AuWbrCreate_PMFS,       /* parent and mfs */
25236 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
25237 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
25238 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
25239 +
25240 +       AuWbrCreate_Def = AuWbrCreate_TDP
25241 +};
25242 +
25243 +enum {
25244 +       AuWbrCopyup_TDP,        /* top down parent */
25245 +       AuWbrCopyup_BUP,        /* bottom up parent */
25246 +       AuWbrCopyup_BU,         /* bottom up */
25247 +
25248 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
25249 +};
25250 +
25251 +/* ---------------------------------------------------------------------- */
25252 +
25253 +struct au_opt_add {
25254 +       aufs_bindex_t   bindex;
25255 +       char            *pathname;
25256 +       int             perm;
25257 +       struct path     path;
25258 +};
25259 +
25260 +struct au_opt_del {
25261 +       char            *pathname;
25262 +       struct path     h_path;
25263 +};
25264 +
25265 +struct au_opt_mod {
25266 +       char            *path;
25267 +       int             perm;
25268 +       struct dentry   *h_root;
25269 +};
25270 +
25271 +struct au_opt_xino {
25272 +       char            *path;
25273 +       struct file     *file;
25274 +};
25275 +
25276 +struct au_opt_xino_itrunc {
25277 +       aufs_bindex_t   bindex;
25278 +};
25279 +
25280 +struct au_opt_wbr_create {
25281 +       int                     wbr_create;
25282 +       int                     mfs_second;
25283 +       unsigned long long      mfsrr_watermark;
25284 +};
25285 +
25286 +struct au_opt {
25287 +       int type;
25288 +       union {
25289 +               struct au_opt_xino      xino;
25290 +               struct au_opt_xino_itrunc xino_itrunc;
25291 +               struct au_opt_add       add;
25292 +               struct au_opt_del       del;
25293 +               struct au_opt_mod       mod;
25294 +               int                     dirwh;
25295 +               int                     rdcache;
25296 +               unsigned int            rdblk;
25297 +               unsigned int            rdhash;
25298 +               int                     udba;
25299 +               struct au_opt_wbr_create wbr_create;
25300 +               int                     wbr_copyup;
25301 +               unsigned int            fhsm_second;
25302 +       };
25303 +};
25304 +
25305 +/* opts flags */
25306 +#define AuOpts_REMOUNT         1
25307 +#define AuOpts_REFRESH         (1 << 1)
25308 +#define AuOpts_TRUNC_XIB       (1 << 2)
25309 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
25310 +#define AuOpts_REFRESH_IDOP    (1 << 4)
25311 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
25312 +#define au_fset_opts(flags, name) \
25313 +       do { (flags) |= AuOpts_##name; } while (0)
25314 +#define au_fclr_opts(flags, name) \
25315 +       do { (flags) &= ~AuOpts_##name; } while (0)
25316 +
25317 +struct au_opts {
25318 +       struct au_opt   *opt;
25319 +       int             max_opt;
25320 +
25321 +       unsigned int    given_udba;
25322 +       unsigned int    flags;
25323 +       unsigned long   sb_flags;
25324 +};
25325 +
25326 +/* ---------------------------------------------------------------------- */
25327 +
25328 +/* opts.c */
25329 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
25330 +const char *au_optstr_udba(int udba);
25331 +const char *au_optstr_wbr_copyup(int wbr_copyup);
25332 +const char *au_optstr_wbr_create(int wbr_create);
25333 +
25334 +void au_opts_free(struct au_opts *opts);
25335 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
25336 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
25337 +                  unsigned int pending);
25338 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
25339 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
25340 +
25341 +unsigned int au_opt_udba(struct super_block *sb);
25342 +
25343 +#endif /* __KERNEL__ */
25344 +#endif /* __AUFS_OPTS_H__ */
25345 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
25346 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
25347 +++ linux/fs/aufs/plink.c       2017-03-02 14:24:13.866922487 +0100
25348 @@ -0,0 +1,514 @@
25349 +/*
25350 + * Copyright (C) 2005-2017 Junjiro R. Okajima
25351 + *
25352 + * This program, aufs is free software; you can redistribute it and/or modify
25353 + * it under the terms of the GNU General Public License as published by
25354 + * the Free Software Foundation; either version 2 of the License, or
25355 + * (at your option) any later version.
25356 + *
25357 + * This program is distributed in the hope that it will be useful,
25358 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25359 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25360 + * GNU General Public License for more details.
25361 + *
25362 + * You should have received a copy of the GNU General Public License
25363 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25364 + */
25365 +
25366 +/*
25367 + * pseudo-link
25368 + */
25369 +
25370 +#include "aufs.h"
25371 +
25372 +/*
25373 + * the pseudo-link maintenance mode.
25374 + * during a user process maintains the pseudo-links,
25375 + * prohibit adding a new plink and branch manipulation.
25376 + *
25377 + * Flags
25378 + * NOPLM:
25379 + *     For entry functions which will handle plink, and i_mutex is already held
25380 + *     in VFS.
25381 + *     They cannot wait and should return an error at once.
25382 + *     Callers has to check the error.
25383 + * NOPLMW:
25384 + *     For entry functions which will handle plink, but i_mutex is not held
25385 + *     in VFS.
25386 + *     They can wait the plink maintenance mode to finish.
25387 + *
25388 + * They behave like F_SETLK and F_SETLKW.
25389 + * If the caller never handle plink, then both flags are unnecessary.
25390 + */
25391 +
25392 +int au_plink_maint(struct super_block *sb, int flags)
25393 +{
25394 +       int err;
25395 +       pid_t pid, ppid;
25396 +       struct task_struct *parent, *prev;
25397 +       struct au_sbinfo *sbi;
25398 +
25399 +       SiMustAnyLock(sb);
25400 +
25401 +       err = 0;
25402 +       if (!au_opt_test(au_mntflags(sb), PLINK))
25403 +               goto out;
25404 +
25405 +       sbi = au_sbi(sb);
25406 +       pid = sbi->si_plink_maint_pid;
25407 +       if (!pid || pid == current->pid)
25408 +               goto out;
25409 +
25410 +       /* todo: it highly depends upon /sbin/mount.aufs */
25411 +       prev = NULL;
25412 +       parent = current;
25413 +       ppid = 0;
25414 +       rcu_read_lock();
25415 +       while (1) {
25416 +               parent = rcu_dereference(parent->real_parent);
25417 +               if (parent == prev)
25418 +                       break;
25419 +               ppid = task_pid_vnr(parent);
25420 +               if (pid == ppid) {
25421 +                       rcu_read_unlock();
25422 +                       goto out;
25423 +               }
25424 +               prev = parent;
25425 +       }
25426 +       rcu_read_unlock();
25427 +
25428 +       if (au_ftest_lock(flags, NOPLMW)) {
25429 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
25430 +               /* AuDebugOn(!lockdep_depth(current)); */
25431 +               while (sbi->si_plink_maint_pid) {
25432 +                       si_read_unlock(sb);
25433 +                       /* gave up wake_up_bit() */
25434 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
25435 +
25436 +                       if (au_ftest_lock(flags, FLUSH))
25437 +                               au_nwt_flush(&sbi->si_nowait);
25438 +                       si_noflush_read_lock(sb);
25439 +               }
25440 +       } else if (au_ftest_lock(flags, NOPLM)) {
25441 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
25442 +               err = -EAGAIN;
25443 +       }
25444 +
25445 +out:
25446 +       return err;
25447 +}
25448 +
25449 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
25450 +{
25451 +       spin_lock(&sbinfo->si_plink_maint_lock);
25452 +       sbinfo->si_plink_maint_pid = 0;
25453 +       spin_unlock(&sbinfo->si_plink_maint_lock);
25454 +       wake_up_all(&sbinfo->si_plink_wq);
25455 +}
25456 +
25457 +int au_plink_maint_enter(struct super_block *sb)
25458 +{
25459 +       int err;
25460 +       struct au_sbinfo *sbinfo;
25461 +
25462 +       err = 0;
25463 +       sbinfo = au_sbi(sb);
25464 +       /* make sure i am the only one in this fs */
25465 +       si_write_lock(sb, AuLock_FLUSH);
25466 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
25467 +               spin_lock(&sbinfo->si_plink_maint_lock);
25468 +               if (!sbinfo->si_plink_maint_pid)
25469 +                       sbinfo->si_plink_maint_pid = current->pid;
25470 +               else
25471 +                       err = -EBUSY;
25472 +               spin_unlock(&sbinfo->si_plink_maint_lock);
25473 +       }
25474 +       si_write_unlock(sb);
25475 +
25476 +       return err;
25477 +}
25478 +
25479 +/* ---------------------------------------------------------------------- */
25480 +
25481 +#ifdef CONFIG_AUFS_DEBUG
25482 +void au_plink_list(struct super_block *sb)
25483 +{
25484 +       int i;
25485 +       struct au_sbinfo *sbinfo;
25486 +       struct hlist_head *plink_hlist;
25487 +       struct au_icntnr *icntnr;
25488 +
25489 +       SiMustAnyLock(sb);
25490 +
25491 +       sbinfo = au_sbi(sb);
25492 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25493 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25494 +
25495 +       for (i = 0; i < AuPlink_NHASH; i++) {
25496 +               plink_hlist = &sbinfo->si_plink[i].head;
25497 +               rcu_read_lock();
25498 +               hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25499 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
25500 +               rcu_read_unlock();
25501 +       }
25502 +}
25503 +#endif
25504 +
25505 +/* is the inode pseudo-linked? */
25506 +int au_plink_test(struct inode *inode)
25507 +{
25508 +       int found, i;
25509 +       struct au_sbinfo *sbinfo;
25510 +       struct hlist_head *plink_hlist;
25511 +       struct au_icntnr *icntnr;
25512 +
25513 +       sbinfo = au_sbi(inode->i_sb);
25514 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
25515 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
25516 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25517 +
25518 +       found = 0;
25519 +       i = au_plink_hash(inode->i_ino);
25520 +       plink_hlist = &sbinfo->si_plink[i].head;
25521 +       rcu_read_lock();
25522 +       hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25523 +               if (&icntnr->vfs_inode == inode) {
25524 +                       found = 1;
25525 +                       break;
25526 +               }
25527 +       rcu_read_unlock();
25528 +       return found;
25529 +}
25530 +
25531 +/* ---------------------------------------------------------------------- */
25532 +
25533 +/*
25534 + * generate a name for plink.
25535 + * the file will be stored under AUFS_WH_PLINKDIR.
25536 + */
25537 +/* 20 is max digits length of ulong 64 */
25538 +#define PLINK_NAME_LEN ((20 + 1) * 2)
25539 +
25540 +static int plink_name(char *name, int len, struct inode *inode,
25541 +                     aufs_bindex_t bindex)
25542 +{
25543 +       int rlen;
25544 +       struct inode *h_inode;
25545 +
25546 +       h_inode = au_h_iptr(inode, bindex);
25547 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
25548 +       return rlen;
25549 +}
25550 +
25551 +struct au_do_plink_lkup_args {
25552 +       struct dentry **errp;
25553 +       struct qstr *tgtname;
25554 +       struct dentry *h_parent;
25555 +       struct au_branch *br;
25556 +};
25557 +
25558 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
25559 +                                      struct dentry *h_parent,
25560 +                                      struct au_branch *br)
25561 +{
25562 +       struct dentry *h_dentry;
25563 +       struct inode *h_inode;
25564 +
25565 +       h_inode = d_inode(h_parent);
25566 +       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
25567 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
25568 +       inode_unlock(h_inode);
25569 +       return h_dentry;
25570 +}
25571 +
25572 +static void au_call_do_plink_lkup(void *args)
25573 +{
25574 +       struct au_do_plink_lkup_args *a = args;
25575 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
25576 +}
25577 +
25578 +/* lookup the plink-ed @inode under the branch at @bindex */
25579 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
25580 +{
25581 +       struct dentry *h_dentry, *h_parent;
25582 +       struct au_branch *br;
25583 +       int wkq_err;
25584 +       char a[PLINK_NAME_LEN];
25585 +       struct qstr tgtname = QSTR_INIT(a, 0);
25586 +
25587 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25588 +
25589 +       br = au_sbr(inode->i_sb, bindex);
25590 +       h_parent = br->br_wbr->wbr_plink;
25591 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25592 +
25593 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25594 +               struct au_do_plink_lkup_args args = {
25595 +                       .errp           = &h_dentry,
25596 +                       .tgtname        = &tgtname,
25597 +                       .h_parent       = h_parent,
25598 +                       .br             = br
25599 +               };
25600 +
25601 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
25602 +               if (unlikely(wkq_err))
25603 +                       h_dentry = ERR_PTR(wkq_err);
25604 +       } else
25605 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
25606 +
25607 +       return h_dentry;
25608 +}
25609 +
25610 +/* create a pseudo-link */
25611 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
25612 +                     struct dentry *h_dentry, struct au_branch *br)
25613 +{
25614 +       int err;
25615 +       struct path h_path = {
25616 +               .mnt = au_br_mnt(br)
25617 +       };
25618 +       struct inode *h_dir, *delegated;
25619 +
25620 +       h_dir = d_inode(h_parent);
25621 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
25622 +again:
25623 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
25624 +       err = PTR_ERR(h_path.dentry);
25625 +       if (IS_ERR(h_path.dentry))
25626 +               goto out;
25627 +
25628 +       err = 0;
25629 +       /* wh.plink dir is not monitored */
25630 +       /* todo: is it really safe? */
25631 +       if (d_is_positive(h_path.dentry)
25632 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
25633 +               delegated = NULL;
25634 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
25635 +               if (unlikely(err == -EWOULDBLOCK)) {
25636 +                       pr_warn("cannot retry for NFSv4 delegation"
25637 +                               " for an internal unlink\n");
25638 +                       iput(delegated);
25639 +               }
25640 +               dput(h_path.dentry);
25641 +               h_path.dentry = NULL;
25642 +               if (!err)
25643 +                       goto again;
25644 +       }
25645 +       if (!err && d_is_negative(h_path.dentry)) {
25646 +               delegated = NULL;
25647 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
25648 +               if (unlikely(err == -EWOULDBLOCK)) {
25649 +                       pr_warn("cannot retry for NFSv4 delegation"
25650 +                               " for an internal link\n");
25651 +                       iput(delegated);
25652 +               }
25653 +       }
25654 +       dput(h_path.dentry);
25655 +
25656 +out:
25657 +       inode_unlock(h_dir);
25658 +       return err;
25659 +}
25660 +
25661 +struct do_whplink_args {
25662 +       int *errp;
25663 +       struct qstr *tgt;
25664 +       struct dentry *h_parent;
25665 +       struct dentry *h_dentry;
25666 +       struct au_branch *br;
25667 +};
25668 +
25669 +static void call_do_whplink(void *args)
25670 +{
25671 +       struct do_whplink_args *a = args;
25672 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
25673 +}
25674 +
25675 +static int whplink(struct dentry *h_dentry, struct inode *inode,
25676 +                  aufs_bindex_t bindex, struct au_branch *br)
25677 +{
25678 +       int err, wkq_err;
25679 +       struct au_wbr *wbr;
25680 +       struct dentry *h_parent;
25681 +       char a[PLINK_NAME_LEN];
25682 +       struct qstr tgtname = QSTR_INIT(a, 0);
25683 +
25684 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
25685 +       h_parent = wbr->wbr_plink;
25686 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25687 +
25688 +       /* always superio. */
25689 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
25690 +               struct do_whplink_args args = {
25691 +                       .errp           = &err,
25692 +                       .tgt            = &tgtname,
25693 +                       .h_parent       = h_parent,
25694 +                       .h_dentry       = h_dentry,
25695 +                       .br             = br
25696 +               };
25697 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
25698 +               if (unlikely(wkq_err))
25699 +                       err = wkq_err;
25700 +       } else
25701 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
25702 +
25703 +       return err;
25704 +}
25705 +
25706 +/*
25707 + * create a new pseudo-link for @h_dentry on @bindex.
25708 + * the linked inode is held in aufs @inode.
25709 + */
25710 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25711 +                    struct dentry *h_dentry)
25712 +{
25713 +       struct super_block *sb;
25714 +       struct au_sbinfo *sbinfo;
25715 +       struct hlist_head *plink_hlist;
25716 +       struct au_icntnr *icntnr;
25717 +       struct au_sphlhead *sphl;
25718 +       int found, err, cnt, i;
25719 +
25720 +       sb = inode->i_sb;
25721 +       sbinfo = au_sbi(sb);
25722 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25723 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25724 +
25725 +       found = au_plink_test(inode);
25726 +       if (found)
25727 +               return;
25728 +
25729 +       i = au_plink_hash(inode->i_ino);
25730 +       sphl = sbinfo->si_plink + i;
25731 +       plink_hlist = &sphl->head;
25732 +       au_igrab(inode);
25733 +
25734 +       spin_lock(&sphl->spin);
25735 +       hlist_for_each_entry(icntnr, plink_hlist, plink) {
25736 +               if (&icntnr->vfs_inode == inode) {
25737 +                       found = 1;
25738 +                       break;
25739 +               }
25740 +       }
25741 +       if (!found) {
25742 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
25743 +               hlist_add_head_rcu(&icntnr->plink, plink_hlist);
25744 +       }
25745 +       spin_unlock(&sphl->spin);
25746 +       if (!found) {
25747 +               cnt = au_sphl_count(sphl);
25748 +#define msg "unexpectedly unblanced or too many pseudo-links"
25749 +               if (cnt > AUFS_PLINK_WARN)
25750 +                       AuWarn1(msg ", %d\n", cnt);
25751 +#undef msg
25752 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
25753 +               if (unlikely(err)) {
25754 +                       pr_warn("err %d, damaged pseudo link.\n", err);
25755 +                       au_sphl_del_rcu(&icntnr->plink, sphl);
25756 +                       iput(&icntnr->vfs_inode);
25757 +               }
25758 +       } else
25759 +               iput(&icntnr->vfs_inode);
25760 +}
25761 +
25762 +/* free all plinks */
25763 +void au_plink_put(struct super_block *sb, int verbose)
25764 +{
25765 +       int i, warned;
25766 +       struct au_sbinfo *sbinfo;
25767 +       struct hlist_head *plink_hlist;
25768 +       struct hlist_node *tmp;
25769 +       struct au_icntnr *icntnr;
25770 +
25771 +       SiMustWriteLock(sb);
25772 +
25773 +       sbinfo = au_sbi(sb);
25774 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25775 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25776 +
25777 +       /* no spin_lock since sbinfo is write-locked */
25778 +       warned = 0;
25779 +       for (i = 0; i < AuPlink_NHASH; i++) {
25780 +               plink_hlist = &sbinfo->si_plink[i].head;
25781 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
25782 +                       pr_warn("pseudo-link is not flushed");
25783 +                       warned = 1;
25784 +               }
25785 +               hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink)
25786 +                       iput(&icntnr->vfs_inode);
25787 +               INIT_HLIST_HEAD(plink_hlist);
25788 +       }
25789 +}
25790 +
25791 +void au_plink_clean(struct super_block *sb, int verbose)
25792 +{
25793 +       struct dentry *root;
25794 +
25795 +       root = sb->s_root;
25796 +       aufs_write_lock(root);
25797 +       if (au_opt_test(au_mntflags(sb), PLINK))
25798 +               au_plink_put(sb, verbose);
25799 +       aufs_write_unlock(root);
25800 +}
25801 +
25802 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25803 +{
25804 +       int do_put;
25805 +       aufs_bindex_t btop, bbot, bindex;
25806 +
25807 +       do_put = 0;
25808 +       btop = au_ibtop(inode);
25809 +       bbot = au_ibbot(inode);
25810 +       if (btop >= 0) {
25811 +               for (bindex = btop; bindex <= bbot; bindex++) {
25812 +                       if (!au_h_iptr(inode, bindex)
25813 +                           || au_ii_br_id(inode, bindex) != br_id)
25814 +                               continue;
25815 +                       au_set_h_iptr(inode, bindex, NULL, 0);
25816 +                       do_put = 1;
25817 +                       break;
25818 +               }
25819 +               if (do_put)
25820 +                       for (bindex = btop; bindex <= bbot; bindex++)
25821 +                               if (au_h_iptr(inode, bindex)) {
25822 +                                       do_put = 0;
25823 +                                       break;
25824 +                               }
25825 +       } else
25826 +               do_put = 1;
25827 +
25828 +       return do_put;
25829 +}
25830 +
25831 +/* free the plinks on a branch specified by @br_id */
25832 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25833 +{
25834 +       struct au_sbinfo *sbinfo;
25835 +       struct hlist_head *plink_hlist;
25836 +       struct hlist_node *tmp;
25837 +       struct au_icntnr *icntnr;
25838 +       struct inode *inode;
25839 +       int i, do_put;
25840 +
25841 +       SiMustWriteLock(sb);
25842 +
25843 +       sbinfo = au_sbi(sb);
25844 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25845 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25846 +
25847 +       /* no spin_lock since sbinfo is write-locked */
25848 +       for (i = 0; i < AuPlink_NHASH; i++) {
25849 +               plink_hlist = &sbinfo->si_plink[i].head;
25850 +               hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink) {
25851 +                       inode = au_igrab(&icntnr->vfs_inode);
25852 +                       ii_write_lock_child(inode);
25853 +                       do_put = au_plink_do_half_refresh(inode, br_id);
25854 +                       if (do_put) {
25855 +                               hlist_del(&icntnr->plink);
25856 +                               iput(inode);
25857 +                       }
25858 +                       ii_write_unlock(inode);
25859 +                       iput(inode);
25860 +               }
25861 +       }
25862 +}
25863 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25864 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
25865 +++ linux/fs/aufs/poll.c        2017-03-02 14:24:13.866922487 +0100
25866 @@ -0,0 +1,52 @@
25867 +/*
25868 + * Copyright (C) 2005-2017 Junjiro R. Okajima
25869 + *
25870 + * This program, aufs is free software; you can redistribute it and/or modify
25871 + * it under the terms of the GNU General Public License as published by
25872 + * the Free Software Foundation; either version 2 of the License, or
25873 + * (at your option) any later version.
25874 + *
25875 + * This program is distributed in the hope that it will be useful,
25876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25878 + * GNU General Public License for more details.
25879 + *
25880 + * You should have received a copy of the GNU General Public License
25881 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25882 + */
25883 +
25884 +/*
25885 + * poll operation
25886 + * There is only one filesystem which implements ->poll operation, currently.
25887 + */
25888 +
25889 +#include "aufs.h"
25890 +
25891 +unsigned int aufs_poll(struct file *file, poll_table *wait)
25892 +{
25893 +       unsigned int mask;
25894 +       int err;
25895 +       struct file *h_file;
25896 +       struct super_block *sb;
25897 +
25898 +       /* We should pretend an error happened. */
25899 +       mask = POLLERR /* | POLLIN | POLLOUT */;
25900 +       sb = file->f_path.dentry->d_sb;
25901 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
25902 +
25903 +       h_file = au_read_pre(file, /*keep_fi*/0);
25904 +       err = PTR_ERR(h_file);
25905 +       if (IS_ERR(h_file))
25906 +               goto out;
25907 +
25908 +       /* it is not an error if h_file has no operation */
25909 +       mask = DEFAULT_POLLMASK;
25910 +       if (h_file->f_op->poll)
25911 +               mask = h_file->f_op->poll(h_file, wait);
25912 +       fput(h_file); /* instead of au_read_post() */
25913 +
25914 +out:
25915 +       si_read_unlock(sb);
25916 +       AuTraceErr((int)mask);
25917 +       return mask;
25918 +}
25919 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25920 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
25921 +++ linux/fs/aufs/posix_acl.c   2017-03-02 14:24:13.866922487 +0100
25922 @@ -0,0 +1,102 @@
25923 +/*
25924 + * Copyright (C) 2014-2017 Junjiro R. Okajima
25925 + *
25926 + * This program, aufs is free software; you can redistribute it and/or modify
25927 + * it under the terms of the GNU General Public License as published by
25928 + * the Free Software Foundation; either version 2 of the License, or
25929 + * (at your option) any later version.
25930 + *
25931 + * This program is distributed in the hope that it will be useful,
25932 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25933 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25934 + * GNU General Public License for more details.
25935 + *
25936 + * You should have received a copy of the GNU General Public License
25937 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25938 + */
25939 +
25940 +/*
25941 + * posix acl operations
25942 + */
25943 +
25944 +#include <linux/fs.h>
25945 +#include "aufs.h"
25946 +
25947 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25948 +{
25949 +       struct posix_acl *acl;
25950 +       int err;
25951 +       aufs_bindex_t bindex;
25952 +       struct inode *h_inode;
25953 +       struct super_block *sb;
25954 +
25955 +       acl = NULL;
25956 +       sb = inode->i_sb;
25957 +       si_read_lock(sb, AuLock_FLUSH);
25958 +       ii_read_lock_child(inode);
25959 +       if (!(sb->s_flags & MS_POSIXACL))
25960 +               goto out;
25961 +
25962 +       bindex = au_ibtop(inode);
25963 +       h_inode = au_h_iptr(inode, bindex);
25964 +       if (unlikely(!h_inode
25965 +                    || ((h_inode->i_mode & S_IFMT)
25966 +                        != (inode->i_mode & S_IFMT)))) {
25967 +               err = au_busy_or_stale();
25968 +               acl = ERR_PTR(err);
25969 +               goto out;
25970 +       }
25971 +
25972 +       /* always topmost only */
25973 +       acl = get_acl(h_inode, type);
25974 +       if (!IS_ERR_OR_NULL(acl))
25975 +               set_cached_acl(inode, type, acl);
25976 +
25977 +out:
25978 +       ii_read_unlock(inode);
25979 +       si_read_unlock(sb);
25980 +
25981 +       AuTraceErrPtr(acl);
25982 +       return acl;
25983 +}
25984 +
25985 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25986 +{
25987 +       int err;
25988 +       ssize_t ssz;
25989 +       struct dentry *dentry;
25990 +       struct au_sxattr arg = {
25991 +               .type = AU_ACL_SET,
25992 +               .u.acl_set = {
25993 +                       .acl    = acl,
25994 +                       .type   = type
25995 +               },
25996 +       };
25997 +
25998 +       IMustLock(inode);
25999 +
26000 +       if (inode->i_ino == AUFS_ROOT_INO)
26001 +               dentry = dget(inode->i_sb->s_root);
26002 +       else {
26003 +               dentry = d_find_alias(inode);
26004 +               if (!dentry)
26005 +                       dentry = d_find_any_alias(inode);
26006 +               if (!dentry) {
26007 +                       pr_warn("cannot handle this inode, "
26008 +                               "please report to aufs-users ML\n");
26009 +                       err = -ENOENT;
26010 +                       goto out;
26011 +               }
26012 +       }
26013 +
26014 +       ssz = au_sxattr(dentry, inode, &arg);
26015 +       dput(dentry);
26016 +       err = ssz;
26017 +       if (ssz >= 0) {
26018 +               err = 0;
26019 +               set_cached_acl(inode, type, acl);
26020 +       }
26021 +
26022 +out:
26023 +       return err;
26024 +}
26025 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
26026 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
26027 +++ linux/fs/aufs/procfs.c      2017-03-02 14:24:13.866922487 +0100
26028 @@ -0,0 +1,169 @@
26029 +/*
26030 + * Copyright (C) 2010-2017 Junjiro R. Okajima
26031 + *
26032 + * This program, aufs is free software; you can redistribute it and/or modify
26033 + * it under the terms of the GNU General Public License as published by
26034 + * the Free Software Foundation; either version 2 of the License, or
26035 + * (at your option) any later version.
26036 + *
26037 + * This program is distributed in the hope that it will be useful,
26038 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26039 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26040 + * GNU General Public License for more details.
26041 + *
26042 + * You should have received a copy of the GNU General Public License
26043 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26044 + */
26045 +
26046 +/*
26047 + * procfs interfaces
26048 + */
26049 +
26050 +#include <linux/proc_fs.h>
26051 +#include "aufs.h"
26052 +
26053 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
26054 +{
26055 +       struct au_sbinfo *sbinfo;
26056 +
26057 +       sbinfo = file->private_data;
26058 +       if (sbinfo) {
26059 +               au_plink_maint_leave(sbinfo);
26060 +               kobject_put(&sbinfo->si_kobj);
26061 +       }
26062 +
26063 +       return 0;
26064 +}
26065 +
26066 +static void au_procfs_plm_write_clean(struct file *file)
26067 +{
26068 +       struct au_sbinfo *sbinfo;
26069 +
26070 +       sbinfo = file->private_data;
26071 +       if (sbinfo)
26072 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
26073 +}
26074 +
26075 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
26076 +{
26077 +       int err;
26078 +       struct super_block *sb;
26079 +       struct au_sbinfo *sbinfo;
26080 +
26081 +       err = -EBUSY;
26082 +       if (unlikely(file->private_data))
26083 +               goto out;
26084 +
26085 +       sb = NULL;
26086 +       /* don't use au_sbilist_lock() here */
26087 +       spin_lock(&au_sbilist.spin);
26088 +       hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
26089 +               if (id == sysaufs_si_id(sbinfo)) {
26090 +                       kobject_get(&sbinfo->si_kobj);
26091 +                       sb = sbinfo->si_sb;
26092 +                       break;
26093 +               }
26094 +       spin_unlock(&au_sbilist.spin);
26095 +
26096 +       err = -EINVAL;
26097 +       if (unlikely(!sb))
26098 +               goto out;
26099 +
26100 +       err = au_plink_maint_enter(sb);
26101 +       if (!err)
26102 +               /* keep kobject_get() */
26103 +               file->private_data = sbinfo;
26104 +       else
26105 +               kobject_put(&sbinfo->si_kobj);
26106 +out:
26107 +       return err;
26108 +}
26109 +
26110 +/*
26111 + * Accept a valid "si=xxxx" only.
26112 + * Once it is accepted successfully, accept "clean" too.
26113 + */
26114 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
26115 +                                  size_t count, loff_t *ppos)
26116 +{
26117 +       ssize_t err;
26118 +       unsigned long id;
26119 +       /* last newline is allowed */
26120 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
26121 +
26122 +       err = -EACCES;
26123 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26124 +               goto out;
26125 +
26126 +       err = -EINVAL;
26127 +       if (unlikely(count > sizeof(buf)))
26128 +               goto out;
26129 +
26130 +       err = copy_from_user(buf, ubuf, count);
26131 +       if (unlikely(err)) {
26132 +               err = -EFAULT;
26133 +               goto out;
26134 +       }
26135 +       buf[count] = 0;
26136 +
26137 +       err = -EINVAL;
26138 +       if (!strcmp("clean", buf)) {
26139 +               au_procfs_plm_write_clean(file);
26140 +               goto out_success;
26141 +       } else if (unlikely(strncmp("si=", buf, 3)))
26142 +               goto out;
26143 +
26144 +       err = kstrtoul(buf + 3, 16, &id);
26145 +       if (unlikely(err))
26146 +               goto out;
26147 +
26148 +       err = au_procfs_plm_write_si(file, id);
26149 +       if (unlikely(err))
26150 +               goto out;
26151 +
26152 +out_success:
26153 +       err = count; /* success */
26154 +out:
26155 +       return err;
26156 +}
26157 +
26158 +static const struct file_operations au_procfs_plm_fop = {
26159 +       .write          = au_procfs_plm_write,
26160 +       .release        = au_procfs_plm_release,
26161 +       .owner          = THIS_MODULE
26162 +};
26163 +
26164 +/* ---------------------------------------------------------------------- */
26165 +
26166 +static struct proc_dir_entry *au_procfs_dir;
26167 +
26168 +void au_procfs_fin(void)
26169 +{
26170 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
26171 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
26172 +}
26173 +
26174 +int __init au_procfs_init(void)
26175 +{
26176 +       int err;
26177 +       struct proc_dir_entry *entry;
26178 +
26179 +       err = -ENOMEM;
26180 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
26181 +       if (unlikely(!au_procfs_dir))
26182 +               goto out;
26183 +
26184 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
26185 +                           au_procfs_dir, &au_procfs_plm_fop);
26186 +       if (unlikely(!entry))
26187 +               goto out_dir;
26188 +
26189 +       err = 0;
26190 +       goto out; /* success */
26191 +
26192 +
26193 +out_dir:
26194 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
26195 +out:
26196 +       return err;
26197 +}
26198 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
26199 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
26200 +++ linux/fs/aufs/rdu.c 2017-03-02 14:24:13.866922487 +0100
26201 @@ -0,0 +1,381 @@
26202 +/*
26203 + * Copyright (C) 2005-2017 Junjiro R. Okajima
26204 + *
26205 + * This program, aufs is free software; you can redistribute it and/or modify
26206 + * it under the terms of the GNU General Public License as published by
26207 + * the Free Software Foundation; either version 2 of the License, or
26208 + * (at your option) any later version.
26209 + *
26210 + * This program is distributed in the hope that it will be useful,
26211 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26212 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26213 + * GNU General Public License for more details.
26214 + *
26215 + * You should have received a copy of the GNU General Public License
26216 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26217 + */
26218 +
26219 +/*
26220 + * readdir in userspace.
26221 + */
26222 +
26223 +#include <linux/compat.h>
26224 +#include <linux/fs_stack.h>
26225 +#include <linux/security.h>
26226 +#include "aufs.h"
26227 +
26228 +/* bits for struct aufs_rdu.flags */
26229 +#define        AuRdu_CALLED    1
26230 +#define        AuRdu_CONT      (1 << 1)
26231 +#define        AuRdu_FULL      (1 << 2)
26232 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
26233 +#define au_fset_rdu(flags, name) \
26234 +       do { (flags) |= AuRdu_##name; } while (0)
26235 +#define au_fclr_rdu(flags, name) \
26236 +       do { (flags) &= ~AuRdu_##name; } while (0)
26237 +
26238 +struct au_rdu_arg {
26239 +       struct dir_context              ctx;
26240 +       struct aufs_rdu                 *rdu;
26241 +       union au_rdu_ent_ul             ent;
26242 +       unsigned long                   end;
26243 +
26244 +       struct super_block              *sb;
26245 +       int                             err;
26246 +};
26247 +
26248 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
26249 +                      loff_t offset, u64 h_ino, unsigned int d_type)
26250 +{
26251 +       int err, len;
26252 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
26253 +       struct aufs_rdu *rdu = arg->rdu;
26254 +       struct au_rdu_ent ent;
26255 +
26256 +       err = 0;
26257 +       arg->err = 0;
26258 +       au_fset_rdu(rdu->cookie.flags, CALLED);
26259 +       len = au_rdu_len(nlen);
26260 +       if (arg->ent.ul + len  < arg->end) {
26261 +               ent.ino = h_ino;
26262 +               ent.bindex = rdu->cookie.bindex;
26263 +               ent.type = d_type;
26264 +               ent.nlen = nlen;
26265 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
26266 +                       ent.type = DT_UNKNOWN;
26267 +
26268 +               /* unnecessary to support mmap_sem since this is a dir */
26269 +               err = -EFAULT;
26270 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
26271 +                       goto out;
26272 +               if (copy_to_user(arg->ent.e->name, name, nlen))
26273 +                       goto out;
26274 +               /* the terminating NULL */
26275 +               if (__put_user(0, arg->ent.e->name + nlen))
26276 +                       goto out;
26277 +               err = 0;
26278 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
26279 +               arg->ent.ul += len;
26280 +               rdu->rent++;
26281 +       } else {
26282 +               err = -EFAULT;
26283 +               au_fset_rdu(rdu->cookie.flags, FULL);
26284 +               rdu->full = 1;
26285 +               rdu->tail = arg->ent;
26286 +       }
26287 +
26288 +out:
26289 +       /* AuTraceErr(err); */
26290 +       return err;
26291 +}
26292 +
26293 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
26294 +{
26295 +       int err;
26296 +       loff_t offset;
26297 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
26298 +
26299 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
26300 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
26301 +       err = offset;
26302 +       if (unlikely(offset != cookie->h_pos))
26303 +               goto out;
26304 +
26305 +       err = 0;
26306 +       do {
26307 +               arg->err = 0;
26308 +               au_fclr_rdu(cookie->flags, CALLED);
26309 +               /* smp_mb(); */
26310 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
26311 +               if (err >= 0)
26312 +                       err = arg->err;
26313 +       } while (!err
26314 +                && au_ftest_rdu(cookie->flags, CALLED)
26315 +                && !au_ftest_rdu(cookie->flags, FULL));
26316 +       cookie->h_pos = h_file->f_pos;
26317 +
26318 +out:
26319 +       AuTraceErr(err);
26320 +       return err;
26321 +}
26322 +
26323 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
26324 +{
26325 +       int err;
26326 +       aufs_bindex_t bbot;
26327 +       struct au_rdu_arg arg = {
26328 +               .ctx = {
26329 +                       .actor = au_rdu_fill
26330 +               }
26331 +       };
26332 +       struct dentry *dentry;
26333 +       struct inode *inode;
26334 +       struct file *h_file;
26335 +       struct au_rdu_cookie *cookie = &rdu->cookie;
26336 +
26337 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
26338 +       if (unlikely(err)) {
26339 +               err = -EFAULT;
26340 +               AuTraceErr(err);
26341 +               goto out;
26342 +       }
26343 +       rdu->rent = 0;
26344 +       rdu->tail = rdu->ent;
26345 +       rdu->full = 0;
26346 +       arg.rdu = rdu;
26347 +       arg.ent = rdu->ent;
26348 +       arg.end = arg.ent.ul;
26349 +       arg.end += rdu->sz;
26350 +
26351 +       err = -ENOTDIR;
26352 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
26353 +               goto out;
26354 +
26355 +       err = security_file_permission(file, MAY_READ);
26356 +       AuTraceErr(err);
26357 +       if (unlikely(err))
26358 +               goto out;
26359 +
26360 +       dentry = file->f_path.dentry;
26361 +       inode = d_inode(dentry);
26362 +       inode_lock_shared(inode);
26363 +
26364 +       arg.sb = inode->i_sb;
26365 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
26366 +       if (unlikely(err))
26367 +               goto out_mtx;
26368 +       err = au_alive_dir(dentry);
26369 +       if (unlikely(err))
26370 +               goto out_si;
26371 +       /* todo: reval? */
26372 +       fi_read_lock(file);
26373 +
26374 +       err = -EAGAIN;
26375 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
26376 +                    && cookie->generation != au_figen(file)))
26377 +               goto out_unlock;
26378 +
26379 +       err = 0;
26380 +       if (!rdu->blk) {
26381 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
26382 +               if (!rdu->blk)
26383 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
26384 +       }
26385 +       bbot = au_fbtop(file);
26386 +       if (cookie->bindex < bbot)
26387 +               cookie->bindex = bbot;
26388 +       bbot = au_fbbot_dir(file);
26389 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
26390 +       for (; !err && cookie->bindex <= bbot;
26391 +            cookie->bindex++, cookie->h_pos = 0) {
26392 +               h_file = au_hf_dir(file, cookie->bindex);
26393 +               if (!h_file)
26394 +                       continue;
26395 +
26396 +               au_fclr_rdu(cookie->flags, FULL);
26397 +               err = au_rdu_do(h_file, &arg);
26398 +               AuTraceErr(err);
26399 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
26400 +                       break;
26401 +       }
26402 +       AuDbg("rent %llu\n", rdu->rent);
26403 +
26404 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
26405 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
26406 +               au_fset_rdu(cookie->flags, CONT);
26407 +               cookie->generation = au_figen(file);
26408 +       }
26409 +
26410 +       ii_read_lock_child(inode);
26411 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
26412 +       ii_read_unlock(inode);
26413 +
26414 +out_unlock:
26415 +       fi_read_unlock(file);
26416 +out_si:
26417 +       si_read_unlock(arg.sb);
26418 +out_mtx:
26419 +       inode_unlock_shared(inode);
26420 +out:
26421 +       AuTraceErr(err);
26422 +       return err;
26423 +}
26424 +
26425 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
26426 +{
26427 +       int err;
26428 +       ino_t ino;
26429 +       unsigned long long nent;
26430 +       union au_rdu_ent_ul *u;
26431 +       struct au_rdu_ent ent;
26432 +       struct super_block *sb;
26433 +
26434 +       err = 0;
26435 +       nent = rdu->nent;
26436 +       u = &rdu->ent;
26437 +       sb = file->f_path.dentry->d_sb;
26438 +       si_read_lock(sb, AuLock_FLUSH);
26439 +       while (nent-- > 0) {
26440 +               /* unnecessary to support mmap_sem since this is a dir */
26441 +               err = copy_from_user(&ent, u->e, sizeof(ent));
26442 +               if (!err)
26443 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
26444 +               if (unlikely(err)) {
26445 +                       err = -EFAULT;
26446 +                       AuTraceErr(err);
26447 +                       break;
26448 +               }
26449 +
26450 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
26451 +               if (!ent.wh)
26452 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
26453 +               else
26454 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
26455 +                                       &ino);
26456 +               if (unlikely(err)) {
26457 +                       AuTraceErr(err);
26458 +                       break;
26459 +               }
26460 +
26461 +               err = __put_user(ino, &u->e->ino);
26462 +               if (unlikely(err)) {
26463 +                       err = -EFAULT;
26464 +                       AuTraceErr(err);
26465 +                       break;
26466 +               }
26467 +               u->ul += au_rdu_len(ent.nlen);
26468 +       }
26469 +       si_read_unlock(sb);
26470 +
26471 +       return err;
26472 +}
26473 +
26474 +/* ---------------------------------------------------------------------- */
26475 +
26476 +static int au_rdu_verify(struct aufs_rdu *rdu)
26477 +{
26478 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
26479 +             "%llu, b%d, 0x%x, g%u}\n",
26480 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
26481 +             rdu->blk,
26482 +             rdu->rent, rdu->shwh, rdu->full,
26483 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
26484 +             rdu->cookie.generation);
26485 +
26486 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
26487 +               return 0;
26488 +
26489 +       AuDbg("%u:%u\n",
26490 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
26491 +       return -EINVAL;
26492 +}
26493 +
26494 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26495 +{
26496 +       long err, e;
26497 +       struct aufs_rdu rdu;
26498 +       void __user *p = (void __user *)arg;
26499 +
26500 +       err = copy_from_user(&rdu, p, sizeof(rdu));
26501 +       if (unlikely(err)) {
26502 +               err = -EFAULT;
26503 +               AuTraceErr(err);
26504 +               goto out;
26505 +       }
26506 +       err = au_rdu_verify(&rdu);
26507 +       if (unlikely(err))
26508 +               goto out;
26509 +
26510 +       switch (cmd) {
26511 +       case AUFS_CTL_RDU:
26512 +               err = au_rdu(file, &rdu);
26513 +               if (unlikely(err))
26514 +                       break;
26515 +
26516 +               e = copy_to_user(p, &rdu, sizeof(rdu));
26517 +               if (unlikely(e)) {
26518 +                       err = -EFAULT;
26519 +                       AuTraceErr(err);
26520 +               }
26521 +               break;
26522 +       case AUFS_CTL_RDU_INO:
26523 +               err = au_rdu_ino(file, &rdu);
26524 +               break;
26525 +
26526 +       default:
26527 +               /* err = -ENOTTY; */
26528 +               err = -EINVAL;
26529 +       }
26530 +
26531 +out:
26532 +       AuTraceErr(err);
26533 +       return err;
26534 +}
26535 +
26536 +#ifdef CONFIG_COMPAT
26537 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26538 +{
26539 +       long err, e;
26540 +       struct aufs_rdu rdu;
26541 +       void __user *p = compat_ptr(arg);
26542 +
26543 +       /* todo: get_user()? */
26544 +       err = copy_from_user(&rdu, p, sizeof(rdu));
26545 +       if (unlikely(err)) {
26546 +               err = -EFAULT;
26547 +               AuTraceErr(err);
26548 +               goto out;
26549 +       }
26550 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
26551 +       err = au_rdu_verify(&rdu);
26552 +       if (unlikely(err))
26553 +               goto out;
26554 +
26555 +       switch (cmd) {
26556 +       case AUFS_CTL_RDU:
26557 +               err = au_rdu(file, &rdu);
26558 +               if (unlikely(err))
26559 +                       break;
26560 +
26561 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
26562 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
26563 +               e = copy_to_user(p, &rdu, sizeof(rdu));
26564 +               if (unlikely(e)) {
26565 +                       err = -EFAULT;
26566 +                       AuTraceErr(err);
26567 +               }
26568 +               break;
26569 +       case AUFS_CTL_RDU_INO:
26570 +               err = au_rdu_ino(file, &rdu);
26571 +               break;
26572 +
26573 +       default:
26574 +               /* err = -ENOTTY; */
26575 +               err = -EINVAL;
26576 +       }
26577 +
26578 +out:
26579 +       AuTraceErr(err);
26580 +       return err;
26581 +}
26582 +#endif
26583 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
26584 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
26585 +++ linux/fs/aufs/rwsem.h       2017-03-02 14:24:13.866922487 +0100
26586 @@ -0,0 +1,198 @@
26587 +/*
26588 + * Copyright (C) 2005-2017 Junjiro R. Okajima
26589 + *
26590 + * This program, aufs is free software; you can redistribute it and/or modify
26591 + * it under the terms of the GNU General Public License as published by
26592 + * the Free Software Foundation; either version 2 of the License, or
26593 + * (at your option) any later version.
26594 + *
26595 + * This program is distributed in the hope that it will be useful,
26596 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26597 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26598 + * GNU General Public License for more details.
26599 + *
26600 + * You should have received a copy of the GNU General Public License
26601 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26602 + */
26603 +
26604 +/*
26605 + * simple read-write semaphore wrappers
26606 + */
26607 +
26608 +#ifndef __AUFS_RWSEM_H__
26609 +#define __AUFS_RWSEM_H__
26610 +
26611 +#ifdef __KERNEL__
26612 +
26613 +#include "debug.h"
26614 +
26615 +struct au_rwsem {
26616 +       struct rw_semaphore     rwsem;
26617 +#ifdef CONFIG_AUFS_DEBUG
26618 +       /* just for debugging, not almighty counter */
26619 +       atomic_t                rcnt, wcnt;
26620 +#endif
26621 +};
26622 +
26623 +#ifdef CONFIG_LOCKDEP
26624 +#define au_lockdep_set_name(rw)                                                \
26625 +       lockdep_set_class_and_name(&(rw)->rwsem,                        \
26626 +                                  /*original key*/(rw)->rwsem.dep_map.key, \
26627 +                                  /*name*/#rw)
26628 +#else
26629 +#define au_lockdep_set_name(rw) do {} while (0)
26630 +#endif
26631 +
26632 +#ifdef CONFIG_AUFS_DEBUG
26633 +#define AuDbgCntInit(rw) do { \
26634 +       atomic_set(&(rw)->rcnt, 0); \
26635 +       atomic_set(&(rw)->wcnt, 0); \
26636 +       smp_mb(); /* atomic set */ \
26637 +} while (0)
26638 +
26639 +#define AuDbgCnt(rw, cnt)      atomic_read(&(rw)->cnt)
26640 +#define AuDbgCntInc(rw, cnt)   atomic_inc(&(rw)->cnt)
26641 +#define AuDbgCntDec(rw, cnt)   WARN_ON(atomic_dec_return(&(rw)->cnt) < 0)
26642 +#define AuDbgRcntInc(rw)       AuDbgCntInc(rw, rcnt)
26643 +#define AuDbgRcntDec(rw)       AuDbgCntDec(rw, rcnt)
26644 +#define AuDbgWcntInc(rw)       AuDbgCntInc(rw, wcnt)
26645 +#define AuDbgWcntDec(rw)       AuDbgCntDec(rw, wcnt)
26646 +#else
26647 +#define AuDbgCnt(rw, cnt)      0
26648 +#define AuDbgCntInit(rw)       do {} while (0)
26649 +#define AuDbgRcntInc(rw)       do {} while (0)
26650 +#define AuDbgRcntDec(rw)       do {} while (0)
26651 +#define AuDbgWcntInc(rw)       do {} while (0)
26652 +#define AuDbgWcntDec(rw)       do {} while (0)
26653 +#endif /* CONFIG_AUFS_DEBUG */
26654 +
26655 +/* to debug easier, do not make them inlined functions */
26656 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(&(rw)->rwsem))
26657 +/* rwsem_is_locked() is unusable */
26658 +#define AuRwMustReadLock(rw)   AuDebugOn(AuDbgCnt(rw, rcnt) <= 0)
26659 +#define AuRwMustWriteLock(rw)  AuDebugOn(AuDbgCnt(rw, wcnt) <= 0)
26660 +#define AuRwMustAnyLock(rw)    AuDebugOn(AuDbgCnt(rw, rcnt) <= 0       \
26661 +                                         && AuDbgCnt(rw, wcnt) <= 0)
26662 +#define AuRwDestroy(rw)                AuDebugOn(AuDbgCnt(rw, rcnt)            \
26663 +                                         || AuDbgCnt(rw, wcnt))
26664 +
26665 +#define au_rw_init(rw) do {                    \
26666 +               AuDbgCntInit(rw);               \
26667 +               init_rwsem(&(rw)->rwsem);       \
26668 +               au_lockdep_set_name(rw);        \
26669 +       } while (0)
26670 +
26671 +#define au_rw_init_wlock(rw) do {              \
26672 +               au_rw_init(rw);                 \
26673 +               down_write(&(rw)->rwsem);       \
26674 +               AuDbgWcntInc(rw);               \
26675 +       } while (0)
26676 +
26677 +#define au_rw_init_wlock_nested(rw, lsc) do { \
26678 +               au_rw_init(rw);                         \
26679 +               down_write_nested(&(rw)->rwsem, lsc);   \
26680 +               AuDbgWcntInc(rw);                       \
26681 +       } while (0)
26682 +
26683 +static inline void au_rw_read_lock(struct au_rwsem *rw)
26684 +{
26685 +       down_read(&rw->rwsem);
26686 +       AuDbgRcntInc(rw);
26687 +}
26688 +
26689 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26690 +{
26691 +       down_read_nested(&rw->rwsem, lsc);
26692 +       AuDbgRcntInc(rw);
26693 +}
26694 +
26695 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
26696 +{
26697 +       AuRwMustReadLock(rw);
26698 +       AuDbgRcntDec(rw);
26699 +       up_read(&rw->rwsem);
26700 +}
26701 +
26702 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26703 +{
26704 +       AuRwMustWriteLock(rw);
26705 +       AuDbgRcntInc(rw);
26706 +       AuDbgWcntDec(rw);
26707 +       downgrade_write(&rw->rwsem);
26708 +}
26709 +
26710 +static inline void au_rw_write_lock(struct au_rwsem *rw)
26711 +{
26712 +       down_write(&rw->rwsem);
26713 +       AuDbgWcntInc(rw);
26714 +}
26715 +
26716 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26717 +                                          unsigned int lsc)
26718 +{
26719 +       down_write_nested(&rw->rwsem, lsc);
26720 +       AuDbgWcntInc(rw);
26721 +}
26722 +
26723 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
26724 +{
26725 +       AuRwMustWriteLock(rw);
26726 +       AuDbgWcntDec(rw);
26727 +       up_write(&rw->rwsem);
26728 +}
26729 +
26730 +/* why is not _nested version defined */
26731 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
26732 +{
26733 +       int ret;
26734 +
26735 +       ret = down_read_trylock(&rw->rwsem);
26736 +       if (ret)
26737 +               AuDbgRcntInc(rw);
26738 +       return ret;
26739 +}
26740 +
26741 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
26742 +{
26743 +       int ret;
26744 +
26745 +       ret = down_write_trylock(&rw->rwsem);
26746 +       if (ret)
26747 +               AuDbgWcntInc(rw);
26748 +       return ret;
26749 +}
26750 +
26751 +#undef AuDbgCntDec
26752 +#undef AuDbgRcntInc
26753 +#undef AuDbgRcntDec
26754 +#undef AuDbgWcntDec
26755 +
26756 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26757 +static inline void prefix##_read_lock(param) \
26758 +{ au_rw_read_lock(rwsem); } \
26759 +static inline void prefix##_write_lock(param) \
26760 +{ au_rw_write_lock(rwsem); } \
26761 +static inline int prefix##_read_trylock(param) \
26762 +{ return au_rw_read_trylock(rwsem); } \
26763 +static inline int prefix##_write_trylock(param) \
26764 +{ return au_rw_write_trylock(rwsem); }
26765 +/* why is not _nested version defined */
26766 +/* static inline void prefix##_read_trylock_nested(param, lsc)
26767 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
26768 +static inline void prefix##_write_trylock_nestd(param, lsc)
26769 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
26770 +
26771 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26772 +static inline void prefix##_read_unlock(param) \
26773 +{ au_rw_read_unlock(rwsem); } \
26774 +static inline void prefix##_write_unlock(param) \
26775 +{ au_rw_write_unlock(rwsem); } \
26776 +static inline void prefix##_downgrade_lock(param) \
26777 +{ au_rw_dgrade_lock(rwsem); }
26778 +
26779 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26780 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26781 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26782 +
26783 +#endif /* __KERNEL__ */
26784 +#endif /* __AUFS_RWSEM_H__ */
26785 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26786 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
26787 +++ linux/fs/aufs/sbinfo.c      2017-03-02 14:24:13.866922487 +0100
26788 @@ -0,0 +1,304 @@
26789 +/*
26790 + * Copyright (C) 2005-2017 Junjiro R. Okajima
26791 + *
26792 + * This program, aufs is free software; you can redistribute it and/or modify
26793 + * it under the terms of the GNU General Public License as published by
26794 + * the Free Software Foundation; either version 2 of the License, or
26795 + * (at your option) any later version.
26796 + *
26797 + * This program is distributed in the hope that it will be useful,
26798 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26799 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26800 + * GNU General Public License for more details.
26801 + *
26802 + * You should have received a copy of the GNU General Public License
26803 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26804 + */
26805 +
26806 +/*
26807 + * superblock private data
26808 + */
26809 +
26810 +#include "aufs.h"
26811 +
26812 +/*
26813 + * they are necessary regardless sysfs is disabled.
26814 + */
26815 +void au_si_free(struct kobject *kobj)
26816 +{
26817 +       int i;
26818 +       struct au_sbinfo *sbinfo;
26819 +       char *locked __maybe_unused; /* debug only */
26820 +
26821 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26822 +       for (i = 0; i < AuPlink_NHASH; i++)
26823 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
26824 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
26825 +
26826 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
26827 +       percpu_counter_destroy(&sbinfo->si_ninodes);
26828 +       AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
26829 +       percpu_counter_destroy(&sbinfo->si_nfiles);
26830 +
26831 +       au_rw_write_lock(&sbinfo->si_rwsem);
26832 +       au_br_free(sbinfo);
26833 +       au_rw_write_unlock(&sbinfo->si_rwsem);
26834 +
26835 +       au_delayed_kfree(sbinfo->si_branch);
26836 +       mutex_destroy(&sbinfo->si_xib_mtx);
26837 +       AuRwDestroy(&sbinfo->si_rwsem);
26838 +
26839 +       au_delayed_kfree(sbinfo);
26840 +}
26841 +
26842 +int au_si_alloc(struct super_block *sb)
26843 +{
26844 +       int err, i;
26845 +       struct au_sbinfo *sbinfo;
26846 +
26847 +       err = -ENOMEM;
26848 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
26849 +       if (unlikely(!sbinfo))
26850 +               goto out;
26851 +
26852 +       /* will be reallocated separately */
26853 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26854 +       if (unlikely(!sbinfo->si_branch))
26855 +               goto out_sbinfo;
26856 +
26857 +       err = sysaufs_si_init(sbinfo);
26858 +       if (unlikely(err))
26859 +               goto out_br;
26860 +
26861 +       au_nwt_init(&sbinfo->si_nowait);
26862 +       au_rw_init_wlock(&sbinfo->si_rwsem);
26863 +
26864 +       percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
26865 +       percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
26866 +
26867 +       sbinfo->si_bbot = -1;
26868 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
26869 +
26870 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26871 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
26872 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26873 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
26874 +
26875 +       au_fhsm_init(sbinfo);
26876 +
26877 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
26878 +
26879 +       sbinfo->si_xino_jiffy = jiffies;
26880 +       sbinfo->si_xino_expire
26881 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
26882 +       mutex_init(&sbinfo->si_xib_mtx);
26883 +       sbinfo->si_xino_brid = -1;
26884 +       /* leave si_xib_last_pindex and si_xib_next_bit */
26885 +
26886 +       au_sphl_init(&sbinfo->si_aopen);
26887 +
26888 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
26889 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26890 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26891 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26892 +
26893 +       for (i = 0; i < AuPlink_NHASH; i++)
26894 +               au_sphl_init(sbinfo->si_plink + i);
26895 +       init_waitqueue_head(&sbinfo->si_plink_wq);
26896 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
26897 +
26898 +       au_sphl_init(&sbinfo->si_files);
26899 +
26900 +       /* with getattr by default */
26901 +       sbinfo->si_iop_array = aufs_iop;
26902 +
26903 +       /* leave other members for sysaufs and si_mnt. */
26904 +       sbinfo->si_sb = sb;
26905 +       sb->s_fs_info = sbinfo;
26906 +       si_pid_set(sb);
26907 +       return 0; /* success */
26908 +
26909 +out_br:
26910 +       au_delayed_kfree(sbinfo->si_branch);
26911 +out_sbinfo:
26912 +       au_delayed_kfree(sbinfo);
26913 +out:
26914 +       return err;
26915 +}
26916 +
26917 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
26918 +{
26919 +       int err, sz;
26920 +       struct au_branch **brp;
26921 +
26922 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
26923 +
26924 +       err = -ENOMEM;
26925 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
26926 +       if (unlikely(!sz))
26927 +               sz = sizeof(*brp);
26928 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
26929 +                          may_shrink);
26930 +       if (brp) {
26931 +               sbinfo->si_branch = brp;
26932 +               err = 0;
26933 +       }
26934 +
26935 +       return err;
26936 +}
26937 +
26938 +/* ---------------------------------------------------------------------- */
26939 +
26940 +unsigned int au_sigen_inc(struct super_block *sb)
26941 +{
26942 +       unsigned int gen;
26943 +       struct inode *inode;
26944 +
26945 +       SiMustWriteLock(sb);
26946 +
26947 +       gen = ++au_sbi(sb)->si_generation;
26948 +       au_update_digen(sb->s_root);
26949 +       inode = d_inode(sb->s_root);
26950 +       au_update_iigen(inode, /*half*/0);
26951 +       inode->i_version++;
26952 +       return gen;
26953 +}
26954 +
26955 +aufs_bindex_t au_new_br_id(struct super_block *sb)
26956 +{
26957 +       aufs_bindex_t br_id;
26958 +       int i;
26959 +       struct au_sbinfo *sbinfo;
26960 +
26961 +       SiMustWriteLock(sb);
26962 +
26963 +       sbinfo = au_sbi(sb);
26964 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26965 +               br_id = ++sbinfo->si_last_br_id;
26966 +               AuDebugOn(br_id < 0);
26967 +               if (br_id && au_br_index(sb, br_id) < 0)
26968 +                       return br_id;
26969 +       }
26970 +
26971 +       return -1;
26972 +}
26973 +
26974 +/* ---------------------------------------------------------------------- */
26975 +
26976 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26977 +int si_read_lock(struct super_block *sb, int flags)
26978 +{
26979 +       int err;
26980 +
26981 +       err = 0;
26982 +       if (au_ftest_lock(flags, FLUSH))
26983 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26984 +
26985 +       si_noflush_read_lock(sb);
26986 +       err = au_plink_maint(sb, flags);
26987 +       if (unlikely(err))
26988 +               si_read_unlock(sb);
26989 +
26990 +       return err;
26991 +}
26992 +
26993 +int si_write_lock(struct super_block *sb, int flags)
26994 +{
26995 +       int err;
26996 +
26997 +       if (au_ftest_lock(flags, FLUSH))
26998 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26999 +
27000 +       si_noflush_write_lock(sb);
27001 +       err = au_plink_maint(sb, flags);
27002 +       if (unlikely(err))
27003 +               si_write_unlock(sb);
27004 +
27005 +       return err;
27006 +}
27007 +
27008 +/* dentry and super_block lock. call at entry point */
27009 +int aufs_read_lock(struct dentry *dentry, int flags)
27010 +{
27011 +       int err;
27012 +       struct super_block *sb;
27013 +
27014 +       sb = dentry->d_sb;
27015 +       err = si_read_lock(sb, flags);
27016 +       if (unlikely(err))
27017 +               goto out;
27018 +
27019 +       if (au_ftest_lock(flags, DW))
27020 +               di_write_lock_child(dentry);
27021 +       else
27022 +               di_read_lock_child(dentry, flags);
27023 +
27024 +       if (au_ftest_lock(flags, GEN)) {
27025 +               err = au_digen_test(dentry, au_sigen(sb));
27026 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
27027 +                       AuDebugOn(!err && au_dbrange_test(dentry));
27028 +               else if (!err)
27029 +                       err = au_dbrange_test(dentry);
27030 +               if (unlikely(err))
27031 +                       aufs_read_unlock(dentry, flags);
27032 +       }
27033 +
27034 +out:
27035 +       return err;
27036 +}
27037 +
27038 +void aufs_read_unlock(struct dentry *dentry, int flags)
27039 +{
27040 +       if (au_ftest_lock(flags, DW))
27041 +               di_write_unlock(dentry);
27042 +       else
27043 +               di_read_unlock(dentry, flags);
27044 +       si_read_unlock(dentry->d_sb);
27045 +}
27046 +
27047 +void aufs_write_lock(struct dentry *dentry)
27048 +{
27049 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
27050 +       di_write_lock_child(dentry);
27051 +}
27052 +
27053 +void aufs_write_unlock(struct dentry *dentry)
27054 +{
27055 +       di_write_unlock(dentry);
27056 +       si_write_unlock(dentry->d_sb);
27057 +}
27058 +
27059 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
27060 +{
27061 +       int err;
27062 +       unsigned int sigen;
27063 +       struct super_block *sb;
27064 +
27065 +       sb = d1->d_sb;
27066 +       err = si_read_lock(sb, flags);
27067 +       if (unlikely(err))
27068 +               goto out;
27069 +
27070 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
27071 +
27072 +       if (au_ftest_lock(flags, GEN)) {
27073 +               sigen = au_sigen(sb);
27074 +               err = au_digen_test(d1, sigen);
27075 +               AuDebugOn(!err && au_dbrange_test(d1));
27076 +               if (!err) {
27077 +                       err = au_digen_test(d2, sigen);
27078 +                       AuDebugOn(!err && au_dbrange_test(d2));
27079 +               }
27080 +               if (unlikely(err))
27081 +                       aufs_read_and_write_unlock2(d1, d2);
27082 +       }
27083 +
27084 +out:
27085 +       return err;
27086 +}
27087 +
27088 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
27089 +{
27090 +       di_write_unlock2(d1, d2);
27091 +       si_read_unlock(d1->d_sb);
27092 +}
27093 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
27094 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
27095 +++ linux/fs/aufs/spl.h 2017-03-02 14:24:13.866922487 +0100
27096 @@ -0,0 +1,113 @@
27097 +/*
27098 + * Copyright (C) 2005-2017 Junjiro R. Okajima
27099 + *
27100 + * This program, aufs is free software; you can redistribute it and/or modify
27101 + * it under the terms of the GNU General Public License as published by
27102 + * the Free Software Foundation; either version 2 of the License, or
27103 + * (at your option) any later version.
27104 + *
27105 + * This program is distributed in the hope that it will be useful,
27106 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27107 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27108 + * GNU General Public License for more details.
27109 + *
27110 + * You should have received a copy of the GNU General Public License
27111 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27112 + */
27113 +
27114 +/*
27115 + * simple list protected by a spinlock
27116 + */
27117 +
27118 +#ifndef __AUFS_SPL_H__
27119 +#define __AUFS_SPL_H__
27120 +
27121 +#ifdef __KERNEL__
27122 +
27123 +#if 0
27124 +struct au_splhead {
27125 +       spinlock_t              spin;
27126 +       struct list_head        head;
27127 +};
27128 +
27129 +static inline void au_spl_init(struct au_splhead *spl)
27130 +{
27131 +       spin_lock_init(&spl->spin);
27132 +       INIT_LIST_HEAD(&spl->head);
27133 +}
27134 +
27135 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
27136 +{
27137 +       spin_lock(&spl->spin);
27138 +       list_add(list, &spl->head);
27139 +       spin_unlock(&spl->spin);
27140 +}
27141 +
27142 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
27143 +{
27144 +       spin_lock(&spl->spin);
27145 +       list_del(list);
27146 +       spin_unlock(&spl->spin);
27147 +}
27148 +
27149 +static inline void au_spl_del_rcu(struct list_head *list,
27150 +                                 struct au_splhead *spl)
27151 +{
27152 +       spin_lock(&spl->spin);
27153 +       list_del_rcu(list);
27154 +       spin_unlock(&spl->spin);
27155 +}
27156 +#endif
27157 +
27158 +/* ---------------------------------------------------------------------- */
27159 +
27160 +struct au_sphlhead {
27161 +       spinlock_t              spin;
27162 +       struct hlist_head       head;
27163 +};
27164 +
27165 +static inline void au_sphl_init(struct au_sphlhead *sphl)
27166 +{
27167 +       spin_lock_init(&sphl->spin);
27168 +       INIT_HLIST_HEAD(&sphl->head);
27169 +}
27170 +
27171 +static inline void au_sphl_add(struct hlist_node *hlist,
27172 +                              struct au_sphlhead *sphl)
27173 +{
27174 +       spin_lock(&sphl->spin);
27175 +       hlist_add_head(hlist, &sphl->head);
27176 +       spin_unlock(&sphl->spin);
27177 +}
27178 +
27179 +static inline void au_sphl_del(struct hlist_node *hlist,
27180 +                              struct au_sphlhead *sphl)
27181 +{
27182 +       spin_lock(&sphl->spin);
27183 +       hlist_del(hlist);
27184 +       spin_unlock(&sphl->spin);
27185 +}
27186 +
27187 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
27188 +                                  struct au_sphlhead *sphl)
27189 +{
27190 +       spin_lock(&sphl->spin);
27191 +       hlist_del_rcu(hlist);
27192 +       spin_unlock(&sphl->spin);
27193 +}
27194 +
27195 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
27196 +{
27197 +       unsigned long cnt;
27198 +       struct hlist_node *pos;
27199 +
27200 +       cnt = 0;
27201 +       spin_lock(&sphl->spin);
27202 +       hlist_for_each(pos, &sphl->head)
27203 +               cnt++;
27204 +       spin_unlock(&sphl->spin);
27205 +       return cnt;
27206 +}
27207 +
27208 +#endif /* __KERNEL__ */
27209 +#endif /* __AUFS_SPL_H__ */
27210 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
27211 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
27212 +++ linux/fs/aufs/super.c       2017-03-02 14:24:13.866922487 +0100
27213 @@ -0,0 +1,1044 @@
27214 +/*
27215 + * Copyright (C) 2005-2017 Junjiro R. Okajima
27216 + *
27217 + * This program, aufs is free software; you can redistribute it and/or modify
27218 + * it under the terms of the GNU General Public License as published by
27219 + * the Free Software Foundation; either version 2 of the License, or
27220 + * (at your option) any later version.
27221 + *
27222 + * This program is distributed in the hope that it will be useful,
27223 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27224 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27225 + * GNU General Public License for more details.
27226 + *
27227 + * You should have received a copy of the GNU General Public License
27228 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27229 + */
27230 +
27231 +/*
27232 + * mount and super_block operations
27233 + */
27234 +
27235 +#include <linux/mm.h>
27236 +#include <linux/seq_file.h>
27237 +#include <linux/statfs.h>
27238 +#include <linux/vmalloc.h>
27239 +#include "aufs.h"
27240 +
27241 +/*
27242 + * super_operations
27243 + */
27244 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
27245 +{
27246 +       struct au_icntnr *c;
27247 +
27248 +       c = au_cache_alloc_icntnr();
27249 +       if (c) {
27250 +               au_icntnr_init(c);
27251 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
27252 +               c->iinfo.ii_hinode = NULL;
27253 +               return &c->vfs_inode;
27254 +       }
27255 +       return NULL;
27256 +}
27257 +
27258 +static void aufs_destroy_inode_cb(struct rcu_head *head)
27259 +{
27260 +       struct inode *inode = container_of(head, struct inode, i_rcu);
27261 +
27262 +       au_cache_dfree_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
27263 +}
27264 +
27265 +static void aufs_destroy_inode(struct inode *inode)
27266 +{
27267 +       if (!au_is_bad_inode(inode))
27268 +               au_iinfo_fin(inode);
27269 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
27270 +}
27271 +
27272 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
27273 +{
27274 +       struct inode *inode;
27275 +       int err;
27276 +
27277 +       inode = iget_locked(sb, ino);
27278 +       if (unlikely(!inode)) {
27279 +               inode = ERR_PTR(-ENOMEM);
27280 +               goto out;
27281 +       }
27282 +       if (!(inode->i_state & I_NEW))
27283 +               goto out;
27284 +
27285 +       err = au_xigen_new(inode);
27286 +       if (!err)
27287 +               err = au_iinfo_init(inode);
27288 +       if (!err)
27289 +               inode->i_version++;
27290 +       else {
27291 +               iget_failed(inode);
27292 +               inode = ERR_PTR(err);
27293 +       }
27294 +
27295 +out:
27296 +       /* never return NULL */
27297 +       AuDebugOn(!inode);
27298 +       AuTraceErrPtr(inode);
27299 +       return inode;
27300 +}
27301 +
27302 +/* lock free root dinfo */
27303 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
27304 +{
27305 +       int err;
27306 +       aufs_bindex_t bindex, bbot;
27307 +       struct path path;
27308 +       struct au_hdentry *hdp;
27309 +       struct au_branch *br;
27310 +       au_br_perm_str_t perm;
27311 +
27312 +       err = 0;
27313 +       bbot = au_sbbot(sb);
27314 +       bindex = 0;
27315 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
27316 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
27317 +               br = au_sbr(sb, bindex);
27318 +               path.mnt = au_br_mnt(br);
27319 +               path.dentry = hdp->hd_dentry;
27320 +               err = au_seq_path(seq, &path);
27321 +               if (!err) {
27322 +                       au_optstr_br_perm(&perm, br->br_perm);
27323 +                       seq_printf(seq, "=%s", perm.a);
27324 +                       if (bindex != bbot)
27325 +                               seq_putc(seq, ':');
27326 +               }
27327 +       }
27328 +       if (unlikely(err || seq_has_overflowed(seq)))
27329 +               err = -E2BIG;
27330 +
27331 +       return err;
27332 +}
27333 +
27334 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
27335 +                      const char *append)
27336 +{
27337 +       char *p;
27338 +
27339 +       p = fmt;
27340 +       while (*pat != ':')
27341 +               *p++ = *pat++;
27342 +       *p++ = *pat++;
27343 +       strcpy(p, append);
27344 +       AuDebugOn(strlen(fmt) >= len);
27345 +}
27346 +
27347 +static void au_show_wbr_create(struct seq_file *m, int v,
27348 +                              struct au_sbinfo *sbinfo)
27349 +{
27350 +       const char *pat;
27351 +       char fmt[32];
27352 +       struct au_wbr_mfs *mfs;
27353 +
27354 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27355 +
27356 +       seq_puts(m, ",create=");
27357 +       pat = au_optstr_wbr_create(v);
27358 +       mfs = &sbinfo->si_wbr_mfs;
27359 +       switch (v) {
27360 +       case AuWbrCreate_TDP:
27361 +       case AuWbrCreate_RR:
27362 +       case AuWbrCreate_MFS:
27363 +       case AuWbrCreate_PMFS:
27364 +               seq_puts(m, pat);
27365 +               break;
27366 +       case AuWbrCreate_MFSRR:
27367 +       case AuWbrCreate_TDMFS:
27368 +       case AuWbrCreate_PMFSRR:
27369 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
27370 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
27371 +               break;
27372 +       case AuWbrCreate_MFSV:
27373 +       case AuWbrCreate_PMFSV:
27374 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
27375 +               seq_printf(m, fmt,
27376 +                          jiffies_to_msecs(mfs->mfs_expire)
27377 +                          / MSEC_PER_SEC);
27378 +               break;
27379 +       case AuWbrCreate_MFSRRV:
27380 +       case AuWbrCreate_TDMFSV:
27381 +       case AuWbrCreate_PMFSRRV:
27382 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
27383 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
27384 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
27385 +               break;
27386 +       default:
27387 +               BUG();
27388 +       }
27389 +}
27390 +
27391 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
27392 +{
27393 +#ifdef CONFIG_SYSFS
27394 +       return 0;
27395 +#else
27396 +       int err;
27397 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
27398 +       aufs_bindex_t bindex, brid;
27399 +       struct qstr *name;
27400 +       struct file *f;
27401 +       struct dentry *d, *h_root;
27402 +
27403 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27404 +
27405 +       err = 0;
27406 +       f = au_sbi(sb)->si_xib;
27407 +       if (!f)
27408 +               goto out;
27409 +
27410 +       /* stop printing the default xino path on the first writable branch */
27411 +       h_root = NULL;
27412 +       brid = au_xino_brid(sb);
27413 +       if (brid >= 0) {
27414 +               bindex = au_br_index(sb, brid);
27415 +               h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
27416 +       }
27417 +       d = f->f_path.dentry;
27418 +       name = &d->d_name;
27419 +       /* safe ->d_parent because the file is unlinked */
27420 +       if (d->d_parent == h_root
27421 +           && name->len == len
27422 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
27423 +               goto out;
27424 +
27425 +       seq_puts(seq, ",xino=");
27426 +       err = au_xino_path(seq, f);
27427 +
27428 +out:
27429 +       return err;
27430 +#endif
27431 +}
27432 +
27433 +/* seq_file will re-call me in case of too long string */
27434 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
27435 +{
27436 +       int err;
27437 +       unsigned int mnt_flags, v;
27438 +       struct super_block *sb;
27439 +       struct au_sbinfo *sbinfo;
27440 +
27441 +#define AuBool(name, str) do { \
27442 +       v = au_opt_test(mnt_flags, name); \
27443 +       if (v != au_opt_test(AuOpt_Def, name)) \
27444 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
27445 +} while (0)
27446 +
27447 +#define AuStr(name, str) do { \
27448 +       v = mnt_flags & AuOptMask_##name; \
27449 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
27450 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
27451 +} while (0)
27452 +
27453 +#define AuUInt(name, str, val) do { \
27454 +       if (val != AUFS_##name##_DEF) \
27455 +               seq_printf(m, "," #str "=%u", val); \
27456 +} while (0)
27457 +
27458 +       sb = dentry->d_sb;
27459 +       if (sb->s_flags & MS_POSIXACL)
27460 +               seq_puts(m, ",acl");
27461 +
27462 +       /* lock free root dinfo */
27463 +       si_noflush_read_lock(sb);
27464 +       sbinfo = au_sbi(sb);
27465 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
27466 +
27467 +       mnt_flags = au_mntflags(sb);
27468 +       if (au_opt_test(mnt_flags, XINO)) {
27469 +               err = au_show_xino(m, sb);
27470 +               if (unlikely(err))
27471 +                       goto out;
27472 +       } else
27473 +               seq_puts(m, ",noxino");
27474 +
27475 +       AuBool(TRUNC_XINO, trunc_xino);
27476 +       AuStr(UDBA, udba);
27477 +       AuBool(SHWH, shwh);
27478 +       AuBool(PLINK, plink);
27479 +       AuBool(DIO, dio);
27480 +       AuBool(DIRPERM1, dirperm1);
27481 +
27482 +       v = sbinfo->si_wbr_create;
27483 +       if (v != AuWbrCreate_Def)
27484 +               au_show_wbr_create(m, v, sbinfo);
27485 +
27486 +       v = sbinfo->si_wbr_copyup;
27487 +       if (v != AuWbrCopyup_Def)
27488 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
27489 +
27490 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
27491 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
27492 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
27493 +
27494 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
27495 +
27496 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
27497 +       AuUInt(RDCACHE, rdcache, v);
27498 +
27499 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
27500 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
27501 +
27502 +       au_fhsm_show(m, sbinfo);
27503 +
27504 +       AuBool(SUM, sum);
27505 +       /* AuBool(SUM_W, wsum); */
27506 +       AuBool(WARN_PERM, warn_perm);
27507 +       AuBool(VERBOSE, verbose);
27508 +
27509 +out:
27510 +       /* be sure to print "br:" last */
27511 +       if (!sysaufs_brs) {
27512 +               seq_puts(m, ",br:");
27513 +               au_show_brs(m, sb);
27514 +       }
27515 +       si_read_unlock(sb);
27516 +       return 0;
27517 +
27518 +#undef AuBool
27519 +#undef AuStr
27520 +#undef AuUInt
27521 +}
27522 +
27523 +/* ---------------------------------------------------------------------- */
27524 +
27525 +/* sum mode which returns the summation for statfs(2) */
27526 +
27527 +static u64 au_add_till_max(u64 a, u64 b)
27528 +{
27529 +       u64 old;
27530 +
27531 +       old = a;
27532 +       a += b;
27533 +       if (old <= a)
27534 +               return a;
27535 +       return ULLONG_MAX;
27536 +}
27537 +
27538 +static u64 au_mul_till_max(u64 a, long mul)
27539 +{
27540 +       u64 old;
27541 +
27542 +       old = a;
27543 +       a *= mul;
27544 +       if (old <= a)
27545 +               return a;
27546 +       return ULLONG_MAX;
27547 +}
27548 +
27549 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
27550 +{
27551 +       int err;
27552 +       long bsize, factor;
27553 +       u64 blocks, bfree, bavail, files, ffree;
27554 +       aufs_bindex_t bbot, bindex, i;
27555 +       unsigned char shared;
27556 +       struct path h_path;
27557 +       struct super_block *h_sb;
27558 +
27559 +       err = 0;
27560 +       bsize = LONG_MAX;
27561 +       files = 0;
27562 +       ffree = 0;
27563 +       blocks = 0;
27564 +       bfree = 0;
27565 +       bavail = 0;
27566 +       bbot = au_sbbot(sb);
27567 +       for (bindex = 0; bindex <= bbot; bindex++) {
27568 +               h_path.mnt = au_sbr_mnt(sb, bindex);
27569 +               h_sb = h_path.mnt->mnt_sb;
27570 +               shared = 0;
27571 +               for (i = 0; !shared && i < bindex; i++)
27572 +                       shared = (au_sbr_sb(sb, i) == h_sb);
27573 +               if (shared)
27574 +                       continue;
27575 +
27576 +               /* sb->s_root for NFS is unreliable */
27577 +               h_path.dentry = h_path.mnt->mnt_root;
27578 +               err = vfs_statfs(&h_path, buf);
27579 +               if (unlikely(err))
27580 +                       goto out;
27581 +
27582 +               if (bsize > buf->f_bsize) {
27583 +                       /*
27584 +                        * we will reduce bsize, so we have to expand blocks
27585 +                        * etc. to match them again
27586 +                        */
27587 +                       factor = (bsize / buf->f_bsize);
27588 +                       blocks = au_mul_till_max(blocks, factor);
27589 +                       bfree = au_mul_till_max(bfree, factor);
27590 +                       bavail = au_mul_till_max(bavail, factor);
27591 +                       bsize = buf->f_bsize;
27592 +               }
27593 +
27594 +               factor = (buf->f_bsize / bsize);
27595 +               blocks = au_add_till_max(blocks,
27596 +                               au_mul_till_max(buf->f_blocks, factor));
27597 +               bfree = au_add_till_max(bfree,
27598 +                               au_mul_till_max(buf->f_bfree, factor));
27599 +               bavail = au_add_till_max(bavail,
27600 +                               au_mul_till_max(buf->f_bavail, factor));
27601 +               files = au_add_till_max(files, buf->f_files);
27602 +               ffree = au_add_till_max(ffree, buf->f_ffree);
27603 +       }
27604 +
27605 +       buf->f_bsize = bsize;
27606 +       buf->f_blocks = blocks;
27607 +       buf->f_bfree = bfree;
27608 +       buf->f_bavail = bavail;
27609 +       buf->f_files = files;
27610 +       buf->f_ffree = ffree;
27611 +       buf->f_frsize = 0;
27612 +
27613 +out:
27614 +       return err;
27615 +}
27616 +
27617 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
27618 +{
27619 +       int err;
27620 +       struct path h_path;
27621 +       struct super_block *sb;
27622 +
27623 +       /* lock free root dinfo */
27624 +       sb = dentry->d_sb;
27625 +       si_noflush_read_lock(sb);
27626 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
27627 +               /* sb->s_root for NFS is unreliable */
27628 +               h_path.mnt = au_sbr_mnt(sb, 0);
27629 +               h_path.dentry = h_path.mnt->mnt_root;
27630 +               err = vfs_statfs(&h_path, buf);
27631 +       } else
27632 +               err = au_statfs_sum(sb, buf);
27633 +       si_read_unlock(sb);
27634 +
27635 +       if (!err) {
27636 +               buf->f_type = AUFS_SUPER_MAGIC;
27637 +               buf->f_namelen = AUFS_MAX_NAMELEN;
27638 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27639 +       }
27640 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27641 +
27642 +       return err;
27643 +}
27644 +
27645 +/* ---------------------------------------------------------------------- */
27646 +
27647 +static int aufs_sync_fs(struct super_block *sb, int wait)
27648 +{
27649 +       int err, e;
27650 +       aufs_bindex_t bbot, bindex;
27651 +       struct au_branch *br;
27652 +       struct super_block *h_sb;
27653 +
27654 +       err = 0;
27655 +       si_noflush_read_lock(sb);
27656 +       bbot = au_sbbot(sb);
27657 +       for (bindex = 0; bindex <= bbot; bindex++) {
27658 +               br = au_sbr(sb, bindex);
27659 +               if (!au_br_writable(br->br_perm))
27660 +                       continue;
27661 +
27662 +               h_sb = au_sbr_sb(sb, bindex);
27663 +               e = vfsub_sync_filesystem(h_sb, wait);
27664 +               if (unlikely(e && !err))
27665 +                       err = e;
27666 +               /* go on even if an error happens */
27667 +       }
27668 +       si_read_unlock(sb);
27669 +
27670 +       return err;
27671 +}
27672 +
27673 +/* ---------------------------------------------------------------------- */
27674 +
27675 +/* final actions when unmounting a file system */
27676 +static void aufs_put_super(struct super_block *sb)
27677 +{
27678 +       struct au_sbinfo *sbinfo;
27679 +
27680 +       sbinfo = au_sbi(sb);
27681 +       if (!sbinfo)
27682 +               return;
27683 +
27684 +       dbgaufs_si_fin(sbinfo);
27685 +       kobject_put(&sbinfo->si_kobj);
27686 +}
27687 +
27688 +/* ---------------------------------------------------------------------- */
27689 +
27690 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
27691 +                    struct super_block *sb, void *arg)
27692 +{
27693 +       void *array;
27694 +       unsigned long long n, sz;
27695 +
27696 +       array = NULL;
27697 +       n = 0;
27698 +       if (!*hint)
27699 +               goto out;
27700 +
27701 +       if (*hint > ULLONG_MAX / sizeof(array)) {
27702 +               array = ERR_PTR(-EMFILE);
27703 +               pr_err("hint %llu\n", *hint);
27704 +               goto out;
27705 +       }
27706 +
27707 +       sz = sizeof(array) * *hint;
27708 +       array = kzalloc(sz, GFP_NOFS);
27709 +       if (unlikely(!array))
27710 +               array = vzalloc(sz);
27711 +       if (unlikely(!array)) {
27712 +               array = ERR_PTR(-ENOMEM);
27713 +               goto out;
27714 +       }
27715 +
27716 +       n = cb(sb, array, *hint, arg);
27717 +       AuDebugOn(n > *hint);
27718 +
27719 +out:
27720 +       *hint = n;
27721 +       return array;
27722 +}
27723 +
27724 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
27725 +                                      unsigned long long max __maybe_unused,
27726 +                                      void *arg)
27727 +{
27728 +       unsigned long long n;
27729 +       struct inode **p, *inode;
27730 +       struct list_head *head;
27731 +
27732 +       n = 0;
27733 +       p = a;
27734 +       head = arg;
27735 +       spin_lock(&sb->s_inode_list_lock);
27736 +       list_for_each_entry(inode, head, i_sb_list) {
27737 +               if (!au_is_bad_inode(inode)
27738 +                   && au_ii(inode)->ii_btop >= 0) {
27739 +                       spin_lock(&inode->i_lock);
27740 +                       if (atomic_read(&inode->i_count)) {
27741 +                               au_igrab(inode);
27742 +                               *p++ = inode;
27743 +                               n++;
27744 +                               AuDebugOn(n > max);
27745 +                       }
27746 +                       spin_unlock(&inode->i_lock);
27747 +               }
27748 +       }
27749 +       spin_unlock(&sb->s_inode_list_lock);
27750 +
27751 +       return n;
27752 +}
27753 +
27754 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27755 +{
27756 +       *max = au_ninodes(sb);
27757 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
27758 +}
27759 +
27760 +void au_iarray_free(struct inode **a, unsigned long long max)
27761 +{
27762 +       unsigned long long ull;
27763 +
27764 +       for (ull = 0; ull < max; ull++)
27765 +               iput(a[ull]);
27766 +       kvfree(a);
27767 +}
27768 +
27769 +/* ---------------------------------------------------------------------- */
27770 +
27771 +/*
27772 + * refresh dentry and inode at remount time.
27773 + */
27774 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27775 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27776 +                     struct dentry *parent)
27777 +{
27778 +       int err;
27779 +
27780 +       di_write_lock_child(dentry);
27781 +       di_read_lock_parent(parent, AuLock_IR);
27782 +       err = au_refresh_dentry(dentry, parent);
27783 +       if (!err && dir_flags)
27784 +               au_hn_reset(d_inode(dentry), dir_flags);
27785 +       di_read_unlock(parent, AuLock_IR);
27786 +       di_write_unlock(dentry);
27787 +
27788 +       return err;
27789 +}
27790 +
27791 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27792 +                          struct au_sbinfo *sbinfo,
27793 +                          const unsigned int dir_flags, unsigned int do_idop)
27794 +{
27795 +       int err;
27796 +       struct dentry *parent;
27797 +
27798 +       err = 0;
27799 +       parent = dget_parent(dentry);
27800 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
27801 +               if (d_really_is_positive(dentry)) {
27802 +                       if (!d_is_dir(dentry))
27803 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
27804 +                                                parent);
27805 +                       else {
27806 +                               err = au_do_refresh(dentry, dir_flags, parent);
27807 +                               if (unlikely(err))
27808 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27809 +                       }
27810 +               } else
27811 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27812 +               AuDbgDentry(dentry);
27813 +       }
27814 +       dput(parent);
27815 +
27816 +       if (!err) {
27817 +               if (do_idop)
27818 +                       au_refresh_dop(dentry, /*force_reval*/0);
27819 +       } else
27820 +               au_refresh_dop(dentry, /*force_reval*/1);
27821 +
27822 +       AuTraceErr(err);
27823 +       return err;
27824 +}
27825 +
27826 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
27827 +{
27828 +       int err, i, j, ndentry, e;
27829 +       unsigned int sigen;
27830 +       struct au_dcsub_pages dpages;
27831 +       struct au_dpage *dpage;
27832 +       struct dentry **dentries, *d;
27833 +       struct au_sbinfo *sbinfo;
27834 +       struct dentry *root = sb->s_root;
27835 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
27836 +
27837 +       if (do_idop)
27838 +               au_refresh_dop(root, /*force_reval*/0);
27839 +
27840 +       err = au_dpages_init(&dpages, GFP_NOFS);
27841 +       if (unlikely(err))
27842 +               goto out;
27843 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
27844 +       if (unlikely(err))
27845 +               goto out_dpages;
27846 +
27847 +       sigen = au_sigen(sb);
27848 +       sbinfo = au_sbi(sb);
27849 +       for (i = 0; i < dpages.ndpage; i++) {
27850 +               dpage = dpages.dpages + i;
27851 +               dentries = dpage->dentries;
27852 +               ndentry = dpage->ndentry;
27853 +               for (j = 0; j < ndentry; j++) {
27854 +                       d = dentries[j];
27855 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
27856 +                                           do_idop);
27857 +                       if (unlikely(e && !err))
27858 +                               err = e;
27859 +                       /* go on even err */
27860 +               }
27861 +       }
27862 +
27863 +out_dpages:
27864 +       au_dpages_free(&dpages);
27865 +out:
27866 +       return err;
27867 +}
27868 +
27869 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
27870 +{
27871 +       int err, e;
27872 +       unsigned int sigen;
27873 +       unsigned long long max, ull;
27874 +       struct inode *inode, **array;
27875 +
27876 +       array = au_iarray_alloc(sb, &max);
27877 +       err = PTR_ERR(array);
27878 +       if (IS_ERR(array))
27879 +               goto out;
27880 +
27881 +       err = 0;
27882 +       sigen = au_sigen(sb);
27883 +       for (ull = 0; ull < max; ull++) {
27884 +               inode = array[ull];
27885 +               if (unlikely(!inode))
27886 +                       break;
27887 +
27888 +               e = 0;
27889 +               ii_write_lock_child(inode);
27890 +               if (au_iigen(inode, NULL) != sigen) {
27891 +                       e = au_refresh_hinode_self(inode);
27892 +                       if (unlikely(e)) {
27893 +                               au_refresh_iop(inode, /*force_getattr*/1);
27894 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
27895 +                               if (!err)
27896 +                                       err = e;
27897 +                               /* go on even if err */
27898 +                       }
27899 +               }
27900 +               if (!e && do_idop)
27901 +                       au_refresh_iop(inode, /*force_getattr*/0);
27902 +               ii_write_unlock(inode);
27903 +       }
27904 +
27905 +       au_iarray_free(array, max);
27906 +
27907 +out:
27908 +       return err;
27909 +}
27910 +
27911 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
27912 +{
27913 +       int err, e;
27914 +       unsigned int udba;
27915 +       aufs_bindex_t bindex, bbot;
27916 +       struct dentry *root;
27917 +       struct inode *inode;
27918 +       struct au_branch *br;
27919 +       struct au_sbinfo *sbi;
27920 +
27921 +       au_sigen_inc(sb);
27922 +       sbi = au_sbi(sb);
27923 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
27924 +
27925 +       root = sb->s_root;
27926 +       DiMustNoWaiters(root);
27927 +       inode = d_inode(root);
27928 +       IiMustNoWaiters(inode);
27929 +
27930 +       udba = au_opt_udba(sb);
27931 +       bbot = au_sbbot(sb);
27932 +       for (bindex = 0; bindex <= bbot; bindex++) {
27933 +               br = au_sbr(sb, bindex);
27934 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
27935 +               if (unlikely(err))
27936 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27937 +                               bindex, err);
27938 +               /* go on even if err */
27939 +       }
27940 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
27941 +
27942 +       if (do_idop) {
27943 +               if (au_ftest_si(sbi, NO_DREVAL)) {
27944 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
27945 +                       sb->s_d_op = &aufs_dop_noreval;
27946 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
27947 +                       sbi->si_iop_array = aufs_iop_nogetattr;
27948 +               } else {
27949 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
27950 +                       sb->s_d_op = &aufs_dop;
27951 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
27952 +                       sbi->si_iop_array = aufs_iop;
27953 +               }
27954 +               pr_info("reset to %pf and %pf\n",
27955 +                       sb->s_d_op, sbi->si_iop_array);
27956 +       }
27957 +
27958 +       di_write_unlock(root);
27959 +       err = au_refresh_d(sb, do_idop);
27960 +       e = au_refresh_i(sb, do_idop);
27961 +       if (unlikely(e && !err))
27962 +               err = e;
27963 +       /* aufs_write_lock() calls ..._child() */
27964 +       di_write_lock_child(root);
27965 +
27966 +       au_cpup_attr_all(inode, /*force*/1);
27967 +
27968 +       if (unlikely(err))
27969 +               AuIOErr("refresh failed, ignored, %d\n", err);
27970 +}
27971 +
27972 +/* stop extra interpretation of errno in mount(8), and strange error messages */
27973 +static int cvt_err(int err)
27974 +{
27975 +       AuTraceErr(err);
27976 +
27977 +       switch (err) {
27978 +       case -ENOENT:
27979 +       case -ENOTDIR:
27980 +       case -EEXIST:
27981 +       case -EIO:
27982 +               err = -EINVAL;
27983 +       }
27984 +       return err;
27985 +}
27986 +
27987 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27988 +{
27989 +       int err, do_dx;
27990 +       unsigned int mntflags;
27991 +       struct au_opts opts = {
27992 +               .opt = NULL
27993 +       };
27994 +       struct dentry *root;
27995 +       struct inode *inode;
27996 +       struct au_sbinfo *sbinfo;
27997 +
27998 +       err = 0;
27999 +       root = sb->s_root;
28000 +       if (!data || !*data) {
28001 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
28002 +               if (!err) {
28003 +                       di_write_lock_child(root);
28004 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
28005 +                       aufs_write_unlock(root);
28006 +               }
28007 +               goto out;
28008 +       }
28009 +
28010 +       err = -ENOMEM;
28011 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
28012 +       if (unlikely(!opts.opt))
28013 +               goto out;
28014 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
28015 +       opts.flags = AuOpts_REMOUNT;
28016 +       opts.sb_flags = *flags;
28017 +
28018 +       /* parse it before aufs lock */
28019 +       err = au_opts_parse(sb, data, &opts);
28020 +       if (unlikely(err))
28021 +               goto out_opts;
28022 +
28023 +       sbinfo = au_sbi(sb);
28024 +       inode = d_inode(root);
28025 +       inode_lock(inode);
28026 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
28027 +       if (unlikely(err))
28028 +               goto out_mtx;
28029 +       di_write_lock_child(root);
28030 +
28031 +       /* au_opts_remount() may return an error */
28032 +       err = au_opts_remount(sb, &opts);
28033 +       au_opts_free(&opts);
28034 +
28035 +       if (au_ftest_opts(opts.flags, REFRESH))
28036 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
28037 +
28038 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
28039 +               mntflags = au_mntflags(sb);
28040 +               do_dx = !!au_opt_test(mntflags, DIO);
28041 +               au_dy_arefresh(do_dx);
28042 +       }
28043 +
28044 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
28045 +       aufs_write_unlock(root);
28046 +
28047 +out_mtx:
28048 +       inode_unlock(inode);
28049 +out_opts:
28050 +       au_delayed_free_page((unsigned long)opts.opt);
28051 +out:
28052 +       err = cvt_err(err);
28053 +       AuTraceErr(err);
28054 +       return err;
28055 +}
28056 +
28057 +static const struct super_operations aufs_sop = {
28058 +       .alloc_inode    = aufs_alloc_inode,
28059 +       .destroy_inode  = aufs_destroy_inode,
28060 +       /* always deleting, no clearing */
28061 +       .drop_inode     = generic_delete_inode,
28062 +       .show_options   = aufs_show_options,
28063 +       .statfs         = aufs_statfs,
28064 +       .put_super      = aufs_put_super,
28065 +       .sync_fs        = aufs_sync_fs,
28066 +       .remount_fs     = aufs_remount_fs
28067 +};
28068 +
28069 +/* ---------------------------------------------------------------------- */
28070 +
28071 +static int alloc_root(struct super_block *sb)
28072 +{
28073 +       int err;
28074 +       struct inode *inode;
28075 +       struct dentry *root;
28076 +
28077 +       err = -ENOMEM;
28078 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
28079 +       err = PTR_ERR(inode);
28080 +       if (IS_ERR(inode))
28081 +               goto out;
28082 +
28083 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
28084 +       inode->i_fop = &aufs_dir_fop;
28085 +       inode->i_mode = S_IFDIR;
28086 +       set_nlink(inode, 2);
28087 +       unlock_new_inode(inode);
28088 +
28089 +       root = d_make_root(inode);
28090 +       if (unlikely(!root))
28091 +               goto out;
28092 +       err = PTR_ERR(root);
28093 +       if (IS_ERR(root))
28094 +               goto out;
28095 +
28096 +       err = au_di_init(root);
28097 +       if (!err) {
28098 +               sb->s_root = root;
28099 +               return 0; /* success */
28100 +       }
28101 +       dput(root);
28102 +
28103 +out:
28104 +       return err;
28105 +}
28106 +
28107 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
28108 +                          int silent __maybe_unused)
28109 +{
28110 +       int err;
28111 +       struct au_opts opts = {
28112 +               .opt = NULL
28113 +       };
28114 +       struct au_sbinfo *sbinfo;
28115 +       struct dentry *root;
28116 +       struct inode *inode;
28117 +       char *arg = raw_data;
28118 +
28119 +       if (unlikely(!arg || !*arg)) {
28120 +               err = -EINVAL;
28121 +               pr_err("no arg\n");
28122 +               goto out;
28123 +       }
28124 +
28125 +       err = -ENOMEM;
28126 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
28127 +       if (unlikely(!opts.opt))
28128 +               goto out;
28129 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
28130 +       opts.sb_flags = sb->s_flags;
28131 +
28132 +       err = au_si_alloc(sb);
28133 +       if (unlikely(err))
28134 +               goto out_opts;
28135 +       sbinfo = au_sbi(sb);
28136 +
28137 +       /* all timestamps always follow the ones on the branch */
28138 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
28139 +       sb->s_op = &aufs_sop;
28140 +       sb->s_d_op = &aufs_dop;
28141 +       sb->s_magic = AUFS_SUPER_MAGIC;
28142 +       sb->s_maxbytes = 0;
28143 +       sb->s_stack_depth = 1;
28144 +       au_export_init(sb);
28145 +       au_xattr_init(sb);
28146 +
28147 +       err = alloc_root(sb);
28148 +       if (unlikely(err)) {
28149 +               si_write_unlock(sb);
28150 +               goto out_info;
28151 +       }
28152 +       root = sb->s_root;
28153 +       inode = d_inode(root);
28154 +
28155 +       /*
28156 +        * actually we can parse options regardless aufs lock here.
28157 +        * but at remount time, parsing must be done before aufs lock.
28158 +        * so we follow the same rule.
28159 +        */
28160 +       ii_write_lock_parent(inode);
28161 +       aufs_write_unlock(root);
28162 +       err = au_opts_parse(sb, arg, &opts);
28163 +       if (unlikely(err))
28164 +               goto out_root;
28165 +
28166 +       /* lock vfs_inode first, then aufs. */
28167 +       inode_lock(inode);
28168 +       aufs_write_lock(root);
28169 +       err = au_opts_mount(sb, &opts);
28170 +       au_opts_free(&opts);
28171 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
28172 +               sb->s_d_op = &aufs_dop_noreval;
28173 +               pr_info("%pf\n", sb->s_d_op);
28174 +               au_refresh_dop(root, /*force_reval*/0);
28175 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
28176 +               au_refresh_iop(inode, /*force_getattr*/0);
28177 +       }
28178 +       aufs_write_unlock(root);
28179 +       inode_unlock(inode);
28180 +       if (!err)
28181 +               goto out_opts; /* success */
28182 +
28183 +out_root:
28184 +       dput(root);
28185 +       sb->s_root = NULL;
28186 +out_info:
28187 +       dbgaufs_si_fin(sbinfo);
28188 +       kobject_put(&sbinfo->si_kobj);
28189 +       sb->s_fs_info = NULL;
28190 +out_opts:
28191 +       au_delayed_free_page((unsigned long)opts.opt);
28192 +out:
28193 +       AuTraceErr(err);
28194 +       err = cvt_err(err);
28195 +       AuTraceErr(err);
28196 +       return err;
28197 +}
28198 +
28199 +/* ---------------------------------------------------------------------- */
28200 +
28201 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
28202 +                                const char *dev_name __maybe_unused,
28203 +                                void *raw_data)
28204 +{
28205 +       struct dentry *root;
28206 +       struct super_block *sb;
28207 +
28208 +       /* all timestamps always follow the ones on the branch */
28209 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
28210 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
28211 +       if (IS_ERR(root))
28212 +               goto out;
28213 +
28214 +       sb = root->d_sb;
28215 +       si_write_lock(sb, !AuLock_FLUSH);
28216 +       sysaufs_brs_add(sb, 0);
28217 +       si_write_unlock(sb);
28218 +       au_sbilist_add(sb);
28219 +
28220 +out:
28221 +       return root;
28222 +}
28223 +
28224 +static void aufs_kill_sb(struct super_block *sb)
28225 +{
28226 +       struct au_sbinfo *sbinfo;
28227 +
28228 +       sbinfo = au_sbi(sb);
28229 +       if (sbinfo) {
28230 +               au_sbilist_del(sb);
28231 +               aufs_write_lock(sb->s_root);
28232 +               au_fhsm_fin(sb);
28233 +               if (sbinfo->si_wbr_create_ops->fin)
28234 +                       sbinfo->si_wbr_create_ops->fin(sb);
28235 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
28236 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
28237 +                       au_remount_refresh(sb, /*do_idop*/0);
28238 +               }
28239 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
28240 +                       au_plink_put(sb, /*verbose*/1);
28241 +               au_xino_clr(sb);
28242 +               sbinfo->si_sb = NULL;
28243 +               aufs_write_unlock(sb->s_root);
28244 +               au_nwt_flush(&sbinfo->si_nowait);
28245 +       }
28246 +       kill_anon_super(sb);
28247 +}
28248 +
28249 +struct file_system_type aufs_fs_type = {
28250 +       .name           = AUFS_FSTYPE,
28251 +       /* a race between rename and others */
28252 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
28253 +       .mount          = aufs_mount,
28254 +       .kill_sb        = aufs_kill_sb,
28255 +       /* no need to __module_get() and module_put(). */
28256 +       .owner          = THIS_MODULE,
28257 +};
28258 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
28259 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
28260 +++ linux/fs/aufs/super.h       2017-03-02 14:24:13.866922487 +0100
28261 @@ -0,0 +1,617 @@
28262 +/*
28263 + * Copyright (C) 2005-2017 Junjiro R. Okajima
28264 + *
28265 + * This program, aufs is free software; you can redistribute it and/or modify
28266 + * it under the terms of the GNU General Public License as published by
28267 + * the Free Software Foundation; either version 2 of the License, or
28268 + * (at your option) any later version.
28269 + *
28270 + * This program is distributed in the hope that it will be useful,
28271 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28272 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28273 + * GNU General Public License for more details.
28274 + *
28275 + * You should have received a copy of the GNU General Public License
28276 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28277 + */
28278 +
28279 +/*
28280 + * super_block operations
28281 + */
28282 +
28283 +#ifndef __AUFS_SUPER_H__
28284 +#define __AUFS_SUPER_H__
28285 +
28286 +#ifdef __KERNEL__
28287 +
28288 +#include <linux/fs.h>
28289 +#include <linux/kobject.h>
28290 +#include "rwsem.h"
28291 +#include "spl.h"
28292 +#include "wkq.h"
28293 +
28294 +/* policies to select one among multiple writable branches */
28295 +struct au_wbr_copyup_operations {
28296 +       int (*copyup)(struct dentry *dentry);
28297 +};
28298 +
28299 +#define AuWbr_DIR      1               /* target is a dir */
28300 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
28301 +
28302 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
28303 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
28304 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
28305 +
28306 +struct au_wbr_create_operations {
28307 +       int (*create)(struct dentry *dentry, unsigned int flags);
28308 +       int (*init)(struct super_block *sb);
28309 +       int (*fin)(struct super_block *sb);
28310 +};
28311 +
28312 +struct au_wbr_mfs {
28313 +       struct mutex    mfs_lock; /* protect this structure */
28314 +       unsigned long   mfs_jiffy;
28315 +       unsigned long   mfs_expire;
28316 +       aufs_bindex_t   mfs_bindex;
28317 +
28318 +       unsigned long long      mfsrr_bytes;
28319 +       unsigned long long      mfsrr_watermark;
28320 +};
28321 +
28322 +#define AuPlink_NHASH 100
28323 +static inline int au_plink_hash(ino_t ino)
28324 +{
28325 +       return ino % AuPlink_NHASH;
28326 +}
28327 +
28328 +/* File-based Hierarchical Storage Management */
28329 +struct au_fhsm {
28330 +#ifdef CONFIG_AUFS_FHSM
28331 +       /* allow only one process who can receive the notification */
28332 +       spinlock_t              fhsm_spin;
28333 +       pid_t                   fhsm_pid;
28334 +       wait_queue_head_t       fhsm_wqh;
28335 +       atomic_t                fhsm_readable;
28336 +
28337 +       /* these are protected by si_rwsem */
28338 +       unsigned long           fhsm_expire;
28339 +       aufs_bindex_t           fhsm_bottom;
28340 +#endif
28341 +};
28342 +
28343 +struct au_branch;
28344 +struct au_sbinfo {
28345 +       /* nowait tasks in the system-wide workqueue */
28346 +       struct au_nowait_tasks  si_nowait;
28347 +
28348 +       /*
28349 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
28350 +        * rwsem for au_sbinfo is necessary.
28351 +        */
28352 +       struct au_rwsem         si_rwsem;
28353 +
28354 +       /*
28355 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
28356 +        * remount.
28357 +        */
28358 +       struct percpu_counter   si_ninodes, si_nfiles;
28359 +
28360 +       /* branch management */
28361 +       unsigned int            si_generation;
28362 +
28363 +       /* see AuSi_ flags */
28364 +       unsigned char           au_si_status;
28365 +
28366 +       aufs_bindex_t           si_bbot;
28367 +
28368 +       /* dirty trick to keep br_id plus */
28369 +       unsigned int            si_last_br_id :
28370 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
28371 +       struct au_branch        **si_branch;
28372 +
28373 +       /* policy to select a writable branch */
28374 +       unsigned char           si_wbr_copyup;
28375 +       unsigned char           si_wbr_create;
28376 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
28377 +       struct au_wbr_create_operations *si_wbr_create_ops;
28378 +
28379 +       /* round robin */
28380 +       atomic_t                si_wbr_rr_next;
28381 +
28382 +       /* most free space */
28383 +       struct au_wbr_mfs       si_wbr_mfs;
28384 +
28385 +       /* File-based Hierarchical Storage Management */
28386 +       struct au_fhsm          si_fhsm;
28387 +
28388 +       /* mount flags */
28389 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
28390 +       unsigned int            si_mntflags;
28391 +
28392 +       /* external inode number (bitmap and translation table) */
28393 +       vfs_readf_t             si_xread;
28394 +       vfs_writef_t            si_xwrite;
28395 +       struct file             *si_xib;
28396 +       struct mutex            si_xib_mtx; /* protect xib members */
28397 +       unsigned long           *si_xib_buf;
28398 +       unsigned long           si_xib_last_pindex;
28399 +       int                     si_xib_next_bit;
28400 +       aufs_bindex_t           si_xino_brid;
28401 +       unsigned long           si_xino_jiffy;
28402 +       unsigned long           si_xino_expire;
28403 +       /* reserved for future use */
28404 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
28405 +
28406 +#ifdef CONFIG_AUFS_EXPORT
28407 +       /* i_generation */
28408 +       struct file             *si_xigen;
28409 +       atomic_t                si_xigen_next;
28410 +#endif
28411 +
28412 +       /* dirty trick to suppoer atomic_open */
28413 +       struct au_sphlhead      si_aopen;
28414 +
28415 +       /* vdir parameters */
28416 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
28417 +       unsigned int            si_rdblk;       /* deblk size */
28418 +       unsigned int            si_rdhash;      /* hash size */
28419 +
28420 +       /*
28421 +        * If the number of whiteouts are larger than si_dirwh, leave all of
28422 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
28423 +        * future fsck.aufs or kernel thread will remove them later.
28424 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
28425 +        */
28426 +       unsigned int            si_dirwh;
28427 +
28428 +       /* pseudo_link list */
28429 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
28430 +       wait_queue_head_t       si_plink_wq;
28431 +       spinlock_t              si_plink_maint_lock;
28432 +       pid_t                   si_plink_maint_pid;
28433 +
28434 +       /* file list */
28435 +       struct au_sphlhead      si_files;
28436 +
28437 +       /* with/without getattr, brother of sb->s_d_op */
28438 +       struct inode_operations *si_iop_array;
28439 +
28440 +       /*
28441 +        * sysfs and lifetime management.
28442 +        * this is not a small structure and it may be a waste of memory in case
28443 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
28444 +        * but using sysfs is majority.
28445 +        */
28446 +       struct kobject          si_kobj;
28447 +#ifdef CONFIG_DEBUG_FS
28448 +       struct dentry            *si_dbgaufs;
28449 +       struct dentry            *si_dbgaufs_plink;
28450 +       struct dentry            *si_dbgaufs_xib;
28451 +#ifdef CONFIG_AUFS_EXPORT
28452 +       struct dentry            *si_dbgaufs_xigen;
28453 +#endif
28454 +#endif
28455 +
28456 +#ifdef CONFIG_AUFS_SBILIST
28457 +       struct hlist_node       si_list;
28458 +#endif
28459 +
28460 +       /* dirty, necessary for unmounting, sysfs and sysrq */
28461 +       struct super_block      *si_sb;
28462 +};
28463 +
28464 +/* sbinfo status flags */
28465 +/*
28466 + * set true when refresh_dirs() failed at remount time.
28467 + * then try refreshing dirs at access time again.
28468 + * if it is false, refreshing dirs at access time is unnecesary
28469 + */
28470 +#define AuSi_FAILED_REFRESH_DIR        1
28471 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
28472 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
28473 +
28474 +#ifndef CONFIG_AUFS_FHSM
28475 +#undef AuSi_FHSM
28476 +#define AuSi_FHSM              0
28477 +#endif
28478 +
28479 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
28480 +                                          unsigned int flag)
28481 +{
28482 +       AuRwMustAnyLock(&sbi->si_rwsem);
28483 +       return sbi->au_si_status & flag;
28484 +}
28485 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
28486 +#define au_fset_si(sbinfo, name) do { \
28487 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28488 +       (sbinfo)->au_si_status |= AuSi_##name; \
28489 +} while (0)
28490 +#define au_fclr_si(sbinfo, name) do { \
28491 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28492 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
28493 +} while (0)
28494 +
28495 +/* ---------------------------------------------------------------------- */
28496 +
28497 +/* policy to select one among writable branches */
28498 +#define AuWbrCopyup(sbinfo, ...) \
28499 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
28500 +#define AuWbrCreate(sbinfo, ...) \
28501 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
28502 +
28503 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
28504 +#define AuLock_DW              1               /* write-lock dentry */
28505 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
28506 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
28507 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
28508 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
28509 +                                               /* except RENAME_EXCHANGE */
28510 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
28511 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
28512 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
28513 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
28514 +#define au_fset_lock(flags, name) \
28515 +       do { (flags) |= AuLock_##name; } while (0)
28516 +#define au_fclr_lock(flags, name) \
28517 +       do { (flags) &= ~AuLock_##name; } while (0)
28518 +
28519 +/* ---------------------------------------------------------------------- */
28520 +
28521 +/* super.c */
28522 +extern struct file_system_type aufs_fs_type;
28523 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
28524 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
28525 +                                          unsigned long long max, void *arg);
28526 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
28527 +                    struct super_block *sb, void *arg);
28528 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
28529 +void au_iarray_free(struct inode **a, unsigned long long max);
28530 +
28531 +/* sbinfo.c */
28532 +void au_si_free(struct kobject *kobj);
28533 +int au_si_alloc(struct super_block *sb);
28534 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
28535 +
28536 +unsigned int au_sigen_inc(struct super_block *sb);
28537 +aufs_bindex_t au_new_br_id(struct super_block *sb);
28538 +
28539 +int si_read_lock(struct super_block *sb, int flags);
28540 +int si_write_lock(struct super_block *sb, int flags);
28541 +int aufs_read_lock(struct dentry *dentry, int flags);
28542 +void aufs_read_unlock(struct dentry *dentry, int flags);
28543 +void aufs_write_lock(struct dentry *dentry);
28544 +void aufs_write_unlock(struct dentry *dentry);
28545 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
28546 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
28547 +
28548 +/* wbr_policy.c */
28549 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
28550 +extern struct au_wbr_create_operations au_wbr_create_ops[];
28551 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
28552 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
28553 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
28554 +
28555 +/* mvdown.c */
28556 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
28557 +
28558 +#ifdef CONFIG_AUFS_FHSM
28559 +/* fhsm.c */
28560 +
28561 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
28562 +{
28563 +       pid_t pid;
28564 +
28565 +       spin_lock(&fhsm->fhsm_spin);
28566 +       pid = fhsm->fhsm_pid;
28567 +       spin_unlock(&fhsm->fhsm_spin);
28568 +
28569 +       return pid;
28570 +}
28571 +
28572 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
28573 +void au_fhsm_wrote_all(struct super_block *sb, int force);
28574 +int au_fhsm_fd(struct super_block *sb, int oflags);
28575 +int au_fhsm_br_alloc(struct au_branch *br);
28576 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
28577 +void au_fhsm_fin(struct super_block *sb);
28578 +void au_fhsm_init(struct au_sbinfo *sbinfo);
28579 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
28580 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
28581 +#else
28582 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
28583 +          int force)
28584 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
28585 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
28586 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
28587 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
28588 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
28589 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
28590 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
28591 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
28592 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
28593 +#endif
28594 +
28595 +/* ---------------------------------------------------------------------- */
28596 +
28597 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
28598 +{
28599 +       return sb->s_fs_info;
28600 +}
28601 +
28602 +/* ---------------------------------------------------------------------- */
28603 +
28604 +#ifdef CONFIG_AUFS_EXPORT
28605 +int au_test_nfsd(void);
28606 +void au_export_init(struct super_block *sb);
28607 +void au_xigen_inc(struct inode *inode);
28608 +int au_xigen_new(struct inode *inode);
28609 +int au_xigen_set(struct super_block *sb, struct file *base);
28610 +void au_xigen_clr(struct super_block *sb);
28611 +
28612 +static inline int au_busy_or_stale(void)
28613 +{
28614 +       if (!au_test_nfsd())
28615 +               return -EBUSY;
28616 +       return -ESTALE;
28617 +}
28618 +#else
28619 +AuStubInt0(au_test_nfsd, void)
28620 +AuStubVoid(au_export_init, struct super_block *sb)
28621 +AuStubVoid(au_xigen_inc, struct inode *inode)
28622 +AuStubInt0(au_xigen_new, struct inode *inode)
28623 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
28624 +AuStubVoid(au_xigen_clr, struct super_block *sb)
28625 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
28626 +#endif /* CONFIG_AUFS_EXPORT */
28627 +
28628 +/* ---------------------------------------------------------------------- */
28629 +
28630 +#ifdef CONFIG_AUFS_SBILIST
28631 +/* module.c */
28632 +extern struct au_sphlhead au_sbilist;
28633 +
28634 +static inline void au_sbilist_init(void)
28635 +{
28636 +       au_sphl_init(&au_sbilist);
28637 +}
28638 +
28639 +static inline void au_sbilist_add(struct super_block *sb)
28640 +{
28641 +       au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
28642 +}
28643 +
28644 +static inline void au_sbilist_del(struct super_block *sb)
28645 +{
28646 +       au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
28647 +}
28648 +
28649 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
28650 +static inline void au_sbilist_lock(void)
28651 +{
28652 +       spin_lock(&au_sbilist.spin);
28653 +}
28654 +
28655 +static inline void au_sbilist_unlock(void)
28656 +{
28657 +       spin_unlock(&au_sbilist.spin);
28658 +}
28659 +#define AuGFP_SBILIST  GFP_ATOMIC
28660 +#else
28661 +AuStubVoid(au_sbilist_lock, void)
28662 +AuStubVoid(au_sbilist_unlock, void)
28663 +#define AuGFP_SBILIST  GFP_NOFS
28664 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
28665 +#else
28666 +AuStubVoid(au_sbilist_init, void)
28667 +AuStubVoid(au_sbilist_add, struct super_block *sb)
28668 +AuStubVoid(au_sbilist_del, struct super_block *sb)
28669 +AuStubVoid(au_sbilist_lock, void)
28670 +AuStubVoid(au_sbilist_unlock, void)
28671 +#define AuGFP_SBILIST  GFP_NOFS
28672 +#endif
28673 +
28674 +/* ---------------------------------------------------------------------- */
28675 +
28676 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28677 +{
28678 +       /*
28679 +        * This function is a dynamic '__init' function actually,
28680 +        * so the tiny check for si_rwsem is unnecessary.
28681 +        */
28682 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
28683 +#ifdef CONFIG_DEBUG_FS
28684 +       sbinfo->si_dbgaufs = NULL;
28685 +       sbinfo->si_dbgaufs_plink = NULL;
28686 +       sbinfo->si_dbgaufs_xib = NULL;
28687 +#ifdef CONFIG_AUFS_EXPORT
28688 +       sbinfo->si_dbgaufs_xigen = NULL;
28689 +#endif
28690 +#endif
28691 +}
28692 +
28693 +/* ---------------------------------------------------------------------- */
28694 +
28695 +/* current->atomic_flags */
28696 +/* this value should never corrupt the ones defined in linux/sched.h */
28697 +#define PFA_AUFS       7
28698 +
28699 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
28700 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
28701 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
28702 +
28703 +static inline int si_pid_test(struct super_block *sb)
28704 +{
28705 +       return !!task_test_aufs(current);
28706 +}
28707 +
28708 +static inline void si_pid_clr(struct super_block *sb)
28709 +{
28710 +       AuDebugOn(!task_test_aufs(current));
28711 +       task_clear_aufs(current);
28712 +}
28713 +
28714 +static inline void si_pid_set(struct super_block *sb)
28715 +{
28716 +       AuDebugOn(task_test_aufs(current));
28717 +       task_set_aufs(current);
28718 +}
28719 +
28720 +/* ---------------------------------------------------------------------- */
28721 +
28722 +/* lock superblock. mainly for entry point functions */
28723 +/*
28724 + * __si_read_lock, __si_write_lock,
28725 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
28726 + */
28727 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
28728 +
28729 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28730 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28731 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28732 +
28733 +static inline void si_noflush_read_lock(struct super_block *sb)
28734 +{
28735 +       __si_read_lock(sb);
28736 +       si_pid_set(sb);
28737 +}
28738 +
28739 +static inline int si_noflush_read_trylock(struct super_block *sb)
28740 +{
28741 +       int locked;
28742 +
28743 +       locked = __si_read_trylock(sb);
28744 +       if (locked)
28745 +               si_pid_set(sb);
28746 +       return locked;
28747 +}
28748 +
28749 +static inline void si_noflush_write_lock(struct super_block *sb)
28750 +{
28751 +       __si_write_lock(sb);
28752 +       si_pid_set(sb);
28753 +}
28754 +
28755 +static inline int si_noflush_write_trylock(struct super_block *sb)
28756 +{
28757 +       int locked;
28758 +
28759 +       locked = __si_write_trylock(sb);
28760 +       if (locked)
28761 +               si_pid_set(sb);
28762 +       return locked;
28763 +}
28764 +
28765 +#if 0 /* reserved */
28766 +static inline int si_read_trylock(struct super_block *sb, int flags)
28767 +{
28768 +       if (au_ftest_lock(flags, FLUSH))
28769 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28770 +       return si_noflush_read_trylock(sb);
28771 +}
28772 +#endif
28773 +
28774 +static inline void si_read_unlock(struct super_block *sb)
28775 +{
28776 +       si_pid_clr(sb);
28777 +       __si_read_unlock(sb);
28778 +}
28779 +
28780 +#if 0 /* reserved */
28781 +static inline int si_write_trylock(struct super_block *sb, int flags)
28782 +{
28783 +       if (au_ftest_lock(flags, FLUSH))
28784 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28785 +       return si_noflush_write_trylock(sb);
28786 +}
28787 +#endif
28788 +
28789 +static inline void si_write_unlock(struct super_block *sb)
28790 +{
28791 +       si_pid_clr(sb);
28792 +       __si_write_unlock(sb);
28793 +}
28794 +
28795 +#if 0 /* reserved */
28796 +static inline void si_downgrade_lock(struct super_block *sb)
28797 +{
28798 +       __si_downgrade_lock(sb);
28799 +}
28800 +#endif
28801 +
28802 +/* ---------------------------------------------------------------------- */
28803 +
28804 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
28805 +{
28806 +       SiMustAnyLock(sb);
28807 +       return au_sbi(sb)->si_bbot;
28808 +}
28809 +
28810 +static inline unsigned int au_mntflags(struct super_block *sb)
28811 +{
28812 +       SiMustAnyLock(sb);
28813 +       return au_sbi(sb)->si_mntflags;
28814 +}
28815 +
28816 +static inline unsigned int au_sigen(struct super_block *sb)
28817 +{
28818 +       SiMustAnyLock(sb);
28819 +       return au_sbi(sb)->si_generation;
28820 +}
28821 +
28822 +static inline unsigned long long au_ninodes(struct super_block *sb)
28823 +{
28824 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
28825 +
28826 +       BUG_ON(n < 0);
28827 +       return n;
28828 +}
28829 +
28830 +static inline void au_ninodes_inc(struct super_block *sb)
28831 +{
28832 +       percpu_counter_inc(&au_sbi(sb)->si_ninodes);
28833 +}
28834 +
28835 +static inline void au_ninodes_dec(struct super_block *sb)
28836 +{
28837 +       percpu_counter_dec(&au_sbi(sb)->si_ninodes);
28838 +}
28839 +
28840 +static inline unsigned long long au_nfiles(struct super_block *sb)
28841 +{
28842 +       s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
28843 +
28844 +       BUG_ON(n < 0);
28845 +       return n;
28846 +}
28847 +
28848 +static inline void au_nfiles_inc(struct super_block *sb)
28849 +{
28850 +       percpu_counter_inc(&au_sbi(sb)->si_nfiles);
28851 +}
28852 +
28853 +static inline void au_nfiles_dec(struct super_block *sb)
28854 +{
28855 +       percpu_counter_dec(&au_sbi(sb)->si_nfiles);
28856 +}
28857 +
28858 +static inline struct au_branch *au_sbr(struct super_block *sb,
28859 +                                      aufs_bindex_t bindex)
28860 +{
28861 +       SiMustAnyLock(sb);
28862 +       return au_sbi(sb)->si_branch[0 + bindex];
28863 +}
28864 +
28865 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28866 +{
28867 +       SiMustWriteLock(sb);
28868 +       au_sbi(sb)->si_xino_brid = brid;
28869 +}
28870 +
28871 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28872 +{
28873 +       SiMustAnyLock(sb);
28874 +       return au_sbi(sb)->si_xino_brid;
28875 +}
28876 +
28877 +#endif /* __KERNEL__ */
28878 +#endif /* __AUFS_SUPER_H__ */
28879 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28880 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
28881 +++ linux/fs/aufs/sysaufs.c     2017-03-02 14:24:13.866922487 +0100
28882 @@ -0,0 +1,104 @@
28883 +/*
28884 + * Copyright (C) 2005-2017 Junjiro R. Okajima
28885 + *
28886 + * This program, aufs is free software; you can redistribute it and/or modify
28887 + * it under the terms of the GNU General Public License as published by
28888 + * the Free Software Foundation; either version 2 of the License, or
28889 + * (at your option) any later version.
28890 + *
28891 + * This program is distributed in the hope that it will be useful,
28892 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28893 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28894 + * GNU General Public License for more details.
28895 + *
28896 + * You should have received a copy of the GNU General Public License
28897 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28898 + */
28899 +
28900 +/*
28901 + * sysfs interface and lifetime management
28902 + * they are necessary regardless sysfs is disabled.
28903 + */
28904 +
28905 +#include <linux/random.h>
28906 +#include "aufs.h"
28907 +
28908 +unsigned long sysaufs_si_mask;
28909 +struct kset *sysaufs_kset;
28910 +
28911 +#define AuSiAttr(_name) { \
28912 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
28913 +       .show   = sysaufs_si_##_name,                           \
28914 +}
28915 +
28916 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28917 +struct attribute *sysaufs_si_attrs[] = {
28918 +       &sysaufs_si_attr_xi_path.attr,
28919 +       NULL,
28920 +};
28921 +
28922 +static const struct sysfs_ops au_sbi_ops = {
28923 +       .show   = sysaufs_si_show
28924 +};
28925 +
28926 +static struct kobj_type au_sbi_ktype = {
28927 +       .release        = au_si_free,
28928 +       .sysfs_ops      = &au_sbi_ops,
28929 +       .default_attrs  = sysaufs_si_attrs
28930 +};
28931 +
28932 +/* ---------------------------------------------------------------------- */
28933 +
28934 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
28935 +{
28936 +       int err;
28937 +
28938 +       sbinfo->si_kobj.kset = sysaufs_kset;
28939 +       /* cf. sysaufs_name() */
28940 +       err = kobject_init_and_add
28941 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
28942 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28943 +
28944 +       dbgaufs_si_null(sbinfo);
28945 +       if (!err) {
28946 +               err = dbgaufs_si_init(sbinfo);
28947 +               if (unlikely(err))
28948 +                       kobject_put(&sbinfo->si_kobj);
28949 +       }
28950 +       return err;
28951 +}
28952 +
28953 +void sysaufs_fin(void)
28954 +{
28955 +       dbgaufs_fin();
28956 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28957 +       kset_unregister(sysaufs_kset);
28958 +}
28959 +
28960 +int __init sysaufs_init(void)
28961 +{
28962 +       int err;
28963 +
28964 +       do {
28965 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28966 +       } while (!sysaufs_si_mask);
28967 +
28968 +       err = -EINVAL;
28969 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28970 +       if (unlikely(!sysaufs_kset))
28971 +               goto out;
28972 +       err = PTR_ERR(sysaufs_kset);
28973 +       if (IS_ERR(sysaufs_kset))
28974 +               goto out;
28975 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28976 +       if (unlikely(err)) {
28977 +               kset_unregister(sysaufs_kset);
28978 +               goto out;
28979 +       }
28980 +
28981 +       err = dbgaufs_init();
28982 +       if (unlikely(err))
28983 +               sysaufs_fin();
28984 +out:
28985 +       return err;
28986 +}
28987 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28988 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
28989 +++ linux/fs/aufs/sysaufs.h     2017-03-02 14:24:13.866922487 +0100
28990 @@ -0,0 +1,101 @@
28991 +/*
28992 + * Copyright (C) 2005-2017 Junjiro R. Okajima
28993 + *
28994 + * This program, aufs is free software; you can redistribute it and/or modify
28995 + * it under the terms of the GNU General Public License as published by
28996 + * the Free Software Foundation; either version 2 of the License, or
28997 + * (at your option) any later version.
28998 + *
28999 + * This program is distributed in the hope that it will be useful,
29000 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29001 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29002 + * GNU General Public License for more details.
29003 + *
29004 + * You should have received a copy of the GNU General Public License
29005 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29006 + */
29007 +
29008 +/*
29009 + * sysfs interface and mount lifetime management
29010 + */
29011 +
29012 +#ifndef __SYSAUFS_H__
29013 +#define __SYSAUFS_H__
29014 +
29015 +#ifdef __KERNEL__
29016 +
29017 +#include <linux/sysfs.h>
29018 +#include "module.h"
29019 +
29020 +struct super_block;
29021 +struct au_sbinfo;
29022 +
29023 +struct sysaufs_si_attr {
29024 +       struct attribute attr;
29025 +       int (*show)(struct seq_file *seq, struct super_block *sb);
29026 +};
29027 +
29028 +/* ---------------------------------------------------------------------- */
29029 +
29030 +/* sysaufs.c */
29031 +extern unsigned long sysaufs_si_mask;
29032 +extern struct kset *sysaufs_kset;
29033 +extern struct attribute *sysaufs_si_attrs[];
29034 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
29035 +int __init sysaufs_init(void);
29036 +void sysaufs_fin(void);
29037 +
29038 +/* ---------------------------------------------------------------------- */
29039 +
29040 +/* some people doesn't like to show a pointer in kernel */
29041 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
29042 +{
29043 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
29044 +}
29045 +
29046 +#define SysaufsSiNamePrefix    "si_"
29047 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
29048 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
29049 +{
29050 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
29051 +                sysaufs_si_id(sbinfo));
29052 +}
29053 +
29054 +struct au_branch;
29055 +#ifdef CONFIG_SYSFS
29056 +/* sysfs.c */
29057 +extern struct attribute_group *sysaufs_attr_group;
29058 +
29059 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
29060 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
29061 +                        char *buf);
29062 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
29063 +#ifdef CONFIG_COMPAT
29064 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
29065 +#endif
29066 +
29067 +void sysaufs_br_init(struct au_branch *br);
29068 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
29069 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
29070 +
29071 +#define sysaufs_brs_init()     do {} while (0)
29072 +
29073 +#else
29074 +#define sysaufs_attr_group     NULL
29075 +
29076 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
29077 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
29078 +       struct attribute *attr, char *buf)
29079 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
29080 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
29081 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
29082 +
29083 +static inline void sysaufs_brs_init(void)
29084 +{
29085 +       sysaufs_brs = 0;
29086 +}
29087 +
29088 +#endif /* CONFIG_SYSFS */
29089 +
29090 +#endif /* __KERNEL__ */
29091 +#endif /* __SYSAUFS_H__ */
29092 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
29093 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
29094 +++ linux/fs/aufs/sysfs.c       2017-03-02 14:24:13.866922487 +0100
29095 @@ -0,0 +1,376 @@
29096 +/*
29097 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29098 + *
29099 + * This program, aufs is free software; you can redistribute it and/or modify
29100 + * it under the terms of the GNU General Public License as published by
29101 + * the Free Software Foundation; either version 2 of the License, or
29102 + * (at your option) any later version.
29103 + *
29104 + * This program is distributed in the hope that it will be useful,
29105 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29106 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29107 + * GNU General Public License for more details.
29108 + *
29109 + * You should have received a copy of the GNU General Public License
29110 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29111 + */
29112 +
29113 +/*
29114 + * sysfs interface
29115 + */
29116 +
29117 +#include <linux/compat.h>
29118 +#include <linux/seq_file.h>
29119 +#include "aufs.h"
29120 +
29121 +#ifdef CONFIG_AUFS_FS_MODULE
29122 +/* this entry violates the "one line per file" policy of sysfs */
29123 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
29124 +                          char *buf)
29125 +{
29126 +       ssize_t err;
29127 +       static char *conf =
29128 +/* this file is generated at compiling */
29129 +#include "conf.str"
29130 +               ;
29131 +
29132 +       err = snprintf(buf, PAGE_SIZE, conf);
29133 +       if (unlikely(err >= PAGE_SIZE))
29134 +               err = -EFBIG;
29135 +       return err;
29136 +}
29137 +
29138 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
29139 +#endif
29140 +
29141 +static struct attribute *au_attr[] = {
29142 +#ifdef CONFIG_AUFS_FS_MODULE
29143 +       &au_config_attr.attr,
29144 +#endif
29145 +       NULL,   /* need to NULL terminate the list of attributes */
29146 +};
29147 +
29148 +static struct attribute_group sysaufs_attr_group_body = {
29149 +       .attrs = au_attr
29150 +};
29151 +
29152 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
29153 +
29154 +/* ---------------------------------------------------------------------- */
29155 +
29156 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
29157 +{
29158 +       int err;
29159 +
29160 +       SiMustAnyLock(sb);
29161 +
29162 +       err = 0;
29163 +       if (au_opt_test(au_mntflags(sb), XINO)) {
29164 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
29165 +               seq_putc(seq, '\n');
29166 +       }
29167 +       return err;
29168 +}
29169 +
29170 +/*
29171 + * the lifetime of branch is independent from the entry under sysfs.
29172 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
29173 + * unlinked.
29174 + */
29175 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
29176 +                        aufs_bindex_t bindex, int idx)
29177 +{
29178 +       int err;
29179 +       struct path path;
29180 +       struct dentry *root;
29181 +       struct au_branch *br;
29182 +       au_br_perm_str_t perm;
29183 +
29184 +       AuDbg("b%d\n", bindex);
29185 +
29186 +       err = 0;
29187 +       root = sb->s_root;
29188 +       di_read_lock_parent(root, !AuLock_IR);
29189 +       br = au_sbr(sb, bindex);
29190 +
29191 +       switch (idx) {
29192 +       case AuBrSysfs_BR:
29193 +               path.mnt = au_br_mnt(br);
29194 +               path.dentry = au_h_dptr(root, bindex);
29195 +               err = au_seq_path(seq, &path);
29196 +               if (!err) {
29197 +                       au_optstr_br_perm(&perm, br->br_perm);
29198 +                       seq_printf(seq, "=%s\n", perm.a);
29199 +               }
29200 +               break;
29201 +       case AuBrSysfs_BRID:
29202 +               seq_printf(seq, "%d\n", br->br_id);
29203 +               break;
29204 +       }
29205 +       di_read_unlock(root, !AuLock_IR);
29206 +       if (unlikely(err || seq_has_overflowed(seq)))
29207 +               err = -E2BIG;
29208 +
29209 +       return err;
29210 +}
29211 +
29212 +/* ---------------------------------------------------------------------- */
29213 +
29214 +static struct seq_file *au_seq(char *p, ssize_t len)
29215 +{
29216 +       struct seq_file *seq;
29217 +
29218 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
29219 +       if (seq) {
29220 +               /* mutex_init(&seq.lock); */
29221 +               seq->buf = p;
29222 +               seq->size = len;
29223 +               return seq; /* success */
29224 +       }
29225 +
29226 +       seq = ERR_PTR(-ENOMEM);
29227 +       return seq;
29228 +}
29229 +
29230 +#define SysaufsBr_PREFIX       "br"
29231 +#define SysaufsBrid_PREFIX     "brid"
29232 +
29233 +/* todo: file size may exceed PAGE_SIZE */
29234 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
29235 +                       char *buf)
29236 +{
29237 +       ssize_t err;
29238 +       int idx;
29239 +       long l;
29240 +       aufs_bindex_t bbot;
29241 +       struct au_sbinfo *sbinfo;
29242 +       struct super_block *sb;
29243 +       struct seq_file *seq;
29244 +       char *name;
29245 +       struct attribute **cattr;
29246 +
29247 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29248 +       sb = sbinfo->si_sb;
29249 +
29250 +       /*
29251 +        * prevent a race condition between sysfs and aufs.
29252 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
29253 +        * prohibits maintaining the sysfs entries.
29254 +        * hew we acquire read lock after sysfs_get_active_two().
29255 +        * on the other hand, the remount process may maintain the sysfs/aufs
29256 +        * entries after acquiring write lock.
29257 +        * it can cause a deadlock.
29258 +        * simply we gave up processing read here.
29259 +        */
29260 +       err = -EBUSY;
29261 +       if (unlikely(!si_noflush_read_trylock(sb)))
29262 +               goto out;
29263 +
29264 +       seq = au_seq(buf, PAGE_SIZE);
29265 +       err = PTR_ERR(seq);
29266 +       if (IS_ERR(seq))
29267 +               goto out_unlock;
29268 +
29269 +       name = (void *)attr->name;
29270 +       cattr = sysaufs_si_attrs;
29271 +       while (*cattr) {
29272 +               if (!strcmp(name, (*cattr)->name)) {
29273 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
29274 +                               ->show(seq, sb);
29275 +                       goto out_seq;
29276 +               }
29277 +               cattr++;
29278 +       }
29279 +
29280 +       if (!strncmp(name, SysaufsBrid_PREFIX,
29281 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
29282 +               idx = AuBrSysfs_BRID;
29283 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
29284 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
29285 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
29286 +               idx = AuBrSysfs_BR;
29287 +               name += sizeof(SysaufsBr_PREFIX) - 1;
29288 +       } else
29289 +                 BUG();
29290 +
29291 +       err = kstrtol(name, 10, &l);
29292 +       if (!err) {
29293 +               bbot = au_sbbot(sb);
29294 +               if (l <= bbot)
29295 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
29296 +               else
29297 +                       err = -ENOENT;
29298 +       }
29299 +
29300 +out_seq:
29301 +       if (!err) {
29302 +               err = seq->count;
29303 +               /* sysfs limit */
29304 +               if (unlikely(err == PAGE_SIZE))
29305 +                       err = -EFBIG;
29306 +       }
29307 +       au_delayed_kfree(seq);
29308 +out_unlock:
29309 +       si_read_unlock(sb);
29310 +out:
29311 +       return err;
29312 +}
29313 +
29314 +/* ---------------------------------------------------------------------- */
29315 +
29316 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
29317 +{
29318 +       int err;
29319 +       int16_t brid;
29320 +       aufs_bindex_t bindex, bbot;
29321 +       size_t sz;
29322 +       char *buf;
29323 +       struct seq_file *seq;
29324 +       struct au_branch *br;
29325 +
29326 +       si_read_lock(sb, AuLock_FLUSH);
29327 +       bbot = au_sbbot(sb);
29328 +       err = bbot + 1;
29329 +       if (!arg)
29330 +               goto out;
29331 +
29332 +       err = -ENOMEM;
29333 +       buf = (void *)__get_free_page(GFP_NOFS);
29334 +       if (unlikely(!buf))
29335 +               goto out;
29336 +
29337 +       seq = au_seq(buf, PAGE_SIZE);
29338 +       err = PTR_ERR(seq);
29339 +       if (IS_ERR(seq))
29340 +               goto out_buf;
29341 +
29342 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
29343 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
29344 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
29345 +               if (unlikely(err))
29346 +                       break;
29347 +
29348 +               br = au_sbr(sb, bindex);
29349 +               brid = br->br_id;
29350 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
29351 +               err = __put_user(brid, &arg->id);
29352 +               if (unlikely(err))
29353 +                       break;
29354 +
29355 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
29356 +               err = __put_user(br->br_perm, &arg->perm);
29357 +               if (unlikely(err))
29358 +                       break;
29359 +
29360 +               err = au_seq_path(seq, &br->br_path);
29361 +               if (unlikely(err))
29362 +                       break;
29363 +               seq_putc(seq, '\0');
29364 +               if (!seq_has_overflowed(seq)) {
29365 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
29366 +                       seq->count = 0;
29367 +                       if (unlikely(err))
29368 +                               break;
29369 +               } else {
29370 +                       err = -E2BIG;
29371 +                       goto out_seq;
29372 +               }
29373 +       }
29374 +       if (unlikely(err))
29375 +               err = -EFAULT;
29376 +
29377 +out_seq:
29378 +       au_delayed_kfree(seq);
29379 +out_buf:
29380 +       au_delayed_free_page((unsigned long)buf);
29381 +out:
29382 +       si_read_unlock(sb);
29383 +       return err;
29384 +}
29385 +
29386 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
29387 +{
29388 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
29389 +}
29390 +
29391 +#ifdef CONFIG_COMPAT
29392 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
29393 +{
29394 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
29395 +}
29396 +#endif
29397 +
29398 +/* ---------------------------------------------------------------------- */
29399 +
29400 +void sysaufs_br_init(struct au_branch *br)
29401 +{
29402 +       int i;
29403 +       struct au_brsysfs *br_sysfs;
29404 +       struct attribute *attr;
29405 +
29406 +       br_sysfs = br->br_sysfs;
29407 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29408 +               attr = &br_sysfs->attr;
29409 +               sysfs_attr_init(attr);
29410 +               attr->name = br_sysfs->name;
29411 +               attr->mode = S_IRUGO;
29412 +               br_sysfs++;
29413 +       }
29414 +}
29415 +
29416 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
29417 +{
29418 +       struct au_branch *br;
29419 +       struct kobject *kobj;
29420 +       struct au_brsysfs *br_sysfs;
29421 +       int i;
29422 +       aufs_bindex_t bbot;
29423 +
29424 +       dbgaufs_brs_del(sb, bindex);
29425 +
29426 +       if (!sysaufs_brs)
29427 +               return;
29428 +
29429 +       kobj = &au_sbi(sb)->si_kobj;
29430 +       bbot = au_sbbot(sb);
29431 +       for (; bindex <= bbot; bindex++) {
29432 +               br = au_sbr(sb, bindex);
29433 +               br_sysfs = br->br_sysfs;
29434 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29435 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
29436 +                       br_sysfs++;
29437 +               }
29438 +       }
29439 +}
29440 +
29441 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
29442 +{
29443 +       int err, i;
29444 +       aufs_bindex_t bbot;
29445 +       struct kobject *kobj;
29446 +       struct au_branch *br;
29447 +       struct au_brsysfs *br_sysfs;
29448 +
29449 +       dbgaufs_brs_add(sb, bindex);
29450 +
29451 +       if (!sysaufs_brs)
29452 +               return;
29453 +
29454 +       kobj = &au_sbi(sb)->si_kobj;
29455 +       bbot = au_sbbot(sb);
29456 +       for (; bindex <= bbot; bindex++) {
29457 +               br = au_sbr(sb, bindex);
29458 +               br_sysfs = br->br_sysfs;
29459 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
29460 +                        SysaufsBr_PREFIX "%d", bindex);
29461 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
29462 +                        SysaufsBrid_PREFIX "%d", bindex);
29463 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29464 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
29465 +                       if (unlikely(err))
29466 +                               pr_warn("failed %s under sysfs(%d)\n",
29467 +                                       br_sysfs->name, err);
29468 +                       br_sysfs++;
29469 +               }
29470 +       }
29471 +}
29472 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
29473 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
29474 +++ linux/fs/aufs/sysrq.c       2017-03-02 14:24:13.866922487 +0100
29475 @@ -0,0 +1,157 @@
29476 +/*
29477 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29478 + *
29479 + * This program, aufs is free software; you can redistribute it and/or modify
29480 + * it under the terms of the GNU General Public License as published by
29481 + * the Free Software Foundation; either version 2 of the License, or
29482 + * (at your option) any later version.
29483 + *
29484 + * This program is distributed in the hope that it will be useful,
29485 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29486 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29487 + * GNU General Public License for more details.
29488 + *
29489 + * You should have received a copy of the GNU General Public License
29490 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29491 + */
29492 +
29493 +/*
29494 + * magic sysrq hanlder
29495 + */
29496 +
29497 +/* #include <linux/sysrq.h> */
29498 +#include <linux/writeback.h>
29499 +#include "aufs.h"
29500 +
29501 +/* ---------------------------------------------------------------------- */
29502 +
29503 +static void sysrq_sb(struct super_block *sb)
29504 +{
29505 +       char *plevel;
29506 +       struct au_sbinfo *sbinfo;
29507 +       struct file *file;
29508 +       struct au_sphlhead *files;
29509 +       struct au_finfo *finfo;
29510 +
29511 +       plevel = au_plevel;
29512 +       au_plevel = KERN_WARNING;
29513 +
29514 +       /* since we define pr_fmt, call printk directly */
29515 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
29516 +
29517 +       sbinfo = au_sbi(sb);
29518 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
29519 +       pr("superblock\n");
29520 +       au_dpri_sb(sb);
29521 +
29522 +#if 0
29523 +       pr("root dentry\n");
29524 +       au_dpri_dentry(sb->s_root);
29525 +       pr("root inode\n");
29526 +       au_dpri_inode(d_inode(sb->s_root));
29527 +#endif
29528 +
29529 +#if 0
29530 +       do {
29531 +               int err, i, j, ndentry;
29532 +               struct au_dcsub_pages dpages;
29533 +               struct au_dpage *dpage;
29534 +
29535 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
29536 +               if (unlikely(err))
29537 +                       break;
29538 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
29539 +               if (!err)
29540 +                       for (i = 0; i < dpages.ndpage; i++) {
29541 +                               dpage = dpages.dpages + i;
29542 +                               ndentry = dpage->ndentry;
29543 +                               for (j = 0; j < ndentry; j++)
29544 +                                       au_dpri_dentry(dpage->dentries[j]);
29545 +                       }
29546 +               au_dpages_free(&dpages);
29547 +       } while (0);
29548 +#endif
29549 +
29550 +#if 1
29551 +       {
29552 +               struct inode *i;
29553 +
29554 +               pr("isolated inode\n");
29555 +               spin_lock(&sb->s_inode_list_lock);
29556 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
29557 +                       spin_lock(&i->i_lock);
29558 +                       if (1 || hlist_empty(&i->i_dentry))
29559 +                               au_dpri_inode(i);
29560 +                       spin_unlock(&i->i_lock);
29561 +               }
29562 +               spin_unlock(&sb->s_inode_list_lock);
29563 +       }
29564 +#endif
29565 +       pr("files\n");
29566 +       files = &au_sbi(sb)->si_files;
29567 +       spin_lock(&files->spin);
29568 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
29569 +               umode_t mode;
29570 +
29571 +               file = finfo->fi_file;
29572 +               mode = file_inode(file)->i_mode;
29573 +               if (!special_file(mode))
29574 +                       au_dpri_file(file);
29575 +       }
29576 +       spin_unlock(&files->spin);
29577 +       pr("done\n");
29578 +
29579 +#undef pr
29580 +       au_plevel = plevel;
29581 +}
29582 +
29583 +/* ---------------------------------------------------------------------- */
29584 +
29585 +/* module parameter */
29586 +static char *aufs_sysrq_key = "a";
29587 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
29588 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
29589 +
29590 +static void au_sysrq(int key __maybe_unused)
29591 +{
29592 +       struct au_sbinfo *sbinfo;
29593 +
29594 +       lockdep_off();
29595 +       au_sbilist_lock();
29596 +       hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
29597 +               sysrq_sb(sbinfo->si_sb);
29598 +       au_sbilist_unlock();
29599 +       lockdep_on();
29600 +}
29601 +
29602 +static struct sysrq_key_op au_sysrq_op = {
29603 +       .handler        = au_sysrq,
29604 +       .help_msg       = "Aufs",
29605 +       .action_msg     = "Aufs",
29606 +       .enable_mask    = SYSRQ_ENABLE_DUMP
29607 +};
29608 +
29609 +/* ---------------------------------------------------------------------- */
29610 +
29611 +int __init au_sysrq_init(void)
29612 +{
29613 +       int err;
29614 +       char key;
29615 +
29616 +       err = -1;
29617 +       key = *aufs_sysrq_key;
29618 +       if ('a' <= key && key <= 'z')
29619 +               err = register_sysrq_key(key, &au_sysrq_op);
29620 +       if (unlikely(err))
29621 +               pr_err("err %d, sysrq=%c\n", err, key);
29622 +       return err;
29623 +}
29624 +
29625 +void au_sysrq_fin(void)
29626 +{
29627 +       int err;
29628 +
29629 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
29630 +       if (unlikely(err))
29631 +               pr_err("err %d (ignored)\n", err);
29632 +}
29633 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
29634 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
29635 +++ linux/fs/aufs/vdir.c        2017-03-02 14:24:13.866922487 +0100
29636 @@ -0,0 +1,900 @@
29637 +/*
29638 + * Copyright (C) 2005-2017 Junjiro R. Okajima
29639 + *
29640 + * This program, aufs is free software; you can redistribute it and/or modify
29641 + * it under the terms of the GNU General Public License as published by
29642 + * the Free Software Foundation; either version 2 of the License, or
29643 + * (at your option) any later version.
29644 + *
29645 + * This program is distributed in the hope that it will be useful,
29646 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29647 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29648 + * GNU General Public License for more details.
29649 + *
29650 + * You should have received a copy of the GNU General Public License
29651 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29652 + */
29653 +
29654 +/*
29655 + * virtual or vertical directory
29656 + */
29657 +
29658 +#include "aufs.h"
29659 +
29660 +static unsigned int calc_size(int nlen)
29661 +{
29662 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
29663 +}
29664 +
29665 +static int set_deblk_end(union au_vdir_deblk_p *p,
29666 +                        union au_vdir_deblk_p *deblk_end)
29667 +{
29668 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29669 +               p->de->de_str.len = 0;
29670 +               /* smp_mb(); */
29671 +               return 0;
29672 +       }
29673 +       return -1; /* error */
29674 +}
29675 +
29676 +/* returns true or false */
29677 +static int is_deblk_end(union au_vdir_deblk_p *p,
29678 +                       union au_vdir_deblk_p *deblk_end)
29679 +{
29680 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
29681 +               return !p->de->de_str.len;
29682 +       return 1;
29683 +}
29684 +
29685 +static unsigned char *last_deblk(struct au_vdir *vdir)
29686 +{
29687 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
29688 +}
29689 +
29690 +/* ---------------------------------------------------------------------- */
29691 +
29692 +/* estimate the appropriate size for name hash table */
29693 +unsigned int au_rdhash_est(loff_t sz)
29694 +{
29695 +       unsigned int n;
29696 +
29697 +       n = UINT_MAX;
29698 +       sz >>= 10;
29699 +       if (sz < n)
29700 +               n = sz;
29701 +       if (sz < AUFS_RDHASH_DEF)
29702 +               n = AUFS_RDHASH_DEF;
29703 +       /* pr_info("n %u\n", n); */
29704 +       return n;
29705 +}
29706 +
29707 +/*
29708 + * the allocated memory has to be freed by
29709 + * au_nhash_wh_free() or au_nhash_de_free().
29710 + */
29711 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
29712 +{
29713 +       struct hlist_head *head;
29714 +       unsigned int u;
29715 +       size_t sz;
29716 +
29717 +       sz = sizeof(*nhash->nh_head) * num_hash;
29718 +       head = kmalloc(sz, gfp);
29719 +       if (head) {
29720 +               nhash->nh_num = num_hash;
29721 +               nhash->nh_head = head;
29722 +               for (u = 0; u < num_hash; u++)
29723 +                       INIT_HLIST_HEAD(head++);
29724 +               return 0; /* success */
29725 +       }
29726 +
29727 +       return -ENOMEM;
29728 +}
29729 +
29730 +static void nhash_count(struct hlist_head *head)
29731 +{
29732 +#if 0
29733 +       unsigned long n;
29734 +       struct hlist_node *pos;
29735 +
29736 +       n = 0;
29737 +       hlist_for_each(pos, head)
29738 +               n++;
29739 +       pr_info("%lu\n", n);
29740 +#endif
29741 +}
29742 +
29743 +static void au_nhash_wh_do_free(struct hlist_head *head)
29744 +{
29745 +       struct au_vdir_wh *pos;
29746 +       struct hlist_node *node;
29747 +
29748 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
29749 +               au_delayed_kfree(pos);
29750 +}
29751 +
29752 +static void au_nhash_de_do_free(struct hlist_head *head)
29753 +{
29754 +       struct au_vdir_dehstr *pos;
29755 +       struct hlist_node *node;
29756 +
29757 +       hlist_for_each_entry_safe(pos, node, head, hash)
29758 +               au_cache_dfree_vdir_dehstr(pos);
29759 +}
29760 +
29761 +static void au_nhash_do_free(struct au_nhash *nhash,
29762 +                            void (*free)(struct hlist_head *head))
29763 +{
29764 +       unsigned int n;
29765 +       struct hlist_head *head;
29766 +
29767 +       n = nhash->nh_num;
29768 +       if (!n)
29769 +               return;
29770 +
29771 +       head = nhash->nh_head;
29772 +       while (n-- > 0) {
29773 +               nhash_count(head);
29774 +               free(head++);
29775 +       }
29776 +       au_delayed_kfree(nhash->nh_head);
29777 +}
29778 +
29779 +void au_nhash_wh_free(struct au_nhash *whlist)
29780 +{
29781 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
29782 +}
29783 +
29784 +static void au_nhash_de_free(struct au_nhash *delist)
29785 +{
29786 +       au_nhash_do_free(delist, au_nhash_de_do_free);
29787 +}
29788 +
29789 +/* ---------------------------------------------------------------------- */
29790 +
29791 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29792 +                           int limit)
29793 +{
29794 +       int num;
29795 +       unsigned int u, n;
29796 +       struct hlist_head *head;
29797 +       struct au_vdir_wh *pos;
29798 +
29799 +       num = 0;
29800 +       n = whlist->nh_num;
29801 +       head = whlist->nh_head;
29802 +       for (u = 0; u < n; u++, head++)
29803 +               hlist_for_each_entry(pos, head, wh_hash)
29804 +                       if (pos->wh_bindex == btgt && ++num > limit)
29805 +                               return 1;
29806 +       return 0;
29807 +}
29808 +
29809 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
29810 +                                      unsigned char *name,
29811 +                                      unsigned int len)
29812 +{
29813 +       unsigned int v;
29814 +       /* const unsigned int magic_bit = 12; */
29815 +
29816 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29817 +
29818 +       v = 0;
29819 +       if (len > 8)
29820 +               len = 8;
29821 +       while (len--)
29822 +               v += *name++;
29823 +       /* v = hash_long(v, magic_bit); */
29824 +       v %= nhash->nh_num;
29825 +       return nhash->nh_head + v;
29826 +}
29827 +
29828 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29829 +                             int nlen)
29830 +{
29831 +       return str->len == nlen && !memcmp(str->name, name, nlen);
29832 +}
29833 +
29834 +/* returns found or not */
29835 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
29836 +{
29837 +       struct hlist_head *head;
29838 +       struct au_vdir_wh *pos;
29839 +       struct au_vdir_destr *str;
29840 +
29841 +       head = au_name_hash(whlist, name, nlen);
29842 +       hlist_for_each_entry(pos, head, wh_hash) {
29843 +               str = &pos->wh_str;
29844 +               AuDbg("%.*s\n", str->len, str->name);
29845 +               if (au_nhash_test_name(str, name, nlen))
29846 +                       return 1;
29847 +       }
29848 +       return 0;
29849 +}
29850 +
29851 +/* returns found(true) or not */
29852 +static int test_known(struct au_nhash *delist, char *name, int nlen)
29853 +{
29854 +       struct hlist_head *head;
29855 +       struct au_vdir_dehstr *pos;
29856 +       struct au_vdir_destr *str;
29857 +
29858 +       head = au_name_hash(delist, name, nlen);
29859 +       hlist_for_each_entry(pos, head, hash) {
29860 +               str = pos->str;
29861 +               AuDbg("%.*s\n", str->len, str->name);
29862 +               if (au_nhash_test_name(str, name, nlen))
29863 +                       return 1;
29864 +       }
29865 +       return 0;
29866 +}
29867 +
29868 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29869 +                           unsigned char d_type)
29870 +{
29871 +#ifdef CONFIG_AUFS_SHWH
29872 +       wh->wh_ino = ino;
29873 +       wh->wh_type = d_type;
29874 +#endif
29875 +}
29876 +
29877 +/* ---------------------------------------------------------------------- */
29878 +
29879 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29880 +                      unsigned int d_type, aufs_bindex_t bindex,
29881 +                      unsigned char shwh)
29882 +{
29883 +       int err;
29884 +       struct au_vdir_destr *str;
29885 +       struct au_vdir_wh *wh;
29886 +
29887 +       AuDbg("%.*s\n", nlen, name);
29888 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29889 +
29890 +       err = -ENOMEM;
29891 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
29892 +       if (unlikely(!wh))
29893 +               goto out;
29894 +
29895 +       err = 0;
29896 +       wh->wh_bindex = bindex;
29897 +       if (shwh)
29898 +               au_shwh_init_wh(wh, ino, d_type);
29899 +       str = &wh->wh_str;
29900 +       str->len = nlen;
29901 +       memcpy(str->name, name, nlen);
29902 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
29903 +       /* smp_mb(); */
29904 +
29905 +out:
29906 +       return err;
29907 +}
29908 +
29909 +static int append_deblk(struct au_vdir *vdir)
29910 +{
29911 +       int err;
29912 +       unsigned long ul;
29913 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29914 +       union au_vdir_deblk_p p, deblk_end;
29915 +       unsigned char **o;
29916 +
29917 +       err = -ENOMEM;
29918 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29919 +                       GFP_NOFS, /*may_shrink*/0);
29920 +       if (unlikely(!o))
29921 +               goto out;
29922 +
29923 +       vdir->vd_deblk = o;
29924 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29925 +       if (p.deblk) {
29926 +               ul = vdir->vd_nblk++;
29927 +               vdir->vd_deblk[ul] = p.deblk;
29928 +               vdir->vd_last.ul = ul;
29929 +               vdir->vd_last.p.deblk = p.deblk;
29930 +               deblk_end.deblk = p.deblk + deblk_sz;
29931 +               err = set_deblk_end(&p, &deblk_end);
29932 +       }
29933 +
29934 +out:
29935 +       return err;
29936 +}
29937 +
29938 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29939 +                    unsigned int d_type, struct au_nhash *delist)
29940 +{
29941 +       int err;
29942 +       unsigned int sz;
29943 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29944 +       union au_vdir_deblk_p p, *room, deblk_end;
29945 +       struct au_vdir_dehstr *dehstr;
29946 +
29947 +       p.deblk = last_deblk(vdir);
29948 +       deblk_end.deblk = p.deblk + deblk_sz;
29949 +       room = &vdir->vd_last.p;
29950 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29951 +                 || !is_deblk_end(room, &deblk_end));
29952 +
29953 +       sz = calc_size(nlen);
29954 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29955 +               err = append_deblk(vdir);
29956 +               if (unlikely(err))
29957 +                       goto out;
29958 +
29959 +               p.deblk = last_deblk(vdir);
29960 +               deblk_end.deblk = p.deblk + deblk_sz;
29961 +               /* smp_mb(); */
29962 +               AuDebugOn(room->deblk != p.deblk);
29963 +       }
29964 +
29965 +       err = -ENOMEM;
29966 +       dehstr = au_cache_alloc_vdir_dehstr();
29967 +       if (unlikely(!dehstr))
29968 +               goto out;
29969 +
29970 +       dehstr->str = &room->de->de_str;
29971 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29972 +       room->de->de_ino = ino;
29973 +       room->de->de_type = d_type;
29974 +       room->de->de_str.len = nlen;
29975 +       memcpy(room->de->de_str.name, name, nlen);
29976 +
29977 +       err = 0;
29978 +       room->deblk += sz;
29979 +       if (unlikely(set_deblk_end(room, &deblk_end)))
29980 +               err = append_deblk(vdir);
29981 +       /* smp_mb(); */
29982 +
29983 +out:
29984 +       return err;
29985 +}
29986 +
29987 +/* ---------------------------------------------------------------------- */
29988 +
29989 +void au_vdir_free(struct au_vdir *vdir, int atonce)
29990 +{
29991 +       unsigned char **deblk;
29992 +
29993 +       deblk = vdir->vd_deblk;
29994 +       if (!atonce) {
29995 +               while (vdir->vd_nblk--)
29996 +                       au_delayed_kfree(*deblk++);
29997 +               au_delayed_kfree(vdir->vd_deblk);
29998 +               au_cache_dfree_vdir(vdir);
29999 +       } else {
30000 +               /* not delayed */
30001 +               while (vdir->vd_nblk--)
30002 +                       kfree(*deblk++);
30003 +               kfree(vdir->vd_deblk);
30004 +               au_cache_free_vdir(vdir);
30005 +       }
30006 +}
30007 +
30008 +static struct au_vdir *alloc_vdir(struct file *file)
30009 +{
30010 +       struct au_vdir *vdir;
30011 +       struct super_block *sb;
30012 +       int err;
30013 +
30014 +       sb = file->f_path.dentry->d_sb;
30015 +       SiMustAnyLock(sb);
30016 +
30017 +       err = -ENOMEM;
30018 +       vdir = au_cache_alloc_vdir();
30019 +       if (unlikely(!vdir))
30020 +               goto out;
30021 +
30022 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
30023 +       if (unlikely(!vdir->vd_deblk))
30024 +               goto out_free;
30025 +
30026 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
30027 +       if (!vdir->vd_deblk_sz) {
30028 +               /* estimate the appropriate size for deblk */
30029 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
30030 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
30031 +       }
30032 +       vdir->vd_nblk = 0;
30033 +       vdir->vd_version = 0;
30034 +       vdir->vd_jiffy = 0;
30035 +       err = append_deblk(vdir);
30036 +       if (!err)
30037 +               return vdir; /* success */
30038 +
30039 +       au_delayed_kfree(vdir->vd_deblk);
30040 +
30041 +out_free:
30042 +       au_cache_dfree_vdir(vdir);
30043 +out:
30044 +       vdir = ERR_PTR(err);
30045 +       return vdir;
30046 +}
30047 +
30048 +static int reinit_vdir(struct au_vdir *vdir)
30049 +{
30050 +       int err;
30051 +       union au_vdir_deblk_p p, deblk_end;
30052 +
30053 +       while (vdir->vd_nblk > 1) {
30054 +               au_delayed_kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
30055 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
30056 +               vdir->vd_nblk--;
30057 +       }
30058 +       p.deblk = vdir->vd_deblk[0];
30059 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
30060 +       err = set_deblk_end(&p, &deblk_end);
30061 +       /* keep vd_dblk_sz */
30062 +       vdir->vd_last.ul = 0;
30063 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
30064 +       vdir->vd_version = 0;
30065 +       vdir->vd_jiffy = 0;
30066 +       /* smp_mb(); */
30067 +       return err;
30068 +}
30069 +
30070 +/* ---------------------------------------------------------------------- */
30071 +
30072 +#define AuFillVdir_CALLED      1
30073 +#define AuFillVdir_WHABLE      (1 << 1)
30074 +#define AuFillVdir_SHWH                (1 << 2)
30075 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
30076 +#define au_fset_fillvdir(flags, name) \
30077 +       do { (flags) |= AuFillVdir_##name; } while (0)
30078 +#define au_fclr_fillvdir(flags, name) \
30079 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
30080 +
30081 +#ifndef CONFIG_AUFS_SHWH
30082 +#undef AuFillVdir_SHWH
30083 +#define AuFillVdir_SHWH                0
30084 +#endif
30085 +
30086 +struct fillvdir_arg {
30087 +       struct dir_context      ctx;
30088 +       struct file             *file;
30089 +       struct au_vdir          *vdir;
30090 +       struct au_nhash         delist;
30091 +       struct au_nhash         whlist;
30092 +       aufs_bindex_t           bindex;
30093 +       unsigned int            flags;
30094 +       int                     err;
30095 +};
30096 +
30097 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
30098 +                   loff_t offset __maybe_unused, u64 h_ino,
30099 +                   unsigned int d_type)
30100 +{
30101 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
30102 +       char *name = (void *)__name;
30103 +       struct super_block *sb;
30104 +       ino_t ino;
30105 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
30106 +
30107 +       arg->err = 0;
30108 +       sb = arg->file->f_path.dentry->d_sb;
30109 +       au_fset_fillvdir(arg->flags, CALLED);
30110 +       /* smp_mb(); */
30111 +       if (nlen <= AUFS_WH_PFX_LEN
30112 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
30113 +               if (test_known(&arg->delist, name, nlen)
30114 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
30115 +                       goto out; /* already exists or whiteouted */
30116 +
30117 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
30118 +               if (!arg->err) {
30119 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
30120 +                               d_type = DT_UNKNOWN;
30121 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
30122 +                                            d_type, &arg->delist);
30123 +               }
30124 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
30125 +               name += AUFS_WH_PFX_LEN;
30126 +               nlen -= AUFS_WH_PFX_LEN;
30127 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
30128 +                       goto out; /* already whiteouted */
30129 +
30130 +               if (shwh)
30131 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
30132 +                                            &ino);
30133 +               if (!arg->err) {
30134 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
30135 +                               d_type = DT_UNKNOWN;
30136 +                       arg->err = au_nhash_append_wh
30137 +                               (&arg->whlist, name, nlen, ino, d_type,
30138 +                                arg->bindex, shwh);
30139 +               }
30140 +       }
30141 +
30142 +out:
30143 +       if (!arg->err)
30144 +               arg->vdir->vd_jiffy = jiffies;
30145 +       /* smp_mb(); */
30146 +       AuTraceErr(arg->err);
30147 +       return arg->err;
30148 +}
30149 +
30150 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
30151 +                         struct au_nhash *whlist, struct au_nhash *delist)
30152 +{
30153 +#ifdef CONFIG_AUFS_SHWH
30154 +       int err;
30155 +       unsigned int nh, u;
30156 +       struct hlist_head *head;
30157 +       struct au_vdir_wh *pos;
30158 +       struct hlist_node *n;
30159 +       char *p, *o;
30160 +       struct au_vdir_destr *destr;
30161 +
30162 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
30163 +
30164 +       err = -ENOMEM;
30165 +       o = p = (void *)__get_free_page(GFP_NOFS);
30166 +       if (unlikely(!p))
30167 +               goto out;
30168 +
30169 +       err = 0;
30170 +       nh = whlist->nh_num;
30171 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
30172 +       p += AUFS_WH_PFX_LEN;
30173 +       for (u = 0; u < nh; u++) {
30174 +               head = whlist->nh_head + u;
30175 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
30176 +                       destr = &pos->wh_str;
30177 +                       memcpy(p, destr->name, destr->len);
30178 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
30179 +                                       pos->wh_ino, pos->wh_type, delist);
30180 +                       if (unlikely(err))
30181 +                               break;
30182 +               }
30183 +       }
30184 +
30185 +       au_delayed_free_page((unsigned long)o);
30186 +
30187 +out:
30188 +       AuTraceErr(err);
30189 +       return err;
30190 +#else
30191 +       return 0;
30192 +#endif
30193 +}
30194 +
30195 +static int au_do_read_vdir(struct fillvdir_arg *arg)
30196 +{
30197 +       int err;
30198 +       unsigned int rdhash;
30199 +       loff_t offset;
30200 +       aufs_bindex_t bbot, bindex, btop;
30201 +       unsigned char shwh;
30202 +       struct file *hf, *file;
30203 +       struct super_block *sb;
30204 +
30205 +       file = arg->file;
30206 +       sb = file->f_path.dentry->d_sb;
30207 +       SiMustAnyLock(sb);
30208 +
30209 +       rdhash = au_sbi(sb)->si_rdhash;
30210 +       if (!rdhash)
30211 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
30212 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
30213 +       if (unlikely(err))
30214 +               goto out;
30215 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
30216 +       if (unlikely(err))
30217 +               goto out_delist;
30218 +
30219 +       err = 0;
30220 +       arg->flags = 0;
30221 +       shwh = 0;
30222 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
30223 +               shwh = 1;
30224 +               au_fset_fillvdir(arg->flags, SHWH);
30225 +       }
30226 +       btop = au_fbtop(file);
30227 +       bbot = au_fbbot_dir(file);
30228 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
30229 +               hf = au_hf_dir(file, bindex);
30230 +               if (!hf)
30231 +                       continue;
30232 +
30233 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
30234 +               err = offset;
30235 +               if (unlikely(offset))
30236 +                       break;
30237 +
30238 +               arg->bindex = bindex;
30239 +               au_fclr_fillvdir(arg->flags, WHABLE);
30240 +               if (shwh
30241 +                   || (bindex != bbot
30242 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
30243 +                       au_fset_fillvdir(arg->flags, WHABLE);
30244 +               do {
30245 +                       arg->err = 0;
30246 +                       au_fclr_fillvdir(arg->flags, CALLED);
30247 +                       /* smp_mb(); */
30248 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
30249 +                       if (err >= 0)
30250 +                               err = arg->err;
30251 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
30252 +
30253 +               /*
30254 +                * dir_relax() may be good for concurrency, but aufs should not
30255 +                * use it since it will cause a lockdep problem.
30256 +                */
30257 +       }
30258 +
30259 +       if (!err && shwh)
30260 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
30261 +
30262 +       au_nhash_wh_free(&arg->whlist);
30263 +
30264 +out_delist:
30265 +       au_nhash_de_free(&arg->delist);
30266 +out:
30267 +       return err;
30268 +}
30269 +
30270 +static int read_vdir(struct file *file, int may_read)
30271 +{
30272 +       int err;
30273 +       unsigned long expire;
30274 +       unsigned char do_read;
30275 +       struct fillvdir_arg arg = {
30276 +               .ctx = {
30277 +                       .actor = fillvdir
30278 +               }
30279 +       };
30280 +       struct inode *inode;
30281 +       struct au_vdir *vdir, *allocated;
30282 +
30283 +       err = 0;
30284 +       inode = file_inode(file);
30285 +       IMustLock(inode);
30286 +       IiMustWriteLock(inode);
30287 +       SiMustAnyLock(inode->i_sb);
30288 +
30289 +       allocated = NULL;
30290 +       do_read = 0;
30291 +       expire = au_sbi(inode->i_sb)->si_rdcache;
30292 +       vdir = au_ivdir(inode);
30293 +       if (!vdir) {
30294 +               do_read = 1;
30295 +               vdir = alloc_vdir(file);
30296 +               err = PTR_ERR(vdir);
30297 +               if (IS_ERR(vdir))
30298 +                       goto out;
30299 +               err = 0;
30300 +               allocated = vdir;
30301 +       } else if (may_read
30302 +                  && (inode->i_version != vdir->vd_version
30303 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
30304 +               do_read = 1;
30305 +               err = reinit_vdir(vdir);
30306 +               if (unlikely(err))
30307 +                       goto out;
30308 +       }
30309 +
30310 +       if (!do_read)
30311 +               return 0; /* success */
30312 +
30313 +       arg.file = file;
30314 +       arg.vdir = vdir;
30315 +       err = au_do_read_vdir(&arg);
30316 +       if (!err) {
30317 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
30318 +               vdir->vd_version = inode->i_version;
30319 +               vdir->vd_last.ul = 0;
30320 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
30321 +               if (allocated)
30322 +                       au_set_ivdir(inode, allocated);
30323 +       } else if (allocated)
30324 +               au_vdir_free(allocated, /*atonce*/0);
30325 +
30326 +out:
30327 +       return err;
30328 +}
30329 +
30330 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
30331 +{
30332 +       int err, rerr;
30333 +       unsigned long ul, n;
30334 +       const unsigned int deblk_sz = src->vd_deblk_sz;
30335 +
30336 +       AuDebugOn(tgt->vd_nblk != 1);
30337 +
30338 +       err = -ENOMEM;
30339 +       if (tgt->vd_nblk < src->vd_nblk) {
30340 +               unsigned char **p;
30341 +
30342 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
30343 +                               GFP_NOFS, /*may_shrink*/0);
30344 +               if (unlikely(!p))
30345 +                       goto out;
30346 +               tgt->vd_deblk = p;
30347 +       }
30348 +
30349 +       if (tgt->vd_deblk_sz != deblk_sz) {
30350 +               unsigned char *p;
30351 +
30352 +               tgt->vd_deblk_sz = deblk_sz;
30353 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
30354 +                               /*may_shrink*/1);
30355 +               if (unlikely(!p))
30356 +                       goto out;
30357 +               tgt->vd_deblk[0] = p;
30358 +       }
30359 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
30360 +       tgt->vd_version = src->vd_version;
30361 +       tgt->vd_jiffy = src->vd_jiffy;
30362 +
30363 +       n = src->vd_nblk;
30364 +       for (ul = 1; ul < n; ul++) {
30365 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
30366 +                                           GFP_NOFS);
30367 +               if (unlikely(!tgt->vd_deblk[ul]))
30368 +                       goto out;
30369 +               tgt->vd_nblk++;
30370 +       }
30371 +       tgt->vd_nblk = n;
30372 +       tgt->vd_last.ul = tgt->vd_last.ul;
30373 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
30374 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
30375 +               - src->vd_deblk[src->vd_last.ul];
30376 +       /* smp_mb(); */
30377 +       return 0; /* success */
30378 +
30379 +out:
30380 +       rerr = reinit_vdir(tgt);
30381 +       BUG_ON(rerr);
30382 +       return err;
30383 +}
30384 +
30385 +int au_vdir_init(struct file *file)
30386 +{
30387 +       int err;
30388 +       struct inode *inode;
30389 +       struct au_vdir *vdir_cache, *allocated;
30390 +
30391 +       /* test file->f_pos here instead of ctx->pos */
30392 +       err = read_vdir(file, !file->f_pos);
30393 +       if (unlikely(err))
30394 +               goto out;
30395 +
30396 +       allocated = NULL;
30397 +       vdir_cache = au_fvdir_cache(file);
30398 +       if (!vdir_cache) {
30399 +               vdir_cache = alloc_vdir(file);
30400 +               err = PTR_ERR(vdir_cache);
30401 +               if (IS_ERR(vdir_cache))
30402 +                       goto out;
30403 +               allocated = vdir_cache;
30404 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
30405 +               /* test file->f_pos here instead of ctx->pos */
30406 +               err = reinit_vdir(vdir_cache);
30407 +               if (unlikely(err))
30408 +                       goto out;
30409 +       } else
30410 +               return 0; /* success */
30411 +
30412 +       inode = file_inode(file);
30413 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
30414 +       if (!err) {
30415 +               file->f_version = inode->i_version;
30416 +               if (allocated)
30417 +                       au_set_fvdir_cache(file, allocated);
30418 +       } else if (allocated)
30419 +               au_vdir_free(allocated, /*atonce*/0);
30420 +
30421 +out:
30422 +       return err;
30423 +}
30424 +
30425 +static loff_t calc_offset(struct au_vdir *vdir)
30426 +{
30427 +       loff_t offset;
30428 +       union au_vdir_deblk_p p;
30429 +
30430 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
30431 +       offset = vdir->vd_last.p.deblk - p.deblk;
30432 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
30433 +       return offset;
30434 +}
30435 +
30436 +/* returns true or false */
30437 +static int seek_vdir(struct file *file, struct dir_context *ctx)
30438 +{
30439 +       int valid;
30440 +       unsigned int deblk_sz;
30441 +       unsigned long ul, n;
30442 +       loff_t offset;
30443 +       union au_vdir_deblk_p p, deblk_end;
30444 +       struct au_vdir *vdir_cache;
30445 +
30446 +       valid = 1;
30447 +       vdir_cache = au_fvdir_cache(file);
30448 +       offset = calc_offset(vdir_cache);
30449 +       AuDbg("offset %lld\n", offset);
30450 +       if (ctx->pos == offset)
30451 +               goto out;
30452 +
30453 +       vdir_cache->vd_last.ul = 0;
30454 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
30455 +       if (!ctx->pos)
30456 +               goto out;
30457 +
30458 +       valid = 0;
30459 +       deblk_sz = vdir_cache->vd_deblk_sz;
30460 +       ul = div64_u64(ctx->pos, deblk_sz);
30461 +       AuDbg("ul %lu\n", ul);
30462 +       if (ul >= vdir_cache->vd_nblk)
30463 +               goto out;
30464 +
30465 +       n = vdir_cache->vd_nblk;
30466 +       for (; ul < n; ul++) {
30467 +               p.deblk = vdir_cache->vd_deblk[ul];
30468 +               deblk_end.deblk = p.deblk + deblk_sz;
30469 +               offset = ul;
30470 +               offset *= deblk_sz;
30471 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
30472 +                       unsigned int l;
30473 +
30474 +                       l = calc_size(p.de->de_str.len);
30475 +                       offset += l;
30476 +                       p.deblk += l;
30477 +               }
30478 +               if (!is_deblk_end(&p, &deblk_end)) {
30479 +                       valid = 1;
30480 +                       vdir_cache->vd_last.ul = ul;
30481 +                       vdir_cache->vd_last.p = p;
30482 +                       break;
30483 +               }
30484 +       }
30485 +
30486 +out:
30487 +       /* smp_mb(); */
30488 +       AuTraceErr(!valid);
30489 +       return valid;
30490 +}
30491 +
30492 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
30493 +{
30494 +       unsigned int l, deblk_sz;
30495 +       union au_vdir_deblk_p deblk_end;
30496 +       struct au_vdir *vdir_cache;
30497 +       struct au_vdir_de *de;
30498 +
30499 +       vdir_cache = au_fvdir_cache(file);
30500 +       if (!seek_vdir(file, ctx))
30501 +               return 0;
30502 +
30503 +       deblk_sz = vdir_cache->vd_deblk_sz;
30504 +       while (1) {
30505 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30506 +               deblk_end.deblk += deblk_sz;
30507 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
30508 +                       de = vdir_cache->vd_last.p.de;
30509 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
30510 +                             de->de_str.len, de->de_str.name, ctx->pos,
30511 +                             (unsigned long)de->de_ino, de->de_type);
30512 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
30513 +                                              de->de_str.len, de->de_ino,
30514 +                                              de->de_type))) {
30515 +                               /* todo: ignore the error caused by udba? */
30516 +                               /* return err; */
30517 +                               return 0;
30518 +                       }
30519 +
30520 +                       l = calc_size(de->de_str.len);
30521 +                       vdir_cache->vd_last.p.deblk += l;
30522 +                       ctx->pos += l;
30523 +               }
30524 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
30525 +                       vdir_cache->vd_last.ul++;
30526 +                       vdir_cache->vd_last.p.deblk
30527 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30528 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
30529 +                       continue;
30530 +               }
30531 +               break;
30532 +       }
30533 +
30534 +       /* smp_mb(); */
30535 +       return 0;
30536 +}
30537 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
30538 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
30539 +++ linux/fs/aufs/vfsub.c       2017-03-02 14:24:13.870255912 +0100
30540 @@ -0,0 +1,899 @@
30541 +/*
30542 + * Copyright (C) 2005-2017 Junjiro R. Okajima
30543 + *
30544 + * This program, aufs is free software; you can redistribute it and/or modify
30545 + * it under the terms of the GNU General Public License as published by
30546 + * the Free Software Foundation; either version 2 of the License, or
30547 + * (at your option) any later version.
30548 + *
30549 + * This program is distributed in the hope that it will be useful,
30550 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30551 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30552 + * GNU General Public License for more details.
30553 + *
30554 + * You should have received a copy of the GNU General Public License
30555 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30556 + */
30557 +
30558 +/*
30559 + * sub-routines for VFS
30560 + */
30561 +
30562 +#include <linux/namei.h>
30563 +#include <linux/nsproxy.h>
30564 +#include <linux/security.h>
30565 +#include <linux/splice.h>
30566 +#ifdef CONFIG_AUFS_BR_FUSE
30567 +#include "../fs/mount.h"
30568 +#endif
30569 +#include "aufs.h"
30570 +
30571 +#ifdef CONFIG_AUFS_BR_FUSE
30572 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
30573 +{
30574 +       struct nsproxy *ns;
30575 +
30576 +       if (!au_test_fuse(h_sb) || !au_userns)
30577 +               return 0;
30578 +
30579 +       ns = current->nsproxy;
30580 +       /* no {get,put}_nsproxy(ns) */
30581 +       return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
30582 +}
30583 +#endif
30584 +
30585 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
30586 +{
30587 +       int err;
30588 +
30589 +       lockdep_off();
30590 +       down_read(&h_sb->s_umount);
30591 +       err = __sync_filesystem(h_sb, wait);
30592 +       up_read(&h_sb->s_umount);
30593 +       lockdep_on();
30594 +
30595 +       return err;
30596 +}
30597 +
30598 +/* ---------------------------------------------------------------------- */
30599 +
30600 +int vfsub_update_h_iattr(struct path *h_path, int *did)
30601 +{
30602 +       int err;
30603 +       struct kstat st;
30604 +       struct super_block *h_sb;
30605 +
30606 +       /* for remote fs, leave work for its getattr or d_revalidate */
30607 +       /* for bad i_attr fs, handle them in aufs_getattr() */
30608 +       /* still some fs may acquire i_mutex. we need to skip them */
30609 +       err = 0;
30610 +       if (!did)
30611 +               did = &err;
30612 +       h_sb = h_path->dentry->d_sb;
30613 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
30614 +       if (*did)
30615 +               err = vfs_getattr(h_path, &st);
30616 +
30617 +       return err;
30618 +}
30619 +
30620 +/* ---------------------------------------------------------------------- */
30621 +
30622 +struct file *vfsub_dentry_open(struct path *path, int flags)
30623 +{
30624 +       struct file *file;
30625 +
30626 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
30627 +                          current_cred());
30628 +       if (!IS_ERR_OR_NULL(file)
30629 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
30630 +               i_readcount_inc(d_inode(path->dentry));
30631 +
30632 +       return file;
30633 +}
30634 +
30635 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
30636 +{
30637 +       struct file *file;
30638 +
30639 +       lockdep_off();
30640 +       file = filp_open(path,
30641 +                        oflags /* | __FMODE_NONOTIFY */,
30642 +                        mode);
30643 +       lockdep_on();
30644 +       if (IS_ERR(file))
30645 +               goto out;
30646 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30647 +
30648 +out:
30649 +       return file;
30650 +}
30651 +
30652 +/*
30653 + * Ideally this function should call VFS:do_last() in order to keep all its
30654 + * checkings. But it is very hard for aufs to regenerate several VFS internal
30655 + * structure such as nameidata. This is a second (or third) best approach.
30656 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
30657 + */
30658 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30659 +                     struct vfsub_aopen_args *args, struct au_branch *br)
30660 +{
30661 +       int err;
30662 +       struct file *file = args->file;
30663 +       /* copied from linux/fs/namei.c:atomic_open() */
30664 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
30665 +
30666 +       IMustLock(dir);
30667 +       AuDebugOn(!dir->i_op->atomic_open);
30668 +
30669 +       err = au_br_test_oflag(args->open_flag, br);
30670 +       if (unlikely(err))
30671 +               goto out;
30672 +
30673 +       args->file->f_path.dentry = DENTRY_NOT_SET;
30674 +       args->file->f_path.mnt = au_br_mnt(br);
30675 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
30676 +                                    args->create_mode, args->opened);
30677 +       if (err >= 0) {
30678 +               /* some filesystems don't set FILE_CREATED while succeeded? */
30679 +               if (*args->opened & FILE_CREATED)
30680 +                       fsnotify_create(dir, dentry);
30681 +       } else
30682 +               goto out;
30683 +
30684 +
30685 +       if (!err) {
30686 +               /* todo: call VFS:may_open() here */
30687 +               err = open_check_o_direct(file);
30688 +               /* todo: ima_file_check() too? */
30689 +               if (!err && (args->open_flag & __FMODE_EXEC))
30690 +                       err = deny_write_access(file);
30691 +               if (unlikely(err))
30692 +                       /* note that the file is created and still opened */
30693 +                       goto out;
30694 +       }
30695 +
30696 +       au_br_get(br);
30697 +       fsnotify_open(file);
30698 +
30699 +out:
30700 +       return err;
30701 +}
30702 +
30703 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30704 +{
30705 +       int err;
30706 +
30707 +       err = kern_path(name, flags, path);
30708 +       if (!err && d_is_positive(path->dentry))
30709 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30710 +       return err;
30711 +}
30712 +
30713 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
30714 +                                            struct dentry *parent, int len)
30715 +{
30716 +       struct path path = {
30717 +               .mnt = NULL
30718 +       };
30719 +
30720 +       path.dentry = lookup_one_len_unlocked(name, parent, len);
30721 +       if (IS_ERR(path.dentry))
30722 +               goto out;
30723 +       if (d_is_positive(path.dentry))
30724 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30725 +
30726 +out:
30727 +       AuTraceErrPtr(path.dentry);
30728 +       return path.dentry;
30729 +}
30730 +
30731 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30732 +                                   int len)
30733 +{
30734 +       struct path path = {
30735 +               .mnt = NULL
30736 +       };
30737 +
30738 +       /* VFS checks it too, but by WARN_ON_ONCE() */
30739 +       IMustLock(d_inode(parent));
30740 +
30741 +       path.dentry = lookup_one_len(name, parent, len);
30742 +       if (IS_ERR(path.dentry))
30743 +               goto out;
30744 +       if (d_is_positive(path.dentry))
30745 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30746 +
30747 +out:
30748 +       AuTraceErrPtr(path.dentry);
30749 +       return path.dentry;
30750 +}
30751 +
30752 +void vfsub_call_lkup_one(void *args)
30753 +{
30754 +       struct vfsub_lkup_one_args *a = args;
30755 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
30756 +}
30757 +
30758 +/* ---------------------------------------------------------------------- */
30759 +
30760 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30761 +                                struct dentry *d2, struct au_hinode *hdir2)
30762 +{
30763 +       struct dentry *d;
30764 +
30765 +       lockdep_off();
30766 +       d = lock_rename(d1, d2);
30767 +       lockdep_on();
30768 +       au_hn_suspend(hdir1);
30769 +       if (hdir1 != hdir2)
30770 +               au_hn_suspend(hdir2);
30771 +
30772 +       return d;
30773 +}
30774 +
30775 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30776 +                        struct dentry *d2, struct au_hinode *hdir2)
30777 +{
30778 +       au_hn_resume(hdir1);
30779 +       if (hdir1 != hdir2)
30780 +               au_hn_resume(hdir2);
30781 +       lockdep_off();
30782 +       unlock_rename(d1, d2);
30783 +       lockdep_on();
30784 +}
30785 +
30786 +/* ---------------------------------------------------------------------- */
30787 +
30788 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
30789 +{
30790 +       int err;
30791 +       struct dentry *d;
30792 +
30793 +       IMustLock(dir);
30794 +
30795 +       d = path->dentry;
30796 +       path->dentry = d->d_parent;
30797 +       err = security_path_mknod(path, d, mode, 0);
30798 +       path->dentry = d;
30799 +       if (unlikely(err))
30800 +               goto out;
30801 +
30802 +       lockdep_off();
30803 +       err = vfs_create(dir, path->dentry, mode, want_excl);
30804 +       lockdep_on();
30805 +       if (!err) {
30806 +               struct path tmp = *path;
30807 +               int did;
30808 +
30809 +               vfsub_update_h_iattr(&tmp, &did);
30810 +               if (did) {
30811 +                       tmp.dentry = path->dentry->d_parent;
30812 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30813 +               }
30814 +               /*ignore*/
30815 +       }
30816 +
30817 +out:
30818 +       return err;
30819 +}
30820 +
30821 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30822 +{
30823 +       int err;
30824 +       struct dentry *d;
30825 +
30826 +       IMustLock(dir);
30827 +
30828 +       d = path->dentry;
30829 +       path->dentry = d->d_parent;
30830 +       err = security_path_symlink(path, d, symname);
30831 +       path->dentry = d;
30832 +       if (unlikely(err))
30833 +               goto out;
30834 +
30835 +       lockdep_off();
30836 +       err = vfs_symlink(dir, path->dentry, symname);
30837 +       lockdep_on();
30838 +       if (!err) {
30839 +               struct path tmp = *path;
30840 +               int did;
30841 +
30842 +               vfsub_update_h_iattr(&tmp, &did);
30843 +               if (did) {
30844 +                       tmp.dentry = path->dentry->d_parent;
30845 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30846 +               }
30847 +               /*ignore*/
30848 +       }
30849 +
30850 +out:
30851 +       return err;
30852 +}
30853 +
30854 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30855 +{
30856 +       int err;
30857 +       struct dentry *d;
30858 +
30859 +       IMustLock(dir);
30860 +
30861 +       d = path->dentry;
30862 +       path->dentry = d->d_parent;
30863 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
30864 +       path->dentry = d;
30865 +       if (unlikely(err))
30866 +               goto out;
30867 +
30868 +       lockdep_off();
30869 +       err = vfs_mknod(dir, path->dentry, mode, dev);
30870 +       lockdep_on();
30871 +       if (!err) {
30872 +               struct path tmp = *path;
30873 +               int did;
30874 +
30875 +               vfsub_update_h_iattr(&tmp, &did);
30876 +               if (did) {
30877 +                       tmp.dentry = path->dentry->d_parent;
30878 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30879 +               }
30880 +               /*ignore*/
30881 +       }
30882 +
30883 +out:
30884 +       return err;
30885 +}
30886 +
30887 +static int au_test_nlink(struct inode *inode)
30888 +{
30889 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30890 +
30891 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
30892 +           || inode->i_nlink < link_max)
30893 +               return 0;
30894 +       return -EMLINK;
30895 +}
30896 +
30897 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30898 +              struct inode **delegated_inode)
30899 +{
30900 +       int err;
30901 +       struct dentry *d;
30902 +
30903 +       IMustLock(dir);
30904 +
30905 +       err = au_test_nlink(d_inode(src_dentry));
30906 +       if (unlikely(err))
30907 +               return err;
30908 +
30909 +       /* we don't call may_linkat() */
30910 +       d = path->dentry;
30911 +       path->dentry = d->d_parent;
30912 +       err = security_path_link(src_dentry, path, d);
30913 +       path->dentry = d;
30914 +       if (unlikely(err))
30915 +               goto out;
30916 +
30917 +       lockdep_off();
30918 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
30919 +       lockdep_on();
30920 +       if (!err) {
30921 +               struct path tmp = *path;
30922 +               int did;
30923 +
30924 +               /* fuse has different memory inode for the same inumber */
30925 +               vfsub_update_h_iattr(&tmp, &did);
30926 +               if (did) {
30927 +                       tmp.dentry = path->dentry->d_parent;
30928 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30929 +                       tmp.dentry = src_dentry;
30930 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30931 +               }
30932 +               /*ignore*/
30933 +       }
30934 +
30935 +out:
30936 +       return err;
30937 +}
30938 +
30939 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
30940 +                struct inode *dir, struct path *path,
30941 +                struct inode **delegated_inode, unsigned int flags)
30942 +{
30943 +       int err;
30944 +       struct path tmp = {
30945 +               .mnt    = path->mnt
30946 +       };
30947 +       struct dentry *d;
30948 +
30949 +       IMustLock(dir);
30950 +       IMustLock(src_dir);
30951 +
30952 +       d = path->dentry;
30953 +       path->dentry = d->d_parent;
30954 +       tmp.dentry = src_dentry->d_parent;
30955 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
30956 +       path->dentry = d;
30957 +       if (unlikely(err))
30958 +               goto out;
30959 +
30960 +       lockdep_off();
30961 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
30962 +                        delegated_inode, flags);
30963 +       lockdep_on();
30964 +       if (!err) {
30965 +               int did;
30966 +
30967 +               tmp.dentry = d->d_parent;
30968 +               vfsub_update_h_iattr(&tmp, &did);
30969 +               if (did) {
30970 +                       tmp.dentry = src_dentry;
30971 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30972 +                       tmp.dentry = src_dentry->d_parent;
30973 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30974 +               }
30975 +               /*ignore*/
30976 +       }
30977 +
30978 +out:
30979 +       return err;
30980 +}
30981 +
30982 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30983 +{
30984 +       int err;
30985 +       struct dentry *d;
30986 +
30987 +       IMustLock(dir);
30988 +
30989 +       d = path->dentry;
30990 +       path->dentry = d->d_parent;
30991 +       err = security_path_mkdir(path, d, mode);
30992 +       path->dentry = d;
30993 +       if (unlikely(err))
30994 +               goto out;
30995 +
30996 +       lockdep_off();
30997 +       err = vfs_mkdir(dir, path->dentry, mode);
30998 +       lockdep_on();
30999 +       if (!err) {
31000 +               struct path tmp = *path;
31001 +               int did;
31002 +
31003 +               vfsub_update_h_iattr(&tmp, &did);
31004 +               if (did) {
31005 +                       tmp.dentry = path->dentry->d_parent;
31006 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
31007 +               }
31008 +               /*ignore*/
31009 +       }
31010 +
31011 +out:
31012 +       return err;
31013 +}
31014 +
31015 +int vfsub_rmdir(struct inode *dir, struct path *path)
31016 +{
31017 +       int err;
31018 +       struct dentry *d;
31019 +
31020 +       IMustLock(dir);
31021 +
31022 +       d = path->dentry;
31023 +       path->dentry = d->d_parent;
31024 +       err = security_path_rmdir(path, d);
31025 +       path->dentry = d;
31026 +       if (unlikely(err))
31027 +               goto out;
31028 +
31029 +       lockdep_off();
31030 +       err = vfs_rmdir(dir, path->dentry);
31031 +       lockdep_on();
31032 +       if (!err) {
31033 +               struct path tmp = {
31034 +                       .dentry = path->dentry->d_parent,
31035 +                       .mnt    = path->mnt
31036 +               };
31037 +
31038 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
31039 +       }
31040 +
31041 +out:
31042 +       return err;
31043 +}
31044 +
31045 +/* ---------------------------------------------------------------------- */
31046 +
31047 +/* todo: support mmap_sem? */
31048 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31049 +                    loff_t *ppos)
31050 +{
31051 +       ssize_t err;
31052 +
31053 +       lockdep_off();
31054 +       err = vfs_read(file, ubuf, count, ppos);
31055 +       lockdep_on();
31056 +       if (err >= 0)
31057 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
31058 +       return err;
31059 +}
31060 +
31061 +/* todo: kernel_read()? */
31062 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31063 +                    loff_t *ppos)
31064 +{
31065 +       ssize_t err;
31066 +       mm_segment_t oldfs;
31067 +       union {
31068 +               void *k;
31069 +               char __user *u;
31070 +       } buf;
31071 +
31072 +       buf.k = kbuf;
31073 +       oldfs = get_fs();
31074 +       set_fs(KERNEL_DS);
31075 +       err = vfsub_read_u(file, buf.u, count, ppos);
31076 +       set_fs(oldfs);
31077 +       return err;
31078 +}
31079 +
31080 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31081 +                     loff_t *ppos)
31082 +{
31083 +       ssize_t err;
31084 +
31085 +       lockdep_off();
31086 +       err = vfs_write(file, ubuf, count, ppos);
31087 +       lockdep_on();
31088 +       if (err >= 0)
31089 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
31090 +       return err;
31091 +}
31092 +
31093 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
31094 +{
31095 +       ssize_t err;
31096 +       mm_segment_t oldfs;
31097 +       union {
31098 +               void *k;
31099 +               const char __user *u;
31100 +       } buf;
31101 +
31102 +       buf.k = kbuf;
31103 +       oldfs = get_fs();
31104 +       set_fs(KERNEL_DS);
31105 +       err = vfsub_write_u(file, buf.u, count, ppos);
31106 +       set_fs(oldfs);
31107 +       return err;
31108 +}
31109 +
31110 +int vfsub_flush(struct file *file, fl_owner_t id)
31111 +{
31112 +       int err;
31113 +
31114 +       err = 0;
31115 +       if (file->f_op->flush) {
31116 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
31117 +                       err = file->f_op->flush(file, id);
31118 +               else {
31119 +                       lockdep_off();
31120 +                       err = file->f_op->flush(file, id);
31121 +                       lockdep_on();
31122 +               }
31123 +               if (!err)
31124 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
31125 +               /*ignore*/
31126 +       }
31127 +       return err;
31128 +}
31129 +
31130 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
31131 +{
31132 +       int err;
31133 +
31134 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
31135 +
31136 +       lockdep_off();
31137 +       err = iterate_dir(file, ctx);
31138 +       lockdep_on();
31139 +       if (err >= 0)
31140 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
31141 +       return err;
31142 +}
31143 +
31144 +long vfsub_splice_to(struct file *in, loff_t *ppos,
31145 +                    struct pipe_inode_info *pipe, size_t len,
31146 +                    unsigned int flags)
31147 +{
31148 +       long err;
31149 +
31150 +       lockdep_off();
31151 +       err = do_splice_to(in, ppos, pipe, len, flags);
31152 +       lockdep_on();
31153 +       file_accessed(in);
31154 +       if (err >= 0)
31155 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
31156 +       return err;
31157 +}
31158 +
31159 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31160 +                      loff_t *ppos, size_t len, unsigned int flags)
31161 +{
31162 +       long err;
31163 +
31164 +       lockdep_off();
31165 +       err = do_splice_from(pipe, out, ppos, len, flags);
31166 +       lockdep_on();
31167 +       if (err >= 0)
31168 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
31169 +       return err;
31170 +}
31171 +
31172 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
31173 +{
31174 +       int err;
31175 +
31176 +       /* file can be NULL */
31177 +       lockdep_off();
31178 +       err = vfs_fsync(file, datasync);
31179 +       lockdep_on();
31180 +       if (!err) {
31181 +               if (!path) {
31182 +                       AuDebugOn(!file);
31183 +                       path = &file->f_path;
31184 +               }
31185 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
31186 +       }
31187 +       return err;
31188 +}
31189 +
31190 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
31191 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31192 +               struct file *h_file)
31193 +{
31194 +       int err;
31195 +       struct inode *h_inode;
31196 +       struct super_block *h_sb;
31197 +
31198 +       if (!h_file) {
31199 +               err = vfsub_truncate(h_path, length);
31200 +               goto out;
31201 +       }
31202 +
31203 +       h_inode = d_inode(h_path->dentry);
31204 +       h_sb = h_inode->i_sb;
31205 +       lockdep_off();
31206 +       sb_start_write(h_sb);
31207 +       lockdep_on();
31208 +       err = locks_verify_truncate(h_inode, h_file, length);
31209 +       if (!err)
31210 +               err = security_path_truncate(h_path);
31211 +       if (!err) {
31212 +               lockdep_off();
31213 +               err = do_truncate(h_path->dentry, length, attr, h_file);
31214 +               lockdep_on();
31215 +       }
31216 +       lockdep_off();
31217 +       sb_end_write(h_sb);
31218 +       lockdep_on();
31219 +
31220 +out:
31221 +       return err;
31222 +}
31223 +
31224 +/* ---------------------------------------------------------------------- */
31225 +
31226 +struct au_vfsub_mkdir_args {
31227 +       int *errp;
31228 +       struct inode *dir;
31229 +       struct path *path;
31230 +       int mode;
31231 +};
31232 +
31233 +static void au_call_vfsub_mkdir(void *args)
31234 +{
31235 +       struct au_vfsub_mkdir_args *a = args;
31236 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
31237 +}
31238 +
31239 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
31240 +{
31241 +       int err, do_sio, wkq_err;
31242 +
31243 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
31244 +       if (!do_sio) {
31245 +               lockdep_off();
31246 +               err = vfsub_mkdir(dir, path, mode);
31247 +               lockdep_on();
31248 +       } else {
31249 +               struct au_vfsub_mkdir_args args = {
31250 +                       .errp   = &err,
31251 +                       .dir    = dir,
31252 +                       .path   = path,
31253 +                       .mode   = mode
31254 +               };
31255 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
31256 +               if (unlikely(wkq_err))
31257 +                       err = wkq_err;
31258 +       }
31259 +
31260 +       return err;
31261 +}
31262 +
31263 +struct au_vfsub_rmdir_args {
31264 +       int *errp;
31265 +       struct inode *dir;
31266 +       struct path *path;
31267 +};
31268 +
31269 +static void au_call_vfsub_rmdir(void *args)
31270 +{
31271 +       struct au_vfsub_rmdir_args *a = args;
31272 +       *a->errp = vfsub_rmdir(a->dir, a->path);
31273 +}
31274 +
31275 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
31276 +{
31277 +       int err, do_sio, wkq_err;
31278 +
31279 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
31280 +       if (!do_sio) {
31281 +               lockdep_off();
31282 +               err = vfsub_rmdir(dir, path);
31283 +               lockdep_on();
31284 +       } else {
31285 +               struct au_vfsub_rmdir_args args = {
31286 +                       .errp   = &err,
31287 +                       .dir    = dir,
31288 +                       .path   = path
31289 +               };
31290 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
31291 +               if (unlikely(wkq_err))
31292 +                       err = wkq_err;
31293 +       }
31294 +
31295 +       return err;
31296 +}
31297 +
31298 +/* ---------------------------------------------------------------------- */
31299 +
31300 +struct notify_change_args {
31301 +       int *errp;
31302 +       struct path *path;
31303 +       struct iattr *ia;
31304 +       struct inode **delegated_inode;
31305 +};
31306 +
31307 +static void call_notify_change(void *args)
31308 +{
31309 +       struct notify_change_args *a = args;
31310 +       struct inode *h_inode;
31311 +
31312 +       h_inode = d_inode(a->path->dentry);
31313 +       IMustLock(h_inode);
31314 +
31315 +       *a->errp = -EPERM;
31316 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
31317 +               lockdep_off();
31318 +               *a->errp = notify_change(a->path->dentry, a->ia,
31319 +                                        a->delegated_inode);
31320 +               lockdep_on();
31321 +               if (!*a->errp)
31322 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
31323 +       }
31324 +       AuTraceErr(*a->errp);
31325 +}
31326 +
31327 +int vfsub_notify_change(struct path *path, struct iattr *ia,
31328 +                       struct inode **delegated_inode)
31329 +{
31330 +       int err;
31331 +       struct notify_change_args args = {
31332 +               .errp                   = &err,
31333 +               .path                   = path,
31334 +               .ia                     = ia,
31335 +               .delegated_inode        = delegated_inode
31336 +       };
31337 +
31338 +       call_notify_change(&args);
31339 +
31340 +       return err;
31341 +}
31342 +
31343 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31344 +                           struct inode **delegated_inode)
31345 +{
31346 +       int err, wkq_err;
31347 +       struct notify_change_args args = {
31348 +               .errp                   = &err,
31349 +               .path                   = path,
31350 +               .ia                     = ia,
31351 +               .delegated_inode        = delegated_inode
31352 +       };
31353 +
31354 +       wkq_err = au_wkq_wait(call_notify_change, &args);
31355 +       if (unlikely(wkq_err))
31356 +               err = wkq_err;
31357 +
31358 +       return err;
31359 +}
31360 +
31361 +/* ---------------------------------------------------------------------- */
31362 +
31363 +struct unlink_args {
31364 +       int *errp;
31365 +       struct inode *dir;
31366 +       struct path *path;
31367 +       struct inode **delegated_inode;
31368 +};
31369 +
31370 +static void call_unlink(void *args)
31371 +{
31372 +       struct unlink_args *a = args;
31373 +       struct dentry *d = a->path->dentry;
31374 +       struct inode *h_inode;
31375 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
31376 +                                     && au_dcount(d) == 1);
31377 +
31378 +       IMustLock(a->dir);
31379 +
31380 +       a->path->dentry = d->d_parent;
31381 +       *a->errp = security_path_unlink(a->path, d);
31382 +       a->path->dentry = d;
31383 +       if (unlikely(*a->errp))
31384 +               return;
31385 +
31386 +       if (!stop_sillyrename)
31387 +               dget(d);
31388 +       h_inode = NULL;
31389 +       if (d_is_positive(d)) {
31390 +               h_inode = d_inode(d);
31391 +               ihold(h_inode);
31392 +       }
31393 +
31394 +       lockdep_off();
31395 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
31396 +       lockdep_on();
31397 +       if (!*a->errp) {
31398 +               struct path tmp = {
31399 +                       .dentry = d->d_parent,
31400 +                       .mnt    = a->path->mnt
31401 +               };
31402 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
31403 +       }
31404 +
31405 +       if (!stop_sillyrename)
31406 +               dput(d);
31407 +       if (h_inode)
31408 +               iput(h_inode);
31409 +
31410 +       AuTraceErr(*a->errp);
31411 +}
31412 +
31413 +/*
31414 + * @dir: must be locked.
31415 + * @dentry: target dentry.
31416 + */
31417 +int vfsub_unlink(struct inode *dir, struct path *path,
31418 +                struct inode **delegated_inode, int force)
31419 +{
31420 +       int err;
31421 +       struct unlink_args args = {
31422 +               .errp                   = &err,
31423 +               .dir                    = dir,
31424 +               .path                   = path,
31425 +               .delegated_inode        = delegated_inode
31426 +       };
31427 +
31428 +       if (!force)
31429 +               call_unlink(&args);
31430 +       else {
31431 +               int wkq_err;
31432 +
31433 +               wkq_err = au_wkq_wait(call_unlink, &args);
31434 +               if (unlikely(wkq_err))
31435 +                       err = wkq_err;
31436 +       }
31437 +
31438 +       return err;
31439 +}
31440 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
31441 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
31442 +++ linux/fs/aufs/vfsub.h       2017-03-02 14:24:13.870255912 +0100
31443 @@ -0,0 +1,318 @@
31444 +/*
31445 + * Copyright (C) 2005-2017 Junjiro R. Okajima
31446 + *
31447 + * This program, aufs is free software; you can redistribute it and/or modify
31448 + * it under the terms of the GNU General Public License as published by
31449 + * the Free Software Foundation; either version 2 of the License, or
31450 + * (at your option) any later version.
31451 + *
31452 + * This program is distributed in the hope that it will be useful,
31453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31455 + * GNU General Public License for more details.
31456 + *
31457 + * You should have received a copy of the GNU General Public License
31458 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31459 + */
31460 +
31461 +/*
31462 + * sub-routines for VFS
31463 + */
31464 +
31465 +#ifndef __AUFS_VFSUB_H__
31466 +#define __AUFS_VFSUB_H__
31467 +
31468 +#ifdef __KERNEL__
31469 +
31470 +#include <linux/fs.h>
31471 +#include <linux/mount.h>
31472 +#include <linux/posix_acl.h>
31473 +#include <linux/xattr.h>
31474 +#include "debug.h"
31475 +
31476 +/* copied from linux/fs/internal.h */
31477 +/* todo: BAD approach!! */
31478 +extern void __mnt_drop_write(struct vfsmount *);
31479 +extern int open_check_o_direct(struct file *f);
31480 +
31481 +/* ---------------------------------------------------------------------- */
31482 +
31483 +/* lock subclass for lower inode */
31484 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
31485 +/* reduce? gave up. */
31486 +enum {
31487 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
31488 +       AuLsc_I_PARENT,         /* lower inode, parent first */
31489 +       AuLsc_I_PARENT2,        /* copyup dirs */
31490 +       AuLsc_I_PARENT3,        /* copyup wh */
31491 +       AuLsc_I_CHILD,
31492 +       AuLsc_I_CHILD2,
31493 +       AuLsc_I_End
31494 +};
31495 +
31496 +/* to debug easier, do not make them inlined functions */
31497 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
31498 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
31499 +
31500 +/* ---------------------------------------------------------------------- */
31501 +
31502 +static inline void vfsub_drop_nlink(struct inode *inode)
31503 +{
31504 +       AuDebugOn(!inode->i_nlink);
31505 +       drop_nlink(inode);
31506 +}
31507 +
31508 +static inline void vfsub_dead_dir(struct inode *inode)
31509 +{
31510 +       AuDebugOn(!S_ISDIR(inode->i_mode));
31511 +       inode->i_flags |= S_DEAD;
31512 +       clear_nlink(inode);
31513 +}
31514 +
31515 +static inline int vfsub_native_ro(struct inode *inode)
31516 +{
31517 +       return (inode->i_sb->s_flags & MS_RDONLY)
31518 +               || IS_RDONLY(inode)
31519 +               /* || IS_APPEND(inode) */
31520 +               || IS_IMMUTABLE(inode);
31521 +}
31522 +
31523 +#ifdef CONFIG_AUFS_BR_FUSE
31524 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
31525 +#else
31526 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
31527 +#endif
31528 +
31529 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
31530 +
31531 +/* ---------------------------------------------------------------------- */
31532 +
31533 +int vfsub_update_h_iattr(struct path *h_path, int *did);
31534 +struct file *vfsub_dentry_open(struct path *path, int flags);
31535 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
31536 +struct vfsub_aopen_args {
31537 +       struct file     *file;
31538 +       unsigned int    open_flag;
31539 +       umode_t         create_mode;
31540 +       int             *opened;
31541 +};
31542 +struct au_branch;
31543 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
31544 +                     struct vfsub_aopen_args *args, struct au_branch *br);
31545 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
31546 +
31547 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
31548 +                                            struct dentry *parent, int len);
31549 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
31550 +                                   int len);
31551 +
31552 +struct vfsub_lkup_one_args {
31553 +       struct dentry **errp;
31554 +       struct qstr *name;
31555 +       struct dentry *parent;
31556 +};
31557 +
31558 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
31559 +                                           struct dentry *parent)
31560 +{
31561 +       return vfsub_lookup_one_len(name->name, parent, name->len);
31562 +}
31563 +
31564 +void vfsub_call_lkup_one(void *args);
31565 +
31566 +/* ---------------------------------------------------------------------- */
31567 +
31568 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
31569 +{
31570 +       int err;
31571 +
31572 +       lockdep_off();
31573 +       err = mnt_want_write(mnt);
31574 +       lockdep_on();
31575 +       return err;
31576 +}
31577 +
31578 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
31579 +{
31580 +       lockdep_off();
31581 +       mnt_drop_write(mnt);
31582 +       lockdep_on();
31583 +}
31584 +
31585 +#if 0 /* reserved */
31586 +static inline void vfsub_mnt_drop_write_file(struct file *file)
31587 +{
31588 +       lockdep_off();
31589 +       mnt_drop_write_file(file);
31590 +       lockdep_on();
31591 +}
31592 +#endif
31593 +
31594 +/* ---------------------------------------------------------------------- */
31595 +
31596 +struct au_hinode;
31597 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
31598 +                                struct dentry *d2, struct au_hinode *hdir2);
31599 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
31600 +                        struct dentry *d2, struct au_hinode *hdir2);
31601 +
31602 +int vfsub_create(struct inode *dir, struct path *path, int mode,
31603 +                bool want_excl);
31604 +int vfsub_symlink(struct inode *dir, struct path *path,
31605 +                 const char *symname);
31606 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
31607 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
31608 +              struct path *path, struct inode **delegated_inode);
31609 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
31610 +                struct inode *hdir, struct path *path,
31611 +                struct inode **delegated_inode, unsigned int flags);
31612 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
31613 +int vfsub_rmdir(struct inode *dir, struct path *path);
31614 +
31615 +/* ---------------------------------------------------------------------- */
31616 +
31617 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31618 +                    loff_t *ppos);
31619 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31620 +                       loff_t *ppos);
31621 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31622 +                     loff_t *ppos);
31623 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
31624 +                     loff_t *ppos);
31625 +int vfsub_flush(struct file *file, fl_owner_t id);
31626 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
31627 +
31628 +static inline loff_t vfsub_f_size_read(struct file *file)
31629 +{
31630 +       return i_size_read(file_inode(file));
31631 +}
31632 +
31633 +static inline unsigned int vfsub_file_flags(struct file *file)
31634 +{
31635 +       unsigned int flags;
31636 +
31637 +       spin_lock(&file->f_lock);
31638 +       flags = file->f_flags;
31639 +       spin_unlock(&file->f_lock);
31640 +
31641 +       return flags;
31642 +}
31643 +
31644 +static inline int vfsub_file_execed(struct file *file)
31645 +{
31646 +       /* todo: direct access f_flags */
31647 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
31648 +}
31649 +
31650 +#if 0 /* reserved */
31651 +static inline void vfsub_file_accessed(struct file *h_file)
31652 +{
31653 +       file_accessed(h_file);
31654 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
31655 +}
31656 +#endif
31657 +
31658 +#if 0 /* reserved */
31659 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
31660 +                                    struct dentry *h_dentry)
31661 +{
31662 +       struct path h_path = {
31663 +               .dentry = h_dentry,
31664 +               .mnt    = h_mnt
31665 +       };
31666 +       touch_atime(&h_path);
31667 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
31668 +}
31669 +#endif
31670 +
31671 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
31672 +                                   int flags)
31673 +{
31674 +       return update_time(h_inode, ts, flags);
31675 +       /* no vfsub_update_h_iattr() since we don't have struct path */
31676 +}
31677 +
31678 +#ifdef CONFIG_FS_POSIX_ACL
31679 +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
31680 +{
31681 +       int err;
31682 +
31683 +       err = posix_acl_chmod(h_inode, h_mode);
31684 +       if (err == -EOPNOTSUPP)
31685 +               err = 0;
31686 +       return err;
31687 +}
31688 +#else
31689 +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
31690 +#endif
31691 +
31692 +long vfsub_splice_to(struct file *in, loff_t *ppos,
31693 +                    struct pipe_inode_info *pipe, size_t len,
31694 +                    unsigned int flags);
31695 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31696 +                      loff_t *ppos, size_t len, unsigned int flags);
31697 +
31698 +static inline long vfsub_truncate(struct path *path, loff_t length)
31699 +{
31700 +       long err;
31701 +
31702 +       lockdep_off();
31703 +       err = vfs_truncate(path, length);
31704 +       lockdep_on();
31705 +       return err;
31706 +}
31707 +
31708 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31709 +               struct file *h_file);
31710 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
31711 +
31712 +/* ---------------------------------------------------------------------- */
31713 +
31714 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
31715 +{
31716 +       loff_t err;
31717 +
31718 +       lockdep_off();
31719 +       err = vfs_llseek(file, offset, origin);
31720 +       lockdep_on();
31721 +       return err;
31722 +}
31723 +
31724 +/* ---------------------------------------------------------------------- */
31725 +
31726 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
31727 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
31728 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31729 +                           struct inode **delegated_inode);
31730 +int vfsub_notify_change(struct path *path, struct iattr *ia,
31731 +                       struct inode **delegated_inode);
31732 +int vfsub_unlink(struct inode *dir, struct path *path,
31733 +                struct inode **delegated_inode, int force);
31734 +
31735 +/* ---------------------------------------------------------------------- */
31736 +
31737 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
31738 +                                const void *value, size_t size, int flags)
31739 +{
31740 +       int err;
31741 +
31742 +       lockdep_off();
31743 +       err = vfs_setxattr(dentry, name, value, size, flags);
31744 +       lockdep_on();
31745 +
31746 +       return err;
31747 +}
31748 +
31749 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31750 +{
31751 +       int err;
31752 +
31753 +       lockdep_off();
31754 +       err = vfs_removexattr(dentry, name);
31755 +       lockdep_on();
31756 +
31757 +       return err;
31758 +}
31759 +
31760 +#endif /* __KERNEL__ */
31761 +#endif /* __AUFS_VFSUB_H__ */
31762 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31763 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
31764 +++ linux/fs/aufs/wbr_policy.c  2017-03-02 14:24:13.870255912 +0100
31765 @@ -0,0 +1,830 @@
31766 +/*
31767 + * Copyright (C) 2005-2017 Junjiro R. Okajima
31768 + *
31769 + * This program, aufs is free software; you can redistribute it and/or modify
31770 + * it under the terms of the GNU General Public License as published by
31771 + * the Free Software Foundation; either version 2 of the License, or
31772 + * (at your option) any later version.
31773 + *
31774 + * This program is distributed in the hope that it will be useful,
31775 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31776 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31777 + * GNU General Public License for more details.
31778 + *
31779 + * You should have received a copy of the GNU General Public License
31780 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31781 + */
31782 +
31783 +/*
31784 + * policies for selecting one among multiple writable branches
31785 + */
31786 +
31787 +#include <linux/statfs.h>
31788 +#include "aufs.h"
31789 +
31790 +/* subset of cpup_attr() */
31791 +static noinline_for_stack
31792 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31793 +{
31794 +       int err, sbits;
31795 +       struct iattr ia;
31796 +       struct inode *h_isrc;
31797 +
31798 +       h_isrc = d_inode(h_src);
31799 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31800 +       ia.ia_mode = h_isrc->i_mode;
31801 +       ia.ia_uid = h_isrc->i_uid;
31802 +       ia.ia_gid = h_isrc->i_gid;
31803 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
31804 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
31805 +       /* no delegation since it is just created */
31806 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31807 +
31808 +       /* is this nfs only? */
31809 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31810 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31811 +               ia.ia_mode = h_isrc->i_mode;
31812 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31813 +       }
31814 +
31815 +       return err;
31816 +}
31817 +
31818 +#define AuCpdown_PARENT_OPQ    1
31819 +#define AuCpdown_WHED          (1 << 1)
31820 +#define AuCpdown_MADE_DIR      (1 << 2)
31821 +#define AuCpdown_DIROPQ                (1 << 3)
31822 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
31823 +#define au_fset_cpdown(flags, name) \
31824 +       do { (flags) |= AuCpdown_##name; } while (0)
31825 +#define au_fclr_cpdown(flags, name) \
31826 +       do { (flags) &= ~AuCpdown_##name; } while (0)
31827 +
31828 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
31829 +                            unsigned int *flags)
31830 +{
31831 +       int err;
31832 +       struct dentry *opq_dentry;
31833 +
31834 +       opq_dentry = au_diropq_create(dentry, bdst);
31835 +       err = PTR_ERR(opq_dentry);
31836 +       if (IS_ERR(opq_dentry))
31837 +               goto out;
31838 +       dput(opq_dentry);
31839 +       au_fset_cpdown(*flags, DIROPQ);
31840 +
31841 +out:
31842 +       return err;
31843 +}
31844 +
31845 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31846 +                           struct inode *dir, aufs_bindex_t bdst)
31847 +{
31848 +       int err;
31849 +       struct path h_path;
31850 +       struct au_branch *br;
31851 +
31852 +       br = au_sbr(dentry->d_sb, bdst);
31853 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31854 +       err = PTR_ERR(h_path.dentry);
31855 +       if (IS_ERR(h_path.dentry))
31856 +               goto out;
31857 +
31858 +       err = 0;
31859 +       if (d_is_positive(h_path.dentry)) {
31860 +               h_path.mnt = au_br_mnt(br);
31861 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31862 +                                         dentry);
31863 +       }
31864 +       dput(h_path.dentry);
31865 +
31866 +out:
31867 +       return err;
31868 +}
31869 +
31870 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
31871 +                        struct au_pin *pin,
31872 +                        struct dentry *h_parent, void *arg)
31873 +{
31874 +       int err, rerr;
31875 +       aufs_bindex_t bopq, btop;
31876 +       struct path h_path;
31877 +       struct dentry *parent;
31878 +       struct inode *h_dir, *h_inode, *inode, *dir;
31879 +       unsigned int *flags = arg;
31880 +
31881 +       btop = au_dbtop(dentry);
31882 +       /* dentry is di-locked */
31883 +       parent = dget_parent(dentry);
31884 +       dir = d_inode(parent);
31885 +       h_dir = d_inode(h_parent);
31886 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31887 +       IMustLock(h_dir);
31888 +
31889 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
31890 +       if (unlikely(err < 0))
31891 +               goto out;
31892 +       h_path.dentry = au_h_dptr(dentry, bdst);
31893 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31894 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31895 +                             S_IRWXU | S_IRUGO | S_IXUGO);
31896 +       if (unlikely(err))
31897 +               goto out_put;
31898 +       au_fset_cpdown(*flags, MADE_DIR);
31899 +
31900 +       bopq = au_dbdiropq(dentry);
31901 +       au_fclr_cpdown(*flags, WHED);
31902 +       au_fclr_cpdown(*flags, DIROPQ);
31903 +       if (au_dbwh(dentry) == bdst)
31904 +               au_fset_cpdown(*flags, WHED);
31905 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31906 +               au_fset_cpdown(*flags, PARENT_OPQ);
31907 +       h_inode = d_inode(h_path.dentry);
31908 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
31909 +       if (au_ftest_cpdown(*flags, WHED)) {
31910 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
31911 +               if (unlikely(err)) {
31912 +                       inode_unlock(h_inode);
31913 +                       goto out_dir;
31914 +               }
31915 +       }
31916 +
31917 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
31918 +       inode_unlock(h_inode);
31919 +       if (unlikely(err))
31920 +               goto out_opq;
31921 +
31922 +       if (au_ftest_cpdown(*flags, WHED)) {
31923 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31924 +               if (unlikely(err))
31925 +                       goto out_opq;
31926 +       }
31927 +
31928 +       inode = d_inode(dentry);
31929 +       if (au_ibbot(inode) < bdst)
31930 +               au_set_ibbot(inode, bdst);
31931 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31932 +                     au_hi_flags(inode, /*isdir*/1));
31933 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
31934 +       goto out; /* success */
31935 +
31936 +       /* revert */
31937 +out_opq:
31938 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
31939 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
31940 +               rerr = au_diropq_remove(dentry, bdst);
31941 +               inode_unlock(h_inode);
31942 +               if (unlikely(rerr)) {
31943 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31944 +                               dentry, bdst, rerr);
31945 +                       err = -EIO;
31946 +                       goto out;
31947 +               }
31948 +       }
31949 +out_dir:
31950 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
31951 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31952 +               if (unlikely(rerr)) {
31953 +                       AuIOErr("failed removing %pd b%d (%d)\n",
31954 +                               dentry, bdst, rerr);
31955 +                       err = -EIO;
31956 +               }
31957 +       }
31958 +out_put:
31959 +       au_set_h_dptr(dentry, bdst, NULL);
31960 +       if (au_dbbot(dentry) == bdst)
31961 +               au_update_dbbot(dentry);
31962 +out:
31963 +       dput(parent);
31964 +       return err;
31965 +}
31966 +
31967 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31968 +{
31969 +       int err;
31970 +       unsigned int flags;
31971 +
31972 +       flags = 0;
31973 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
31974 +
31975 +       return err;
31976 +}
31977 +
31978 +/* ---------------------------------------------------------------------- */
31979 +
31980 +/* policies for create */
31981 +
31982 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
31983 +{
31984 +       int err, i, j, ndentry;
31985 +       aufs_bindex_t bopq;
31986 +       struct au_dcsub_pages dpages;
31987 +       struct au_dpage *dpage;
31988 +       struct dentry **dentries, *parent, *d;
31989 +
31990 +       err = au_dpages_init(&dpages, GFP_NOFS);
31991 +       if (unlikely(err))
31992 +               goto out;
31993 +       parent = dget_parent(dentry);
31994 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
31995 +       if (unlikely(err))
31996 +               goto out_free;
31997 +
31998 +       err = bindex;
31999 +       for (i = 0; i < dpages.ndpage; i++) {
32000 +               dpage = dpages.dpages + i;
32001 +               dentries = dpage->dentries;
32002 +               ndentry = dpage->ndentry;
32003 +               for (j = 0; j < ndentry; j++) {
32004 +                       d = dentries[j];
32005 +                       di_read_lock_parent2(d, !AuLock_IR);
32006 +                       bopq = au_dbdiropq(d);
32007 +                       di_read_unlock(d, !AuLock_IR);
32008 +                       if (bopq >= 0 && bopq < err)
32009 +                               err = bopq;
32010 +               }
32011 +       }
32012 +
32013 +out_free:
32014 +       dput(parent);
32015 +       au_dpages_free(&dpages);
32016 +out:
32017 +       return err;
32018 +}
32019 +
32020 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
32021 +{
32022 +       for (; bindex >= 0; bindex--)
32023 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
32024 +                       return bindex;
32025 +       return -EROFS;
32026 +}
32027 +
32028 +/* top down parent */
32029 +static int au_wbr_create_tdp(struct dentry *dentry,
32030 +                            unsigned int flags __maybe_unused)
32031 +{
32032 +       int err;
32033 +       aufs_bindex_t btop, bindex;
32034 +       struct super_block *sb;
32035 +       struct dentry *parent, *h_parent;
32036 +
32037 +       sb = dentry->d_sb;
32038 +       btop = au_dbtop(dentry);
32039 +       err = btop;
32040 +       if (!au_br_rdonly(au_sbr(sb, btop)))
32041 +               goto out;
32042 +
32043 +       err = -EROFS;
32044 +       parent = dget_parent(dentry);
32045 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
32046 +               h_parent = au_h_dptr(parent, bindex);
32047 +               if (!h_parent || d_is_negative(h_parent))
32048 +                       continue;
32049 +
32050 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
32051 +                       err = bindex;
32052 +                       break;
32053 +               }
32054 +       }
32055 +       dput(parent);
32056 +
32057 +       /* bottom up here */
32058 +       if (unlikely(err < 0)) {
32059 +               err = au_wbr_bu(sb, btop - 1);
32060 +               if (err >= 0)
32061 +                       err = au_wbr_nonopq(dentry, err);
32062 +       }
32063 +
32064 +out:
32065 +       AuDbg("b%d\n", err);
32066 +       return err;
32067 +}
32068 +
32069 +/* ---------------------------------------------------------------------- */
32070 +
32071 +/* an exception for the policy other than tdp */
32072 +static int au_wbr_create_exp(struct dentry *dentry)
32073 +{
32074 +       int err;
32075 +       aufs_bindex_t bwh, bdiropq;
32076 +       struct dentry *parent;
32077 +
32078 +       err = -1;
32079 +       bwh = au_dbwh(dentry);
32080 +       parent = dget_parent(dentry);
32081 +       bdiropq = au_dbdiropq(parent);
32082 +       if (bwh >= 0) {
32083 +               if (bdiropq >= 0)
32084 +                       err = min(bdiropq, bwh);
32085 +               else
32086 +                       err = bwh;
32087 +               AuDbg("%d\n", err);
32088 +       } else if (bdiropq >= 0) {
32089 +               err = bdiropq;
32090 +               AuDbg("%d\n", err);
32091 +       }
32092 +       dput(parent);
32093 +
32094 +       if (err >= 0)
32095 +               err = au_wbr_nonopq(dentry, err);
32096 +
32097 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
32098 +               err = -1;
32099 +
32100 +       AuDbg("%d\n", err);
32101 +       return err;
32102 +}
32103 +
32104 +/* ---------------------------------------------------------------------- */
32105 +
32106 +/* round robin */
32107 +static int au_wbr_create_init_rr(struct super_block *sb)
32108 +{
32109 +       int err;
32110 +
32111 +       err = au_wbr_bu(sb, au_sbbot(sb));
32112 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
32113 +       /* smp_mb(); */
32114 +
32115 +       AuDbg("b%d\n", err);
32116 +       return err;
32117 +}
32118 +
32119 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
32120 +{
32121 +       int err, nbr;
32122 +       unsigned int u;
32123 +       aufs_bindex_t bindex, bbot;
32124 +       struct super_block *sb;
32125 +       atomic_t *next;
32126 +
32127 +       err = au_wbr_create_exp(dentry);
32128 +       if (err >= 0)
32129 +               goto out;
32130 +
32131 +       sb = dentry->d_sb;
32132 +       next = &au_sbi(sb)->si_wbr_rr_next;
32133 +       bbot = au_sbbot(sb);
32134 +       nbr = bbot + 1;
32135 +       for (bindex = 0; bindex <= bbot; bindex++) {
32136 +               if (!au_ftest_wbr(flags, DIR)) {
32137 +                       err = atomic_dec_return(next) + 1;
32138 +                       /* modulo for 0 is meaningless */
32139 +                       if (unlikely(!err))
32140 +                               err = atomic_dec_return(next) + 1;
32141 +               } else
32142 +                       err = atomic_read(next);
32143 +               AuDbg("%d\n", err);
32144 +               u = err;
32145 +               err = u % nbr;
32146 +               AuDbg("%d\n", err);
32147 +               if (!au_br_rdonly(au_sbr(sb, err)))
32148 +                       break;
32149 +               err = -EROFS;
32150 +       }
32151 +
32152 +       if (err >= 0)
32153 +               err = au_wbr_nonopq(dentry, err);
32154 +
32155 +out:
32156 +       AuDbg("%d\n", err);
32157 +       return err;
32158 +}
32159 +
32160 +/* ---------------------------------------------------------------------- */
32161 +
32162 +/* most free space */
32163 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
32164 +{
32165 +       struct super_block *sb;
32166 +       struct au_branch *br;
32167 +       struct au_wbr_mfs *mfs;
32168 +       struct dentry *h_parent;
32169 +       aufs_bindex_t bindex, bbot;
32170 +       int err;
32171 +       unsigned long long b, bavail;
32172 +       struct path h_path;
32173 +       /* reduce the stack usage */
32174 +       struct kstatfs *st;
32175 +
32176 +       st = kmalloc(sizeof(*st), GFP_NOFS);
32177 +       if (unlikely(!st)) {
32178 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
32179 +               return;
32180 +       }
32181 +
32182 +       bavail = 0;
32183 +       sb = dentry->d_sb;
32184 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32185 +       MtxMustLock(&mfs->mfs_lock);
32186 +       mfs->mfs_bindex = -EROFS;
32187 +       mfs->mfsrr_bytes = 0;
32188 +       if (!parent) {
32189 +               bindex = 0;
32190 +               bbot = au_sbbot(sb);
32191 +       } else {
32192 +               bindex = au_dbtop(parent);
32193 +               bbot = au_dbtaildir(parent);
32194 +       }
32195 +
32196 +       for (; bindex <= bbot; bindex++) {
32197 +               if (parent) {
32198 +                       h_parent = au_h_dptr(parent, bindex);
32199 +                       if (!h_parent || d_is_negative(h_parent))
32200 +                               continue;
32201 +               }
32202 +               br = au_sbr(sb, bindex);
32203 +               if (au_br_rdonly(br))
32204 +                       continue;
32205 +
32206 +               /* sb->s_root for NFS is unreliable */
32207 +               h_path.mnt = au_br_mnt(br);
32208 +               h_path.dentry = h_path.mnt->mnt_root;
32209 +               err = vfs_statfs(&h_path, st);
32210 +               if (unlikely(err)) {
32211 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
32212 +                       continue;
32213 +               }
32214 +
32215 +               /* when the available size is equal, select the lower one */
32216 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
32217 +                            || sizeof(b) < sizeof(st->f_bsize));
32218 +               b = st->f_bavail * st->f_bsize;
32219 +               br->br_wbr->wbr_bytes = b;
32220 +               if (b >= bavail) {
32221 +                       bavail = b;
32222 +                       mfs->mfs_bindex = bindex;
32223 +                       mfs->mfs_jiffy = jiffies;
32224 +               }
32225 +       }
32226 +
32227 +       mfs->mfsrr_bytes = bavail;
32228 +       AuDbg("b%d\n", mfs->mfs_bindex);
32229 +       au_delayed_kfree(st);
32230 +}
32231 +
32232 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
32233 +{
32234 +       int err;
32235 +       struct dentry *parent;
32236 +       struct super_block *sb;
32237 +       struct au_wbr_mfs *mfs;
32238 +
32239 +       err = au_wbr_create_exp(dentry);
32240 +       if (err >= 0)
32241 +               goto out;
32242 +
32243 +       sb = dentry->d_sb;
32244 +       parent = NULL;
32245 +       if (au_ftest_wbr(flags, PARENT))
32246 +               parent = dget_parent(dentry);
32247 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32248 +       mutex_lock(&mfs->mfs_lock);
32249 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
32250 +           || mfs->mfs_bindex < 0
32251 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
32252 +               au_mfs(dentry, parent);
32253 +       mutex_unlock(&mfs->mfs_lock);
32254 +       err = mfs->mfs_bindex;
32255 +       dput(parent);
32256 +
32257 +       if (err >= 0)
32258 +               err = au_wbr_nonopq(dentry, err);
32259 +
32260 +out:
32261 +       AuDbg("b%d\n", err);
32262 +       return err;
32263 +}
32264 +
32265 +static int au_wbr_create_init_mfs(struct super_block *sb)
32266 +{
32267 +       struct au_wbr_mfs *mfs;
32268 +
32269 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32270 +       mutex_init(&mfs->mfs_lock);
32271 +       mfs->mfs_jiffy = 0;
32272 +       mfs->mfs_bindex = -EROFS;
32273 +
32274 +       return 0;
32275 +}
32276 +
32277 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
32278 +{
32279 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
32280 +       return 0;
32281 +}
32282 +
32283 +/* ---------------------------------------------------------------------- */
32284 +
32285 +/* top down regardless parent, and then mfs */
32286 +static int au_wbr_create_tdmfs(struct dentry *dentry,
32287 +                              unsigned int flags __maybe_unused)
32288 +{
32289 +       int err;
32290 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
32291 +       unsigned long long watermark;
32292 +       struct super_block *sb;
32293 +       struct au_wbr_mfs *mfs;
32294 +       struct au_branch *br;
32295 +       struct dentry *parent;
32296 +
32297 +       sb = dentry->d_sb;
32298 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32299 +       mutex_lock(&mfs->mfs_lock);
32300 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
32301 +           || mfs->mfs_bindex < 0)
32302 +               au_mfs(dentry, /*parent*/NULL);
32303 +       watermark = mfs->mfsrr_watermark;
32304 +       bmfs = mfs->mfs_bindex;
32305 +       mutex_unlock(&mfs->mfs_lock);
32306 +
32307 +       /* another style of au_wbr_create_exp() */
32308 +       bwh = au_dbwh(dentry);
32309 +       parent = dget_parent(dentry);
32310 +       btail = au_dbtaildir(parent);
32311 +       if (bwh >= 0 && bwh < btail)
32312 +               btail = bwh;
32313 +
32314 +       err = au_wbr_nonopq(dentry, btail);
32315 +       if (unlikely(err < 0))
32316 +               goto out;
32317 +       btail = err;
32318 +       bfound = -1;
32319 +       for (bindex = 0; bindex <= btail; bindex++) {
32320 +               br = au_sbr(sb, bindex);
32321 +               if (au_br_rdonly(br))
32322 +                       continue;
32323 +               if (br->br_wbr->wbr_bytes > watermark) {
32324 +                       bfound = bindex;
32325 +                       break;
32326 +               }
32327 +       }
32328 +       err = bfound;
32329 +       if (err < 0)
32330 +               err = bmfs;
32331 +
32332 +out:
32333 +       dput(parent);
32334 +       AuDbg("b%d\n", err);
32335 +       return err;
32336 +}
32337 +
32338 +/* ---------------------------------------------------------------------- */
32339 +
32340 +/* most free space and then round robin */
32341 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
32342 +{
32343 +       int err;
32344 +       struct au_wbr_mfs *mfs;
32345 +
32346 +       err = au_wbr_create_mfs(dentry, flags);
32347 +       if (err >= 0) {
32348 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
32349 +               mutex_lock(&mfs->mfs_lock);
32350 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
32351 +                       err = au_wbr_create_rr(dentry, flags);
32352 +               mutex_unlock(&mfs->mfs_lock);
32353 +       }
32354 +
32355 +       AuDbg("b%d\n", err);
32356 +       return err;
32357 +}
32358 +
32359 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
32360 +{
32361 +       int err;
32362 +
32363 +       au_wbr_create_init_mfs(sb); /* ignore */
32364 +       err = au_wbr_create_init_rr(sb);
32365 +
32366 +       return err;
32367 +}
32368 +
32369 +/* ---------------------------------------------------------------------- */
32370 +
32371 +/* top down parent and most free space */
32372 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
32373 +{
32374 +       int err, e2;
32375 +       unsigned long long b;
32376 +       aufs_bindex_t bindex, btop, bbot;
32377 +       struct super_block *sb;
32378 +       struct dentry *parent, *h_parent;
32379 +       struct au_branch *br;
32380 +
32381 +       err = au_wbr_create_tdp(dentry, flags);
32382 +       if (unlikely(err < 0))
32383 +               goto out;
32384 +       parent = dget_parent(dentry);
32385 +       btop = au_dbtop(parent);
32386 +       bbot = au_dbtaildir(parent);
32387 +       if (btop == bbot)
32388 +               goto out_parent; /* success */
32389 +
32390 +       e2 = au_wbr_create_mfs(dentry, flags);
32391 +       if (e2 < 0)
32392 +               goto out_parent; /* success */
32393 +
32394 +       /* when the available size is equal, select upper one */
32395 +       sb = dentry->d_sb;
32396 +       br = au_sbr(sb, err);
32397 +       b = br->br_wbr->wbr_bytes;
32398 +       AuDbg("b%d, %llu\n", err, b);
32399 +
32400 +       for (bindex = btop; bindex <= bbot; bindex++) {
32401 +               h_parent = au_h_dptr(parent, bindex);
32402 +               if (!h_parent || d_is_negative(h_parent))
32403 +                       continue;
32404 +
32405 +               br = au_sbr(sb, bindex);
32406 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
32407 +                       b = br->br_wbr->wbr_bytes;
32408 +                       err = bindex;
32409 +                       AuDbg("b%d, %llu\n", err, b);
32410 +               }
32411 +       }
32412 +
32413 +       if (err >= 0)
32414 +               err = au_wbr_nonopq(dentry, err);
32415 +
32416 +out_parent:
32417 +       dput(parent);
32418 +out:
32419 +       AuDbg("b%d\n", err);
32420 +       return err;
32421 +}
32422 +
32423 +/* ---------------------------------------------------------------------- */
32424 +
32425 +/*
32426 + * - top down parent
32427 + * - most free space with parent
32428 + * - most free space round-robin regardless parent
32429 + */
32430 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
32431 +{
32432 +       int err;
32433 +       unsigned long long watermark;
32434 +       struct super_block *sb;
32435 +       struct au_branch *br;
32436 +       struct au_wbr_mfs *mfs;
32437 +
32438 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
32439 +       if (unlikely(err < 0))
32440 +               goto out;
32441 +
32442 +       sb = dentry->d_sb;
32443 +       br = au_sbr(sb, err);
32444 +       mfs = &au_sbi(sb)->si_wbr_mfs;
32445 +       mutex_lock(&mfs->mfs_lock);
32446 +       watermark = mfs->mfsrr_watermark;
32447 +       mutex_unlock(&mfs->mfs_lock);
32448 +       if (br->br_wbr->wbr_bytes < watermark)
32449 +               /* regardless the parent dir */
32450 +               err = au_wbr_create_mfsrr(dentry, flags);
32451 +
32452 +out:
32453 +       AuDbg("b%d\n", err);
32454 +       return err;
32455 +}
32456 +
32457 +/* ---------------------------------------------------------------------- */
32458 +
32459 +/* policies for copyup */
32460 +
32461 +/* top down parent */
32462 +static int au_wbr_copyup_tdp(struct dentry *dentry)
32463 +{
32464 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
32465 +}
32466 +
32467 +/* bottom up parent */
32468 +static int au_wbr_copyup_bup(struct dentry *dentry)
32469 +{
32470 +       int err;
32471 +       aufs_bindex_t bindex, btop;
32472 +       struct dentry *parent, *h_parent;
32473 +       struct super_block *sb;
32474 +
32475 +       err = -EROFS;
32476 +       sb = dentry->d_sb;
32477 +       parent = dget_parent(dentry);
32478 +       btop = au_dbtop(parent);
32479 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
32480 +               h_parent = au_h_dptr(parent, bindex);
32481 +               if (!h_parent || d_is_negative(h_parent))
32482 +                       continue;
32483 +
32484 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
32485 +                       err = bindex;
32486 +                       break;
32487 +               }
32488 +       }
32489 +       dput(parent);
32490 +
32491 +       /* bottom up here */
32492 +       if (unlikely(err < 0))
32493 +               err = au_wbr_bu(sb, btop - 1);
32494 +
32495 +       AuDbg("b%d\n", err);
32496 +       return err;
32497 +}
32498 +
32499 +/* bottom up */
32500 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
32501 +{
32502 +       int err;
32503 +
32504 +       err = au_wbr_bu(dentry->d_sb, btop);
32505 +       AuDbg("b%d\n", err);
32506 +       if (err > btop)
32507 +               err = au_wbr_nonopq(dentry, err);
32508 +
32509 +       AuDbg("b%d\n", err);
32510 +       return err;
32511 +}
32512 +
32513 +static int au_wbr_copyup_bu(struct dentry *dentry)
32514 +{
32515 +       int err;
32516 +       aufs_bindex_t btop;
32517 +
32518 +       btop = au_dbtop(dentry);
32519 +       err = au_wbr_do_copyup_bu(dentry, btop);
32520 +       return err;
32521 +}
32522 +
32523 +/* ---------------------------------------------------------------------- */
32524 +
32525 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
32526 +       [AuWbrCopyup_TDP] = {
32527 +               .copyup = au_wbr_copyup_tdp
32528 +       },
32529 +       [AuWbrCopyup_BUP] = {
32530 +               .copyup = au_wbr_copyup_bup
32531 +       },
32532 +       [AuWbrCopyup_BU] = {
32533 +               .copyup = au_wbr_copyup_bu
32534 +       }
32535 +};
32536 +
32537 +struct au_wbr_create_operations au_wbr_create_ops[] = {
32538 +       [AuWbrCreate_TDP] = {
32539 +               .create = au_wbr_create_tdp
32540 +       },
32541 +       [AuWbrCreate_RR] = {
32542 +               .create = au_wbr_create_rr,
32543 +               .init   = au_wbr_create_init_rr
32544 +       },
32545 +       [AuWbrCreate_MFS] = {
32546 +               .create = au_wbr_create_mfs,
32547 +               .init   = au_wbr_create_init_mfs,
32548 +               .fin    = au_wbr_create_fin_mfs
32549 +       },
32550 +       [AuWbrCreate_MFSV] = {
32551 +               .create = au_wbr_create_mfs,
32552 +               .init   = au_wbr_create_init_mfs,
32553 +               .fin    = au_wbr_create_fin_mfs
32554 +       },
32555 +       [AuWbrCreate_MFSRR] = {
32556 +               .create = au_wbr_create_mfsrr,
32557 +               .init   = au_wbr_create_init_mfsrr,
32558 +               .fin    = au_wbr_create_fin_mfs
32559 +       },
32560 +       [AuWbrCreate_MFSRRV] = {
32561 +               .create = au_wbr_create_mfsrr,
32562 +               .init   = au_wbr_create_init_mfsrr,
32563 +               .fin    = au_wbr_create_fin_mfs
32564 +       },
32565 +       [AuWbrCreate_TDMFS] = {
32566 +               .create = au_wbr_create_tdmfs,
32567 +               .init   = au_wbr_create_init_mfs,
32568 +               .fin    = au_wbr_create_fin_mfs
32569 +       },
32570 +       [AuWbrCreate_TDMFSV] = {
32571 +               .create = au_wbr_create_tdmfs,
32572 +               .init   = au_wbr_create_init_mfs,
32573 +               .fin    = au_wbr_create_fin_mfs
32574 +       },
32575 +       [AuWbrCreate_PMFS] = {
32576 +               .create = au_wbr_create_pmfs,
32577 +               .init   = au_wbr_create_init_mfs,
32578 +               .fin    = au_wbr_create_fin_mfs
32579 +       },
32580 +       [AuWbrCreate_PMFSV] = {
32581 +               .create = au_wbr_create_pmfs,
32582 +               .init   = au_wbr_create_init_mfs,
32583 +               .fin    = au_wbr_create_fin_mfs
32584 +       },
32585 +       [AuWbrCreate_PMFSRR] = {
32586 +               .create = au_wbr_create_pmfsrr,
32587 +               .init   = au_wbr_create_init_mfsrr,
32588 +               .fin    = au_wbr_create_fin_mfs
32589 +       },
32590 +       [AuWbrCreate_PMFSRRV] = {
32591 +               .create = au_wbr_create_pmfsrr,
32592 +               .init   = au_wbr_create_init_mfsrr,
32593 +               .fin    = au_wbr_create_fin_mfs
32594 +       }
32595 +};
32596 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
32597 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
32598 +++ linux/fs/aufs/whout.c       2017-03-02 14:24:13.870255912 +0100
32599 @@ -0,0 +1,1061 @@
32600 +/*
32601 + * Copyright (C) 2005-2017 Junjiro R. Okajima
32602 + *
32603 + * This program, aufs is free software; you can redistribute it and/or modify
32604 + * it under the terms of the GNU General Public License as published by
32605 + * the Free Software Foundation; either version 2 of the License, or
32606 + * (at your option) any later version.
32607 + *
32608 + * This program is distributed in the hope that it will be useful,
32609 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32610 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32611 + * GNU General Public License for more details.
32612 + *
32613 + * You should have received a copy of the GNU General Public License
32614 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32615 + */
32616 +
32617 +/*
32618 + * whiteout for logical deletion and opaque directory
32619 + */
32620 +
32621 +#include "aufs.h"
32622 +
32623 +#define WH_MASK                        S_IRUGO
32624 +
32625 +/*
32626 + * If a directory contains this file, then it is opaque.  We start with the
32627 + * .wh. flag so that it is blocked by lookup.
32628 + */
32629 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
32630 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
32631 +
32632 +/*
32633 + * generate whiteout name, which is NOT terminated by NULL.
32634 + * @name: original d_name.name
32635 + * @len: original d_name.len
32636 + * @wh: whiteout qstr
32637 + * returns zero when succeeds, otherwise error.
32638 + * succeeded value as wh->name should be freed by kfree().
32639 + */
32640 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
32641 +{
32642 +       char *p;
32643 +
32644 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
32645 +               return -ENAMETOOLONG;
32646 +
32647 +       wh->len = name->len + AUFS_WH_PFX_LEN;
32648 +       p = kmalloc(wh->len, GFP_NOFS);
32649 +       wh->name = p;
32650 +       if (p) {
32651 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32652 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
32653 +               /* smp_mb(); */
32654 +               return 0;
32655 +       }
32656 +       return -ENOMEM;
32657 +}
32658 +
32659 +/* ---------------------------------------------------------------------- */
32660 +
32661 +/*
32662 + * test if the @wh_name exists under @h_parent.
32663 + * @try_sio specifies the necessary of super-io.
32664 + */
32665 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
32666 +{
32667 +       int err;
32668 +       struct dentry *wh_dentry;
32669 +
32670 +       if (!try_sio)
32671 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
32672 +       else
32673 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
32674 +       err = PTR_ERR(wh_dentry);
32675 +       if (IS_ERR(wh_dentry)) {
32676 +               if (err == -ENAMETOOLONG)
32677 +                       err = 0;
32678 +               goto out;
32679 +       }
32680 +
32681 +       err = 0;
32682 +       if (d_is_negative(wh_dentry))
32683 +               goto out_wh; /* success */
32684 +
32685 +       err = 1;
32686 +       if (d_is_reg(wh_dentry))
32687 +               goto out_wh; /* success */
32688 +
32689 +       err = -EIO;
32690 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
32691 +               wh_dentry, d_inode(wh_dentry)->i_mode);
32692 +
32693 +out_wh:
32694 +       dput(wh_dentry);
32695 +out:
32696 +       return err;
32697 +}
32698 +
32699 +/*
32700 + * test if the @h_dentry sets opaque or not.
32701 + */
32702 +int au_diropq_test(struct dentry *h_dentry)
32703 +{
32704 +       int err;
32705 +       struct inode *h_dir;
32706 +
32707 +       h_dir = d_inode(h_dentry);
32708 +       err = au_wh_test(h_dentry, &diropq_name,
32709 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
32710 +       return err;
32711 +}
32712 +
32713 +/*
32714 + * returns a negative dentry whose name is unique and temporary.
32715 + */
32716 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32717 +                            struct qstr *prefix)
32718 +{
32719 +       struct dentry *dentry;
32720 +       int i;
32721 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
32722 +               *name, *p;
32723 +       /* strict atomic_t is unnecessary here */
32724 +       static unsigned short cnt;
32725 +       struct qstr qs;
32726 +
32727 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
32728 +
32729 +       name = defname;
32730 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
32731 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
32732 +               dentry = ERR_PTR(-ENAMETOOLONG);
32733 +               if (unlikely(qs.len > NAME_MAX))
32734 +                       goto out;
32735 +               dentry = ERR_PTR(-ENOMEM);
32736 +               name = kmalloc(qs.len + 1, GFP_NOFS);
32737 +               if (unlikely(!name))
32738 +                       goto out;
32739 +       }
32740 +
32741 +       /* doubly whiteout-ed */
32742 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
32743 +       p = name + AUFS_WH_PFX_LEN * 2;
32744 +       memcpy(p, prefix->name, prefix->len);
32745 +       p += prefix->len;
32746 +       *p++ = '.';
32747 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
32748 +
32749 +       qs.name = name;
32750 +       for (i = 0; i < 3; i++) {
32751 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
32752 +               dentry = au_sio_lkup_one(&qs, h_parent);
32753 +               if (IS_ERR(dentry) || d_is_negative(dentry))
32754 +                       goto out_name;
32755 +               dput(dentry);
32756 +       }
32757 +       /* pr_warn("could not get random name\n"); */
32758 +       dentry = ERR_PTR(-EEXIST);
32759 +       AuDbg("%.*s\n", AuLNPair(&qs));
32760 +       BUG();
32761 +
32762 +out_name:
32763 +       if (name != defname)
32764 +               au_delayed_kfree(name);
32765 +out:
32766 +       AuTraceErrPtr(dentry);
32767 +       return dentry;
32768 +}
32769 +
32770 +/*
32771 + * rename the @h_dentry on @br to the whiteouted temporary name.
32772 + */
32773 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
32774 +{
32775 +       int err;
32776 +       struct path h_path = {
32777 +               .mnt = au_br_mnt(br)
32778 +       };
32779 +       struct inode *h_dir, *delegated;
32780 +       struct dentry *h_parent;
32781 +
32782 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
32783 +       h_dir = d_inode(h_parent);
32784 +       IMustLock(h_dir);
32785 +
32786 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
32787 +       err = PTR_ERR(h_path.dentry);
32788 +       if (IS_ERR(h_path.dentry))
32789 +               goto out;
32790 +
32791 +       /* under the same dir, no need to lock_rename() */
32792 +       delegated = NULL;
32793 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
32794 +                          /*flags*/0);
32795 +       AuTraceErr(err);
32796 +       if (unlikely(err == -EWOULDBLOCK)) {
32797 +               pr_warn("cannot retry for NFSv4 delegation"
32798 +                       " for an internal rename\n");
32799 +               iput(delegated);
32800 +       }
32801 +       dput(h_path.dentry);
32802 +
32803 +out:
32804 +       AuTraceErr(err);
32805 +       return err;
32806 +}
32807 +
32808 +/* ---------------------------------------------------------------------- */
32809 +/*
32810 + * functions for removing a whiteout
32811 + */
32812 +
32813 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32814 +{
32815 +       int err, force;
32816 +       struct inode *delegated;
32817 +
32818 +       /*
32819 +        * forces superio when the dir has a sticky bit.
32820 +        * this may be a violation of unix fs semantics.
32821 +        */
32822 +       force = (h_dir->i_mode & S_ISVTX)
32823 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
32824 +       delegated = NULL;
32825 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
32826 +       if (unlikely(err == -EWOULDBLOCK)) {
32827 +               pr_warn("cannot retry for NFSv4 delegation"
32828 +                       " for an internal unlink\n");
32829 +               iput(delegated);
32830 +       }
32831 +       return err;
32832 +}
32833 +
32834 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32835 +                       struct dentry *dentry)
32836 +{
32837 +       int err;
32838 +
32839 +       err = do_unlink_wh(h_dir, h_path);
32840 +       if (!err && dentry)
32841 +               au_set_dbwh(dentry, -1);
32842 +
32843 +       return err;
32844 +}
32845 +
32846 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32847 +                         struct au_branch *br)
32848 +{
32849 +       int err;
32850 +       struct path h_path = {
32851 +               .mnt = au_br_mnt(br)
32852 +       };
32853 +
32854 +       err = 0;
32855 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
32856 +       if (IS_ERR(h_path.dentry))
32857 +               err = PTR_ERR(h_path.dentry);
32858 +       else {
32859 +               if (d_is_reg(h_path.dentry))
32860 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
32861 +               dput(h_path.dentry);
32862 +       }
32863 +
32864 +       return err;
32865 +}
32866 +
32867 +/* ---------------------------------------------------------------------- */
32868 +/*
32869 + * initialize/clean whiteout for a branch
32870 + */
32871 +
32872 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32873 +                       const int isdir)
32874 +{
32875 +       int err;
32876 +       struct inode *delegated;
32877 +
32878 +       if (d_is_negative(whpath->dentry))
32879 +               return;
32880 +
32881 +       if (isdir)
32882 +               err = vfsub_rmdir(h_dir, whpath);
32883 +       else {
32884 +               delegated = NULL;
32885 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32886 +               if (unlikely(err == -EWOULDBLOCK)) {
32887 +                       pr_warn("cannot retry for NFSv4 delegation"
32888 +                               " for an internal unlink\n");
32889 +                       iput(delegated);
32890 +               }
32891 +       }
32892 +       if (unlikely(err))
32893 +               pr_warn("failed removing %pd (%d), ignored.\n",
32894 +                       whpath->dentry, err);
32895 +}
32896 +
32897 +static int test_linkable(struct dentry *h_root)
32898 +{
32899 +       struct inode *h_dir = d_inode(h_root);
32900 +
32901 +       if (h_dir->i_op->link)
32902 +               return 0;
32903 +
32904 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32905 +              h_root, au_sbtype(h_root->d_sb));
32906 +       return -ENOSYS;
32907 +}
32908 +
32909 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32910 +static int au_whdir(struct inode *h_dir, struct path *path)
32911 +{
32912 +       int err;
32913 +
32914 +       err = -EEXIST;
32915 +       if (d_is_negative(path->dentry)) {
32916 +               int mode = S_IRWXU;
32917 +
32918 +               if (au_test_nfs(path->dentry->d_sb))
32919 +                       mode |= S_IXUGO;
32920 +               err = vfsub_mkdir(h_dir, path, mode);
32921 +       } else if (d_is_dir(path->dentry))
32922 +               err = 0;
32923 +       else
32924 +               pr_err("unknown %pd exists\n", path->dentry);
32925 +
32926 +       return err;
32927 +}
32928 +
32929 +struct au_wh_base {
32930 +       const struct qstr *name;
32931 +       struct dentry *dentry;
32932 +};
32933 +
32934 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32935 +                         struct path *h_path)
32936 +{
32937 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32938 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32939 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32940 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32941 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32942 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32943 +}
32944 +
32945 +/*
32946 + * returns tri-state,
32947 + * minus: error, caller should print the message
32948 + * zero: succuess
32949 + * plus: error, caller should NOT print the message
32950 + */
32951 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32952 +                               int do_plink, struct au_wh_base base[],
32953 +                               struct path *h_path)
32954 +{
32955 +       int err;
32956 +       struct inode *h_dir;
32957 +
32958 +       h_dir = d_inode(h_root);
32959 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32960 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32961 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32962 +       if (do_plink) {
32963 +               err = test_linkable(h_root);
32964 +               if (unlikely(err)) {
32965 +                       err = 1;
32966 +                       goto out;
32967 +               }
32968 +
32969 +               err = au_whdir(h_dir, h_path);
32970 +               if (unlikely(err))
32971 +                       goto out;
32972 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32973 +       } else
32974 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32975 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32976 +       err = au_whdir(h_dir, h_path);
32977 +       if (unlikely(err))
32978 +               goto out;
32979 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32980 +
32981 +out:
32982 +       return err;
32983 +}
32984 +
32985 +/*
32986 + * for the moment, aufs supports the branch filesystem which does not support
32987 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
32988 + * copyup failed. finally, such filesystem will not be used as the writable
32989 + * branch.
32990 + *
32991 + * returns tri-state, see above.
32992 + */
32993 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32994 +                        int do_plink, struct au_wh_base base[],
32995 +                        struct path *h_path)
32996 +{
32997 +       int err;
32998 +       struct inode *h_dir;
32999 +
33000 +       WbrWhMustWriteLock(wbr);
33001 +
33002 +       err = test_linkable(h_root);
33003 +       if (unlikely(err)) {
33004 +               err = 1;
33005 +               goto out;
33006 +       }
33007 +
33008 +       /*
33009 +        * todo: should this create be done in /sbin/mount.aufs helper?
33010 +        */
33011 +       err = -EEXIST;
33012 +       h_dir = d_inode(h_root);
33013 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
33014 +               h_path->dentry = base[AuBrWh_BASE].dentry;
33015 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
33016 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
33017 +               err = 0;
33018 +       else
33019 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
33020 +       if (unlikely(err))
33021 +               goto out;
33022 +
33023 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
33024 +       if (do_plink) {
33025 +               err = au_whdir(h_dir, h_path);
33026 +               if (unlikely(err))
33027 +                       goto out;
33028 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
33029 +       } else
33030 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
33031 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
33032 +
33033 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
33034 +       err = au_whdir(h_dir, h_path);
33035 +       if (unlikely(err))
33036 +               goto out;
33037 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
33038 +
33039 +out:
33040 +       return err;
33041 +}
33042 +
33043 +/*
33044 + * initialize the whiteout base file/dir for @br.
33045 + */
33046 +int au_wh_init(struct au_branch *br, struct super_block *sb)
33047 +{
33048 +       int err, i;
33049 +       const unsigned char do_plink
33050 +               = !!au_opt_test(au_mntflags(sb), PLINK);
33051 +       struct inode *h_dir;
33052 +       struct path path = br->br_path;
33053 +       struct dentry *h_root = path.dentry;
33054 +       struct au_wbr *wbr = br->br_wbr;
33055 +       static const struct qstr base_name[] = {
33056 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
33057 +                                         sizeof(AUFS_BASE_NAME) - 1),
33058 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
33059 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
33060 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
33061 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
33062 +       };
33063 +       struct au_wh_base base[] = {
33064 +               [AuBrWh_BASE] = {
33065 +                       .name   = base_name + AuBrWh_BASE,
33066 +                       .dentry = NULL
33067 +               },
33068 +               [AuBrWh_PLINK] = {
33069 +                       .name   = base_name + AuBrWh_PLINK,
33070 +                       .dentry = NULL
33071 +               },
33072 +               [AuBrWh_ORPH] = {
33073 +                       .name   = base_name + AuBrWh_ORPH,
33074 +                       .dentry = NULL
33075 +               }
33076 +       };
33077 +
33078 +       if (wbr)
33079 +               WbrWhMustWriteLock(wbr);
33080 +
33081 +       for (i = 0; i < AuBrWh_Last; i++) {
33082 +               /* doubly whiteouted */
33083 +               struct dentry *d;
33084 +
33085 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
33086 +               err = PTR_ERR(d);
33087 +               if (IS_ERR(d))
33088 +                       goto out;
33089 +
33090 +               base[i].dentry = d;
33091 +               AuDebugOn(wbr
33092 +                         && wbr->wbr_wh[i]
33093 +                         && wbr->wbr_wh[i] != base[i].dentry);
33094 +       }
33095 +
33096 +       if (wbr)
33097 +               for (i = 0; i < AuBrWh_Last; i++) {
33098 +                       dput(wbr->wbr_wh[i]);
33099 +                       wbr->wbr_wh[i] = NULL;
33100 +               }
33101 +
33102 +       err = 0;
33103 +       if (!au_br_writable(br->br_perm)) {
33104 +               h_dir = d_inode(h_root);
33105 +               au_wh_init_ro(h_dir, base, &path);
33106 +       } else if (!au_br_wh_linkable(br->br_perm)) {
33107 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
33108 +               if (err > 0)
33109 +                       goto out;
33110 +               else if (err)
33111 +                       goto out_err;
33112 +       } else {
33113 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
33114 +               if (err > 0)
33115 +                       goto out;
33116 +               else if (err)
33117 +                       goto out_err;
33118 +       }
33119 +       goto out; /* success */
33120 +
33121 +out_err:
33122 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
33123 +              err, h_root, au_sbtype(h_root->d_sb));
33124 +out:
33125 +       for (i = 0; i < AuBrWh_Last; i++)
33126 +               dput(base[i].dentry);
33127 +       return err;
33128 +}
33129 +
33130 +/* ---------------------------------------------------------------------- */
33131 +/*
33132 + * whiteouts are all hard-linked usually.
33133 + * when its link count reaches a ceiling, we create a new whiteout base
33134 + * asynchronously.
33135 + */
33136 +
33137 +struct reinit_br_wh {
33138 +       struct super_block *sb;
33139 +       struct au_branch *br;
33140 +};
33141 +
33142 +static void reinit_br_wh(void *arg)
33143 +{
33144 +       int err;
33145 +       aufs_bindex_t bindex;
33146 +       struct path h_path;
33147 +       struct reinit_br_wh *a = arg;
33148 +       struct au_wbr *wbr;
33149 +       struct inode *dir, *delegated;
33150 +       struct dentry *h_root;
33151 +       struct au_hinode *hdir;
33152 +
33153 +       err = 0;
33154 +       wbr = a->br->br_wbr;
33155 +       /* big aufs lock */
33156 +       si_noflush_write_lock(a->sb);
33157 +       if (!au_br_writable(a->br->br_perm))
33158 +               goto out;
33159 +       bindex = au_br_index(a->sb, a->br->br_id);
33160 +       if (unlikely(bindex < 0))
33161 +               goto out;
33162 +
33163 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
33164 +       dir = d_inode(a->sb->s_root);
33165 +       hdir = au_hi(dir, bindex);
33166 +       h_root = au_h_dptr(a->sb->s_root, bindex);
33167 +       AuDebugOn(h_root != au_br_dentry(a->br));
33168 +
33169 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
33170 +       wbr_wh_write_lock(wbr);
33171 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
33172 +                         h_root, a->br);
33173 +       if (!err) {
33174 +               h_path.dentry = wbr->wbr_whbase;
33175 +               h_path.mnt = au_br_mnt(a->br);
33176 +               delegated = NULL;
33177 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
33178 +                                  /*force*/0);
33179 +               if (unlikely(err == -EWOULDBLOCK)) {
33180 +                       pr_warn("cannot retry for NFSv4 delegation"
33181 +                               " for an internal unlink\n");
33182 +                       iput(delegated);
33183 +               }
33184 +       } else {
33185 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
33186 +               err = 0;
33187 +       }
33188 +       dput(wbr->wbr_whbase);
33189 +       wbr->wbr_whbase = NULL;
33190 +       if (!err)
33191 +               err = au_wh_init(a->br, a->sb);
33192 +       wbr_wh_write_unlock(wbr);
33193 +       au_hn_inode_unlock(hdir);
33194 +       di_read_unlock(a->sb->s_root, AuLock_IR);
33195 +       if (!err)
33196 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
33197 +
33198 +out:
33199 +       if (wbr)
33200 +               atomic_dec(&wbr->wbr_wh_running);
33201 +       au_br_put(a->br);
33202 +       si_write_unlock(a->sb);
33203 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
33204 +       au_delayed_kfree(arg);
33205 +       if (unlikely(err))
33206 +               AuIOErr("err %d\n", err);
33207 +}
33208 +
33209 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
33210 +{
33211 +       int do_dec, wkq_err;
33212 +       struct reinit_br_wh *arg;
33213 +
33214 +       do_dec = 1;
33215 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
33216 +               goto out;
33217 +
33218 +       /* ignore ENOMEM */
33219 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
33220 +       if (arg) {
33221 +               /*
33222 +                * dec(wh_running), kfree(arg) and dec(br_count)
33223 +                * in reinit function
33224 +                */
33225 +               arg->sb = sb;
33226 +               arg->br = br;
33227 +               au_br_get(br);
33228 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
33229 +               if (unlikely(wkq_err)) {
33230 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
33231 +                       au_br_put(br);
33232 +                       au_delayed_kfree(arg);
33233 +               }
33234 +               do_dec = 0;
33235 +       }
33236 +
33237 +out:
33238 +       if (do_dec)
33239 +               atomic_dec(&br->br_wbr->wbr_wh_running);
33240 +}
33241 +
33242 +/* ---------------------------------------------------------------------- */
33243 +
33244 +/*
33245 + * create the whiteout @wh.
33246 + */
33247 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
33248 +                            struct dentry *wh)
33249 +{
33250 +       int err;
33251 +       struct path h_path = {
33252 +               .dentry = wh
33253 +       };
33254 +       struct au_branch *br;
33255 +       struct au_wbr *wbr;
33256 +       struct dentry *h_parent;
33257 +       struct inode *h_dir, *delegated;
33258 +
33259 +       h_parent = wh->d_parent; /* dir inode is locked */
33260 +       h_dir = d_inode(h_parent);
33261 +       IMustLock(h_dir);
33262 +
33263 +       br = au_sbr(sb, bindex);
33264 +       h_path.mnt = au_br_mnt(br);
33265 +       wbr = br->br_wbr;
33266 +       wbr_wh_read_lock(wbr);
33267 +       if (wbr->wbr_whbase) {
33268 +               delegated = NULL;
33269 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
33270 +               if (unlikely(err == -EWOULDBLOCK)) {
33271 +                       pr_warn("cannot retry for NFSv4 delegation"
33272 +                               " for an internal link\n");
33273 +                       iput(delegated);
33274 +               }
33275 +               if (!err || err != -EMLINK)
33276 +                       goto out;
33277 +
33278 +               /* link count full. re-initialize br_whbase. */
33279 +               kick_reinit_br_wh(sb, br);
33280 +       }
33281 +
33282 +       /* return this error in this context */
33283 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
33284 +       if (!err)
33285 +               au_fhsm_wrote(sb, bindex, /*force*/0);
33286 +
33287 +out:
33288 +       wbr_wh_read_unlock(wbr);
33289 +       return err;
33290 +}
33291 +
33292 +/* ---------------------------------------------------------------------- */
33293 +
33294 +/*
33295 + * create or remove the diropq.
33296 + */
33297 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
33298 +                               unsigned int flags)
33299 +{
33300 +       struct dentry *opq_dentry, *h_dentry;
33301 +       struct super_block *sb;
33302 +       struct au_branch *br;
33303 +       int err;
33304 +
33305 +       sb = dentry->d_sb;
33306 +       br = au_sbr(sb, bindex);
33307 +       h_dentry = au_h_dptr(dentry, bindex);
33308 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
33309 +       if (IS_ERR(opq_dentry))
33310 +               goto out;
33311 +
33312 +       if (au_ftest_diropq(flags, CREATE)) {
33313 +               err = link_or_create_wh(sb, bindex, opq_dentry);
33314 +               if (!err) {
33315 +                       au_set_dbdiropq(dentry, bindex);
33316 +                       goto out; /* success */
33317 +               }
33318 +       } else {
33319 +               struct path tmp = {
33320 +                       .dentry = opq_dentry,
33321 +                       .mnt    = au_br_mnt(br)
33322 +               };
33323 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
33324 +               if (!err)
33325 +                       au_set_dbdiropq(dentry, -1);
33326 +       }
33327 +       dput(opq_dentry);
33328 +       opq_dentry = ERR_PTR(err);
33329 +
33330 +out:
33331 +       return opq_dentry;
33332 +}
33333 +
33334 +struct do_diropq_args {
33335 +       struct dentry **errp;
33336 +       struct dentry *dentry;
33337 +       aufs_bindex_t bindex;
33338 +       unsigned int flags;
33339 +};
33340 +
33341 +static void call_do_diropq(void *args)
33342 +{
33343 +       struct do_diropq_args *a = args;
33344 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
33345 +}
33346 +
33347 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33348 +                            unsigned int flags)
33349 +{
33350 +       struct dentry *diropq, *h_dentry;
33351 +
33352 +       h_dentry = au_h_dptr(dentry, bindex);
33353 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
33354 +               diropq = do_diropq(dentry, bindex, flags);
33355 +       else {
33356 +               int wkq_err;
33357 +               struct do_diropq_args args = {
33358 +                       .errp           = &diropq,
33359 +                       .dentry         = dentry,
33360 +                       .bindex         = bindex,
33361 +                       .flags          = flags
33362 +               };
33363 +
33364 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
33365 +               if (unlikely(wkq_err))
33366 +                       diropq = ERR_PTR(wkq_err);
33367 +       }
33368 +
33369 +       return diropq;
33370 +}
33371 +
33372 +/* ---------------------------------------------------------------------- */
33373 +
33374 +/*
33375 + * lookup whiteout dentry.
33376 + * @h_parent: lower parent dentry which must exist and be locked
33377 + * @base_name: name of dentry which will be whiteouted
33378 + * returns dentry for whiteout.
33379 + */
33380 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33381 +                         struct au_branch *br)
33382 +{
33383 +       int err;
33384 +       struct qstr wh_name;
33385 +       struct dentry *wh_dentry;
33386 +
33387 +       err = au_wh_name_alloc(&wh_name, base_name);
33388 +       wh_dentry = ERR_PTR(err);
33389 +       if (!err) {
33390 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
33391 +               au_delayed_kfree(wh_name.name);
33392 +       }
33393 +       return wh_dentry;
33394 +}
33395 +
33396 +/*
33397 + * link/create a whiteout for @dentry on @bindex.
33398 + */
33399 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33400 +                           struct dentry *h_parent)
33401 +{
33402 +       struct dentry *wh_dentry;
33403 +       struct super_block *sb;
33404 +       int err;
33405 +
33406 +       sb = dentry->d_sb;
33407 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
33408 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
33409 +               err = link_or_create_wh(sb, bindex, wh_dentry);
33410 +               if (!err) {
33411 +                       au_set_dbwh(dentry, bindex);
33412 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
33413 +               } else {
33414 +                       dput(wh_dentry);
33415 +                       wh_dentry = ERR_PTR(err);
33416 +               }
33417 +       }
33418 +
33419 +       return wh_dentry;
33420 +}
33421 +
33422 +/* ---------------------------------------------------------------------- */
33423 +
33424 +/* Delete all whiteouts in this directory on branch bindex. */
33425 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
33426 +                          aufs_bindex_t bindex, struct au_branch *br)
33427 +{
33428 +       int err;
33429 +       unsigned long ul, n;
33430 +       struct qstr wh_name;
33431 +       char *p;
33432 +       struct hlist_head *head;
33433 +       struct au_vdir_wh *pos;
33434 +       struct au_vdir_destr *str;
33435 +
33436 +       err = -ENOMEM;
33437 +       p = (void *)__get_free_page(GFP_NOFS);
33438 +       wh_name.name = p;
33439 +       if (unlikely(!wh_name.name))
33440 +               goto out;
33441 +
33442 +       err = 0;
33443 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
33444 +       p += AUFS_WH_PFX_LEN;
33445 +       n = whlist->nh_num;
33446 +       head = whlist->nh_head;
33447 +       for (ul = 0; !err && ul < n; ul++, head++) {
33448 +               hlist_for_each_entry(pos, head, wh_hash) {
33449 +                       if (pos->wh_bindex != bindex)
33450 +                               continue;
33451 +
33452 +                       str = &pos->wh_str;
33453 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
33454 +                               memcpy(p, str->name, str->len);
33455 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
33456 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
33457 +                               if (!err)
33458 +                                       continue;
33459 +                               break;
33460 +                       }
33461 +                       AuIOErr("whiteout name too long %.*s\n",
33462 +                               str->len, str->name);
33463 +                       err = -EIO;
33464 +                       break;
33465 +               }
33466 +       }
33467 +       au_delayed_free_page((unsigned long)wh_name.name);
33468 +
33469 +out:
33470 +       return err;
33471 +}
33472 +
33473 +struct del_wh_children_args {
33474 +       int *errp;
33475 +       struct dentry *h_dentry;
33476 +       struct au_nhash *whlist;
33477 +       aufs_bindex_t bindex;
33478 +       struct au_branch *br;
33479 +};
33480 +
33481 +static void call_del_wh_children(void *args)
33482 +{
33483 +       struct del_wh_children_args *a = args;
33484 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
33485 +}
33486 +
33487 +/* ---------------------------------------------------------------------- */
33488 +
33489 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
33490 +{
33491 +       struct au_whtmp_rmdir *whtmp;
33492 +       int err;
33493 +       unsigned int rdhash;
33494 +
33495 +       SiMustAnyLock(sb);
33496 +
33497 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
33498 +       if (unlikely(!whtmp)) {
33499 +               whtmp = ERR_PTR(-ENOMEM);
33500 +               goto out;
33501 +       }
33502 +
33503 +       /* no estimation for dir size */
33504 +       rdhash = au_sbi(sb)->si_rdhash;
33505 +       if (!rdhash)
33506 +               rdhash = AUFS_RDHASH_DEF;
33507 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
33508 +       if (unlikely(err)) {
33509 +               au_delayed_kfree(whtmp);
33510 +               whtmp = ERR_PTR(err);
33511 +       }
33512 +
33513 +out:
33514 +       return whtmp;
33515 +}
33516 +
33517 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
33518 +{
33519 +       if (whtmp->br)
33520 +               au_br_put(whtmp->br);
33521 +       dput(whtmp->wh_dentry);
33522 +       iput(whtmp->dir);
33523 +       au_nhash_wh_free(&whtmp->whlist);
33524 +       au_delayed_kfree(whtmp);
33525 +}
33526 +
33527 +/*
33528 + * rmdir the whiteouted temporary named dir @h_dentry.
33529 + * @whlist: whiteouted children.
33530 + */
33531 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33532 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
33533 +{
33534 +       int err;
33535 +       unsigned int h_nlink;
33536 +       struct path h_tmp;
33537 +       struct inode *wh_inode, *h_dir;
33538 +       struct au_branch *br;
33539 +
33540 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
33541 +       IMustLock(h_dir);
33542 +
33543 +       br = au_sbr(dir->i_sb, bindex);
33544 +       wh_inode = d_inode(wh_dentry);
33545 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
33546 +
33547 +       /*
33548 +        * someone else might change some whiteouts while we were sleeping.
33549 +        * it means this whlist may have an obsoleted entry.
33550 +        */
33551 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
33552 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
33553 +       else {
33554 +               int wkq_err;
33555 +               struct del_wh_children_args args = {
33556 +                       .errp           = &err,
33557 +                       .h_dentry       = wh_dentry,
33558 +                       .whlist         = whlist,
33559 +                       .bindex         = bindex,
33560 +                       .br             = br
33561 +               };
33562 +
33563 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
33564 +               if (unlikely(wkq_err))
33565 +                       err = wkq_err;
33566 +       }
33567 +       inode_unlock(wh_inode);
33568 +
33569 +       if (!err) {
33570 +               h_tmp.dentry = wh_dentry;
33571 +               h_tmp.mnt = au_br_mnt(br);
33572 +               h_nlink = h_dir->i_nlink;
33573 +               err = vfsub_rmdir(h_dir, &h_tmp);
33574 +               /* some fs doesn't change the parent nlink in some cases */
33575 +               h_nlink -= h_dir->i_nlink;
33576 +       }
33577 +
33578 +       if (!err) {
33579 +               if (au_ibtop(dir) == bindex) {
33580 +                       /* todo: dir->i_mutex is necessary */
33581 +                       au_cpup_attr_timesizes(dir);
33582 +                       if (h_nlink)
33583 +                               vfsub_drop_nlink(dir);
33584 +               }
33585 +               return 0; /* success */
33586 +       }
33587 +
33588 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
33589 +       return err;
33590 +}
33591 +
33592 +static void call_rmdir_whtmp(void *args)
33593 +{
33594 +       int err;
33595 +       aufs_bindex_t bindex;
33596 +       struct au_whtmp_rmdir *a = args;
33597 +       struct super_block *sb;
33598 +       struct dentry *h_parent;
33599 +       struct inode *h_dir;
33600 +       struct au_hinode *hdir;
33601 +
33602 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
33603 +       /* inode_lock(a->dir); */
33604 +       err = -EROFS;
33605 +       sb = a->dir->i_sb;
33606 +       si_read_lock(sb, !AuLock_FLUSH);
33607 +       if (!au_br_writable(a->br->br_perm))
33608 +               goto out;
33609 +       bindex = au_br_index(sb, a->br->br_id);
33610 +       if (unlikely(bindex < 0))
33611 +               goto out;
33612 +
33613 +       err = -EIO;
33614 +       ii_write_lock_parent(a->dir);
33615 +       h_parent = dget_parent(a->wh_dentry);
33616 +       h_dir = d_inode(h_parent);
33617 +       hdir = au_hi(a->dir, bindex);
33618 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
33619 +       if (unlikely(err))
33620 +               goto out_mnt;
33621 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
33622 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
33623 +                         a->br);
33624 +       if (!err)
33625 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
33626 +       au_hn_inode_unlock(hdir);
33627 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
33628 +
33629 +out_mnt:
33630 +       dput(h_parent);
33631 +       ii_write_unlock(a->dir);
33632 +out:
33633 +       /* inode_unlock(a->dir); */
33634 +       au_whtmp_rmdir_free(a);
33635 +       si_read_unlock(sb);
33636 +       au_nwt_done(&au_sbi(sb)->si_nowait);
33637 +       if (unlikely(err))
33638 +               AuIOErr("err %d\n", err);
33639 +}
33640 +
33641 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33642 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
33643 +{
33644 +       int wkq_err;
33645 +       struct super_block *sb;
33646 +
33647 +       IMustLock(dir);
33648 +
33649 +       /* all post-process will be done in do_rmdir_whtmp(). */
33650 +       sb = dir->i_sb;
33651 +       args->dir = au_igrab(dir);
33652 +       args->br = au_sbr(sb, bindex);
33653 +       au_br_get(args->br);
33654 +       args->wh_dentry = dget(wh_dentry);
33655 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
33656 +       if (unlikely(wkq_err)) {
33657 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
33658 +               au_whtmp_rmdir_free(args);
33659 +       }
33660 +}
33661 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
33662 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
33663 +++ linux/fs/aufs/whout.h       2017-03-02 14:24:13.870255912 +0100
33664 @@ -0,0 +1,85 @@
33665 +/*
33666 + * Copyright (C) 2005-2017 Junjiro R. Okajima
33667 + *
33668 + * This program, aufs is free software; you can redistribute it and/or modify
33669 + * it under the terms of the GNU General Public License as published by
33670 + * the Free Software Foundation; either version 2 of the License, or
33671 + * (at your option) any later version.
33672 + *
33673 + * This program is distributed in the hope that it will be useful,
33674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33676 + * GNU General Public License for more details.
33677 + *
33678 + * You should have received a copy of the GNU General Public License
33679 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33680 + */
33681 +
33682 +/*
33683 + * whiteout for logical deletion and opaque directory
33684 + */
33685 +
33686 +#ifndef __AUFS_WHOUT_H__
33687 +#define __AUFS_WHOUT_H__
33688 +
33689 +#ifdef __KERNEL__
33690 +
33691 +#include "dir.h"
33692 +
33693 +/* whout.c */
33694 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
33695 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
33696 +int au_diropq_test(struct dentry *h_dentry);
33697 +struct au_branch;
33698 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
33699 +                            struct qstr *prefix);
33700 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
33701 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
33702 +                       struct dentry *dentry);
33703 +int au_wh_init(struct au_branch *br, struct super_block *sb);
33704 +
33705 +/* diropq flags */
33706 +#define AuDiropq_CREATE        1
33707 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
33708 +#define au_fset_diropq(flags, name) \
33709 +       do { (flags) |= AuDiropq_##name; } while (0)
33710 +#define au_fclr_diropq(flags, name) \
33711 +       do { (flags) &= ~AuDiropq_##name; } while (0)
33712 +
33713 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33714 +                            unsigned int flags);
33715 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33716 +                         struct au_branch *br);
33717 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33718 +                           struct dentry *h_parent);
33719 +
33720 +/* real rmdir for the whiteout-ed dir */
33721 +struct au_whtmp_rmdir {
33722 +       struct inode *dir;
33723 +       struct au_branch *br;
33724 +       struct dentry *wh_dentry;
33725 +       struct au_nhash whlist;
33726 +};
33727 +
33728 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
33729 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
33730 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33731 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
33732 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33733 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
33734 +
33735 +/* ---------------------------------------------------------------------- */
33736 +
33737 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
33738 +                                             aufs_bindex_t bindex)
33739 +{
33740 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
33741 +}
33742 +
33743 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
33744 +{
33745 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
33746 +}
33747 +
33748 +#endif /* __KERNEL__ */
33749 +#endif /* __AUFS_WHOUT_H__ */
33750 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
33751 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
33752 +++ linux/fs/aufs/wkq.c 2017-03-02 14:24:13.870255912 +0100
33753 @@ -0,0 +1,213 @@
33754 +/*
33755 + * Copyright (C) 2005-2017 Junjiro R. Okajima
33756 + *
33757 + * This program, aufs is free software; you can redistribute it and/or modify
33758 + * it under the terms of the GNU General Public License as published by
33759 + * the Free Software Foundation; either version 2 of the License, or
33760 + * (at your option) any later version.
33761 + *
33762 + * This program is distributed in the hope that it will be useful,
33763 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33764 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33765 + * GNU General Public License for more details.
33766 + *
33767 + * You should have received a copy of the GNU General Public License
33768 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33769 + */
33770 +
33771 +/*
33772 + * workqueue for asynchronous/super-io operations
33773 + * todo: try new dredential scheme
33774 + */
33775 +
33776 +#include <linux/module.h>
33777 +#include "aufs.h"
33778 +
33779 +/* internal workqueue named AUFS_WKQ_NAME */
33780 +
33781 +static struct workqueue_struct *au_wkq;
33782 +
33783 +struct au_wkinfo {
33784 +       struct work_struct wk;
33785 +       struct kobject *kobj;
33786 +
33787 +       unsigned int flags; /* see wkq.h */
33788 +
33789 +       au_wkq_func_t func;
33790 +       void *args;
33791 +
33792 +       struct completion *comp;
33793 +};
33794 +
33795 +/* ---------------------------------------------------------------------- */
33796 +
33797 +static void wkq_func(struct work_struct *wk)
33798 +{
33799 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
33800 +
33801 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
33802 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
33803 +
33804 +       wkinfo->func(wkinfo->args);
33805 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
33806 +               complete(wkinfo->comp);
33807 +       else {
33808 +               kobject_put(wkinfo->kobj);
33809 +               module_put(THIS_MODULE); /* todo: ?? */
33810 +               au_delayed_kfree(wkinfo);
33811 +       }
33812 +}
33813 +
33814 +/*
33815 + * Since struct completion is large, try allocating it dynamically.
33816 + */
33817 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
33818 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
33819 +
33820 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33821 +{
33822 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33823 +       if (*comp) {
33824 +               init_completion(*comp);
33825 +               wkinfo->comp = *comp;
33826 +               return 0;
33827 +       }
33828 +       return -ENOMEM;
33829 +}
33830 +
33831 +static void au_wkq_comp_free(struct completion *comp)
33832 +{
33833 +       au_delayed_kfree(comp);
33834 +}
33835 +
33836 +#else
33837 +
33838 +/* no braces */
33839 +#define AuWkqCompDeclare(name) \
33840 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
33841 +       struct completion *comp = &_ ## name
33842 +
33843 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33844 +{
33845 +       wkinfo->comp = *comp;
33846 +       return 0;
33847 +}
33848 +
33849 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33850 +{
33851 +       /* empty */
33852 +}
33853 +#endif /* 4KSTACKS */
33854 +
33855 +static void au_wkq_run(struct au_wkinfo *wkinfo)
33856 +{
33857 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
33858 +               if (au_wkq_test()) {
33859 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33860 +                               " due to a dead dir by UDBA?\n");
33861 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33862 +               }
33863 +       } else
33864 +               au_dbg_verify_kthread();
33865 +
33866 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
33867 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
33868 +               queue_work(au_wkq, &wkinfo->wk);
33869 +       } else {
33870 +               INIT_WORK(&wkinfo->wk, wkq_func);
33871 +               schedule_work(&wkinfo->wk);
33872 +       }
33873 +}
33874 +
33875 +/*
33876 + * Be careful. It is easy to make deadlock happen.
33877 + * processA: lock, wkq and wait
33878 + * processB: wkq and wait, lock in wkq
33879 + * --> deadlock
33880 + */
33881 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
33882 +{
33883 +       int err;
33884 +       AuWkqCompDeclare(comp);
33885 +       struct au_wkinfo wkinfo = {
33886 +               .flags  = flags,
33887 +               .func   = func,
33888 +               .args   = args
33889 +       };
33890 +
33891 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
33892 +       if (!err) {
33893 +               au_wkq_run(&wkinfo);
33894 +               /* no timeout, no interrupt */
33895 +               wait_for_completion(wkinfo.comp);
33896 +               au_wkq_comp_free(comp);
33897 +               destroy_work_on_stack(&wkinfo.wk);
33898 +       }
33899 +
33900 +       return err;
33901 +
33902 +}
33903 +
33904 +/*
33905 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33906 + * problem in a concurrent umounting.
33907 + */
33908 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33909 +                 unsigned int flags)
33910 +{
33911 +       int err;
33912 +       struct au_wkinfo *wkinfo;
33913 +
33914 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
33915 +
33916 +       /*
33917 +        * wkq_func() must free this wkinfo.
33918 +        * it highly depends upon the implementation of workqueue.
33919 +        */
33920 +       err = 0;
33921 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33922 +       if (wkinfo) {
33923 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
33924 +               wkinfo->flags = flags & ~AuWkq_WAIT;
33925 +               wkinfo->func = func;
33926 +               wkinfo->args = args;
33927 +               wkinfo->comp = NULL;
33928 +               kobject_get(wkinfo->kobj);
33929 +               __module_get(THIS_MODULE); /* todo: ?? */
33930 +
33931 +               au_wkq_run(wkinfo);
33932 +       } else {
33933 +               err = -ENOMEM;
33934 +               au_nwt_done(&au_sbi(sb)->si_nowait);
33935 +       }
33936 +
33937 +       return err;
33938 +}
33939 +
33940 +/* ---------------------------------------------------------------------- */
33941 +
33942 +void au_nwt_init(struct au_nowait_tasks *nwt)
33943 +{
33944 +       atomic_set(&nwt->nw_len, 0);
33945 +       /* smp_mb(); */ /* atomic_set */
33946 +       init_waitqueue_head(&nwt->nw_wq);
33947 +}
33948 +
33949 +void au_wkq_fin(void)
33950 +{
33951 +       destroy_workqueue(au_wkq);
33952 +}
33953 +
33954 +int __init au_wkq_init(void)
33955 +{
33956 +       int err;
33957 +
33958 +       err = 0;
33959 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
33960 +       if (IS_ERR(au_wkq))
33961 +               err = PTR_ERR(au_wkq);
33962 +       else if (!au_wkq)
33963 +               err = -ENOMEM;
33964 +
33965 +       return err;
33966 +}
33967 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33968 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
33969 +++ linux/fs/aufs/wkq.h 2017-03-02 14:24:13.870255912 +0100
33970 @@ -0,0 +1,93 @@
33971 +/*
33972 + * Copyright (C) 2005-2017 Junjiro R. Okajima
33973 + *
33974 + * This program, aufs is free software; you can redistribute it and/or modify
33975 + * it under the terms of the GNU General Public License as published by
33976 + * the Free Software Foundation; either version 2 of the License, or
33977 + * (at your option) any later version.
33978 + *
33979 + * This program is distributed in the hope that it will be useful,
33980 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33981 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33982 + * GNU General Public License for more details.
33983 + *
33984 + * You should have received a copy of the GNU General Public License
33985 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33986 + */
33987 +
33988 +/*
33989 + * workqueue for asynchronous/super-io operations
33990 + * todo: try new credentials management scheme
33991 + */
33992 +
33993 +#ifndef __AUFS_WKQ_H__
33994 +#define __AUFS_WKQ_H__
33995 +
33996 +#ifdef __KERNEL__
33997 +
33998 +#include <linux/percpu_counter.h>
33999 +
34000 +struct super_block;
34001 +
34002 +/* ---------------------------------------------------------------------- */
34003 +
34004 +/*
34005 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
34006 + */
34007 +struct au_nowait_tasks {
34008 +       atomic_t                nw_len;
34009 +       wait_queue_head_t       nw_wq;
34010 +};
34011 +
34012 +/* ---------------------------------------------------------------------- */
34013 +
34014 +typedef void (*au_wkq_func_t)(void *args);
34015 +
34016 +/* wkq flags */
34017 +#define AuWkq_WAIT     1
34018 +#define AuWkq_NEST     (1 << 1)
34019 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
34020 +#define au_fset_wkq(flags, name) \
34021 +       do { (flags) |= AuWkq_##name; } while (0)
34022 +#define au_fclr_wkq(flags, name) \
34023 +       do { (flags) &= ~AuWkq_##name; } while (0)
34024 +
34025 +#ifndef CONFIG_AUFS_HNOTIFY
34026 +#undef AuWkq_NEST
34027 +#define AuWkq_NEST     0
34028 +#endif
34029 +
34030 +/* wkq.c */
34031 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
34032 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
34033 +                 unsigned int flags);
34034 +void au_nwt_init(struct au_nowait_tasks *nwt);
34035 +int __init au_wkq_init(void);
34036 +void au_wkq_fin(void);
34037 +
34038 +/* ---------------------------------------------------------------------- */
34039 +
34040 +static inline int au_wkq_test(void)
34041 +{
34042 +       return current->flags & PF_WQ_WORKER;
34043 +}
34044 +
34045 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
34046 +{
34047 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
34048 +}
34049 +
34050 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
34051 +{
34052 +       if (atomic_dec_and_test(&nwt->nw_len))
34053 +               wake_up_all(&nwt->nw_wq);
34054 +}
34055 +
34056 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
34057 +{
34058 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
34059 +       return 0;
34060 +}
34061 +
34062 +#endif /* __KERNEL__ */
34063 +#endif /* __AUFS_WKQ_H__ */
34064 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
34065 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
34066 +++ linux/fs/aufs/xattr.c       2017-03-02 14:24:13.870255912 +0100
34067 @@ -0,0 +1,357 @@
34068 +/*
34069 + * Copyright (C) 2014-2017 Junjiro R. Okajima
34070 + *
34071 + * This program, aufs is free software; you can redistribute it and/or modify
34072 + * it under the terms of the GNU General Public License as published by
34073 + * the Free Software Foundation; either version 2 of the License, or
34074 + * (at your option) any later version.
34075 + *
34076 + * This program is distributed in the hope that it will be useful,
34077 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34078 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34079 + * GNU General Public License for more details.
34080 + *
34081 + * You should have received a copy of the GNU General Public License
34082 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34083 + */
34084 +
34085 +/*
34086 + * handling xattr functions
34087 + */
34088 +
34089 +#include <linux/fs.h>
34090 +#include <linux/posix_acl_xattr.h>
34091 +#include <linux/xattr.h>
34092 +#include "aufs.h"
34093 +
34094 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
34095 +{
34096 +       if (!ignore_flags)
34097 +               goto out;
34098 +       switch (err) {
34099 +       case -ENOMEM:
34100 +       case -EDQUOT:
34101 +               goto out;
34102 +       }
34103 +
34104 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
34105 +               err = 0;
34106 +               goto out;
34107 +       }
34108 +
34109 +#define cmp(brattr, prefix) do {                                       \
34110 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
34111 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
34112 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
34113 +                               err = 0;                                \
34114 +                       goto out;                                       \
34115 +               }                                                       \
34116 +       } while (0)
34117 +
34118 +       cmp(SEC, SECURITY);
34119 +       cmp(SYS, SYSTEM);
34120 +       cmp(TR, TRUSTED);
34121 +       cmp(USR, USER);
34122 +#undef cmp
34123 +
34124 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
34125 +               err = 0;
34126 +
34127 +out:
34128 +       return err;
34129 +}
34130 +
34131 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
34132 +
34133 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
34134 +                           char *name, char **buf, unsigned int ignore_flags,
34135 +                           unsigned int verbose)
34136 +{
34137 +       int err;
34138 +       ssize_t ssz;
34139 +       struct inode *h_idst;
34140 +
34141 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
34142 +       err = ssz;
34143 +       if (unlikely(err <= 0)) {
34144 +               if (err == -ENODATA
34145 +                   || (err == -EOPNOTSUPP
34146 +                       && ((ignore_flags & au_xattr_out_of_list)
34147 +                           || (au_test_nfs_noacl(d_inode(h_src))
34148 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
34149 +                                   || !strcmp(name,
34150 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
34151 +                           ))
34152 +                       err = 0;
34153 +               if (err && (verbose || au_debug_test()))
34154 +                       pr_err("%s, err %d\n", name, err);
34155 +               goto out;
34156 +       }
34157 +
34158 +       /* unlock it temporary */
34159 +       h_idst = d_inode(h_dst);
34160 +       inode_unlock(h_idst);
34161 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
34162 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
34163 +       if (unlikely(err)) {
34164 +               if (verbose || au_debug_test())
34165 +                       pr_err("%s, err %d\n", name, err);
34166 +               err = au_xattr_ignore(err, name, ignore_flags);
34167 +       }
34168 +
34169 +out:
34170 +       return err;
34171 +}
34172 +
34173 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
34174 +                 unsigned int verbose)
34175 +{
34176 +       int err, unlocked, acl_access, acl_default;
34177 +       ssize_t ssz;
34178 +       struct inode *h_isrc, *h_idst;
34179 +       char *value, *p, *o, *e;
34180 +
34181 +       /* try stopping to update the source inode while we are referencing */
34182 +       /* there should not be the parent-child relationship between them */
34183 +       h_isrc = d_inode(h_src);
34184 +       h_idst = d_inode(h_dst);
34185 +       inode_unlock(h_idst);
34186 +       inode_lock_nested(h_isrc, AuLsc_I_CHILD);
34187 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
34188 +       unlocked = 0;
34189 +
34190 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
34191 +       ssz = vfs_listxattr(h_src, NULL, 0);
34192 +       err = ssz;
34193 +       if (unlikely(err < 0)) {
34194 +               AuTraceErr(err);
34195 +               if (err == -ENODATA
34196 +                   || err == -EOPNOTSUPP)
34197 +                       err = 0;        /* ignore */
34198 +               goto out;
34199 +       }
34200 +
34201 +       err = 0;
34202 +       p = NULL;
34203 +       o = NULL;
34204 +       if (ssz) {
34205 +               err = -ENOMEM;
34206 +               p = kmalloc(ssz, GFP_NOFS);
34207 +               o = p;
34208 +               if (unlikely(!p))
34209 +                       goto out;
34210 +               err = vfs_listxattr(h_src, p, ssz);
34211 +       }
34212 +       inode_unlock(h_isrc);
34213 +       unlocked = 1;
34214 +       AuDbg("err %d, ssz %zd\n", err, ssz);
34215 +       if (unlikely(err < 0))
34216 +               goto out_free;
34217 +
34218 +       err = 0;
34219 +       e = p + ssz;
34220 +       value = NULL;
34221 +       acl_access = 0;
34222 +       acl_default = 0;
34223 +       while (!err && p < e) {
34224 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
34225 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
34226 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
34227 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
34228 +                                       - 1);
34229 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
34230 +                                      verbose);
34231 +               p += strlen(p) + 1;
34232 +       }
34233 +       AuTraceErr(err);
34234 +       ignore_flags |= au_xattr_out_of_list;
34235 +       if (!err && !acl_access) {
34236 +               err = au_do_cpup_xattr(h_dst, h_src,
34237 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
34238 +                                      ignore_flags, verbose);
34239 +               AuTraceErr(err);
34240 +       }
34241 +       if (!err && !acl_default) {
34242 +               err = au_do_cpup_xattr(h_dst, h_src,
34243 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
34244 +                                      ignore_flags, verbose);
34245 +               AuTraceErr(err);
34246 +       }
34247 +
34248 +       if (value)
34249 +               au_delayed_kfree(value);
34250 +
34251 +out_free:
34252 +       if (o)
34253 +               au_delayed_kfree(o);
34254 +out:
34255 +       if (!unlocked)
34256 +               inode_unlock(h_isrc);
34257 +       AuTraceErr(err);
34258 +       return err;
34259 +}
34260 +
34261 +/* ---------------------------------------------------------------------- */
34262 +
34263 +static int au_smack_reentering(struct super_block *sb)
34264 +{
34265 +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
34266 +       /*
34267 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
34268 +        * i_op->getxattr(). ouch.
34269 +        */
34270 +       return si_pid_test(sb);
34271 +#else
34272 +       return 0;
34273 +#endif
34274 +}
34275 +
34276 +enum {
34277 +       AU_XATTR_LIST,
34278 +       AU_XATTR_GET
34279 +};
34280 +
34281 +struct au_lgxattr {
34282 +       int type;
34283 +       union {
34284 +               struct {
34285 +                       char    *list;
34286 +                       size_t  size;
34287 +               } list;
34288 +               struct {
34289 +                       const char      *name;
34290 +                       void            *value;
34291 +                       size_t          size;
34292 +               } get;
34293 +       } u;
34294 +};
34295 +
34296 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
34297 +{
34298 +       ssize_t err;
34299 +       int reenter;
34300 +       struct path h_path;
34301 +       struct super_block *sb;
34302 +
34303 +       sb = dentry->d_sb;
34304 +       reenter = au_smack_reentering(sb);
34305 +       if (!reenter) {
34306 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
34307 +               if (unlikely(err))
34308 +                       goto out;
34309 +       }
34310 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
34311 +       if (unlikely(err))
34312 +               goto out_si;
34313 +       if (unlikely(!h_path.dentry))
34314 +               /* illegally overlapped or something */
34315 +               goto out_di; /* pretending success */
34316 +
34317 +       /* always topmost entry only */
34318 +       switch (arg->type) {
34319 +       case AU_XATTR_LIST:
34320 +               err = vfs_listxattr(h_path.dentry,
34321 +                                   arg->u.list.list, arg->u.list.size);
34322 +               break;
34323 +       case AU_XATTR_GET:
34324 +               AuDebugOn(d_is_negative(h_path.dentry));
34325 +               err = vfs_getxattr(h_path.dentry,
34326 +                                  arg->u.get.name, arg->u.get.value,
34327 +                                  arg->u.get.size);
34328 +               break;
34329 +       }
34330 +
34331 +out_di:
34332 +       if (!reenter)
34333 +               di_read_unlock(dentry, AuLock_IR);
34334 +out_si:
34335 +       if (!reenter)
34336 +               si_read_unlock(sb);
34337 +out:
34338 +       AuTraceErr(err);
34339 +       return err;
34340 +}
34341 +
34342 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
34343 +{
34344 +       struct au_lgxattr arg = {
34345 +               .type = AU_XATTR_LIST,
34346 +               .u.list = {
34347 +                       .list   = list,
34348 +                       .size   = size
34349 +               },
34350 +       };
34351 +
34352 +       return au_lgxattr(dentry, &arg);
34353 +}
34354 +
34355 +static ssize_t au_getxattr(struct dentry *dentry,
34356 +                          struct inode *inode __maybe_unused,
34357 +                          const char *name, void *value, size_t size)
34358 +{
34359 +       struct au_lgxattr arg = {
34360 +               .type = AU_XATTR_GET,
34361 +               .u.get = {
34362 +                       .name   = name,
34363 +                       .value  = value,
34364 +                       .size   = size
34365 +               },
34366 +       };
34367 +
34368 +       return au_lgxattr(dentry, &arg);
34369 +}
34370 +
34371 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
34372 +                      const char *name, const void *value, size_t size,
34373 +                      int flags)
34374 +{
34375 +       struct au_sxattr arg = {
34376 +               .type = AU_XATTR_SET,
34377 +               .u.set = {
34378 +                       .name   = name,
34379 +                       .value  = value,
34380 +                       .size   = size,
34381 +                       .flags  = flags
34382 +               },
34383 +       };
34384 +
34385 +       return au_sxattr(dentry, inode, &arg);
34386 +}
34387 +
34388 +/* ---------------------------------------------------------------------- */
34389 +
34390 +static int au_xattr_get(const struct xattr_handler *handler,
34391 +                       struct dentry *dentry, struct inode *inode,
34392 +                       const char *name, void *buffer, size_t size)
34393 +{
34394 +       return au_getxattr(dentry, inode, name, buffer, size);
34395 +}
34396 +
34397 +static int au_xattr_set(const struct xattr_handler *handler,
34398 +                       struct dentry *dentry, struct inode *inode,
34399 +                       const char *name, const void *value, size_t size,
34400 +                       int flags)
34401 +{
34402 +       return au_setxattr(dentry, inode, name, value, size, flags);
34403 +}
34404 +
34405 +static const struct xattr_handler au_xattr_handler = {
34406 +       .name   = "",
34407 +       .prefix = "",
34408 +       .get    = au_xattr_get,
34409 +       .set    = au_xattr_set
34410 +};
34411 +
34412 +static const struct xattr_handler *au_xattr_handlers[] = {
34413 +#ifdef CONFIG_FS_POSIX_ACL
34414 +       &posix_acl_access_xattr_handler,
34415 +       &posix_acl_default_xattr_handler,
34416 +#endif
34417 +       &au_xattr_handler, /* must be last */
34418 +       NULL
34419 +};
34420 +
34421 +void au_xattr_init(struct super_block *sb)
34422 +{
34423 +       sb->s_xattr = au_xattr_handlers;
34424 +}
34425 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
34426 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
34427 +++ linux/fs/aufs/xino.c        2017-03-02 14:24:13.870255912 +0100
34428 @@ -0,0 +1,1318 @@
34429 +/*
34430 + * Copyright (C) 2005-2017 Junjiro R. Okajima
34431 + *
34432 + * This program, aufs is free software; you can redistribute it and/or modify
34433 + * it under the terms of the GNU General Public License as published by
34434 + * the Free Software Foundation; either version 2 of the License, or
34435 + * (at your option) any later version.
34436 + *
34437 + * This program is distributed in the hope that it will be useful,
34438 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34439 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34440 + * GNU General Public License for more details.
34441 + *
34442 + * You should have received a copy of the GNU General Public License
34443 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34444 + */
34445 +
34446 +/*
34447 + * external inode number translation table and bitmap
34448 + */
34449 +
34450 +#include <linux/seq_file.h>
34451 +#include <linux/statfs.h>
34452 +#include "aufs.h"
34453 +
34454 +/* todo: unnecessary to support mmap_sem since kernel-space? */
34455 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
34456 +                  loff_t *pos)
34457 +{
34458 +       ssize_t err;
34459 +       mm_segment_t oldfs;
34460 +       union {
34461 +               void *k;
34462 +               char __user *u;
34463 +       } buf;
34464 +
34465 +       buf.k = kbuf;
34466 +       oldfs = get_fs();
34467 +       set_fs(KERNEL_DS);
34468 +       do {
34469 +               /* todo: signal_pending? */
34470 +               err = func(file, buf.u, size, pos);
34471 +       } while (err == -EAGAIN || err == -EINTR);
34472 +       set_fs(oldfs);
34473 +
34474 +#if 0 /* reserved for future use */
34475 +       if (err > 0)
34476 +               fsnotify_access(file->f_path.dentry);
34477 +#endif
34478 +
34479 +       return err;
34480 +}
34481 +
34482 +/* ---------------------------------------------------------------------- */
34483 +
34484 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34485 +                              size_t size, loff_t *pos);
34486 +
34487 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
34488 +                             size_t size, loff_t *pos)
34489 +{
34490 +       ssize_t err;
34491 +       mm_segment_t oldfs;
34492 +       union {
34493 +               void *k;
34494 +               const char __user *u;
34495 +       } buf;
34496 +       int i;
34497 +       const int prevent_endless = 10;
34498 +
34499 +       i = 0;
34500 +       buf.k = kbuf;
34501 +       oldfs = get_fs();
34502 +       set_fs(KERNEL_DS);
34503 +       do {
34504 +               err = func(file, buf.u, size, pos);
34505 +               if (err == -EINTR
34506 +                   && !au_wkq_test()
34507 +                   && fatal_signal_pending(current)) {
34508 +                       set_fs(oldfs);
34509 +                       err = xino_fwrite_wkq(func, file, kbuf, size, pos);
34510 +                       BUG_ON(err == -EINTR);
34511 +                       oldfs = get_fs();
34512 +                       set_fs(KERNEL_DS);
34513 +               }
34514 +       } while (i++ < prevent_endless
34515 +                && (err == -EAGAIN || err == -EINTR));
34516 +       set_fs(oldfs);
34517 +
34518 +#if 0 /* reserved for future use */
34519 +       if (err > 0)
34520 +               fsnotify_modify(file->f_path.dentry);
34521 +#endif
34522 +
34523 +       return err;
34524 +}
34525 +
34526 +struct do_xino_fwrite_args {
34527 +       ssize_t *errp;
34528 +       vfs_writef_t func;
34529 +       struct file *file;
34530 +       void *buf;
34531 +       size_t size;
34532 +       loff_t *pos;
34533 +};
34534 +
34535 +static void call_do_xino_fwrite(void *args)
34536 +{
34537 +       struct do_xino_fwrite_args *a = args;
34538 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
34539 +}
34540 +
34541 +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34542 +                              size_t size, loff_t *pos)
34543 +{
34544 +       ssize_t err;
34545 +       int wkq_err;
34546 +       struct do_xino_fwrite_args args = {
34547 +               .errp   = &err,
34548 +               .func   = func,
34549 +               .file   = file,
34550 +               .buf    = buf,
34551 +               .size   = size,
34552 +               .pos    = pos
34553 +       };
34554 +
34555 +       /*
34556 +        * it breaks RLIMIT_FSIZE and normal user's limit,
34557 +        * users should care about quota and real 'filesystem full.'
34558 +        */
34559 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
34560 +       if (unlikely(wkq_err))
34561 +               err = wkq_err;
34562 +
34563 +       return err;
34564 +}
34565 +
34566 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
34567 +                   size_t size, loff_t *pos)
34568 +{
34569 +       ssize_t err;
34570 +
34571 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
34572 +               lockdep_off();
34573 +               err = do_xino_fwrite(func, file, buf, size, pos);
34574 +               lockdep_on();
34575 +       } else
34576 +               err = xino_fwrite_wkq(func, file, buf, size, pos);
34577 +
34578 +       return err;
34579 +}
34580 +
34581 +/* ---------------------------------------------------------------------- */
34582 +
34583 +/*
34584 + * create a new xinofile at the same place/path as @base_file.
34585 + */
34586 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
34587 +{
34588 +       struct file *file;
34589 +       struct dentry *base, *parent;
34590 +       struct inode *dir, *delegated;
34591 +       struct qstr *name;
34592 +       struct path path;
34593 +       int err;
34594 +
34595 +       base = base_file->f_path.dentry;
34596 +       parent = base->d_parent; /* dir inode is locked */
34597 +       dir = d_inode(parent);
34598 +       IMustLock(dir);
34599 +
34600 +       file = ERR_PTR(-EINVAL);
34601 +       name = &base->d_name;
34602 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
34603 +       if (IS_ERR(path.dentry)) {
34604 +               file = (void *)path.dentry;
34605 +               pr_err("%pd lookup err %ld\n",
34606 +                      base, PTR_ERR(path.dentry));
34607 +               goto out;
34608 +       }
34609 +
34610 +       /* no need to mnt_want_write() since we call dentry_open() later */
34611 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
34612 +       if (unlikely(err)) {
34613 +               file = ERR_PTR(err);
34614 +               pr_err("%pd create err %d\n", base, err);
34615 +               goto out_dput;
34616 +       }
34617 +
34618 +       path.mnt = base_file->f_path.mnt;
34619 +       file = vfsub_dentry_open(&path,
34620 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34621 +                                /* | __FMODE_NONOTIFY */);
34622 +       if (IS_ERR(file)) {
34623 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
34624 +               goto out_dput;
34625 +       }
34626 +
34627 +       delegated = NULL;
34628 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
34629 +       if (unlikely(err == -EWOULDBLOCK)) {
34630 +               pr_warn("cannot retry for NFSv4 delegation"
34631 +                       " for an internal unlink\n");
34632 +               iput(delegated);
34633 +       }
34634 +       if (unlikely(err)) {
34635 +               pr_err("%pd unlink err %d\n", base, err);
34636 +               goto out_fput;
34637 +       }
34638 +
34639 +       if (copy_src) {
34640 +               /* no one can touch copy_src xino */
34641 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
34642 +               if (unlikely(err)) {
34643 +                       pr_err("%pd copy err %d\n", base, err);
34644 +                       goto out_fput;
34645 +               }
34646 +       }
34647 +       goto out_dput; /* success */
34648 +
34649 +out_fput:
34650 +       fput(file);
34651 +       file = ERR_PTR(err);
34652 +out_dput:
34653 +       dput(path.dentry);
34654 +out:
34655 +       return file;
34656 +}
34657 +
34658 +struct au_xino_lock_dir {
34659 +       struct au_hinode *hdir;
34660 +       struct dentry *parent;
34661 +       struct inode *dir;
34662 +};
34663 +
34664 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
34665 +                            struct au_xino_lock_dir *ldir)
34666 +{
34667 +       aufs_bindex_t brid, bindex;
34668 +
34669 +       ldir->hdir = NULL;
34670 +       bindex = -1;
34671 +       brid = au_xino_brid(sb);
34672 +       if (brid >= 0)
34673 +               bindex = au_br_index(sb, brid);
34674 +       if (bindex >= 0) {
34675 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
34676 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
34677 +       } else {
34678 +               ldir->parent = dget_parent(xino->f_path.dentry);
34679 +               ldir->dir = d_inode(ldir->parent);
34680 +               inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
34681 +       }
34682 +}
34683 +
34684 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
34685 +{
34686 +       if (ldir->hdir)
34687 +               au_hn_inode_unlock(ldir->hdir);
34688 +       else {
34689 +               inode_unlock(ldir->dir);
34690 +               dput(ldir->parent);
34691 +       }
34692 +}
34693 +
34694 +/* ---------------------------------------------------------------------- */
34695 +
34696 +/* trucate xino files asynchronously */
34697 +
34698 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
34699 +{
34700 +       int err;
34701 +       unsigned long jiffy;
34702 +       blkcnt_t blocks;
34703 +       aufs_bindex_t bi, bbot;
34704 +       struct kstatfs *st;
34705 +       struct au_branch *br;
34706 +       struct file *new_xino, *file;
34707 +       struct super_block *h_sb;
34708 +       struct au_xino_lock_dir ldir;
34709 +
34710 +       err = -ENOMEM;
34711 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34712 +       if (unlikely(!st))
34713 +               goto out;
34714 +
34715 +       err = -EINVAL;
34716 +       bbot = au_sbbot(sb);
34717 +       if (unlikely(bindex < 0 || bbot < bindex))
34718 +               goto out_st;
34719 +       br = au_sbr(sb, bindex);
34720 +       file = br->br_xino.xi_file;
34721 +       if (!file)
34722 +               goto out_st;
34723 +
34724 +       err = vfs_statfs(&file->f_path, st);
34725 +       if (unlikely(err))
34726 +               AuErr1("statfs err %d, ignored\n", err);
34727 +       jiffy = jiffies;
34728 +       blocks = file_inode(file)->i_blocks;
34729 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34730 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
34731 +
34732 +       au_xino_lock_dir(sb, file, &ldir);
34733 +       /* mnt_want_write() is unnecessary here */
34734 +       new_xino = au_xino_create2(file, file);
34735 +       au_xino_unlock_dir(&ldir);
34736 +       err = PTR_ERR(new_xino);
34737 +       if (IS_ERR(new_xino)) {
34738 +               pr_err("err %d, ignored\n", err);
34739 +               goto out_st;
34740 +       }
34741 +       err = 0;
34742 +       fput(file);
34743 +       br->br_xino.xi_file = new_xino;
34744 +
34745 +       h_sb = au_br_sb(br);
34746 +       for (bi = 0; bi <= bbot; bi++) {
34747 +               if (unlikely(bi == bindex))
34748 +                       continue;
34749 +               br = au_sbr(sb, bi);
34750 +               if (au_br_sb(br) != h_sb)
34751 +                       continue;
34752 +
34753 +               fput(br->br_xino.xi_file);
34754 +               br->br_xino.xi_file = new_xino;
34755 +               get_file(new_xino);
34756 +       }
34757 +
34758 +       err = vfs_statfs(&new_xino->f_path, st);
34759 +       if (!err) {
34760 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34761 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
34762 +                       st->f_bfree, st->f_blocks);
34763 +               if (file_inode(new_xino)->i_blocks < blocks)
34764 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
34765 +       } else
34766 +               AuErr1("statfs err %d, ignored\n", err);
34767 +
34768 +out_st:
34769 +       au_delayed_kfree(st);
34770 +out:
34771 +       return err;
34772 +}
34773 +
34774 +struct xino_do_trunc_args {
34775 +       struct super_block *sb;
34776 +       struct au_branch *br;
34777 +};
34778 +
34779 +static void xino_do_trunc(void *_args)
34780 +{
34781 +       struct xino_do_trunc_args *args = _args;
34782 +       struct super_block *sb;
34783 +       struct au_branch *br;
34784 +       struct inode *dir;
34785 +       int err;
34786 +       aufs_bindex_t bindex;
34787 +
34788 +       err = 0;
34789 +       sb = args->sb;
34790 +       dir = d_inode(sb->s_root);
34791 +       br = args->br;
34792 +
34793 +       si_noflush_write_lock(sb);
34794 +       ii_read_lock_parent(dir);
34795 +       bindex = au_br_index(sb, br->br_id);
34796 +       err = au_xino_trunc(sb, bindex);
34797 +       ii_read_unlock(dir);
34798 +       if (unlikely(err))
34799 +               pr_warn("err b%d, (%d)\n", bindex, err);
34800 +       atomic_dec(&br->br_xino_running);
34801 +       au_br_put(br);
34802 +       si_write_unlock(sb);
34803 +       au_nwt_done(&au_sbi(sb)->si_nowait);
34804 +       au_delayed_kfree(args);
34805 +}
34806 +
34807 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
34808 +{
34809 +       int err;
34810 +       struct kstatfs st;
34811 +       struct au_sbinfo *sbinfo;
34812 +
34813 +       /* todo: si_xino_expire and the ratio should be customizable */
34814 +       sbinfo = au_sbi(sb);
34815 +       if (time_before(jiffies,
34816 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
34817 +               return 0;
34818 +
34819 +       /* truncation border */
34820 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
34821 +       if (unlikely(err)) {
34822 +               AuErr1("statfs err %d, ignored\n", err);
34823 +               return 0;
34824 +       }
34825 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
34826 +               return 0;
34827 +
34828 +       return 1;
34829 +}
34830 +
34831 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
34832 +{
34833 +       struct xino_do_trunc_args *args;
34834 +       int wkq_err;
34835 +
34836 +       if (!xino_trunc_test(sb, br))
34837 +               return;
34838 +
34839 +       if (atomic_inc_return(&br->br_xino_running) > 1)
34840 +               goto out;
34841 +
34842 +       /* lock and kfree() will be called in trunc_xino() */
34843 +       args = kmalloc(sizeof(*args), GFP_NOFS);
34844 +       if (unlikely(!args)) {
34845 +               AuErr1("no memory\n");
34846 +               goto out;
34847 +       }
34848 +
34849 +       au_br_get(br);
34850 +       args->sb = sb;
34851 +       args->br = br;
34852 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
34853 +       if (!wkq_err)
34854 +               return; /* success */
34855 +
34856 +       pr_err("wkq %d\n", wkq_err);
34857 +       au_br_put(br);
34858 +       au_delayed_kfree(args);
34859 +
34860 +out:
34861 +       atomic_dec(&br->br_xino_running);
34862 +}
34863 +
34864 +/* ---------------------------------------------------------------------- */
34865 +
34866 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
34867 +                           ino_t h_ino, ino_t ino)
34868 +{
34869 +       loff_t pos;
34870 +       ssize_t sz;
34871 +
34872 +       pos = h_ino;
34873 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34874 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34875 +               return -EFBIG;
34876 +       }
34877 +       pos *= sizeof(ino);
34878 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34879 +       if (sz == sizeof(ino))
34880 +               return 0; /* success */
34881 +
34882 +       AuIOErr("write failed (%zd)\n", sz);
34883 +       return -EIO;
34884 +}
34885 +
34886 +/*
34887 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
34888 + * at the position of @h_ino.
34889 + * even if @ino is zero, it is written to the xinofile and means no entry.
34890 + * if the size of the xino file on a specific filesystem exceeds the watermark,
34891 + * try truncating it.
34892 + */
34893 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34894 +                 ino_t ino)
34895 +{
34896 +       int err;
34897 +       unsigned int mnt_flags;
34898 +       struct au_branch *br;
34899 +
34900 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34901 +                    || ((loff_t)-1) > 0);
34902 +       SiMustAnyLock(sb);
34903 +
34904 +       mnt_flags = au_mntflags(sb);
34905 +       if (!au_opt_test(mnt_flags, XINO))
34906 +               return 0;
34907 +
34908 +       br = au_sbr(sb, bindex);
34909 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34910 +                              h_ino, ino);
34911 +       if (!err) {
34912 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
34913 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34914 +                       xino_try_trunc(sb, br);
34915 +               return 0; /* success */
34916 +       }
34917 +
34918 +       AuIOErr("write failed (%d)\n", err);
34919 +       return -EIO;
34920 +}
34921 +
34922 +/* ---------------------------------------------------------------------- */
34923 +
34924 +/* aufs inode number bitmap */
34925 +
34926 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34927 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
34928 +{
34929 +       ino_t ino;
34930 +
34931 +       AuDebugOn(bit < 0 || page_bits <= bit);
34932 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34933 +       return ino;
34934 +}
34935 +
34936 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34937 +{
34938 +       AuDebugOn(ino < AUFS_FIRST_INO);
34939 +       ino -= AUFS_FIRST_INO;
34940 +       *pindex = ino / page_bits;
34941 +       *bit = ino % page_bits;
34942 +}
34943 +
34944 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
34945 +{
34946 +       int err;
34947 +       loff_t pos;
34948 +       ssize_t sz;
34949 +       struct au_sbinfo *sbinfo;
34950 +       struct file *xib;
34951 +       unsigned long *p;
34952 +
34953 +       sbinfo = au_sbi(sb);
34954 +       MtxMustLock(&sbinfo->si_xib_mtx);
34955 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34956 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
34957 +
34958 +       if (pindex == sbinfo->si_xib_last_pindex)
34959 +               return 0;
34960 +
34961 +       xib = sbinfo->si_xib;
34962 +       p = sbinfo->si_xib_buf;
34963 +       pos = sbinfo->si_xib_last_pindex;
34964 +       pos *= PAGE_SIZE;
34965 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34966 +       if (unlikely(sz != PAGE_SIZE))
34967 +               goto out;
34968 +
34969 +       pos = pindex;
34970 +       pos *= PAGE_SIZE;
34971 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
34972 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34973 +       else {
34974 +               memset(p, 0, PAGE_SIZE);
34975 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34976 +       }
34977 +       if (sz == PAGE_SIZE) {
34978 +               sbinfo->si_xib_last_pindex = pindex;
34979 +               return 0; /* success */
34980 +       }
34981 +
34982 +out:
34983 +       AuIOErr1("write failed (%zd)\n", sz);
34984 +       err = sz;
34985 +       if (sz >= 0)
34986 +               err = -EIO;
34987 +       return err;
34988 +}
34989 +
34990 +/* ---------------------------------------------------------------------- */
34991 +
34992 +static void au_xib_clear_bit(struct inode *inode)
34993 +{
34994 +       int err, bit;
34995 +       unsigned long pindex;
34996 +       struct super_block *sb;
34997 +       struct au_sbinfo *sbinfo;
34998 +
34999 +       AuDebugOn(inode->i_nlink);
35000 +
35001 +       sb = inode->i_sb;
35002 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
35003 +       AuDebugOn(page_bits <= bit);
35004 +       sbinfo = au_sbi(sb);
35005 +       mutex_lock(&sbinfo->si_xib_mtx);
35006 +       err = xib_pindex(sb, pindex);
35007 +       if (!err) {
35008 +               clear_bit(bit, sbinfo->si_xib_buf);
35009 +               sbinfo->si_xib_next_bit = bit;
35010 +       }
35011 +       mutex_unlock(&sbinfo->si_xib_mtx);
35012 +}
35013 +
35014 +/* for s_op->delete_inode() */
35015 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
35016 +{
35017 +       int err;
35018 +       unsigned int mnt_flags;
35019 +       aufs_bindex_t bindex, bbot, bi;
35020 +       unsigned char try_trunc;
35021 +       struct au_iinfo *iinfo;
35022 +       struct super_block *sb;
35023 +       struct au_hinode *hi;
35024 +       struct inode *h_inode;
35025 +       struct au_branch *br;
35026 +       vfs_writef_t xwrite;
35027 +
35028 +       AuDebugOn(au_is_bad_inode(inode));
35029 +
35030 +       sb = inode->i_sb;
35031 +       mnt_flags = au_mntflags(sb);
35032 +       if (!au_opt_test(mnt_flags, XINO)
35033 +           || inode->i_ino == AUFS_ROOT_INO)
35034 +               return;
35035 +
35036 +       if (unlinked) {
35037 +               au_xigen_inc(inode);
35038 +               au_xib_clear_bit(inode);
35039 +       }
35040 +
35041 +       iinfo = au_ii(inode);
35042 +       bindex = iinfo->ii_btop;
35043 +       if (bindex < 0)
35044 +               return;
35045 +
35046 +       xwrite = au_sbi(sb)->si_xwrite;
35047 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
35048 +       hi = au_hinode(iinfo, bindex);
35049 +       bbot = iinfo->ii_bbot;
35050 +       for (; bindex <= bbot; bindex++, hi++) {
35051 +               h_inode = hi->hi_inode;
35052 +               if (!h_inode
35053 +                   || (!unlinked && h_inode->i_nlink))
35054 +                       continue;
35055 +
35056 +               /* inode may not be revalidated */
35057 +               bi = au_br_index(sb, hi->hi_id);
35058 +               if (bi < 0)
35059 +                       continue;
35060 +
35061 +               br = au_sbr(sb, bi);
35062 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
35063 +                                      h_inode->i_ino, /*ino*/0);
35064 +               if (!err && try_trunc
35065 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
35066 +                       xino_try_trunc(sb, br);
35067 +       }
35068 +}
35069 +
35070 +/* get an unused inode number from bitmap */
35071 +ino_t au_xino_new_ino(struct super_block *sb)
35072 +{
35073 +       ino_t ino;
35074 +       unsigned long *p, pindex, ul, pend;
35075 +       struct au_sbinfo *sbinfo;
35076 +       struct file *file;
35077 +       int free_bit, err;
35078 +
35079 +       if (!au_opt_test(au_mntflags(sb), XINO))
35080 +               return iunique(sb, AUFS_FIRST_INO);
35081 +
35082 +       sbinfo = au_sbi(sb);
35083 +       mutex_lock(&sbinfo->si_xib_mtx);
35084 +       p = sbinfo->si_xib_buf;
35085 +       free_bit = sbinfo->si_xib_next_bit;
35086 +       if (free_bit < page_bits && !test_bit(free_bit, p))
35087 +               goto out; /* success */
35088 +       free_bit = find_first_zero_bit(p, page_bits);
35089 +       if (free_bit < page_bits)
35090 +               goto out; /* success */
35091 +
35092 +       pindex = sbinfo->si_xib_last_pindex;
35093 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
35094 +               err = xib_pindex(sb, ul);
35095 +               if (unlikely(err))
35096 +                       goto out_err;
35097 +               free_bit = find_first_zero_bit(p, page_bits);
35098 +               if (free_bit < page_bits)
35099 +                       goto out; /* success */
35100 +       }
35101 +
35102 +       file = sbinfo->si_xib;
35103 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
35104 +       for (ul = pindex + 1; ul <= pend; ul++) {
35105 +               err = xib_pindex(sb, ul);
35106 +               if (unlikely(err))
35107 +                       goto out_err;
35108 +               free_bit = find_first_zero_bit(p, page_bits);
35109 +               if (free_bit < page_bits)
35110 +                       goto out; /* success */
35111 +       }
35112 +       BUG();
35113 +
35114 +out:
35115 +       set_bit(free_bit, p);
35116 +       sbinfo->si_xib_next_bit = free_bit + 1;
35117 +       pindex = sbinfo->si_xib_last_pindex;
35118 +       mutex_unlock(&sbinfo->si_xib_mtx);
35119 +       ino = xib_calc_ino(pindex, free_bit);
35120 +       AuDbg("i%lu\n", (unsigned long)ino);
35121 +       return ino;
35122 +out_err:
35123 +       mutex_unlock(&sbinfo->si_xib_mtx);
35124 +       AuDbg("i0\n");
35125 +       return 0;
35126 +}
35127 +
35128 +/*
35129 + * read @ino from xinofile for the specified branch{@sb, @bindex}
35130 + * at the position of @h_ino.
35131 + * if @ino does not exist and @do_new is true, get new one.
35132 + */
35133 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
35134 +                ino_t *ino)
35135 +{
35136 +       int err;
35137 +       ssize_t sz;
35138 +       loff_t pos;
35139 +       struct file *file;
35140 +       struct au_sbinfo *sbinfo;
35141 +
35142 +       *ino = 0;
35143 +       if (!au_opt_test(au_mntflags(sb), XINO))
35144 +               return 0; /* no xino */
35145 +
35146 +       err = 0;
35147 +       sbinfo = au_sbi(sb);
35148 +       pos = h_ino;
35149 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
35150 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
35151 +               return -EFBIG;
35152 +       }
35153 +       pos *= sizeof(*ino);
35154 +
35155 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
35156 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
35157 +               return 0; /* no ino */
35158 +
35159 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
35160 +       if (sz == sizeof(*ino))
35161 +               return 0; /* success */
35162 +
35163 +       err = sz;
35164 +       if (unlikely(sz >= 0)) {
35165 +               err = -EIO;
35166 +               AuIOErr("xino read error (%zd)\n", sz);
35167 +       }
35168 +
35169 +       return err;
35170 +}
35171 +
35172 +/* ---------------------------------------------------------------------- */
35173 +
35174 +/* create and set a new xino file */
35175 +
35176 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
35177 +{
35178 +       struct file *file;
35179 +       struct dentry *h_parent, *d;
35180 +       struct inode *h_dir, *inode;
35181 +       int err;
35182 +
35183 +       /*
35184 +        * at mount-time, and the xino file is the default path,
35185 +        * hnotify is disabled so we have no notify events to ignore.
35186 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
35187 +        */
35188 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
35189 +                              /* | __FMODE_NONOTIFY */,
35190 +                              S_IRUGO | S_IWUGO);
35191 +       if (IS_ERR(file)) {
35192 +               if (!silent)
35193 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
35194 +               return file;
35195 +       }
35196 +
35197 +       /* keep file count */
35198 +       err = 0;
35199 +       inode = file_inode(file);
35200 +       h_parent = dget_parent(file->f_path.dentry);
35201 +       h_dir = d_inode(h_parent);
35202 +       inode_lock_nested(h_dir, AuLsc_I_PARENT);
35203 +       /* mnt_want_write() is unnecessary here */
35204 +       /* no delegation since it is just created */
35205 +       if (inode->i_nlink)
35206 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
35207 +                                  /*force*/0);
35208 +       inode_unlock(h_dir);
35209 +       dput(h_parent);
35210 +       if (unlikely(err)) {
35211 +               if (!silent)
35212 +                       pr_err("unlink %s(%d)\n", fname, err);
35213 +               goto out;
35214 +       }
35215 +
35216 +       err = -EINVAL;
35217 +       d = file->f_path.dentry;
35218 +       if (unlikely(sb == d->d_sb)) {
35219 +               if (!silent)
35220 +                       pr_err("%s must be outside\n", fname);
35221 +               goto out;
35222 +       }
35223 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
35224 +               if (!silent)
35225 +                       pr_err("xino doesn't support %s(%s)\n",
35226 +                              fname, au_sbtype(d->d_sb));
35227 +               goto out;
35228 +       }
35229 +       return file; /* success */
35230 +
35231 +out:
35232 +       fput(file);
35233 +       file = ERR_PTR(err);
35234 +       return file;
35235 +}
35236 +
35237 +/*
35238 + * find another branch who is on the same filesystem of the specified
35239 + * branch{@btgt}. search until @bbot.
35240 + */
35241 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
35242 +                       aufs_bindex_t bbot)
35243 +{
35244 +       aufs_bindex_t bindex;
35245 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
35246 +
35247 +       for (bindex = 0; bindex < btgt; bindex++)
35248 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
35249 +                       return bindex;
35250 +       for (bindex++; bindex <= bbot; bindex++)
35251 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
35252 +                       return bindex;
35253 +       return -1;
35254 +}
35255 +
35256 +/* ---------------------------------------------------------------------- */
35257 +
35258 +/*
35259 + * initialize the xinofile for the specified branch @br
35260 + * at the place/path where @base_file indicates.
35261 + * test whether another branch is on the same filesystem or not,
35262 + * if @do_test is true.
35263 + */
35264 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
35265 +              struct file *base_file, int do_test)
35266 +{
35267 +       int err;
35268 +       ino_t ino;
35269 +       aufs_bindex_t bbot, bindex;
35270 +       struct au_branch *shared_br, *b;
35271 +       struct file *file;
35272 +       struct super_block *tgt_sb;
35273 +
35274 +       shared_br = NULL;
35275 +       bbot = au_sbbot(sb);
35276 +       if (do_test) {
35277 +               tgt_sb = au_br_sb(br);
35278 +               for (bindex = 0; bindex <= bbot; bindex++) {
35279 +                       b = au_sbr(sb, bindex);
35280 +                       if (tgt_sb == au_br_sb(b)) {
35281 +                               shared_br = b;
35282 +                               break;
35283 +                       }
35284 +               }
35285 +       }
35286 +
35287 +       if (!shared_br || !shared_br->br_xino.xi_file) {
35288 +               struct au_xino_lock_dir ldir;
35289 +
35290 +               au_xino_lock_dir(sb, base_file, &ldir);
35291 +               /* mnt_want_write() is unnecessary here */
35292 +               file = au_xino_create2(base_file, NULL);
35293 +               au_xino_unlock_dir(&ldir);
35294 +               err = PTR_ERR(file);
35295 +               if (IS_ERR(file))
35296 +                       goto out;
35297 +               br->br_xino.xi_file = file;
35298 +       } else {
35299 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
35300 +               get_file(br->br_xino.xi_file);
35301 +       }
35302 +
35303 +       ino = AUFS_ROOT_INO;
35304 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
35305 +                              h_ino, ino);
35306 +       if (unlikely(err)) {
35307 +               fput(br->br_xino.xi_file);
35308 +               br->br_xino.xi_file = NULL;
35309 +       }
35310 +
35311 +out:
35312 +       return err;
35313 +}
35314 +
35315 +/* ---------------------------------------------------------------------- */
35316 +
35317 +/* trucate a xino bitmap file */
35318 +
35319 +/* todo: slow */
35320 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
35321 +{
35322 +       int err, bit;
35323 +       ssize_t sz;
35324 +       unsigned long pindex;
35325 +       loff_t pos, pend;
35326 +       struct au_sbinfo *sbinfo;
35327 +       vfs_readf_t func;
35328 +       ino_t *ino;
35329 +       unsigned long *p;
35330 +
35331 +       err = 0;
35332 +       sbinfo = au_sbi(sb);
35333 +       MtxMustLock(&sbinfo->si_xib_mtx);
35334 +       p = sbinfo->si_xib_buf;
35335 +       func = sbinfo->si_xread;
35336 +       pend = vfsub_f_size_read(file);
35337 +       pos = 0;
35338 +       while (pos < pend) {
35339 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
35340 +               err = sz;
35341 +               if (unlikely(sz <= 0))
35342 +                       goto out;
35343 +
35344 +               err = 0;
35345 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
35346 +                       if (unlikely(*ino < AUFS_FIRST_INO))
35347 +                               continue;
35348 +
35349 +                       xib_calc_bit(*ino, &pindex, &bit);
35350 +                       AuDebugOn(page_bits <= bit);
35351 +                       err = xib_pindex(sb, pindex);
35352 +                       if (!err)
35353 +                               set_bit(bit, p);
35354 +                       else
35355 +                               goto out;
35356 +               }
35357 +       }
35358 +
35359 +out:
35360 +       return err;
35361 +}
35362 +
35363 +static int xib_restore(struct super_block *sb)
35364 +{
35365 +       int err;
35366 +       aufs_bindex_t bindex, bbot;
35367 +       void *page;
35368 +
35369 +       err = -ENOMEM;
35370 +       page = (void *)__get_free_page(GFP_NOFS);
35371 +       if (unlikely(!page))
35372 +               goto out;
35373 +
35374 +       err = 0;
35375 +       bbot = au_sbbot(sb);
35376 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
35377 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
35378 +                       err = do_xib_restore
35379 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
35380 +               else
35381 +                       AuDbg("b%d\n", bindex);
35382 +       au_delayed_free_page((unsigned long)page);
35383 +
35384 +out:
35385 +       return err;
35386 +}
35387 +
35388 +int au_xib_trunc(struct super_block *sb)
35389 +{
35390 +       int err;
35391 +       ssize_t sz;
35392 +       loff_t pos;
35393 +       struct au_xino_lock_dir ldir;
35394 +       struct au_sbinfo *sbinfo;
35395 +       unsigned long *p;
35396 +       struct file *file;
35397 +
35398 +       SiMustWriteLock(sb);
35399 +
35400 +       err = 0;
35401 +       sbinfo = au_sbi(sb);
35402 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
35403 +               goto out;
35404 +
35405 +       file = sbinfo->si_xib;
35406 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
35407 +               goto out;
35408 +
35409 +       au_xino_lock_dir(sb, file, &ldir);
35410 +       /* mnt_want_write() is unnecessary here */
35411 +       file = au_xino_create2(sbinfo->si_xib, NULL);
35412 +       au_xino_unlock_dir(&ldir);
35413 +       err = PTR_ERR(file);
35414 +       if (IS_ERR(file))
35415 +               goto out;
35416 +       fput(sbinfo->si_xib);
35417 +       sbinfo->si_xib = file;
35418 +
35419 +       p = sbinfo->si_xib_buf;
35420 +       memset(p, 0, PAGE_SIZE);
35421 +       pos = 0;
35422 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
35423 +       if (unlikely(sz != PAGE_SIZE)) {
35424 +               err = sz;
35425 +               AuIOErr("err %d\n", err);
35426 +               if (sz >= 0)
35427 +                       err = -EIO;
35428 +               goto out;
35429 +       }
35430 +
35431 +       mutex_lock(&sbinfo->si_xib_mtx);
35432 +       /* mnt_want_write() is unnecessary here */
35433 +       err = xib_restore(sb);
35434 +       mutex_unlock(&sbinfo->si_xib_mtx);
35435 +
35436 +out:
35437 +       return err;
35438 +}
35439 +
35440 +/* ---------------------------------------------------------------------- */
35441 +
35442 +/*
35443 + * xino mount option handlers
35444 + */
35445 +
35446 +/* xino bitmap */
35447 +static void xino_clear_xib(struct super_block *sb)
35448 +{
35449 +       struct au_sbinfo *sbinfo;
35450 +
35451 +       SiMustWriteLock(sb);
35452 +
35453 +       sbinfo = au_sbi(sb);
35454 +       sbinfo->si_xread = NULL;
35455 +       sbinfo->si_xwrite = NULL;
35456 +       if (sbinfo->si_xib)
35457 +               fput(sbinfo->si_xib);
35458 +       sbinfo->si_xib = NULL;
35459 +       if (sbinfo->si_xib_buf)
35460 +               au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
35461 +       sbinfo->si_xib_buf = NULL;
35462 +}
35463 +
35464 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
35465 +{
35466 +       int err;
35467 +       loff_t pos;
35468 +       struct au_sbinfo *sbinfo;
35469 +       struct file *file;
35470 +
35471 +       SiMustWriteLock(sb);
35472 +
35473 +       sbinfo = au_sbi(sb);
35474 +       file = au_xino_create2(base, sbinfo->si_xib);
35475 +       err = PTR_ERR(file);
35476 +       if (IS_ERR(file))
35477 +               goto out;
35478 +       if (sbinfo->si_xib)
35479 +               fput(sbinfo->si_xib);
35480 +       sbinfo->si_xib = file;
35481 +       sbinfo->si_xread = vfs_readf(file);
35482 +       sbinfo->si_xwrite = vfs_writef(file);
35483 +
35484 +       err = -ENOMEM;
35485 +       if (!sbinfo->si_xib_buf)
35486 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
35487 +       if (unlikely(!sbinfo->si_xib_buf))
35488 +               goto out_unset;
35489 +
35490 +       sbinfo->si_xib_last_pindex = 0;
35491 +       sbinfo->si_xib_next_bit = 0;
35492 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
35493 +               pos = 0;
35494 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
35495 +                                 PAGE_SIZE, &pos);
35496 +               if (unlikely(err != PAGE_SIZE))
35497 +                       goto out_free;
35498 +       }
35499 +       err = 0;
35500 +       goto out; /* success */
35501 +
35502 +out_free:
35503 +       if (sbinfo->si_xib_buf)
35504 +               au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
35505 +       sbinfo->si_xib_buf = NULL;
35506 +       if (err >= 0)
35507 +               err = -EIO;
35508 +out_unset:
35509 +       fput(sbinfo->si_xib);
35510 +       sbinfo->si_xib = NULL;
35511 +       sbinfo->si_xread = NULL;
35512 +       sbinfo->si_xwrite = NULL;
35513 +out:
35514 +       return err;
35515 +}
35516 +
35517 +/* xino for each branch */
35518 +static void xino_clear_br(struct super_block *sb)
35519 +{
35520 +       aufs_bindex_t bindex, bbot;
35521 +       struct au_branch *br;
35522 +
35523 +       bbot = au_sbbot(sb);
35524 +       for (bindex = 0; bindex <= bbot; bindex++) {
35525 +               br = au_sbr(sb, bindex);
35526 +               if (!br || !br->br_xino.xi_file)
35527 +                       continue;
35528 +
35529 +               fput(br->br_xino.xi_file);
35530 +               br->br_xino.xi_file = NULL;
35531 +       }
35532 +}
35533 +
35534 +static int au_xino_set_br(struct super_block *sb, struct file *base)
35535 +{
35536 +       int err;
35537 +       ino_t ino;
35538 +       aufs_bindex_t bindex, bbot, bshared;
35539 +       struct {
35540 +               struct file *old, *new;
35541 +       } *fpair, *p;
35542 +       struct au_branch *br;
35543 +       struct inode *inode;
35544 +       vfs_writef_t writef;
35545 +
35546 +       SiMustWriteLock(sb);
35547 +
35548 +       err = -ENOMEM;
35549 +       bbot = au_sbbot(sb);
35550 +       fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
35551 +       if (unlikely(!fpair))
35552 +               goto out;
35553 +
35554 +       inode = d_inode(sb->s_root);
35555 +       ino = AUFS_ROOT_INO;
35556 +       writef = au_sbi(sb)->si_xwrite;
35557 +       for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
35558 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
35559 +               if (bshared >= 0) {
35560 +                       /* shared xino */
35561 +                       *p = fpair[bshared];
35562 +                       get_file(p->new);
35563 +               }
35564 +
35565 +               if (!p->new) {
35566 +                       /* new xino */
35567 +                       br = au_sbr(sb, bindex);
35568 +                       p->old = br->br_xino.xi_file;
35569 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
35570 +                       err = PTR_ERR(p->new);
35571 +                       if (IS_ERR(p->new)) {
35572 +                               p->new = NULL;
35573 +                               goto out_pair;
35574 +                       }
35575 +               }
35576 +
35577 +               err = au_xino_do_write(writef, p->new,
35578 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
35579 +               if (unlikely(err))
35580 +                       goto out_pair;
35581 +       }
35582 +
35583 +       for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
35584 +               br = au_sbr(sb, bindex);
35585 +               if (br->br_xino.xi_file)
35586 +                       fput(br->br_xino.xi_file);
35587 +               get_file(p->new);
35588 +               br->br_xino.xi_file = p->new;
35589 +       }
35590 +
35591 +out_pair:
35592 +       for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
35593 +               if (p->new)
35594 +                       fput(p->new);
35595 +               else
35596 +                       break;
35597 +       au_delayed_kfree(fpair);
35598 +out:
35599 +       return err;
35600 +}
35601 +
35602 +void au_xino_clr(struct super_block *sb)
35603 +{
35604 +       struct au_sbinfo *sbinfo;
35605 +
35606 +       au_xigen_clr(sb);
35607 +       xino_clear_xib(sb);
35608 +       xino_clear_br(sb);
35609 +       sbinfo = au_sbi(sb);
35610 +       /* lvalue, do not call au_mntflags() */
35611 +       au_opt_clr(sbinfo->si_mntflags, XINO);
35612 +}
35613 +
35614 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
35615 +{
35616 +       int err, skip;
35617 +       struct dentry *parent, *cur_parent;
35618 +       struct qstr *dname, *cur_name;
35619 +       struct file *cur_xino;
35620 +       struct inode *dir;
35621 +       struct au_sbinfo *sbinfo;
35622 +
35623 +       SiMustWriteLock(sb);
35624 +
35625 +       err = 0;
35626 +       sbinfo = au_sbi(sb);
35627 +       parent = dget_parent(xino->file->f_path.dentry);
35628 +       if (remount) {
35629 +               skip = 0;
35630 +               dname = &xino->file->f_path.dentry->d_name;
35631 +               cur_xino = sbinfo->si_xib;
35632 +               if (cur_xino) {
35633 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
35634 +                       cur_name = &cur_xino->f_path.dentry->d_name;
35635 +                       skip = (cur_parent == parent
35636 +                               && au_qstreq(dname, cur_name));
35637 +                       dput(cur_parent);
35638 +               }
35639 +               if (skip)
35640 +                       goto out;
35641 +       }
35642 +
35643 +       au_opt_set(sbinfo->si_mntflags, XINO);
35644 +       dir = d_inode(parent);
35645 +       inode_lock_nested(dir, AuLsc_I_PARENT);
35646 +       /* mnt_want_write() is unnecessary here */
35647 +       err = au_xino_set_xib(sb, xino->file);
35648 +       if (!err)
35649 +               err = au_xigen_set(sb, xino->file);
35650 +       if (!err)
35651 +               err = au_xino_set_br(sb, xino->file);
35652 +       inode_unlock(dir);
35653 +       if (!err)
35654 +               goto out; /* success */
35655 +
35656 +       /* reset all */
35657 +       AuIOErr("failed creating xino(%d).\n", err);
35658 +       au_xigen_clr(sb);
35659 +       xino_clear_xib(sb);
35660 +
35661 +out:
35662 +       dput(parent);
35663 +       return err;
35664 +}
35665 +
35666 +/* ---------------------------------------------------------------------- */
35667 +
35668 +/*
35669 + * create a xinofile at the default place/path.
35670 + */
35671 +struct file *au_xino_def(struct super_block *sb)
35672 +{
35673 +       struct file *file;
35674 +       char *page, *p;
35675 +       struct au_branch *br;
35676 +       struct super_block *h_sb;
35677 +       struct path path;
35678 +       aufs_bindex_t bbot, bindex, bwr;
35679 +
35680 +       br = NULL;
35681 +       bbot = au_sbbot(sb);
35682 +       bwr = -1;
35683 +       for (bindex = 0; bindex <= bbot; bindex++) {
35684 +               br = au_sbr(sb, bindex);
35685 +               if (au_br_writable(br->br_perm)
35686 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
35687 +                       bwr = bindex;
35688 +                       break;
35689 +               }
35690 +       }
35691 +
35692 +       if (bwr >= 0) {
35693 +               file = ERR_PTR(-ENOMEM);
35694 +               page = (void *)__get_free_page(GFP_NOFS);
35695 +               if (unlikely(!page))
35696 +                       goto out;
35697 +               path.mnt = au_br_mnt(br);
35698 +               path.dentry = au_h_dptr(sb->s_root, bwr);
35699 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
35700 +               file = (void *)p;
35701 +               if (!IS_ERR(p)) {
35702 +                       strcat(p, "/" AUFS_XINO_FNAME);
35703 +                       AuDbg("%s\n", p);
35704 +                       file = au_xino_create(sb, p, /*silent*/0);
35705 +                       if (!IS_ERR(file))
35706 +                               au_xino_brid_set(sb, br->br_id);
35707 +               }
35708 +               au_delayed_free_page((unsigned long)page);
35709 +       } else {
35710 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
35711 +               if (IS_ERR(file))
35712 +                       goto out;
35713 +               h_sb = file->f_path.dentry->d_sb;
35714 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
35715 +                       pr_err("xino doesn't support %s(%s)\n",
35716 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
35717 +                       fput(file);
35718 +                       file = ERR_PTR(-EINVAL);
35719 +               }
35720 +               if (!IS_ERR(file))
35721 +                       au_xino_brid_set(sb, -1);
35722 +       }
35723 +
35724 +out:
35725 +       return file;
35726 +}
35727 +
35728 +/* ---------------------------------------------------------------------- */
35729 +
35730 +int au_xino_path(struct seq_file *seq, struct file *file)
35731 +{
35732 +       int err;
35733 +
35734 +       err = au_seq_path(seq, &file->f_path);
35735 +       if (unlikely(err))
35736 +               goto out;
35737 +
35738 +#define Deleted "\\040(deleted)"
35739 +       seq->count -= sizeof(Deleted) - 1;
35740 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
35741 +                        sizeof(Deleted) - 1));
35742 +#undef Deleted
35743 +
35744 +out:
35745 +       return err;
35746 +}
35747 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
35748 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
35749 +++ linux/include/uapi/linux/aufs_type.h        2017-03-02 14:24:13.870255912 +0100
35750 @@ -0,0 +1,419 @@
35751 +/*
35752 + * Copyright (C) 2005-2017 Junjiro R. Okajima
35753 + *
35754 + * This program, aufs is free software; you can redistribute it and/or modify
35755 + * it under the terms of the GNU General Public License as published by
35756 + * the Free Software Foundation; either version 2 of the License, or
35757 + * (at your option) any later version.
35758 + *
35759 + * This program is distributed in the hope that it will be useful,
35760 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35761 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35762 + * GNU General Public License for more details.
35763 + *
35764 + * You should have received a copy of the GNU General Public License
35765 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35766 + */
35767 +
35768 +#ifndef __AUFS_TYPE_H__
35769 +#define __AUFS_TYPE_H__
35770 +
35771 +#define AUFS_NAME      "aufs"
35772 +
35773 +#ifdef __KERNEL__
35774 +/*
35775 + * define it before including all other headers.
35776 + * sched.h may use pr_* macros before defining "current", so define the
35777 + * no-current version first, and re-define later.
35778 + */
35779 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
35780 +#include <linux/sched.h>
35781 +#undef pr_fmt
35782 +#define pr_fmt(fmt) \
35783 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
35784 +               (int)sizeof(current->comm), current->comm, current->pid
35785 +#else
35786 +#include <stdint.h>
35787 +#include <sys/types.h>
35788 +#endif /* __KERNEL__ */
35789 +
35790 +#include <linux/limits.h>
35791 +
35792 +#define AUFS_VERSION   "4.x-rcN-20170220"
35793 +
35794 +/* todo? move this to linux-2.6.19/include/magic.h */
35795 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
35796 +
35797 +/* ---------------------------------------------------------------------- */
35798 +
35799 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
35800 +typedef int8_t aufs_bindex_t;
35801 +#define AUFS_BRANCH_MAX 127
35802 +#else
35803 +typedef int16_t aufs_bindex_t;
35804 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
35805 +#define AUFS_BRANCH_MAX 511
35806 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
35807 +#define AUFS_BRANCH_MAX 1023
35808 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
35809 +#define AUFS_BRANCH_MAX 32767
35810 +#endif
35811 +#endif
35812 +
35813 +#ifdef __KERNEL__
35814 +#ifndef AUFS_BRANCH_MAX
35815 +#error unknown CONFIG_AUFS_BRANCH_MAX value
35816 +#endif
35817 +#endif /* __KERNEL__ */
35818 +
35819 +/* ---------------------------------------------------------------------- */
35820 +
35821 +#define AUFS_FSTYPE            AUFS_NAME
35822 +
35823 +#define AUFS_ROOT_INO          2
35824 +#define AUFS_FIRST_INO         11
35825 +
35826 +#define AUFS_WH_PFX            ".wh."
35827 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
35828 +#define AUFS_WH_TMP_LEN                4
35829 +/* a limit for rmdir/rename a dir and copyup */
35830 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
35831 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
35832 +                               - 1                     /* dot */\
35833 +                               - AUFS_WH_TMP_LEN)      /* hex */
35834 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
35835 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
35836 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
35837 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
35838 +#define AUFS_DIRWH_DEF         3
35839 +#define AUFS_RDCACHE_DEF       10 /* seconds */
35840 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
35841 +#define AUFS_RDBLK_DEF         512 /* bytes */
35842 +#define AUFS_RDHASH_DEF                32
35843 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
35844 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
35845 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
35846 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
35847 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
35848 +
35849 +/* pseudo-link maintenace under /proc */
35850 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
35851 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
35852 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35853 +
35854 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
35855 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
35856 +
35857 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
35858 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
35859 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
35860 +
35861 +/* doubly whiteouted */
35862 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
35863 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
35864 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
35865 +
35866 +/* branch permissions and attributes */
35867 +#define AUFS_BRPERM_RW         "rw"
35868 +#define AUFS_BRPERM_RO         "ro"
35869 +#define AUFS_BRPERM_RR         "rr"
35870 +#define AUFS_BRATTR_COO_REG    "coo_reg"
35871 +#define AUFS_BRATTR_COO_ALL    "coo_all"
35872 +#define AUFS_BRATTR_FHSM       "fhsm"
35873 +#define AUFS_BRATTR_UNPIN      "unpin"
35874 +#define AUFS_BRATTR_ICEX       "icex"
35875 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
35876 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
35877 +#define AUFS_BRATTR_ICEX_TR    "icextr"
35878 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
35879 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
35880 +#define AUFS_BRRATTR_WH                "wh"
35881 +#define AUFS_BRWATTR_NLWH      "nolwh"
35882 +#define AUFS_BRWATTR_MOO       "moo"
35883 +
35884 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
35885 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
35886 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
35887 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35888 +
35889 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
35890 +#define AuBrAttr_COO_ALL       (1 << 4)
35891 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35892 +
35893 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
35894 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
35895 +                                                  branch. meaningless since
35896 +                                                  linux-3.18-rc1 */
35897 +
35898 +/* ignore error in copying XATTR */
35899 +#define AuBrAttr_ICEX_SEC      (1 << 7)
35900 +#define AuBrAttr_ICEX_SYS      (1 << 8)
35901 +#define AuBrAttr_ICEX_TR       (1 << 9)
35902 +#define AuBrAttr_ICEX_USR      (1 << 10)
35903 +#define AuBrAttr_ICEX_OTH      (1 << 11)
35904 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
35905 +                                | AuBrAttr_ICEX_SYS    \
35906 +                                | AuBrAttr_ICEX_TR     \
35907 +                                | AuBrAttr_ICEX_USR    \
35908 +                                | AuBrAttr_ICEX_OTH)
35909 +
35910 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
35911 +#define AuBrRAttr_Mask         AuBrRAttr_WH
35912 +
35913 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
35914 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
35915 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35916 +
35917 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35918 +
35919 +/* #warning test userspace */
35920 +#ifdef __KERNEL__
35921 +#ifndef CONFIG_AUFS_FHSM
35922 +#undef AuBrAttr_FHSM
35923 +#define AuBrAttr_FHSM          0
35924 +#endif
35925 +#ifndef CONFIG_AUFS_XATTR
35926 +#undef AuBrAttr_ICEX
35927 +#define AuBrAttr_ICEX          0
35928 +#undef AuBrAttr_ICEX_SEC
35929 +#define AuBrAttr_ICEX_SEC      0
35930 +#undef AuBrAttr_ICEX_SYS
35931 +#define AuBrAttr_ICEX_SYS      0
35932 +#undef AuBrAttr_ICEX_TR
35933 +#define AuBrAttr_ICEX_TR       0
35934 +#undef AuBrAttr_ICEX_USR
35935 +#define AuBrAttr_ICEX_USR      0
35936 +#undef AuBrAttr_ICEX_OTH
35937 +#define AuBrAttr_ICEX_OTH      0
35938 +#endif
35939 +#endif
35940 +
35941 +/* the longest combination */
35942 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35943 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
35944 +                              "+" AUFS_BRATTR_COO_REG          \
35945 +                              "+" AUFS_BRATTR_FHSM             \
35946 +                              "+" AUFS_BRATTR_UNPIN            \
35947 +                              "+" AUFS_BRATTR_ICEX_SEC         \
35948 +                              "+" AUFS_BRATTR_ICEX_SYS         \
35949 +                              "+" AUFS_BRATTR_ICEX_USR         \
35950 +                              "+" AUFS_BRATTR_ICEX_OTH         \
35951 +                              "+" AUFS_BRWATTR_NLWH)
35952 +
35953 +typedef struct {
35954 +       char a[AuBrPermStrSz];
35955 +} au_br_perm_str_t;
35956 +
35957 +static inline int au_br_writable(int brperm)
35958 +{
35959 +       return brperm & AuBrPerm_RW;
35960 +}
35961 +
35962 +static inline int au_br_whable(int brperm)
35963 +{
35964 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35965 +}
35966 +
35967 +static inline int au_br_wh_linkable(int brperm)
35968 +{
35969 +       return !(brperm & AuBrWAttr_NoLinkWH);
35970 +}
35971 +
35972 +static inline int au_br_cmoo(int brperm)
35973 +{
35974 +       return brperm & AuBrAttr_CMOO_Mask;
35975 +}
35976 +
35977 +static inline int au_br_fhsm(int brperm)
35978 +{
35979 +       return brperm & AuBrAttr_FHSM;
35980 +}
35981 +
35982 +/* ---------------------------------------------------------------------- */
35983 +
35984 +/* ioctl */
35985 +enum {
35986 +       /* readdir in userspace */
35987 +       AuCtl_RDU,
35988 +       AuCtl_RDU_INO,
35989 +
35990 +       AuCtl_WBR_FD,   /* pathconf wrapper */
35991 +       AuCtl_IBUSY,    /* busy inode */
35992 +       AuCtl_MVDOWN,   /* move-down */
35993 +       AuCtl_BR,       /* info about branches */
35994 +       AuCtl_FHSM_FD   /* connection for fhsm */
35995 +};
35996 +
35997 +/* borrowed from linux/include/linux/kernel.h */
35998 +#ifndef ALIGN
35999 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
36000 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
36001 +#endif
36002 +
36003 +/* borrowed from linux/include/linux/compiler-gcc3.h */
36004 +#ifndef __aligned
36005 +#define __aligned(x)                   __attribute__((aligned(x)))
36006 +#endif
36007 +
36008 +#ifdef __KERNEL__
36009 +#ifndef __packed
36010 +#define __packed                       __attribute__((packed))
36011 +#endif
36012 +#endif
36013 +
36014 +struct au_rdu_cookie {
36015 +       uint64_t        h_pos;
36016 +       int16_t         bindex;
36017 +       uint8_t         flags;
36018 +       uint8_t         pad;
36019 +       uint32_t        generation;
36020 +} __aligned(8);
36021 +
36022 +struct au_rdu_ent {
36023 +       uint64_t        ino;
36024 +       int16_t         bindex;
36025 +       uint8_t         type;
36026 +       uint8_t         nlen;
36027 +       uint8_t         wh;
36028 +       char            name[0];
36029 +} __aligned(8);
36030 +
36031 +static inline int au_rdu_len(int nlen)
36032 +{
36033 +       /* include the terminating NULL */
36034 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
36035 +                    sizeof(uint64_t));
36036 +}
36037 +
36038 +union au_rdu_ent_ul {
36039 +       struct au_rdu_ent __user        *e;
36040 +       uint64_t                        ul;
36041 +};
36042 +
36043 +enum {
36044 +       AufsCtlRduV_SZ,
36045 +       AufsCtlRduV_End
36046 +};
36047 +
36048 +struct aufs_rdu {
36049 +       /* input */
36050 +       union {
36051 +               uint64_t        sz;     /* AuCtl_RDU */
36052 +               uint64_t        nent;   /* AuCtl_RDU_INO */
36053 +       };
36054 +       union au_rdu_ent_ul     ent;
36055 +       uint16_t                verify[AufsCtlRduV_End];
36056 +
36057 +       /* input/output */
36058 +       uint32_t                blk;
36059 +
36060 +       /* output */
36061 +       union au_rdu_ent_ul     tail;
36062 +       /* number of entries which were added in a single call */
36063 +       uint64_t                rent;
36064 +       uint8_t                 full;
36065 +       uint8_t                 shwh;
36066 +
36067 +       struct au_rdu_cookie    cookie;
36068 +} __aligned(8);
36069 +
36070 +/* ---------------------------------------------------------------------- */
36071 +
36072 +struct aufs_wbr_fd {
36073 +       uint32_t        oflags;
36074 +       int16_t         brid;
36075 +} __aligned(8);
36076 +
36077 +/* ---------------------------------------------------------------------- */
36078 +
36079 +struct aufs_ibusy {
36080 +       uint64_t        ino, h_ino;
36081 +       int16_t         bindex;
36082 +} __aligned(8);
36083 +
36084 +/* ---------------------------------------------------------------------- */
36085 +
36086 +/* error code for move-down */
36087 +/* the actual message strings are implemented in aufs-util.git */
36088 +enum {
36089 +       EAU_MVDOWN_OPAQUE = 1,
36090 +       EAU_MVDOWN_WHITEOUT,
36091 +       EAU_MVDOWN_UPPER,
36092 +       EAU_MVDOWN_BOTTOM,
36093 +       EAU_MVDOWN_NOUPPER,
36094 +       EAU_MVDOWN_NOLOWERBR,
36095 +       EAU_Last
36096 +};
36097 +
36098 +/* flags for move-down */
36099 +#define AUFS_MVDOWN_DMSG       1
36100 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
36101 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
36102 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
36103 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
36104 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
36105 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
36106 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
36107 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
36108 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
36109 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
36110 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
36111 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
36112 +
36113 +/* index for move-down */
36114 +enum {
36115 +       AUFS_MVDOWN_UPPER,
36116 +       AUFS_MVDOWN_LOWER,
36117 +       AUFS_MVDOWN_NARRAY
36118 +};
36119 +
36120 +/*
36121 + * additional info of move-down
36122 + * number of free blocks and inodes.
36123 + * subset of struct kstatfs, but smaller and always 64bit.
36124 + */
36125 +struct aufs_stfs {
36126 +       uint64_t        f_blocks;
36127 +       uint64_t        f_bavail;
36128 +       uint64_t        f_files;
36129 +       uint64_t        f_ffree;
36130 +};
36131 +
36132 +struct aufs_stbr {
36133 +       int16_t                 brid;   /* optional input */
36134 +       int16_t                 bindex; /* output */
36135 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
36136 +} __aligned(8);
36137 +
36138 +struct aufs_mvdown {
36139 +       uint32_t                flags;                  /* input/output */
36140 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
36141 +       int8_t                  au_errno;               /* output */
36142 +} __aligned(8);
36143 +
36144 +/* ---------------------------------------------------------------------- */
36145 +
36146 +union aufs_brinfo {
36147 +       /* PATH_MAX may differ between kernel-space and user-space */
36148 +       char    _spacer[4096];
36149 +       struct {
36150 +               int16_t id;
36151 +               int     perm;
36152 +               char    path[0];
36153 +       };
36154 +} __aligned(8);
36155 +
36156 +/* ---------------------------------------------------------------------- */
36157 +
36158 +#define AuCtlType              'A'
36159 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
36160 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
36161 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
36162 +                                    struct aufs_wbr_fd)
36163 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
36164 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
36165 +                                     struct aufs_mvdown)
36166 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
36167 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
36168 +
36169 +#endif /* __AUFS_TYPE_H__ */
36170 aufs4.x-rcN loopback patch
36171
36172 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
36173 index b63f68b..c0f4b8c 100644
36174 --- a/drivers/block/loop.c
36175 +++ b/drivers/block/loop.c
36176 @@ -551,7 +551,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
36177  }
36178  
36179  struct switch_request {
36180 -       struct file *file;
36181 +       struct file *file, *virt_file;
36182         struct completion wait;
36183  };
36184  
36185 @@ -577,6 +577,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
36186         mapping = file->f_mapping;
36187         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
36188         lo->lo_backing_file = file;
36189 +       lo->lo_backing_virt_file = p->virt_file;
36190         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
36191                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
36192         lo->old_gfp_mask = mapping_gfp_mask(mapping);
36193 @@ -589,11 +590,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
36194   * First it needs to flush existing IO, it does this by sending a magic
36195   * BIO down the pipe. The completion of this BIO does the actual switch.
36196   */
36197 -static int loop_switch(struct loop_device *lo, struct file *file)
36198 +static int loop_switch(struct loop_device *lo, struct file *file,
36199 +                      struct file *virt_file)
36200  {
36201         struct switch_request w;
36202  
36203         w.file = file;
36204 +       w.virt_file = virt_file;
36205  
36206         /* freeze queue and wait for completion of scheduled requests */
36207         blk_mq_freeze_queue(lo->lo_queue);
36208 @@ -612,7 +615,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
36209   */
36210  static int loop_flush(struct loop_device *lo)
36211  {
36212 -       return loop_switch(lo, NULL);
36213 +       return loop_switch(lo, NULL, NULL);
36214 +}
36215 +
36216 +static struct file *loop_real_file(struct file *file)
36217 +{
36218 +       struct file *f = NULL;
36219 +
36220 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
36221 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
36222 +       return f;
36223  }
36224  
36225  static void loop_reread_partitions(struct loop_device *lo,
36226 @@ -649,6 +661,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
36227                           unsigned int arg)
36228  {
36229         struct file     *file, *old_file;
36230 +       struct file     *f, *virt_file = NULL, *old_virt_file;
36231         struct inode    *inode;
36232         int             error;
36233  
36234 @@ -665,9 +678,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
36235         file = fget(arg);
36236         if (!file)
36237                 goto out;
36238 +       f = loop_real_file(file);
36239 +       if (f) {
36240 +               virt_file = file;
36241 +               file = f;
36242 +               get_file(file);
36243 +       }
36244  
36245         inode = file->f_mapping->host;
36246         old_file = lo->lo_backing_file;
36247 +       old_virt_file = lo->lo_backing_virt_file;
36248  
36249         error = -EINVAL;
36250  
36251 @@ -679,17 +699,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
36252                 goto out_putf;
36253  
36254         /* and ... switch */
36255 -       error = loop_switch(lo, file);
36256 +       error = loop_switch(lo, file, virt_file);
36257         if (error)
36258                 goto out_putf;
36259  
36260         fput(old_file);
36261 +       if (old_virt_file)
36262 +               fput(old_virt_file);
36263         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
36264                 loop_reread_partitions(lo, bdev);
36265         return 0;
36266  
36267   out_putf:
36268         fput(file);
36269 +       if (virt_file)
36270 +               fput(virt_file);
36271   out:
36272         return error;
36273  }
36274 @@ -876,7 +900,7 @@ static int loop_prepare_queue(struct loop_device *lo)
36275  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36276                        struct block_device *bdev, unsigned int arg)
36277  {
36278 -       struct file     *file, *f;
36279 +       struct file     *file, *f, *virt_file = NULL;
36280         struct inode    *inode;
36281         struct address_space *mapping;
36282         unsigned lo_blocksize;
36283 @@ -891,6 +915,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36284         file = fget(arg);
36285         if (!file)
36286                 goto out;
36287 +       f = loop_real_file(file);
36288 +       if (f) {
36289 +               virt_file = file;
36290 +               file = f;
36291 +               get_file(file);
36292 +       }
36293  
36294         error = -EBUSY;
36295         if (lo->lo_state != Lo_unbound)
36296 @@ -943,6 +973,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36297         lo->lo_device = bdev;
36298         lo->lo_flags = lo_flags;
36299         lo->lo_backing_file = file;
36300 +       lo->lo_backing_virt_file = virt_file;
36301         lo->transfer = NULL;
36302         lo->ioctl = NULL;
36303         lo->lo_sizelimit = 0;
36304 @@ -975,6 +1006,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36305  
36306   out_putf:
36307         fput(file);
36308 +       if (virt_file)
36309 +               fput(virt_file);
36310   out:
36311         /* This is safe: open() is still holding a reference. */
36312         module_put(THIS_MODULE);
36313 @@ -1021,6 +1054,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
36314  static int loop_clr_fd(struct loop_device *lo)
36315  {
36316         struct file *filp = lo->lo_backing_file;
36317 +       struct file *virt_filp = lo->lo_backing_virt_file;
36318         gfp_t gfp = lo->old_gfp_mask;
36319         struct block_device *bdev = lo->lo_device;
36320  
36321 @@ -1052,6 +1086,7 @@ static int loop_clr_fd(struct loop_device *lo)
36322         spin_lock_irq(&lo->lo_lock);
36323         lo->lo_state = Lo_rundown;
36324         lo->lo_backing_file = NULL;
36325 +       lo->lo_backing_virt_file = NULL;
36326         spin_unlock_irq(&lo->lo_lock);
36327  
36328         loop_release_xfer(lo);
36329 @@ -1096,6 +1131,8 @@ static int loop_clr_fd(struct loop_device *lo)
36330          * bd_mutex which is usually taken before lo_ctl_mutex.
36331          */
36332         fput(filp);
36333 +       if (virt_filp)
36334 +               fput(virt_filp);
36335         return 0;
36336  }
36337  
36338 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
36339 index fb2237c..c3888c5 100644
36340 --- a/drivers/block/loop.h
36341 +++ b/drivers/block/loop.h
36342 @@ -46,7 +46,7 @@ struct loop_device {
36343         int             (*ioctl)(struct loop_device *, int cmd, 
36344                                  unsigned long arg); 
36345  
36346 -       struct file *   lo_backing_file;
36347 +       struct file *   lo_backing_file, *lo_backing_virt_file;
36348         struct block_device *lo_device;
36349         unsigned        lo_blocksize;
36350         void            *key_data; 
36351 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
36352 index 6da0f10..d211f9e 100644
36353 --- a/fs/aufs/f_op.c
36354 +++ b/fs/aufs/f_op.c
36355 @@ -351,7 +351,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
36356         if (IS_ERR(h_file))
36357                 goto out;
36358  
36359 -       if (au_test_loopback_kthread()) {
36360 +       if (0 && au_test_loopback_kthread()) {
36361                 au_warn_loopback(h_file->f_path.dentry->d_sb);
36362                 if (file->f_mapping != h_file->f_mapping) {
36363                         file->f_mapping = h_file->f_mapping;
36364 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
36365 index 61e9197..9910bea 100644
36366 --- a/fs/aufs/loop.c
36367 +++ b/fs/aufs/loop.c
36368 @@ -132,3 +132,19 @@ void au_loopback_fin(void)
36369                 symbol_put(loop_backing_file);
36370         au_delayed_kfree(au_warn_loopback_array);
36371  }
36372 +
36373 +/* ---------------------------------------------------------------------- */
36374 +
36375 +/* support the loopback block device insude aufs */
36376 +
36377 +struct file *aufs_real_loop(struct file *file)
36378 +{
36379 +       struct file *f;
36380 +
36381 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
36382 +       fi_read_lock(file);
36383 +       f = au_hf_top(file);
36384 +       fi_read_unlock(file);
36385 +       AuDebugOn(!f);
36386 +       return f;
36387 +}
36388 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
36389 index e2df495..36e5052 100644
36390 --- a/fs/aufs/loop.h
36391 +++ b/fs/aufs/loop.h
36392 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
36393  
36394  int au_loopback_init(void);
36395  void au_loopback_fin(void);
36396 +
36397 +struct file *aufs_real_loop(struct file *file);
36398  #else
36399 +AuStub(struct file *, loop_backing_file, return NULL)
36400 +
36401  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
36402            struct dentry *h_adding)
36403  AuStubInt0(au_test_loopback_kthread, void)
36404 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
36405  
36406  AuStubInt0(au_loopback_init, void)
36407  AuStubVoid(au_loopback_fin, void)
36408 +
36409 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
36410  #endif /* BLK_DEV_LOOP */
36411  
36412  #endif /* __KERNEL__ */
36413 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
36414 index 0e9bbc3..b90d52f 100644
36415 --- a/fs/aufs/super.c
36416 +++ b/fs/aufs/super.c
36417 @@ -837,7 +837,10 @@ static const struct super_operations aufs_sop = {
36418         .statfs         = aufs_statfs,
36419         .put_super      = aufs_put_super,
36420         .sync_fs        = aufs_sync_fs,
36421 -       .remount_fs     = aufs_remount_fs
36422 +       .remount_fs     = aufs_remount_fs,
36423 +#ifdef CONFIG_AUFS_BDEV_LOOP
36424 +       .real_loop      = aufs_real_loop
36425 +#endif
36426  };
36427  
36428  /* ---------------------------------------------------------------------- */
36429 diff --git a/include/linux/fs.h b/include/linux/fs.h
36430 index a808c7c..214b51f 100644
36431 --- a/include/linux/fs.h
36432 +++ b/include/linux/fs.h
36433 @@ -1791,6 +1791,10 @@ struct super_operations {
36434                                   struct shrink_control *);
36435         long (*free_cached_objects)(struct super_block *,
36436                                     struct shrink_control *);
36437 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
36438 +       /* and aufs */
36439 +       struct file *(*real_loop)(struct file *);
36440 +#endif
36441  };
36442  
36443  /*
This page took 4.268185 seconds and 4 git commands to generate.