]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs4.patch
- up to 4.2.1; builds on x8664; not tested
[packages/kernel.git] / kernel-aufs4.patch
1 aufs4.x-rcN kbuild patch
2
3 diff --git a/fs/Kconfig b/fs/Kconfig
4 index 011f433..b1083f6 100644
5 --- a/fs/Kconfig
6 +++ b/fs/Kconfig
7 @@ -218,6 +218,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 cb20e4b..dd81418 100644
17 --- a/fs/Makefile
18 +++ b/fs/Makefile
19 @@ -126,3 +126,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 1ff9942..31efc0a 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 8133cef..04beb19 100644
40 --- a/MAINTAINERS
41 +++ b/MAINTAINERS
42 @@ -1939,6 +1939,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 f7a4c9d..23103ad 100644
64 --- a/drivers/block/loop.c
65 +++ b/drivers/block/loop.c
66 @@ -560,6 +560,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(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 7a3f3e5..0b40298 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/read_write.c b/fs/read_write.c
105 index 819ef3f..fd0414e 100644
106 --- a/fs/read_write.c
107 +++ b/fs/read_write.c
108 @@ -494,6 +494,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
109  }
110  EXPORT_SYMBOL(__vfs_write);
111  
112 +vfs_readf_t vfs_readf(struct file *file)
113 +{
114 +       const struct file_operations *fop = file->f_op;
115 +
116 +       if (fop->read)
117 +               return fop->read;
118 +       if (fop->read_iter)
119 +               return new_sync_read;
120 +       return ERR_PTR(-ENOSYS);
121 +}
122 +
123 +vfs_writef_t vfs_writef(struct file *file)
124 +{
125 +       const struct file_operations *fop = file->f_op;
126 +
127 +       if (fop->write)
128 +               return fop->write;
129 +       if (fop->write_iter)
130 +               return new_sync_write;
131 +       return ERR_PTR(-ENOSYS);
132 +}
133 +
134  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
135  {
136         mm_segment_t old_fs;
137 diff --git a/fs/splice.c b/fs/splice.c
138 index 5fc1e50..5f8385a 100644
139 --- a/fs/splice.c
140 +++ b/fs/splice.c
141 @@ -1102,8 +1102,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
142  /*
143   * Attempt to initiate a splice from pipe to file.
144   */
145 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
146 -                          loff_t *ppos, size_t len, unsigned int flags)
147 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
148 +                   loff_t *ppos, size_t len, unsigned int flags)
149  {
150         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
151                                 loff_t *, size_t, unsigned int);
152 @@ -1119,9 +1119,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
153  /*
154   * Attempt to initiate a splice from a file to a pipe.
155   */
156 -static long do_splice_to(struct file *in, loff_t *ppos,
157 -                        struct pipe_inode_info *pipe, size_t len,
158 -                        unsigned int flags)
159 +long do_splice_to(struct file *in, loff_t *ppos,
160 +                 struct pipe_inode_info *pipe, size_t len,
161 +                 unsigned int flags)
162  {
163         ssize_t (*splice_read)(struct file *, loff_t *,
164                                struct pipe_inode_info *, size_t, unsigned int);
165 diff --git a/include/linux/file.h b/include/linux/file.h
166 index f87d308..9a290b3 100644
167 --- a/include/linux/file.h
168 +++ b/include/linux/file.h
169 @@ -19,6 +19,7 @@ struct dentry;
170  struct path;
171  extern struct file *alloc_file(struct path *, fmode_t mode,
172         const struct file_operations *fop);
173 +extern struct file *get_empty_filp(void);
174  
175  static inline void fput_light(struct file *file, int fput_needed)
176  {
177 diff --git a/include/linux/fs.h b/include/linux/fs.h
178 index a0653e5..86080ea 100644
179 --- a/include/linux/fs.h
180 +++ b/include/linux/fs.h
181 @@ -1661,6 +1661,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
182                               struct iovec *fast_pointer,
183                               struct iovec **ret_pointer);
184  
185 +typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
186 +typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
187 +                               loff_t *);
188 +vfs_readf_t vfs_readf(struct file *file);
189 +vfs_writef_t vfs_writef(struct file *file);
190 +
191  extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
192  extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
193  extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
194 diff --git a/include/linux/splice.h b/include/linux/splice.h
195 index da2751d..2e0fca6 100644
196 --- a/include/linux/splice.h
197 +++ b/include/linux/splice.h
198 @@ -83,4 +83,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
199  extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
200  
201  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
202 +
203 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
204 +                          loff_t *ppos, size_t len, unsigned int flags);
205 +extern long do_splice_to(struct file *in, loff_t *ppos,
206 +                        struct pipe_inode_info *pipe, size_t len,
207 +                        unsigned int flags);
208  #endif
209 aufs4.x-rcN mmap patch
210
211 diff --git a/fs/buffer.c b/fs/buffer.c
212 index 1cf7a53..076aff86 100644
213 --- a/fs/buffer.c
214 +++ b/fs/buffer.c
215 @@ -2473,7 +2473,7 @@ int block_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf,
216          * Update file times before taking page lock. We may end up failing the
217          * fault so this update may be superfluous but who really cares...
218          */
219 -       file_update_time(vma->vm_file);
220 +       vma_file_update_time(vma);
221  
222         ret = __block_page_mkwrite(vma, vmf, get_block);
223         sb_end_pagefault(sb);
224 diff --git a/fs/proc/base.c b/fs/proc/base.c
225 index 87782e8..b287e64 100644
226 --- a/fs/proc/base.c
227 +++ b/fs/proc/base.c
228 @@ -1934,7 +1934,7 @@ static int proc_map_files_get_link(struct dentry *dentry, struct path *path)
229         down_read(&mm->mmap_sem);
230         vma = find_exact_vma(mm, vm_start, vm_end);
231         if (vma && vma->vm_file) {
232 -               *path = vma->vm_file->f_path;
233 +               *path = vma_pr_or_file(vma)->f_path;
234                 path_get(path);
235                 rc = 0;
236         }
237 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
238 index f8595e8..cb8eda0 100644
239 --- a/fs/proc/nommu.c
240 +++ b/fs/proc/nommu.c
241 @@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
242         file = region->vm_file;
243  
244         if (file) {
245 -               struct inode *inode = file_inode(region->vm_file);
246 +               struct inode *inode;
247 +
248 +               file = vmr_pr_or_file(region);
249 +               inode = file_inode(file);
250                 dev = inode->i_sb->s_dev;
251                 ino = inode->i_ino;
252         }
253 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
254 index ca1e091..8940e47 100644
255 --- a/fs/proc/task_mmu.c
256 +++ b/fs/proc/task_mmu.c
257 @@ -279,7 +279,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
258         const char *name = NULL;
259  
260         if (file) {
261 -               struct inode *inode = file_inode(vma->vm_file);
262 +               struct inode *inode;
263 +
264 +               file = vma_pr_or_file(vma);
265 +               inode = file_inode(file);
266                 dev = inode->i_sb->s_dev;
267                 ino = inode->i_ino;
268                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
269 @@ -1479,7 +1482,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
270         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
271         struct vm_area_struct *vma = v;
272         struct numa_maps *md = &numa_priv->md;
273 -       struct file *file = vma->vm_file;
274 +       struct file *file = vma_pr_or_file(vma);
275         struct mm_struct *mm = vma->vm_mm;
276         struct mm_walk walk = {
277                 .hugetlb_entry = gather_hugetlb_stats,
278 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
279 index e0d64c9..7aa92db 100644
280 --- a/fs/proc/task_nommu.c
281 +++ b/fs/proc/task_nommu.c
282 @@ -160,7 +160,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
283         file = vma->vm_file;
284  
285         if (file) {
286 -               struct inode *inode = file_inode(vma->vm_file);
287 +               struct inode *inode;
288 +
289 +               file = vma_pr_or_file(vma);
290 +               inode = file_inode(file);
291                 dev = inode->i_sb->s_dev;
292                 ino = inode->i_ino;
293                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
294 diff --git a/include/linux/mm.h b/include/linux/mm.h
295 index 2e872f9..2494ed2 100644
296 --- a/include/linux/mm.h
297 +++ b/include/linux/mm.h
298 @@ -1173,6 +1173,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
299  }
300  #endif
301  
302 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
303 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
304 +                                     int);
305 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
306 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
307 +
308 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
309 +                                                               __LINE__)
310 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
311 +                                                         __LINE__)
312 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
313 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
314 +
315 +#ifndef CONFIG_MMU
316 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
317 +extern void vmr_do_fput(struct vm_region *, const char[], int);
318 +
319 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
320 +                                                         __LINE__)
321 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
322 +#endif /* !CONFIG_MMU */
323 +
324  extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
325  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
326                 void *buf, int len, int write);
327 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
328 index 0038ac7..409940d 100644
329 --- a/include/linux/mm_types.h
330 +++ b/include/linux/mm_types.h
331 @@ -259,6 +259,7 @@ struct vm_region {
332         unsigned long   vm_top;         /* region allocated to here */
333         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
334         struct file     *vm_file;       /* the backing file or NULL */
335 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
336  
337         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
338         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
339 @@ -323,6 +324,7 @@ struct vm_area_struct {
340         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
341                                            units, *not* PAGE_CACHE_SIZE */
342         struct file * vm_file;          /* File we map to (can be NULL). */
343 +       struct file *vm_prfile;         /* shadow of vm_file */
344         void * vm_private_data;         /* was vm_pte (shared mem) */
345  
346  #ifndef CONFIG_MMU
347 diff --git a/kernel/fork.c b/kernel/fork.c
348 index 1bfefc6..a86bd7f 100644
349 --- a/kernel/fork.c
350 +++ b/kernel/fork.c
351 @@ -456,7 +456,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
352                         struct inode *inode = file_inode(file);
353                         struct address_space *mapping = file->f_mapping;
354  
355 -                       get_file(file);
356 +                       vma_get_file(tmp);
357                         if (tmp->vm_flags & VM_DENYWRITE)
358                                 atomic_dec(&inode->i_writecount);
359                         i_mmap_lock_write(mapping);
360 diff --git a/mm/Makefile b/mm/Makefile
361 index 98c4eae..3f0c9b9 100644
362 --- a/mm/Makefile
363 +++ b/mm/Makefile
364 @@ -21,7 +21,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o \
365                            mm_init.o mmu_context.o percpu.o slab_common.o \
366                            compaction.o vmacache.o \
367                            interval_tree.o list_lru.o workingset.o \
368 -                          debug.o $(mmu-y)
369 +                          prfile.o debug.o $(mmu-y)
370  
371  obj-y += init-mm.o
372  
373 diff --git a/mm/filemap.c b/mm/filemap.c
374 index 1283fc8..128f18f 100644
375 --- a/mm/filemap.c
376 +++ b/mm/filemap.c
377 @@ -2089,7 +2089,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
378         int ret = VM_FAULT_LOCKED;
379  
380         sb_start_pagefault(inode->i_sb);
381 -       file_update_time(vma->vm_file);
382 +       vma_file_update_time(vma);
383         lock_page(page);
384         if (page->mapping != inode->i_mapping) {
385                 unlock_page(page);
386 diff --git a/mm/memory.c b/mm/memory.c
387 index a84fbb7..13973d2 100644
388 --- a/mm/memory.c
389 +++ b/mm/memory.c
390 @@ -2034,7 +2034,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,
391                 }
392  
393                 if (!page_mkwrite)
394 -                       file_update_time(vma->vm_file);
395 +                       vma_file_update_time(vma);
396         }
397  
398         return VM_FAULT_WRITE;
399 diff --git a/mm/mmap.c b/mm/mmap.c
400 index aa632ad..3ff75d3 100644
401 --- a/mm/mmap.c
402 +++ b/mm/mmap.c
403 @@ -274,7 +274,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
404         if (vma->vm_ops && vma->vm_ops->close)
405                 vma->vm_ops->close(vma);
406         if (vma->vm_file)
407 -               fput(vma->vm_file);
408 +               vma_fput(vma);
409         mpol_put(vma_policy(vma));
410         kmem_cache_free(vm_area_cachep, vma);
411         return next;
412 @@ -886,7 +886,7 @@ again:                      remove_next = 1 + (end > next->vm_end);
413         if (remove_next) {
414                 if (file) {
415                         uprobe_munmap(next, next->vm_start, next->vm_end);
416 -                       fput(file);
417 +                       vma_fput(vma);
418                 }
419                 if (next->anon_vma)
420                         anon_vma_merge(vma, next);
421 @@ -1671,8 +1671,8 @@ out:
422         return addr;
423  
424  unmap_and_free_vma:
425 +       vma_fput(vma);
426         vma->vm_file = NULL;
427 -       fput(file);
428  
429         /* Undo any partial mapping done by a device driver. */
430         unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
431 @@ -2473,7 +2473,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
432                 goto out_free_mpol;
433  
434         if (new->vm_file)
435 -               get_file(new->vm_file);
436 +               vma_get_file(new);
437  
438         if (new->vm_ops && new->vm_ops->open)
439                 new->vm_ops->open(new);
440 @@ -2492,7 +2492,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
441         if (new->vm_ops && new->vm_ops->close)
442                 new->vm_ops->close(new);
443         if (new->vm_file)
444 -               fput(new->vm_file);
445 +               vma_fput(new);
446         unlink_anon_vmas(new);
447   out_free_mpol:
448         mpol_put(vma_policy(new));
449 @@ -2635,7 +2635,6 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
450         struct vm_area_struct *vma;
451         unsigned long populate = 0;
452         unsigned long ret = -EINVAL;
453 -       struct file *file;
454  
455         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. "
456                         "See Documentation/vm/remap_file_pages.txt.\n",
457 @@ -2679,10 +2678,10 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
458                 munlock_vma_pages_range(vma, start, start + size);
459         }
460  
461 -       file = get_file(vma->vm_file);
462 +       vma_get_file(vma);
463         ret = do_mmap_pgoff(vma->vm_file, start, size,
464                         prot, flags, pgoff, &populate);
465 -       fput(file);
466 +       vma_fput(vma);
467  out:
468         up_write(&mm->mmap_sem);
469         if (populate)
470 @@ -2949,7 +2948,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
471                         if (anon_vma_clone(new_vma, vma))
472                                 goto out_free_mempol;
473                         if (new_vma->vm_file)
474 -                               get_file(new_vma->vm_file);
475 +                               vma_get_file(new_vma);
476                         if (new_vma->vm_ops && new_vma->vm_ops->open)
477                                 new_vma->vm_ops->open(new_vma);
478                         vma_link(mm, new_vma, prev, rb_link, rb_parent);
479 diff --git a/mm/nommu.c b/mm/nommu.c
480 index 58ea364..f937b7e 100644
481 --- a/mm/nommu.c
482 +++ b/mm/nommu.c
483 @@ -671,7 +671,7 @@ static void __put_nommu_region(struct vm_region *region)
484                 up_write(&nommu_region_sem);
485  
486                 if (region->vm_file)
487 -                       fput(region->vm_file);
488 +                       vmr_fput(region);
489  
490                 /* IO memory and memory shared directly out of the pagecache
491                  * from ramfs/tmpfs mustn't be released here */
492 @@ -829,7 +829,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
493         if (vma->vm_ops && vma->vm_ops->close)
494                 vma->vm_ops->close(vma);
495         if (vma->vm_file)
496 -               fput(vma->vm_file);
497 +               vma_fput(vma);
498         put_nommu_region(vma->vm_region);
499         kmem_cache_free(vm_area_cachep, vma);
500  }
501 @@ -1354,7 +1354,7 @@ unsigned long do_mmap_pgoff(struct file *file,
502                                         goto error_just_free;
503                                 }
504                         }
505 -                       fput(region->vm_file);
506 +                       vmr_fput(region);
507                         kmem_cache_free(vm_region_jar, region);
508                         region = pregion;
509                         result = start;
510 @@ -1429,10 +1429,10 @@ error_just_free:
511         up_write(&nommu_region_sem);
512  error:
513         if (region->vm_file)
514 -               fput(region->vm_file);
515 +               vmr_fput(region);
516         kmem_cache_free(vm_region_jar, region);
517         if (vma->vm_file)
518 -               fput(vma->vm_file);
519 +               vma_fput(vma);
520         kmem_cache_free(vm_area_cachep, vma);
521         return ret;
522  
523 diff --git a/mm/prfile.c b/mm/prfile.c
524 new file mode 100644
525 index 0000000..b323b8a
526 --- /dev/null
527 +++ b/mm/prfile.c
528 @@ -0,0 +1,86 @@
529 +/*
530 + * Mainly for aufs which mmap(2) diffrent file and wants to print different path
531 + * in /proc/PID/maps.
532 + * Call these functions via macros defined in linux/mm.h.
533 + *
534 + * See Documentation/filesystems/aufs/design/06mmap.txt
535 + *
536 + * Copyright (c) 2014 Junjro R. Okajima
537 + * Copyright (c) 2014 Ian Campbell
538 + */
539 +
540 +#include <linux/mm.h>
541 +#include <linux/file.h>
542 +#include <linux/fs.h>
543 +
544 +/* #define PRFILE_TRACE */
545 +static inline void prfile_trace(struct file *f, struct file *pr,
546 +                             const char func[], int line, const char func2[])
547 +{
548 +#ifdef PRFILE_TRACE
549 +       if (pr)
550 +               pr_info("%s:%d: %s, %s\n", func, line, func2,
551 +                       f ? (char *)f->f_path.dentry->d_name.name : "(null)");
552 +#endif
553 +}
554 +
555 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
556 +                            int line)
557 +{
558 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
559 +
560 +       prfile_trace(f, pr, func, line, __func__);
561 +       file_update_time(f);
562 +       if (f && pr)
563 +               file_update_time(pr);
564 +}
565 +
566 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
567 +                              int line)
568 +{
569 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
570 +
571 +       prfile_trace(f, pr, func, line, __func__);
572 +       return (f && pr) ? pr : f;
573 +}
574 +
575 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
576 +{
577 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
578 +
579 +       prfile_trace(f, pr, func, line, __func__);
580 +       get_file(f);
581 +       if (f && pr)
582 +               get_file(pr);
583 +}
584 +
585 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
586 +{
587 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
588 +
589 +       prfile_trace(f, pr, func, line, __func__);
590 +       fput(f);
591 +       if (f && pr)
592 +               fput(pr);
593 +}
594 +
595 +#ifndef CONFIG_MMU
596 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
597 +                              int line)
598 +{
599 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
600 +
601 +       prfile_trace(f, pr, func, line, __func__);
602 +       return (f && pr) ? pr : f;
603 +}
604 +
605 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
606 +{
607 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
608 +
609 +       prfile_trace(f, pr, func, line, __func__);
610 +       fput(f);
611 +       if (f && pr)
612 +               fput(pr);
613 +}
614 +#endif /* !CONFIG_MMU */
615 aufs4.x-rcN standalone patch
616
617 diff --git a/fs/dcache.c b/fs/dcache.c
618 index 0b40298..4844e61 100644
619 --- a/fs/dcache.c
620 +++ b/fs/dcache.c
621 @@ -1269,6 +1269,7 @@ rename_retry:
622         seq = 1;
623         goto again;
624  }
625 +EXPORT_SYMBOL(d_walk);
626  
627  /*
628   * Search for at least 1 mount point in the dentry's subdirs.
629 diff --git a/fs/file_table.c b/fs/file_table.c
630 index 7f9d407..8c9ec1d 100644
631 --- a/fs/file_table.c
632 +++ b/fs/file_table.c
633 @@ -146,6 +146,7 @@ over:
634         }
635         return ERR_PTR(-ENFILE);
636  }
637 +EXPORT_SYMBOL(get_empty_filp);
638  
639  /**
640   * alloc_file - allocate and initialize a 'struct file'
641 @@ -307,6 +308,7 @@ void put_filp(struct file *file)
642                 file_free(file);
643         }
644  }
645 +EXPORT_SYMBOL(put_filp);
646  
647  void __init files_init(unsigned long mempages)
648  { 
649 diff --git a/fs/inode.c b/fs/inode.c
650 index d30640f..1ecc715 100644
651 --- a/fs/inode.c
652 +++ b/fs/inode.c
653 @@ -58,6 +58,7 @@ static struct hlist_head *inode_hashtable __read_mostly;
654  static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
655  
656  __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
657 +EXPORT_SYMBOL(inode_sb_list_lock);
658  
659  /*
660   * Empty aops. Can be used for the cases where the user does not
661 diff --git a/fs/namespace.c b/fs/namespace.c
662 index c7cb8a5..08723de 100644
663 --- a/fs/namespace.c
664 +++ b/fs/namespace.c
665 @@ -463,6 +463,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
666         mnt_dec_writers(real_mount(mnt));
667         preempt_enable();
668  }
669 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
670  
671  /**
672   * mnt_drop_write - give up write access to a mount
673 @@ -1779,6 +1780,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
674         }
675         return 0;
676  }
677 +EXPORT_SYMBOL(iterate_mounts);
678  
679  static void cleanup_group_ids(struct mount *mnt, struct mount *end)
680  {
681 diff --git a/fs/notify/group.c b/fs/notify/group.c
682 index d16b62c..06ca6bc 100644
683 --- a/fs/notify/group.c
684 +++ b/fs/notify/group.c
685 @@ -22,6 +22,7 @@
686  #include <linux/srcu.h>
687  #include <linux/rculist.h>
688  #include <linux/wait.h>
689 +#include <linux/module.h>
690  
691  #include <linux/fsnotify_backend.h>
692  #include "fsnotify.h"
693 @@ -72,6 +73,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
694  {
695         atomic_inc(&group->refcnt);
696  }
697 +EXPORT_SYMBOL(fsnotify_get_group);
698  
699  /*
700   * Drop a reference to a group.  Free it if it's through.
701 @@ -81,6 +83,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
702         if (atomic_dec_and_test(&group->refcnt))
703                 fsnotify_final_destroy_group(group);
704  }
705 +EXPORT_SYMBOL(fsnotify_put_group);
706  
707  /*
708   * Create a new fsnotify_group and hold a reference for the group returned.
709 @@ -109,6 +112,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
710  
711         return group;
712  }
713 +EXPORT_SYMBOL(fsnotify_alloc_group);
714  
715  int fsnotify_fasync(int fd, struct file *file, int on)
716  {
717 diff --git a/fs/notify/mark.c b/fs/notify/mark.c
718 index 92e48c7..d2c4b68 100644
719 --- a/fs/notify/mark.c
720 +++ b/fs/notify/mark.c
721 @@ -109,6 +109,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
722                 mark->free_mark(mark);
723         }
724  }
725 +EXPORT_SYMBOL(fsnotify_put_mark);
726  
727  /* Calculate mask of events for a list of marks */
728  u32 fsnotify_recalc_mask(struct hlist_head *head)
729 @@ -202,6 +203,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
730         fsnotify_destroy_mark_locked(mark, group);
731         mutex_unlock(&group->mark_mutex);
732  }
733 +EXPORT_SYMBOL(fsnotify_destroy_mark);
734  
735  /*
736   * Destroy all marks in the given list. The marks must be already detached from
737 @@ -376,6 +378,7 @@ err:
738  
739         return ret;
740  }
741 +EXPORT_SYMBOL(fsnotify_add_mark);
742  
743  int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
744                       struct inode *inode, struct vfsmount *mnt, int allow_dups)
745 @@ -455,6 +458,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
746         atomic_set(&mark->refcnt, 1);
747         mark->free_mark = free_mark;
748  }
749 +EXPORT_SYMBOL(fsnotify_init_mark);
750  
751  static int fsnotify_mark_destroy(void *ignored)
752  {
753 diff --git a/fs/open.c b/fs/open.c
754 index e33dab2..b84b828 100644
755 --- a/fs/open.c
756 +++ b/fs/open.c
757 @@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
758         mutex_unlock(&dentry->d_inode->i_mutex);
759         return ret;
760  }
761 +EXPORT_SYMBOL(do_truncate);
762  
763  long vfs_truncate(struct path *path, loff_t length)
764  {
765 @@ -678,6 +679,7 @@ int open_check_o_direct(struct file *f)
766         }
767         return 0;
768  }
769 +EXPORT_SYMBOL(open_check_o_direct);
770  
771  static int do_dentry_open(struct file *f,
772                           struct inode *inode,
773 diff --git a/fs/read_write.c b/fs/read_write.c
774 index fd0414e..8ace6ec 100644
775 --- a/fs/read_write.c
776 +++ b/fs/read_write.c
777 @@ -504,6 +504,7 @@ vfs_readf_t vfs_readf(struct file *file)
778                 return new_sync_read;
779         return ERR_PTR(-ENOSYS);
780  }
781 +EXPORT_SYMBOL(vfs_readf);
782  
783  vfs_writef_t vfs_writef(struct file *file)
784  {
785 @@ -515,6 +516,7 @@ vfs_writef_t vfs_writef(struct file *file)
786                 return new_sync_write;
787         return ERR_PTR(-ENOSYS);
788  }
789 +EXPORT_SYMBOL(vfs_writef);
790  
791  ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
792  {
793 diff --git a/fs/splice.c b/fs/splice.c
794 index 5f8385a..f76067e 100644
795 --- a/fs/splice.c
796 +++ b/fs/splice.c
797 @@ -1115,6 +1115,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
798  
799         return splice_write(pipe, out, ppos, len, flags);
800  }
801 +EXPORT_SYMBOL(do_splice_from);
802  
803  /*
804   * Attempt to initiate a splice from a file to a pipe.
805 @@ -1141,6 +1142,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
806  
807         return splice_read(in, ppos, pipe, len, flags);
808  }
809 +EXPORT_SYMBOL(do_splice_to);
810  
811  /**
812   * splice_direct_to_actor - splices data directly between two non-pipes
813 diff --git a/fs/xattr.c b/fs/xattr.c
814 index 072fee1..a7677af 100644
815 --- a/fs/xattr.c
816 +++ b/fs/xattr.c
817 @@ -207,6 +207,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
818         *xattr_value = value;
819         return error;
820  }
821 +EXPORT_SYMBOL(vfs_getxattr_alloc);
822  
823  /* Compare an extended attribute value with the given value */
824  int vfs_xattr_cmp(struct dentry *dentry, const char *xattr_name,
825 diff --git a/security/commoncap.c b/security/commoncap.c
826 index d103f5a4..393654e 100644
827 --- a/security/commoncap.c
828 +++ b/security/commoncap.c
829 @@ -970,12 +970,14 @@ int cap_mmap_addr(unsigned long addr)
830         }
831         return ret;
832  }
833 +EXPORT_SYMBOL(cap_mmap_addr);
834  
835  int cap_mmap_file(struct file *file, unsigned long reqprot,
836                   unsigned long prot, unsigned long flags)
837  {
838         return 0;
839  }
840 +EXPORT_SYMBOL(cap_mmap_file);
841  
842  #ifdef CONFIG_SECURITY
843  
844 diff --git a/security/device_cgroup.c b/security/device_cgroup.c
845 index 188c1d2..426d9af 100644
846 --- a/security/device_cgroup.c
847 +++ b/security/device_cgroup.c
848 @@ -7,6 +7,7 @@
849  #include <linux/device_cgroup.h>
850  #include <linux/cgroup.h>
851  #include <linux/ctype.h>
852 +#include <linux/export.h>
853  #include <linux/list.h>
854  #include <linux/uaccess.h>
855  #include <linux/seq_file.h>
856 @@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
857         return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
858                         access);
859  }
860 +EXPORT_SYMBOL(__devcgroup_inode_permission);
861  
862  int devcgroup_inode_mknod(int mode, dev_t dev)
863  {
864 diff --git a/security/security.c b/security/security.c
865 index 595fffa..346bad6 100644
866 --- a/security/security.c
867 +++ b/security/security.c
868 @@ -438,6 +438,7 @@ int security_path_rmdir(struct path *dir, struct dentry *dentry)
869                 return 0;
870         return call_int_hook(path_rmdir, 0, dir, dentry);
871  }
872 +EXPORT_SYMBOL(security_path_rmdir);
873  
874  int security_path_unlink(struct path *dir, struct dentry *dentry)
875  {
876 @@ -454,6 +455,7 @@ int security_path_symlink(struct path *dir, struct dentry *dentry,
877                 return 0;
878         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
879  }
880 +EXPORT_SYMBOL(security_path_symlink);
881  
882  int security_path_link(struct dentry *old_dentry, struct path *new_dir,
883                        struct dentry *new_dentry)
884 @@ -462,6 +464,7 @@ int security_path_link(struct dentry *old_dentry, struct path *new_dir,
885                 return 0;
886         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
887  }
888 +EXPORT_SYMBOL(security_path_link);
889  
890  int security_path_rename(struct path *old_dir, struct dentry *old_dentry,
891                          struct path *new_dir, struct dentry *new_dentry,
892 @@ -489,6 +492,7 @@ int security_path_truncate(struct path *path)
893                 return 0;
894         return call_int_hook(path_truncate, 0, path);
895  }
896 +EXPORT_SYMBOL(security_path_truncate);
897  
898  int security_path_chmod(struct path *path, umode_t mode)
899  {
900 @@ -496,6 +500,7 @@ int security_path_chmod(struct path *path, umode_t mode)
901                 return 0;
902         return call_int_hook(path_chmod, 0, path, mode);
903  }
904 +EXPORT_SYMBOL(security_path_chmod);
905  
906  int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
907  {
908 @@ -503,6 +508,7 @@ int security_path_chown(struct path *path, kuid_t uid, kgid_t gid)
909                 return 0;
910         return call_int_hook(path_chown, 0, path, uid, gid);
911  }
912 +EXPORT_SYMBOL(security_path_chown);
913  
914  int security_path_chroot(struct path *path)
915  {
916 @@ -588,6 +594,7 @@ int security_inode_readlink(struct dentry *dentry)
917                 return 0;
918         return call_int_hook(inode_readlink, 0, dentry);
919  }
920 +EXPORT_SYMBOL(security_inode_readlink);
921  
922  int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
923                                bool rcu)
924 @@ -603,6 +610,7 @@ int security_inode_permission(struct inode *inode, int mask)
925                 return 0;
926         return call_int_hook(inode_permission, 0, inode, mask);
927  }
928 +EXPORT_SYMBOL(security_inode_permission);
929  
930  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
931  {
932 @@ -741,6 +749,7 @@ int security_file_permission(struct file *file, int mask)
933  
934         return fsnotify_perm(file, mask);
935  }
936 +EXPORT_SYMBOL(security_file_permission);
937  
938  int security_file_alloc(struct file *file)
939  {
940 @@ -800,6 +809,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
941                 return ret;
942         return ima_file_mmap(file, prot);
943  }
944 +EXPORT_SYMBOL(security_mmap_file);
945  
946  int security_mmap_addr(unsigned long addr)
947  {
948 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
949 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
950 +++ linux/Documentation/ABI/testing/debugfs-aufs        2015-09-24 10:47:58.244719488 +0200
951 @@ -0,0 +1,50 @@
952 +What:          /debug/aufs/si_<id>/
953 +Date:          March 2009
954 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
955 +Description:
956 +               Under /debug/aufs, a directory named si_<id> is created
957 +               per aufs mount, where <id> is a unique id generated
958 +               internally.
959 +
960 +What:          /debug/aufs/si_<id>/plink
961 +Date:          Apr 2013
962 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
963 +Description:
964 +               It has three lines and shows the information about the
965 +               pseudo-link. The first line is a single number
966 +               representing a number of buckets. The second line is a
967 +               number of pseudo-links per buckets (separated by a
968 +               blank). The last line is a single number representing a
969 +               total number of psedo-links.
970 +               When the aufs mount option 'noplink' is specified, it
971 +               will show "1\n0\n0\n".
972 +
973 +What:          /debug/aufs/si_<id>/xib
974 +Date:          March 2009
975 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
976 +Description:
977 +               It shows the consumed blocks by xib (External Inode Number
978 +               Bitmap), its block size and file size.
979 +               When the aufs mount option 'noxino' is specified, it
980 +               will be empty. About XINO files, see the aufs manual.
981 +
982 +What:          /debug/aufs/si_<id>/xino0, xino1 ... xinoN
983 +Date:          March 2009
984 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
985 +Description:
986 +               It shows the consumed blocks by xino (External Inode Number
987 +               Translation Table), its link count, block size and file
988 +               size.
989 +               When the aufs mount option 'noxino' is specified, it
990 +               will be empty. About XINO files, see the aufs manual.
991 +
992 +What:          /debug/aufs/si_<id>/xigen
993 +Date:          March 2009
994 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
995 +Description:
996 +               It shows the consumed blocks by xigen (External Inode
997 +               Generation Table), its block size and file size.
998 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
999 +               be created.
1000 +               When the aufs mount option 'noxino' is specified, it
1001 +               will be empty. About XINO files, see the aufs manual.
1002 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1003 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1004 +++ linux/Documentation/ABI/testing/sysfs-aufs  2015-09-24 10:47:58.244719488 +0200
1005 @@ -0,0 +1,31 @@
1006 +What:          /sys/fs/aufs/si_<id>/
1007 +Date:          March 2009
1008 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1009 +Description:
1010 +               Under /sys/fs/aufs, a directory named si_<id> is created
1011 +               per aufs mount, where <id> is a unique id generated
1012 +               internally.
1013 +
1014 +What:          /sys/fs/aufs/si_<id>/br0, br1 ... brN
1015 +Date:          March 2009
1016 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1017 +Description:
1018 +               It shows the abolute path of a member directory (which
1019 +               is called branch) in aufs, and its permission.
1020 +
1021 +What:          /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1022 +Date:          July 2013
1023 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1024 +Description:
1025 +               It shows the id of a member directory (which is called
1026 +               branch) in aufs.
1027 +
1028 +What:          /sys/fs/aufs/si_<id>/xi_path
1029 +Date:          March 2009
1030 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1031 +Description:
1032 +               It shows the abolute path of XINO (External Inode Number
1033 +               Bitmap, Translation Table and Generation Table) file
1034 +               even if it is the default path.
1035 +               When the aufs mount option 'noxino' is specified, it
1036 +               will be empty. About XINO files, see the aufs manual.
1037 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1038 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1039 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2015-09-24 10:47:58.244719488 +0200
1040 @@ -0,0 +1,170 @@
1041 +
1042 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1043 +# 
1044 +# This program is free software; you can redistribute it and/or modify
1045 +# it under the terms of the GNU General Public License as published by
1046 +# the Free Software Foundation; either version 2 of the License, or
1047 +# (at your option) any later version.
1048 +# 
1049 +# This program is distributed in the hope that it will be useful,
1050 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1051 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1052 +# GNU General Public License for more details.
1053 +# 
1054 +# You should have received a copy of the GNU General Public License
1055 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1056 +
1057 +Introduction
1058 +----------------------------------------
1059 +
1060 +aufs [ei ju: ef es] | [a u f s]
1061 +1. abbrev. for "advanced multi-layered unification filesystem".
1062 +2. abbrev. for "another unionfs".
1063 +3. abbrev. for "auf das" in German which means "on the" in English.
1064 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1065 +   But "Filesystem aufs Filesystem" is hard to understand.
1066 +
1067 +AUFS is a filesystem with features:
1068 +- multi layered stackable unification filesystem, the member directory
1069 +  is called as a branch.
1070 +- branch permission and attribute, 'readonly', 'real-readonly',
1071 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1072 +  combination.
1073 +- internal "file copy-on-write".
1074 +- logical deletion, whiteout.
1075 +- dynamic branch manipulation, adding, deleting and changing permission.
1076 +- allow bypassing aufs, user's direct branch access.
1077 +- external inode number translation table and bitmap which maintains the
1078 +  persistent aufs inode number.
1079 +- seekable directory, including NFS readdir.
1080 +- file mapping, mmap and sharing pages.
1081 +- pseudo-link, hardlink over branches.
1082 +- loopback mounted filesystem as a branch.
1083 +- several policies to select one among multiple writable branches.
1084 +- revert a single systemcall when an error occurs in aufs.
1085 +- and more...
1086 +
1087 +
1088 +Multi Layered Stackable Unification Filesystem
1089 +----------------------------------------------------------------------
1090 +Most people already knows what it is.
1091 +It is a filesystem which unifies several directories and provides a
1092 +merged single directory. When users access a file, the access will be
1093 +passed/re-directed/converted (sorry, I am not sure which English word is
1094 +correct) to the real file on the member filesystem. The member
1095 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1096 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1097 +readonly branch is handled by creating 'whiteout' on the upper writable
1098 +branch.
1099 +
1100 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1101 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1102 +different approaches to implement the merged-view.
1103 +The former tries putting it into VFS, and the latter implements as a
1104 +separate filesystem.
1105 +(If I misunderstand about these implementations, please let me know and
1106 +I shall correct it. Because it is a long time ago when I read their
1107 +source files last time).
1108 +
1109 +UnionMount's approach will be able to small, but may be hard to share
1110 +branches between several UnionMount since the whiteout in it is
1111 +implemented in the inode on branch filesystem and always
1112 +shared. According to Bharata's post, readdir does not seems to be
1113 +finished yet.
1114 +There are several missing features known in this implementations such as
1115 +- for users, the inode number may change silently. eg. copy-up.
1116 +- link(2) may break by copy-up.
1117 +- read(2) may get an obsoleted filedata (fstat(2) too).
1118 +- fcntl(F_SETLK) may be broken by copy-up.
1119 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1120 +  open(O_RDWR).
1121 +
1122 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1123 +merged into mainline. This is another implementation of UnionMount as a
1124 +separated filesystem. All the limitations and known problems which
1125 +UnionMount are equally inherited to "overlay" filesystem.
1126 +
1127 +Unionfs has a longer history. When I started implementing a stackable
1128 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1129 +inode, dentry and file objects and they have an array pointing lower
1130 +same kind objects. After contributing many patches for Unionfs, I
1131 +re-started my project AUFS (Jun 2006).
1132 +
1133 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1134 +implemented my own ideas, approaches and enhancements and it became
1135 +totally different one.
1136 +
1137 +Comparing DM snapshot and fs based implementation
1138 +- the number of bytes to be copied between devices is much smaller.
1139 +- the type of filesystem must be one and only.
1140 +- the fs must be writable, no readonly fs, even for the lower original
1141 +  device. so the compression fs will not be usable. but if we use
1142 +  loopback mount, we may address this issue.
1143 +  for instance,
1144 +       mount /cdrom/squashfs.img /sq
1145 +       losetup /sq/ext2.img
1146 +       losetup /somewhere/cow
1147 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1148 +- it will be difficult (or needs more operations) to extract the
1149 +  difference between the original device and COW.
1150 +- DM snapshot-merge may help a lot when users try merging. in the
1151 +  fs-layer union, users will use rsync(1).
1152 +
1153 +You may want to read my old paper "Filesystems in LiveCD"
1154 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1155 +
1156 +
1157 +Several characters/aspects/persona of aufs
1158 +----------------------------------------------------------------------
1159 +
1160 +Aufs has several characters, aspects or persona.
1161 +1. a filesystem, callee of VFS helper
1162 +2. sub-VFS, caller of VFS helper for branches
1163 +3. a virtual filesystem which maintains persistent inode number
1164 +4. reader/writer of files on branches such like an application
1165 +
1166 +1. Callee of VFS Helper
1167 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1168 +unlink(2) from an application reaches sys_unlink() kernel function and
1169 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1170 +calls filesystem specific unlink operation. Actually aufs implements the
1171 +unlink operation but it behaves like a redirector.
1172 +
1173 +2. Caller of VFS Helper for Branches
1174 +aufs_unlink() passes the unlink request to the branch filesystem as if
1175 +it were called from VFS. So the called unlink operation of the branch
1176 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1177 +every necessary pre/post operation for the branch filesystem.
1178 +- acquire the lock for the parent dir on a branch
1179 +- lookup in a branch
1180 +- revalidate dentry on a branch
1181 +- mnt_want_write() for a branch
1182 +- vfs_unlink() for a branch
1183 +- mnt_drop_write() for a branch
1184 +- release the lock on a branch
1185 +
1186 +3. Persistent Inode Number
1187 +One of the most important issue for a filesystem is to maintain inode
1188 +numbers. This is particularly important to support exporting a
1189 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1190 +backend block device for its own. But some storage is necessary to
1191 +keep and maintain the inode numbers. It may be a large space and may not
1192 +suit to keep in memory. Aufs rents some space from its first writable
1193 +branch filesystem (by default) and creates file(s) on it. These files
1194 +are created by aufs internally and removed soon (currently) keeping
1195 +opened.
1196 +Note: Because these files are removed, they are totally gone after
1197 +      unmounting aufs. It means the inode numbers are not persistent
1198 +      across unmount or reboot. I have a plan to make them really
1199 +      persistent which will be important for aufs on NFS server.
1200 +
1201 +4. Read/Write Files Internally (copy-on-write)
1202 +Because a branch can be readonly, when you write a file on it, aufs will
1203 +"copy-up" it to the upper writable branch internally. And then write the
1204 +originally requested thing to the file. Generally kernel doesn't
1205 +open/read/write file actively. In aufs, even a single write may cause a
1206 +internal "file copy". This behaviour is very similar to cp(1) command.
1207 +
1208 +Some people may think it is better to pass such work to user space
1209 +helper, instead of doing in kernel space. Actually I am still thinking
1210 +about it. But currently I have implemented it in kernel space.
1211 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1212 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1213 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2015-09-24 10:47:58.244719488 +0200
1214 @@ -0,0 +1,258 @@
1215 +
1216 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1217 +# 
1218 +# This program is free software; you can redistribute it and/or modify
1219 +# it under the terms of the GNU General Public License as published by
1220 +# the Free Software Foundation; either version 2 of the License, or
1221 +# (at your option) any later version.
1222 +# 
1223 +# This program is distributed in the hope that it will be useful,
1224 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1225 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1226 +# GNU General Public License for more details.
1227 +# 
1228 +# You should have received a copy of the GNU General Public License
1229 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1230 +
1231 +Basic Aufs Internal Structure
1232 +
1233 +Superblock/Inode/Dentry/File Objects
1234 +----------------------------------------------------------------------
1235 +As like an ordinary filesystem, aufs has its own
1236 +superblock/inode/dentry/file objects. All these objects have a
1237 +dynamically allocated array and store the same kind of pointers to the
1238 +lower filesystem, branch.
1239 +For example, when you build a union with one readwrite branch and one
1240 +readonly, mounted /au, /rw and /ro respectively.
1241 +- /au = /rw + /ro
1242 +- /ro/fileA exists but /rw/fileA
1243 +
1244 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1245 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1246 +- [0] = NULL (because /rw/fileA doesn't exist)
1247 +- [1] = /ro/fileA
1248 +
1249 +This style of an array is essentially same to the aufs
1250 +superblock/inode/dentry/file objects.
1251 +
1252 +Because aufs supports manipulating branches, ie. add/delete/change
1253 +branches dynamically, these objects has its own generation. When
1254 +branches are changed, the generation in aufs superblock is
1255 +incremented. And a generation in other object are compared when it is
1256 +accessed. When a generation in other objects are obsoleted, aufs
1257 +refreshes the internal array.
1258 +
1259 +
1260 +Superblock
1261 +----------------------------------------------------------------------
1262 +Additionally aufs superblock has some data for policies to select one
1263 +among multiple writable branches, XIB files, pseudo-links and kobject.
1264 +See below in detail.
1265 +About the policies which supports copy-down a directory, see
1266 +wbr_policy.txt too.
1267 +
1268 +
1269 +Branch and XINO(External Inode Number Translation Table)
1270 +----------------------------------------------------------------------
1271 +Every branch has its own xino (external inode number translation table)
1272 +file. The xino file is created and unlinked by aufs internally. When two
1273 +members of a union exist on the same filesystem, they share the single
1274 +xino file.
1275 +The struct of a xino file is simple, just a sequence of aufs inode
1276 +numbers which is indexed by the lower inode number.
1277 +In the above sample, assume the inode number of /ro/fileA is i111 and
1278 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1279 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1280 +
1281 +When the inode numbers are not contiguous, the xino file will be sparse
1282 +which has a hole in it and doesn't consume as much disk space as it
1283 +might appear. If your branch filesystem consumes disk space for such
1284 +holes, then you should specify 'xino=' option at mounting aufs.
1285 +
1286 +Aufs has a mount option to free the disk blocks for such holes in XINO
1287 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1288 +meet a problem of disk shortage due to XINO files, then you should try
1289 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1290 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1291 +the holes in XINO files.
1292 +
1293 +Also a writable branch has three kinds of "whiteout bases". All these
1294 +are existed when the branch is joined to aufs, and their names are
1295 +whiteout-ed doubly, so that users will never see their names in aufs
1296 +hierarchy.
1297 +1. a regular file which will be hardlinked to all whiteouts.
1298 +2. a directory to store a pseudo-link.
1299 +3. a directory to store an "orphan"-ed file temporary.
1300 +
1301 +1. Whiteout Base
1302 +   When you remove a file on a readonly branch, aufs handles it as a
1303 +   logical deletion and creates a whiteout on the upper writable branch
1304 +   as a hardlink of this file in order not to consume inode on the
1305 +   writable branch.
1306 +2. Pseudo-link Dir
1307 +   See below, Pseudo-link.
1308 +3. Step-Parent Dir
1309 +   When "fileC" exists on the lower readonly branch only and it is
1310 +   opened and removed with its parent dir, and then user writes
1311 +   something into it, then aufs copies-up fileC to this
1312 +   directory. Because there is no other dir to store fileC. After
1313 +   creating a file under this dir, the file is unlinked.
1314 +
1315 +Because aufs supports manipulating branches, ie. add/delete/change
1316 +dynamically, a branch has its own id. When the branch order changes,
1317 +aufs finds the new index by searching the branch id.
1318 +
1319 +
1320 +Pseudo-link
1321 +----------------------------------------------------------------------
1322 +Assume "fileA" exists on the lower readonly branch only and it is
1323 +hardlinked to "fileB" on the branch. When you write something to fileA,
1324 +aufs copies-up it to the upper writable branch. Additionally aufs
1325 +creates a hardlink under the Pseudo-link Directory of the writable
1326 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1327 +simple list. If fileB is read after unlinking fileA, aufs returns
1328 +filedata from the pseudo-link instead of the lower readonly
1329 +branch. Because the pseudo-link is based upon the inode, to keep the
1330 +inode number by xino (see above) is essentially necessary.
1331 +
1332 +All the hardlinks under the Pseudo-link Directory of the writable branch
1333 +should be restored in a proper location later. Aufs provides a utility
1334 +to do this. The userspace helpers executed at remounting and unmounting
1335 +aufs by default.
1336 +During this utility is running, it puts aufs into the pseudo-link
1337 +maintenance mode. In this mode, only the process which began the
1338 +maintenance mode (and its child processes) is allowed to operate in
1339 +aufs. Some other processes which are not related to the pseudo-link will
1340 +be allowed to run too, but the rest have to return an error or wait
1341 +until the maintenance mode ends. If a process already acquires an inode
1342 +mutex (in VFS), it has to return an error.
1343 +
1344 +
1345 +XIB(external inode number bitmap)
1346 +----------------------------------------------------------------------
1347 +Addition to the xino file per a branch, aufs has an external inode number
1348 +bitmap in a superblock object. It is also an internal file such like a
1349 +xino file.
1350 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1351 +not.
1352 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1353 +
1354 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1355 +reduce the number of consumed disk blocks for these files.
1356 +
1357 +
1358 +Virtual or Vertical Dir, and Readdir in Userspace
1359 +----------------------------------------------------------------------
1360 +In order to support multiple layers (branches), aufs readdir operation
1361 +constructs a virtual dir block on memory. For readdir, aufs calls
1362 +vfs_readdir() internally for each dir on branches, merges their entries
1363 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1364 +object. So the file object has its entry list until it is closed. The
1365 +entry list will be updated when the file position is zero and becomes
1366 +obsoleted. This decision is made in aufs automatically.
1367 +
1368 +The dynamically allocated memory block for the name of entries has a
1369 +unit of 512 bytes (by default) and stores the names contiguously (no
1370 +padding). Another block for each entry is handled by kmem_cache too.
1371 +During building dir blocks, aufs creates hash list and judging whether
1372 +the entry is whiteouted by its upper branch or already listed.
1373 +The merged result is cached in the corresponding inode object and
1374 +maintained by a customizable life-time option.
1375 +
1376 +Some people may call it can be a security hole or invite DoS attack
1377 +since the opened and once readdir-ed dir (file object) holds its entry
1378 +list and becomes a pressure for system memory. But I'd say it is similar
1379 +to files under /proc or /sys. The virtual files in them also holds a
1380 +memory page (generally) while they are opened. When an idea to reduce
1381 +memory for them is introduced, it will be applied to aufs too.
1382 +For those who really hate this situation, I've developed readdir(3)
1383 +library which operates this merging in userspace. You just need to set
1384 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1385 +kernel space for readdir(3).
1386 +
1387 +
1388 +Workqueue
1389 +----------------------------------------------------------------------
1390 +Aufs sometimes requires privilege access to a branch. For instance,
1391 +in copy-up/down operation. When a user process is going to make changes
1392 +to a file which exists in the lower readonly branch only, and the mode
1393 +of one of ancestor directories may not be writable by a user
1394 +process. Here aufs copy-up the file with its ancestors and they may
1395 +require privilege to set its owner/group/mode/etc.
1396 +This is a typical case of a application character of aufs (see
1397 +Introduction).
1398 +
1399 +Aufs uses workqueue synchronously for this case. It creates its own
1400 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1401 +passes the request to call mkdir or write (for example), and wait for
1402 +its completion. This approach solves a problem of a signal handler
1403 +simply.
1404 +If aufs didn't adopt the workqueue and changed the privilege of the
1405 +process, then the process may receive the unexpected SIGXFSZ or other
1406 +signals.
1407 +
1408 +Also aufs uses the system global workqueue ("events" kernel thread) too
1409 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1410 +whiteout base and etc. This is unrelated to a privilege.
1411 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1412 +superblock at the beginning, at the same time waits for the completion
1413 +of all queued asynchronous tasks.
1414 +
1415 +
1416 +Whiteout
1417 +----------------------------------------------------------------------
1418 +The whiteout in aufs is very similar to Unionfs's. That is represented
1419 +by its filename. UnionMount takes an approach of a file mode, but I am
1420 +afraid several utilities (find(1) or something) will have to support it.
1421 +
1422 +Basically the whiteout represents "logical deletion" which stops aufs to
1423 +lookup further, but also it represents "dir is opaque" which also stop
1424 +further lookup.
1425 +
1426 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1427 +In order to make several functions in a single systemcall to be
1428 +revertible, aufs adopts an approach to rename a directory to a temporary
1429 +unique whiteouted name.
1430 +For example, in rename(2) dir where the target dir already existed, aufs
1431 +renames the target dir to a temporary unique whiteouted name before the
1432 +actual rename on a branch, and then handles other actions (make it opaque,
1433 +update the attributes, etc). If an error happens in these actions, aufs
1434 +simply renames the whiteouted name back and returns an error. If all are
1435 +succeeded, aufs registers a function to remove the whiteouted unique
1436 +temporary name completely and asynchronously to the system global
1437 +workqueue.
1438 +
1439 +
1440 +Copy-up
1441 +----------------------------------------------------------------------
1442 +It is a well-known feature or concept.
1443 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1444 +internally and makes change to the new file on the upper writable branch.
1445 +When the trigger systemcall does not update the timestamps of the parent
1446 +dir, aufs reverts it after copy-up.
1447 +
1448 +
1449 +Move-down (aufs3.9 and later)
1450 +----------------------------------------------------------------------
1451 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1452 +the lower readonly branch to the upper writable branch when a user
1453 +changes something about the file.
1454 +"Move-down" is an opposite action of copy-up. Basically this action is
1455 +ran manually instead of automatically and internally.
1456 +For desgin and implementation, aufs has to consider these issues.
1457 +- whiteout for the file may exist on the lower branch.
1458 +- ancestor directories may not exist on the lower branch.
1459 +- diropq for the ancestor directories may exist on the upper branch.
1460 +- free space on the lower branch will reduce.
1461 +- another access to the file may happen during moving-down, including
1462 +  UDBA (see "Revalidate Dentry and UDBA").
1463 +- the file should not be hard-linked nor pseudo-linked. they should be
1464 +  handled by auplink utility later.
1465 +
1466 +Sometimes users want to move-down a file from the upper writable branch
1467 +to the lower readonly or writable branch. For instance,
1468 +- the free space of the upper writable branch is going to run out.
1469 +- create a new intermediate branch between the upper and lower branch.
1470 +- etc.
1471 +
1472 +For this purpose, use "aumvdown" command in aufs-util.git.
1473 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1474 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1475 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2015-09-24 10:47:58.244719488 +0200
1476 @@ -0,0 +1,85 @@
1477 +
1478 +# Copyright (C) 2015 Junjiro R. Okajima
1479 +# 
1480 +# This program is free software; you can redistribute it and/or modify
1481 +# it under the terms of the GNU General Public License as published by
1482 +# the Free Software Foundation; either version 2 of the License, or
1483 +# (at your option) any later version.
1484 +# 
1485 +# This program is distributed in the hope that it will be useful,
1486 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1487 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1488 +# GNU General Public License for more details.
1489 +# 
1490 +# You should have received a copy of the GNU General Public License
1491 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1492 +
1493 +Support for a branch who has its ->atomic_open()
1494 +----------------------------------------------------------------------
1495 +The filesystems who implement its ->atomic_open() are not majority. For
1496 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1497 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1498 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1499 +sure whether all filesystems who have ->atomic_open() behave like this,
1500 +but NFSv4 surely returns the error.
1501 +
1502 +In order to support ->atomic_open() for aufs, there are a few
1503 +approaches.
1504 +
1505 +A. Introduce aufs_atomic_open()
1506 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1507 +     branch fs.
1508 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1509 +   an aufs user Pip Cet's approach
1510 +   - calls aufs_create(), VFS finish_open() and notify_change().
1511 +   - pass fake-mode to finish_open(), and then correct the mode by
1512 +     notify_change().
1513 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1514 +   - no aufs_atomic_open().
1515 +   - aufs_lookup() registers the TID to an aufs internal object.
1516 +   - aufs_create() does nothing when the matching TID is registered, but
1517 +     registers the mode.
1518 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1519 +     TID is registered.
1520 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1521 +   credential
1522 +   - no aufs_atomic_open().
1523 +   - aufs_create() registers the TID to an internal object. this info
1524 +     represents "this process created this file just now."
1525 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1526 +     registered TID and re-try open() with superuser's credential.
1527 +
1528 +Pros and cons for each approach.
1529 +
1530 +A.
1531 +   - straightforward but highly depends upon VFS internal.
1532 +   - the atomic behavaiour is kept.
1533 +   - some of parameters such as nameidata are hard to reproduce for
1534 +     branch fs.
1535 +   - large overhead.
1536 +B.
1537 +   - easy to implement.
1538 +   - the atomic behavaiour is lost.
1539 +C.
1540 +   - the atomic behavaiour is kept.
1541 +   - dirty and tricky.
1542 +   - VFS checks whether the file is created correctly after calling
1543 +     ->create(), which means this approach doesn't work.
1544 +D.
1545 +   - easy to implement.
1546 +   - the atomic behavaiour is lost.
1547 +   - to open a file with superuser's credential and give it to a user
1548 +     process is a bad idea, since the file object keeps the credential
1549 +     in it. It may affect LSM or something. This approach doesn't work
1550 +     either.
1551 +
1552 +The approach A is ideal, but it hard to implement. So here is a
1553 +variation of A, which is to be implemented.
1554 +
1555 +A-1. Introduce aufs_atomic_open()
1556 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1557 +       vfs_create() and finish_open().
1558 +     - the demerit is that the several checks after branch fs
1559 +       ->atomic_open() are lost. in the ordinary case, the checks are
1560 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1561 +       be implemented in aufs, but not all I am afraid.
1562 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1563 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1564 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2015-09-24 10:47:58.244719488 +0200
1565 @@ -0,0 +1,113 @@
1566 +
1567 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1568 +# 
1569 +# This program is free software; you can redistribute it and/or modify
1570 +# it under the terms of the GNU General Public License as published by
1571 +# the Free Software Foundation; either version 2 of the License, or
1572 +# (at your option) any later version.
1573 +# 
1574 +# This program is distributed in the hope that it will be useful,
1575 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1576 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1577 +# GNU General Public License for more details.
1578 +# 
1579 +# You should have received a copy of the GNU General Public License
1580 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1581 +
1582 +Lookup in a Branch
1583 +----------------------------------------------------------------------
1584 +Since aufs has a character of sub-VFS (see Introduction), it operates
1585 +lookup for branches as VFS does. It may be a heavy work. But almost all
1586 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1587 +directly connected to its parent. Digging down the directory hierarchy
1588 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1589 +aufs calls it.
1590 +
1591 +When a branch is a remote filesystem, aufs basically relies upon its
1592 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1593 +them.
1594 +For d_revalidate, aufs implements three levels of revalidate tests. See
1595 +"Revalidate Dentry and UDBA" in detail.
1596 +
1597 +
1598 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1599 +----------------------------------------------------------------------
1600 +Let's try case study.
1601 +- aufs has two branches, upper readwrite and lower readonly.
1602 +  /au = /rw + /ro
1603 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1604 +- user invoked "chmod a+rx /au/dirA"
1605 +- the internal copy-up is activated and "/rw/dirA" is created and its
1606 +  permission bits are set to world readable.
1607 +- then "/au/dirA" becomes world readable?
1608 +
1609 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1610 +or it may be a natively readonly filesystem. If aufs respects the lower
1611 +branch, it should not respond readdir request from other users. But user
1612 +allowed it by chmod. Should really aufs rejects showing the entries
1613 +under /ro/dirA?
1614 +
1615 +To be honest, I don't have a good solution for this case. So aufs
1616 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1617 +users.
1618 +When dirperm1 is specified, aufs checks only the highest one for the
1619 +directory permission, and shows the entries. Otherwise, as usual, checks
1620 +every dir existing on all branches and rejects the request.
1621 +
1622 +As a side effect, dirperm1 option improves the performance of aufs
1623 +because the number of permission check is reduced when the number of
1624 +branch is many.
1625 +
1626 +
1627 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1628 +----------------------------------------------------------------------
1629 +Generally VFS helpers re-validate a dentry as a part of lookup.
1630 +0. digging down the directory hierarchy.
1631 +1. lock the parent dir by its i_mutex.
1632 +2. lookup the final (child) entry.
1633 +3. revalidate it.
1634 +4. call the actual operation (create, unlink, etc.)
1635 +5. unlock the parent dir
1636 +
1637 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1638 +called. Actually aufs implements it and checks the dentry on a branch is
1639 +still valid.
1640 +But it is not enough. Because aufs has to release the lock for the
1641 +parent dir on a branch at the end of ->lookup() (step 2) and
1642 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1643 +held by VFS.
1644 +If the file on a branch is changed directly, eg. bypassing aufs, after
1645 +aufs released the lock, then the subsequent operation may cause
1646 +something unpleasant result.
1647 +
1648 +This situation is a result of VFS architecture, ->lookup() and
1649 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1650 +design from VFS's point of view. It is just not suitable for sub-VFS
1651 +character in aufs.
1652 +
1653 +Aufs supports such case by three level of revalidation which is
1654 +selectable by user.
1655 +1. Simple Revalidate
1656 +   Addition to the native flow in VFS's, confirm the child-parent
1657 +   relationship on the branch just after locking the parent dir on the
1658 +   branch in the "actual operation" (step 4). When this validation
1659 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1660 +   checks the validation of the dentry on branches.
1661 +2. Monitor Changes Internally by Inotify/Fsnotify
1662 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1663 +   the dentry on the branch, and returns EBUSY if it finds different
1664 +   dentry.
1665 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1666 +   during it is in cache. When the event is notified, aufs registers a
1667 +   function to kernel 'events' thread by schedule_work(). And the
1668 +   function sets some special status to the cached aufs dentry and inode
1669 +   private data. If they are not cached, then aufs has nothing to
1670 +   do. When the same file is accessed through aufs (step 0-3) later,
1671 +   aufs will detect the status and refresh all necessary data.
1672 +   In this mode, aufs has to ignore the event which is fired by aufs
1673 +   itself.
1674 +3. No Extra Validation
1675 +   This is the simplest test and doesn't add any additional revalidation
1676 +   test, and skip the revalidation in step 4. It is useful and improves
1677 +   aufs performance when system surely hide the aufs branches from user,
1678 +   by over-mounting something (or another method).
1679 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1680 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1681 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2015-09-24 10:47:58.244719488 +0200
1682 @@ -0,0 +1,74 @@
1683 +
1684 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1685 +# 
1686 +# This program is free software; you can redistribute it and/or modify
1687 +# it under the terms of the GNU General Public License as published by
1688 +# the Free Software Foundation; either version 2 of the License, or
1689 +# (at your option) any later version.
1690 +# 
1691 +# This program is distributed in the hope that it will be useful,
1692 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1693 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1694 +# GNU General Public License for more details.
1695 +# 
1696 +# You should have received a copy of the GNU General Public License
1697 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1698 +
1699 +Branch Manipulation
1700 +
1701 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1702 +and changing its permission/attribute, there are a lot of works to do.
1703 +
1704 +
1705 +Add a Branch
1706 +----------------------------------------------------------------------
1707 +o Confirm the adding dir exists outside of aufs, including loopback
1708 +  mount, and its various attributes.
1709 +o Initialize the xino file and whiteout bases if necessary.
1710 +  See struct.txt.
1711 +
1712 +o Check the owner/group/mode of the directory
1713 +  When the owner/group/mode of the adding directory differs from the
1714 +  existing branch, aufs issues a warning because it may impose a
1715 +  security risk.
1716 +  For example, when a upper writable branch has a world writable empty
1717 +  top directory, a malicious user can create any files on the writable
1718 +  branch directly, like copy-up and modify manually. If something like
1719 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1720 +  writable branch, and the writable branch is world-writable, then a
1721 +  malicious guy may create /etc/passwd on the writable branch directly
1722 +  and the infected file will be valid in aufs.
1723 +  I am afraid it can be a security issue, but aufs can do nothing except
1724 +  producing a warning.
1725 +
1726 +
1727 +Delete a Branch
1728 +----------------------------------------------------------------------
1729 +o Confirm the deleting branch is not busy
1730 +  To be general, there is one merit to adopt "remount" interface to
1731 +  manipulate branches. It is to discard caches. At deleting a branch,
1732 +  aufs checks the still cached (and connected) dentries and inodes. If
1733 +  there are any, then they are all in-use. An inode without its
1734 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1735 +
1736 +  For the cached one, aufs checks whether the same named entry exists on
1737 +  other branches.
1738 +  If the cached one is a directory, because aufs provides a merged view
1739 +  to users, as long as one dir is left on any branch aufs can show the
1740 +  dir to users. In this case, the branch can be removed from aufs.
1741 +  Otherwise aufs rejects deleting the branch.
1742 +
1743 +  If any file on the deleting branch is opened by aufs, then aufs
1744 +  rejects deleting.
1745 +
1746 +
1747 +Modify the Permission of a Branch
1748 +----------------------------------------------------------------------
1749 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1750 +  See struct.txt.
1751 +
1752 +o rw --> ro: Confirm the modifying branch is not busy
1753 +  Aufs rejects the request if any of these conditions are true.
1754 +  - a file on the branch is mmap-ed.
1755 +  - a regular file on the branch is opened for write and there is no
1756 +    same named entry on the upper branch.
1757 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1758 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1759 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2015-09-24 10:47:58.244719488 +0200
1760 @@ -0,0 +1,64 @@
1761 +
1762 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1763 +# 
1764 +# This program is free software; you can redistribute it and/or modify
1765 +# it under the terms of the GNU General Public License as published by
1766 +# the Free Software Foundation; either version 2 of the License, or
1767 +# (at your option) any later version.
1768 +# 
1769 +# This program is distributed in the hope that it will be useful,
1770 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1771 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1772 +# GNU General Public License for more details.
1773 +# 
1774 +# You should have received a copy of the GNU General Public License
1775 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1776 +
1777 +Policies to Select One among Multiple Writable Branches
1778 +----------------------------------------------------------------------
1779 +When the number of writable branch is more than one, aufs has to decide
1780 +the target branch for file creation or copy-up. By default, the highest
1781 +writable branch which has the parent (or ancestor) dir of the target
1782 +file is chosen (top-down-parent policy).
1783 +By user's request, aufs implements some other policies to select the
1784 +writable branch, for file creation several policies, round-robin,
1785 +most-free-space, and other policies. For copy-up, top-down-parent,
1786 +bottom-up-parent, bottom-up and others.
1787 +
1788 +As expected, the round-robin policy selects the branch in circular. When
1789 +you have two writable branches and creates 10 new files, 5 files will be
1790 +created for each branch. mkdir(2) systemcall is an exception. When you
1791 +create 10 new directories, all will be created on the same branch.
1792 +And the most-free-space policy selects the one which has most free
1793 +space among the writable branches. The amount of free space will be
1794 +checked by aufs internally, and users can specify its time interval.
1795 +
1796 +The policies for copy-up is more simple,
1797 +top-down-parent is equivalent to the same named on in create policy,
1798 +bottom-up-parent selects the writable branch where the parent dir
1799 +exists and the nearest upper one from the copyup-source,
1800 +bottom-up selects the nearest upper writable branch from the
1801 +copyup-source, regardless the existence of the parent dir.
1802 +
1803 +There are some rules or exceptions to apply these policies.
1804 +- If there is a readonly branch above the policy-selected branch and
1805 +  the parent dir is marked as opaque (a variation of whiteout), or the
1806 +  target (creating) file is whiteout-ed on the upper readonly branch,
1807 +  then the result of the policy is ignored and the target file will be
1808 +  created on the nearest upper writable branch than the readonly branch.
1809 +- If there is a writable branch above the policy-selected branch and
1810 +  the parent dir is marked as opaque or the target file is whiteouted
1811 +  on the branch, then the result of the policy is ignored and the target
1812 +  file will be created on the highest one among the upper writable
1813 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1814 +  it as usual.
1815 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1816 +  They try selecting the branch where the source exists as possible
1817 +  since copyup a large file will take long time. If it can't be,
1818 +  ie. the branch where the source exists is readonly, then they will
1819 +  follow the copyup policy.
1820 +- There is an exception for rename(2) when the target exists.
1821 +  If the rename target exists, aufs compares the index of the branches
1822 +  where the source and the target exists and selects the higher
1823 +  one. If the selected branch is readonly, then aufs follows the
1824 +  copyup policy.
1825 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1826 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1827 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2015-09-24 10:47:58.244719488 +0200
1828 @@ -0,0 +1,120 @@
1829 +
1830 +# Copyright (C) 2011-2015 Junjiro R. Okajima
1831 +# 
1832 +# This program is free software; you can redistribute it and/or modify
1833 +# it under the terms of the GNU General Public License as published by
1834 +# the Free Software Foundation; either version 2 of the License, or
1835 +# (at your option) any later version.
1836 +# 
1837 +# This program is distributed in the hope that it will be useful,
1838 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1839 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1840 +# GNU General Public License for more details.
1841 +# 
1842 +# You should have received a copy of the GNU General Public License
1843 +# along with this program; if not, write to the Free Software
1844 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1845 +
1846 +
1847 +File-based Hierarchical Storage Management (FHSM)
1848 +----------------------------------------------------------------------
1849 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1850 +storage world. Aufs provides this feature as file-based with multiple
1851 +writable branches, based upon the principle of "Colder, the Lower".
1852 +Here the word "colder" means that the less used files, and "lower" means
1853 +that the position in the order of the stacked branches vertically.
1854 +These multiple writable branches are prioritized, ie. the topmost one
1855 +should be the fastest drive and be used heavily.
1856 +
1857 +o Characters in aufs FHSM story
1858 +- aufs itself and a new branch attribute.
1859 +- a new ioctl interface to move-down and to establish a connection with
1860 +  the daemon ("move-down" is a converse of "copy-up").
1861 +- userspace tool and daemon.
1862 +
1863 +The userspace daemon establishes a connection with aufs and waits for
1864 +the notification. The notified information is very similar to struct
1865 +statfs containing the number of consumed blocks and inodes.
1866 +When the consumed blocks/inodes of a branch exceeds the user-specified
1867 +upper watermark, the daemon activates its move-down process until the
1868 +consumed blocks/inodes reaches the user-specified lower watermark.
1869 +
1870 +The actual move-down is done by aufs based upon the request from
1871 +user-space since we need to maintain the inode number and the internal
1872 +pointer arrays in aufs.
1873 +
1874 +Currently aufs FHSM handles the regular files only. Additionally they
1875 +must not be hard-linked nor pseudo-linked.
1876 +
1877 +
1878 +o Cowork of aufs and the user-space daemon
1879 +  During the userspace daemon established the connection, aufs sends a
1880 +  small notification to it whenever aufs writes something into the
1881 +  writable branch. But it may cost high since aufs issues statfs(2)
1882 +  internally. So user can specify a new option to cache the
1883 +  info. Actually the notification is controlled by these factors.
1884 +  + the specified cache time.
1885 +  + classified as "force" by aufs internally.
1886 +  Until the specified time expires, aufs doesn't send the info
1887 +  except the forced cases. When aufs decide forcing, the info is always
1888 +  notified to userspace.
1889 +  For example, the number of free inodes is generally large enough and
1890 +  the shortage of it happens rarely. So aufs doesn't force the
1891 +  notification when creating a new file, directory and others. This is
1892 +  the typical case which aufs doesn't force.
1893 +  When aufs writes the actual filedata and the files consumes any of new
1894 +  blocks, the aufs forces notifying.
1895 +
1896 +
1897 +o Interfaces in aufs
1898 +- New branch attribute.
1899 +  + fhsm
1900 +    Specifies that the branch is managed by FHSM feature. In other word,
1901 +    participant in the FHSM.
1902 +    When nofhsm is set to the branch, it will not be the source/target
1903 +    branch of the move-down operation. This attribute is set
1904 +    independently from coo and moo attributes, and if you want full
1905 +    FHSM, you should specify them as well.
1906 +- New mount option.
1907 +  + fhsm_sec
1908 +    Specifies a second to suppress many less important info to be
1909 +    notified.
1910 +- New ioctl.
1911 +  + AUFS_CTL_FHSM_FD
1912 +    create a new file descriptor which userspace can read the notification
1913 +    (a subset of struct statfs) from aufs.
1914 +- Module parameter 'brs'
1915 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
1916 +  be set.
1917 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
1918 +  When there are two or more branches with fhsm attributes,
1919 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
1920 +  terminates it. As a result of remounting and branch-manipulation, the
1921 +  number of branches with fhsm attribute can be one. In this case,
1922 +  /sbin/mount.aufs will terminate the user-space daemon.
1923 +
1924 +
1925 +Finally the operation is done as these steps in kernel-space.
1926 +- make sure that,
1927 +  + no one else is using the file.
1928 +  + the file is not hard-linked.
1929 +  + the file is not pseudo-linked.
1930 +  + the file is a regular file.
1931 +  + the parent dir is not opaqued.
1932 +- find the target writable branch.
1933 +- make sure the file is not whiteout-ed by the upper (than the target)
1934 +  branch.
1935 +- make the parent dir on the target branch.
1936 +- mutex lock the inode on the branch.
1937 +- unlink the whiteout on the target branch (if exists).
1938 +- lookup and create the whiteout-ed temporary name on the target branch.
1939 +- copy the file as the whiteout-ed temporary name on the target branch.
1940 +- rename the whiteout-ed temporary name to the original name.
1941 +- unlink the file on the source branch.
1942 +- maintain the internal pointer array and the external inode number
1943 +  table (XINO).
1944 +- maintain the timestamps and other attributes of the parent dir and the
1945 +  file.
1946 +
1947 +And of course, in every step, an error may happen. So the operation
1948 +should restore the original file state after an error happens.
1949 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
1950 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
1951 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2015-09-24 10:47:58.244719488 +0200
1952 @@ -0,0 +1,72 @@
1953 +
1954 +# Copyright (C) 2005-2015 Junjiro R. Okajima
1955 +# 
1956 +# This program is free software; you can redistribute it and/or modify
1957 +# it under the terms of the GNU General Public License as published by
1958 +# the Free Software Foundation; either version 2 of the License, or
1959 +# (at your option) any later version.
1960 +# 
1961 +# This program is distributed in the hope that it will be useful,
1962 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1963 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1964 +# GNU General Public License for more details.
1965 +# 
1966 +# You should have received a copy of the GNU General Public License
1967 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1968 +
1969 +mmap(2) -- File Memory Mapping
1970 +----------------------------------------------------------------------
1971 +In aufs, the file-mapped pages are handled by a branch fs directly, no
1972 +interaction with aufs. It means aufs_mmap() calls the branch fs's
1973 +->mmap().
1974 +This approach is simple and good, but there is one problem.
1975 +Under /proc, several entries show the mmapped files by its path (with
1976 +device and inode number), and the printed path will be the path on the
1977 +branch fs's instead of virtual aufs's.
1978 +This is not a problem in most cases, but some utilities lsof(1) (and its
1979 +user) may expect the path on aufs.
1980 +
1981 +To address this issue, aufs adds a new member called vm_prfile in struct
1982 +vm_area_struct (and struct vm_region). The original vm_file points to
1983 +the file on the branch fs in order to handle everything correctly as
1984 +usual. The new vm_prfile points to a virtual file in aufs, and the
1985 +show-functions in procfs refers to vm_prfile if it is set.
1986 +Also we need to maintain several other places where touching vm_file
1987 +such like
1988 +- fork()/clone() copies vma and the reference count of vm_file is
1989 +  incremented.
1990 +- merging vma maintains the ref count too.
1991 +
1992 +This is not a good approach. It just fakes the printed path. But it
1993 +leaves all behaviour around f_mapping unchanged. This is surely an
1994 +advantage.
1995 +Actually aufs had adopted another complicated approach which calls
1996 +generic_file_mmap() and handles struct vm_operations_struct. In this
1997 +approach, aufs met a hard problem and I could not solve it without
1998 +switching the approach.
1999 +
2000 +There may be one more another approach which is
2001 +- bind-mount the branch-root onto the aufs-root internally
2002 +- grab the new vfsmount (ie. struct mount)
2003 +- lazy-umount the branch-root internally
2004 +- in open(2) the aufs-file, open the branch-file with the hidden
2005 +  vfsmount (instead of the original branch's vfsmount)
2006 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2007 +  but it may be possible from userspace by the mount helper.
2008 +
2009 +Adding the internal hidden vfsmount and using it in opening a file, the
2010 +file path under /proc will be printed correctly. This approach looks
2011 +smarter, but is not possible I am afraid.
2012 +- aufs-root may be bind-mount later. when it happens, another hidden
2013 +  vfsmount will be required.
2014 +- it is hard to get the chance to bind-mount and lazy-umount
2015 +  + in kernel-space, FS can have vfsmount in open(2) via
2016 +    file->f_path, and aufs can know its vfsmount. But several locks are
2017 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2018 +    here, then it may cause a deadlock.
2019 +  + in user-space, bind-mount doesn't invoke the mount helper.
2020 +- since /proc shows dev and ino, aufs has to give vma these info. it
2021 +  means a new member vm_prinode will be necessary. this is essentially
2022 +  equivalent to vm_prfile described above.
2023 +
2024 +I have to give up this "looks-smater" approach.
2025 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2026 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2027 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2015-09-24 10:47:58.244719488 +0200
2028 @@ -0,0 +1,96 @@
2029 +
2030 +# Copyright (C) 2014-2015 Junjiro R. Okajima
2031 +#
2032 +# This program is free software; you can redistribute it and/or modify
2033 +# it under the terms of the GNU General Public License as published by
2034 +# the Free Software Foundation; either version 2 of the License, or
2035 +# (at your option) any later version.
2036 +#
2037 +# This program is distributed in the hope that it will be useful,
2038 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2039 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2040 +# GNU General Public License for more details.
2041 +#
2042 +# You should have received a copy of the GNU General Public License
2043 +# along with this program; if not, write to the Free Software
2044 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2045 +
2046 +
2047 +Listing XATTR/EA and getting the value
2048 +----------------------------------------------------------------------
2049 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2050 +shows the values from the topmost existing file. This behaviour is good
2051 +for the non-dir entries since the bahaviour exactly matches the shown
2052 +information. But for the directories, aufs considers all the same named
2053 +entries on the lower branches. Which means, if one of the lower entry
2054 +rejects readdir call, then aufs returns an error even if the topmost
2055 +entry allows it. This behaviour is necessary to respect the branch fs's
2056 +security, but can make users confused since the user-visible standard
2057 +attributes don't match the behaviour.
2058 +To address this issue, aufs has a mount option called dirperm1 which
2059 +checks the permission for the topmost entry only, and ignores the lower
2060 +entry's permission.
2061 +
2062 +A similar issue can happen around XATTR.
2063 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2064 +always set. Otherwise these very unpleasant situation would happen.
2065 +- listxattr(2) may return the duplicated entries.
2066 +- users may not be able to remove or reset the XATTR forever,
2067 +
2068 +
2069 +XATTR/EA support in the internal (copy,move)-(up,down)
2070 +----------------------------------------------------------------------
2071 +Generally the extended attributes of inode are categorized as these.
2072 +- "security" for LSM and capability.
2073 +- "system" for posix ACL, 'acl' mount option is required for the branch
2074 +  fs generally.
2075 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2076 +- "user" for userspace, 'user_xattr' mount option is required for the
2077 +  branch fs generally.
2078 +
2079 +Moreover there are some other categories. Aufs handles these rather
2080 +unpopular categories as the ordinary ones, ie. there is no special
2081 +condition nor exception.
2082 +
2083 +In copy-up, the support for XATTR on the dst branch may differ from the
2084 +src branch. In this case, the copy-up operation will get an error and
2085 +the original user operation which triggered the copy-up will fail. It
2086 +can happen that even all copy-up will fail.
2087 +When both of src and dst branches support XATTR and if an error occurs
2088 +during copying XATTR, then the copy-up should fail obviously. That is a
2089 +good reason and aufs should return an error to userspace. But when only
2090 +the src branch support that XATTR, aufs should not return an error.
2091 +For example, the src branch supports ACL but the dst branch doesn't
2092 +because the dst branch may natively un-support it or temporary
2093 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2094 +may NOT return an error even if the XATTR is not supported. It is
2095 +totally up to the branch fs.
2096 +
2097 +Anyway when the aufs internal copy-up gets an error from the dst branch
2098 +fs, then aufs tries removing the just copied entry and returns the error
2099 +to the userspace. The worst case of this situation will be all copy-up
2100 +will fail.
2101 +
2102 +For the copy-up operation, there two basic approaches.
2103 +- copy the specified XATTR only (by category above), and return the
2104 +  error unconditionally if it happens.
2105 +- copy all XATTR, and ignore the error on the specified category only.
2106 +
2107 +In order to support XATTR and to implement the correct behaviour, aufs
2108 +chooses the latter approach and introduces some new branch attributes,
2109 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2110 +They correspond to the XATTR namespaces (see above). Additionally, to be
2111 +convenient, "icex" is also provided which means all "icex*" attributes
2112 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2113 +
2114 +The meaning of these attributes is to ignore the error from setting
2115 +XATTR on that branch.
2116 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2117 +error from the dst branch according to the specified attributes.
2118 +
2119 +Some XATTR may have its default value. The default value may come from
2120 +the parent dir or the environment. If the default value is set at the
2121 +file creating-time, it will be overwritten by copy-up.
2122 +Some contradiction may happen I am afraid.
2123 +Do we need another attribute to stop copying XATTR? I am unsure. For
2124 +now, aufs implements the branch attributes to ignore the error.
2125 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2126 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2127 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2015-09-24 10:47:58.248052907 +0200
2128 @@ -0,0 +1,58 @@
2129 +
2130 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2131 +# 
2132 +# This program is free software; you can redistribute it and/or modify
2133 +# it under the terms of the GNU General Public License as published by
2134 +# the Free Software Foundation; either version 2 of the License, or
2135 +# (at your option) any later version.
2136 +# 
2137 +# This program is distributed in the hope that it will be useful,
2138 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2139 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2140 +# GNU General Public License for more details.
2141 +# 
2142 +# You should have received a copy of the GNU General Public License
2143 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2144 +
2145 +Export Aufs via NFS
2146 +----------------------------------------------------------------------
2147 +Here is an approach.
2148 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2149 +  generation.
2150 +- iget_locked(): initialize aufs inode generation for a new inode, and
2151 +  store it in xigen file.
2152 +- destroy_inode(): increment aufs inode generation and store it in xigen
2153 +  file. it is necessary even if it is not unlinked, because any data of
2154 +  inode may be changed by UDBA.
2155 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2156 +  build file handle by
2157 +  + branch id (4 bytes)
2158 +  + superblock generation (4 bytes)
2159 +  + inode number (4 or 8 bytes)
2160 +  + parent dir inode number (4 or 8 bytes)
2161 +  + inode generation (4 bytes))
2162 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2163 +    bytes)
2164 +  + file handle for a branch (by exportfs_encode_fh())
2165 +- fh_to_dentry():
2166 +  + find the index of a branch from its id in handle, and check it is
2167 +    still exist in aufs.
2168 +  + 1st level: get the inode number from handle and search it in cache.
2169 +  + 2nd level: if not found in cache, get the parent inode number from
2170 +    the handle and search it in cache. and then open the found parent
2171 +    dir, find the matching inode number by vfs_readdir() and get its
2172 +    name, and call lookup_one_len() for the target dentry.
2173 +  + 3rd level: if the parent dir is not cached, call
2174 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2175 +    build a pathname of it, convert it a pathname in aufs, call
2176 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2177 +    the 2nd level.
2178 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2179 +    for every branch, but not itself. to get this, (currently) aufs
2180 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2181 +    idea, but I didn't get other approach.
2182 +  + test the generation of the gotten inode.
2183 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2184 +  convert it into ESTALE for NFSD.
2185 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2186 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2187 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2188 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2189 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2015-09-24 10:47:58.248052907 +0200
2190 @@ -0,0 +1,52 @@
2191 +
2192 +# Copyright (C) 2005-2015 Junjiro R. Okajima
2193 +# 
2194 +# This program is free software; you can redistribute it and/or modify
2195 +# it under the terms of the GNU General Public License as published by
2196 +# the Free Software Foundation; either version 2 of the License, or
2197 +# (at your option) any later version.
2198 +# 
2199 +# This program is distributed in the hope that it will be useful,
2200 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2201 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2202 +# GNU General Public License for more details.
2203 +# 
2204 +# You should have received a copy of the GNU General Public License
2205 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2206 +
2207 +Show Whiteout Mode (shwh)
2208 +----------------------------------------------------------------------
2209 +Generally aufs hides the name of whiteouts. But in some cases, to show
2210 +them is very useful for users. For instance, creating a new middle layer
2211 +(branch) by merging existing layers.
2212 +
2213 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2214 +When you have three branches,
2215 +- Bottom: 'system', squashfs (underlying base system), read-only
2216 +- Middle: 'mods', squashfs, read-only
2217 +- Top: 'overlay', ram (tmpfs), read-write
2218 +
2219 +The top layer is loaded at boot time and saved at shutdown, to preserve
2220 +the changes made to the system during the session.
2221 +When larger changes have been made, or smaller changes have accumulated,
2222 +the size of the saved top layer data grows. At this point, it would be
2223 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2224 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2225 +restoring save and load speed.
2226 +
2227 +This merging is simplified by the use of another aufs mount, of just the
2228 +two overlay branches using the 'shwh' option.
2229 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2230 +       aufs /livesys/merge_union
2231 +
2232 +A merged view of these two branches is then available at
2233 +/livesys/merge_union, and the new feature is that the whiteouts are
2234 +visible!
2235 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2236 +writing to all branches. Also the default mode for all branches is 'ro'.
2237 +It is now possible to save the combined contents of the two overlay
2238 +branches to a new squashfs, e.g.:
2239 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2240 +
2241 +This new squashfs archive can be stored on the boot device and the
2242 +initramfs will use it to replace the old one at the next boot.
2243 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2244 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2245 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2015-09-24 10:47:58.248052907 +0200
2246 @@ -0,0 +1,47 @@
2247 +
2248 +# Copyright (C) 2010-2015 Junjiro R. Okajima
2249 +#
2250 +# This program is free software; you can redistribute it and/or modify
2251 +# it under the terms of the GNU General Public License as published by
2252 +# the Free Software Foundation; either version 2 of the License, or
2253 +# (at your option) any later version.
2254 +#
2255 +# This program is distributed in the hope that it will be useful,
2256 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2257 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2258 +# GNU General Public License for more details.
2259 +#
2260 +# You should have received a copy of the GNU General Public License
2261 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2262 +
2263 +Dynamically customizable FS operations
2264 +----------------------------------------------------------------------
2265 +Generally FS operations (struct inode_operations, struct
2266 +address_space_operations, struct file_operations, etc.) are defined as
2267 +"static const", but it never means that FS have only one set of
2268 +operation. Some FS have multiple sets of them. For instance, ext2 has
2269 +three sets, one for XIP, for NOBH, and for normal.
2270 +Since aufs overrides and redirects these operations, sometimes aufs has
2271 +to change its behaviour according to the branch FS type. More importantly
2272 +VFS acts differently if a function (member in the struct) is set or
2273 +not. It means aufs should have several sets of operations and select one
2274 +among them according to the branch FS definition.
2275 +
2276 +In order to solve this problem and not to affect the behaviour of VFS,
2277 +aufs defines these operations dynamically. For instance, aufs defines
2278 +dummy direct_IO function for struct address_space_operations, but it may
2279 +not be set to the address_space_operations actually. When the branch FS
2280 +doesn't have it, aufs doesn't set it to its address_space_operations
2281 +while the function definition itself is still alive. So the behaviour
2282 +itself will not change, and it will return an error when direct_IO is
2283 +not set.
2284 +
2285 +The lifetime of these dynamically generated operation object is
2286 +maintained by aufs branch object. When the branch is removed from aufs,
2287 +the reference counter of the object is decremented. When it reaches
2288 +zero, the dynamically generated operation object will be freed.
2289 +
2290 +This approach is designed to support AIO (io_submit), Direct I/O and
2291 +XIP (DAX) mainly.
2292 +Currently this approach is applied to address_space_operations for
2293 +regular files only.
2294 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2295 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2296 +++ linux/Documentation/filesystems/aufs/README 2015-09-24 10:47:58.244719488 +0200
2297 @@ -0,0 +1,383 @@
2298 +
2299 +Aufs4 -- advanced multi layered unification filesystem version 4.x
2300 +http://aufs.sf.net
2301 +Junjiro R. Okajima
2302 +
2303 +
2304 +0. Introduction
2305 +----------------------------------------
2306 +In the early days, aufs was entirely re-designed and re-implemented
2307 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2308 +improvements and implementations, it becomes totally different from
2309 +Unionfs while keeping the basic features.
2310 +Recently, Unionfs Version 2.x series begin taking some of the same
2311 +approaches to aufs1's.
2312 +Unionfs is being developed by Professor Erez Zadok at Stony Brook
2313 +University and his team.
2314 +
2315 +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
2316 +If you want older kernel version support, try aufs2-2.6.git or
2317 +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2318 +
2319 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2320 +      According to Christoph Hellwig, linux rejects all union-type
2321 +      filesystems but UnionMount.
2322 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2323 +
2324 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2325 +    UnionMount, and he pointed out an issue around a directory mutex
2326 +    lock and aufs addressed it. But it is still unsure whether aufs will
2327 +    be merged (or any other union solution).
2328 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2329 +
2330 +
2331 +1. Features
2332 +----------------------------------------
2333 +- unite several directories into a single virtual filesystem. The member
2334 +  directory is called as a branch.
2335 +- you can specify the permission flags to the branch, which are 'readonly',
2336 +  'readwrite' and 'whiteout-able.'
2337 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2338 +  readonly branch are modifiable logically.
2339 +- dynamic branch manipulation, add, del.
2340 +- etc...
2341 +
2342 +Also there are many enhancements in aufs, such as:
2343 +- test only the highest one for the directory permission (dirperm1)
2344 +- copyup on open (coo=)
2345 +- 'move' policy for copy-up between two writable branches, after
2346 +  checking free space.
2347 +- xattr, acl
2348 +- readdir(3) in userspace.
2349 +- keep inode number by external inode number table
2350 +- keep the timestamps of file/dir in internal copyup operation
2351 +- seekable directory, supporting NFS readdir.
2352 +- whiteout is hardlinked in order to reduce the consumption of inodes
2353 +  on branch
2354 +- do not copyup, nor create a whiteout when it is unnecessary
2355 +- revert a single systemcall when an error occurs in aufs
2356 +- remount interface instead of ioctl
2357 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2358 +- loopback mounted filesystem as a branch
2359 +- kernel thread for removing the dir who has a plenty of whiteouts
2360 +- support copyup sparse file (a file which has a 'hole' in it)
2361 +- default permission flags for branches
2362 +- selectable permission flags for ro branch, whether whiteout can
2363 +  exist or not
2364 +- export via NFS.
2365 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2366 +- support multiple writable branches, some policies to select one
2367 +  among multiple writable branches.
2368 +- a new semantics for link(2) and rename(2) to support multiple
2369 +  writable branches.
2370 +- no glibc changes are required.
2371 +- pseudo hardlink (hardlink over branches)
2372 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2373 +  including NFS or remote filesystem branch.
2374 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2375 +- and more...
2376 +
2377 +Currently these features are dropped temporary from aufs4.
2378 +See design/08plan.txt in detail.
2379 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2380 +  (robr)
2381 +- statistics of aufs thread (/sys/fs/aufs/stat)
2382 +
2383 +Features or just an idea in the future (see also design/*.txt),
2384 +- reorder the branch index without del/re-add.
2385 +- permanent xino files for NFSD
2386 +- an option for refreshing the opened files after add/del branches
2387 +- light version, without branch manipulation. (unnecessary?)
2388 +- copyup in userspace
2389 +- inotify in userspace
2390 +- readv/writev
2391 +
2392 +
2393 +2. Download
2394 +----------------------------------------
2395 +There are three GIT trees for aufs4, aufs4-linux.git,
2396 +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
2397 +"aufs-util.git."
2398 +While the aufs-util is always necessary, you need either of aufs4-linux
2399 +or aufs4-standalone.
2400 +
2401 +The aufs4-linux tree includes the whole linux mainline GIT tree,
2402 +git://git.kernel.org/.../torvalds/linux.git.
2403 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2404 +build aufs4 as an external kernel module.
2405 +Several extra patches are not included in this tree. Only
2406 +aufs4-standalone tree contains them. They are describe in the later
2407 +section "Configuration and Compilation."
2408 +
2409 +On the other hand, the aufs4-standalone tree has only aufs source files
2410 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2411 +But you need to apply all aufs patches manually.
2412 +
2413 +You will find GIT branches whose name is in form of "aufs4.x" where "x"
2414 +represents the linux kernel version, "linux-4.x". For instance,
2415 +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2416 +"aufs4.x-rcN" branch.
2417 +
2418 +o aufs4-linux tree
2419 +$ git clone --reference /your/linux/git/tree \
2420 +       git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
2421 +- if you don't have linux GIT tree, then remove "--reference ..."
2422 +$ cd aufs4-linux.git
2423 +$ git checkout origin/aufs4.0
2424 +
2425 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2426 +leave the patch-work to GIT.
2427 +$ cd /your/linux/git/tree
2428 +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2429 +$ git fetch aufs4
2430 +$ git checkout -b my4.0 v4.0
2431 +$ (add your local change...)
2432 +$ git pull aufs4 aufs4.0
2433 +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2434 +- you may need to solve some conflicts between your_changes and
2435 +  aufs4.0. in this case, git-rerere is recommended so that you can
2436 +  solve the similar conflicts automatically when you upgrade to 4.1 or
2437 +  later in the future.
2438 +
2439 +o aufs4-standalone tree
2440 +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2441 +$ cd aufs4-standalone.git
2442 +$ git checkout origin/aufs4.0
2443 +
2444 +o aufs-util tree
2445 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2446 +- note that the public aufs-util.git is on SourceForge instead of
2447 +  GitHUB.
2448 +$ cd aufs-util.git
2449 +$ git checkout origin/aufs4.0
2450 +
2451 +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2452 +The minor version number, 'x' in '4.x', of aufs may not always
2453 +follow the minor version number of the kernel.
2454 +Because changes in the kernel that cause the use of a new
2455 +minor version number do not always require changes to aufs-util.
2456 +
2457 +Since aufs-util has its own minor version number, you may not be
2458 +able to find a GIT branch in aufs-util for your kernel's
2459 +exact minor version number.
2460 +In this case, you should git-checkout the branch for the
2461 +nearest lower number.
2462 +
2463 +For (an unreleased) example:
2464 +If you are using "linux-4.10" and the "aufs4.10" branch
2465 +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
2466 +or something numerically smaller is the branch for your kernel.
2467 +
2468 +Also you can view all branches by
2469 +       $ git branch -a
2470 +
2471 +
2472 +3. Configuration and Compilation
2473 +----------------------------------------
2474 +Make sure you have git-checkout'ed the correct branch.
2475 +
2476 +For aufs4-linux tree,
2477 +- enable CONFIG_AUFS_FS.
2478 +- set other aufs configurations if necessary.
2479 +
2480 +For aufs4-standalone tree,
2481 +There are several ways to build.
2482 +
2483 +1.
2484 +- apply ./aufs4-kbuild.patch to your kernel source files.
2485 +- apply ./aufs4-base.patch too.
2486 +- apply ./aufs4-mmap.patch too.
2487 +- apply ./aufs4-standalone.patch too, if you have a plan to set
2488 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
2489 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2490 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2491 +- enable CONFIG_AUFS_FS, you can select either
2492 +  =m or =y.
2493 +- and build your kernel as usual.
2494 +- install the built kernel.
2495 +  Note: Since linux-3.9, every filesystem module requires an alias
2496 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2497 +  modules.aliases file if you set CONFIG_AUFS_FS=m.
2498 +- install the header files too by "make headers_install" to the
2499 +  directory where you specify. By default, it is $PWD/usr.
2500 +  "make help" shows a brief note for headers_install.
2501 +- and reboot your system.
2502 +
2503 +2.
2504 +- module only (CONFIG_AUFS_FS=m).
2505 +- apply ./aufs4-base.patch to your kernel source files.
2506 +- apply ./aufs4-mmap.patch too.
2507 +- apply ./aufs4-standalone.patch too.
2508 +- build your kernel, don't forget "make headers_install", and reboot.
2509 +- edit ./config.mk and set other aufs configurations if necessary.
2510 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2511 +  every aufs configurations.
2512 +- build the module by simple "make".
2513 +  Note: Since linux-3.9, every filesystem module requires an alias
2514 +  "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2515 +  modules.aliases file.
2516 +- you can specify ${KDIR} make variable which points to your kernel
2517 +  source tree.
2518 +- install the files
2519 +  + run "make install" to install the aufs module, or copy the built
2520 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2521 +  + run "make install_headers" (instead of headers_install) to install
2522 +    the modified aufs header file (you can specify DESTDIR which is
2523 +    available in aufs standalone version's Makefile only), or copy
2524 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2525 +    you like manually. By default, the target directory is $PWD/usr.
2526 +- no need to apply aufs4-kbuild.patch, nor copying source files to your
2527 +  kernel source tree.
2528 +
2529 +Note: The header file aufs_type.h is necessary to build aufs-util
2530 +      as well as "make headers_install" in the kernel source tree.
2531 +      headers_install is subject to be forgotten, but it is essentially
2532 +      necessary, not only for building aufs-util.
2533 +      You may not meet problems without headers_install in some older
2534 +      version though.
2535 +
2536 +And then,
2537 +- read README in aufs-util, build and install it
2538 +- note that your distribution may contain an obsoleted version of
2539 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2540 +  utilities, make sure that your compiler refers the correct aufs header
2541 +  file which is built by "make headers_install."
2542 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2543 +  then run "make install_ulib" too. And refer to the aufs manual in
2544 +  detail.
2545 +
2546 +There several other patches in aufs4-standalone.git. They are all
2547 +optional. When you meet some problems, they will help you.
2548 +- aufs4-loopback.patch
2549 +  Supports a nested loopback mount in a branch-fs. This patch is
2550 +  unnecessary until aufs produces a message like "you may want to try
2551 +  another patch for loopback file".
2552 +- vfs-ino.patch
2553 +  Modifies a system global kernel internal function get_next_ino() in
2554 +  order to stop assigning 0 for an inode-number. Not directly related to
2555 +  aufs, but recommended generally.
2556 +- tmpfs-idr.patch
2557 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2558 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2559 +  duplication of inode number, which is important for backup tools and
2560 +  other utilities. When you find aufs XINO files for tmpfs branch
2561 +  growing too much, try this patch.
2562 +
2563 +
2564 +4. Usage
2565 +----------------------------------------
2566 +At first, make sure aufs-util are installed, and please read the aufs
2567 +manual, aufs.5 in aufs-util.git tree.
2568 +$ man -l aufs.5
2569 +
2570 +And then,
2571 +$ mkdir /tmp/rw /tmp/aufs
2572 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2573 +
2574 +Here is another example. The result is equivalent.
2575 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2576 +  Or
2577 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2578 +# mount -o remount,append:${HOME} /tmp/aufs
2579 +
2580 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2581 +you modify a file under /tmp/aufs, the one on your home directory is
2582 +not affected, instead the same named file will be newly created under
2583 +/tmp/rw. And all of your modification to a file will be applied to
2584 +the one under /tmp/rw. This is called the file based Copy on Write
2585 +(COW) method.
2586 +Aufs mount options are described in aufs.5.
2587 +If you run chroot or something and make your aufs as a root directory,
2588 +then you need to customize the shutdown script. See the aufs manual in
2589 +detail.
2590 +
2591 +Additionally, there are some sample usages of aufs which are a
2592 +diskless system with network booting, and LiveCD over NFS.
2593 +See sample dir in CVS tree on SourceForge.
2594 +
2595 +
2596 +5. Contact
2597 +----------------------------------------
2598 +When you have any problems or strange behaviour in aufs, please let me
2599 +know with:
2600 +- /proc/mounts (instead of the output of mount(8))
2601 +- /sys/module/aufs/*
2602 +- /sys/fs/aufs/* (if you have them)
2603 +- /debug/aufs/* (if you have them)
2604 +- linux kernel version
2605 +  if your kernel is not plain, for example modified by distributor,
2606 +  the url where i can download its source is necessary too.
2607 +- aufs version which was printed at loading the module or booting the
2608 +  system, instead of the date you downloaded.
2609 +- configuration (define/undefine CONFIG_AUFS_xxx)
2610 +- kernel configuration or /proc/config.gz (if you have it)
2611 +- behaviour which you think to be incorrect
2612 +- actual operation, reproducible one is better
2613 +- mailto: aufs-users at lists.sourceforge.net
2614 +
2615 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2616 +and Feature Requests) on SourceForge. Please join and write to
2617 +aufs-users ML.
2618 +
2619 +
2620 +6. Acknowledgements
2621 +----------------------------------------
2622 +Thanks to everyone who have tried and are using aufs, whoever
2623 +have reported a bug or any feedback.
2624 +
2625 +Especially donators:
2626 +Tomas Matejicek(slax.org) made a donation (much more than once).
2627 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2628 +       scripts) is making "doubling" donations.
2629 +       Unfortunately I cannot list all of the donators, but I really
2630 +       appreciate.
2631 +       It ends Aug 2010, but the ordinary donation URL is still available.
2632 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2633 +Dai Itasaka made a donation (2007/8).
2634 +Chuck Smith made a donation (2008/4, 10 and 12).
2635 +Henk Schoneveld made a donation (2008/9).
2636 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2637 +Francois Dupoux made a donation (2008/11).
2638 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2639 +       aufs2 GIT tree (2009/2).
2640 +William Grant made a donation (2009/3).
2641 +Patrick Lane made a donation (2009/4).
2642 +The Mail Archive (mail-archive.com) made donations (2009/5).
2643 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2644 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2645 +Pavel Pronskiy made a donation (2011/2).
2646 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2647 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2648 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2649 +11).
2650 +Sam Liddicott made a donation (2011/9).
2651 +Era Scarecrow made a donation (2013/4).
2652 +Bor Ratajc made a donation (2013/4).
2653 +Alessandro Gorreta made a donation (2013/4).
2654 +POIRETTE Marc made a donation (2013/4).
2655 +Alessandro Gorreta made a donation (2013/4).
2656 +lauri kasvandik made a donation (2013/5).
2657 +"pemasu from Finland" made a donation (2013/7).
2658 +The Parted Magic Project made a donation (2013/9 and 11).
2659 +Pavel Barta made a donation (2013/10).
2660 +Nikolay Pertsev made a donation (2014/5).
2661 +James B made a donation (2014/7 and 2015/7).
2662 +Stefano Di Biase made a donation (2014/8).
2663 +Daniel Epellei made a donation (2015/1).
2664 +
2665 +Thank you very much.
2666 +Donations are always, including future donations, very important and
2667 +helpful for me to keep on developing aufs.
2668 +
2669 +
2670 +7.
2671 +----------------------------------------
2672 +If you are an experienced user, no explanation is needed. Aufs is
2673 +just a linux filesystem.
2674 +
2675 +
2676 +Enjoy!
2677 +
2678 +# Local variables: ;
2679 +# mode: text;
2680 +# End: ;
2681 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2682 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2683 +++ linux/fs/aufs/aufs.h        2015-09-24 10:47:58.248052907 +0200
2684 @@ -0,0 +1,59 @@
2685 +/*
2686 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2687 + *
2688 + * This program, aufs is free software; you can redistribute it and/or modify
2689 + * it under the terms of the GNU General Public License as published by
2690 + * the Free Software Foundation; either version 2 of the License, or
2691 + * (at your option) any later version.
2692 + *
2693 + * This program is distributed in the hope that it will be useful,
2694 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2695 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2696 + * GNU General Public License for more details.
2697 + *
2698 + * You should have received a copy of the GNU General Public License
2699 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2700 + */
2701 +
2702 +/*
2703 + * all header files
2704 + */
2705 +
2706 +#ifndef __AUFS_H__
2707 +#define __AUFS_H__
2708 +
2709 +#ifdef __KERNEL__
2710 +
2711 +#define AuStub(type, name, body, ...) \
2712 +       static inline type name(__VA_ARGS__) { body; }
2713 +
2714 +#define AuStubVoid(name, ...) \
2715 +       AuStub(void, name, , __VA_ARGS__)
2716 +#define AuStubInt0(name, ...) \
2717 +       AuStub(int, name, return 0, __VA_ARGS__)
2718 +
2719 +#include "debug.h"
2720 +
2721 +#include "branch.h"
2722 +#include "cpup.h"
2723 +#include "dcsub.h"
2724 +#include "dbgaufs.h"
2725 +#include "dentry.h"
2726 +#include "dir.h"
2727 +#include "dynop.h"
2728 +#include "file.h"
2729 +#include "fstype.h"
2730 +#include "inode.h"
2731 +#include "loop.h"
2732 +#include "module.h"
2733 +#include "opts.h"
2734 +#include "rwsem.h"
2735 +#include "spl.h"
2736 +#include "super.h"
2737 +#include "sysaufs.h"
2738 +#include "vfsub.h"
2739 +#include "whout.h"
2740 +#include "wkq.h"
2741 +
2742 +#endif /* __KERNEL__ */
2743 +#endif /* __AUFS_H__ */
2744 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2745 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2746 +++ linux/fs/aufs/branch.c      2015-09-24 10:47:58.248052907 +0200
2747 @@ -0,0 +1,1414 @@
2748 +/*
2749 + * Copyright (C) 2005-2015 Junjiro R. Okajima
2750 + *
2751 + * This program, aufs is free software; you can redistribute it and/or modify
2752 + * it under the terms of the GNU General Public License as published by
2753 + * the Free Software Foundation; either version 2 of the License, or
2754 + * (at your option) any later version.
2755 + *
2756 + * This program is distributed in the hope that it will be useful,
2757 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2758 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2759 + * GNU General Public License for more details.
2760 + *
2761 + * You should have received a copy of the GNU General Public License
2762 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2763 + */
2764 +
2765 +/*
2766 + * branch management
2767 + */
2768 +
2769 +#include <linux/compat.h>
2770 +#include <linux/statfs.h>
2771 +#include "aufs.h"
2772 +
2773 +/*
2774 + * free a single branch
2775 + */
2776 +static void au_br_do_free(struct au_branch *br)
2777 +{
2778 +       int i;
2779 +       struct au_wbr *wbr;
2780 +       struct au_dykey **key;
2781 +
2782 +       au_hnotify_fin_br(br);
2783 +
2784 +       if (br->br_xino.xi_file)
2785 +               fput(br->br_xino.xi_file);
2786 +       mutex_destroy(&br->br_xino.xi_nondir_mtx);
2787 +
2788 +       AuDebugOn(atomic_read(&br->br_count));
2789 +
2790 +       wbr = br->br_wbr;
2791 +       if (wbr) {
2792 +               for (i = 0; i < AuBrWh_Last; i++)
2793 +                       dput(wbr->wbr_wh[i]);
2794 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2795 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2796 +       }
2797 +
2798 +       if (br->br_fhsm) {
2799 +               au_br_fhsm_fin(br->br_fhsm);
2800 +               kfree(br->br_fhsm);
2801 +       }
2802 +
2803 +       key = br->br_dykey;
2804 +       for (i = 0; i < AuBrDynOp; i++, key++)
2805 +               if (*key)
2806 +                       au_dy_put(*key);
2807 +               else
2808 +                       break;
2809 +
2810 +       /* recursive lock, s_umount of branch's */
2811 +       lockdep_off();
2812 +       path_put(&br->br_path);
2813 +       lockdep_on();
2814 +       kfree(wbr);
2815 +       kfree(br);
2816 +}
2817 +
2818 +/*
2819 + * frees all branches
2820 + */
2821 +void au_br_free(struct au_sbinfo *sbinfo)
2822 +{
2823 +       aufs_bindex_t bmax;
2824 +       struct au_branch **br;
2825 +
2826 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2827 +
2828 +       bmax = sbinfo->si_bend + 1;
2829 +       br = sbinfo->si_branch;
2830 +       while (bmax--)
2831 +               au_br_do_free(*br++);
2832 +}
2833 +
2834 +/*
2835 + * find the index of a branch which is specified by @br_id.
2836 + */
2837 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2838 +{
2839 +       aufs_bindex_t bindex, bend;
2840 +
2841 +       bend = au_sbend(sb);
2842 +       for (bindex = 0; bindex <= bend; bindex++)
2843 +               if (au_sbr_id(sb, bindex) == br_id)
2844 +                       return bindex;
2845 +       return -1;
2846 +}
2847 +
2848 +/* ---------------------------------------------------------------------- */
2849 +
2850 +/*
2851 + * add a branch
2852 + */
2853 +
2854 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2855 +                       struct dentry *h_root)
2856 +{
2857 +       if (unlikely(h_adding == h_root
2858 +                    || au_test_loopback_overlap(sb, h_adding)))
2859 +               return 1;
2860 +       if (h_adding->d_sb != h_root->d_sb)
2861 +               return 0;
2862 +       return au_test_subdir(h_adding, h_root)
2863 +               || au_test_subdir(h_root, h_adding);
2864 +}
2865 +
2866 +/*
2867 + * returns a newly allocated branch. @new_nbranch is a number of branches
2868 + * after adding a branch.
2869 + */
2870 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2871 +                                    int perm)
2872 +{
2873 +       struct au_branch *add_branch;
2874 +       struct dentry *root;
2875 +       struct inode *inode;
2876 +       int err;
2877 +
2878 +       err = -ENOMEM;
2879 +       root = sb->s_root;
2880 +       add_branch = kmalloc(sizeof(*add_branch), GFP_NOFS);
2881 +       if (unlikely(!add_branch))
2882 +               goto out;
2883 +
2884 +       err = au_hnotify_init_br(add_branch, perm);
2885 +       if (unlikely(err))
2886 +               goto out_br;
2887 +
2888 +       add_branch->br_wbr = NULL;
2889 +       if (au_br_writable(perm)) {
2890 +               /* may be freed separately at changing the branch permission */
2891 +               add_branch->br_wbr = kmalloc(sizeof(*add_branch->br_wbr),
2892 +                                            GFP_NOFS);
2893 +               if (unlikely(!add_branch->br_wbr))
2894 +                       goto out_hnotify;
2895 +       }
2896 +
2897 +       add_branch->br_fhsm = NULL;
2898 +       if (au_br_fhsm(perm)) {
2899 +               err = au_fhsm_br_alloc(add_branch);
2900 +               if (unlikely(err))
2901 +                       goto out_wbr;
2902 +       }
2903 +
2904 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch);
2905 +       if (!err)
2906 +               err = au_di_realloc(au_di(root), new_nbranch);
2907 +       if (!err) {
2908 +               inode = d_inode(root);
2909 +               err = au_ii_realloc(au_ii(inode), new_nbranch);
2910 +       }
2911 +       if (!err)
2912 +               return add_branch; /* success */
2913 +
2914 +out_wbr:
2915 +       kfree(add_branch->br_wbr);
2916 +out_hnotify:
2917 +       au_hnotify_fin_br(add_branch);
2918 +out_br:
2919 +       kfree(add_branch);
2920 +out:
2921 +       return ERR_PTR(err);
2922 +}
2923 +
2924 +/*
2925 + * test if the branch permission is legal or not.
2926 + */
2927 +static int test_br(struct inode *inode, int brperm, char *path)
2928 +{
2929 +       int err;
2930 +
2931 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
2932 +       if (!err)
2933 +               goto out;
2934 +
2935 +       err = -EINVAL;
2936 +       pr_err("write permission for readonly mount or inode, %s\n", path);
2937 +
2938 +out:
2939 +       return err;
2940 +}
2941 +
2942 +/*
2943 + * returns:
2944 + * 0: success, the caller will add it
2945 + * plus: success, it is already unified, the caller should ignore it
2946 + * minus: error
2947 + */
2948 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
2949 +{
2950 +       int err;
2951 +       aufs_bindex_t bend, bindex;
2952 +       struct dentry *root, *h_dentry;
2953 +       struct inode *inode, *h_inode;
2954 +
2955 +       root = sb->s_root;
2956 +       bend = au_sbend(sb);
2957 +       if (unlikely(bend >= 0
2958 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
2959 +               err = 1;
2960 +               if (!remount) {
2961 +                       err = -EINVAL;
2962 +                       pr_err("%s duplicated\n", add->pathname);
2963 +               }
2964 +               goto out;
2965 +       }
2966 +
2967 +       err = -ENOSPC; /* -E2BIG; */
2968 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
2969 +                    || AUFS_BRANCH_MAX - 1 <= bend)) {
2970 +               pr_err("number of branches exceeded %s\n", add->pathname);
2971 +               goto out;
2972 +       }
2973 +
2974 +       err = -EDOM;
2975 +       if (unlikely(add->bindex < 0 || bend + 1 < add->bindex)) {
2976 +               pr_err("bad index %d\n", add->bindex);
2977 +               goto out;
2978 +       }
2979 +
2980 +       inode = d_inode(add->path.dentry);
2981 +       err = -ENOENT;
2982 +       if (unlikely(!inode->i_nlink)) {
2983 +               pr_err("no existence %s\n", add->pathname);
2984 +               goto out;
2985 +       }
2986 +
2987 +       err = -EINVAL;
2988 +       if (unlikely(inode->i_sb == sb)) {
2989 +               pr_err("%s must be outside\n", add->pathname);
2990 +               goto out;
2991 +       }
2992 +
2993 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
2994 +               pr_err("unsupported filesystem, %s (%s)\n",
2995 +                      add->pathname, au_sbtype(inode->i_sb));
2996 +               goto out;
2997 +       }
2998 +
2999 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3000 +               pr_err("already stacked, %s (%s)\n",
3001 +                      add->pathname, au_sbtype(inode->i_sb));
3002 +               goto out;
3003 +       }
3004 +
3005 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3006 +       if (unlikely(err))
3007 +               goto out;
3008 +
3009 +       if (bend < 0)
3010 +               return 0; /* success */
3011 +
3012 +       err = -EINVAL;
3013 +       for (bindex = 0; bindex <= bend; bindex++)
3014 +               if (unlikely(test_overlap(sb, add->path.dentry,
3015 +                                         au_h_dptr(root, bindex)))) {
3016 +                       pr_err("%s is overlapped\n", add->pathname);
3017 +                       goto out;
3018 +               }
3019 +
3020 +       err = 0;
3021 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3022 +               h_dentry = au_h_dptr(root, 0);
3023 +               h_inode = d_inode(h_dentry);
3024 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3025 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3026 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3027 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3028 +                               add->pathname,
3029 +                               i_uid_read(inode), i_gid_read(inode),
3030 +                               (inode->i_mode & S_IALLUGO),
3031 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3032 +                               (h_inode->i_mode & S_IALLUGO));
3033 +       }
3034 +
3035 +out:
3036 +       return err;
3037 +}
3038 +
3039 +/*
3040 + * initialize or clean the whiteouts for an adding branch
3041 + */
3042 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3043 +                        int new_perm)
3044 +{
3045 +       int err, old_perm;
3046 +       aufs_bindex_t bindex;
3047 +       struct mutex *h_mtx;
3048 +       struct au_wbr *wbr;
3049 +       struct au_hinode *hdir;
3050 +       struct dentry *h_dentry;
3051 +
3052 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3053 +       if (unlikely(err))
3054 +               goto out;
3055 +
3056 +       wbr = br->br_wbr;
3057 +       old_perm = br->br_perm;
3058 +       br->br_perm = new_perm;
3059 +       hdir = NULL;
3060 +       h_mtx = NULL;
3061 +       bindex = au_br_index(sb, br->br_id);
3062 +       if (0 <= bindex) {
3063 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3064 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
3065 +       } else {
3066 +               h_dentry = au_br_dentry(br);
3067 +               h_mtx = &d_inode(h_dentry)->i_mutex;
3068 +               mutex_lock_nested(h_mtx, AuLsc_I_PARENT);
3069 +       }
3070 +       if (!wbr)
3071 +               err = au_wh_init(br, sb);
3072 +       else {
3073 +               wbr_wh_write_lock(wbr);
3074 +               err = au_wh_init(br, sb);
3075 +               wbr_wh_write_unlock(wbr);
3076 +       }
3077 +       if (hdir)
3078 +               au_hn_imtx_unlock(hdir);
3079 +       else
3080 +               mutex_unlock(h_mtx);
3081 +       vfsub_mnt_drop_write(au_br_mnt(br));
3082 +       br->br_perm = old_perm;
3083 +
3084 +       if (!err && wbr && !au_br_writable(new_perm)) {
3085 +               kfree(wbr);
3086 +               br->br_wbr = NULL;
3087 +       }
3088 +
3089 +out:
3090 +       return err;
3091 +}
3092 +
3093 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3094 +                      int perm)
3095 +{
3096 +       int err;
3097 +       struct kstatfs kst;
3098 +       struct au_wbr *wbr;
3099 +
3100 +       wbr = br->br_wbr;
3101 +       au_rw_init(&wbr->wbr_wh_rwsem);
3102 +       memset(wbr->wbr_wh, 0, sizeof(wbr->wbr_wh));
3103 +       atomic_set(&wbr->wbr_wh_running, 0);
3104 +       wbr->wbr_bytes = 0;
3105 +
3106 +       /*
3107 +        * a limit for rmdir/rename a dir
3108 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3109 +        */
3110 +       err = vfs_statfs(&br->br_path, &kst);
3111 +       if (unlikely(err))
3112 +               goto out;
3113 +       err = -EINVAL;
3114 +       if (kst.f_namelen >= NAME_MAX)
3115 +               err = au_br_init_wh(sb, br, perm);
3116 +       else
3117 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3118 +                      au_br_dentry(br),
3119 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3120 +
3121 +out:
3122 +       return err;
3123 +}
3124 +
3125 +/* initialize a new branch */
3126 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3127 +                     struct au_opt_add *add)
3128 +{
3129 +       int err;
3130 +       struct inode *h_inode;
3131 +
3132 +       err = 0;
3133 +       memset(&br->br_xino, 0, sizeof(br->br_xino));
3134 +       mutex_init(&br->br_xino.xi_nondir_mtx);
3135 +       br->br_perm = add->perm;
3136 +       br->br_path = add->path; /* set first, path_get() later */
3137 +       spin_lock_init(&br->br_dykey_lock);
3138 +       memset(br->br_dykey, 0, sizeof(br->br_dykey));
3139 +       atomic_set(&br->br_count, 0);
3140 +       atomic_set(&br->br_xino_running, 0);
3141 +       br->br_id = au_new_br_id(sb);
3142 +       AuDebugOn(br->br_id < 0);
3143 +
3144 +       if (au_br_writable(add->perm)) {
3145 +               err = au_wbr_init(br, sb, add->perm);
3146 +               if (unlikely(err))
3147 +                       goto out_err;
3148 +       }
3149 +
3150 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3151 +               h_inode = d_inode(add->path.dentry);
3152 +               err = au_xino_br(sb, br, h_inode->i_ino,
3153 +                                au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3154 +               if (unlikely(err)) {
3155 +                       AuDebugOn(br->br_xino.xi_file);
3156 +                       goto out_err;
3157 +               }
3158 +       }
3159 +
3160 +       sysaufs_br_init(br);
3161 +       path_get(&br->br_path);
3162 +       goto out; /* success */
3163 +
3164 +out_err:
3165 +       memset(&br->br_path, 0, sizeof(br->br_path));
3166 +out:
3167 +       return err;
3168 +}
3169 +
3170 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3171 +                            struct au_branch *br, aufs_bindex_t bend,
3172 +                            aufs_bindex_t amount)
3173 +{
3174 +       struct au_branch **brp;
3175 +
3176 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3177 +
3178 +       brp = sbinfo->si_branch + bindex;
3179 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3180 +       *brp = br;
3181 +       sbinfo->si_bend++;
3182 +       if (unlikely(bend < 0))
3183 +               sbinfo->si_bend = 0;
3184 +}
3185 +
3186 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3187 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3188 +{
3189 +       struct au_hdentry *hdp;
3190 +
3191 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3192 +
3193 +       hdp = dinfo->di_hdentry + bindex;
3194 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3195 +       au_h_dentry_init(hdp);
3196 +       dinfo->di_bend++;
3197 +       if (unlikely(bend < 0))
3198 +               dinfo->di_bstart = 0;
3199 +}
3200 +
3201 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3202 +                            aufs_bindex_t bend, aufs_bindex_t amount)
3203 +{
3204 +       struct au_hinode *hip;
3205 +
3206 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3207 +
3208 +       hip = iinfo->ii_hinode + bindex;
3209 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3210 +       hip->hi_inode = NULL;
3211 +       au_hn_init(hip);
3212 +       iinfo->ii_bend++;
3213 +       if (unlikely(bend < 0))
3214 +               iinfo->ii_bstart = 0;
3215 +}
3216 +
3217 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3218 +                        aufs_bindex_t bindex)
3219 +{
3220 +       struct dentry *root, *h_dentry;
3221 +       struct inode *root_inode, *h_inode;
3222 +       aufs_bindex_t bend, amount;
3223 +
3224 +       root = sb->s_root;
3225 +       root_inode = d_inode(root);
3226 +       bend = au_sbend(sb);
3227 +       amount = bend + 1 - bindex;
3228 +       h_dentry = au_br_dentry(br);
3229 +       au_sbilist_lock();
3230 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bend, amount);
3231 +       au_br_do_add_hdp(au_di(root), bindex, bend, amount);
3232 +       au_br_do_add_hip(au_ii(root_inode), bindex, bend, amount);
3233 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3234 +       h_inode = d_inode(h_dentry);
3235 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3236 +       au_sbilist_unlock();
3237 +}
3238 +
3239 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3240 +{
3241 +       int err;
3242 +       aufs_bindex_t bend, add_bindex;
3243 +       struct dentry *root, *h_dentry;
3244 +       struct inode *root_inode;
3245 +       struct au_branch *add_branch;
3246 +
3247 +       root = sb->s_root;
3248 +       root_inode = d_inode(root);
3249 +       IMustLock(root_inode);
3250 +       err = test_add(sb, add, remount);
3251 +       if (unlikely(err < 0))
3252 +               goto out;
3253 +       if (err) {
3254 +               err = 0;
3255 +               goto out; /* success */
3256 +       }
3257 +
3258 +       bend = au_sbend(sb);
3259 +       add_branch = au_br_alloc(sb, bend + 2, add->perm);
3260 +       err = PTR_ERR(add_branch);
3261 +       if (IS_ERR(add_branch))
3262 +               goto out;
3263 +
3264 +       err = au_br_init(add_branch, sb, add);
3265 +       if (unlikely(err)) {
3266 +               au_br_do_free(add_branch);
3267 +               goto out;
3268 +       }
3269 +
3270 +       add_bindex = add->bindex;
3271 +       if (!remount)
3272 +               au_br_do_add(sb, add_branch, add_bindex);
3273 +       else {
3274 +               sysaufs_brs_del(sb, add_bindex);
3275 +               au_br_do_add(sb, add_branch, add_bindex);
3276 +               sysaufs_brs_add(sb, add_bindex);
3277 +       }
3278 +
3279 +       h_dentry = add->path.dentry;
3280 +       if (!add_bindex) {
3281 +               au_cpup_attr_all(root_inode, /*force*/1);
3282 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3283 +       } else
3284 +               au_add_nlink(root_inode, d_inode(h_dentry));
3285 +
3286 +       /*
3287 +        * this test/set prevents aufs from handling unnecesary notify events
3288 +        * of xino files, in case of re-adding a writable branch which was
3289 +        * once detached from aufs.
3290 +        */
3291 +       if (au_xino_brid(sb) < 0
3292 +           && au_br_writable(add_branch->br_perm)
3293 +           && !au_test_fs_bad_xino(h_dentry->d_sb)
3294 +           && add_branch->br_xino.xi_file
3295 +           && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
3296 +               au_xino_brid_set(sb, add_branch->br_id);
3297 +
3298 +out:
3299 +       return err;
3300 +}
3301 +
3302 +/* ---------------------------------------------------------------------- */
3303 +
3304 +static unsigned long long au_farray_cb(void *a,
3305 +                                      unsigned long long max __maybe_unused,
3306 +                                      void *arg)
3307 +{
3308 +       unsigned long long n;
3309 +       struct file **p, *f;
3310 +       struct au_sphlhead *files;
3311 +       struct au_finfo *finfo;
3312 +       struct super_block *sb = arg;
3313 +
3314 +       n = 0;
3315 +       p = a;
3316 +       files = &au_sbi(sb)->si_files;
3317 +       spin_lock(&files->spin);
3318 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3319 +               f = finfo->fi_file;
3320 +               if (file_count(f)
3321 +                   && !special_file(file_inode(f)->i_mode)) {
3322 +                       get_file(f);
3323 +                       *p++ = f;
3324 +                       n++;
3325 +                       AuDebugOn(n > max);
3326 +               }
3327 +       }
3328 +       spin_unlock(&files->spin);
3329 +
3330 +       return n;
3331 +}
3332 +
3333 +static struct file **au_farray_alloc(struct super_block *sb,
3334 +                                    unsigned long long *max)
3335 +{
3336 +       *max = atomic_long_read(&au_sbi(sb)->si_nfiles);
3337 +       return au_array_alloc(max, au_farray_cb, sb);
3338 +}
3339 +
3340 +static void au_farray_free(struct file **a, unsigned long long max)
3341 +{
3342 +       unsigned long long ull;
3343 +
3344 +       for (ull = 0; ull < max; ull++)
3345 +               if (a[ull])
3346 +                       fput(a[ull]);
3347 +       au_array_free(a);
3348 +}
3349 +
3350 +/* ---------------------------------------------------------------------- */
3351 +
3352 +/*
3353 + * delete a branch
3354 + */
3355 +
3356 +/* to show the line number, do not make it inlined function */
3357 +#define AuVerbose(do_info, fmt, ...) do { \
3358 +       if (do_info) \
3359 +               pr_info(fmt, ##__VA_ARGS__); \
3360 +} while (0)
3361 +
3362 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t bstart,
3363 +                        aufs_bindex_t bend)
3364 +{
3365 +       return (inode && !S_ISDIR(inode->i_mode)) || bstart == bend;
3366 +}
3367 +
3368 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t bstart,
3369 +                        aufs_bindex_t bend)
3370 +{
3371 +       return au_test_ibusy(d_inode(dentry), bstart, bend);
3372 +}
3373 +
3374 +/*
3375 + * test if the branch is deletable or not.
3376 + */
3377 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3378 +                           unsigned int sigen, const unsigned int verbose)
3379 +{
3380 +       int err, i, j, ndentry;
3381 +       aufs_bindex_t bstart, bend;
3382 +       struct au_dcsub_pages dpages;
3383 +       struct au_dpage *dpage;
3384 +       struct dentry *d;
3385 +
3386 +       err = au_dpages_init(&dpages, GFP_NOFS);
3387 +       if (unlikely(err))
3388 +               goto out;
3389 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3390 +       if (unlikely(err))
3391 +               goto out_dpages;
3392 +
3393 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3394 +               dpage = dpages.dpages + i;
3395 +               ndentry = dpage->ndentry;
3396 +               for (j = 0; !err && j < ndentry; j++) {
3397 +                       d = dpage->dentries[j];
3398 +                       AuDebugOn(au_dcount(d) <= 0);
3399 +                       if (!au_digen_test(d, sigen)) {
3400 +                               di_read_lock_child(d, AuLock_IR);
3401 +                               if (unlikely(au_dbrange_test(d))) {
3402 +                                       di_read_unlock(d, AuLock_IR);
3403 +                                       continue;
3404 +                               }
3405 +                       } else {
3406 +                               di_write_lock_child(d);
3407 +                               if (unlikely(au_dbrange_test(d))) {
3408 +                                       di_write_unlock(d);
3409 +                                       continue;
3410 +                               }
3411 +                               err = au_reval_dpath(d, sigen);
3412 +                               if (!err)
3413 +                                       di_downgrade_lock(d, AuLock_IR);
3414 +                               else {
3415 +                                       di_write_unlock(d);
3416 +                                       break;
3417 +                               }
3418 +                       }
3419 +
3420 +                       /* AuDbgDentry(d); */
3421 +                       bstart = au_dbstart(d);
3422 +                       bend = au_dbend(d);
3423 +                       if (bstart <= bindex
3424 +                           && bindex <= bend
3425 +                           && au_h_dptr(d, bindex)
3426 +                           && au_test_dbusy(d, bstart, bend)) {
3427 +                               err = -EBUSY;
3428 +                               AuVerbose(verbose, "busy %pd\n", d);
3429 +                               AuDbgDentry(d);
3430 +                       }
3431 +                       di_read_unlock(d, AuLock_IR);
3432 +               }
3433 +       }
3434 +
3435 +out_dpages:
3436 +       au_dpages_free(&dpages);
3437 +out:
3438 +       return err;
3439 +}
3440 +
3441 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3442 +                          unsigned int sigen, const unsigned int verbose)
3443 +{
3444 +       int err;
3445 +       unsigned long long max, ull;
3446 +       struct inode *i, **array;
3447 +       aufs_bindex_t bstart, bend;
3448 +
3449 +       array = au_iarray_alloc(sb, &max);
3450 +       err = PTR_ERR(array);
3451 +       if (IS_ERR(array))
3452 +               goto out;
3453 +
3454 +       err = 0;
3455 +       AuDbg("b%d\n", bindex);
3456 +       for (ull = 0; !err && ull < max; ull++) {
3457 +               i = array[ull];
3458 +               if (unlikely(!i))
3459 +                       break;
3460 +               if (i->i_ino == AUFS_ROOT_INO)
3461 +                       continue;
3462 +
3463 +               /* AuDbgInode(i); */
3464 +               if (au_iigen(i, NULL) == sigen)
3465 +                       ii_read_lock_child(i);
3466 +               else {
3467 +                       ii_write_lock_child(i);
3468 +                       err = au_refresh_hinode_self(i);
3469 +                       au_iigen_dec(i);
3470 +                       if (!err)
3471 +                               ii_downgrade_lock(i);
3472 +                       else {
3473 +                               ii_write_unlock(i);
3474 +                               break;
3475 +                       }
3476 +               }
3477 +
3478 +               bstart = au_ibstart(i);
3479 +               bend = au_ibend(i);
3480 +               if (bstart <= bindex
3481 +                   && bindex <= bend
3482 +                   && au_h_iptr(i, bindex)
3483 +                   && au_test_ibusy(i, bstart, bend)) {
3484 +                       err = -EBUSY;
3485 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3486 +                       AuDbgInode(i);
3487 +               }
3488 +               ii_read_unlock(i);
3489 +       }
3490 +       au_iarray_free(array, max);
3491 +
3492 +out:
3493 +       return err;
3494 +}
3495 +
3496 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3497 +                             const unsigned int verbose)
3498 +{
3499 +       int err;
3500 +       unsigned int sigen;
3501 +
3502 +       sigen = au_sigen(root->d_sb);
3503 +       DiMustNoWaiters(root);
3504 +       IiMustNoWaiters(d_inode(root));
3505 +       di_write_unlock(root);
3506 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3507 +       if (!err)
3508 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3509 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3510 +
3511 +       return err;
3512 +}
3513 +
3514 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3515 +                        struct file **to_free, int *idx)
3516 +{
3517 +       int err;
3518 +       unsigned char matched, root;
3519 +       aufs_bindex_t bindex, bend;
3520 +       struct au_fidir *fidir;
3521 +       struct au_hfile *hfile;
3522 +
3523 +       err = 0;
3524 +       root = IS_ROOT(file->f_path.dentry);
3525 +       if (root) {
3526 +               get_file(file);
3527 +               to_free[*idx] = file;
3528 +               (*idx)++;
3529 +               goto out;
3530 +       }
3531 +
3532 +       matched = 0;
3533 +       fidir = au_fi(file)->fi_hdir;
3534 +       AuDebugOn(!fidir);
3535 +       bend = au_fbend_dir(file);
3536 +       for (bindex = au_fbstart(file); bindex <= bend; bindex++) {
3537 +               hfile = fidir->fd_hfile + bindex;
3538 +               if (!hfile->hf_file)
3539 +                       continue;
3540 +
3541 +               if (hfile->hf_br->br_id == br_id) {
3542 +                       matched = 1;
3543 +                       break;
3544 +               }
3545 +       }
3546 +       if (matched)
3547 +               err = -EBUSY;
3548 +
3549 +out:
3550 +       return err;
3551 +}
3552 +
3553 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3554 +                         struct file **to_free, int opened)
3555 +{
3556 +       int err, idx;
3557 +       unsigned long long ull, max;
3558 +       aufs_bindex_t bstart;
3559 +       struct file *file, **array;
3560 +       struct dentry *root;
3561 +       struct au_hfile *hfile;
3562 +
3563 +       array = au_farray_alloc(sb, &max);
3564 +       err = PTR_ERR(array);
3565 +       if (IS_ERR(array))
3566 +               goto out;
3567 +
3568 +       err = 0;
3569 +       idx = 0;
3570 +       root = sb->s_root;
3571 +       di_write_unlock(root);
3572 +       for (ull = 0; ull < max; ull++) {
3573 +               file = array[ull];
3574 +               if (unlikely(!file))
3575 +                       break;
3576 +
3577 +               /* AuDbg("%pD\n", file); */
3578 +               fi_read_lock(file);
3579 +               bstart = au_fbstart(file);
3580 +               if (!d_is_dir(file->f_path.dentry)) {
3581 +                       hfile = &au_fi(file)->fi_htop;
3582 +                       if (hfile->hf_br->br_id == br_id)
3583 +                               err = -EBUSY;
3584 +               } else
3585 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3586 +               fi_read_unlock(file);
3587 +               if (unlikely(err))
3588 +                       break;
3589 +       }
3590 +       di_write_lock_child(root);
3591 +       au_farray_free(array, max);
3592 +       AuDebugOn(idx > opened);
3593 +
3594 +out:
3595 +       return err;
3596 +}
3597 +
3598 +static void br_del_file(struct file **to_free, unsigned long long opened,
3599 +                         aufs_bindex_t br_id)
3600 +{
3601 +       unsigned long long ull;
3602 +       aufs_bindex_t bindex, bstart, bend, bfound;
3603 +       struct file *file;
3604 +       struct au_fidir *fidir;
3605 +       struct au_hfile *hfile;
3606 +
3607 +       for (ull = 0; ull < opened; ull++) {
3608 +               file = to_free[ull];
3609 +               if (unlikely(!file))
3610 +                       break;
3611 +
3612 +               /* AuDbg("%pD\n", file); */
3613 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3614 +               bfound = -1;
3615 +               fidir = au_fi(file)->fi_hdir;
3616 +               AuDebugOn(!fidir);
3617 +               fi_write_lock(file);
3618 +               bstart = au_fbstart(file);
3619 +               bend = au_fbend_dir(file);
3620 +               for (bindex = bstart; bindex <= bend; bindex++) {
3621 +                       hfile = fidir->fd_hfile + bindex;
3622 +                       if (!hfile->hf_file)
3623 +                               continue;
3624 +
3625 +                       if (hfile->hf_br->br_id == br_id) {
3626 +                               bfound = bindex;
3627 +                               break;
3628 +                       }
3629 +               }
3630 +               AuDebugOn(bfound < 0);
3631 +               au_set_h_fptr(file, bfound, NULL);
3632 +               if (bfound == bstart) {
3633 +                       for (bstart++; bstart <= bend; bstart++)
3634 +                               if (au_hf_dir(file, bstart)) {
3635 +                                       au_set_fbstart(file, bstart);
3636 +                                       break;
3637 +                               }
3638 +               }
3639 +               fi_write_unlock(file);
3640 +       }
3641 +}
3642 +
3643 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3644 +                            const aufs_bindex_t bindex,
3645 +                            const aufs_bindex_t bend)
3646 +{
3647 +       struct au_branch **brp, **p;
3648 +
3649 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3650 +
3651 +       brp = sbinfo->si_branch + bindex;
3652 +       if (bindex < bend)
3653 +               memmove(brp, brp + 1, sizeof(*brp) * (bend - bindex));
3654 +       sbinfo->si_branch[0 + bend] = NULL;
3655 +       sbinfo->si_bend--;
3656 +
3657 +       p = krealloc(sbinfo->si_branch, sizeof(*p) * bend, AuGFP_SBILIST);
3658 +       if (p)
3659 +               sbinfo->si_branch = p;
3660 +       /* harmless error */
3661 +}
3662 +
3663 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3664 +                            const aufs_bindex_t bend)
3665 +{
3666 +       struct au_hdentry *hdp, *p;
3667 +
3668 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3669 +
3670 +       hdp = dinfo->di_hdentry;
3671 +       if (bindex < bend)
3672 +               memmove(hdp + bindex, hdp + bindex + 1,
3673 +                       sizeof(*hdp) * (bend - bindex));
3674 +       hdp[0 + bend].hd_dentry = NULL;
3675 +       dinfo->di_bend--;
3676 +
3677 +       p = krealloc(hdp, sizeof(*p) * bend, AuGFP_SBILIST);
3678 +       if (p)
3679 +               dinfo->di_hdentry = p;
3680 +       /* harmless error */
3681 +}
3682 +
3683 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3684 +                            const aufs_bindex_t bend)
3685 +{
3686 +       struct au_hinode *hip, *p;
3687 +
3688 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3689 +
3690 +       hip = iinfo->ii_hinode + bindex;
3691 +       if (bindex < bend)
3692 +               memmove(hip, hip + 1, sizeof(*hip) * (bend - bindex));
3693 +       iinfo->ii_hinode[0 + bend].hi_inode = NULL;
3694 +       au_hn_init(iinfo->ii_hinode + bend);
3695 +       iinfo->ii_bend--;
3696 +
3697 +       p = krealloc(iinfo->ii_hinode, sizeof(*p) * bend, AuGFP_SBILIST);
3698 +       if (p)
3699 +               iinfo->ii_hinode = p;
3700 +       /* harmless error */
3701 +}
3702 +
3703 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3704 +                        struct au_branch *br)
3705 +{
3706 +       aufs_bindex_t bend;
3707 +       struct au_sbinfo *sbinfo;
3708 +       struct dentry *root, *h_root;
3709 +       struct inode *inode, *h_inode;
3710 +       struct au_hinode *hinode;
3711 +
3712 +       SiMustWriteLock(sb);
3713 +
3714 +       root = sb->s_root;
3715 +       inode = d_inode(root);
3716 +       sbinfo = au_sbi(sb);
3717 +       bend = sbinfo->si_bend;
3718 +
3719 +       h_root = au_h_dptr(root, bindex);
3720 +       hinode = au_hi(inode, bindex);
3721 +       h_inode = au_igrab(hinode->hi_inode);
3722 +       au_hiput(hinode);
3723 +
3724 +       au_sbilist_lock();
3725 +       au_br_do_del_brp(sbinfo, bindex, bend);
3726 +       au_br_do_del_hdp(au_di(root), bindex, bend);
3727 +       au_br_do_del_hip(au_ii(inode), bindex, bend);
3728 +       au_sbilist_unlock();
3729 +
3730 +       dput(h_root);
3731 +       iput(h_inode);
3732 +       au_br_do_free(br);
3733 +}
3734 +
3735 +static unsigned long long empty_cb(void *array, unsigned long long max,
3736 +                                  void *arg)
3737 +{
3738 +       return max;
3739 +}
3740 +
3741 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3742 +{
3743 +       int err, rerr, i;
3744 +       unsigned long long opened;
3745 +       unsigned int mnt_flags;
3746 +       aufs_bindex_t bindex, bend, br_id;
3747 +       unsigned char do_wh, verbose;
3748 +       struct au_branch *br;
3749 +       struct au_wbr *wbr;
3750 +       struct dentry *root;
3751 +       struct file **to_free;
3752 +
3753 +       err = 0;
3754 +       opened = 0;
3755 +       to_free = NULL;
3756 +       root = sb->s_root;
3757 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3758 +       if (bindex < 0) {
3759 +               if (remount)
3760 +                       goto out; /* success */
3761 +               err = -ENOENT;
3762 +               pr_err("%s no such branch\n", del->pathname);
3763 +               goto out;
3764 +       }
3765 +       AuDbg("bindex b%d\n", bindex);
3766 +
3767 +       err = -EBUSY;
3768 +       mnt_flags = au_mntflags(sb);
3769 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3770 +       bend = au_sbend(sb);
3771 +       if (unlikely(!bend)) {
3772 +               AuVerbose(verbose, "no more branches left\n");
3773 +               goto out;
3774 +       }
3775 +       br = au_sbr(sb, bindex);
3776 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3777 +
3778 +       br_id = br->br_id;
3779 +       opened = atomic_read(&br->br_count);
3780 +       if (unlikely(opened)) {
3781 +               to_free = au_array_alloc(&opened, empty_cb, NULL);
3782 +               err = PTR_ERR(to_free);
3783 +               if (IS_ERR(to_free))
3784 +                       goto out;
3785 +
3786 +               err = test_file_busy(sb, br_id, to_free, opened);
3787 +               if (unlikely(err)) {
3788 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3789 +                       goto out;
3790 +               }
3791 +       }
3792 +
3793 +       wbr = br->br_wbr;
3794 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3795 +       if (do_wh) {
3796 +               /* instead of WbrWhMustWriteLock(wbr) */
3797 +               SiMustWriteLock(sb);
3798 +               for (i = 0; i < AuBrWh_Last; i++) {
3799 +                       dput(wbr->wbr_wh[i]);
3800 +                       wbr->wbr_wh[i] = NULL;
3801 +               }
3802 +       }
3803 +
3804 +       err = test_children_busy(root, bindex, verbose);
3805 +       if (unlikely(err)) {
3806 +               if (do_wh)
3807 +                       goto out_wh;
3808 +               goto out;
3809 +       }
3810 +
3811 +       err = 0;
3812 +       if (to_free) {
3813 +               /*
3814 +                * now we confirmed the branch is deletable.
3815 +                * let's free the remaining opened dirs on the branch.
3816 +                */
3817 +               di_write_unlock(root);
3818 +               br_del_file(to_free, opened, br_id);
3819 +               di_write_lock_child(root);
3820 +       }
3821 +
3822 +       if (!remount)
3823 +               au_br_do_del(sb, bindex, br);
3824 +       else {
3825 +               sysaufs_brs_del(sb, bindex);
3826 +               au_br_do_del(sb, bindex, br);
3827 +               sysaufs_brs_add(sb, bindex);
3828 +       }
3829 +
3830 +       if (!bindex) {
3831 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3832 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3833 +       } else
3834 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3835 +       if (au_opt_test(mnt_flags, PLINK))
3836 +               au_plink_half_refresh(sb, br_id);
3837 +
3838 +       if (au_xino_brid(sb) == br_id)
3839 +               au_xino_brid_set(sb, -1);
3840 +       goto out; /* success */
3841 +
3842 +out_wh:
3843 +       /* revert */
3844 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3845 +       if (rerr)
3846 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3847 +                       del->pathname, rerr);
3848 +out:
3849 +       if (to_free)
3850 +               au_farray_free(to_free, opened);
3851 +       return err;
3852 +}
3853 +
3854 +/* ---------------------------------------------------------------------- */
3855 +
3856 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3857 +{
3858 +       int err;
3859 +       aufs_bindex_t bstart, bend;
3860 +       struct aufs_ibusy ibusy;
3861 +       struct inode *inode, *h_inode;
3862 +
3863 +       err = -EPERM;
3864 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
3865 +               goto out;
3866 +
3867 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3868 +       if (!err)
3869 +               err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3870 +       if (unlikely(err)) {
3871 +               err = -EFAULT;
3872 +               AuTraceErr(err);
3873 +               goto out;
3874 +       }
3875 +
3876 +       err = -EINVAL;
3877 +       si_read_lock(sb, AuLock_FLUSH);
3878 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbend(sb)))
3879 +               goto out_unlock;
3880 +
3881 +       err = 0;
3882 +       ibusy.h_ino = 0; /* invalid */
3883 +       inode = ilookup(sb, ibusy.ino);
3884 +       if (!inode
3885 +           || inode->i_ino == AUFS_ROOT_INO
3886 +           || is_bad_inode(inode))
3887 +               goto out_unlock;
3888 +
3889 +       ii_read_lock_child(inode);
3890 +       bstart = au_ibstart(inode);
3891 +       bend = au_ibend(inode);
3892 +       if (bstart <= ibusy.bindex && ibusy.bindex <= bend) {
3893 +               h_inode = au_h_iptr(inode, ibusy.bindex);
3894 +               if (h_inode && au_test_ibusy(inode, bstart, bend))
3895 +                       ibusy.h_ino = h_inode->i_ino;
3896 +       }
3897 +       ii_read_unlock(inode);
3898 +       iput(inode);
3899 +
3900 +out_unlock:
3901 +       si_read_unlock(sb);
3902 +       if (!err) {
3903 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
3904 +               if (unlikely(err)) {
3905 +                       err = -EFAULT;
3906 +                       AuTraceErr(err);
3907 +               }
3908 +       }
3909 +out:
3910 +       return err;
3911 +}
3912 +
3913 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
3914 +{
3915 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
3916 +}
3917 +
3918 +#ifdef CONFIG_COMPAT
3919 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
3920 +{
3921 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
3922 +}
3923 +#endif
3924 +
3925 +/* ---------------------------------------------------------------------- */
3926 +
3927 +/*
3928 + * change a branch permission
3929 + */
3930 +
3931 +static void au_warn_ima(void)
3932 +{
3933 +#ifdef CONFIG_IMA
3934 +       /* since it doesn't support mark_files_ro() */
3935 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
3936 +#endif
3937 +}
3938 +
3939 +static int do_need_sigen_inc(int a, int b)
3940 +{
3941 +       return au_br_whable(a) && !au_br_whable(b);
3942 +}
3943 +
3944 +static int need_sigen_inc(int old, int new)
3945 +{
3946 +       return do_need_sigen_inc(old, new)
3947 +               || do_need_sigen_inc(new, old);
3948 +}
3949 +
3950 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
3951 +{
3952 +       int err, do_warn;
3953 +       unsigned int mnt_flags;
3954 +       unsigned long long ull, max;
3955 +       aufs_bindex_t br_id;
3956 +       unsigned char verbose, writer;
3957 +       struct file *file, *hf, **array;
3958 +       struct au_hfile *hfile;
3959 +
3960 +       mnt_flags = au_mntflags(sb);
3961 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3962 +
3963 +       array = au_farray_alloc(sb, &max);
3964 +       err = PTR_ERR(array);
3965 +       if (IS_ERR(array))
3966 +               goto out;
3967 +
3968 +       do_warn = 0;
3969 +       br_id = au_sbr_id(sb, bindex);
3970 +       for (ull = 0; ull < max; ull++) {
3971 +               file = array[ull];
3972 +               if (unlikely(!file))
3973 +                       break;
3974 +
3975 +               /* AuDbg("%pD\n", file); */
3976 +               fi_read_lock(file);
3977 +               if (unlikely(au_test_mmapped(file))) {
3978 +                       err = -EBUSY;
3979 +                       AuVerbose(verbose, "mmapped %pD\n", file);
3980 +                       AuDbgFile(file);
3981 +                       FiMustNoWaiters(file);
3982 +                       fi_read_unlock(file);
3983 +                       goto out_array;
3984 +               }
3985 +
3986 +               hfile = &au_fi(file)->fi_htop;
3987 +               hf = hfile->hf_file;
3988 +               if (!d_is_reg(file->f_path.dentry)
3989 +                   || !(file->f_mode & FMODE_WRITE)
3990 +                   || hfile->hf_br->br_id != br_id
3991 +                   || !(hf->f_mode & FMODE_WRITE))
3992 +                       array[ull] = NULL;
3993 +               else {
3994 +                       do_warn = 1;
3995 +                       get_file(file);
3996 +               }
3997 +
3998 +               FiMustNoWaiters(file);
3999 +               fi_read_unlock(file);
4000 +               fput(file);
4001 +       }
4002 +
4003 +       err = 0;
4004 +       if (do_warn)
4005 +               au_warn_ima();
4006 +
4007 +       for (ull = 0; ull < max; ull++) {
4008 +               file = array[ull];
4009 +               if (!file)
4010 +                       continue;
4011 +
4012 +               /* todo: already flushed? */
4013 +               /*
4014 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4015 +                * approach which resets f_mode and calls mnt_drop_write() and
4016 +                * file_release_write() for each file, because the branch
4017 +                * attribute in aufs world is totally different from the native
4018 +                * fs rw/ro mode.
4019 +               */
4020 +               /* fi_read_lock(file); */
4021 +               hfile = &au_fi(file)->fi_htop;
4022 +               hf = hfile->hf_file;
4023 +               /* fi_read_unlock(file); */
4024 +               spin_lock(&hf->f_lock);
4025 +               writer = !!(hf->f_mode & FMODE_WRITER);
4026 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4027 +               spin_unlock(&hf->f_lock);
4028 +               if (writer) {
4029 +                       put_write_access(file_inode(hf));
4030 +                       __mnt_drop_write(hf->f_path.mnt);
4031 +               }
4032 +       }
4033 +
4034 +out_array:
4035 +       au_farray_free(array, max);
4036 +out:
4037 +       AuTraceErr(err);
4038 +       return err;
4039 +}
4040 +
4041 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4042 +             int *do_refresh)
4043 +{
4044 +       int err, rerr;
4045 +       aufs_bindex_t bindex;
4046 +       struct dentry *root;
4047 +       struct au_branch *br;
4048 +       struct au_br_fhsm *bf;
4049 +
4050 +       root = sb->s_root;
4051 +       bindex = au_find_dbindex(root, mod->h_root);
4052 +       if (bindex < 0) {
4053 +               if (remount)
4054 +                       return 0; /* success */
4055 +               err = -ENOENT;
4056 +               pr_err("%s no such branch\n", mod->path);
4057 +               goto out;
4058 +       }
4059 +       AuDbg("bindex b%d\n", bindex);
4060 +
4061 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4062 +       if (unlikely(err))
4063 +               goto out;
4064 +
4065 +       br = au_sbr(sb, bindex);
4066 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4067 +       if (br->br_perm == mod->perm)
4068 +               return 0; /* success */
4069 +
4070 +       /* pre-allocate for non-fhsm --> fhsm */
4071 +       bf = NULL;
4072 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4073 +               err = au_fhsm_br_alloc(br);
4074 +               if (unlikely(err))
4075 +                       goto out;
4076 +               bf = br->br_fhsm;
4077 +               br->br_fhsm = NULL;
4078 +       }
4079 +
4080 +       if (au_br_writable(br->br_perm)) {
4081 +               /* remove whiteout base */
4082 +               err = au_br_init_wh(sb, br, mod->perm);
4083 +               if (unlikely(err))
4084 +                       goto out_bf;
4085 +
4086 +               if (!au_br_writable(mod->perm)) {
4087 +                       /* rw --> ro, file might be mmapped */
4088 +                       DiMustNoWaiters(root);
4089 +                       IiMustNoWaiters(d_inode(root));
4090 +                       di_write_unlock(root);
4091 +                       err = au_br_mod_files_ro(sb, bindex);
4092 +                       /* aufs_write_lock() calls ..._child() */
4093 +                       di_write_lock_child(root);
4094 +
4095 +                       if (unlikely(err)) {
4096 +                               rerr = -ENOMEM;
4097 +                               br->br_wbr = kmalloc(sizeof(*br->br_wbr),
4098 +                                                    GFP_NOFS);
4099 +                               if (br->br_wbr)
4100 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4101 +                               if (unlikely(rerr)) {
4102 +                                       AuIOErr("nested error %d (%d)\n",
4103 +                                               rerr, err);
4104 +                                       br->br_perm = mod->perm;
4105 +                               }
4106 +                       }
4107 +               }
4108 +       } else if (au_br_writable(mod->perm)) {
4109 +               /* ro --> rw */
4110 +               err = -ENOMEM;
4111 +               br->br_wbr = kmalloc(sizeof(*br->br_wbr), GFP_NOFS);
4112 +               if (br->br_wbr) {
4113 +                       err = au_wbr_init(br, sb, mod->perm);
4114 +                       if (unlikely(err)) {
4115 +                               kfree(br->br_wbr);
4116 +                               br->br_wbr = NULL;
4117 +                       }
4118 +               }
4119 +       }
4120 +       if (unlikely(err))
4121 +               goto out_bf;
4122 +
4123 +       if (au_br_fhsm(br->br_perm)) {
4124 +               if (!au_br_fhsm(mod->perm)) {
4125 +                       /* fhsm --> non-fhsm */
4126 +                       au_br_fhsm_fin(br->br_fhsm);
4127 +                       kfree(br->br_fhsm);
4128 +                       br->br_fhsm = NULL;
4129 +               }
4130 +       } else if (au_br_fhsm(mod->perm))
4131 +               /* non-fhsm --> fhsm */
4132 +               br->br_fhsm = bf;
4133 +
4134 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4135 +       br->br_perm = mod->perm;
4136 +       goto out; /* success */
4137 +
4138 +out_bf:
4139 +       kfree(bf);
4140 +out:
4141 +       AuTraceErr(err);
4142 +       return err;
4143 +}
4144 +
4145 +/* ---------------------------------------------------------------------- */
4146 +
4147 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4148 +{
4149 +       int err;
4150 +       struct kstatfs kstfs;
4151 +
4152 +       err = vfs_statfs(&br->br_path, &kstfs);
4153 +       if (!err) {
4154 +               stfs->f_blocks = kstfs.f_blocks;
4155 +               stfs->f_bavail = kstfs.f_bavail;
4156 +               stfs->f_files = kstfs.f_files;
4157 +               stfs->f_ffree = kstfs.f_ffree;
4158 +       }
4159 +
4160 +       return err;
4161 +}
4162 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4163 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4164 +++ linux/fs/aufs/branch.h      2015-09-24 10:47:58.248052907 +0200
4165 @@ -0,0 +1,279 @@
4166 +/*
4167 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4168 + *
4169 + * This program, aufs is free software; you can redistribute it and/or modify
4170 + * it under the terms of the GNU General Public License as published by
4171 + * the Free Software Foundation; either version 2 of the License, or
4172 + * (at your option) any later version.
4173 + *
4174 + * This program is distributed in the hope that it will be useful,
4175 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4176 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4177 + * GNU General Public License for more details.
4178 + *
4179 + * You should have received a copy of the GNU General Public License
4180 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4181 + */
4182 +
4183 +/*
4184 + * branch filesystems and xino for them
4185 + */
4186 +
4187 +#ifndef __AUFS_BRANCH_H__
4188 +#define __AUFS_BRANCH_H__
4189 +
4190 +#ifdef __KERNEL__
4191 +
4192 +#include <linux/mount.h>
4193 +#include "dynop.h"
4194 +#include "rwsem.h"
4195 +#include "super.h"
4196 +
4197 +/* ---------------------------------------------------------------------- */
4198 +
4199 +/* a xino file */
4200 +struct au_xino_file {
4201 +       struct file             *xi_file;
4202 +       struct mutex            xi_nondir_mtx;
4203 +
4204 +       /* todo: make xino files an array to support huge inode number */
4205 +
4206 +#ifdef CONFIG_DEBUG_FS
4207 +       struct dentry            *xi_dbgaufs;
4208 +#endif
4209 +};
4210 +
4211 +/* File-based Hierarchical Storage Management */
4212 +struct au_br_fhsm {
4213 +#ifdef CONFIG_AUFS_FHSM
4214 +       struct mutex            bf_lock;
4215 +       unsigned long           bf_jiffy;
4216 +       struct aufs_stfs        bf_stfs;
4217 +       int                     bf_readable;
4218 +#endif
4219 +};
4220 +
4221 +/* members for writable branch only */
4222 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4223 +struct au_wbr {
4224 +       struct au_rwsem         wbr_wh_rwsem;
4225 +       struct dentry           *wbr_wh[AuBrWh_Last];
4226 +       atomic_t                wbr_wh_running;
4227 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4228 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4229 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4230 +
4231 +       /* mfs mode */
4232 +       unsigned long long      wbr_bytes;
4233 +};
4234 +
4235 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4236 +#define AuBrDynOp (AuDyLast * 4)
4237 +
4238 +#ifdef CONFIG_AUFS_HFSNOTIFY
4239 +/* support for asynchronous destruction */
4240 +struct au_br_hfsnotify {
4241 +       struct fsnotify_group   *hfsn_group;
4242 +};
4243 +#endif
4244 +
4245 +/* sysfs entries */
4246 +struct au_brsysfs {
4247 +       char                    name[16];
4248 +       struct attribute        attr;
4249 +};
4250 +
4251 +enum {
4252 +       AuBrSysfs_BR,
4253 +       AuBrSysfs_BRID,
4254 +       AuBrSysfs_Last
4255 +};
4256 +
4257 +/* protected by superblock rwsem */
4258 +struct au_branch {
4259 +       struct au_xino_file     br_xino;
4260 +
4261 +       aufs_bindex_t           br_id;
4262 +
4263 +       int                     br_perm;
4264 +       struct path             br_path;
4265 +       spinlock_t              br_dykey_lock;
4266 +       struct au_dykey         *br_dykey[AuBrDynOp];
4267 +       atomic_t                br_count;
4268 +
4269 +       struct au_wbr           *br_wbr;
4270 +       struct au_br_fhsm       *br_fhsm;
4271 +
4272 +       /* xino truncation */
4273 +       atomic_t                br_xino_running;
4274 +
4275 +#ifdef CONFIG_AUFS_HFSNOTIFY
4276 +       struct au_br_hfsnotify  *br_hfsn;
4277 +#endif
4278 +
4279 +#ifdef CONFIG_SYSFS
4280 +       /* entries under sysfs per mount-point */
4281 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4282 +#endif
4283 +};
4284 +
4285 +/* ---------------------------------------------------------------------- */
4286 +
4287 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4288 +{
4289 +       return br->br_path.mnt;
4290 +}
4291 +
4292 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4293 +{
4294 +       return br->br_path.dentry;
4295 +}
4296 +
4297 +static inline struct super_block *au_br_sb(struct au_branch *br)
4298 +{
4299 +       return au_br_mnt(br)->mnt_sb;
4300 +}
4301 +
4302 +static inline int au_br_rdonly(struct au_branch *br)
4303 +{
4304 +       return ((au_br_sb(br)->s_flags & MS_RDONLY)
4305 +               || !au_br_writable(br->br_perm))
4306 +               ? -EROFS : 0;
4307 +}
4308 +
4309 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4310 +{
4311 +#ifdef CONFIG_AUFS_HNOTIFY
4312 +       return !(brperm & AuBrPerm_RR);
4313 +#else
4314 +       return 0;
4315 +#endif
4316 +}
4317 +
4318 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4319 +{
4320 +       int err, exec_flag;
4321 +
4322 +       err = 0;
4323 +       exec_flag = oflag & __FMODE_EXEC;
4324 +       if (unlikely(exec_flag && (au_br_mnt(br)->mnt_flags & MNT_NOEXEC)))
4325 +               err = -EACCES;
4326 +
4327 +       return err;
4328 +}
4329 +
4330 +/* ---------------------------------------------------------------------- */
4331 +
4332 +/* branch.c */
4333 +struct au_sbinfo;
4334 +void au_br_free(struct au_sbinfo *sinfo);
4335 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4336 +struct au_opt_add;
4337 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4338 +struct au_opt_del;
4339 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4340 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4341 +#ifdef CONFIG_COMPAT
4342 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4343 +#endif
4344 +struct au_opt_mod;
4345 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4346 +             int *do_refresh);
4347 +struct aufs_stfs;
4348 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4349 +
4350 +/* xino.c */
4351 +static const loff_t au_loff_max = LLONG_MAX;
4352 +
4353 +int au_xib_trunc(struct super_block *sb);
4354 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
4355 +                  loff_t *pos);
4356 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4357 +                   size_t size, loff_t *pos);
4358 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4359 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4360 +ino_t au_xino_new_ino(struct super_block *sb);
4361 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4362 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4363 +                 ino_t ino);
4364 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4365 +                ino_t *ino);
4366 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4367 +              struct file *base_file, int do_test);
4368 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4369 +
4370 +struct au_opt_xino;
4371 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4372 +void au_xino_clr(struct super_block *sb);
4373 +struct file *au_xino_def(struct super_block *sb);
4374 +int au_xino_path(struct seq_file *seq, struct file *file);
4375 +
4376 +/* ---------------------------------------------------------------------- */
4377 +
4378 +/* Superblock to branch */
4379 +static inline
4380 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4381 +{
4382 +       return au_sbr(sb, bindex)->br_id;
4383 +}
4384 +
4385 +static inline
4386 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4387 +{
4388 +       return au_br_mnt(au_sbr(sb, bindex));
4389 +}
4390 +
4391 +static inline
4392 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4393 +{
4394 +       return au_br_sb(au_sbr(sb, bindex));
4395 +}
4396 +
4397 +static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4398 +{
4399 +       atomic_dec(&au_sbr(sb, bindex)->br_count);
4400 +}
4401 +
4402 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4403 +{
4404 +       return au_sbr(sb, bindex)->br_perm;
4405 +}
4406 +
4407 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4408 +{
4409 +       return au_br_whable(au_sbr_perm(sb, bindex));
4410 +}
4411 +
4412 +/* ---------------------------------------------------------------------- */
4413 +
4414 +/*
4415 + * wbr_wh_read_lock, wbr_wh_write_lock
4416 + * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4417 + */
4418 +AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4419 +
4420 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4421 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4422 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4423 +
4424 +/* ---------------------------------------------------------------------- */
4425 +
4426 +#ifdef CONFIG_AUFS_FHSM
4427 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4428 +{
4429 +       mutex_init(&brfhsm->bf_lock);
4430 +       brfhsm->bf_jiffy = 0;
4431 +       brfhsm->bf_readable = 0;
4432 +}
4433 +
4434 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4435 +{
4436 +       mutex_destroy(&brfhsm->bf_lock);
4437 +}
4438 +#else
4439 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4440 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4441 +#endif
4442 +
4443 +#endif /* __KERNEL__ */
4444 +#endif /* __AUFS_BRANCH_H__ */
4445 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4446 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4447 +++ linux/fs/aufs/conf.mk       2015-09-24 10:47:58.248052907 +0200
4448 @@ -0,0 +1,38 @@
4449 +
4450 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4451 +
4452 +define AuConf
4453 +ifdef ${1}
4454 +AuConfStr += ${1}=${${1}}
4455 +endif
4456 +endef
4457 +
4458 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4459 +       SBILIST \
4460 +       HNOTIFY HFSNOTIFY \
4461 +       EXPORT INO_T_64 \
4462 +       XATTR \
4463 +       FHSM \
4464 +       RDU \
4465 +       SHWH \
4466 +       BR_RAMFS \
4467 +       BR_FUSE POLL \
4468 +       BR_HFSPLUS \
4469 +       BDEV_LOOP \
4470 +       DEBUG MAGIC_SYSRQ
4471 +$(foreach i, ${AuConfAll}, \
4472 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4473 +
4474 +AuConfName = ${obj}/conf.str
4475 +${AuConfName}.tmp: FORCE
4476 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4477 +${AuConfName}: ${AuConfName}.tmp
4478 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4479 +       echo '  GEN    ' $@; \
4480 +       cp -p $< $@; \
4481 +       }
4482 +FORCE:
4483 +clean-files += ${AuConfName} ${AuConfName}.tmp
4484 +${obj}/sysfs.o: ${AuConfName}
4485 +
4486 +-include ${srctree}/${src}/conf_priv.mk
4487 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4488 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4489 +++ linux/fs/aufs/cpup.c        2015-09-24 10:47:58.248052907 +0200
4490 @@ -0,0 +1,1319 @@
4491 +/*
4492 + * Copyright (C) 2005-2015 Junjiro R. Okajima
4493 + *
4494 + * This program, aufs is free software; you can redistribute it and/or modify
4495 + * it under the terms of the GNU General Public License as published by
4496 + * the Free Software Foundation; either version 2 of the License, or
4497 + * (at your option) any later version.
4498 + *
4499 + * This program is distributed in the hope that it will be useful,
4500 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4501 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4502 + * GNU General Public License for more details.
4503 + *
4504 + * You should have received a copy of the GNU General Public License
4505 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4506 + */
4507 +
4508 +/*
4509 + * copy-up functions, see wbr_policy.c for copy-down
4510 + */
4511 +
4512 +#include <linux/fs_stack.h>
4513 +#include <linux/mm.h>
4514 +#include "aufs.h"
4515 +
4516 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4517 +{
4518 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4519 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4520 +
4521 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4522 +
4523 +       dst->i_flags |= iflags & ~mask;
4524 +       if (au_test_fs_notime(dst->i_sb))
4525 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4526 +}
4527 +
4528 +void au_cpup_attr_timesizes(struct inode *inode)
4529 +{
4530 +       struct inode *h_inode;
4531 +
4532 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4533 +       fsstack_copy_attr_times(inode, h_inode);
4534 +       fsstack_copy_inode_size(inode, h_inode);
4535 +}
4536 +
4537 +void au_cpup_attr_nlink(struct inode *inode, int force)
4538 +{
4539 +       struct inode *h_inode;
4540 +       struct super_block *sb;
4541 +       aufs_bindex_t bindex, bend;
4542 +
4543 +       sb = inode->i_sb;
4544 +       bindex = au_ibstart(inode);
4545 +       h_inode = au_h_iptr(inode, bindex);
4546 +       if (!force
4547 +           && !S_ISDIR(h_inode->i_mode)
4548 +           && au_opt_test(au_mntflags(sb), PLINK)
4549 +           && au_plink_test(inode))
4550 +               return;
4551 +
4552 +       /*
4553 +        * 0 can happen in revalidating.
4554 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4555 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4556 +        * case.
4557 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4558 +        *       the incorrect link count.
4559 +        */
4560 +       set_nlink(inode, h_inode->i_nlink);
4561 +
4562 +       /*
4563 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4564 +        * it may includes whplink directory.
4565 +        */
4566 +       if (S_ISDIR(h_inode->i_mode)) {
4567 +               bend = au_ibend(inode);
4568 +               for (bindex++; bindex <= bend; bindex++) {
4569 +                       h_inode = au_h_iptr(inode, bindex);
4570 +                       if (h_inode)
4571 +                               au_add_nlink(inode, h_inode);
4572 +               }
4573 +       }
4574 +}
4575 +
4576 +void au_cpup_attr_changeable(struct inode *inode)
4577 +{
4578 +       struct inode *h_inode;
4579 +
4580 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4581 +       inode->i_mode = h_inode->i_mode;
4582 +       inode->i_uid = h_inode->i_uid;
4583 +       inode->i_gid = h_inode->i_gid;
4584 +       au_cpup_attr_timesizes(inode);
4585 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4586 +}
4587 +
4588 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4589 +{
4590 +       struct au_iinfo *iinfo = au_ii(inode);
4591 +
4592 +       IiMustWriteLock(inode);
4593 +
4594 +       iinfo->ii_higen = h_inode->i_generation;
4595 +       iinfo->ii_hsb1 = h_inode->i_sb;
4596 +}
4597 +
4598 +void au_cpup_attr_all(struct inode *inode, int force)
4599 +{
4600 +       struct inode *h_inode;
4601 +
4602 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
4603 +       au_cpup_attr_changeable(inode);
4604 +       if (inode->i_nlink > 0)
4605 +               au_cpup_attr_nlink(inode, force);
4606 +       inode->i_rdev = h_inode->i_rdev;
4607 +       inode->i_blkbits = h_inode->i_blkbits;
4608 +       au_cpup_igen(inode, h_inode);
4609 +}
4610 +
4611 +/* ---------------------------------------------------------------------- */
4612 +
4613 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4614 +
4615 +/* keep the timestamps of the parent dir when cpup */
4616 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4617 +                   struct path *h_path)
4618 +{
4619 +       struct inode *h_inode;
4620 +
4621 +       dt->dt_dentry = dentry;
4622 +       dt->dt_h_path = *h_path;
4623 +       h_inode = d_inode(h_path->dentry);
4624 +       dt->dt_atime = h_inode->i_atime;
4625 +       dt->dt_mtime = h_inode->i_mtime;
4626 +       /* smp_mb(); */
4627 +}
4628 +
4629 +void au_dtime_revert(struct au_dtime *dt)
4630 +{
4631 +       struct iattr attr;
4632 +       int err;
4633 +
4634 +       attr.ia_atime = dt->dt_atime;
4635 +       attr.ia_mtime = dt->dt_mtime;
4636 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4637 +               | ATTR_ATIME | ATTR_ATIME_SET;
4638 +
4639 +       /* no delegation since this is a directory */
4640 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4641 +       if (unlikely(err))
4642 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4643 +}
4644 +
4645 +/* ---------------------------------------------------------------------- */
4646 +
4647 +/* internal use only */
4648 +struct au_cpup_reg_attr {
4649 +       int             valid;
4650 +       struct kstat    st;
4651 +       unsigned int    iflags; /* inode->i_flags */
4652 +};
4653 +
4654 +static noinline_for_stack
4655 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4656 +              struct au_cpup_reg_attr *h_src_attr)
4657 +{
4658 +       int err, sbits, icex;
4659 +       unsigned int mnt_flags;
4660 +       unsigned char verbose;
4661 +       struct iattr ia;
4662 +       struct path h_path;
4663 +       struct inode *h_isrc, *h_idst;
4664 +       struct kstat *h_st;
4665 +       struct au_branch *br;
4666 +
4667 +       h_path.dentry = au_h_dptr(dst, bindex);
4668 +       h_idst = d_inode(h_path.dentry);
4669 +       br = au_sbr(dst->d_sb, bindex);
4670 +       h_path.mnt = au_br_mnt(br);
4671 +       h_isrc = d_inode(h_src);
4672 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4673 +               | ATTR_ATIME | ATTR_MTIME
4674 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4675 +       if (h_src_attr && h_src_attr->valid) {
4676 +               h_st = &h_src_attr->st;
4677 +               ia.ia_uid = h_st->uid;
4678 +               ia.ia_gid = h_st->gid;
4679 +               ia.ia_atime = h_st->atime;
4680 +               ia.ia_mtime = h_st->mtime;
4681 +               if (h_idst->i_mode != h_st->mode
4682 +                   && !S_ISLNK(h_idst->i_mode)) {
4683 +                       ia.ia_valid |= ATTR_MODE;
4684 +                       ia.ia_mode = h_st->mode;
4685 +               }
4686 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4687 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4688 +       } else {
4689 +               ia.ia_uid = h_isrc->i_uid;
4690 +               ia.ia_gid = h_isrc->i_gid;
4691 +               ia.ia_atime = h_isrc->i_atime;
4692 +               ia.ia_mtime = h_isrc->i_mtime;
4693 +               if (h_idst->i_mode != h_isrc->i_mode
4694 +                   && !S_ISLNK(h_idst->i_mode)) {
4695 +                       ia.ia_valid |= ATTR_MODE;
4696 +                       ia.ia_mode = h_isrc->i_mode;
4697 +               }
4698 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4699 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4700 +       }
4701 +       /* no delegation since it is just created */
4702 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4703 +
4704 +       /* is this nfs only? */
4705 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4706 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4707 +               ia.ia_mode = h_isrc->i_mode;
4708 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4709 +       }
4710 +
4711 +       icex = br->br_perm & AuBrAttr_ICEX;
4712 +       if (!err) {
4713 +               mnt_flags = au_mntflags(dst->d_sb);
4714 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4715 +               err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4716 +       }
4717 +
4718 +       return err;
4719 +}
4720 +
4721 +/* ---------------------------------------------------------------------- */
4722 +
4723 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4724 +                          char *buf, unsigned long blksize)
4725 +{
4726 +       int err;
4727 +       size_t sz, rbytes, wbytes;
4728 +       unsigned char all_zero;
4729 +       char *p, *zp;
4730 +       struct mutex *h_mtx;
4731 +       /* reduce stack usage */
4732 +       struct iattr *ia;
4733 +
4734 +       zp = page_address(ZERO_PAGE(0));
4735 +       if (unlikely(!zp))
4736 +               return -ENOMEM; /* possible? */
4737 +
4738 +       err = 0;
4739 +       all_zero = 0;
4740 +       while (len) {
4741 +               AuDbg("len %lld\n", len);
4742 +               sz = blksize;
4743 +               if (len < blksize)
4744 +                       sz = len;
4745 +
4746 +               rbytes = 0;
4747 +               /* todo: signal_pending? */
4748 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4749 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4750 +                       err = rbytes;
4751 +               }
4752 +               if (unlikely(err < 0))
4753 +                       break;
4754 +
4755 +               all_zero = 0;
4756 +               if (len >= rbytes && rbytes == blksize)
4757 +                       all_zero = !memcmp(buf, zp, rbytes);
4758 +               if (!all_zero) {
4759 +                       wbytes = rbytes;
4760 +                       p = buf;
4761 +                       while (wbytes) {
4762 +                               size_t b;
4763 +
4764 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4765 +                               err = b;
4766 +                               /* todo: signal_pending? */
4767 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
4768 +                                       continue;
4769 +                               if (unlikely(err < 0))
4770 +                                       break;
4771 +                               wbytes -= b;
4772 +                               p += b;
4773 +                       }
4774 +                       if (unlikely(err < 0))
4775 +                               break;
4776 +               } else {
4777 +                       loff_t res;
4778 +
4779 +                       AuLabel(hole);
4780 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4781 +                       err = res;
4782 +                       if (unlikely(res < 0))
4783 +                               break;
4784 +               }
4785 +               len -= rbytes;
4786 +               err = 0;
4787 +       }
4788 +
4789 +       /* the last block may be a hole */
4790 +       if (!err && all_zero) {
4791 +               AuLabel(last hole);
4792 +
4793 +               err = 1;
4794 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
4795 +                       /* nfs requires this step to make last hole */
4796 +                       /* is this only nfs? */
4797 +                       do {
4798 +                               /* todo: signal_pending? */
4799 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4800 +                       } while (err == -EAGAIN || err == -EINTR);
4801 +                       if (err == 1)
4802 +                               dst->f_pos--;
4803 +               }
4804 +
4805 +               if (err == 1) {
4806 +                       ia = (void *)buf;
4807 +                       ia->ia_size = dst->f_pos;
4808 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4809 +                       ia->ia_file = dst;
4810 +                       h_mtx = &file_inode(dst)->i_mutex;
4811 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
4812 +                       /* no delegation since it is just created */
4813 +                       err = vfsub_notify_change(&dst->f_path, ia,
4814 +                                                 /*delegated*/NULL);
4815 +                       mutex_unlock(h_mtx);
4816 +               }
4817 +       }
4818 +
4819 +       return err;
4820 +}
4821 +
4822 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
4823 +{
4824 +       int err;
4825 +       unsigned long blksize;
4826 +       unsigned char do_kfree;
4827 +       char *buf;
4828 +
4829 +       err = -ENOMEM;
4830 +       blksize = dst->f_path.dentry->d_sb->s_blocksize;
4831 +       if (!blksize || PAGE_SIZE < blksize)
4832 +               blksize = PAGE_SIZE;
4833 +       AuDbg("blksize %lu\n", blksize);
4834 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4835 +       if (do_kfree)
4836 +               buf = kmalloc(blksize, GFP_NOFS);
4837 +       else
4838 +               buf = (void *)__get_free_page(GFP_NOFS);
4839 +       if (unlikely(!buf))
4840 +               goto out;
4841 +
4842 +       if (len > (1 << 22))
4843 +               AuDbg("copying a large file %lld\n", (long long)len);
4844 +
4845 +       src->f_pos = 0;
4846 +       dst->f_pos = 0;
4847 +       err = au_do_copy_file(dst, src, len, buf, blksize);
4848 +       if (do_kfree)
4849 +               kfree(buf);
4850 +       else
4851 +               free_page((unsigned long)buf);
4852 +
4853 +out:
4854 +       return err;
4855 +}
4856 +
4857 +/*
4858 + * to support a sparse file which is opened with O_APPEND,
4859 + * we need to close the file.
4860 + */
4861 +static int au_cp_regular(struct au_cp_generic *cpg)
4862 +{
4863 +       int err, i;
4864 +       enum { SRC, DST };
4865 +       struct {
4866 +               aufs_bindex_t bindex;
4867 +               unsigned int flags;
4868 +               struct dentry *dentry;
4869 +               int force_wr;
4870 +               struct file *file;
4871 +               void *label;
4872 +       } *f, file[] = {
4873 +               {
4874 +                       .bindex = cpg->bsrc,
4875 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
4876 +                       .label = &&out
4877 +               },
4878 +               {
4879 +                       .bindex = cpg->bdst,
4880 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
4881 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
4882 +                       .label = &&out_src
4883 +               }
4884 +       };
4885 +       struct super_block *sb;
4886 +
4887 +       /* bsrc branch can be ro/rw. */
4888 +       sb = cpg->dentry->d_sb;
4889 +       f = file;
4890 +       for (i = 0; i < 2; i++, f++) {
4891 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
4892 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
4893 +                                   /*file*/NULL, f->force_wr);
4894 +               err = PTR_ERR(f->file);
4895 +               if (IS_ERR(f->file))
4896 +                       goto *f->label;
4897 +       }
4898 +
4899 +       /* try stopping to update while we copyup */
4900 +       IMustLock(d_inode(file[SRC].dentry));
4901 +       err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
4902 +
4903 +       fput(file[DST].file);
4904 +       au_sbr_put(sb, file[DST].bindex);
4905 +
4906 +out_src:
4907 +       fput(file[SRC].file);
4908 +       au_sbr_put(sb, file[SRC].bindex);
4909 +out:
4910 +       return err;
4911 +}
4912 +
4913 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
4914 +                             struct au_cpup_reg_attr *h_src_attr)
4915 +{
4916 +       int err, rerr;
4917 +       loff_t l;
4918 +       struct path h_path;
4919 +       struct inode *h_src_inode, *h_dst_inode;
4920 +
4921 +       err = 0;
4922 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
4923 +       l = i_size_read(h_src_inode);
4924 +       if (cpg->len == -1 || l < cpg->len)
4925 +               cpg->len = l;
4926 +       if (cpg->len) {
4927 +               /* try stopping to update while we are referencing */
4928 +               mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4929 +               au_pin_hdir_unlock(cpg->pin);
4930 +
4931 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
4932 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
4933 +               h_src_attr->iflags = h_src_inode->i_flags;
4934 +               if (!au_test_nfs(h_src_inode->i_sb))
4935 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
4936 +               else {
4937 +                       mutex_unlock(&h_src_inode->i_mutex);
4938 +                       err = vfs_getattr(&h_path, &h_src_attr->st);
4939 +                       mutex_lock_nested(&h_src_inode->i_mutex, AuLsc_I_CHILD);
4940 +               }
4941 +               if (unlikely(err)) {
4942 +                       mutex_unlock(&h_src_inode->i_mutex);
4943 +                       goto out;
4944 +               }
4945 +               h_src_attr->valid = 1;
4946 +               err = au_cp_regular(cpg);
4947 +               mutex_unlock(&h_src_inode->i_mutex);
4948 +               rerr = au_pin_hdir_relock(cpg->pin);
4949 +               if (!err && rerr)
4950 +                       err = rerr;
4951 +       }
4952 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
4953 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
4954 +               h_dst_inode = d_inode(h_path.dentry);
4955 +               spin_lock(&h_dst_inode->i_lock);
4956 +               h_dst_inode->i_state |= I_LINKABLE;
4957 +               spin_unlock(&h_dst_inode->i_lock);
4958 +       }
4959 +
4960 +out:
4961 +       return err;
4962 +}
4963 +
4964 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
4965 +                             struct inode *h_dir)
4966 +{
4967 +       int err, symlen;
4968 +       mm_segment_t old_fs;
4969 +       union {
4970 +               char *k;
4971 +               char __user *u;
4972 +       } sym;
4973 +       struct inode *h_inode = d_inode(h_src);
4974 +       const struct inode_operations *h_iop = h_inode->i_op;
4975 +
4976 +       err = -ENOSYS;
4977 +       if (unlikely(!h_iop->readlink))
4978 +               goto out;
4979 +
4980 +       err = -ENOMEM;
4981 +       sym.k = (void *)__get_free_page(GFP_NOFS);
4982 +       if (unlikely(!sym.k))
4983 +               goto out;
4984 +
4985 +       /* unnecessary to support mmap_sem since symlink is not mmap-able */
4986 +       old_fs = get_fs();
4987 +       set_fs(KERNEL_DS);
4988 +       symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
4989 +       err = symlen;
4990 +       set_fs(old_fs);
4991 +
4992 +       if (symlen > 0) {
4993 +               sym.k[symlen] = 0;
4994 +               err = vfsub_symlink(h_dir, h_path, sym.k);
4995 +       }
4996 +       free_page((unsigned long)sym.k);
4997 +
4998 +out:
4999 +       return err;
5000 +}
5001 +
5002 +static noinline_for_stack
5003 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5004 +              struct au_cpup_reg_attr *h_src_attr)
5005 +{
5006 +       int err;
5007 +       umode_t mode;
5008 +       unsigned int mnt_flags;
5009 +       unsigned char isdir, isreg, force;
5010 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5011 +       struct au_dtime dt;
5012 +       struct path h_path;
5013 +       struct dentry *h_src, *h_dst, *h_parent;
5014 +       struct inode *h_inode, *h_dir, *dir, *inode;
5015 +       struct super_block *sb;
5016 +
5017 +       /* bsrc branch can be ro/rw. */
5018 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5019 +       h_inode = d_inode(h_src);
5020 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5021 +
5022 +       /* try stopping to be referenced while we are creating */
5023 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5024 +       if (au_ftest_cpup(cpg->flags, RENAME))
5025 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5026 +                                 AUFS_WH_PFX_LEN));
5027 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5028 +       h_dir = d_inode(h_parent);
5029 +       IMustLock(h_dir);
5030 +       AuDebugOn(h_parent != h_dst->d_parent);
5031 +
5032 +       sb = cpg->dentry->d_sb;
5033 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5034 +       if (do_dt) {
5035 +               h_path.dentry = h_parent;
5036 +               au_dtime_store(&dt, dst_parent, &h_path);
5037 +       }
5038 +       h_path.dentry = h_dst;
5039 +
5040 +       isreg = 0;
5041 +       isdir = 0;
5042 +       mode = h_inode->i_mode;
5043 +       switch (mode & S_IFMT) {
5044 +       case S_IFREG:
5045 +               isreg = 1;
5046 +               err = vfsub_create(h_dir, &h_path, mode | S_IWUSR,
5047 +                                  /*want_excl*/true);
5048 +               if (!err)
5049 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5050 +               break;
5051 +       case S_IFDIR:
5052 +               isdir = 1;
5053 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5054 +               if (!err) {
5055 +                       /*
5056 +                        * strange behaviour from the users view,
5057 +                        * particularry setattr case
5058 +                        */
5059 +                       dir = d_inode(dst_parent);
5060 +                       if (au_ibstart(dir) == cpg->bdst)
5061 +                               au_cpup_attr_nlink(dir, /*force*/1);
5062 +                       inode = d_inode(cpg->dentry);
5063 +                       au_cpup_attr_nlink(inode, /*force*/1);
5064 +               }
5065 +               break;
5066 +       case S_IFLNK:
5067 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5068 +               break;
5069 +       case S_IFCHR:
5070 +       case S_IFBLK:
5071 +               AuDebugOn(!capable(CAP_MKNOD));
5072 +               /*FALLTHROUGH*/
5073 +       case S_IFIFO:
5074 +       case S_IFSOCK:
5075 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5076 +               break;
5077 +       default:
5078 +               AuIOErr("Unknown inode type 0%o\n", mode);
5079 +               err = -EIO;
5080 +       }
5081 +
5082 +       mnt_flags = au_mntflags(sb);
5083 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5084 +           && !isdir
5085 +           && au_opt_test(mnt_flags, XINO)
5086 +           && (h_inode->i_nlink == 1
5087 +               || (h_inode->i_state & I_LINKABLE))
5088 +           /* todo: unnecessary? */
5089 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5090 +           && cpg->bdst < cpg->bsrc
5091 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5092 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5093 +               /* ignore this error */
5094 +
5095 +       if (!err) {
5096 +               force = 0;
5097 +               if (isreg) {
5098 +                       force = !!cpg->len;
5099 +                       if (cpg->len == -1)
5100 +                               force = !!i_size_read(h_inode);
5101 +               }
5102 +               au_fhsm_wrote(sb, cpg->bdst, force);
5103 +       }
5104 +
5105 +       if (do_dt)
5106 +               au_dtime_revert(&dt);
5107 +       return err;
5108 +}
5109 +
5110 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5111 +{
5112 +       int err;
5113 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5114 +       struct inode *h_dir;
5115 +       aufs_bindex_t bdst;
5116 +
5117 +       dentry = cpg->dentry;
5118 +       bdst = cpg->bdst;
5119 +       h_dentry = au_h_dptr(dentry, bdst);
5120 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5121 +               dget(h_dentry);
5122 +               au_set_h_dptr(dentry, bdst, NULL);
5123 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5124 +               if (!err)
5125 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5126 +               au_set_h_dptr(dentry, bdst, h_dentry);
5127 +       } else {
5128 +               err = 0;
5129 +               parent = dget_parent(dentry);
5130 +               h_parent = au_h_dptr(parent, bdst);
5131 +               dput(parent);
5132 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5133 +               if (IS_ERR(h_path->dentry))
5134 +                       err = PTR_ERR(h_path->dentry);
5135 +       }
5136 +       if (unlikely(err))
5137 +               goto out;
5138 +
5139 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5140 +       h_dir = d_inode(h_parent);
5141 +       IMustLock(h_dir);
5142 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5143 +       /* no delegation since it is just created */
5144 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
5145 +       dput(h_path->dentry);
5146 +
5147 +out:
5148 +       return err;
5149 +}
5150 +
5151 +/*
5152 + * copyup the @dentry from @bsrc to @bdst.
5153 + * the caller must set the both of lower dentries.
5154 + * @len is for truncating when it is -1 copyup the entire file.
5155 + * in link/rename cases, @dst_parent may be different from the real one.
5156 + * basic->bsrc can be larger than basic->bdst.
5157 + */
5158 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5159 +{
5160 +       int err, rerr;
5161 +       aufs_bindex_t old_ibstart;
5162 +       unsigned char isdir, plink;
5163 +       struct dentry *h_src, *h_dst, *h_parent;
5164 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5165 +       struct super_block *sb;
5166 +       struct au_branch *br;
5167 +       /* to reuduce stack size */
5168 +       struct {
5169 +               struct au_dtime dt;
5170 +               struct path h_path;
5171 +               struct au_cpup_reg_attr h_src_attr;
5172 +       } *a;
5173 +
5174 +       err = -ENOMEM;
5175 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5176 +       if (unlikely(!a))
5177 +               goto out;
5178 +       a->h_src_attr.valid = 0;
5179 +
5180 +       sb = cpg->dentry->d_sb;
5181 +       br = au_sbr(sb, cpg->bdst);
5182 +       a->h_path.mnt = au_br_mnt(br);
5183 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5184 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5185 +       h_dir = d_inode(h_parent);
5186 +       IMustLock(h_dir);
5187 +
5188 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5189 +       inode = d_inode(cpg->dentry);
5190 +
5191 +       if (!dst_parent)
5192 +               dst_parent = dget_parent(cpg->dentry);
5193 +       else
5194 +               dget(dst_parent);
5195 +
5196 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5197 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5198 +       if (dst_inode) {
5199 +               if (unlikely(!plink)) {
5200 +                       err = -EIO;
5201 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5202 +                               "but plink is disabled\n",
5203 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5204 +                       goto out_parent;
5205 +               }
5206 +
5207 +               if (dst_inode->i_nlink) {
5208 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5209 +
5210 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5211 +                       err = PTR_ERR(h_src);
5212 +                       if (IS_ERR(h_src))
5213 +                               goto out_parent;
5214 +                       if (unlikely(d_is_negative(h_src))) {
5215 +                               err = -EIO;
5216 +                               AuIOErr("i%lu exists on a upper branch "
5217 +                                       "but not pseudo-linked\n",
5218 +                                       inode->i_ino);
5219 +                               dput(h_src);
5220 +                               goto out_parent;
5221 +                       }
5222 +
5223 +                       if (do_dt) {
5224 +                               a->h_path.dentry = h_parent;
5225 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5226 +                       }
5227 +
5228 +                       a->h_path.dentry = h_dst;
5229 +                       delegated = NULL;
5230 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5231 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5232 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5233 +                       if (do_dt)
5234 +                               au_dtime_revert(&a->dt);
5235 +                       if (unlikely(err == -EWOULDBLOCK)) {
5236 +                               pr_warn("cannot retry for NFSv4 delegation"
5237 +                                       " for an internal link\n");
5238 +                               iput(delegated);
5239 +                       }
5240 +                       dput(h_src);
5241 +                       goto out_parent;
5242 +               } else
5243 +                       /* todo: cpup_wh_file? */
5244 +                       /* udba work */
5245 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5246 +       }
5247 +
5248 +       isdir = S_ISDIR(inode->i_mode);
5249 +       old_ibstart = au_ibstart(inode);
5250 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5251 +       if (unlikely(err))
5252 +               goto out_rev;
5253 +       dst_inode = d_inode(h_dst);
5254 +       mutex_lock_nested(&dst_inode->i_mutex, AuLsc_I_CHILD2);
5255 +       /* todo: necessary? */
5256 +       /* au_pin_hdir_unlock(cpg->pin); */
5257 +
5258 +       err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
5259 +       if (unlikely(err)) {
5260 +               /* todo: necessary? */
5261 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5262 +               mutex_unlock(&dst_inode->i_mutex);
5263 +               goto out_rev;
5264 +       }
5265 +
5266 +       if (cpg->bdst < old_ibstart) {
5267 +               if (S_ISREG(inode->i_mode)) {
5268 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5269 +                       if (unlikely(err)) {
5270 +                               /* ignore an error */
5271 +                               /* au_pin_hdir_relock(cpg->pin); */
5272 +                               mutex_unlock(&dst_inode->i_mutex);
5273 +                               goto out_rev;
5274 +                       }
5275 +               }
5276 +               au_set_ibstart(inode, cpg->bdst);
5277 +       } else
5278 +               au_set_ibend(inode, cpg->bdst);
5279 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5280 +                     au_hi_flags(inode, isdir));
5281 +
5282 +       /* todo: necessary? */
5283 +       /* err = au_pin_hdir_relock(cpg->pin); */
5284 +       mutex_unlock(&dst_inode->i_mutex);
5285 +       if (unlikely(err))
5286 +               goto out_rev;
5287 +
5288 +       src_inode = d_inode(h_src);
5289 +       if (!isdir
5290 +           && (src_inode->i_nlink > 1
5291 +               || src_inode->i_state & I_LINKABLE)
5292 +           && plink)
5293 +               au_plink_append(inode, cpg->bdst, h_dst);
5294 +
5295 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5296 +               a->h_path.dentry = h_dst;
5297 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5298 +       }
5299 +       if (!err)
5300 +               goto out_parent; /* success */
5301 +
5302 +       /* revert */
5303 +out_rev:
5304 +       a->h_path.dentry = h_parent;
5305 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5306 +       a->h_path.dentry = h_dst;
5307 +       rerr = 0;
5308 +       if (d_is_positive(h_dst)) {
5309 +               if (!isdir) {
5310 +                       /* no delegation since it is just created */
5311 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5312 +                                           /*delegated*/NULL, /*force*/0);
5313 +               } else
5314 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5315 +       }
5316 +       au_dtime_revert(&a->dt);
5317 +       if (rerr) {
5318 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5319 +               err = -EIO;
5320 +       }
5321 +out_parent:
5322 +       dput(dst_parent);
5323 +       kfree(a);
5324 +out:
5325 +       return err;
5326 +}
5327 +
5328 +#if 0 /* reserved */
5329 +struct au_cpup_single_args {
5330 +       int *errp;
5331 +       struct au_cp_generic *cpg;
5332 +       struct dentry *dst_parent;
5333 +};
5334 +
5335 +static void au_call_cpup_single(void *args)
5336 +{
5337 +       struct au_cpup_single_args *a = args;
5338 +
5339 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5340 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5341 +       au_pin_hdir_release(a->cpg->pin);
5342 +}
5343 +#endif
5344 +
5345 +/*
5346 + * prevent SIGXFSZ in copy-up.
5347 + * testing CAP_MKNOD is for generic fs,
5348 + * but CAP_FSETID is for xfs only, currently.
5349 + */
5350 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5351 +{
5352 +       int do_sio;
5353 +       struct super_block *sb;
5354 +       struct inode *h_dir;
5355 +
5356 +       do_sio = 0;
5357 +       sb = au_pinned_parent(pin)->d_sb;
5358 +       if (!au_wkq_test()
5359 +           && (!au_sbi(sb)->si_plink_maint_pid
5360 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5361 +               switch (mode & S_IFMT) {
5362 +               case S_IFREG:
5363 +                       /* no condition about RLIMIT_FSIZE and the file size */
5364 +                       do_sio = 1;
5365 +                       break;
5366 +               case S_IFCHR:
5367 +               case S_IFBLK:
5368 +                       do_sio = !capable(CAP_MKNOD);
5369 +                       break;
5370 +               }
5371 +               if (!do_sio)
5372 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5373 +                                 && !capable(CAP_FSETID));
5374 +               /* this workaround may be removed in the future */
5375 +               if (!do_sio) {
5376 +                       h_dir = au_pinned_h_dir(pin);
5377 +                       do_sio = h_dir->i_mode & S_ISVTX;
5378 +               }
5379 +       }
5380 +
5381 +       return do_sio;
5382 +}
5383 +
5384 +#if 0 /* reserved */
5385 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5386 +{
5387 +       int err, wkq_err;
5388 +       struct dentry *h_dentry;
5389 +
5390 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5391 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5392 +               err = au_cpup_single(cpg, dst_parent);
5393 +       else {
5394 +               struct au_cpup_single_args args = {
5395 +                       .errp           = &err,
5396 +                       .cpg            = cpg,
5397 +                       .dst_parent     = dst_parent
5398 +               };
5399 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5400 +               if (unlikely(wkq_err))
5401 +                       err = wkq_err;
5402 +       }
5403 +
5404 +       return err;
5405 +}
5406 +#endif
5407 +
5408 +/*
5409 + * copyup the @dentry from the first active lower branch to @bdst,
5410 + * using au_cpup_single().
5411 + */
5412 +static int au_cpup_simple(struct au_cp_generic *cpg)
5413 +{
5414 +       int err;
5415 +       unsigned int flags_orig;
5416 +       struct dentry *dentry;
5417 +
5418 +       AuDebugOn(cpg->bsrc < 0);
5419 +
5420 +       dentry = cpg->dentry;
5421 +       DiMustWriteLock(dentry);
5422 +
5423 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5424 +       if (!err) {
5425 +               flags_orig = cpg->flags;
5426 +               au_fset_cpup(cpg->flags, RENAME);
5427 +               err = au_cpup_single(cpg, NULL);
5428 +               cpg->flags = flags_orig;
5429 +               if (!err)
5430 +                       return 0; /* success */
5431 +
5432 +               /* revert */
5433 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5434 +               au_set_dbstart(dentry, cpg->bsrc);
5435 +       }
5436 +
5437 +       return err;
5438 +}
5439 +
5440 +struct au_cpup_simple_args {
5441 +       int *errp;
5442 +       struct au_cp_generic *cpg;
5443 +};
5444 +
5445 +static void au_call_cpup_simple(void *args)
5446 +{
5447 +       struct au_cpup_simple_args *a = args;
5448 +
5449 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5450 +       *a->errp = au_cpup_simple(a->cpg);
5451 +       au_pin_hdir_release(a->cpg->pin);
5452 +}
5453 +
5454 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5455 +{
5456 +       int err, wkq_err;
5457 +       struct dentry *dentry, *parent;
5458 +       struct file *h_file;
5459 +       struct inode *h_dir;
5460 +
5461 +       dentry = cpg->dentry;
5462 +       h_file = NULL;
5463 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5464 +               AuDebugOn(cpg->bsrc < 0);
5465 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5466 +               err = PTR_ERR(h_file);
5467 +               if (IS_ERR(h_file))
5468 +                       goto out;
5469 +       }
5470 +
5471 +       parent = dget_parent(dentry);
5472 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5473 +       if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5474 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5475 +               err = au_cpup_simple(cpg);
5476 +       else {
5477 +               struct au_cpup_simple_args args = {
5478 +                       .errp           = &err,
5479 +                       .cpg            = cpg
5480 +               };
5481 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5482 +               if (unlikely(wkq_err))
5483 +                       err = wkq_err;
5484 +       }
5485 +
5486 +       dput(parent);
5487 +       if (h_file)
5488 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5489 +
5490 +out:
5491 +       return err;
5492 +}
5493 +
5494 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5495 +{
5496 +       aufs_bindex_t bsrc, bend;
5497 +       struct dentry *dentry, *h_dentry;
5498 +
5499 +       if (cpg->bsrc < 0) {
5500 +               dentry = cpg->dentry;
5501 +               bend = au_dbend(dentry);
5502 +               for (bsrc = cpg->bdst + 1; bsrc <= bend; bsrc++) {
5503 +                       h_dentry = au_h_dptr(dentry, bsrc);
5504 +                       if (h_dentry) {
5505 +                               AuDebugOn(d_is_negative(h_dentry));
5506 +                               break;
5507 +                       }
5508 +               }
5509 +               AuDebugOn(bsrc > bend);
5510 +               cpg->bsrc = bsrc;
5511 +       }
5512 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5513 +       return au_do_sio_cpup_simple(cpg);
5514 +}
5515 +
5516 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5517 +{
5518 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5519 +       return au_do_sio_cpup_simple(cpg);
5520 +}
5521 +
5522 +/* ---------------------------------------------------------------------- */
5523 +
5524 +/*
5525 + * copyup the deleted file for writing.
5526 + */
5527 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5528 +                        struct file *file)
5529 +{
5530 +       int err;
5531 +       unsigned int flags_orig;
5532 +       aufs_bindex_t bsrc_orig;
5533 +       struct dentry *h_d_dst, *h_d_start;
5534 +       struct au_dinfo *dinfo;
5535 +       struct au_hdentry *hdp;
5536 +
5537 +       dinfo = au_di(cpg->dentry);
5538 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5539 +
5540 +       bsrc_orig = cpg->bsrc;
5541 +       cpg->bsrc = dinfo->di_bstart;
5542 +       hdp = dinfo->di_hdentry;
5543 +       h_d_dst = hdp[0 + cpg->bdst].hd_dentry;
5544 +       dinfo->di_bstart = cpg->bdst;
5545 +       hdp[0 + cpg->bdst].hd_dentry = wh_dentry;
5546 +       h_d_start = NULL;
5547 +       if (file) {
5548 +               h_d_start = hdp[0 + cpg->bsrc].hd_dentry;
5549 +               hdp[0 + cpg->bsrc].hd_dentry = au_hf_top(file)->f_path.dentry;
5550 +       }
5551 +       flags_orig = cpg->flags;
5552 +       cpg->flags = !AuCpup_DTIME;
5553 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5554 +       cpg->flags = flags_orig;
5555 +       if (file) {
5556 +               if (!err)
5557 +                       err = au_reopen_nondir(file);
5558 +               hdp[0 + cpg->bsrc].hd_dentry = h_d_start;
5559 +       }
5560 +       hdp[0 + cpg->bdst].hd_dentry = h_d_dst;
5561 +       dinfo->di_bstart = cpg->bsrc;
5562 +       cpg->bsrc = bsrc_orig;
5563 +
5564 +       return err;
5565 +}
5566 +
5567 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5568 +{
5569 +       int err;
5570 +       aufs_bindex_t bdst;
5571 +       struct au_dtime dt;
5572 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5573 +       struct au_branch *br;
5574 +       struct path h_path;
5575 +
5576 +       dentry = cpg->dentry;
5577 +       bdst = cpg->bdst;
5578 +       br = au_sbr(dentry->d_sb, bdst);
5579 +       parent = dget_parent(dentry);
5580 +       h_parent = au_h_dptr(parent, bdst);
5581 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5582 +       err = PTR_ERR(wh_dentry);
5583 +       if (IS_ERR(wh_dentry))
5584 +               goto out;
5585 +
5586 +       h_path.dentry = h_parent;
5587 +       h_path.mnt = au_br_mnt(br);
5588 +       au_dtime_store(&dt, parent, &h_path);
5589 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5590 +       if (unlikely(err))
5591 +               goto out_wh;
5592 +
5593 +       dget(wh_dentry);
5594 +       h_path.dentry = wh_dentry;
5595 +       if (!d_is_dir(wh_dentry)) {
5596 +               /* no delegation since it is just created */
5597 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5598 +                                  /*delegated*/NULL, /*force*/0);
5599 +       } else
5600 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5601 +       if (unlikely(err)) {
5602 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5603 +                       wh_dentry, err);
5604 +               err = -EIO;
5605 +       }
5606 +       au_dtime_revert(&dt);
5607 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5608 +
5609 +out_wh:
5610 +       dput(wh_dentry);
5611 +out:
5612 +       dput(parent);
5613 +       return err;
5614 +}
5615 +
5616 +struct au_cpup_wh_args {
5617 +       int *errp;
5618 +       struct au_cp_generic *cpg;
5619 +       struct file *file;
5620 +};
5621 +
5622 +static void au_call_cpup_wh(void *args)
5623 +{
5624 +       struct au_cpup_wh_args *a = args;
5625 +
5626 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5627 +       *a->errp = au_cpup_wh(a->cpg, a->file);
5628 +       au_pin_hdir_release(a->cpg->pin);
5629 +}
5630 +
5631 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5632 +{
5633 +       int err, wkq_err;
5634 +       aufs_bindex_t bdst;
5635 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
5636 +       struct inode *dir, *h_dir, *h_tmpdir;
5637 +       struct au_wbr *wbr;
5638 +       struct au_pin wh_pin, *pin_orig;
5639 +
5640 +       dentry = cpg->dentry;
5641 +       bdst = cpg->bdst;
5642 +       parent = dget_parent(dentry);
5643 +       dir = d_inode(parent);
5644 +       h_orph = NULL;
5645 +       h_parent = NULL;
5646 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
5647 +       h_tmpdir = h_dir;
5648 +       pin_orig = NULL;
5649 +       if (!h_dir->i_nlink) {
5650 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5651 +               h_orph = wbr->wbr_orph;
5652 +
5653 +               h_parent = dget(au_h_dptr(parent, bdst));
5654 +               au_set_h_dptr(parent, bdst, dget(h_orph));
5655 +               h_tmpdir = d_inode(h_orph);
5656 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5657 +
5658 +               mutex_lock_nested(&h_tmpdir->i_mutex, AuLsc_I_PARENT3);
5659 +               /* todo: au_h_open_pre()? */
5660 +
5661 +               pin_orig = cpg->pin;
5662 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
5663 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5664 +               cpg->pin = &wh_pin;
5665 +       }
5666 +
5667 +       if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5668 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5669 +               err = au_cpup_wh(cpg, file);
5670 +       else {
5671 +               struct au_cpup_wh_args args = {
5672 +                       .errp   = &err,
5673 +                       .cpg    = cpg,
5674 +                       .file   = file
5675 +               };
5676 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5677 +               if (unlikely(wkq_err))
5678 +                       err = wkq_err;
5679 +       }
5680 +
5681 +       if (h_orph) {
5682 +               mutex_unlock(&h_tmpdir->i_mutex);
5683 +               /* todo: au_h_open_post()? */
5684 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
5685 +               au_set_h_dptr(parent, bdst, h_parent);
5686 +               AuDebugOn(!pin_orig);
5687 +               cpg->pin = pin_orig;
5688 +       }
5689 +       iput(h_dir);
5690 +       dput(parent);
5691 +
5692 +       return err;
5693 +}
5694 +
5695 +/* ---------------------------------------------------------------------- */
5696 +
5697 +/*
5698 + * generic routine for both of copy-up and copy-down.
5699 + */
5700 +/* cf. revalidate function in file.c */
5701 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5702 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5703 +                        struct au_pin *pin,
5704 +                        struct dentry *h_parent, void *arg),
5705 +              void *arg)
5706 +{
5707 +       int err;
5708 +       struct au_pin pin;
5709 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
5710 +
5711 +       err = 0;
5712 +       parent = dget_parent(dentry);
5713 +       if (IS_ROOT(parent))
5714 +               goto out;
5715 +
5716 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5717 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5718 +
5719 +       /* do not use au_dpage */
5720 +       real_parent = parent;
5721 +       while (1) {
5722 +               dput(parent);
5723 +               parent = dget_parent(dentry);
5724 +               h_parent = au_h_dptr(parent, bdst);
5725 +               if (h_parent)
5726 +                       goto out; /* success */
5727 +
5728 +               /* find top dir which is necessary to cpup */
5729 +               do {
5730 +                       d = parent;
5731 +                       dput(parent);
5732 +                       parent = dget_parent(d);
5733 +                       di_read_lock_parent3(parent, !AuLock_IR);
5734 +                       h_parent = au_h_dptr(parent, bdst);
5735 +                       di_read_unlock(parent, !AuLock_IR);
5736 +               } while (!h_parent);
5737 +
5738 +               if (d != real_parent)
5739 +                       di_write_lock_child3(d);
5740 +
5741 +               /* somebody else might create while we were sleeping */
5742 +               h_dentry = au_h_dptr(d, bdst);
5743 +               if (!h_dentry || d_is_negative(h_dentry)) {
5744 +                       if (h_dentry)
5745 +                               au_update_dbstart(d);
5746 +
5747 +                       au_pin_set_dentry(&pin, d);
5748 +                       err = au_do_pin(&pin);
5749 +                       if (!err) {
5750 +                               err = cp(d, bdst, &pin, h_parent, arg);
5751 +                               au_unpin(&pin);
5752 +                       }
5753 +               }
5754 +
5755 +               if (d != real_parent)
5756 +                       di_write_unlock(d);
5757 +               if (unlikely(err))
5758 +                       break;
5759 +       }
5760 +
5761 +out:
5762 +       dput(parent);
5763 +       return err;
5764 +}
5765 +
5766 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
5767 +                      struct au_pin *pin,
5768 +                      struct dentry *h_parent __maybe_unused,
5769 +                      void *arg __maybe_unused)
5770 +{
5771 +       struct au_cp_generic cpg = {
5772 +               .dentry = dentry,
5773 +               .bdst   = bdst,
5774 +               .bsrc   = -1,
5775 +               .len    = 0,
5776 +               .pin    = pin,
5777 +               .flags  = AuCpup_DTIME
5778 +       };
5779 +       return au_sio_cpup_simple(&cpg);
5780 +}
5781 +
5782 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5783 +{
5784 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5785 +}
5786 +
5787 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5788 +{
5789 +       int err;
5790 +       struct dentry *parent;
5791 +       struct inode *dir;
5792 +
5793 +       parent = dget_parent(dentry);
5794 +       dir = d_inode(parent);
5795 +       err = 0;
5796 +       if (au_h_iptr(dir, bdst))
5797 +               goto out;
5798 +
5799 +       di_read_unlock(parent, AuLock_IR);
5800 +       di_write_lock_parent(parent);
5801 +       /* someone else might change our inode while we were sleeping */
5802 +       if (!au_h_iptr(dir, bdst))
5803 +               err = au_cpup_dirs(dentry, bdst);
5804 +       di_downgrade_lock(parent, AuLock_IR);
5805 +
5806 +out:
5807 +       dput(parent);
5808 +       return err;
5809 +}
5810 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
5811 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
5812 +++ linux/fs/aufs/cpup.h        2015-09-24 10:47:58.248052907 +0200
5813 @@ -0,0 +1,94 @@
5814 +/*
5815 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5816 + *
5817 + * This program, aufs is free software; you can redistribute it and/or modify
5818 + * it under the terms of the GNU General Public License as published by
5819 + * the Free Software Foundation; either version 2 of the License, or
5820 + * (at your option) any later version.
5821 + *
5822 + * This program is distributed in the hope that it will be useful,
5823 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5824 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5825 + * GNU General Public License for more details.
5826 + *
5827 + * You should have received a copy of the GNU General Public License
5828 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5829 + */
5830 +
5831 +/*
5832 + * copy-up/down functions
5833 + */
5834 +
5835 +#ifndef __AUFS_CPUP_H__
5836 +#define __AUFS_CPUP_H__
5837 +
5838 +#ifdef __KERNEL__
5839 +
5840 +#include <linux/path.h>
5841 +
5842 +struct inode;
5843 +struct file;
5844 +struct au_pin;
5845 +
5846 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
5847 +void au_cpup_attr_timesizes(struct inode *inode);
5848 +void au_cpup_attr_nlink(struct inode *inode, int force);
5849 +void au_cpup_attr_changeable(struct inode *inode);
5850 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
5851 +void au_cpup_attr_all(struct inode *inode, int force);
5852 +
5853 +/* ---------------------------------------------------------------------- */
5854 +
5855 +struct au_cp_generic {
5856 +       struct dentry   *dentry;
5857 +       aufs_bindex_t   bdst, bsrc;
5858 +       loff_t          len;
5859 +       struct au_pin   *pin;
5860 +       unsigned int    flags;
5861 +};
5862 +
5863 +/* cpup flags */
5864 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
5865 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
5866 +                                                  for link(2) */
5867 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
5868 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
5869 +                                                  cpup */
5870 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
5871 +                                                  existing entry */
5872 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
5873 +                                                  the branch is marked as RO */
5874 +
5875 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
5876 +#define au_fset_cpup(flags, name) \
5877 +       do { (flags) |= AuCpup_##name; } while (0)
5878 +#define au_fclr_cpup(flags, name) \
5879 +       do { (flags) &= ~AuCpup_##name; } while (0)
5880 +
5881 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
5882 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
5883 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
5884 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
5885 +
5886 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5887 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
5888 +                        struct au_pin *pin,
5889 +                        struct dentry *h_parent, void *arg),
5890 +              void *arg);
5891 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5892 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
5893 +
5894 +/* ---------------------------------------------------------------------- */
5895 +
5896 +/* keep timestamps when copyup */
5897 +struct au_dtime {
5898 +       struct dentry *dt_dentry;
5899 +       struct path dt_h_path;
5900 +       struct timespec dt_atime, dt_mtime;
5901 +};
5902 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
5903 +                   struct path *h_path);
5904 +void au_dtime_revert(struct au_dtime *dt);
5905 +
5906 +#endif /* __KERNEL__ */
5907 +#endif /* __AUFS_CPUP_H__ */
5908 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
5909 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
5910 +++ linux/fs/aufs/dbgaufs.c     2015-09-24 10:47:58.248052907 +0200
5911 @@ -0,0 +1,432 @@
5912 +/*
5913 + * Copyright (C) 2005-2015 Junjiro R. Okajima
5914 + *
5915 + * This program, aufs is free software; you can redistribute it and/or modify
5916 + * it under the terms of the GNU General Public License as published by
5917 + * the Free Software Foundation; either version 2 of the License, or
5918 + * (at your option) any later version.
5919 + *
5920 + * This program is distributed in the hope that it will be useful,
5921 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5922 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5923 + * GNU General Public License for more details.
5924 + *
5925 + * You should have received a copy of the GNU General Public License
5926 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
5927 + */
5928 +
5929 +/*
5930 + * debugfs interface
5931 + */
5932 +
5933 +#include <linux/debugfs.h>
5934 +#include "aufs.h"
5935 +
5936 +#ifndef CONFIG_SYSFS
5937 +#error DEBUG_FS depends upon SYSFS
5938 +#endif
5939 +
5940 +static struct dentry *dbgaufs;
5941 +static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
5942 +
5943 +/* 20 is max digits length of ulong 64 */
5944 +struct dbgaufs_arg {
5945 +       int n;
5946 +       char a[20 * 4];
5947 +};
5948 +
5949 +/*
5950 + * common function for all XINO files
5951 + */
5952 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
5953 +                             struct file *file)
5954 +{
5955 +       kfree(file->private_data);
5956 +       return 0;
5957 +}
5958 +
5959 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
5960 +{
5961 +       int err;
5962 +       struct kstat st;
5963 +       struct dbgaufs_arg *p;
5964 +
5965 +       err = -ENOMEM;
5966 +       p = kmalloc(sizeof(*p), GFP_NOFS);
5967 +       if (unlikely(!p))
5968 +               goto out;
5969 +
5970 +       err = 0;
5971 +       p->n = 0;
5972 +       file->private_data = p;
5973 +       if (!xf)
5974 +               goto out;
5975 +
5976 +       err = vfs_getattr(&xf->f_path, &st);
5977 +       if (!err) {
5978 +               if (do_fcnt)
5979 +                       p->n = snprintf
5980 +                               (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
5981 +                                (long)file_count(xf), st.blocks, st.blksize,
5982 +                                (long long)st.size);
5983 +               else
5984 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
5985 +                                       st.blocks, st.blksize,
5986 +                                       (long long)st.size);
5987 +               AuDebugOn(p->n >= sizeof(p->a));
5988 +       } else {
5989 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
5990 +               err = 0;
5991 +       }
5992 +
5993 +out:
5994 +       return err;
5995 +
5996 +}
5997 +
5998 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
5999 +                              size_t count, loff_t *ppos)
6000 +{
6001 +       struct dbgaufs_arg *p;
6002 +
6003 +       p = file->private_data;
6004 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6005 +}
6006 +
6007 +/* ---------------------------------------------------------------------- */
6008 +
6009 +struct dbgaufs_plink_arg {
6010 +       int n;
6011 +       char a[];
6012 +};
6013 +
6014 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6015 +                                struct file *file)
6016 +{
6017 +       free_page((unsigned long)file->private_data);
6018 +       return 0;
6019 +}
6020 +
6021 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6022 +{
6023 +       int err, i, limit;
6024 +       unsigned long n, sum;
6025 +       struct dbgaufs_plink_arg *p;
6026 +       struct au_sbinfo *sbinfo;
6027 +       struct super_block *sb;
6028 +       struct au_sphlhead *sphl;
6029 +
6030 +       err = -ENOMEM;
6031 +       p = (void *)get_zeroed_page(GFP_NOFS);
6032 +       if (unlikely(!p))
6033 +               goto out;
6034 +
6035 +       err = -EFBIG;
6036 +       sbinfo = inode->i_private;
6037 +       sb = sbinfo->si_sb;
6038 +       si_noflush_read_lock(sb);
6039 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6040 +               limit = PAGE_SIZE - sizeof(p->n);
6041 +
6042 +               /* the number of buckets */
6043 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6044 +               p->n += n;
6045 +               limit -= n;
6046 +
6047 +               sum = 0;
6048 +               for (i = 0, sphl = sbinfo->si_plink;
6049 +                    i < AuPlink_NHASH;
6050 +                    i++, sphl++) {
6051 +                       n = au_sphl_count(sphl);
6052 +                       sum += n;
6053 +
6054 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6055 +                       p->n += n;
6056 +                       limit -= n;
6057 +                       if (unlikely(limit <= 0))
6058 +                               goto out_free;
6059 +               }
6060 +               p->a[p->n - 1] = '\n';
6061 +
6062 +               /* the sum of plinks */
6063 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6064 +               p->n += n;
6065 +               limit -= n;
6066 +               if (unlikely(limit <= 0))
6067 +                       goto out_free;
6068 +       } else {
6069 +#define str "1\n0\n0\n"
6070 +               p->n = sizeof(str) - 1;
6071 +               strcpy(p->a, str);
6072 +#undef str
6073 +       }
6074 +       si_read_unlock(sb);
6075 +
6076 +       err = 0;
6077 +       file->private_data = p;
6078 +       goto out; /* success */
6079 +
6080 +out_free:
6081 +       free_page((unsigned long)p);
6082 +out:
6083 +       return err;
6084 +}
6085 +
6086 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6087 +                                 size_t count, loff_t *ppos)
6088 +{
6089 +       struct dbgaufs_plink_arg *p;
6090 +
6091 +       p = file->private_data;
6092 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6093 +}
6094 +
6095 +static const struct file_operations dbgaufs_plink_fop = {
6096 +       .owner          = THIS_MODULE,
6097 +       .open           = dbgaufs_plink_open,
6098 +       .release        = dbgaufs_plink_release,
6099 +       .read           = dbgaufs_plink_read
6100 +};
6101 +
6102 +/* ---------------------------------------------------------------------- */
6103 +
6104 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6105 +{
6106 +       int err;
6107 +       struct au_sbinfo *sbinfo;
6108 +       struct super_block *sb;
6109 +
6110 +       sbinfo = inode->i_private;
6111 +       sb = sbinfo->si_sb;
6112 +       si_noflush_read_lock(sb);
6113 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6114 +       si_read_unlock(sb);
6115 +       return err;
6116 +}
6117 +
6118 +static const struct file_operations dbgaufs_xib_fop = {
6119 +       .owner          = THIS_MODULE,
6120 +       .open           = dbgaufs_xib_open,
6121 +       .release        = dbgaufs_xi_release,
6122 +       .read           = dbgaufs_xi_read
6123 +};
6124 +
6125 +/* ---------------------------------------------------------------------- */
6126 +
6127 +#define DbgaufsXi_PREFIX "xi"
6128 +
6129 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6130 +{
6131 +       int err;
6132 +       long l;
6133 +       struct au_sbinfo *sbinfo;
6134 +       struct super_block *sb;
6135 +       struct file *xf;
6136 +       struct qstr *name;
6137 +
6138 +       err = -ENOENT;
6139 +       xf = NULL;
6140 +       name = &file->f_path.dentry->d_name;
6141 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6142 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6143 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6144 +               goto out;
6145 +       err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6146 +       if (unlikely(err))
6147 +               goto out;
6148 +
6149 +       sbinfo = inode->i_private;
6150 +       sb = sbinfo->si_sb;
6151 +       si_noflush_read_lock(sb);
6152 +       if (l <= au_sbend(sb)) {
6153 +               xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6154 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6155 +       } else
6156 +               err = -ENOENT;
6157 +       si_read_unlock(sb);
6158 +
6159 +out:
6160 +       return err;
6161 +}
6162 +
6163 +static const struct file_operations dbgaufs_xino_fop = {
6164 +       .owner          = THIS_MODULE,
6165 +       .open           = dbgaufs_xino_open,
6166 +       .release        = dbgaufs_xi_release,
6167 +       .read           = dbgaufs_xi_read
6168 +};
6169 +
6170 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6171 +{
6172 +       aufs_bindex_t bend;
6173 +       struct au_branch *br;
6174 +       struct au_xino_file *xi;
6175 +
6176 +       if (!au_sbi(sb)->si_dbgaufs)
6177 +               return;
6178 +
6179 +       bend = au_sbend(sb);
6180 +       for (; bindex <= bend; bindex++) {
6181 +               br = au_sbr(sb, bindex);
6182 +               xi = &br->br_xino;
6183 +               debugfs_remove(xi->xi_dbgaufs);
6184 +               xi->xi_dbgaufs = NULL;
6185 +       }
6186 +}
6187 +
6188 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6189 +{
6190 +       struct au_sbinfo *sbinfo;
6191 +       struct dentry *parent;
6192 +       struct au_branch *br;
6193 +       struct au_xino_file *xi;
6194 +       aufs_bindex_t bend;
6195 +       char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6196 +
6197 +       sbinfo = au_sbi(sb);
6198 +       parent = sbinfo->si_dbgaufs;
6199 +       if (!parent)
6200 +               return;
6201 +
6202 +       bend = au_sbend(sb);
6203 +       for (; bindex <= bend; bindex++) {
6204 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6205 +               br = au_sbr(sb, bindex);
6206 +               xi = &br->br_xino;
6207 +               AuDebugOn(xi->xi_dbgaufs);
6208 +               xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6209 +                                                    sbinfo, &dbgaufs_xino_fop);
6210 +               /* ignore an error */
6211 +               if (unlikely(!xi->xi_dbgaufs))
6212 +                       AuWarn1("failed %s under debugfs\n", name);
6213 +       }
6214 +}
6215 +
6216 +/* ---------------------------------------------------------------------- */
6217 +
6218 +#ifdef CONFIG_AUFS_EXPORT
6219 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6220 +{
6221 +       int err;
6222 +       struct au_sbinfo *sbinfo;
6223 +       struct super_block *sb;
6224 +
6225 +       sbinfo = inode->i_private;
6226 +       sb = sbinfo->si_sb;
6227 +       si_noflush_read_lock(sb);
6228 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6229 +       si_read_unlock(sb);
6230 +       return err;
6231 +}
6232 +
6233 +static const struct file_operations dbgaufs_xigen_fop = {
6234 +       .owner          = THIS_MODULE,
6235 +       .open           = dbgaufs_xigen_open,
6236 +       .release        = dbgaufs_xi_release,
6237 +       .read           = dbgaufs_xi_read
6238 +};
6239 +
6240 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6241 +{
6242 +       int err;
6243 +
6244 +       /*
6245 +        * This function is a dynamic '__init' function actually,
6246 +        * so the tiny check for si_rwsem is unnecessary.
6247 +        */
6248 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6249 +
6250 +       err = -EIO;
6251 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6252 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6253 +                &dbgaufs_xigen_fop);
6254 +       if (sbinfo->si_dbgaufs_xigen)
6255 +               err = 0;
6256 +
6257 +       return err;
6258 +}
6259 +#else
6260 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6261 +{
6262 +       return 0;
6263 +}
6264 +#endif /* CONFIG_AUFS_EXPORT */
6265 +
6266 +/* ---------------------------------------------------------------------- */
6267 +
6268 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6269 +{
6270 +       /*
6271 +        * This function is a dynamic '__fin' function actually,
6272 +        * so the tiny check for si_rwsem is unnecessary.
6273 +        */
6274 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6275 +
6276 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6277 +       sbinfo->si_dbgaufs = NULL;
6278 +       kobject_put(&sbinfo->si_kobj);
6279 +}
6280 +
6281 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6282 +{
6283 +       int err;
6284 +       char name[SysaufsSiNameLen];
6285 +
6286 +       /*
6287 +        * This function is a dynamic '__init' function actually,
6288 +        * so the tiny check for si_rwsem is unnecessary.
6289 +        */
6290 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6291 +
6292 +       err = -ENOENT;
6293 +       if (!dbgaufs) {
6294 +               AuErr1("/debug/aufs is uninitialized\n");
6295 +               goto out;
6296 +       }
6297 +
6298 +       err = -EIO;
6299 +       sysaufs_name(sbinfo, name);
6300 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6301 +       if (unlikely(!sbinfo->si_dbgaufs))
6302 +               goto out;
6303 +       kobject_get(&sbinfo->si_kobj);
6304 +
6305 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6306 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6307 +                &dbgaufs_xib_fop);
6308 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6309 +               goto out_dir;
6310 +
6311 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6312 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6313 +                &dbgaufs_plink_fop);
6314 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6315 +               goto out_dir;
6316 +
6317 +       err = dbgaufs_xigen_init(sbinfo);
6318 +       if (!err)
6319 +               goto out; /* success */
6320 +
6321 +out_dir:
6322 +       dbgaufs_si_fin(sbinfo);
6323 +out:
6324 +       return err;
6325 +}
6326 +
6327 +/* ---------------------------------------------------------------------- */
6328 +
6329 +void dbgaufs_fin(void)
6330 +{
6331 +       debugfs_remove(dbgaufs);
6332 +}
6333 +
6334 +int __init dbgaufs_init(void)
6335 +{
6336 +       int err;
6337 +
6338 +       err = -EIO;
6339 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6340 +       if (dbgaufs)
6341 +               err = 0;
6342 +       return err;
6343 +}
6344 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6345 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6346 +++ linux/fs/aufs/dbgaufs.h     2015-09-24 10:47:58.248052907 +0200
6347 @@ -0,0 +1,48 @@
6348 +/*
6349 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6350 + *
6351 + * This program, aufs is free software; you can redistribute it and/or modify
6352 + * it under the terms of the GNU General Public License as published by
6353 + * the Free Software Foundation; either version 2 of the License, or
6354 + * (at your option) any later version.
6355 + *
6356 + * This program is distributed in the hope that it will be useful,
6357 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6358 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6359 + * GNU General Public License for more details.
6360 + *
6361 + * You should have received a copy of the GNU General Public License
6362 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6363 + */
6364 +
6365 +/*
6366 + * debugfs interface
6367 + */
6368 +
6369 +#ifndef __DBGAUFS_H__
6370 +#define __DBGAUFS_H__
6371 +
6372 +#ifdef __KERNEL__
6373 +
6374 +struct super_block;
6375 +struct au_sbinfo;
6376 +
6377 +#ifdef CONFIG_DEBUG_FS
6378 +/* dbgaufs.c */
6379 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6380 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6381 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6382 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6383 +void dbgaufs_fin(void);
6384 +int __init dbgaufs_init(void);
6385 +#else
6386 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6387 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6388 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6389 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6390 +AuStubVoid(dbgaufs_fin, void)
6391 +AuStubInt0(__init dbgaufs_init, void)
6392 +#endif /* CONFIG_DEBUG_FS */
6393 +
6394 +#endif /* __KERNEL__ */
6395 +#endif /* __DBGAUFS_H__ */
6396 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6397 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6398 +++ linux/fs/aufs/dcsub.c       2015-09-24 10:47:58.248052907 +0200
6399 @@ -0,0 +1,224 @@
6400 +/*
6401 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6402 + *
6403 + * This program, aufs is free software; you can redistribute it and/or modify
6404 + * it under the terms of the GNU General Public License as published by
6405 + * the Free Software Foundation; either version 2 of the License, or
6406 + * (at your option) any later version.
6407 + *
6408 + * This program is distributed in the hope that it will be useful,
6409 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6410 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6411 + * GNU General Public License for more details.
6412 + *
6413 + * You should have received a copy of the GNU General Public License
6414 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6415 + */
6416 +
6417 +/*
6418 + * sub-routines for dentry cache
6419 + */
6420 +
6421 +#include "aufs.h"
6422 +
6423 +static void au_dpage_free(struct au_dpage *dpage)
6424 +{
6425 +       int i;
6426 +       struct dentry **p;
6427 +
6428 +       p = dpage->dentries;
6429 +       for (i = 0; i < dpage->ndentry; i++)
6430 +               dput(*p++);
6431 +       free_page((unsigned long)dpage->dentries);
6432 +}
6433 +
6434 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6435 +{
6436 +       int err;
6437 +       void *p;
6438 +
6439 +       err = -ENOMEM;
6440 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6441 +       if (unlikely(!dpages->dpages))
6442 +               goto out;
6443 +
6444 +       p = (void *)__get_free_page(gfp);
6445 +       if (unlikely(!p))
6446 +               goto out_dpages;
6447 +
6448 +       dpages->dpages[0].ndentry = 0;
6449 +       dpages->dpages[0].dentries = p;
6450 +       dpages->ndpage = 1;
6451 +       return 0; /* success */
6452 +
6453 +out_dpages:
6454 +       kfree(dpages->dpages);
6455 +out:
6456 +       return err;
6457 +}
6458 +
6459 +void au_dpages_free(struct au_dcsub_pages *dpages)
6460 +{
6461 +       int i;
6462 +       struct au_dpage *p;
6463 +
6464 +       p = dpages->dpages;
6465 +       for (i = 0; i < dpages->ndpage; i++)
6466 +               au_dpage_free(p++);
6467 +       kfree(dpages->dpages);
6468 +}
6469 +
6470 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6471 +                           struct dentry *dentry, gfp_t gfp)
6472 +{
6473 +       int err, sz;
6474 +       struct au_dpage *dpage;
6475 +       void *p;
6476 +
6477 +       dpage = dpages->dpages + dpages->ndpage - 1;
6478 +       sz = PAGE_SIZE / sizeof(dentry);
6479 +       if (unlikely(dpage->ndentry >= sz)) {
6480 +               AuLabel(new dpage);
6481 +               err = -ENOMEM;
6482 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6483 +               p = au_kzrealloc(dpages->dpages, sz,
6484 +                                sz + sizeof(*dpages->dpages), gfp);
6485 +               if (unlikely(!p))
6486 +                       goto out;
6487 +
6488 +               dpages->dpages = p;
6489 +               dpage = dpages->dpages + dpages->ndpage;
6490 +               p = (void *)__get_free_page(gfp);
6491 +               if (unlikely(!p))
6492 +                       goto out;
6493 +
6494 +               dpage->ndentry = 0;
6495 +               dpage->dentries = p;
6496 +               dpages->ndpage++;
6497 +       }
6498 +
6499 +       AuDebugOn(au_dcount(dentry) <= 0);
6500 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6501 +       return 0; /* success */
6502 +
6503 +out:
6504 +       return err;
6505 +}
6506 +
6507 +/* todo: BAD approach */
6508 +/* copied from linux/fs/dcache.c */
6509 +enum d_walk_ret {
6510 +       D_WALK_CONTINUE,
6511 +       D_WALK_QUIT,
6512 +       D_WALK_NORETRY,
6513 +       D_WALK_SKIP,
6514 +};
6515 +
6516 +extern void d_walk(struct dentry *parent, void *data,
6517 +                  enum d_walk_ret (*enter)(void *, struct dentry *),
6518 +                  void (*finish)(void *));
6519 +
6520 +struct ac_dpages_arg {
6521 +       int err;
6522 +       struct au_dcsub_pages *dpages;
6523 +       struct super_block *sb;
6524 +       au_dpages_test test;
6525 +       void *arg;
6526 +};
6527 +
6528 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6529 +{
6530 +       enum d_walk_ret ret;
6531 +       struct ac_dpages_arg *arg = _arg;
6532 +
6533 +       ret = D_WALK_CONTINUE;
6534 +       if (dentry->d_sb == arg->sb
6535 +           && !IS_ROOT(dentry)
6536 +           && au_dcount(dentry) > 0
6537 +           && au_di(dentry)
6538 +           && (!arg->test || arg->test(dentry, arg->arg))) {
6539 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6540 +               if (unlikely(arg->err))
6541 +                       ret = D_WALK_QUIT;
6542 +       }
6543 +
6544 +       return ret;
6545 +}
6546 +
6547 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6548 +                  au_dpages_test test, void *arg)
6549 +{
6550 +       struct ac_dpages_arg args = {
6551 +               .err    = 0,
6552 +               .dpages = dpages,
6553 +               .sb     = root->d_sb,
6554 +               .test   = test,
6555 +               .arg    = arg
6556 +       };
6557 +
6558 +       d_walk(root, &args, au_call_dpages_append, NULL);
6559 +
6560 +       return args.err;
6561 +}
6562 +
6563 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6564 +                      int do_include, au_dpages_test test, void *arg)
6565 +{
6566 +       int err;
6567 +
6568 +       err = 0;
6569 +       write_seqlock(&rename_lock);
6570 +       spin_lock(&dentry->d_lock);
6571 +       if (do_include
6572 +           && au_dcount(dentry) > 0
6573 +           && (!test || test(dentry, arg)))
6574 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6575 +       spin_unlock(&dentry->d_lock);
6576 +       if (unlikely(err))
6577 +               goto out;
6578 +
6579 +       /*
6580 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
6581 +        * mount
6582 +        */
6583 +       while (!IS_ROOT(dentry)) {
6584 +               dentry = dentry->d_parent; /* rename_lock is locked */
6585 +               spin_lock(&dentry->d_lock);
6586 +               if (au_dcount(dentry) > 0
6587 +                   && (!test || test(dentry, arg)))
6588 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
6589 +               spin_unlock(&dentry->d_lock);
6590 +               if (unlikely(err))
6591 +                       break;
6592 +       }
6593 +
6594 +out:
6595 +       write_sequnlock(&rename_lock);
6596 +       return err;
6597 +}
6598 +
6599 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6600 +{
6601 +       return au_di(dentry) && dentry->d_sb == arg;
6602 +}
6603 +
6604 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6605 +                           struct dentry *dentry, int do_include)
6606 +{
6607 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
6608 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
6609 +}
6610 +
6611 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
6612 +{
6613 +       struct path path[2] = {
6614 +               {
6615 +                       .dentry = d1
6616 +               },
6617 +               {
6618 +                       .dentry = d2
6619 +               }
6620 +       };
6621 +
6622 +       return path_is_under(path + 0, path + 1);
6623 +}
6624 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6625 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
6626 +++ linux/fs/aufs/dcsub.h       2015-09-24 10:47:58.251386326 +0200
6627 @@ -0,0 +1,136 @@
6628 +/*
6629 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6630 + *
6631 + * This program, aufs is free software; you can redistribute it and/or modify
6632 + * it under the terms of the GNU General Public License as published by
6633 + * the Free Software Foundation; either version 2 of the License, or
6634 + * (at your option) any later version.
6635 + *
6636 + * This program is distributed in the hope that it will be useful,
6637 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6638 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6639 + * GNU General Public License for more details.
6640 + *
6641 + * You should have received a copy of the GNU General Public License
6642 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6643 + */
6644 +
6645 +/*
6646 + * sub-routines for dentry cache
6647 + */
6648 +
6649 +#ifndef __AUFS_DCSUB_H__
6650 +#define __AUFS_DCSUB_H__
6651 +
6652 +#ifdef __KERNEL__
6653 +
6654 +#include <linux/dcache.h>
6655 +#include <linux/fs.h>
6656 +
6657 +struct au_dpage {
6658 +       int ndentry;
6659 +       struct dentry **dentries;
6660 +};
6661 +
6662 +struct au_dcsub_pages {
6663 +       int ndpage;
6664 +       struct au_dpage *dpages;
6665 +};
6666 +
6667 +/* ---------------------------------------------------------------------- */
6668 +
6669 +/* dcsub.c */
6670 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6671 +void au_dpages_free(struct au_dcsub_pages *dpages);
6672 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6673 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6674 +                  au_dpages_test test, void *arg);
6675 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6676 +                      int do_include, au_dpages_test test, void *arg);
6677 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6678 +                           struct dentry *dentry, int do_include);
6679 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
6680 +
6681 +/* ---------------------------------------------------------------------- */
6682 +
6683 +/*
6684 + * todo: in linux-3.13, several similar (but faster) helpers are added to
6685 + * include/linux/dcache.h. Try them (in the future).
6686 + */
6687 +
6688 +static inline int au_d_hashed_positive(struct dentry *d)
6689 +{
6690 +       int err;
6691 +       struct inode *inode = d_inode(d);
6692 +
6693 +       err = 0;
6694 +       if (unlikely(d_unhashed(d)
6695 +                    || d_is_negative(d)
6696 +                    || !inode->i_nlink))
6697 +               err = -ENOENT;
6698 +       return err;
6699 +}
6700 +
6701 +static inline int au_d_linkable(struct dentry *d)
6702 +{
6703 +       int err;
6704 +       struct inode *inode = d_inode(d);
6705 +
6706 +       err = au_d_hashed_positive(d);
6707 +       if (err
6708 +           && d_is_positive(d)
6709 +           && (inode->i_state & I_LINKABLE))
6710 +               err = 0;
6711 +       return err;
6712 +}
6713 +
6714 +static inline int au_d_alive(struct dentry *d)
6715 +{
6716 +       int err;
6717 +       struct inode *inode;
6718 +
6719 +       err = 0;
6720 +       if (!IS_ROOT(d))
6721 +               err = au_d_hashed_positive(d);
6722 +       else {
6723 +               inode = d_inode(d);
6724 +               if (unlikely(d_unlinked(d)
6725 +                            || d_is_negative(d)
6726 +                            || !inode->i_nlink))
6727 +                       err = -ENOENT;
6728 +       }
6729 +       return err;
6730 +}
6731 +
6732 +static inline int au_alive_dir(struct dentry *d)
6733 +{
6734 +       int err;
6735 +
6736 +       err = au_d_alive(d);
6737 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
6738 +               err = -ENOENT;
6739 +       return err;
6740 +}
6741 +
6742 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
6743 +{
6744 +       return a->len == b->len
6745 +               && !memcmp(a->name, b->name, a->len);
6746 +}
6747 +
6748 +/*
6749 + * by the commit
6750 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6751 + *                     taking d_lock
6752 + * the type of d_lockref.count became int, but the inlined function d_count()
6753 + * still returns unsigned int.
6754 + * I don't know why. Maybe it is for every d_count() users?
6755 + * Anyway au_dcount() lives on.
6756 + */
6757 +static inline int au_dcount(struct dentry *d)
6758 +{
6759 +       return (int)d_count(d);
6760 +}
6761 +
6762 +#endif /* __KERNEL__ */
6763 +#endif /* __AUFS_DCSUB_H__ */
6764 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6765 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
6766 +++ linux/fs/aufs/debug.c       2015-09-24 10:47:58.251386326 +0200
6767 @@ -0,0 +1,440 @@
6768 +/*
6769 + * Copyright (C) 2005-2015 Junjiro R. Okajima
6770 + *
6771 + * This program, aufs is free software; you can redistribute it and/or modify
6772 + * it under the terms of the GNU General Public License as published by
6773 + * the Free Software Foundation; either version 2 of the License, or
6774 + * (at your option) any later version.
6775 + *
6776 + * This program is distributed in the hope that it will be useful,
6777 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6778 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6779 + * GNU General Public License for more details.
6780 + *
6781 + * You should have received a copy of the GNU General Public License
6782 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6783 + */
6784 +
6785 +/*
6786 + * debug print functions
6787 + */
6788 +
6789 +#include "aufs.h"
6790 +
6791 +/* Returns 0, or -errno.  arg is in kp->arg. */
6792 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
6793 +{
6794 +       int err, n;
6795 +
6796 +       err = kstrtoint(val, 0, &n);
6797 +       if (!err) {
6798 +               if (n > 0)
6799 +                       au_debug_on();
6800 +               else
6801 +                       au_debug_off();
6802 +       }
6803 +       return err;
6804 +}
6805 +
6806 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
6807 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
6808 +{
6809 +       atomic_t *a;
6810 +
6811 +       a = kp->arg;
6812 +       return sprintf(buffer, "%d", atomic_read(a));
6813 +}
6814 +
6815 +static struct kernel_param_ops param_ops_atomic_t = {
6816 +       .set = param_atomic_t_set,
6817 +       .get = param_atomic_t_get
6818 +       /* void (*free)(void *arg) */
6819 +};
6820 +
6821 +atomic_t aufs_debug = ATOMIC_INIT(0);
6822 +MODULE_PARM_DESC(debug, "debug print");
6823 +module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
6824 +
6825 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
6826 +char *au_plevel = KERN_DEBUG;
6827 +#define dpri(fmt, ...) do {                                    \
6828 +       if ((au_plevel                                          \
6829 +            && strcmp(au_plevel, KERN_DEBUG))                  \
6830 +           || au_debug_test())                                 \
6831 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
6832 +} while (0)
6833 +
6834 +/* ---------------------------------------------------------------------- */
6835 +
6836 +void au_dpri_whlist(struct au_nhash *whlist)
6837 +{
6838 +       unsigned long ul, n;
6839 +       struct hlist_head *head;
6840 +       struct au_vdir_wh *pos;
6841 +
6842 +       n = whlist->nh_num;
6843 +       head = whlist->nh_head;
6844 +       for (ul = 0; ul < n; ul++) {
6845 +               hlist_for_each_entry(pos, head, wh_hash)
6846 +                       dpri("b%d, %.*s, %d\n",
6847 +                            pos->wh_bindex,
6848 +                            pos->wh_str.len, pos->wh_str.name,
6849 +                            pos->wh_str.len);
6850 +               head++;
6851 +       }
6852 +}
6853 +
6854 +void au_dpri_vdir(struct au_vdir *vdir)
6855 +{
6856 +       unsigned long ul;
6857 +       union au_vdir_deblk_p p;
6858 +       unsigned char *o;
6859 +
6860 +       if (!vdir || IS_ERR(vdir)) {
6861 +               dpri("err %ld\n", PTR_ERR(vdir));
6862 +               return;
6863 +       }
6864 +
6865 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
6866 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
6867 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
6868 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
6869 +               p.deblk = vdir->vd_deblk[ul];
6870 +               o = p.deblk;
6871 +               dpri("[%lu]: %p\n", ul, o);
6872 +       }
6873 +}
6874 +
6875 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
6876 +                       struct dentry *wh)
6877 +{
6878 +       char *n = NULL;
6879 +       int l = 0;
6880 +
6881 +       if (!inode || IS_ERR(inode)) {
6882 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
6883 +               return -1;
6884 +       }
6885 +
6886 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
6887 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
6888 +                    && sizeof(inode->i_blocks) != sizeof(u64));
6889 +       if (wh) {
6890 +               n = (void *)wh->d_name.name;
6891 +               l = wh->d_name.len;
6892 +       }
6893 +
6894 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
6895 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
6896 +            bindex, inode,
6897 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
6898 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
6899 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
6900 +            hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
6901 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
6902 +            inode->i_state, inode->i_flags, inode->i_version,
6903 +            inode->i_generation,
6904 +            l ? ", wh " : "", l, n);
6905 +       return 0;
6906 +}
6907 +
6908 +void au_dpri_inode(struct inode *inode)
6909 +{
6910 +       struct au_iinfo *iinfo;
6911 +       aufs_bindex_t bindex;
6912 +       int err, hn;
6913 +
6914 +       err = do_pri_inode(-1, inode, -1, NULL);
6915 +       if (err || !au_test_aufs(inode->i_sb))
6916 +               return;
6917 +
6918 +       iinfo = au_ii(inode);
6919 +       if (!iinfo)
6920 +               return;
6921 +       dpri("i-1: bstart %d, bend %d, gen %d\n",
6922 +            iinfo->ii_bstart, iinfo->ii_bend, au_iigen(inode, NULL));
6923 +       if (iinfo->ii_bstart < 0)
6924 +               return;
6925 +       hn = 0;
6926 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend; bindex++) {
6927 +               hn = !!au_hn(iinfo->ii_hinode + bindex);
6928 +               do_pri_inode(bindex, iinfo->ii_hinode[0 + bindex].hi_inode, hn,
6929 +                            iinfo->ii_hinode[0 + bindex].hi_whdentry);
6930 +       }
6931 +}
6932 +
6933 +void au_dpri_dalias(struct inode *inode)
6934 +{
6935 +       struct dentry *d;
6936 +
6937 +       spin_lock(&inode->i_lock);
6938 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
6939 +               au_dpri_dentry(d);
6940 +       spin_unlock(&inode->i_lock);
6941 +}
6942 +
6943 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
6944 +{
6945 +       struct dentry *wh = NULL;
6946 +       int hn;
6947 +       struct au_iinfo *iinfo;
6948 +
6949 +       if (!dentry || IS_ERR(dentry)) {
6950 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
6951 +               return -1;
6952 +       }
6953 +       /* do not call dget_parent() here */
6954 +       /* note: access d_xxx without d_lock */
6955 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
6956 +            bindex, dentry, dentry,
6957 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
6958 +            au_dcount(dentry), dentry->d_flags,
6959 +            d_unhashed(dentry) ? "un" : "");
6960 +       hn = -1;
6961 +       if (bindex >= 0
6962 +           && d_is_positive(dentry)
6963 +           && au_test_aufs(dentry->d_sb)) {
6964 +               iinfo = au_ii(d_inode(dentry));
6965 +               if (iinfo) {
6966 +                       hn = !!au_hn(iinfo->ii_hinode + bindex);
6967 +                       wh = iinfo->ii_hinode[0 + bindex].hi_whdentry;
6968 +               }
6969 +       }
6970 +       do_pri_inode(bindex, d_inode(dentry), hn, wh);
6971 +       return 0;
6972 +}
6973 +
6974 +void au_dpri_dentry(struct dentry *dentry)
6975 +{
6976 +       struct au_dinfo *dinfo;
6977 +       aufs_bindex_t bindex;
6978 +       int err;
6979 +       struct au_hdentry *hdp;
6980 +
6981 +       err = do_pri_dentry(-1, dentry);
6982 +       if (err || !au_test_aufs(dentry->d_sb))
6983 +               return;
6984 +
6985 +       dinfo = au_di(dentry);
6986 +       if (!dinfo)
6987 +               return;
6988 +       dpri("d-1: bstart %d, bend %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
6989 +            dinfo->di_bstart, dinfo->di_bend,
6990 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
6991 +            dinfo->di_tmpfile);
6992 +       if (dinfo->di_bstart < 0)
6993 +               return;
6994 +       hdp = dinfo->di_hdentry;
6995 +       for (bindex = dinfo->di_bstart; bindex <= dinfo->di_bend; bindex++)
6996 +               do_pri_dentry(bindex, hdp[0 + bindex].hd_dentry);
6997 +}
6998 +
6999 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7000 +{
7001 +       char a[32];
7002 +
7003 +       if (!file || IS_ERR(file)) {
7004 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7005 +               return -1;
7006 +       }
7007 +       a[0] = 0;
7008 +       if (bindex < 0
7009 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7010 +           && au_test_aufs(file->f_path.dentry->d_sb)
7011 +           && au_fi(file))
7012 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7013 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7014 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7015 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7016 +            file->f_version, file->f_pos, a);
7017 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7018 +               do_pri_dentry(bindex, file->f_path.dentry);
7019 +       return 0;
7020 +}
7021 +
7022 +void au_dpri_file(struct file *file)
7023 +{
7024 +       struct au_finfo *finfo;
7025 +       struct au_fidir *fidir;
7026 +       struct au_hfile *hfile;
7027 +       aufs_bindex_t bindex;
7028 +       int err;
7029 +
7030 +       err = do_pri_file(-1, file);
7031 +       if (err
7032 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7033 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7034 +               return;
7035 +
7036 +       finfo = au_fi(file);
7037 +       if (!finfo)
7038 +               return;
7039 +       if (finfo->fi_btop < 0)
7040 +               return;
7041 +       fidir = finfo->fi_hdir;
7042 +       if (!fidir)
7043 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7044 +       else
7045 +               for (bindex = finfo->fi_btop;
7046 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7047 +                    bindex++) {
7048 +                       hfile = fidir->fd_hfile + bindex;
7049 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7050 +               }
7051 +}
7052 +
7053 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7054 +{
7055 +       struct vfsmount *mnt;
7056 +       struct super_block *sb;
7057 +
7058 +       if (!br || IS_ERR(br))
7059 +               goto out;
7060 +       mnt = au_br_mnt(br);
7061 +       if (!mnt || IS_ERR(mnt))
7062 +               goto out;
7063 +       sb = mnt->mnt_sb;
7064 +       if (!sb || IS_ERR(sb))
7065 +               goto out;
7066 +
7067 +       dpri("s%d: {perm 0x%x, id %d, cnt %d, wbr %p}, "
7068 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7069 +            "xino %d\n",
7070 +            bindex, br->br_perm, br->br_id, atomic_read(&br->br_count),
7071 +            br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7072 +            sb->s_flags, sb->s_count,
7073 +            atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7074 +       return 0;
7075 +
7076 +out:
7077 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7078 +       return -1;
7079 +}
7080 +
7081 +void au_dpri_sb(struct super_block *sb)
7082 +{
7083 +       struct au_sbinfo *sbinfo;
7084 +       aufs_bindex_t bindex;
7085 +       int err;
7086 +       /* to reuduce stack size */
7087 +       struct {
7088 +               struct vfsmount mnt;
7089 +               struct au_branch fake;
7090 +       } *a;
7091 +
7092 +       /* this function can be called from magic sysrq */
7093 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7094 +       if (unlikely(!a)) {
7095 +               dpri("no memory\n");
7096 +               return;
7097 +       }
7098 +
7099 +       a->mnt.mnt_sb = sb;
7100 +       a->fake.br_perm = 0;
7101 +       a->fake.br_path.mnt = &a->mnt;
7102 +       a->fake.br_xino.xi_file = NULL;
7103 +       atomic_set(&a->fake.br_count, 0);
7104 +       smp_mb(); /* atomic_set */
7105 +       err = do_pri_br(-1, &a->fake);
7106 +       kfree(a);
7107 +       dpri("dev 0x%x\n", sb->s_dev);
7108 +       if (err || !au_test_aufs(sb))
7109 +               return;
7110 +
7111 +       sbinfo = au_sbi(sb);
7112 +       if (!sbinfo)
7113 +               return;
7114 +       dpri("nw %d, gen %u, kobj %d\n",
7115 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7116 +            atomic_read(&sbinfo->si_kobj.kref.refcount));
7117 +       for (bindex = 0; bindex <= sbinfo->si_bend; bindex++)
7118 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7119 +}
7120 +
7121 +/* ---------------------------------------------------------------------- */
7122 +
7123 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7124 +{
7125 +       struct inode *h_inode, *inode = d_inode(dentry);
7126 +       struct dentry *h_dentry;
7127 +       aufs_bindex_t bindex, bend, bi;
7128 +
7129 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7130 +               return;
7131 +
7132 +       bend = au_dbend(dentry);
7133 +       bi = au_ibend(inode);
7134 +       if (bi < bend)
7135 +               bend = bi;
7136 +       bindex = au_dbstart(dentry);
7137 +       bi = au_ibstart(inode);
7138 +       if (bi > bindex)
7139 +               bindex = bi;
7140 +
7141 +       for (; bindex <= bend; bindex++) {
7142 +               h_dentry = au_h_dptr(dentry, bindex);
7143 +               if (!h_dentry)
7144 +                       continue;
7145 +               h_inode = au_h_iptr(inode, bindex);
7146 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7147 +                       au_debug_on();
7148 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7149 +                       AuDbgDentry(dentry);
7150 +                       AuDbgInode(inode);
7151 +                       au_debug_off();
7152 +                       BUG();
7153 +               }
7154 +       }
7155 +}
7156 +
7157 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7158 +{
7159 +       int err, i, j;
7160 +       struct au_dcsub_pages dpages;
7161 +       struct au_dpage *dpage;
7162 +       struct dentry **dentries;
7163 +
7164 +       err = au_dpages_init(&dpages, GFP_NOFS);
7165 +       AuDebugOn(err);
7166 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7167 +       AuDebugOn(err);
7168 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7169 +               dpage = dpages.dpages + i;
7170 +               dentries = dpage->dentries;
7171 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7172 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7173 +       }
7174 +       au_dpages_free(&dpages);
7175 +}
7176 +
7177 +void au_dbg_verify_kthread(void)
7178 +{
7179 +       if (au_wkq_test()) {
7180 +               au_dbg_blocked();
7181 +               /*
7182 +                * It may be recursive, but udba=notify between two aufs mounts,
7183 +                * where a single ro branch is shared, is not a problem.
7184 +                */
7185 +               /* WARN_ON(1); */
7186 +       }
7187 +}
7188 +
7189 +/* ---------------------------------------------------------------------- */
7190 +
7191 +int __init au_debug_init(void)
7192 +{
7193 +       aufs_bindex_t bindex;
7194 +       struct au_vdir_destr destr;
7195 +
7196 +       bindex = -1;
7197 +       AuDebugOn(bindex >= 0);
7198 +
7199 +       destr.len = -1;
7200 +       AuDebugOn(destr.len < NAME_MAX);
7201 +
7202 +#ifdef CONFIG_4KSTACKS
7203 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7204 +#endif
7205 +
7206 +       return 0;
7207 +}
7208 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7209 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7210 +++ linux/fs/aufs/debug.h       2015-09-24 10:47:58.251386326 +0200
7211 @@ -0,0 +1,225 @@
7212 +/*
7213 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7214 + *
7215 + * This program, aufs is free software; you can redistribute it and/or modify
7216 + * it under the terms of the GNU General Public License as published by
7217 + * the Free Software Foundation; either version 2 of the License, or
7218 + * (at your option) any later version.
7219 + *
7220 + * This program is distributed in the hope that it will be useful,
7221 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7222 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7223 + * GNU General Public License for more details.
7224 + *
7225 + * You should have received a copy of the GNU General Public License
7226 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7227 + */
7228 +
7229 +/*
7230 + * debug print functions
7231 + */
7232 +
7233 +#ifndef __AUFS_DEBUG_H__
7234 +#define __AUFS_DEBUG_H__
7235 +
7236 +#ifdef __KERNEL__
7237 +
7238 +#include <linux/atomic.h>
7239 +#include <linux/module.h>
7240 +#include <linux/kallsyms.h>
7241 +#include <linux/sysrq.h>
7242 +
7243 +#ifdef CONFIG_AUFS_DEBUG
7244 +#define AuDebugOn(a)           BUG_ON(a)
7245 +
7246 +/* module parameter */
7247 +extern atomic_t aufs_debug;
7248 +static inline void au_debug_on(void)
7249 +{
7250 +       atomic_inc(&aufs_debug);
7251 +}
7252 +static inline void au_debug_off(void)
7253 +{
7254 +       atomic_dec_if_positive(&aufs_debug);
7255 +}
7256 +
7257 +static inline int au_debug_test(void)
7258 +{
7259 +       return atomic_read(&aufs_debug) > 0;
7260 +}
7261 +#else
7262 +#define AuDebugOn(a)           do {} while (0)
7263 +AuStubVoid(au_debug_on, void)
7264 +AuStubVoid(au_debug_off, void)
7265 +AuStubInt0(au_debug_test, void)
7266 +#endif /* CONFIG_AUFS_DEBUG */
7267 +
7268 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7269 +
7270 +/* ---------------------------------------------------------------------- */
7271 +
7272 +/* debug print */
7273 +
7274 +#define AuDbg(fmt, ...) do { \
7275 +       if (au_debug_test()) \
7276 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7277 +} while (0)
7278 +#define AuLabel(l)             AuDbg(#l "\n")
7279 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7280 +#define AuWarn1(fmt, ...) do { \
7281 +       static unsigned char _c; \
7282 +       if (!_c++) \
7283 +               pr_warn(fmt, ##__VA_ARGS__); \
7284 +} while (0)
7285 +
7286 +#define AuErr1(fmt, ...) do { \
7287 +       static unsigned char _c; \
7288 +       if (!_c++) \
7289 +               pr_err(fmt, ##__VA_ARGS__); \
7290 +} while (0)
7291 +
7292 +#define AuIOErr1(fmt, ...) do { \
7293 +       static unsigned char _c; \
7294 +       if (!_c++) \
7295 +               AuIOErr(fmt, ##__VA_ARGS__); \
7296 +} while (0)
7297 +
7298 +#define AuUnsupportMsg "This operation is not supported." \
7299 +                       " Please report this application to aufs-users ML."
7300 +#define AuUnsupport(fmt, ...) do { \
7301 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7302 +       dump_stack(); \
7303 +} while (0)
7304 +
7305 +#define AuTraceErr(e) do { \
7306 +       if (unlikely((e) < 0)) \
7307 +               AuDbg("err %d\n", (int)(e)); \
7308 +} while (0)
7309 +
7310 +#define AuTraceErrPtr(p) do { \
7311 +       if (IS_ERR(p)) \
7312 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7313 +} while (0)
7314 +
7315 +/* dirty macros for debug print, use with "%.*s" and caution */
7316 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7317 +
7318 +/* ---------------------------------------------------------------------- */
7319 +
7320 +struct dentry;
7321 +#ifdef CONFIG_AUFS_DEBUG
7322 +extern struct mutex au_dbg_mtx;
7323 +extern char *au_plevel;
7324 +struct au_nhash;
7325 +void au_dpri_whlist(struct au_nhash *whlist);
7326 +struct au_vdir;
7327 +void au_dpri_vdir(struct au_vdir *vdir);
7328 +struct inode;
7329 +void au_dpri_inode(struct inode *inode);
7330 +void au_dpri_dalias(struct inode *inode);
7331 +void au_dpri_dentry(struct dentry *dentry);
7332 +struct file;
7333 +void au_dpri_file(struct file *filp);
7334 +struct super_block;
7335 +void au_dpri_sb(struct super_block *sb);
7336 +
7337 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7338 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7339 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7340 +void au_dbg_verify_kthread(void);
7341 +
7342 +int __init au_debug_init(void);
7343 +
7344 +#define AuDbgWhlist(w) do { \
7345 +       mutex_lock(&au_dbg_mtx); \
7346 +       AuDbg(#w "\n"); \
7347 +       au_dpri_whlist(w); \
7348 +       mutex_unlock(&au_dbg_mtx); \
7349 +} while (0)
7350 +
7351 +#define AuDbgVdir(v) do { \
7352 +       mutex_lock(&au_dbg_mtx); \
7353 +       AuDbg(#v "\n"); \
7354 +       au_dpri_vdir(v); \
7355 +       mutex_unlock(&au_dbg_mtx); \
7356 +} while (0)
7357 +
7358 +#define AuDbgInode(i) do { \
7359 +       mutex_lock(&au_dbg_mtx); \
7360 +       AuDbg(#i "\n"); \
7361 +       au_dpri_inode(i); \
7362 +       mutex_unlock(&au_dbg_mtx); \
7363 +} while (0)
7364 +
7365 +#define AuDbgDAlias(i) do { \
7366 +       mutex_lock(&au_dbg_mtx); \
7367 +       AuDbg(#i "\n"); \
7368 +       au_dpri_dalias(i); \
7369 +       mutex_unlock(&au_dbg_mtx); \
7370 +} while (0)
7371 +
7372 +#define AuDbgDentry(d) do { \
7373 +       mutex_lock(&au_dbg_mtx); \
7374 +       AuDbg(#d "\n"); \
7375 +       au_dpri_dentry(d); \
7376 +       mutex_unlock(&au_dbg_mtx); \
7377 +} while (0)
7378 +
7379 +#define AuDbgFile(f) do { \
7380 +       mutex_lock(&au_dbg_mtx); \
7381 +       AuDbg(#f "\n"); \
7382 +       au_dpri_file(f); \
7383 +       mutex_unlock(&au_dbg_mtx); \
7384 +} while (0)
7385 +
7386 +#define AuDbgSb(sb) do { \
7387 +       mutex_lock(&au_dbg_mtx); \
7388 +       AuDbg(#sb "\n"); \
7389 +       au_dpri_sb(sb); \
7390 +       mutex_unlock(&au_dbg_mtx); \
7391 +} while (0)
7392 +
7393 +#define AuDbgSym(addr) do {                            \
7394 +       char sym[KSYM_SYMBOL_LEN];                      \
7395 +       sprint_symbol(sym, (unsigned long)addr);        \
7396 +       AuDbg("%s\n", sym);                             \
7397 +} while (0)
7398 +#else
7399 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7400 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7401 +AuStubVoid(au_dbg_verify_kthread, void)
7402 +AuStubInt0(__init au_debug_init, void)
7403 +
7404 +#define AuDbgWhlist(w)         do {} while (0)
7405 +#define AuDbgVdir(v)           do {} while (0)
7406 +#define AuDbgInode(i)          do {} while (0)
7407 +#define AuDbgDAlias(i)         do {} while (0)
7408 +#define AuDbgDentry(d)         do {} while (0)
7409 +#define AuDbgFile(f)           do {} while (0)
7410 +#define AuDbgSb(sb)            do {} while (0)
7411 +#define AuDbgSym(addr)         do {} while (0)
7412 +#endif /* CONFIG_AUFS_DEBUG */
7413 +
7414 +/* ---------------------------------------------------------------------- */
7415 +
7416 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7417 +int __init au_sysrq_init(void);
7418 +void au_sysrq_fin(void);
7419 +
7420 +#ifdef CONFIG_HW_CONSOLE
7421 +#define au_dbg_blocked() do { \
7422 +       WARN_ON(1); \
7423 +       handle_sysrq('w'); \
7424 +} while (0)
7425 +#else
7426 +AuStubVoid(au_dbg_blocked, void)
7427 +#endif
7428 +
7429 +#else
7430 +AuStubInt0(__init au_sysrq_init, void)
7431 +AuStubVoid(au_sysrq_fin, void)
7432 +AuStubVoid(au_dbg_blocked, void)
7433 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7434 +
7435 +#endif /* __KERNEL__ */
7436 +#endif /* __AUFS_DEBUG_H__ */
7437 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7438 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7439 +++ linux/fs/aufs/dentry.c      2015-09-24 10:47:58.251386326 +0200
7440 @@ -0,0 +1,1105 @@
7441 +/*
7442 + * Copyright (C) 2005-2015 Junjiro R. Okajima
7443 + *
7444 + * This program, aufs is free software; you can redistribute it and/or modify
7445 + * it under the terms of the GNU General Public License as published by
7446 + * the Free Software Foundation; either version 2 of the License, or
7447 + * (at your option) any later version.
7448 + *
7449 + * This program is distributed in the hope that it will be useful,
7450 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7451 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7452 + * GNU General Public License for more details.
7453 + *
7454 + * You should have received a copy of the GNU General Public License
7455 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7456 + */
7457 +
7458 +/*
7459 + * lookup and dentry operations
7460 + */
7461 +
7462 +#include <linux/namei.h>
7463 +#include "aufs.h"
7464 +
7465 +#define AuLkup_ALLOW_NEG       1
7466 +#define AuLkup_IGNORE_PERM     (1 << 1)
7467 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
7468 +#define au_fset_lkup(flags, name) \
7469 +       do { (flags) |= AuLkup_##name; } while (0)
7470 +#define au_fclr_lkup(flags, name) \
7471 +       do { (flags) &= ~AuLkup_##name; } while (0)
7472 +
7473 +struct au_do_lookup_args {
7474 +       unsigned int            flags;
7475 +       mode_t                  type;
7476 +};
7477 +
7478 +/*
7479 + * returns positive/negative dentry, NULL or an error.
7480 + * NULL means whiteout-ed or not-found.
7481 + */
7482 +static struct dentry*
7483 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7484 +            aufs_bindex_t bindex, struct qstr *wh_name,
7485 +            struct au_do_lookup_args *args)
7486 +{
7487 +       struct dentry *h_dentry;
7488 +       struct inode *h_inode;
7489 +       struct au_branch *br;
7490 +       int wh_found, opq;
7491 +       unsigned char wh_able;
7492 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7493 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7494 +                                                         IGNORE_PERM);
7495 +
7496 +       wh_found = 0;
7497 +       br = au_sbr(dentry->d_sb, bindex);
7498 +       wh_able = !!au_br_whable(br->br_perm);
7499 +       if (wh_able)
7500 +               wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
7501 +       h_dentry = ERR_PTR(wh_found);
7502 +       if (!wh_found)
7503 +               goto real_lookup;
7504 +       if (unlikely(wh_found < 0))
7505 +               goto out;
7506 +
7507 +       /* We found a whiteout */
7508 +       /* au_set_dbend(dentry, bindex); */
7509 +       au_set_dbwh(dentry, bindex);
7510 +       if (!allow_neg)
7511 +               return NULL; /* success */
7512 +
7513 +real_lookup:
7514 +       if (!ignore_perm)
7515 +               h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7516 +       else
7517 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7518 +       if (IS_ERR(h_dentry)) {
7519 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7520 +                   && !allow_neg)
7521 +                       h_dentry = NULL;
7522 +               goto out;
7523 +       }
7524 +
7525 +       h_inode = d_inode(h_dentry);
7526 +       if (d_is_negative(h_dentry)) {
7527 +               if (!allow_neg)
7528 +                       goto out_neg;
7529 +       } else if (wh_found
7530 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7531 +               goto out_neg;
7532 +
7533 +       if (au_dbend(dentry) <= bindex)
7534 +               au_set_dbend(dentry, bindex);
7535 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
7536 +               au_set_dbstart(dentry, bindex);
7537 +       au_set_h_dptr(dentry, bindex, h_dentry);
7538 +
7539 +       if (!d_is_dir(h_dentry)
7540 +           || !wh_able
7541 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
7542 +               goto out; /* success */
7543 +
7544 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
7545 +       opq = au_diropq_test(h_dentry);
7546 +       mutex_unlock(&h_inode->i_mutex);
7547 +       if (opq > 0)
7548 +               au_set_dbdiropq(dentry, bindex);
7549 +       else if (unlikely(opq < 0)) {
7550 +               au_set_h_dptr(dentry, bindex, NULL);
7551 +               h_dentry = ERR_PTR(opq);
7552 +       }
7553 +       goto out;
7554 +
7555 +out_neg:
7556 +       dput(h_dentry);
7557 +       h_dentry = NULL;
7558 +out:
7559 +       return h_dentry;
7560 +}
7561 +
7562 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7563 +{
7564 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7565 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7566 +               return -EPERM;
7567 +       return 0;
7568 +}
7569 +
7570 +/*
7571 + * returns the number of lower positive dentries,
7572 + * otherwise an error.
7573 + * can be called at unlinking with @type is zero.
7574 + */
7575 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type)
7576 +{
7577 +       int npositive, err;
7578 +       aufs_bindex_t bindex, btail, bdiropq;
7579 +       unsigned char isdir, dirperm1;
7580 +       struct qstr whname;
7581 +       struct au_do_lookup_args args = {
7582 +               .flags          = 0,
7583 +               .type           = type
7584 +       };
7585 +       const struct qstr *name = &dentry->d_name;
7586 +       struct dentry *parent;
7587 +       struct super_block *sb;
7588 +
7589 +       sb = dentry->d_sb;
7590 +       err = au_test_shwh(sb, name);
7591 +       if (unlikely(err))
7592 +               goto out;
7593 +
7594 +       err = au_wh_name_alloc(&whname, name);
7595 +       if (unlikely(err))
7596 +               goto out;
7597 +
7598 +       isdir = !!d_is_dir(dentry);
7599 +       if (!type)
7600 +               au_fset_lkup(args.flags, ALLOW_NEG);
7601 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
7602 +
7603 +       npositive = 0;
7604 +       parent = dget_parent(dentry);
7605 +       btail = au_dbtaildir(parent);
7606 +       for (bindex = bstart; bindex <= btail; bindex++) {
7607 +               struct dentry *h_parent, *h_dentry;
7608 +               struct inode *h_inode, *h_dir;
7609 +
7610 +               h_dentry = au_h_dptr(dentry, bindex);
7611 +               if (h_dentry) {
7612 +                       if (d_is_positive(h_dentry))
7613 +                               npositive++;
7614 +                       if (type != S_IFDIR)
7615 +                               break;
7616 +                       continue;
7617 +               }
7618 +               h_parent = au_h_dptr(parent, bindex);
7619 +               if (!h_parent || !d_is_dir(h_parent))
7620 +                       continue;
7621 +
7622 +               h_dir = d_inode(h_parent);
7623 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
7624 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7625 +                                       &args);
7626 +               mutex_unlock(&h_dir->i_mutex);
7627 +               err = PTR_ERR(h_dentry);
7628 +               if (IS_ERR(h_dentry))
7629 +                       goto out_parent;
7630 +               if (h_dentry)
7631 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
7632 +               if (dirperm1)
7633 +                       au_fset_lkup(args.flags, IGNORE_PERM);
7634 +
7635 +               if (au_dbwh(dentry) >= 0)
7636 +                       break;
7637 +               if (!h_dentry)
7638 +                       continue;
7639 +               if (d_is_negative(h_dentry))
7640 +                       continue;
7641 +               h_inode = d_inode(h_dentry);
7642 +               npositive++;
7643 +               if (!args.type)
7644 +                       args.type = h_inode->i_mode & S_IFMT;
7645 +               if (args.type != S_IFDIR)
7646 +                       break;
7647 +               else if (isdir) {
7648 +                       /* the type of lower may be different */
7649 +                       bdiropq = au_dbdiropq(dentry);
7650 +                       if (bdiropq >= 0 && bdiropq <= bindex)
7651 +                               break;
7652 +               }
7653 +       }
7654 +
7655 +       if (npositive) {
7656 +               AuLabel(positive);
7657 +               au_update_dbstart(dentry);
7658 +       }
7659 +       err = npositive;
7660 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
7661 +                    && au_dbstart(dentry) < 0)) {
7662 +               err = -EIO;
7663 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7664 +                       dentry, err);
7665 +       }
7666 +
7667 +out_parent:
7668 +       dput(parent);
7669 +       kfree(whname.name);
7670 +out:
7671 +       return err;
7672 +}
7673 +
7674 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
7675 +{
7676 +       struct dentry *dentry;
7677 +       int wkq_err;
7678 +
7679 +       if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
7680 +               dentry = vfsub_lkup_one(name, parent);
7681 +       else {
7682 +               struct vfsub_lkup_one_args args = {
7683 +                       .errp   = &dentry,
7684 +                       .name   = name,
7685 +                       .parent = parent
7686 +               };
7687 +
7688 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
7689 +               if (unlikely(wkq_err))
7690 +                       dentry = ERR_PTR(wkq_err);
7691 +       }
7692 +
7693 +       return dentry;
7694 +}
7695 +
7696 +/*
7697 + * lookup @dentry on @bindex which should be negative.
7698 + */
7699 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
7700 +{
7701 +       int err;
7702 +       struct dentry *parent, *h_parent, *h_dentry;
7703 +       struct au_branch *br;
7704 +
7705 +       parent = dget_parent(dentry);
7706 +       h_parent = au_h_dptr(parent, bindex);
7707 +       br = au_sbr(dentry->d_sb, bindex);
7708 +       if (wh)
7709 +               h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7710 +       else
7711 +               h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
7712 +       err = PTR_ERR(h_dentry);
7713 +       if (IS_ERR(h_dentry))
7714 +               goto out;
7715 +       if (unlikely(d_is_positive(h_dentry))) {
7716 +               err = -EIO;
7717 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
7718 +               dput(h_dentry);
7719 +               goto out;
7720 +       }
7721 +
7722 +       err = 0;
7723 +       if (bindex < au_dbstart(dentry))
7724 +               au_set_dbstart(dentry, bindex);
7725 +       if (au_dbend(dentry) < bindex)
7726 +               au_set_dbend(dentry, bindex);
7727 +       au_set_h_dptr(dentry, bindex, h_dentry);
7728 +
7729 +out:
7730 +       dput(parent);
7731 +       return err;
7732 +}
7733 +
7734 +/* ---------------------------------------------------------------------- */
7735 +
7736 +/* subset of struct inode */
7737 +struct au_iattr {
7738 +       unsigned long           i_ino;
7739 +       /* unsigned int         i_nlink; */
7740 +       kuid_t                  i_uid;
7741 +       kgid_t                  i_gid;
7742 +       u64                     i_version;
7743 +/*
7744 +       loff_t                  i_size;
7745 +       blkcnt_t                i_blocks;
7746 +*/
7747 +       umode_t                 i_mode;
7748 +};
7749 +
7750 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7751 +{
7752 +       ia->i_ino = h_inode->i_ino;
7753 +       /* ia->i_nlink = h_inode->i_nlink; */
7754 +       ia->i_uid = h_inode->i_uid;
7755 +       ia->i_gid = h_inode->i_gid;
7756 +       ia->i_version = h_inode->i_version;
7757 +/*
7758 +       ia->i_size = h_inode->i_size;
7759 +       ia->i_blocks = h_inode->i_blocks;
7760 +*/
7761 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
7762 +}
7763 +
7764 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7765 +{
7766 +       return ia->i_ino != h_inode->i_ino
7767 +               /* || ia->i_nlink != h_inode->i_nlink */
7768 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
7769 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
7770 +               || ia->i_version != h_inode->i_version
7771 +/*
7772 +               || ia->i_size != h_inode->i_size
7773 +               || ia->i_blocks != h_inode->i_blocks
7774 +*/
7775 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
7776 +}
7777 +
7778 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7779 +                             struct au_branch *br)
7780 +{
7781 +       int err;
7782 +       struct au_iattr ia;
7783 +       struct inode *h_inode;
7784 +       struct dentry *h_d;
7785 +       struct super_block *h_sb;
7786 +
7787 +       err = 0;
7788 +       memset(&ia, -1, sizeof(ia));
7789 +       h_sb = h_dentry->d_sb;
7790 +       h_inode = NULL;
7791 +       if (d_is_positive(h_dentry)) {
7792 +               h_inode = d_inode(h_dentry);
7793 +               au_iattr_save(&ia, h_inode);
7794 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
7795 +               /* nfs d_revalidate may return 0 for negative dentry */
7796 +               /* fuse d_revalidate always return 0 for negative dentry */
7797 +               goto out;
7798 +
7799 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
7800 +       h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
7801 +       err = PTR_ERR(h_d);
7802 +       if (IS_ERR(h_d))
7803 +               goto out;
7804 +
7805 +       err = 0;
7806 +       if (unlikely(h_d != h_dentry
7807 +                    || d_inode(h_d) != h_inode
7808 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
7809 +               err = au_busy_or_stale();
7810 +       dput(h_d);
7811 +
7812 +out:
7813 +       AuTraceErr(err);
7814 +       return err;
7815 +}
7816 +
7817 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
7818 +               struct dentry *h_parent, struct au_branch *br)
7819 +{
7820 +       int err;
7821 +
7822 +       err = 0;
7823 +       if (udba == AuOpt_UDBA_REVAL
7824 +           && !au_test_fs_remote(h_dentry->d_sb)) {
7825 +               IMustLock(h_dir);
7826 +               err = (d_inode(h_dentry->d_parent) != h_dir);
7827 +       } else if (udba != AuOpt_UDBA_NONE)
7828 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
7829 +
7830 +       return err;
7831 +}
7832 +
7833 +/* ---------------------------------------------------------------------- */
7834 +
7835 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
7836 +{
7837 +       int err;
7838 +       aufs_bindex_t new_bindex, bindex, bend, bwh, bdiropq;
7839 +       struct au_hdentry tmp, *p, *q;
7840 +       struct au_dinfo *dinfo;
7841 +       struct super_block *sb;
7842 +
7843 +       DiMustWriteLock(dentry);
7844 +
7845 +       sb = dentry->d_sb;
7846 +       dinfo = au_di(dentry);
7847 +       bend = dinfo->di_bend;
7848 +       bwh = dinfo->di_bwh;
7849 +       bdiropq = dinfo->di_bdiropq;
7850 +       p = dinfo->di_hdentry + dinfo->di_bstart;
7851 +       for (bindex = dinfo->di_bstart; bindex <= bend; bindex++, p++) {
7852 +               if (!p->hd_dentry)
7853 +                       continue;
7854 +
7855 +               new_bindex = au_br_index(sb, p->hd_id);
7856 +               if (new_bindex == bindex)
7857 +                       continue;
7858 +
7859 +               if (dinfo->di_bwh == bindex)
7860 +                       bwh = new_bindex;
7861 +               if (dinfo->di_bdiropq == bindex)
7862 +                       bdiropq = new_bindex;
7863 +               if (new_bindex < 0) {
7864 +                       au_hdput(p);
7865 +                       p->hd_dentry = NULL;
7866 +                       continue;
7867 +               }
7868 +
7869 +               /* swap two lower dentries, and loop again */
7870 +               q = dinfo->di_hdentry + new_bindex;
7871 +               tmp = *q;
7872 +               *q = *p;
7873 +               *p = tmp;
7874 +               if (tmp.hd_dentry) {
7875 +                       bindex--;
7876 +                       p--;
7877 +               }
7878 +       }
7879 +
7880 +       dinfo->di_bwh = -1;
7881 +       if (bwh >= 0 && bwh <= au_sbend(sb) && au_sbr_whable(sb, bwh))
7882 +               dinfo->di_bwh = bwh;
7883 +
7884 +       dinfo->di_bdiropq = -1;
7885 +       if (bdiropq >= 0
7886 +           && bdiropq <= au_sbend(sb)
7887 +           && au_sbr_whable(sb, bdiropq))
7888 +               dinfo->di_bdiropq = bdiropq;
7889 +
7890 +       err = -EIO;
7891 +       dinfo->di_bstart = -1;
7892 +       dinfo->di_bend = -1;
7893 +       bend = au_dbend(parent);
7894 +       p = dinfo->di_hdentry;
7895 +       for (bindex = 0; bindex <= bend; bindex++, p++)
7896 +               if (p->hd_dentry) {
7897 +                       dinfo->di_bstart = bindex;
7898 +                       break;
7899 +               }
7900 +
7901 +       if (dinfo->di_bstart >= 0) {
7902 +               p = dinfo->di_hdentry + bend;
7903 +               for (bindex = bend; bindex >= 0; bindex--, p--)
7904 +                       if (p->hd_dentry) {
7905 +                               dinfo->di_bend = bindex;
7906 +                               err = 0;
7907 +                               break;
7908 +                       }
7909 +       }
7910 +
7911 +       return err;
7912 +}
7913 +
7914 +static void au_do_hide(struct dentry *dentry)
7915 +{
7916 +       struct inode *inode;
7917 +
7918 +       if (d_really_is_positive(dentry)) {
7919 +               inode = d_inode(dentry);
7920 +               if (!d_is_dir(dentry)) {
7921 +                       if (inode->i_nlink && !d_unhashed(dentry))
7922 +                               drop_nlink(inode);
7923 +               } else {
7924 +                       clear_nlink(inode);
7925 +                       /* stop next lookup */
7926 +                       inode->i_flags |= S_DEAD;
7927 +               }
7928 +               smp_mb(); /* necessary? */
7929 +       }
7930 +       d_drop(dentry);
7931 +}
7932 +
7933 +static int au_hide_children(struct dentry *parent)
7934 +{
7935 +       int err, i, j, ndentry;
7936 +       struct au_dcsub_pages dpages;
7937 +       struct au_dpage *dpage;
7938 +       struct dentry *dentry;
7939 +
7940 +       err = au_dpages_init(&dpages, GFP_NOFS);
7941 +       if (unlikely(err))
7942 +               goto out;
7943 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
7944 +       if (unlikely(err))
7945 +               goto out_dpages;
7946 +
7947 +       /* in reverse order */
7948 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
7949 +               dpage = dpages.dpages + i;
7950 +               ndentry = dpage->ndentry;
7951 +               for (j = ndentry - 1; j >= 0; j--) {
7952 +                       dentry = dpage->dentries[j];
7953 +                       if (dentry != parent)
7954 +                               au_do_hide(dentry);
7955 +               }
7956 +       }
7957 +
7958 +out_dpages:
7959 +       au_dpages_free(&dpages);
7960 +out:
7961 +       return err;
7962 +}
7963 +
7964 +static void au_hide(struct dentry *dentry)
7965 +{
7966 +       int err;
7967 +
7968 +       AuDbgDentry(dentry);
7969 +       if (d_is_dir(dentry)) {
7970 +               /* shrink_dcache_parent(dentry); */
7971 +               err = au_hide_children(dentry);
7972 +               if (unlikely(err))
7973 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
7974 +                               dentry, err);
7975 +       }
7976 +       au_do_hide(dentry);
7977 +}
7978 +
7979 +/*
7980 + * By adding a dirty branch, a cached dentry may be affected in various ways.
7981 + *
7982 + * a dirty branch is added
7983 + * - on the top of layers
7984 + * - in the middle of layers
7985 + * - to the bottom of layers
7986 + *
7987 + * on the added branch there exists
7988 + * - a whiteout
7989 + * - a diropq
7990 + * - a same named entry
7991 + *   + exist
7992 + *     * negative --> positive
7993 + *     * positive --> positive
7994 + *      - type is unchanged
7995 + *      - type is changed
7996 + *   + doesn't exist
7997 + *     * negative --> negative
7998 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
7999 + * - none
8000 + */
8001 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8002 +                              struct au_dinfo *tmp)
8003 +{
8004 +       int err;
8005 +       aufs_bindex_t bindex, bend;
8006 +       struct {
8007 +               struct dentry *dentry;
8008 +               struct inode *inode;
8009 +               mode_t mode;
8010 +       } orig_h, tmp_h;
8011 +       struct au_hdentry *hd;
8012 +       struct inode *inode, *h_inode;
8013 +       struct dentry *h_dentry;
8014 +
8015 +       err = 0;
8016 +       AuDebugOn(dinfo->di_bstart < 0);
8017 +       orig_h.mode = 0;
8018 +       orig_h.dentry = dinfo->di_hdentry[dinfo->di_bstart].hd_dentry;
8019 +       orig_h.inode = NULL;
8020 +       if (d_is_positive(orig_h.dentry)) {
8021 +               orig_h.inode = d_inode(orig_h.dentry);
8022 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8023 +       }
8024 +       memset(&tmp_h, 0, sizeof(tmp_h));
8025 +       if (tmp->di_bstart >= 0) {
8026 +               tmp_h.dentry = tmp->di_hdentry[tmp->di_bstart].hd_dentry;
8027 +               tmp_h.inode = NULL;
8028 +               if (d_is_positive(tmp_h.dentry)) {
8029 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8030 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8031 +               }
8032 +       }
8033 +
8034 +       inode = NULL;
8035 +       if (d_really_is_positive(dentry))
8036 +               inode = d_inode(dentry);
8037 +       if (!orig_h.inode) {
8038 +               AuDbg("nagative originally\n");
8039 +               if (inode) {
8040 +                       au_hide(dentry);
8041 +                       goto out;
8042 +               }
8043 +               AuDebugOn(inode);
8044 +               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8045 +               AuDebugOn(dinfo->di_bdiropq != -1);
8046 +
8047 +               if (!tmp_h.inode) {
8048 +                       AuDbg("negative --> negative\n");
8049 +                       /* should have only one negative lower */
8050 +                       if (tmp->di_bstart >= 0
8051 +                           && tmp->di_bstart < dinfo->di_bstart) {
8052 +                               AuDebugOn(tmp->di_bstart != tmp->di_bend);
8053 +                               AuDebugOn(dinfo->di_bstart != dinfo->di_bend);
8054 +                               au_set_h_dptr(dentry, dinfo->di_bstart, NULL);
8055 +                               au_di_cp(dinfo, tmp);
8056 +                               hd = tmp->di_hdentry + tmp->di_bstart;
8057 +                               au_set_h_dptr(dentry, tmp->di_bstart,
8058 +                                             dget(hd->hd_dentry));
8059 +                       }
8060 +                       au_dbg_verify_dinode(dentry);
8061 +               } else {
8062 +                       AuDbg("negative --> positive\n");
8063 +                       /*
8064 +                        * similar to the behaviour of creating with bypassing
8065 +                        * aufs.
8066 +                        * unhash it in order to force an error in the
8067 +                        * succeeding create operation.
8068 +                        * we should not set S_DEAD here.
8069 +                        */
8070 +                       d_drop(dentry);
8071 +                       /* au_di_swap(tmp, dinfo); */
8072 +                       au_dbg_verify_dinode(dentry);
8073 +               }
8074 +       } else {
8075 +               AuDbg("positive originally\n");
8076 +               /* inode may be NULL */
8077 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8078 +               if (!tmp_h.inode) {
8079 +                       AuDbg("positive --> negative\n");
8080 +                       /* or bypassing aufs */
8081 +                       au_hide(dentry);
8082 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_bstart)
8083 +                               dinfo->di_bwh = tmp->di_bwh;
8084 +                       if (inode)
8085 +                               err = au_refresh_hinode_self(inode);
8086 +                       au_dbg_verify_dinode(dentry);
8087 +               } else if (orig_h.mode == tmp_h.mode) {
8088 +                       AuDbg("positive --> positive, same type\n");
8089 +                       if (!S_ISDIR(orig_h.mode)
8090 +                           && dinfo->di_bstart > tmp->di_bstart) {
8091 +                               /*
8092 +                                * similar to the behaviour of removing and
8093 +                                * creating.
8094 +                                */
8095 +                               au_hide(dentry);
8096 +                               if (inode)
8097 +                                       err = au_refresh_hinode_self(inode);
8098 +                               au_dbg_verify_dinode(dentry);
8099 +                       } else {
8100 +                               /* fill empty slots */
8101 +                               if (dinfo->di_bstart > tmp->di_bstart)
8102 +                                       dinfo->di_bstart = tmp->di_bstart;
8103 +                               if (dinfo->di_bend < tmp->di_bend)
8104 +                                       dinfo->di_bend = tmp->di_bend;
8105 +                               dinfo->di_bwh = tmp->di_bwh;
8106 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8107 +                               hd = tmp->di_hdentry;
8108 +                               bend = dinfo->di_bend;
8109 +                               for (bindex = tmp->di_bstart; bindex <= bend;
8110 +                                    bindex++) {
8111 +                                       if (au_h_dptr(dentry, bindex))
8112 +                                               continue;
8113 +                                       h_dentry = hd[bindex].hd_dentry;
8114 +                                       if (!h_dentry)
8115 +                                               continue;
8116 +                                       AuDebugOn(d_is_negative(h_dentry));
8117 +                                       h_inode = d_inode(h_dentry);
8118 +                                       AuDebugOn(orig_h.mode
8119 +                                                 != (h_inode->i_mode
8120 +                                                     & S_IFMT));
8121 +                                       au_set_h_dptr(dentry, bindex,
8122 +                                                     dget(h_dentry));
8123 +                               }
8124 +                               err = au_refresh_hinode(inode, dentry);
8125 +                               au_dbg_verify_dinode(dentry);
8126 +                       }
8127 +               } else {
8128 +                       AuDbg("positive --> positive, different type\n");
8129 +                       /* similar to the behaviour of removing and creating */
8130 +                       au_hide(dentry);
8131 +                       if (inode)
8132 +                               err = au_refresh_hinode_self(inode);
8133 +                       au_dbg_verify_dinode(dentry);
8134 +               }
8135 +       }
8136 +
8137 +out:
8138 +       return err;
8139 +}
8140 +
8141 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8142 +{
8143 +       int err, ebrange;
8144 +       unsigned int sigen;
8145 +       struct au_dinfo *dinfo, *tmp;
8146 +       struct super_block *sb;
8147 +       struct inode *inode;
8148 +
8149 +       DiMustWriteLock(dentry);
8150 +       AuDebugOn(IS_ROOT(dentry));
8151 +       AuDebugOn(d_really_is_negative(parent));
8152 +
8153 +       sb = dentry->d_sb;
8154 +       sigen = au_sigen(sb);
8155 +       err = au_digen_test(parent, sigen);
8156 +       if (unlikely(err))
8157 +               goto out;
8158 +
8159 +       dinfo = au_di(dentry);
8160 +       err = au_di_realloc(dinfo, au_sbend(sb) + 1);
8161 +       if (unlikely(err))
8162 +               goto out;
8163 +       ebrange = au_dbrange_test(dentry);
8164 +       if (!ebrange)
8165 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8166 +
8167 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8168 +               AuDebugOn(au_dbstart(dentry) < 0 && au_dbend(dentry) >= 0);
8169 +               if (d_really_is_positive(dentry)) {
8170 +                       inode = d_inode(dentry);
8171 +                       err = au_refresh_hinode_self(inode);
8172 +               }
8173 +               au_dbg_verify_dinode(dentry);
8174 +               if (!err)
8175 +                       goto out_dgen; /* success */
8176 +               goto out;
8177 +       }
8178 +
8179 +       /* temporary dinfo */
8180 +       AuDbgDentry(dentry);
8181 +       err = -ENOMEM;
8182 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8183 +       if (unlikely(!tmp))
8184 +               goto out;
8185 +       au_di_swap(tmp, dinfo);
8186 +       /* returns the number of positive dentries */
8187 +       /*
8188 +        * if current working dir is removed, it returns an error.
8189 +        * but the dentry is legal.
8190 +        */
8191 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
8192 +       AuDbgDentry(dentry);
8193 +       au_di_swap(tmp, dinfo);
8194 +       if (err == -ENOENT)
8195 +               err = 0;
8196 +       if (err >= 0) {
8197 +               /* compare/refresh by dinfo */
8198 +               AuDbgDentry(dentry);
8199 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8200 +               au_dbg_verify_dinode(dentry);
8201 +               AuTraceErr(err);
8202 +       }
8203 +       au_rw_write_unlock(&tmp->di_rwsem);
8204 +       au_di_free(tmp);
8205 +       if (unlikely(err))
8206 +               goto out;
8207 +
8208 +out_dgen:
8209 +       au_update_digen(dentry);
8210 +out:
8211 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8212 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8213 +               AuDbgDentry(dentry);
8214 +       }
8215 +       AuTraceErr(err);
8216 +       return err;
8217 +}
8218 +
8219 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8220 +                          struct dentry *dentry, aufs_bindex_t bindex)
8221 +{
8222 +       int err, valid;
8223 +
8224 +       err = 0;
8225 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8226 +               goto out;
8227 +
8228 +       AuDbg("b%d\n", bindex);
8229 +       /*
8230 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8231 +        * due to whiteout and branch permission.
8232 +        */
8233 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8234 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8235 +       /* it may return tri-state */
8236 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8237 +
8238 +       if (unlikely(valid < 0))
8239 +               err = valid;
8240 +       else if (!valid)
8241 +               err = -EINVAL;
8242 +
8243 +out:
8244 +       AuTraceErr(err);
8245 +       return err;
8246 +}
8247 +
8248 +/* todo: remove this */
8249 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8250 +                         unsigned int flags, int do_udba)
8251 +{
8252 +       int err;
8253 +       umode_t mode, h_mode;
8254 +       aufs_bindex_t bindex, btail, bstart, ibs, ibe;
8255 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8256 +       struct inode *h_inode, *h_cached_inode;
8257 +       struct dentry *h_dentry;
8258 +       struct qstr *name, *h_name;
8259 +
8260 +       err = 0;
8261 +       plus = 0;
8262 +       mode = 0;
8263 +       ibs = -1;
8264 +       ibe = -1;
8265 +       unhashed = !!d_unhashed(dentry);
8266 +       is_root = !!IS_ROOT(dentry);
8267 +       name = &dentry->d_name;
8268 +       tmpfile = au_di(dentry)->di_tmpfile;
8269 +
8270 +       /*
8271 +        * Theoretically, REVAL test should be unnecessary in case of
8272 +        * {FS,I}NOTIFY.
8273 +        * But {fs,i}notify doesn't fire some necessary events,
8274 +        *      IN_ATTRIB for atime/nlink/pageio
8275 +        * Let's do REVAL test too.
8276 +        */
8277 +       if (do_udba && inode) {
8278 +               mode = (inode->i_mode & S_IFMT);
8279 +               plus = (inode->i_nlink > 0);
8280 +               ibs = au_ibstart(inode);
8281 +               ibe = au_ibend(inode);
8282 +       }
8283 +
8284 +       bstart = au_dbstart(dentry);
8285 +       btail = bstart;
8286 +       if (inode && S_ISDIR(inode->i_mode))
8287 +               btail = au_dbtaildir(dentry);
8288 +       for (bindex = bstart; bindex <= btail; bindex++) {
8289 +               h_dentry = au_h_dptr(dentry, bindex);
8290 +               if (!h_dentry)
8291 +                       continue;
8292 +
8293 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8294 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8295 +               spin_lock(&h_dentry->d_lock);
8296 +               h_name = &h_dentry->d_name;
8297 +               if (unlikely(do_udba
8298 +                            && !is_root
8299 +                            && ((!h_nfs
8300 +                                 && (unhashed != !!d_unhashed(h_dentry)
8301 +                                     || (!tmpfile
8302 +                                         && !au_qstreq(name, h_name))
8303 +                                         ))
8304 +                                || (h_nfs
8305 +                                    && !(flags & LOOKUP_OPEN)
8306 +                                    && (h_dentry->d_flags
8307 +                                        & DCACHE_NFSFS_RENAMED)))
8308 +                           )) {
8309 +                       int h_unhashed;
8310 +
8311 +                       h_unhashed = d_unhashed(h_dentry);
8312 +                       spin_unlock(&h_dentry->d_lock);
8313 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8314 +                             unhashed, h_unhashed, dentry, h_dentry);
8315 +                       goto err;
8316 +               }
8317 +               spin_unlock(&h_dentry->d_lock);
8318 +
8319 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8320 +               if (unlikely(err))
8321 +                       /* do not goto err, to keep the errno */
8322 +                       break;
8323 +
8324 +               /* todo: plink too? */
8325 +               if (!do_udba)
8326 +                       continue;
8327 +
8328 +               /* UDBA tests */
8329 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8330 +                       goto err;
8331 +
8332 +               h_inode = NULL;
8333 +               if (d_is_positive(h_dentry))
8334 +                       h_inode = d_inode(h_dentry);
8335 +               h_plus = plus;
8336 +               h_mode = mode;
8337 +               h_cached_inode = h_inode;
8338 +               if (h_inode) {
8339 +                       h_mode = (h_inode->i_mode & S_IFMT);
8340 +                       h_plus = (h_inode->i_nlink > 0);
8341 +               }
8342 +               if (inode && ibs <= bindex && bindex <= ibe)
8343 +                       h_cached_inode = au_h_iptr(inode, bindex);
8344 +
8345 +               if (!h_nfs) {
8346 +                       if (unlikely(plus != h_plus && !tmpfile))
8347 +                               goto err;
8348 +               } else {
8349 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8350 +                                    && !is_root
8351 +                                    && !IS_ROOT(h_dentry)
8352 +                                    && unhashed != d_unhashed(h_dentry)))
8353 +                               goto err;
8354 +               }
8355 +               if (unlikely(mode != h_mode
8356 +                            || h_cached_inode != h_inode))
8357 +                       goto err;
8358 +               continue;
8359 +
8360 +err:
8361 +               err = -EINVAL;
8362 +               break;
8363 +       }
8364 +
8365 +       AuTraceErr(err);
8366 +       return err;
8367 +}
8368 +
8369 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8370 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8371 +{
8372 +       int err;
8373 +       struct dentry *parent;
8374 +
8375 +       if (!au_digen_test(dentry, sigen))
8376 +               return 0;
8377 +
8378 +       parent = dget_parent(dentry);
8379 +       di_read_lock_parent(parent, AuLock_IR);
8380 +       AuDebugOn(au_digen_test(parent, sigen));
8381 +       au_dbg_verify_gen(parent, sigen);
8382 +       err = au_refresh_dentry(dentry, parent);
8383 +       di_read_unlock(parent, AuLock_IR);
8384 +       dput(parent);
8385 +       AuTraceErr(err);
8386 +       return err;
8387 +}
8388 +
8389 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8390 +{
8391 +       int err;
8392 +       struct dentry *d, *parent;
8393 +
8394 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8395 +               return simple_reval_dpath(dentry, sigen);
8396 +
8397 +       /* slow loop, keep it simple and stupid */
8398 +       /* cf: au_cpup_dirs() */
8399 +       err = 0;
8400 +       parent = NULL;
8401 +       while (au_digen_test(dentry, sigen)) {
8402 +               d = dentry;
8403 +               while (1) {
8404 +                       dput(parent);
8405 +                       parent = dget_parent(d);
8406 +                       if (!au_digen_test(parent, sigen))
8407 +                               break;
8408 +                       d = parent;
8409 +               }
8410 +
8411 +               if (d != dentry)
8412 +                       di_write_lock_child2(d);
8413 +
8414 +               /* someone might update our dentry while we were sleeping */
8415 +               if (au_digen_test(d, sigen)) {
8416 +                       /*
8417 +                        * todo: consolidate with simple_reval_dpath(),
8418 +                        * do_refresh() and au_reval_for_attr().
8419 +                        */
8420 +                       di_read_lock_parent(parent, AuLock_IR);
8421 +                       err = au_refresh_dentry(d, parent);
8422 +                       di_read_unlock(parent, AuLock_IR);
8423 +               }
8424 +
8425 +               if (d != dentry)
8426 +                       di_write_unlock(d);
8427 +               dput(parent);
8428 +               if (unlikely(err))
8429 +                       break;
8430 +       }
8431 +
8432 +       return err;
8433 +}
8434 +
8435 +/*
8436 + * if valid returns 1, otherwise 0.
8437 + */
8438 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8439 +{
8440 +       int valid, err;
8441 +       unsigned int sigen;
8442 +       unsigned char do_udba;
8443 +       struct super_block *sb;
8444 +       struct inode *inode;
8445 +
8446 +       /* todo: support rcu-walk? */
8447 +       if (flags & LOOKUP_RCU)
8448 +               return -ECHILD;
8449 +
8450 +       valid = 0;
8451 +       if (unlikely(!au_di(dentry)))
8452 +               goto out;
8453 +
8454 +       valid = 1;
8455 +       sb = dentry->d_sb;
8456 +       /*
8457 +        * todo: very ugly
8458 +        * i_mutex of parent dir may be held,
8459 +        * but we should not return 'invalid' due to busy.
8460 +        */
8461 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8462 +       if (unlikely(err)) {
8463 +               valid = err;
8464 +               AuTraceErr(err);
8465 +               goto out;
8466 +       }
8467 +       inode = NULL;
8468 +       if (d_really_is_positive(dentry))
8469 +               inode = d_inode(dentry);
8470 +       if (unlikely(inode && is_bad_inode(inode))) {
8471 +               err = -EINVAL;
8472 +               AuTraceErr(err);
8473 +               goto out_dgrade;
8474 +       }
8475 +       if (unlikely(au_dbrange_test(dentry))) {
8476 +               err = -EINVAL;
8477 +               AuTraceErr(err);
8478 +               goto out_dgrade;
8479 +       }
8480 +
8481 +       sigen = au_sigen(sb);
8482 +       if (au_digen_test(dentry, sigen)) {
8483 +               AuDebugOn(IS_ROOT(dentry));
8484 +               err = au_reval_dpath(dentry, sigen);
8485 +               if (unlikely(err)) {
8486 +                       AuTraceErr(err);
8487 +                       goto out_dgrade;
8488 +               }
8489 +       }
8490 +       di_downgrade_lock(dentry, AuLock_IR);
8491 +
8492 +       err = -EINVAL;
8493 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
8494 +           && inode
8495 +           && !(inode->i_state && I_LINKABLE)
8496 +           && (IS_DEADDIR(inode) || !inode->i_nlink))
8497 +               goto out_inval;
8498 +
8499 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8500 +       if (do_udba && inode) {
8501 +               aufs_bindex_t bstart = au_ibstart(inode);
8502 +               struct inode *h_inode;
8503 +
8504 +               if (bstart >= 0) {
8505 +                       h_inode = au_h_iptr(inode, bstart);
8506 +                       if (h_inode && au_test_higen(inode, h_inode))
8507 +                               goto out_inval;
8508 +               }
8509 +       }
8510 +
8511 +       err = h_d_revalidate(dentry, inode, flags, do_udba);
8512 +       if (unlikely(!err && do_udba && au_dbstart(dentry) < 0)) {
8513 +               err = -EIO;
8514 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
8515 +                     dentry, err);
8516 +       }
8517 +       goto out_inval;
8518 +
8519 +out_dgrade:
8520 +       di_downgrade_lock(dentry, AuLock_IR);
8521 +out_inval:
8522 +       aufs_read_unlock(dentry, AuLock_IR);
8523 +       AuTraceErr(err);
8524 +       valid = !err;
8525 +out:
8526 +       if (!valid) {
8527 +               AuDbg("%pd invalid, %d\n", dentry, valid);
8528 +               d_drop(dentry);
8529 +       }
8530 +       return valid;
8531 +}
8532 +
8533 +static void aufs_d_release(struct dentry *dentry)
8534 +{
8535 +       if (au_di(dentry)) {
8536 +               au_di_fin(dentry);
8537 +               au_hn_di_reinit(dentry);
8538 +       }
8539 +}
8540 +
8541 +const struct dentry_operations aufs_dop = {
8542 +       .d_revalidate           = aufs_d_revalidate,
8543 +       .d_weak_revalidate      = aufs_d_revalidate,
8544 +       .d_release              = aufs_d_release
8545 +};
8546 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8547 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
8548 +++ linux/fs/aufs/dentry.h      2015-09-24 10:47:58.251386326 +0200
8549 @@ -0,0 +1,233 @@
8550 +/*
8551 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8552 + *
8553 + * This program, aufs is free software; you can redistribute it and/or modify
8554 + * it under the terms of the GNU General Public License as published by
8555 + * the Free Software Foundation; either version 2 of the License, or
8556 + * (at your option) any later version.
8557 + *
8558 + * This program is distributed in the hope that it will be useful,
8559 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8560 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8561 + * GNU General Public License for more details.
8562 + *
8563 + * You should have received a copy of the GNU General Public License
8564 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8565 + */
8566 +
8567 +/*
8568 + * lookup and dentry operations
8569 + */
8570 +
8571 +#ifndef __AUFS_DENTRY_H__
8572 +#define __AUFS_DENTRY_H__
8573 +
8574 +#ifdef __KERNEL__
8575 +
8576 +#include <linux/dcache.h>
8577 +#include "rwsem.h"
8578 +
8579 +struct au_hdentry {
8580 +       struct dentry           *hd_dentry;
8581 +       aufs_bindex_t           hd_id;
8582 +};
8583 +
8584 +struct au_dinfo {
8585 +       atomic_t                di_generation;
8586 +
8587 +       struct au_rwsem         di_rwsem;
8588 +       aufs_bindex_t           di_bstart, di_bend, di_bwh, di_bdiropq;
8589 +       unsigned char           di_tmpfile; /* to allow the different name */
8590 +       struct au_hdentry       *di_hdentry;
8591 +} ____cacheline_aligned_in_smp;
8592 +
8593 +/* ---------------------------------------------------------------------- */
8594 +
8595 +/* dentry.c */
8596 +extern const struct dentry_operations aufs_dop;
8597 +struct au_branch;
8598 +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
8599 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8600 +               struct dentry *h_parent, struct au_branch *br);
8601 +
8602 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t bstart, mode_t type);
8603 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
8604 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
8605 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
8606 +
8607 +/* dinfo.c */
8608 +void au_di_init_once(void *_di);
8609 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8610 +void au_di_free(struct au_dinfo *dinfo);
8611 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8612 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
8613 +int au_di_init(struct dentry *dentry);
8614 +void au_di_fin(struct dentry *dentry);
8615 +int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8616 +
8617 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8618 +void di_read_unlock(struct dentry *d, int flags);
8619 +void di_downgrade_lock(struct dentry *d, int flags);
8620 +void di_write_lock(struct dentry *d, unsigned int lsc);
8621 +void di_write_unlock(struct dentry *d);
8622 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8623 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8624 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8625 +
8626 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
8627 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
8628 +aufs_bindex_t au_dbtail(struct dentry *dentry);
8629 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8630 +
8631 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8632 +                  struct dentry *h_dentry);
8633 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
8634 +int au_dbrange_test(struct dentry *dentry);
8635 +void au_update_digen(struct dentry *dentry);
8636 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
8637 +void au_update_dbstart(struct dentry *dentry);
8638 +void au_update_dbend(struct dentry *dentry);
8639 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8640 +
8641 +/* ---------------------------------------------------------------------- */
8642 +
8643 +static inline struct au_dinfo *au_di(struct dentry *dentry)
8644 +{
8645 +       return dentry->d_fsdata;
8646 +}
8647 +
8648 +/* ---------------------------------------------------------------------- */
8649 +
8650 +/* lock subclass for dinfo */
8651 +enum {
8652 +       AuLsc_DI_CHILD,         /* child first */
8653 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
8654 +       AuLsc_DI_CHILD3,        /* copyup dirs */
8655 +       AuLsc_DI_PARENT,
8656 +       AuLsc_DI_PARENT2,
8657 +       AuLsc_DI_PARENT3,
8658 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
8659 +};
8660 +
8661 +/*
8662 + * di_read_lock_child, di_write_lock_child,
8663 + * di_read_lock_child2, di_write_lock_child2,
8664 + * di_read_lock_child3, di_write_lock_child3,
8665 + * di_read_lock_parent, di_write_lock_parent,
8666 + * di_read_lock_parent2, di_write_lock_parent2,
8667 + * di_read_lock_parent3, di_write_lock_parent3,
8668 + */
8669 +#define AuReadLockFunc(name, lsc) \
8670 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
8671 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8672 +
8673 +#define AuWriteLockFunc(name, lsc) \
8674 +static inline void di_write_lock_##name(struct dentry *d) \
8675 +{ di_write_lock(d, AuLsc_DI_##lsc); }
8676 +
8677 +#define AuRWLockFuncs(name, lsc) \
8678 +       AuReadLockFunc(name, lsc) \
8679 +       AuWriteLockFunc(name, lsc)
8680 +
8681 +AuRWLockFuncs(child, CHILD);
8682 +AuRWLockFuncs(child2, CHILD2);
8683 +AuRWLockFuncs(child3, CHILD3);
8684 +AuRWLockFuncs(parent, PARENT);
8685 +AuRWLockFuncs(parent2, PARENT2);
8686 +AuRWLockFuncs(parent3, PARENT3);
8687 +
8688 +#undef AuReadLockFunc
8689 +#undef AuWriteLockFunc
8690 +#undef AuRWLockFuncs
8691 +
8692 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
8693 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
8694 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
8695 +
8696 +/* ---------------------------------------------------------------------- */
8697 +
8698 +/* todo: memory barrier? */
8699 +static inline unsigned int au_digen(struct dentry *d)
8700 +{
8701 +       return atomic_read(&au_di(d)->di_generation);
8702 +}
8703 +
8704 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8705 +{
8706 +       hdentry->hd_dentry = NULL;
8707 +}
8708 +
8709 +static inline void au_hdput(struct au_hdentry *hd)
8710 +{
8711 +       if (hd)
8712 +               dput(hd->hd_dentry);
8713 +}
8714 +
8715 +static inline aufs_bindex_t au_dbstart(struct dentry *dentry)
8716 +{
8717 +       DiMustAnyLock(dentry);
8718 +       return au_di(dentry)->di_bstart;
8719 +}
8720 +
8721 +static inline aufs_bindex_t au_dbend(struct dentry *dentry)
8722 +{
8723 +       DiMustAnyLock(dentry);
8724 +       return au_di(dentry)->di_bend;
8725 +}
8726 +
8727 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8728 +{
8729 +       DiMustAnyLock(dentry);
8730 +       return au_di(dentry)->di_bwh;
8731 +}
8732 +
8733 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8734 +{
8735 +       DiMustAnyLock(dentry);
8736 +       return au_di(dentry)->di_bdiropq;
8737 +}
8738 +
8739 +/* todo: hard/soft set? */
8740 +static inline void au_set_dbstart(struct dentry *dentry, aufs_bindex_t bindex)
8741 +{
8742 +       DiMustWriteLock(dentry);
8743 +       au_di(dentry)->di_bstart = bindex;
8744 +}
8745 +
8746 +static inline void au_set_dbend(struct dentry *dentry, aufs_bindex_t bindex)
8747 +{
8748 +       DiMustWriteLock(dentry);
8749 +       au_di(dentry)->di_bend = bindex;
8750 +}
8751 +
8752 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
8753 +{
8754 +       DiMustWriteLock(dentry);
8755 +       /* dbwh can be outside of bstart - bend range */
8756 +       au_di(dentry)->di_bwh = bindex;
8757 +}
8758 +
8759 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
8760 +{
8761 +       DiMustWriteLock(dentry);
8762 +       au_di(dentry)->di_bdiropq = bindex;
8763 +}
8764 +
8765 +/* ---------------------------------------------------------------------- */
8766 +
8767 +#ifdef CONFIG_AUFS_HNOTIFY
8768 +static inline void au_digen_dec(struct dentry *d)
8769 +{
8770 +       atomic_dec(&au_di(d)->di_generation);
8771 +}
8772 +
8773 +static inline void au_hn_di_reinit(struct dentry *dentry)
8774 +{
8775 +       dentry->d_fsdata = NULL;
8776 +}
8777 +#else
8778 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
8779 +#endif /* CONFIG_AUFS_HNOTIFY */
8780 +
8781 +#endif /* __KERNEL__ */
8782 +#endif /* __AUFS_DENTRY_H__ */
8783 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
8784 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
8785 +++ linux/fs/aufs/dinfo.c       2015-09-24 10:47:58.251386326 +0200
8786 @@ -0,0 +1,550 @@
8787 +/*
8788 + * Copyright (C) 2005-2015 Junjiro R. Okajima
8789 + *
8790 + * This program, aufs is free software; you can redistribute it and/or modify
8791 + * it under the terms of the GNU General Public License as published by
8792 + * the Free Software Foundation; either version 2 of the License, or
8793 + * (at your option) any later version.
8794 + *
8795 + * This program is distributed in the hope that it will be useful,
8796 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8797 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8798 + * GNU General Public License for more details.
8799 + *
8800 + * You should have received a copy of the GNU General Public License
8801 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8802 + */
8803 +
8804 +/*
8805 + * dentry private data
8806 + */
8807 +
8808 +#include "aufs.h"
8809 +
8810 +void au_di_init_once(void *_dinfo)
8811 +{
8812 +       struct au_dinfo *dinfo = _dinfo;
8813 +       static struct lock_class_key aufs_di;
8814 +
8815 +       au_rw_init(&dinfo->di_rwsem);
8816 +       au_rw_class(&dinfo->di_rwsem, &aufs_di);
8817 +}
8818 +
8819 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
8820 +{
8821 +       struct au_dinfo *dinfo;
8822 +       int nbr, i;
8823 +
8824 +       dinfo = au_cache_alloc_dinfo();
8825 +       if (unlikely(!dinfo))
8826 +               goto out;
8827 +
8828 +       nbr = au_sbend(sb) + 1;
8829 +       if (nbr <= 0)
8830 +               nbr = 1;
8831 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
8832 +       if (dinfo->di_hdentry) {
8833 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
8834 +               dinfo->di_bstart = -1;
8835 +               dinfo->di_bend = -1;
8836 +               dinfo->di_bwh = -1;
8837 +               dinfo->di_bdiropq = -1;
8838 +               dinfo->di_tmpfile = 0;
8839 +               for (i = 0; i < nbr; i++)
8840 +                       dinfo->di_hdentry[i].hd_id = -1;
8841 +               goto out;
8842 +       }
8843 +
8844 +       au_cache_free_dinfo(dinfo);
8845 +       dinfo = NULL;
8846 +
8847 +out:
8848 +       return dinfo;
8849 +}
8850 +
8851 +void au_di_free(struct au_dinfo *dinfo)
8852 +{
8853 +       struct au_hdentry *p;
8854 +       aufs_bindex_t bend, bindex;
8855 +
8856 +       /* dentry may not be revalidated */
8857 +       bindex = dinfo->di_bstart;
8858 +       if (bindex >= 0) {
8859 +               bend = dinfo->di_bend;
8860 +               p = dinfo->di_hdentry + bindex;
8861 +               while (bindex++ <= bend)
8862 +                       au_hdput(p++);
8863 +       }
8864 +       kfree(dinfo->di_hdentry);
8865 +       au_cache_free_dinfo(dinfo);
8866 +}
8867 +
8868 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
8869 +{
8870 +       struct au_hdentry *p;
8871 +       aufs_bindex_t bi;
8872 +
8873 +       AuRwMustWriteLock(&a->di_rwsem);
8874 +       AuRwMustWriteLock(&b->di_rwsem);
8875 +
8876 +#define DiSwap(v, name)                                \
8877 +       do {                                    \
8878 +               v = a->di_##name;               \
8879 +               a->di_##name = b->di_##name;    \
8880 +               b->di_##name = v;               \
8881 +       } while (0)
8882 +
8883 +       DiSwap(p, hdentry);
8884 +       DiSwap(bi, bstart);
8885 +       DiSwap(bi, bend);
8886 +       DiSwap(bi, bwh);
8887 +       DiSwap(bi, bdiropq);
8888 +       /* smp_mb(); */
8889 +
8890 +#undef DiSwap
8891 +}
8892 +
8893 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
8894 +{
8895 +       AuRwMustWriteLock(&dst->di_rwsem);
8896 +       AuRwMustWriteLock(&src->di_rwsem);
8897 +
8898 +       dst->di_bstart = src->di_bstart;
8899 +       dst->di_bend = src->di_bend;
8900 +       dst->di_bwh = src->di_bwh;
8901 +       dst->di_bdiropq = src->di_bdiropq;
8902 +       /* smp_mb(); */
8903 +}
8904 +
8905 +int au_di_init(struct dentry *dentry)
8906 +{
8907 +       int err;
8908 +       struct super_block *sb;
8909 +       struct au_dinfo *dinfo;
8910 +
8911 +       err = 0;
8912 +       sb = dentry->d_sb;
8913 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
8914 +       if (dinfo) {
8915 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
8916 +               /* smp_mb(); */ /* atomic_set */
8917 +               dentry->d_fsdata = dinfo;
8918 +       } else
8919 +               err = -ENOMEM;
8920 +
8921 +       return err;
8922 +}
8923 +
8924 +void au_di_fin(struct dentry *dentry)
8925 +{
8926 +       struct au_dinfo *dinfo;
8927 +
8928 +       dinfo = au_di(dentry);
8929 +       AuRwDestroy(&dinfo->di_rwsem);
8930 +       au_di_free(dinfo);
8931 +}
8932 +
8933 +int au_di_realloc(struct au_dinfo *dinfo, int nbr)
8934 +{
8935 +       int err, sz;
8936 +       struct au_hdentry *hdp;
8937 +
8938 +       AuRwMustWriteLock(&dinfo->di_rwsem);
8939 +
8940 +       err = -ENOMEM;
8941 +       sz = sizeof(*hdp) * (dinfo->di_bend + 1);
8942 +       if (!sz)
8943 +               sz = sizeof(*hdp);
8944 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
8945 +       if (hdp) {
8946 +               dinfo->di_hdentry = hdp;
8947 +               err = 0;
8948 +       }
8949 +
8950 +       return err;
8951 +}
8952 +
8953 +/* ---------------------------------------------------------------------- */
8954 +
8955 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
8956 +{
8957 +       switch (lsc) {
8958 +       case AuLsc_DI_CHILD:
8959 +               ii_write_lock_child(inode);
8960 +               break;
8961 +       case AuLsc_DI_CHILD2:
8962 +               ii_write_lock_child2(inode);
8963 +               break;
8964 +       case AuLsc_DI_CHILD3:
8965 +               ii_write_lock_child3(inode);
8966 +               break;
8967 +       case AuLsc_DI_PARENT:
8968 +               ii_write_lock_parent(inode);
8969 +               break;
8970 +       case AuLsc_DI_PARENT2:
8971 +               ii_write_lock_parent2(inode);
8972 +               break;
8973 +       case AuLsc_DI_PARENT3:
8974 +               ii_write_lock_parent3(inode);
8975 +               break;
8976 +       default:
8977 +               BUG();
8978 +       }
8979 +}
8980 +
8981 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
8982 +{
8983 +       switch (lsc) {
8984 +       case AuLsc_DI_CHILD:
8985 +               ii_read_lock_child(inode);
8986 +               break;
8987 +       case AuLsc_DI_CHILD2:
8988 +               ii_read_lock_child2(inode);
8989 +               break;
8990 +       case AuLsc_DI_CHILD3:
8991 +               ii_read_lock_child3(inode);
8992 +               break;
8993 +       case AuLsc_DI_PARENT:
8994 +               ii_read_lock_parent(inode);
8995 +               break;
8996 +       case AuLsc_DI_PARENT2:
8997 +               ii_read_lock_parent2(inode);
8998 +               break;
8999 +       case AuLsc_DI_PARENT3:
9000 +               ii_read_lock_parent3(inode);
9001 +               break;
9002 +       default:
9003 +               BUG();
9004 +       }
9005 +}
9006 +
9007 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9008 +{
9009 +       struct inode *inode;
9010 +
9011 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9012 +       if (d_really_is_positive(d)) {
9013 +               inode = d_inode(d);
9014 +               if (au_ftest_lock(flags, IW))
9015 +                       do_ii_write_lock(inode, lsc);
9016 +               else if (au_ftest_lock(flags, IR))
9017 +                       do_ii_read_lock(inode, lsc);
9018 +       }
9019 +}
9020 +
9021 +void di_read_unlock(struct dentry *d, int flags)
9022 +{
9023 +       struct inode *inode;
9024 +
9025 +       if (d_really_is_positive(d)) {
9026 +               inode = d_inode(d);
9027 +               if (au_ftest_lock(flags, IW)) {
9028 +                       au_dbg_verify_dinode(d);
9029 +                       ii_write_unlock(inode);
9030 +               } else if (au_ftest_lock(flags, IR)) {
9031 +                       au_dbg_verify_dinode(d);
9032 +                       ii_read_unlock(inode);
9033 +               }
9034 +       }
9035 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9036 +}
9037 +
9038 +void di_downgrade_lock(struct dentry *d, int flags)
9039 +{
9040 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9041 +               ii_downgrade_lock(d_inode(d));
9042 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9043 +}
9044 +
9045 +void di_write_lock(struct dentry *d, unsigned int lsc)
9046 +{
9047 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9048 +       if (d_really_is_positive(d))
9049 +               do_ii_write_lock(d_inode(d), lsc);
9050 +}
9051 +
9052 +void di_write_unlock(struct dentry *d)
9053 +{
9054 +       au_dbg_verify_dinode(d);
9055 +       if (d_really_is_positive(d))
9056 +               ii_write_unlock(d_inode(d));
9057 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9058 +}
9059 +
9060 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9061 +{
9062 +       AuDebugOn(d1 == d2
9063 +                 || d_inode(d1) == d_inode(d2)
9064 +                 || d1->d_sb != d2->d_sb);
9065 +
9066 +       if (isdir && au_test_subdir(d1, d2)) {
9067 +               di_write_lock_child(d1);
9068 +               di_write_lock_child2(d2);
9069 +       } else {
9070 +               /* there should be no races */
9071 +               di_write_lock_child(d2);
9072 +               di_write_lock_child2(d1);
9073 +       }
9074 +}
9075 +
9076 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9077 +{
9078 +       AuDebugOn(d1 == d2
9079 +                 || d_inode(d1) == d_inode(d2)
9080 +                 || d1->d_sb != d2->d_sb);
9081 +
9082 +       if (isdir && au_test_subdir(d1, d2)) {
9083 +               di_write_lock_parent(d1);
9084 +               di_write_lock_parent2(d2);
9085 +       } else {
9086 +               /* there should be no races */
9087 +               di_write_lock_parent(d2);
9088 +               di_write_lock_parent2(d1);
9089 +       }
9090 +}
9091 +
9092 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9093 +{
9094 +       di_write_unlock(d1);
9095 +       if (d_inode(d1) == d_inode(d2))
9096 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9097 +       else
9098 +               di_write_unlock(d2);
9099 +}
9100 +
9101 +/* ---------------------------------------------------------------------- */
9102 +
9103 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9104 +{
9105 +       struct dentry *d;
9106 +
9107 +       DiMustAnyLock(dentry);
9108 +
9109 +       if (au_dbstart(dentry) < 0 || bindex < au_dbstart(dentry))
9110 +               return NULL;
9111 +       AuDebugOn(bindex < 0);
9112 +       d = au_di(dentry)->di_hdentry[0 + bindex].hd_dentry;
9113 +       AuDebugOn(d && au_dcount(d) <= 0);
9114 +       return d;
9115 +}
9116 +
9117 +/*
9118 + * extended version of au_h_dptr().
9119 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9120 + * error.
9121 + */
9122 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9123 +{
9124 +       struct dentry *h_dentry;
9125 +       struct inode *inode, *h_inode;
9126 +
9127 +       AuDebugOn(d_really_is_negative(dentry));
9128 +
9129 +       h_dentry = NULL;
9130 +       if (au_dbstart(dentry) <= bindex
9131 +           && bindex <= au_dbend(dentry))
9132 +               h_dentry = au_h_dptr(dentry, bindex);
9133 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9134 +               dget(h_dentry);
9135 +               goto out; /* success */
9136 +       }
9137 +
9138 +       inode = d_inode(dentry);
9139 +       AuDebugOn(bindex < au_ibstart(inode));
9140 +       AuDebugOn(au_ibend(inode) < bindex);
9141 +       h_inode = au_h_iptr(inode, bindex);
9142 +       h_dentry = d_find_alias(h_inode);
9143 +       if (h_dentry) {
9144 +               if (!IS_ERR(h_dentry)) {
9145 +                       if (!au_d_linkable(h_dentry))
9146 +                               goto out; /* success */
9147 +                       dput(h_dentry);
9148 +               } else
9149 +                       goto out;
9150 +       }
9151 +
9152 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9153 +               h_dentry = au_plink_lkup(inode, bindex);
9154 +               AuDebugOn(!h_dentry);
9155 +               if (!IS_ERR(h_dentry)) {
9156 +                       if (!au_d_hashed_positive(h_dentry))
9157 +                               goto out; /* success */
9158 +                       dput(h_dentry);
9159 +                       h_dentry = NULL;
9160 +               }
9161 +       }
9162 +
9163 +out:
9164 +       AuDbgDentry(h_dentry);
9165 +       return h_dentry;
9166 +}
9167 +
9168 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9169 +{
9170 +       aufs_bindex_t bend, bwh;
9171 +
9172 +       bend = au_dbend(dentry);
9173 +       if (0 <= bend) {
9174 +               bwh = au_dbwh(dentry);
9175 +               if (!bwh)
9176 +                       return bwh;
9177 +               if (0 < bwh && bwh < bend)
9178 +                       return bwh - 1;
9179 +       }
9180 +       return bend;
9181 +}
9182 +
9183 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9184 +{
9185 +       aufs_bindex_t bend, bopq;
9186 +
9187 +       bend = au_dbtail(dentry);
9188 +       if (0 <= bend) {
9189 +               bopq = au_dbdiropq(dentry);
9190 +               if (0 <= bopq && bopq < bend)
9191 +                       bend = bopq;
9192 +       }
9193 +       return bend;
9194 +}
9195 +
9196 +/* ---------------------------------------------------------------------- */
9197 +
9198 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9199 +                  struct dentry *h_dentry)
9200 +{
9201 +       struct au_hdentry *hd = au_di(dentry)->di_hdentry + bindex;
9202 +       struct au_branch *br;
9203 +
9204 +       DiMustWriteLock(dentry);
9205 +
9206 +       au_hdput(hd);
9207 +       hd->hd_dentry = h_dentry;
9208 +       if (h_dentry) {
9209 +               br = au_sbr(dentry->d_sb, bindex);
9210 +               hd->hd_id = br->br_id;
9211 +       }
9212 +}
9213 +
9214 +int au_dbrange_test(struct dentry *dentry)
9215 +{
9216 +       int err;
9217 +       aufs_bindex_t bstart, bend;
9218 +
9219 +       err = 0;
9220 +       bstart = au_dbstart(dentry);
9221 +       bend = au_dbend(dentry);
9222 +       if (bstart >= 0)
9223 +               AuDebugOn(bend < 0 && bstart > bend);
9224 +       else {
9225 +               err = -EIO;
9226 +               AuDebugOn(bend >= 0);
9227 +       }
9228 +
9229 +       return err;
9230 +}
9231 +
9232 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9233 +{
9234 +       int err;
9235 +
9236 +       err = 0;
9237 +       if (unlikely(au_digen(dentry) != sigen
9238 +                    || au_iigen_test(d_inode(dentry), sigen)))
9239 +               err = -EIO;
9240 +
9241 +       return err;
9242 +}
9243 +
9244 +void au_update_digen(struct dentry *dentry)
9245 +{
9246 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9247 +       /* smp_mb(); */ /* atomic_set */
9248 +}
9249 +
9250 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9251 +{
9252 +       struct au_dinfo *dinfo;
9253 +       struct dentry *h_d;
9254 +       struct au_hdentry *hdp;
9255 +
9256 +       DiMustWriteLock(dentry);
9257 +
9258 +       dinfo = au_di(dentry);
9259 +       if (!dinfo || dinfo->di_bstart < 0)
9260 +               return;
9261 +
9262 +       hdp = dinfo->di_hdentry;
9263 +       if (do_put_zero) {
9264 +               aufs_bindex_t bindex, bend;
9265 +
9266 +               bend = dinfo->di_bend;
9267 +               for (bindex = dinfo->di_bstart; bindex <= bend; bindex++) {
9268 +                       h_d = hdp[0 + bindex].hd_dentry;
9269 +                       if (h_d && d_is_negative(h_d))
9270 +                               au_set_h_dptr(dentry, bindex, NULL);
9271 +               }
9272 +       }
9273 +
9274 +       dinfo->di_bstart = -1;
9275 +       while (++dinfo->di_bstart <= dinfo->di_bend)
9276 +               if (hdp[0 + dinfo->di_bstart].hd_dentry)
9277 +                       break;
9278 +       if (dinfo->di_bstart > dinfo->di_bend) {
9279 +               dinfo->di_bstart = -1;
9280 +               dinfo->di_bend = -1;
9281 +               return;
9282 +       }
9283 +
9284 +       dinfo->di_bend++;
9285 +       while (0 <= --dinfo->di_bend)
9286 +               if (hdp[0 + dinfo->di_bend].hd_dentry)
9287 +                       break;
9288 +       AuDebugOn(dinfo->di_bstart > dinfo->di_bend || dinfo->di_bend < 0);
9289 +}
9290 +
9291 +void au_update_dbstart(struct dentry *dentry)
9292 +{
9293 +       aufs_bindex_t bindex, bend;
9294 +       struct dentry *h_dentry;
9295 +
9296 +       bend = au_dbend(dentry);
9297 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
9298 +               h_dentry = au_h_dptr(dentry, bindex);
9299 +               if (!h_dentry)
9300 +                       continue;
9301 +               if (d_is_positive(h_dentry)) {
9302 +                       au_set_dbstart(dentry, bindex);
9303 +                       return;
9304 +               }
9305 +               au_set_h_dptr(dentry, bindex, NULL);
9306 +       }
9307 +}
9308 +
9309 +void au_update_dbend(struct dentry *dentry)
9310 +{
9311 +       aufs_bindex_t bindex, bstart;
9312 +       struct dentry *h_dentry;
9313 +
9314 +       bstart = au_dbstart(dentry);
9315 +       for (bindex = au_dbend(dentry); bindex >= bstart; bindex--) {
9316 +               h_dentry = au_h_dptr(dentry, bindex);
9317 +               if (!h_dentry)
9318 +                       continue;
9319 +               if (d_is_positive(h_dentry)) {
9320 +                       au_set_dbend(dentry, bindex);
9321 +                       return;
9322 +               }
9323 +               au_set_h_dptr(dentry, bindex, NULL);
9324 +       }
9325 +}
9326 +
9327 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9328 +{
9329 +       aufs_bindex_t bindex, bend;
9330 +
9331 +       bend = au_dbend(dentry);
9332 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++)
9333 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9334 +                       return bindex;
9335 +       return -1;
9336 +}
9337 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9338 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9339 +++ linux/fs/aufs/dir.c 2015-09-24 10:47:58.251386326 +0200
9340 @@ -0,0 +1,753 @@
9341 +/*
9342 + * Copyright (C) 2005-2015 Junjiro R. Okajima
9343 + *
9344 + * This program, aufs is free software; you can redistribute it and/or modify
9345 + * it under the terms of the GNU General Public License as published by
9346 + * the Free Software Foundation; either version 2 of the License, or
9347 + * (at your option) any later version.
9348 + *
9349 + * This program is distributed in the hope that it will be useful,
9350 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9351 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9352 + * GNU General Public License for more details.
9353 + *
9354 + * You should have received a copy of the GNU General Public License
9355 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9356 + */
9357 +
9358 +/*
9359 + * directory operations
9360 + */
9361 +
9362 +#include <linux/fs_stack.h>
9363 +#include "aufs.h"
9364 +
9365 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9366 +{
9367 +       unsigned int nlink;
9368 +
9369 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9370 +
9371 +       nlink = dir->i_nlink;
9372 +       nlink += h_dir->i_nlink - 2;
9373 +       if (h_dir->i_nlink < 2)
9374 +               nlink += 2;
9375 +       smp_mb(); /* for i_nlink */
9376 +       /* 0 can happen in revaliding */
9377 +       set_nlink(dir, nlink);
9378 +}
9379 +
9380 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9381 +{
9382 +       unsigned int nlink;
9383 +
9384 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9385 +
9386 +       nlink = dir->i_nlink;
9387 +       nlink -= h_dir->i_nlink - 2;
9388 +       if (h_dir->i_nlink < 2)
9389 +               nlink -= 2;
9390 +       smp_mb(); /* for i_nlink */
9391 +       /* nlink == 0 means the branch-fs is broken */
9392 +       set_nlink(dir, nlink);
9393 +}
9394 +
9395 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9396 +{
9397 +       loff_t sz;
9398 +       aufs_bindex_t bindex, bend;
9399 +       struct file *h_file;
9400 +       struct dentry *h_dentry;
9401 +
9402 +       sz = 0;
9403 +       if (file) {
9404 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
9405 +
9406 +               bend = au_fbend_dir(file);
9407 +               for (bindex = au_fbstart(file);
9408 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9409 +                    bindex++) {
9410 +                       h_file = au_hf_dir(file, bindex);
9411 +                       if (h_file && file_inode(h_file))
9412 +                               sz += vfsub_f_size_read(h_file);
9413 +               }
9414 +       } else {
9415 +               AuDebugOn(!dentry);
9416 +               AuDebugOn(!d_is_dir(dentry));
9417 +
9418 +               bend = au_dbtaildir(dentry);
9419 +               for (bindex = au_dbstart(dentry);
9420 +                    bindex <= bend && sz < KMALLOC_MAX_SIZE;
9421 +                    bindex++) {
9422 +                       h_dentry = au_h_dptr(dentry, bindex);
9423 +                       if (h_dentry && d_is_positive(h_dentry))
9424 +                               sz += i_size_read(d_inode(h_dentry));
9425 +               }
9426 +       }
9427 +       if (sz < KMALLOC_MAX_SIZE)
9428 +               sz = roundup_pow_of_two(sz);
9429 +       if (sz > KMALLOC_MAX_SIZE)
9430 +               sz = KMALLOC_MAX_SIZE;
9431 +       else if (sz < NAME_MAX) {
9432 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9433 +               sz = AUFS_RDBLK_DEF;
9434 +       }
9435 +       return sz;
9436 +}
9437 +
9438 +struct au_dir_ts_arg {
9439 +       struct dentry *dentry;
9440 +       aufs_bindex_t brid;
9441 +};
9442 +
9443 +static void au_do_dir_ts(void *arg)
9444 +{
9445 +       struct au_dir_ts_arg *a = arg;
9446 +       struct au_dtime dt;
9447 +       struct path h_path;
9448 +       struct inode *dir, *h_dir;
9449 +       struct super_block *sb;
9450 +       struct au_branch *br;
9451 +       struct au_hinode *hdir;
9452 +       int err;
9453 +       aufs_bindex_t bstart, bindex;
9454 +
9455 +       sb = a->dentry->d_sb;
9456 +       if (d_really_is_negative(a->dentry))
9457 +               goto out;
9458 +       aufs_read_lock(a->dentry, AuLock_DW | AuLock_DIR); /* noflush */
9459 +
9460 +       /* no dir->i_mutex lock */
9461 +       dir = d_inode(a->dentry);
9462 +       bstart = au_ibstart(dir);
9463 +       bindex = au_br_index(sb, a->brid);
9464 +       if (bindex < bstart)
9465 +               goto out_unlock;
9466 +
9467 +       br = au_sbr(sb, bindex);
9468 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
9469 +       if (!h_path.dentry)
9470 +               goto out_unlock;
9471 +       h_path.mnt = au_br_mnt(br);
9472 +       au_dtime_store(&dt, a->dentry, &h_path);
9473 +
9474 +       br = au_sbr(sb, bstart);
9475 +       if (!au_br_writable(br->br_perm))
9476 +               goto out_unlock;
9477 +       h_path.dentry = au_h_dptr(a->dentry, bstart);
9478 +       h_path.mnt = au_br_mnt(br);
9479 +       err = vfsub_mnt_want_write(h_path.mnt);
9480 +       if (err)
9481 +               goto out_unlock;
9482 +       hdir = au_hi(dir, bstart);
9483 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
9484 +       h_dir = au_h_iptr(dir, bstart);
9485 +       if (h_dir->i_nlink
9486 +           && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9487 +               dt.dt_h_path = h_path;
9488 +               au_dtime_revert(&dt);
9489 +       }
9490 +       au_hn_imtx_unlock(hdir);
9491 +       vfsub_mnt_drop_write(h_path.mnt);
9492 +       au_cpup_attr_timesizes(dir);
9493 +
9494 +out_unlock:
9495 +       aufs_read_unlock(a->dentry, AuLock_DW);
9496 +out:
9497 +       dput(a->dentry);
9498 +       au_nwt_done(&au_sbi(sb)->si_nowait);
9499 +       kfree(arg);
9500 +}
9501 +
9502 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9503 +{
9504 +       int perm, wkq_err;
9505 +       aufs_bindex_t bstart;
9506 +       struct au_dir_ts_arg *arg;
9507 +       struct dentry *dentry;
9508 +       struct super_block *sb;
9509 +
9510 +       IMustLock(dir);
9511 +
9512 +       dentry = d_find_any_alias(dir);
9513 +       AuDebugOn(!dentry);
9514 +       sb = dentry->d_sb;
9515 +       bstart = au_ibstart(dir);
9516 +       if (bstart == bindex) {
9517 +               au_cpup_attr_timesizes(dir);
9518 +               goto out;
9519 +       }
9520 +
9521 +       perm = au_sbr_perm(sb, bstart);
9522 +       if (!au_br_writable(perm))
9523 +               goto out;
9524 +
9525 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
9526 +       if (!arg)
9527 +               goto out;
9528 +
9529 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9530 +       arg->brid = au_sbr_id(sb, bindex);
9531 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9532 +       if (unlikely(wkq_err)) {
9533 +               pr_err("wkq %d\n", wkq_err);
9534 +               dput(dentry);
9535 +               kfree(arg);
9536 +       }
9537 +
9538 +out:
9539 +       dput(dentry);
9540 +}
9541 +
9542 +/* ---------------------------------------------------------------------- */
9543 +
9544 +static int reopen_dir(struct file *file)
9545 +{
9546 +       int err;
9547 +       unsigned int flags;
9548 +       aufs_bindex_t bindex, btail, bstart;
9549 +       struct dentry *dentry, *h_dentry;
9550 +       struct file *h_file;
9551 +
9552 +       /* open all lower dirs */
9553 +       dentry = file->f_path.dentry;
9554 +       bstart = au_dbstart(dentry);
9555 +       for (bindex = au_fbstart(file); bindex < bstart; bindex++)
9556 +               au_set_h_fptr(file, bindex, NULL);
9557 +       au_set_fbstart(file, bstart);
9558 +
9559 +       btail = au_dbtaildir(dentry);
9560 +       for (bindex = au_fbend_dir(file); btail < bindex; bindex--)
9561 +               au_set_h_fptr(file, bindex, NULL);
9562 +       au_set_fbend_dir(file, btail);
9563 +
9564 +       flags = vfsub_file_flags(file);
9565 +       for (bindex = bstart; bindex <= btail; bindex++) {
9566 +               h_dentry = au_h_dptr(dentry, bindex);
9567 +               if (!h_dentry)
9568 +                       continue;
9569 +               h_file = au_hf_dir(file, bindex);
9570 +               if (h_file)
9571 +                       continue;
9572 +
9573 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9574 +               err = PTR_ERR(h_file);
9575 +               if (IS_ERR(h_file))
9576 +                       goto out; /* close all? */
9577 +               au_set_h_fptr(file, bindex, h_file);
9578 +       }
9579 +       au_update_figen(file);
9580 +       /* todo: necessary? */
9581 +       /* file->f_ra = h_file->f_ra; */
9582 +       err = 0;
9583 +
9584 +out:
9585 +       return err;
9586 +}
9587 +
9588 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
9589 +{
9590 +       int err;
9591 +       aufs_bindex_t bindex, btail;
9592 +       struct dentry *dentry, *h_dentry;
9593 +
9594 +       FiMustWriteLock(file);
9595 +       AuDebugOn(h_file);
9596 +
9597 +       err = 0;
9598 +       dentry = file->f_path.dentry;
9599 +       file->f_version = d_inode(dentry)->i_version;
9600 +       bindex = au_dbstart(dentry);
9601 +       au_set_fbstart(file, bindex);
9602 +       btail = au_dbtaildir(dentry);
9603 +       au_set_fbend_dir(file, btail);
9604 +       for (; !err && bindex <= btail; bindex++) {
9605 +               h_dentry = au_h_dptr(dentry, bindex);
9606 +               if (!h_dentry)
9607 +                       continue;
9608 +
9609 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
9610 +               if (IS_ERR(h_file)) {
9611 +                       err = PTR_ERR(h_file);
9612 +                       break;
9613 +               }
9614 +               au_set_h_fptr(file, bindex, h_file);
9615 +       }
9616 +       au_update_figen(file);
9617 +       /* todo: necessary? */
9618 +       /* file->f_ra = h_file->f_ra; */
9619 +       if (!err)
9620 +               return 0; /* success */
9621 +
9622 +       /* close all */
9623 +       for (bindex = au_fbstart(file); bindex <= btail; bindex++)
9624 +               au_set_h_fptr(file, bindex, NULL);
9625 +       au_set_fbstart(file, -1);
9626 +       au_set_fbend_dir(file, -1);
9627 +
9628 +       return err;
9629 +}
9630 +
9631 +static int aufs_open_dir(struct inode *inode __maybe_unused,
9632 +                        struct file *file)
9633 +{
9634 +       int err;
9635 +       struct super_block *sb;
9636 +       struct au_fidir *fidir;
9637 +
9638 +       err = -ENOMEM;
9639 +       sb = file->f_path.dentry->d_sb;
9640 +       si_read_lock(sb, AuLock_FLUSH);
9641 +       fidir = au_fidir_alloc(sb);
9642 +       if (fidir) {
9643 +               struct au_do_open_args args = {
9644 +                       .open   = do_open_dir,
9645 +                       .fidir  = fidir
9646 +               };
9647 +               err = au_do_open(file, &args);
9648 +               if (unlikely(err))
9649 +                       kfree(fidir);
9650 +       }
9651 +       si_read_unlock(sb);
9652 +       return err;
9653 +}
9654 +
9655 +static int aufs_release_dir(struct inode *inode __maybe_unused,
9656 +                           struct file *file)
9657 +{
9658 +       struct au_vdir *vdir_cache;
9659 +       struct au_finfo *finfo;
9660 +       struct au_fidir *fidir;
9661 +       aufs_bindex_t bindex, bend;
9662 +
9663 +       finfo = au_fi(file);
9664 +       fidir = finfo->fi_hdir;
9665 +       if (fidir) {
9666 +               au_sphl_del(&finfo->fi_hlist,
9667 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
9668 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9669 +               if (vdir_cache)
9670 +                       au_vdir_free(vdir_cache);
9671 +
9672 +               bindex = finfo->fi_btop;
9673 +               if (bindex >= 0) {
9674 +                       /*
9675 +                        * calls fput() instead of filp_close(),
9676 +                        * since no dnotify or lock for the lower file.
9677 +                        */
9678 +                       bend = fidir->fd_bbot;
9679 +                       for (; bindex <= bend; bindex++)
9680 +                               au_set_h_fptr(file, bindex, NULL);
9681 +               }
9682 +               kfree(fidir);
9683 +               finfo->fi_hdir = NULL;
9684 +       }
9685 +       au_finfo_fin(file);
9686 +       return 0;
9687 +}
9688 +
9689 +/* ---------------------------------------------------------------------- */
9690 +
9691 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
9692 +{
9693 +       int err;
9694 +       aufs_bindex_t bindex, bend;
9695 +       struct file *h_file;
9696 +
9697 +       err = 0;
9698 +       bend = au_fbend_dir(file);
9699 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9700 +               h_file = au_hf_dir(file, bindex);
9701 +               if (h_file)
9702 +                       err = vfsub_flush(h_file, id);
9703 +       }
9704 +       return err;
9705 +}
9706 +
9707 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
9708 +{
9709 +       return au_do_flush(file, id, au_do_flush_dir);
9710 +}
9711 +
9712 +/* ---------------------------------------------------------------------- */
9713 +
9714 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9715 +{
9716 +       int err;
9717 +       aufs_bindex_t bend, bindex;
9718 +       struct inode *inode;
9719 +       struct super_block *sb;
9720 +
9721 +       err = 0;
9722 +       sb = dentry->d_sb;
9723 +       inode = d_inode(dentry);
9724 +       IMustLock(inode);
9725 +       bend = au_dbend(dentry);
9726 +       for (bindex = au_dbstart(dentry); !err && bindex <= bend; bindex++) {
9727 +               struct path h_path;
9728 +
9729 +               if (au_test_ro(sb, bindex, inode))
9730 +                       continue;
9731 +               h_path.dentry = au_h_dptr(dentry, bindex);
9732 +               if (!h_path.dentry)
9733 +                       continue;
9734 +
9735 +               h_path.mnt = au_sbr_mnt(sb, bindex);
9736 +               err = vfsub_fsync(NULL, &h_path, datasync);
9737 +       }
9738 +
9739 +       return err;
9740 +}
9741 +
9742 +static int au_do_fsync_dir(struct file *file, int datasync)
9743 +{
9744 +       int err;
9745 +       aufs_bindex_t bend, bindex;
9746 +       struct file *h_file;
9747 +       struct super_block *sb;
9748 +       struct inode *inode;
9749 +
9750 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9751 +       if (unlikely(err))
9752 +               goto out;
9753 +
9754 +       inode = file_inode(file);
9755 +       sb = inode->i_sb;
9756 +       bend = au_fbend_dir(file);
9757 +       for (bindex = au_fbstart(file); !err && bindex <= bend; bindex++) {
9758 +               h_file = au_hf_dir(file, bindex);
9759 +               if (!h_file || au_test_ro(sb, bindex, inode))
9760 +                       continue;
9761 +
9762 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
9763 +       }
9764 +
9765 +out:
9766 +       return err;
9767 +}
9768 +
9769 +/*
9770 + * @file may be NULL
9771 + */
9772 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
9773 +                         int datasync)
9774 +{
9775 +       int err;
9776 +       struct dentry *dentry;
9777 +       struct inode *inode;
9778 +       struct super_block *sb;
9779 +       struct mutex *mtx;
9780 +
9781 +       err = 0;
9782 +       dentry = file->f_path.dentry;
9783 +       inode = d_inode(dentry);
9784 +       mtx = &inode->i_mutex;
9785 +       mutex_lock(mtx);
9786 +       sb = dentry->d_sb;
9787 +       si_noflush_read_lock(sb);
9788 +       if (file)
9789 +               err = au_do_fsync_dir(file, datasync);
9790 +       else {
9791 +               di_write_lock_child(dentry);
9792 +               err = au_do_fsync_dir_no_file(dentry, datasync);
9793 +       }
9794 +       au_cpup_attr_timesizes(inode);
9795 +       di_write_unlock(dentry);
9796 +       if (file)
9797 +               fi_write_unlock(file);
9798 +
9799 +       si_read_unlock(sb);
9800 +       mutex_unlock(mtx);
9801 +       return err;
9802 +}
9803 +
9804 +/* ---------------------------------------------------------------------- */
9805 +
9806 +static int aufs_iterate(struct file *file, struct dir_context *ctx)
9807 +{
9808 +       int err;
9809 +       struct dentry *dentry;
9810 +       struct inode *inode, *h_inode;
9811 +       struct super_block *sb;
9812 +
9813 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
9814 +
9815 +       dentry = file->f_path.dentry;
9816 +       inode = d_inode(dentry);
9817 +       IMustLock(inode);
9818 +
9819 +       sb = dentry->d_sb;
9820 +       si_read_lock(sb, AuLock_FLUSH);
9821 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
9822 +       if (unlikely(err))
9823 +               goto out;
9824 +       err = au_alive_dir(dentry);
9825 +       if (!err)
9826 +               err = au_vdir_init(file);
9827 +       di_downgrade_lock(dentry, AuLock_IR);
9828 +       if (unlikely(err))
9829 +               goto out_unlock;
9830 +
9831 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
9832 +       if (!au_test_nfsd()) {
9833 +               err = au_vdir_fill_de(file, ctx);
9834 +               fsstack_copy_attr_atime(inode, h_inode);
9835 +       } else {
9836 +               /*
9837 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
9838 +                * encode_fh() and others.
9839 +                */
9840 +               atomic_inc(&h_inode->i_count);
9841 +               di_read_unlock(dentry, AuLock_IR);
9842 +               si_read_unlock(sb);
9843 +               err = au_vdir_fill_de(file, ctx);
9844 +               fsstack_copy_attr_atime(inode, h_inode);
9845 +               fi_write_unlock(file);
9846 +               iput(h_inode);
9847 +
9848 +               AuTraceErr(err);
9849 +               return err;
9850 +       }
9851 +
9852 +out_unlock:
9853 +       di_read_unlock(dentry, AuLock_IR);
9854 +       fi_write_unlock(file);
9855 +out:
9856 +       si_read_unlock(sb);
9857 +       return err;
9858 +}
9859 +
9860 +/* ---------------------------------------------------------------------- */
9861 +
9862 +#define AuTestEmpty_WHONLY     1
9863 +#define AuTestEmpty_CALLED     (1 << 1)
9864 +#define AuTestEmpty_SHWH       (1 << 2)
9865 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
9866 +#define au_fset_testempty(flags, name) \
9867 +       do { (flags) |= AuTestEmpty_##name; } while (0)
9868 +#define au_fclr_testempty(flags, name) \
9869 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
9870 +
9871 +#ifndef CONFIG_AUFS_SHWH
9872 +#undef AuTestEmpty_SHWH
9873 +#define AuTestEmpty_SHWH       0
9874 +#endif
9875 +
9876 +struct test_empty_arg {
9877 +       struct dir_context ctx;
9878 +       struct au_nhash *whlist;
9879 +       unsigned int flags;
9880 +       int err;
9881 +       aufs_bindex_t bindex;
9882 +};
9883 +
9884 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
9885 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
9886 +                        unsigned int d_type)
9887 +{
9888 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
9889 +                                                 ctx);
9890 +       char *name = (void *)__name;
9891 +
9892 +       arg->err = 0;
9893 +       au_fset_testempty(arg->flags, CALLED);
9894 +       /* smp_mb(); */
9895 +       if (name[0] == '.'
9896 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
9897 +               goto out; /* success */
9898 +
9899 +       if (namelen <= AUFS_WH_PFX_LEN
9900 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
9901 +               if (au_ftest_testempty(arg->flags, WHONLY)
9902 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
9903 +                       arg->err = -ENOTEMPTY;
9904 +               goto out;
9905 +       }
9906 +
9907 +       name += AUFS_WH_PFX_LEN;
9908 +       namelen -= AUFS_WH_PFX_LEN;
9909 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
9910 +               arg->err = au_nhash_append_wh
9911 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
9912 +                        au_ftest_testempty(arg->flags, SHWH));
9913 +
9914 +out:
9915 +       /* smp_mb(); */
9916 +       AuTraceErr(arg->err);
9917 +       return arg->err;
9918 +}
9919 +
9920 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9921 +{
9922 +       int err;
9923 +       struct file *h_file;
9924 +
9925 +       h_file = au_h_open(dentry, arg->bindex,
9926 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
9927 +                          /*file*/NULL, /*force_wr*/0);
9928 +       err = PTR_ERR(h_file);
9929 +       if (IS_ERR(h_file))
9930 +               goto out;
9931 +
9932 +       err = 0;
9933 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
9934 +           && !file_inode(h_file)->i_nlink)
9935 +               goto out_put;
9936 +
9937 +       do {
9938 +               arg->err = 0;
9939 +               au_fclr_testempty(arg->flags, CALLED);
9940 +               /* smp_mb(); */
9941 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
9942 +               if (err >= 0)
9943 +                       err = arg->err;
9944 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
9945 +
9946 +out_put:
9947 +       fput(h_file);
9948 +       au_sbr_put(dentry->d_sb, arg->bindex);
9949 +out:
9950 +       return err;
9951 +}
9952 +
9953 +struct do_test_empty_args {
9954 +       int *errp;
9955 +       struct dentry *dentry;
9956 +       struct test_empty_arg *arg;
9957 +};
9958 +
9959 +static void call_do_test_empty(void *args)
9960 +{
9961 +       struct do_test_empty_args *a = args;
9962 +       *a->errp = do_test_empty(a->dentry, a->arg);
9963 +}
9964 +
9965 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
9966 +{
9967 +       int err, wkq_err;
9968 +       struct dentry *h_dentry;
9969 +       struct inode *h_inode;
9970 +
9971 +       h_dentry = au_h_dptr(dentry, arg->bindex);
9972 +       h_inode = d_inode(h_dentry);
9973 +       /* todo: i_mode changes anytime? */
9974 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
9975 +       err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
9976 +       mutex_unlock(&h_inode->i_mutex);
9977 +       if (!err)
9978 +               err = do_test_empty(dentry, arg);
9979 +       else {
9980 +               struct do_test_empty_args args = {
9981 +                       .errp   = &err,
9982 +                       .dentry = dentry,
9983 +                       .arg    = arg
9984 +               };
9985 +               unsigned int flags = arg->flags;
9986 +
9987 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
9988 +               if (unlikely(wkq_err))
9989 +                       err = wkq_err;
9990 +               arg->flags = flags;
9991 +       }
9992 +
9993 +       return err;
9994 +}
9995 +
9996 +int au_test_empty_lower(struct dentry *dentry)
9997 +{
9998 +       int err;
9999 +       unsigned int rdhash;
10000 +       aufs_bindex_t bindex, bstart, btail;
10001 +       struct au_nhash whlist;
10002 +       struct test_empty_arg arg = {
10003 +               .ctx = {
10004 +                       .actor = test_empty_cb
10005 +               }
10006 +       };
10007 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10008 +
10009 +       SiMustAnyLock(dentry->d_sb);
10010 +
10011 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10012 +       if (!rdhash)
10013 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10014 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10015 +       if (unlikely(err))
10016 +               goto out;
10017 +
10018 +       arg.flags = 0;
10019 +       arg.whlist = &whlist;
10020 +       bstart = au_dbstart(dentry);
10021 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10022 +               au_fset_testempty(arg.flags, SHWH);
10023 +       test_empty = do_test_empty;
10024 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10025 +               test_empty = sio_test_empty;
10026 +       arg.bindex = bstart;
10027 +       err = test_empty(dentry, &arg);
10028 +       if (unlikely(err))
10029 +               goto out_whlist;
10030 +
10031 +       au_fset_testempty(arg.flags, WHONLY);
10032 +       btail = au_dbtaildir(dentry);
10033 +       for (bindex = bstart + 1; !err && bindex <= btail; bindex++) {
10034 +               struct dentry *h_dentry;
10035 +
10036 +               h_dentry = au_h_dptr(dentry, bindex);
10037 +               if (h_dentry && d_is_positive(h_dentry)) {
10038 +                       arg.bindex = bindex;
10039 +                       err = test_empty(dentry, &arg);
10040 +               }
10041 +       }
10042 +
10043 +out_whlist:
10044 +       au_nhash_wh_free(&whlist);
10045 +out:
10046 +       return err;
10047 +}
10048 +
10049 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10050 +{
10051 +       int err;
10052 +       struct test_empty_arg arg = {
10053 +               .ctx = {
10054 +                       .actor = test_empty_cb
10055 +               }
10056 +       };
10057 +       aufs_bindex_t bindex, btail;
10058 +
10059 +       err = 0;
10060 +       arg.whlist = whlist;
10061 +       arg.flags = AuTestEmpty_WHONLY;
10062 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10063 +               au_fset_testempty(arg.flags, SHWH);
10064 +       btail = au_dbtaildir(dentry);
10065 +       for (bindex = au_dbstart(dentry); !err && bindex <= btail; bindex++) {
10066 +               struct dentry *h_dentry;
10067 +
10068 +               h_dentry = au_h_dptr(dentry, bindex);
10069 +               if (h_dentry && d_is_positive(h_dentry)) {
10070 +                       arg.bindex = bindex;
10071 +                       err = sio_test_empty(dentry, &arg);
10072 +               }
10073 +       }
10074 +
10075 +       return err;
10076 +}
10077 +
10078 +/* ---------------------------------------------------------------------- */
10079 +
10080 +const struct file_operations aufs_dir_fop = {
10081 +       .owner          = THIS_MODULE,
10082 +       .llseek         = default_llseek,
10083 +       .read           = generic_read_dir,
10084 +       .iterate        = aufs_iterate,
10085 +       .unlocked_ioctl = aufs_ioctl_dir,
10086 +#ifdef CONFIG_COMPAT
10087 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10088 +#endif
10089 +       .open           = aufs_open_dir,
10090 +       .release        = aufs_release_dir,
10091 +       .flush          = aufs_flush_dir,
10092 +       .fsync          = aufs_fsync_dir
10093 +};
10094 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10095 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10096 +++ linux/fs/aufs/dir.h 2015-09-24 10:47:58.251386326 +0200
10097 @@ -0,0 +1,131 @@
10098 +/*
10099 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10100 + *
10101 + * This program, aufs is free software; you can redistribute it and/or modify
10102 + * it under the terms of the GNU General Public License as published by
10103 + * the Free Software Foundation; either version 2 of the License, or
10104 + * (at your option) any later version.
10105 + *
10106 + * This program is distributed in the hope that it will be useful,
10107 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10108 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10109 + * GNU General Public License for more details.
10110 + *
10111 + * You should have received a copy of the GNU General Public License
10112 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10113 + */
10114 +
10115 +/*
10116 + * directory operations
10117 + */
10118 +
10119 +#ifndef __AUFS_DIR_H__
10120 +#define __AUFS_DIR_H__
10121 +
10122 +#ifdef __KERNEL__
10123 +
10124 +#include <linux/fs.h>
10125 +
10126 +/* ---------------------------------------------------------------------- */
10127 +
10128 +/* need to be faster and smaller */
10129 +
10130 +struct au_nhash {
10131 +       unsigned int            nh_num;
10132 +       struct hlist_head       *nh_head;
10133 +};
10134 +
10135 +struct au_vdir_destr {
10136 +       unsigned char   len;
10137 +       unsigned char   name[0];
10138 +} __packed;
10139 +
10140 +struct au_vdir_dehstr {
10141 +       struct hlist_node       hash;
10142 +       struct au_vdir_destr    *str;
10143 +} ____cacheline_aligned_in_smp;
10144 +
10145 +struct au_vdir_de {
10146 +       ino_t                   de_ino;
10147 +       unsigned char           de_type;
10148 +       /* caution: packed */
10149 +       struct au_vdir_destr    de_str;
10150 +} __packed;
10151 +
10152 +struct au_vdir_wh {
10153 +       struct hlist_node       wh_hash;
10154 +#ifdef CONFIG_AUFS_SHWH
10155 +       ino_t                   wh_ino;
10156 +       aufs_bindex_t           wh_bindex;
10157 +       unsigned char           wh_type;
10158 +#else
10159 +       aufs_bindex_t           wh_bindex;
10160 +#endif
10161 +       /* caution: packed */
10162 +       struct au_vdir_destr    wh_str;
10163 +} __packed;
10164 +
10165 +union au_vdir_deblk_p {
10166 +       unsigned char           *deblk;
10167 +       struct au_vdir_de       *de;
10168 +};
10169 +
10170 +struct au_vdir {
10171 +       unsigned char   **vd_deblk;
10172 +       unsigned long   vd_nblk;
10173 +       struct {
10174 +               unsigned long           ul;
10175 +               union au_vdir_deblk_p   p;
10176 +       } vd_last;
10177 +
10178 +       unsigned long   vd_version;
10179 +       unsigned int    vd_deblk_sz;
10180 +       unsigned long   vd_jiffy;
10181 +} ____cacheline_aligned_in_smp;
10182 +
10183 +/* ---------------------------------------------------------------------- */
10184 +
10185 +/* dir.c */
10186 +extern const struct file_operations aufs_dir_fop;
10187 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10188 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10189 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10190 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10191 +int au_test_empty_lower(struct dentry *dentry);
10192 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10193 +
10194 +/* vdir.c */
10195 +unsigned int au_rdhash_est(loff_t sz);
10196 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10197 +void au_nhash_wh_free(struct au_nhash *whlist);
10198 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10199 +                           int limit);
10200 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10201 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10202 +                      unsigned int d_type, aufs_bindex_t bindex,
10203 +                      unsigned char shwh);
10204 +void au_vdir_free(struct au_vdir *vdir);
10205 +int au_vdir_init(struct file *file);
10206 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10207 +
10208 +/* ioctl.c */
10209 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10210 +
10211 +#ifdef CONFIG_AUFS_RDU
10212 +/* rdu.c */
10213 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10214 +#ifdef CONFIG_COMPAT
10215 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10216 +                        unsigned long arg);
10217 +#endif
10218 +#else
10219 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10220 +       unsigned int cmd, unsigned long arg)
10221 +#ifdef CONFIG_COMPAT
10222 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10223 +       unsigned int cmd, unsigned long arg)
10224 +#endif
10225 +#endif
10226 +
10227 +#endif /* __KERNEL__ */
10228 +#endif /* __AUFS_DIR_H__ */
10229 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10230 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
10231 +++ linux/fs/aufs/dynop.c       2015-09-24 10:47:58.251386326 +0200
10232 @@ -0,0 +1,369 @@
10233 +/*
10234 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10235 + *
10236 + * This program, aufs is free software; you can redistribute it and/or modify
10237 + * it under the terms of the GNU General Public License as published by
10238 + * the Free Software Foundation; either version 2 of the License, or
10239 + * (at your option) any later version.
10240 + *
10241 + * This program is distributed in the hope that it will be useful,
10242 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10243 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10244 + * GNU General Public License for more details.
10245 + *
10246 + * You should have received a copy of the GNU General Public License
10247 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10248 + */
10249 +
10250 +/*
10251 + * dynamically customizable operations for regular files
10252 + */
10253 +
10254 +#include "aufs.h"
10255 +
10256 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
10257 +
10258 +/*
10259 + * How large will these lists be?
10260 + * Usually just a few elements, 20-30 at most for each, I guess.
10261 + */
10262 +static struct au_splhead dynop[AuDyLast];
10263 +
10264 +static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
10265 +{
10266 +       struct au_dykey *key, *tmp;
10267 +       struct list_head *head;
10268 +
10269 +       key = NULL;
10270 +       head = &spl->head;
10271 +       rcu_read_lock();
10272 +       list_for_each_entry_rcu(tmp, head, dk_list)
10273 +               if (tmp->dk_op.dy_hop == h_op) {
10274 +                       key = tmp;
10275 +                       kref_get(&key->dk_kref);
10276 +                       break;
10277 +               }
10278 +       rcu_read_unlock();
10279 +
10280 +       return key;
10281 +}
10282 +
10283 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
10284 +{
10285 +       struct au_dykey **k, *found;
10286 +       const void *h_op = key->dk_op.dy_hop;
10287 +       int i;
10288 +
10289 +       found = NULL;
10290 +       k = br->br_dykey;
10291 +       for (i = 0; i < AuBrDynOp; i++)
10292 +               if (k[i]) {
10293 +                       if (k[i]->dk_op.dy_hop == h_op) {
10294 +                               found = k[i];
10295 +                               break;
10296 +                       }
10297 +               } else
10298 +                       break;
10299 +       if (!found) {
10300 +               spin_lock(&br->br_dykey_lock);
10301 +               for (; i < AuBrDynOp; i++)
10302 +                       if (k[i]) {
10303 +                               if (k[i]->dk_op.dy_hop == h_op) {
10304 +                                       found = k[i];
10305 +                                       break;
10306 +                               }
10307 +                       } else {
10308 +                               k[i] = key;
10309 +                               break;
10310 +                       }
10311 +               spin_unlock(&br->br_dykey_lock);
10312 +               BUG_ON(i == AuBrDynOp); /* expand the array */
10313 +       }
10314 +
10315 +       return found;
10316 +}
10317 +
10318 +/* kref_get() if @key is already added */
10319 +static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
10320 +{
10321 +       struct au_dykey *tmp, *found;
10322 +       struct list_head *head;
10323 +       const void *h_op = key->dk_op.dy_hop;
10324 +
10325 +       found = NULL;
10326 +       head = &spl->head;
10327 +       spin_lock(&spl->spin);
10328 +       list_for_each_entry(tmp, head, dk_list)
10329 +               if (tmp->dk_op.dy_hop == h_op) {
10330 +                       kref_get(&tmp->dk_kref);
10331 +                       found = tmp;
10332 +                       break;
10333 +               }
10334 +       if (!found)
10335 +               list_add_rcu(&key->dk_list, head);
10336 +       spin_unlock(&spl->spin);
10337 +
10338 +       if (!found)
10339 +               DyPrSym(key);
10340 +       return found;
10341 +}
10342 +
10343 +static void dy_free_rcu(struct rcu_head *rcu)
10344 +{
10345 +       struct au_dykey *key;
10346 +
10347 +       key = container_of(rcu, struct au_dykey, dk_rcu);
10348 +       DyPrSym(key);
10349 +       kfree(key);
10350 +}
10351 +
10352 +static void dy_free(struct kref *kref)
10353 +{
10354 +       struct au_dykey *key;
10355 +       struct au_splhead *spl;
10356 +
10357 +       key = container_of(kref, struct au_dykey, dk_kref);
10358 +       spl = dynop + key->dk_op.dy_type;
10359 +       au_spl_del_rcu(&key->dk_list, spl);
10360 +       call_rcu(&key->dk_rcu, dy_free_rcu);
10361 +}
10362 +
10363 +void au_dy_put(struct au_dykey *key)
10364 +{
10365 +       kref_put(&key->dk_kref, dy_free);
10366 +}
10367 +
10368 +/* ---------------------------------------------------------------------- */
10369 +
10370 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10371 +
10372 +#ifdef CONFIG_AUFS_DEBUG
10373 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
10374 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
10375 +#else
10376 +#define DyDbgDeclare(cnt)      do {} while (0)
10377 +#define DyDbgInc(cnt)          do {} while (0)
10378 +#endif
10379 +
10380 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
10381 +       DyDbgInc(cnt);                                                  \
10382 +       if (h_op->func) {                                               \
10383 +               if (src.func)                                           \
10384 +                       dst.func = src.func;                            \
10385 +               else                                                    \
10386 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
10387 +       }                                                               \
10388 +} while (0)
10389 +
10390 +#define DySetForce(func, dst, src) do {                \
10391 +       AuDebugOn(!src.func);                   \
10392 +       DyDbgInc(cnt);                          \
10393 +       dst.func = src.func;                    \
10394 +} while (0)
10395 +
10396 +#define DySetAop(func) \
10397 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10398 +#define DySetAopForce(func) \
10399 +       DySetForce(func, dyaop->da_op, aufs_aop)
10400 +
10401 +static void dy_aop(struct au_dykey *key, const void *h_op,
10402 +                  struct super_block *h_sb __maybe_unused)
10403 +{
10404 +       struct au_dyaop *dyaop = (void *)key;
10405 +       const struct address_space_operations *h_aop = h_op;
10406 +       DyDbgDeclare(cnt);
10407 +
10408 +       AuDbg("%s\n", au_sbtype(h_sb));
10409 +
10410 +       DySetAop(writepage);
10411 +       DySetAopForce(readpage);        /* force */
10412 +       DySetAop(writepages);
10413 +       DySetAop(set_page_dirty);
10414 +       DySetAop(readpages);
10415 +       DySetAop(write_begin);
10416 +       DySetAop(write_end);
10417 +       DySetAop(bmap);
10418 +       DySetAop(invalidatepage);
10419 +       DySetAop(releasepage);
10420 +       DySetAop(freepage);
10421 +       /* this one will be changed according to an aufs mount option */
10422 +       DySetAop(direct_IO);
10423 +       DySetAop(migratepage);
10424 +       DySetAop(launder_page);
10425 +       DySetAop(is_partially_uptodate);
10426 +       DySetAop(is_dirty_writeback);
10427 +       DySetAop(error_remove_page);
10428 +       DySetAop(swap_activate);
10429 +       DySetAop(swap_deactivate);
10430 +
10431 +       DyDbgSize(cnt, *h_aop);
10432 +}
10433 +
10434 +/* ---------------------------------------------------------------------- */
10435 +
10436 +static void dy_bug(struct kref *kref)
10437 +{
10438 +       BUG();
10439 +}
10440 +
10441 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10442 +{
10443 +       struct au_dykey *key, *old;
10444 +       struct au_splhead *spl;
10445 +       struct op {
10446 +               unsigned int sz;
10447 +               void (*set)(struct au_dykey *key, const void *h_op,
10448 +                           struct super_block *h_sb __maybe_unused);
10449 +       };
10450 +       static const struct op a[] = {
10451 +               [AuDy_AOP] = {
10452 +                       .sz     = sizeof(struct au_dyaop),
10453 +                       .set    = dy_aop
10454 +               }
10455 +       };
10456 +       const struct op *p;
10457 +
10458 +       spl = dynop + op->dy_type;
10459 +       key = dy_gfind_get(spl, op->dy_hop);
10460 +       if (key)
10461 +               goto out_add; /* success */
10462 +
10463 +       p = a + op->dy_type;
10464 +       key = kzalloc(p->sz, GFP_NOFS);
10465 +       if (unlikely(!key)) {
10466 +               key = ERR_PTR(-ENOMEM);
10467 +               goto out;
10468 +       }
10469 +
10470 +       key->dk_op.dy_hop = op->dy_hop;
10471 +       kref_init(&key->dk_kref);
10472 +       p->set(key, op->dy_hop, au_br_sb(br));
10473 +       old = dy_gadd(spl, key);
10474 +       if (old) {
10475 +               kfree(key);
10476 +               key = old;
10477 +       }
10478 +
10479 +out_add:
10480 +       old = dy_bradd(br, key);
10481 +       if (old)
10482 +               /* its ref-count should never be zero here */
10483 +               kref_put(&key->dk_kref, dy_bug);
10484 +out:
10485 +       return key;
10486 +}
10487 +
10488 +/* ---------------------------------------------------------------------- */
10489 +/*
10490 + * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
10491 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
10492 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10493 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10494 + * See the aufs manual in detail.
10495 + */
10496 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10497 +{
10498 +       if (!do_dx)
10499 +               dyaop->da_op.direct_IO = NULL;
10500 +       else
10501 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
10502 +}
10503 +
10504 +static struct au_dyaop *dy_aget(struct au_branch *br,
10505 +                               const struct address_space_operations *h_aop,
10506 +                               int do_dx)
10507 +{
10508 +       struct au_dyaop *dyaop;
10509 +       struct au_dynop op;
10510 +
10511 +       op.dy_type = AuDy_AOP;
10512 +       op.dy_haop = h_aop;
10513 +       dyaop = (void *)dy_get(&op, br);
10514 +       if (IS_ERR(dyaop))
10515 +               goto out;
10516 +       dy_adx(dyaop, do_dx);
10517 +
10518 +out:
10519 +       return dyaop;
10520 +}
10521 +
10522 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10523 +               struct inode *h_inode)
10524 +{
10525 +       int err, do_dx;
10526 +       struct super_block *sb;
10527 +       struct au_branch *br;
10528 +       struct au_dyaop *dyaop;
10529 +
10530 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
10531 +       IiMustWriteLock(inode);
10532 +
10533 +       sb = inode->i_sb;
10534 +       br = au_sbr(sb, bindex);
10535 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10536 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10537 +       err = PTR_ERR(dyaop);
10538 +       if (IS_ERR(dyaop))
10539 +               /* unnecessary to call dy_fput() */
10540 +               goto out;
10541 +
10542 +       err = 0;
10543 +       inode->i_mapping->a_ops = &dyaop->da_op;
10544 +
10545 +out:
10546 +       return err;
10547 +}
10548 +
10549 +/*
10550 + * Is it safe to replace a_ops during the inode/file is in operation?
10551 + * Yes, I hope so.
10552 + */
10553 +int au_dy_irefresh(struct inode *inode)
10554 +{
10555 +       int err;
10556 +       aufs_bindex_t bstart;
10557 +       struct inode *h_inode;
10558 +
10559 +       err = 0;
10560 +       if (S_ISREG(inode->i_mode)) {
10561 +               bstart = au_ibstart(inode);
10562 +               h_inode = au_h_iptr(inode, bstart);
10563 +               err = au_dy_iaop(inode, bstart, h_inode);
10564 +       }
10565 +       return err;
10566 +}
10567 +
10568 +void au_dy_arefresh(int do_dx)
10569 +{
10570 +       struct au_splhead *spl;
10571 +       struct list_head *head;
10572 +       struct au_dykey *key;
10573 +
10574 +       spl = dynop + AuDy_AOP;
10575 +       head = &spl->head;
10576 +       spin_lock(&spl->spin);
10577 +       list_for_each_entry(key, head, dk_list)
10578 +               dy_adx((void *)key, do_dx);
10579 +       spin_unlock(&spl->spin);
10580 +}
10581 +
10582 +/* ---------------------------------------------------------------------- */
10583 +
10584 +void __init au_dy_init(void)
10585 +{
10586 +       int i;
10587 +
10588 +       /* make sure that 'struct au_dykey *' can be any type */
10589 +       BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
10590 +
10591 +       for (i = 0; i < AuDyLast; i++)
10592 +               au_spl_init(dynop + i);
10593 +}
10594 +
10595 +void au_dy_fin(void)
10596 +{
10597 +       int i;
10598 +
10599 +       for (i = 0; i < AuDyLast; i++)
10600 +               WARN_ON(!list_empty(&dynop[i].head));
10601 +}
10602 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10603 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
10604 +++ linux/fs/aufs/dynop.h       2015-09-24 10:47:58.251386326 +0200
10605 @@ -0,0 +1,74 @@
10606 +/*
10607 + * Copyright (C) 2010-2015 Junjiro R. Okajima
10608 + *
10609 + * This program, aufs is free software; you can redistribute it and/or modify
10610 + * it under the terms of the GNU General Public License as published by
10611 + * the Free Software Foundation; either version 2 of the License, or
10612 + * (at your option) any later version.
10613 + *
10614 + * This program is distributed in the hope that it will be useful,
10615 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10616 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10617 + * GNU General Public License for more details.
10618 + *
10619 + * You should have received a copy of the GNU General Public License
10620 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10621 + */
10622 +
10623 +/*
10624 + * dynamically customizable operations (for regular files only)
10625 + */
10626 +
10627 +#ifndef __AUFS_DYNOP_H__
10628 +#define __AUFS_DYNOP_H__
10629 +
10630 +#ifdef __KERNEL__
10631 +
10632 +#include <linux/fs.h>
10633 +#include <linux/kref.h>
10634 +
10635 +enum {AuDy_AOP, AuDyLast};
10636 +
10637 +struct au_dynop {
10638 +       int                                             dy_type;
10639 +       union {
10640 +               const void                              *dy_hop;
10641 +               const struct address_space_operations   *dy_haop;
10642 +       };
10643 +};
10644 +
10645 +struct au_dykey {
10646 +       union {
10647 +               struct list_head        dk_list;
10648 +               struct rcu_head         dk_rcu;
10649 +       };
10650 +       struct au_dynop         dk_op;
10651 +
10652 +       /*
10653 +        * during I am in the branch local array, kref is gotten. when the
10654 +        * branch is removed, kref is put.
10655 +        */
10656 +       struct kref             dk_kref;
10657 +};
10658 +
10659 +/* stop unioning since their sizes are very different from each other */
10660 +struct au_dyaop {
10661 +       struct au_dykey                 da_key;
10662 +       struct address_space_operations da_op; /* not const */
10663 +};
10664 +
10665 +/* ---------------------------------------------------------------------- */
10666 +
10667 +/* dynop.c */
10668 +struct au_branch;
10669 +void au_dy_put(struct au_dykey *key);
10670 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10671 +               struct inode *h_inode);
10672 +int au_dy_irefresh(struct inode *inode);
10673 +void au_dy_arefresh(int do_dio);
10674 +
10675 +void __init au_dy_init(void);
10676 +void au_dy_fin(void);
10677 +
10678 +#endif /* __KERNEL__ */
10679 +#endif /* __AUFS_DYNOP_H__ */
10680 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10681 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
10682 +++ linux/fs/aufs/export.c      2015-09-24 10:47:58.251386326 +0200
10683 @@ -0,0 +1,832 @@
10684 +/*
10685 + * Copyright (C) 2005-2015 Junjiro R. Okajima
10686 + *
10687 + * This program, aufs is free software; you can redistribute it and/or modify
10688 + * it under the terms of the GNU General Public License as published by
10689 + * the Free Software Foundation; either version 2 of the License, or
10690 + * (at your option) any later version.
10691 + *
10692 + * This program is distributed in the hope that it will be useful,
10693 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10694 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10695 + * GNU General Public License for more details.
10696 + *
10697 + * You should have received a copy of the GNU General Public License
10698 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10699 + */
10700 +
10701 +/*
10702 + * export via nfs
10703 + */
10704 +
10705 +#include <linux/exportfs.h>
10706 +#include <linux/fs_struct.h>
10707 +#include <linux/namei.h>
10708 +#include <linux/nsproxy.h>
10709 +#include <linux/random.h>
10710 +#include <linux/writeback.h>
10711 +#include "../fs/mount.h"
10712 +#include "aufs.h"
10713 +
10714 +union conv {
10715 +#ifdef CONFIG_AUFS_INO_T_64
10716 +       __u32 a[2];
10717 +#else
10718 +       __u32 a[1];
10719 +#endif
10720 +       ino_t ino;
10721 +};
10722 +
10723 +static ino_t decode_ino(__u32 *a)
10724 +{
10725 +       union conv u;
10726 +
10727 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10728 +       u.a[0] = a[0];
10729 +#ifdef CONFIG_AUFS_INO_T_64
10730 +       u.a[1] = a[1];
10731 +#endif
10732 +       return u.ino;
10733 +}
10734 +
10735 +static void encode_ino(__u32 *a, ino_t ino)
10736 +{
10737 +       union conv u;
10738 +
10739 +       u.ino = ino;
10740 +       a[0] = u.a[0];
10741 +#ifdef CONFIG_AUFS_INO_T_64
10742 +       a[1] = u.a[1];
10743 +#endif
10744 +}
10745 +
10746 +/* NFS file handle */
10747 +enum {
10748 +       Fh_br_id,
10749 +       Fh_sigen,
10750 +#ifdef CONFIG_AUFS_INO_T_64
10751 +       /* support 64bit inode number */
10752 +       Fh_ino1,
10753 +       Fh_ino2,
10754 +       Fh_dir_ino1,
10755 +       Fh_dir_ino2,
10756 +#else
10757 +       Fh_ino1,
10758 +       Fh_dir_ino1,
10759 +#endif
10760 +       Fh_igen,
10761 +       Fh_h_type,
10762 +       Fh_tail,
10763 +
10764 +       Fh_ino = Fh_ino1,
10765 +       Fh_dir_ino = Fh_dir_ino1
10766 +};
10767 +
10768 +static int au_test_anon(struct dentry *dentry)
10769 +{
10770 +       /* note: read d_flags without d_lock */
10771 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
10772 +}
10773 +
10774 +int au_test_nfsd(void)
10775 +{
10776 +       int ret;
10777 +       struct task_struct *tsk = current;
10778 +       char comm[sizeof(tsk->comm)];
10779 +
10780 +       ret = 0;
10781 +       if (tsk->flags & PF_KTHREAD) {
10782 +               get_task_comm(comm, tsk);
10783 +               ret = !strcmp(comm, "nfsd");
10784 +       }
10785 +
10786 +       return ret;
10787 +}
10788 +
10789 +/* ---------------------------------------------------------------------- */
10790 +/* inode generation external table */
10791 +
10792 +void au_xigen_inc(struct inode *inode)
10793 +{
10794 +       loff_t pos;
10795 +       ssize_t sz;
10796 +       __u32 igen;
10797 +       struct super_block *sb;
10798 +       struct au_sbinfo *sbinfo;
10799 +
10800 +       sb = inode->i_sb;
10801 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
10802 +
10803 +       sbinfo = au_sbi(sb);
10804 +       pos = inode->i_ino;
10805 +       pos *= sizeof(igen);
10806 +       igen = inode->i_generation + 1;
10807 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
10808 +                        sizeof(igen), &pos);
10809 +       if (sz == sizeof(igen))
10810 +               return; /* success */
10811 +
10812 +       if (unlikely(sz >= 0))
10813 +               AuIOErr("xigen error (%zd)\n", sz);
10814 +}
10815 +
10816 +int au_xigen_new(struct inode *inode)
10817 +{
10818 +       int err;
10819 +       loff_t pos;
10820 +       ssize_t sz;
10821 +       struct super_block *sb;
10822 +       struct au_sbinfo *sbinfo;
10823 +       struct file *file;
10824 +
10825 +       err = 0;
10826 +       /* todo: dirty, at mount time */
10827 +       if (inode->i_ino == AUFS_ROOT_INO)
10828 +               goto out;
10829 +       sb = inode->i_sb;
10830 +       SiMustAnyLock(sb);
10831 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
10832 +               goto out;
10833 +
10834 +       err = -EFBIG;
10835 +       pos = inode->i_ino;
10836 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
10837 +               AuIOErr1("too large i%lld\n", pos);
10838 +               goto out;
10839 +       }
10840 +       pos *= sizeof(inode->i_generation);
10841 +
10842 +       err = 0;
10843 +       sbinfo = au_sbi(sb);
10844 +       file = sbinfo->si_xigen;
10845 +       BUG_ON(!file);
10846 +
10847 +       if (vfsub_f_size_read(file)
10848 +           < pos + sizeof(inode->i_generation)) {
10849 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
10850 +               sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
10851 +                                sizeof(inode->i_generation), &pos);
10852 +       } else
10853 +               sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
10854 +                               sizeof(inode->i_generation), &pos);
10855 +       if (sz == sizeof(inode->i_generation))
10856 +               goto out; /* success */
10857 +
10858 +       err = sz;
10859 +       if (unlikely(sz >= 0)) {
10860 +               err = -EIO;
10861 +               AuIOErr("xigen error (%zd)\n", sz);
10862 +       }
10863 +
10864 +out:
10865 +       return err;
10866 +}
10867 +
10868 +int au_xigen_set(struct super_block *sb, struct file *base)
10869 +{
10870 +       int err;
10871 +       struct au_sbinfo *sbinfo;
10872 +       struct file *file;
10873 +
10874 +       SiMustWriteLock(sb);
10875 +
10876 +       sbinfo = au_sbi(sb);
10877 +       file = au_xino_create2(base, sbinfo->si_xigen);
10878 +       err = PTR_ERR(file);
10879 +       if (IS_ERR(file))
10880 +               goto out;
10881 +       err = 0;
10882 +       if (sbinfo->si_xigen)
10883 +               fput(sbinfo->si_xigen);
10884 +       sbinfo->si_xigen = file;
10885 +
10886 +out:
10887 +       return err;
10888 +}
10889 +
10890 +void au_xigen_clr(struct super_block *sb)
10891 +{
10892 +       struct au_sbinfo *sbinfo;
10893 +
10894 +       SiMustWriteLock(sb);
10895 +
10896 +       sbinfo = au_sbi(sb);
10897 +       if (sbinfo->si_xigen) {
10898 +               fput(sbinfo->si_xigen);
10899 +               sbinfo->si_xigen = NULL;
10900 +       }
10901 +}
10902 +
10903 +/* ---------------------------------------------------------------------- */
10904 +
10905 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
10906 +                                   ino_t dir_ino)
10907 +{
10908 +       struct dentry *dentry, *d;
10909 +       struct inode *inode;
10910 +       unsigned int sigen;
10911 +
10912 +       dentry = NULL;
10913 +       inode = ilookup(sb, ino);
10914 +       if (!inode)
10915 +               goto out;
10916 +
10917 +       dentry = ERR_PTR(-ESTALE);
10918 +       sigen = au_sigen(sb);
10919 +       if (unlikely(is_bad_inode(inode)
10920 +                    || IS_DEADDIR(inode)
10921 +                    || sigen != au_iigen(inode, NULL)))
10922 +               goto out_iput;
10923 +
10924 +       dentry = NULL;
10925 +       if (!dir_ino || S_ISDIR(inode->i_mode))
10926 +               dentry = d_find_alias(inode);
10927 +       else {
10928 +               spin_lock(&inode->i_lock);
10929 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
10930 +                       spin_lock(&d->d_lock);
10931 +                       if (!au_test_anon(d)
10932 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
10933 +                               dentry = dget_dlock(d);
10934 +                               spin_unlock(&d->d_lock);
10935 +                               break;
10936 +                       }
10937 +                       spin_unlock(&d->d_lock);
10938 +               }
10939 +               spin_unlock(&inode->i_lock);
10940 +       }
10941 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
10942 +               /* need to refresh */
10943 +               dput(dentry);
10944 +               dentry = NULL;
10945 +       }
10946 +
10947 +out_iput:
10948 +       iput(inode);
10949 +out:
10950 +       AuTraceErrPtr(dentry);
10951 +       return dentry;
10952 +}
10953 +
10954 +/* ---------------------------------------------------------------------- */
10955 +
10956 +/* todo: dirty? */
10957 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
10958 +
10959 +struct au_compare_mnt_args {
10960 +       /* input */
10961 +       struct super_block *sb;
10962 +
10963 +       /* output */
10964 +       struct vfsmount *mnt;
10965 +};
10966 +
10967 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
10968 +{
10969 +       struct au_compare_mnt_args *a = arg;
10970 +
10971 +       if (mnt->mnt_sb != a->sb)
10972 +               return 0;
10973 +       a->mnt = mntget(mnt);
10974 +       return 1;
10975 +}
10976 +
10977 +static struct vfsmount *au_mnt_get(struct super_block *sb)
10978 +{
10979 +       int err;
10980 +       struct path root;
10981 +       struct au_compare_mnt_args args = {
10982 +               .sb = sb
10983 +       };
10984 +
10985 +       get_fs_root(current->fs, &root);
10986 +       rcu_read_lock();
10987 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
10988 +       rcu_read_unlock();
10989 +       path_put(&root);
10990 +       AuDebugOn(!err);
10991 +       AuDebugOn(!args.mnt);
10992 +       return args.mnt;
10993 +}
10994 +
10995 +struct au_nfsd_si_lock {
10996 +       unsigned int sigen;
10997 +       aufs_bindex_t bindex, br_id;
10998 +       unsigned char force_lock;
10999 +};
11000 +
11001 +static int si_nfsd_read_lock(struct super_block *sb,
11002 +                            struct au_nfsd_si_lock *nsi_lock)
11003 +{
11004 +       int err;
11005 +       aufs_bindex_t bindex;
11006 +
11007 +       si_read_lock(sb, AuLock_FLUSH);
11008 +
11009 +       /* branch id may be wrapped around */
11010 +       err = 0;
11011 +       bindex = au_br_index(sb, nsi_lock->br_id);
11012 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11013 +               goto out; /* success */
11014 +
11015 +       err = -ESTALE;
11016 +       bindex = -1;
11017 +       if (!nsi_lock->force_lock)
11018 +               si_read_unlock(sb);
11019 +
11020 +out:
11021 +       nsi_lock->bindex = bindex;
11022 +       return err;
11023 +}
11024 +
11025 +struct find_name_by_ino {
11026 +       struct dir_context ctx;
11027 +       int called, found;
11028 +       ino_t ino;
11029 +       char *name;
11030 +       int namelen;
11031 +};
11032 +
11033 +static int
11034 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11035 +                loff_t offset, u64 ino, unsigned int d_type)
11036 +{
11037 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11038 +                                                 ctx);
11039 +
11040 +       a->called++;
11041 +       if (a->ino != ino)
11042 +               return 0;
11043 +
11044 +       memcpy(a->name, name, namelen);
11045 +       a->namelen = namelen;
11046 +       a->found = 1;
11047 +       return 1;
11048 +}
11049 +
11050 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11051 +                                    struct au_nfsd_si_lock *nsi_lock)
11052 +{
11053 +       struct dentry *dentry, *parent;
11054 +       struct file *file;
11055 +       struct inode *dir;
11056 +       struct find_name_by_ino arg = {
11057 +               .ctx = {
11058 +                       .actor = find_name_by_ino
11059 +               }
11060 +       };
11061 +       int err;
11062 +
11063 +       parent = path->dentry;
11064 +       if (nsi_lock)
11065 +               si_read_unlock(parent->d_sb);
11066 +       file = vfsub_dentry_open(path, au_dir_roflags);
11067 +       dentry = (void *)file;
11068 +       if (IS_ERR(file))
11069 +               goto out;
11070 +
11071 +       dentry = ERR_PTR(-ENOMEM);
11072 +       arg.name = (void *)__get_free_page(GFP_NOFS);
11073 +       if (unlikely(!arg.name))
11074 +               goto out_file;
11075 +       arg.ino = ino;
11076 +       arg.found = 0;
11077 +       do {
11078 +               arg.called = 0;
11079 +               /* smp_mb(); */
11080 +               err = vfsub_iterate_dir(file, &arg.ctx);
11081 +       } while (!err && !arg.found && arg.called);
11082 +       dentry = ERR_PTR(err);
11083 +       if (unlikely(err))
11084 +               goto out_name;
11085 +       /* instead of ENOENT */
11086 +       dentry = ERR_PTR(-ESTALE);
11087 +       if (!arg.found)
11088 +               goto out_name;
11089 +
11090 +       /* do not call vfsub_lkup_one() */
11091 +       dir = d_inode(parent);
11092 +       mutex_lock(&dir->i_mutex);
11093 +       dentry = vfsub_lookup_one_len(arg.name, parent, arg.namelen);
11094 +       mutex_unlock(&dir->i_mutex);
11095 +       AuTraceErrPtr(dentry);
11096 +       if (IS_ERR(dentry))
11097 +               goto out_name;
11098 +       AuDebugOn(au_test_anon(dentry));
11099 +       if (unlikely(d_really_is_negative(dentry))) {
11100 +               dput(dentry);
11101 +               dentry = ERR_PTR(-ENOENT);
11102 +       }
11103 +
11104 +out_name:
11105 +       free_page((unsigned long)arg.name);
11106 +out_file:
11107 +       fput(file);
11108 +out:
11109 +       if (unlikely(nsi_lock
11110 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11111 +               if (!IS_ERR(dentry)) {
11112 +                       dput(dentry);
11113 +                       dentry = ERR_PTR(-ESTALE);
11114 +               }
11115 +       AuTraceErrPtr(dentry);
11116 +       return dentry;
11117 +}
11118 +
11119 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11120 +                                       ino_t dir_ino,
11121 +                                       struct au_nfsd_si_lock *nsi_lock)
11122 +{
11123 +       struct dentry *dentry;
11124 +       struct path path;
11125 +
11126 +       if (dir_ino != AUFS_ROOT_INO) {
11127 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
11128 +               dentry = path.dentry;
11129 +               if (!path.dentry || IS_ERR(path.dentry))
11130 +                       goto out;
11131 +               AuDebugOn(au_test_anon(path.dentry));
11132 +       } else
11133 +               path.dentry = dget(sb->s_root);
11134 +
11135 +       path.mnt = au_mnt_get(sb);
11136 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11137 +       path_put(&path);
11138 +
11139 +out:
11140 +       AuTraceErrPtr(dentry);
11141 +       return dentry;
11142 +}
11143 +
11144 +/* ---------------------------------------------------------------------- */
11145 +
11146 +static int h_acceptable(void *expv, struct dentry *dentry)
11147 +{
11148 +       return 1;
11149 +}
11150 +
11151 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11152 +                          char *buf, int len, struct super_block *sb)
11153 +{
11154 +       char *p;
11155 +       int n;
11156 +       struct path path;
11157 +
11158 +       p = d_path(h_rootpath, buf, len);
11159 +       if (IS_ERR(p))
11160 +               goto out;
11161 +       n = strlen(p);
11162 +
11163 +       path.mnt = h_rootpath->mnt;
11164 +       path.dentry = h_parent;
11165 +       p = d_path(&path, buf, len);
11166 +       if (IS_ERR(p))
11167 +               goto out;
11168 +       if (n != 1)
11169 +               p += n;
11170 +
11171 +       path.mnt = au_mnt_get(sb);
11172 +       path.dentry = sb->s_root;
11173 +       p = d_path(&path, buf, len - strlen(p));
11174 +       mntput(path.mnt);
11175 +       if (IS_ERR(p))
11176 +               goto out;
11177 +       if (n != 1)
11178 +               p[strlen(p)] = '/';
11179 +
11180 +out:
11181 +       AuTraceErrPtr(p);
11182 +       return p;
11183 +}
11184 +
11185 +static
11186 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11187 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
11188 +{
11189 +       struct dentry *dentry, *h_parent, *root;
11190 +       struct super_block *h_sb;
11191 +       char *pathname, *p;
11192 +       struct vfsmount *h_mnt;
11193 +       struct au_branch *br;
11194 +       int err;
11195 +       struct path path;
11196 +
11197 +       br = au_sbr(sb, nsi_lock->bindex);
11198 +       h_mnt = au_br_mnt(br);
11199 +       h_sb = h_mnt->mnt_sb;
11200 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
11201 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11202 +                                     fh_len - Fh_tail, fh[Fh_h_type],
11203 +                                     h_acceptable, /*context*/NULL);
11204 +       dentry = h_parent;
11205 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
11206 +               AuWarn1("%s decode_fh failed, %ld\n",
11207 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
11208 +               goto out;
11209 +       }
11210 +       dentry = NULL;
11211 +       if (unlikely(au_test_anon(h_parent))) {
11212 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
11213 +                       au_sbtype(h_sb));
11214 +               goto out_h_parent;
11215 +       }
11216 +
11217 +       dentry = ERR_PTR(-ENOMEM);
11218 +       pathname = (void *)__get_free_page(GFP_NOFS);
11219 +       if (unlikely(!pathname))
11220 +               goto out_h_parent;
11221 +
11222 +       root = sb->s_root;
11223 +       path.mnt = h_mnt;
11224 +       di_read_lock_parent(root, !AuLock_IR);
11225 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
11226 +       di_read_unlock(root, !AuLock_IR);
11227 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11228 +       dentry = (void *)p;
11229 +       if (IS_ERR(p))
11230 +               goto out_pathname;
11231 +
11232 +       si_read_unlock(sb);
11233 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11234 +       dentry = ERR_PTR(err);
11235 +       if (unlikely(err))
11236 +               goto out_relock;
11237 +
11238 +       dentry = ERR_PTR(-ENOENT);
11239 +       AuDebugOn(au_test_anon(path.dentry));
11240 +       if (unlikely(d_really_is_negative(path.dentry)))
11241 +               goto out_path;
11242 +
11243 +       if (ino != d_inode(path.dentry)->i_ino)
11244 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11245 +       else
11246 +               dentry = dget(path.dentry);
11247 +
11248 +out_path:
11249 +       path_put(&path);
11250 +out_relock:
11251 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11252 +               if (!IS_ERR(dentry)) {
11253 +                       dput(dentry);
11254 +                       dentry = ERR_PTR(-ESTALE);
11255 +               }
11256 +out_pathname:
11257 +       free_page((unsigned long)pathname);
11258 +out_h_parent:
11259 +       dput(h_parent);
11260 +out:
11261 +       AuTraceErrPtr(dentry);
11262 +       return dentry;
11263 +}
11264 +
11265 +/* ---------------------------------------------------------------------- */
11266 +
11267 +static struct dentry *
11268 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11269 +                 int fh_type)
11270 +{
11271 +       struct dentry *dentry;
11272 +       __u32 *fh = fid->raw;
11273 +       struct au_branch *br;
11274 +       ino_t ino, dir_ino;
11275 +       struct au_nfsd_si_lock nsi_lock = {
11276 +               .force_lock     = 0
11277 +       };
11278 +
11279 +       dentry = ERR_PTR(-ESTALE);
11280 +       /* it should never happen, but the file handle is unreliable */
11281 +       if (unlikely(fh_len < Fh_tail))
11282 +               goto out;
11283 +       nsi_lock.sigen = fh[Fh_sigen];
11284 +       nsi_lock.br_id = fh[Fh_br_id];
11285 +
11286 +       /* branch id may be wrapped around */
11287 +       br = NULL;
11288 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
11289 +               goto out;
11290 +       nsi_lock.force_lock = 1;
11291 +
11292 +       /* is this inode still cached? */
11293 +       ino = decode_ino(fh + Fh_ino);
11294 +       /* it should never happen */
11295 +       if (unlikely(ino == AUFS_ROOT_INO))
11296 +               goto out;
11297 +
11298 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11299 +       dentry = decode_by_ino(sb, ino, dir_ino);
11300 +       if (IS_ERR(dentry))
11301 +               goto out_unlock;
11302 +       if (dentry)
11303 +               goto accept;
11304 +
11305 +       /* is the parent dir cached? */
11306 +       br = au_sbr(sb, nsi_lock.bindex);
11307 +       atomic_inc(&br->br_count);
11308 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11309 +       if (IS_ERR(dentry))
11310 +               goto out_unlock;
11311 +       if (dentry)
11312 +               goto accept;
11313 +
11314 +       /* lookup path */
11315 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
11316 +       if (IS_ERR(dentry))
11317 +               goto out_unlock;
11318 +       if (unlikely(!dentry))
11319 +               /* todo?: make it ESTALE */
11320 +               goto out_unlock;
11321 +
11322 +accept:
11323 +       if (!au_digen_test(dentry, au_sigen(sb))
11324 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
11325 +               goto out_unlock; /* success */
11326 +
11327 +       dput(dentry);
11328 +       dentry = ERR_PTR(-ESTALE);
11329 +out_unlock:
11330 +       if (br)
11331 +               atomic_dec(&br->br_count);
11332 +       si_read_unlock(sb);
11333 +out:
11334 +       AuTraceErrPtr(dentry);
11335 +       return dentry;
11336 +}
11337 +
11338 +#if 0 /* reserved for future use */
11339 +/* support subtreecheck option */
11340 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11341 +                                       int fh_len, int fh_type)
11342 +{
11343 +       struct dentry *parent;
11344 +       __u32 *fh = fid->raw;
11345 +       ino_t dir_ino;
11346 +
11347 +       dir_ino = decode_ino(fh + Fh_dir_ino);
11348 +       parent = decode_by_ino(sb, dir_ino, 0);
11349 +       if (IS_ERR(parent))
11350 +               goto out;
11351 +       if (!parent)
11352 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11353 +                                       dir_ino, fh, fh_len);
11354 +
11355 +out:
11356 +       AuTraceErrPtr(parent);
11357 +       return parent;
11358 +}
11359 +#endif
11360 +
11361 +/* ---------------------------------------------------------------------- */
11362 +
11363 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11364 +                         struct inode *dir)
11365 +{
11366 +       int err;
11367 +       aufs_bindex_t bindex;
11368 +       struct super_block *sb, *h_sb;
11369 +       struct dentry *dentry, *parent, *h_parent;
11370 +       struct inode *h_dir;
11371 +       struct au_branch *br;
11372 +
11373 +       err = -ENOSPC;
11374 +       if (unlikely(*max_len <= Fh_tail)) {
11375 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11376 +               goto out;
11377 +       }
11378 +
11379 +       err = FILEID_ROOT;
11380 +       if (inode->i_ino == AUFS_ROOT_INO) {
11381 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
11382 +               goto out;
11383 +       }
11384 +
11385 +       h_parent = NULL;
11386 +       sb = inode->i_sb;
11387 +       err = si_read_lock(sb, AuLock_FLUSH);
11388 +       if (unlikely(err))
11389 +               goto out;
11390 +
11391 +#ifdef CONFIG_AUFS_DEBUG
11392 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11393 +               AuWarn1("NFS-exporting requires xino\n");
11394 +#endif
11395 +       err = -EIO;
11396 +       parent = NULL;
11397 +       ii_read_lock_child(inode);
11398 +       bindex = au_ibstart(inode);
11399 +       if (!dir) {
11400 +               dentry = d_find_any_alias(inode);
11401 +               if (unlikely(!dentry))
11402 +                       goto out_unlock;
11403 +               AuDebugOn(au_test_anon(dentry));
11404 +               parent = dget_parent(dentry);
11405 +               dput(dentry);
11406 +               if (unlikely(!parent))
11407 +                       goto out_unlock;
11408 +               if (d_really_is_positive(parent))
11409 +                       dir = d_inode(parent);
11410 +       }
11411 +
11412 +       ii_read_lock_parent(dir);
11413 +       h_dir = au_h_iptr(dir, bindex);
11414 +       ii_read_unlock(dir);
11415 +       if (unlikely(!h_dir))
11416 +               goto out_parent;
11417 +       h_parent = d_find_any_alias(h_dir);
11418 +       if (unlikely(!h_parent))
11419 +               goto out_hparent;
11420 +
11421 +       err = -EPERM;
11422 +       br = au_sbr(sb, bindex);
11423 +       h_sb = au_br_sb(br);
11424 +       if (unlikely(!h_sb->s_export_op)) {
11425 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
11426 +               goto out_hparent;
11427 +       }
11428 +
11429 +       fh[Fh_br_id] = br->br_id;
11430 +       fh[Fh_sigen] = au_sigen(sb);
11431 +       encode_ino(fh + Fh_ino, inode->i_ino);
11432 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
11433 +       fh[Fh_igen] = inode->i_generation;
11434 +
11435 +       *max_len -= Fh_tail;
11436 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11437 +                                          max_len,
11438 +                                          /*connectable or subtreecheck*/0);
11439 +       err = fh[Fh_h_type];
11440 +       *max_len += Fh_tail;
11441 +       /* todo: macros? */
11442 +       if (err != FILEID_INVALID)
11443 +               err = 99;
11444 +       else
11445 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11446 +
11447 +out_hparent:
11448 +       dput(h_parent);
11449 +out_parent:
11450 +       dput(parent);
11451 +out_unlock:
11452 +       ii_read_unlock(inode);
11453 +       si_read_unlock(sb);
11454 +out:
11455 +       if (unlikely(err < 0))
11456 +               err = FILEID_INVALID;
11457 +       return err;
11458 +}
11459 +
11460 +/* ---------------------------------------------------------------------- */
11461 +
11462 +static int aufs_commit_metadata(struct inode *inode)
11463 +{
11464 +       int err;
11465 +       aufs_bindex_t bindex;
11466 +       struct super_block *sb;
11467 +       struct inode *h_inode;
11468 +       int (*f)(struct inode *inode);
11469 +
11470 +       sb = inode->i_sb;
11471 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
11472 +       ii_write_lock_child(inode);
11473 +       bindex = au_ibstart(inode);
11474 +       AuDebugOn(bindex < 0);
11475 +       h_inode = au_h_iptr(inode, bindex);
11476 +
11477 +       f = h_inode->i_sb->s_export_op->commit_metadata;
11478 +       if (f)
11479 +               err = f(h_inode);
11480 +       else {
11481 +               struct writeback_control wbc = {
11482 +                       .sync_mode      = WB_SYNC_ALL,
11483 +                       .nr_to_write    = 0 /* metadata only */
11484 +               };
11485 +
11486 +               err = sync_inode(h_inode, &wbc);
11487 +       }
11488 +
11489 +       au_cpup_attr_timesizes(inode);
11490 +       ii_write_unlock(inode);
11491 +       si_read_unlock(sb);
11492 +       return err;
11493 +}
11494 +
11495 +/* ---------------------------------------------------------------------- */
11496 +
11497 +static struct export_operations aufs_export_op = {
11498 +       .fh_to_dentry           = aufs_fh_to_dentry,
11499 +       /* .fh_to_parent        = aufs_fh_to_parent, */
11500 +       .encode_fh              = aufs_encode_fh,
11501 +       .commit_metadata        = aufs_commit_metadata
11502 +};
11503 +
11504 +void au_export_init(struct super_block *sb)
11505 +{
11506 +       struct au_sbinfo *sbinfo;
11507 +       __u32 u;
11508 +
11509 +       sb->s_export_op = &aufs_export_op;
11510 +       sbinfo = au_sbi(sb);
11511 +       sbinfo->si_xigen = NULL;
11512 +       get_random_bytes(&u, sizeof(u));
11513 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
11514 +       atomic_set(&sbinfo->si_xigen_next, u);
11515 +}
11516 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11517 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
11518 +++ linux/fs/aufs/fhsm.c        2015-09-24 10:47:58.251386326 +0200
11519 @@ -0,0 +1,426 @@
11520 +/*
11521 + * Copyright (C) 2011-2015 Junjiro R. Okajima
11522 + *
11523 + * This program, aufs is free software; you can redistribute it and/or modify
11524 + * it under the terms of the GNU General Public License as published by
11525 + * the Free Software Foundation; either version 2 of the License, or
11526 + * (at your option) any later version.
11527 + *
11528 + * This program is distributed in the hope that it will be useful,
11529 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11530 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11531 + * GNU General Public License for more details.
11532 + *
11533 + * You should have received a copy of the GNU General Public License
11534 + * along with this program; if not, write to the Free Software
11535 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
11536 + */
11537 +
11538 +/*
11539 + * File-based Hierarchy Storage Management
11540 + */
11541 +
11542 +#include <linux/anon_inodes.h>
11543 +#include <linux/poll.h>
11544 +#include <linux/seq_file.h>
11545 +#include <linux/statfs.h>
11546 +#include "aufs.h"
11547 +
11548 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11549 +{
11550 +       struct au_sbinfo *sbinfo;
11551 +       struct au_fhsm *fhsm;
11552 +
11553 +       SiMustAnyLock(sb);
11554 +
11555 +       sbinfo = au_sbi(sb);
11556 +       fhsm = &sbinfo->si_fhsm;
11557 +       AuDebugOn(!fhsm);
11558 +       return fhsm->fhsm_bottom;
11559 +}
11560 +
11561 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11562 +{
11563 +       struct au_sbinfo *sbinfo;
11564 +       struct au_fhsm *fhsm;
11565 +
11566 +       SiMustWriteLock(sb);
11567 +
11568 +       sbinfo = au_sbi(sb);
11569 +       fhsm = &sbinfo->si_fhsm;
11570 +       AuDebugOn(!fhsm);
11571 +       fhsm->fhsm_bottom = bindex;
11572 +}
11573 +
11574 +/* ---------------------------------------------------------------------- */
11575 +
11576 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11577 +{
11578 +       struct au_br_fhsm *bf;
11579 +
11580 +       bf = br->br_fhsm;
11581 +       MtxMustLock(&bf->bf_lock);
11582 +
11583 +       return !bf->bf_readable
11584 +               || time_after(jiffies,
11585 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11586 +}
11587 +
11588 +/* ---------------------------------------------------------------------- */
11589 +
11590 +static void au_fhsm_notify(struct super_block *sb, int val)
11591 +{
11592 +       struct au_sbinfo *sbinfo;
11593 +       struct au_fhsm *fhsm;
11594 +
11595 +       SiMustAnyLock(sb);
11596 +
11597 +       sbinfo = au_sbi(sb);
11598 +       fhsm = &sbinfo->si_fhsm;
11599 +       if (au_fhsm_pid(fhsm)
11600 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
11601 +               atomic_set(&fhsm->fhsm_readable, val);
11602 +               if (val)
11603 +                       wake_up(&fhsm->fhsm_wqh);
11604 +       }
11605 +}
11606 +
11607 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11608 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
11609 +{
11610 +       int err;
11611 +       struct au_branch *br;
11612 +       struct au_br_fhsm *bf;
11613 +
11614 +       br = au_sbr(sb, bindex);
11615 +       AuDebugOn(au_br_rdonly(br));
11616 +       bf = br->br_fhsm;
11617 +       AuDebugOn(!bf);
11618 +
11619 +       if (do_lock)
11620 +               mutex_lock(&bf->bf_lock);
11621 +       else
11622 +               MtxMustLock(&bf->bf_lock);
11623 +
11624 +       /* sb->s_root for NFS is unreliable */
11625 +       err = au_br_stfs(br, &bf->bf_stfs);
11626 +       if (unlikely(err)) {
11627 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11628 +               goto out;
11629 +       }
11630 +
11631 +       bf->bf_jiffy = jiffies;
11632 +       bf->bf_readable = 1;
11633 +       if (do_notify)
11634 +               au_fhsm_notify(sb, /*val*/1);
11635 +       if (rstfs)
11636 +               *rstfs = bf->bf_stfs;
11637 +
11638 +out:
11639 +       if (do_lock)
11640 +               mutex_unlock(&bf->bf_lock);
11641 +       au_fhsm_notify(sb, /*val*/1);
11642 +
11643 +       return err;
11644 +}
11645 +
11646 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11647 +{
11648 +       int err;
11649 +       struct au_sbinfo *sbinfo;
11650 +       struct au_fhsm *fhsm;
11651 +       struct au_branch *br;
11652 +       struct au_br_fhsm *bf;
11653 +
11654 +       AuDbg("b%d, force %d\n", bindex, force);
11655 +       SiMustAnyLock(sb);
11656 +
11657 +       sbinfo = au_sbi(sb);
11658 +       fhsm = &sbinfo->si_fhsm;
11659 +       if (!au_ftest_si(sbinfo, FHSM)
11660 +           || fhsm->fhsm_bottom == bindex)
11661 +               return;
11662 +
11663 +       br = au_sbr(sb, bindex);
11664 +       bf = br->br_fhsm;
11665 +       AuDebugOn(!bf);
11666 +       mutex_lock(&bf->bf_lock);
11667 +       if (force
11668 +           || au_fhsm_pid(fhsm)
11669 +           || au_fhsm_test_jiffy(sbinfo, br))
11670 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11671 +                                 /*do_notify*/1);
11672 +       mutex_unlock(&bf->bf_lock);
11673 +}
11674 +
11675 +void au_fhsm_wrote_all(struct super_block *sb, int force)
11676 +{
11677 +       aufs_bindex_t bindex, bend;
11678 +       struct au_branch *br;
11679 +
11680 +       /* exclude the bottom */
11681 +       bend = au_fhsm_bottom(sb);
11682 +       for (bindex = 0; bindex < bend; bindex++) {
11683 +               br = au_sbr(sb, bindex);
11684 +               if (au_br_fhsm(br->br_perm))
11685 +                       au_fhsm_wrote(sb, bindex, force);
11686 +       }
11687 +}
11688 +
11689 +/* ---------------------------------------------------------------------- */
11690 +
11691 +static unsigned int au_fhsm_poll(struct file *file,
11692 +                                struct poll_table_struct *wait)
11693 +{
11694 +       unsigned int mask;
11695 +       struct au_sbinfo *sbinfo;
11696 +       struct au_fhsm *fhsm;
11697 +
11698 +       mask = 0;
11699 +       sbinfo = file->private_data;
11700 +       fhsm = &sbinfo->si_fhsm;
11701 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
11702 +       if (atomic_read(&fhsm->fhsm_readable))
11703 +               mask = POLLIN /* | POLLRDNORM */;
11704 +
11705 +       AuTraceErr((int)mask);
11706 +       return mask;
11707 +}
11708 +
11709 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11710 +                             struct aufs_stfs *stfs, __s16 brid)
11711 +{
11712 +       int err;
11713 +
11714 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11715 +       if (!err)
11716 +               err = __put_user(brid, &stbr->brid);
11717 +       if (unlikely(err))
11718 +               err = -EFAULT;
11719 +
11720 +       return err;
11721 +}
11722 +
11723 +static ssize_t au_fhsm_do_read(struct super_block *sb,
11724 +                              struct aufs_stbr __user *stbr, size_t count)
11725 +{
11726 +       ssize_t err;
11727 +       int nstbr;
11728 +       aufs_bindex_t bindex, bend;
11729 +       struct au_branch *br;
11730 +       struct au_br_fhsm *bf;
11731 +
11732 +       /* except the bottom branch */
11733 +       err = 0;
11734 +       nstbr = 0;
11735 +       bend = au_fhsm_bottom(sb);
11736 +       for (bindex = 0; !err && bindex < bend; bindex++) {
11737 +               br = au_sbr(sb, bindex);
11738 +               if (!au_br_fhsm(br->br_perm))
11739 +                       continue;
11740 +
11741 +               bf = br->br_fhsm;
11742 +               mutex_lock(&bf->bf_lock);
11743 +               if (bf->bf_readable) {
11744 +                       err = -EFAULT;
11745 +                       if (count >= sizeof(*stbr))
11746 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
11747 +                                                         br->br_id);
11748 +                       if (!err) {
11749 +                               bf->bf_readable = 0;
11750 +                               count -= sizeof(*stbr);
11751 +                               nstbr++;
11752 +                       }
11753 +               }
11754 +               mutex_unlock(&bf->bf_lock);
11755 +       }
11756 +       if (!err)
11757 +               err = sizeof(*stbr) * nstbr;
11758 +
11759 +       return err;
11760 +}
11761 +
11762 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
11763 +                          loff_t *pos)
11764 +{
11765 +       ssize_t err;
11766 +       int readable;
11767 +       aufs_bindex_t nfhsm, bindex, bend;
11768 +       struct au_sbinfo *sbinfo;
11769 +       struct au_fhsm *fhsm;
11770 +       struct au_branch *br;
11771 +       struct super_block *sb;
11772 +
11773 +       err = 0;
11774 +       sbinfo = file->private_data;
11775 +       fhsm = &sbinfo->si_fhsm;
11776 +need_data:
11777 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
11778 +       if (!atomic_read(&fhsm->fhsm_readable)) {
11779 +               if (vfsub_file_flags(file) & O_NONBLOCK)
11780 +                       err = -EAGAIN;
11781 +               else
11782 +                       err = wait_event_interruptible_locked_irq
11783 +                               (fhsm->fhsm_wqh,
11784 +                                atomic_read(&fhsm->fhsm_readable));
11785 +       }
11786 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
11787 +       if (unlikely(err))
11788 +               goto out;
11789 +
11790 +       /* sb may already be dead */
11791 +       au_rw_read_lock(&sbinfo->si_rwsem);
11792 +       readable = atomic_read(&fhsm->fhsm_readable);
11793 +       if (readable > 0) {
11794 +               sb = sbinfo->si_sb;
11795 +               AuDebugOn(!sb);
11796 +               /* exclude the bottom branch */
11797 +               nfhsm = 0;
11798 +               bend = au_fhsm_bottom(sb);
11799 +               for (bindex = 0; bindex < bend; bindex++) {
11800 +                       br = au_sbr(sb, bindex);
11801 +                       if (au_br_fhsm(br->br_perm))
11802 +                               nfhsm++;
11803 +               }
11804 +               err = -EMSGSIZE;
11805 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
11806 +                       atomic_set(&fhsm->fhsm_readable, 0);
11807 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
11808 +                                            count);
11809 +               }
11810 +       }
11811 +       au_rw_read_unlock(&sbinfo->si_rwsem);
11812 +       if (!readable)
11813 +               goto need_data;
11814 +
11815 +out:
11816 +       return err;
11817 +}
11818 +
11819 +static int au_fhsm_release(struct inode *inode, struct file *file)
11820 +{
11821 +       struct au_sbinfo *sbinfo;
11822 +       struct au_fhsm *fhsm;
11823 +
11824 +       /* sb may already be dead */
11825 +       sbinfo = file->private_data;
11826 +       fhsm = &sbinfo->si_fhsm;
11827 +       spin_lock(&fhsm->fhsm_spin);
11828 +       fhsm->fhsm_pid = 0;
11829 +       spin_unlock(&fhsm->fhsm_spin);
11830 +       kobject_put(&sbinfo->si_kobj);
11831 +
11832 +       return 0;
11833 +}
11834 +
11835 +static const struct file_operations au_fhsm_fops = {
11836 +       .owner          = THIS_MODULE,
11837 +       .llseek         = noop_llseek,
11838 +       .read           = au_fhsm_read,
11839 +       .poll           = au_fhsm_poll,
11840 +       .release        = au_fhsm_release
11841 +};
11842 +
11843 +int au_fhsm_fd(struct super_block *sb, int oflags)
11844 +{
11845 +       int err, fd;
11846 +       struct au_sbinfo *sbinfo;
11847 +       struct au_fhsm *fhsm;
11848 +
11849 +       err = -EPERM;
11850 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
11851 +               goto out;
11852 +
11853 +       err = -EINVAL;
11854 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
11855 +               goto out;
11856 +
11857 +       err = 0;
11858 +       sbinfo = au_sbi(sb);
11859 +       fhsm = &sbinfo->si_fhsm;
11860 +       spin_lock(&fhsm->fhsm_spin);
11861 +       if (!fhsm->fhsm_pid)
11862 +               fhsm->fhsm_pid = current->pid;
11863 +       else
11864 +               err = -EBUSY;
11865 +       spin_unlock(&fhsm->fhsm_spin);
11866 +       if (unlikely(err))
11867 +               goto out;
11868 +
11869 +       oflags |= O_RDONLY;
11870 +       /* oflags |= FMODE_NONOTIFY; */
11871 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
11872 +       err = fd;
11873 +       if (unlikely(fd < 0))
11874 +               goto out_pid;
11875 +
11876 +       /* succeed reglardless 'fhsm' status */
11877 +       kobject_get(&sbinfo->si_kobj);
11878 +       si_noflush_read_lock(sb);
11879 +       if (au_ftest_si(sbinfo, FHSM))
11880 +               au_fhsm_wrote_all(sb, /*force*/0);
11881 +       si_read_unlock(sb);
11882 +       goto out; /* success */
11883 +
11884 +out_pid:
11885 +       spin_lock(&fhsm->fhsm_spin);
11886 +       fhsm->fhsm_pid = 0;
11887 +       spin_unlock(&fhsm->fhsm_spin);
11888 +out:
11889 +       AuTraceErr(err);
11890 +       return err;
11891 +}
11892 +
11893 +/* ---------------------------------------------------------------------- */
11894 +
11895 +int au_fhsm_br_alloc(struct au_branch *br)
11896 +{
11897 +       int err;
11898 +
11899 +       err = 0;
11900 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
11901 +       if (br->br_fhsm)
11902 +               au_br_fhsm_init(br->br_fhsm);
11903 +       else
11904 +               err = -ENOMEM;
11905 +
11906 +       return err;
11907 +}
11908 +
11909 +/* ---------------------------------------------------------------------- */
11910 +
11911 +void au_fhsm_fin(struct super_block *sb)
11912 +{
11913 +       au_fhsm_notify(sb, /*val*/-1);
11914 +}
11915 +
11916 +void au_fhsm_init(struct au_sbinfo *sbinfo)
11917 +{
11918 +       struct au_fhsm *fhsm;
11919 +
11920 +       fhsm = &sbinfo->si_fhsm;
11921 +       spin_lock_init(&fhsm->fhsm_spin);
11922 +       init_waitqueue_head(&fhsm->fhsm_wqh);
11923 +       atomic_set(&fhsm->fhsm_readable, 0);
11924 +       fhsm->fhsm_expire
11925 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
11926 +       fhsm->fhsm_bottom = -1;
11927 +}
11928 +
11929 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
11930 +{
11931 +       sbinfo->si_fhsm.fhsm_expire
11932 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
11933 +}
11934 +
11935 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
11936 +{
11937 +       unsigned int u;
11938 +
11939 +       if (!au_ftest_si(sbinfo, FHSM))
11940 +               return;
11941 +
11942 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
11943 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
11944 +               seq_printf(seq, ",fhsm_sec=%u", u);
11945 +}
11946 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
11947 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
11948 +++ linux/fs/aufs/file.c        2015-09-24 10:47:58.251386326 +0200
11949 @@ -0,0 +1,841 @@
11950 +/*
11951 + * Copyright (C) 2005-2015 Junjiro R. Okajima
11952 + *
11953 + * This program, aufs is free software; you can redistribute it and/or modify
11954 + * it under the terms of the GNU General Public License as published by
11955 + * the Free Software Foundation; either version 2 of the License, or
11956 + * (at your option) any later version.
11957 + *
11958 + * This program is distributed in the hope that it will be useful,
11959 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
11960 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11961 + * GNU General Public License for more details.
11962 + *
11963 + * You should have received a copy of the GNU General Public License
11964 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
11965 + */
11966 +
11967 +/*
11968 + * handling file/dir, and address_space operation
11969 + */
11970 +
11971 +#ifdef CONFIG_AUFS_DEBUG
11972 +#include <linux/migrate.h>
11973 +#endif
11974 +#include <linux/pagemap.h>
11975 +#include "aufs.h"
11976 +
11977 +/* drop flags for writing */
11978 +unsigned int au_file_roflags(unsigned int flags)
11979 +{
11980 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
11981 +       flags |= O_RDONLY | O_NOATIME;
11982 +       return flags;
11983 +}
11984 +
11985 +/* common functions to regular file and dir */
11986 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
11987 +                      struct file *file, int force_wr)
11988 +{
11989 +       struct file *h_file;
11990 +       struct dentry *h_dentry;
11991 +       struct inode *h_inode;
11992 +       struct super_block *sb;
11993 +       struct au_branch *br;
11994 +       struct path h_path;
11995 +       int err;
11996 +
11997 +       /* a race condition can happen between open and unlink/rmdir */
11998 +       h_file = ERR_PTR(-ENOENT);
11999 +       h_dentry = au_h_dptr(dentry, bindex);
12000 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
12001 +               goto out;
12002 +       h_inode = d_inode(h_dentry);
12003 +       spin_lock(&h_dentry->d_lock);
12004 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
12005 +               /* || !d_inode(dentry)->i_nlink */
12006 +               ;
12007 +       spin_unlock(&h_dentry->d_lock);
12008 +       if (unlikely(err))
12009 +               goto out;
12010 +
12011 +       sb = dentry->d_sb;
12012 +       br = au_sbr(sb, bindex);
12013 +       err = au_br_test_oflag(flags, br);
12014 +       h_file = ERR_PTR(err);
12015 +       if (unlikely(err))
12016 +               goto out;
12017 +
12018 +       /* drop flags for writing */
12019 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
12020 +               if (force_wr && !(flags & O_WRONLY))
12021 +                       force_wr = 0;
12022 +               flags = au_file_roflags(flags);
12023 +               if (force_wr) {
12024 +                       h_file = ERR_PTR(-EROFS);
12025 +                       flags = au_file_roflags(flags);
12026 +                       if (unlikely(vfsub_native_ro(h_inode)
12027 +                                    || IS_APPEND(h_inode)))
12028 +                               goto out;
12029 +                       flags &= ~O_ACCMODE;
12030 +                       flags |= O_WRONLY;
12031 +               }
12032 +       }
12033 +       flags &= ~O_CREAT;
12034 +       atomic_inc(&br->br_count);
12035 +       h_path.dentry = h_dentry;
12036 +       h_path.mnt = au_br_mnt(br);
12037 +       h_file = vfsub_dentry_open(&h_path, flags);
12038 +       if (IS_ERR(h_file))
12039 +               goto out_br;
12040 +
12041 +       if (flags & __FMODE_EXEC) {
12042 +               err = deny_write_access(h_file);
12043 +               if (unlikely(err)) {
12044 +                       fput(h_file);
12045 +                       h_file = ERR_PTR(err);
12046 +                       goto out_br;
12047 +               }
12048 +       }
12049 +       fsnotify_open(h_file);
12050 +       goto out; /* success */
12051 +
12052 +out_br:
12053 +       atomic_dec(&br->br_count);
12054 +out:
12055 +       return h_file;
12056 +}
12057 +
12058 +static int au_cmoo(struct dentry *dentry)
12059 +{
12060 +       int err, cmoo;
12061 +       unsigned int udba;
12062 +       struct path h_path;
12063 +       struct au_pin pin;
12064 +       struct au_cp_generic cpg = {
12065 +               .dentry = dentry,
12066 +               .bdst   = -1,
12067 +               .bsrc   = -1,
12068 +               .len    = -1,
12069 +               .pin    = &pin,
12070 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
12071 +       };
12072 +       struct inode *delegated;
12073 +       struct super_block *sb;
12074 +       struct au_sbinfo *sbinfo;
12075 +       struct au_fhsm *fhsm;
12076 +       pid_t pid;
12077 +       struct au_branch *br;
12078 +       struct dentry *parent;
12079 +       struct au_hinode *hdir;
12080 +
12081 +       DiMustWriteLock(dentry);
12082 +       IiMustWriteLock(d_inode(dentry));
12083 +
12084 +       err = 0;
12085 +       if (IS_ROOT(dentry))
12086 +               goto out;
12087 +       cpg.bsrc = au_dbstart(dentry);
12088 +       if (!cpg.bsrc)
12089 +               goto out;
12090 +
12091 +       sb = dentry->d_sb;
12092 +       sbinfo = au_sbi(sb);
12093 +       fhsm = &sbinfo->si_fhsm;
12094 +       pid = au_fhsm_pid(fhsm);
12095 +       if (pid
12096 +           && (current->pid == pid
12097 +               || current->real_parent->pid == pid))
12098 +               goto out;
12099 +
12100 +       br = au_sbr(sb, cpg.bsrc);
12101 +       cmoo = au_br_cmoo(br->br_perm);
12102 +       if (!cmoo)
12103 +               goto out;
12104 +       if (!d_is_reg(dentry))
12105 +               cmoo &= AuBrAttr_COO_ALL;
12106 +       if (!cmoo)
12107 +               goto out;
12108 +
12109 +       parent = dget_parent(dentry);
12110 +       di_write_lock_parent(parent);
12111 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12112 +       cpg.bdst = err;
12113 +       if (unlikely(err < 0)) {
12114 +               err = 0;        /* there is no upper writable branch */
12115 +               goto out_dgrade;
12116 +       }
12117 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12118 +
12119 +       /* do not respect the coo attrib for the target branch */
12120 +       err = au_cpup_dirs(dentry, cpg.bdst);
12121 +       if (unlikely(err))
12122 +               goto out_dgrade;
12123 +
12124 +       di_downgrade_lock(parent, AuLock_IR);
12125 +       udba = au_opt_udba(sb);
12126 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
12127 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12128 +       if (unlikely(err))
12129 +               goto out_parent;
12130 +
12131 +       err = au_sio_cpup_simple(&cpg);
12132 +       au_unpin(&pin);
12133 +       if (unlikely(err))
12134 +               goto out_parent;
12135 +       if (!(cmoo & AuBrWAttr_MOO))
12136 +               goto out_parent; /* success */
12137 +
12138 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
12139 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12140 +       if (unlikely(err))
12141 +               goto out_parent;
12142 +
12143 +       h_path.mnt = au_br_mnt(br);
12144 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
12145 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
12146 +       delegated = NULL;
12147 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12148 +       au_unpin(&pin);
12149 +       /* todo: keep h_dentry or not? */
12150 +       if (unlikely(err == -EWOULDBLOCK)) {
12151 +               pr_warn("cannot retry for NFSv4 delegation"
12152 +                       " for an internal unlink\n");
12153 +               iput(delegated);
12154 +       }
12155 +       if (unlikely(err)) {
12156 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
12157 +                      dentry, err);
12158 +               err = 0;
12159 +       }
12160 +       goto out_parent; /* success */
12161 +
12162 +out_dgrade:
12163 +       di_downgrade_lock(parent, AuLock_IR);
12164 +out_parent:
12165 +       di_read_unlock(parent, AuLock_IR);
12166 +       dput(parent);
12167 +out:
12168 +       AuTraceErr(err);
12169 +       return err;
12170 +}
12171 +
12172 +int au_do_open(struct file *file, struct au_do_open_args *args)
12173 +{
12174 +       int err, no_lock = args->no_lock;
12175 +       struct dentry *dentry;
12176 +       struct au_finfo *finfo;
12177 +
12178 +       if (!no_lock)
12179 +               err = au_finfo_init(file, args->fidir);
12180 +       else {
12181 +               lockdep_off();
12182 +               err = au_finfo_init(file, args->fidir);
12183 +               lockdep_on();
12184 +       }
12185 +       if (unlikely(err))
12186 +               goto out;
12187 +
12188 +       dentry = file->f_path.dentry;
12189 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
12190 +       if (!no_lock) {
12191 +               di_write_lock_child(dentry);
12192 +               err = au_cmoo(dentry);
12193 +               di_downgrade_lock(dentry, AuLock_IR);
12194 +               if (!err)
12195 +                       err = args->open(file, vfsub_file_flags(file), NULL);
12196 +               di_read_unlock(dentry, AuLock_IR);
12197 +       } else {
12198 +               err = au_cmoo(dentry);
12199 +               if (!err)
12200 +                       err = args->open(file, vfsub_file_flags(file),
12201 +                                        args->h_file);
12202 +               if (!err && au_fbstart(file) != au_dbstart(dentry))
12203 +                       /*
12204 +                        * cmoo happens after h_file was opened.
12205 +                        * need to refresh file later.
12206 +                        */
12207 +                       atomic_dec(&au_fi(file)->fi_generation);
12208 +       }
12209 +
12210 +       finfo = au_fi(file);
12211 +       if (!err) {
12212 +               finfo->fi_file = file;
12213 +               au_sphl_add(&finfo->fi_hlist,
12214 +                           &au_sbi(file->f_path.dentry->d_sb)->si_files);
12215 +       }
12216 +       if (!no_lock)
12217 +               fi_write_unlock(file);
12218 +       else {
12219 +               lockdep_off();
12220 +               fi_write_unlock(file);
12221 +               lockdep_on();
12222 +       }
12223 +       if (unlikely(err)) {
12224 +               finfo->fi_hdir = NULL;
12225 +               au_finfo_fin(file);
12226 +       }
12227 +
12228 +out:
12229 +       return err;
12230 +}
12231 +
12232 +int au_reopen_nondir(struct file *file)
12233 +{
12234 +       int err;
12235 +       aufs_bindex_t bstart;
12236 +       struct dentry *dentry;
12237 +       struct file *h_file, *h_file_tmp;
12238 +
12239 +       dentry = file->f_path.dentry;
12240 +       bstart = au_dbstart(dentry);
12241 +       h_file_tmp = NULL;
12242 +       if (au_fbstart(file) == bstart) {
12243 +               h_file = au_hf_top(file);
12244 +               if (file->f_mode == h_file->f_mode)
12245 +                       return 0; /* success */
12246 +               h_file_tmp = h_file;
12247 +               get_file(h_file_tmp);
12248 +               au_set_h_fptr(file, bstart, NULL);
12249 +       }
12250 +       AuDebugOn(au_fi(file)->fi_hdir);
12251 +       /*
12252 +        * it can happen
12253 +        * file exists on both of rw and ro
12254 +        * open --> dbstart and fbstart are both 0
12255 +        * prepend a branch as rw, "rw" become ro
12256 +        * remove rw/file
12257 +        * delete the top branch, "rw" becomes rw again
12258 +        *      --> dbstart is 1, fbstart is still 0
12259 +        * write --> fbstart is 0 but dbstart is 1
12260 +        */
12261 +       /* AuDebugOn(au_fbstart(file) < bstart); */
12262 +
12263 +       h_file = au_h_open(dentry, bstart, vfsub_file_flags(file) & ~O_TRUNC,
12264 +                          file, /*force_wr*/0);
12265 +       err = PTR_ERR(h_file);
12266 +       if (IS_ERR(h_file)) {
12267 +               if (h_file_tmp) {
12268 +                       atomic_inc(&au_sbr(dentry->d_sb, bstart)->br_count);
12269 +                       au_set_h_fptr(file, bstart, h_file_tmp);
12270 +                       h_file_tmp = NULL;
12271 +               }
12272 +               goto out; /* todo: close all? */
12273 +       }
12274 +
12275 +       err = 0;
12276 +       au_set_fbstart(file, bstart);
12277 +       au_set_h_fptr(file, bstart, h_file);
12278 +       au_update_figen(file);
12279 +       /* todo: necessary? */
12280 +       /* file->f_ra = h_file->f_ra; */
12281 +
12282 +out:
12283 +       if (h_file_tmp)
12284 +               fput(h_file_tmp);
12285 +       return err;
12286 +}
12287 +
12288 +/* ---------------------------------------------------------------------- */
12289 +
12290 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12291 +                       struct dentry *hi_wh)
12292 +{
12293 +       int err;
12294 +       aufs_bindex_t bstart;
12295 +       struct au_dinfo *dinfo;
12296 +       struct dentry *h_dentry;
12297 +       struct au_hdentry *hdp;
12298 +
12299 +       dinfo = au_di(file->f_path.dentry);
12300 +       AuRwMustWriteLock(&dinfo->di_rwsem);
12301 +
12302 +       bstart = dinfo->di_bstart;
12303 +       dinfo->di_bstart = btgt;
12304 +       hdp = dinfo->di_hdentry;
12305 +       h_dentry = hdp[0 + btgt].hd_dentry;
12306 +       hdp[0 + btgt].hd_dentry = hi_wh;
12307 +       err = au_reopen_nondir(file);
12308 +       hdp[0 + btgt].hd_dentry = h_dentry;
12309 +       dinfo->di_bstart = bstart;
12310 +
12311 +       return err;
12312 +}
12313 +
12314 +static int au_ready_to_write_wh(struct file *file, loff_t len,
12315 +                               aufs_bindex_t bcpup, struct au_pin *pin)
12316 +{
12317 +       int err;
12318 +       struct inode *inode, *h_inode;
12319 +       struct dentry *h_dentry, *hi_wh;
12320 +       struct au_cp_generic cpg = {
12321 +               .dentry = file->f_path.dentry,
12322 +               .bdst   = bcpup,
12323 +               .bsrc   = -1,
12324 +               .len    = len,
12325 +               .pin    = pin
12326 +       };
12327 +
12328 +       au_update_dbstart(cpg.dentry);
12329 +       inode = d_inode(cpg.dentry);
12330 +       h_inode = NULL;
12331 +       if (au_dbstart(cpg.dentry) <= bcpup
12332 +           && au_dbend(cpg.dentry) >= bcpup) {
12333 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
12334 +               if (h_dentry && d_is_positive(h_dentry))
12335 +                       h_inode = d_inode(h_dentry);
12336 +       }
12337 +       hi_wh = au_hi_wh(inode, bcpup);
12338 +       if (!hi_wh && !h_inode)
12339 +               err = au_sio_cpup_wh(&cpg, file);
12340 +       else
12341 +               /* already copied-up after unlink */
12342 +               err = au_reopen_wh(file, bcpup, hi_wh);
12343 +
12344 +       if (!err
12345 +           && (inode->i_nlink > 1
12346 +               || (inode->i_state & I_LINKABLE))
12347 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12348 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
12349 +
12350 +       return err;
12351 +}
12352 +
12353 +/*
12354 + * prepare the @file for writing.
12355 + */
12356 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
12357 +{
12358 +       int err;
12359 +       aufs_bindex_t dbstart;
12360 +       struct dentry *parent;
12361 +       struct inode *inode;
12362 +       struct super_block *sb;
12363 +       struct file *h_file;
12364 +       struct au_cp_generic cpg = {
12365 +               .dentry = file->f_path.dentry,
12366 +               .bdst   = -1,
12367 +               .bsrc   = -1,
12368 +               .len    = len,
12369 +               .pin    = pin,
12370 +               .flags  = AuCpup_DTIME
12371 +       };
12372 +
12373 +       sb = cpg.dentry->d_sb;
12374 +       inode = d_inode(cpg.dentry);
12375 +       cpg.bsrc = au_fbstart(file);
12376 +       err = au_test_ro(sb, cpg.bsrc, inode);
12377 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
12378 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12379 +                            /*flags*/0);
12380 +               goto out;
12381 +       }
12382 +
12383 +       /* need to cpup or reopen */
12384 +       parent = dget_parent(cpg.dentry);
12385 +       di_write_lock_parent(parent);
12386 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12387 +       cpg.bdst = err;
12388 +       if (unlikely(err < 0))
12389 +               goto out_dgrade;
12390 +       err = 0;
12391 +
12392 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12393 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
12394 +               if (unlikely(err))
12395 +                       goto out_dgrade;
12396 +       }
12397 +
12398 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12399 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12400 +       if (unlikely(err))
12401 +               goto out_dgrade;
12402 +
12403 +       dbstart = au_dbstart(cpg.dentry);
12404 +       if (dbstart <= cpg.bdst)
12405 +               cpg.bsrc = cpg.bdst;
12406 +
12407 +       if (dbstart <= cpg.bdst         /* just reopen */
12408 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
12409 +               ) {
12410 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
12411 +               if (IS_ERR(h_file))
12412 +                       err = PTR_ERR(h_file);
12413 +               else {
12414 +                       di_downgrade_lock(parent, AuLock_IR);
12415 +                       if (dbstart > cpg.bdst)
12416 +                               err = au_sio_cpup_simple(&cpg);
12417 +                       if (!err)
12418 +                               err = au_reopen_nondir(file);
12419 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
12420 +               }
12421 +       } else {                        /* copyup as wh and reopen */
12422 +               /*
12423 +                * since writable hfsplus branch is not supported,
12424 +                * h_open_pre/post() are unnecessary.
12425 +                */
12426 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
12427 +               di_downgrade_lock(parent, AuLock_IR);
12428 +       }
12429 +
12430 +       if (!err) {
12431 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
12432 +               goto out_dput; /* success */
12433 +       }
12434 +       au_unpin(pin);
12435 +       goto out_unlock;
12436 +
12437 +out_dgrade:
12438 +       di_downgrade_lock(parent, AuLock_IR);
12439 +out_unlock:
12440 +       di_read_unlock(parent, AuLock_IR);
12441 +out_dput:
12442 +       dput(parent);
12443 +out:
12444 +       return err;
12445 +}
12446 +
12447 +/* ---------------------------------------------------------------------- */
12448 +
12449 +int au_do_flush(struct file *file, fl_owner_t id,
12450 +               int (*flush)(struct file *file, fl_owner_t id))
12451 +{
12452 +       int err;
12453 +       struct super_block *sb;
12454 +       struct inode *inode;
12455 +
12456 +       inode = file_inode(file);
12457 +       sb = inode->i_sb;
12458 +       si_noflush_read_lock(sb);
12459 +       fi_read_lock(file);
12460 +       ii_read_lock_child(inode);
12461 +
12462 +       err = flush(file, id);
12463 +       au_cpup_attr_timesizes(inode);
12464 +
12465 +       ii_read_unlock(inode);
12466 +       fi_read_unlock(file);
12467 +       si_read_unlock(sb);
12468 +       return err;
12469 +}
12470 +
12471 +/* ---------------------------------------------------------------------- */
12472 +
12473 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
12474 +{
12475 +       int err;
12476 +       struct au_pin pin;
12477 +       struct au_finfo *finfo;
12478 +       struct dentry *parent, *hi_wh;
12479 +       struct inode *inode;
12480 +       struct super_block *sb;
12481 +       struct au_cp_generic cpg = {
12482 +               .dentry = file->f_path.dentry,
12483 +               .bdst   = -1,
12484 +               .bsrc   = -1,
12485 +               .len    = -1,
12486 +               .pin    = &pin,
12487 +               .flags  = AuCpup_DTIME
12488 +       };
12489 +
12490 +       FiMustWriteLock(file);
12491 +
12492 +       err = 0;
12493 +       finfo = au_fi(file);
12494 +       sb = cpg.dentry->d_sb;
12495 +       inode = d_inode(cpg.dentry);
12496 +       cpg.bdst = au_ibstart(inode);
12497 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
12498 +               goto out;
12499 +
12500 +       parent = dget_parent(cpg.dentry);
12501 +       if (au_test_ro(sb, cpg.bdst, inode)) {
12502 +               di_read_lock_parent(parent, !AuLock_IR);
12503 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12504 +               cpg.bdst = err;
12505 +               di_read_unlock(parent, !AuLock_IR);
12506 +               if (unlikely(err < 0))
12507 +                       goto out_parent;
12508 +               err = 0;
12509 +       }
12510 +
12511 +       di_read_lock_parent(parent, AuLock_IR);
12512 +       hi_wh = au_hi_wh(inode, cpg.bdst);
12513 +       if (!S_ISDIR(inode->i_mode)
12514 +           && au_opt_test(au_mntflags(sb), PLINK)
12515 +           && au_plink_test(inode)
12516 +           && !d_unhashed(cpg.dentry)
12517 +           && cpg.bdst < au_dbstart(cpg.dentry)) {
12518 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
12519 +               if (unlikely(err))
12520 +                       goto out_unlock;
12521 +
12522 +               /* always superio. */
12523 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
12524 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12525 +               if (!err) {
12526 +                       err = au_sio_cpup_simple(&cpg);
12527 +                       au_unpin(&pin);
12528 +               }
12529 +       } else if (hi_wh) {
12530 +               /* already copied-up after unlink */
12531 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
12532 +               *need_reopen = 0;
12533 +       }
12534 +
12535 +out_unlock:
12536 +       di_read_unlock(parent, AuLock_IR);
12537 +out_parent:
12538 +       dput(parent);
12539 +out:
12540 +       return err;
12541 +}
12542 +
12543 +static void au_do_refresh_dir(struct file *file)
12544 +{
12545 +       aufs_bindex_t bindex, bend, new_bindex, brid;
12546 +       struct au_hfile *p, tmp, *q;
12547 +       struct au_finfo *finfo;
12548 +       struct super_block *sb;
12549 +       struct au_fidir *fidir;
12550 +
12551 +       FiMustWriteLock(file);
12552 +
12553 +       sb = file->f_path.dentry->d_sb;
12554 +       finfo = au_fi(file);
12555 +       fidir = finfo->fi_hdir;
12556 +       AuDebugOn(!fidir);
12557 +       p = fidir->fd_hfile + finfo->fi_btop;
12558 +       brid = p->hf_br->br_id;
12559 +       bend = fidir->fd_bbot;
12560 +       for (bindex = finfo->fi_btop; bindex <= bend; bindex++, p++) {
12561 +               if (!p->hf_file)
12562 +                       continue;
12563 +
12564 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
12565 +               if (new_bindex == bindex)
12566 +                       continue;
12567 +               if (new_bindex < 0) {
12568 +                       au_set_h_fptr(file, bindex, NULL);
12569 +                       continue;
12570 +               }
12571 +
12572 +               /* swap two lower inode, and loop again */
12573 +               q = fidir->fd_hfile + new_bindex;
12574 +               tmp = *q;
12575 +               *q = *p;
12576 +               *p = tmp;
12577 +               if (tmp.hf_file) {
12578 +                       bindex--;
12579 +                       p--;
12580 +               }
12581 +       }
12582 +
12583 +       p = fidir->fd_hfile;
12584 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
12585 +               bend = au_sbend(sb);
12586 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bend;
12587 +                    finfo->fi_btop++, p++)
12588 +                       if (p->hf_file) {
12589 +                               if (file_inode(p->hf_file))
12590 +                                       break;
12591 +                               au_hfput(p, file);
12592 +                       }
12593 +       } else {
12594 +               bend = au_br_index(sb, brid);
12595 +               for (finfo->fi_btop = 0; finfo->fi_btop < bend;
12596 +                    finfo->fi_btop++, p++)
12597 +                       if (p->hf_file)
12598 +                               au_hfput(p, file);
12599 +               bend = au_sbend(sb);
12600 +       }
12601 +
12602 +       p = fidir->fd_hfile + bend;
12603 +       for (fidir->fd_bbot = bend; fidir->fd_bbot >= finfo->fi_btop;
12604 +            fidir->fd_bbot--, p--)
12605 +               if (p->hf_file) {
12606 +                       if (file_inode(p->hf_file))
12607 +                               break;
12608 +                       au_hfput(p, file);
12609 +               }
12610 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
12611 +}
12612 +
12613 +/*
12614 + * after branch manipulating, refresh the file.
12615 + */
12616 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
12617 +{
12618 +       int err, need_reopen;
12619 +       aufs_bindex_t bend, bindex;
12620 +       struct dentry *dentry;
12621 +       struct au_finfo *finfo;
12622 +       struct au_hfile *hfile;
12623 +
12624 +       dentry = file->f_path.dentry;
12625 +       finfo = au_fi(file);
12626 +       if (!finfo->fi_hdir) {
12627 +               hfile = &finfo->fi_htop;
12628 +               AuDebugOn(!hfile->hf_file);
12629 +               bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12630 +               AuDebugOn(bindex < 0);
12631 +               if (bindex != finfo->fi_btop)
12632 +                       au_set_fbstart(file, bindex);
12633 +       } else {
12634 +               err = au_fidir_realloc(finfo, au_sbend(dentry->d_sb) + 1);
12635 +               if (unlikely(err))
12636 +                       goto out;
12637 +               au_do_refresh_dir(file);
12638 +       }
12639 +
12640 +       err = 0;
12641 +       need_reopen = 1;
12642 +       if (!au_test_mmapped(file))
12643 +               err = au_file_refresh_by_inode(file, &need_reopen);
12644 +       if (!err && need_reopen && !d_unlinked(dentry))
12645 +               err = reopen(file);
12646 +       if (!err) {
12647 +               au_update_figen(file);
12648 +               goto out; /* success */
12649 +       }
12650 +
12651 +       /* error, close all lower files */
12652 +       if (finfo->fi_hdir) {
12653 +               bend = au_fbend_dir(file);
12654 +               for (bindex = au_fbstart(file); bindex <= bend; bindex++)
12655 +                       au_set_h_fptr(file, bindex, NULL);
12656 +       }
12657 +
12658 +out:
12659 +       return err;
12660 +}
12661 +
12662 +/* common function to regular file and dir */
12663 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12664 +                         int wlock)
12665 +{
12666 +       int err;
12667 +       unsigned int sigen, figen;
12668 +       aufs_bindex_t bstart;
12669 +       unsigned char pseudo_link;
12670 +       struct dentry *dentry;
12671 +       struct inode *inode;
12672 +
12673 +       err = 0;
12674 +       dentry = file->f_path.dentry;
12675 +       inode = d_inode(dentry);
12676 +       sigen = au_sigen(dentry->d_sb);
12677 +       fi_write_lock(file);
12678 +       figen = au_figen(file);
12679 +       di_write_lock_child(dentry);
12680 +       bstart = au_dbstart(dentry);
12681 +       pseudo_link = (bstart != au_ibstart(inode));
12682 +       if (sigen == figen && !pseudo_link && au_fbstart(file) == bstart) {
12683 +               if (!wlock) {
12684 +                       di_downgrade_lock(dentry, AuLock_IR);
12685 +                       fi_downgrade_lock(file);
12686 +               }
12687 +               goto out; /* success */
12688 +       }
12689 +
12690 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
12691 +       if (au_digen_test(dentry, sigen)) {
12692 +               err = au_reval_dpath(dentry, sigen);
12693 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
12694 +       }
12695 +
12696 +       if (!err)
12697 +               err = refresh_file(file, reopen);
12698 +       if (!err) {
12699 +               if (!wlock) {
12700 +                       di_downgrade_lock(dentry, AuLock_IR);
12701 +                       fi_downgrade_lock(file);
12702 +               }
12703 +       } else {
12704 +               di_write_unlock(dentry);
12705 +               fi_write_unlock(file);
12706 +       }
12707 +
12708 +out:
12709 +       return err;
12710 +}
12711 +
12712 +/* ---------------------------------------------------------------------- */
12713 +
12714 +/* cf. aufs_nopage() */
12715 +/* for madvise(2) */
12716 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
12717 +{
12718 +       unlock_page(page);
12719 +       return 0;
12720 +}
12721 +
12722 +/* it will never be called, but necessary to support O_DIRECT */
12723 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
12724 +                             loff_t offset)
12725 +{ BUG(); return 0; }
12726 +
12727 +/* they will never be called. */
12728 +#ifdef CONFIG_AUFS_DEBUG
12729 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
12730 +                           loff_t pos, unsigned len, unsigned flags,
12731 +                           struct page **pagep, void **fsdata)
12732 +{ AuUnsupport(); return 0; }
12733 +static int aufs_write_end(struct file *file, struct address_space *mapping,
12734 +                         loff_t pos, unsigned len, unsigned copied,
12735 +                         struct page *page, void *fsdata)
12736 +{ AuUnsupport(); return 0; }
12737 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
12738 +{ AuUnsupport(); return 0; }
12739 +
12740 +static int aufs_set_page_dirty(struct page *page)
12741 +{ AuUnsupport(); return 0; }
12742 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
12743 +                               unsigned int length)
12744 +{ AuUnsupport(); }
12745 +static int aufs_releasepage(struct page *page, gfp_t gfp)
12746 +{ AuUnsupport(); return 0; }
12747 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
12748 +                           struct page *page, enum migrate_mode mode)
12749 +{ AuUnsupport(); return 0; }
12750 +static int aufs_launder_page(struct page *page)
12751 +{ AuUnsupport(); return 0; }
12752 +static int aufs_is_partially_uptodate(struct page *page,
12753 +                                     unsigned long from,
12754 +                                     unsigned long count)
12755 +{ AuUnsupport(); return 0; }
12756 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
12757 +                                   bool *writeback)
12758 +{ AuUnsupport(); }
12759 +static int aufs_error_remove_page(struct address_space *mapping,
12760 +                                 struct page *page)
12761 +{ AuUnsupport(); return 0; }
12762 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
12763 +                             sector_t *span)
12764 +{ AuUnsupport(); return 0; }
12765 +static void aufs_swap_deactivate(struct file *file)
12766 +{ AuUnsupport(); }
12767 +#endif /* CONFIG_AUFS_DEBUG */
12768 +
12769 +const struct address_space_operations aufs_aop = {
12770 +       .readpage               = aufs_readpage,
12771 +       .direct_IO              = aufs_direct_IO,
12772 +#ifdef CONFIG_AUFS_DEBUG
12773 +       .writepage              = aufs_writepage,
12774 +       /* no writepages, because of writepage */
12775 +       .set_page_dirty         = aufs_set_page_dirty,
12776 +       /* no readpages, because of readpage */
12777 +       .write_begin            = aufs_write_begin,
12778 +       .write_end              = aufs_write_end,
12779 +       /* no bmap, no block device */
12780 +       .invalidatepage         = aufs_invalidatepage,
12781 +       .releasepage            = aufs_releasepage,
12782 +       .migratepage            = aufs_migratepage,
12783 +       .launder_page           = aufs_launder_page,
12784 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
12785 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
12786 +       .error_remove_page      = aufs_error_remove_page,
12787 +       .swap_activate          = aufs_swap_activate,
12788 +       .swap_deactivate        = aufs_swap_deactivate
12789 +#endif /* CONFIG_AUFS_DEBUG */
12790 +};
12791 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
12792 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
12793 +++ linux/fs/aufs/file.h        2015-09-24 10:47:58.251386326 +0200
12794 @@ -0,0 +1,291 @@
12795 +/*
12796 + * Copyright (C) 2005-2015 Junjiro R. Okajima
12797 + *
12798 + * This program, aufs is free software; you can redistribute it and/or modify
12799 + * it under the terms of the GNU General Public License as published by
12800 + * the Free Software Foundation; either version 2 of the License, or
12801 + * (at your option) any later version.
12802 + *
12803 + * This program is distributed in the hope that it will be useful,
12804 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12805 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12806 + * GNU General Public License for more details.
12807 + *
12808 + * You should have received a copy of the GNU General Public License
12809 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12810 + */
12811 +
12812 +/*
12813 + * file operations
12814 + */
12815 +
12816 +#ifndef __AUFS_FILE_H__
12817 +#define __AUFS_FILE_H__
12818 +
12819 +#ifdef __KERNEL__
12820 +
12821 +#include <linux/file.h>
12822 +#include <linux/fs.h>
12823 +#include <linux/poll.h>
12824 +#include "rwsem.h"
12825 +
12826 +struct au_branch;
12827 +struct au_hfile {
12828 +       struct file             *hf_file;
12829 +       struct au_branch        *hf_br;
12830 +};
12831 +
12832 +struct au_vdir;
12833 +struct au_fidir {
12834 +       aufs_bindex_t           fd_bbot;
12835 +       aufs_bindex_t           fd_nent;
12836 +       struct au_vdir          *fd_vdir_cache;
12837 +       struct au_hfile         fd_hfile[];
12838 +};
12839 +
12840 +static inline int au_fidir_sz(int nent)
12841 +{
12842 +       AuDebugOn(nent < 0);
12843 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
12844 +}
12845 +
12846 +struct au_finfo {
12847 +       atomic_t                fi_generation;
12848 +
12849 +       struct au_rwsem         fi_rwsem;
12850 +       aufs_bindex_t           fi_btop;
12851 +
12852 +       /* do not union them */
12853 +       struct {                                /* for non-dir */
12854 +               struct au_hfile                 fi_htop;
12855 +               atomic_t                        fi_mmapped;
12856 +       };
12857 +       struct au_fidir         *fi_hdir;       /* for dir only */
12858 +
12859 +       struct hlist_node       fi_hlist;
12860 +       struct file             *fi_file;       /* very ugly */
12861 +} ____cacheline_aligned_in_smp;
12862 +
12863 +/* ---------------------------------------------------------------------- */
12864 +
12865 +/* file.c */
12866 +extern const struct address_space_operations aufs_aop;
12867 +unsigned int au_file_roflags(unsigned int flags);
12868 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
12869 +                      struct file *file, int force_wr);
12870 +struct au_do_open_args {
12871 +       int             no_lock;
12872 +       int             (*open)(struct file *file, int flags,
12873 +                               struct file *h_file);
12874 +       struct au_fidir *fidir;
12875 +       struct file     *h_file;
12876 +};
12877 +int au_do_open(struct file *file, struct au_do_open_args *args);
12878 +int au_reopen_nondir(struct file *file);
12879 +struct au_pin;
12880 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
12881 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12882 +                         int wlock);
12883 +int au_do_flush(struct file *file, fl_owner_t id,
12884 +               int (*flush)(struct file *file, fl_owner_t id));
12885 +
12886 +/* poll.c */
12887 +#ifdef CONFIG_AUFS_POLL
12888 +unsigned int aufs_poll(struct file *file, poll_table *wait);
12889 +#endif
12890 +
12891 +#ifdef CONFIG_AUFS_BR_HFSPLUS
12892 +/* hfsplus.c */
12893 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
12894 +                          int force_wr);
12895 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
12896 +                   struct file *h_file);
12897 +#else
12898 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
12899 +       aufs_bindex_t bindex, int force_wr)
12900 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
12901 +          struct file *h_file);
12902 +#endif
12903 +
12904 +/* f_op.c */
12905 +extern const struct file_operations aufs_file_fop;
12906 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
12907 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
12908 +struct file *au_read_pre(struct file *file, int keep_fi);
12909 +
12910 +/* finfo.c */
12911 +void au_hfput(struct au_hfile *hf, struct file *file);
12912 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
12913 +                  struct file *h_file);
12914 +
12915 +void au_update_figen(struct file *file);
12916 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
12917 +int au_fidir_realloc(struct au_finfo *finfo, int nbr);
12918 +
12919 +void au_fi_init_once(void *_fi);
12920 +void au_finfo_fin(struct file *file);
12921 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
12922 +
12923 +/* ioctl.c */
12924 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
12925 +#ifdef CONFIG_COMPAT
12926 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
12927 +                          unsigned long arg);
12928 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
12929 +                             unsigned long arg);
12930 +#endif
12931 +
12932 +/* ---------------------------------------------------------------------- */
12933 +
12934 +static inline struct au_finfo *au_fi(struct file *file)
12935 +{
12936 +       return file->private_data;
12937 +}
12938 +
12939 +/* ---------------------------------------------------------------------- */
12940 +
12941 +/*
12942 + * fi_read_lock, fi_write_lock,
12943 + * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
12944 + */
12945 +AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
12946 +
12947 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
12948 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
12949 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
12950 +
12951 +/* ---------------------------------------------------------------------- */
12952 +
12953 +/* todo: hard/soft set? */
12954 +static inline aufs_bindex_t au_fbstart(struct file *file)
12955 +{
12956 +       FiMustAnyLock(file);
12957 +       return au_fi(file)->fi_btop;
12958 +}
12959 +
12960 +static inline aufs_bindex_t au_fbend_dir(struct file *file)
12961 +{
12962 +       FiMustAnyLock(file);
12963 +       AuDebugOn(!au_fi(file)->fi_hdir);
12964 +       return au_fi(file)->fi_hdir->fd_bbot;
12965 +}
12966 +
12967 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
12968 +{
12969 +       FiMustAnyLock(file);
12970 +       AuDebugOn(!au_fi(file)->fi_hdir);
12971 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
12972 +}
12973 +
12974 +static inline void au_set_fbstart(struct file *file, aufs_bindex_t bindex)
12975 +{
12976 +       FiMustWriteLock(file);
12977 +       au_fi(file)->fi_btop = bindex;
12978 +}
12979 +
12980 +static inline void au_set_fbend_dir(struct file *file, aufs_bindex_t bindex)
12981 +{
12982 +       FiMustWriteLock(file);
12983 +       AuDebugOn(!au_fi(file)->fi_hdir);
12984 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
12985 +}
12986 +
12987 +static inline void au_set_fvdir_cache(struct file *file,
12988 +                                     struct au_vdir *vdir_cache)
12989 +{
12990 +       FiMustWriteLock(file);
12991 +       AuDebugOn(!au_fi(file)->fi_hdir);
12992 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
12993 +}
12994 +
12995 +static inline struct file *au_hf_top(struct file *file)
12996 +{
12997 +       FiMustAnyLock(file);
12998 +       AuDebugOn(au_fi(file)->fi_hdir);
12999 +       return au_fi(file)->fi_htop.hf_file;
13000 +}
13001 +
13002 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13003 +{
13004 +       FiMustAnyLock(file);
13005 +       AuDebugOn(!au_fi(file)->fi_hdir);
13006 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
13007 +}
13008 +
13009 +/* todo: memory barrier? */
13010 +static inline unsigned int au_figen(struct file *f)
13011 +{
13012 +       return atomic_read(&au_fi(f)->fi_generation);
13013 +}
13014 +
13015 +static inline void au_set_mmapped(struct file *f)
13016 +{
13017 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13018 +               return;
13019 +       pr_warn("fi_mmapped wrapped around\n");
13020 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13021 +               ;
13022 +}
13023 +
13024 +static inline void au_unset_mmapped(struct file *f)
13025 +{
13026 +       atomic_dec(&au_fi(f)->fi_mmapped);
13027 +}
13028 +
13029 +static inline int au_test_mmapped(struct file *f)
13030 +{
13031 +       return atomic_read(&au_fi(f)->fi_mmapped);
13032 +}
13033 +
13034 +/* customize vma->vm_file */
13035 +
13036 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13037 +                                      struct file *file)
13038 +{
13039 +       struct file *f;
13040 +
13041 +       f = vma->vm_file;
13042 +       get_file(file);
13043 +       vma->vm_file = file;
13044 +       fput(f);
13045 +}
13046 +
13047 +#ifdef CONFIG_MMU
13048 +#define AuDbgVmRegion(file, vma) do {} while (0)
13049 +
13050 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13051 +                                   struct file *file)
13052 +{
13053 +       au_do_vm_file_reset(vma, file);
13054 +}
13055 +#else
13056 +#define AuDbgVmRegion(file, vma) \
13057 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13058 +
13059 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
13060 +                                   struct file *file)
13061 +{
13062 +       struct file *f;
13063 +
13064 +       au_do_vm_file_reset(vma, file);
13065 +       f = vma->vm_region->vm_file;
13066 +       get_file(file);
13067 +       vma->vm_region->vm_file = file;
13068 +       fput(f);
13069 +}
13070 +#endif /* CONFIG_MMU */
13071 +
13072 +/* handle vma->vm_prfile */
13073 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
13074 +                                   struct file *file)
13075 +{
13076 +       get_file(file);
13077 +       vma->vm_prfile = file;
13078 +#ifndef CONFIG_MMU
13079 +       get_file(file);
13080 +       vma->vm_region->vm_prfile = file;
13081 +#endif
13082 +}
13083 +
13084 +#endif /* __KERNEL__ */
13085 +#endif /* __AUFS_FILE_H__ */
13086 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13087 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
13088 +++ linux/fs/aufs/finfo.c       2015-09-24 10:47:58.251386326 +0200
13089 @@ -0,0 +1,157 @@
13090 +/*
13091 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13092 + *
13093 + * This program, aufs is free software; you can redistribute it and/or modify
13094 + * it under the terms of the GNU General Public License as published by
13095 + * the Free Software Foundation; either version 2 of the License, or
13096 + * (at your option) any later version.
13097 + *
13098 + * This program is distributed in the hope that it will be useful,
13099 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13100 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13101 + * GNU General Public License for more details.
13102 + *
13103 + * You should have received a copy of the GNU General Public License
13104 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13105 + */
13106 +
13107 +/*
13108 + * file private data
13109 + */
13110 +
13111 +#include "aufs.h"
13112 +
13113 +void au_hfput(struct au_hfile *hf, struct file *file)
13114 +{
13115 +       /* todo: direct access f_flags */
13116 +       if (vfsub_file_flags(file) & __FMODE_EXEC)
13117 +               allow_write_access(hf->hf_file);
13118 +       fput(hf->hf_file);
13119 +       hf->hf_file = NULL;
13120 +       atomic_dec(&hf->hf_br->br_count);
13121 +       hf->hf_br = NULL;
13122 +}
13123 +
13124 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13125 +{
13126 +       struct au_finfo *finfo = au_fi(file);
13127 +       struct au_hfile *hf;
13128 +       struct au_fidir *fidir;
13129 +
13130 +       fidir = finfo->fi_hdir;
13131 +       if (!fidir) {
13132 +               AuDebugOn(finfo->fi_btop != bindex);
13133 +               hf = &finfo->fi_htop;
13134 +       } else
13135 +               hf = fidir->fd_hfile + bindex;
13136 +
13137 +       if (hf && hf->hf_file)
13138 +               au_hfput(hf, file);
13139 +       if (val) {
13140 +               FiMustWriteLock(file);
13141 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
13142 +               hf->hf_file = val;
13143 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
13144 +       }
13145 +}
13146 +
13147 +void au_update_figen(struct file *file)
13148 +{
13149 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
13150 +       /* smp_mb(); */ /* atomic_set */
13151 +}
13152 +
13153 +/* ---------------------------------------------------------------------- */
13154 +
13155 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
13156 +{
13157 +       struct au_fidir *fidir;
13158 +       int nbr;
13159 +
13160 +       nbr = au_sbend(sb) + 1;
13161 +       if (nbr < 2)
13162 +               nbr = 2; /* initial allocate for 2 branches */
13163 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13164 +       if (fidir) {
13165 +               fidir->fd_bbot = -1;
13166 +               fidir->fd_nent = nbr;
13167 +               fidir->fd_vdir_cache = NULL;
13168 +       }
13169 +
13170 +       return fidir;
13171 +}
13172 +
13173 +int au_fidir_realloc(struct au_finfo *finfo, int nbr)
13174 +{
13175 +       int err;
13176 +       struct au_fidir *fidir, *p;
13177 +
13178 +       AuRwMustWriteLock(&finfo->fi_rwsem);
13179 +       fidir = finfo->fi_hdir;
13180 +       AuDebugOn(!fidir);
13181 +
13182 +       err = -ENOMEM;
13183 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13184 +                        GFP_NOFS);
13185 +       if (p) {
13186 +               p->fd_nent = nbr;
13187 +               finfo->fi_hdir = p;
13188 +               err = 0;
13189 +       }
13190 +
13191 +       return err;
13192 +}
13193 +
13194 +/* ---------------------------------------------------------------------- */
13195 +
13196 +void au_finfo_fin(struct file *file)
13197 +{
13198 +       struct au_finfo *finfo;
13199 +
13200 +       au_nfiles_dec(file->f_path.dentry->d_sb);
13201 +
13202 +       finfo = au_fi(file);
13203 +       AuDebugOn(finfo->fi_hdir);
13204 +       AuRwDestroy(&finfo->fi_rwsem);
13205 +       au_cache_free_finfo(finfo);
13206 +}
13207 +
13208 +void au_fi_init_once(void *_finfo)
13209 +{
13210 +       struct au_finfo *finfo = _finfo;
13211 +       static struct lock_class_key aufs_fi;
13212 +
13213 +       au_rw_init(&finfo->fi_rwsem);
13214 +       au_rw_class(&finfo->fi_rwsem, &aufs_fi);
13215 +}
13216 +
13217 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
13218 +{
13219 +       int err;
13220 +       struct au_finfo *finfo;
13221 +       struct dentry *dentry;
13222 +
13223 +       err = -ENOMEM;
13224 +       dentry = file->f_path.dentry;
13225 +       finfo = au_cache_alloc_finfo();
13226 +       if (unlikely(!finfo))
13227 +               goto out;
13228 +
13229 +       err = 0;
13230 +       au_nfiles_inc(dentry->d_sb);
13231 +       /* verbose coding for lock class name */
13232 +       if (!fidir)
13233 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcNonDir_FIINFO);
13234 +       else
13235 +               au_rw_class(&finfo->fi_rwsem, au_lc_key + AuLcDir_FIINFO);
13236 +       au_rw_write_lock(&finfo->fi_rwsem);
13237 +       finfo->fi_btop = -1;
13238 +       finfo->fi_hdir = fidir;
13239 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
13240 +       /* smp_mb(); */ /* atomic_set */
13241 +
13242 +       file->private_data = finfo;
13243 +
13244 +out:
13245 +       return err;
13246 +}
13247 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13248 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
13249 +++ linux/fs/aufs/f_op.c        2015-09-24 10:47:58.251386326 +0200
13250 @@ -0,0 +1,738 @@
13251 +/*
13252 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13253 + *
13254 + * This program, aufs is free software; you can redistribute it and/or modify
13255 + * it under the terms of the GNU General Public License as published by
13256 + * the Free Software Foundation; either version 2 of the License, or
13257 + * (at your option) any later version.
13258 + *
13259 + * This program is distributed in the hope that it will be useful,
13260 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13261 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13262 + * GNU General Public License for more details.
13263 + *
13264 + * You should have received a copy of the GNU General Public License
13265 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
13266 + */
13267 +
13268 +/*
13269 + * file and vm operations
13270 + */
13271 +
13272 +#include <linux/aio.h>
13273 +#include <linux/fs_stack.h>
13274 +#include <linux/mman.h>
13275 +#include <linux/security.h>
13276 +#include "aufs.h"
13277 +
13278 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
13279 +{
13280 +       int err;
13281 +       aufs_bindex_t bindex;
13282 +       struct dentry *dentry;
13283 +       struct au_finfo *finfo;
13284 +       struct inode *h_inode;
13285 +
13286 +       FiMustWriteLock(file);
13287 +
13288 +       err = 0;
13289 +       dentry = file->f_path.dentry;
13290 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
13291 +       finfo = au_fi(file);
13292 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
13293 +       atomic_set(&finfo->fi_mmapped, 0);
13294 +       bindex = au_dbstart(dentry);
13295 +       if (!h_file)
13296 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
13297 +       else
13298 +               get_file(h_file);
13299 +       if (IS_ERR(h_file))
13300 +               err = PTR_ERR(h_file);
13301 +       else {
13302 +               if ((flags & __O_TMPFILE)
13303 +                   && !(flags & O_EXCL)) {
13304 +                       h_inode = file_inode(h_file);
13305 +                       spin_lock(&h_inode->i_lock);
13306 +                       h_inode->i_state |= I_LINKABLE;
13307 +                       spin_unlock(&h_inode->i_lock);
13308 +               }
13309 +               au_set_fbstart(file, bindex);
13310 +               au_set_h_fptr(file, bindex, h_file);
13311 +               au_update_figen(file);
13312 +               /* todo: necessary? */
13313 +               /* file->f_ra = h_file->f_ra; */
13314 +       }
13315 +
13316 +       return err;
13317 +}
13318 +
13319 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
13320 +                           struct file *file)
13321 +{
13322 +       int err;
13323 +       struct super_block *sb;
13324 +       struct au_do_open_args args = {
13325 +               .open   = au_do_open_nondir
13326 +       };
13327 +
13328 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13329 +             file, vfsub_file_flags(file), file->f_mode);
13330 +
13331 +       sb = file->f_path.dentry->d_sb;
13332 +       si_read_lock(sb, AuLock_FLUSH);
13333 +       err = au_do_open(file, &args);
13334 +       si_read_unlock(sb);
13335 +       return err;
13336 +}
13337 +
13338 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13339 +{
13340 +       struct au_finfo *finfo;
13341 +       aufs_bindex_t bindex;
13342 +
13343 +       finfo = au_fi(file);
13344 +       au_sphl_del(&finfo->fi_hlist,
13345 +                   &au_sbi(file->f_path.dentry->d_sb)->si_files);
13346 +       bindex = finfo->fi_btop;
13347 +       if (bindex >= 0)
13348 +               au_set_h_fptr(file, bindex, NULL);
13349 +
13350 +       au_finfo_fin(file);
13351 +       return 0;
13352 +}
13353 +
13354 +/* ---------------------------------------------------------------------- */
13355 +
13356 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
13357 +{
13358 +       int err;
13359 +       struct file *h_file;
13360 +
13361 +       err = 0;
13362 +       h_file = au_hf_top(file);
13363 +       if (h_file)
13364 +               err = vfsub_flush(h_file, id);
13365 +       return err;
13366 +}
13367 +
13368 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13369 +{
13370 +       return au_do_flush(file, id, au_do_flush_nondir);
13371 +}
13372 +
13373 +/* ---------------------------------------------------------------------- */
13374 +/*
13375 + * read and write functions acquire [fdi]_rwsem once, but release before
13376 + * mmap_sem. This is because to stop a race condition between mmap(2).
13377 + * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13378 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13379 + * read functions after [fdi]_rwsem are released, but it should be harmless.
13380 + */
13381 +
13382 +/* Callers should call au_read_post() or fput() in the end */
13383 +struct file *au_read_pre(struct file *file, int keep_fi)
13384 +{
13385 +       struct file *h_file;
13386 +       int err;
13387 +
13388 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
13389 +       if (!err) {
13390 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
13391 +               h_file = au_hf_top(file);
13392 +               get_file(h_file);
13393 +               if (!keep_fi)
13394 +                       fi_read_unlock(file);
13395 +       } else
13396 +               h_file = ERR_PTR(err);
13397 +
13398 +       return h_file;
13399 +}
13400 +
13401 +static void au_read_post(struct inode *inode, struct file *h_file)
13402 +{
13403 +       /* update without lock, I don't think it a problem */
13404 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
13405 +       fput(h_file);
13406 +}
13407 +
13408 +struct au_write_pre {
13409 +       blkcnt_t blks;
13410 +       aufs_bindex_t bstart;
13411 +};
13412 +
13413 +/*
13414 + * return with iinfo is write-locked
13415 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13416 + * end
13417 + */
13418 +static struct file *au_write_pre(struct file *file, int do_ready,
13419 +                                struct au_write_pre *wpre)
13420 +{
13421 +       struct file *h_file;
13422 +       struct dentry *dentry;
13423 +       int err;
13424 +       struct au_pin pin;
13425 +
13426 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13427 +       h_file = ERR_PTR(err);
13428 +       if (unlikely(err))
13429 +               goto out;
13430 +
13431 +       dentry = file->f_path.dentry;
13432 +       if (do_ready) {
13433 +               err = au_ready_to_write(file, -1, &pin);
13434 +               if (unlikely(err)) {
13435 +                       h_file = ERR_PTR(err);
13436 +                       di_write_unlock(dentry);
13437 +                       goto out_fi;
13438 +               }
13439 +       }
13440 +
13441 +       di_downgrade_lock(dentry, /*flags*/0);
13442 +       if (wpre)
13443 +               wpre->bstart = au_fbstart(file);
13444 +       h_file = au_hf_top(file);
13445 +       get_file(h_file);
13446 +       if (wpre)
13447 +               wpre->blks = file_inode(h_file)->i_blocks;
13448 +       if (do_ready)
13449 +               au_unpin(&pin);
13450 +       di_read_unlock(dentry, /*flags*/0);
13451 +
13452 +out_fi:
13453 +       fi_write_unlock(file);
13454 +out:
13455 +       return h_file;
13456 +}
13457 +
13458 +static void au_write_post(struct inode *inode, struct file *h_file,
13459 +                         struct au_write_pre *wpre, ssize_t written)
13460 +{
13461 +       struct inode *h_inode;
13462 +
13463 +       au_cpup_attr_timesizes(inode);
13464 +       AuDebugOn(au_ibstart(inode) != wpre->bstart);
13465 +       h_inode = file_inode(h_file);
13466 +       inode->i_mode = h_inode->i_mode;
13467 +       ii_write_unlock(inode);
13468 +       fput(h_file);
13469 +
13470 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13471 +       if (written > 0)
13472 +               au_fhsm_wrote(inode->i_sb, wpre->bstart,
13473 +                             /*force*/h_inode->i_blocks > wpre->blks);
13474 +}
13475 +
13476 +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13477 +                        loff_t *ppos)
13478 +{
13479 +       ssize_t err;
13480 +       struct inode *inode;
13481 +       struct file *h_file;
13482 +       struct super_block *sb;
13483 +
13484 +       inode = file_inode(file);
13485 +       sb = inode->i_sb;
13486 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13487 +
13488 +       h_file = au_read_pre(file, /*keep_fi*/0);
13489 +       err = PTR_ERR(h_file);
13490 +       if (IS_ERR(h_file))
13491 +               goto out;
13492 +
13493 +       /* filedata may be obsoleted by concurrent copyup, but no problem */
13494 +       err = vfsub_read_u(h_file, buf, count, ppos);
13495 +       /* todo: necessary? */
13496 +       /* file->f_ra = h_file->f_ra; */
13497 +       au_read_post(inode, h_file);
13498 +
13499 +out:
13500 +       si_read_unlock(sb);
13501 +       return err;
13502 +}
13503 +
13504 +/*
13505 + * todo: very ugly
13506 + * it locks both of i_mutex and si_rwsem for read in safe.
13507 + * if the plink maintenance mode continues forever (that is the problem),
13508 + * may loop forever.
13509 + */
13510 +static void au_mtx_and_read_lock(struct inode *inode)
13511 +{
13512 +       int err;
13513 +       struct super_block *sb = inode->i_sb;
13514 +
13515 +       while (1) {
13516 +               mutex_lock(&inode->i_mutex);
13517 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13518 +               if (!err)
13519 +                       break;
13520 +               mutex_unlock(&inode->i_mutex);
13521 +               si_read_lock(sb, AuLock_NOPLMW);
13522 +               si_read_unlock(sb);
13523 +       }
13524 +}
13525 +
13526 +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13527 +                         size_t count, loff_t *ppos)
13528 +{
13529 +       ssize_t err;
13530 +       struct au_write_pre wpre;
13531 +       struct inode *inode;
13532 +       struct file *h_file;
13533 +       char __user *buf = (char __user *)ubuf;
13534 +
13535 +       inode = file_inode(file);
13536 +       au_mtx_and_read_lock(inode);
13537 +
13538 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13539 +       err = PTR_ERR(h_file);
13540 +       if (IS_ERR(h_file))
13541 +               goto out;
13542 +
13543 +       err = vfsub_write_u(h_file, buf, count, ppos);
13544 +       au_write_post(inode, h_file, &wpre, err);
13545 +
13546 +out:
13547 +       si_read_unlock(inode->i_sb);
13548 +       mutex_unlock(&inode->i_mutex);
13549 +       return err;
13550 +}
13551 +
13552 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13553 +                         struct iov_iter *iov_iter)
13554 +{
13555 +       ssize_t err;
13556 +       struct file *file;
13557 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
13558 +
13559 +       err = security_file_permission(h_file, rw);
13560 +       if (unlikely(err))
13561 +               goto out;
13562 +
13563 +       err = -ENOSYS;
13564 +       iter = NULL;
13565 +       if (rw == MAY_READ)
13566 +               iter = h_file->f_op->read_iter;
13567 +       else if (rw == MAY_WRITE)
13568 +               iter = h_file->f_op->write_iter;
13569 +
13570 +       file = kio->ki_filp;
13571 +       kio->ki_filp = h_file;
13572 +       if (iter) {
13573 +               lockdep_off();
13574 +               err = iter(kio, iov_iter);
13575 +               lockdep_on();
13576 +       } else
13577 +               /* currently there is no such fs */
13578 +               WARN_ON_ONCE(1);
13579 +       kio->ki_filp = file;
13580 +
13581 +out:
13582 +       return err;
13583 +}
13584 +
13585 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13586 +{
13587 +       ssize_t err;
13588 +       struct file *file, *h_file;
13589 +       struct inode *inode;
13590 +       struct super_block *sb;
13591 +
13592 +       file = kio->ki_filp;
13593 +       inode = file_inode(file);
13594 +       sb = inode->i_sb;
13595 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13596 +
13597 +       h_file = au_read_pre(file, /*keep_fi*/0);
13598 +       err = PTR_ERR(h_file);
13599 +       if (IS_ERR(h_file))
13600 +               goto out;
13601 +
13602 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
13603 +       /* todo: necessary? */
13604 +       /* file->f_ra = h_file->f_ra; */
13605 +       au_read_post(inode, h_file);
13606 +
13607 +out:
13608 +       si_read_unlock(sb);
13609 +       return err;
13610 +}
13611 +
13612 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
13613 +{
13614 +       ssize_t err;
13615 +       struct au_write_pre wpre;
13616 +       struct inode *inode;
13617 +       struct file *file, *h_file;
13618 +
13619 +       file = kio->ki_filp;
13620 +       inode = file_inode(file);
13621 +       au_mtx_and_read_lock(inode);
13622 +
13623 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13624 +       err = PTR_ERR(h_file);
13625 +       if (IS_ERR(h_file))
13626 +               goto out;
13627 +
13628 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
13629 +       au_write_post(inode, h_file, &wpre, err);
13630 +
13631 +out:
13632 +       si_read_unlock(inode->i_sb);
13633 +       mutex_unlock(&inode->i_mutex);
13634 +       return err;
13635 +}
13636 +
13637 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13638 +                               struct pipe_inode_info *pipe, size_t len,
13639 +                               unsigned int flags)
13640 +{
13641 +       ssize_t err;
13642 +       struct file *h_file;
13643 +       struct inode *inode;
13644 +       struct super_block *sb;
13645 +
13646 +       inode = file_inode(file);
13647 +       sb = inode->i_sb;
13648 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13649 +
13650 +       h_file = au_read_pre(file, /*keep_fi*/1);
13651 +       err = PTR_ERR(h_file);
13652 +       if (IS_ERR(h_file))
13653 +               goto out;
13654 +
13655 +       if (au_test_loopback_kthread()) {
13656 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
13657 +               if (file->f_mapping != h_file->f_mapping) {
13658 +                       file->f_mapping = h_file->f_mapping;
13659 +                       smp_mb(); /* unnecessary? */
13660 +               }
13661 +       }
13662 +       fi_read_unlock(file);
13663 +
13664 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13665 +       /* todo: necessasry? */
13666 +       /* file->f_ra = h_file->f_ra; */
13667 +       au_read_post(inode, h_file);
13668 +
13669 +out:
13670 +       si_read_unlock(sb);
13671 +       return err;
13672 +}
13673 +
13674 +static ssize_t
13675 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13676 +                 size_t len, unsigned int flags)
13677 +{
13678 +       ssize_t err;
13679 +       struct au_write_pre wpre;
13680 +       struct inode *inode;
13681 +       struct file *h_file;
13682 +
13683 +       inode = file_inode(file);
13684 +       au_mtx_and_read_lock(inode);
13685 +
13686 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13687 +       err = PTR_ERR(h_file);
13688 +       if (IS_ERR(h_file))
13689 +               goto out;
13690 +
13691 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
13692 +       au_write_post(inode, h_file, &wpre, err);
13693 +
13694 +out:
13695 +       si_read_unlock(inode->i_sb);
13696 +       mutex_unlock(&inode->i_mutex);
13697 +       return err;
13698 +}
13699 +
13700 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13701 +                          loff_t len)
13702 +{
13703 +       long err;
13704 +       struct au_write_pre wpre;
13705 +       struct inode *inode;
13706 +       struct file *h_file;
13707 +
13708 +       inode = file_inode(file);
13709 +       au_mtx_and_read_lock(inode);
13710 +
13711 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13712 +       err = PTR_ERR(h_file);
13713 +       if (IS_ERR(h_file))
13714 +               goto out;
13715 +
13716 +       lockdep_off();
13717 +       err = vfs_fallocate(h_file, mode, offset, len);
13718 +       lockdep_on();
13719 +       au_write_post(inode, h_file, &wpre, /*written*/1);
13720 +
13721 +out:
13722 +       si_read_unlock(inode->i_sb);
13723 +       mutex_unlock(&inode->i_mutex);
13724 +       return err;
13725 +}
13726 +
13727 +/* ---------------------------------------------------------------------- */
13728 +
13729 +/*
13730 + * The locking order around current->mmap_sem.
13731 + * - in most and regular cases
13732 + *   file I/O syscall -- aufs_read() or something
13733 + *     -- si_rwsem for read -- mmap_sem
13734 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
13735 + * - in mmap case
13736 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
13737 + * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
13738 + * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
13739 + * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
13740 + * It means that when aufs acquires si_rwsem for write, the process should never
13741 + * acquire mmap_sem.
13742 + *
13743 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
13744 + * problem either since any directory is not able to be mmap-ed.
13745 + * The similar scenario is applied to aufs_readlink() too.
13746 + */
13747 +
13748 +#if 0 /* stop calling security_file_mmap() */
13749 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
13750 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
13751 +
13752 +static unsigned long au_arch_prot_conv(unsigned long flags)
13753 +{
13754 +       /* currently ppc64 only */
13755 +#ifdef CONFIG_PPC64
13756 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
13757 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
13758 +       return AuConv_VM_PROT(flags, SAO);
13759 +#else
13760 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
13761 +       return 0;
13762 +#endif
13763 +}
13764 +
13765 +static unsigned long au_prot_conv(unsigned long flags)
13766 +{
13767 +       return AuConv_VM_PROT(flags, READ)
13768 +               | AuConv_VM_PROT(flags, WRITE)
13769 +               | AuConv_VM_PROT(flags, EXEC)
13770 +               | au_arch_prot_conv(flags);
13771 +}
13772 +
13773 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
13774 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
13775 +
13776 +static unsigned long au_flag_conv(unsigned long flags)
13777 +{
13778 +       return AuConv_VM_MAP(flags, GROWSDOWN)
13779 +               | AuConv_VM_MAP(flags, DENYWRITE)
13780 +               | AuConv_VM_MAP(flags, LOCKED);
13781 +}
13782 +#endif
13783 +
13784 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
13785 +{
13786 +       int err;
13787 +       const unsigned char wlock
13788 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
13789 +       struct super_block *sb;
13790 +       struct file *h_file;
13791 +       struct inode *inode;
13792 +
13793 +       AuDbgVmRegion(file, vma);
13794 +
13795 +       inode = file_inode(file);
13796 +       sb = inode->i_sb;
13797 +       lockdep_off();
13798 +       si_read_lock(sb, AuLock_NOPLMW);
13799 +
13800 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
13801 +       lockdep_on();
13802 +       err = PTR_ERR(h_file);
13803 +       if (IS_ERR(h_file))
13804 +               goto out;
13805 +
13806 +       err = 0;
13807 +       au_set_mmapped(file);
13808 +       au_vm_file_reset(vma, h_file);
13809 +       /*
13810 +        * we cannot call security_mmap_file() here since it may acquire
13811 +        * mmap_sem or i_mutex.
13812 +        *
13813 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
13814 +        *                       au_flag_conv(vma->vm_flags));
13815 +        */
13816 +       if (!err)
13817 +               err = h_file->f_op->mmap(h_file, vma);
13818 +       if (!err) {
13819 +               au_vm_prfile_set(vma, file);
13820 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
13821 +               goto out_fput; /* success */
13822 +       }
13823 +       au_unset_mmapped(file);
13824 +       au_vm_file_reset(vma, file);
13825 +
13826 +out_fput:
13827 +       lockdep_off();
13828 +       ii_write_unlock(inode);
13829 +       lockdep_on();
13830 +       fput(h_file);
13831 +out:
13832 +       lockdep_off();
13833 +       si_read_unlock(sb);
13834 +       lockdep_on();
13835 +       AuTraceErr(err);
13836 +       return err;
13837 +}
13838 +
13839 +/* ---------------------------------------------------------------------- */
13840 +
13841 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
13842 +                            int datasync)
13843 +{
13844 +       int err;
13845 +       struct au_write_pre wpre;
13846 +       struct inode *inode;
13847 +       struct file *h_file;
13848 +
13849 +       err = 0; /* -EBADF; */ /* posix? */
13850 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13851 +               goto out;
13852 +
13853 +       inode = file_inode(file);
13854 +       au_mtx_and_read_lock(inode);
13855 +
13856 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13857 +       err = PTR_ERR(h_file);
13858 +       if (IS_ERR(h_file))
13859 +               goto out_unlock;
13860 +
13861 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
13862 +       au_write_post(inode, h_file, &wpre, /*written*/0);
13863 +
13864 +out_unlock:
13865 +       si_read_unlock(inode->i_sb);
13866 +       mutex_unlock(&inode->i_mutex);
13867 +out:
13868 +       return err;
13869 +}
13870 +
13871 +/* no one supports this operation, currently */
13872 +#if 0
13873 +static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
13874 +{
13875 +       int err;
13876 +       struct au_write_pre wpre;
13877 +       struct inode *inode;
13878 +       struct file *file, *h_file;
13879 +
13880 +       err = 0; /* -EBADF; */ /* posix? */
13881 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
13882 +               goto out;
13883 +
13884 +       file = kio->ki_filp;
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_unlock;
13892 +
13893 +       err = -ENOSYS;
13894 +       h_file = au_hf_top(file);
13895 +       if (h_file->f_op->aio_fsync) {
13896 +               struct mutex *h_mtx;
13897 +
13898 +               h_mtx = &file_inode(h_file)->i_mutex;
13899 +               if (!is_sync_kiocb(kio)) {
13900 +                       get_file(h_file);
13901 +                       fput(file);
13902 +               }
13903 +               kio->ki_filp = h_file;
13904 +               err = h_file->f_op->aio_fsync(kio, datasync);
13905 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
13906 +               if (!err)
13907 +                       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
13908 +               /*ignore*/
13909 +               mutex_unlock(h_mtx);
13910 +       }
13911 +       au_write_post(inode, h_file, &wpre, /*written*/0);
13912 +
13913 +out_unlock:
13914 +       si_read_unlock(inode->sb);
13915 +       mutex_unlock(&inode->i_mutex);
13916 +out:
13917 +       return err;
13918 +}
13919 +#endif
13920 +
13921 +static int aufs_fasync(int fd, struct file *file, int flag)
13922 +{
13923 +       int err;
13924 +       struct file *h_file;
13925 +       struct super_block *sb;
13926 +
13927 +       sb = file->f_path.dentry->d_sb;
13928 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13929 +
13930 +       h_file = au_read_pre(file, /*keep_fi*/0);
13931 +       err = PTR_ERR(h_file);
13932 +       if (IS_ERR(h_file))
13933 +               goto out;
13934 +
13935 +       if (h_file->f_op->fasync)
13936 +               err = h_file->f_op->fasync(fd, h_file, flag);
13937 +       fput(h_file); /* instead of au_read_post() */
13938 +
13939 +out:
13940 +       si_read_unlock(sb);
13941 +       return err;
13942 +}
13943 +
13944 +/* ---------------------------------------------------------------------- */
13945 +
13946 +/* no one supports this operation, currently */
13947 +#if 0
13948 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
13949 +                            size_t len, loff_t *pos, int more)
13950 +{
13951 +}
13952 +#endif
13953 +
13954 +/* ---------------------------------------------------------------------- */
13955 +
13956 +const struct file_operations aufs_file_fop = {
13957 +       .owner          = THIS_MODULE,
13958 +
13959 +       .llseek         = default_llseek,
13960 +
13961 +       .read           = aufs_read,
13962 +       .write          = aufs_write,
13963 +       .read_iter      = aufs_read_iter,
13964 +       .write_iter     = aufs_write_iter,
13965 +
13966 +#ifdef CONFIG_AUFS_POLL
13967 +       .poll           = aufs_poll,
13968 +#endif
13969 +       .unlocked_ioctl = aufs_ioctl_nondir,
13970 +#ifdef CONFIG_COMPAT
13971 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
13972 +#endif
13973 +       .mmap           = aufs_mmap,
13974 +       .open           = aufs_open_nondir,
13975 +       .flush          = aufs_flush_nondir,
13976 +       .release        = aufs_release_nondir,
13977 +       .fsync          = aufs_fsync_nondir,
13978 +       /* .aio_fsync   = aufs_aio_fsync_nondir, */
13979 +       .fasync         = aufs_fasync,
13980 +       /* .sendpage    = aufs_sendpage, */
13981 +       .splice_write   = aufs_splice_write,
13982 +       .splice_read    = aufs_splice_read,
13983 +#if 0
13984 +       .aio_splice_write = aufs_aio_splice_write,
13985 +       .aio_splice_read  = aufs_aio_splice_read,
13986 +#endif
13987 +       .fallocate      = aufs_fallocate
13988 +};
13989 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
13990 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
13991 +++ linux/fs/aufs/fstype.h      2015-09-24 10:47:58.254719746 +0200
13992 @@ -0,0 +1,400 @@
13993 +/*
13994 + * Copyright (C) 2005-2015 Junjiro R. Okajima
13995 + *
13996 + * This program, aufs is free software; you can redistribute it and/or modify
13997 + * it under the terms of the GNU General Public License as published by
13998 + * the Free Software Foundation; either version 2 of the License, or
13999 + * (at your option) any later version.
14000 + *
14001 + * This program is distributed in the hope that it will be useful,
14002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14004 + * GNU General Public License for more details.
14005 + *
14006 + * You should have received a copy of the GNU General Public License
14007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14008 + */
14009 +
14010 +/*
14011 + * judging filesystem type
14012 + */
14013 +
14014 +#ifndef __AUFS_FSTYPE_H__
14015 +#define __AUFS_FSTYPE_H__
14016 +
14017 +#ifdef __KERNEL__
14018 +
14019 +#include <linux/fs.h>
14020 +#include <linux/magic.h>
14021 +#include <linux/romfs_fs.h>
14022 +#include <linux/nfs_fs.h>
14023 +
14024 +static inline int au_test_aufs(struct super_block *sb)
14025 +{
14026 +       return sb->s_magic == AUFS_SUPER_MAGIC;
14027 +}
14028 +
14029 +static inline const char *au_sbtype(struct super_block *sb)
14030 +{
14031 +       return sb->s_type->name;
14032 +}
14033 +
14034 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14035 +{
14036 +#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
14037 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
14038 +#else
14039 +       return 0;
14040 +#endif
14041 +}
14042 +
14043 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
14044 +{
14045 +#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
14046 +       return sb->s_magic == ROMFS_MAGIC;
14047 +#else
14048 +       return 0;
14049 +#endif
14050 +}
14051 +
14052 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
14053 +{
14054 +#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
14055 +       return sb->s_magic == CRAMFS_MAGIC;
14056 +#endif
14057 +       return 0;
14058 +}
14059 +
14060 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14061 +{
14062 +#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
14063 +       return sb->s_magic == NFS_SUPER_MAGIC;
14064 +#else
14065 +       return 0;
14066 +#endif
14067 +}
14068 +
14069 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
14070 +{
14071 +#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
14072 +       return sb->s_magic == FUSE_SUPER_MAGIC;
14073 +#else
14074 +       return 0;
14075 +#endif
14076 +}
14077 +
14078 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
14079 +{
14080 +#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
14081 +       return sb->s_magic == XFS_SB_MAGIC;
14082 +#else
14083 +       return 0;
14084 +#endif
14085 +}
14086 +
14087 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
14088 +{
14089 +#ifdef CONFIG_TMPFS
14090 +       return sb->s_magic == TMPFS_MAGIC;
14091 +#else
14092 +       return 0;
14093 +#endif
14094 +}
14095 +
14096 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
14097 +{
14098 +#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
14099 +       return !strcmp(au_sbtype(sb), "ecryptfs");
14100 +#else
14101 +       return 0;
14102 +#endif
14103 +}
14104 +
14105 +static inline int au_test_ramfs(struct super_block *sb)
14106 +{
14107 +       return sb->s_magic == RAMFS_MAGIC;
14108 +}
14109 +
14110 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14111 +{
14112 +#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
14113 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
14114 +#else
14115 +       return 0;
14116 +#endif
14117 +}
14118 +
14119 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14120 +{
14121 +#ifdef CONFIG_PROC_FS
14122 +       return sb->s_magic == PROC_SUPER_MAGIC;
14123 +#else
14124 +       return 0;
14125 +#endif
14126 +}
14127 +
14128 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14129 +{
14130 +#ifdef CONFIG_SYSFS
14131 +       return sb->s_magic == SYSFS_MAGIC;
14132 +#else
14133 +       return 0;
14134 +#endif
14135 +}
14136 +
14137 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14138 +{
14139 +#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
14140 +       return sb->s_magic == CONFIGFS_MAGIC;
14141 +#else
14142 +       return 0;
14143 +#endif
14144 +}
14145 +
14146 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
14147 +{
14148 +#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
14149 +       return sb->s_magic == MINIX3_SUPER_MAGIC
14150 +               || sb->s_magic == MINIX2_SUPER_MAGIC
14151 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
14152 +               || sb->s_magic == MINIX_SUPER_MAGIC
14153 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
14154 +#else
14155 +       return 0;
14156 +#endif
14157 +}
14158 +
14159 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
14160 +{
14161 +#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
14162 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
14163 +#else
14164 +       return 0;
14165 +#endif
14166 +}
14167 +
14168 +static inline int au_test_msdos(struct super_block *sb)
14169 +{
14170 +       return au_test_fat(sb);
14171 +}
14172 +
14173 +static inline int au_test_vfat(struct super_block *sb)
14174 +{
14175 +       return au_test_fat(sb);
14176 +}
14177 +
14178 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14179 +{
14180 +#ifdef CONFIG_SECURITYFS
14181 +       return sb->s_magic == SECURITYFS_MAGIC;
14182 +#else
14183 +       return 0;
14184 +#endif
14185 +}
14186 +
14187 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14188 +{
14189 +#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
14190 +       return sb->s_magic == SQUASHFS_MAGIC;
14191 +#else
14192 +       return 0;
14193 +#endif
14194 +}
14195 +
14196 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14197 +{
14198 +#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
14199 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
14200 +#else
14201 +       return 0;
14202 +#endif
14203 +}
14204 +
14205 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14206 +{
14207 +#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
14208 +       return sb->s_magic == XENFS_SUPER_MAGIC;
14209 +#else
14210 +       return 0;
14211 +#endif
14212 +}
14213 +
14214 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14215 +{
14216 +#ifdef CONFIG_DEBUG_FS
14217 +       return sb->s_magic == DEBUGFS_MAGIC;
14218 +#else
14219 +       return 0;
14220 +#endif
14221 +}
14222 +
14223 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14224 +{
14225 +#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
14226 +       return sb->s_magic == NILFS_SUPER_MAGIC;
14227 +#else
14228 +       return 0;
14229 +#endif
14230 +}
14231 +
14232 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14233 +{
14234 +#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
14235 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14236 +#else
14237 +       return 0;
14238 +#endif
14239 +}
14240 +
14241 +/* ---------------------------------------------------------------------- */
14242 +/*
14243 + * they can't be an aufs branch.
14244 + */
14245 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
14246 +{
14247 +       return
14248 +#ifndef CONFIG_AUFS_BR_RAMFS
14249 +               au_test_ramfs(sb) ||
14250 +#endif
14251 +               au_test_procfs(sb)
14252 +               || au_test_sysfs(sb)
14253 +               || au_test_configfs(sb)
14254 +               || au_test_debugfs(sb)
14255 +               || au_test_securityfs(sb)
14256 +               || au_test_xenfs(sb)
14257 +               || au_test_ecryptfs(sb)
14258 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
14259 +               || au_test_aufs(sb); /* will be supported in next version */
14260 +}
14261 +
14262 +static inline int au_test_fs_remote(struct super_block *sb)
14263 +{
14264 +       return !au_test_tmpfs(sb)
14265 +#ifdef CONFIG_AUFS_BR_RAMFS
14266 +               && !au_test_ramfs(sb)
14267 +#endif
14268 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14269 +}
14270 +
14271 +/* ---------------------------------------------------------------------- */
14272 +
14273 +/*
14274 + * Note: these functions (below) are created after reading ->getattr() in all
14275 + * filesystems under linux/fs. it means we have to do so in every update...
14276 + */
14277 +
14278 +/*
14279 + * some filesystems require getattr to refresh the inode attributes before
14280 + * referencing.
14281 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14282 + * and leave the work for d_revalidate()
14283 + */
14284 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14285 +{
14286 +       return au_test_nfs(sb)
14287 +               || au_test_fuse(sb)
14288 +               /* || au_test_btrfs(sb) */      /* untested */
14289 +               ;
14290 +}
14291 +
14292 +/*
14293 + * filesystems which don't maintain i_size or i_blocks.
14294 + */
14295 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14296 +{
14297 +       return au_test_xfs(sb)
14298 +               || au_test_btrfs(sb)
14299 +               || au_test_ubifs(sb)
14300 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
14301 +               /* || au_test_minix(sb) */      /* untested */
14302 +               ;
14303 +}
14304 +
14305 +/*
14306 + * filesystems which don't store the correct value in some of their inode
14307 + * attributes.
14308 + */
14309 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
14310 +{
14311 +       return au_test_fs_bad_iattr_size(sb)
14312 +               || au_test_fat(sb)
14313 +               || au_test_msdos(sb)
14314 +               || au_test_vfat(sb);
14315 +}
14316 +
14317 +/* they don't check i_nlink in link(2) */
14318 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14319 +{
14320 +       return au_test_tmpfs(sb)
14321 +#ifdef CONFIG_AUFS_BR_RAMFS
14322 +               || au_test_ramfs(sb)
14323 +#endif
14324 +               || au_test_ubifs(sb)
14325 +               || au_test_hfsplus(sb);
14326 +}
14327 +
14328 +/*
14329 + * filesystems which sets S_NOATIME and S_NOCMTIME.
14330 + */
14331 +static inline int au_test_fs_notime(struct super_block *sb)
14332 +{
14333 +       return au_test_nfs(sb)
14334 +               || au_test_fuse(sb)
14335 +               || au_test_ubifs(sb)
14336 +               ;
14337 +}
14338 +
14339 +/* temporary support for i#1 in cramfs */
14340 +static inline int au_test_fs_unique_ino(struct inode *inode)
14341 +{
14342 +       if (au_test_cramfs(inode->i_sb))
14343 +               return inode->i_ino != 1;
14344 +       return 1;
14345 +}
14346 +
14347 +/* ---------------------------------------------------------------------- */
14348 +
14349 +/*
14350 + * the filesystem where the xino files placed must support i/o after unlink and
14351 + * maintain i_size and i_blocks.
14352 + */
14353 +static inline int au_test_fs_bad_xino(struct super_block *sb)
14354 +{
14355 +       return au_test_fs_remote(sb)
14356 +               || au_test_fs_bad_iattr_size(sb)
14357 +               /* don't want unnecessary work for xino */
14358 +               || au_test_aufs(sb)
14359 +               || au_test_ecryptfs(sb)
14360 +               || au_test_nilfs(sb);
14361 +}
14362 +
14363 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
14364 +{
14365 +       return au_test_tmpfs(sb)
14366 +               || au_test_ramfs(sb);
14367 +}
14368 +
14369 +/*
14370 + * test if the @sb is real-readonly.
14371 + */
14372 +static inline int au_test_fs_rr(struct super_block *sb)
14373 +{
14374 +       return au_test_squashfs(sb)
14375 +               || au_test_iso9660(sb)
14376 +               || au_test_cramfs(sb)
14377 +               || au_test_romfs(sb);
14378 +}
14379 +
14380 +/*
14381 + * test if the @inode is nfs with 'noacl' option
14382 + * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14383 + */
14384 +static inline int au_test_nfs_noacl(struct inode *inode)
14385 +{
14386 +       return au_test_nfs(inode->i_sb)
14387 +               /* && IS_POSIXACL(inode) */
14388 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
14389 +}
14390 +
14391 +#endif /* __KERNEL__ */
14392 +#endif /* __AUFS_FSTYPE_H__ */
14393 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14394 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
14395 +++ linux/fs/aufs/hfsnotify.c   2015-09-24 10:47:58.254719746 +0200
14396 @@ -0,0 +1,288 @@
14397 +/*
14398 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14399 + *
14400 + * This program, aufs is free software; you can redistribute it and/or modify
14401 + * it under the terms of the GNU General Public License as published by
14402 + * the Free Software Foundation; either version 2 of the License, or
14403 + * (at your option) any later version.
14404 + *
14405 + * This program is distributed in the hope that it will be useful,
14406 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14407 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14408 + * GNU General Public License for more details.
14409 + *
14410 + * You should have received a copy of the GNU General Public License
14411 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14412 + */
14413 +
14414 +/*
14415 + * fsnotify for the lower directories
14416 + */
14417 +
14418 +#include "aufs.h"
14419 +
14420 +/* FS_IN_IGNORED is unnecessary */
14421 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14422 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
14423 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
14424 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
14425 +
14426 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
14427 +{
14428 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14429 +                                            hn_mark);
14430 +       AuDbg("here\n");
14431 +       au_cache_free_hnotify(hn);
14432 +       smp_mb__before_atomic();
14433 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
14434 +               wake_up(&au_hfsn_wq);
14435 +}
14436 +
14437 +static int au_hfsn_alloc(struct au_hinode *hinode)
14438 +{
14439 +       int err;
14440 +       struct au_hnotify *hn;
14441 +       struct super_block *sb;
14442 +       struct au_branch *br;
14443 +       struct fsnotify_mark *mark;
14444 +       aufs_bindex_t bindex;
14445 +
14446 +       hn = hinode->hi_notify;
14447 +       sb = hn->hn_aufs_inode->i_sb;
14448 +       bindex = au_br_index(sb, hinode->hi_id);
14449 +       br = au_sbr(sb, bindex);
14450 +       AuDebugOn(!br->br_hfsn);
14451 +
14452 +       mark = &hn->hn_mark;
14453 +       fsnotify_init_mark(mark, au_hfsn_free_mark);
14454 +       mark->mask = AuHfsnMask;
14455 +       /*
14456 +        * by udba rename or rmdir, aufs assign a new inode to the known
14457 +        * h_inode, so specify 1 to allow dups.
14458 +        */
14459 +       lockdep_off();
14460 +       err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
14461 +                                /*mnt*/NULL, /*allow_dups*/1);
14462 +       /* even if err */
14463 +       fsnotify_put_mark(mark);
14464 +       lockdep_on();
14465 +
14466 +       return err;
14467 +}
14468 +
14469 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
14470 +{
14471 +       struct fsnotify_mark *mark;
14472 +       unsigned long long ull;
14473 +       struct fsnotify_group *group;
14474 +
14475 +       ull = atomic64_inc_return(&au_hfsn_ifree);
14476 +       BUG_ON(!ull);
14477 +
14478 +       mark = &hn->hn_mark;
14479 +       spin_lock(&mark->lock);
14480 +       group = mark->group;
14481 +       fsnotify_get_group(group);
14482 +       spin_unlock(&mark->lock);
14483 +       lockdep_off();
14484 +       fsnotify_destroy_mark(mark, group);
14485 +       fsnotify_put_group(group);
14486 +       lockdep_on();
14487 +
14488 +       /* free hn by myself */
14489 +       return 0;
14490 +}
14491 +
14492 +/* ---------------------------------------------------------------------- */
14493 +
14494 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
14495 +{
14496 +       struct fsnotify_mark *mark;
14497 +
14498 +       mark = &hinode->hi_notify->hn_mark;
14499 +       spin_lock(&mark->lock);
14500 +       if (do_set) {
14501 +               AuDebugOn(mark->mask & AuHfsnMask);
14502 +               mark->mask |= AuHfsnMask;
14503 +       } else {
14504 +               AuDebugOn(!(mark->mask & AuHfsnMask));
14505 +               mark->mask &= ~AuHfsnMask;
14506 +       }
14507 +       spin_unlock(&mark->lock);
14508 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
14509 +}
14510 +
14511 +/* ---------------------------------------------------------------------- */
14512 +
14513 +/* #define AuDbgHnotify */
14514 +#ifdef AuDbgHnotify
14515 +static char *au_hfsn_name(u32 mask)
14516 +{
14517 +#ifdef CONFIG_AUFS_DEBUG
14518 +#define test_ret(flag)                         \
14519 +       do {                                    \
14520 +               if (mask & flag)                \
14521 +                       return #flag;           \
14522 +       } while (0)
14523 +       test_ret(FS_ACCESS);
14524 +       test_ret(FS_MODIFY);
14525 +       test_ret(FS_ATTRIB);
14526 +       test_ret(FS_CLOSE_WRITE);
14527 +       test_ret(FS_CLOSE_NOWRITE);
14528 +       test_ret(FS_OPEN);
14529 +       test_ret(FS_MOVED_FROM);
14530 +       test_ret(FS_MOVED_TO);
14531 +       test_ret(FS_CREATE);
14532 +       test_ret(FS_DELETE);
14533 +       test_ret(FS_DELETE_SELF);
14534 +       test_ret(FS_MOVE_SELF);
14535 +       test_ret(FS_UNMOUNT);
14536 +       test_ret(FS_Q_OVERFLOW);
14537 +       test_ret(FS_IN_IGNORED);
14538 +       test_ret(FS_ISDIR);
14539 +       test_ret(FS_IN_ONESHOT);
14540 +       test_ret(FS_EVENT_ON_CHILD);
14541 +       return "";
14542 +#undef test_ret
14543 +#else
14544 +       return "??";
14545 +#endif
14546 +}
14547 +#endif
14548 +
14549 +/* ---------------------------------------------------------------------- */
14550 +
14551 +static void au_hfsn_free_group(struct fsnotify_group *group)
14552 +{
14553 +       struct au_br_hfsnotify *hfsn = group->private;
14554 +
14555 +       AuDbg("here\n");
14556 +       kfree(hfsn);
14557 +}
14558 +
14559 +static int au_hfsn_handle_event(struct fsnotify_group *group,
14560 +                               struct inode *inode,
14561 +                               struct fsnotify_mark *inode_mark,
14562 +                               struct fsnotify_mark *vfsmount_mark,
14563 +                               u32 mask, void *data, int data_type,
14564 +                               const unsigned char *file_name, u32 cookie)
14565 +{
14566 +       int err;
14567 +       struct au_hnotify *hnotify;
14568 +       struct inode *h_dir, *h_inode;
14569 +       struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
14570 +
14571 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
14572 +
14573 +       err = 0;
14574 +       /* if FS_UNMOUNT happens, there must be another bug */
14575 +       AuDebugOn(mask & FS_UNMOUNT);
14576 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
14577 +               goto out;
14578 +
14579 +       h_dir = inode;
14580 +       h_inode = NULL;
14581 +#ifdef AuDbgHnotify
14582 +       au_debug_on();
14583 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14584 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14585 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14586 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
14587 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14588 +               /* WARN_ON(1); */
14589 +       }
14590 +       au_debug_off();
14591 +#endif
14592 +
14593 +       AuDebugOn(!inode_mark);
14594 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14595 +       err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
14596 +
14597 +out:
14598 +       return err;
14599 +}
14600 +
14601 +static struct fsnotify_ops au_hfsn_ops = {
14602 +       .handle_event           = au_hfsn_handle_event,
14603 +       .free_group_priv        = au_hfsn_free_group
14604 +};
14605 +
14606 +/* ---------------------------------------------------------------------- */
14607 +
14608 +static void au_hfsn_fin_br(struct au_branch *br)
14609 +{
14610 +       struct au_br_hfsnotify *hfsn;
14611 +
14612 +       hfsn = br->br_hfsn;
14613 +       if (hfsn) {
14614 +               lockdep_off();
14615 +               fsnotify_put_group(hfsn->hfsn_group);
14616 +               lockdep_on();
14617 +       }
14618 +}
14619 +
14620 +static int au_hfsn_init_br(struct au_branch *br, int perm)
14621 +{
14622 +       int err;
14623 +       struct fsnotify_group *group;
14624 +       struct au_br_hfsnotify *hfsn;
14625 +
14626 +       err = 0;
14627 +       br->br_hfsn = NULL;
14628 +       if (!au_br_hnotifyable(perm))
14629 +               goto out;
14630 +
14631 +       err = -ENOMEM;
14632 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14633 +       if (unlikely(!hfsn))
14634 +               goto out;
14635 +
14636 +       err = 0;
14637 +       group = fsnotify_alloc_group(&au_hfsn_ops);
14638 +       if (IS_ERR(group)) {
14639 +               err = PTR_ERR(group);
14640 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
14641 +               goto out_hfsn;
14642 +       }
14643 +
14644 +       group->private = hfsn;
14645 +       hfsn->hfsn_group = group;
14646 +       br->br_hfsn = hfsn;
14647 +       goto out; /* success */
14648 +
14649 +out_hfsn:
14650 +       kfree(hfsn);
14651 +out:
14652 +       return err;
14653 +}
14654 +
14655 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14656 +{
14657 +       int err;
14658 +
14659 +       err = 0;
14660 +       if (!br->br_hfsn)
14661 +               err = au_hfsn_init_br(br, perm);
14662 +
14663 +       return err;
14664 +}
14665 +
14666 +/* ---------------------------------------------------------------------- */
14667 +
14668 +static void au_hfsn_fin(void)
14669 +{
14670 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14671 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14672 +}
14673 +
14674 +const struct au_hnotify_op au_hnotify_op = {
14675 +       .ctl            = au_hfsn_ctl,
14676 +       .alloc          = au_hfsn_alloc,
14677 +       .free           = au_hfsn_free,
14678 +
14679 +       .fin            = au_hfsn_fin,
14680 +
14681 +       .reset_br       = au_hfsn_reset_br,
14682 +       .fin_br         = au_hfsn_fin_br,
14683 +       .init_br        = au_hfsn_init_br
14684 +};
14685 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
14686 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
14687 +++ linux/fs/aufs/hfsplus.c     2015-09-24 10:47:58.254719746 +0200
14688 @@ -0,0 +1,56 @@
14689 +/*
14690 + * Copyright (C) 2010-2015 Junjiro R. Okajima
14691 + *
14692 + * This program, aufs is free software; you can redistribute it and/or modify
14693 + * it under the terms of the GNU General Public License as published by
14694 + * the Free Software Foundation; either version 2 of the License, or
14695 + * (at your option) any later version.
14696 + *
14697 + * This program is distributed in the hope that it will be useful,
14698 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14699 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14700 + * GNU General Public License for more details.
14701 + *
14702 + * You should have received a copy of the GNU General Public License
14703 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14704 + */
14705 +
14706 +/*
14707 + * special support for filesystems which aqucires an inode mutex
14708 + * at final closing a file, eg, hfsplus.
14709 + *
14710 + * This trick is very simple and stupid, just to open the file before really
14711 + * neceeary open to tell hfsplus that this is not the final closing.
14712 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
14713 + * and au_h_open_post() after releasing it.
14714 + */
14715 +
14716 +#include "aufs.h"
14717 +
14718 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
14719 +                          int force_wr)
14720 +{
14721 +       struct file *h_file;
14722 +       struct dentry *h_dentry;
14723 +
14724 +       h_dentry = au_h_dptr(dentry, bindex);
14725 +       AuDebugOn(!h_dentry);
14726 +       AuDebugOn(d_is_negative(h_dentry));
14727 +
14728 +       h_file = NULL;
14729 +       if (au_test_hfsplus(h_dentry->d_sb)
14730 +           && d_is_reg(h_dentry))
14731 +               h_file = au_h_open(dentry, bindex,
14732 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
14733 +                                  /*file*/NULL, force_wr);
14734 +       return h_file;
14735 +}
14736 +
14737 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
14738 +                   struct file *h_file)
14739 +{
14740 +       if (h_file) {
14741 +               fput(h_file);
14742 +               au_sbr_put(dentry->d_sb, bindex);
14743 +       }
14744 +}
14745 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
14746 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
14747 +++ linux/fs/aufs/hnotify.c     2015-09-24 10:47:58.254719746 +0200
14748 @@ -0,0 +1,710 @@
14749 +/*
14750 + * Copyright (C) 2005-2015 Junjiro R. Okajima
14751 + *
14752 + * This program, aufs is free software; you can redistribute it and/or modify
14753 + * it under the terms of the GNU General Public License as published by
14754 + * the Free Software Foundation; either version 2 of the License, or
14755 + * (at your option) any later version.
14756 + *
14757 + * This program is distributed in the hope that it will be useful,
14758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14760 + * GNU General Public License for more details.
14761 + *
14762 + * You should have received a copy of the GNU General Public License
14763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14764 + */
14765 +
14766 +/*
14767 + * abstraction to notify the direct changes on lower directories
14768 + */
14769 +
14770 +#include "aufs.h"
14771 +
14772 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
14773 +{
14774 +       int err;
14775 +       struct au_hnotify *hn;
14776 +
14777 +       err = -ENOMEM;
14778 +       hn = au_cache_alloc_hnotify();
14779 +       if (hn) {
14780 +               hn->hn_aufs_inode = inode;
14781 +               hinode->hi_notify = hn;
14782 +               err = au_hnotify_op.alloc(hinode);
14783 +               AuTraceErr(err);
14784 +               if (unlikely(err)) {
14785 +                       hinode->hi_notify = NULL;
14786 +                       au_cache_free_hnotify(hn);
14787 +                       /*
14788 +                        * The upper dir was removed by udba, but the same named
14789 +                        * dir left. In this case, aufs assignes a new inode
14790 +                        * number and set the monitor again.
14791 +                        * For the lower dir, the old monitnor is still left.
14792 +                        */
14793 +                       if (err == -EEXIST)
14794 +                               err = 0;
14795 +               }
14796 +       }
14797 +
14798 +       AuTraceErr(err);
14799 +       return err;
14800 +}
14801 +
14802 +void au_hn_free(struct au_hinode *hinode)
14803 +{
14804 +       struct au_hnotify *hn;
14805 +
14806 +       hn = hinode->hi_notify;
14807 +       if (hn) {
14808 +               hinode->hi_notify = NULL;
14809 +               if (au_hnotify_op.free(hinode, hn))
14810 +                       au_cache_free_hnotify(hn);
14811 +       }
14812 +}
14813 +
14814 +/* ---------------------------------------------------------------------- */
14815 +
14816 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
14817 +{
14818 +       if (hinode->hi_notify)
14819 +               au_hnotify_op.ctl(hinode, do_set);
14820 +}
14821 +
14822 +void au_hn_reset(struct inode *inode, unsigned int flags)
14823 +{
14824 +       aufs_bindex_t bindex, bend;
14825 +       struct inode *hi;
14826 +       struct dentry *iwhdentry;
14827 +
14828 +       bend = au_ibend(inode);
14829 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
14830 +               hi = au_h_iptr(inode, bindex);
14831 +               if (!hi)
14832 +                       continue;
14833 +
14834 +               /* mutex_lock_nested(&hi->i_mutex, AuLsc_I_CHILD); */
14835 +               iwhdentry = au_hi_wh(inode, bindex);
14836 +               if (iwhdentry)
14837 +                       dget(iwhdentry);
14838 +               au_igrab(hi);
14839 +               au_set_h_iptr(inode, bindex, NULL, 0);
14840 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
14841 +                             flags & ~AuHi_XINO);
14842 +               iput(hi);
14843 +               dput(iwhdentry);
14844 +               /* mutex_unlock(&hi->i_mutex); */
14845 +       }
14846 +}
14847 +
14848 +/* ---------------------------------------------------------------------- */
14849 +
14850 +static int hn_xino(struct inode *inode, struct inode *h_inode)
14851 +{
14852 +       int err;
14853 +       aufs_bindex_t bindex, bend, bfound, bstart;
14854 +       struct inode *h_i;
14855 +
14856 +       err = 0;
14857 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14858 +               pr_warn("branch root dir was changed\n");
14859 +               goto out;
14860 +       }
14861 +
14862 +       bfound = -1;
14863 +       bend = au_ibend(inode);
14864 +       bstart = au_ibstart(inode);
14865 +#if 0 /* reserved for future use */
14866 +       if (bindex == bend) {
14867 +               /* keep this ino in rename case */
14868 +               goto out;
14869 +       }
14870 +#endif
14871 +       for (bindex = bstart; bindex <= bend; bindex++)
14872 +               if (au_h_iptr(inode, bindex) == h_inode) {
14873 +                       bfound = bindex;
14874 +                       break;
14875 +               }
14876 +       if (bfound < 0)
14877 +               goto out;
14878 +
14879 +       for (bindex = bstart; bindex <= bend; bindex++) {
14880 +               h_i = au_h_iptr(inode, bindex);
14881 +               if (!h_i)
14882 +                       continue;
14883 +
14884 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
14885 +               /* ignore this error */
14886 +               /* bad action? */
14887 +       }
14888 +
14889 +       /* children inode number will be broken */
14890 +
14891 +out:
14892 +       AuTraceErr(err);
14893 +       return err;
14894 +}
14895 +
14896 +static int hn_gen_tree(struct dentry *dentry)
14897 +{
14898 +       int err, i, j, ndentry;
14899 +       struct au_dcsub_pages dpages;
14900 +       struct au_dpage *dpage;
14901 +       struct dentry **dentries;
14902 +
14903 +       err = au_dpages_init(&dpages, GFP_NOFS);
14904 +       if (unlikely(err))
14905 +               goto out;
14906 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
14907 +       if (unlikely(err))
14908 +               goto out_dpages;
14909 +
14910 +       for (i = 0; i < dpages.ndpage; i++) {
14911 +               dpage = dpages.dpages + i;
14912 +               dentries = dpage->dentries;
14913 +               ndentry = dpage->ndentry;
14914 +               for (j = 0; j < ndentry; j++) {
14915 +                       struct dentry *d;
14916 +
14917 +                       d = dentries[j];
14918 +                       if (IS_ROOT(d))
14919 +                               continue;
14920 +
14921 +                       au_digen_dec(d);
14922 +                       if (d_really_is_positive(d))
14923 +                               /* todo: reset children xino?
14924 +                                  cached children only? */
14925 +                               au_iigen_dec(d_inode(d));
14926 +               }
14927 +       }
14928 +
14929 +out_dpages:
14930 +       au_dpages_free(&dpages);
14931 +
14932 +#if 0
14933 +       /* discard children */
14934 +       dentry_unhash(dentry);
14935 +       dput(dentry);
14936 +#endif
14937 +out:
14938 +       return err;
14939 +}
14940 +
14941 +/*
14942 + * return 0 if processed.
14943 + */
14944 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
14945 +                          const unsigned int isdir)
14946 +{
14947 +       int err;
14948 +       struct dentry *d;
14949 +       struct qstr *dname;
14950 +
14951 +       err = 1;
14952 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
14953 +               pr_warn("branch root dir was changed\n");
14954 +               err = 0;
14955 +               goto out;
14956 +       }
14957 +
14958 +       if (!isdir) {
14959 +               AuDebugOn(!name);
14960 +               au_iigen_dec(inode);
14961 +               spin_lock(&inode->i_lock);
14962 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
14963 +                       spin_lock(&d->d_lock);
14964 +                       dname = &d->d_name;
14965 +                       if (dname->len != nlen
14966 +                           && memcmp(dname->name, name, nlen)) {
14967 +                               spin_unlock(&d->d_lock);
14968 +                               continue;
14969 +                       }
14970 +                       err = 0;
14971 +                       au_digen_dec(d);
14972 +                       spin_unlock(&d->d_lock);
14973 +                       break;
14974 +               }
14975 +               spin_unlock(&inode->i_lock);
14976 +       } else {
14977 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
14978 +               d = d_find_any_alias(inode);
14979 +               if (!d) {
14980 +                       au_iigen_dec(inode);
14981 +                       goto out;
14982 +               }
14983 +
14984 +               spin_lock(&d->d_lock);
14985 +               dname = &d->d_name;
14986 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
14987 +                       spin_unlock(&d->d_lock);
14988 +                       err = hn_gen_tree(d);
14989 +                       spin_lock(&d->d_lock);
14990 +               }
14991 +               spin_unlock(&d->d_lock);
14992 +               dput(d);
14993 +       }
14994 +
14995 +out:
14996 +       AuTraceErr(err);
14997 +       return err;
14998 +}
14999 +
15000 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
15001 +{
15002 +       int err;
15003 +
15004 +       if (IS_ROOT(dentry)) {
15005 +               pr_warn("branch root dir was changed\n");
15006 +               return 0;
15007 +       }
15008 +
15009 +       err = 0;
15010 +       if (!isdir) {
15011 +               au_digen_dec(dentry);
15012 +               if (d_really_is_positive(dentry))
15013 +                       au_iigen_dec(d_inode(dentry));
15014 +       } else {
15015 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
15016 +               if (d_really_is_positive(dentry))
15017 +                       err = hn_gen_tree(dentry);
15018 +       }
15019 +
15020 +       AuTraceErr(err);
15021 +       return err;
15022 +}
15023 +
15024 +/* ---------------------------------------------------------------------- */
15025 +
15026 +/* hnotify job flags */
15027 +#define AuHnJob_XINO0          1
15028 +#define AuHnJob_GEN            (1 << 1)
15029 +#define AuHnJob_DIRENT         (1 << 2)
15030 +#define AuHnJob_ISDIR          (1 << 3)
15031 +#define AuHnJob_TRYXINO0       (1 << 4)
15032 +#define AuHnJob_MNTPNT         (1 << 5)
15033 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
15034 +#define au_fset_hnjob(flags, name) \
15035 +       do { (flags) |= AuHnJob_##name; } while (0)
15036 +#define au_fclr_hnjob(flags, name) \
15037 +       do { (flags) &= ~AuHnJob_##name; } while (0)
15038 +
15039 +enum {
15040 +       AuHn_CHILD,
15041 +       AuHn_PARENT,
15042 +       AuHnLast
15043 +};
15044 +
15045 +struct au_hnotify_args {
15046 +       struct inode *h_dir, *dir, *h_child_inode;
15047 +       u32 mask;
15048 +       unsigned int flags[AuHnLast];
15049 +       unsigned int h_child_nlen;
15050 +       char h_child_name[];
15051 +};
15052 +
15053 +struct hn_job_args {
15054 +       unsigned int flags;
15055 +       struct inode *inode, *h_inode, *dir, *h_dir;
15056 +       struct dentry *dentry;
15057 +       char *h_name;
15058 +       int h_nlen;
15059 +};
15060 +
15061 +static int hn_job(struct hn_job_args *a)
15062 +{
15063 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
15064 +       int e;
15065 +
15066 +       /* reset xino */
15067 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15068 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
15069 +
15070 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
15071 +           && a->inode
15072 +           && a->h_inode) {
15073 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
15074 +               if (!a->h_inode->i_nlink
15075 +                   && !(a->h_inode->i_state & I_LINKABLE))
15076 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
15077 +               mutex_unlock(&a->h_inode->i_mutex);
15078 +       }
15079 +
15080 +       /* make the generation obsolete */
15081 +       if (au_ftest_hnjob(a->flags, GEN)) {
15082 +               e = -1;
15083 +               if (a->inode)
15084 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
15085 +                                             isdir);
15086 +               if (e && a->dentry)
15087 +                       hn_gen_by_name(a->dentry, isdir);
15088 +               /* ignore this error */
15089 +       }
15090 +
15091 +       /* make dir entries obsolete */
15092 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15093 +               struct au_vdir *vdir;
15094 +
15095 +               vdir = au_ivdir(a->inode);
15096 +               if (vdir)
15097 +                       vdir->vd_jiffy = 0;
15098 +               /* IMustLock(a->inode); */
15099 +               /* a->inode->i_version++; */
15100 +       }
15101 +
15102 +       /* can do nothing but warn */
15103 +       if (au_ftest_hnjob(a->flags, MNTPNT)
15104 +           && a->dentry
15105 +           && d_mountpoint(a->dentry))
15106 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
15107 +
15108 +       return 0;
15109 +}
15110 +
15111 +/* ---------------------------------------------------------------------- */
15112 +
15113 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15114 +                                          struct inode *dir)
15115 +{
15116 +       struct dentry *dentry, *d, *parent;
15117 +       struct qstr *dname;
15118 +
15119 +       parent = d_find_any_alias(dir);
15120 +       if (!parent)
15121 +               return NULL;
15122 +
15123 +       dentry = NULL;
15124 +       spin_lock(&parent->d_lock);
15125 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
15126 +               /* AuDbg("%pd\n", d); */
15127 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
15128 +               dname = &d->d_name;
15129 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
15130 +                       goto cont_unlock;
15131 +               if (au_di(d))
15132 +                       au_digen_dec(d);
15133 +               else
15134 +                       goto cont_unlock;
15135 +               if (au_dcount(d) > 0) {
15136 +                       dentry = dget_dlock(d);
15137 +                       spin_unlock(&d->d_lock);
15138 +                       break;
15139 +               }
15140 +
15141 +cont_unlock:
15142 +               spin_unlock(&d->d_lock);
15143 +       }
15144 +       spin_unlock(&parent->d_lock);
15145 +       dput(parent);
15146 +
15147 +       if (dentry)
15148 +               di_write_lock_child(dentry);
15149 +
15150 +       return dentry;
15151 +}
15152 +
15153 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15154 +                                        aufs_bindex_t bindex, ino_t h_ino)
15155 +{
15156 +       struct inode *inode;
15157 +       ino_t ino;
15158 +       int err;
15159 +
15160 +       inode = NULL;
15161 +       err = au_xino_read(sb, bindex, h_ino, &ino);
15162 +       if (!err && ino)
15163 +               inode = ilookup(sb, ino);
15164 +       if (!inode)
15165 +               goto out;
15166 +
15167 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
15168 +               pr_warn("wrong root branch\n");
15169 +               iput(inode);
15170 +               inode = NULL;
15171 +               goto out;
15172 +       }
15173 +
15174 +       ii_write_lock_child(inode);
15175 +
15176 +out:
15177 +       return inode;
15178 +}
15179 +
15180 +static void au_hn_bh(void *_args)
15181 +{
15182 +       struct au_hnotify_args *a = _args;
15183 +       struct super_block *sb;
15184 +       aufs_bindex_t bindex, bend, bfound;
15185 +       unsigned char xino, try_iput;
15186 +       int err;
15187 +       struct inode *inode;
15188 +       ino_t h_ino;
15189 +       struct hn_job_args args;
15190 +       struct dentry *dentry;
15191 +       struct au_sbinfo *sbinfo;
15192 +
15193 +       AuDebugOn(!_args);
15194 +       AuDebugOn(!a->h_dir);
15195 +       AuDebugOn(!a->dir);
15196 +       AuDebugOn(!a->mask);
15197 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15198 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
15199 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
15200 +
15201 +       inode = NULL;
15202 +       dentry = NULL;
15203 +       /*
15204 +        * do not lock a->dir->i_mutex here
15205 +        * because of d_revalidate() may cause a deadlock.
15206 +        */
15207 +       sb = a->dir->i_sb;
15208 +       AuDebugOn(!sb);
15209 +       sbinfo = au_sbi(sb);
15210 +       AuDebugOn(!sbinfo);
15211 +       si_write_lock(sb, AuLock_NOPLMW);
15212 +
15213 +       ii_read_lock_parent(a->dir);
15214 +       bfound = -1;
15215 +       bend = au_ibend(a->dir);
15216 +       for (bindex = au_ibstart(a->dir); bindex <= bend; bindex++)
15217 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15218 +                       bfound = bindex;
15219 +                       break;
15220 +               }
15221 +       ii_read_unlock(a->dir);
15222 +       if (unlikely(bfound < 0))
15223 +               goto out;
15224 +
15225 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
15226 +       h_ino = 0;
15227 +       if (a->h_child_inode)
15228 +               h_ino = a->h_child_inode->i_ino;
15229 +
15230 +       if (a->h_child_nlen
15231 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15232 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15233 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15234 +                                             a->dir);
15235 +       try_iput = 0;
15236 +       if (dentry && d_really_is_positive(dentry))
15237 +               inode = d_inode(dentry);
15238 +       if (xino && !inode && h_ino
15239 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15240 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15241 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15242 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15243 +               try_iput = 1;
15244 +           }
15245 +
15246 +       args.flags = a->flags[AuHn_CHILD];
15247 +       args.dentry = dentry;
15248 +       args.inode = inode;
15249 +       args.h_inode = a->h_child_inode;
15250 +       args.dir = a->dir;
15251 +       args.h_dir = a->h_dir;
15252 +       args.h_name = a->h_child_name;
15253 +       args.h_nlen = a->h_child_nlen;
15254 +       err = hn_job(&args);
15255 +       if (dentry) {
15256 +               if (au_di(dentry))
15257 +                       di_write_unlock(dentry);
15258 +               dput(dentry);
15259 +       }
15260 +       if (inode && try_iput) {
15261 +               ii_write_unlock(inode);
15262 +               iput(inode);
15263 +       }
15264 +
15265 +       ii_write_lock_parent(a->dir);
15266 +       args.flags = a->flags[AuHn_PARENT];
15267 +       args.dentry = NULL;
15268 +       args.inode = a->dir;
15269 +       args.h_inode = a->h_dir;
15270 +       args.dir = NULL;
15271 +       args.h_dir = NULL;
15272 +       args.h_name = NULL;
15273 +       args.h_nlen = 0;
15274 +       err = hn_job(&args);
15275 +       ii_write_unlock(a->dir);
15276 +
15277 +out:
15278 +       iput(a->h_child_inode);
15279 +       iput(a->h_dir);
15280 +       iput(a->dir);
15281 +       si_write_unlock(sb);
15282 +       au_nwt_done(&sbinfo->si_nowait);
15283 +       kfree(a);
15284 +}
15285 +
15286 +/* ---------------------------------------------------------------------- */
15287 +
15288 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15289 +              struct qstr *h_child_qstr, struct inode *h_child_inode)
15290 +{
15291 +       int err, len;
15292 +       unsigned int flags[AuHnLast], f;
15293 +       unsigned char isdir, isroot, wh;
15294 +       struct inode *dir;
15295 +       struct au_hnotify_args *args;
15296 +       char *p, *h_child_name;
15297 +
15298 +       err = 0;
15299 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15300 +       dir = igrab(hnotify->hn_aufs_inode);
15301 +       if (!dir)
15302 +               goto out;
15303 +
15304 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
15305 +       wh = 0;
15306 +       h_child_name = (void *)h_child_qstr->name;
15307 +       len = h_child_qstr->len;
15308 +       if (h_child_name) {
15309 +               if (len > AUFS_WH_PFX_LEN
15310 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15311 +                       h_child_name += AUFS_WH_PFX_LEN;
15312 +                       len -= AUFS_WH_PFX_LEN;
15313 +                       wh = 1;
15314 +               }
15315 +       }
15316 +
15317 +       isdir = 0;
15318 +       if (h_child_inode)
15319 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
15320 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
15321 +       flags[AuHn_CHILD] = 0;
15322 +       if (isdir)
15323 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
15324 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15325 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
15326 +       switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15327 +       case FS_MOVED_FROM:
15328 +       case FS_MOVED_TO:
15329 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15330 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15331 +               /*FALLTHROUGH*/
15332 +       case FS_CREATE:
15333 +               AuDebugOn(!h_child_name);
15334 +               break;
15335 +
15336 +       case FS_DELETE:
15337 +               /*
15338 +                * aufs never be able to get this child inode.
15339 +                * revalidation should be in d_revalidate()
15340 +                * by checking i_nlink, i_generation or d_unhashed().
15341 +                */
15342 +               AuDebugOn(!h_child_name);
15343 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15344 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15345 +               break;
15346 +
15347 +       default:
15348 +               AuDebugOn(1);
15349 +       }
15350 +
15351 +       if (wh)
15352 +               h_child_inode = NULL;
15353 +
15354 +       err = -ENOMEM;
15355 +       /* iput() and kfree() will be called in au_hnotify() */
15356 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
15357 +       if (unlikely(!args)) {
15358 +               AuErr1("no memory\n");
15359 +               iput(dir);
15360 +               goto out;
15361 +       }
15362 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15363 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15364 +       args->mask = mask;
15365 +       args->dir = dir;
15366 +       args->h_dir = igrab(h_dir);
15367 +       if (h_child_inode)
15368 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
15369 +       args->h_child_inode = h_child_inode;
15370 +       args->h_child_nlen = len;
15371 +       if (len) {
15372 +               p = (void *)args;
15373 +               p += sizeof(*args);
15374 +               memcpy(p, h_child_name, len);
15375 +               p[len] = 0;
15376 +       }
15377 +
15378 +       /* NFS fires the event for silly-renamed one from kworker */
15379 +       f = 0;
15380 +       if (!dir->i_nlink
15381 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
15382 +               f = AuWkq_NEST;
15383 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
15384 +       if (unlikely(err)) {
15385 +               pr_err("wkq %d\n", err);
15386 +               iput(args->h_child_inode);
15387 +               iput(args->h_dir);
15388 +               iput(args->dir);
15389 +               kfree(args);
15390 +       }
15391 +
15392 +out:
15393 +       return err;
15394 +}
15395 +
15396 +/* ---------------------------------------------------------------------- */
15397 +
15398 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15399 +{
15400 +       int err;
15401 +
15402 +       AuDebugOn(!(udba & AuOptMask_UDBA));
15403 +
15404 +       err = 0;
15405 +       if (au_hnotify_op.reset_br)
15406 +               err = au_hnotify_op.reset_br(udba, br, perm);
15407 +
15408 +       return err;
15409 +}
15410 +
15411 +int au_hnotify_init_br(struct au_branch *br, int perm)
15412 +{
15413 +       int err;
15414 +
15415 +       err = 0;
15416 +       if (au_hnotify_op.init_br)
15417 +               err = au_hnotify_op.init_br(br, perm);
15418 +
15419 +       return err;
15420 +}
15421 +
15422 +void au_hnotify_fin_br(struct au_branch *br)
15423 +{
15424 +       if (au_hnotify_op.fin_br)
15425 +               au_hnotify_op.fin_br(br);
15426 +}
15427 +
15428 +static void au_hn_destroy_cache(void)
15429 +{
15430 +       kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
15431 +       au_cachep[AuCache_HNOTIFY] = NULL;
15432 +}
15433 +
15434 +int __init au_hnotify_init(void)
15435 +{
15436 +       int err;
15437 +
15438 +       err = -ENOMEM;
15439 +       au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
15440 +       if (au_cachep[AuCache_HNOTIFY]) {
15441 +               err = 0;
15442 +               if (au_hnotify_op.init)
15443 +                       err = au_hnotify_op.init();
15444 +               if (unlikely(err))
15445 +                       au_hn_destroy_cache();
15446 +       }
15447 +       AuTraceErr(err);
15448 +       return err;
15449 +}
15450 +
15451 +void au_hnotify_fin(void)
15452 +{
15453 +       if (au_hnotify_op.fin)
15454 +               au_hnotify_op.fin();
15455 +       /* cf. au_cache_fin() */
15456 +       if (au_cachep[AuCache_HNOTIFY])
15457 +               au_hn_destroy_cache();
15458 +}
15459 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15460 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
15461 +++ linux/fs/aufs/iinfo.c       2015-09-24 10:47:58.254719746 +0200
15462 @@ -0,0 +1,277 @@
15463 +/*
15464 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15465 + *
15466 + * This program, aufs is free software; you can redistribute it and/or modify
15467 + * it under the terms of the GNU General Public License as published by
15468 + * the Free Software Foundation; either version 2 of the License, or
15469 + * (at your option) any later version.
15470 + *
15471 + * This program is distributed in the hope that it will be useful,
15472 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15473 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15474 + * GNU General Public License for more details.
15475 + *
15476 + * You should have received a copy of the GNU General Public License
15477 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15478 + */
15479 +
15480 +/*
15481 + * inode private data
15482 + */
15483 +
15484 +#include "aufs.h"
15485 +
15486 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
15487 +{
15488 +       struct inode *h_inode;
15489 +
15490 +       IiMustAnyLock(inode);
15491 +
15492 +       h_inode = au_ii(inode)->ii_hinode[0 + bindex].hi_inode;
15493 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15494 +       return h_inode;
15495 +}
15496 +
15497 +/* todo: hard/soft set? */
15498 +void au_hiput(struct au_hinode *hinode)
15499 +{
15500 +       au_hn_free(hinode);
15501 +       dput(hinode->hi_whdentry);
15502 +       iput(hinode->hi_inode);
15503 +}
15504 +
15505 +unsigned int au_hi_flags(struct inode *inode, int isdir)
15506 +{
15507 +       unsigned int flags;
15508 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
15509 +
15510 +       flags = 0;
15511 +       if (au_opt_test(mnt_flags, XINO))
15512 +               au_fset_hi(flags, XINO);
15513 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15514 +               au_fset_hi(flags, HNOTIFY);
15515 +       return flags;
15516 +}
15517 +
15518 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15519 +                  struct inode *h_inode, unsigned int flags)
15520 +{
15521 +       struct au_hinode *hinode;
15522 +       struct inode *hi;
15523 +       struct au_iinfo *iinfo = au_ii(inode);
15524 +
15525 +       IiMustWriteLock(inode);
15526 +
15527 +       hinode = iinfo->ii_hinode + bindex;
15528 +       hi = hinode->hi_inode;
15529 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15530 +
15531 +       if (hi)
15532 +               au_hiput(hinode);
15533 +       hinode->hi_inode = h_inode;
15534 +       if (h_inode) {
15535 +               int err;
15536 +               struct super_block *sb = inode->i_sb;
15537 +               struct au_branch *br;
15538 +
15539 +               AuDebugOn(inode->i_mode
15540 +                         && (h_inode->i_mode & S_IFMT)
15541 +                         != (inode->i_mode & S_IFMT));
15542 +               if (bindex == iinfo->ii_bstart)
15543 +                       au_cpup_igen(inode, h_inode);
15544 +               br = au_sbr(sb, bindex);
15545 +               hinode->hi_id = br->br_id;
15546 +               if (au_ftest_hi(flags, XINO)) {
15547 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
15548 +                                           inode->i_ino);
15549 +                       if (unlikely(err))
15550 +                               AuIOErr1("failed au_xino_write() %d\n", err);
15551 +               }
15552 +
15553 +               if (au_ftest_hi(flags, HNOTIFY)
15554 +                   && au_br_hnotifyable(br->br_perm)) {
15555 +                       err = au_hn_alloc(hinode, inode);
15556 +                       if (unlikely(err))
15557 +                               AuIOErr1("au_hn_alloc() %d\n", err);
15558 +               }
15559 +       }
15560 +}
15561 +
15562 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15563 +                 struct dentry *h_wh)
15564 +{
15565 +       struct au_hinode *hinode;
15566 +
15567 +       IiMustWriteLock(inode);
15568 +
15569 +       hinode = au_ii(inode)->ii_hinode + bindex;
15570 +       AuDebugOn(hinode->hi_whdentry);
15571 +       hinode->hi_whdentry = h_wh;
15572 +}
15573 +
15574 +void au_update_iigen(struct inode *inode, int half)
15575 +{
15576 +       struct au_iinfo *iinfo;
15577 +       struct au_iigen *iigen;
15578 +       unsigned int sigen;
15579 +
15580 +       sigen = au_sigen(inode->i_sb);
15581 +       iinfo = au_ii(inode);
15582 +       iigen = &iinfo->ii_generation;
15583 +       spin_lock(&iinfo->ii_genspin);
15584 +       iigen->ig_generation = sigen;
15585 +       if (half)
15586 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15587 +       else
15588 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
15589 +       spin_unlock(&iinfo->ii_genspin);
15590 +}
15591 +
15592 +/* it may be called at remount time, too */
15593 +void au_update_ibrange(struct inode *inode, int do_put_zero)
15594 +{
15595 +       struct au_iinfo *iinfo;
15596 +       aufs_bindex_t bindex, bend;
15597 +
15598 +       iinfo = au_ii(inode);
15599 +       if (!iinfo)
15600 +               return;
15601 +
15602 +       IiMustWriteLock(inode);
15603 +
15604 +       if (do_put_zero && iinfo->ii_bstart >= 0) {
15605 +               for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15606 +                    bindex++) {
15607 +                       struct inode *h_i;
15608 +
15609 +                       h_i = iinfo->ii_hinode[0 + bindex].hi_inode;
15610 +                       if (h_i
15611 +                           && !h_i->i_nlink
15612 +                           && !(h_i->i_state & I_LINKABLE))
15613 +                               au_set_h_iptr(inode, bindex, NULL, 0);
15614 +               }
15615 +       }
15616 +
15617 +       iinfo->ii_bstart = -1;
15618 +       iinfo->ii_bend = -1;
15619 +       bend = au_sbend(inode->i_sb);
15620 +       for (bindex = 0; bindex <= bend; bindex++)
15621 +               if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15622 +                       iinfo->ii_bstart = bindex;
15623 +                       break;
15624 +               }
15625 +       if (iinfo->ii_bstart >= 0)
15626 +               for (bindex = bend; bindex >= iinfo->ii_bstart; bindex--)
15627 +                       if (iinfo->ii_hinode[0 + bindex].hi_inode) {
15628 +                               iinfo->ii_bend = bindex;
15629 +                               break;
15630 +                       }
15631 +       AuDebugOn(iinfo->ii_bstart > iinfo->ii_bend);
15632 +}
15633 +
15634 +/* ---------------------------------------------------------------------- */
15635 +
15636 +void au_icntnr_init_once(void *_c)
15637 +{
15638 +       struct au_icntnr *c = _c;
15639 +       struct au_iinfo *iinfo = &c->iinfo;
15640 +       static struct lock_class_key aufs_ii;
15641 +
15642 +       spin_lock_init(&iinfo->ii_genspin);
15643 +       au_rw_init(&iinfo->ii_rwsem);
15644 +       au_rw_class(&iinfo->ii_rwsem, &aufs_ii);
15645 +       inode_init_once(&c->vfs_inode);
15646 +}
15647 +
15648 +int au_iinfo_init(struct inode *inode)
15649 +{
15650 +       struct au_iinfo *iinfo;
15651 +       struct super_block *sb;
15652 +       int nbr, i;
15653 +
15654 +       sb = inode->i_sb;
15655 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
15656 +       nbr = au_sbend(sb) + 1;
15657 +       if (unlikely(nbr <= 0))
15658 +               nbr = 1;
15659 +       iinfo->ii_hinode = kcalloc(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15660 +       if (iinfo->ii_hinode) {
15661 +               au_ninodes_inc(sb);
15662 +               for (i = 0; i < nbr; i++)
15663 +                       iinfo->ii_hinode[i].hi_id = -1;
15664 +
15665 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
15666 +               iinfo->ii_bstart = -1;
15667 +               iinfo->ii_bend = -1;
15668 +               iinfo->ii_vdir = NULL;
15669 +               return 0;
15670 +       }
15671 +       return -ENOMEM;
15672 +}
15673 +
15674 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr)
15675 +{
15676 +       int err, sz;
15677 +       struct au_hinode *hip;
15678 +
15679 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
15680 +
15681 +       err = -ENOMEM;
15682 +       sz = sizeof(*hip) * (iinfo->ii_bend + 1);
15683 +       if (!sz)
15684 +               sz = sizeof(*hip);
15685 +       hip = au_kzrealloc(iinfo->ii_hinode, sz, sizeof(*hip) * nbr, GFP_NOFS);
15686 +       if (hip) {
15687 +               iinfo->ii_hinode = hip;
15688 +               err = 0;
15689 +       }
15690 +
15691 +       return err;
15692 +}
15693 +
15694 +void au_iinfo_fin(struct inode *inode)
15695 +{
15696 +       struct au_iinfo *iinfo;
15697 +       struct au_hinode *hi;
15698 +       struct super_block *sb;
15699 +       aufs_bindex_t bindex, bend;
15700 +       const unsigned char unlinked = !inode->i_nlink;
15701 +
15702 +       iinfo = au_ii(inode);
15703 +       /* bad_inode case */
15704 +       if (!iinfo)
15705 +               return;
15706 +
15707 +       sb = inode->i_sb;
15708 +       au_ninodes_dec(sb);
15709 +       if (si_pid_test(sb))
15710 +               au_xino_delete_inode(inode, unlinked);
15711 +       else {
15712 +               /*
15713 +                * it is safe to hide the dependency between sbinfo and
15714 +                * sb->s_umount.
15715 +                */
15716 +               lockdep_off();
15717 +               si_noflush_read_lock(sb);
15718 +               au_xino_delete_inode(inode, unlinked);
15719 +               si_read_unlock(sb);
15720 +               lockdep_on();
15721 +       }
15722 +
15723 +       if (iinfo->ii_vdir)
15724 +               au_vdir_free(iinfo->ii_vdir);
15725 +
15726 +       bindex = iinfo->ii_bstart;
15727 +       if (bindex >= 0) {
15728 +               hi = iinfo->ii_hinode + bindex;
15729 +               bend = iinfo->ii_bend;
15730 +               while (bindex++ <= bend) {
15731 +                       if (hi->hi_inode)
15732 +                               au_hiput(hi);
15733 +                       hi++;
15734 +               }
15735 +       }
15736 +       kfree(iinfo->ii_hinode);
15737 +       iinfo->ii_hinode = NULL;
15738 +       AuRwDestroy(&iinfo->ii_rwsem);
15739 +}
15740 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
15741 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
15742 +++ linux/fs/aufs/inode.c       2015-09-24 10:47:58.254719746 +0200
15743 @@ -0,0 +1,500 @@
15744 +/*
15745 + * Copyright (C) 2005-2015 Junjiro R. Okajima
15746 + *
15747 + * This program, aufs is free software; you can redistribute it and/or modify
15748 + * it under the terms of the GNU General Public License as published by
15749 + * the Free Software Foundation; either version 2 of the License, or
15750 + * (at your option) any later version.
15751 + *
15752 + * This program is distributed in the hope that it will be useful,
15753 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15754 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15755 + * GNU General Public License for more details.
15756 + *
15757 + * You should have received a copy of the GNU General Public License
15758 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15759 + */
15760 +
15761 +/*
15762 + * inode functions
15763 + */
15764 +
15765 +#include "aufs.h"
15766 +
15767 +struct inode *au_igrab(struct inode *inode)
15768 +{
15769 +       if (inode) {
15770 +               AuDebugOn(!atomic_read(&inode->i_count));
15771 +               ihold(inode);
15772 +       }
15773 +       return inode;
15774 +}
15775 +
15776 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
15777 +{
15778 +       au_cpup_attr_all(inode, /*force*/0);
15779 +       au_update_iigen(inode, /*half*/1);
15780 +       if (do_version)
15781 +               inode->i_version++;
15782 +}
15783 +
15784 +static int au_ii_refresh(struct inode *inode, int *update)
15785 +{
15786 +       int err, e;
15787 +       umode_t type;
15788 +       aufs_bindex_t bindex, new_bindex;
15789 +       struct super_block *sb;
15790 +       struct au_iinfo *iinfo;
15791 +       struct au_hinode *p, *q, tmp;
15792 +
15793 +       IiMustWriteLock(inode);
15794 +
15795 +       *update = 0;
15796 +       sb = inode->i_sb;
15797 +       type = inode->i_mode & S_IFMT;
15798 +       iinfo = au_ii(inode);
15799 +       err = au_ii_realloc(iinfo, au_sbend(sb) + 1);
15800 +       if (unlikely(err))
15801 +               goto out;
15802 +
15803 +       AuDebugOn(iinfo->ii_bstart < 0);
15804 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15805 +       for (bindex = iinfo->ii_bstart; bindex <= iinfo->ii_bend;
15806 +            bindex++, p++) {
15807 +               if (!p->hi_inode)
15808 +                       continue;
15809 +
15810 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
15811 +               new_bindex = au_br_index(sb, p->hi_id);
15812 +               if (new_bindex == bindex)
15813 +                       continue;
15814 +
15815 +               if (new_bindex < 0) {
15816 +                       *update = 1;
15817 +                       au_hiput(p);
15818 +                       p->hi_inode = NULL;
15819 +                       continue;
15820 +               }
15821 +
15822 +               if (new_bindex < iinfo->ii_bstart)
15823 +                       iinfo->ii_bstart = new_bindex;
15824 +               if (iinfo->ii_bend < new_bindex)
15825 +                       iinfo->ii_bend = new_bindex;
15826 +               /* swap two lower inode, and loop again */
15827 +               q = iinfo->ii_hinode + new_bindex;
15828 +               tmp = *q;
15829 +               *q = *p;
15830 +               *p = tmp;
15831 +               if (tmp.hi_inode) {
15832 +                       bindex--;
15833 +                       p--;
15834 +               }
15835 +       }
15836 +       au_update_ibrange(inode, /*do_put_zero*/0);
15837 +       e = au_dy_irefresh(inode);
15838 +       if (unlikely(e && !err))
15839 +               err = e;
15840 +
15841 +out:
15842 +       AuTraceErr(err);
15843 +       return err;
15844 +}
15845 +
15846 +int au_refresh_hinode_self(struct inode *inode)
15847 +{
15848 +       int err, update;
15849 +
15850 +       err = au_ii_refresh(inode, &update);
15851 +       if (!err)
15852 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
15853 +
15854 +       AuTraceErr(err);
15855 +       return err;
15856 +}
15857 +
15858 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
15859 +{
15860 +       int err, e, update;
15861 +       unsigned int flags;
15862 +       umode_t mode;
15863 +       aufs_bindex_t bindex, bend;
15864 +       unsigned char isdir;
15865 +       struct au_hinode *p;
15866 +       struct au_iinfo *iinfo;
15867 +
15868 +       err = au_ii_refresh(inode, &update);
15869 +       if (unlikely(err))
15870 +               goto out;
15871 +
15872 +       update = 0;
15873 +       iinfo = au_ii(inode);
15874 +       p = iinfo->ii_hinode + iinfo->ii_bstart;
15875 +       mode = (inode->i_mode & S_IFMT);
15876 +       isdir = S_ISDIR(mode);
15877 +       flags = au_hi_flags(inode, isdir);
15878 +       bend = au_dbend(dentry);
15879 +       for (bindex = au_dbstart(dentry); bindex <= bend; bindex++) {
15880 +               struct inode *h_i, *h_inode;
15881 +               struct dentry *h_d;
15882 +
15883 +               h_d = au_h_dptr(dentry, bindex);
15884 +               if (!h_d || d_is_negative(h_d))
15885 +                       continue;
15886 +
15887 +               h_inode = d_inode(h_d);
15888 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
15889 +               if (iinfo->ii_bstart <= bindex && bindex <= iinfo->ii_bend) {
15890 +                       h_i = au_h_iptr(inode, bindex);
15891 +                       if (h_i) {
15892 +                               if (h_i == h_inode)
15893 +                                       continue;
15894 +                               err = -EIO;
15895 +                               break;
15896 +                       }
15897 +               }
15898 +               if (bindex < iinfo->ii_bstart)
15899 +                       iinfo->ii_bstart = bindex;
15900 +               if (iinfo->ii_bend < bindex)
15901 +                       iinfo->ii_bend = bindex;
15902 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
15903 +               update = 1;
15904 +       }
15905 +       au_update_ibrange(inode, /*do_put_zero*/0);
15906 +       e = au_dy_irefresh(inode);
15907 +       if (unlikely(e && !err))
15908 +               err = e;
15909 +       if (!err)
15910 +               au_refresh_hinode_attr(inode, update && isdir);
15911 +
15912 +out:
15913 +       AuTraceErr(err);
15914 +       return err;
15915 +}
15916 +
15917 +static int set_inode(struct inode *inode, struct dentry *dentry)
15918 +{
15919 +       int err;
15920 +       unsigned int flags;
15921 +       umode_t mode;
15922 +       aufs_bindex_t bindex, bstart, btail;
15923 +       unsigned char isdir;
15924 +       struct dentry *h_dentry;
15925 +       struct inode *h_inode;
15926 +       struct au_iinfo *iinfo;
15927 +
15928 +       IiMustWriteLock(inode);
15929 +
15930 +       err = 0;
15931 +       isdir = 0;
15932 +       bstart = au_dbstart(dentry);
15933 +       h_dentry = au_h_dptr(dentry, bstart);
15934 +       h_inode = d_inode(h_dentry);
15935 +       mode = h_inode->i_mode;
15936 +       switch (mode & S_IFMT) {
15937 +       case S_IFREG:
15938 +               btail = au_dbtail(dentry);
15939 +               inode->i_op = &aufs_iop;
15940 +               inode->i_fop = &aufs_file_fop;
15941 +               err = au_dy_iaop(inode, bstart, h_inode);
15942 +               if (unlikely(err))
15943 +                       goto out;
15944 +               break;
15945 +       case S_IFDIR:
15946 +               isdir = 1;
15947 +               btail = au_dbtaildir(dentry);
15948 +               inode->i_op = &aufs_dir_iop;
15949 +               inode->i_fop = &aufs_dir_fop;
15950 +               break;
15951 +       case S_IFLNK:
15952 +               btail = au_dbtail(dentry);
15953 +               inode->i_op = &aufs_symlink_iop;
15954 +               break;
15955 +       case S_IFBLK:
15956 +       case S_IFCHR:
15957 +       case S_IFIFO:
15958 +       case S_IFSOCK:
15959 +               btail = au_dbtail(dentry);
15960 +               inode->i_op = &aufs_iop;
15961 +               init_special_inode(inode, mode, h_inode->i_rdev);
15962 +               break;
15963 +       default:
15964 +               AuIOErr("Unknown file type 0%o\n", mode);
15965 +               err = -EIO;
15966 +               goto out;
15967 +       }
15968 +
15969 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
15970 +       flags = au_hi_flags(inode, isdir);
15971 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
15972 +           && au_ftest_hi(flags, HNOTIFY)
15973 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
15974 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
15975 +               au_fclr_hi(flags, HNOTIFY);
15976 +       iinfo = au_ii(inode);
15977 +       iinfo->ii_bstart = bstart;
15978 +       iinfo->ii_bend = btail;
15979 +       for (bindex = bstart; bindex <= btail; bindex++) {
15980 +               h_dentry = au_h_dptr(dentry, bindex);
15981 +               if (h_dentry)
15982 +                       au_set_h_iptr(inode, bindex,
15983 +                                     au_igrab(d_inode(h_dentry)), flags);
15984 +       }
15985 +       au_cpup_attr_all(inode, /*force*/1);
15986 +       /*
15987 +        * to force calling aufs_get_acl() every time,
15988 +        * do not call cache_no_acl() for aufs inode.
15989 +        */
15990 +
15991 +out:
15992 +       return err;
15993 +}
15994 +
15995 +/*
15996 + * successful returns with iinfo write_locked
15997 + * minus: errno
15998 + * zero: success, matched
15999 + * plus: no error, but unmatched
16000 + */
16001 +static int reval_inode(struct inode *inode, struct dentry *dentry)
16002 +{
16003 +       int err;
16004 +       unsigned int gen;
16005 +       struct au_iigen iigen;
16006 +       aufs_bindex_t bindex, bend;
16007 +       struct inode *h_inode, *h_dinode;
16008 +       struct dentry *h_dentry;
16009 +
16010 +       /*
16011 +        * before this function, if aufs got any iinfo lock, it must be only
16012 +        * one, the parent dir.
16013 +        * it can happen by UDBA and the obsoleted inode number.
16014 +        */
16015 +       err = -EIO;
16016 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
16017 +               goto out;
16018 +
16019 +       err = 1;
16020 +       ii_write_lock_new_child(inode);
16021 +       h_dentry = au_h_dptr(dentry, au_dbstart(dentry));
16022 +       h_dinode = d_inode(h_dentry);
16023 +       bend = au_ibend(inode);
16024 +       for (bindex = au_ibstart(inode); bindex <= bend; bindex++) {
16025 +               h_inode = au_h_iptr(inode, bindex);
16026 +               if (!h_inode || h_inode != h_dinode)
16027 +                       continue;
16028 +
16029 +               err = 0;
16030 +               gen = au_iigen(inode, &iigen);
16031 +               if (gen == au_digen(dentry)
16032 +                   && !au_ig_ftest(iigen.ig_flags, HALF_REFRESHED))
16033 +                       break;
16034 +
16035 +               /* fully refresh inode using dentry */
16036 +               err = au_refresh_hinode(inode, dentry);
16037 +               if (!err)
16038 +                       au_update_iigen(inode, /*half*/0);
16039 +               break;
16040 +       }
16041 +
16042 +       if (unlikely(err))
16043 +               ii_write_unlock(inode);
16044 +out:
16045 +       return err;
16046 +}
16047 +
16048 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16049 +          unsigned int d_type, ino_t *ino)
16050 +{
16051 +       int err;
16052 +       struct mutex *mtx;
16053 +
16054 +       /* prevent hardlinked inode number from race condition */
16055 +       mtx = NULL;
16056 +       if (d_type != DT_DIR) {
16057 +               mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16058 +               mutex_lock(mtx);
16059 +       }
16060 +       err = au_xino_read(sb, bindex, h_ino, ino);
16061 +       if (unlikely(err))
16062 +               goto out;
16063 +
16064 +       if (!*ino) {
16065 +               err = -EIO;
16066 +               *ino = au_xino_new_ino(sb);
16067 +               if (unlikely(!*ino))
16068 +                       goto out;
16069 +               err = au_xino_write(sb, bindex, h_ino, *ino);
16070 +               if (unlikely(err))
16071 +                       goto out;
16072 +       }
16073 +
16074 +out:
16075 +       if (mtx)
16076 +               mutex_unlock(mtx);
16077 +       return err;
16078 +}
16079 +
16080 +/* successful returns with iinfo write_locked */
16081 +/* todo: return with unlocked? */
16082 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
16083 +{
16084 +       struct inode *inode, *h_inode;
16085 +       struct dentry *h_dentry;
16086 +       struct super_block *sb;
16087 +       struct mutex *mtx;
16088 +       ino_t h_ino, ino;
16089 +       int err;
16090 +       aufs_bindex_t bstart;
16091 +
16092 +       sb = dentry->d_sb;
16093 +       bstart = au_dbstart(dentry);
16094 +       h_dentry = au_h_dptr(dentry, bstart);
16095 +       h_inode = d_inode(h_dentry);
16096 +       h_ino = h_inode->i_ino;
16097 +
16098 +       /*
16099 +        * stop 'race'-ing between hardlinks under different
16100 +        * parents.
16101 +        */
16102 +       mtx = NULL;
16103 +       if (!d_is_dir(h_dentry))
16104 +               mtx = &au_sbr(sb, bstart)->br_xino.xi_nondir_mtx;
16105 +
16106 +new_ino:
16107 +       if (mtx)
16108 +               mutex_lock(mtx);
16109 +       err = au_xino_read(sb, bstart, h_ino, &ino);
16110 +       inode = ERR_PTR(err);
16111 +       if (unlikely(err))
16112 +               goto out;
16113 +
16114 +       if (!ino) {
16115 +               ino = au_xino_new_ino(sb);
16116 +               if (unlikely(!ino)) {
16117 +                       inode = ERR_PTR(-EIO);
16118 +                       goto out;
16119 +               }
16120 +       }
16121 +
16122 +       AuDbg("i%lu\n", (unsigned long)ino);
16123 +       inode = au_iget_locked(sb, ino);
16124 +       err = PTR_ERR(inode);
16125 +       if (IS_ERR(inode))
16126 +               goto out;
16127 +
16128 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16129 +       if (inode->i_state & I_NEW) {
16130 +               /* verbose coding for lock class name */
16131 +               if (unlikely(d_is_symlink(h_dentry)))
16132 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16133 +                                   au_lc_key + AuLcSymlink_IIINFO);
16134 +               else if (unlikely(d_is_dir(h_dentry)))
16135 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16136 +                                   au_lc_key + AuLcDir_IIINFO);
16137 +               else /* likely */
16138 +                       au_rw_class(&au_ii(inode)->ii_rwsem,
16139 +                                   au_lc_key + AuLcNonDir_IIINFO);
16140 +
16141 +               ii_write_lock_new_child(inode);
16142 +               err = set_inode(inode, dentry);
16143 +               if (!err) {
16144 +                       unlock_new_inode(inode);
16145 +                       goto out; /* success */
16146 +               }
16147 +
16148 +               /*
16149 +                * iget_failed() calls iput(), but we need to call
16150 +                * ii_write_unlock() after iget_failed(). so dirty hack for
16151 +                * i_count.
16152 +                */
16153 +               atomic_inc(&inode->i_count);
16154 +               iget_failed(inode);
16155 +               ii_write_unlock(inode);
16156 +               au_xino_write(sb, bstart, h_ino, /*ino*/0);
16157 +               /* ignore this error */
16158 +               goto out_iput;
16159 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
16160 +               /*
16161 +                * horrible race condition between lookup, readdir and copyup
16162 +                * (or something).
16163 +                */
16164 +               if (mtx)
16165 +                       mutex_unlock(mtx);
16166 +               err = reval_inode(inode, dentry);
16167 +               if (unlikely(err < 0)) {
16168 +                       mtx = NULL;
16169 +                       goto out_iput;
16170 +               }
16171 +
16172 +               if (!err) {
16173 +                       mtx = NULL;
16174 +                       goto out; /* success */
16175 +               } else if (mtx)
16176 +                       mutex_lock(mtx);
16177 +       }
16178 +
16179 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
16180 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
16181 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
16182 +                       bstart, au_sbtype(h_dentry->d_sb), dentry,
16183 +                       (unsigned long)h_ino, (unsigned long)ino);
16184 +       ino = 0;
16185 +       err = au_xino_write(sb, bstart, h_ino, /*ino*/0);
16186 +       if (!err) {
16187 +               iput(inode);
16188 +               if (mtx)
16189 +                       mutex_unlock(mtx);
16190 +               goto new_ino;
16191 +       }
16192 +
16193 +out_iput:
16194 +       iput(inode);
16195 +       inode = ERR_PTR(err);
16196 +out:
16197 +       if (mtx)
16198 +               mutex_unlock(mtx);
16199 +       return inode;
16200 +}
16201 +
16202 +/* ---------------------------------------------------------------------- */
16203 +
16204 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16205 +              struct inode *inode)
16206 +{
16207 +       int err;
16208 +       struct inode *hi;
16209 +
16210 +       err = au_br_rdonly(au_sbr(sb, bindex));
16211 +
16212 +       /* pseudo-link after flushed may happen out of bounds */
16213 +       if (!err
16214 +           && inode
16215 +           && au_ibstart(inode) <= bindex
16216 +           && bindex <= au_ibend(inode)) {
16217 +               /*
16218 +                * permission check is unnecessary since vfsub routine
16219 +                * will be called later
16220 +                */
16221 +               hi = au_h_iptr(inode, bindex);
16222 +               if (hi)
16223 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
16224 +       }
16225 +
16226 +       return err;
16227 +}
16228 +
16229 +int au_test_h_perm(struct inode *h_inode, int mask)
16230 +{
16231 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
16232 +               return 0;
16233 +       return inode_permission(h_inode, mask);
16234 +}
16235 +
16236 +int au_test_h_perm_sio(struct inode *h_inode, int mask)
16237 +{
16238 +       if (au_test_nfs(h_inode->i_sb)
16239 +           && (mask & MAY_WRITE)
16240 +           && S_ISDIR(h_inode->i_mode))
16241 +               mask |= MAY_READ; /* force permission check */
16242 +       return au_test_h_perm(h_inode, mask);
16243 +}
16244 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16245 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
16246 +++ linux/fs/aufs/inode.h       2015-09-24 10:47:58.254719746 +0200
16247 @@ -0,0 +1,673 @@
16248 +/*
16249 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16250 + *
16251 + * This program, aufs is free software; you can redistribute it and/or modify
16252 + * it under the terms of the GNU General Public License as published by
16253 + * the Free Software Foundation; either version 2 of the License, or
16254 + * (at your option) any later version.
16255 + *
16256 + * This program is distributed in the hope that it will be useful,
16257 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16258 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16259 + * GNU General Public License for more details.
16260 + *
16261 + * You should have received a copy of the GNU General Public License
16262 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16263 + */
16264 +
16265 +/*
16266 + * inode operations
16267 + */
16268 +
16269 +#ifndef __AUFS_INODE_H__
16270 +#define __AUFS_INODE_H__
16271 +
16272 +#ifdef __KERNEL__
16273 +
16274 +#include <linux/fsnotify.h>
16275 +#include "rwsem.h"
16276 +
16277 +struct vfsmount;
16278 +
16279 +struct au_hnotify {
16280 +#ifdef CONFIG_AUFS_HNOTIFY
16281 +#ifdef CONFIG_AUFS_HFSNOTIFY
16282 +       /* never use fsnotify_add_vfsmount_mark() */
16283 +       struct fsnotify_mark            hn_mark;
16284 +#endif
16285 +       struct inode                    *hn_aufs_inode; /* no get/put */
16286 +#endif
16287 +} ____cacheline_aligned_in_smp;
16288 +
16289 +struct au_hinode {
16290 +       struct inode            *hi_inode;
16291 +       aufs_bindex_t           hi_id;
16292 +#ifdef CONFIG_AUFS_HNOTIFY
16293 +       struct au_hnotify       *hi_notify;
16294 +#endif
16295 +
16296 +       /* reference to the copied-up whiteout with get/put */
16297 +       struct dentry           *hi_whdentry;
16298 +};
16299 +
16300 +/* ig_flags */
16301 +#define AuIG_HALF_REFRESHED            1
16302 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
16303 +#define au_ig_fset(flags, name) \
16304 +       do { (flags) |= AuIG_##name; } while (0)
16305 +#define au_ig_fclr(flags, name) \
16306 +       do { (flags) &= ~AuIG_##name; } while (0)
16307 +
16308 +struct au_iigen {
16309 +       __u32           ig_generation, ig_flags;
16310 +};
16311 +
16312 +struct au_vdir;
16313 +struct au_iinfo {
16314 +       spinlock_t              ii_genspin;
16315 +       struct au_iigen         ii_generation;
16316 +       struct super_block      *ii_hsb1;       /* no get/put */
16317 +
16318 +       struct au_rwsem         ii_rwsem;
16319 +       aufs_bindex_t           ii_bstart, ii_bend;
16320 +       __u32                   ii_higen;
16321 +       struct au_hinode        *ii_hinode;
16322 +       struct au_vdir          *ii_vdir;
16323 +};
16324 +
16325 +struct au_icntnr {
16326 +       struct au_iinfo iinfo;
16327 +       struct inode vfs_inode;
16328 +} ____cacheline_aligned_in_smp;
16329 +
16330 +/* au_pin flags */
16331 +#define AuPin_DI_LOCKED                1
16332 +#define AuPin_MNT_WRITE                (1 << 1)
16333 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
16334 +#define au_fset_pin(flags, name) \
16335 +       do { (flags) |= AuPin_##name; } while (0)
16336 +#define au_fclr_pin(flags, name) \
16337 +       do { (flags) &= ~AuPin_##name; } while (0)
16338 +
16339 +struct au_pin {
16340 +       /* input */
16341 +       struct dentry *dentry;
16342 +       unsigned int udba;
16343 +       unsigned char lsc_di, lsc_hi, flags;
16344 +       aufs_bindex_t bindex;
16345 +
16346 +       /* output */
16347 +       struct dentry *parent;
16348 +       struct au_hinode *hdir;
16349 +       struct vfsmount *h_mnt;
16350 +
16351 +       /* temporary unlock/relock for copyup */
16352 +       struct dentry *h_dentry, *h_parent;
16353 +       struct au_branch *br;
16354 +       struct task_struct *task;
16355 +};
16356 +
16357 +void au_pin_hdir_unlock(struct au_pin *p);
16358 +int au_pin_hdir_lock(struct au_pin *p);
16359 +int au_pin_hdir_relock(struct au_pin *p);
16360 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
16361 +void au_pin_hdir_acquire_nest(struct au_pin *p);
16362 +void au_pin_hdir_release(struct au_pin *p);
16363 +
16364 +/* ---------------------------------------------------------------------- */
16365 +
16366 +static inline struct au_iinfo *au_ii(struct inode *inode)
16367 +{
16368 +       struct au_iinfo *iinfo;
16369 +
16370 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
16371 +       if (iinfo->ii_hinode)
16372 +               return iinfo;
16373 +       return NULL; /* debugging bad_inode case */
16374 +}
16375 +
16376 +/* ---------------------------------------------------------------------- */
16377 +
16378 +/* inode.c */
16379 +struct inode *au_igrab(struct inode *inode);
16380 +int au_refresh_hinode_self(struct inode *inode);
16381 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16382 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16383 +          unsigned int d_type, ino_t *ino);
16384 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
16385 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16386 +              struct inode *inode);
16387 +int au_test_h_perm(struct inode *h_inode, int mask);
16388 +int au_test_h_perm_sio(struct inode *h_inode, int mask);
16389 +
16390 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16391 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
16392 +{
16393 +#ifdef CONFIG_AUFS_SHWH
16394 +       return au_ino(sb, bindex, h_ino, d_type, ino);
16395 +#else
16396 +       return 0;
16397 +#endif
16398 +}
16399 +
16400 +/* i_op.c */
16401 +extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
16402 +
16403 +/* au_wr_dir flags */
16404 +#define AuWrDir_ADD_ENTRY      1
16405 +#define AuWrDir_ISDIR          (1 << 1)
16406 +#define AuWrDir_TMPFILE                (1 << 2)
16407 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
16408 +#define au_fset_wrdir(flags, name) \
16409 +       do { (flags) |= AuWrDir_##name; } while (0)
16410 +#define au_fclr_wrdir(flags, name) \
16411 +       do { (flags) &= ~AuWrDir_##name; } while (0)
16412 +
16413 +struct au_wr_dir_args {
16414 +       aufs_bindex_t force_btgt;
16415 +       unsigned char flags;
16416 +};
16417 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16418 +             struct au_wr_dir_args *args);
16419 +
16420 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
16421 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16422 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16423 +                unsigned int udba, unsigned char flags);
16424 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16425 +          unsigned int udba, unsigned char flags) __must_check;
16426 +int au_do_pin(struct au_pin *pin) __must_check;
16427 +void au_unpin(struct au_pin *pin);
16428 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16429 +
16430 +#define AuIcpup_DID_CPUP       1
16431 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
16432 +#define au_fset_icpup(flags, name) \
16433 +       do { (flags) |= AuIcpup_##name; } while (0)
16434 +#define au_fclr_icpup(flags, name) \
16435 +       do { (flags) &= ~AuIcpup_##name; } while (0)
16436 +
16437 +struct au_icpup_args {
16438 +       unsigned char flags;
16439 +       unsigned char pin_flags;
16440 +       aufs_bindex_t btgt;
16441 +       unsigned int udba;
16442 +       struct au_pin pin;
16443 +       struct path h_path;
16444 +       struct inode *h_inode;
16445 +};
16446 +
16447 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16448 +                    struct au_icpup_args *a);
16449 +
16450 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
16451 +
16452 +/* i_op_add.c */
16453 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16454 +              struct dentry *h_parent, int isdir);
16455 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16456 +              dev_t dev);
16457 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
16458 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
16459 +               bool want_excl);
16460 +struct vfsub_aopen_args;
16461 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16462 +                      struct vfsub_aopen_args *args);
16463 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
16464 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
16465 +             struct dentry *dentry);
16466 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
16467 +
16468 +/* i_op_del.c */
16469 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16470 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16471 +              struct dentry *h_parent, int isdir);
16472 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
16473 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
16474 +
16475 +/* i_op_ren.c */
16476 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16477 +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16478 +               struct inode *dir, struct dentry *dentry);
16479 +
16480 +/* iinfo.c */
16481 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16482 +void au_hiput(struct au_hinode *hinode);
16483 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16484 +                 struct dentry *h_wh);
16485 +unsigned int au_hi_flags(struct inode *inode, int isdir);
16486 +
16487 +/* hinode flags */
16488 +#define AuHi_XINO      1
16489 +#define AuHi_HNOTIFY   (1 << 1)
16490 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
16491 +#define au_fset_hi(flags, name) \
16492 +       do { (flags) |= AuHi_##name; } while (0)
16493 +#define au_fclr_hi(flags, name) \
16494 +       do { (flags) &= ~AuHi_##name; } while (0)
16495 +
16496 +#ifndef CONFIG_AUFS_HNOTIFY
16497 +#undef AuHi_HNOTIFY
16498 +#define AuHi_HNOTIFY   0
16499 +#endif
16500 +
16501 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16502 +                  struct inode *h_inode, unsigned int flags);
16503 +
16504 +void au_update_iigen(struct inode *inode, int half);
16505 +void au_update_ibrange(struct inode *inode, int do_put_zero);
16506 +
16507 +void au_icntnr_init_once(void *_c);
16508 +int au_iinfo_init(struct inode *inode);
16509 +void au_iinfo_fin(struct inode *inode);
16510 +int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
16511 +
16512 +#ifdef CONFIG_PROC_FS
16513 +/* plink.c */
16514 +int au_plink_maint(struct super_block *sb, int flags);
16515 +struct au_sbinfo;
16516 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16517 +int au_plink_maint_enter(struct super_block *sb);
16518 +#ifdef CONFIG_AUFS_DEBUG
16519 +void au_plink_list(struct super_block *sb);
16520 +#else
16521 +AuStubVoid(au_plink_list, struct super_block *sb)
16522 +#endif
16523 +int au_plink_test(struct inode *inode);
16524 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16525 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16526 +                    struct dentry *h_dentry);
16527 +void au_plink_put(struct super_block *sb, int verbose);
16528 +void au_plink_clean(struct super_block *sb, int verbose);
16529 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
16530 +#else
16531 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16532 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16533 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16534 +AuStubVoid(au_plink_list, struct super_block *sb);
16535 +AuStubInt0(au_plink_test, struct inode *inode);
16536 +AuStub(struct dentry *, au_plink_lkup, return NULL,
16537 +       struct inode *inode, aufs_bindex_t bindex);
16538 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16539 +          struct dentry *h_dentry);
16540 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16541 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16542 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16543 +#endif /* CONFIG_PROC_FS */
16544 +
16545 +#ifdef CONFIG_AUFS_XATTR
16546 +/* xattr.c */
16547 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16548 +                 unsigned int verbose);
16549 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
16550 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
16551 +                     size_t size);
16552 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
16553 +                 size_t size, int flags);
16554 +int aufs_removexattr(struct dentry *dentry, const char *name);
16555 +
16556 +/* void au_xattr_init(struct super_block *sb); */
16557 +#else
16558 +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
16559 +          int ignore_flags, unsigned int verbose);
16560 +/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16561 +#endif
16562 +
16563 +#ifdef CONFIG_FS_POSIX_ACL
16564 +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16565 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16566 +#endif
16567 +
16568 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16569 +enum {
16570 +       AU_XATTR_SET,
16571 +       AU_XATTR_REMOVE,
16572 +       AU_ACL_SET
16573 +};
16574 +
16575 +struct au_srxattr {
16576 +       int type;
16577 +       union {
16578 +               struct {
16579 +                       const char      *name;
16580 +                       const void      *value;
16581 +                       size_t          size;
16582 +                       int             flags;
16583 +               } set;
16584 +               struct {
16585 +                       const char      *name;
16586 +               } remove;
16587 +               struct {
16588 +                       struct posix_acl *acl;
16589 +                       int             type;
16590 +               } acl_set;
16591 +       } u;
16592 +};
16593 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg);
16594 +#endif
16595 +
16596 +/* ---------------------------------------------------------------------- */
16597 +
16598 +/* lock subclass for iinfo */
16599 +enum {
16600 +       AuLsc_II_CHILD,         /* child first */
16601 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
16602 +       AuLsc_II_CHILD3,        /* copyup dirs */
16603 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
16604 +       AuLsc_II_PARENT2,
16605 +       AuLsc_II_PARENT3,       /* copyup dirs */
16606 +       AuLsc_II_NEW_CHILD
16607 +};
16608 +
16609 +/*
16610 + * ii_read_lock_child, ii_write_lock_child,
16611 + * ii_read_lock_child2, ii_write_lock_child2,
16612 + * ii_read_lock_child3, ii_write_lock_child3,
16613 + * ii_read_lock_parent, ii_write_lock_parent,
16614 + * ii_read_lock_parent2, ii_write_lock_parent2,
16615 + * ii_read_lock_parent3, ii_write_lock_parent3,
16616 + * ii_read_lock_new_child, ii_write_lock_new_child,
16617 + */
16618 +#define AuReadLockFunc(name, lsc) \
16619 +static inline void ii_read_lock_##name(struct inode *i) \
16620 +{ \
16621 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16622 +}
16623 +
16624 +#define AuWriteLockFunc(name, lsc) \
16625 +static inline void ii_write_lock_##name(struct inode *i) \
16626 +{ \
16627 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16628 +}
16629 +
16630 +#define AuRWLockFuncs(name, lsc) \
16631 +       AuReadLockFunc(name, lsc) \
16632 +       AuWriteLockFunc(name, lsc)
16633 +
16634 +AuRWLockFuncs(child, CHILD);
16635 +AuRWLockFuncs(child2, CHILD2);
16636 +AuRWLockFuncs(child3, CHILD3);
16637 +AuRWLockFuncs(parent, PARENT);
16638 +AuRWLockFuncs(parent2, PARENT2);
16639 +AuRWLockFuncs(parent3, PARENT3);
16640 +AuRWLockFuncs(new_child, NEW_CHILD);
16641 +
16642 +#undef AuReadLockFunc
16643 +#undef AuWriteLockFunc
16644 +#undef AuRWLockFuncs
16645 +
16646 +/*
16647 + * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
16648 + */
16649 +AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
16650 +
16651 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
16652 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
16653 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
16654 +
16655 +/* ---------------------------------------------------------------------- */
16656 +
16657 +static inline void au_icntnr_init(struct au_icntnr *c)
16658 +{
16659 +#ifdef CONFIG_AUFS_DEBUG
16660 +       c->vfs_inode.i_mode = 0;
16661 +#endif
16662 +}
16663 +
16664 +static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
16665 +{
16666 +       unsigned int gen;
16667 +       struct au_iinfo *iinfo;
16668 +
16669 +       iinfo = au_ii(inode);
16670 +       spin_lock(&iinfo->ii_genspin);
16671 +       if (iigen)
16672 +               *iigen = iinfo->ii_generation;
16673 +       gen = iinfo->ii_generation.ig_generation;
16674 +       spin_unlock(&iinfo->ii_genspin);
16675 +
16676 +       return gen;
16677 +}
16678 +
16679 +/* tiny test for inode number */
16680 +/* tmpfs generation is too rough */
16681 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
16682 +{
16683 +       struct au_iinfo *iinfo;
16684 +
16685 +       iinfo = au_ii(inode);
16686 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
16687 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
16688 +                && iinfo->ii_higen == h_inode->i_generation);
16689 +}
16690 +
16691 +static inline void au_iigen_dec(struct inode *inode)
16692 +{
16693 +       struct au_iinfo *iinfo;
16694 +
16695 +       iinfo = au_ii(inode);
16696 +       spin_lock(&iinfo->ii_genspin);
16697 +       iinfo->ii_generation.ig_generation--;
16698 +       spin_unlock(&iinfo->ii_genspin);
16699 +}
16700 +
16701 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
16702 +{
16703 +       int err;
16704 +
16705 +       err = 0;
16706 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
16707 +               err = -EIO;
16708 +
16709 +       return err;
16710 +}
16711 +
16712 +/* ---------------------------------------------------------------------- */
16713 +
16714 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
16715 +                                       aufs_bindex_t bindex)
16716 +{
16717 +       IiMustAnyLock(inode);
16718 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
16719 +}
16720 +
16721 +static inline aufs_bindex_t au_ibstart(struct inode *inode)
16722 +{
16723 +       IiMustAnyLock(inode);
16724 +       return au_ii(inode)->ii_bstart;
16725 +}
16726 +
16727 +static inline aufs_bindex_t au_ibend(struct inode *inode)
16728 +{
16729 +       IiMustAnyLock(inode);
16730 +       return au_ii(inode)->ii_bend;
16731 +}
16732 +
16733 +static inline struct au_vdir *au_ivdir(struct inode *inode)
16734 +{
16735 +       IiMustAnyLock(inode);
16736 +       return au_ii(inode)->ii_vdir;
16737 +}
16738 +
16739 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
16740 +{
16741 +       IiMustAnyLock(inode);
16742 +       return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
16743 +}
16744 +
16745 +static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
16746 +{
16747 +       IiMustWriteLock(inode);
16748 +       au_ii(inode)->ii_bstart = bindex;
16749 +}
16750 +
16751 +static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
16752 +{
16753 +       IiMustWriteLock(inode);
16754 +       au_ii(inode)->ii_bend = bindex;
16755 +}
16756 +
16757 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
16758 +{
16759 +       IiMustWriteLock(inode);
16760 +       au_ii(inode)->ii_vdir = vdir;
16761 +}
16762 +
16763 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
16764 +{
16765 +       IiMustAnyLock(inode);
16766 +       return au_ii(inode)->ii_hinode + bindex;
16767 +}
16768 +
16769 +/* ---------------------------------------------------------------------- */
16770 +
16771 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
16772 +{
16773 +       if (pin)
16774 +               return pin->parent;
16775 +       return NULL;
16776 +}
16777 +
16778 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
16779 +{
16780 +       if (pin && pin->hdir)
16781 +               return pin->hdir->hi_inode;
16782 +       return NULL;
16783 +}
16784 +
16785 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
16786 +{
16787 +       if (pin)
16788 +               return pin->hdir;
16789 +       return NULL;
16790 +}
16791 +
16792 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
16793 +{
16794 +       if (pin)
16795 +               pin->dentry = dentry;
16796 +}
16797 +
16798 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
16799 +                                          unsigned char lflag)
16800 +{
16801 +       if (pin) {
16802 +               if (lflag)
16803 +                       au_fset_pin(pin->flags, DI_LOCKED);
16804 +               else
16805 +                       au_fclr_pin(pin->flags, DI_LOCKED);
16806 +       }
16807 +}
16808 +
16809 +#if 0 /* reserved */
16810 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
16811 +{
16812 +       if (pin) {
16813 +               dput(pin->parent);
16814 +               pin->parent = dget(parent);
16815 +       }
16816 +}
16817 +#endif
16818 +
16819 +/* ---------------------------------------------------------------------- */
16820 +
16821 +struct au_branch;
16822 +#ifdef CONFIG_AUFS_HNOTIFY
16823 +struct au_hnotify_op {
16824 +       void (*ctl)(struct au_hinode *hinode, int do_set);
16825 +       int (*alloc)(struct au_hinode *hinode);
16826 +
16827 +       /*
16828 +        * if it returns true, the the caller should free hinode->hi_notify,
16829 +        * otherwise ->free() frees it.
16830 +        */
16831 +       int (*free)(struct au_hinode *hinode,
16832 +                   struct au_hnotify *hn) __must_check;
16833 +
16834 +       void (*fin)(void);
16835 +       int (*init)(void);
16836 +
16837 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
16838 +       void (*fin_br)(struct au_branch *br);
16839 +       int (*init_br)(struct au_branch *br, int perm);
16840 +};
16841 +
16842 +/* hnotify.c */
16843 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
16844 +void au_hn_free(struct au_hinode *hinode);
16845 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
16846 +void au_hn_reset(struct inode *inode, unsigned int flags);
16847 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
16848 +              struct qstr *h_child_qstr, struct inode *h_child_inode);
16849 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
16850 +int au_hnotify_init_br(struct au_branch *br, int perm);
16851 +void au_hnotify_fin_br(struct au_branch *br);
16852 +int __init au_hnotify_init(void);
16853 +void au_hnotify_fin(void);
16854 +
16855 +/* hfsnotify.c */
16856 +extern const struct au_hnotify_op au_hnotify_op;
16857 +
16858 +static inline
16859 +void au_hn_init(struct au_hinode *hinode)
16860 +{
16861 +       hinode->hi_notify = NULL;
16862 +}
16863 +
16864 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
16865 +{
16866 +       return hinode->hi_notify;
16867 +}
16868 +
16869 +#else
16870 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
16871 +       struct au_hinode *hinode __maybe_unused,
16872 +       struct inode *inode __maybe_unused)
16873 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
16874 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
16875 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
16876 +          int do_set __maybe_unused)
16877 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
16878 +          unsigned int flags __maybe_unused)
16879 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
16880 +          struct au_branch *br __maybe_unused,
16881 +          int perm __maybe_unused)
16882 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
16883 +          int perm __maybe_unused)
16884 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
16885 +AuStubInt0(__init au_hnotify_init, void)
16886 +AuStubVoid(au_hnotify_fin, void)
16887 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
16888 +#endif /* CONFIG_AUFS_HNOTIFY */
16889 +
16890 +static inline void au_hn_suspend(struct au_hinode *hdir)
16891 +{
16892 +       au_hn_ctl(hdir, /*do_set*/0);
16893 +}
16894 +
16895 +static inline void au_hn_resume(struct au_hinode *hdir)
16896 +{
16897 +       au_hn_ctl(hdir, /*do_set*/1);
16898 +}
16899 +
16900 +static inline void au_hn_imtx_lock(struct au_hinode *hdir)
16901 +{
16902 +       mutex_lock(&hdir->hi_inode->i_mutex);
16903 +       au_hn_suspend(hdir);
16904 +}
16905 +
16906 +static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
16907 +                                         unsigned int sc __maybe_unused)
16908 +{
16909 +       mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
16910 +       au_hn_suspend(hdir);
16911 +}
16912 +
16913 +static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
16914 +{
16915 +       au_hn_resume(hdir);
16916 +       mutex_unlock(&hdir->hi_inode->i_mutex);
16917 +}
16918 +
16919 +#endif /* __KERNEL__ */
16920 +#endif /* __AUFS_INODE_H__ */
16921 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
16922 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
16923 +++ linux/fs/aufs/ioctl.c       2015-09-24 10:47:58.254719746 +0200
16924 @@ -0,0 +1,219 @@
16925 +/*
16926 + * Copyright (C) 2005-2015 Junjiro R. Okajima
16927 + *
16928 + * This program, aufs is free software; you can redistribute it and/or modify
16929 + * it under the terms of the GNU General Public License as published by
16930 + * the Free Software Foundation; either version 2 of the License, or
16931 + * (at your option) any later version.
16932 + *
16933 + * This program is distributed in the hope that it will be useful,
16934 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16935 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16936 + * GNU General Public License for more details.
16937 + *
16938 + * You should have received a copy of the GNU General Public License
16939 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16940 + */
16941 +
16942 +/*
16943 + * ioctl
16944 + * plink-management and readdir in userspace.
16945 + * assist the pathconf(3) wrapper library.
16946 + * move-down
16947 + * File-based Hierarchical Storage Management.
16948 + */
16949 +
16950 +#include <linux/compat.h>
16951 +#include <linux/file.h>
16952 +#include "aufs.h"
16953 +
16954 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
16955 +{
16956 +       int err, fd;
16957 +       aufs_bindex_t wbi, bindex, bend;
16958 +       struct file *h_file;
16959 +       struct super_block *sb;
16960 +       struct dentry *root;
16961 +       struct au_branch *br;
16962 +       struct aufs_wbr_fd wbrfd = {
16963 +               .oflags = au_dir_roflags,
16964 +               .brid   = -1
16965 +       };
16966 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
16967 +               | O_NOATIME | O_CLOEXEC;
16968 +
16969 +       AuDebugOn(wbrfd.oflags & ~valid);
16970 +
16971 +       if (arg) {
16972 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
16973 +               if (unlikely(err)) {
16974 +                       err = -EFAULT;
16975 +                       goto out;
16976 +               }
16977 +
16978 +               err = -EINVAL;
16979 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
16980 +               wbrfd.oflags |= au_dir_roflags;
16981 +               AuDbg("0%o\n", wbrfd.oflags);
16982 +               if (unlikely(wbrfd.oflags & ~valid))
16983 +                       goto out;
16984 +       }
16985 +
16986 +       fd = get_unused_fd_flags(0);
16987 +       err = fd;
16988 +       if (unlikely(fd < 0))
16989 +               goto out;
16990 +
16991 +       h_file = ERR_PTR(-EINVAL);
16992 +       wbi = 0;
16993 +       br = NULL;
16994 +       sb = path->dentry->d_sb;
16995 +       root = sb->s_root;
16996 +       aufs_read_lock(root, AuLock_IR);
16997 +       bend = au_sbend(sb);
16998 +       if (wbrfd.brid >= 0) {
16999 +               wbi = au_br_index(sb, wbrfd.brid);
17000 +               if (unlikely(wbi < 0 || wbi > bend))
17001 +                       goto out_unlock;
17002 +       }
17003 +
17004 +       h_file = ERR_PTR(-ENOENT);
17005 +       br = au_sbr(sb, wbi);
17006 +       if (!au_br_writable(br->br_perm)) {
17007 +               if (arg)
17008 +                       goto out_unlock;
17009 +
17010 +               bindex = wbi + 1;
17011 +               wbi = -1;
17012 +               for (; bindex <= bend; bindex++) {
17013 +                       br = au_sbr(sb, bindex);
17014 +                       if (au_br_writable(br->br_perm)) {
17015 +                               wbi = bindex;
17016 +                               br = au_sbr(sb, wbi);
17017 +                               break;
17018 +                       }
17019 +               }
17020 +       }
17021 +       AuDbg("wbi %d\n", wbi);
17022 +       if (wbi >= 0)
17023 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17024 +                                  /*force_wr*/0);
17025 +
17026 +out_unlock:
17027 +       aufs_read_unlock(root, AuLock_IR);
17028 +       err = PTR_ERR(h_file);
17029 +       if (IS_ERR(h_file))
17030 +               goto out_fd;
17031 +
17032 +       atomic_dec(&br->br_count); /* cf. au_h_open() */
17033 +       fd_install(fd, h_file);
17034 +       err = fd;
17035 +       goto out; /* success */
17036 +
17037 +out_fd:
17038 +       put_unused_fd(fd);
17039 +out:
17040 +       AuTraceErr(err);
17041 +       return err;
17042 +}
17043 +
17044 +/* ---------------------------------------------------------------------- */
17045 +
17046 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17047 +{
17048 +       long err;
17049 +       struct dentry *dentry;
17050 +
17051 +       switch (cmd) {
17052 +       case AUFS_CTL_RDU:
17053 +       case AUFS_CTL_RDU_INO:
17054 +               err = au_rdu_ioctl(file, cmd, arg);
17055 +               break;
17056 +
17057 +       case AUFS_CTL_WBR_FD:
17058 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17059 +               break;
17060 +
17061 +       case AUFS_CTL_IBUSY:
17062 +               err = au_ibusy_ioctl(file, arg);
17063 +               break;
17064 +
17065 +       case AUFS_CTL_BRINFO:
17066 +               err = au_brinfo_ioctl(file, arg);
17067 +               break;
17068 +
17069 +       case AUFS_CTL_FHSM_FD:
17070 +               dentry = file->f_path.dentry;
17071 +               if (IS_ROOT(dentry))
17072 +                       err = au_fhsm_fd(dentry->d_sb, arg);
17073 +               else
17074 +                       err = -ENOTTY;
17075 +               break;
17076 +
17077 +       default:
17078 +               /* do not call the lower */
17079 +               AuDbg("0x%x\n", cmd);
17080 +               err = -ENOTTY;
17081 +       }
17082 +
17083 +       AuTraceErr(err);
17084 +       return err;
17085 +}
17086 +
17087 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17088 +{
17089 +       long err;
17090 +
17091 +       switch (cmd) {
17092 +       case AUFS_CTL_MVDOWN:
17093 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
17094 +               break;
17095 +
17096 +       case AUFS_CTL_WBR_FD:
17097 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
17098 +               break;
17099 +
17100 +       default:
17101 +               /* do not call the lower */
17102 +               AuDbg("0x%x\n", cmd);
17103 +               err = -ENOTTY;
17104 +       }
17105 +
17106 +       AuTraceErr(err);
17107 +       return err;
17108 +}
17109 +
17110 +#ifdef CONFIG_COMPAT
17111 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17112 +                          unsigned long arg)
17113 +{
17114 +       long err;
17115 +
17116 +       switch (cmd) {
17117 +       case AUFS_CTL_RDU:
17118 +       case AUFS_CTL_RDU_INO:
17119 +               err = au_rdu_compat_ioctl(file, cmd, arg);
17120 +               break;
17121 +
17122 +       case AUFS_CTL_IBUSY:
17123 +               err = au_ibusy_compat_ioctl(file, arg);
17124 +               break;
17125 +
17126 +       case AUFS_CTL_BRINFO:
17127 +               err = au_brinfo_compat_ioctl(file, arg);
17128 +               break;
17129 +
17130 +       default:
17131 +               err = aufs_ioctl_dir(file, cmd, arg);
17132 +       }
17133 +
17134 +       AuTraceErr(err);
17135 +       return err;
17136 +}
17137 +
17138 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17139 +                             unsigned long arg)
17140 +{
17141 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17142 +}
17143 +#endif
17144 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17145 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
17146 +++ linux/fs/aufs/i_op_add.c    2015-09-24 10:47:58.254719746 +0200
17147 @@ -0,0 +1,932 @@
17148 +/*
17149 + * Copyright (C) 2005-2015 Junjiro R. Okajima
17150 + *
17151 + * This program, aufs is free software; you can redistribute it and/or modify
17152 + * it under the terms of the GNU General Public License as published by
17153 + * the Free Software Foundation; either version 2 of the License, or
17154 + * (at your option) any later version.
17155 + *
17156 + * This program is distributed in the hope that it will be useful,
17157 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17158 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17159 + * GNU General Public License for more details.
17160 + *
17161 + * You should have received a copy of the GNU General Public License
17162 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17163 + */
17164 +
17165 +/*
17166 + * inode operations (add entry)
17167 + */
17168 +
17169 +#include "aufs.h"
17170 +
17171 +/*
17172 + * final procedure of adding a new entry, except link(2).
17173 + * remove whiteout, instantiate, copyup the parent dir's times and size
17174 + * and update version.
17175 + * if it failed, re-create the removed whiteout.
17176 + */
17177 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
17178 +                 struct dentry *wh_dentry, struct dentry *dentry)
17179 +{
17180 +       int err, rerr;
17181 +       aufs_bindex_t bwh;
17182 +       struct path h_path;
17183 +       struct super_block *sb;
17184 +       struct inode *inode, *h_dir;
17185 +       struct dentry *wh;
17186 +
17187 +       bwh = -1;
17188 +       sb = dir->i_sb;
17189 +       if (wh_dentry) {
17190 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
17191 +               IMustLock(h_dir);
17192 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17193 +               bwh = au_dbwh(dentry);
17194 +               h_path.dentry = wh_dentry;
17195 +               h_path.mnt = au_sbr_mnt(sb, bindex);
17196 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17197 +                                         dentry);
17198 +               if (unlikely(err))
17199 +                       goto out;
17200 +       }
17201 +
17202 +       inode = au_new_inode(dentry, /*must_new*/1);
17203 +       if (!IS_ERR(inode)) {
17204 +               d_instantiate(dentry, inode);
17205 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
17206 +               IMustLock(dir);
17207 +               au_dir_ts(dir, bindex);
17208 +               dir->i_version++;
17209 +               au_fhsm_wrote(sb, bindex, /*force*/0);
17210 +               return 0; /* success */
17211 +       }
17212 +
17213 +       err = PTR_ERR(inode);
17214 +       if (!wh_dentry)
17215 +               goto out;
17216 +
17217 +       /* revert */
17218 +       /* dir inode is locked */
17219 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17220 +       rerr = PTR_ERR(wh);
17221 +       if (IS_ERR(wh)) {
17222 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17223 +                       dentry, err, rerr);
17224 +               err = -EIO;
17225 +       } else
17226 +               dput(wh);
17227 +
17228 +out:
17229 +       return err;
17230 +}
17231 +
17232 +static int au_d_may_add(struct dentry *dentry)
17233 +{
17234 +       int err;
17235 +
17236 +       err = 0;
17237 +       if (unlikely(d_unhashed(dentry)))
17238 +               err = -ENOENT;
17239 +       if (unlikely(d_really_is_positive(dentry)))
17240 +               err = -EEXIST;
17241 +       return err;
17242 +}
17243 +
17244 +/*
17245 + * simple tests for the adding inode operations.
17246 + * following the checks in vfs, plus the parent-child relationship.
17247 + */
17248 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17249 +              struct dentry *h_parent, int isdir)
17250 +{
17251 +       int err;
17252 +       umode_t h_mode;
17253 +       struct dentry *h_dentry;
17254 +       struct inode *h_inode;
17255 +
17256 +       err = -ENAMETOOLONG;
17257 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17258 +               goto out;
17259 +
17260 +       h_dentry = au_h_dptr(dentry, bindex);
17261 +       if (d_really_is_negative(dentry)) {
17262 +               err = -EEXIST;
17263 +               if (unlikely(d_is_positive(h_dentry)))
17264 +                       goto out;
17265 +       } else {
17266 +               /* rename(2) case */
17267 +               err = -EIO;
17268 +               if (unlikely(d_is_negative(h_dentry)))
17269 +                       goto out;
17270 +               h_inode = d_inode(h_dentry);
17271 +               if (unlikely(!h_inode->i_nlink))
17272 +                       goto out;
17273 +
17274 +               h_mode = h_inode->i_mode;
17275 +               if (!isdir) {
17276 +                       err = -EISDIR;
17277 +                       if (unlikely(S_ISDIR(h_mode)))
17278 +                               goto out;
17279 +               } else if (unlikely(!S_ISDIR(h_mode))) {
17280 +                       err = -ENOTDIR;
17281 +                       goto out;
17282 +               }
17283 +       }
17284 +
17285 +       err = 0;
17286 +       /* expected parent dir is locked */
17287 +       if (unlikely(h_parent != h_dentry->d_parent))
17288 +               err = -EIO;
17289 +
17290 +out:
17291 +       AuTraceErr(err);
17292 +       return err;
17293 +}
17294 +
17295 +/*
17296 + * initial procedure of adding a new entry.
17297 + * prepare writable branch and the parent dir, lock it,
17298 + * and lookup whiteout for the new entry.
17299 + */
17300 +static struct dentry*
17301 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17302 +                 struct dentry *src_dentry, struct au_pin *pin,
17303 +                 struct au_wr_dir_args *wr_dir_args)
17304 +{
17305 +       struct dentry *wh_dentry, *h_parent;
17306 +       struct super_block *sb;
17307 +       struct au_branch *br;
17308 +       int err;
17309 +       unsigned int udba;
17310 +       aufs_bindex_t bcpup;
17311 +
17312 +       AuDbg("%pd\n", dentry);
17313 +
17314 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17315 +       bcpup = err;
17316 +       wh_dentry = ERR_PTR(err);
17317 +       if (unlikely(err < 0))
17318 +               goto out;
17319 +
17320 +       sb = dentry->d_sb;
17321 +       udba = au_opt_udba(sb);
17322 +       err = au_pin(pin, dentry, bcpup, udba,
17323 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17324 +       wh_dentry = ERR_PTR(err);
17325 +       if (unlikely(err))
17326 +               goto out;
17327 +
17328 +       h_parent = au_pinned_h_parent(pin);
17329 +       if (udba != AuOpt_UDBA_NONE
17330 +           && au_dbstart(dentry) == bcpup)
17331 +               err = au_may_add(dentry, bcpup, h_parent,
17332 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17333 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17334 +               err = -ENAMETOOLONG;
17335 +       wh_dentry = ERR_PTR(err);
17336 +       if (unlikely(err))
17337 +               goto out_unpin;
17338 +
17339 +       br = au_sbr(sb, bcpup);
17340 +       if (dt) {
17341 +               struct path tmp = {
17342 +                       .dentry = h_parent,
17343 +                       .mnt    = au_br_mnt(br)
17344 +               };
17345 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17346 +       }
17347 +
17348 +       wh_dentry = NULL;
17349 +       if (bcpup != au_dbwh(dentry))
17350 +               goto out; /* success */
17351 +
17352 +       /*
17353 +        * ENAMETOOLONG here means that if we allowed create such name, then it
17354 +        * would not be able to removed in the future. So we don't allow such
17355 +        * name here and we don't handle ENAMETOOLONG differently here.
17356 +        */
17357 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17358 +
17359 +out_unpin:
17360 +       if (IS_ERR(wh_dentry))
17361 +               au_unpin(pin);
17362 +out:
17363 +       return wh_dentry;
17364 +}
17365 +
17366 +/* ---------------------------------------------------------------------- */
17367 +
17368 +enum { Mknod, Symlink, Creat };
17369 +struct simple_arg {
17370 +       int type;
17371 +       union {
17372 +               struct {
17373 +                       umode_t                 mode;
17374 +                       bool                    want_excl;
17375 +                       bool                    try_aopen;
17376 +                       struct vfsub_aopen_args *aopen;
17377 +               } c;
17378 +               struct {
17379 +                       const char *symname;
17380 +               } s;
17381 +               struct {
17382 +                       umode_t mode;
17383 +                       dev_t dev;
17384 +               } m;
17385 +       } u;
17386 +};
17387 +
17388 +static int add_simple(struct inode *dir, struct dentry *dentry,
17389 +                     struct simple_arg *arg)
17390 +{
17391 +       int err, rerr;
17392 +       aufs_bindex_t bstart;
17393 +       unsigned char created;
17394 +       const unsigned char try_aopen
17395 +               = (arg->type == Creat && arg->u.c.try_aopen);
17396 +       struct dentry *wh_dentry, *parent;
17397 +       struct inode *h_dir;
17398 +       struct super_block *sb;
17399 +       struct au_branch *br;
17400 +       /* to reuduce stack size */
17401 +       struct {
17402 +               struct au_dtime dt;
17403 +               struct au_pin pin;
17404 +               struct path h_path;
17405 +               struct au_wr_dir_args wr_dir_args;
17406 +       } *a;
17407 +
17408 +       AuDbg("%pd\n", dentry);
17409 +       IMustLock(dir);
17410 +
17411 +       err = -ENOMEM;
17412 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17413 +       if (unlikely(!a))
17414 +               goto out;
17415 +       a->wr_dir_args.force_btgt = -1;
17416 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17417 +
17418 +       parent = dentry->d_parent; /* dir inode is locked */
17419 +       if (!try_aopen) {
17420 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17421 +               if (unlikely(err))
17422 +                       goto out_free;
17423 +       }
17424 +       err = au_d_may_add(dentry);
17425 +       if (unlikely(err))
17426 +               goto out_unlock;
17427 +       if (!try_aopen)
17428 +               di_write_lock_parent(parent);
17429 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17430 +                                     &a->pin, &a->wr_dir_args);
17431 +       err = PTR_ERR(wh_dentry);
17432 +       if (IS_ERR(wh_dentry))
17433 +               goto out_parent;
17434 +
17435 +       bstart = au_dbstart(dentry);
17436 +       sb = dentry->d_sb;
17437 +       br = au_sbr(sb, bstart);
17438 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
17439 +       a->h_path.mnt = au_br_mnt(br);
17440 +       h_dir = au_pinned_h_dir(&a->pin);
17441 +       switch (arg->type) {
17442 +       case Creat:
17443 +               err = 0;
17444 +               if (!try_aopen || !h_dir->i_op->atomic_open)
17445 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17446 +                                          arg->u.c.want_excl);
17447 +               else
17448 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17449 +                                               arg->u.c.aopen, br);
17450 +               break;
17451 +       case Symlink:
17452 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
17453 +               break;
17454 +       case Mknod:
17455 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17456 +                                 arg->u.m.dev);
17457 +               break;
17458 +       default:
17459 +               BUG();
17460 +       }
17461 +       created = !err;
17462 +       if (!err)
17463 +               err = epilog(dir, bstart, wh_dentry, dentry);
17464 +
17465 +       /* revert */
17466 +       if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
17467 +               /* no delegation since it is just created */
17468 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17469 +                                   /*force*/0);
17470 +               if (rerr) {
17471 +                       AuIOErr("%pd revert failure(%d, %d)\n",
17472 +                               dentry, err, rerr);
17473 +                       err = -EIO;
17474 +               }
17475 +               au_dtime_revert(&a->dt);
17476 +       }
17477 +
17478 +       if (!err && try_aopen && !h_dir->i_op->atomic_open)
17479 +               *arg->u.c.aopen->opened |= FILE_CREATED;
17480 +
17481 +       au_unpin(&a->pin);
17482 +       dput(wh_dentry);
17483 +
17484 +out_parent:
17485 +       if (!try_aopen)
17486 +               di_write_unlock(parent);
17487 +out_unlock:
17488 +       if (unlikely(err)) {
17489 +               au_update_dbstart(dentry);
17490 +               d_drop(dentry);
17491 +       }
17492 +       if (!try_aopen)
17493 +               aufs_read_unlock(dentry, AuLock_DW);
17494 +out_free:
17495 +       kfree(a);
17496 +out:
17497 +       return err;
17498 +}
17499 +
17500 +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17501 +              dev_t dev)
17502 +{
17503 +       struct simple_arg arg = {
17504 +               .type = Mknod,
17505 +               .u.m = {
17506 +                       .mode   = mode,
17507 +                       .dev    = dev
17508 +               }
17509 +       };
17510 +       return add_simple(dir, dentry, &arg);
17511 +}
17512 +
17513 +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17514 +{
17515 +       struct simple_arg arg = {
17516 +               .type = Symlink,
17517 +               .u.s.symname = symname
17518 +       };
17519 +       return add_simple(dir, dentry, &arg);
17520 +}
17521 +
17522 +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
17523 +               bool want_excl)
17524 +{
17525 +       struct simple_arg arg = {
17526 +               .type = Creat,
17527 +               .u.c = {
17528 +                       .mode           = mode,
17529 +                       .want_excl      = want_excl
17530 +               }
17531 +       };
17532 +       return add_simple(dir, dentry, &arg);
17533 +}
17534 +
17535 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17536 +                      struct vfsub_aopen_args *aopen_args)
17537 +{
17538 +       struct simple_arg arg = {
17539 +               .type = Creat,
17540 +               .u.c = {
17541 +                       .mode           = aopen_args->create_mode,
17542 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
17543 +                       .try_aopen      = true,
17544 +                       .aopen          = aopen_args
17545 +               }
17546 +       };
17547 +       return add_simple(dir, dentry, &arg);
17548 +}
17549 +
17550 +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17551 +{
17552 +       int err;
17553 +       aufs_bindex_t bindex;
17554 +       struct super_block *sb;
17555 +       struct dentry *parent, *h_parent, *h_dentry;
17556 +       struct inode *h_dir, *inode;
17557 +       struct vfsmount *h_mnt;
17558 +       struct au_wr_dir_args wr_dir_args = {
17559 +               .force_btgt     = -1,
17560 +               .flags          = AuWrDir_TMPFILE
17561 +       };
17562 +
17563 +       /* copy-up may happen */
17564 +       mutex_lock(&dir->i_mutex);
17565 +
17566 +       sb = dir->i_sb;
17567 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17568 +       if (unlikely(err))
17569 +               goto out;
17570 +
17571 +       err = au_di_init(dentry);
17572 +       if (unlikely(err))
17573 +               goto out_si;
17574 +
17575 +       err = -EBUSY;
17576 +       parent = d_find_any_alias(dir);
17577 +       AuDebugOn(!parent);
17578 +       di_write_lock_parent(parent);
17579 +       if (unlikely(d_inode(parent) != dir))
17580 +               goto out_parent;
17581 +
17582 +       err = au_digen_test(parent, au_sigen(sb));
17583 +       if (unlikely(err))
17584 +               goto out_parent;
17585 +
17586 +       bindex = au_dbstart(parent);
17587 +       au_set_dbstart(dentry, bindex);
17588 +       au_set_dbend(dentry, bindex);
17589 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17590 +       bindex = err;
17591 +       if (unlikely(err < 0))
17592 +               goto out_parent;
17593 +
17594 +       err = -EOPNOTSUPP;
17595 +       h_dir = au_h_iptr(dir, bindex);
17596 +       if (unlikely(!h_dir->i_op->tmpfile))
17597 +               goto out_parent;
17598 +
17599 +       h_mnt = au_sbr_mnt(sb, bindex);
17600 +       err = vfsub_mnt_want_write(h_mnt);
17601 +       if (unlikely(err))
17602 +               goto out_parent;
17603 +
17604 +       h_parent = au_h_dptr(parent, bindex);
17605 +       err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
17606 +       if (unlikely(err))
17607 +               goto out_mnt;
17608 +
17609 +       err = -ENOMEM;
17610 +       h_dentry = d_alloc(h_parent, &dentry->d_name);
17611 +       if (unlikely(!h_dentry))
17612 +               goto out_mnt;
17613 +
17614 +       err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17615 +       if (unlikely(err))
17616 +               goto out_dentry;
17617 +
17618 +       au_set_dbstart(dentry, bindex);
17619 +       au_set_dbend(dentry, bindex);
17620 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
17621 +       inode = au_new_inode(dentry, /*must_new*/1);
17622 +       if (IS_ERR(inode)) {
17623 +               err = PTR_ERR(inode);
17624 +               au_set_h_dptr(dentry, bindex, NULL);
17625 +               au_set_dbstart(dentry, -1);
17626 +               au_set_dbend(dentry, -1);
17627 +       } else {
17628 +               if (!inode->i_nlink)
17629 +                       set_nlink(inode, 1);
17630 +               d_tmpfile(dentry, inode);
17631 +               au_di(dentry)->di_tmpfile = 1;
17632 +
17633 +               /* update without i_mutex */
17634 +               if (au_ibstart(dir) == au_dbstart(dentry))
17635 +                       au_cpup_attr_timesizes(dir);
17636 +       }
17637 +
17638 +out_dentry:
17639 +       dput(h_dentry);
17640 +out_mnt:
17641 +       vfsub_mnt_drop_write(h_mnt);
17642 +out_parent:
17643 +       di_write_unlock(parent);
17644 +       dput(parent);
17645 +       di_write_unlock(dentry);
17646 +       if (!err)
17647 +#if 0
17648 +               /* verbose coding for lock class name */
17649 +               au_rw_class(&au_di(dentry)->di_rwsem,
17650 +                           au_lc_key + AuLcNonDir_DIINFO);
17651 +#else
17652 +               ;
17653 +#endif
17654 +       else {
17655 +               au_di_fin(dentry);
17656 +               dentry->d_fsdata = NULL;
17657 +       }
17658 +out_si:
17659 +       si_read_unlock(sb);
17660 +out:
17661 +       mutex_unlock(&dir->i_mutex);
17662 +       return err;
17663 +}
17664 +
17665 +/* ---------------------------------------------------------------------- */
17666 +
17667 +struct au_link_args {
17668 +       aufs_bindex_t bdst, bsrc;
17669 +       struct au_pin pin;
17670 +       struct path h_path;
17671 +       struct dentry *src_parent, *parent;
17672 +};
17673 +
17674 +static int au_cpup_before_link(struct dentry *src_dentry,
17675 +                              struct au_link_args *a)
17676 +{
17677 +       int err;
17678 +       struct dentry *h_src_dentry;
17679 +       struct au_cp_generic cpg = {
17680 +               .dentry = src_dentry,
17681 +               .bdst   = a->bdst,
17682 +               .bsrc   = a->bsrc,
17683 +               .len    = -1,
17684 +               .pin    = &a->pin,
17685 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
17686 +       };
17687 +
17688 +       di_read_lock_parent(a->src_parent, AuLock_IR);
17689 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
17690 +       if (unlikely(err))
17691 +               goto out;
17692 +
17693 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
17694 +       err = au_pin(&a->pin, src_dentry, a->bdst,
17695 +                    au_opt_udba(src_dentry->d_sb),
17696 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17697 +       if (unlikely(err))
17698 +               goto out;
17699 +
17700 +       err = au_sio_cpup_simple(&cpg);
17701 +       au_unpin(&a->pin);
17702 +
17703 +out:
17704 +       di_read_unlock(a->src_parent, AuLock_IR);
17705 +       return err;
17706 +}
17707 +
17708 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
17709 +                          struct au_link_args *a)
17710 +{
17711 +       int err;
17712 +       unsigned char plink;
17713 +       aufs_bindex_t bend;
17714 +       struct dentry *h_src_dentry;
17715 +       struct inode *h_inode, *inode, *delegated;
17716 +       struct super_block *sb;
17717 +       struct file *h_file;
17718 +
17719 +       plink = 0;
17720 +       h_inode = NULL;
17721 +       sb = src_dentry->d_sb;
17722 +       inode = d_inode(src_dentry);
17723 +       if (au_ibstart(inode) <= a->bdst)
17724 +               h_inode = au_h_iptr(inode, a->bdst);
17725 +       if (!h_inode || !h_inode->i_nlink) {
17726 +               /* copyup src_dentry as the name of dentry. */
17727 +               bend = au_dbend(dentry);
17728 +               if (bend < a->bsrc)
17729 +                       au_set_dbend(dentry, a->bsrc);
17730 +               au_set_h_dptr(dentry, a->bsrc,
17731 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
17732 +               dget(a->h_path.dentry);
17733 +               au_set_h_dptr(dentry, a->bdst, NULL);
17734 +               AuDbg("temporary d_inode...\n");
17735 +               spin_lock(&dentry->d_lock);
17736 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
17737 +               spin_unlock(&dentry->d_lock);
17738 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
17739 +               if (IS_ERR(h_file))
17740 +                       err = PTR_ERR(h_file);
17741 +               else {
17742 +                       struct au_cp_generic cpg = {
17743 +                               .dentry = dentry,
17744 +                               .bdst   = a->bdst,
17745 +                               .bsrc   = -1,
17746 +                               .len    = -1,
17747 +                               .pin    = &a->pin,
17748 +                               .flags  = AuCpup_KEEPLINO
17749 +                       };
17750 +                       err = au_sio_cpup_simple(&cpg);
17751 +                       au_h_open_post(dentry, a->bsrc, h_file);
17752 +                       if (!err) {
17753 +                               dput(a->h_path.dentry);
17754 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17755 +                       } else
17756 +                               au_set_h_dptr(dentry, a->bdst,
17757 +                                             a->h_path.dentry);
17758 +               }
17759 +               spin_lock(&dentry->d_lock);
17760 +               dentry->d_inode = NULL; /* restore */
17761 +               spin_unlock(&dentry->d_lock);
17762 +               AuDbg("temporary d_inode...done\n");
17763 +               au_set_h_dptr(dentry, a->bsrc, NULL);
17764 +               au_set_dbend(dentry, bend);
17765 +       } else {
17766 +               /* the inode of src_dentry already exists on a.bdst branch */
17767 +               h_src_dentry = d_find_alias(h_inode);
17768 +               if (!h_src_dentry && au_plink_test(inode)) {
17769 +                       plink = 1;
17770 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
17771 +                       err = PTR_ERR(h_src_dentry);
17772 +                       if (IS_ERR(h_src_dentry))
17773 +                               goto out;
17774 +
17775 +                       if (unlikely(d_is_negative(h_src_dentry))) {
17776 +                               dput(h_src_dentry);
17777 +                               h_src_dentry = NULL;
17778 +                       }
17779 +
17780 +               }
17781 +               if (h_src_dentry) {
17782 +                       delegated = NULL;
17783 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17784 +                                        &a->h_path, &delegated);
17785 +                       if (unlikely(err == -EWOULDBLOCK)) {
17786 +                               pr_warn("cannot retry for NFSv4 delegation"
17787 +                                       " for an internal link\n");
17788 +                               iput(delegated);
17789 +                       }
17790 +                       dput(h_src_dentry);
17791 +               } else {
17792 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
17793 +                               h_inode->i_ino, a->bdst);
17794 +                       err = -EIO;
17795 +               }
17796 +       }
17797 +
17798 +       if (!err && !plink)
17799 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
17800 +
17801 +out:
17802 +       AuTraceErr(err);
17803 +       return err;
17804 +}
17805 +
17806 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
17807 +             struct dentry *dentry)
17808 +{
17809 +       int err, rerr;
17810 +       struct au_dtime dt;
17811 +       struct au_link_args *a;
17812 +       struct dentry *wh_dentry, *h_src_dentry;
17813 +       struct inode *inode, *delegated;
17814 +       struct super_block *sb;
17815 +       struct au_wr_dir_args wr_dir_args = {
17816 +               /* .force_btgt  = -1, */
17817 +               .flags          = AuWrDir_ADD_ENTRY
17818 +       };
17819 +
17820 +       IMustLock(dir);
17821 +       inode = d_inode(src_dentry);
17822 +       IMustLock(inode);
17823 +
17824 +       err = -ENOMEM;
17825 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17826 +       if (unlikely(!a))
17827 +               goto out;
17828 +
17829 +       a->parent = dentry->d_parent; /* dir inode is locked */
17830 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
17831 +                                       AuLock_NOPLM | AuLock_GEN);
17832 +       if (unlikely(err))
17833 +               goto out_kfree;
17834 +       err = au_d_linkable(src_dentry);
17835 +       if (unlikely(err))
17836 +               goto out_unlock;
17837 +       err = au_d_may_add(dentry);
17838 +       if (unlikely(err))
17839 +               goto out_unlock;
17840 +
17841 +       a->src_parent = dget_parent(src_dentry);
17842 +       wr_dir_args.force_btgt = au_ibstart(inode);
17843 +
17844 +       di_write_lock_parent(a->parent);
17845 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
17846 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
17847 +                                     &wr_dir_args);
17848 +       err = PTR_ERR(wh_dentry);
17849 +       if (IS_ERR(wh_dentry))
17850 +               goto out_parent;
17851 +
17852 +       err = 0;
17853 +       sb = dentry->d_sb;
17854 +       a->bdst = au_dbstart(dentry);
17855 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
17856 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
17857 +       a->bsrc = au_ibstart(inode);
17858 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17859 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
17860 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
17861 +       if (!h_src_dentry) {
17862 +               a->bsrc = au_dbstart(src_dentry);
17863 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
17864 +               AuDebugOn(!h_src_dentry);
17865 +       } else if (IS_ERR(h_src_dentry)) {
17866 +               err = PTR_ERR(h_src_dentry);
17867 +               goto out_parent;
17868 +       }
17869 +
17870 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
17871 +               if (a->bdst < a->bsrc
17872 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
17873 +                       err = au_cpup_or_link(src_dentry, dentry, a);
17874 +               else {
17875 +                       delegated = NULL;
17876 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
17877 +                                        &a->h_path, &delegated);
17878 +                       if (unlikely(err == -EWOULDBLOCK)) {
17879 +                               pr_warn("cannot retry for NFSv4 delegation"
17880 +                                       " for an internal link\n");
17881 +                               iput(delegated);
17882 +                       }
17883 +               }
17884 +               dput(h_src_dentry);
17885 +       } else {
17886 +               /*
17887 +                * copyup src_dentry to the branch we process,
17888 +                * and then link(2) to it.
17889 +                */
17890 +               dput(h_src_dentry);
17891 +               if (a->bdst < a->bsrc
17892 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
17893 +                       au_unpin(&a->pin);
17894 +                       di_write_unlock(a->parent);
17895 +                       err = au_cpup_before_link(src_dentry, a);
17896 +                       di_write_lock_parent(a->parent);
17897 +                       if (!err)
17898 +                               err = au_pin(&a->pin, dentry, a->bdst,
17899 +                                            au_opt_udba(sb),
17900 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17901 +                       if (unlikely(err))
17902 +                               goto out_wh;
17903 +               }
17904 +               if (!err) {
17905 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
17906 +                       err = -ENOENT;
17907 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
17908 +                               delegated = NULL;
17909 +                               err = vfsub_link(h_src_dentry,
17910 +                                                au_pinned_h_dir(&a->pin),
17911 +                                                &a->h_path, &delegated);
17912 +                               if (unlikely(err == -EWOULDBLOCK)) {
17913 +                                       pr_warn("cannot retry"
17914 +                                               " for NFSv4 delegation"
17915 +                                               " for an internal link\n");
17916 +                                       iput(delegated);
17917 +                               }
17918 +                       }
17919 +               }
17920 +       }
17921 +       if (unlikely(err))
17922 +               goto out_unpin;
17923 +
17924 +       if (wh_dentry) {
17925 +               a->h_path.dentry = wh_dentry;
17926 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
17927 +                                         dentry);
17928 +               if (unlikely(err))
17929 +                       goto out_revert;
17930 +       }
17931 +
17932 +       au_dir_ts(dir, a->bdst);
17933 +       dir->i_version++;
17934 +       inc_nlink(inode);
17935 +       inode->i_ctime = dir->i_ctime;
17936 +       d_instantiate(dentry, au_igrab(inode));
17937 +       if (d_unhashed(a->h_path.dentry))
17938 +               /* some filesystem calls d_drop() */
17939 +               d_drop(dentry);
17940 +       /* some filesystems consume an inode even hardlink */
17941 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
17942 +       goto out_unpin; /* success */
17943 +
17944 +out_revert:
17945 +       /* no delegation since it is just created */
17946 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
17947 +                           /*delegated*/NULL, /*force*/0);
17948 +       if (unlikely(rerr)) {
17949 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
17950 +               err = -EIO;
17951 +       }
17952 +       au_dtime_revert(&dt);
17953 +out_unpin:
17954 +       au_unpin(&a->pin);
17955 +out_wh:
17956 +       dput(wh_dentry);
17957 +out_parent:
17958 +       di_write_unlock(a->parent);
17959 +       dput(a->src_parent);
17960 +out_unlock:
17961 +       if (unlikely(err)) {
17962 +               au_update_dbstart(dentry);
17963 +               d_drop(dentry);
17964 +       }
17965 +       aufs_read_and_write_unlock2(dentry, src_dentry);
17966 +out_kfree:
17967 +       kfree(a);
17968 +out:
17969 +       AuTraceErr(err);
17970 +       return err;
17971 +}
17972 +
17973 +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
17974 +{
17975 +       int err, rerr;
17976 +       aufs_bindex_t bindex;
17977 +       unsigned char diropq;
17978 +       struct path h_path;
17979 +       struct dentry *wh_dentry, *parent, *opq_dentry;
17980 +       struct mutex *h_mtx;
17981 +       struct super_block *sb;
17982 +       struct {
17983 +               struct au_pin pin;
17984 +               struct au_dtime dt;
17985 +       } *a; /* reduce the stack usage */
17986 +       struct au_wr_dir_args wr_dir_args = {
17987 +               .force_btgt     = -1,
17988 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
17989 +       };
17990 +
17991 +       IMustLock(dir);
17992 +
17993 +       err = -ENOMEM;
17994 +       a = kmalloc(sizeof(*a), GFP_NOFS);
17995 +       if (unlikely(!a))
17996 +               goto out;
17997 +
17998 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17999 +       if (unlikely(err))
18000 +               goto out_free;
18001 +       err = au_d_may_add(dentry);
18002 +       if (unlikely(err))
18003 +               goto out_unlock;
18004 +
18005 +       parent = dentry->d_parent; /* dir inode is locked */
18006 +       di_write_lock_parent(parent);
18007 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18008 +                                     &a->pin, &wr_dir_args);
18009 +       err = PTR_ERR(wh_dentry);
18010 +       if (IS_ERR(wh_dentry))
18011 +               goto out_parent;
18012 +
18013 +       sb = dentry->d_sb;
18014 +       bindex = au_dbstart(dentry);
18015 +       h_path.dentry = au_h_dptr(dentry, bindex);
18016 +       h_path.mnt = au_sbr_mnt(sb, bindex);
18017 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18018 +       if (unlikely(err))
18019 +               goto out_unpin;
18020 +
18021 +       /* make the dir opaque */
18022 +       diropq = 0;
18023 +       h_mtx = &d_inode(h_path.dentry)->i_mutex;
18024 +       if (wh_dentry
18025 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
18026 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18027 +               opq_dentry = au_diropq_create(dentry, bindex);
18028 +               mutex_unlock(h_mtx);
18029 +               err = PTR_ERR(opq_dentry);
18030 +               if (IS_ERR(opq_dentry))
18031 +                       goto out_dir;
18032 +               dput(opq_dentry);
18033 +               diropq = 1;
18034 +       }
18035 +
18036 +       err = epilog(dir, bindex, wh_dentry, dentry);
18037 +       if (!err) {
18038 +               inc_nlink(dir);
18039 +               goto out_unpin; /* success */
18040 +       }
18041 +
18042 +       /* revert */
18043 +       if (diropq) {
18044 +               AuLabel(revert opq);
18045 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
18046 +               rerr = au_diropq_remove(dentry, bindex);
18047 +               mutex_unlock(h_mtx);
18048 +               if (rerr) {
18049 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18050 +                               dentry, err, rerr);
18051 +                       err = -EIO;
18052 +               }
18053 +       }
18054 +
18055 +out_dir:
18056 +       AuLabel(revert dir);
18057 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18058 +       if (rerr) {
18059 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
18060 +                       dentry, err, rerr);
18061 +               err = -EIO;
18062 +       }
18063 +       au_dtime_revert(&a->dt);
18064 +out_unpin:
18065 +       au_unpin(&a->pin);
18066 +       dput(wh_dentry);
18067 +out_parent:
18068 +       di_write_unlock(parent);
18069 +out_unlock:
18070 +       if (unlikely(err)) {
18071 +               au_update_dbstart(dentry);
18072 +               d_drop(dentry);
18073 +       }
18074 +       aufs_read_unlock(dentry, AuLock_DW);
18075 +out_free:
18076 +       kfree(a);
18077 +out:
18078 +       return err;
18079 +}
18080 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18081 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
18082 +++ linux/fs/aufs/i_op.c        2015-09-24 10:47:58.254719746 +0200
18083 @@ -0,0 +1,1483 @@
18084 +/*
18085 + * Copyright (C) 2005-2015 Junjiro R. Okajima
18086 + *
18087 + * This program, aufs is free software; you can redistribute it and/or modify
18088 + * it under the terms of the GNU General Public License as published by
18089 + * the Free Software Foundation; either version 2 of the License, or
18090 + * (at your option) any later version.
18091 + *
18092 + * This program is distributed in the hope that it will be useful,
18093 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18094 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18095 + * GNU General Public License for more details.
18096 + *
18097 + * You should have received a copy of the GNU General Public License
18098 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18099 + */
18100 +
18101 +/*
18102 + * inode operations (except add/del/rename)
18103 + */
18104 +
18105 +#include <linux/device_cgroup.h>
18106 +#include <linux/fs_stack.h>
18107 +#include <linux/mm.h>
18108 +#include <linux/namei.h>
18109 +#include <linux/security.h>
18110 +#include "aufs.h"
18111 +
18112 +static int h_permission(struct inode *h_inode, int mask,
18113 +                       struct vfsmount *h_mnt, int brperm)
18114 +{
18115 +       int err;
18116 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18117 +
18118 +       err = -EACCES;
18119 +       if ((write_mask && IS_IMMUTABLE(h_inode))
18120 +           || ((mask & MAY_EXEC)
18121 +               && S_ISREG(h_inode->i_mode)
18122 +               && ((h_mnt->mnt_flags & MNT_NOEXEC)
18123 +                   || !(h_inode->i_mode & S_IXUGO))))
18124 +               goto out;
18125 +
18126 +       /*
18127 +        * - skip the lower fs test in the case of write to ro branch.
18128 +        * - nfs dir permission write check is optimized, but a policy for
18129 +        *   link/rename requires a real check.
18130 +        * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18131 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
18132 +        */
18133 +       if ((write_mask && !au_br_writable(brperm))
18134 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18135 +               && write_mask && !(mask & MAY_READ))
18136 +           || !h_inode->i_op->permission) {
18137 +               /* AuLabel(generic_permission); */
18138 +               /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
18139 +               err = generic_permission(h_inode, mask);
18140 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18141 +                       err = h_inode->i_op->permission(h_inode, mask);
18142 +               AuTraceErr(err);
18143 +       } else {
18144 +               /* AuLabel(h_inode->permission); */
18145 +               err = h_inode->i_op->permission(h_inode, mask);
18146 +               AuTraceErr(err);
18147 +       }
18148 +
18149 +       if (!err)
18150 +               err = devcgroup_inode_permission(h_inode, mask);
18151 +       if (!err)
18152 +               err = security_inode_permission(h_inode, mask);
18153 +
18154 +#if 0
18155 +       if (!err) {
18156 +               /* todo: do we need to call ima_path_check()? */
18157 +               struct path h_path = {
18158 +                       .dentry =
18159 +                       .mnt    = h_mnt
18160 +               };
18161 +               err = ima_path_check(&h_path,
18162 +                                    mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18163 +                                    IMA_COUNT_LEAVE);
18164 +       }
18165 +#endif
18166 +
18167 +out:
18168 +       return err;
18169 +}
18170 +
18171 +static int aufs_permission(struct inode *inode, int mask)
18172 +{
18173 +       int err;
18174 +       aufs_bindex_t bindex, bend;
18175 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18176 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18177 +       struct inode *h_inode;
18178 +       struct super_block *sb;
18179 +       struct au_branch *br;
18180 +
18181 +       /* todo: support rcu-walk? */
18182 +       if (mask & MAY_NOT_BLOCK)
18183 +               return -ECHILD;
18184 +
18185 +       sb = inode->i_sb;
18186 +       si_read_lock(sb, AuLock_FLUSH);
18187 +       ii_read_lock_child(inode);
18188 +#if 0
18189 +       err = au_iigen_test(inode, au_sigen(sb));
18190 +       if (unlikely(err))
18191 +               goto out;
18192 +#endif
18193 +
18194 +       if (!isdir
18195 +           || write_mask
18196 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
18197 +               err = au_busy_or_stale();
18198 +               h_inode = au_h_iptr(inode, au_ibstart(inode));
18199 +               if (unlikely(!h_inode
18200 +                            || (h_inode->i_mode & S_IFMT)
18201 +                            != (inode->i_mode & S_IFMT)))
18202 +                       goto out;
18203 +
18204 +               err = 0;
18205 +               bindex = au_ibstart(inode);
18206 +               br = au_sbr(sb, bindex);
18207 +               err = h_permission(h_inode, mask, au_br_mnt(br), br->br_perm);
18208 +               if (write_mask
18209 +                   && !err
18210 +                   && !special_file(h_inode->i_mode)) {
18211 +                       /* test whether the upper writable branch exists */
18212 +                       err = -EROFS;
18213 +                       for (; bindex >= 0; bindex--)
18214 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
18215 +                                       err = 0;
18216 +                                       break;
18217 +                               }
18218 +               }
18219 +               goto out;
18220 +       }
18221 +
18222 +       /* non-write to dir */
18223 +       err = 0;
18224 +       bend = au_ibend(inode);
18225 +       for (bindex = au_ibstart(inode); !err && bindex <= bend; bindex++) {
18226 +               h_inode = au_h_iptr(inode, bindex);
18227 +               if (h_inode) {
18228 +                       err = au_busy_or_stale();
18229 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
18230 +                               break;
18231 +
18232 +                       br = au_sbr(sb, bindex);
18233 +                       err = h_permission(h_inode, mask, au_br_mnt(br),
18234 +                                          br->br_perm);
18235 +               }
18236 +       }
18237 +
18238 +out:
18239 +       ii_read_unlock(inode);
18240 +       si_read_unlock(sb);
18241 +       return err;
18242 +}
18243 +
18244 +/* ---------------------------------------------------------------------- */
18245 +
18246 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
18247 +                                 unsigned int flags)
18248 +{
18249 +       struct dentry *ret, *parent;
18250 +       struct inode *inode;
18251 +       struct super_block *sb;
18252 +       int err, npositive;
18253 +
18254 +       IMustLock(dir);
18255 +
18256 +       /* todo: support rcu-walk? */
18257 +       ret = ERR_PTR(-ECHILD);
18258 +       if (flags & LOOKUP_RCU)
18259 +               goto out;
18260 +
18261 +       ret = ERR_PTR(-ENAMETOOLONG);
18262 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18263 +               goto out;
18264 +
18265 +       sb = dir->i_sb;
18266 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18267 +       ret = ERR_PTR(err);
18268 +       if (unlikely(err))
18269 +               goto out;
18270 +
18271 +       err = au_di_init(dentry);
18272 +       ret = ERR_PTR(err);
18273 +       if (unlikely(err))
18274 +               goto out_si;
18275 +
18276 +       inode = NULL;
18277 +       npositive = 0; /* suppress a warning */
18278 +       parent = dentry->d_parent; /* dir inode is locked */
18279 +       di_read_lock_parent(parent, AuLock_IR);
18280 +       err = au_alive_dir(parent);
18281 +       if (!err)
18282 +               err = au_digen_test(parent, au_sigen(sb));
18283 +       if (!err) {
18284 +               npositive = au_lkup_dentry(dentry, au_dbstart(parent),
18285 +                                          /*type*/0);
18286 +               err = npositive;
18287 +       }
18288 +       di_read_unlock(parent, AuLock_IR);
18289 +       ret = ERR_PTR(err);
18290 +       if (unlikely(err < 0))
18291 +               goto out_unlock;
18292 +
18293 +       if (npositive) {
18294 +               inode = au_new_inode(dentry, /*must_new*/0);
18295 +               if (IS_ERR(inode)) {
18296 +                       ret = (void *)inode;
18297 +                       inode = NULL;
18298 +                       goto out_unlock;
18299 +               }
18300 +       }
18301 +
18302 +       if (inode)
18303 +               atomic_inc(&inode->i_count);
18304 +       ret = d_splice_alias(inode, dentry);
18305 +#if 0
18306 +       if (unlikely(d_need_lookup(dentry))) {
18307 +               spin_lock(&dentry->d_lock);
18308 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18309 +               spin_unlock(&dentry->d_lock);
18310 +       } else
18311 +#endif
18312 +       if (inode) {
18313 +               if (!IS_ERR(ret)) {
18314 +                       iput(inode);
18315 +                       if (ret && ret != dentry)
18316 +                               ii_write_unlock(inode);
18317 +               } else {
18318 +                       ii_write_unlock(inode);
18319 +                       iput(inode);
18320 +                       inode = NULL;
18321 +               }
18322 +       }
18323 +
18324 +out_unlock:
18325 +       di_write_unlock(dentry);
18326 +       if (inode) {
18327 +               /* verbose coding for lock class name */
18328 +               if (unlikely(S_ISLNK(inode->i_mode)))
18329 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18330 +                                   au_lc_key + AuLcSymlink_DIINFO);
18331 +               else if (unlikely(S_ISDIR(inode->i_mode)))
18332 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18333 +                                   au_lc_key + AuLcDir_DIINFO);
18334 +               else /* likely */
18335 +                       au_rw_class(&au_di(dentry)->di_rwsem,
18336 +                                   au_lc_key + AuLcNonDir_DIINFO);
18337 +       }
18338 +out_si:
18339 +       si_read_unlock(sb);
18340 +out:
18341 +       return ret;
18342 +}
18343 +
18344 +/* ---------------------------------------------------------------------- */
18345 +
18346 +struct aopen_node {
18347 +       struct hlist_node hlist;
18348 +       struct file *file, *h_file;
18349 +};
18350 +
18351 +static int au_do_aopen(struct inode *inode, struct file *file)
18352 +{
18353 +       struct au_sphlhead *aopen;
18354 +       struct aopen_node *node;
18355 +       struct au_do_open_args args = {
18356 +               .no_lock        = 1,
18357 +               .open           = au_do_open_nondir
18358 +       };
18359 +
18360 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
18361 +       spin_lock(&aopen->spin);
18362 +       hlist_for_each_entry(node, &aopen->head, hlist)
18363 +               if (node->file == file) {
18364 +                       args.h_file = node->h_file;
18365 +                       break;
18366 +               }
18367 +       spin_unlock(&aopen->spin);
18368 +       /* AuDebugOn(!args.h_file); */
18369 +
18370 +       return au_do_open(file, &args);
18371 +}
18372 +
18373 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18374 +                           struct file *file, unsigned int open_flag,
18375 +                           umode_t create_mode, int *opened)
18376 +{
18377 +       int err, h_opened = *opened;
18378 +       struct dentry *parent;
18379 +       struct dentry *d;
18380 +       struct au_sphlhead *aopen;
18381 +       struct vfsub_aopen_args args = {
18382 +               .open_flag      = open_flag,
18383 +               .create_mode    = create_mode,
18384 +               .opened         = &h_opened
18385 +       };
18386 +       struct aopen_node aopen_node = {
18387 +               .file   = file
18388 +       };
18389 +
18390 +       IMustLock(dir);
18391 +       AuDbg("open_flag 0x%x\n", open_flag);
18392 +       AuDbgDentry(dentry);
18393 +
18394 +       err = 0;
18395 +       if (!au_di(dentry)) {
18396 +               d = aufs_lookup(dir, dentry, /*flags*/0);
18397 +               if (IS_ERR(d)) {
18398 +                       err = PTR_ERR(d);
18399 +                       goto out;
18400 +               } else if (d) {
18401 +                       /*
18402 +                        * obsoleted dentry found.
18403 +                        * another error will be returned later.
18404 +                        */
18405 +                       d_drop(d);
18406 +                       dput(d);
18407 +                       AuDbgDentry(d);
18408 +               }
18409 +               AuDbgDentry(dentry);
18410 +       }
18411 +
18412 +       if (d_is_positive(dentry)
18413 +           || d_unhashed(dentry)
18414 +           || d_unlinked(dentry)
18415 +           || !(open_flag & O_CREAT))
18416 +               goto out_no_open;
18417 +
18418 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18419 +       if (unlikely(err))
18420 +               goto out;
18421 +
18422 +       parent = dentry->d_parent;      /* dir is locked */
18423 +       di_write_lock_parent(parent);
18424 +       err = au_lkup_dentry(dentry, /*bstart*/0, /*type*/0);
18425 +       if (unlikely(err))
18426 +               goto out_unlock;
18427 +
18428 +       AuDbgDentry(dentry);
18429 +       if (d_is_positive(dentry))
18430 +               goto out_unlock;
18431 +
18432 +       args.file = get_empty_filp();
18433 +       err = PTR_ERR(args.file);
18434 +       if (IS_ERR(args.file))
18435 +               goto out_unlock;
18436 +
18437 +       args.file->f_flags = file->f_flags;
18438 +       err = au_aopen_or_create(dir, dentry, &args);
18439 +       AuTraceErr(err);
18440 +       AuDbgFile(args.file);
18441 +       if (unlikely(err < 0)) {
18442 +               if (h_opened & FILE_OPENED)
18443 +                       fput(args.file);
18444 +               else
18445 +                       put_filp(args.file);
18446 +               goto out_unlock;
18447 +       }
18448 +
18449 +       /* some filesystems don't set FILE_CREATED while succeeded? */
18450 +       *opened |= FILE_CREATED;
18451 +       if (h_opened & FILE_OPENED)
18452 +               aopen_node.h_file = args.file;
18453 +       else {
18454 +               put_filp(args.file);
18455 +               args.file = NULL;
18456 +       }
18457 +       aopen = &au_sbi(dir->i_sb)->si_aopen;
18458 +       au_sphl_add(&aopen_node.hlist, aopen);
18459 +       err = finish_open(file, dentry, au_do_aopen, opened);
18460 +       au_sphl_del(&aopen_node.hlist, aopen);
18461 +       AuTraceErr(err);
18462 +       AuDbgFile(file);
18463 +       if (aopen_node.h_file)
18464 +               fput(aopen_node.h_file);
18465 +
18466 +out_unlock:
18467 +       di_write_unlock(parent);
18468 +       aufs_read_unlock(dentry, AuLock_DW);
18469 +       AuDbgDentry(dentry);
18470 +       if (unlikely(err))
18471 +               goto out;
18472 +out_no_open:
18473 +       if (!err && !(*opened & FILE_CREATED)) {
18474 +               AuLabel(out_no_open);
18475 +               dget(dentry);
18476 +               err = finish_no_open(file, dentry);
18477 +       }
18478 +out:
18479 +       AuDbg("%pd%s%s\n", dentry,
18480 +             (*opened & FILE_CREATED) ? " created" : "",
18481 +             (*opened & FILE_OPENED) ? " opened" : "");
18482 +       AuTraceErr(err);
18483 +       return err;
18484 +}
18485 +
18486 +
18487 +/* ---------------------------------------------------------------------- */
18488 +
18489 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18490 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
18491 +                         aufs_bindex_t bstart)
18492 +{
18493 +       int err;
18494 +       struct dentry *h_parent;
18495 +       struct inode *h_dir;
18496 +
18497 +       if (add_entry)
18498 +               IMustLock(d_inode(parent));
18499 +       else
18500 +               di_write_lock_parent(parent);
18501 +
18502 +       err = 0;
18503 +       if (!au_h_dptr(parent, bcpup)) {
18504 +               if (bstart > bcpup)
18505 +                       err = au_cpup_dirs(dentry, bcpup);
18506 +               else if (bstart < bcpup)
18507 +                       err = au_cpdown_dirs(dentry, bcpup);
18508 +               else
18509 +                       BUG();
18510 +       }
18511 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
18512 +               h_parent = au_h_dptr(parent, bcpup);
18513 +               h_dir = d_inode(h_parent);
18514 +               mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
18515 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
18516 +               /* todo: no unlock here */
18517 +               mutex_unlock(&h_dir->i_mutex);
18518 +
18519 +               AuDbg("bcpup %d\n", bcpup);
18520 +               if (!err) {
18521 +                       if (d_really_is_negative(dentry))
18522 +                               au_set_h_dptr(dentry, bstart, NULL);
18523 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
18524 +               }
18525 +       }
18526 +
18527 +       if (!add_entry)
18528 +               di_write_unlock(parent);
18529 +       if (!err)
18530 +               err = bcpup; /* success */
18531 +
18532 +       AuTraceErr(err);
18533 +       return err;
18534 +}
18535 +
18536 +/*
18537 + * decide the branch and the parent dir where we will create a new entry.
18538 + * returns new bindex or an error.
18539 + * copyup the parent dir if needed.
18540 + */
18541 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18542 +             struct au_wr_dir_args *args)
18543 +{
18544 +       int err;
18545 +       unsigned int flags;
18546 +       aufs_bindex_t bcpup, bstart, src_bstart;
18547 +       const unsigned char add_entry
18548 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
18549 +               | au_ftest_wrdir(args->flags, TMPFILE);
18550 +       struct super_block *sb;
18551 +       struct dentry *parent;
18552 +       struct au_sbinfo *sbinfo;
18553 +
18554 +       sb = dentry->d_sb;
18555 +       sbinfo = au_sbi(sb);
18556 +       parent = dget_parent(dentry);
18557 +       bstart = au_dbstart(dentry);
18558 +       bcpup = bstart;
18559 +       if (args->force_btgt < 0) {
18560 +               if (src_dentry) {
18561 +                       src_bstart = au_dbstart(src_dentry);
18562 +                       if (src_bstart < bstart)
18563 +                               bcpup = src_bstart;
18564 +               } else if (add_entry) {
18565 +                       flags = 0;
18566 +                       if (au_ftest_wrdir(args->flags, ISDIR))
18567 +                               au_fset_wbr(flags, DIR);
18568 +                       err = AuWbrCreate(sbinfo, dentry, flags);
18569 +                       bcpup = err;
18570 +               }
18571 +
18572 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
18573 +                       if (add_entry)
18574 +                               err = AuWbrCopyup(sbinfo, dentry);
18575 +                       else {
18576 +                               if (!IS_ROOT(dentry)) {
18577 +                                       di_read_lock_parent(parent, !AuLock_IR);
18578 +                                       err = AuWbrCopyup(sbinfo, dentry);
18579 +                                       di_read_unlock(parent, !AuLock_IR);
18580 +                               } else
18581 +                                       err = AuWbrCopyup(sbinfo, dentry);
18582 +                       }
18583 +                       bcpup = err;
18584 +                       if (unlikely(err < 0))
18585 +                               goto out;
18586 +               }
18587 +       } else {
18588 +               bcpup = args->force_btgt;
18589 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
18590 +       }
18591 +
18592 +       AuDbg("bstart %d, bcpup %d\n", bstart, bcpup);
18593 +       err = bcpup;
18594 +       if (bcpup == bstart)
18595 +               goto out; /* success */
18596 +
18597 +       /* copyup the new parent into the branch we process */
18598 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, bstart);
18599 +       if (err >= 0) {
18600 +               if (d_really_is_negative(dentry)) {
18601 +                       au_set_h_dptr(dentry, bstart, NULL);
18602 +                       au_set_dbstart(dentry, bcpup);
18603 +                       au_set_dbend(dentry, bcpup);
18604 +               }
18605 +               AuDebugOn(add_entry
18606 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
18607 +                         && !au_h_dptr(dentry, bcpup));
18608 +       }
18609 +
18610 +out:
18611 +       dput(parent);
18612 +       return err;
18613 +}
18614 +
18615 +/* ---------------------------------------------------------------------- */
18616 +
18617 +void au_pin_hdir_unlock(struct au_pin *p)
18618 +{
18619 +       if (p->hdir)
18620 +               au_hn_imtx_unlock(p->hdir);
18621 +}
18622 +
18623 +int au_pin_hdir_lock(struct au_pin *p)
18624 +{
18625 +       int err;
18626 +
18627 +       err = 0;
18628 +       if (!p->hdir)
18629 +               goto out;
18630 +
18631 +       /* even if an error happens later, keep this lock */
18632 +       au_hn_imtx_lock_nested(p->hdir, p->lsc_hi);
18633 +
18634 +       err = -EBUSY;
18635 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
18636 +               goto out;
18637 +
18638 +       err = 0;
18639 +       if (p->h_dentry)
18640 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
18641 +                                 p->h_parent, p->br);
18642 +
18643 +out:
18644 +       return err;
18645 +}
18646 +
18647 +int au_pin_hdir_relock(struct au_pin *p)
18648 +{
18649 +       int err, i;
18650 +       struct inode *h_i;
18651 +       struct dentry *h_d[] = {
18652 +               p->h_dentry,
18653 +               p->h_parent
18654 +       };
18655 +
18656 +       err = au_pin_hdir_lock(p);
18657 +       if (unlikely(err))
18658 +               goto out;
18659 +
18660 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
18661 +               if (!h_d[i])
18662 +                       continue;
18663 +               if (d_is_positive(h_d[i])) {
18664 +                       h_i = d_inode(h_d[i]);
18665 +                       err = !h_i->i_nlink;
18666 +               }
18667 +       }
18668 +
18669 +out:
18670 +       return err;
18671 +}
18672 +
18673 +void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
18674 +{
18675 +#if defined(CONFIG_DEBUG_MUTEXES) || defined(CONFIG_SMP)
18676 +       p->hdir->hi_inode->i_mutex.owner = task;
18677 +#endif
18678 +}
18679 +
18680 +void au_pin_hdir_acquire_nest(struct au_pin *p)
18681 +{
18682 +       if (p->hdir) {
18683 +               mutex_acquire_nest(&p->hdir->hi_inode->i_mutex.dep_map,
18684 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
18685 +               au_pin_hdir_set_owner(p, current);
18686 +       }
18687 +}
18688 +
18689 +void au_pin_hdir_release(struct au_pin *p)
18690 +{
18691 +       if (p->hdir) {
18692 +               au_pin_hdir_set_owner(p, p->task);
18693 +               mutex_release(&p->hdir->hi_inode->i_mutex.dep_map, 1, _RET_IP_);
18694 +       }
18695 +}
18696 +
18697 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
18698 +{
18699 +       if (pin && pin->parent)
18700 +               return au_h_dptr(pin->parent, pin->bindex);
18701 +       return NULL;
18702 +}
18703 +
18704 +void au_unpin(struct au_pin *p)
18705 +{
18706 +       if (p->hdir)
18707 +               au_pin_hdir_unlock(p);
18708 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
18709 +               vfsub_mnt_drop_write(p->h_mnt);
18710 +       if (!p->hdir)
18711 +               return;
18712 +
18713 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18714 +               di_read_unlock(p->parent, AuLock_IR);
18715 +       iput(p->hdir->hi_inode);
18716 +       dput(p->parent);
18717 +       p->parent = NULL;
18718 +       p->hdir = NULL;
18719 +       p->h_mnt = NULL;
18720 +       /* do not clear p->task */
18721 +}
18722 +
18723 +int au_do_pin(struct au_pin *p)
18724 +{
18725 +       int err;
18726 +       struct super_block *sb;
18727 +       struct inode *h_dir;
18728 +
18729 +       err = 0;
18730 +       sb = p->dentry->d_sb;
18731 +       p->br = au_sbr(sb, p->bindex);
18732 +       if (IS_ROOT(p->dentry)) {
18733 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
18734 +                       p->h_mnt = au_br_mnt(p->br);
18735 +                       err = vfsub_mnt_want_write(p->h_mnt);
18736 +                       if (unlikely(err)) {
18737 +                               au_fclr_pin(p->flags, MNT_WRITE);
18738 +                               goto out_err;
18739 +                       }
18740 +               }
18741 +               goto out;
18742 +       }
18743 +
18744 +       p->h_dentry = NULL;
18745 +       if (p->bindex <= au_dbend(p->dentry))
18746 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
18747 +
18748 +       p->parent = dget_parent(p->dentry);
18749 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
18750 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
18751 +
18752 +       h_dir = NULL;
18753 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
18754 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
18755 +       if (p->hdir)
18756 +               h_dir = p->hdir->hi_inode;
18757 +
18758 +       /*
18759 +        * udba case, or
18760 +        * if DI_LOCKED is not set, then p->parent may be different
18761 +        * and h_parent can be NULL.
18762 +        */
18763 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
18764 +               err = -EBUSY;
18765 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
18766 +                       di_read_unlock(p->parent, AuLock_IR);
18767 +               dput(p->parent);
18768 +               p->parent = NULL;
18769 +               goto out_err;
18770 +       }
18771 +
18772 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
18773 +               p->h_mnt = au_br_mnt(p->br);
18774 +               err = vfsub_mnt_want_write(p->h_mnt);
18775 +               if (unlikely(err)) {
18776 +                       au_fclr_pin(p->flags, MNT_WRITE);
18777 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
18778 +                               di_read_unlock(p->parent, AuLock_IR);
18779 +                       dput(p->parent);
18780 +                       p->parent = NULL;
18781 +                       goto out_err;
18782 +               }
18783 +       }
18784 +
18785 +       au_igrab(h_dir);
18786 +       err = au_pin_hdir_lock(p);
18787 +       if (!err)
18788 +               goto out; /* success */
18789 +
18790 +       au_unpin(p);
18791 +
18792 +out_err:
18793 +       pr_err("err %d\n", err);
18794 +       err = au_busy_or_stale();
18795 +out:
18796 +       return err;
18797 +}
18798 +
18799 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
18800 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18801 +                unsigned int udba, unsigned char flags)
18802 +{
18803 +       p->dentry = dentry;
18804 +       p->udba = udba;
18805 +       p->lsc_di = lsc_di;
18806 +       p->lsc_hi = lsc_hi;
18807 +       p->flags = flags;
18808 +       p->bindex = bindex;
18809 +
18810 +       p->parent = NULL;
18811 +       p->hdir = NULL;
18812 +       p->h_mnt = NULL;
18813 +
18814 +       p->h_dentry = NULL;
18815 +       p->h_parent = NULL;
18816 +       p->br = NULL;
18817 +       p->task = current;
18818 +}
18819 +
18820 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18821 +          unsigned int udba, unsigned char flags)
18822 +{
18823 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
18824 +                   udba, flags);
18825 +       return au_do_pin(pin);
18826 +}
18827 +
18828 +/* ---------------------------------------------------------------------- */
18829 +
18830 +/*
18831 + * ->setattr() and ->getattr() are called in various cases.
18832 + * chmod, stat: dentry is revalidated.
18833 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
18834 + *               unhashed.
18835 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
18836 + */
18837 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
18838 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
18839 +{
18840 +       int err;
18841 +       struct dentry *parent;
18842 +
18843 +       err = 0;
18844 +       if (au_digen_test(dentry, sigen)) {
18845 +               parent = dget_parent(dentry);
18846 +               di_read_lock_parent(parent, AuLock_IR);
18847 +               err = au_refresh_dentry(dentry, parent);
18848 +               di_read_unlock(parent, AuLock_IR);
18849 +               dput(parent);
18850 +       }
18851 +
18852 +       AuTraceErr(err);
18853 +       return err;
18854 +}
18855 +
18856 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18857 +                    struct au_icpup_args *a)
18858 +{
18859 +       int err;
18860 +       loff_t sz;
18861 +       aufs_bindex_t bstart, ibstart;
18862 +       struct dentry *hi_wh, *parent;
18863 +       struct inode *inode;
18864 +       struct au_wr_dir_args wr_dir_args = {
18865 +               .force_btgt     = -1,
18866 +               .flags          = 0
18867 +       };
18868 +
18869 +       if (d_is_dir(dentry))
18870 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
18871 +       /* plink or hi_wh() case */
18872 +       bstart = au_dbstart(dentry);
18873 +       inode = d_inode(dentry);
18874 +       ibstart = au_ibstart(inode);
18875 +       if (bstart != ibstart && !au_test_ro(inode->i_sb, ibstart, inode))
18876 +               wr_dir_args.force_btgt = ibstart;
18877 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
18878 +       if (unlikely(err < 0))
18879 +               goto out;
18880 +       a->btgt = err;
18881 +       if (err != bstart)
18882 +               au_fset_icpup(a->flags, DID_CPUP);
18883 +
18884 +       err = 0;
18885 +       a->pin_flags = AuPin_MNT_WRITE;
18886 +       parent = NULL;
18887 +       if (!IS_ROOT(dentry)) {
18888 +               au_fset_pin(a->pin_flags, DI_LOCKED);
18889 +               parent = dget_parent(dentry);
18890 +               di_write_lock_parent(parent);
18891 +       }
18892 +
18893 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
18894 +       if (unlikely(err))
18895 +               goto out_parent;
18896 +
18897 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
18898 +       sz = -1;
18899 +       a->h_inode = d_inode(a->h_path.dentry);
18900 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
18901 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18902 +               if (ia->ia_size < i_size_read(a->h_inode))
18903 +                       sz = ia->ia_size;
18904 +               mutex_unlock(&a->h_inode->i_mutex);
18905 +       }
18906 +
18907 +       hi_wh = NULL;
18908 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
18909 +               hi_wh = au_hi_wh(inode, a->btgt);
18910 +               if (!hi_wh) {
18911 +                       struct au_cp_generic cpg = {
18912 +                               .dentry = dentry,
18913 +                               .bdst   = a->btgt,
18914 +                               .bsrc   = -1,
18915 +                               .len    = sz,
18916 +                               .pin    = &a->pin
18917 +                       };
18918 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
18919 +                       if (unlikely(err))
18920 +                               goto out_unlock;
18921 +                       hi_wh = au_hi_wh(inode, a->btgt);
18922 +                       /* todo: revalidate hi_wh? */
18923 +               }
18924 +       }
18925 +
18926 +       if (parent) {
18927 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
18928 +               di_downgrade_lock(parent, AuLock_IR);
18929 +               dput(parent);
18930 +               parent = NULL;
18931 +       }
18932 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
18933 +               goto out; /* success */
18934 +
18935 +       if (!d_unhashed(dentry)) {
18936 +               struct au_cp_generic cpg = {
18937 +                       .dentry = dentry,
18938 +                       .bdst   = a->btgt,
18939 +                       .bsrc   = bstart,
18940 +                       .len    = sz,
18941 +                       .pin    = &a->pin,
18942 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
18943 +               };
18944 +               err = au_sio_cpup_simple(&cpg);
18945 +               if (!err)
18946 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
18947 +       } else if (!hi_wh)
18948 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
18949 +       else
18950 +               a->h_path.dentry = hi_wh; /* do not dget here */
18951 +
18952 +out_unlock:
18953 +       a->h_inode = d_inode(a->h_path.dentry);
18954 +       if (!err)
18955 +               goto out; /* success */
18956 +       au_unpin(&a->pin);
18957 +out_parent:
18958 +       if (parent) {
18959 +               di_write_unlock(parent);
18960 +               dput(parent);
18961 +       }
18962 +out:
18963 +       if (!err)
18964 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
18965 +       return err;
18966 +}
18967 +
18968 +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
18969 +{
18970 +       int err;
18971 +       struct inode *inode, *delegated;
18972 +       struct super_block *sb;
18973 +       struct file *file;
18974 +       struct au_icpup_args *a;
18975 +
18976 +       inode = d_inode(dentry);
18977 +       IMustLock(inode);
18978 +
18979 +       err = -ENOMEM;
18980 +       a = kzalloc(sizeof(*a), GFP_NOFS);
18981 +       if (unlikely(!a))
18982 +               goto out;
18983 +
18984 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
18985 +               ia->ia_valid &= ~ATTR_MODE;
18986 +
18987 +       file = NULL;
18988 +       sb = dentry->d_sb;
18989 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18990 +       if (unlikely(err))
18991 +               goto out_kfree;
18992 +
18993 +       if (ia->ia_valid & ATTR_FILE) {
18994 +               /* currently ftruncate(2) only */
18995 +               AuDebugOn(!d_is_reg(dentry));
18996 +               file = ia->ia_file;
18997 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
18998 +               if (unlikely(err))
18999 +                       goto out_si;
19000 +               ia->ia_file = au_hf_top(file);
19001 +               a->udba = AuOpt_UDBA_NONE;
19002 +       } else {
19003 +               /* fchmod() doesn't pass ia_file */
19004 +               a->udba = au_opt_udba(sb);
19005 +               di_write_lock_child(dentry);
19006 +               /* no d_unlinked(), to set UDBA_NONE for root */
19007 +               if (d_unhashed(dentry))
19008 +                       a->udba = AuOpt_UDBA_NONE;
19009 +               if (a->udba != AuOpt_UDBA_NONE) {
19010 +                       AuDebugOn(IS_ROOT(dentry));
19011 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
19012 +                       if (unlikely(err))
19013 +                               goto out_dentry;
19014 +               }
19015 +       }
19016 +
19017 +       err = au_pin_and_icpup(dentry, ia, a);
19018 +       if (unlikely(err < 0))
19019 +               goto out_dentry;
19020 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
19021 +               ia->ia_file = NULL;
19022 +               ia->ia_valid &= ~ATTR_FILE;
19023 +       }
19024 +
19025 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19026 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19027 +           == (ATTR_MODE | ATTR_CTIME)) {
19028 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
19029 +               if (unlikely(err))
19030 +                       goto out_unlock;
19031 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19032 +                  && (ia->ia_valid & ATTR_CTIME)) {
19033 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
19034 +               if (unlikely(err))
19035 +                       goto out_unlock;
19036 +       }
19037 +
19038 +       if (ia->ia_valid & ATTR_SIZE) {
19039 +               struct file *f;
19040 +
19041 +               if (ia->ia_size < i_size_read(inode))
19042 +                       /* unmap only */
19043 +                       truncate_setsize(inode, ia->ia_size);
19044 +
19045 +               f = NULL;
19046 +               if (ia->ia_valid & ATTR_FILE)
19047 +                       f = ia->ia_file;
19048 +               mutex_unlock(&a->h_inode->i_mutex);
19049 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
19050 +               mutex_lock_nested(&a->h_inode->i_mutex, AuLsc_I_CHILD);
19051 +       } else {
19052 +               delegated = NULL;
19053 +               while (1) {
19054 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
19055 +                       if (delegated) {
19056 +                               err = break_deleg_wait(&delegated);
19057 +                               if (!err)
19058 +                                       continue;
19059 +                       }
19060 +                       break;
19061 +               }
19062 +       }
19063 +       if (!err)
19064 +               au_cpup_attr_changeable(inode);
19065 +
19066 +out_unlock:
19067 +       mutex_unlock(&a->h_inode->i_mutex);
19068 +       au_unpin(&a->pin);
19069 +       if (unlikely(err))
19070 +               au_update_dbstart(dentry);
19071 +out_dentry:
19072 +       di_write_unlock(dentry);
19073 +       if (file) {
19074 +               fi_write_unlock(file);
19075 +               ia->ia_file = file;
19076 +               ia->ia_valid |= ATTR_FILE;
19077 +       }
19078 +out_si:
19079 +       si_read_unlock(sb);
19080 +out_kfree:
19081 +       kfree(a);
19082 +out:
19083 +       AuTraceErr(err);
19084 +       return err;
19085 +}
19086 +
19087 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19088 +static int au_h_path_to_set_attr(struct dentry *dentry,
19089 +                                struct au_icpup_args *a, struct path *h_path)
19090 +{
19091 +       int err;
19092 +       struct super_block *sb;
19093 +
19094 +       sb = dentry->d_sb;
19095 +       a->udba = au_opt_udba(sb);
19096 +       /* no d_unlinked(), to set UDBA_NONE for root */
19097 +       if (d_unhashed(dentry))
19098 +               a->udba = AuOpt_UDBA_NONE;
19099 +       if (a->udba != AuOpt_UDBA_NONE) {
19100 +               AuDebugOn(IS_ROOT(dentry));
19101 +               err = au_reval_for_attr(dentry, au_sigen(sb));
19102 +               if (unlikely(err))
19103 +                       goto out;
19104 +       }
19105 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19106 +       if (unlikely(err < 0))
19107 +               goto out;
19108 +
19109 +       h_path->dentry = a->h_path.dentry;
19110 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
19111 +
19112 +out:
19113 +       return err;
19114 +}
19115 +
19116 +ssize_t au_srxattr(struct dentry *dentry, struct au_srxattr *arg)
19117 +{
19118 +       int err;
19119 +       struct path h_path;
19120 +       struct super_block *sb;
19121 +       struct au_icpup_args *a;
19122 +       struct inode *inode, *h_inode;
19123 +
19124 +       inode = d_inode(dentry);
19125 +       IMustLock(inode);
19126 +
19127 +       err = -ENOMEM;
19128 +       a = kzalloc(sizeof(*a), GFP_NOFS);
19129 +       if (unlikely(!a))
19130 +               goto out;
19131 +
19132 +       sb = dentry->d_sb;
19133 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19134 +       if (unlikely(err))
19135 +               goto out_kfree;
19136 +
19137 +       h_path.dentry = NULL;   /* silence gcc */
19138 +       di_write_lock_child(dentry);
19139 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
19140 +       if (unlikely(err))
19141 +               goto out_di;
19142 +
19143 +       mutex_unlock(&a->h_inode->i_mutex);
19144 +       switch (arg->type) {
19145 +       case AU_XATTR_SET:
19146 +               err = vfsub_setxattr(h_path.dentry,
19147 +                                    arg->u.set.name, arg->u.set.value,
19148 +                                    arg->u.set.size, arg->u.set.flags);
19149 +               break;
19150 +       case AU_XATTR_REMOVE:
19151 +               err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
19152 +               break;
19153 +       case AU_ACL_SET:
19154 +               err = -EOPNOTSUPP;
19155 +               h_inode = d_inode(h_path.dentry);
19156 +               if (h_inode->i_op->set_acl)
19157 +                       err = h_inode->i_op->set_acl(h_inode,
19158 +                                                    arg->u.acl_set.acl,
19159 +                                                    arg->u.acl_set.type);
19160 +               break;
19161 +       }
19162 +       if (!err)
19163 +               au_cpup_attr_timesizes(inode);
19164 +
19165 +       au_unpin(&a->pin);
19166 +       if (unlikely(err))
19167 +               au_update_dbstart(dentry);
19168 +
19169 +out_di:
19170 +       di_write_unlock(dentry);
19171 +       si_read_unlock(sb);
19172 +out_kfree:
19173 +       kfree(a);
19174 +out:
19175 +       AuTraceErr(err);
19176 +       return err;
19177 +}
19178 +#endif
19179 +
19180 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19181 +                            unsigned int nlink)
19182 +{
19183 +       unsigned int n;
19184 +
19185 +       inode->i_mode = st->mode;
19186 +       /* don't i_[ug]id_write() here */
19187 +       inode->i_uid = st->uid;
19188 +       inode->i_gid = st->gid;
19189 +       inode->i_atime = st->atime;
19190 +       inode->i_mtime = st->mtime;
19191 +       inode->i_ctime = st->ctime;
19192 +
19193 +       au_cpup_attr_nlink(inode, /*force*/0);
19194 +       if (S_ISDIR(inode->i_mode)) {
19195 +               n = inode->i_nlink;
19196 +               n -= nlink;
19197 +               n += st->nlink;
19198 +               smp_mb(); /* for i_nlink */
19199 +               /* 0 can happen */
19200 +               set_nlink(inode, n);
19201 +       }
19202 +
19203 +       spin_lock(&inode->i_lock);
19204 +       inode->i_blocks = st->blocks;
19205 +       i_size_write(inode, st->size);
19206 +       spin_unlock(&inode->i_lock);
19207 +}
19208 +
19209 +/*
19210 + * common routine for aufs_getattr() and aufs_getxattr().
19211 + * returns zero or negative (an error).
19212 + * @dentry will be read-locked in success.
19213 + */
19214 +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
19215 +{
19216 +       int err;
19217 +       unsigned int mnt_flags, sigen;
19218 +       unsigned char udba_none;
19219 +       aufs_bindex_t bindex;
19220 +       struct super_block *sb, *h_sb;
19221 +       struct inode *inode;
19222 +
19223 +       h_path->mnt = NULL;
19224 +       h_path->dentry = NULL;
19225 +
19226 +       err = 0;
19227 +       sb = dentry->d_sb;
19228 +       mnt_flags = au_mntflags(sb);
19229 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
19230 +
19231 +       /* support fstat(2) */
19232 +       if (!d_unlinked(dentry) && !udba_none) {
19233 +               sigen = au_sigen(sb);
19234 +               err = au_digen_test(dentry, sigen);
19235 +               if (!err) {
19236 +                       di_read_lock_child(dentry, AuLock_IR);
19237 +                       err = au_dbrange_test(dentry);
19238 +                       if (unlikely(err)) {
19239 +                               di_read_unlock(dentry, AuLock_IR);
19240 +                               goto out;
19241 +                       }
19242 +               } else {
19243 +                       AuDebugOn(IS_ROOT(dentry));
19244 +                       di_write_lock_child(dentry);
19245 +                       err = au_dbrange_test(dentry);
19246 +                       if (!err)
19247 +                               err = au_reval_for_attr(dentry, sigen);
19248 +                       if (!err)
19249 +                               di_downgrade_lock(dentry, AuLock_IR);
19250 +                       else {
19251 +                               di_write_unlock(dentry);
19252 +                               goto out;
19253 +                       }
19254 +               }
19255 +       } else
19256 +               di_read_lock_child(dentry, AuLock_IR);
19257 +
19258 +       inode = d_inode(dentry);
19259 +       bindex = au_ibstart(inode);
19260 +       h_path->mnt = au_sbr_mnt(sb, bindex);
19261 +       h_sb = h_path->mnt->mnt_sb;
19262 +       if (!force
19263 +           && !au_test_fs_bad_iattr(h_sb)
19264 +           && udba_none)
19265 +               goto out; /* success */
19266 +
19267 +       if (au_dbstart(dentry) == bindex)
19268 +               h_path->dentry = au_h_dptr(dentry, bindex);
19269 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
19270 +               h_path->dentry = au_plink_lkup(inode, bindex);
19271 +               if (IS_ERR(h_path->dentry))
19272 +                       /* pretending success */
19273 +                       h_path->dentry = NULL;
19274 +               else
19275 +                       dput(h_path->dentry);
19276 +       }
19277 +
19278 +out:
19279 +       return err;
19280 +}
19281 +
19282 +static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19283 +                       struct dentry *dentry, struct kstat *st)
19284 +{
19285 +       int err;
19286 +       unsigned char positive;
19287 +       struct path h_path;
19288 +       struct inode *inode;
19289 +       struct super_block *sb;
19290 +
19291 +       inode = d_inode(dentry);
19292 +       sb = dentry->d_sb;
19293 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19294 +       if (unlikely(err))
19295 +               goto out;
19296 +       err = au_h_path_getattr(dentry, /*force*/0, &h_path);
19297 +       if (unlikely(err))
19298 +               goto out_si;
19299 +       if (unlikely(!h_path.dentry))
19300 +               /* illegally overlapped or something */
19301 +               goto out_fill; /* pretending success */
19302 +
19303 +       positive = d_is_positive(h_path.dentry);
19304 +       if (positive)
19305 +               err = vfs_getattr(&h_path, st);
19306 +       if (!err) {
19307 +               if (positive)
19308 +                       au_refresh_iattr(inode, st,
19309 +                                        d_inode(h_path.dentry)->i_nlink);
19310 +               goto out_fill; /* success */
19311 +       }
19312 +       AuTraceErr(err);
19313 +       goto out_di;
19314 +
19315 +out_fill:
19316 +       generic_fillattr(inode, st);
19317 +out_di:
19318 +       di_read_unlock(dentry, AuLock_IR);
19319 +out_si:
19320 +       si_read_unlock(sb);
19321 +out:
19322 +       AuTraceErr(err);
19323 +       return err;
19324 +}
19325 +
19326 +/* ---------------------------------------------------------------------- */
19327 +
19328 +/*
19329 + * Assumption:
19330 + * - the number of symlinks is not so many.
19331 + *
19332 + * Structure:
19333 + * - sbinfo (instead of iinfo) contains an hlist of struct au_symlink.
19334 + *   If iinfo contained the hlist, then it would be rather large waste of memory
19335 + *   I am afraid.
19336 + * - struct au_symlink contains the necessary info for h_inode follow_link() and
19337 + *   put_link().
19338 + */
19339 +
19340 +struct au_symlink {
19341 +       union {
19342 +               struct hlist_node hlist;
19343 +               struct rcu_head rcu;
19344 +       };
19345 +
19346 +       struct inode *h_inode;
19347 +       void *h_cookie;
19348 +};
19349 +
19350 +static void au_symlink_add(struct super_block *sb, struct au_symlink *slink,
19351 +                          struct inode *h_inode, void *cookie)
19352 +{
19353 +       struct au_sbinfo *sbinfo;
19354 +
19355 +       ihold(h_inode);
19356 +       slink->h_inode = h_inode;
19357 +       slink->h_cookie = cookie;
19358 +       sbinfo = au_sbi(sb);
19359 +       au_sphl_add(&slink->hlist, &sbinfo->si_symlink);
19360 +}
19361 +
19362 +static void au_symlink_del(struct super_block *sb, struct au_symlink *slink)
19363 +{
19364 +       struct au_sbinfo *sbinfo;
19365 +
19366 +       /* do not iput() within rcu */
19367 +       iput(slink->h_inode);
19368 +       slink->h_inode = NULL;
19369 +       sbinfo = au_sbi(sb);
19370 +       au_sphl_del_rcu(&slink->hlist, &sbinfo->si_symlink);
19371 +       kfree_rcu(slink, rcu);
19372 +}
19373 +
19374 +static const char *aufs_follow_link(struct dentry *dentry, void **cookie)
19375 +{
19376 +       const char *ret;
19377 +       struct inode *inode, *h_inode;
19378 +       struct dentry *h_dentry;
19379 +       struct au_symlink *slink;
19380 +       int err;
19381 +       aufs_bindex_t bindex;
19382 +
19383 +       ret = NULL; /* supress a warning */
19384 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19385 +       if (unlikely(err))
19386 +               goto out;
19387 +
19388 +       err = au_d_hashed_positive(dentry);
19389 +       if (unlikely(err))
19390 +               goto out_unlock;
19391 +
19392 +       err = -EINVAL;
19393 +       inode = d_inode(dentry);
19394 +       bindex = au_ibstart(inode);
19395 +       h_inode = au_h_iptr(inode, bindex);
19396 +       if (unlikely(!h_inode->i_op->follow_link))
19397 +               goto out_unlock;
19398 +
19399 +       err = -ENOMEM;
19400 +       slink = kmalloc(sizeof(*slink), GFP_NOFS);
19401 +       if (unlikely(!slink))
19402 +               goto out_unlock;
19403 +
19404 +       err = -EBUSY;
19405 +       h_dentry = NULL;
19406 +       if (au_dbstart(dentry) <= bindex) {
19407 +               h_dentry = au_h_dptr(dentry, bindex);
19408 +               if (h_dentry)
19409 +                       dget(h_dentry);
19410 +       }
19411 +       if (!h_dentry) {
19412 +               h_dentry = d_find_any_alias(h_inode);
19413 +               if (IS_ERR(h_dentry)) {
19414 +                       err = PTR_ERR(h_dentry);
19415 +                       goto out_free;
19416 +               }
19417 +       }
19418 +       if (unlikely(!h_dentry))
19419 +               goto out_free;
19420 +
19421 +       err = 0;
19422 +       AuDbg("%pf\n", h_inode->i_op->follow_link);
19423 +       AuDbgDentry(h_dentry);
19424 +       ret = h_inode->i_op->follow_link(h_dentry, cookie);
19425 +       dput(h_dentry);
19426 +
19427 +       if (!IS_ERR_OR_NULL(ret)) {
19428 +               au_symlink_add(inode->i_sb, slink, h_inode, *cookie);
19429 +               *cookie = slink;
19430 +               AuDbg("slink %p\n", slink);
19431 +               goto out_unlock; /* success */
19432 +       }
19433 +
19434 +out_free:
19435 +       slink->h_inode = NULL;
19436 +       kfree_rcu(slink, rcu);
19437 +out_unlock:
19438 +       aufs_read_unlock(dentry, AuLock_IR);
19439 +out:
19440 +       if (unlikely(err))
19441 +               ret = ERR_PTR(err);
19442 +       AuTraceErrPtr(ret);
19443 +       return ret;
19444 +}
19445 +
19446 +static void aufs_put_link(struct inode *inode, void *cookie)
19447 +{
19448 +       struct au_symlink *slink;
19449 +       struct inode *h_inode;
19450 +
19451 +       slink = cookie;
19452 +       AuDbg("slink %p\n", slink);
19453 +       h_inode = slink->h_inode;
19454 +       AuDbg("%pf\n", h_inode->i_op->put_link);
19455 +       AuDbgInode(h_inode);
19456 +       if (h_inode->i_op->put_link)
19457 +               h_inode->i_op->put_link(h_inode, slink->h_cookie);
19458 +       au_symlink_del(inode->i_sb, slink);
19459 +}
19460 +
19461 +/* ---------------------------------------------------------------------- */
19462 +
19463 +static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
19464 +{
19465 +       int err;
19466 +       struct super_block *sb;
19467 +       struct inode *h_inode;
19468 +
19469 +       sb = inode->i_sb;
19470 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
19471 +       lockdep_off();
19472 +       si_read_lock(sb, AuLock_FLUSH);
19473 +       ii_write_lock_child(inode);
19474 +       lockdep_on();
19475 +       h_inode = au_h_iptr(inode, au_ibstart(inode));
19476 +       err = vfsub_update_time(h_inode, ts, flags);
19477 +       lockdep_off();
19478 +       if (!err)
19479 +               au_cpup_attr_timesizes(inode);
19480 +       ii_write_unlock(inode);
19481 +       si_read_unlock(sb);
19482 +       lockdep_on();
19483 +
19484 +       if (!err && (flags & S_VERSION))
19485 +               inode_inc_iversion(inode);
19486 +
19487 +       return err;
19488 +}
19489 +
19490 +/* ---------------------------------------------------------------------- */
19491 +
19492 +struct inode_operations aufs_symlink_iop = {
19493 +       .permission     = aufs_permission,
19494 +#ifdef CONFIG_FS_POSIX_ACL
19495 +       .get_acl        = aufs_get_acl,
19496 +       .set_acl        = aufs_set_acl, /* unsupport for symlink? */
19497 +#endif
19498 +
19499 +       .setattr        = aufs_setattr,
19500 +       .getattr        = aufs_getattr,
19501 +
19502 +#ifdef CONFIG_AUFS_XATTR
19503 +       .setxattr       = aufs_setxattr,
19504 +       .getxattr       = aufs_getxattr,
19505 +       .listxattr      = aufs_listxattr,
19506 +       .removexattr    = aufs_removexattr,
19507 +#endif
19508 +
19509 +       .readlink       = generic_readlink,
19510 +       .follow_link    = aufs_follow_link,
19511 +       .put_link       = aufs_put_link,
19512 +
19513 +       /* .update_time = aufs_update_time */
19514 +};
19515 +
19516 +struct inode_operations aufs_dir_iop = {
19517 +       .create         = aufs_create,
19518 +       .lookup         = aufs_lookup,
19519 +       .link           = aufs_link,
19520 +       .unlink         = aufs_unlink,
19521 +       .symlink        = aufs_symlink,
19522 +       .mkdir          = aufs_mkdir,
19523 +       .rmdir          = aufs_rmdir,
19524 +       .mknod          = aufs_mknod,
19525 +       .rename         = aufs_rename,
19526 +
19527 +       .permission     = aufs_permission,
19528 +#ifdef CONFIG_FS_POSIX_ACL
19529 +       .get_acl        = aufs_get_acl,
19530 +       .set_acl        = aufs_set_acl,
19531 +#endif
19532 +
19533 +       .setattr        = aufs_setattr,
19534 +       .getattr        = aufs_getattr,
19535 +
19536 +#ifdef CONFIG_AUFS_XATTR
19537 +       .setxattr       = aufs_setxattr,
19538 +       .getxattr       = aufs_getxattr,
19539 +       .listxattr      = aufs_listxattr,
19540 +       .removexattr    = aufs_removexattr,
19541 +#endif
19542 +
19543 +       .update_time    = aufs_update_time,
19544 +       .atomic_open    = aufs_atomic_open,
19545 +       .tmpfile        = aufs_tmpfile
19546 +};
19547 +
19548 +struct inode_operations aufs_iop = {
19549 +       .permission     = aufs_permission,
19550 +#ifdef CONFIG_FS_POSIX_ACL
19551 +       .get_acl        = aufs_get_acl,
19552 +       .set_acl        = aufs_set_acl,
19553 +#endif
19554 +
19555 +       .setattr        = aufs_setattr,
19556 +       .getattr        = aufs_getattr,
19557 +
19558 +#ifdef CONFIG_AUFS_XATTR
19559 +       .setxattr       = aufs_setxattr,
19560 +       .getxattr       = aufs_getxattr,
19561 +       .listxattr      = aufs_listxattr,
19562 +       .removexattr    = aufs_removexattr,
19563 +#endif
19564 +
19565 +       .update_time    = aufs_update_time
19566 +};
19567 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19568 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
19569 +++ linux/fs/aufs/i_op_del.c    2015-09-24 10:47:58.254719746 +0200
19570 @@ -0,0 +1,510 @@
19571 +/*
19572 + * Copyright (C) 2005-2015 Junjiro R. Okajima
19573 + *
19574 + * This program, aufs is free software; you can redistribute it and/or modify
19575 + * it under the terms of the GNU General Public License as published by
19576 + * the Free Software Foundation; either version 2 of the License, or
19577 + * (at your option) any later version.
19578 + *
19579 + * This program is distributed in the hope that it will be useful,
19580 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19581 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19582 + * GNU General Public License for more details.
19583 + *
19584 + * You should have received a copy of the GNU General Public License
19585 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19586 + */
19587 +
19588 +/*
19589 + * inode operations (del entry)
19590 + */
19591 +
19592 +#include "aufs.h"
19593 +
19594 +/*
19595 + * decide if a new whiteout for @dentry is necessary or not.
19596 + * when it is necessary, prepare the parent dir for the upper branch whose
19597 + * branch index is @bcpup for creation. the actual creation of the whiteout will
19598 + * be done by caller.
19599 + * return value:
19600 + * 0: wh is unnecessary
19601 + * plus: wh is necessary
19602 + * minus: error
19603 + */
19604 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
19605 +{
19606 +       int need_wh, err;
19607 +       aufs_bindex_t bstart;
19608 +       struct super_block *sb;
19609 +
19610 +       sb = dentry->d_sb;
19611 +       bstart = au_dbstart(dentry);
19612 +       if (*bcpup < 0) {
19613 +               *bcpup = bstart;
19614 +               if (au_test_ro(sb, bstart, d_inode(dentry))) {
19615 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
19616 +                       *bcpup = err;
19617 +                       if (unlikely(err < 0))
19618 +                               goto out;
19619 +               }
19620 +       } else
19621 +               AuDebugOn(bstart < *bcpup
19622 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
19623 +       AuDbg("bcpup %d, bstart %d\n", *bcpup, bstart);
19624 +
19625 +       if (*bcpup != bstart) {
19626 +               err = au_cpup_dirs(dentry, *bcpup);
19627 +               if (unlikely(err))
19628 +                       goto out;
19629 +               need_wh = 1;
19630 +       } else {
19631 +               struct au_dinfo *dinfo, *tmp;
19632 +
19633 +               need_wh = -ENOMEM;
19634 +               dinfo = au_di(dentry);
19635 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19636 +               if (tmp) {
19637 +                       au_di_cp(tmp, dinfo);
19638 +                       au_di_swap(tmp, dinfo);
19639 +                       /* returns the number of positive dentries */
19640 +                       need_wh = au_lkup_dentry(dentry, bstart + 1, /*type*/0);
19641 +                       au_di_swap(tmp, dinfo);
19642 +                       au_rw_write_unlock(&tmp->di_rwsem);
19643 +                       au_di_free(tmp);
19644 +               }
19645 +       }
19646 +       AuDbg("need_wh %d\n", need_wh);
19647 +       err = need_wh;
19648 +
19649 +out:
19650 +       return err;
19651 +}
19652 +
19653 +/*
19654 + * simple tests for the del-entry operations.
19655 + * following the checks in vfs, plus the parent-child relationship.
19656 + */
19657 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19658 +              struct dentry *h_parent, int isdir)
19659 +{
19660 +       int err;
19661 +       umode_t h_mode;
19662 +       struct dentry *h_dentry, *h_latest;
19663 +       struct inode *h_inode;
19664 +
19665 +       h_dentry = au_h_dptr(dentry, bindex);
19666 +       if (d_really_is_positive(dentry)) {
19667 +               err = -ENOENT;
19668 +               if (unlikely(d_is_negative(h_dentry)))
19669 +                       goto out;
19670 +               h_inode = d_inode(h_dentry);
19671 +               if (unlikely(!h_inode->i_nlink))
19672 +                       goto out;
19673 +
19674 +               h_mode = h_inode->i_mode;
19675 +               if (!isdir) {
19676 +                       err = -EISDIR;
19677 +                       if (unlikely(S_ISDIR(h_mode)))
19678 +                               goto out;
19679 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19680 +                       err = -ENOTDIR;
19681 +                       goto out;
19682 +               }
19683 +       } else {
19684 +               /* rename(2) case */
19685 +               err = -EIO;
19686 +               if (unlikely(d_is_positive(h_dentry)))
19687 +                       goto out;
19688 +       }
19689 +
19690 +       err = -ENOENT;
19691 +       /* expected parent dir is locked */
19692 +       if (unlikely(h_parent != h_dentry->d_parent))
19693 +               goto out;
19694 +       err = 0;
19695 +
19696 +       /*
19697 +        * rmdir a dir may break the consistency on some filesystem.
19698 +        * let's try heavy test.
19699 +        */
19700 +       err = -EACCES;
19701 +       if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
19702 +                    && au_test_h_perm(d_inode(h_parent),
19703 +                                      MAY_EXEC | MAY_WRITE)))
19704 +               goto out;
19705 +
19706 +       h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
19707 +       err = -EIO;
19708 +       if (IS_ERR(h_latest))
19709 +               goto out;
19710 +       if (h_latest == h_dentry)
19711 +               err = 0;
19712 +       dput(h_latest);
19713 +
19714 +out:
19715 +       return err;
19716 +}
19717 +
19718 +/*
19719 + * decide the branch where we operate for @dentry. the branch index will be set
19720 + * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
19721 + * dir for reverting.
19722 + * when a new whiteout is necessary, create it.
19723 + */
19724 +static struct dentry*
19725 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
19726 +                   struct au_dtime *dt, struct au_pin *pin)
19727 +{
19728 +       struct dentry *wh_dentry;
19729 +       struct super_block *sb;
19730 +       struct path h_path;
19731 +       int err, need_wh;
19732 +       unsigned int udba;
19733 +       aufs_bindex_t bcpup;
19734 +
19735 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
19736 +       wh_dentry = ERR_PTR(need_wh);
19737 +       if (unlikely(need_wh < 0))
19738 +               goto out;
19739 +
19740 +       sb = dentry->d_sb;
19741 +       udba = au_opt_udba(sb);
19742 +       bcpup = *rbcpup;
19743 +       err = au_pin(pin, dentry, bcpup, udba,
19744 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19745 +       wh_dentry = ERR_PTR(err);
19746 +       if (unlikely(err))
19747 +               goto out;
19748 +
19749 +       h_path.dentry = au_pinned_h_parent(pin);
19750 +       if (udba != AuOpt_UDBA_NONE
19751 +           && au_dbstart(dentry) == bcpup) {
19752 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
19753 +               wh_dentry = ERR_PTR(err);
19754 +               if (unlikely(err))
19755 +                       goto out_unpin;
19756 +       }
19757 +
19758 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
19759 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
19760 +       wh_dentry = NULL;
19761 +       if (!need_wh)
19762 +               goto out; /* success, no need to create whiteout */
19763 +
19764 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
19765 +       if (IS_ERR(wh_dentry))
19766 +               goto out_unpin;
19767 +
19768 +       /* returns with the parent is locked and wh_dentry is dget-ed */
19769 +       goto out; /* success */
19770 +
19771 +out_unpin:
19772 +       au_unpin(pin);
19773 +out:
19774 +       return wh_dentry;
19775 +}
19776 +
19777 +/*
19778 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
19779 + * in order to be revertible and save time for removing many child whiteouts
19780 + * under the dir.
19781 + * returns 1 when there are too many child whiteout and caller should remove
19782 + * them asynchronously. returns 0 when the number of children is enough small to
19783 + * remove now or the branch fs is a remote fs.
19784 + * otherwise return an error.
19785 + */
19786 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
19787 +                          struct au_nhash *whlist, struct inode *dir)
19788 +{
19789 +       int rmdir_later, err, dirwh;
19790 +       struct dentry *h_dentry;
19791 +       struct super_block *sb;
19792 +       struct inode *inode;
19793 +
19794 +       sb = dentry->d_sb;
19795 +       SiMustAnyLock(sb);
19796 +       h_dentry = au_h_dptr(dentry, bindex);
19797 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
19798 +       if (unlikely(err))
19799 +               goto out;
19800 +
19801 +       /* stop monitoring */
19802 +       inode = d_inode(dentry);
19803 +       au_hn_free(au_hi(inode, bindex));
19804 +
19805 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
19806 +               dirwh = au_sbi(sb)->si_dirwh;
19807 +               rmdir_later = (dirwh <= 1);
19808 +               if (!rmdir_later)
19809 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
19810 +                                                             dirwh);
19811 +               if (rmdir_later)
19812 +                       return rmdir_later;
19813 +       }
19814 +
19815 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
19816 +       if (unlikely(err)) {
19817 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
19818 +                       h_dentry, bindex, err);
19819 +               err = 0;
19820 +       }
19821 +
19822 +out:
19823 +       AuTraceErr(err);
19824 +       return err;
19825 +}
19826 +
19827 +/*
19828 + * final procedure for deleting a entry.
19829 + * maintain dentry and iattr.
19830 + */
19831 +static void epilog(struct inode *dir, struct dentry *dentry,
19832 +                  aufs_bindex_t bindex)
19833 +{
19834 +       struct inode *inode;
19835 +
19836 +       inode = d_inode(dentry);
19837 +       d_drop(dentry);
19838 +       inode->i_ctime = dir->i_ctime;
19839 +
19840 +       au_dir_ts(dir, bindex);
19841 +       dir->i_version++;
19842 +}
19843 +
19844 +/*
19845 + * when an error happened, remove the created whiteout and revert everything.
19846 + */
19847 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
19848 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
19849 +                    struct dentry *dentry, struct au_dtime *dt)
19850 +{
19851 +       int rerr;
19852 +       struct path h_path = {
19853 +               .dentry = wh_dentry,
19854 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
19855 +       };
19856 +
19857 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
19858 +       if (!rerr) {
19859 +               au_set_dbwh(dentry, bwh);
19860 +               au_dtime_revert(dt);
19861 +               return 0;
19862 +       }
19863 +
19864 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
19865 +       return -EIO;
19866 +}
19867 +
19868 +/* ---------------------------------------------------------------------- */
19869 +
19870 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
19871 +{
19872 +       int err;
19873 +       aufs_bindex_t bwh, bindex, bstart;
19874 +       struct inode *inode, *h_dir, *delegated;
19875 +       struct dentry *parent, *wh_dentry;
19876 +       /* to reuduce stack size */
19877 +       struct {
19878 +               struct au_dtime dt;
19879 +               struct au_pin pin;
19880 +               struct path h_path;
19881 +       } *a;
19882 +
19883 +       IMustLock(dir);
19884 +
19885 +       err = -ENOMEM;
19886 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19887 +       if (unlikely(!a))
19888 +               goto out;
19889 +
19890 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19891 +       if (unlikely(err))
19892 +               goto out_free;
19893 +       err = au_d_hashed_positive(dentry);
19894 +       if (unlikely(err))
19895 +               goto out_unlock;
19896 +       inode = d_inode(dentry);
19897 +       IMustLock(inode);
19898 +       err = -EISDIR;
19899 +       if (unlikely(d_is_dir(dentry)))
19900 +               goto out_unlock; /* possible? */
19901 +
19902 +       bstart = au_dbstart(dentry);
19903 +       bwh = au_dbwh(dentry);
19904 +       bindex = -1;
19905 +       parent = dentry->d_parent; /* dir inode is locked */
19906 +       di_write_lock_parent(parent);
19907 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
19908 +                                       &a->pin);
19909 +       err = PTR_ERR(wh_dentry);
19910 +       if (IS_ERR(wh_dentry))
19911 +               goto out_parent;
19912 +
19913 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, bstart);
19914 +       a->h_path.dentry = au_h_dptr(dentry, bstart);
19915 +       dget(a->h_path.dentry);
19916 +       if (bindex == bstart) {
19917 +               h_dir = au_pinned_h_dir(&a->pin);
19918 +               delegated = NULL;
19919 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
19920 +               if (unlikely(err == -EWOULDBLOCK)) {
19921 +                       pr_warn("cannot retry for NFSv4 delegation"
19922 +                               " for an internal unlink\n");
19923 +                       iput(delegated);
19924 +               }
19925 +       } else {
19926 +               /* dir inode is locked */
19927 +               h_dir = d_inode(wh_dentry->d_parent);
19928 +               IMustLock(h_dir);
19929 +               err = 0;
19930 +       }
19931 +
19932 +       if (!err) {
19933 +               vfsub_drop_nlink(inode);
19934 +               epilog(dir, dentry, bindex);
19935 +
19936 +               /* update target timestamps */
19937 +               if (bindex == bstart) {
19938 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
19939 +                       /*ignore*/
19940 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
19941 +               } else
19942 +                       /* todo: this timestamp may be reverted later */
19943 +                       inode->i_ctime = h_dir->i_ctime;
19944 +               goto out_unpin; /* success */
19945 +       }
19946 +
19947 +       /* revert */
19948 +       if (wh_dentry) {
19949 +               int rerr;
19950 +
19951 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
19952 +                                &a->dt);
19953 +               if (rerr)
19954 +                       err = rerr;
19955 +       }
19956 +
19957 +out_unpin:
19958 +       au_unpin(&a->pin);
19959 +       dput(wh_dentry);
19960 +       dput(a->h_path.dentry);
19961 +out_parent:
19962 +       di_write_unlock(parent);
19963 +out_unlock:
19964 +       aufs_read_unlock(dentry, AuLock_DW);
19965 +out_free:
19966 +       kfree(a);
19967 +out:
19968 +       return err;
19969 +}
19970 +
19971 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
19972 +{
19973 +       int err, rmdir_later;
19974 +       aufs_bindex_t bwh, bindex, bstart;
19975 +       struct inode *inode;
19976 +       struct dentry *parent, *wh_dentry, *h_dentry;
19977 +       struct au_whtmp_rmdir *args;
19978 +       /* to reuduce stack size */
19979 +       struct {
19980 +               struct au_dtime dt;
19981 +               struct au_pin pin;
19982 +       } *a;
19983 +
19984 +       IMustLock(dir);
19985 +
19986 +       err = -ENOMEM;
19987 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19988 +       if (unlikely(!a))
19989 +               goto out;
19990 +
19991 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
19992 +       if (unlikely(err))
19993 +               goto out_free;
19994 +       err = au_alive_dir(dentry);
19995 +       if (unlikely(err))
19996 +               goto out_unlock;
19997 +       inode = d_inode(dentry);
19998 +       IMustLock(inode);
19999 +       err = -ENOTDIR;
20000 +       if (unlikely(!d_is_dir(dentry)))
20001 +               goto out_unlock; /* possible? */
20002 +
20003 +       err = -ENOMEM;
20004 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20005 +       if (unlikely(!args))
20006 +               goto out_unlock;
20007 +
20008 +       parent = dentry->d_parent; /* dir inode is locked */
20009 +       di_write_lock_parent(parent);
20010 +       err = au_test_empty(dentry, &args->whlist);
20011 +       if (unlikely(err))
20012 +               goto out_parent;
20013 +
20014 +       bstart = au_dbstart(dentry);
20015 +       bwh = au_dbwh(dentry);
20016 +       bindex = -1;
20017 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20018 +                                       &a->pin);
20019 +       err = PTR_ERR(wh_dentry);
20020 +       if (IS_ERR(wh_dentry))
20021 +               goto out_parent;
20022 +
20023 +       h_dentry = au_h_dptr(dentry, bstart);
20024 +       dget(h_dentry);
20025 +       rmdir_later = 0;
20026 +       if (bindex == bstart) {
20027 +               err = renwh_and_rmdir(dentry, bstart, &args->whlist, dir);
20028 +               if (err > 0) {
20029 +                       rmdir_later = err;
20030 +                       err = 0;
20031 +               }
20032 +       } else {
20033 +               /* stop monitoring */
20034 +               au_hn_free(au_hi(inode, bstart));
20035 +
20036 +               /* dir inode is locked */
20037 +               IMustLock(d_inode(wh_dentry->d_parent));
20038 +               err = 0;
20039 +       }
20040 +
20041 +       if (!err) {
20042 +               vfsub_dead_dir(inode);
20043 +               au_set_dbdiropq(dentry, -1);
20044 +               epilog(dir, dentry, bindex);
20045 +
20046 +               if (rmdir_later) {
20047 +                       au_whtmp_kick_rmdir(dir, bstart, h_dentry, args);
20048 +                       args = NULL;
20049 +               }
20050 +
20051 +               goto out_unpin; /* success */
20052 +       }
20053 +
20054 +       /* revert */
20055 +       AuLabel(revert);
20056 +       if (wh_dentry) {
20057 +               int rerr;
20058 +
20059 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20060 +                                &a->dt);
20061 +               if (rerr)
20062 +                       err = rerr;
20063 +       }
20064 +
20065 +out_unpin:
20066 +       au_unpin(&a->pin);
20067 +       dput(wh_dentry);
20068 +       dput(h_dentry);
20069 +out_parent:
20070 +       di_write_unlock(parent);
20071 +       if (args)
20072 +               au_whtmp_rmdir_free(args);
20073 +out_unlock:
20074 +       aufs_read_unlock(dentry, AuLock_DW);
20075 +out_free:
20076 +       kfree(a);
20077 +out:
20078 +       AuTraceErr(err);
20079 +       return err;
20080 +}
20081 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20082 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
20083 +++ linux/fs/aufs/i_op_ren.c    2015-09-24 10:47:58.254719746 +0200
20084 @@ -0,0 +1,1017 @@
20085 +/*
20086 + * Copyright (C) 2005-2015 Junjiro R. Okajima
20087 + *
20088 + * This program, aufs is free software; you can redistribute it and/or modify
20089 + * it under the terms of the GNU General Public License as published by
20090 + * the Free Software Foundation; either version 2 of the License, or
20091 + * (at your option) any later version.
20092 + *
20093 + * This program is distributed in the hope that it will be useful,
20094 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20095 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20096 + * GNU General Public License for more details.
20097 + *
20098 + * You should have received a copy of the GNU General Public License
20099 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20100 + */
20101 +
20102 +/*
20103 + * inode operation (rename entry)
20104 + * todo: this is crazy monster
20105 + */
20106 +
20107 +#include "aufs.h"
20108 +
20109 +enum { AuSRC, AuDST, AuSrcDst };
20110 +enum { AuPARENT, AuCHILD, AuParentChild };
20111 +
20112 +#define AuRen_ISDIR    1
20113 +#define AuRen_ISSAMEDIR        (1 << 1)
20114 +#define AuRen_WHSRC    (1 << 2)
20115 +#define AuRen_WHDST    (1 << 3)
20116 +#define AuRen_MNT_WRITE        (1 << 4)
20117 +#define AuRen_DT_DSTDIR        (1 << 5)
20118 +#define AuRen_DIROPQ   (1 << 6)
20119 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
20120 +#define au_fset_ren(flags, name) \
20121 +       do { (flags) |= AuRen_##name; } while (0)
20122 +#define au_fclr_ren(flags, name) \
20123 +       do { (flags) &= ~AuRen_##name; } while (0)
20124 +
20125 +struct au_ren_args {
20126 +       struct {
20127 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
20128 +                       *wh_dentry;
20129 +               struct inode *dir, *inode;
20130 +               struct au_hinode *hdir;
20131 +               struct au_dtime dt[AuParentChild];
20132 +               aufs_bindex_t bstart;
20133 +       } sd[AuSrcDst];
20134 +
20135 +#define src_dentry     sd[AuSRC].dentry
20136 +#define src_dir                sd[AuSRC].dir
20137 +#define src_inode      sd[AuSRC].inode
20138 +#define src_h_dentry   sd[AuSRC].h_dentry
20139 +#define src_parent     sd[AuSRC].parent
20140 +#define src_h_parent   sd[AuSRC].h_parent
20141 +#define src_wh_dentry  sd[AuSRC].wh_dentry
20142 +#define src_hdir       sd[AuSRC].hdir
20143 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
20144 +#define src_dt         sd[AuSRC].dt
20145 +#define src_bstart     sd[AuSRC].bstart
20146 +
20147 +#define dst_dentry     sd[AuDST].dentry
20148 +#define dst_dir                sd[AuDST].dir
20149 +#define dst_inode      sd[AuDST].inode
20150 +#define dst_h_dentry   sd[AuDST].h_dentry
20151 +#define dst_parent     sd[AuDST].parent
20152 +#define dst_h_parent   sd[AuDST].h_parent
20153 +#define dst_wh_dentry  sd[AuDST].wh_dentry
20154 +#define dst_hdir       sd[AuDST].hdir
20155 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
20156 +#define dst_dt         sd[AuDST].dt
20157 +#define dst_bstart     sd[AuDST].bstart
20158 +
20159 +       struct dentry *h_trap;
20160 +       struct au_branch *br;
20161 +       struct au_hinode *src_hinode;
20162 +       struct path h_path;
20163 +       struct au_nhash whlist;
20164 +       aufs_bindex_t btgt, src_bwh, src_bdiropq;
20165 +
20166 +       unsigned int flags;
20167 +
20168 +       struct au_whtmp_rmdir *thargs;
20169 +       struct dentry *h_dst;
20170 +};
20171 +
20172 +/* ---------------------------------------------------------------------- */
20173 +
20174 +/*
20175 + * functions for reverting.
20176 + * when an error happened in a single rename systemcall, we should revert
20177 + * everything as if nothing happend.
20178 + * we don't need to revert the copied-up/down the parent dir since they are
20179 + * harmless.
20180 + */
20181 +
20182 +#define RevertFailure(fmt, ...) do { \
20183 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20184 +               ##__VA_ARGS__, err, rerr); \
20185 +       err = -EIO; \
20186 +} while (0)
20187 +
20188 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
20189 +{
20190 +       int rerr;
20191 +
20192 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20193 +       rerr = au_diropq_remove(a->src_dentry, a->btgt);
20194 +       au_hn_imtx_unlock(a->src_hinode);
20195 +       au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
20196 +       if (rerr)
20197 +               RevertFailure("remove diropq %pd", a->src_dentry);
20198 +}
20199 +
20200 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
20201 +{
20202 +       int rerr;
20203 +       struct inode *delegated;
20204 +
20205 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20206 +                                         a->src_h_parent);
20207 +       rerr = PTR_ERR(a->h_path.dentry);
20208 +       if (IS_ERR(a->h_path.dentry)) {
20209 +               RevertFailure("lkup one %pd", a->src_dentry);
20210 +               return;
20211 +       }
20212 +
20213 +       delegated = NULL;
20214 +       rerr = vfsub_rename(a->dst_h_dir,
20215 +                           au_h_dptr(a->src_dentry, a->btgt),
20216 +                           a->src_h_dir, &a->h_path, &delegated);
20217 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20218 +               pr_warn("cannot retry for NFSv4 delegation"
20219 +                       " for an internal rename\n");
20220 +               iput(delegated);
20221 +       }
20222 +       d_drop(a->h_path.dentry);
20223 +       dput(a->h_path.dentry);
20224 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20225 +       if (rerr)
20226 +               RevertFailure("rename %pd", a->src_dentry);
20227 +}
20228 +
20229 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
20230 +{
20231 +       int rerr;
20232 +       struct inode *delegated;
20233 +
20234 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20235 +                                         a->dst_h_parent);
20236 +       rerr = PTR_ERR(a->h_path.dentry);
20237 +       if (IS_ERR(a->h_path.dentry)) {
20238 +               RevertFailure("lkup one %pd", a->dst_dentry);
20239 +               return;
20240 +       }
20241 +       if (d_is_positive(a->h_path.dentry)) {
20242 +               d_drop(a->h_path.dentry);
20243 +               dput(a->h_path.dentry);
20244 +               return;
20245 +       }
20246 +
20247 +       delegated = NULL;
20248 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20249 +                           &delegated);
20250 +       if (unlikely(rerr == -EWOULDBLOCK)) {
20251 +               pr_warn("cannot retry for NFSv4 delegation"
20252 +                       " for an internal rename\n");
20253 +               iput(delegated);
20254 +       }
20255 +       d_drop(a->h_path.dentry);
20256 +       dput(a->h_path.dentry);
20257 +       if (!rerr)
20258 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20259 +       else
20260 +               RevertFailure("rename %pd", a->h_dst);
20261 +}
20262 +
20263 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20264 +{
20265 +       int rerr;
20266 +
20267 +       a->h_path.dentry = a->src_wh_dentry;
20268 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
20269 +       au_set_dbwh(a->src_dentry, a->src_bwh);
20270 +       if (rerr)
20271 +               RevertFailure("unlink %pd", a->src_wh_dentry);
20272 +}
20273 +#undef RevertFailure
20274 +
20275 +/* ---------------------------------------------------------------------- */
20276 +
20277 +/*
20278 + * when we have to copyup the renaming entry, do it with the rename-target name
20279 + * in order to minimize the cost (the later actual rename is unnecessary).
20280 + * otherwise rename it on the target branch.
20281 + */
20282 +static int au_ren_or_cpup(struct au_ren_args *a)
20283 +{
20284 +       int err;
20285 +       struct dentry *d;
20286 +       struct inode *delegated;
20287 +
20288 +       d = a->src_dentry;
20289 +       if (au_dbstart(d) == a->btgt) {
20290 +               a->h_path.dentry = a->dst_h_dentry;
20291 +               if (au_ftest_ren(a->flags, DIROPQ)
20292 +                   && au_dbdiropq(d) == a->btgt)
20293 +                       au_fclr_ren(a->flags, DIROPQ);
20294 +               AuDebugOn(au_dbstart(d) != a->btgt);
20295 +               delegated = NULL;
20296 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
20297 +                                  a->dst_h_dir, &a->h_path, &delegated);
20298 +               if (unlikely(err == -EWOULDBLOCK)) {
20299 +                       pr_warn("cannot retry for NFSv4 delegation"
20300 +                               " for an internal rename\n");
20301 +                       iput(delegated);
20302 +               }
20303 +       } else
20304 +               BUG();
20305 +
20306 +       if (!err && a->h_dst)
20307 +               /* it will be set to dinfo later */
20308 +               dget(a->h_dst);
20309 +
20310 +       return err;
20311 +}
20312 +
20313 +/* cf. aufs_rmdir() */
20314 +static int au_ren_del_whtmp(struct au_ren_args *a)
20315 +{
20316 +       int err;
20317 +       struct inode *dir;
20318 +
20319 +       dir = a->dst_dir;
20320 +       SiMustAnyLock(dir->i_sb);
20321 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20322 +                                    au_sbi(dir->i_sb)->si_dirwh)
20323 +           || au_test_fs_remote(a->h_dst->d_sb)) {
20324 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20325 +               if (unlikely(err))
20326 +                       pr_warn("failed removing whtmp dir %pd (%d), "
20327 +                               "ignored.\n", a->h_dst, err);
20328 +       } else {
20329 +               au_nhash_wh_free(&a->thargs->whlist);
20330 +               a->thargs->whlist = a->whlist;
20331 +               a->whlist.nh_num = 0;
20332 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20333 +               dput(a->h_dst);
20334 +               a->thargs = NULL;
20335 +       }
20336 +
20337 +       return 0;
20338 +}
20339 +
20340 +/* make it 'opaque' dir. */
20341 +static int au_ren_diropq(struct au_ren_args *a)
20342 +{
20343 +       int err;
20344 +       struct dentry *diropq;
20345 +
20346 +       err = 0;
20347 +       a->src_bdiropq = au_dbdiropq(a->src_dentry);
20348 +       a->src_hinode = au_hi(a->src_inode, a->btgt);
20349 +       au_hn_imtx_lock_nested(a->src_hinode, AuLsc_I_CHILD);
20350 +       diropq = au_diropq_create(a->src_dentry, a->btgt);
20351 +       au_hn_imtx_unlock(a->src_hinode);
20352 +       if (IS_ERR(diropq))
20353 +               err = PTR_ERR(diropq);
20354 +       else
20355 +               dput(diropq);
20356 +
20357 +       return err;
20358 +}
20359 +
20360 +static int do_rename(struct au_ren_args *a)
20361 +{
20362 +       int err;
20363 +       struct dentry *d, *h_d;
20364 +
20365 +       /* prepare workqueue args for asynchronous rmdir */
20366 +       h_d = a->dst_h_dentry;
20367 +       if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
20368 +               err = -ENOMEM;
20369 +               a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
20370 +               if (unlikely(!a->thargs))
20371 +                       goto out;
20372 +               a->h_dst = dget(h_d);
20373 +       }
20374 +
20375 +       /* create whiteout for src_dentry */
20376 +       if (au_ftest_ren(a->flags, WHSRC)) {
20377 +               a->src_bwh = au_dbwh(a->src_dentry);
20378 +               AuDebugOn(a->src_bwh >= 0);
20379 +               a->src_wh_dentry
20380 +                       = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
20381 +               err = PTR_ERR(a->src_wh_dentry);
20382 +               if (IS_ERR(a->src_wh_dentry))
20383 +                       goto out_thargs;
20384 +       }
20385 +
20386 +       /* lookup whiteout for dentry */
20387 +       if (au_ftest_ren(a->flags, WHDST)) {
20388 +               h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
20389 +                                a->br);
20390 +               err = PTR_ERR(h_d);
20391 +               if (IS_ERR(h_d))
20392 +                       goto out_whsrc;
20393 +               if (d_is_negative(h_d))
20394 +                       dput(h_d);
20395 +               else
20396 +                       a->dst_wh_dentry = h_d;
20397 +       }
20398 +
20399 +       /* rename dentry to tmpwh */
20400 +       if (a->thargs) {
20401 +               err = au_whtmp_ren(a->dst_h_dentry, a->br);
20402 +               if (unlikely(err))
20403 +                       goto out_whdst;
20404 +
20405 +               d = a->dst_dentry;
20406 +               au_set_h_dptr(d, a->btgt, NULL);
20407 +               err = au_lkup_neg(d, a->btgt, /*wh*/0);
20408 +               if (unlikely(err))
20409 +                       goto out_whtmp;
20410 +               a->dst_h_dentry = au_h_dptr(d, a->btgt);
20411 +       }
20412 +
20413 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_bstart != a->btgt);
20414 +
20415 +       /* rename by vfs_rename or cpup */
20416 +       d = a->dst_dentry;
20417 +       if (au_ftest_ren(a->flags, ISDIR)
20418 +           && (a->dst_wh_dentry
20419 +               || au_dbdiropq(d) == a->btgt
20420 +               /* hide the lower to keep xino */
20421 +               || a->btgt < au_dbend(d)
20422 +               || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
20423 +               au_fset_ren(a->flags, DIROPQ);
20424 +       err = au_ren_or_cpup(a);
20425 +       if (unlikely(err))
20426 +               /* leave the copied-up one */
20427 +               goto out_whtmp;
20428 +
20429 +       /* make dir opaque */
20430 +       if (au_ftest_ren(a->flags, DIROPQ)) {
20431 +               err = au_ren_diropq(a);
20432 +               if (unlikely(err))
20433 +                       goto out_rename;
20434 +       }
20435 +
20436 +       /* update target timestamps */
20437 +       AuDebugOn(au_dbstart(a->src_dentry) != a->btgt);
20438 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20439 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
20440 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
20441 +
20442 +       /* remove whiteout for dentry */
20443 +       if (a->dst_wh_dentry) {
20444 +               a->h_path.dentry = a->dst_wh_dentry;
20445 +               err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20446 +                                         a->dst_dentry);
20447 +               if (unlikely(err))
20448 +                       goto out_diropq;
20449 +       }
20450 +
20451 +       /* remove whtmp */
20452 +       if (a->thargs)
20453 +               au_ren_del_whtmp(a); /* ignore this error */
20454 +
20455 +       au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
20456 +       err = 0;
20457 +       goto out_success;
20458 +
20459 +out_diropq:
20460 +       if (au_ftest_ren(a->flags, DIROPQ))
20461 +               au_ren_rev_diropq(err, a);
20462 +out_rename:
20463 +       au_ren_rev_rename(err, a);
20464 +       dput(a->h_dst);
20465 +out_whtmp:
20466 +       if (a->thargs)
20467 +               au_ren_rev_whtmp(err, a);
20468 +out_whdst:
20469 +       dput(a->dst_wh_dentry);
20470 +       a->dst_wh_dentry = NULL;
20471 +out_whsrc:
20472 +       if (a->src_wh_dentry)
20473 +               au_ren_rev_whsrc(err, a);
20474 +out_success:
20475 +       dput(a->src_wh_dentry);
20476 +       dput(a->dst_wh_dentry);
20477 +out_thargs:
20478 +       if (a->thargs) {
20479 +               dput(a->h_dst);
20480 +               au_whtmp_rmdir_free(a->thargs);
20481 +               a->thargs = NULL;
20482 +       }
20483 +out:
20484 +       return err;
20485 +}
20486 +
20487 +/* ---------------------------------------------------------------------- */
20488 +
20489 +/*
20490 + * test if @dentry dir can be rename destination or not.
20491 + * success means, it is a logically empty dir.
20492 + */
20493 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
20494 +{
20495 +       return au_test_empty(dentry, whlist);
20496 +}
20497 +
20498 +/*
20499 + * test if @dentry dir can be rename source or not.
20500 + * if it can, return 0 and @children is filled.
20501 + * success means,
20502 + * - it is a logically empty dir.
20503 + * - or, it exists on writable branch and has no children including whiteouts
20504 + *       on the lower branch.
20505 + */
20506 +static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20507 +{
20508 +       int err;
20509 +       unsigned int rdhash;
20510 +       aufs_bindex_t bstart;
20511 +
20512 +       bstart = au_dbstart(dentry);
20513 +       if (bstart != btgt) {
20514 +               struct au_nhash whlist;
20515 +
20516 +               SiMustAnyLock(dentry->d_sb);
20517 +               rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20518 +               if (!rdhash)
20519 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20520 +                                                          dentry));
20521 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20522 +               if (unlikely(err))
20523 +                       goto out;
20524 +               err = au_test_empty(dentry, &whlist);
20525 +               au_nhash_wh_free(&whlist);
20526 +               goto out;
20527 +       }
20528 +
20529 +       if (bstart == au_dbtaildir(dentry))
20530 +               return 0; /* success */
20531 +
20532 +       err = au_test_empty_lower(dentry);
20533 +
20534 +out:
20535 +       if (err == -ENOTEMPTY) {
20536 +               AuWarn1("renaming dir who has child(ren) on multiple branches,"
20537 +                       " is not supported\n");
20538 +               err = -EXDEV;
20539 +       }
20540 +       return err;
20541 +}
20542 +
20543 +/* side effect: sets whlist and h_dentry */
20544 +static int au_ren_may_dir(struct au_ren_args *a)
20545 +{
20546 +       int err;
20547 +       unsigned int rdhash;
20548 +       struct dentry *d;
20549 +
20550 +       d = a->dst_dentry;
20551 +       SiMustAnyLock(d->d_sb);
20552 +
20553 +       err = 0;
20554 +       if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20555 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
20556 +               if (!rdhash)
20557 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20558 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20559 +               if (unlikely(err))
20560 +                       goto out;
20561 +
20562 +               au_set_dbstart(d, a->dst_bstart);
20563 +               err = may_rename_dstdir(d, &a->whlist);
20564 +               au_set_dbstart(d, a->btgt);
20565 +       }
20566 +       a->dst_h_dentry = au_h_dptr(d, au_dbstart(d));
20567 +       if (unlikely(err))
20568 +               goto out;
20569 +
20570 +       d = a->src_dentry;
20571 +       a->src_h_dentry = au_h_dptr(d, au_dbstart(d));
20572 +       if (au_ftest_ren(a->flags, ISDIR)) {
20573 +               err = may_rename_srcdir(d, a->btgt);
20574 +               if (unlikely(err)) {
20575 +                       au_nhash_wh_free(&a->whlist);
20576 +                       a->whlist.nh_num = 0;
20577 +               }
20578 +       }
20579 +out:
20580 +       return err;
20581 +}
20582 +
20583 +/* ---------------------------------------------------------------------- */
20584 +
20585 +/*
20586 + * simple tests for rename.
20587 + * following the checks in vfs, plus the parent-child relationship.
20588 + */
20589 +static int au_may_ren(struct au_ren_args *a)
20590 +{
20591 +       int err, isdir;
20592 +       struct inode *h_inode;
20593 +
20594 +       if (a->src_bstart == a->btgt) {
20595 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20596 +                                au_ftest_ren(a->flags, ISDIR));
20597 +               if (unlikely(err))
20598 +                       goto out;
20599 +               err = -EINVAL;
20600 +               if (unlikely(a->src_h_dentry == a->h_trap))
20601 +                       goto out;
20602 +       }
20603 +
20604 +       err = 0;
20605 +       if (a->dst_bstart != a->btgt)
20606 +               goto out;
20607 +
20608 +       err = -ENOTEMPTY;
20609 +       if (unlikely(a->dst_h_dentry == a->h_trap))
20610 +               goto out;
20611 +
20612 +       err = -EIO;
20613 +       isdir = !!au_ftest_ren(a->flags, ISDIR);
20614 +       if (d_really_is_negative(a->dst_dentry)) {
20615 +               if (d_is_negative(a->dst_h_dentry))
20616 +                       err = au_may_add(a->dst_dentry, a->btgt,
20617 +                                        a->dst_h_parent, isdir);
20618 +       } else {
20619 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
20620 +                       goto out;
20621 +               h_inode = d_inode(a->dst_h_dentry);
20622 +               if (h_inode->i_nlink)
20623 +                       err = au_may_del(a->dst_dentry, a->btgt,
20624 +                                        a->dst_h_parent, isdir);
20625 +       }
20626 +
20627 +out:
20628 +       if (unlikely(err == -ENOENT || err == -EEXIST))
20629 +               err = -EIO;
20630 +       AuTraceErr(err);
20631 +       return err;
20632 +}
20633 +
20634 +/* ---------------------------------------------------------------------- */
20635 +
20636 +/*
20637 + * locking order
20638 + * (VFS)
20639 + * - src_dir and dir by lock_rename()
20640 + * - inode if exitsts
20641 + * (aufs)
20642 + * - lock all
20643 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20644 + *     + si_read_lock
20645 + *     + di_write_lock2_child()
20646 + *       + di_write_lock_child()
20647 + *        + ii_write_lock_child()
20648 + *       + di_write_lock_child2()
20649 + *        + ii_write_lock_child2()
20650 + *     + src_parent and parent
20651 + *       + di_write_lock_parent()
20652 + *        + ii_write_lock_parent()
20653 + *       + di_write_lock_parent2()
20654 + *        + ii_write_lock_parent2()
20655 + *   + lower src_dir and dir by vfsub_lock_rename()
20656 + *   + verify the every relationships between child and parent. if any
20657 + *     of them failed, unlock all and return -EBUSY.
20658 + */
20659 +static void au_ren_unlock(struct au_ren_args *a)
20660 +{
20661 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20662 +                           a->dst_h_parent, a->dst_hdir);
20663 +       if (au_ftest_ren(a->flags, MNT_WRITE))
20664 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
20665 +}
20666 +
20667 +static int au_ren_lock(struct au_ren_args *a)
20668 +{
20669 +       int err;
20670 +       unsigned int udba;
20671 +
20672 +       err = 0;
20673 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20674 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
20675 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20676 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
20677 +
20678 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
20679 +       if (unlikely(err))
20680 +               goto out;
20681 +       au_fset_ren(a->flags, MNT_WRITE);
20682 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20683 +                                     a->dst_h_parent, a->dst_hdir);
20684 +       udba = au_opt_udba(a->src_dentry->d_sb);
20685 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
20686 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
20687 +               err = au_busy_or_stale();
20688 +       if (!err && au_dbstart(a->src_dentry) == a->btgt)
20689 +               err = au_h_verify(a->src_h_dentry, udba,
20690 +                                 d_inode(a->src_h_parent), a->src_h_parent,
20691 +                                 a->br);
20692 +       if (!err && au_dbstart(a->dst_dentry) == a->btgt)
20693 +               err = au_h_verify(a->dst_h_dentry, udba,
20694 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
20695 +                                 a->br);
20696 +       if (!err)
20697 +               goto out; /* success */
20698 +
20699 +       err = au_busy_or_stale();
20700 +       au_ren_unlock(a);
20701 +
20702 +out:
20703 +       return err;
20704 +}
20705 +
20706 +/* ---------------------------------------------------------------------- */
20707 +
20708 +static void au_ren_refresh_dir(struct au_ren_args *a)
20709 +{
20710 +       struct inode *dir;
20711 +
20712 +       dir = a->dst_dir;
20713 +       dir->i_version++;
20714 +       if (au_ftest_ren(a->flags, ISDIR)) {
20715 +               /* is this updating defined in POSIX? */
20716 +               au_cpup_attr_timesizes(a->src_inode);
20717 +               au_cpup_attr_nlink(dir, /*force*/1);
20718 +       }
20719 +
20720 +       au_dir_ts(dir, a->btgt);
20721 +
20722 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
20723 +               return;
20724 +
20725 +       dir = a->src_dir;
20726 +       dir->i_version++;
20727 +       if (au_ftest_ren(a->flags, ISDIR))
20728 +               au_cpup_attr_nlink(dir, /*force*/1);
20729 +       au_dir_ts(dir, a->btgt);
20730 +}
20731 +
20732 +static void au_ren_refresh(struct au_ren_args *a)
20733 +{
20734 +       aufs_bindex_t bend, bindex;
20735 +       struct dentry *d, *h_d;
20736 +       struct inode *i, *h_i;
20737 +       struct super_block *sb;
20738 +
20739 +       d = a->dst_dentry;
20740 +       d_drop(d);
20741 +       if (a->h_dst)
20742 +               /* already dget-ed by au_ren_or_cpup() */
20743 +               au_set_h_dptr(d, a->btgt, a->h_dst);
20744 +
20745 +       i = a->dst_inode;
20746 +       if (i) {
20747 +               if (!au_ftest_ren(a->flags, ISDIR))
20748 +                       vfsub_drop_nlink(i);
20749 +               else {
20750 +                       vfsub_dead_dir(i);
20751 +                       au_cpup_attr_timesizes(i);
20752 +               }
20753 +               au_update_dbrange(d, /*do_put_zero*/1);
20754 +       } else {
20755 +               bend = a->btgt;
20756 +               for (bindex = au_dbstart(d); bindex < bend; bindex++)
20757 +                       au_set_h_dptr(d, bindex, NULL);
20758 +               bend = au_dbend(d);
20759 +               for (bindex = a->btgt + 1; bindex <= bend; bindex++)
20760 +                       au_set_h_dptr(d, bindex, NULL);
20761 +               au_update_dbrange(d, /*do_put_zero*/0);
20762 +       }
20763 +
20764 +       d = a->src_dentry;
20765 +       au_set_dbwh(d, -1);
20766 +       bend = au_dbend(d);
20767 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20768 +               h_d = au_h_dptr(d, bindex);
20769 +               if (h_d)
20770 +                       au_set_h_dptr(d, bindex, NULL);
20771 +       }
20772 +       au_set_dbend(d, a->btgt);
20773 +
20774 +       sb = d->d_sb;
20775 +       i = a->src_inode;
20776 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
20777 +               return; /* success */
20778 +
20779 +       bend = au_ibend(i);
20780 +       for (bindex = a->btgt + 1; bindex <= bend; bindex++) {
20781 +               h_i = au_h_iptr(i, bindex);
20782 +               if (h_i) {
20783 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
20784 +                       /* ignore this error */
20785 +                       au_set_h_iptr(i, bindex, NULL, 0);
20786 +               }
20787 +       }
20788 +       au_set_ibend(i, a->btgt);
20789 +}
20790 +
20791 +/* ---------------------------------------------------------------------- */
20792 +
20793 +/* mainly for link(2) and rename(2) */
20794 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
20795 +{
20796 +       aufs_bindex_t bdiropq, bwh;
20797 +       struct dentry *parent;
20798 +       struct au_branch *br;
20799 +
20800 +       parent = dentry->d_parent;
20801 +       IMustLock(d_inode(parent)); /* dir is locked */
20802 +
20803 +       bdiropq = au_dbdiropq(parent);
20804 +       bwh = au_dbwh(dentry);
20805 +       br = au_sbr(dentry->d_sb, btgt);
20806 +       if (au_br_rdonly(br)
20807 +           || (0 <= bdiropq && bdiropq < btgt)
20808 +           || (0 <= bwh && bwh < btgt))
20809 +               btgt = -1;
20810 +
20811 +       AuDbg("btgt %d\n", btgt);
20812 +       return btgt;
20813 +}
20814 +
20815 +/* sets src_bstart, dst_bstart and btgt */
20816 +static int au_ren_wbr(struct au_ren_args *a)
20817 +{
20818 +       int err;
20819 +       struct au_wr_dir_args wr_dir_args = {
20820 +               /* .force_btgt  = -1, */
20821 +               .flags          = AuWrDir_ADD_ENTRY
20822 +       };
20823 +
20824 +       a->src_bstart = au_dbstart(a->src_dentry);
20825 +       a->dst_bstart = au_dbstart(a->dst_dentry);
20826 +       if (au_ftest_ren(a->flags, ISDIR))
20827 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
20828 +       wr_dir_args.force_btgt = a->src_bstart;
20829 +       if (a->dst_inode && a->dst_bstart < a->src_bstart)
20830 +               wr_dir_args.force_btgt = a->dst_bstart;
20831 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
20832 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
20833 +       a->btgt = err;
20834 +
20835 +       return err;
20836 +}
20837 +
20838 +static void au_ren_dt(struct au_ren_args *a)
20839 +{
20840 +       a->h_path.dentry = a->src_h_parent;
20841 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
20842 +       if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
20843 +               a->h_path.dentry = a->dst_h_parent;
20844 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
20845 +       }
20846 +
20847 +       au_fclr_ren(a->flags, DT_DSTDIR);
20848 +       if (!au_ftest_ren(a->flags, ISDIR))
20849 +               return;
20850 +
20851 +       a->h_path.dentry = a->src_h_dentry;
20852 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
20853 +       if (d_is_positive(a->dst_h_dentry)) {
20854 +               au_fset_ren(a->flags, DT_DSTDIR);
20855 +               a->h_path.dentry = a->dst_h_dentry;
20856 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
20857 +       }
20858 +}
20859 +
20860 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
20861 +{
20862 +       struct dentry *h_d;
20863 +       struct mutex *h_mtx;
20864 +
20865 +       au_dtime_revert(a->src_dt + AuPARENT);
20866 +       if (!au_ftest_ren(a->flags, ISSAMEDIR))
20867 +               au_dtime_revert(a->dst_dt + AuPARENT);
20868 +
20869 +       if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
20870 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
20871 +               h_mtx = &d_inode(h_d)->i_mutex;
20872 +               mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20873 +               au_dtime_revert(a->src_dt + AuCHILD);
20874 +               mutex_unlock(h_mtx);
20875 +
20876 +               if (au_ftest_ren(a->flags, DT_DSTDIR)) {
20877 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
20878 +                       h_mtx = &d_inode(h_d)->i_mutex;
20879 +                       mutex_lock_nested(h_mtx, AuLsc_I_CHILD);
20880 +                       au_dtime_revert(a->dst_dt + AuCHILD);
20881 +                       mutex_unlock(h_mtx);
20882 +               }
20883 +       }
20884 +}
20885 +
20886 +/* ---------------------------------------------------------------------- */
20887 +
20888 +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
20889 +               struct inode *_dst_dir, struct dentry *_dst_dentry)
20890 +{
20891 +       int err, flags;
20892 +       /* reduce stack space */
20893 +       struct au_ren_args *a;
20894 +
20895 +       AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
20896 +       IMustLock(_src_dir);
20897 +       IMustLock(_dst_dir);
20898 +
20899 +       err = -ENOMEM;
20900 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
20901 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20902 +       if (unlikely(!a))
20903 +               goto out;
20904 +
20905 +       a->src_dir = _src_dir;
20906 +       a->src_dentry = _src_dentry;
20907 +       a->src_inode = NULL;
20908 +       if (d_really_is_positive(a->src_dentry))
20909 +               a->src_inode = d_inode(a->src_dentry);
20910 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
20911 +       a->dst_dir = _dst_dir;
20912 +       a->dst_dentry = _dst_dentry;
20913 +       a->dst_inode = NULL;
20914 +       if (d_really_is_positive(a->dst_dentry))
20915 +               a->dst_inode = d_inode(a->dst_dentry);
20916 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
20917 +       if (a->dst_inode) {
20918 +               IMustLock(a->dst_inode);
20919 +               au_igrab(a->dst_inode);
20920 +       }
20921 +
20922 +       err = -ENOTDIR;
20923 +       flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
20924 +       if (d_is_dir(a->src_dentry)) {
20925 +               au_fset_ren(a->flags, ISDIR);
20926 +               if (unlikely(d_really_is_positive(a->dst_dentry)
20927 +                            && !d_is_dir(a->dst_dentry)))
20928 +                       goto out_free;
20929 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
20930 +                                               AuLock_DIR | flags);
20931 +       } else
20932 +               err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
20933 +                                               flags);
20934 +       if (unlikely(err))
20935 +               goto out_free;
20936 +
20937 +       err = au_d_hashed_positive(a->src_dentry);
20938 +       if (unlikely(err))
20939 +               goto out_unlock;
20940 +       err = -ENOENT;
20941 +       if (a->dst_inode) {
20942 +               /*
20943 +                * If it is a dir, VFS unhash dst_dentry before this
20944 +                * function. It means we cannot rely upon d_unhashed().
20945 +                */
20946 +               if (unlikely(!a->dst_inode->i_nlink))
20947 +                       goto out_unlock;
20948 +               if (!S_ISDIR(a->dst_inode->i_mode)) {
20949 +                       err = au_d_hashed_positive(a->dst_dentry);
20950 +                       if (unlikely(err))
20951 +                               goto out_unlock;
20952 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
20953 +                       goto out_unlock;
20954 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
20955 +               goto out_unlock;
20956 +
20957 +       /*
20958 +        * is it possible?
20959 +        * yes, it happend (in linux-3.3-rcN) but I don't know why.
20960 +        * there may exist a problem somewhere else.
20961 +        */
20962 +       err = -EINVAL;
20963 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
20964 +               goto out_unlock;
20965 +
20966 +       au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
20967 +       di_write_lock_parent(a->dst_parent);
20968 +
20969 +       /* which branch we process */
20970 +       err = au_ren_wbr(a);
20971 +       if (unlikely(err < 0))
20972 +               goto out_parent;
20973 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
20974 +       a->h_path.mnt = au_br_mnt(a->br);
20975 +
20976 +       /* are they available to be renamed */
20977 +       err = au_ren_may_dir(a);
20978 +       if (unlikely(err))
20979 +               goto out_children;
20980 +
20981 +       /* prepare the writable parent dir on the same branch */
20982 +       if (a->dst_bstart == a->btgt) {
20983 +               au_fset_ren(a->flags, WHDST);
20984 +       } else {
20985 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
20986 +               if (unlikely(err))
20987 +                       goto out_children;
20988 +       }
20989 +
20990 +       if (a->src_dir != a->dst_dir) {
20991 +               /*
20992 +                * this temporary unlock is safe,
20993 +                * because both dir->i_mutex are locked.
20994 +                */
20995 +               di_write_unlock(a->dst_parent);
20996 +               di_write_lock_parent(a->src_parent);
20997 +               err = au_wr_dir_need_wh(a->src_dentry,
20998 +                                       au_ftest_ren(a->flags, ISDIR),
20999 +                                       &a->btgt);
21000 +               di_write_unlock(a->src_parent);
21001 +               di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
21002 +               au_fclr_ren(a->flags, ISSAMEDIR);
21003 +       } else
21004 +               err = au_wr_dir_need_wh(a->src_dentry,
21005 +                                       au_ftest_ren(a->flags, ISDIR),
21006 +                                       &a->btgt);
21007 +       if (unlikely(err < 0))
21008 +               goto out_children;
21009 +       if (err)
21010 +               au_fset_ren(a->flags, WHSRC);
21011 +
21012 +       /* cpup src */
21013 +       if (a->src_bstart != a->btgt) {
21014 +               struct au_pin pin;
21015 +
21016 +               err = au_pin(&pin, a->src_dentry, a->btgt,
21017 +                            au_opt_udba(a->src_dentry->d_sb),
21018 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21019 +               if (!err) {
21020 +                       struct au_cp_generic cpg = {
21021 +                               .dentry = a->src_dentry,
21022 +                               .bdst   = a->btgt,
21023 +                               .bsrc   = a->src_bstart,
21024 +                               .len    = -1,
21025 +                               .pin    = &pin,
21026 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
21027 +                       };
21028 +                       AuDebugOn(au_dbstart(a->src_dentry) != a->src_bstart);
21029 +                       err = au_sio_cpup_simple(&cpg);
21030 +                       au_unpin(&pin);
21031 +               }
21032 +               if (unlikely(err))
21033 +                       goto out_children;
21034 +               a->src_bstart = a->btgt;
21035 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21036 +               au_fset_ren(a->flags, WHSRC);
21037 +       }
21038 +
21039 +       /* lock them all */
21040 +       err = au_ren_lock(a);
21041 +       if (unlikely(err))
21042 +               /* leave the copied-up one */
21043 +               goto out_children;
21044 +
21045 +       if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21046 +               err = au_may_ren(a);
21047 +       else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21048 +               err = -ENAMETOOLONG;
21049 +       if (unlikely(err))
21050 +               goto out_hdir;
21051 +
21052 +       /* store timestamps to be revertible */
21053 +       au_ren_dt(a);
21054 +
21055 +       /* here we go */
21056 +       err = do_rename(a);
21057 +       if (unlikely(err))
21058 +               goto out_dt;
21059 +
21060 +       /* update dir attributes */
21061 +       au_ren_refresh_dir(a);
21062 +
21063 +       /* dput/iput all lower dentries */
21064 +       au_ren_refresh(a);
21065 +
21066 +       goto out_hdir; /* success */
21067 +
21068 +out_dt:
21069 +       au_ren_rev_dt(err, a);
21070 +out_hdir:
21071 +       au_ren_unlock(a);
21072 +out_children:
21073 +       au_nhash_wh_free(&a->whlist);
21074 +       if (err && a->dst_inode && a->dst_bstart != a->btgt) {
21075 +               AuDbg("bstart %d, btgt %d\n", a->dst_bstart, a->btgt);
21076 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
21077 +               au_set_dbstart(a->dst_dentry, a->dst_bstart);
21078 +       }
21079 +out_parent:
21080 +       if (!err)
21081 +               d_move(a->src_dentry, a->dst_dentry);
21082 +       else {
21083 +               au_update_dbstart(a->dst_dentry);
21084 +               if (!a->dst_inode)
21085 +                       d_drop(a->dst_dentry);
21086 +       }
21087 +       if (au_ftest_ren(a->flags, ISSAMEDIR))
21088 +               di_write_unlock(a->dst_parent);
21089 +       else
21090 +               di_write_unlock2(a->src_parent, a->dst_parent);
21091 +out_unlock:
21092 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
21093 +out_free:
21094 +       iput(a->dst_inode);
21095 +       if (a->thargs)
21096 +               au_whtmp_rmdir_free(a->thargs);
21097 +       kfree(a);
21098 +out:
21099 +       AuTraceErr(err);
21100 +       return err;
21101 +}
21102 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21103 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
21104 +++ linux/fs/aufs/Kconfig       2015-09-24 10:47:58.248052907 +0200
21105 @@ -0,0 +1,185 @@
21106 +config AUFS_FS
21107 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
21108 +       help
21109 +       Aufs is a stackable unification filesystem such as Unionfs,
21110 +       which unifies several directories and provides a merged single
21111 +       directory.
21112 +       In the early days, aufs was entirely re-designed and
21113 +       re-implemented Unionfs Version 1.x series. Introducing many
21114 +       original ideas, approaches and improvements, it becomes totally
21115 +       different from Unionfs while keeping the basic features.
21116 +
21117 +if AUFS_FS
21118 +choice
21119 +       prompt "Maximum number of branches"
21120 +       default AUFS_BRANCH_MAX_127
21121 +       help
21122 +       Specifies the maximum number of branches (or member directories)
21123 +       in a single aufs. The larger value consumes more system
21124 +       resources and has a minor impact to performance.
21125 +config AUFS_BRANCH_MAX_127
21126 +       bool "127"
21127 +       help
21128 +       Specifies the maximum number of branches (or member directories)
21129 +       in a single aufs. The larger value consumes more system
21130 +       resources and has a minor impact to performance.
21131 +config AUFS_BRANCH_MAX_511
21132 +       bool "511"
21133 +       help
21134 +       Specifies the maximum number of branches (or member directories)
21135 +       in a single aufs. The larger value consumes more system
21136 +       resources and has a minor impact to performance.
21137 +config AUFS_BRANCH_MAX_1023
21138 +       bool "1023"
21139 +       help
21140 +       Specifies the maximum number of branches (or member directories)
21141 +       in a single aufs. The larger value consumes more system
21142 +       resources and has a minor impact to performance.
21143 +config AUFS_BRANCH_MAX_32767
21144 +       bool "32767"
21145 +       help
21146 +       Specifies the maximum number of branches (or member directories)
21147 +       in a single aufs. The larger value consumes more system
21148 +       resources and has a minor impact to performance.
21149 +endchoice
21150 +
21151 +config AUFS_SBILIST
21152 +       bool
21153 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
21154 +       default y
21155 +       help
21156 +       Automatic configuration for internal use.
21157 +       When aufs supports Magic SysRq or /proc, enabled automatically.
21158 +
21159 +config AUFS_HNOTIFY
21160 +       bool "Detect direct branch access (bypassing aufs)"
21161 +       help
21162 +       If you want to modify files on branches directly, eg. bypassing aufs,
21163 +       and want aufs to detect the changes of them fully, then enable this
21164 +       option and use 'udba=notify' mount option.
21165 +       Currently there is only one available configuration, "fsnotify".
21166 +       It will have a negative impact to the performance.
21167 +       See detail in aufs.5.
21168 +
21169 +choice
21170 +       prompt "method" if AUFS_HNOTIFY
21171 +       default AUFS_HFSNOTIFY
21172 +config AUFS_HFSNOTIFY
21173 +       bool "fsnotify"
21174 +       select FSNOTIFY
21175 +endchoice
21176 +
21177 +config AUFS_EXPORT
21178 +       bool "NFS-exportable aufs"
21179 +       depends on EXPORTFS
21180 +       help
21181 +       If you want to export your mounted aufs via NFS, then enable this
21182 +       option. There are several requirements for this configuration.
21183 +       See detail in aufs.5.
21184 +
21185 +config AUFS_INO_T_64
21186 +       bool
21187 +       depends on AUFS_EXPORT
21188 +       depends on 64BIT && !(ALPHA || S390)
21189 +       default y
21190 +       help
21191 +       Automatic configuration for internal use.
21192 +       /* typedef unsigned long/int __kernel_ino_t */
21193 +       /* alpha and s390x are int */
21194 +
21195 +config AUFS_XATTR
21196 +       bool "support for XATTR/EA (including Security Labels)"
21197 +       help
21198 +       If your branch fs supports XATTR/EA and you want to make them
21199 +       available in aufs too, then enable this opsion and specify the
21200 +       branch attributes for EA.
21201 +       See detail in aufs.5.
21202 +
21203 +config AUFS_FHSM
21204 +       bool "File-based Hierarchical Storage Management"
21205 +       help
21206 +       Hierarchical Storage Management (or HSM) is a well-known feature
21207 +       in the storage world. Aufs provides this feature as file-based.
21208 +       with multiple branches.
21209 +       These multiple branches are prioritized, ie. the topmost one
21210 +       should be the fastest drive and be used heavily.
21211 +
21212 +config AUFS_RDU
21213 +       bool "Readdir in userspace"
21214 +       help
21215 +       Aufs has two methods to provide a merged view for a directory,
21216 +       by a user-space library and by kernel-space natively. The latter
21217 +       is always enabled but sometimes large and slow.
21218 +       If you enable this option, install the library in aufs2-util
21219 +       package, and set some environment variables for your readdir(3),
21220 +       then the work will be handled in user-space which generally
21221 +       shows better performance in most cases.
21222 +       See detail in aufs.5.
21223 +
21224 +config AUFS_SHWH
21225 +       bool "Show whiteouts"
21226 +       help
21227 +       If you want to make the whiteouts in aufs visible, then enable
21228 +       this option and specify 'shwh' mount option. Although it may
21229 +       sounds like philosophy or something, but in technically it
21230 +       simply shows the name of whiteout with keeping its behaviour.
21231 +
21232 +config AUFS_BR_RAMFS
21233 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
21234 +       help
21235 +       If you want to use ramfs as an aufs branch fs, then enable this
21236 +       option. Generally tmpfs is recommended.
21237 +       Aufs prohibited them to be a branch fs by default, because
21238 +       initramfs becomes unusable after switch_root or something
21239 +       generally. If you sets initramfs as an aufs branch and boot your
21240 +       system by switch_root, you will meet a problem easily since the
21241 +       files in initramfs may be inaccessible.
21242 +       Unless you are going to use ramfs as an aufs branch fs without
21243 +       switch_root or something, leave it N.
21244 +
21245 +config AUFS_BR_FUSE
21246 +       bool "Fuse fs as an aufs branch"
21247 +       depends on FUSE_FS
21248 +       select AUFS_POLL
21249 +       help
21250 +       If you want to use fuse-based userspace filesystem as an aufs
21251 +       branch fs, then enable this option.
21252 +       It implements the internal poll(2) operation which is
21253 +       implemented by fuse only (curretnly).
21254 +
21255 +config AUFS_POLL
21256 +       bool
21257 +       help
21258 +       Automatic configuration for internal use.
21259 +
21260 +config AUFS_BR_HFSPLUS
21261 +       bool "Hfsplus as an aufs branch"
21262 +       depends on HFSPLUS_FS
21263 +       default y
21264 +       help
21265 +       If you want to use hfsplus fs as an aufs branch fs, then enable
21266 +       this option. This option introduces a small overhead at
21267 +       copying-up a file on hfsplus.
21268 +
21269 +config AUFS_BDEV_LOOP
21270 +       bool
21271 +       depends on BLK_DEV_LOOP
21272 +       default y
21273 +       help
21274 +       Automatic configuration for internal use.
21275 +       Convert =[ym] into =y.
21276 +
21277 +config AUFS_DEBUG
21278 +       bool "Debug aufs"
21279 +       help
21280 +       Enable this to compile aufs internal debug code.
21281 +       It will have a negative impact to the performance.
21282 +
21283 +config AUFS_MAGIC_SYSRQ
21284 +       bool
21285 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
21286 +       default y
21287 +       help
21288 +       Automatic configuration for internal use.
21289 +       When aufs supports Magic SysRq, enabled automatically.
21290 +endif
21291 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21292 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
21293 +++ linux/fs/aufs/loop.c        2015-09-24 10:47:58.254719746 +0200
21294 @@ -0,0 +1,145 @@
21295 +/*
21296 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21297 + *
21298 + * This program, aufs is free software; you can redistribute it and/or modify
21299 + * it under the terms of the GNU General Public License as published by
21300 + * the Free Software Foundation; either version 2 of the License, or
21301 + * (at your option) any later version.
21302 + *
21303 + * This program is distributed in the hope that it will be useful,
21304 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21305 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21306 + * GNU General Public License for more details.
21307 + *
21308 + * You should have received a copy of the GNU General Public License
21309 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21310 + */
21311 +
21312 +/*
21313 + * support for loopback block device as a branch
21314 + */
21315 +
21316 +#include "aufs.h"
21317 +
21318 +/* added into drivers/block/loop.c */
21319 +static struct file *(*backing_file_func)(struct super_block *sb);
21320 +
21321 +/*
21322 + * test if two lower dentries have overlapping branches.
21323 + */
21324 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
21325 +{
21326 +       struct super_block *h_sb;
21327 +       struct file *backing_file;
21328 +
21329 +       if (unlikely(!backing_file_func)) {
21330 +               /* don't load "loop" module here */
21331 +               backing_file_func = symbol_get(loop_backing_file);
21332 +               if (unlikely(!backing_file_func))
21333 +                       /* "loop" module is not loaded */
21334 +                       return 0;
21335 +       }
21336 +
21337 +       h_sb = h_adding->d_sb;
21338 +       backing_file = backing_file_func(h_sb);
21339 +       if (!backing_file)
21340 +               return 0;
21341 +
21342 +       h_adding = backing_file->f_path.dentry;
21343 +       /*
21344 +        * h_adding can be local NFS.
21345 +        * in this case aufs cannot detect the loop.
21346 +        */
21347 +       if (unlikely(h_adding->d_sb == sb))
21348 +               return 1;
21349 +       return !!au_test_subdir(h_adding, sb->s_root);
21350 +}
21351 +
21352 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21353 +int au_test_loopback_kthread(void)
21354 +{
21355 +       int ret;
21356 +       struct task_struct *tsk = current;
21357 +       char c, comm[sizeof(tsk->comm)];
21358 +
21359 +       ret = 0;
21360 +       if (tsk->flags & PF_KTHREAD) {
21361 +               get_task_comm(comm, tsk);
21362 +               c = comm[4];
21363 +               ret = ('0' <= c && c <= '9'
21364 +                      && !strncmp(comm, "loop", 4));
21365 +       }
21366 +
21367 +       return ret;
21368 +}
21369 +
21370 +/* ---------------------------------------------------------------------- */
21371 +
21372 +#define au_warn_loopback_step  16
21373 +static int au_warn_loopback_nelem = au_warn_loopback_step;
21374 +static unsigned long *au_warn_loopback_array;
21375 +
21376 +void au_warn_loopback(struct super_block *h_sb)
21377 +{
21378 +       int i, new_nelem;
21379 +       unsigned long *a, magic;
21380 +       static DEFINE_SPINLOCK(spin);
21381 +
21382 +       magic = h_sb->s_magic;
21383 +       spin_lock(&spin);
21384 +       a = au_warn_loopback_array;
21385 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21386 +               if (a[i] == magic) {
21387 +                       spin_unlock(&spin);
21388 +                       return;
21389 +               }
21390 +
21391 +       /* h_sb is new to us, print it */
21392 +       if (i < au_warn_loopback_nelem) {
21393 +               a[i] = magic;
21394 +               goto pr;
21395 +       }
21396 +
21397 +       /* expand the array */
21398 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21399 +       a = au_kzrealloc(au_warn_loopback_array,
21400 +                        au_warn_loopback_nelem * sizeof(unsigned long),
21401 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC);
21402 +       if (a) {
21403 +               au_warn_loopback_nelem = new_nelem;
21404 +               au_warn_loopback_array = a;
21405 +               a[i] = magic;
21406 +               goto pr;
21407 +       }
21408 +
21409 +       spin_unlock(&spin);
21410 +       AuWarn1("realloc failed, ignored\n");
21411 +       return;
21412 +
21413 +pr:
21414 +       spin_unlock(&spin);
21415 +       pr_warn("you may want to try another patch for loopback file "
21416 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
21417 +}
21418 +
21419 +int au_loopback_init(void)
21420 +{
21421 +       int err;
21422 +       struct super_block *sb __maybe_unused;
21423 +
21424 +       AuDebugOn(sizeof(sb->s_magic) != sizeof(unsigned long));
21425 +
21426 +       err = 0;
21427 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21428 +                                        sizeof(unsigned long), GFP_NOFS);
21429 +       if (unlikely(!au_warn_loopback_array))
21430 +               err = -ENOMEM;
21431 +
21432 +       return err;
21433 +}
21434 +
21435 +void au_loopback_fin(void)
21436 +{
21437 +       symbol_put(loop_backing_file);
21438 +       kfree(au_warn_loopback_array);
21439 +}
21440 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21441 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
21442 +++ linux/fs/aufs/loop.h        2015-09-24 10:47:58.254719746 +0200
21443 @@ -0,0 +1,52 @@
21444 +/*
21445 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21446 + *
21447 + * This program, aufs is free software; you can redistribute it and/or modify
21448 + * it under the terms of the GNU General Public License as published by
21449 + * the Free Software Foundation; either version 2 of the License, or
21450 + * (at your option) any later version.
21451 + *
21452 + * This program is distributed in the hope that it will be useful,
21453 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21454 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21455 + * GNU General Public License for more details.
21456 + *
21457 + * You should have received a copy of the GNU General Public License
21458 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21459 + */
21460 +
21461 +/*
21462 + * support for loopback mount as a branch
21463 + */
21464 +
21465 +#ifndef __AUFS_LOOP_H__
21466 +#define __AUFS_LOOP_H__
21467 +
21468 +#ifdef __KERNEL__
21469 +
21470 +struct dentry;
21471 +struct super_block;
21472 +
21473 +#ifdef CONFIG_AUFS_BDEV_LOOP
21474 +/* drivers/block/loop.c */
21475 +struct file *loop_backing_file(struct super_block *sb);
21476 +
21477 +/* loop.c */
21478 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
21479 +int au_test_loopback_kthread(void);
21480 +void au_warn_loopback(struct super_block *h_sb);
21481 +
21482 +int au_loopback_init(void);
21483 +void au_loopback_fin(void);
21484 +#else
21485 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
21486 +          struct dentry *h_adding)
21487 +AuStubInt0(au_test_loopback_kthread, void)
21488 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21489 +
21490 +AuStubInt0(au_loopback_init, void)
21491 +AuStubVoid(au_loopback_fin, void)
21492 +#endif /* BLK_DEV_LOOP */
21493 +
21494 +#endif /* __KERNEL__ */
21495 +#endif /* __AUFS_LOOP_H__ */
21496 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21497 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
21498 +++ linux/fs/aufs/magic.mk      2015-09-24 10:47:58.254719746 +0200
21499 @@ -0,0 +1,30 @@
21500 +
21501 +# defined in ${srctree}/fs/fuse/inode.c
21502 +# tristate
21503 +ifdef CONFIG_FUSE_FS
21504 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21505 +endif
21506 +
21507 +# defined in ${srctree}/fs/xfs/xfs_sb.h
21508 +# tristate
21509 +ifdef CONFIG_XFS_FS
21510 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
21511 +endif
21512 +
21513 +# defined in ${srctree}/fs/configfs/mount.c
21514 +# tristate
21515 +ifdef CONFIG_CONFIGFS_FS
21516 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21517 +endif
21518 +
21519 +# defined in ${srctree}/fs/ubifs/ubifs.h
21520 +# tristate
21521 +ifdef CONFIG_UBIFS_FS
21522 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21523 +endif
21524 +
21525 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21526 +# tristate
21527 +ifdef CONFIG_HFSPLUS_FS
21528 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21529 +endif
21530 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21531 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
21532 +++ linux/fs/aufs/Makefile      2015-09-24 10:47:58.248052907 +0200
21533 @@ -0,0 +1,44 @@
21534 +
21535 +include ${src}/magic.mk
21536 +ifeq (${CONFIG_AUFS_FS},m)
21537 +include ${src}/conf.mk
21538 +endif
21539 +-include ${src}/priv_def.mk
21540 +
21541 +# cf. include/linux/kernel.h
21542 +# enable pr_debug
21543 +ccflags-y += -DDEBUG
21544 +# sparse requires the full pathname
21545 +ifdef M
21546 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
21547 +else
21548 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
21549 +endif
21550 +
21551 +obj-$(CONFIG_AUFS_FS) += aufs.o
21552 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21553 +       wkq.o vfsub.o dcsub.o \
21554 +       cpup.o whout.o wbr_policy.o \
21555 +       dinfo.o dentry.o \
21556 +       dynop.o \
21557 +       finfo.o file.o f_op.o \
21558 +       dir.o vdir.o \
21559 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
21560 +       mvdown.o ioctl.o
21561 +
21562 +# all are boolean
21563 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
21564 +aufs-$(CONFIG_SYSFS) += sysfs.o
21565 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21566 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21567 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21568 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
21569 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
21570 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21571 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
21572 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
21573 +aufs-$(CONFIG_AUFS_POLL) += poll.o
21574 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
21575 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21576 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21577 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
21578 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21579 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
21580 +++ linux/fs/aufs/module.c      2015-09-24 10:47:58.254719746 +0200
21581 @@ -0,0 +1,210 @@
21582 +/*
21583 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21584 + *
21585 + * This program, aufs is free software; you can redistribute it and/or modify
21586 + * it under the terms of the GNU General Public License as published by
21587 + * the Free Software Foundation; either version 2 of the License, or
21588 + * (at your option) any later version.
21589 + *
21590 + * This program is distributed in the hope that it will be useful,
21591 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21592 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21593 + * GNU General Public License for more details.
21594 + *
21595 + * You should have received a copy of the GNU General Public License
21596 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21597 + */
21598 +
21599 +/*
21600 + * module global variables and operations
21601 + */
21602 +
21603 +#include <linux/module.h>
21604 +#include <linux/seq_file.h>
21605 +#include "aufs.h"
21606 +
21607 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21608 +{
21609 +       if (new_sz <= nused)
21610 +               return p;
21611 +
21612 +       p = krealloc(p, new_sz, gfp);
21613 +       if (p)
21614 +               memset(p + nused, 0, new_sz - nused);
21615 +       return p;
21616 +}
21617 +
21618 +/* ---------------------------------------------------------------------- */
21619 +
21620 +/*
21621 + * aufs caches
21622 + */
21623 +struct kmem_cache *au_cachep[AuCache_Last];
21624 +static int __init au_cache_init(void)
21625 +{
21626 +       au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
21627 +       if (au_cachep[AuCache_DINFO])
21628 +               /* SLAB_DESTROY_BY_RCU */
21629 +               au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
21630 +                                                       au_icntnr_init_once);
21631 +       if (au_cachep[AuCache_ICNTNR])
21632 +               au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
21633 +                                                      au_fi_init_once);
21634 +       if (au_cachep[AuCache_FINFO])
21635 +               au_cachep[AuCache_VDIR] = AuCache(au_vdir);
21636 +       if (au_cachep[AuCache_VDIR])
21637 +               au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
21638 +       if (au_cachep[AuCache_DEHSTR])
21639 +               return 0;
21640 +
21641 +       return -ENOMEM;
21642 +}
21643 +
21644 +static void au_cache_fin(void)
21645 +{
21646 +       int i;
21647 +
21648 +       /*
21649 +        * Make sure all delayed rcu free inodes are flushed before we
21650 +        * destroy cache.
21651 +        */
21652 +       rcu_barrier();
21653 +
21654 +       /* excluding AuCache_HNOTIFY */
21655 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
21656 +       for (i = 0; i < AuCache_HNOTIFY; i++)
21657 +               if (au_cachep[i]) {
21658 +                       kmem_cache_destroy(au_cachep[i]);
21659 +                       au_cachep[i] = NULL;
21660 +               }
21661 +}
21662 +
21663 +/* ---------------------------------------------------------------------- */
21664 +
21665 +int au_dir_roflags;
21666 +
21667 +#ifdef CONFIG_AUFS_SBILIST
21668 +/*
21669 + * iterate_supers_type() doesn't protect us from
21670 + * remounting (branch management)
21671 + */
21672 +struct au_splhead au_sbilist;
21673 +#endif
21674 +
21675 +struct lock_class_key au_lc_key[AuLcKey_Last];
21676 +
21677 +/*
21678 + * functions for module interface.
21679 + */
21680 +MODULE_LICENSE("GPL");
21681 +/* MODULE_LICENSE("GPL v2"); */
21682 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
21683 +MODULE_DESCRIPTION(AUFS_NAME
21684 +       " -- Advanced multi layered unification filesystem");
21685 +MODULE_VERSION(AUFS_VERSION);
21686 +MODULE_ALIAS_FS(AUFS_NAME);
21687 +
21688 +/* this module parameter has no meaning when SYSFS is disabled */
21689 +int sysaufs_brs = 1;
21690 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
21691 +module_param_named(brs, sysaufs_brs, int, S_IRUGO);
21692 +
21693 +/* this module parameter has no meaning when USER_NS is disabled */
21694 +static bool au_userns;
21695 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
21696 +module_param_named(allow_userns, au_userns, bool, S_IRUGO);
21697 +
21698 +/* ---------------------------------------------------------------------- */
21699 +
21700 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
21701 +
21702 +int au_seq_path(struct seq_file *seq, struct path *path)
21703 +{
21704 +       return seq_path(seq, path, au_esc_chars);
21705 +}
21706 +
21707 +/* ---------------------------------------------------------------------- */
21708 +
21709 +static int __init aufs_init(void)
21710 +{
21711 +       int err, i;
21712 +       char *p;
21713 +
21714 +       p = au_esc_chars;
21715 +       for (i = 1; i <= ' '; i++)
21716 +               *p++ = i;
21717 +       *p++ = '\\';
21718 +       *p++ = '\x7f';
21719 +       *p = 0;
21720 +
21721 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
21722 +
21723 +       au_sbilist_init();
21724 +       sysaufs_brs_init();
21725 +       au_debug_init();
21726 +       au_dy_init();
21727 +       err = sysaufs_init();
21728 +       if (unlikely(err))
21729 +               goto out;
21730 +       err = au_procfs_init();
21731 +       if (unlikely(err))
21732 +               goto out_sysaufs;
21733 +       err = au_wkq_init();
21734 +       if (unlikely(err))
21735 +               goto out_procfs;
21736 +       err = au_loopback_init();
21737 +       if (unlikely(err))
21738 +               goto out_wkq;
21739 +       err = au_hnotify_init();
21740 +       if (unlikely(err))
21741 +               goto out_loopback;
21742 +       err = au_sysrq_init();
21743 +       if (unlikely(err))
21744 +               goto out_hin;
21745 +       err = au_cache_init();
21746 +       if (unlikely(err))
21747 +               goto out_sysrq;
21748 +
21749 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
21750 +       err = register_filesystem(&aufs_fs_type);
21751 +       if (unlikely(err))
21752 +               goto out_cache;
21753 +
21754 +       /* since we define pr_fmt, call printk directly */
21755 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
21756 +       goto out; /* success */
21757 +
21758 +out_cache:
21759 +       au_cache_fin();
21760 +out_sysrq:
21761 +       au_sysrq_fin();
21762 +out_hin:
21763 +       au_hnotify_fin();
21764 +out_loopback:
21765 +       au_loopback_fin();
21766 +out_wkq:
21767 +       au_wkq_fin();
21768 +out_procfs:
21769 +       au_procfs_fin();
21770 +out_sysaufs:
21771 +       sysaufs_fin();
21772 +       au_dy_fin();
21773 +out:
21774 +       return err;
21775 +}
21776 +
21777 +static void __exit aufs_exit(void)
21778 +{
21779 +       unregister_filesystem(&aufs_fs_type);
21780 +       au_cache_fin();
21781 +       au_sysrq_fin();
21782 +       au_hnotify_fin();
21783 +       au_loopback_fin();
21784 +       au_wkq_fin();
21785 +       au_procfs_fin();
21786 +       sysaufs_fin();
21787 +       au_dy_fin();
21788 +}
21789 +
21790 +module_init(aufs_init);
21791 +module_exit(aufs_exit);
21792 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
21793 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
21794 +++ linux/fs/aufs/module.h      2015-09-24 10:47:58.254719746 +0200
21795 @@ -0,0 +1,104 @@
21796 +/*
21797 + * Copyright (C) 2005-2015 Junjiro R. Okajima
21798 + *
21799 + * This program, aufs is free software; you can redistribute it and/or modify
21800 + * it under the terms of the GNU General Public License as published by
21801 + * the Free Software Foundation; either version 2 of the License, or
21802 + * (at your option) any later version.
21803 + *
21804 + * This program is distributed in the hope that it will be useful,
21805 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21806 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21807 + * GNU General Public License for more details.
21808 + *
21809 + * You should have received a copy of the GNU General Public License
21810 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21811 + */
21812 +
21813 +/*
21814 + * module initialization and module-global
21815 + */
21816 +
21817 +#ifndef __AUFS_MODULE_H__
21818 +#define __AUFS_MODULE_H__
21819 +
21820 +#ifdef __KERNEL__
21821 +
21822 +#include <linux/slab.h>
21823 +
21824 +struct path;
21825 +struct seq_file;
21826 +
21827 +/* module parameters */
21828 +extern int sysaufs_brs;
21829 +
21830 +/* ---------------------------------------------------------------------- */
21831 +
21832 +extern int au_dir_roflags;
21833 +
21834 +enum {
21835 +       AuLcNonDir_FIINFO,
21836 +       AuLcNonDir_DIINFO,
21837 +       AuLcNonDir_IIINFO,
21838 +
21839 +       AuLcDir_FIINFO,
21840 +       AuLcDir_DIINFO,
21841 +       AuLcDir_IIINFO,
21842 +
21843 +       AuLcSymlink_DIINFO,
21844 +       AuLcSymlink_IIINFO,
21845 +
21846 +       AuLcKey_Last
21847 +};
21848 +extern struct lock_class_key au_lc_key[AuLcKey_Last];
21849 +
21850 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
21851 +int au_seq_path(struct seq_file *seq, struct path *path);
21852 +
21853 +#ifdef CONFIG_PROC_FS
21854 +/* procfs.c */
21855 +int __init au_procfs_init(void);
21856 +void au_procfs_fin(void);
21857 +#else
21858 +AuStubInt0(au_procfs_init, void);
21859 +AuStubVoid(au_procfs_fin, void);
21860 +#endif
21861 +
21862 +/* ---------------------------------------------------------------------- */
21863 +
21864 +/* kmem cache */
21865 +enum {
21866 +       AuCache_DINFO,
21867 +       AuCache_ICNTNR,
21868 +       AuCache_FINFO,
21869 +       AuCache_VDIR,
21870 +       AuCache_DEHSTR,
21871 +       AuCache_HNOTIFY, /* must be last */
21872 +       AuCache_Last
21873 +};
21874 +
21875 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
21876 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
21877 +#define AuCacheCtor(type, ctor)        \
21878 +       kmem_cache_create(#type, sizeof(struct type), \
21879 +                         __alignof__(struct type), AuCacheFlags, ctor)
21880 +
21881 +extern struct kmem_cache *au_cachep[];
21882 +
21883 +#define AuCacheFuncs(name, index) \
21884 +static inline struct au_##name *au_cache_alloc_##name(void) \
21885 +{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
21886 +static inline void au_cache_free_##name(struct au_##name *p) \
21887 +{ kmem_cache_free(au_cachep[AuCache_##index], p); }
21888 +
21889 +AuCacheFuncs(dinfo, DINFO);
21890 +AuCacheFuncs(icntnr, ICNTNR);
21891 +AuCacheFuncs(finfo, FINFO);
21892 +AuCacheFuncs(vdir, VDIR);
21893 +AuCacheFuncs(vdir_dehstr, DEHSTR);
21894 +#ifdef CONFIG_AUFS_HNOTIFY
21895 +AuCacheFuncs(hnotify, HNOTIFY);
21896 +#endif
21897 +
21898 +#endif /* __KERNEL__ */
21899 +#endif /* __AUFS_MODULE_H__ */
21900 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
21901 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
21902 +++ linux/fs/aufs/mvdown.c      2015-09-24 10:47:58.254719746 +0200
21903 @@ -0,0 +1,694 @@
21904 +/*
21905 + * Copyright (C) 2011-2015 Junjiro R. Okajima
21906 + *
21907 + * This program, aufs is free software; you can redistribute it and/or modify
21908 + * it under the terms of the GNU General Public License as published by
21909 + * the Free Software Foundation; either version 2 of the License, or
21910 + * (at your option) any later version.
21911 + *
21912 + * This program is distributed in the hope that it will be useful,
21913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21915 + * GNU General Public License for more details.
21916 + *
21917 + * You should have received a copy of the GNU General Public License
21918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21919 + */
21920 +
21921 +/*
21922 + * move-down, opposite of copy-up
21923 + */
21924 +
21925 +#include "aufs.h"
21926 +
21927 +struct au_mvd_args {
21928 +       struct {
21929 +               struct super_block *h_sb;
21930 +               struct dentry *h_parent;
21931 +               struct au_hinode *hdir;
21932 +               struct inode *h_dir, *h_inode;
21933 +               struct au_pin pin;
21934 +       } info[AUFS_MVDOWN_NARRAY];
21935 +
21936 +       struct aufs_mvdown mvdown;
21937 +       struct dentry *dentry, *parent;
21938 +       struct inode *inode, *dir;
21939 +       struct super_block *sb;
21940 +       aufs_bindex_t bopq, bwh, bfound;
21941 +       unsigned char rename_lock;
21942 +};
21943 +
21944 +#define mvd_errno              mvdown.au_errno
21945 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
21946 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
21947 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
21948 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
21949 +
21950 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
21951 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
21952 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
21953 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
21954 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
21955 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
21956 +
21957 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
21958 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
21959 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
21960 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
21961 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
21962 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
21963 +
21964 +#define AU_MVD_PR(flag, ...) do {                      \
21965 +               if (flag)                               \
21966 +                       pr_err(__VA_ARGS__);            \
21967 +       } while (0)
21968 +
21969 +static int find_lower_writable(struct au_mvd_args *a)
21970 +{
21971 +       struct super_block *sb;
21972 +       aufs_bindex_t bindex, bend;
21973 +       struct au_branch *br;
21974 +
21975 +       sb = a->sb;
21976 +       bindex = a->mvd_bsrc;
21977 +       bend = au_sbend(sb);
21978 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
21979 +               for (bindex++; bindex <= bend; bindex++) {
21980 +                       br = au_sbr(sb, bindex);
21981 +                       if (au_br_fhsm(br->br_perm)
21982 +                           && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
21983 +                               return bindex;
21984 +               }
21985 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
21986 +               for (bindex++; bindex <= bend; bindex++) {
21987 +                       br = au_sbr(sb, bindex);
21988 +                       if (!au_br_rdonly(br))
21989 +                               return bindex;
21990 +               }
21991 +       else
21992 +               for (bindex++; bindex <= bend; bindex++) {
21993 +                       br = au_sbr(sb, bindex);
21994 +                       if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
21995 +                               if (au_br_rdonly(br))
21996 +                                       a->mvdown.flags
21997 +                                               |= AUFS_MVDOWN_ROLOWER_R;
21998 +                               return bindex;
21999 +                       }
22000 +               }
22001 +
22002 +       return -1;
22003 +}
22004 +
22005 +/* make the parent dir on bdst */
22006 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
22007 +{
22008 +       int err;
22009 +
22010 +       err = 0;
22011 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22012 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22013 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22014 +       a->mvd_h_dst_parent = NULL;
22015 +       if (au_dbend(a->parent) >= a->mvd_bdst)
22016 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22017 +       if (!a->mvd_h_dst_parent) {
22018 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22019 +               if (unlikely(err)) {
22020 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
22021 +                       goto out;
22022 +               }
22023 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22024 +       }
22025 +
22026 +out:
22027 +       AuTraceErr(err);
22028 +       return err;
22029 +}
22030 +
22031 +/* lock them all */
22032 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
22033 +{
22034 +       int err;
22035 +       struct dentry *h_trap;
22036 +
22037 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22038 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
22039 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22040 +                    au_opt_udba(a->sb),
22041 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22042 +       AuTraceErr(err);
22043 +       if (unlikely(err)) {
22044 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
22045 +               goto out;
22046 +       }
22047 +
22048 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22049 +               a->rename_lock = 0;
22050 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22051 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22052 +                           au_opt_udba(a->sb),
22053 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22054 +               err = au_do_pin(&a->mvd_pin_src);
22055 +               AuTraceErr(err);
22056 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22057 +               if (unlikely(err)) {
22058 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
22059 +                       goto out_dst;
22060 +               }
22061 +               goto out; /* success */
22062 +       }
22063 +
22064 +       a->rename_lock = 1;
22065 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
22066 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22067 +                    au_opt_udba(a->sb),
22068 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22069 +       AuTraceErr(err);
22070 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
22071 +       if (unlikely(err)) {
22072 +               AU_MVD_PR(dmsg, "pin_src failed\n");
22073 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22074 +               goto out_dst;
22075 +       }
22076 +       au_pin_hdir_unlock(&a->mvd_pin_src);
22077 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22078 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
22079 +       if (h_trap) {
22080 +               err = (h_trap != a->mvd_h_src_parent);
22081 +               if (err)
22082 +                       err = (h_trap != a->mvd_h_dst_parent);
22083 +       }
22084 +       BUG_ON(err); /* it should never happen */
22085 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22086 +               err = -EBUSY;
22087 +               AuTraceErr(err);
22088 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22089 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22090 +               au_pin_hdir_lock(&a->mvd_pin_src);
22091 +               au_unpin(&a->mvd_pin_src);
22092 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22093 +               goto out_dst;
22094 +       }
22095 +       goto out; /* success */
22096 +
22097 +out_dst:
22098 +       au_unpin(&a->mvd_pin_dst);
22099 +out:
22100 +       AuTraceErr(err);
22101 +       return err;
22102 +}
22103 +
22104 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
22105 +{
22106 +       if (!a->rename_lock)
22107 +               au_unpin(&a->mvd_pin_src);
22108 +       else {
22109 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22110 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
22111 +               au_pin_hdir_lock(&a->mvd_pin_src);
22112 +               au_unpin(&a->mvd_pin_src);
22113 +               au_pin_hdir_lock(&a->mvd_pin_dst);
22114 +       }
22115 +       au_unpin(&a->mvd_pin_dst);
22116 +}
22117 +
22118 +/* copy-down the file */
22119 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
22120 +{
22121 +       int err;
22122 +       struct au_cp_generic cpg = {
22123 +               .dentry = a->dentry,
22124 +               .bdst   = a->mvd_bdst,
22125 +               .bsrc   = a->mvd_bsrc,
22126 +               .len    = -1,
22127 +               .pin    = &a->mvd_pin_dst,
22128 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
22129 +       };
22130 +
22131 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
22132 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22133 +               au_fset_cpup(cpg.flags, OVERWRITE);
22134 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22135 +               au_fset_cpup(cpg.flags, RWDST);
22136 +       err = au_sio_cpdown_simple(&cpg);
22137 +       if (unlikely(err))
22138 +               AU_MVD_PR(dmsg, "cpdown failed\n");
22139 +
22140 +       AuTraceErr(err);
22141 +       return err;
22142 +}
22143 +
22144 +/*
22145 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
22146 + * were sleeping
22147 + */
22148 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
22149 +{
22150 +       int err;
22151 +       struct path h_path;
22152 +       struct au_branch *br;
22153 +       struct inode *delegated;
22154 +
22155 +       br = au_sbr(a->sb, a->mvd_bdst);
22156 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22157 +       err = PTR_ERR(h_path.dentry);
22158 +       if (IS_ERR(h_path.dentry)) {
22159 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
22160 +               goto out;
22161 +       }
22162 +
22163 +       err = 0;
22164 +       if (d_is_positive(h_path.dentry)) {
22165 +               h_path.mnt = au_br_mnt(br);
22166 +               delegated = NULL;
22167 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
22168 +                                  &delegated, /*force*/0);
22169 +               if (unlikely(err == -EWOULDBLOCK)) {
22170 +                       pr_warn("cannot retry for NFSv4 delegation"
22171 +                               " for an internal unlink\n");
22172 +                       iput(delegated);
22173 +               }
22174 +               if (unlikely(err))
22175 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
22176 +       }
22177 +       dput(h_path.dentry);
22178 +
22179 +out:
22180 +       AuTraceErr(err);
22181 +       return err;
22182 +}
22183 +
22184 +/*
22185 + * unlink the topmost h_dentry
22186 + */
22187 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
22188 +{
22189 +       int err;
22190 +       struct path h_path;
22191 +       struct inode *delegated;
22192 +
22193 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22194 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
22195 +       delegated = NULL;
22196 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22197 +       if (unlikely(err == -EWOULDBLOCK)) {
22198 +               pr_warn("cannot retry for NFSv4 delegation"
22199 +                       " for an internal unlink\n");
22200 +               iput(delegated);
22201 +       }
22202 +       if (unlikely(err))
22203 +               AU_MVD_PR(dmsg, "unlink failed\n");
22204 +
22205 +       AuTraceErr(err);
22206 +       return err;
22207 +}
22208 +
22209 +/* Since mvdown succeeded, we ignore an error of this function */
22210 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22211 +{
22212 +       int err;
22213 +       struct au_branch *br;
22214 +
22215 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22216 +       br = au_sbr(a->sb, a->mvd_bsrc);
22217 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22218 +       if (!err) {
22219 +               br = au_sbr(a->sb, a->mvd_bdst);
22220 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22221 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22222 +       }
22223 +       if (!err)
22224 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22225 +       else
22226 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22227 +}
22228 +
22229 +/*
22230 + * copy-down the file and unlink the bsrc file.
22231 + * - unlink the bdst whout if exist
22232 + * - copy-down the file (with whtmp name and rename)
22233 + * - unlink the bsrc file
22234 + */
22235 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
22236 +{
22237 +       int err;
22238 +
22239 +       err = au_do_mkdir(dmsg, a);
22240 +       if (!err)
22241 +               err = au_do_lock(dmsg, a);
22242 +       if (unlikely(err))
22243 +               goto out;
22244 +
22245 +       /*
22246 +        * do not revert the activities we made on bdst since they should be
22247 +        * harmless in aufs.
22248 +        */
22249 +
22250 +       err = au_do_cpdown(dmsg, a);
22251 +       if (!err)
22252 +               err = au_do_unlink_wh(dmsg, a);
22253 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22254 +               err = au_do_unlink(dmsg, a);
22255 +       if (unlikely(err))
22256 +               goto out_unlock;
22257 +
22258 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
22259 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
22260 +       if (find_lower_writable(a) < 0)
22261 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22262 +
22263 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22264 +               au_do_stfs(dmsg, a);
22265 +
22266 +       /* maintain internal array */
22267 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22268 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
22269 +               au_set_dbstart(a->dentry, a->mvd_bdst);
22270 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
22271 +               au_set_ibstart(a->inode, a->mvd_bdst);
22272 +       }
22273 +       if (au_dbend(a->dentry) < a->mvd_bdst)
22274 +               au_set_dbend(a->dentry, a->mvd_bdst);
22275 +       if (au_ibend(a->inode) < a->mvd_bdst)
22276 +               au_set_ibend(a->inode, a->mvd_bdst);
22277 +
22278 +out_unlock:
22279 +       au_do_unlock(dmsg, a);
22280 +out:
22281 +       AuTraceErr(err);
22282 +       return err;
22283 +}
22284 +
22285 +/* ---------------------------------------------------------------------- */
22286 +
22287 +/* make sure the file is idle */
22288 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
22289 +{
22290 +       int err, plinked;
22291 +
22292 +       err = 0;
22293 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
22294 +       if (au_dbstart(a->dentry) == a->mvd_bsrc
22295 +           && au_dcount(a->dentry) == 1
22296 +           && atomic_read(&a->inode->i_count) == 1
22297 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
22298 +           && (!plinked || !au_plink_test(a->inode))
22299 +           && a->inode->i_nlink == 1)
22300 +               goto out;
22301 +
22302 +       err = -EBUSY;
22303 +       AU_MVD_PR(dmsg,
22304 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
22305 +                 a->mvd_bsrc, au_dbstart(a->dentry), au_dcount(a->dentry),
22306 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
22307 +                 a->mvd_h_src_inode->i_nlink,
22308 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
22309 +
22310 +out:
22311 +       AuTraceErr(err);
22312 +       return err;
22313 +}
22314 +
22315 +/* make sure the parent dir is fine */
22316 +static int au_mvd_args_parent(const unsigned char dmsg,
22317 +                             struct au_mvd_args *a)
22318 +{
22319 +       int err;
22320 +       aufs_bindex_t bindex;
22321 +
22322 +       err = 0;
22323 +       if (unlikely(au_alive_dir(a->parent))) {
22324 +               err = -ENOENT;
22325 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
22326 +               goto out;
22327 +       }
22328 +
22329 +       a->bopq = au_dbdiropq(a->parent);
22330 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
22331 +       AuDbg("b%d\n", bindex);
22332 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
22333 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
22334 +               err = -EINVAL;
22335 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
22336 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
22337 +                         a->bopq, a->mvd_bdst);
22338 +       }
22339 +
22340 +out:
22341 +       AuTraceErr(err);
22342 +       return err;
22343 +}
22344 +
22345 +static int au_mvd_args_intermediate(const unsigned char dmsg,
22346 +                                   struct au_mvd_args *a)
22347 +{
22348 +       int err;
22349 +       struct au_dinfo *dinfo, *tmp;
22350 +
22351 +       /* lookup the next lower positive entry */
22352 +       err = -ENOMEM;
22353 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
22354 +       if (unlikely(!tmp))
22355 +               goto out;
22356 +
22357 +       a->bfound = -1;
22358 +       a->bwh = -1;
22359 +       dinfo = au_di(a->dentry);
22360 +       au_di_cp(tmp, dinfo);
22361 +       au_di_swap(tmp, dinfo);
22362 +
22363 +       /* returns the number of positive dentries */
22364 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1, /*type*/0);
22365 +       if (!err)
22366 +               a->bwh = au_dbwh(a->dentry);
22367 +       else if (err > 0)
22368 +               a->bfound = au_dbstart(a->dentry);
22369 +
22370 +       au_di_swap(tmp, dinfo);
22371 +       au_rw_write_unlock(&tmp->di_rwsem);
22372 +       au_di_free(tmp);
22373 +       if (unlikely(err < 0))
22374 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
22375 +
22376 +       /*
22377 +        * here, we have these cases.
22378 +        * bfound == -1
22379 +        *      no positive dentry under bsrc. there are more sub-cases.
22380 +        *      bwh < 0
22381 +        *              there no whiteout, we can safely move-down.
22382 +        *      bwh <= bsrc
22383 +        *              impossible
22384 +        *      bsrc < bwh && bwh < bdst
22385 +        *              there is a whiteout on RO branch. cannot proceed.
22386 +        *      bwh == bdst
22387 +        *              there is a whiteout on the RW target branch. it should
22388 +        *              be removed.
22389 +        *      bdst < bwh
22390 +        *              there is a whiteout somewhere unrelated branch.
22391 +        * -1 < bfound && bfound <= bsrc
22392 +        *      impossible.
22393 +        * bfound < bdst
22394 +        *      found, but it is on RO branch between bsrc and bdst. cannot
22395 +        *      proceed.
22396 +        * bfound == bdst
22397 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
22398 +        *      error.
22399 +        * bdst < bfound
22400 +        *      found, after we create the file on bdst, it will be hidden.
22401 +        */
22402 +
22403 +       AuDebugOn(a->bfound == -1
22404 +                 && a->bwh != -1
22405 +                 && a->bwh <= a->mvd_bsrc);
22406 +       AuDebugOn(-1 < a->bfound
22407 +                 && a->bfound <= a->mvd_bsrc);
22408 +
22409 +       err = -EINVAL;
22410 +       if (a->bfound == -1
22411 +           && a->mvd_bsrc < a->bwh
22412 +           && a->bwh != -1
22413 +           && a->bwh < a->mvd_bdst) {
22414 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
22415 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
22416 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
22417 +               goto out;
22418 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
22419 +               a->mvd_errno = EAU_MVDOWN_UPPER;
22420 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
22421 +                         a->mvd_bdst, a->bfound);
22422 +               goto out;
22423 +       }
22424 +
22425 +       err = 0; /* success */
22426 +
22427 +out:
22428 +       AuTraceErr(err);
22429 +       return err;
22430 +}
22431 +
22432 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
22433 +{
22434 +       int err;
22435 +
22436 +       err = 0;
22437 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22438 +           && a->bfound == a->mvd_bdst)
22439 +               err = -EEXIST;
22440 +       AuTraceErr(err);
22441 +       return err;
22442 +}
22443 +
22444 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
22445 +{
22446 +       int err;
22447 +       struct au_branch *br;
22448 +
22449 +       err = -EISDIR;
22450 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
22451 +               goto out;
22452 +
22453 +       err = -EINVAL;
22454 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
22455 +               a->mvd_bsrc = au_ibstart(a->inode);
22456 +       else {
22457 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
22458 +               if (unlikely(a->mvd_bsrc < 0
22459 +                            || (a->mvd_bsrc < au_dbstart(a->dentry)
22460 +                                || au_dbend(a->dentry) < a->mvd_bsrc
22461 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
22462 +                            || (a->mvd_bsrc < au_ibstart(a->inode)
22463 +                                || au_ibend(a->inode) < a->mvd_bsrc
22464 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
22465 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
22466 +                       AU_MVD_PR(dmsg, "no upper\n");
22467 +                       goto out;
22468 +               }
22469 +       }
22470 +       if (unlikely(a->mvd_bsrc == au_sbend(a->sb))) {
22471 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
22472 +               AU_MVD_PR(dmsg, "on the bottom\n");
22473 +               goto out;
22474 +       }
22475 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
22476 +       br = au_sbr(a->sb, a->mvd_bsrc);
22477 +       err = au_br_rdonly(br);
22478 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
22479 +               if (unlikely(err))
22480 +                       goto out;
22481 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
22482 +                    || IS_APPEND(a->mvd_h_src_inode))) {
22483 +               if (err)
22484 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22485 +               /* go on */
22486 +       } else
22487 +               goto out;
22488 +
22489 +       err = -EINVAL;
22490 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22491 +               a->mvd_bdst = find_lower_writable(a);
22492 +               if (unlikely(a->mvd_bdst < 0)) {
22493 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
22494 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
22495 +                       goto out;
22496 +               }
22497 +       } else {
22498 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22499 +               if (unlikely(a->mvd_bdst < 0
22500 +                            || au_sbend(a->sb) < a->mvd_bdst)) {
22501 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22502 +                       AU_MVD_PR(dmsg, "no lower brid\n");
22503 +                       goto out;
22504 +               }
22505 +       }
22506 +
22507 +       err = au_mvd_args_busy(dmsg, a);
22508 +       if (!err)
22509 +               err = au_mvd_args_parent(dmsg, a);
22510 +       if (!err)
22511 +               err = au_mvd_args_intermediate(dmsg, a);
22512 +       if (!err)
22513 +               err = au_mvd_args_exist(dmsg, a);
22514 +       if (!err)
22515 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22516 +
22517 +out:
22518 +       AuTraceErr(err);
22519 +       return err;
22520 +}
22521 +
22522 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22523 +{
22524 +       int err, e;
22525 +       unsigned char dmsg;
22526 +       struct au_mvd_args *args;
22527 +
22528 +       err = -EPERM;
22529 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
22530 +               goto out;
22531 +
22532 +       err = -ENOMEM;
22533 +       args = kmalloc(sizeof(*args), GFP_NOFS);
22534 +       if (unlikely(!args))
22535 +               goto out;
22536 +
22537 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22538 +       if (!err)
22539 +               err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
22540 +       if (unlikely(err)) {
22541 +               err = -EFAULT;
22542 +               AuTraceErr(err);
22543 +               goto out_free;
22544 +       }
22545 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
22546 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22547 +       args->mvdown.au_errno = 0;
22548 +       args->dentry = dentry;
22549 +       args->inode = d_inode(dentry);
22550 +       args->sb = dentry->d_sb;
22551 +
22552 +       err = -ENOENT;
22553 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22554 +       args->parent = dget_parent(dentry);
22555 +       args->dir = d_inode(args->parent);
22556 +       mutex_lock_nested(&args->dir->i_mutex, I_MUTEX_PARENT);
22557 +       dput(args->parent);
22558 +       if (unlikely(args->parent != dentry->d_parent)) {
22559 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
22560 +               goto out_dir;
22561 +       }
22562 +
22563 +       mutex_lock_nested(&args->inode->i_mutex, I_MUTEX_CHILD);
22564 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH);
22565 +       if (unlikely(err))
22566 +               goto out_inode;
22567 +
22568 +       di_write_lock_parent(args->parent);
22569 +       err = au_mvd_args(dmsg, args);
22570 +       if (unlikely(err))
22571 +               goto out_parent;
22572 +
22573 +       err = au_do_mvdown(dmsg, args);
22574 +       if (unlikely(err))
22575 +               goto out_parent;
22576 +
22577 +       au_cpup_attr_timesizes(args->dir);
22578 +       au_cpup_attr_timesizes(args->inode);
22579 +       au_cpup_igen(args->inode, au_h_iptr(args->inode, args->mvd_bdst));
22580 +       /* au_digen_dec(dentry); */
22581 +
22582 +out_parent:
22583 +       di_write_unlock(args->parent);
22584 +       aufs_read_unlock(dentry, AuLock_DW);
22585 +out_inode:
22586 +       mutex_unlock(&args->inode->i_mutex);
22587 +out_dir:
22588 +       mutex_unlock(&args->dir->i_mutex);
22589 +out_free:
22590 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
22591 +       if (unlikely(e))
22592 +               err = -EFAULT;
22593 +       kfree(args);
22594 +out:
22595 +       AuTraceErr(err);
22596 +       return err;
22597 +}
22598 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
22599 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
22600 +++ linux/fs/aufs/opts.c        2015-09-24 10:47:58.254719746 +0200
22601 @@ -0,0 +1,1835 @@
22602 +/*
22603 + * Copyright (C) 2005-2015 Junjiro R. Okajima
22604 + *
22605 + * This program, aufs is free software; you can redistribute it and/or modify
22606 + * it under the terms of the GNU General Public License as published by
22607 + * the Free Software Foundation; either version 2 of the License, or
22608 + * (at your option) any later version.
22609 + *
22610 + * This program is distributed in the hope that it will be useful,
22611 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22612 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22613 + * GNU General Public License for more details.
22614 + *
22615 + * You should have received a copy of the GNU General Public License
22616 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22617 + */
22618 +
22619 +/*
22620 + * mount options/flags
22621 + */
22622 +
22623 +#include <linux/namei.h>
22624 +#include <linux/types.h> /* a distribution requires */
22625 +#include <linux/parser.h>
22626 +#include "aufs.h"
22627 +
22628 +/* ---------------------------------------------------------------------- */
22629 +
22630 +enum {
22631 +       Opt_br,
22632 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
22633 +       Opt_idel, Opt_imod,
22634 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
22635 +       Opt_rdblk_def, Opt_rdhash_def,
22636 +       Opt_xino, Opt_noxino,
22637 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
22638 +       Opt_trunc_xino_path, Opt_itrunc_xino,
22639 +       Opt_trunc_xib, Opt_notrunc_xib,
22640 +       Opt_shwh, Opt_noshwh,
22641 +       Opt_plink, Opt_noplink, Opt_list_plink,
22642 +       Opt_udba,
22643 +       Opt_dio, Opt_nodio,
22644 +       Opt_diropq_a, Opt_diropq_w,
22645 +       Opt_warn_perm, Opt_nowarn_perm,
22646 +       Opt_wbr_copyup, Opt_wbr_create,
22647 +       Opt_fhsm_sec,
22648 +       Opt_verbose, Opt_noverbose,
22649 +       Opt_sum, Opt_nosum, Opt_wsum,
22650 +       Opt_dirperm1, Opt_nodirperm1,
22651 +       Opt_acl, Opt_noacl,
22652 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
22653 +};
22654 +
22655 +static match_table_t options = {
22656 +       {Opt_br, "br=%s"},
22657 +       {Opt_br, "br:%s"},
22658 +
22659 +       {Opt_add, "add=%d:%s"},
22660 +       {Opt_add, "add:%d:%s"},
22661 +       {Opt_add, "ins=%d:%s"},
22662 +       {Opt_add, "ins:%d:%s"},
22663 +       {Opt_append, "append=%s"},
22664 +       {Opt_append, "append:%s"},
22665 +       {Opt_prepend, "prepend=%s"},
22666 +       {Opt_prepend, "prepend:%s"},
22667 +
22668 +       {Opt_del, "del=%s"},
22669 +       {Opt_del, "del:%s"},
22670 +       /* {Opt_idel, "idel:%d"}, */
22671 +       {Opt_mod, "mod=%s"},
22672 +       {Opt_mod, "mod:%s"},
22673 +       /* {Opt_imod, "imod:%d:%s"}, */
22674 +
22675 +       {Opt_dirwh, "dirwh=%d"},
22676 +
22677 +       {Opt_xino, "xino=%s"},
22678 +       {Opt_noxino, "noxino"},
22679 +       {Opt_trunc_xino, "trunc_xino"},
22680 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
22681 +       {Opt_notrunc_xino, "notrunc_xino"},
22682 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
22683 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
22684 +       /* {Opt_zxino, "zxino=%s"}, */
22685 +       {Opt_trunc_xib, "trunc_xib"},
22686 +       {Opt_notrunc_xib, "notrunc_xib"},
22687 +
22688 +#ifdef CONFIG_PROC_FS
22689 +       {Opt_plink, "plink"},
22690 +#else
22691 +       {Opt_ignore_silent, "plink"},
22692 +#endif
22693 +
22694 +       {Opt_noplink, "noplink"},
22695 +
22696 +#ifdef CONFIG_AUFS_DEBUG
22697 +       {Opt_list_plink, "list_plink"},
22698 +#endif
22699 +
22700 +       {Opt_udba, "udba=%s"},
22701 +
22702 +       {Opt_dio, "dio"},
22703 +       {Opt_nodio, "nodio"},
22704 +
22705 +#ifdef CONFIG_AUFS_FHSM
22706 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
22707 +#else
22708 +       {Opt_ignore_silent, "fhsm_sec=%d"},
22709 +#endif
22710 +
22711 +       {Opt_diropq_a, "diropq=always"},
22712 +       {Opt_diropq_a, "diropq=a"},
22713 +       {Opt_diropq_w, "diropq=whiteouted"},
22714 +       {Opt_diropq_w, "diropq=w"},
22715 +
22716 +       {Opt_warn_perm, "warn_perm"},
22717 +       {Opt_nowarn_perm, "nowarn_perm"},
22718 +
22719 +       /* keep them temporary */
22720 +       {Opt_ignore_silent, "nodlgt"},
22721 +       {Opt_ignore_silent, "clean_plink"},
22722 +
22723 +#ifdef CONFIG_AUFS_SHWH
22724 +       {Opt_shwh, "shwh"},
22725 +#endif
22726 +       {Opt_noshwh, "noshwh"},
22727 +
22728 +       {Opt_dirperm1, "dirperm1"},
22729 +       {Opt_nodirperm1, "nodirperm1"},
22730 +
22731 +       {Opt_verbose, "verbose"},
22732 +       {Opt_verbose, "v"},
22733 +       {Opt_noverbose, "noverbose"},
22734 +       {Opt_noverbose, "quiet"},
22735 +       {Opt_noverbose, "q"},
22736 +       {Opt_noverbose, "silent"},
22737 +
22738 +       {Opt_sum, "sum"},
22739 +       {Opt_nosum, "nosum"},
22740 +       {Opt_wsum, "wsum"},
22741 +
22742 +       {Opt_rdcache, "rdcache=%d"},
22743 +       {Opt_rdblk, "rdblk=%d"},
22744 +       {Opt_rdblk_def, "rdblk=def"},
22745 +       {Opt_rdhash, "rdhash=%d"},
22746 +       {Opt_rdhash_def, "rdhash=def"},
22747 +
22748 +       {Opt_wbr_create, "create=%s"},
22749 +       {Opt_wbr_create, "create_policy=%s"},
22750 +       {Opt_wbr_copyup, "cpup=%s"},
22751 +       {Opt_wbr_copyup, "copyup=%s"},
22752 +       {Opt_wbr_copyup, "copyup_policy=%s"},
22753 +
22754 +       /* generic VFS flag */
22755 +#ifdef CONFIG_FS_POSIX_ACL
22756 +       {Opt_acl, "acl"},
22757 +       {Opt_noacl, "noacl"},
22758 +#else
22759 +       {Opt_ignore_silent, "acl"},
22760 +       {Opt_ignore_silent, "noacl"},
22761 +#endif
22762 +
22763 +       /* internal use for the scripts */
22764 +       {Opt_ignore_silent, "si=%s"},
22765 +
22766 +       {Opt_br, "dirs=%s"},
22767 +       {Opt_ignore, "debug=%d"},
22768 +       {Opt_ignore, "delete=whiteout"},
22769 +       {Opt_ignore, "delete=all"},
22770 +       {Opt_ignore, "imap=%s"},
22771 +
22772 +       /* temporary workaround, due to old mount(8)? */
22773 +       {Opt_ignore_silent, "relatime"},
22774 +
22775 +       {Opt_err, NULL}
22776 +};
22777 +
22778 +/* ---------------------------------------------------------------------- */
22779 +
22780 +static const char *au_parser_pattern(int val, match_table_t tbl)
22781 +{
22782 +       struct match_token *p;
22783 +
22784 +       p = tbl;
22785 +       while (p->pattern) {
22786 +               if (p->token == val)
22787 +                       return p->pattern;
22788 +               p++;
22789 +       }
22790 +       BUG();
22791 +       return "??";
22792 +}
22793 +
22794 +static const char *au_optstr(int *val, match_table_t tbl)
22795 +{
22796 +       struct match_token *p;
22797 +       int v;
22798 +
22799 +       v = *val;
22800 +       if (!v)
22801 +               goto out;
22802 +       p = tbl;
22803 +       while (p->pattern) {
22804 +               if (p->token
22805 +                   && (v & p->token) == p->token) {
22806 +                       *val &= ~p->token;
22807 +                       return p->pattern;
22808 +               }
22809 +               p++;
22810 +       }
22811 +
22812 +out:
22813 +       return NULL;
22814 +}
22815 +
22816 +/* ---------------------------------------------------------------------- */
22817 +
22818 +static match_table_t brperm = {
22819 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
22820 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
22821 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
22822 +       {0, NULL}
22823 +};
22824 +
22825 +static match_table_t brattr = {
22826 +       /* general */
22827 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
22828 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
22829 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
22830 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
22831 +#ifdef CONFIG_AUFS_FHSM
22832 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
22833 +#endif
22834 +#ifdef CONFIG_AUFS_XATTR
22835 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
22836 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
22837 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
22838 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
22839 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
22840 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
22841 +#endif
22842 +
22843 +       /* ro/rr branch */
22844 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
22845 +
22846 +       /* rw branch */
22847 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
22848 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
22849 +
22850 +       {0, NULL}
22851 +};
22852 +
22853 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
22854 +{
22855 +       int attr, v;
22856 +       char *p;
22857 +
22858 +       attr = 0;
22859 +       do {
22860 +               p = strchr(str, '+');
22861 +               if (p)
22862 +                       *p = 0;
22863 +               v = match_token(str, table, args);
22864 +               if (v) {
22865 +                       if (v & AuBrAttr_CMOO_Mask)
22866 +                               attr &= ~AuBrAttr_CMOO_Mask;
22867 +                       attr |= v;
22868 +               } else {
22869 +                       if (p)
22870 +                               *p = '+';
22871 +                       pr_warn("ignored branch attribute %s\n", str);
22872 +                       break;
22873 +               }
22874 +               if (p)
22875 +                       str = p + 1;
22876 +       } while (p);
22877 +
22878 +       return attr;
22879 +}
22880 +
22881 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
22882 +{
22883 +       int sz;
22884 +       const char *p;
22885 +       char *q;
22886 +
22887 +       q = str->a;
22888 +       *q = 0;
22889 +       p = au_optstr(&perm, brattr);
22890 +       if (p) {
22891 +               sz = strlen(p);
22892 +               memcpy(q, p, sz + 1);
22893 +               q += sz;
22894 +       } else
22895 +               goto out;
22896 +
22897 +       do {
22898 +               p = au_optstr(&perm, brattr);
22899 +               if (p) {
22900 +                       *q++ = '+';
22901 +                       sz = strlen(p);
22902 +                       memcpy(q, p, sz + 1);
22903 +                       q += sz;
22904 +               }
22905 +       } while (p);
22906 +
22907 +out:
22908 +       return q - str->a;
22909 +}
22910 +
22911 +static int noinline_for_stack br_perm_val(char *perm)
22912 +{
22913 +       int val, bad, sz;
22914 +       char *p;
22915 +       substring_t args[MAX_OPT_ARGS];
22916 +       au_br_perm_str_t attr;
22917 +
22918 +       p = strchr(perm, '+');
22919 +       if (p)
22920 +               *p = 0;
22921 +       val = match_token(perm, brperm, args);
22922 +       if (!val) {
22923 +               if (p)
22924 +                       *p = '+';
22925 +               pr_warn("ignored branch permission %s\n", perm);
22926 +               val = AuBrPerm_RO;
22927 +               goto out;
22928 +       }
22929 +       if (!p)
22930 +               goto out;
22931 +
22932 +       val |= br_attr_val(p + 1, brattr, args);
22933 +
22934 +       bad = 0;
22935 +       switch (val & AuBrPerm_Mask) {
22936 +       case AuBrPerm_RO:
22937 +       case AuBrPerm_RR:
22938 +               bad = val & AuBrWAttr_Mask;
22939 +               val &= ~AuBrWAttr_Mask;
22940 +               break;
22941 +       case AuBrPerm_RW:
22942 +               bad = val & AuBrRAttr_Mask;
22943 +               val &= ~AuBrRAttr_Mask;
22944 +               break;
22945 +       }
22946 +
22947 +       /*
22948 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
22949 +        * does not treat it as an error, just warning.
22950 +        * this is a tiny guard for the user operation.
22951 +        */
22952 +       if (val & AuBrAttr_UNPIN) {
22953 +               bad |= AuBrAttr_UNPIN;
22954 +               val &= ~AuBrAttr_UNPIN;
22955 +       }
22956 +
22957 +       if (unlikely(bad)) {
22958 +               sz = au_do_optstr_br_attr(&attr, bad);
22959 +               AuDebugOn(!sz);
22960 +               pr_warn("ignored branch attribute %s\n", attr.a);
22961 +       }
22962 +
22963 +out:
22964 +       return val;
22965 +}
22966 +
22967 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
22968 +{
22969 +       au_br_perm_str_t attr;
22970 +       const char *p;
22971 +       char *q;
22972 +       int sz;
22973 +
22974 +       q = str->a;
22975 +       p = au_optstr(&perm, brperm);
22976 +       AuDebugOn(!p || !*p);
22977 +       sz = strlen(p);
22978 +       memcpy(q, p, sz + 1);
22979 +       q += sz;
22980 +
22981 +       sz = au_do_optstr_br_attr(&attr, perm);
22982 +       if (sz) {
22983 +               *q++ = '+';
22984 +               memcpy(q, attr.a, sz + 1);
22985 +       }
22986 +
22987 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
22988 +}
22989 +
22990 +/* ---------------------------------------------------------------------- */
22991 +
22992 +static match_table_t udbalevel = {
22993 +       {AuOpt_UDBA_REVAL, "reval"},
22994 +       {AuOpt_UDBA_NONE, "none"},
22995 +#ifdef CONFIG_AUFS_HNOTIFY
22996 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
22997 +#ifdef CONFIG_AUFS_HFSNOTIFY
22998 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
22999 +#endif
23000 +#endif
23001 +       {-1, NULL}
23002 +};
23003 +
23004 +static int noinline_for_stack udba_val(char *str)
23005 +{
23006 +       substring_t args[MAX_OPT_ARGS];
23007 +
23008 +       return match_token(str, udbalevel, args);
23009 +}
23010 +
23011 +const char *au_optstr_udba(int udba)
23012 +{
23013 +       return au_parser_pattern(udba, udbalevel);
23014 +}
23015 +
23016 +/* ---------------------------------------------------------------------- */
23017 +
23018 +static match_table_t au_wbr_create_policy = {
23019 +       {AuWbrCreate_TDP, "tdp"},
23020 +       {AuWbrCreate_TDP, "top-down-parent"},
23021 +       {AuWbrCreate_RR, "rr"},
23022 +       {AuWbrCreate_RR, "round-robin"},
23023 +       {AuWbrCreate_MFS, "mfs"},
23024 +       {AuWbrCreate_MFS, "most-free-space"},
23025 +       {AuWbrCreate_MFSV, "mfs:%d"},
23026 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
23027 +
23028 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
23029 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23030 +       {AuWbrCreate_PMFS, "pmfs"},
23031 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
23032 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23033 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
23034 +
23035 +       {-1, NULL}
23036 +};
23037 +
23038 +/*
23039 + * cf. linux/lib/parser.c and cmdline.c
23040 + * gave up calling memparse() since it uses simple_strtoull() instead of
23041 + * kstrto...().
23042 + */
23043 +static int noinline_for_stack
23044 +au_match_ull(substring_t *s, unsigned long long *result)
23045 +{
23046 +       int err;
23047 +       unsigned int len;
23048 +       char a[32];
23049 +
23050 +       err = -ERANGE;
23051 +       len = s->to - s->from;
23052 +       if (len + 1 <= sizeof(a)) {
23053 +               memcpy(a, s->from, len);
23054 +               a[len] = '\0';
23055 +               err = kstrtoull(a, 0, result);
23056 +       }
23057 +       return err;
23058 +}
23059 +
23060 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23061 +                           struct au_opt_wbr_create *create)
23062 +{
23063 +       int err;
23064 +       unsigned long long ull;
23065 +
23066 +       err = 0;
23067 +       if (!au_match_ull(arg, &ull))
23068 +               create->mfsrr_watermark = ull;
23069 +       else {
23070 +               pr_err("bad integer in %s\n", str);
23071 +               err = -EINVAL;
23072 +       }
23073 +
23074 +       return err;
23075 +}
23076 +
23077 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
23078 +                         struct au_opt_wbr_create *create)
23079 +{
23080 +       int n, err;
23081 +
23082 +       err = 0;
23083 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
23084 +               create->mfs_second = n;
23085 +       else {
23086 +               pr_err("bad integer in %s\n", str);
23087 +               err = -EINVAL;
23088 +       }
23089 +
23090 +       return err;
23091 +}
23092 +
23093 +static int noinline_for_stack
23094 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
23095 +{
23096 +       int err, e;
23097 +       substring_t args[MAX_OPT_ARGS];
23098 +
23099 +       err = match_token(str, au_wbr_create_policy, args);
23100 +       create->wbr_create = err;
23101 +       switch (err) {
23102 +       case AuWbrCreate_MFSRRV:
23103 +       case AuWbrCreate_PMFSRRV:
23104 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23105 +               if (!e)
23106 +                       e = au_wbr_mfs_sec(&args[1], str, create);
23107 +               if (unlikely(e))
23108 +                       err = e;
23109 +               break;
23110 +       case AuWbrCreate_MFSRR:
23111 +       case AuWbrCreate_PMFSRR:
23112 +               e = au_wbr_mfs_wmark(&args[0], str, create);
23113 +               if (unlikely(e)) {
23114 +                       err = e;
23115 +                       break;
23116 +               }
23117 +               /*FALLTHROUGH*/
23118 +       case AuWbrCreate_MFS:
23119 +       case AuWbrCreate_PMFS:
23120 +               create->mfs_second = AUFS_MFS_DEF_SEC;
23121 +               break;
23122 +       case AuWbrCreate_MFSV:
23123 +       case AuWbrCreate_PMFSV:
23124 +               e = au_wbr_mfs_sec(&args[0], str, create);
23125 +               if (unlikely(e))
23126 +                       err = e;
23127 +               break;
23128 +       }
23129 +
23130 +       return err;
23131 +}
23132 +
23133 +const char *au_optstr_wbr_create(int wbr_create)
23134 +{
23135 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
23136 +}
23137 +
23138 +static match_table_t au_wbr_copyup_policy = {
23139 +       {AuWbrCopyup_TDP, "tdp"},
23140 +       {AuWbrCopyup_TDP, "top-down-parent"},
23141 +       {AuWbrCopyup_BUP, "bup"},
23142 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
23143 +       {AuWbrCopyup_BU, "bu"},
23144 +       {AuWbrCopyup_BU, "bottom-up"},
23145 +       {-1, NULL}
23146 +};
23147 +
23148 +static int noinline_for_stack au_wbr_copyup_val(char *str)
23149 +{
23150 +       substring_t args[MAX_OPT_ARGS];
23151 +
23152 +       return match_token(str, au_wbr_copyup_policy, args);
23153 +}
23154 +
23155 +const char *au_optstr_wbr_copyup(int wbr_copyup)
23156 +{
23157 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
23158 +}
23159 +
23160 +/* ---------------------------------------------------------------------- */
23161 +
23162 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23163 +
23164 +static void dump_opts(struct au_opts *opts)
23165 +{
23166 +#ifdef CONFIG_AUFS_DEBUG
23167 +       /* reduce stack space */
23168 +       union {
23169 +               struct au_opt_add *add;
23170 +               struct au_opt_del *del;
23171 +               struct au_opt_mod *mod;
23172 +               struct au_opt_xino *xino;
23173 +               struct au_opt_xino_itrunc *xino_itrunc;
23174 +               struct au_opt_wbr_create *create;
23175 +       } u;
23176 +       struct au_opt *opt;
23177 +
23178 +       opt = opts->opt;
23179 +       while (opt->type != Opt_tail) {
23180 +               switch (opt->type) {
23181 +               case Opt_add:
23182 +                       u.add = &opt->add;
23183 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
23184 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23185 +                                 u.add->path.dentry);
23186 +                       break;
23187 +               case Opt_del:
23188 +               case Opt_idel:
23189 +                       u.del = &opt->del;
23190 +                       AuDbg("del {%s, %p}\n",
23191 +                             u.del->pathname, u.del->h_path.dentry);
23192 +                       break;
23193 +               case Opt_mod:
23194 +               case Opt_imod:
23195 +                       u.mod = &opt->mod;
23196 +                       AuDbg("mod {%s, 0x%x, %p}\n",
23197 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
23198 +                       break;
23199 +               case Opt_append:
23200 +                       u.add = &opt->add;
23201 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
23202 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23203 +                                 u.add->path.dentry);
23204 +                       break;
23205 +               case Opt_prepend:
23206 +                       u.add = &opt->add;
23207 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23208 +                                 u.add->bindex, u.add->pathname, u.add->perm,
23209 +                                 u.add->path.dentry);
23210 +                       break;
23211 +               case Opt_dirwh:
23212 +                       AuDbg("dirwh %d\n", opt->dirwh);
23213 +                       break;
23214 +               case Opt_rdcache:
23215 +                       AuDbg("rdcache %d\n", opt->rdcache);
23216 +                       break;
23217 +               case Opt_rdblk:
23218 +                       AuDbg("rdblk %u\n", opt->rdblk);
23219 +                       break;
23220 +               case Opt_rdblk_def:
23221 +                       AuDbg("rdblk_def\n");
23222 +                       break;
23223 +               case Opt_rdhash:
23224 +                       AuDbg("rdhash %u\n", opt->rdhash);
23225 +                       break;
23226 +               case Opt_rdhash_def:
23227 +                       AuDbg("rdhash_def\n");
23228 +                       break;
23229 +               case Opt_xino:
23230 +                       u.xino = &opt->xino;
23231 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
23232 +                       break;
23233 +               case Opt_trunc_xino:
23234 +                       AuLabel(trunc_xino);
23235 +                       break;
23236 +               case Opt_notrunc_xino:
23237 +                       AuLabel(notrunc_xino);
23238 +                       break;
23239 +               case Opt_trunc_xino_path:
23240 +               case Opt_itrunc_xino:
23241 +                       u.xino_itrunc = &opt->xino_itrunc;
23242 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23243 +                       break;
23244 +               case Opt_noxino:
23245 +                       AuLabel(noxino);
23246 +                       break;
23247 +               case Opt_trunc_xib:
23248 +                       AuLabel(trunc_xib);
23249 +                       break;
23250 +               case Opt_notrunc_xib:
23251 +                       AuLabel(notrunc_xib);
23252 +                       break;
23253 +               case Opt_shwh:
23254 +                       AuLabel(shwh);
23255 +                       break;
23256 +               case Opt_noshwh:
23257 +                       AuLabel(noshwh);
23258 +                       break;
23259 +               case Opt_dirperm1:
23260 +                       AuLabel(dirperm1);
23261 +                       break;
23262 +               case Opt_nodirperm1:
23263 +                       AuLabel(nodirperm1);
23264 +                       break;
23265 +               case Opt_plink:
23266 +                       AuLabel(plink);
23267 +                       break;
23268 +               case Opt_noplink:
23269 +                       AuLabel(noplink);
23270 +                       break;
23271 +               case Opt_list_plink:
23272 +                       AuLabel(list_plink);
23273 +                       break;
23274 +               case Opt_udba:
23275 +                       AuDbg("udba %d, %s\n",
23276 +                                 opt->udba, au_optstr_udba(opt->udba));
23277 +                       break;
23278 +               case Opt_dio:
23279 +                       AuLabel(dio);
23280 +                       break;
23281 +               case Opt_nodio:
23282 +                       AuLabel(nodio);
23283 +                       break;
23284 +               case Opt_diropq_a:
23285 +                       AuLabel(diropq_a);
23286 +                       break;
23287 +               case Opt_diropq_w:
23288 +                       AuLabel(diropq_w);
23289 +                       break;
23290 +               case Opt_warn_perm:
23291 +                       AuLabel(warn_perm);
23292 +                       break;
23293 +               case Opt_nowarn_perm:
23294 +                       AuLabel(nowarn_perm);
23295 +                       break;
23296 +               case Opt_verbose:
23297 +                       AuLabel(verbose);
23298 +                       break;
23299 +               case Opt_noverbose:
23300 +                       AuLabel(noverbose);
23301 +                       break;
23302 +               case Opt_sum:
23303 +                       AuLabel(sum);
23304 +                       break;
23305 +               case Opt_nosum:
23306 +                       AuLabel(nosum);
23307 +                       break;
23308 +               case Opt_wsum:
23309 +                       AuLabel(wsum);
23310 +                       break;
23311 +               case Opt_wbr_create:
23312 +                       u.create = &opt->wbr_create;
23313 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
23314 +                                 au_optstr_wbr_create(u.create->wbr_create));
23315 +                       switch (u.create->wbr_create) {
23316 +                       case AuWbrCreate_MFSV:
23317 +                       case AuWbrCreate_PMFSV:
23318 +                               AuDbg("%d sec\n", u.create->mfs_second);
23319 +                               break;
23320 +                       case AuWbrCreate_MFSRR:
23321 +                               AuDbg("%llu watermark\n",
23322 +                                         u.create->mfsrr_watermark);
23323 +                               break;
23324 +                       case AuWbrCreate_MFSRRV:
23325 +                       case AuWbrCreate_PMFSRRV:
23326 +                               AuDbg("%llu watermark, %d sec\n",
23327 +                                         u.create->mfsrr_watermark,
23328 +                                         u.create->mfs_second);
23329 +                               break;
23330 +                       }
23331 +                       break;
23332 +               case Opt_wbr_copyup:
23333 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23334 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
23335 +                       break;
23336 +               case Opt_fhsm_sec:
23337 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
23338 +                       break;
23339 +               case Opt_acl:
23340 +                       AuLabel(acl);
23341 +                       break;
23342 +               case Opt_noacl:
23343 +                       AuLabel(noacl);
23344 +                       break;
23345 +               default:
23346 +                       BUG();
23347 +               }
23348 +               opt++;
23349 +       }
23350 +#endif
23351 +}
23352 +
23353 +void au_opts_free(struct au_opts *opts)
23354 +{
23355 +       struct au_opt *opt;
23356 +
23357 +       opt = opts->opt;
23358 +       while (opt->type != Opt_tail) {
23359 +               switch (opt->type) {
23360 +               case Opt_add:
23361 +               case Opt_append:
23362 +               case Opt_prepend:
23363 +                       path_put(&opt->add.path);
23364 +                       break;
23365 +               case Opt_del:
23366 +               case Opt_idel:
23367 +                       path_put(&opt->del.h_path);
23368 +                       break;
23369 +               case Opt_mod:
23370 +               case Opt_imod:
23371 +                       dput(opt->mod.h_root);
23372 +                       break;
23373 +               case Opt_xino:
23374 +                       fput(opt->xino.file);
23375 +                       break;
23376 +               }
23377 +               opt++;
23378 +       }
23379 +}
23380 +
23381 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
23382 +                  aufs_bindex_t bindex)
23383 +{
23384 +       int err;
23385 +       struct au_opt_add *add = &opt->add;
23386 +       char *p;
23387 +
23388 +       add->bindex = bindex;
23389 +       add->perm = AuBrPerm_RO;
23390 +       add->pathname = opt_str;
23391 +       p = strchr(opt_str, '=');
23392 +       if (p) {
23393 +               *p++ = 0;
23394 +               if (*p)
23395 +                       add->perm = br_perm_val(p);
23396 +       }
23397 +
23398 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
23399 +       if (!err) {
23400 +               if (!p) {
23401 +                       add->perm = AuBrPerm_RO;
23402 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
23403 +                               add->perm = AuBrPerm_RR;
23404 +                       else if (!bindex && !(sb_flags & MS_RDONLY))
23405 +                               add->perm = AuBrPerm_RW;
23406 +               }
23407 +               opt->type = Opt_add;
23408 +               goto out;
23409 +       }
23410 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
23411 +       err = -EINVAL;
23412 +
23413 +out:
23414 +       return err;
23415 +}
23416 +
23417 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
23418 +{
23419 +       int err;
23420 +
23421 +       del->pathname = args[0].from;
23422 +       AuDbg("del path %s\n", del->pathname);
23423 +
23424 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
23425 +       if (unlikely(err))
23426 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
23427 +
23428 +       return err;
23429 +}
23430 +
23431 +#if 0 /* reserved for future use */
23432 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
23433 +                             struct au_opt_del *del, substring_t args[])
23434 +{
23435 +       int err;
23436 +       struct dentry *root;
23437 +
23438 +       err = -EINVAL;
23439 +       root = sb->s_root;
23440 +       aufs_read_lock(root, AuLock_FLUSH);
23441 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23442 +               pr_err("out of bounds, %d\n", bindex);
23443 +               goto out;
23444 +       }
23445 +
23446 +       err = 0;
23447 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
23448 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
23449 +
23450 +out:
23451 +       aufs_read_unlock(root, !AuLock_IR);
23452 +       return err;
23453 +}
23454 +#endif
23455 +
23456 +static int noinline_for_stack
23457 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
23458 +{
23459 +       int err;
23460 +       struct path path;
23461 +       char *p;
23462 +
23463 +       err = -EINVAL;
23464 +       mod->path = args[0].from;
23465 +       p = strchr(mod->path, '=');
23466 +       if (unlikely(!p)) {
23467 +               pr_err("no permssion %s\n", args[0].from);
23468 +               goto out;
23469 +       }
23470 +
23471 +       *p++ = 0;
23472 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
23473 +       if (unlikely(err)) {
23474 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
23475 +               goto out;
23476 +       }
23477 +
23478 +       mod->perm = br_perm_val(p);
23479 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23480 +       mod->h_root = dget(path.dentry);
23481 +       path_put(&path);
23482 +
23483 +out:
23484 +       return err;
23485 +}
23486 +
23487 +#if 0 /* reserved for future use */
23488 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23489 +                             struct au_opt_mod *mod, substring_t args[])
23490 +{
23491 +       int err;
23492 +       struct dentry *root;
23493 +
23494 +       err = -EINVAL;
23495 +       root = sb->s_root;
23496 +       aufs_read_lock(root, AuLock_FLUSH);
23497 +       if (bindex < 0 || au_sbend(sb) < bindex) {
23498 +               pr_err("out of bounds, %d\n", bindex);
23499 +               goto out;
23500 +       }
23501 +
23502 +       err = 0;
23503 +       mod->perm = br_perm_val(args[1].from);
23504 +       AuDbg("mod path %s, perm 0x%x, %s\n",
23505 +             mod->path, mod->perm, args[1].from);
23506 +       mod->h_root = dget(au_h_dptr(root, bindex));
23507 +
23508 +out:
23509 +       aufs_read_unlock(root, !AuLock_IR);
23510 +       return err;
23511 +}
23512 +#endif
23513 +
23514 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23515 +                             substring_t args[])
23516 +{
23517 +       int err;
23518 +       struct file *file;
23519 +
23520 +       file = au_xino_create(sb, args[0].from, /*silent*/0);
23521 +       err = PTR_ERR(file);
23522 +       if (IS_ERR(file))
23523 +               goto out;
23524 +
23525 +       err = -EINVAL;
23526 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
23527 +               fput(file);
23528 +               pr_err("%s must be outside\n", args[0].from);
23529 +               goto out;
23530 +       }
23531 +
23532 +       err = 0;
23533 +       xino->file = file;
23534 +       xino->path = args[0].from;
23535 +
23536 +out:
23537 +       return err;
23538 +}
23539 +
23540 +static int noinline_for_stack
23541 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
23542 +                              struct au_opt_xino_itrunc *xino_itrunc,
23543 +                              substring_t args[])
23544 +{
23545 +       int err;
23546 +       aufs_bindex_t bend, bindex;
23547 +       struct path path;
23548 +       struct dentry *root;
23549 +
23550 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23551 +       if (unlikely(err)) {
23552 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
23553 +               goto out;
23554 +       }
23555 +
23556 +       xino_itrunc->bindex = -1;
23557 +       root = sb->s_root;
23558 +       aufs_read_lock(root, AuLock_FLUSH);
23559 +       bend = au_sbend(sb);
23560 +       for (bindex = 0; bindex <= bend; bindex++) {
23561 +               if (au_h_dptr(root, bindex) == path.dentry) {
23562 +                       xino_itrunc->bindex = bindex;
23563 +                       break;
23564 +               }
23565 +       }
23566 +       aufs_read_unlock(root, !AuLock_IR);
23567 +       path_put(&path);
23568 +
23569 +       if (unlikely(xino_itrunc->bindex < 0)) {
23570 +               pr_err("no such branch %s\n", args[0].from);
23571 +               err = -EINVAL;
23572 +       }
23573 +
23574 +out:
23575 +       return err;
23576 +}
23577 +
23578 +/* called without aufs lock */
23579 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
23580 +{
23581 +       int err, n, token;
23582 +       aufs_bindex_t bindex;
23583 +       unsigned char skipped;
23584 +       struct dentry *root;
23585 +       struct au_opt *opt, *opt_tail;
23586 +       char *opt_str;
23587 +       /* reduce the stack space */
23588 +       union {
23589 +               struct au_opt_xino_itrunc *xino_itrunc;
23590 +               struct au_opt_wbr_create *create;
23591 +       } u;
23592 +       struct {
23593 +               substring_t args[MAX_OPT_ARGS];
23594 +       } *a;
23595 +
23596 +       err = -ENOMEM;
23597 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23598 +       if (unlikely(!a))
23599 +               goto out;
23600 +
23601 +       root = sb->s_root;
23602 +       err = 0;
23603 +       bindex = 0;
23604 +       opt = opts->opt;
23605 +       opt_tail = opt + opts->max_opt - 1;
23606 +       opt->type = Opt_tail;
23607 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
23608 +               err = -EINVAL;
23609 +               skipped = 0;
23610 +               token = match_token(opt_str, options, a->args);
23611 +               switch (token) {
23612 +               case Opt_br:
23613 +                       err = 0;
23614 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
23615 +                              && *opt_str) {
23616 +                               err = opt_add(opt, opt_str, opts->sb_flags,
23617 +                                             bindex++);
23618 +                               if (unlikely(!err && ++opt > opt_tail)) {
23619 +                                       err = -E2BIG;
23620 +                                       break;
23621 +                               }
23622 +                               opt->type = Opt_tail;
23623 +                               skipped = 1;
23624 +                       }
23625 +                       break;
23626 +               case Opt_add:
23627 +                       if (unlikely(match_int(&a->args[0], &n))) {
23628 +                               pr_err("bad integer in %s\n", opt_str);
23629 +                               break;
23630 +                       }
23631 +                       bindex = n;
23632 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
23633 +                                     bindex);
23634 +                       if (!err)
23635 +                               opt->type = token;
23636 +                       break;
23637 +               case Opt_append:
23638 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23639 +                                     /*dummy bindex*/1);
23640 +                       if (!err)
23641 +                               opt->type = token;
23642 +                       break;
23643 +               case Opt_prepend:
23644 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
23645 +                                     /*bindex*/0);
23646 +                       if (!err)
23647 +                               opt->type = token;
23648 +                       break;
23649 +               case Opt_del:
23650 +                       err = au_opts_parse_del(&opt->del, a->args);
23651 +                       if (!err)
23652 +                               opt->type = token;
23653 +                       break;
23654 +#if 0 /* reserved for future use */
23655 +               case Opt_idel:
23656 +                       del->pathname = "(indexed)";
23657 +                       if (unlikely(match_int(&args[0], &n))) {
23658 +                               pr_err("bad integer in %s\n", opt_str);
23659 +                               break;
23660 +                       }
23661 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
23662 +                       if (!err)
23663 +                               opt->type = token;
23664 +                       break;
23665 +#endif
23666 +               case Opt_mod:
23667 +                       err = au_opts_parse_mod(&opt->mod, a->args);
23668 +                       if (!err)
23669 +                               opt->type = token;
23670 +                       break;
23671 +#ifdef IMOD /* reserved for future use */
23672 +               case Opt_imod:
23673 +                       u.mod->path = "(indexed)";
23674 +                       if (unlikely(match_int(&a->args[0], &n))) {
23675 +                               pr_err("bad integer in %s\n", opt_str);
23676 +                               break;
23677 +                       }
23678 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
23679 +                       if (!err)
23680 +                               opt->type = token;
23681 +                       break;
23682 +#endif
23683 +               case Opt_xino:
23684 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
23685 +                       if (!err)
23686 +                               opt->type = token;
23687 +                       break;
23688 +
23689 +               case Opt_trunc_xino_path:
23690 +                       err = au_opts_parse_xino_itrunc_path
23691 +                               (sb, &opt->xino_itrunc, a->args);
23692 +                       if (!err)
23693 +                               opt->type = token;
23694 +                       break;
23695 +
23696 +               case Opt_itrunc_xino:
23697 +                       u.xino_itrunc = &opt->xino_itrunc;
23698 +                       if (unlikely(match_int(&a->args[0], &n))) {
23699 +                               pr_err("bad integer in %s\n", opt_str);
23700 +                               break;
23701 +                       }
23702 +                       u.xino_itrunc->bindex = n;
23703 +                       aufs_read_lock(root, AuLock_FLUSH);
23704 +                       if (n < 0 || au_sbend(sb) < n) {
23705 +                               pr_err("out of bounds, %d\n", n);
23706 +                               aufs_read_unlock(root, !AuLock_IR);
23707 +                               break;
23708 +                       }
23709 +                       aufs_read_unlock(root, !AuLock_IR);
23710 +                       err = 0;
23711 +                       opt->type = token;
23712 +                       break;
23713 +
23714 +               case Opt_dirwh:
23715 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
23716 +                               break;
23717 +                       err = 0;
23718 +                       opt->type = token;
23719 +                       break;
23720 +
23721 +               case Opt_rdcache:
23722 +                       if (unlikely(match_int(&a->args[0], &n))) {
23723 +                               pr_err("bad integer in %s\n", opt_str);
23724 +                               break;
23725 +                       }
23726 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
23727 +                               pr_err("rdcache must be smaller than %d\n",
23728 +                                      AUFS_RDCACHE_MAX);
23729 +                               break;
23730 +                       }
23731 +                       opt->rdcache = n;
23732 +                       err = 0;
23733 +                       opt->type = token;
23734 +                       break;
23735 +               case Opt_rdblk:
23736 +                       if (unlikely(match_int(&a->args[0], &n)
23737 +                                    || n < 0
23738 +                                    || n > KMALLOC_MAX_SIZE)) {
23739 +                               pr_err("bad integer in %s\n", opt_str);
23740 +                               break;
23741 +                       }
23742 +                       if (unlikely(n && n < NAME_MAX)) {
23743 +                               pr_err("rdblk must be larger than %d\n",
23744 +                                      NAME_MAX);
23745 +                               break;
23746 +                       }
23747 +                       opt->rdblk = n;
23748 +                       err = 0;
23749 +                       opt->type = token;
23750 +                       break;
23751 +               case Opt_rdhash:
23752 +                       if (unlikely(match_int(&a->args[0], &n)
23753 +                                    || n < 0
23754 +                                    || n * sizeof(struct hlist_head)
23755 +                                    > KMALLOC_MAX_SIZE)) {
23756 +                               pr_err("bad integer in %s\n", opt_str);
23757 +                               break;
23758 +                       }
23759 +                       opt->rdhash = n;
23760 +                       err = 0;
23761 +                       opt->type = token;
23762 +                       break;
23763 +
23764 +               case Opt_trunc_xino:
23765 +               case Opt_notrunc_xino:
23766 +               case Opt_noxino:
23767 +               case Opt_trunc_xib:
23768 +               case Opt_notrunc_xib:
23769 +               case Opt_shwh:
23770 +               case Opt_noshwh:
23771 +               case Opt_dirperm1:
23772 +               case Opt_nodirperm1:
23773 +               case Opt_plink:
23774 +               case Opt_noplink:
23775 +               case Opt_list_plink:
23776 +               case Opt_dio:
23777 +               case Opt_nodio:
23778 +               case Opt_diropq_a:
23779 +               case Opt_diropq_w:
23780 +               case Opt_warn_perm:
23781 +               case Opt_nowarn_perm:
23782 +               case Opt_verbose:
23783 +               case Opt_noverbose:
23784 +               case Opt_sum:
23785 +               case Opt_nosum:
23786 +               case Opt_wsum:
23787 +               case Opt_rdblk_def:
23788 +               case Opt_rdhash_def:
23789 +               case Opt_acl:
23790 +               case Opt_noacl:
23791 +                       err = 0;
23792 +                       opt->type = token;
23793 +                       break;
23794 +
23795 +               case Opt_udba:
23796 +                       opt->udba = udba_val(a->args[0].from);
23797 +                       if (opt->udba >= 0) {
23798 +                               err = 0;
23799 +                               opt->type = token;
23800 +                       } else
23801 +                               pr_err("wrong value, %s\n", opt_str);
23802 +                       break;
23803 +
23804 +               case Opt_wbr_create:
23805 +                       u.create = &opt->wbr_create;
23806 +                       u.create->wbr_create
23807 +                               = au_wbr_create_val(a->args[0].from, u.create);
23808 +                       if (u.create->wbr_create >= 0) {
23809 +                               err = 0;
23810 +                               opt->type = token;
23811 +                       } else
23812 +                               pr_err("wrong value, %s\n", opt_str);
23813 +                       break;
23814 +               case Opt_wbr_copyup:
23815 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
23816 +                       if (opt->wbr_copyup >= 0) {
23817 +                               err = 0;
23818 +                               opt->type = token;
23819 +                       } else
23820 +                               pr_err("wrong value, %s\n", opt_str);
23821 +                       break;
23822 +
23823 +               case Opt_fhsm_sec:
23824 +                       if (unlikely(match_int(&a->args[0], &n)
23825 +                                    || n < 0)) {
23826 +                               pr_err("bad integer in %s\n", opt_str);
23827 +                               break;
23828 +                       }
23829 +                       if (sysaufs_brs) {
23830 +                               opt->fhsm_second = n;
23831 +                               opt->type = token;
23832 +                       } else
23833 +                               pr_warn("ignored %s\n", opt_str);
23834 +                       err = 0;
23835 +                       break;
23836 +
23837 +               case Opt_ignore:
23838 +                       pr_warn("ignored %s\n", opt_str);
23839 +                       /*FALLTHROUGH*/
23840 +               case Opt_ignore_silent:
23841 +                       skipped = 1;
23842 +                       err = 0;
23843 +                       break;
23844 +               case Opt_err:
23845 +                       pr_err("unknown option %s\n", opt_str);
23846 +                       break;
23847 +               }
23848 +
23849 +               if (!err && !skipped) {
23850 +                       if (unlikely(++opt > opt_tail)) {
23851 +                               err = -E2BIG;
23852 +                               opt--;
23853 +                               opt->type = Opt_tail;
23854 +                               break;
23855 +                       }
23856 +                       opt->type = Opt_tail;
23857 +               }
23858 +       }
23859 +
23860 +       kfree(a);
23861 +       dump_opts(opts);
23862 +       if (unlikely(err))
23863 +               au_opts_free(opts);
23864 +
23865 +out:
23866 +       return err;
23867 +}
23868 +
23869 +static int au_opt_wbr_create(struct super_block *sb,
23870 +                            struct au_opt_wbr_create *create)
23871 +{
23872 +       int err;
23873 +       struct au_sbinfo *sbinfo;
23874 +
23875 +       SiMustWriteLock(sb);
23876 +
23877 +       err = 1; /* handled */
23878 +       sbinfo = au_sbi(sb);
23879 +       if (sbinfo->si_wbr_create_ops->fin) {
23880 +               err = sbinfo->si_wbr_create_ops->fin(sb);
23881 +               if (!err)
23882 +                       err = 1;
23883 +       }
23884 +
23885 +       sbinfo->si_wbr_create = create->wbr_create;
23886 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
23887 +       switch (create->wbr_create) {
23888 +       case AuWbrCreate_MFSRRV:
23889 +       case AuWbrCreate_MFSRR:
23890 +       case AuWbrCreate_PMFSRR:
23891 +       case AuWbrCreate_PMFSRRV:
23892 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
23893 +               /*FALLTHROUGH*/
23894 +       case AuWbrCreate_MFS:
23895 +       case AuWbrCreate_MFSV:
23896 +       case AuWbrCreate_PMFS:
23897 +       case AuWbrCreate_PMFSV:
23898 +               sbinfo->si_wbr_mfs.mfs_expire
23899 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
23900 +               break;
23901 +       }
23902 +
23903 +       if (sbinfo->si_wbr_create_ops->init)
23904 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
23905 +
23906 +       return err;
23907 +}
23908 +
23909 +/*
23910 + * returns,
23911 + * plus: processed without an error
23912 + * zero: unprocessed
23913 + */
23914 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
23915 +                        struct au_opts *opts)
23916 +{
23917 +       int err;
23918 +       struct au_sbinfo *sbinfo;
23919 +
23920 +       SiMustWriteLock(sb);
23921 +
23922 +       err = 1; /* handled */
23923 +       sbinfo = au_sbi(sb);
23924 +       switch (opt->type) {
23925 +       case Opt_udba:
23926 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
23927 +               sbinfo->si_mntflags |= opt->udba;
23928 +               opts->given_udba |= opt->udba;
23929 +               break;
23930 +
23931 +       case Opt_plink:
23932 +               au_opt_set(sbinfo->si_mntflags, PLINK);
23933 +               break;
23934 +       case Opt_noplink:
23935 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23936 +                       au_plink_put(sb, /*verbose*/1);
23937 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
23938 +               break;
23939 +       case Opt_list_plink:
23940 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
23941 +                       au_plink_list(sb);
23942 +               break;
23943 +
23944 +       case Opt_dio:
23945 +               au_opt_set(sbinfo->si_mntflags, DIO);
23946 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
23947 +               break;
23948 +       case Opt_nodio:
23949 +               au_opt_clr(sbinfo->si_mntflags, DIO);
23950 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
23951 +               break;
23952 +
23953 +       case Opt_fhsm_sec:
23954 +               au_fhsm_set(sbinfo, opt->fhsm_second);
23955 +               break;
23956 +
23957 +       case Opt_diropq_a:
23958 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
23959 +               break;
23960 +       case Opt_diropq_w:
23961 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
23962 +               break;
23963 +
23964 +       case Opt_warn_perm:
23965 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
23966 +               break;
23967 +       case Opt_nowarn_perm:
23968 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
23969 +               break;
23970 +
23971 +       case Opt_verbose:
23972 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
23973 +               break;
23974 +       case Opt_noverbose:
23975 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
23976 +               break;
23977 +
23978 +       case Opt_sum:
23979 +               au_opt_set(sbinfo->si_mntflags, SUM);
23980 +               break;
23981 +       case Opt_wsum:
23982 +               au_opt_clr(sbinfo->si_mntflags, SUM);
23983 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
23984 +       case Opt_nosum:
23985 +               au_opt_clr(sbinfo->si_mntflags, SUM);
23986 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
23987 +               break;
23988 +
23989 +       case Opt_wbr_create:
23990 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
23991 +               break;
23992 +       case Opt_wbr_copyup:
23993 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
23994 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
23995 +               break;
23996 +
23997 +       case Opt_dirwh:
23998 +               sbinfo->si_dirwh = opt->dirwh;
23999 +               break;
24000 +
24001 +       case Opt_rdcache:
24002 +               sbinfo->si_rdcache
24003 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
24004 +               break;
24005 +       case Opt_rdblk:
24006 +               sbinfo->si_rdblk = opt->rdblk;
24007 +               break;
24008 +       case Opt_rdblk_def:
24009 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24010 +               break;
24011 +       case Opt_rdhash:
24012 +               sbinfo->si_rdhash = opt->rdhash;
24013 +               break;
24014 +       case Opt_rdhash_def:
24015 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24016 +               break;
24017 +
24018 +       case Opt_shwh:
24019 +               au_opt_set(sbinfo->si_mntflags, SHWH);
24020 +               break;
24021 +       case Opt_noshwh:
24022 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
24023 +               break;
24024 +
24025 +       case Opt_dirperm1:
24026 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24027 +               break;
24028 +       case Opt_nodirperm1:
24029 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24030 +               break;
24031 +
24032 +       case Opt_trunc_xino:
24033 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24034 +               break;
24035 +       case Opt_notrunc_xino:
24036 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24037 +               break;
24038 +
24039 +       case Opt_trunc_xino_path:
24040 +       case Opt_itrunc_xino:
24041 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24042 +               if (!err)
24043 +                       err = 1;
24044 +               break;
24045 +
24046 +       case Opt_trunc_xib:
24047 +               au_fset_opts(opts->flags, TRUNC_XIB);
24048 +               break;
24049 +       case Opt_notrunc_xib:
24050 +               au_fclr_opts(opts->flags, TRUNC_XIB);
24051 +               break;
24052 +
24053 +       case Opt_acl:
24054 +               sb->s_flags |= MS_POSIXACL;
24055 +               break;
24056 +       case Opt_noacl:
24057 +               sb->s_flags &= ~MS_POSIXACL;
24058 +               break;
24059 +
24060 +       default:
24061 +               err = 0;
24062 +               break;
24063 +       }
24064 +
24065 +       return err;
24066 +}
24067 +
24068 +/*
24069 + * returns tri-state.
24070 + * plus: processed without an error
24071 + * zero: unprocessed
24072 + * minus: error
24073 + */
24074 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24075 +                    struct au_opts *opts)
24076 +{
24077 +       int err, do_refresh;
24078 +
24079 +       err = 0;
24080 +       switch (opt->type) {
24081 +       case Opt_append:
24082 +               opt->add.bindex = au_sbend(sb) + 1;
24083 +               if (opt->add.bindex < 0)
24084 +                       opt->add.bindex = 0;
24085 +               goto add;
24086 +       case Opt_prepend:
24087 +               opt->add.bindex = 0;
24088 +       add: /* indented label */
24089 +       case Opt_add:
24090 +               err = au_br_add(sb, &opt->add,
24091 +                               au_ftest_opts(opts->flags, REMOUNT));
24092 +               if (!err) {
24093 +                       err = 1;
24094 +                       au_fset_opts(opts->flags, REFRESH);
24095 +               }
24096 +               break;
24097 +
24098 +       case Opt_del:
24099 +       case Opt_idel:
24100 +               err = au_br_del(sb, &opt->del,
24101 +                               au_ftest_opts(opts->flags, REMOUNT));
24102 +               if (!err) {
24103 +                       err = 1;
24104 +                       au_fset_opts(opts->flags, TRUNC_XIB);
24105 +                       au_fset_opts(opts->flags, REFRESH);
24106 +               }
24107 +               break;
24108 +
24109 +       case Opt_mod:
24110 +       case Opt_imod:
24111 +               err = au_br_mod(sb, &opt->mod,
24112 +                               au_ftest_opts(opts->flags, REMOUNT),
24113 +                               &do_refresh);
24114 +               if (!err) {
24115 +                       err = 1;
24116 +                       if (do_refresh)
24117 +                               au_fset_opts(opts->flags, REFRESH);
24118 +               }
24119 +               break;
24120 +       }
24121 +
24122 +       return err;
24123 +}
24124 +
24125 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24126 +                      struct au_opt_xino **opt_xino,
24127 +                      struct au_opts *opts)
24128 +{
24129 +       int err;
24130 +       aufs_bindex_t bend, bindex;
24131 +       struct dentry *root, *parent, *h_root;
24132 +
24133 +       err = 0;
24134 +       switch (opt->type) {
24135 +       case Opt_xino:
24136 +               err = au_xino_set(sb, &opt->xino,
24137 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
24138 +               if (unlikely(err))
24139 +                       break;
24140 +
24141 +               *opt_xino = &opt->xino;
24142 +               au_xino_brid_set(sb, -1);
24143 +
24144 +               /* safe d_parent access */
24145 +               parent = opt->xino.file->f_path.dentry->d_parent;
24146 +               root = sb->s_root;
24147 +               bend = au_sbend(sb);
24148 +               for (bindex = 0; bindex <= bend; bindex++) {
24149 +                       h_root = au_h_dptr(root, bindex);
24150 +                       if (h_root == parent) {
24151 +                               au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24152 +                               break;
24153 +                       }
24154 +               }
24155 +               break;
24156 +
24157 +       case Opt_noxino:
24158 +               au_xino_clr(sb);
24159 +               au_xino_brid_set(sb, -1);
24160 +               *opt_xino = (void *)-1;
24161 +               break;
24162 +       }
24163 +
24164 +       return err;
24165 +}
24166 +
24167 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24168 +                  unsigned int pending)
24169 +{
24170 +       int err, fhsm;
24171 +       aufs_bindex_t bindex, bend;
24172 +       unsigned char do_plink, skip, do_free;
24173 +       struct au_branch *br;
24174 +       struct au_wbr *wbr;
24175 +       struct dentry *root;
24176 +       struct inode *dir, *h_dir;
24177 +       struct au_sbinfo *sbinfo;
24178 +       struct au_hinode *hdir;
24179 +
24180 +       SiMustAnyLock(sb);
24181 +
24182 +       sbinfo = au_sbi(sb);
24183 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24184 +
24185 +       if (!(sb_flags & MS_RDONLY)) {
24186 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
24187 +                       pr_warn("first branch should be rw\n");
24188 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
24189 +                       pr_warn("shwh should be used with ro\n");
24190 +       }
24191 +
24192 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
24193 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
24194 +               pr_warn("udba=*notify requires xino\n");
24195 +
24196 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
24197 +               pr_warn("dirperm1 breaks the protection"
24198 +                       " by the permission bits on the lower branch\n");
24199 +
24200 +       err = 0;
24201 +       fhsm = 0;
24202 +       root = sb->s_root;
24203 +       dir = d_inode(root);
24204 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
24205 +       bend = au_sbend(sb);
24206 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
24207 +               skip = 0;
24208 +               h_dir = au_h_iptr(dir, bindex);
24209 +               br = au_sbr(sb, bindex);
24210 +
24211 +               if ((br->br_perm & AuBrAttr_ICEX)
24212 +                   && !h_dir->i_op->listxattr)
24213 +                       br->br_perm &= ~AuBrAttr_ICEX;
24214 +#if 0
24215 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
24216 +                   && (au_br_sb(br)->s_flags & MS_NOSEC))
24217 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
24218 +#endif
24219 +
24220 +               do_free = 0;
24221 +               wbr = br->br_wbr;
24222 +               if (wbr)
24223 +                       wbr_wh_read_lock(wbr);
24224 +
24225 +               if (!au_br_writable(br->br_perm)) {
24226 +                       do_free = !!wbr;
24227 +                       skip = (!wbr
24228 +                               || (!wbr->wbr_whbase
24229 +                                   && !wbr->wbr_plink
24230 +                                   && !wbr->wbr_orph));
24231 +               } else if (!au_br_wh_linkable(br->br_perm)) {
24232 +                       /* skip = (!br->br_whbase && !br->br_orph); */
24233 +                       skip = (!wbr || !wbr->wbr_whbase);
24234 +                       if (skip && wbr) {
24235 +                               if (do_plink)
24236 +                                       skip = !!wbr->wbr_plink;
24237 +                               else
24238 +                                       skip = !wbr->wbr_plink;
24239 +                       }
24240 +               } else {
24241 +                       /* skip = (br->br_whbase && br->br_ohph); */
24242 +                       skip = (wbr && wbr->wbr_whbase);
24243 +                       if (skip) {
24244 +                               if (do_plink)
24245 +                                       skip = !!wbr->wbr_plink;
24246 +                               else
24247 +                                       skip = !wbr->wbr_plink;
24248 +                       }
24249 +               }
24250 +               if (wbr)
24251 +                       wbr_wh_read_unlock(wbr);
24252 +
24253 +               if (au_br_fhsm(br->br_perm)) {
24254 +                       fhsm++;
24255 +                       AuDebugOn(!br->br_fhsm);
24256 +               }
24257 +
24258 +               if (skip)
24259 +                       continue;
24260 +
24261 +               hdir = au_hi(dir, bindex);
24262 +               au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
24263 +               if (wbr)
24264 +                       wbr_wh_write_lock(wbr);
24265 +               err = au_wh_init(br, sb);
24266 +               if (wbr)
24267 +                       wbr_wh_write_unlock(wbr);
24268 +               au_hn_imtx_unlock(hdir);
24269 +
24270 +               if (!err && do_free) {
24271 +                       kfree(wbr);
24272 +                       br->br_wbr = NULL;
24273 +               }
24274 +       }
24275 +
24276 +       if (fhsm >= 2) {
24277 +               au_fset_si(sbinfo, FHSM);
24278 +               for (bindex = bend; bindex >= 0; bindex--) {
24279 +                       br = au_sbr(sb, bindex);
24280 +                       if (au_br_fhsm(br->br_perm)) {
24281 +                               au_fhsm_set_bottom(sb, bindex);
24282 +                               break;
24283 +                       }
24284 +               }
24285 +       } else {
24286 +               au_fclr_si(sbinfo, FHSM);
24287 +               au_fhsm_set_bottom(sb, -1);
24288 +       }
24289 +
24290 +       return err;
24291 +}
24292 +
24293 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24294 +{
24295 +       int err;
24296 +       unsigned int tmp;
24297 +       aufs_bindex_t bindex, bend;
24298 +       struct au_opt *opt;
24299 +       struct au_opt_xino *opt_xino, xino;
24300 +       struct au_sbinfo *sbinfo;
24301 +       struct au_branch *br;
24302 +       struct inode *dir;
24303 +
24304 +       SiMustWriteLock(sb);
24305 +
24306 +       err = 0;
24307 +       opt_xino = NULL;
24308 +       opt = opts->opt;
24309 +       while (err >= 0 && opt->type != Opt_tail)
24310 +               err = au_opt_simple(sb, opt++, opts);
24311 +       if (err > 0)
24312 +               err = 0;
24313 +       else if (unlikely(err < 0))
24314 +               goto out;
24315 +
24316 +       /* disable xino and udba temporary */
24317 +       sbinfo = au_sbi(sb);
24318 +       tmp = sbinfo->si_mntflags;
24319 +       au_opt_clr(sbinfo->si_mntflags, XINO);
24320 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
24321 +
24322 +       opt = opts->opt;
24323 +       while (err >= 0 && opt->type != Opt_tail)
24324 +               err = au_opt_br(sb, opt++, opts);
24325 +       if (err > 0)
24326 +               err = 0;
24327 +       else if (unlikely(err < 0))
24328 +               goto out;
24329 +
24330 +       bend = au_sbend(sb);
24331 +       if (unlikely(bend < 0)) {
24332 +               err = -EINVAL;
24333 +               pr_err("no branches\n");
24334 +               goto out;
24335 +       }
24336 +
24337 +       if (au_opt_test(tmp, XINO))
24338 +               au_opt_set(sbinfo->si_mntflags, XINO);
24339 +       opt = opts->opt;
24340 +       while (!err && opt->type != Opt_tail)
24341 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
24342 +       if (unlikely(err))
24343 +               goto out;
24344 +
24345 +       err = au_opts_verify(sb, sb->s_flags, tmp);
24346 +       if (unlikely(err))
24347 +               goto out;
24348 +
24349 +       /* restore xino */
24350 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
24351 +               xino.file = au_xino_def(sb);
24352 +               err = PTR_ERR(xino.file);
24353 +               if (IS_ERR(xino.file))
24354 +                       goto out;
24355 +
24356 +               err = au_xino_set(sb, &xino, /*remount*/0);
24357 +               fput(xino.file);
24358 +               if (unlikely(err))
24359 +                       goto out;
24360 +       }
24361 +
24362 +       /* restore udba */
24363 +       tmp &= AuOptMask_UDBA;
24364 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24365 +       sbinfo->si_mntflags |= tmp;
24366 +       bend = au_sbend(sb);
24367 +       for (bindex = 0; bindex <= bend; bindex++) {
24368 +               br = au_sbr(sb, bindex);
24369 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
24370 +               if (unlikely(err))
24371 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
24372 +                               bindex, err);
24373 +               /* go on even if err */
24374 +       }
24375 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
24376 +               dir = d_inode(sb->s_root);
24377 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
24378 +       }
24379 +
24380 +out:
24381 +       return err;
24382 +}
24383 +
24384 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
24385 +{
24386 +       int err, rerr;
24387 +       struct inode *dir;
24388 +       struct au_opt_xino *opt_xino;
24389 +       struct au_opt *opt;
24390 +       struct au_sbinfo *sbinfo;
24391 +
24392 +       SiMustWriteLock(sb);
24393 +
24394 +       dir = d_inode(sb->s_root);
24395 +       sbinfo = au_sbi(sb);
24396 +       err = 0;
24397 +       opt_xino = NULL;
24398 +       opt = opts->opt;
24399 +       while (err >= 0 && opt->type != Opt_tail) {
24400 +               err = au_opt_simple(sb, opt, opts);
24401 +               if (!err)
24402 +                       err = au_opt_br(sb, opt, opts);
24403 +               if (!err)
24404 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
24405 +               opt++;
24406 +       }
24407 +       if (err > 0)
24408 +               err = 0;
24409 +       AuTraceErr(err);
24410 +       /* go on even err */
24411 +
24412 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
24413 +       if (unlikely(rerr && !err))
24414 +               err = rerr;
24415 +
24416 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
24417 +               rerr = au_xib_trunc(sb);
24418 +               if (unlikely(rerr && !err))
24419 +                       err = rerr;
24420 +       }
24421 +
24422 +       /* will be handled by the caller */
24423 +       if (!au_ftest_opts(opts->flags, REFRESH)
24424 +           && (opts->given_udba || au_opt_test(sbinfo->si_mntflags, XINO)))
24425 +               au_fset_opts(opts->flags, REFRESH);
24426 +
24427 +       AuDbg("status 0x%x\n", opts->flags);
24428 +       return err;
24429 +}
24430 +
24431 +/* ---------------------------------------------------------------------- */
24432 +
24433 +unsigned int au_opt_udba(struct super_block *sb)
24434 +{
24435 +       return au_mntflags(sb) & AuOptMask_UDBA;
24436 +}
24437 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
24438 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
24439 +++ linux/fs/aufs/opts.h        2015-09-24 10:47:58.254719746 +0200
24440 @@ -0,0 +1,210 @@
24441 +/*
24442 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24443 + *
24444 + * This program, aufs is free software; you can redistribute it and/or modify
24445 + * it under the terms of the GNU General Public License as published by
24446 + * the Free Software Foundation; either version 2 of the License, or
24447 + * (at your option) any later version.
24448 + *
24449 + * This program is distributed in the hope that it will be useful,
24450 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24451 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24452 + * GNU General Public License for more details.
24453 + *
24454 + * You should have received a copy of the GNU General Public License
24455 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24456 + */
24457 +
24458 +/*
24459 + * mount options/flags
24460 + */
24461 +
24462 +#ifndef __AUFS_OPTS_H__
24463 +#define __AUFS_OPTS_H__
24464 +
24465 +#ifdef __KERNEL__
24466 +
24467 +#include <linux/path.h>
24468 +
24469 +struct file;
24470 +struct super_block;
24471 +
24472 +/* ---------------------------------------------------------------------- */
24473 +
24474 +/* mount flags */
24475 +#define AuOpt_XINO             1               /* external inode number bitmap
24476 +                                                  and translation table */
24477 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
24478 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
24479 +#define AuOpt_UDBA_REVAL       (1 << 3)
24480 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
24481 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
24482 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
24483 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
24484 +                                                  bits */
24485 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
24486 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
24487 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
24488 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
24489 +#define AuOpt_VERBOSE          (1 << 13)       /* busy inode when del-branch */
24490 +#define AuOpt_DIO              (1 << 14)       /* direct io */
24491 +
24492 +#ifndef CONFIG_AUFS_HNOTIFY
24493 +#undef AuOpt_UDBA_HNOTIFY
24494 +#define AuOpt_UDBA_HNOTIFY     0
24495 +#endif
24496 +#ifndef CONFIG_AUFS_SHWH
24497 +#undef AuOpt_SHWH
24498 +#define AuOpt_SHWH             0
24499 +#endif
24500 +
24501 +#define AuOpt_Def      (AuOpt_XINO \
24502 +                        | AuOpt_UDBA_REVAL \
24503 +                        | AuOpt_PLINK \
24504 +                        /* | AuOpt_DIRPERM1 */ \
24505 +                        | AuOpt_WARN_PERM)
24506 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24507 +                        | AuOpt_UDBA_REVAL \
24508 +                        | AuOpt_UDBA_HNOTIFY)
24509 +
24510 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
24511 +#define au_opt_set(flags, name) do { \
24512 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24513 +       ((flags) |= AuOpt_##name); \
24514 +} while (0)
24515 +#define au_opt_set_udba(flags, name) do { \
24516 +       (flags) &= ~AuOptMask_UDBA; \
24517 +       ((flags) |= AuOpt_##name); \
24518 +} while (0)
24519 +#define au_opt_clr(flags, name) do { \
24520 +       ((flags) &= ~AuOpt_##name); \
24521 +} while (0)
24522 +
24523 +static inline unsigned int au_opts_plink(unsigned int mntflags)
24524 +{
24525 +#ifdef CONFIG_PROC_FS
24526 +       return mntflags;
24527 +#else
24528 +       return mntflags & ~AuOpt_PLINK;
24529 +#endif
24530 +}
24531 +
24532 +/* ---------------------------------------------------------------------- */
24533 +
24534 +/* policies to select one among multiple writable branches */
24535 +enum {
24536 +       AuWbrCreate_TDP,        /* top down parent */
24537 +       AuWbrCreate_RR,         /* round robin */
24538 +       AuWbrCreate_MFS,        /* most free space */
24539 +       AuWbrCreate_MFSV,       /* mfs with seconds */
24540 +       AuWbrCreate_MFSRR,      /* mfs then rr */
24541 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
24542 +       AuWbrCreate_PMFS,       /* parent and mfs */
24543 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
24544 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
24545 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
24546 +
24547 +       AuWbrCreate_Def = AuWbrCreate_TDP
24548 +};
24549 +
24550 +enum {
24551 +       AuWbrCopyup_TDP,        /* top down parent */
24552 +       AuWbrCopyup_BUP,        /* bottom up parent */
24553 +       AuWbrCopyup_BU,         /* bottom up */
24554 +
24555 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
24556 +};
24557 +
24558 +/* ---------------------------------------------------------------------- */
24559 +
24560 +struct au_opt_add {
24561 +       aufs_bindex_t   bindex;
24562 +       char            *pathname;
24563 +       int             perm;
24564 +       struct path     path;
24565 +};
24566 +
24567 +struct au_opt_del {
24568 +       char            *pathname;
24569 +       struct path     h_path;
24570 +};
24571 +
24572 +struct au_opt_mod {
24573 +       char            *path;
24574 +       int             perm;
24575 +       struct dentry   *h_root;
24576 +};
24577 +
24578 +struct au_opt_xino {
24579 +       char            *path;
24580 +       struct file     *file;
24581 +};
24582 +
24583 +struct au_opt_xino_itrunc {
24584 +       aufs_bindex_t   bindex;
24585 +};
24586 +
24587 +struct au_opt_wbr_create {
24588 +       int                     wbr_create;
24589 +       int                     mfs_second;
24590 +       unsigned long long      mfsrr_watermark;
24591 +};
24592 +
24593 +struct au_opt {
24594 +       int type;
24595 +       union {
24596 +               struct au_opt_xino      xino;
24597 +               struct au_opt_xino_itrunc xino_itrunc;
24598 +               struct au_opt_add       add;
24599 +               struct au_opt_del       del;
24600 +               struct au_opt_mod       mod;
24601 +               int                     dirwh;
24602 +               int                     rdcache;
24603 +               unsigned int            rdblk;
24604 +               unsigned int            rdhash;
24605 +               int                     udba;
24606 +               struct au_opt_wbr_create wbr_create;
24607 +               int                     wbr_copyup;
24608 +               unsigned int            fhsm_second;
24609 +       };
24610 +};
24611 +
24612 +/* opts flags */
24613 +#define AuOpts_REMOUNT         1
24614 +#define AuOpts_REFRESH         (1 << 1)
24615 +#define AuOpts_TRUNC_XIB       (1 << 2)
24616 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
24617 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
24618 +#define au_fset_opts(flags, name) \
24619 +       do { (flags) |= AuOpts_##name; } while (0)
24620 +#define au_fclr_opts(flags, name) \
24621 +       do { (flags) &= ~AuOpts_##name; } while (0)
24622 +
24623 +struct au_opts {
24624 +       struct au_opt   *opt;
24625 +       int             max_opt;
24626 +
24627 +       unsigned int    given_udba;
24628 +       unsigned int    flags;
24629 +       unsigned long   sb_flags;
24630 +};
24631 +
24632 +/* ---------------------------------------------------------------------- */
24633 +
24634 +/* opts.c */
24635 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
24636 +const char *au_optstr_udba(int udba);
24637 +const char *au_optstr_wbr_copyup(int wbr_copyup);
24638 +const char *au_optstr_wbr_create(int wbr_create);
24639 +
24640 +void au_opts_free(struct au_opts *opts);
24641 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
24642 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24643 +                  unsigned int pending);
24644 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
24645 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
24646 +
24647 +unsigned int au_opt_udba(struct super_block *sb);
24648 +
24649 +#endif /* __KERNEL__ */
24650 +#endif /* __AUFS_OPTS_H__ */
24651 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
24652 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
24653 +++ linux/fs/aufs/plink.c       2015-09-24 10:47:58.254719746 +0200
24654 @@ -0,0 +1,528 @@
24655 +/*
24656 + * Copyright (C) 2005-2015 Junjiro R. Okajima
24657 + *
24658 + * This program, aufs is free software; you can redistribute it and/or modify
24659 + * it under the terms of the GNU General Public License as published by
24660 + * the Free Software Foundation; either version 2 of the License, or
24661 + * (at your option) any later version.
24662 + *
24663 + * This program is distributed in the hope that it will be useful,
24664 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24665 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24666 + * GNU General Public License for more details.
24667 + *
24668 + * You should have received a copy of the GNU General Public License
24669 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24670 + */
24671 +
24672 +/*
24673 + * pseudo-link
24674 + */
24675 +
24676 +#include "aufs.h"
24677 +
24678 +/*
24679 + * the pseudo-link maintenance mode.
24680 + * during a user process maintains the pseudo-links,
24681 + * prohibit adding a new plink and branch manipulation.
24682 + *
24683 + * Flags
24684 + * NOPLM:
24685 + *     For entry functions which will handle plink, and i_mutex is already held
24686 + *     in VFS.
24687 + *     They cannot wait and should return an error at once.
24688 + *     Callers has to check the error.
24689 + * NOPLMW:
24690 + *     For entry functions which will handle plink, but i_mutex is not held
24691 + *     in VFS.
24692 + *     They can wait the plink maintenance mode to finish.
24693 + *
24694 + * They behave like F_SETLK and F_SETLKW.
24695 + * If the caller never handle plink, then both flags are unnecessary.
24696 + */
24697 +
24698 +int au_plink_maint(struct super_block *sb, int flags)
24699 +{
24700 +       int err;
24701 +       pid_t pid, ppid;
24702 +       struct au_sbinfo *sbi;
24703 +
24704 +       SiMustAnyLock(sb);
24705 +
24706 +       err = 0;
24707 +       if (!au_opt_test(au_mntflags(sb), PLINK))
24708 +               goto out;
24709 +
24710 +       sbi = au_sbi(sb);
24711 +       pid = sbi->si_plink_maint_pid;
24712 +       if (!pid || pid == current->pid)
24713 +               goto out;
24714 +
24715 +       /* todo: it highly depends upon /sbin/mount.aufs */
24716 +       rcu_read_lock();
24717 +       ppid = task_pid_vnr(rcu_dereference(current->real_parent));
24718 +       rcu_read_unlock();
24719 +       if (pid == ppid)
24720 +               goto out;
24721 +
24722 +       if (au_ftest_lock(flags, NOPLMW)) {
24723 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
24724 +               /* AuDebugOn(!lockdep_depth(current)); */
24725 +               while (sbi->si_plink_maint_pid) {
24726 +                       si_read_unlock(sb);
24727 +                       /* gave up wake_up_bit() */
24728 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
24729 +
24730 +                       if (au_ftest_lock(flags, FLUSH))
24731 +                               au_nwt_flush(&sbi->si_nowait);
24732 +                       si_noflush_read_lock(sb);
24733 +               }
24734 +       } else if (au_ftest_lock(flags, NOPLM)) {
24735 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
24736 +               err = -EAGAIN;
24737 +       }
24738 +
24739 +out:
24740 +       return err;
24741 +}
24742 +
24743 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
24744 +{
24745 +       spin_lock(&sbinfo->si_plink_maint_lock);
24746 +       sbinfo->si_plink_maint_pid = 0;
24747 +       spin_unlock(&sbinfo->si_plink_maint_lock);
24748 +       wake_up_all(&sbinfo->si_plink_wq);
24749 +}
24750 +
24751 +int au_plink_maint_enter(struct super_block *sb)
24752 +{
24753 +       int err;
24754 +       struct au_sbinfo *sbinfo;
24755 +
24756 +       err = 0;
24757 +       sbinfo = au_sbi(sb);
24758 +       /* make sure i am the only one in this fs */
24759 +       si_write_lock(sb, AuLock_FLUSH);
24760 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
24761 +               spin_lock(&sbinfo->si_plink_maint_lock);
24762 +               if (!sbinfo->si_plink_maint_pid)
24763 +                       sbinfo->si_plink_maint_pid = current->pid;
24764 +               else
24765 +                       err = -EBUSY;
24766 +               spin_unlock(&sbinfo->si_plink_maint_lock);
24767 +       }
24768 +       si_write_unlock(sb);
24769 +
24770 +       return err;
24771 +}
24772 +
24773 +/* ---------------------------------------------------------------------- */
24774 +
24775 +#ifdef CONFIG_AUFS_DEBUG
24776 +void au_plink_list(struct super_block *sb)
24777 +{
24778 +       int i;
24779 +       struct au_sbinfo *sbinfo;
24780 +       struct hlist_head *plink_hlist;
24781 +       struct pseudo_link *plink;
24782 +
24783 +       SiMustAnyLock(sb);
24784 +
24785 +       sbinfo = au_sbi(sb);
24786 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
24787 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
24788 +
24789 +       for (i = 0; i < AuPlink_NHASH; i++) {
24790 +               plink_hlist = &sbinfo->si_plink[i].head;
24791 +               rcu_read_lock();
24792 +               hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24793 +                       AuDbg("%lu\n", plink->inode->i_ino);
24794 +               rcu_read_unlock();
24795 +       }
24796 +}
24797 +#endif
24798 +
24799 +/* is the inode pseudo-linked? */
24800 +int au_plink_test(struct inode *inode)
24801 +{
24802 +       int found, i;
24803 +       struct au_sbinfo *sbinfo;
24804 +       struct hlist_head *plink_hlist;
24805 +       struct pseudo_link *plink;
24806 +
24807 +       sbinfo = au_sbi(inode->i_sb);
24808 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
24809 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
24810 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24811 +
24812 +       found = 0;
24813 +       i = au_plink_hash(inode->i_ino);
24814 +       plink_hlist = &sbinfo->si_plink[i].head;
24815 +       rcu_read_lock();
24816 +       hlist_for_each_entry_rcu(plink, plink_hlist, hlist)
24817 +               if (plink->inode == inode) {
24818 +                       found = 1;
24819 +                       break;
24820 +               }
24821 +       rcu_read_unlock();
24822 +       return found;
24823 +}
24824 +
24825 +/* ---------------------------------------------------------------------- */
24826 +
24827 +/*
24828 + * generate a name for plink.
24829 + * the file will be stored under AUFS_WH_PLINKDIR.
24830 + */
24831 +/* 20 is max digits length of ulong 64 */
24832 +#define PLINK_NAME_LEN ((20 + 1) * 2)
24833 +
24834 +static int plink_name(char *name, int len, struct inode *inode,
24835 +                     aufs_bindex_t bindex)
24836 +{
24837 +       int rlen;
24838 +       struct inode *h_inode;
24839 +
24840 +       h_inode = au_h_iptr(inode, bindex);
24841 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
24842 +       return rlen;
24843 +}
24844 +
24845 +struct au_do_plink_lkup_args {
24846 +       struct dentry **errp;
24847 +       struct qstr *tgtname;
24848 +       struct dentry *h_parent;
24849 +       struct au_branch *br;
24850 +};
24851 +
24852 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
24853 +                                      struct dentry *h_parent,
24854 +                                      struct au_branch *br)
24855 +{
24856 +       struct dentry *h_dentry;
24857 +       struct mutex *h_mtx;
24858 +
24859 +       h_mtx = &d_inode(h_parent)->i_mutex;
24860 +       mutex_lock_nested(h_mtx, AuLsc_I_CHILD2);
24861 +       h_dentry = vfsub_lkup_one(tgtname, h_parent);
24862 +       mutex_unlock(h_mtx);
24863 +       return h_dentry;
24864 +}
24865 +
24866 +static void au_call_do_plink_lkup(void *args)
24867 +{
24868 +       struct au_do_plink_lkup_args *a = args;
24869 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
24870 +}
24871 +
24872 +/* lookup the plink-ed @inode under the branch at @bindex */
24873 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
24874 +{
24875 +       struct dentry *h_dentry, *h_parent;
24876 +       struct au_branch *br;
24877 +       int wkq_err;
24878 +       char a[PLINK_NAME_LEN];
24879 +       struct qstr tgtname = QSTR_INIT(a, 0);
24880 +
24881 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
24882 +
24883 +       br = au_sbr(inode->i_sb, bindex);
24884 +       h_parent = br->br_wbr->wbr_plink;
24885 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24886 +
24887 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24888 +               struct au_do_plink_lkup_args args = {
24889 +                       .errp           = &h_dentry,
24890 +                       .tgtname        = &tgtname,
24891 +                       .h_parent       = h_parent,
24892 +                       .br             = br
24893 +               };
24894 +
24895 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
24896 +               if (unlikely(wkq_err))
24897 +                       h_dentry = ERR_PTR(wkq_err);
24898 +       } else
24899 +               h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
24900 +
24901 +       return h_dentry;
24902 +}
24903 +
24904 +/* create a pseudo-link */
24905 +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
24906 +                     struct dentry *h_dentry, struct au_branch *br)
24907 +{
24908 +       int err;
24909 +       struct path h_path = {
24910 +               .mnt = au_br_mnt(br)
24911 +       };
24912 +       struct inode *h_dir, *delegated;
24913 +
24914 +       h_dir = d_inode(h_parent);
24915 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_CHILD2);
24916 +again:
24917 +       h_path.dentry = vfsub_lkup_one(tgt, h_parent);
24918 +       err = PTR_ERR(h_path.dentry);
24919 +       if (IS_ERR(h_path.dentry))
24920 +               goto out;
24921 +
24922 +       err = 0;
24923 +       /* wh.plink dir is not monitored */
24924 +       /* todo: is it really safe? */
24925 +       if (d_is_positive(h_path.dentry)
24926 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
24927 +               delegated = NULL;
24928 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
24929 +               if (unlikely(err == -EWOULDBLOCK)) {
24930 +                       pr_warn("cannot retry for NFSv4 delegation"
24931 +                               " for an internal unlink\n");
24932 +                       iput(delegated);
24933 +               }
24934 +               dput(h_path.dentry);
24935 +               h_path.dentry = NULL;
24936 +               if (!err)
24937 +                       goto again;
24938 +       }
24939 +       if (!err && d_is_negative(h_path.dentry)) {
24940 +               delegated = NULL;
24941 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
24942 +               if (unlikely(err == -EWOULDBLOCK)) {
24943 +                       pr_warn("cannot retry for NFSv4 delegation"
24944 +                               " for an internal link\n");
24945 +                       iput(delegated);
24946 +               }
24947 +       }
24948 +       dput(h_path.dentry);
24949 +
24950 +out:
24951 +       mutex_unlock(&h_dir->i_mutex);
24952 +       return err;
24953 +}
24954 +
24955 +struct do_whplink_args {
24956 +       int *errp;
24957 +       struct qstr *tgt;
24958 +       struct dentry *h_parent;
24959 +       struct dentry *h_dentry;
24960 +       struct au_branch *br;
24961 +};
24962 +
24963 +static void call_do_whplink(void *args)
24964 +{
24965 +       struct do_whplink_args *a = args;
24966 +       *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
24967 +}
24968 +
24969 +static int whplink(struct dentry *h_dentry, struct inode *inode,
24970 +                  aufs_bindex_t bindex, struct au_branch *br)
24971 +{
24972 +       int err, wkq_err;
24973 +       struct au_wbr *wbr;
24974 +       struct dentry *h_parent;
24975 +       char a[PLINK_NAME_LEN];
24976 +       struct qstr tgtname = QSTR_INIT(a, 0);
24977 +
24978 +       wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
24979 +       h_parent = wbr->wbr_plink;
24980 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
24981 +
24982 +       /* always superio. */
24983 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
24984 +               struct do_whplink_args args = {
24985 +                       .errp           = &err,
24986 +                       .tgt            = &tgtname,
24987 +                       .h_parent       = h_parent,
24988 +                       .h_dentry       = h_dentry,
24989 +                       .br             = br
24990 +               };
24991 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
24992 +               if (unlikely(wkq_err))
24993 +                       err = wkq_err;
24994 +       } else
24995 +               err = do_whplink(&tgtname, h_parent, h_dentry, br);
24996 +
24997 +       return err;
24998 +}
24999 +
25000 +/* free a single plink */
25001 +static void do_put_plink(struct pseudo_link *plink, int do_del)
25002 +{
25003 +       if (do_del)
25004 +               hlist_del(&plink->hlist);
25005 +       iput(plink->inode);
25006 +       kfree(plink);
25007 +}
25008 +
25009 +static void do_put_plink_rcu(struct rcu_head *rcu)
25010 +{
25011 +       struct pseudo_link *plink;
25012 +
25013 +       plink = container_of(rcu, struct pseudo_link, rcu);
25014 +       iput(plink->inode);
25015 +       kfree(plink);
25016 +}
25017 +
25018 +/*
25019 + * create a new pseudo-link for @h_dentry on @bindex.
25020 + * the linked inode is held in aufs @inode.
25021 + */
25022 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25023 +                    struct dentry *h_dentry)
25024 +{
25025 +       struct super_block *sb;
25026 +       struct au_sbinfo *sbinfo;
25027 +       struct hlist_head *plink_hlist;
25028 +       struct pseudo_link *plink, *tmp;
25029 +       struct au_sphlhead *sphl;
25030 +       int found, err, cnt, i;
25031 +
25032 +       sb = inode->i_sb;
25033 +       sbinfo = au_sbi(sb);
25034 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25035 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25036 +
25037 +       found = au_plink_test(inode);
25038 +       if (found)
25039 +               return;
25040 +
25041 +       i = au_plink_hash(inode->i_ino);
25042 +       sphl = sbinfo->si_plink + i;
25043 +       plink_hlist = &sphl->head;
25044 +       tmp = kmalloc(sizeof(*plink), GFP_NOFS);
25045 +       if (tmp)
25046 +               tmp->inode = au_igrab(inode);
25047 +       else {
25048 +               err = -ENOMEM;
25049 +               goto out;
25050 +       }
25051 +
25052 +       spin_lock(&sphl->spin);
25053 +       hlist_for_each_entry(plink, plink_hlist, hlist) {
25054 +               if (plink->inode == inode) {
25055 +                       found = 1;
25056 +                       break;
25057 +               }
25058 +       }
25059 +       if (!found)
25060 +               hlist_add_head_rcu(&tmp->hlist, plink_hlist);
25061 +       spin_unlock(&sphl->spin);
25062 +       if (!found) {
25063 +               cnt = au_sphl_count(sphl);
25064 +#define msg "unexpectedly unblanced or too many pseudo-links"
25065 +               if (cnt > AUFS_PLINK_WARN)
25066 +                       AuWarn1(msg ", %d\n", cnt);
25067 +#undef msg
25068 +               err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
25069 +       } else {
25070 +               do_put_plink(tmp, 0);
25071 +               return;
25072 +       }
25073 +
25074 +out:
25075 +       if (unlikely(err)) {
25076 +               pr_warn("err %d, damaged pseudo link.\n", err);
25077 +               if (tmp) {
25078 +                       au_sphl_del_rcu(&tmp->hlist, sphl);
25079 +                       call_rcu(&tmp->rcu, do_put_plink_rcu);
25080 +               }
25081 +       }
25082 +}
25083 +
25084 +/* free all plinks */
25085 +void au_plink_put(struct super_block *sb, int verbose)
25086 +{
25087 +       int i, warned;
25088 +       struct au_sbinfo *sbinfo;
25089 +       struct hlist_head *plink_hlist;
25090 +       struct hlist_node *tmp;
25091 +       struct pseudo_link *plink;
25092 +
25093 +       SiMustWriteLock(sb);
25094 +
25095 +       sbinfo = au_sbi(sb);
25096 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25097 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25098 +
25099 +       /* no spin_lock since sbinfo is write-locked */
25100 +       warned = 0;
25101 +       for (i = 0; i < AuPlink_NHASH; i++) {
25102 +               plink_hlist = &sbinfo->si_plink[i].head;
25103 +               if (!warned && verbose && !hlist_empty(plink_hlist)) {
25104 +                       pr_warn("pseudo-link is not flushed");
25105 +                       warned = 1;
25106 +               }
25107 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist)
25108 +                       do_put_plink(plink, 0);
25109 +               INIT_HLIST_HEAD(plink_hlist);
25110 +       }
25111 +}
25112 +
25113 +void au_plink_clean(struct super_block *sb, int verbose)
25114 +{
25115 +       struct dentry *root;
25116 +
25117 +       root = sb->s_root;
25118 +       aufs_write_lock(root);
25119 +       if (au_opt_test(au_mntflags(sb), PLINK))
25120 +               au_plink_put(sb, verbose);
25121 +       aufs_write_unlock(root);
25122 +}
25123 +
25124 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25125 +{
25126 +       int do_put;
25127 +       aufs_bindex_t bstart, bend, bindex;
25128 +
25129 +       do_put = 0;
25130 +       bstart = au_ibstart(inode);
25131 +       bend = au_ibend(inode);
25132 +       if (bstart >= 0) {
25133 +               for (bindex = bstart; bindex <= bend; bindex++) {
25134 +                       if (!au_h_iptr(inode, bindex)
25135 +                           || au_ii_br_id(inode, bindex) != br_id)
25136 +                               continue;
25137 +                       au_set_h_iptr(inode, bindex, NULL, 0);
25138 +                       do_put = 1;
25139 +                       break;
25140 +               }
25141 +               if (do_put)
25142 +                       for (bindex = bstart; bindex <= bend; bindex++)
25143 +                               if (au_h_iptr(inode, bindex)) {
25144 +                                       do_put = 0;
25145 +                                       break;
25146 +                               }
25147 +       } else
25148 +               do_put = 1;
25149 +
25150 +       return do_put;
25151 +}
25152 +
25153 +/* free the plinks on a branch specified by @br_id */
25154 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25155 +{
25156 +       struct au_sbinfo *sbinfo;
25157 +       struct hlist_head *plink_hlist;
25158 +       struct hlist_node *tmp;
25159 +       struct pseudo_link *plink;
25160 +       struct inode *inode;
25161 +       int i, do_put;
25162 +
25163 +       SiMustWriteLock(sb);
25164 +
25165 +       sbinfo = au_sbi(sb);
25166 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
25167 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
25168 +
25169 +       /* no spin_lock since sbinfo is write-locked */
25170 +       for (i = 0; i < AuPlink_NHASH; i++) {
25171 +               plink_hlist = &sbinfo->si_plink[i].head;
25172 +               hlist_for_each_entry_safe(plink, tmp, plink_hlist, hlist) {
25173 +                       inode = au_igrab(plink->inode);
25174 +                       ii_write_lock_child(inode);
25175 +                       do_put = au_plink_do_half_refresh(inode, br_id);
25176 +                       if (do_put)
25177 +                               do_put_plink(plink, 1);
25178 +                       ii_write_unlock(inode);
25179 +                       iput(inode);
25180 +               }
25181 +       }
25182 +}
25183 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25184 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
25185 +++ linux/fs/aufs/poll.c        2015-09-24 10:47:58.254719746 +0200
25186 @@ -0,0 +1,52 @@
25187 +/*
25188 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25189 + *
25190 + * This program, aufs is free software; you can redistribute it and/or modify
25191 + * it under the terms of the GNU General Public License as published by
25192 + * the Free Software Foundation; either version 2 of the License, or
25193 + * (at your option) any later version.
25194 + *
25195 + * This program is distributed in the hope that it will be useful,
25196 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25197 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25198 + * GNU General Public License for more details.
25199 + *
25200 + * You should have received a copy of the GNU General Public License
25201 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25202 + */
25203 +
25204 +/*
25205 + * poll operation
25206 + * There is only one filesystem which implements ->poll operation, currently.
25207 + */
25208 +
25209 +#include "aufs.h"
25210 +
25211 +unsigned int aufs_poll(struct file *file, poll_table *wait)
25212 +{
25213 +       unsigned int mask;
25214 +       int err;
25215 +       struct file *h_file;
25216 +       struct super_block *sb;
25217 +
25218 +       /* We should pretend an error happened. */
25219 +       mask = POLLERR /* | POLLIN | POLLOUT */;
25220 +       sb = file->f_path.dentry->d_sb;
25221 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
25222 +
25223 +       h_file = au_read_pre(file, /*keep_fi*/0);
25224 +       err = PTR_ERR(h_file);
25225 +       if (IS_ERR(h_file))
25226 +               goto out;
25227 +
25228 +       /* it is not an error if h_file has no operation */
25229 +       mask = DEFAULT_POLLMASK;
25230 +       if (h_file->f_op->poll)
25231 +               mask = h_file->f_op->poll(h_file, wait);
25232 +       fput(h_file); /* instead of au_read_post() */
25233 +
25234 +out:
25235 +       si_read_unlock(sb);
25236 +       AuTraceErr((int)mask);
25237 +       return mask;
25238 +}
25239 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25240 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
25241 +++ linux/fs/aufs/posix_acl.c   2015-09-24 10:47:58.254719746 +0200
25242 @@ -0,0 +1,99 @@
25243 +/*
25244 + * Copyright (C) 2014-2015 Junjiro R. Okajima
25245 + *
25246 + * This program, aufs is free software; you can redistribute it and/or modify
25247 + * it under the terms of the GNU General Public License as published by
25248 + * the Free Software Foundation; either version 2 of the License, or
25249 + * (at your option) any later version.
25250 + *
25251 + * This program is distributed in the hope that it will be useful,
25252 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25253 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25254 + * GNU General Public License for more details.
25255 + *
25256 + * You should have received a copy of the GNU General Public License
25257 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25258 + */
25259 +
25260 +/*
25261 + * posix acl operations
25262 + */
25263 +
25264 +#include <linux/fs.h>
25265 +#include <linux/posix_acl.h>
25266 +#include "aufs.h"
25267 +
25268 +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25269 +{
25270 +       struct posix_acl *acl;
25271 +       int err;
25272 +       aufs_bindex_t bindex;
25273 +       struct inode *h_inode;
25274 +       struct super_block *sb;
25275 +
25276 +       acl = NULL;
25277 +       sb = inode->i_sb;
25278 +       si_read_lock(sb, AuLock_FLUSH);
25279 +       ii_read_lock_child(inode);
25280 +       if (!(sb->s_flags & MS_POSIXACL))
25281 +               goto out;
25282 +
25283 +       bindex = au_ibstart(inode);
25284 +       h_inode = au_h_iptr(inode, bindex);
25285 +       if (unlikely(!h_inode
25286 +                    || ((h_inode->i_mode & S_IFMT)
25287 +                        != (inode->i_mode & S_IFMT)))) {
25288 +               err = au_busy_or_stale();
25289 +               acl = ERR_PTR(err);
25290 +               goto out;
25291 +       }
25292 +
25293 +       /* always topmost only */
25294 +       acl = get_acl(h_inode, type);
25295 +
25296 +out:
25297 +       ii_read_unlock(inode);
25298 +       si_read_unlock(sb);
25299 +
25300 +       AuTraceErrPtr(acl);
25301 +       return acl;
25302 +}
25303 +
25304 +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25305 +{
25306 +       int err;
25307 +       ssize_t ssz;
25308 +       struct dentry *dentry;
25309 +       struct au_srxattr arg = {
25310 +               .type = AU_ACL_SET,
25311 +               .u.acl_set = {
25312 +                       .acl    = acl,
25313 +                       .type   = type
25314 +               },
25315 +       };
25316 +
25317 +       mutex_lock(&inode->i_mutex);
25318 +       if (inode->i_ino == AUFS_ROOT_INO)
25319 +               dentry = dget(inode->i_sb->s_root);
25320 +       else {
25321 +               dentry = d_find_alias(inode);
25322 +               if (!dentry)
25323 +                       dentry = d_find_any_alias(inode);
25324 +               if (!dentry) {
25325 +                       pr_warn("cannot handle this inode, "
25326 +                               "please report to aufs-users ML\n");
25327 +                       err = -ENOENT;
25328 +                       goto out;
25329 +               }
25330 +       }
25331 +
25332 +       ssz = au_srxattr(dentry, &arg);
25333 +       dput(dentry);
25334 +       err = ssz;
25335 +       if (ssz >= 0)
25336 +               err = 0;
25337 +
25338 +out:
25339 +       mutex_unlock(&inode->i_mutex);
25340 +       return err;
25341 +}
25342 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
25343 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
25344 +++ linux/fs/aufs/procfs.c      2015-09-24 10:47:58.254719746 +0200
25345 @@ -0,0 +1,169 @@
25346 +/*
25347 + * Copyright (C) 2010-2015 Junjiro R. Okajima
25348 + *
25349 + * This program, aufs is free software; you can redistribute it and/or modify
25350 + * it under the terms of the GNU General Public License as published by
25351 + * the Free Software Foundation; either version 2 of the License, or
25352 + * (at your option) any later version.
25353 + *
25354 + * This program is distributed in the hope that it will be useful,
25355 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25356 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25357 + * GNU General Public License for more details.
25358 + *
25359 + * You should have received a copy of the GNU General Public License
25360 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25361 + */
25362 +
25363 +/*
25364 + * procfs interfaces
25365 + */
25366 +
25367 +#include <linux/proc_fs.h>
25368 +#include "aufs.h"
25369 +
25370 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
25371 +{
25372 +       struct au_sbinfo *sbinfo;
25373 +
25374 +       sbinfo = file->private_data;
25375 +       if (sbinfo) {
25376 +               au_plink_maint_leave(sbinfo);
25377 +               kobject_put(&sbinfo->si_kobj);
25378 +       }
25379 +
25380 +       return 0;
25381 +}
25382 +
25383 +static void au_procfs_plm_write_clean(struct file *file)
25384 +{
25385 +       struct au_sbinfo *sbinfo;
25386 +
25387 +       sbinfo = file->private_data;
25388 +       if (sbinfo)
25389 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
25390 +}
25391 +
25392 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
25393 +{
25394 +       int err;
25395 +       struct super_block *sb;
25396 +       struct au_sbinfo *sbinfo;
25397 +
25398 +       err = -EBUSY;
25399 +       if (unlikely(file->private_data))
25400 +               goto out;
25401 +
25402 +       sb = NULL;
25403 +       /* don't use au_sbilist_lock() here */
25404 +       spin_lock(&au_sbilist.spin);
25405 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
25406 +               if (id == sysaufs_si_id(sbinfo)) {
25407 +                       kobject_get(&sbinfo->si_kobj);
25408 +                       sb = sbinfo->si_sb;
25409 +                       break;
25410 +               }
25411 +       spin_unlock(&au_sbilist.spin);
25412 +
25413 +       err = -EINVAL;
25414 +       if (unlikely(!sb))
25415 +               goto out;
25416 +
25417 +       err = au_plink_maint_enter(sb);
25418 +       if (!err)
25419 +               /* keep kobject_get() */
25420 +               file->private_data = sbinfo;
25421 +       else
25422 +               kobject_put(&sbinfo->si_kobj);
25423 +out:
25424 +       return err;
25425 +}
25426 +
25427 +/*
25428 + * Accept a valid "si=xxxx" only.
25429 + * Once it is accepted successfully, accept "clean" too.
25430 + */
25431 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
25432 +                                  size_t count, loff_t *ppos)
25433 +{
25434 +       ssize_t err;
25435 +       unsigned long id;
25436 +       /* last newline is allowed */
25437 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
25438 +
25439 +       err = -EACCES;
25440 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25441 +               goto out;
25442 +
25443 +       err = -EINVAL;
25444 +       if (unlikely(count > sizeof(buf)))
25445 +               goto out;
25446 +
25447 +       err = copy_from_user(buf, ubuf, count);
25448 +       if (unlikely(err)) {
25449 +               err = -EFAULT;
25450 +               goto out;
25451 +       }
25452 +       buf[count] = 0;
25453 +
25454 +       err = -EINVAL;
25455 +       if (!strcmp("clean", buf)) {
25456 +               au_procfs_plm_write_clean(file);
25457 +               goto out_success;
25458 +       } else if (unlikely(strncmp("si=", buf, 3)))
25459 +               goto out;
25460 +
25461 +       err = kstrtoul(buf + 3, 16, &id);
25462 +       if (unlikely(err))
25463 +               goto out;
25464 +
25465 +       err = au_procfs_plm_write_si(file, id);
25466 +       if (unlikely(err))
25467 +               goto out;
25468 +
25469 +out_success:
25470 +       err = count; /* success */
25471 +out:
25472 +       return err;
25473 +}
25474 +
25475 +static const struct file_operations au_procfs_plm_fop = {
25476 +       .write          = au_procfs_plm_write,
25477 +       .release        = au_procfs_plm_release,
25478 +       .owner          = THIS_MODULE
25479 +};
25480 +
25481 +/* ---------------------------------------------------------------------- */
25482 +
25483 +static struct proc_dir_entry *au_procfs_dir;
25484 +
25485 +void au_procfs_fin(void)
25486 +{
25487 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25488 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25489 +}
25490 +
25491 +int __init au_procfs_init(void)
25492 +{
25493 +       int err;
25494 +       struct proc_dir_entry *entry;
25495 +
25496 +       err = -ENOMEM;
25497 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25498 +       if (unlikely(!au_procfs_dir))
25499 +               goto out;
25500 +
25501 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25502 +                           au_procfs_dir, &au_procfs_plm_fop);
25503 +       if (unlikely(!entry))
25504 +               goto out_dir;
25505 +
25506 +       err = 0;
25507 +       goto out; /* success */
25508 +
25509 +
25510 +out_dir:
25511 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25512 +out:
25513 +       return err;
25514 +}
25515 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25516 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
25517 +++ linux/fs/aufs/rdu.c 2015-09-24 10:47:58.254719746 +0200
25518 @@ -0,0 +1,388 @@
25519 +/*
25520 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25521 + *
25522 + * This program, aufs is free software; you can redistribute it and/or modify
25523 + * it under the terms of the GNU General Public License as published by
25524 + * the Free Software Foundation; either version 2 of the License, or
25525 + * (at your option) any later version.
25526 + *
25527 + * This program is distributed in the hope that it will be useful,
25528 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25529 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25530 + * GNU General Public License for more details.
25531 + *
25532 + * You should have received a copy of the GNU General Public License
25533 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25534 + */
25535 +
25536 +/*
25537 + * readdir in userspace.
25538 + */
25539 +
25540 +#include <linux/compat.h>
25541 +#include <linux/fs_stack.h>
25542 +#include <linux/security.h>
25543 +#include "aufs.h"
25544 +
25545 +/* bits for struct aufs_rdu.flags */
25546 +#define        AuRdu_CALLED    1
25547 +#define        AuRdu_CONT      (1 << 1)
25548 +#define        AuRdu_FULL      (1 << 2)
25549 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
25550 +#define au_fset_rdu(flags, name) \
25551 +       do { (flags) |= AuRdu_##name; } while (0)
25552 +#define au_fclr_rdu(flags, name) \
25553 +       do { (flags) &= ~AuRdu_##name; } while (0)
25554 +
25555 +struct au_rdu_arg {
25556 +       struct dir_context              ctx;
25557 +       struct aufs_rdu                 *rdu;
25558 +       union au_rdu_ent_ul             ent;
25559 +       unsigned long                   end;
25560 +
25561 +       struct super_block              *sb;
25562 +       int                             err;
25563 +};
25564 +
25565 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
25566 +                      loff_t offset, u64 h_ino, unsigned int d_type)
25567 +{
25568 +       int err, len;
25569 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
25570 +       struct aufs_rdu *rdu = arg->rdu;
25571 +       struct au_rdu_ent ent;
25572 +
25573 +       err = 0;
25574 +       arg->err = 0;
25575 +       au_fset_rdu(rdu->cookie.flags, CALLED);
25576 +       len = au_rdu_len(nlen);
25577 +       if (arg->ent.ul + len  < arg->end) {
25578 +               ent.ino = h_ino;
25579 +               ent.bindex = rdu->cookie.bindex;
25580 +               ent.type = d_type;
25581 +               ent.nlen = nlen;
25582 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
25583 +                       ent.type = DT_UNKNOWN;
25584 +
25585 +               /* unnecessary to support mmap_sem since this is a dir */
25586 +               err = -EFAULT;
25587 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
25588 +                       goto out;
25589 +               if (copy_to_user(arg->ent.e->name, name, nlen))
25590 +                       goto out;
25591 +               /* the terminating NULL */
25592 +               if (__put_user(0, arg->ent.e->name + nlen))
25593 +                       goto out;
25594 +               err = 0;
25595 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
25596 +               arg->ent.ul += len;
25597 +               rdu->rent++;
25598 +       } else {
25599 +               err = -EFAULT;
25600 +               au_fset_rdu(rdu->cookie.flags, FULL);
25601 +               rdu->full = 1;
25602 +               rdu->tail = arg->ent;
25603 +       }
25604 +
25605 +out:
25606 +       /* AuTraceErr(err); */
25607 +       return err;
25608 +}
25609 +
25610 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
25611 +{
25612 +       int err;
25613 +       loff_t offset;
25614 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
25615 +
25616 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
25617 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
25618 +       err = offset;
25619 +       if (unlikely(offset != cookie->h_pos))
25620 +               goto out;
25621 +
25622 +       err = 0;
25623 +       do {
25624 +               arg->err = 0;
25625 +               au_fclr_rdu(cookie->flags, CALLED);
25626 +               /* smp_mb(); */
25627 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
25628 +               if (err >= 0)
25629 +                       err = arg->err;
25630 +       } while (!err
25631 +                && au_ftest_rdu(cookie->flags, CALLED)
25632 +                && !au_ftest_rdu(cookie->flags, FULL));
25633 +       cookie->h_pos = h_file->f_pos;
25634 +
25635 +out:
25636 +       AuTraceErr(err);
25637 +       return err;
25638 +}
25639 +
25640 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
25641 +{
25642 +       int err;
25643 +       aufs_bindex_t bend;
25644 +       struct au_rdu_arg arg = {
25645 +               .ctx = {
25646 +                       .actor = au_rdu_fill
25647 +               }
25648 +       };
25649 +       struct dentry *dentry;
25650 +       struct inode *inode;
25651 +       struct file *h_file;
25652 +       struct au_rdu_cookie *cookie = &rdu->cookie;
25653 +
25654 +       err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
25655 +       if (unlikely(err)) {
25656 +               err = -EFAULT;
25657 +               AuTraceErr(err);
25658 +               goto out;
25659 +       }
25660 +       rdu->rent = 0;
25661 +       rdu->tail = rdu->ent;
25662 +       rdu->full = 0;
25663 +       arg.rdu = rdu;
25664 +       arg.ent = rdu->ent;
25665 +       arg.end = arg.ent.ul;
25666 +       arg.end += rdu->sz;
25667 +
25668 +       err = -ENOTDIR;
25669 +       if (unlikely(!file->f_op->iterate))
25670 +               goto out;
25671 +
25672 +       err = security_file_permission(file, MAY_READ);
25673 +       AuTraceErr(err);
25674 +       if (unlikely(err))
25675 +               goto out;
25676 +
25677 +       dentry = file->f_path.dentry;
25678 +       inode = d_inode(dentry);
25679 +#if 1
25680 +       mutex_lock(&inode->i_mutex);
25681 +#else
25682 +       err = mutex_lock_killable(&inode->i_mutex);
25683 +       AuTraceErr(err);
25684 +       if (unlikely(err))
25685 +               goto out;
25686 +#endif
25687 +
25688 +       arg.sb = inode->i_sb;
25689 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
25690 +       if (unlikely(err))
25691 +               goto out_mtx;
25692 +       err = au_alive_dir(dentry);
25693 +       if (unlikely(err))
25694 +               goto out_si;
25695 +       /* todo: reval? */
25696 +       fi_read_lock(file);
25697 +
25698 +       err = -EAGAIN;
25699 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
25700 +                    && cookie->generation != au_figen(file)))
25701 +               goto out_unlock;
25702 +
25703 +       err = 0;
25704 +       if (!rdu->blk) {
25705 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
25706 +               if (!rdu->blk)
25707 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
25708 +       }
25709 +       bend = au_fbstart(file);
25710 +       if (cookie->bindex < bend)
25711 +               cookie->bindex = bend;
25712 +       bend = au_fbend_dir(file);
25713 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bend); */
25714 +       for (; !err && cookie->bindex <= bend;
25715 +            cookie->bindex++, cookie->h_pos = 0) {
25716 +               h_file = au_hf_dir(file, cookie->bindex);
25717 +               if (!h_file)
25718 +                       continue;
25719 +
25720 +               au_fclr_rdu(cookie->flags, FULL);
25721 +               err = au_rdu_do(h_file, &arg);
25722 +               AuTraceErr(err);
25723 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
25724 +                       break;
25725 +       }
25726 +       AuDbg("rent %llu\n", rdu->rent);
25727 +
25728 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
25729 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
25730 +               au_fset_rdu(cookie->flags, CONT);
25731 +               cookie->generation = au_figen(file);
25732 +       }
25733 +
25734 +       ii_read_lock_child(inode);
25735 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibstart(inode)));
25736 +       ii_read_unlock(inode);
25737 +
25738 +out_unlock:
25739 +       fi_read_unlock(file);
25740 +out_si:
25741 +       si_read_unlock(arg.sb);
25742 +out_mtx:
25743 +       mutex_unlock(&inode->i_mutex);
25744 +out:
25745 +       AuTraceErr(err);
25746 +       return err;
25747 +}
25748 +
25749 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
25750 +{
25751 +       int err;
25752 +       ino_t ino;
25753 +       unsigned long long nent;
25754 +       union au_rdu_ent_ul *u;
25755 +       struct au_rdu_ent ent;
25756 +       struct super_block *sb;
25757 +
25758 +       err = 0;
25759 +       nent = rdu->nent;
25760 +       u = &rdu->ent;
25761 +       sb = file->f_path.dentry->d_sb;
25762 +       si_read_lock(sb, AuLock_FLUSH);
25763 +       while (nent-- > 0) {
25764 +               /* unnecessary to support mmap_sem since this is a dir */
25765 +               err = copy_from_user(&ent, u->e, sizeof(ent));
25766 +               if (!err)
25767 +                       err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
25768 +               if (unlikely(err)) {
25769 +                       err = -EFAULT;
25770 +                       AuTraceErr(err);
25771 +                       break;
25772 +               }
25773 +
25774 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
25775 +               if (!ent.wh)
25776 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
25777 +               else
25778 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
25779 +                                       &ino);
25780 +               if (unlikely(err)) {
25781 +                       AuTraceErr(err);
25782 +                       break;
25783 +               }
25784 +
25785 +               err = __put_user(ino, &u->e->ino);
25786 +               if (unlikely(err)) {
25787 +                       err = -EFAULT;
25788 +                       AuTraceErr(err);
25789 +                       break;
25790 +               }
25791 +               u->ul += au_rdu_len(ent.nlen);
25792 +       }
25793 +       si_read_unlock(sb);
25794 +
25795 +       return err;
25796 +}
25797 +
25798 +/* ---------------------------------------------------------------------- */
25799 +
25800 +static int au_rdu_verify(struct aufs_rdu *rdu)
25801 +{
25802 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
25803 +             "%llu, b%d, 0x%x, g%u}\n",
25804 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
25805 +             rdu->blk,
25806 +             rdu->rent, rdu->shwh, rdu->full,
25807 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
25808 +             rdu->cookie.generation);
25809 +
25810 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
25811 +               return 0;
25812 +
25813 +       AuDbg("%u:%u\n",
25814 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
25815 +       return -EINVAL;
25816 +}
25817 +
25818 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25819 +{
25820 +       long err, e;
25821 +       struct aufs_rdu rdu;
25822 +       void __user *p = (void __user *)arg;
25823 +
25824 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25825 +       if (unlikely(err)) {
25826 +               err = -EFAULT;
25827 +               AuTraceErr(err);
25828 +               goto out;
25829 +       }
25830 +       err = au_rdu_verify(&rdu);
25831 +       if (unlikely(err))
25832 +               goto out;
25833 +
25834 +       switch (cmd) {
25835 +       case AUFS_CTL_RDU:
25836 +               err = au_rdu(file, &rdu);
25837 +               if (unlikely(err))
25838 +                       break;
25839 +
25840 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25841 +               if (unlikely(e)) {
25842 +                       err = -EFAULT;
25843 +                       AuTraceErr(err);
25844 +               }
25845 +               break;
25846 +       case AUFS_CTL_RDU_INO:
25847 +               err = au_rdu_ino(file, &rdu);
25848 +               break;
25849 +
25850 +       default:
25851 +               /* err = -ENOTTY; */
25852 +               err = -EINVAL;
25853 +       }
25854 +
25855 +out:
25856 +       AuTraceErr(err);
25857 +       return err;
25858 +}
25859 +
25860 +#ifdef CONFIG_COMPAT
25861 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25862 +{
25863 +       long err, e;
25864 +       struct aufs_rdu rdu;
25865 +       void __user *p = compat_ptr(arg);
25866 +
25867 +       /* todo: get_user()? */
25868 +       err = copy_from_user(&rdu, p, sizeof(rdu));
25869 +       if (unlikely(err)) {
25870 +               err = -EFAULT;
25871 +               AuTraceErr(err);
25872 +               goto out;
25873 +       }
25874 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
25875 +       err = au_rdu_verify(&rdu);
25876 +       if (unlikely(err))
25877 +               goto out;
25878 +
25879 +       switch (cmd) {
25880 +       case AUFS_CTL_RDU:
25881 +               err = au_rdu(file, &rdu);
25882 +               if (unlikely(err))
25883 +                       break;
25884 +
25885 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
25886 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
25887 +               e = copy_to_user(p, &rdu, sizeof(rdu));
25888 +               if (unlikely(e)) {
25889 +                       err = -EFAULT;
25890 +                       AuTraceErr(err);
25891 +               }
25892 +               break;
25893 +       case AUFS_CTL_RDU_INO:
25894 +               err = au_rdu_ino(file, &rdu);
25895 +               break;
25896 +
25897 +       default:
25898 +               /* err = -ENOTTY; */
25899 +               err = -EINVAL;
25900 +       }
25901 +
25902 +out:
25903 +       AuTraceErr(err);
25904 +       return err;
25905 +}
25906 +#endif
25907 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
25908 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
25909 +++ linux/fs/aufs/rwsem.h       2015-09-24 10:47:58.254719746 +0200
25910 @@ -0,0 +1,191 @@
25911 +/*
25912 + * Copyright (C) 2005-2015 Junjiro R. Okajima
25913 + *
25914 + * This program, aufs is free software; you can redistribute it and/or modify
25915 + * it under the terms of the GNU General Public License as published by
25916 + * the Free Software Foundation; either version 2 of the License, or
25917 + * (at your option) any later version.
25918 + *
25919 + * This program is distributed in the hope that it will be useful,
25920 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25921 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25922 + * GNU General Public License for more details.
25923 + *
25924 + * You should have received a copy of the GNU General Public License
25925 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25926 + */
25927 +
25928 +/*
25929 + * simple read-write semaphore wrappers
25930 + */
25931 +
25932 +#ifndef __AUFS_RWSEM_H__
25933 +#define __AUFS_RWSEM_H__
25934 +
25935 +#ifdef __KERNEL__
25936 +
25937 +#include "debug.h"
25938 +
25939 +struct au_rwsem {
25940 +       struct rw_semaphore     rwsem;
25941 +#ifdef CONFIG_AUFS_DEBUG
25942 +       /* just for debugging, not almighty counter */
25943 +       atomic_t                rcnt, wcnt;
25944 +#endif
25945 +};
25946 +
25947 +#ifdef CONFIG_AUFS_DEBUG
25948 +#define AuDbgCntInit(rw) do { \
25949 +       atomic_set(&(rw)->rcnt, 0); \
25950 +       atomic_set(&(rw)->wcnt, 0); \
25951 +       smp_mb(); /* atomic set */ \
25952 +} while (0)
25953 +
25954 +#define AuDbgRcntInc(rw)       atomic_inc(&(rw)->rcnt)
25955 +#define AuDbgRcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->rcnt) < 0)
25956 +#define AuDbgWcntInc(rw)       atomic_inc(&(rw)->wcnt)
25957 +#define AuDbgWcntDec(rw)       WARN_ON(atomic_dec_return(&(rw)->wcnt) < 0)
25958 +#else
25959 +#define AuDbgCntInit(rw)       do {} while (0)
25960 +#define AuDbgRcntInc(rw)       do {} while (0)
25961 +#define AuDbgRcntDec(rw)       do {} while (0)
25962 +#define AuDbgWcntInc(rw)       do {} while (0)
25963 +#define AuDbgWcntDec(rw)       do {} while (0)
25964 +#endif /* CONFIG_AUFS_DEBUG */
25965 +
25966 +/* to debug easier, do not make them inlined functions */
25967 +#define AuRwMustNoWaiters(rw)  AuDebugOn(!list_empty(&(rw)->rwsem.wait_list))
25968 +/* rwsem_is_locked() is unusable */
25969 +#define AuRwMustReadLock(rw)   AuDebugOn(atomic_read(&(rw)->rcnt) <= 0)
25970 +#define AuRwMustWriteLock(rw)  AuDebugOn(atomic_read(&(rw)->wcnt) <= 0)
25971 +#define AuRwMustAnyLock(rw)    AuDebugOn(atomic_read(&(rw)->rcnt) <= 0 \
25972 +                                       && atomic_read(&(rw)->wcnt) <= 0)
25973 +#define AuRwDestroy(rw)                AuDebugOn(atomic_read(&(rw)->rcnt) \
25974 +                                       || atomic_read(&(rw)->wcnt))
25975 +
25976 +#define au_rw_class(rw, key)   lockdep_set_class(&(rw)->rwsem, key)
25977 +
25978 +static inline void au_rw_init(struct au_rwsem *rw)
25979 +{
25980 +       AuDbgCntInit(rw);
25981 +       init_rwsem(&rw->rwsem);
25982 +}
25983 +
25984 +static inline void au_rw_init_wlock(struct au_rwsem *rw)
25985 +{
25986 +       au_rw_init(rw);
25987 +       down_write(&rw->rwsem);
25988 +       AuDbgWcntInc(rw);
25989 +}
25990 +
25991 +static inline void au_rw_init_wlock_nested(struct au_rwsem *rw,
25992 +                                          unsigned int lsc)
25993 +{
25994 +       au_rw_init(rw);
25995 +       down_write_nested(&rw->rwsem, lsc);
25996 +       AuDbgWcntInc(rw);
25997 +}
25998 +
25999 +static inline void au_rw_read_lock(struct au_rwsem *rw)
26000 +{
26001 +       down_read(&rw->rwsem);
26002 +       AuDbgRcntInc(rw);
26003 +}
26004 +
26005 +static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26006 +{
26007 +       down_read_nested(&rw->rwsem, lsc);
26008 +       AuDbgRcntInc(rw);
26009 +}
26010 +
26011 +static inline void au_rw_read_unlock(struct au_rwsem *rw)
26012 +{
26013 +       AuRwMustReadLock(rw);
26014 +       AuDbgRcntDec(rw);
26015 +       up_read(&rw->rwsem);
26016 +}
26017 +
26018 +static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26019 +{
26020 +       AuRwMustWriteLock(rw);
26021 +       AuDbgRcntInc(rw);
26022 +       AuDbgWcntDec(rw);
26023 +       downgrade_write(&rw->rwsem);
26024 +}
26025 +
26026 +static inline void au_rw_write_lock(struct au_rwsem *rw)
26027 +{
26028 +       down_write(&rw->rwsem);
26029 +       AuDbgWcntInc(rw);
26030 +}
26031 +
26032 +static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26033 +                                          unsigned int lsc)
26034 +{
26035 +       down_write_nested(&rw->rwsem, lsc);
26036 +       AuDbgWcntInc(rw);
26037 +}
26038 +
26039 +static inline void au_rw_write_unlock(struct au_rwsem *rw)
26040 +{
26041 +       AuRwMustWriteLock(rw);
26042 +       AuDbgWcntDec(rw);
26043 +       up_write(&rw->rwsem);
26044 +}
26045 +
26046 +/* why is not _nested version defined */
26047 +static inline int au_rw_read_trylock(struct au_rwsem *rw)
26048 +{
26049 +       int ret;
26050 +
26051 +       ret = down_read_trylock(&rw->rwsem);
26052 +       if (ret)
26053 +               AuDbgRcntInc(rw);
26054 +       return ret;
26055 +}
26056 +
26057 +static inline int au_rw_write_trylock(struct au_rwsem *rw)
26058 +{
26059 +       int ret;
26060 +
26061 +       ret = down_write_trylock(&rw->rwsem);
26062 +       if (ret)
26063 +               AuDbgWcntInc(rw);
26064 +       return ret;
26065 +}
26066 +
26067 +#undef AuDbgCntInit
26068 +#undef AuDbgRcntInc
26069 +#undef AuDbgRcntDec
26070 +#undef AuDbgWcntInc
26071 +#undef AuDbgWcntDec
26072 +
26073 +#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26074 +static inline void prefix##_read_lock(param) \
26075 +{ au_rw_read_lock(rwsem); } \
26076 +static inline void prefix##_write_lock(param) \
26077 +{ au_rw_write_lock(rwsem); } \
26078 +static inline int prefix##_read_trylock(param) \
26079 +{ return au_rw_read_trylock(rwsem); } \
26080 +static inline int prefix##_write_trylock(param) \
26081 +{ return au_rw_write_trylock(rwsem); }
26082 +/* why is not _nested version defined */
26083 +/* static inline void prefix##_read_trylock_nested(param, lsc)
26084 +{ au_rw_read_trylock_nested(rwsem, lsc)); }
26085 +static inline void prefix##_write_trylock_nestd(param, lsc)
26086 +{ au_rw_write_trylock_nested(rwsem, lsc); } */
26087 +
26088 +#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26089 +static inline void prefix##_read_unlock(param) \
26090 +{ au_rw_read_unlock(rwsem); } \
26091 +static inline void prefix##_write_unlock(param) \
26092 +{ au_rw_write_unlock(rwsem); } \
26093 +static inline void prefix##_downgrade_lock(param) \
26094 +{ au_rw_dgrade_lock(rwsem); }
26095 +
26096 +#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26097 +       AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26098 +       AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26099 +
26100 +#endif /* __KERNEL__ */
26101 +#endif /* __AUFS_RWSEM_H__ */
26102 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26103 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
26104 +++ linux/fs/aufs/sbinfo.c      2015-09-24 10:47:58.254719746 +0200
26105 @@ -0,0 +1,360 @@
26106 +/*
26107 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26108 + *
26109 + * This program, aufs is free software; you can redistribute it and/or modify
26110 + * it under the terms of the GNU General Public License as published by
26111 + * the Free Software Foundation; either version 2 of the License, or
26112 + * (at your option) any later version.
26113 + *
26114 + * This program is distributed in the hope that it will be useful,
26115 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26116 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26117 + * GNU General Public License for more details.
26118 + *
26119 + * You should have received a copy of the GNU General Public License
26120 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26121 + */
26122 +
26123 +/*
26124 + * superblock private data
26125 + */
26126 +
26127 +#include "aufs.h"
26128 +
26129 +/*
26130 + * they are necessary regardless sysfs is disabled.
26131 + */
26132 +void au_si_free(struct kobject *kobj)
26133 +{
26134 +       int i;
26135 +       struct au_sbinfo *sbinfo;
26136 +       char *locked __maybe_unused; /* debug only */
26137 +
26138 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
26139 +       for (i = 0; i < AuPlink_NHASH; i++)
26140 +               AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
26141 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
26142 +
26143 +       AuDebugOn(!hlist_empty(&sbinfo->si_symlink.head));
26144 +
26145 +       au_rw_write_lock(&sbinfo->si_rwsem);
26146 +       au_br_free(sbinfo);
26147 +       au_rw_write_unlock(&sbinfo->si_rwsem);
26148 +
26149 +       AuDebugOn(radix_tree_gang_lookup
26150 +                 (&sbinfo->au_si_pid.tree, (void **)&locked,
26151 +                  /*first_index*/PID_MAX_DEFAULT - 1,
26152 +                  /*max_items*/sizeof(locked)/sizeof(*locked)));
26153 +
26154 +       kfree(sbinfo->si_branch);
26155 +       kfree(sbinfo->au_si_pid.bitmap);
26156 +       mutex_destroy(&sbinfo->si_xib_mtx);
26157 +       AuRwDestroy(&sbinfo->si_rwsem);
26158 +
26159 +       kfree(sbinfo);
26160 +}
26161 +
26162 +int au_si_alloc(struct super_block *sb)
26163 +{
26164 +       int err, i;
26165 +       struct au_sbinfo *sbinfo;
26166 +       static struct lock_class_key aufs_si;
26167 +
26168 +       err = -ENOMEM;
26169 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
26170 +       if (unlikely(!sbinfo))
26171 +               goto out;
26172 +
26173 +       BUILD_BUG_ON(sizeof(unsigned long) !=
26174 +                    sizeof(*sbinfo->au_si_pid.bitmap));
26175 +       sbinfo->au_si_pid.bitmap = kcalloc(BITS_TO_LONGS(PID_MAX_DEFAULT),
26176 +                                       sizeof(*sbinfo->au_si_pid.bitmap),
26177 +                                       GFP_NOFS);
26178 +       if (unlikely(!sbinfo->au_si_pid.bitmap))
26179 +               goto out_sbinfo;
26180 +
26181 +       /* will be reallocated separately */
26182 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26183 +       if (unlikely(!sbinfo->si_branch))
26184 +               goto out_pidmap;
26185 +
26186 +       err = sysaufs_si_init(sbinfo);
26187 +       if (unlikely(err))
26188 +               goto out_br;
26189 +
26190 +       au_nwt_init(&sbinfo->si_nowait);
26191 +       au_rw_init_wlock(&sbinfo->si_rwsem);
26192 +       au_rw_class(&sbinfo->si_rwsem, &aufs_si);
26193 +       spin_lock_init(&sbinfo->au_si_pid.tree_lock);
26194 +       INIT_RADIX_TREE(&sbinfo->au_si_pid.tree, GFP_ATOMIC | __GFP_NOFAIL);
26195 +
26196 +       atomic_long_set(&sbinfo->si_ninodes, 0);
26197 +       atomic_long_set(&sbinfo->si_nfiles, 0);
26198 +
26199 +       sbinfo->si_bend = -1;
26200 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
26201 +
26202 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26203 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
26204 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26205 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
26206 +
26207 +       au_fhsm_init(sbinfo);
26208 +
26209 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
26210 +
26211 +       au_sphl_init(&sbinfo->si_symlink);
26212 +
26213 +       sbinfo->si_xino_jiffy = jiffies;
26214 +       sbinfo->si_xino_expire
26215 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
26216 +       mutex_init(&sbinfo->si_xib_mtx);
26217 +       sbinfo->si_xino_brid = -1;
26218 +       /* leave si_xib_last_pindex and si_xib_next_bit */
26219 +
26220 +       au_sphl_init(&sbinfo->si_aopen);
26221 +
26222 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
26223 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26224 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26225 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26226 +
26227 +       for (i = 0; i < AuPlink_NHASH; i++)
26228 +               au_sphl_init(sbinfo->si_plink + i);
26229 +       init_waitqueue_head(&sbinfo->si_plink_wq);
26230 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
26231 +
26232 +       au_sphl_init(&sbinfo->si_files);
26233 +
26234 +       /* leave other members for sysaufs and si_mnt. */
26235 +       sbinfo->si_sb = sb;
26236 +       sb->s_fs_info = sbinfo;
26237 +       si_pid_set(sb);
26238 +       return 0; /* success */
26239 +
26240 +out_br:
26241 +       kfree(sbinfo->si_branch);
26242 +out_pidmap:
26243 +       kfree(sbinfo->au_si_pid.bitmap);
26244 +out_sbinfo:
26245 +       kfree(sbinfo);
26246 +out:
26247 +       return err;
26248 +}
26249 +
26250 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
26251 +{
26252 +       int err, sz;
26253 +       struct au_branch **brp;
26254 +
26255 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
26256 +
26257 +       err = -ENOMEM;
26258 +       sz = sizeof(*brp) * (sbinfo->si_bend + 1);
26259 +       if (unlikely(!sz))
26260 +               sz = sizeof(*brp);
26261 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
26262 +       if (brp) {
26263 +               sbinfo->si_branch = brp;
26264 +               err = 0;
26265 +       }
26266 +
26267 +       return err;
26268 +}
26269 +
26270 +/* ---------------------------------------------------------------------- */
26271 +
26272 +unsigned int au_sigen_inc(struct super_block *sb)
26273 +{
26274 +       unsigned int gen;
26275 +       struct inode *inode;
26276 +
26277 +       SiMustWriteLock(sb);
26278 +
26279 +       gen = ++au_sbi(sb)->si_generation;
26280 +       au_update_digen(sb->s_root);
26281 +       inode = d_inode(sb->s_root);
26282 +       au_update_iigen(inode, /*half*/0);
26283 +       inode->i_version++;
26284 +       return gen;
26285 +}
26286 +
26287 +aufs_bindex_t au_new_br_id(struct super_block *sb)
26288 +{
26289 +       aufs_bindex_t br_id;
26290 +       int i;
26291 +       struct au_sbinfo *sbinfo;
26292 +
26293 +       SiMustWriteLock(sb);
26294 +
26295 +       sbinfo = au_sbi(sb);
26296 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26297 +               br_id = ++sbinfo->si_last_br_id;
26298 +               AuDebugOn(br_id < 0);
26299 +               if (br_id && au_br_index(sb, br_id) < 0)
26300 +                       return br_id;
26301 +       }
26302 +
26303 +       return -1;
26304 +}
26305 +
26306 +/* ---------------------------------------------------------------------- */
26307 +
26308 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26309 +int si_read_lock(struct super_block *sb, int flags)
26310 +{
26311 +       int err;
26312 +
26313 +       err = 0;
26314 +       if (au_ftest_lock(flags, FLUSH))
26315 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26316 +
26317 +       si_noflush_read_lock(sb);
26318 +       err = au_plink_maint(sb, flags);
26319 +       if (unlikely(err))
26320 +               si_read_unlock(sb);
26321 +
26322 +       return err;
26323 +}
26324 +
26325 +int si_write_lock(struct super_block *sb, int flags)
26326 +{
26327 +       int err;
26328 +
26329 +       if (au_ftest_lock(flags, FLUSH))
26330 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
26331 +
26332 +       si_noflush_write_lock(sb);
26333 +       err = au_plink_maint(sb, flags);
26334 +       if (unlikely(err))
26335 +               si_write_unlock(sb);
26336 +
26337 +       return err;
26338 +}
26339 +
26340 +/* dentry and super_block lock. call at entry point */
26341 +int aufs_read_lock(struct dentry *dentry, int flags)
26342 +{
26343 +       int err;
26344 +       struct super_block *sb;
26345 +
26346 +       sb = dentry->d_sb;
26347 +       err = si_read_lock(sb, flags);
26348 +       if (unlikely(err))
26349 +               goto out;
26350 +
26351 +       if (au_ftest_lock(flags, DW))
26352 +               di_write_lock_child(dentry);
26353 +       else
26354 +               di_read_lock_child(dentry, flags);
26355 +
26356 +       if (au_ftest_lock(flags, GEN)) {
26357 +               err = au_digen_test(dentry, au_sigen(sb));
26358 +               AuDebugOn(!err && au_dbrange_test(dentry));
26359 +               if (unlikely(err))
26360 +                       aufs_read_unlock(dentry, flags);
26361 +       }
26362 +
26363 +out:
26364 +       return err;
26365 +}
26366 +
26367 +void aufs_read_unlock(struct dentry *dentry, int flags)
26368 +{
26369 +       if (au_ftest_lock(flags, DW))
26370 +               di_write_unlock(dentry);
26371 +       else
26372 +               di_read_unlock(dentry, flags);
26373 +       si_read_unlock(dentry->d_sb);
26374 +}
26375 +
26376 +void aufs_write_lock(struct dentry *dentry)
26377 +{
26378 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
26379 +       di_write_lock_child(dentry);
26380 +}
26381 +
26382 +void aufs_write_unlock(struct dentry *dentry)
26383 +{
26384 +       di_write_unlock(dentry);
26385 +       si_write_unlock(dentry->d_sb);
26386 +}
26387 +
26388 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
26389 +{
26390 +       int err;
26391 +       unsigned int sigen;
26392 +       struct super_block *sb;
26393 +
26394 +       sb = d1->d_sb;
26395 +       err = si_read_lock(sb, flags);
26396 +       if (unlikely(err))
26397 +               goto out;
26398 +
26399 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIR));
26400 +
26401 +       if (au_ftest_lock(flags, GEN)) {
26402 +               sigen = au_sigen(sb);
26403 +               err = au_digen_test(d1, sigen);
26404 +               AuDebugOn(!err && au_dbrange_test(d1));
26405 +               if (!err) {
26406 +                       err = au_digen_test(d2, sigen);
26407 +                       AuDebugOn(!err && au_dbrange_test(d2));
26408 +               }
26409 +               if (unlikely(err))
26410 +                       aufs_read_and_write_unlock2(d1, d2);
26411 +       }
26412 +
26413 +out:
26414 +       return err;
26415 +}
26416 +
26417 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
26418 +{
26419 +       di_write_unlock2(d1, d2);
26420 +       si_read_unlock(d1->d_sb);
26421 +}
26422 +
26423 +/* ---------------------------------------------------------------------- */
26424 +
26425 +int si_pid_test_slow(struct super_block *sb)
26426 +{
26427 +       void *p;
26428 +
26429 +       rcu_read_lock();
26430 +       p = radix_tree_lookup(&au_sbi(sb)->au_si_pid.tree, current->pid);
26431 +       rcu_read_unlock();
26432 +
26433 +       return (long)!!p;
26434 +}
26435 +
26436 +void si_pid_set_slow(struct super_block *sb)
26437 +{
26438 +       int err;
26439 +       struct au_sbinfo *sbinfo;
26440 +
26441 +       AuDebugOn(si_pid_test_slow(sb));
26442 +
26443 +       sbinfo = au_sbi(sb);
26444 +       err = radix_tree_preload(GFP_NOFS | __GFP_NOFAIL);
26445 +       AuDebugOn(err);
26446 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
26447 +       err = radix_tree_insert(&sbinfo->au_si_pid.tree, current->pid,
26448 +                               /*any valid ptr*/sb);
26449 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26450 +       AuDebugOn(err);
26451 +       radix_tree_preload_end();
26452 +}
26453 +
26454 +void si_pid_clr_slow(struct super_block *sb)
26455 +{
26456 +       void *p;
26457 +       struct au_sbinfo *sbinfo;
26458 +
26459 +       AuDebugOn(!si_pid_test_slow(sb));
26460 +
26461 +       sbinfo = au_sbi(sb);
26462 +       spin_lock(&sbinfo->au_si_pid.tree_lock);
26463 +       p = radix_tree_delete(&sbinfo->au_si_pid.tree, current->pid);
26464 +       spin_unlock(&sbinfo->au_si_pid.tree_lock);
26465 +}
26466 diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26467 --- /usr/share/empty/fs/aufs/spl.h      1970-01-01 01:00:00.000000000 +0100
26468 +++ linux/fs/aufs/spl.h 2015-09-24 10:47:58.254719746 +0200
26469 @@ -0,0 +1,111 @@
26470 +/*
26471 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26472 + *
26473 + * This program, aufs is free software; you can redistribute it and/or modify
26474 + * it under the terms of the GNU General Public License as published by
26475 + * the Free Software Foundation; either version 2 of the License, or
26476 + * (at your option) any later version.
26477 + *
26478 + * This program is distributed in the hope that it will be useful,
26479 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26480 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26481 + * GNU General Public License for more details.
26482 + *
26483 + * You should have received a copy of the GNU General Public License
26484 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26485 + */
26486 +
26487 +/*
26488 + * simple list protected by a spinlock
26489 + */
26490 +
26491 +#ifndef __AUFS_SPL_H__
26492 +#define __AUFS_SPL_H__
26493 +
26494 +#ifdef __KERNEL__
26495 +
26496 +struct au_splhead {
26497 +       spinlock_t              spin;
26498 +       struct list_head        head;
26499 +};
26500 +
26501 +static inline void au_spl_init(struct au_splhead *spl)
26502 +{
26503 +       spin_lock_init(&spl->spin);
26504 +       INIT_LIST_HEAD(&spl->head);
26505 +}
26506 +
26507 +static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26508 +{
26509 +       spin_lock(&spl->spin);
26510 +       list_add(list, &spl->head);
26511 +       spin_unlock(&spl->spin);
26512 +}
26513 +
26514 +static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26515 +{
26516 +       spin_lock(&spl->spin);
26517 +       list_del(list);
26518 +       spin_unlock(&spl->spin);
26519 +}
26520 +
26521 +static inline void au_spl_del_rcu(struct list_head *list,
26522 +                                 struct au_splhead *spl)
26523 +{
26524 +       spin_lock(&spl->spin);
26525 +       list_del_rcu(list);
26526 +       spin_unlock(&spl->spin);
26527 +}
26528 +
26529 +/* ---------------------------------------------------------------------- */
26530 +
26531 +struct au_sphlhead {
26532 +       spinlock_t              spin;
26533 +       struct hlist_head       head;
26534 +};
26535 +
26536 +static inline void au_sphl_init(struct au_sphlhead *sphl)
26537 +{
26538 +       spin_lock_init(&sphl->spin);
26539 +       INIT_HLIST_HEAD(&sphl->head);
26540 +}
26541 +
26542 +static inline void au_sphl_add(struct hlist_node *hlist,
26543 +                              struct au_sphlhead *sphl)
26544 +{
26545 +       spin_lock(&sphl->spin);
26546 +       hlist_add_head(hlist, &sphl->head);
26547 +       spin_unlock(&sphl->spin);
26548 +}
26549 +
26550 +static inline void au_sphl_del(struct hlist_node *hlist,
26551 +                              struct au_sphlhead *sphl)
26552 +{
26553 +       spin_lock(&sphl->spin);
26554 +       hlist_del(hlist);
26555 +       spin_unlock(&sphl->spin);
26556 +}
26557 +
26558 +static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26559 +                                  struct au_sphlhead *sphl)
26560 +{
26561 +       spin_lock(&sphl->spin);
26562 +       hlist_del_rcu(hlist);
26563 +       spin_unlock(&sphl->spin);
26564 +}
26565 +
26566 +static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
26567 +{
26568 +       unsigned long cnt;
26569 +       struct hlist_node *pos;
26570 +
26571 +       cnt = 0;
26572 +       spin_lock(&sphl->spin);
26573 +       hlist_for_each(pos, &sphl->head)
26574 +               cnt++;
26575 +       spin_unlock(&sphl->spin);
26576 +       return cnt;
26577 +}
26578 +
26579 +#endif /* __KERNEL__ */
26580 +#endif /* __AUFS_SPL_H__ */
26581 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
26582 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
26583 +++ linux/fs/aufs/super.c       2015-09-24 10:47:58.254719746 +0200
26584 @@ -0,0 +1,1004 @@
26585 +/*
26586 + * Copyright (C) 2005-2015 Junjiro R. Okajima
26587 + *
26588 + * This program, aufs is free software; you can redistribute it and/or modify
26589 + * it under the terms of the GNU General Public License as published by
26590 + * the Free Software Foundation; either version 2 of the License, or
26591 + * (at your option) any later version.
26592 + *
26593 + * This program is distributed in the hope that it will be useful,
26594 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26595 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26596 + * GNU General Public License for more details.
26597 + *
26598 + * You should have received a copy of the GNU General Public License
26599 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26600 + */
26601 +
26602 +/*
26603 + * mount and super_block operations
26604 + */
26605 +
26606 +#include <linux/mm.h>
26607 +#include <linux/seq_file.h>
26608 +#include <linux/statfs.h>
26609 +#include <linux/vmalloc.h>
26610 +#include "aufs.h"
26611 +
26612 +/*
26613 + * super_operations
26614 + */
26615 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
26616 +{
26617 +       struct au_icntnr *c;
26618 +
26619 +       c = au_cache_alloc_icntnr();
26620 +       if (c) {
26621 +               au_icntnr_init(c);
26622 +               c->vfs_inode.i_version = 1; /* sigen(sb); */
26623 +               c->iinfo.ii_hinode = NULL;
26624 +               return &c->vfs_inode;
26625 +       }
26626 +       return NULL;
26627 +}
26628 +
26629 +static void aufs_destroy_inode_cb(struct rcu_head *head)
26630 +{
26631 +       struct inode *inode = container_of(head, struct inode, i_rcu);
26632 +
26633 +       INIT_HLIST_HEAD(&inode->i_dentry);
26634 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
26635 +}
26636 +
26637 +static void aufs_destroy_inode(struct inode *inode)
26638 +{
26639 +       au_iinfo_fin(inode);
26640 +       call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
26641 +}
26642 +
26643 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
26644 +{
26645 +       struct inode *inode;
26646 +       int err;
26647 +
26648 +       inode = iget_locked(sb, ino);
26649 +       if (unlikely(!inode)) {
26650 +               inode = ERR_PTR(-ENOMEM);
26651 +               goto out;
26652 +       }
26653 +       if (!(inode->i_state & I_NEW))
26654 +               goto out;
26655 +
26656 +       err = au_xigen_new(inode);
26657 +       if (!err)
26658 +               err = au_iinfo_init(inode);
26659 +       if (!err)
26660 +               inode->i_version++;
26661 +       else {
26662 +               iget_failed(inode);
26663 +               inode = ERR_PTR(err);
26664 +       }
26665 +
26666 +out:
26667 +       /* never return NULL */
26668 +       AuDebugOn(!inode);
26669 +       AuTraceErrPtr(inode);
26670 +       return inode;
26671 +}
26672 +
26673 +/* lock free root dinfo */
26674 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
26675 +{
26676 +       int err;
26677 +       aufs_bindex_t bindex, bend;
26678 +       struct path path;
26679 +       struct au_hdentry *hdp;
26680 +       struct au_branch *br;
26681 +       au_br_perm_str_t perm;
26682 +
26683 +       err = 0;
26684 +       bend = au_sbend(sb);
26685 +       hdp = au_di(sb->s_root)->di_hdentry;
26686 +       for (bindex = 0; !err && bindex <= bend; bindex++) {
26687 +               br = au_sbr(sb, bindex);
26688 +               path.mnt = au_br_mnt(br);
26689 +               path.dentry = hdp[bindex].hd_dentry;
26690 +               err = au_seq_path(seq, &path);
26691 +               if (err > 0) {
26692 +                       au_optstr_br_perm(&perm, br->br_perm);
26693 +                       err = seq_printf(seq, "=%s", perm.a);
26694 +                       if (err == -1)
26695 +                               err = -E2BIG;
26696 +               }
26697 +               if (!err && bindex != bend)
26698 +                       err = seq_putc(seq, ':');
26699 +       }
26700 +
26701 +       return err;
26702 +}
26703 +
26704 +static void au_show_wbr_create(struct seq_file *m, int v,
26705 +                              struct au_sbinfo *sbinfo)
26706 +{
26707 +       const char *pat;
26708 +
26709 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26710 +
26711 +       seq_puts(m, ",create=");
26712 +       pat = au_optstr_wbr_create(v);
26713 +       switch (v) {
26714 +       case AuWbrCreate_TDP:
26715 +       case AuWbrCreate_RR:
26716 +       case AuWbrCreate_MFS:
26717 +       case AuWbrCreate_PMFS:
26718 +               seq_puts(m, pat);
26719 +               break;
26720 +       case AuWbrCreate_MFSV:
26721 +               seq_printf(m, /*pat*/"mfs:%lu",
26722 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26723 +                          / MSEC_PER_SEC);
26724 +               break;
26725 +       case AuWbrCreate_PMFSV:
26726 +               seq_printf(m, /*pat*/"pmfs:%lu",
26727 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26728 +                          / MSEC_PER_SEC);
26729 +               break;
26730 +       case AuWbrCreate_MFSRR:
26731 +               seq_printf(m, /*pat*/"mfsrr:%llu",
26732 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26733 +               break;
26734 +       case AuWbrCreate_MFSRRV:
26735 +               seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
26736 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26737 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26738 +                          / MSEC_PER_SEC);
26739 +               break;
26740 +       case AuWbrCreate_PMFSRR:
26741 +               seq_printf(m, /*pat*/"pmfsrr:%llu",
26742 +                          sbinfo->si_wbr_mfs.mfsrr_watermark);
26743 +               break;
26744 +       case AuWbrCreate_PMFSRRV:
26745 +               seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
26746 +                          sbinfo->si_wbr_mfs.mfsrr_watermark,
26747 +                          jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26748 +                          / MSEC_PER_SEC);
26749 +               break;
26750 +       }
26751 +}
26752 +
26753 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
26754 +{
26755 +#ifdef CONFIG_SYSFS
26756 +       return 0;
26757 +#else
26758 +       int err;
26759 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
26760 +       aufs_bindex_t bindex, brid;
26761 +       struct qstr *name;
26762 +       struct file *f;
26763 +       struct dentry *d, *h_root;
26764 +       struct au_hdentry *hdp;
26765 +
26766 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
26767 +
26768 +       err = 0;
26769 +       f = au_sbi(sb)->si_xib;
26770 +       if (!f)
26771 +               goto out;
26772 +
26773 +       /* stop printing the default xino path on the first writable branch */
26774 +       h_root = NULL;
26775 +       brid = au_xino_brid(sb);
26776 +       if (brid >= 0) {
26777 +               bindex = au_br_index(sb, brid);
26778 +               hdp = au_di(sb->s_root)->di_hdentry;
26779 +               h_root = hdp[0 + bindex].hd_dentry;
26780 +       }
26781 +       d = f->f_path.dentry;
26782 +       name = &d->d_name;
26783 +       /* safe ->d_parent because the file is unlinked */
26784 +       if (d->d_parent == h_root
26785 +           && name->len == len
26786 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
26787 +               goto out;
26788 +
26789 +       seq_puts(seq, ",xino=");
26790 +       err = au_xino_path(seq, f);
26791 +
26792 +out:
26793 +       return err;
26794 +#endif
26795 +}
26796 +
26797 +/* seq_file will re-call me in case of too long string */
26798 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
26799 +{
26800 +       int err;
26801 +       unsigned int mnt_flags, v;
26802 +       struct super_block *sb;
26803 +       struct au_sbinfo *sbinfo;
26804 +
26805 +#define AuBool(name, str) do { \
26806 +       v = au_opt_test(mnt_flags, name); \
26807 +       if (v != au_opt_test(AuOpt_Def, name)) \
26808 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
26809 +} while (0)
26810 +
26811 +#define AuStr(name, str) do { \
26812 +       v = mnt_flags & AuOptMask_##name; \
26813 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
26814 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
26815 +} while (0)
26816 +
26817 +#define AuUInt(name, str, val) do { \
26818 +       if (val != AUFS_##name##_DEF) \
26819 +               seq_printf(m, "," #str "=%u", val); \
26820 +} while (0)
26821 +
26822 +       sb = dentry->d_sb;
26823 +       if (sb->s_flags & MS_POSIXACL)
26824 +               seq_puts(m, ",acl");
26825 +
26826 +       /* lock free root dinfo */
26827 +       si_noflush_read_lock(sb);
26828 +       sbinfo = au_sbi(sb);
26829 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
26830 +
26831 +       mnt_flags = au_mntflags(sb);
26832 +       if (au_opt_test(mnt_flags, XINO)) {
26833 +               err = au_show_xino(m, sb);
26834 +               if (unlikely(err))
26835 +                       goto out;
26836 +       } else
26837 +               seq_puts(m, ",noxino");
26838 +
26839 +       AuBool(TRUNC_XINO, trunc_xino);
26840 +       AuStr(UDBA, udba);
26841 +       AuBool(SHWH, shwh);
26842 +       AuBool(PLINK, plink);
26843 +       AuBool(DIO, dio);
26844 +       AuBool(DIRPERM1, dirperm1);
26845 +
26846 +       v = sbinfo->si_wbr_create;
26847 +       if (v != AuWbrCreate_Def)
26848 +               au_show_wbr_create(m, v, sbinfo);
26849 +
26850 +       v = sbinfo->si_wbr_copyup;
26851 +       if (v != AuWbrCopyup_Def)
26852 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
26853 +
26854 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
26855 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
26856 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
26857 +
26858 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
26859 +
26860 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
26861 +       AuUInt(RDCACHE, rdcache, v);
26862 +
26863 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
26864 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
26865 +
26866 +       au_fhsm_show(m, sbinfo);
26867 +
26868 +       AuBool(SUM, sum);
26869 +       /* AuBool(SUM_W, wsum); */
26870 +       AuBool(WARN_PERM, warn_perm);
26871 +       AuBool(VERBOSE, verbose);
26872 +
26873 +out:
26874 +       /* be sure to print "br:" last */
26875 +       if (!sysaufs_brs) {
26876 +               seq_puts(m, ",br:");
26877 +               au_show_brs(m, sb);
26878 +       }
26879 +       si_read_unlock(sb);
26880 +       return 0;
26881 +
26882 +#undef AuBool
26883 +#undef AuStr
26884 +#undef AuUInt
26885 +}
26886 +
26887 +/* ---------------------------------------------------------------------- */
26888 +
26889 +/* sum mode which returns the summation for statfs(2) */
26890 +
26891 +static u64 au_add_till_max(u64 a, u64 b)
26892 +{
26893 +       u64 old;
26894 +
26895 +       old = a;
26896 +       a += b;
26897 +       if (old <= a)
26898 +               return a;
26899 +       return ULLONG_MAX;
26900 +}
26901 +
26902 +static u64 au_mul_till_max(u64 a, long mul)
26903 +{
26904 +       u64 old;
26905 +
26906 +       old = a;
26907 +       a *= mul;
26908 +       if (old <= a)
26909 +               return a;
26910 +       return ULLONG_MAX;
26911 +}
26912 +
26913 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
26914 +{
26915 +       int err;
26916 +       long bsize, factor;
26917 +       u64 blocks, bfree, bavail, files, ffree;
26918 +       aufs_bindex_t bend, bindex, i;
26919 +       unsigned char shared;
26920 +       struct path h_path;
26921 +       struct super_block *h_sb;
26922 +
26923 +       err = 0;
26924 +       bsize = LONG_MAX;
26925 +       files = 0;
26926 +       ffree = 0;
26927 +       blocks = 0;
26928 +       bfree = 0;
26929 +       bavail = 0;
26930 +       bend = au_sbend(sb);
26931 +       for (bindex = 0; bindex <= bend; bindex++) {
26932 +               h_path.mnt = au_sbr_mnt(sb, bindex);
26933 +               h_sb = h_path.mnt->mnt_sb;
26934 +               shared = 0;
26935 +               for (i = 0; !shared && i < bindex; i++)
26936 +                       shared = (au_sbr_sb(sb, i) == h_sb);
26937 +               if (shared)
26938 +                       continue;
26939 +
26940 +               /* sb->s_root for NFS is unreliable */
26941 +               h_path.dentry = h_path.mnt->mnt_root;
26942 +               err = vfs_statfs(&h_path, buf);
26943 +               if (unlikely(err))
26944 +                       goto out;
26945 +
26946 +               if (bsize > buf->f_bsize) {
26947 +                       /*
26948 +                        * we will reduce bsize, so we have to expand blocks
26949 +                        * etc. to match them again
26950 +                        */
26951 +                       factor = (bsize / buf->f_bsize);
26952 +                       blocks = au_mul_till_max(blocks, factor);
26953 +                       bfree = au_mul_till_max(bfree, factor);
26954 +                       bavail = au_mul_till_max(bavail, factor);
26955 +                       bsize = buf->f_bsize;
26956 +               }
26957 +
26958 +               factor = (buf->f_bsize / bsize);
26959 +               blocks = au_add_till_max(blocks,
26960 +                               au_mul_till_max(buf->f_blocks, factor));
26961 +               bfree = au_add_till_max(bfree,
26962 +                               au_mul_till_max(buf->f_bfree, factor));
26963 +               bavail = au_add_till_max(bavail,
26964 +                               au_mul_till_max(buf->f_bavail, factor));
26965 +               files = au_add_till_max(files, buf->f_files);
26966 +               ffree = au_add_till_max(ffree, buf->f_ffree);
26967 +       }
26968 +
26969 +       buf->f_bsize = bsize;
26970 +       buf->f_blocks = blocks;
26971 +       buf->f_bfree = bfree;
26972 +       buf->f_bavail = bavail;
26973 +       buf->f_files = files;
26974 +       buf->f_ffree = ffree;
26975 +       buf->f_frsize = 0;
26976 +
26977 +out:
26978 +       return err;
26979 +}
26980 +
26981 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
26982 +{
26983 +       int err;
26984 +       struct path h_path;
26985 +       struct super_block *sb;
26986 +
26987 +       /* lock free root dinfo */
26988 +       sb = dentry->d_sb;
26989 +       si_noflush_read_lock(sb);
26990 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
26991 +               /* sb->s_root for NFS is unreliable */
26992 +               h_path.mnt = au_sbr_mnt(sb, 0);
26993 +               h_path.dentry = h_path.mnt->mnt_root;
26994 +               err = vfs_statfs(&h_path, buf);
26995 +       } else
26996 +               err = au_statfs_sum(sb, buf);
26997 +       si_read_unlock(sb);
26998 +
26999 +       if (!err) {
27000 +               buf->f_type = AUFS_SUPER_MAGIC;
27001 +               buf->f_namelen = AUFS_MAX_NAMELEN;
27002 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27003 +       }
27004 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27005 +
27006 +       return err;
27007 +}
27008 +
27009 +/* ---------------------------------------------------------------------- */
27010 +
27011 +static int aufs_sync_fs(struct super_block *sb, int wait)
27012 +{
27013 +       int err, e;
27014 +       aufs_bindex_t bend, bindex;
27015 +       struct au_branch *br;
27016 +       struct super_block *h_sb;
27017 +
27018 +       err = 0;
27019 +       si_noflush_read_lock(sb);
27020 +       bend = au_sbend(sb);
27021 +       for (bindex = 0; bindex <= bend; bindex++) {
27022 +               br = au_sbr(sb, bindex);
27023 +               if (!au_br_writable(br->br_perm))
27024 +                       continue;
27025 +
27026 +               h_sb = au_sbr_sb(sb, bindex);
27027 +               if (h_sb->s_op->sync_fs) {
27028 +                       e = h_sb->s_op->sync_fs(h_sb, wait);
27029 +                       if (unlikely(e && !err))
27030 +                               err = e;
27031 +                       /* go on even if an error happens */
27032 +               }
27033 +       }
27034 +       si_read_unlock(sb);
27035 +
27036 +       return err;
27037 +}
27038 +
27039 +/* ---------------------------------------------------------------------- */
27040 +
27041 +/* final actions when unmounting a file system */
27042 +static void aufs_put_super(struct super_block *sb)
27043 +{
27044 +       struct au_sbinfo *sbinfo;
27045 +
27046 +       sbinfo = au_sbi(sb);
27047 +       if (!sbinfo)
27048 +               return;
27049 +
27050 +       dbgaufs_si_fin(sbinfo);
27051 +       kobject_put(&sbinfo->si_kobj);
27052 +}
27053 +
27054 +/* ---------------------------------------------------------------------- */
27055 +
27056 +void au_array_free(void *array)
27057 +{
27058 +       if (array) {
27059 +               if (!is_vmalloc_addr(array))
27060 +                       kfree(array);
27061 +               else
27062 +                       vfree(array);
27063 +       }
27064 +}
27065 +
27066 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg)
27067 +{
27068 +       void *array;
27069 +       unsigned long long n, sz;
27070 +
27071 +       array = NULL;
27072 +       n = 0;
27073 +       if (!*hint)
27074 +               goto out;
27075 +
27076 +       if (*hint > ULLONG_MAX / sizeof(array)) {
27077 +               array = ERR_PTR(-EMFILE);
27078 +               pr_err("hint %llu\n", *hint);
27079 +               goto out;
27080 +       }
27081 +
27082 +       sz = sizeof(array) * *hint;
27083 +       array = kzalloc(sz, GFP_NOFS);
27084 +       if (unlikely(!array))
27085 +               array = vzalloc(sz);
27086 +       if (unlikely(!array)) {
27087 +               array = ERR_PTR(-ENOMEM);
27088 +               goto out;
27089 +       }
27090 +
27091 +       n = cb(array, *hint, arg);
27092 +       AuDebugOn(n > *hint);
27093 +
27094 +out:
27095 +       *hint = n;
27096 +       return array;
27097 +}
27098 +
27099 +static unsigned long long au_iarray_cb(void *a,
27100 +                                      unsigned long long max __maybe_unused,
27101 +                                      void *arg)
27102 +{
27103 +       unsigned long long n;
27104 +       struct inode **p, *inode;
27105 +       struct list_head *head;
27106 +
27107 +       n = 0;
27108 +       p = a;
27109 +       head = arg;
27110 +       spin_lock(&inode_sb_list_lock);
27111 +       list_for_each_entry(inode, head, i_sb_list) {
27112 +               if (!is_bad_inode(inode)
27113 +                   && au_ii(inode)->ii_bstart >= 0) {
27114 +                       spin_lock(&inode->i_lock);
27115 +                       if (atomic_read(&inode->i_count)) {
27116 +                               au_igrab(inode);
27117 +                               *p++ = inode;
27118 +                               n++;
27119 +                               AuDebugOn(n > max);
27120 +                       }
27121 +                       spin_unlock(&inode->i_lock);
27122 +               }
27123 +       }
27124 +       spin_unlock(&inode_sb_list_lock);
27125 +
27126 +       return n;
27127 +}
27128 +
27129 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27130 +{
27131 +       *max = atomic_long_read(&au_sbi(sb)->si_ninodes);
27132 +       return au_array_alloc(max, au_iarray_cb, &sb->s_inodes);
27133 +}
27134 +
27135 +void au_iarray_free(struct inode **a, unsigned long long max)
27136 +{
27137 +       unsigned long long ull;
27138 +
27139 +       for (ull = 0; ull < max; ull++)
27140 +               iput(a[ull]);
27141 +       au_array_free(a);
27142 +}
27143 +
27144 +/* ---------------------------------------------------------------------- */
27145 +
27146 +/*
27147 + * refresh dentry and inode at remount time.
27148 + */
27149 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27150 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27151 +                     struct dentry *parent)
27152 +{
27153 +       int err;
27154 +
27155 +       di_write_lock_child(dentry);
27156 +       di_read_lock_parent(parent, AuLock_IR);
27157 +       err = au_refresh_dentry(dentry, parent);
27158 +       if (!err && dir_flags)
27159 +               au_hn_reset(d_inode(dentry), dir_flags);
27160 +       di_read_unlock(parent, AuLock_IR);
27161 +       di_write_unlock(dentry);
27162 +
27163 +       return err;
27164 +}
27165 +
27166 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27167 +                          struct au_sbinfo *sbinfo,
27168 +                          const unsigned int dir_flags)
27169 +{
27170 +       int err;
27171 +       struct dentry *parent;
27172 +
27173 +       err = 0;
27174 +       parent = dget_parent(dentry);
27175 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
27176 +               if (d_really_is_positive(dentry)) {
27177 +                       if (!d_is_dir(dentry))
27178 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
27179 +                                                parent);
27180 +                       else {
27181 +                               err = au_do_refresh(dentry, dir_flags, parent);
27182 +                               if (unlikely(err))
27183 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27184 +                       }
27185 +               } else
27186 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27187 +               AuDbgDentry(dentry);
27188 +       }
27189 +       dput(parent);
27190 +
27191 +       AuTraceErr(err);
27192 +       return err;
27193 +}
27194 +
27195 +static int au_refresh_d(struct super_block *sb)
27196 +{
27197 +       int err, i, j, ndentry, e;
27198 +       unsigned int sigen;
27199 +       struct au_dcsub_pages dpages;
27200 +       struct au_dpage *dpage;
27201 +       struct dentry **dentries, *d;
27202 +       struct au_sbinfo *sbinfo;
27203 +       struct dentry *root = sb->s_root;
27204 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
27205 +
27206 +       err = au_dpages_init(&dpages, GFP_NOFS);
27207 +       if (unlikely(err))
27208 +               goto out;
27209 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
27210 +       if (unlikely(err))
27211 +               goto out_dpages;
27212 +
27213 +       sigen = au_sigen(sb);
27214 +       sbinfo = au_sbi(sb);
27215 +       for (i = 0; i < dpages.ndpage; i++) {
27216 +               dpage = dpages.dpages + i;
27217 +               dentries = dpage->dentries;
27218 +               ndentry = dpage->ndentry;
27219 +               for (j = 0; j < ndentry; j++) {
27220 +                       d = dentries[j];
27221 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags);
27222 +                       if (unlikely(e && !err))
27223 +                               err = e;
27224 +                       /* go on even err */
27225 +               }
27226 +       }
27227 +
27228 +out_dpages:
27229 +       au_dpages_free(&dpages);
27230 +out:
27231 +       return err;
27232 +}
27233 +
27234 +static int au_refresh_i(struct super_block *sb)
27235 +{
27236 +       int err, e;
27237 +       unsigned int sigen;
27238 +       unsigned long long max, ull;
27239 +       struct inode *inode, **array;
27240 +
27241 +       array = au_iarray_alloc(sb, &max);
27242 +       err = PTR_ERR(array);
27243 +       if (IS_ERR(array))
27244 +               goto out;
27245 +
27246 +       err = 0;
27247 +       sigen = au_sigen(sb);
27248 +       for (ull = 0; ull < max; ull++) {
27249 +               inode = array[ull];
27250 +               if (unlikely(!inode))
27251 +                       break;
27252 +               if (au_iigen(inode, NULL) != sigen) {
27253 +                       ii_write_lock_child(inode);
27254 +                       e = au_refresh_hinode_self(inode);
27255 +                       ii_write_unlock(inode);
27256 +                       if (unlikely(e)) {
27257 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
27258 +                               if (!err)
27259 +                                       err = e;
27260 +                               /* go on even if err */
27261 +                       }
27262 +               }
27263 +       }
27264 +
27265 +       au_iarray_free(array, max);
27266 +
27267 +out:
27268 +       return err;
27269 +}
27270 +
27271 +static void au_remount_refresh(struct super_block *sb)
27272 +{
27273 +       int err, e;
27274 +       unsigned int udba;
27275 +       aufs_bindex_t bindex, bend;
27276 +       struct dentry *root;
27277 +       struct inode *inode;
27278 +       struct au_branch *br;
27279 +
27280 +       au_sigen_inc(sb);
27281 +       au_fclr_si(au_sbi(sb), FAILED_REFRESH_DIR);
27282 +
27283 +       root = sb->s_root;
27284 +       DiMustNoWaiters(root);
27285 +       inode = d_inode(root);
27286 +       IiMustNoWaiters(inode);
27287 +
27288 +       udba = au_opt_udba(sb);
27289 +       bend = au_sbend(sb);
27290 +       for (bindex = 0; bindex <= bend; bindex++) {
27291 +               br = au_sbr(sb, bindex);
27292 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
27293 +               if (unlikely(err))
27294 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27295 +                               bindex, err);
27296 +               /* go on even if err */
27297 +       }
27298 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
27299 +
27300 +       di_write_unlock(root);
27301 +       err = au_refresh_d(sb);
27302 +       e = au_refresh_i(sb);
27303 +       if (unlikely(e && !err))
27304 +               err = e;
27305 +       /* aufs_write_lock() calls ..._child() */
27306 +       di_write_lock_child(root);
27307 +
27308 +       au_cpup_attr_all(inode, /*force*/1);
27309 +
27310 +       if (unlikely(err))
27311 +               AuIOErr("refresh failed, ignored, %d\n", err);
27312 +}
27313 +
27314 +/* stop extra interpretation of errno in mount(8), and strange error messages */
27315 +static int cvt_err(int err)
27316 +{
27317 +       AuTraceErr(err);
27318 +
27319 +       switch (err) {
27320 +       case -ENOENT:
27321 +       case -ENOTDIR:
27322 +       case -EEXIST:
27323 +       case -EIO:
27324 +               err = -EINVAL;
27325 +       }
27326 +       return err;
27327 +}
27328 +
27329 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27330 +{
27331 +       int err, do_dx;
27332 +       unsigned int mntflags;
27333 +       struct au_opts opts;
27334 +       struct dentry *root;
27335 +       struct inode *inode;
27336 +       struct au_sbinfo *sbinfo;
27337 +
27338 +       err = 0;
27339 +       root = sb->s_root;
27340 +       if (!data || !*data) {
27341 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27342 +               if (!err) {
27343 +                       di_write_lock_child(root);
27344 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
27345 +                       aufs_write_unlock(root);
27346 +               }
27347 +               goto out;
27348 +       }
27349 +
27350 +       err = -ENOMEM;
27351 +       memset(&opts, 0, sizeof(opts));
27352 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27353 +       if (unlikely(!opts.opt))
27354 +               goto out;
27355 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27356 +       opts.flags = AuOpts_REMOUNT;
27357 +       opts.sb_flags = *flags;
27358 +
27359 +       /* parse it before aufs lock */
27360 +       err = au_opts_parse(sb, data, &opts);
27361 +       if (unlikely(err))
27362 +               goto out_opts;
27363 +
27364 +       sbinfo = au_sbi(sb);
27365 +       inode = d_inode(root);
27366 +       mutex_lock(&inode->i_mutex);
27367 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27368 +       if (unlikely(err))
27369 +               goto out_mtx;
27370 +       di_write_lock_child(root);
27371 +
27372 +       /* au_opts_remount() may return an error */
27373 +       err = au_opts_remount(sb, &opts);
27374 +       au_opts_free(&opts);
27375 +
27376 +       if (au_ftest_opts(opts.flags, REFRESH))
27377 +               au_remount_refresh(sb);
27378 +
27379 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
27380 +               mntflags = au_mntflags(sb);
27381 +               do_dx = !!au_opt_test(mntflags, DIO);
27382 +               au_dy_arefresh(do_dx);
27383 +       }
27384 +
27385 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
27386 +       aufs_write_unlock(root);
27387 +
27388 +out_mtx:
27389 +       mutex_unlock(&inode->i_mutex);
27390 +out_opts:
27391 +       free_page((unsigned long)opts.opt);
27392 +out:
27393 +       err = cvt_err(err);
27394 +       AuTraceErr(err);
27395 +       return err;
27396 +}
27397 +
27398 +static const struct super_operations aufs_sop = {
27399 +       .alloc_inode    = aufs_alloc_inode,
27400 +       .destroy_inode  = aufs_destroy_inode,
27401 +       /* always deleting, no clearing */
27402 +       .drop_inode     = generic_delete_inode,
27403 +       .show_options   = aufs_show_options,
27404 +       .statfs         = aufs_statfs,
27405 +       .put_super      = aufs_put_super,
27406 +       .sync_fs        = aufs_sync_fs,
27407 +       .remount_fs     = aufs_remount_fs
27408 +};
27409 +
27410 +/* ---------------------------------------------------------------------- */
27411 +
27412 +static int alloc_root(struct super_block *sb)
27413 +{
27414 +       int err;
27415 +       struct inode *inode;
27416 +       struct dentry *root;
27417 +
27418 +       err = -ENOMEM;
27419 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
27420 +       err = PTR_ERR(inode);
27421 +       if (IS_ERR(inode))
27422 +               goto out;
27423 +
27424 +       inode->i_op = &aufs_dir_iop;
27425 +       inode->i_fop = &aufs_dir_fop;
27426 +       inode->i_mode = S_IFDIR;
27427 +       set_nlink(inode, 2);
27428 +       unlock_new_inode(inode);
27429 +
27430 +       root = d_make_root(inode);
27431 +       if (unlikely(!root))
27432 +               goto out;
27433 +       err = PTR_ERR(root);
27434 +       if (IS_ERR(root))
27435 +               goto out;
27436 +
27437 +       err = au_di_init(root);
27438 +       if (!err) {
27439 +               sb->s_root = root;
27440 +               return 0; /* success */
27441 +       }
27442 +       dput(root);
27443 +
27444 +out:
27445 +       return err;
27446 +}
27447 +
27448 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
27449 +                          int silent __maybe_unused)
27450 +{
27451 +       int err;
27452 +       struct au_opts opts;
27453 +       struct dentry *root;
27454 +       struct inode *inode;
27455 +       char *arg = raw_data;
27456 +
27457 +       if (unlikely(!arg || !*arg)) {
27458 +               err = -EINVAL;
27459 +               pr_err("no arg\n");
27460 +               goto out;
27461 +       }
27462 +
27463 +       err = -ENOMEM;
27464 +       memset(&opts, 0, sizeof(opts));
27465 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
27466 +       if (unlikely(!opts.opt))
27467 +               goto out;
27468 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27469 +       opts.sb_flags = sb->s_flags;
27470 +
27471 +       err = au_si_alloc(sb);
27472 +       if (unlikely(err))
27473 +               goto out_opts;
27474 +
27475 +       /* all timestamps always follow the ones on the branch */
27476 +       sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27477 +       sb->s_op = &aufs_sop;
27478 +       sb->s_d_op = &aufs_dop;
27479 +       sb->s_magic = AUFS_SUPER_MAGIC;
27480 +       sb->s_maxbytes = 0;
27481 +       sb->s_stack_depth = 1;
27482 +       au_export_init(sb);
27483 +       /* au_xattr_init(sb); */
27484 +
27485 +       err = alloc_root(sb);
27486 +       if (unlikely(err)) {
27487 +               si_write_unlock(sb);
27488 +               goto out_info;
27489 +       }
27490 +       root = sb->s_root;
27491 +       inode = d_inode(root);
27492 +
27493 +       /*
27494 +        * actually we can parse options regardless aufs lock here.
27495 +        * but at remount time, parsing must be done before aufs lock.
27496 +        * so we follow the same rule.
27497 +        */
27498 +       ii_write_lock_parent(inode);
27499 +       aufs_write_unlock(root);
27500 +       err = au_opts_parse(sb, arg, &opts);
27501 +       if (unlikely(err))
27502 +               goto out_root;
27503 +
27504 +       /* lock vfs_inode first, then aufs. */
27505 +       mutex_lock(&inode->i_mutex);
27506 +       aufs_write_lock(root);
27507 +       err = au_opts_mount(sb, &opts);
27508 +       au_opts_free(&opts);
27509 +       aufs_write_unlock(root);
27510 +       mutex_unlock(&inode->i_mutex);
27511 +       if (!err)
27512 +               goto out_opts; /* success */
27513 +
27514 +out_root:
27515 +       dput(root);
27516 +       sb->s_root = NULL;
27517 +out_info:
27518 +       dbgaufs_si_fin(au_sbi(sb));
27519 +       kobject_put(&au_sbi(sb)->si_kobj);
27520 +       sb->s_fs_info = NULL;
27521 +out_opts:
27522 +       free_page((unsigned long)opts.opt);
27523 +out:
27524 +       AuTraceErr(err);
27525 +       err = cvt_err(err);
27526 +       AuTraceErr(err);
27527 +       return err;
27528 +}
27529 +
27530 +/* ---------------------------------------------------------------------- */
27531 +
27532 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
27533 +                                const char *dev_name __maybe_unused,
27534 +                                void *raw_data)
27535 +{
27536 +       struct dentry *root;
27537 +       struct super_block *sb;
27538 +
27539 +       /* all timestamps always follow the ones on the branch */
27540 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
27541 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
27542 +       if (IS_ERR(root))
27543 +               goto out;
27544 +
27545 +       sb = root->d_sb;
27546 +       si_write_lock(sb, !AuLock_FLUSH);
27547 +       sysaufs_brs_add(sb, 0);
27548 +       si_write_unlock(sb);
27549 +       au_sbilist_add(sb);
27550 +
27551 +out:
27552 +       return root;
27553 +}
27554 +
27555 +static void aufs_kill_sb(struct super_block *sb)
27556 +{
27557 +       struct au_sbinfo *sbinfo;
27558 +
27559 +       sbinfo = au_sbi(sb);
27560 +       if (sbinfo) {
27561 +               au_sbilist_del(sb);
27562 +               aufs_write_lock(sb->s_root);
27563 +               au_fhsm_fin(sb);
27564 +               if (sbinfo->si_wbr_create_ops->fin)
27565 +                       sbinfo->si_wbr_create_ops->fin(sb);
27566 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
27567 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
27568 +                       au_remount_refresh(sb);
27569 +               }
27570 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27571 +                       au_plink_put(sb, /*verbose*/1);
27572 +               au_xino_clr(sb);
27573 +               sbinfo->si_sb = NULL;
27574 +               aufs_write_unlock(sb->s_root);
27575 +               au_nwt_flush(&sbinfo->si_nowait);
27576 +       }
27577 +       kill_anon_super(sb);
27578 +}
27579 +
27580 +struct file_system_type aufs_fs_type = {
27581 +       .name           = AUFS_FSTYPE,
27582 +       /* a race between rename and others */
27583 +       .fs_flags       = FS_RENAME_DOES_D_MOVE,
27584 +       .mount          = aufs_mount,
27585 +       .kill_sb        = aufs_kill_sb,
27586 +       /* no need to __module_get() and module_put(). */
27587 +       .owner          = THIS_MODULE,
27588 +};
27589 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
27590 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
27591 +++ linux/fs/aufs/super.h       2015-09-24 10:47:58.254719746 +0200
27592 @@ -0,0 +1,638 @@
27593 +/*
27594 + * Copyright (C) 2005-2015 Junjiro R. Okajima
27595 + *
27596 + * This program, aufs is free software; you can redistribute it and/or modify
27597 + * it under the terms of the GNU General Public License as published by
27598 + * the Free Software Foundation; either version 2 of the License, or
27599 + * (at your option) any later version.
27600 + *
27601 + * This program is distributed in the hope that it will be useful,
27602 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27603 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27604 + * GNU General Public License for more details.
27605 + *
27606 + * You should have received a copy of the GNU General Public License
27607 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27608 + */
27609 +
27610 +/*
27611 + * super_block operations
27612 + */
27613 +
27614 +#ifndef __AUFS_SUPER_H__
27615 +#define __AUFS_SUPER_H__
27616 +
27617 +#ifdef __KERNEL__
27618 +
27619 +#include <linux/fs.h>
27620 +#include <linux/kobject.h>
27621 +#include "rwsem.h"
27622 +#include "spl.h"
27623 +#include "wkq.h"
27624 +
27625 +/* policies to select one among multiple writable branches */
27626 +struct au_wbr_copyup_operations {
27627 +       int (*copyup)(struct dentry *dentry);
27628 +};
27629 +
27630 +#define AuWbr_DIR      1               /* target is a dir */
27631 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
27632 +
27633 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
27634 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
27635 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
27636 +
27637 +struct au_wbr_create_operations {
27638 +       int (*create)(struct dentry *dentry, unsigned int flags);
27639 +       int (*init)(struct super_block *sb);
27640 +       int (*fin)(struct super_block *sb);
27641 +};
27642 +
27643 +struct au_wbr_mfs {
27644 +       struct mutex    mfs_lock; /* protect this structure */
27645 +       unsigned long   mfs_jiffy;
27646 +       unsigned long   mfs_expire;
27647 +       aufs_bindex_t   mfs_bindex;
27648 +
27649 +       unsigned long long      mfsrr_bytes;
27650 +       unsigned long long      mfsrr_watermark;
27651 +};
27652 +
27653 +struct pseudo_link {
27654 +       union {
27655 +               struct hlist_node hlist;
27656 +               struct rcu_head rcu;
27657 +       };
27658 +       struct inode *inode;
27659 +};
27660 +
27661 +#define AuPlink_NHASH 100
27662 +static inline int au_plink_hash(ino_t ino)
27663 +{
27664 +       return ino % AuPlink_NHASH;
27665 +}
27666 +
27667 +/* File-based Hierarchical Storage Management */
27668 +struct au_fhsm {
27669 +#ifdef CONFIG_AUFS_FHSM
27670 +       /* allow only one process who can receive the notification */
27671 +       spinlock_t              fhsm_spin;
27672 +       pid_t                   fhsm_pid;
27673 +       wait_queue_head_t       fhsm_wqh;
27674 +       atomic_t                fhsm_readable;
27675 +
27676 +       /* these are protected by si_rwsem */
27677 +       unsigned long           fhsm_expire;
27678 +       aufs_bindex_t           fhsm_bottom;
27679 +#endif
27680 +};
27681 +
27682 +struct au_branch;
27683 +struct au_sbinfo {
27684 +       /* nowait tasks in the system-wide workqueue */
27685 +       struct au_nowait_tasks  si_nowait;
27686 +
27687 +       /*
27688 +        * tried sb->s_umount, but failed due to the dependecy between i_mutex.
27689 +        * rwsem for au_sbinfo is necessary.
27690 +        */
27691 +       struct au_rwsem         si_rwsem;
27692 +
27693 +       /* prevent recursive locking in deleting inode */
27694 +       struct {
27695 +               unsigned long           *bitmap;
27696 +               spinlock_t              tree_lock;
27697 +               struct radix_tree_root  tree;
27698 +       } au_si_pid;
27699 +
27700 +       /*
27701 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
27702 +        * remount.
27703 +        */
27704 +       atomic_long_t           si_ninodes, si_nfiles;
27705 +
27706 +       /* branch management */
27707 +       unsigned int            si_generation;
27708 +
27709 +       /* see AuSi_ flags */
27710 +       unsigned char           au_si_status;
27711 +
27712 +       aufs_bindex_t           si_bend;
27713 +
27714 +       /* dirty trick to keep br_id plus */
27715 +       unsigned int            si_last_br_id :
27716 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
27717 +       struct au_branch        **si_branch;
27718 +
27719 +       /* policy to select a writable branch */
27720 +       unsigned char           si_wbr_copyup;
27721 +       unsigned char           si_wbr_create;
27722 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
27723 +       struct au_wbr_create_operations *si_wbr_create_ops;
27724 +
27725 +       /* round robin */
27726 +       atomic_t                si_wbr_rr_next;
27727 +
27728 +       /* most free space */
27729 +       struct au_wbr_mfs       si_wbr_mfs;
27730 +
27731 +       /* File-based Hierarchical Storage Management */
27732 +       struct au_fhsm          si_fhsm;
27733 +
27734 +       /* mount flags */
27735 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
27736 +       unsigned int            si_mntflags;
27737 +
27738 +       /* symlink to follow_link() and put_link() */
27739 +       struct au_sphlhead      si_symlink;
27740 +
27741 +       /* external inode number (bitmap and translation table) */
27742 +       vfs_readf_t             si_xread;
27743 +       vfs_writef_t            si_xwrite;
27744 +       struct file             *si_xib;
27745 +       struct mutex            si_xib_mtx; /* protect xib members */
27746 +       unsigned long           *si_xib_buf;
27747 +       unsigned long           si_xib_last_pindex;
27748 +       int                     si_xib_next_bit;
27749 +       aufs_bindex_t           si_xino_brid;
27750 +       unsigned long           si_xino_jiffy;
27751 +       unsigned long           si_xino_expire;
27752 +       /* reserved for future use */
27753 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
27754 +
27755 +#ifdef CONFIG_AUFS_EXPORT
27756 +       /* i_generation */
27757 +       struct file             *si_xigen;
27758 +       atomic_t                si_xigen_next;
27759 +#endif
27760 +
27761 +       /* dirty trick to suppoer atomic_open */
27762 +       struct au_sphlhead      si_aopen;
27763 +
27764 +       /* vdir parameters */
27765 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
27766 +       unsigned int            si_rdblk;       /* deblk size */
27767 +       unsigned int            si_rdhash;      /* hash size */
27768 +
27769 +       /*
27770 +        * If the number of whiteouts are larger than si_dirwh, leave all of
27771 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
27772 +        * future fsck.aufs or kernel thread will remove them later.
27773 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
27774 +        */
27775 +       unsigned int            si_dirwh;
27776 +
27777 +       /* pseudo_link list */
27778 +       struct au_sphlhead      si_plink[AuPlink_NHASH];
27779 +       wait_queue_head_t       si_plink_wq;
27780 +       spinlock_t              si_plink_maint_lock;
27781 +       pid_t                   si_plink_maint_pid;
27782 +
27783 +       /* file list */
27784 +       struct au_sphlhead      si_files;
27785 +
27786 +       /*
27787 +        * sysfs and lifetime management.
27788 +        * this is not a small structure and it may be a waste of memory in case
27789 +        * of sysfs is disabled, particulary when many aufs-es are mounted.
27790 +        * but using sysfs is majority.
27791 +        */
27792 +       struct kobject          si_kobj;
27793 +#ifdef CONFIG_DEBUG_FS
27794 +       struct dentry            *si_dbgaufs;
27795 +       struct dentry            *si_dbgaufs_plink;
27796 +       struct dentry            *si_dbgaufs_xib;
27797 +#ifdef CONFIG_AUFS_EXPORT
27798 +       struct dentry            *si_dbgaufs_xigen;
27799 +#endif
27800 +#endif
27801 +
27802 +#ifdef CONFIG_AUFS_SBILIST
27803 +       struct list_head        si_list;
27804 +#endif
27805 +
27806 +       /* dirty, necessary for unmounting, sysfs and sysrq */
27807 +       struct super_block      *si_sb;
27808 +};
27809 +
27810 +/* sbinfo status flags */
27811 +/*
27812 + * set true when refresh_dirs() failed at remount time.
27813 + * then try refreshing dirs at access time again.
27814 + * if it is false, refreshing dirs at access time is unnecesary
27815 + */
27816 +#define AuSi_FAILED_REFRESH_DIR        1
27817 +
27818 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
27819 +
27820 +#ifndef CONFIG_AUFS_FHSM
27821 +#undef AuSi_FHSM
27822 +#define AuSi_FHSM              0
27823 +#endif
27824 +
27825 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
27826 +                                          unsigned int flag)
27827 +{
27828 +       AuRwMustAnyLock(&sbi->si_rwsem);
27829 +       return sbi->au_si_status & flag;
27830 +}
27831 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
27832 +#define au_fset_si(sbinfo, name) do { \
27833 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27834 +       (sbinfo)->au_si_status |= AuSi_##name; \
27835 +} while (0)
27836 +#define au_fclr_si(sbinfo, name) do { \
27837 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
27838 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
27839 +} while (0)
27840 +
27841 +/* ---------------------------------------------------------------------- */
27842 +
27843 +/* policy to select one among writable branches */
27844 +#define AuWbrCopyup(sbinfo, ...) \
27845 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
27846 +#define AuWbrCreate(sbinfo, ...) \
27847 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
27848 +
27849 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
27850 +#define AuLock_DW              1               /* write-lock dentry */
27851 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
27852 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
27853 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
27854 +#define AuLock_DIR             (1 << 4)        /* target is a dir */
27855 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
27856 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
27857 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
27858 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
27859 +#define au_fset_lock(flags, name) \
27860 +       do { (flags) |= AuLock_##name; } while (0)
27861 +#define au_fclr_lock(flags, name) \
27862 +       do { (flags) &= ~AuLock_##name; } while (0)
27863 +
27864 +/* ---------------------------------------------------------------------- */
27865 +
27866 +/* super.c */
27867 +extern struct file_system_type aufs_fs_type;
27868 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
27869 +typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
27870 +                                          void *arg);
27871 +void au_array_free(void *array);
27872 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
27873 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
27874 +void au_iarray_free(struct inode **a, unsigned long long max);
27875 +
27876 +/* sbinfo.c */
27877 +void au_si_free(struct kobject *kobj);
27878 +int au_si_alloc(struct super_block *sb);
27879 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
27880 +
27881 +unsigned int au_sigen_inc(struct super_block *sb);
27882 +aufs_bindex_t au_new_br_id(struct super_block *sb);
27883 +
27884 +int si_read_lock(struct super_block *sb, int flags);
27885 +int si_write_lock(struct super_block *sb, int flags);
27886 +int aufs_read_lock(struct dentry *dentry, int flags);
27887 +void aufs_read_unlock(struct dentry *dentry, int flags);
27888 +void aufs_write_lock(struct dentry *dentry);
27889 +void aufs_write_unlock(struct dentry *dentry);
27890 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
27891 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
27892 +
27893 +int si_pid_test_slow(struct super_block *sb);
27894 +void si_pid_set_slow(struct super_block *sb);
27895 +void si_pid_clr_slow(struct super_block *sb);
27896 +
27897 +/* wbr_policy.c */
27898 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
27899 +extern struct au_wbr_create_operations au_wbr_create_ops[];
27900 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
27901 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
27902 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart);
27903 +
27904 +/* mvdown.c */
27905 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
27906 +
27907 +#ifdef CONFIG_AUFS_FHSM
27908 +/* fhsm.c */
27909 +
27910 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
27911 +{
27912 +       pid_t pid;
27913 +
27914 +       spin_lock(&fhsm->fhsm_spin);
27915 +       pid = fhsm->fhsm_pid;
27916 +       spin_unlock(&fhsm->fhsm_spin);
27917 +
27918 +       return pid;
27919 +}
27920 +
27921 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
27922 +void au_fhsm_wrote_all(struct super_block *sb, int force);
27923 +int au_fhsm_fd(struct super_block *sb, int oflags);
27924 +int au_fhsm_br_alloc(struct au_branch *br);
27925 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
27926 +void au_fhsm_fin(struct super_block *sb);
27927 +void au_fhsm_init(struct au_sbinfo *sbinfo);
27928 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
27929 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
27930 +#else
27931 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
27932 +          int force)
27933 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
27934 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
27935 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
27936 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
27937 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
27938 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
27939 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
27940 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
27941 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
27942 +#endif
27943 +
27944 +/* ---------------------------------------------------------------------- */
27945 +
27946 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
27947 +{
27948 +       return sb->s_fs_info;
27949 +}
27950 +
27951 +/* ---------------------------------------------------------------------- */
27952 +
27953 +#ifdef CONFIG_AUFS_EXPORT
27954 +int au_test_nfsd(void);
27955 +void au_export_init(struct super_block *sb);
27956 +void au_xigen_inc(struct inode *inode);
27957 +int au_xigen_new(struct inode *inode);
27958 +int au_xigen_set(struct super_block *sb, struct file *base);
27959 +void au_xigen_clr(struct super_block *sb);
27960 +
27961 +static inline int au_busy_or_stale(void)
27962 +{
27963 +       if (!au_test_nfsd())
27964 +               return -EBUSY;
27965 +       return -ESTALE;
27966 +}
27967 +#else
27968 +AuStubInt0(au_test_nfsd, void)
27969 +AuStubVoid(au_export_init, struct super_block *sb)
27970 +AuStubVoid(au_xigen_inc, struct inode *inode)
27971 +AuStubInt0(au_xigen_new, struct inode *inode)
27972 +AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
27973 +AuStubVoid(au_xigen_clr, struct super_block *sb)
27974 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
27975 +#endif /* CONFIG_AUFS_EXPORT */
27976 +
27977 +/* ---------------------------------------------------------------------- */
27978 +
27979 +#ifdef CONFIG_AUFS_SBILIST
27980 +/* module.c */
27981 +extern struct au_splhead au_sbilist;
27982 +
27983 +static inline void au_sbilist_init(void)
27984 +{
27985 +       au_spl_init(&au_sbilist);
27986 +}
27987 +
27988 +static inline void au_sbilist_add(struct super_block *sb)
27989 +{
27990 +       au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
27991 +}
27992 +
27993 +static inline void au_sbilist_del(struct super_block *sb)
27994 +{
27995 +       au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
27996 +}
27997 +
27998 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
27999 +static inline void au_sbilist_lock(void)
28000 +{
28001 +       spin_lock(&au_sbilist.spin);
28002 +}
28003 +
28004 +static inline void au_sbilist_unlock(void)
28005 +{
28006 +       spin_unlock(&au_sbilist.spin);
28007 +}
28008 +#define AuGFP_SBILIST  GFP_ATOMIC
28009 +#else
28010 +AuStubVoid(au_sbilist_lock, void)
28011 +AuStubVoid(au_sbilist_unlock, void)
28012 +#define AuGFP_SBILIST  GFP_NOFS
28013 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
28014 +#else
28015 +AuStubVoid(au_sbilist_init, void)
28016 +AuStubVoid(au_sbilist_add, struct super_block *sb)
28017 +AuStubVoid(au_sbilist_del, struct super_block *sb)
28018 +AuStubVoid(au_sbilist_lock, void)
28019 +AuStubVoid(au_sbilist_unlock, void)
28020 +#define AuGFP_SBILIST  GFP_NOFS
28021 +#endif
28022 +
28023 +/* ---------------------------------------------------------------------- */
28024 +
28025 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28026 +{
28027 +       /*
28028 +        * This function is a dynamic '__init' function actually,
28029 +        * so the tiny check for si_rwsem is unnecessary.
28030 +        */
28031 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
28032 +#ifdef CONFIG_DEBUG_FS
28033 +       sbinfo->si_dbgaufs = NULL;
28034 +       sbinfo->si_dbgaufs_plink = NULL;
28035 +       sbinfo->si_dbgaufs_xib = NULL;
28036 +#ifdef CONFIG_AUFS_EXPORT
28037 +       sbinfo->si_dbgaufs_xigen = NULL;
28038 +#endif
28039 +#endif
28040 +}
28041 +
28042 +/* ---------------------------------------------------------------------- */
28043 +
28044 +static inline pid_t si_pid_bit(void)
28045 +{
28046 +       /* the origin of pid is 1, but the bitmap's is 0 */
28047 +       return current->pid - 1;
28048 +}
28049 +
28050 +static inline int si_pid_test(struct super_block *sb)
28051 +{
28052 +       pid_t bit;
28053 +
28054 +       bit = si_pid_bit();
28055 +       if (bit < PID_MAX_DEFAULT)
28056 +               return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28057 +       return si_pid_test_slow(sb);
28058 +}
28059 +
28060 +static inline void si_pid_set(struct super_block *sb)
28061 +{
28062 +       pid_t bit;
28063 +
28064 +       bit = si_pid_bit();
28065 +       if (bit < PID_MAX_DEFAULT) {
28066 +               AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
28067 +               set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28068 +               /* smp_mb(); */
28069 +       } else
28070 +               si_pid_set_slow(sb);
28071 +}
28072 +
28073 +static inline void si_pid_clr(struct super_block *sb)
28074 +{
28075 +       pid_t bit;
28076 +
28077 +       bit = si_pid_bit();
28078 +       if (bit < PID_MAX_DEFAULT) {
28079 +               AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
28080 +               clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
28081 +               /* smp_mb(); */
28082 +       } else
28083 +               si_pid_clr_slow(sb);
28084 +}
28085 +
28086 +/* ---------------------------------------------------------------------- */
28087 +
28088 +/* lock superblock. mainly for entry point functions */
28089 +/*
28090 + * __si_read_lock, __si_write_lock,
28091 + * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
28092 + */
28093 +AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
28094 +
28095 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28096 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28097 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28098 +
28099 +static inline void si_noflush_read_lock(struct super_block *sb)
28100 +{
28101 +       __si_read_lock(sb);
28102 +       si_pid_set(sb);
28103 +}
28104 +
28105 +static inline int si_noflush_read_trylock(struct super_block *sb)
28106 +{
28107 +       int locked;
28108 +
28109 +       locked = __si_read_trylock(sb);
28110 +       if (locked)
28111 +               si_pid_set(sb);
28112 +       return locked;
28113 +}
28114 +
28115 +static inline void si_noflush_write_lock(struct super_block *sb)
28116 +{
28117 +       __si_write_lock(sb);
28118 +       si_pid_set(sb);
28119 +}
28120 +
28121 +static inline int si_noflush_write_trylock(struct super_block *sb)
28122 +{
28123 +       int locked;
28124 +
28125 +       locked = __si_write_trylock(sb);
28126 +       if (locked)
28127 +               si_pid_set(sb);
28128 +       return locked;
28129 +}
28130 +
28131 +#if 0 /* reserved */
28132 +static inline int si_read_trylock(struct super_block *sb, int flags)
28133 +{
28134 +       if (au_ftest_lock(flags, FLUSH))
28135 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28136 +       return si_noflush_read_trylock(sb);
28137 +}
28138 +#endif
28139 +
28140 +static inline void si_read_unlock(struct super_block *sb)
28141 +{
28142 +       si_pid_clr(sb);
28143 +       __si_read_unlock(sb);
28144 +}
28145 +
28146 +#if 0 /* reserved */
28147 +static inline int si_write_trylock(struct super_block *sb, int flags)
28148 +{
28149 +       if (au_ftest_lock(flags, FLUSH))
28150 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
28151 +       return si_noflush_write_trylock(sb);
28152 +}
28153 +#endif
28154 +
28155 +static inline void si_write_unlock(struct super_block *sb)
28156 +{
28157 +       si_pid_clr(sb);
28158 +       __si_write_unlock(sb);
28159 +}
28160 +
28161 +#if 0 /* reserved */
28162 +static inline void si_downgrade_lock(struct super_block *sb)
28163 +{
28164 +       __si_downgrade_lock(sb);
28165 +}
28166 +#endif
28167 +
28168 +/* ---------------------------------------------------------------------- */
28169 +
28170 +static inline aufs_bindex_t au_sbend(struct super_block *sb)
28171 +{
28172 +       SiMustAnyLock(sb);
28173 +       return au_sbi(sb)->si_bend;
28174 +}
28175 +
28176 +static inline unsigned int au_mntflags(struct super_block *sb)
28177 +{
28178 +       SiMustAnyLock(sb);
28179 +       return au_sbi(sb)->si_mntflags;
28180 +}
28181 +
28182 +static inline unsigned int au_sigen(struct super_block *sb)
28183 +{
28184 +       SiMustAnyLock(sb);
28185 +       return au_sbi(sb)->si_generation;
28186 +}
28187 +
28188 +static inline void au_ninodes_inc(struct super_block *sb)
28189 +{
28190 +       atomic_long_inc(&au_sbi(sb)->si_ninodes);
28191 +}
28192 +
28193 +static inline void au_ninodes_dec(struct super_block *sb)
28194 +{
28195 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
28196 +       atomic_long_dec(&au_sbi(sb)->si_ninodes);
28197 +}
28198 +
28199 +static inline void au_nfiles_inc(struct super_block *sb)
28200 +{
28201 +       atomic_long_inc(&au_sbi(sb)->si_nfiles);
28202 +}
28203 +
28204 +static inline void au_nfiles_dec(struct super_block *sb)
28205 +{
28206 +       AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
28207 +       atomic_long_dec(&au_sbi(sb)->si_nfiles);
28208 +}
28209 +
28210 +static inline struct au_branch *au_sbr(struct super_block *sb,
28211 +                                      aufs_bindex_t bindex)
28212 +{
28213 +       SiMustAnyLock(sb);
28214 +       return au_sbi(sb)->si_branch[0 + bindex];
28215 +}
28216 +
28217 +static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28218 +{
28219 +       SiMustWriteLock(sb);
28220 +       au_sbi(sb)->si_xino_brid = brid;
28221 +}
28222 +
28223 +static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28224 +{
28225 +       SiMustAnyLock(sb);
28226 +       return au_sbi(sb)->si_xino_brid;
28227 +}
28228 +
28229 +#endif /* __KERNEL__ */
28230 +#endif /* __AUFS_SUPER_H__ */
28231 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28232 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
28233 +++ linux/fs/aufs/sysaufs.c     2015-09-24 10:47:58.254719746 +0200
28234 @@ -0,0 +1,104 @@
28235 +/*
28236 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28237 + *
28238 + * This program, aufs is free software; you can redistribute it and/or modify
28239 + * it under the terms of the GNU General Public License as published by
28240 + * the Free Software Foundation; either version 2 of the License, or
28241 + * (at your option) any later version.
28242 + *
28243 + * This program is distributed in the hope that it will be useful,
28244 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28245 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28246 + * GNU General Public License for more details.
28247 + *
28248 + * You should have received a copy of the GNU General Public License
28249 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28250 + */
28251 +
28252 +/*
28253 + * sysfs interface and lifetime management
28254 + * they are necessary regardless sysfs is disabled.
28255 + */
28256 +
28257 +#include <linux/random.h>
28258 +#include "aufs.h"
28259 +
28260 +unsigned long sysaufs_si_mask;
28261 +struct kset *sysaufs_kset;
28262 +
28263 +#define AuSiAttr(_name) { \
28264 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
28265 +       .show   = sysaufs_si_##_name,                           \
28266 +}
28267 +
28268 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28269 +struct attribute *sysaufs_si_attrs[] = {
28270 +       &sysaufs_si_attr_xi_path.attr,
28271 +       NULL,
28272 +};
28273 +
28274 +static const struct sysfs_ops au_sbi_ops = {
28275 +       .show   = sysaufs_si_show
28276 +};
28277 +
28278 +static struct kobj_type au_sbi_ktype = {
28279 +       .release        = au_si_free,
28280 +       .sysfs_ops      = &au_sbi_ops,
28281 +       .default_attrs  = sysaufs_si_attrs
28282 +};
28283 +
28284 +/* ---------------------------------------------------------------------- */
28285 +
28286 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
28287 +{
28288 +       int err;
28289 +
28290 +       sbinfo->si_kobj.kset = sysaufs_kset;
28291 +       /* cf. sysaufs_name() */
28292 +       err = kobject_init_and_add
28293 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
28294 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28295 +
28296 +       dbgaufs_si_null(sbinfo);
28297 +       if (!err) {
28298 +               err = dbgaufs_si_init(sbinfo);
28299 +               if (unlikely(err))
28300 +                       kobject_put(&sbinfo->si_kobj);
28301 +       }
28302 +       return err;
28303 +}
28304 +
28305 +void sysaufs_fin(void)
28306 +{
28307 +       dbgaufs_fin();
28308 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28309 +       kset_unregister(sysaufs_kset);
28310 +}
28311 +
28312 +int __init sysaufs_init(void)
28313 +{
28314 +       int err;
28315 +
28316 +       do {
28317 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28318 +       } while (!sysaufs_si_mask);
28319 +
28320 +       err = -EINVAL;
28321 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28322 +       if (unlikely(!sysaufs_kset))
28323 +               goto out;
28324 +       err = PTR_ERR(sysaufs_kset);
28325 +       if (IS_ERR(sysaufs_kset))
28326 +               goto out;
28327 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28328 +       if (unlikely(err)) {
28329 +               kset_unregister(sysaufs_kset);
28330 +               goto out;
28331 +       }
28332 +
28333 +       err = dbgaufs_init();
28334 +       if (unlikely(err))
28335 +               sysaufs_fin();
28336 +out:
28337 +       return err;
28338 +}
28339 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28340 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
28341 +++ linux/fs/aufs/sysaufs.h     2015-09-24 10:47:58.254719746 +0200
28342 @@ -0,0 +1,101 @@
28343 +/*
28344 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28345 + *
28346 + * This program, aufs is free software; you can redistribute it and/or modify
28347 + * it under the terms of the GNU General Public License as published by
28348 + * the Free Software Foundation; either version 2 of the License, or
28349 + * (at your option) any later version.
28350 + *
28351 + * This program is distributed in the hope that it will be useful,
28352 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28353 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28354 + * GNU General Public License for more details.
28355 + *
28356 + * You should have received a copy of the GNU General Public License
28357 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28358 + */
28359 +
28360 +/*
28361 + * sysfs interface and mount lifetime management
28362 + */
28363 +
28364 +#ifndef __SYSAUFS_H__
28365 +#define __SYSAUFS_H__
28366 +
28367 +#ifdef __KERNEL__
28368 +
28369 +#include <linux/sysfs.h>
28370 +#include "module.h"
28371 +
28372 +struct super_block;
28373 +struct au_sbinfo;
28374 +
28375 +struct sysaufs_si_attr {
28376 +       struct attribute attr;
28377 +       int (*show)(struct seq_file *seq, struct super_block *sb);
28378 +};
28379 +
28380 +/* ---------------------------------------------------------------------- */
28381 +
28382 +/* sysaufs.c */
28383 +extern unsigned long sysaufs_si_mask;
28384 +extern struct kset *sysaufs_kset;
28385 +extern struct attribute *sysaufs_si_attrs[];
28386 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
28387 +int __init sysaufs_init(void);
28388 +void sysaufs_fin(void);
28389 +
28390 +/* ---------------------------------------------------------------------- */
28391 +
28392 +/* some people doesn't like to show a pointer in kernel */
28393 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
28394 +{
28395 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
28396 +}
28397 +
28398 +#define SysaufsSiNamePrefix    "si_"
28399 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
28400 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
28401 +{
28402 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
28403 +                sysaufs_si_id(sbinfo));
28404 +}
28405 +
28406 +struct au_branch;
28407 +#ifdef CONFIG_SYSFS
28408 +/* sysfs.c */
28409 +extern struct attribute_group *sysaufs_attr_group;
28410 +
28411 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
28412 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28413 +                        char *buf);
28414 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
28415 +#ifdef CONFIG_COMPAT
28416 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
28417 +#endif
28418 +
28419 +void sysaufs_br_init(struct au_branch *br);
28420 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
28421 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
28422 +
28423 +#define sysaufs_brs_init()     do {} while (0)
28424 +
28425 +#else
28426 +#define sysaufs_attr_group     NULL
28427 +
28428 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
28429 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
28430 +       struct attribute *attr, char *buf)
28431 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
28432 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
28433 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
28434 +
28435 +static inline void sysaufs_brs_init(void)
28436 +{
28437 +       sysaufs_brs = 0;
28438 +}
28439 +
28440 +#endif /* CONFIG_SYSFS */
28441 +
28442 +#endif /* __KERNEL__ */
28443 +#endif /* __SYSAUFS_H__ */
28444 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
28445 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
28446 +++ linux/fs/aufs/sysfs.c       2015-09-24 10:47:58.254719746 +0200
28447 @@ -0,0 +1,372 @@
28448 +/*
28449 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28450 + *
28451 + * This program, aufs is free software; you can redistribute it and/or modify
28452 + * it under the terms of the GNU General Public License as published by
28453 + * the Free Software Foundation; either version 2 of the License, or
28454 + * (at your option) any later version.
28455 + *
28456 + * This program is distributed in the hope that it will be useful,
28457 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28458 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28459 + * GNU General Public License for more details.
28460 + *
28461 + * You should have received a copy of the GNU General Public License
28462 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28463 + */
28464 +
28465 +/*
28466 + * sysfs interface
28467 + */
28468 +
28469 +#include <linux/compat.h>
28470 +#include <linux/seq_file.h>
28471 +#include "aufs.h"
28472 +
28473 +#ifdef CONFIG_AUFS_FS_MODULE
28474 +/* this entry violates the "one line per file" policy of sysfs */
28475 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28476 +                          char *buf)
28477 +{
28478 +       ssize_t err;
28479 +       static char *conf =
28480 +/* this file is generated at compiling */
28481 +#include "conf.str"
28482 +               ;
28483 +
28484 +       err = snprintf(buf, PAGE_SIZE, conf);
28485 +       if (unlikely(err >= PAGE_SIZE))
28486 +               err = -EFBIG;
28487 +       return err;
28488 +}
28489 +
28490 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28491 +#endif
28492 +
28493 +static struct attribute *au_attr[] = {
28494 +#ifdef CONFIG_AUFS_FS_MODULE
28495 +       &au_config_attr.attr,
28496 +#endif
28497 +       NULL,   /* need to NULL terminate the list of attributes */
28498 +};
28499 +
28500 +static struct attribute_group sysaufs_attr_group_body = {
28501 +       .attrs = au_attr
28502 +};
28503 +
28504 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28505 +
28506 +/* ---------------------------------------------------------------------- */
28507 +
28508 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28509 +{
28510 +       int err;
28511 +
28512 +       SiMustAnyLock(sb);
28513 +
28514 +       err = 0;
28515 +       if (au_opt_test(au_mntflags(sb), XINO)) {
28516 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
28517 +               seq_putc(seq, '\n');
28518 +       }
28519 +       return err;
28520 +}
28521 +
28522 +/*
28523 + * the lifetime of branch is independent from the entry under sysfs.
28524 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
28525 + * unlinked.
28526 + */
28527 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
28528 +                        aufs_bindex_t bindex, int idx)
28529 +{
28530 +       int err;
28531 +       struct path path;
28532 +       struct dentry *root;
28533 +       struct au_branch *br;
28534 +       au_br_perm_str_t perm;
28535 +
28536 +       AuDbg("b%d\n", bindex);
28537 +
28538 +       err = 0;
28539 +       root = sb->s_root;
28540 +       di_read_lock_parent(root, !AuLock_IR);
28541 +       br = au_sbr(sb, bindex);
28542 +
28543 +       switch (idx) {
28544 +       case AuBrSysfs_BR:
28545 +               path.mnt = au_br_mnt(br);
28546 +               path.dentry = au_h_dptr(root, bindex);
28547 +               au_seq_path(seq, &path);
28548 +               au_optstr_br_perm(&perm, br->br_perm);
28549 +               err = seq_printf(seq, "=%s\n", perm.a);
28550 +               break;
28551 +       case AuBrSysfs_BRID:
28552 +               err = seq_printf(seq, "%d\n", br->br_id);
28553 +               break;
28554 +       }
28555 +       di_read_unlock(root, !AuLock_IR);
28556 +       if (err == -1)
28557 +               err = -E2BIG;
28558 +
28559 +       return err;
28560 +}
28561 +
28562 +/* ---------------------------------------------------------------------- */
28563 +
28564 +static struct seq_file *au_seq(char *p, ssize_t len)
28565 +{
28566 +       struct seq_file *seq;
28567 +
28568 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
28569 +       if (seq) {
28570 +               /* mutex_init(&seq.lock); */
28571 +               seq->buf = p;
28572 +               seq->size = len;
28573 +               return seq; /* success */
28574 +       }
28575 +
28576 +       seq = ERR_PTR(-ENOMEM);
28577 +       return seq;
28578 +}
28579 +
28580 +#define SysaufsBr_PREFIX       "br"
28581 +#define SysaufsBrid_PREFIX     "brid"
28582 +
28583 +/* todo: file size may exceed PAGE_SIZE */
28584 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28585 +                       char *buf)
28586 +{
28587 +       ssize_t err;
28588 +       int idx;
28589 +       long l;
28590 +       aufs_bindex_t bend;
28591 +       struct au_sbinfo *sbinfo;
28592 +       struct super_block *sb;
28593 +       struct seq_file *seq;
28594 +       char *name;
28595 +       struct attribute **cattr;
28596 +
28597 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28598 +       sb = sbinfo->si_sb;
28599 +
28600 +       /*
28601 +        * prevent a race condition between sysfs and aufs.
28602 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
28603 +        * prohibits maintaining the sysfs entries.
28604 +        * hew we acquire read lock after sysfs_get_active_two().
28605 +        * on the other hand, the remount process may maintain the sysfs/aufs
28606 +        * entries after acquiring write lock.
28607 +        * it can cause a deadlock.
28608 +        * simply we gave up processing read here.
28609 +        */
28610 +       err = -EBUSY;
28611 +       if (unlikely(!si_noflush_read_trylock(sb)))
28612 +               goto out;
28613 +
28614 +       seq = au_seq(buf, PAGE_SIZE);
28615 +       err = PTR_ERR(seq);
28616 +       if (IS_ERR(seq))
28617 +               goto out_unlock;
28618 +
28619 +       name = (void *)attr->name;
28620 +       cattr = sysaufs_si_attrs;
28621 +       while (*cattr) {
28622 +               if (!strcmp(name, (*cattr)->name)) {
28623 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
28624 +                               ->show(seq, sb);
28625 +                       goto out_seq;
28626 +               }
28627 +               cattr++;
28628 +       }
28629 +
28630 +       if (!strncmp(name, SysaufsBrid_PREFIX,
28631 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
28632 +               idx = AuBrSysfs_BRID;
28633 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
28634 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
28635 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
28636 +               idx = AuBrSysfs_BR;
28637 +               name += sizeof(SysaufsBr_PREFIX) - 1;
28638 +       } else
28639 +                 BUG();
28640 +
28641 +       err = kstrtol(name, 10, &l);
28642 +       if (!err) {
28643 +               bend = au_sbend(sb);
28644 +               if (l <= bend)
28645 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
28646 +               else
28647 +                       err = -ENOENT;
28648 +       }
28649 +
28650 +out_seq:
28651 +       if (!err) {
28652 +               err = seq->count;
28653 +               /* sysfs limit */
28654 +               if (unlikely(err == PAGE_SIZE))
28655 +                       err = -EFBIG;
28656 +       }
28657 +       kfree(seq);
28658 +out_unlock:
28659 +       si_read_unlock(sb);
28660 +out:
28661 +       return err;
28662 +}
28663 +
28664 +/* ---------------------------------------------------------------------- */
28665 +
28666 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
28667 +{
28668 +       int err;
28669 +       int16_t brid;
28670 +       aufs_bindex_t bindex, bend;
28671 +       size_t sz;
28672 +       char *buf;
28673 +       struct seq_file *seq;
28674 +       struct au_branch *br;
28675 +
28676 +       si_read_lock(sb, AuLock_FLUSH);
28677 +       bend = au_sbend(sb);
28678 +       err = bend + 1;
28679 +       if (!arg)
28680 +               goto out;
28681 +
28682 +       err = -ENOMEM;
28683 +       buf = (void *)__get_free_page(GFP_NOFS);
28684 +       if (unlikely(!buf))
28685 +               goto out;
28686 +
28687 +       seq = au_seq(buf, PAGE_SIZE);
28688 +       err = PTR_ERR(seq);
28689 +       if (IS_ERR(seq))
28690 +               goto out_buf;
28691 +
28692 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
28693 +       for (bindex = 0; bindex <= bend; bindex++, arg++) {
28694 +               err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
28695 +               if (unlikely(err))
28696 +                       break;
28697 +
28698 +               br = au_sbr(sb, bindex);
28699 +               brid = br->br_id;
28700 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
28701 +               err = __put_user(brid, &arg->id);
28702 +               if (unlikely(err))
28703 +                       break;
28704 +
28705 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
28706 +               err = __put_user(br->br_perm, &arg->perm);
28707 +               if (unlikely(err))
28708 +                       break;
28709 +
28710 +               au_seq_path(seq, &br->br_path);
28711 +               err = seq_putc(seq, '\0');
28712 +               if (!err && seq->count <= sz) {
28713 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
28714 +                       seq->count = 0;
28715 +                       if (unlikely(err))
28716 +                               break;
28717 +               } else {
28718 +                       err = -E2BIG;
28719 +                       goto out_seq;
28720 +               }
28721 +       }
28722 +       if (unlikely(err))
28723 +               err = -EFAULT;
28724 +
28725 +out_seq:
28726 +       kfree(seq);
28727 +out_buf:
28728 +       free_page((unsigned long)buf);
28729 +out:
28730 +       si_read_unlock(sb);
28731 +       return err;
28732 +}
28733 +
28734 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
28735 +{
28736 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
28737 +}
28738 +
28739 +#ifdef CONFIG_COMPAT
28740 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
28741 +{
28742 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
28743 +}
28744 +#endif
28745 +
28746 +/* ---------------------------------------------------------------------- */
28747 +
28748 +void sysaufs_br_init(struct au_branch *br)
28749 +{
28750 +       int i;
28751 +       struct au_brsysfs *br_sysfs;
28752 +       struct attribute *attr;
28753 +
28754 +       br_sysfs = br->br_sysfs;
28755 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28756 +               attr = &br_sysfs->attr;
28757 +               sysfs_attr_init(attr);
28758 +               attr->name = br_sysfs->name;
28759 +               attr->mode = S_IRUGO;
28760 +               br_sysfs++;
28761 +       }
28762 +}
28763 +
28764 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
28765 +{
28766 +       struct au_branch *br;
28767 +       struct kobject *kobj;
28768 +       struct au_brsysfs *br_sysfs;
28769 +       int i;
28770 +       aufs_bindex_t bend;
28771 +
28772 +       dbgaufs_brs_del(sb, bindex);
28773 +
28774 +       if (!sysaufs_brs)
28775 +               return;
28776 +
28777 +       kobj = &au_sbi(sb)->si_kobj;
28778 +       bend = au_sbend(sb);
28779 +       for (; bindex <= bend; bindex++) {
28780 +               br = au_sbr(sb, bindex);
28781 +               br_sysfs = br->br_sysfs;
28782 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28783 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
28784 +                       br_sysfs++;
28785 +               }
28786 +       }
28787 +}
28788 +
28789 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
28790 +{
28791 +       int err, i;
28792 +       aufs_bindex_t bend;
28793 +       struct kobject *kobj;
28794 +       struct au_branch *br;
28795 +       struct au_brsysfs *br_sysfs;
28796 +
28797 +       dbgaufs_brs_add(sb, bindex);
28798 +
28799 +       if (!sysaufs_brs)
28800 +               return;
28801 +
28802 +       kobj = &au_sbi(sb)->si_kobj;
28803 +       bend = au_sbend(sb);
28804 +       for (; bindex <= bend; bindex++) {
28805 +               br = au_sbr(sb, bindex);
28806 +               br_sysfs = br->br_sysfs;
28807 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
28808 +                        SysaufsBr_PREFIX "%d", bindex);
28809 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
28810 +                        SysaufsBrid_PREFIX "%d", bindex);
28811 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
28812 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
28813 +                       if (unlikely(err))
28814 +                               pr_warn("failed %s under sysfs(%d)\n",
28815 +                                       br_sysfs->name, err);
28816 +                       br_sysfs++;
28817 +               }
28818 +       }
28819 +}
28820 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
28821 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
28822 +++ linux/fs/aufs/sysrq.c       2015-09-24 10:47:58.254719746 +0200
28823 @@ -0,0 +1,157 @@
28824 +/*
28825 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28826 + *
28827 + * This program, aufs is free software; you can redistribute it and/or modify
28828 + * it under the terms of the GNU General Public License as published by
28829 + * the Free Software Foundation; either version 2 of the License, or
28830 + * (at your option) any later version.
28831 + *
28832 + * This program is distributed in the hope that it will be useful,
28833 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28834 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28835 + * GNU General Public License for more details.
28836 + *
28837 + * You should have received a copy of the GNU General Public License
28838 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28839 + */
28840 +
28841 +/*
28842 + * magic sysrq hanlder
28843 + */
28844 +
28845 +/* #include <linux/sysrq.h> */
28846 +#include <linux/writeback.h>
28847 +#include "aufs.h"
28848 +
28849 +/* ---------------------------------------------------------------------- */
28850 +
28851 +static void sysrq_sb(struct super_block *sb)
28852 +{
28853 +       char *plevel;
28854 +       struct au_sbinfo *sbinfo;
28855 +       struct file *file;
28856 +       struct au_sphlhead *files;
28857 +       struct au_finfo *finfo;
28858 +
28859 +       plevel = au_plevel;
28860 +       au_plevel = KERN_WARNING;
28861 +
28862 +       /* since we define pr_fmt, call printk directly */
28863 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
28864 +
28865 +       sbinfo = au_sbi(sb);
28866 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
28867 +       pr("superblock\n");
28868 +       au_dpri_sb(sb);
28869 +
28870 +#if 0
28871 +       pr("root dentry\n");
28872 +       au_dpri_dentry(sb->s_root);
28873 +       pr("root inode\n");
28874 +       au_dpri_inode(d_inode(sb->s_root));
28875 +#endif
28876 +
28877 +#if 0
28878 +       do {
28879 +               int err, i, j, ndentry;
28880 +               struct au_dcsub_pages dpages;
28881 +               struct au_dpage *dpage;
28882 +
28883 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
28884 +               if (unlikely(err))
28885 +                       break;
28886 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
28887 +               if (!err)
28888 +                       for (i = 0; i < dpages.ndpage; i++) {
28889 +                               dpage = dpages.dpages + i;
28890 +                               ndentry = dpage->ndentry;
28891 +                               for (j = 0; j < ndentry; j++)
28892 +                                       au_dpri_dentry(dpage->dentries[j]);
28893 +                       }
28894 +               au_dpages_free(&dpages);
28895 +       } while (0);
28896 +#endif
28897 +
28898 +#if 1
28899 +       {
28900 +               struct inode *i;
28901 +
28902 +               pr("isolated inode\n");
28903 +               spin_lock(&inode_sb_list_lock);
28904 +               list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
28905 +                       spin_lock(&i->i_lock);
28906 +                       if (1 || hlist_empty(&i->i_dentry))
28907 +                               au_dpri_inode(i);
28908 +                       spin_unlock(&i->i_lock);
28909 +               }
28910 +               spin_unlock(&inode_sb_list_lock);
28911 +       }
28912 +#endif
28913 +       pr("files\n");
28914 +       files = &au_sbi(sb)->si_files;
28915 +       spin_lock(&files->spin);
28916 +       hlist_for_each_entry(finfo, &files->head, fi_hlist) {
28917 +               umode_t mode;
28918 +
28919 +               file = finfo->fi_file;
28920 +               mode = file_inode(file)->i_mode;
28921 +               if (!special_file(mode))
28922 +                       au_dpri_file(file);
28923 +       }
28924 +       spin_unlock(&files->spin);
28925 +       pr("done\n");
28926 +
28927 +#undef pr
28928 +       au_plevel = plevel;
28929 +}
28930 +
28931 +/* ---------------------------------------------------------------------- */
28932 +
28933 +/* module parameter */
28934 +static char *aufs_sysrq_key = "a";
28935 +module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
28936 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
28937 +
28938 +static void au_sysrq(int key __maybe_unused)
28939 +{
28940 +       struct au_sbinfo *sbinfo;
28941 +
28942 +       lockdep_off();
28943 +       au_sbilist_lock();
28944 +       list_for_each_entry(sbinfo, &au_sbilist.head, si_list)
28945 +               sysrq_sb(sbinfo->si_sb);
28946 +       au_sbilist_unlock();
28947 +       lockdep_on();
28948 +}
28949 +
28950 +static struct sysrq_key_op au_sysrq_op = {
28951 +       .handler        = au_sysrq,
28952 +       .help_msg       = "Aufs",
28953 +       .action_msg     = "Aufs",
28954 +       .enable_mask    = SYSRQ_ENABLE_DUMP
28955 +};
28956 +
28957 +/* ---------------------------------------------------------------------- */
28958 +
28959 +int __init au_sysrq_init(void)
28960 +{
28961 +       int err;
28962 +       char key;
28963 +
28964 +       err = -1;
28965 +       key = *aufs_sysrq_key;
28966 +       if ('a' <= key && key <= 'z')
28967 +               err = register_sysrq_key(key, &au_sysrq_op);
28968 +       if (unlikely(err))
28969 +               pr_err("err %d, sysrq=%c\n", err, key);
28970 +       return err;
28971 +}
28972 +
28973 +void au_sysrq_fin(void)
28974 +{
28975 +       int err;
28976 +
28977 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
28978 +       if (unlikely(err))
28979 +               pr_err("err %d (ignored)\n", err);
28980 +}
28981 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
28982 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
28983 +++ linux/fs/aufs/vdir.c        2015-09-24 10:47:58.258053165 +0200
28984 @@ -0,0 +1,888 @@
28985 +/*
28986 + * Copyright (C) 2005-2015 Junjiro R. Okajima
28987 + *
28988 + * This program, aufs is free software; you can redistribute it and/or modify
28989 + * it under the terms of the GNU General Public License as published by
28990 + * the Free Software Foundation; either version 2 of the License, or
28991 + * (at your option) any later version.
28992 + *
28993 + * This program is distributed in the hope that it will be useful,
28994 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28995 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28996 + * GNU General Public License for more details.
28997 + *
28998 + * You should have received a copy of the GNU General Public License
28999 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29000 + */
29001 +
29002 +/*
29003 + * virtual or vertical directory
29004 + */
29005 +
29006 +#include "aufs.h"
29007 +
29008 +static unsigned int calc_size(int nlen)
29009 +{
29010 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
29011 +}
29012 +
29013 +static int set_deblk_end(union au_vdir_deblk_p *p,
29014 +                        union au_vdir_deblk_p *deblk_end)
29015 +{
29016 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29017 +               p->de->de_str.len = 0;
29018 +               /* smp_mb(); */
29019 +               return 0;
29020 +       }
29021 +       return -1; /* error */
29022 +}
29023 +
29024 +/* returns true or false */
29025 +static int is_deblk_end(union au_vdir_deblk_p *p,
29026 +                       union au_vdir_deblk_p *deblk_end)
29027 +{
29028 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
29029 +               return !p->de->de_str.len;
29030 +       return 1;
29031 +}
29032 +
29033 +static unsigned char *last_deblk(struct au_vdir *vdir)
29034 +{
29035 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
29036 +}
29037 +
29038 +/* ---------------------------------------------------------------------- */
29039 +
29040 +/* estimate the apropriate size for name hash table */
29041 +unsigned int au_rdhash_est(loff_t sz)
29042 +{
29043 +       unsigned int n;
29044 +
29045 +       n = UINT_MAX;
29046 +       sz >>= 10;
29047 +       if (sz < n)
29048 +               n = sz;
29049 +       if (sz < AUFS_RDHASH_DEF)
29050 +               n = AUFS_RDHASH_DEF;
29051 +       /* pr_info("n %u\n", n); */
29052 +       return n;
29053 +}
29054 +
29055 +/*
29056 + * the allocated memory has to be freed by
29057 + * au_nhash_wh_free() or au_nhash_de_free().
29058 + */
29059 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
29060 +{
29061 +       struct hlist_head *head;
29062 +       unsigned int u;
29063 +       size_t sz;
29064 +
29065 +       sz = sizeof(*nhash->nh_head) * num_hash;
29066 +       head = kmalloc(sz, gfp);
29067 +       if (head) {
29068 +               nhash->nh_num = num_hash;
29069 +               nhash->nh_head = head;
29070 +               for (u = 0; u < num_hash; u++)
29071 +                       INIT_HLIST_HEAD(head++);
29072 +               return 0; /* success */
29073 +       }
29074 +
29075 +       return -ENOMEM;
29076 +}
29077 +
29078 +static void nhash_count(struct hlist_head *head)
29079 +{
29080 +#if 0
29081 +       unsigned long n;
29082 +       struct hlist_node *pos;
29083 +
29084 +       n = 0;
29085 +       hlist_for_each(pos, head)
29086 +               n++;
29087 +       pr_info("%lu\n", n);
29088 +#endif
29089 +}
29090 +
29091 +static void au_nhash_wh_do_free(struct hlist_head *head)
29092 +{
29093 +       struct au_vdir_wh *pos;
29094 +       struct hlist_node *node;
29095 +
29096 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
29097 +               kfree(pos);
29098 +}
29099 +
29100 +static void au_nhash_de_do_free(struct hlist_head *head)
29101 +{
29102 +       struct au_vdir_dehstr *pos;
29103 +       struct hlist_node *node;
29104 +
29105 +       hlist_for_each_entry_safe(pos, node, head, hash)
29106 +               au_cache_free_vdir_dehstr(pos);
29107 +}
29108 +
29109 +static void au_nhash_do_free(struct au_nhash *nhash,
29110 +                            void (*free)(struct hlist_head *head))
29111 +{
29112 +       unsigned int n;
29113 +       struct hlist_head *head;
29114 +
29115 +       n = nhash->nh_num;
29116 +       if (!n)
29117 +               return;
29118 +
29119 +       head = nhash->nh_head;
29120 +       while (n-- > 0) {
29121 +               nhash_count(head);
29122 +               free(head++);
29123 +       }
29124 +       kfree(nhash->nh_head);
29125 +}
29126 +
29127 +void au_nhash_wh_free(struct au_nhash *whlist)
29128 +{
29129 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
29130 +}
29131 +
29132 +static void au_nhash_de_free(struct au_nhash *delist)
29133 +{
29134 +       au_nhash_do_free(delist, au_nhash_de_do_free);
29135 +}
29136 +
29137 +/* ---------------------------------------------------------------------- */
29138 +
29139 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29140 +                           int limit)
29141 +{
29142 +       int num;
29143 +       unsigned int u, n;
29144 +       struct hlist_head *head;
29145 +       struct au_vdir_wh *pos;
29146 +
29147 +       num = 0;
29148 +       n = whlist->nh_num;
29149 +       head = whlist->nh_head;
29150 +       for (u = 0; u < n; u++, head++)
29151 +               hlist_for_each_entry(pos, head, wh_hash)
29152 +                       if (pos->wh_bindex == btgt && ++num > limit)
29153 +                               return 1;
29154 +       return 0;
29155 +}
29156 +
29157 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
29158 +                                      unsigned char *name,
29159 +                                      unsigned int len)
29160 +{
29161 +       unsigned int v;
29162 +       /* const unsigned int magic_bit = 12; */
29163 +
29164 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29165 +
29166 +       v = 0;
29167 +       while (len--)
29168 +               v += *name++;
29169 +       /* v = hash_long(v, magic_bit); */
29170 +       v %= nhash->nh_num;
29171 +       return nhash->nh_head + v;
29172 +}
29173 +
29174 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29175 +                             int nlen)
29176 +{
29177 +       return str->len == nlen && !memcmp(str->name, name, nlen);
29178 +}
29179 +
29180 +/* returns found or not */
29181 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
29182 +{
29183 +       struct hlist_head *head;
29184 +       struct au_vdir_wh *pos;
29185 +       struct au_vdir_destr *str;
29186 +
29187 +       head = au_name_hash(whlist, name, nlen);
29188 +       hlist_for_each_entry(pos, head, wh_hash) {
29189 +               str = &pos->wh_str;
29190 +               AuDbg("%.*s\n", str->len, str->name);
29191 +               if (au_nhash_test_name(str, name, nlen))
29192 +                       return 1;
29193 +       }
29194 +       return 0;
29195 +}
29196 +
29197 +/* returns found(true) or not */
29198 +static int test_known(struct au_nhash *delist, char *name, int nlen)
29199 +{
29200 +       struct hlist_head *head;
29201 +       struct au_vdir_dehstr *pos;
29202 +       struct au_vdir_destr *str;
29203 +
29204 +       head = au_name_hash(delist, name, nlen);
29205 +       hlist_for_each_entry(pos, head, hash) {
29206 +               str = pos->str;
29207 +               AuDbg("%.*s\n", str->len, str->name);
29208 +               if (au_nhash_test_name(str, name, nlen))
29209 +                       return 1;
29210 +       }
29211 +       return 0;
29212 +}
29213 +
29214 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29215 +                           unsigned char d_type)
29216 +{
29217 +#ifdef CONFIG_AUFS_SHWH
29218 +       wh->wh_ino = ino;
29219 +       wh->wh_type = d_type;
29220 +#endif
29221 +}
29222 +
29223 +/* ---------------------------------------------------------------------- */
29224 +
29225 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29226 +                      unsigned int d_type, aufs_bindex_t bindex,
29227 +                      unsigned char shwh)
29228 +{
29229 +       int err;
29230 +       struct au_vdir_destr *str;
29231 +       struct au_vdir_wh *wh;
29232 +
29233 +       AuDbg("%.*s\n", nlen, name);
29234 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29235 +
29236 +       err = -ENOMEM;
29237 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
29238 +       if (unlikely(!wh))
29239 +               goto out;
29240 +
29241 +       err = 0;
29242 +       wh->wh_bindex = bindex;
29243 +       if (shwh)
29244 +               au_shwh_init_wh(wh, ino, d_type);
29245 +       str = &wh->wh_str;
29246 +       str->len = nlen;
29247 +       memcpy(str->name, name, nlen);
29248 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
29249 +       /* smp_mb(); */
29250 +
29251 +out:
29252 +       return err;
29253 +}
29254 +
29255 +static int append_deblk(struct au_vdir *vdir)
29256 +{
29257 +       int err;
29258 +       unsigned long ul;
29259 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29260 +       union au_vdir_deblk_p p, deblk_end;
29261 +       unsigned char **o;
29262 +
29263 +       err = -ENOMEM;
29264 +       o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29265 +                    GFP_NOFS);
29266 +       if (unlikely(!o))
29267 +               goto out;
29268 +
29269 +       vdir->vd_deblk = o;
29270 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29271 +       if (p.deblk) {
29272 +               ul = vdir->vd_nblk++;
29273 +               vdir->vd_deblk[ul] = p.deblk;
29274 +               vdir->vd_last.ul = ul;
29275 +               vdir->vd_last.p.deblk = p.deblk;
29276 +               deblk_end.deblk = p.deblk + deblk_sz;
29277 +               err = set_deblk_end(&p, &deblk_end);
29278 +       }
29279 +
29280 +out:
29281 +       return err;
29282 +}
29283 +
29284 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29285 +                    unsigned int d_type, struct au_nhash *delist)
29286 +{
29287 +       int err;
29288 +       unsigned int sz;
29289 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
29290 +       union au_vdir_deblk_p p, *room, deblk_end;
29291 +       struct au_vdir_dehstr *dehstr;
29292 +
29293 +       p.deblk = last_deblk(vdir);
29294 +       deblk_end.deblk = p.deblk + deblk_sz;
29295 +       room = &vdir->vd_last.p;
29296 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29297 +                 || !is_deblk_end(room, &deblk_end));
29298 +
29299 +       sz = calc_size(nlen);
29300 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29301 +               err = append_deblk(vdir);
29302 +               if (unlikely(err))
29303 +                       goto out;
29304 +
29305 +               p.deblk = last_deblk(vdir);
29306 +               deblk_end.deblk = p.deblk + deblk_sz;
29307 +               /* smp_mb(); */
29308 +               AuDebugOn(room->deblk != p.deblk);
29309 +       }
29310 +
29311 +       err = -ENOMEM;
29312 +       dehstr = au_cache_alloc_vdir_dehstr();
29313 +       if (unlikely(!dehstr))
29314 +               goto out;
29315 +
29316 +       dehstr->str = &room->de->de_str;
29317 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29318 +       room->de->de_ino = ino;
29319 +       room->de->de_type = d_type;
29320 +       room->de->de_str.len = nlen;
29321 +       memcpy(room->de->de_str.name, name, nlen);
29322 +
29323 +       err = 0;
29324 +       room->deblk += sz;
29325 +       if (unlikely(set_deblk_end(room, &deblk_end)))
29326 +               err = append_deblk(vdir);
29327 +       /* smp_mb(); */
29328 +
29329 +out:
29330 +       return err;
29331 +}
29332 +
29333 +/* ---------------------------------------------------------------------- */
29334 +
29335 +void au_vdir_free(struct au_vdir *vdir)
29336 +{
29337 +       unsigned char **deblk;
29338 +
29339 +       deblk = vdir->vd_deblk;
29340 +       while (vdir->vd_nblk--)
29341 +               kfree(*deblk++);
29342 +       kfree(vdir->vd_deblk);
29343 +       au_cache_free_vdir(vdir);
29344 +}
29345 +
29346 +static struct au_vdir *alloc_vdir(struct file *file)
29347 +{
29348 +       struct au_vdir *vdir;
29349 +       struct super_block *sb;
29350 +       int err;
29351 +
29352 +       sb = file->f_path.dentry->d_sb;
29353 +       SiMustAnyLock(sb);
29354 +
29355 +       err = -ENOMEM;
29356 +       vdir = au_cache_alloc_vdir();
29357 +       if (unlikely(!vdir))
29358 +               goto out;
29359 +
29360 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
29361 +       if (unlikely(!vdir->vd_deblk))
29362 +               goto out_free;
29363 +
29364 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
29365 +       if (!vdir->vd_deblk_sz) {
29366 +               /* estimate the apropriate size for deblk */
29367 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
29368 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
29369 +       }
29370 +       vdir->vd_nblk = 0;
29371 +       vdir->vd_version = 0;
29372 +       vdir->vd_jiffy = 0;
29373 +       err = append_deblk(vdir);
29374 +       if (!err)
29375 +               return vdir; /* success */
29376 +
29377 +       kfree(vdir->vd_deblk);
29378 +
29379 +out_free:
29380 +       au_cache_free_vdir(vdir);
29381 +out:
29382 +       vdir = ERR_PTR(err);
29383 +       return vdir;
29384 +}
29385 +
29386 +static int reinit_vdir(struct au_vdir *vdir)
29387 +{
29388 +       int err;
29389 +       union au_vdir_deblk_p p, deblk_end;
29390 +
29391 +       while (vdir->vd_nblk > 1) {
29392 +               kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
29393 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
29394 +               vdir->vd_nblk--;
29395 +       }
29396 +       p.deblk = vdir->vd_deblk[0];
29397 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
29398 +       err = set_deblk_end(&p, &deblk_end);
29399 +       /* keep vd_dblk_sz */
29400 +       vdir->vd_last.ul = 0;
29401 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29402 +       vdir->vd_version = 0;
29403 +       vdir->vd_jiffy = 0;
29404 +       /* smp_mb(); */
29405 +       return err;
29406 +}
29407 +
29408 +/* ---------------------------------------------------------------------- */
29409 +
29410 +#define AuFillVdir_CALLED      1
29411 +#define AuFillVdir_WHABLE      (1 << 1)
29412 +#define AuFillVdir_SHWH                (1 << 2)
29413 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
29414 +#define au_fset_fillvdir(flags, name) \
29415 +       do { (flags) |= AuFillVdir_##name; } while (0)
29416 +#define au_fclr_fillvdir(flags, name) \
29417 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
29418 +
29419 +#ifndef CONFIG_AUFS_SHWH
29420 +#undef AuFillVdir_SHWH
29421 +#define AuFillVdir_SHWH                0
29422 +#endif
29423 +
29424 +struct fillvdir_arg {
29425 +       struct dir_context      ctx;
29426 +       struct file             *file;
29427 +       struct au_vdir          *vdir;
29428 +       struct au_nhash         delist;
29429 +       struct au_nhash         whlist;
29430 +       aufs_bindex_t           bindex;
29431 +       unsigned int            flags;
29432 +       int                     err;
29433 +};
29434 +
29435 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
29436 +                   loff_t offset __maybe_unused, u64 h_ino,
29437 +                   unsigned int d_type)
29438 +{
29439 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
29440 +       char *name = (void *)__name;
29441 +       struct super_block *sb;
29442 +       ino_t ino;
29443 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
29444 +
29445 +       arg->err = 0;
29446 +       sb = arg->file->f_path.dentry->d_sb;
29447 +       au_fset_fillvdir(arg->flags, CALLED);
29448 +       /* smp_mb(); */
29449 +       if (nlen <= AUFS_WH_PFX_LEN
29450 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
29451 +               if (test_known(&arg->delist, name, nlen)
29452 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
29453 +                       goto out; /* already exists or whiteouted */
29454 +
29455 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
29456 +               if (!arg->err) {
29457 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
29458 +                               d_type = DT_UNKNOWN;
29459 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
29460 +                                            d_type, &arg->delist);
29461 +               }
29462 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
29463 +               name += AUFS_WH_PFX_LEN;
29464 +               nlen -= AUFS_WH_PFX_LEN;
29465 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29466 +                       goto out; /* already whiteouted */
29467 +
29468 +               if (shwh)
29469 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29470 +                                            &ino);
29471 +               if (!arg->err) {
29472 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29473 +                               d_type = DT_UNKNOWN;
29474 +                       arg->err = au_nhash_append_wh
29475 +                               (&arg->whlist, name, nlen, ino, d_type,
29476 +                                arg->bindex, shwh);
29477 +               }
29478 +       }
29479 +
29480 +out:
29481 +       if (!arg->err)
29482 +               arg->vdir->vd_jiffy = jiffies;
29483 +       /* smp_mb(); */
29484 +       AuTraceErr(arg->err);
29485 +       return arg->err;
29486 +}
29487 +
29488 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29489 +                         struct au_nhash *whlist, struct au_nhash *delist)
29490 +{
29491 +#ifdef CONFIG_AUFS_SHWH
29492 +       int err;
29493 +       unsigned int nh, u;
29494 +       struct hlist_head *head;
29495 +       struct au_vdir_wh *pos;
29496 +       struct hlist_node *n;
29497 +       char *p, *o;
29498 +       struct au_vdir_destr *destr;
29499 +
29500 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29501 +
29502 +       err = -ENOMEM;
29503 +       o = p = (void *)__get_free_page(GFP_NOFS);
29504 +       if (unlikely(!p))
29505 +               goto out;
29506 +
29507 +       err = 0;
29508 +       nh = whlist->nh_num;
29509 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29510 +       p += AUFS_WH_PFX_LEN;
29511 +       for (u = 0; u < nh; u++) {
29512 +               head = whlist->nh_head + u;
29513 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29514 +                       destr = &pos->wh_str;
29515 +                       memcpy(p, destr->name, destr->len);
29516 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
29517 +                                       pos->wh_ino, pos->wh_type, delist);
29518 +                       if (unlikely(err))
29519 +                               break;
29520 +               }
29521 +       }
29522 +
29523 +       free_page((unsigned long)o);
29524 +
29525 +out:
29526 +       AuTraceErr(err);
29527 +       return err;
29528 +#else
29529 +       return 0;
29530 +#endif
29531 +}
29532 +
29533 +static int au_do_read_vdir(struct fillvdir_arg *arg)
29534 +{
29535 +       int err;
29536 +       unsigned int rdhash;
29537 +       loff_t offset;
29538 +       aufs_bindex_t bend, bindex, bstart;
29539 +       unsigned char shwh;
29540 +       struct file *hf, *file;
29541 +       struct super_block *sb;
29542 +
29543 +       file = arg->file;
29544 +       sb = file->f_path.dentry->d_sb;
29545 +       SiMustAnyLock(sb);
29546 +
29547 +       rdhash = au_sbi(sb)->si_rdhash;
29548 +       if (!rdhash)
29549 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
29550 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
29551 +       if (unlikely(err))
29552 +               goto out;
29553 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
29554 +       if (unlikely(err))
29555 +               goto out_delist;
29556 +
29557 +       err = 0;
29558 +       arg->flags = 0;
29559 +       shwh = 0;
29560 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
29561 +               shwh = 1;
29562 +               au_fset_fillvdir(arg->flags, SHWH);
29563 +       }
29564 +       bstart = au_fbstart(file);
29565 +       bend = au_fbend_dir(file);
29566 +       for (bindex = bstart; !err && bindex <= bend; bindex++) {
29567 +               hf = au_hf_dir(file, bindex);
29568 +               if (!hf)
29569 +                       continue;
29570 +
29571 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
29572 +               err = offset;
29573 +               if (unlikely(offset))
29574 +                       break;
29575 +
29576 +               arg->bindex = bindex;
29577 +               au_fclr_fillvdir(arg->flags, WHABLE);
29578 +               if (shwh
29579 +                   || (bindex != bend
29580 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
29581 +                       au_fset_fillvdir(arg->flags, WHABLE);
29582 +               do {
29583 +                       arg->err = 0;
29584 +                       au_fclr_fillvdir(arg->flags, CALLED);
29585 +                       /* smp_mb(); */
29586 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
29587 +                       if (err >= 0)
29588 +                               err = arg->err;
29589 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
29590 +
29591 +               /*
29592 +                * dir_relax() may be good for concurrency, but aufs should not
29593 +                * use it since it will cause a lockdep problem.
29594 +                */
29595 +       }
29596 +
29597 +       if (!err && shwh)
29598 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
29599 +
29600 +       au_nhash_wh_free(&arg->whlist);
29601 +
29602 +out_delist:
29603 +       au_nhash_de_free(&arg->delist);
29604 +out:
29605 +       return err;
29606 +}
29607 +
29608 +static int read_vdir(struct file *file, int may_read)
29609 +{
29610 +       int err;
29611 +       unsigned long expire;
29612 +       unsigned char do_read;
29613 +       struct fillvdir_arg arg = {
29614 +               .ctx = {
29615 +                       .actor = fillvdir
29616 +               }
29617 +       };
29618 +       struct inode *inode;
29619 +       struct au_vdir *vdir, *allocated;
29620 +
29621 +       err = 0;
29622 +       inode = file_inode(file);
29623 +       IMustLock(inode);
29624 +       SiMustAnyLock(inode->i_sb);
29625 +
29626 +       allocated = NULL;
29627 +       do_read = 0;
29628 +       expire = au_sbi(inode->i_sb)->si_rdcache;
29629 +       vdir = au_ivdir(inode);
29630 +       if (!vdir) {
29631 +               do_read = 1;
29632 +               vdir = alloc_vdir(file);
29633 +               err = PTR_ERR(vdir);
29634 +               if (IS_ERR(vdir))
29635 +                       goto out;
29636 +               err = 0;
29637 +               allocated = vdir;
29638 +       } else if (may_read
29639 +                  && (inode->i_version != vdir->vd_version
29640 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
29641 +               do_read = 1;
29642 +               err = reinit_vdir(vdir);
29643 +               if (unlikely(err))
29644 +                       goto out;
29645 +       }
29646 +
29647 +       if (!do_read)
29648 +               return 0; /* success */
29649 +
29650 +       arg.file = file;
29651 +       arg.vdir = vdir;
29652 +       err = au_do_read_vdir(&arg);
29653 +       if (!err) {
29654 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
29655 +               vdir->vd_version = inode->i_version;
29656 +               vdir->vd_last.ul = 0;
29657 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29658 +               if (allocated)
29659 +                       au_set_ivdir(inode, allocated);
29660 +       } else if (allocated)
29661 +               au_vdir_free(allocated);
29662 +
29663 +out:
29664 +       return err;
29665 +}
29666 +
29667 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
29668 +{
29669 +       int err, rerr;
29670 +       unsigned long ul, n;
29671 +       const unsigned int deblk_sz = src->vd_deblk_sz;
29672 +
29673 +       AuDebugOn(tgt->vd_nblk != 1);
29674 +
29675 +       err = -ENOMEM;
29676 +       if (tgt->vd_nblk < src->vd_nblk) {
29677 +               unsigned char **p;
29678 +
29679 +               p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
29680 +                            GFP_NOFS);
29681 +               if (unlikely(!p))
29682 +                       goto out;
29683 +               tgt->vd_deblk = p;
29684 +       }
29685 +
29686 +       if (tgt->vd_deblk_sz != deblk_sz) {
29687 +               unsigned char *p;
29688 +
29689 +               tgt->vd_deblk_sz = deblk_sz;
29690 +               p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
29691 +               if (unlikely(!p))
29692 +                       goto out;
29693 +               tgt->vd_deblk[0] = p;
29694 +       }
29695 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
29696 +       tgt->vd_version = src->vd_version;
29697 +       tgt->vd_jiffy = src->vd_jiffy;
29698 +
29699 +       n = src->vd_nblk;
29700 +       for (ul = 1; ul < n; ul++) {
29701 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
29702 +                                           GFP_NOFS);
29703 +               if (unlikely(!tgt->vd_deblk[ul]))
29704 +                       goto out;
29705 +               tgt->vd_nblk++;
29706 +       }
29707 +       tgt->vd_nblk = n;
29708 +       tgt->vd_last.ul = tgt->vd_last.ul;
29709 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
29710 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
29711 +               - src->vd_deblk[src->vd_last.ul];
29712 +       /* smp_mb(); */
29713 +       return 0; /* success */
29714 +
29715 +out:
29716 +       rerr = reinit_vdir(tgt);
29717 +       BUG_ON(rerr);
29718 +       return err;
29719 +}
29720 +
29721 +int au_vdir_init(struct file *file)
29722 +{
29723 +       int err;
29724 +       struct inode *inode;
29725 +       struct au_vdir *vdir_cache, *allocated;
29726 +
29727 +       /* test file->f_pos here instead of ctx->pos */
29728 +       err = read_vdir(file, !file->f_pos);
29729 +       if (unlikely(err))
29730 +               goto out;
29731 +
29732 +       allocated = NULL;
29733 +       vdir_cache = au_fvdir_cache(file);
29734 +       if (!vdir_cache) {
29735 +               vdir_cache = alloc_vdir(file);
29736 +               err = PTR_ERR(vdir_cache);
29737 +               if (IS_ERR(vdir_cache))
29738 +                       goto out;
29739 +               allocated = vdir_cache;
29740 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
29741 +               /* test file->f_pos here instead of ctx->pos */
29742 +               err = reinit_vdir(vdir_cache);
29743 +               if (unlikely(err))
29744 +                       goto out;
29745 +       } else
29746 +               return 0; /* success */
29747 +
29748 +       inode = file_inode(file);
29749 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
29750 +       if (!err) {
29751 +               file->f_version = inode->i_version;
29752 +               if (allocated)
29753 +                       au_set_fvdir_cache(file, allocated);
29754 +       } else if (allocated)
29755 +               au_vdir_free(allocated);
29756 +
29757 +out:
29758 +       return err;
29759 +}
29760 +
29761 +static loff_t calc_offset(struct au_vdir *vdir)
29762 +{
29763 +       loff_t offset;
29764 +       union au_vdir_deblk_p p;
29765 +
29766 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
29767 +       offset = vdir->vd_last.p.deblk - p.deblk;
29768 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
29769 +       return offset;
29770 +}
29771 +
29772 +/* returns true or false */
29773 +static int seek_vdir(struct file *file, struct dir_context *ctx)
29774 +{
29775 +       int valid;
29776 +       unsigned int deblk_sz;
29777 +       unsigned long ul, n;
29778 +       loff_t offset;
29779 +       union au_vdir_deblk_p p, deblk_end;
29780 +       struct au_vdir *vdir_cache;
29781 +
29782 +       valid = 1;
29783 +       vdir_cache = au_fvdir_cache(file);
29784 +       offset = calc_offset(vdir_cache);
29785 +       AuDbg("offset %lld\n", offset);
29786 +       if (ctx->pos == offset)
29787 +               goto out;
29788 +
29789 +       vdir_cache->vd_last.ul = 0;
29790 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
29791 +       if (!ctx->pos)
29792 +               goto out;
29793 +
29794 +       valid = 0;
29795 +       deblk_sz = vdir_cache->vd_deblk_sz;
29796 +       ul = div64_u64(ctx->pos, deblk_sz);
29797 +       AuDbg("ul %lu\n", ul);
29798 +       if (ul >= vdir_cache->vd_nblk)
29799 +               goto out;
29800 +
29801 +       n = vdir_cache->vd_nblk;
29802 +       for (; ul < n; ul++) {
29803 +               p.deblk = vdir_cache->vd_deblk[ul];
29804 +               deblk_end.deblk = p.deblk + deblk_sz;
29805 +               offset = ul;
29806 +               offset *= deblk_sz;
29807 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
29808 +                       unsigned int l;
29809 +
29810 +                       l = calc_size(p.de->de_str.len);
29811 +                       offset += l;
29812 +                       p.deblk += l;
29813 +               }
29814 +               if (!is_deblk_end(&p, &deblk_end)) {
29815 +                       valid = 1;
29816 +                       vdir_cache->vd_last.ul = ul;
29817 +                       vdir_cache->vd_last.p = p;
29818 +                       break;
29819 +               }
29820 +       }
29821 +
29822 +out:
29823 +       /* smp_mb(); */
29824 +       AuTraceErr(!valid);
29825 +       return valid;
29826 +}
29827 +
29828 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
29829 +{
29830 +       unsigned int l, deblk_sz;
29831 +       union au_vdir_deblk_p deblk_end;
29832 +       struct au_vdir *vdir_cache;
29833 +       struct au_vdir_de *de;
29834 +
29835 +       vdir_cache = au_fvdir_cache(file);
29836 +       if (!seek_vdir(file, ctx))
29837 +               return 0;
29838 +
29839 +       deblk_sz = vdir_cache->vd_deblk_sz;
29840 +       while (1) {
29841 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29842 +               deblk_end.deblk += deblk_sz;
29843 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
29844 +                       de = vdir_cache->vd_last.p.de;
29845 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
29846 +                             de->de_str.len, de->de_str.name, ctx->pos,
29847 +                             (unsigned long)de->de_ino, de->de_type);
29848 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
29849 +                                              de->de_str.len, de->de_ino,
29850 +                                              de->de_type))) {
29851 +                               /* todo: ignore the error caused by udba? */
29852 +                               /* return err; */
29853 +                               return 0;
29854 +                       }
29855 +
29856 +                       l = calc_size(de->de_str.len);
29857 +                       vdir_cache->vd_last.p.deblk += l;
29858 +                       ctx->pos += l;
29859 +               }
29860 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
29861 +                       vdir_cache->vd_last.ul++;
29862 +                       vdir_cache->vd_last.p.deblk
29863 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
29864 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
29865 +                       continue;
29866 +               }
29867 +               break;
29868 +       }
29869 +
29870 +       /* smp_mb(); */
29871 +       return 0;
29872 +}
29873 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
29874 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
29875 +++ linux/fs/aufs/vfsub.c       2015-09-24 10:47:58.258053165 +0200
29876 @@ -0,0 +1,848 @@
29877 +/*
29878 + * Copyright (C) 2005-2015 Junjiro R. Okajima
29879 + *
29880 + * This program, aufs is free software; you can redistribute it and/or modify
29881 + * it under the terms of the GNU General Public License as published by
29882 + * the Free Software Foundation; either version 2 of the License, or
29883 + * (at your option) any later version.
29884 + *
29885 + * This program is distributed in the hope that it will be useful,
29886 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29887 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29888 + * GNU General Public License for more details.
29889 + *
29890 + * You should have received a copy of the GNU General Public License
29891 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29892 + */
29893 +
29894 +/*
29895 + * sub-routines for VFS
29896 + */
29897 +
29898 +#include <linux/namei.h>
29899 +#include <linux/security.h>
29900 +#include <linux/splice.h>
29901 +#include "aufs.h"
29902 +
29903 +int vfsub_update_h_iattr(struct path *h_path, int *did)
29904 +{
29905 +       int err;
29906 +       struct kstat st;
29907 +       struct super_block *h_sb;
29908 +
29909 +       /* for remote fs, leave work for its getattr or d_revalidate */
29910 +       /* for bad i_attr fs, handle them in aufs_getattr() */
29911 +       /* still some fs may acquire i_mutex. we need to skip them */
29912 +       err = 0;
29913 +       if (!did)
29914 +               did = &err;
29915 +       h_sb = h_path->dentry->d_sb;
29916 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
29917 +       if (*did)
29918 +               err = vfs_getattr(h_path, &st);
29919 +
29920 +       return err;
29921 +}
29922 +
29923 +/* ---------------------------------------------------------------------- */
29924 +
29925 +struct file *vfsub_dentry_open(struct path *path, int flags)
29926 +{
29927 +       struct file *file;
29928 +
29929 +       file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
29930 +                          current_cred());
29931 +       if (!IS_ERR_OR_NULL(file)
29932 +           && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
29933 +               i_readcount_inc(d_inode(path->dentry));
29934 +
29935 +       return file;
29936 +}
29937 +
29938 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
29939 +{
29940 +       struct file *file;
29941 +
29942 +       lockdep_off();
29943 +       file = filp_open(path,
29944 +                        oflags /* | __FMODE_NONOTIFY */,
29945 +                        mode);
29946 +       lockdep_on();
29947 +       if (IS_ERR(file))
29948 +               goto out;
29949 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
29950 +
29951 +out:
29952 +       return file;
29953 +}
29954 +
29955 +/*
29956 + * Ideally this function should call VFS:do_last() in order to keep all its
29957 + * checkings. But it is very hard for aufs to regenerate several VFS internal
29958 + * structure such as nameidata. This is a second (or third) best approach.
29959 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
29960 + */
29961 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
29962 +                     struct vfsub_aopen_args *args, struct au_branch *br)
29963 +{
29964 +       int err;
29965 +       struct file *file = args->file;
29966 +       /* copied from linux/fs/namei.c:atomic_open() */
29967 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
29968 +
29969 +       IMustLock(dir);
29970 +       AuDebugOn(!dir->i_op->atomic_open);
29971 +
29972 +       err = au_br_test_oflag(args->open_flag, br);
29973 +       if (unlikely(err))
29974 +               goto out;
29975 +
29976 +       args->file->f_path.dentry = DENTRY_NOT_SET;
29977 +       args->file->f_path.mnt = au_br_mnt(br);
29978 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
29979 +                                    args->create_mode, args->opened);
29980 +       if (err >= 0) {
29981 +               /* some filesystems don't set FILE_CREATED while succeeded? */
29982 +               if (*args->opened & FILE_CREATED)
29983 +                       fsnotify_create(dir, dentry);
29984 +       } else
29985 +               goto out;
29986 +
29987 +
29988 +       if (!err) {
29989 +               /* todo: call VFS:may_open() here */
29990 +               err = open_check_o_direct(file);
29991 +               /* todo: ima_file_check() too? */
29992 +               if (!err && (args->open_flag & __FMODE_EXEC))
29993 +                       err = deny_write_access(file);
29994 +               if (unlikely(err))
29995 +                       /* note that the file is created and still opened */
29996 +                       goto out;
29997 +       }
29998 +
29999 +       atomic_inc(&br->br_count);
30000 +       fsnotify_open(file);
30001 +
30002 +out:
30003 +       return err;
30004 +}
30005 +
30006 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30007 +{
30008 +       int err;
30009 +
30010 +       err = kern_path(name, flags, path);
30011 +       if (!err && d_is_positive(path->dentry))
30012 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30013 +       return err;
30014 +}
30015 +
30016 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30017 +                                   int len)
30018 +{
30019 +       struct path path = {
30020 +               .mnt = NULL
30021 +       };
30022 +
30023 +       /* VFS checks it too, but by WARN_ON_ONCE() */
30024 +       IMustLock(d_inode(parent));
30025 +
30026 +       path.dentry = lookup_one_len(name, parent, len);
30027 +       if (IS_ERR(path.dentry))
30028 +               goto out;
30029 +       if (d_is_positive(path.dentry))
30030 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30031 +
30032 +out:
30033 +       AuTraceErrPtr(path.dentry);
30034 +       return path.dentry;
30035 +}
30036 +
30037 +void vfsub_call_lkup_one(void *args)
30038 +{
30039 +       struct vfsub_lkup_one_args *a = args;
30040 +       *a->errp = vfsub_lkup_one(a->name, a->parent);
30041 +}
30042 +
30043 +/* ---------------------------------------------------------------------- */
30044 +
30045 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30046 +                                struct dentry *d2, struct au_hinode *hdir2)
30047 +{
30048 +       struct dentry *d;
30049 +
30050 +       lockdep_off();
30051 +       d = lock_rename(d1, d2);
30052 +       lockdep_on();
30053 +       au_hn_suspend(hdir1);
30054 +       if (hdir1 != hdir2)
30055 +               au_hn_suspend(hdir2);
30056 +
30057 +       return d;
30058 +}
30059 +
30060 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30061 +                        struct dentry *d2, struct au_hinode *hdir2)
30062 +{
30063 +       au_hn_resume(hdir1);
30064 +       if (hdir1 != hdir2)
30065 +               au_hn_resume(hdir2);
30066 +       lockdep_off();
30067 +       unlock_rename(d1, d2);
30068 +       lockdep_on();
30069 +}
30070 +
30071 +/* ---------------------------------------------------------------------- */
30072 +
30073 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
30074 +{
30075 +       int err;
30076 +       struct dentry *d;
30077 +
30078 +       IMustLock(dir);
30079 +
30080 +       d = path->dentry;
30081 +       path->dentry = d->d_parent;
30082 +       err = security_path_mknod(path, d, mode, 0);
30083 +       path->dentry = d;
30084 +       if (unlikely(err))
30085 +               goto out;
30086 +
30087 +       lockdep_off();
30088 +       err = vfs_create(dir, path->dentry, mode, want_excl);
30089 +       lockdep_on();
30090 +       if (!err) {
30091 +               struct path tmp = *path;
30092 +               int did;
30093 +
30094 +               vfsub_update_h_iattr(&tmp, &did);
30095 +               if (did) {
30096 +                       tmp.dentry = path->dentry->d_parent;
30097 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30098 +               }
30099 +               /*ignore*/
30100 +       }
30101 +
30102 +out:
30103 +       return err;
30104 +}
30105 +
30106 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30107 +{
30108 +       int err;
30109 +       struct dentry *d;
30110 +
30111 +       IMustLock(dir);
30112 +
30113 +       d = path->dentry;
30114 +       path->dentry = d->d_parent;
30115 +       err = security_path_symlink(path, d, symname);
30116 +       path->dentry = d;
30117 +       if (unlikely(err))
30118 +               goto out;
30119 +
30120 +       lockdep_off();
30121 +       err = vfs_symlink(dir, path->dentry, symname);
30122 +       lockdep_on();
30123 +       if (!err) {
30124 +               struct path tmp = *path;
30125 +               int did;
30126 +
30127 +               vfsub_update_h_iattr(&tmp, &did);
30128 +               if (did) {
30129 +                       tmp.dentry = path->dentry->d_parent;
30130 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30131 +               }
30132 +               /*ignore*/
30133 +       }
30134 +
30135 +out:
30136 +       return err;
30137 +}
30138 +
30139 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30140 +{
30141 +       int err;
30142 +       struct dentry *d;
30143 +
30144 +       IMustLock(dir);
30145 +
30146 +       d = path->dentry;
30147 +       path->dentry = d->d_parent;
30148 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
30149 +       path->dentry = d;
30150 +       if (unlikely(err))
30151 +               goto out;
30152 +
30153 +       lockdep_off();
30154 +       err = vfs_mknod(dir, path->dentry, mode, dev);
30155 +       lockdep_on();
30156 +       if (!err) {
30157 +               struct path tmp = *path;
30158 +               int did;
30159 +
30160 +               vfsub_update_h_iattr(&tmp, &did);
30161 +               if (did) {
30162 +                       tmp.dentry = path->dentry->d_parent;
30163 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30164 +               }
30165 +               /*ignore*/
30166 +       }
30167 +
30168 +out:
30169 +       return err;
30170 +}
30171 +
30172 +static int au_test_nlink(struct inode *inode)
30173 +{
30174 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30175 +
30176 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
30177 +           || inode->i_nlink < link_max)
30178 +               return 0;
30179 +       return -EMLINK;
30180 +}
30181 +
30182 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30183 +              struct inode **delegated_inode)
30184 +{
30185 +       int err;
30186 +       struct dentry *d;
30187 +
30188 +       IMustLock(dir);
30189 +
30190 +       err = au_test_nlink(d_inode(src_dentry));
30191 +       if (unlikely(err))
30192 +               return err;
30193 +
30194 +       /* we don't call may_linkat() */
30195 +       d = path->dentry;
30196 +       path->dentry = d->d_parent;
30197 +       err = security_path_link(src_dentry, path, d);
30198 +       path->dentry = d;
30199 +       if (unlikely(err))
30200 +               goto out;
30201 +
30202 +       lockdep_off();
30203 +       err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
30204 +       lockdep_on();
30205 +       if (!err) {
30206 +               struct path tmp = *path;
30207 +               int did;
30208 +
30209 +               /* fuse has different memory inode for the same inumber */
30210 +               vfsub_update_h_iattr(&tmp, &did);
30211 +               if (did) {
30212 +                       tmp.dentry = path->dentry->d_parent;
30213 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30214 +                       tmp.dentry = src_dentry;
30215 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30216 +               }
30217 +               /*ignore*/
30218 +       }
30219 +
30220 +out:
30221 +       return err;
30222 +}
30223 +
30224 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
30225 +                struct inode *dir, struct path *path,
30226 +                struct inode **delegated_inode)
30227 +{
30228 +       int err;
30229 +       struct path tmp = {
30230 +               .mnt    = path->mnt
30231 +       };
30232 +       struct dentry *d;
30233 +
30234 +       IMustLock(dir);
30235 +       IMustLock(src_dir);
30236 +
30237 +       d = path->dentry;
30238 +       path->dentry = d->d_parent;
30239 +       tmp.dentry = src_dentry->d_parent;
30240 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
30241 +       path->dentry = d;
30242 +       if (unlikely(err))
30243 +               goto out;
30244 +
30245 +       lockdep_off();
30246 +       err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
30247 +                        delegated_inode, /*flags*/0);
30248 +       lockdep_on();
30249 +       if (!err) {
30250 +               int did;
30251 +
30252 +               tmp.dentry = d->d_parent;
30253 +               vfsub_update_h_iattr(&tmp, &did);
30254 +               if (did) {
30255 +                       tmp.dentry = src_dentry;
30256 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30257 +                       tmp.dentry = src_dentry->d_parent;
30258 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30259 +               }
30260 +               /*ignore*/
30261 +       }
30262 +
30263 +out:
30264 +       return err;
30265 +}
30266 +
30267 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30268 +{
30269 +       int err;
30270 +       struct dentry *d;
30271 +
30272 +       IMustLock(dir);
30273 +
30274 +       d = path->dentry;
30275 +       path->dentry = d->d_parent;
30276 +       err = security_path_mkdir(path, d, mode);
30277 +       path->dentry = d;
30278 +       if (unlikely(err))
30279 +               goto out;
30280 +
30281 +       lockdep_off();
30282 +       err = vfs_mkdir(dir, path->dentry, mode);
30283 +       lockdep_on();
30284 +       if (!err) {
30285 +               struct path tmp = *path;
30286 +               int did;
30287 +
30288 +               vfsub_update_h_iattr(&tmp, &did);
30289 +               if (did) {
30290 +                       tmp.dentry = path->dentry->d_parent;
30291 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
30292 +               }
30293 +               /*ignore*/
30294 +       }
30295 +
30296 +out:
30297 +       return err;
30298 +}
30299 +
30300 +int vfsub_rmdir(struct inode *dir, struct path *path)
30301 +{
30302 +       int err;
30303 +       struct dentry *d;
30304 +
30305 +       IMustLock(dir);
30306 +
30307 +       d = path->dentry;
30308 +       path->dentry = d->d_parent;
30309 +       err = security_path_rmdir(path, d);
30310 +       path->dentry = d;
30311 +       if (unlikely(err))
30312 +               goto out;
30313 +
30314 +       lockdep_off();
30315 +       err = vfs_rmdir(dir, path->dentry);
30316 +       lockdep_on();
30317 +       if (!err) {
30318 +               struct path tmp = {
30319 +                       .dentry = path->dentry->d_parent,
30320 +                       .mnt    = path->mnt
30321 +               };
30322 +
30323 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30324 +       }
30325 +
30326 +out:
30327 +       return err;
30328 +}
30329 +
30330 +/* ---------------------------------------------------------------------- */
30331 +
30332 +/* todo: support mmap_sem? */
30333 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30334 +                    loff_t *ppos)
30335 +{
30336 +       ssize_t err;
30337 +
30338 +       lockdep_off();
30339 +       err = vfs_read(file, ubuf, count, ppos);
30340 +       lockdep_on();
30341 +       if (err >= 0)
30342 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30343 +       return err;
30344 +}
30345 +
30346 +/* todo: kernel_read()? */
30347 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30348 +                    loff_t *ppos)
30349 +{
30350 +       ssize_t err;
30351 +       mm_segment_t oldfs;
30352 +       union {
30353 +               void *k;
30354 +               char __user *u;
30355 +       } buf;
30356 +
30357 +       buf.k = kbuf;
30358 +       oldfs = get_fs();
30359 +       set_fs(KERNEL_DS);
30360 +       err = vfsub_read_u(file, buf.u, count, ppos);
30361 +       set_fs(oldfs);
30362 +       return err;
30363 +}
30364 +
30365 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30366 +                     loff_t *ppos)
30367 +{
30368 +       ssize_t err;
30369 +
30370 +       lockdep_off();
30371 +       err = vfs_write(file, ubuf, count, ppos);
30372 +       lockdep_on();
30373 +       if (err >= 0)
30374 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30375 +       return err;
30376 +}
30377 +
30378 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
30379 +{
30380 +       ssize_t err;
30381 +       mm_segment_t oldfs;
30382 +       union {
30383 +               void *k;
30384 +               const char __user *u;
30385 +       } buf;
30386 +
30387 +       buf.k = kbuf;
30388 +       oldfs = get_fs();
30389 +       set_fs(KERNEL_DS);
30390 +       err = vfsub_write_u(file, buf.u, count, ppos);
30391 +       set_fs(oldfs);
30392 +       return err;
30393 +}
30394 +
30395 +int vfsub_flush(struct file *file, fl_owner_t id)
30396 +{
30397 +       int err;
30398 +
30399 +       err = 0;
30400 +       if (file->f_op->flush) {
30401 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
30402 +                       err = file->f_op->flush(file, id);
30403 +               else {
30404 +                       lockdep_off();
30405 +                       err = file->f_op->flush(file, id);
30406 +                       lockdep_on();
30407 +               }
30408 +               if (!err)
30409 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
30410 +               /*ignore*/
30411 +       }
30412 +       return err;
30413 +}
30414 +
30415 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
30416 +{
30417 +       int err;
30418 +
30419 +       AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
30420 +
30421 +       lockdep_off();
30422 +       err = iterate_dir(file, ctx);
30423 +       lockdep_on();
30424 +       if (err >= 0)
30425 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30426 +       return err;
30427 +}
30428 +
30429 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30430 +                    struct pipe_inode_info *pipe, size_t len,
30431 +                    unsigned int flags)
30432 +{
30433 +       long err;
30434 +
30435 +       lockdep_off();
30436 +       err = do_splice_to(in, ppos, pipe, len, flags);
30437 +       lockdep_on();
30438 +       file_accessed(in);
30439 +       if (err >= 0)
30440 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
30441 +       return err;
30442 +}
30443 +
30444 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30445 +                      loff_t *ppos, size_t len, unsigned int flags)
30446 +{
30447 +       long err;
30448 +
30449 +       lockdep_off();
30450 +       err = do_splice_from(pipe, out, ppos, len, flags);
30451 +       lockdep_on();
30452 +       if (err >= 0)
30453 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
30454 +       return err;
30455 +}
30456 +
30457 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
30458 +{
30459 +       int err;
30460 +
30461 +       /* file can be NULL */
30462 +       lockdep_off();
30463 +       err = vfs_fsync(file, datasync);
30464 +       lockdep_on();
30465 +       if (!err) {
30466 +               if (!path) {
30467 +                       AuDebugOn(!file);
30468 +                       path = &file->f_path;
30469 +               }
30470 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30471 +       }
30472 +       return err;
30473 +}
30474 +
30475 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
30476 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30477 +               struct file *h_file)
30478 +{
30479 +       int err;
30480 +       struct inode *h_inode;
30481 +       struct super_block *h_sb;
30482 +
30483 +       if (!h_file) {
30484 +               err = vfsub_truncate(h_path, length);
30485 +               goto out;
30486 +       }
30487 +
30488 +       h_inode = d_inode(h_path->dentry);
30489 +       h_sb = h_inode->i_sb;
30490 +       lockdep_off();
30491 +       sb_start_write(h_sb);
30492 +       lockdep_on();
30493 +       err = locks_verify_truncate(h_inode, h_file, length);
30494 +       if (!err)
30495 +               err = security_path_truncate(h_path);
30496 +       if (!err) {
30497 +               lockdep_off();
30498 +               err = do_truncate(h_path->dentry, length, attr, h_file);
30499 +               lockdep_on();
30500 +       }
30501 +       lockdep_off();
30502 +       sb_end_write(h_sb);
30503 +       lockdep_on();
30504 +
30505 +out:
30506 +       return err;
30507 +}
30508 +
30509 +/* ---------------------------------------------------------------------- */
30510 +
30511 +struct au_vfsub_mkdir_args {
30512 +       int *errp;
30513 +       struct inode *dir;
30514 +       struct path *path;
30515 +       int mode;
30516 +};
30517 +
30518 +static void au_call_vfsub_mkdir(void *args)
30519 +{
30520 +       struct au_vfsub_mkdir_args *a = args;
30521 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
30522 +}
30523 +
30524 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
30525 +{
30526 +       int err, do_sio, wkq_err;
30527 +
30528 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30529 +       if (!do_sio) {
30530 +               lockdep_off();
30531 +               err = vfsub_mkdir(dir, path, mode);
30532 +               lockdep_on();
30533 +       } else {
30534 +               struct au_vfsub_mkdir_args args = {
30535 +                       .errp   = &err,
30536 +                       .dir    = dir,
30537 +                       .path   = path,
30538 +                       .mode   = mode
30539 +               };
30540 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
30541 +               if (unlikely(wkq_err))
30542 +                       err = wkq_err;
30543 +       }
30544 +
30545 +       return err;
30546 +}
30547 +
30548 +struct au_vfsub_rmdir_args {
30549 +       int *errp;
30550 +       struct inode *dir;
30551 +       struct path *path;
30552 +};
30553 +
30554 +static void au_call_vfsub_rmdir(void *args)
30555 +{
30556 +       struct au_vfsub_rmdir_args *a = args;
30557 +       *a->errp = vfsub_rmdir(a->dir, a->path);
30558 +}
30559 +
30560 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
30561 +{
30562 +       int err, do_sio, wkq_err;
30563 +
30564 +       do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
30565 +       if (!do_sio) {
30566 +               lockdep_off();
30567 +               err = vfsub_rmdir(dir, path);
30568 +               lockdep_on();
30569 +       } else {
30570 +               struct au_vfsub_rmdir_args args = {
30571 +                       .errp   = &err,
30572 +                       .dir    = dir,
30573 +                       .path   = path
30574 +               };
30575 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
30576 +               if (unlikely(wkq_err))
30577 +                       err = wkq_err;
30578 +       }
30579 +
30580 +       return err;
30581 +}
30582 +
30583 +/* ---------------------------------------------------------------------- */
30584 +
30585 +struct notify_change_args {
30586 +       int *errp;
30587 +       struct path *path;
30588 +       struct iattr *ia;
30589 +       struct inode **delegated_inode;
30590 +};
30591 +
30592 +static void call_notify_change(void *args)
30593 +{
30594 +       struct notify_change_args *a = args;
30595 +       struct inode *h_inode;
30596 +
30597 +       h_inode = d_inode(a->path->dentry);
30598 +       IMustLock(h_inode);
30599 +
30600 +       *a->errp = -EPERM;
30601 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
30602 +               lockdep_off();
30603 +               *a->errp = notify_change(a->path->dentry, a->ia,
30604 +                                        a->delegated_inode);
30605 +               lockdep_on();
30606 +               if (!*a->errp)
30607 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
30608 +       }
30609 +       AuTraceErr(*a->errp);
30610 +}
30611 +
30612 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30613 +                       struct inode **delegated_inode)
30614 +{
30615 +       int err;
30616 +       struct notify_change_args args = {
30617 +               .errp                   = &err,
30618 +               .path                   = path,
30619 +               .ia                     = ia,
30620 +               .delegated_inode        = delegated_inode
30621 +       };
30622 +
30623 +       call_notify_change(&args);
30624 +
30625 +       return err;
30626 +}
30627 +
30628 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30629 +                           struct inode **delegated_inode)
30630 +{
30631 +       int err, wkq_err;
30632 +       struct notify_change_args args = {
30633 +               .errp                   = &err,
30634 +               .path                   = path,
30635 +               .ia                     = ia,
30636 +               .delegated_inode        = delegated_inode
30637 +       };
30638 +
30639 +       wkq_err = au_wkq_wait(call_notify_change, &args);
30640 +       if (unlikely(wkq_err))
30641 +               err = wkq_err;
30642 +
30643 +       return err;
30644 +}
30645 +
30646 +/* ---------------------------------------------------------------------- */
30647 +
30648 +struct unlink_args {
30649 +       int *errp;
30650 +       struct inode *dir;
30651 +       struct path *path;
30652 +       struct inode **delegated_inode;
30653 +};
30654 +
30655 +static void call_unlink(void *args)
30656 +{
30657 +       struct unlink_args *a = args;
30658 +       struct dentry *d = a->path->dentry;
30659 +       struct inode *h_inode;
30660 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
30661 +                                     && au_dcount(d) == 1);
30662 +
30663 +       IMustLock(a->dir);
30664 +
30665 +       a->path->dentry = d->d_parent;
30666 +       *a->errp = security_path_unlink(a->path, d);
30667 +       a->path->dentry = d;
30668 +       if (unlikely(*a->errp))
30669 +               return;
30670 +
30671 +       if (!stop_sillyrename)
30672 +               dget(d);
30673 +       h_inode = NULL;
30674 +       if (d_is_positive(d)) {
30675 +               h_inode = d_inode(d);
30676 +               ihold(h_inode);
30677 +       }
30678 +
30679 +       lockdep_off();
30680 +       *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
30681 +       lockdep_on();
30682 +       if (!*a->errp) {
30683 +               struct path tmp = {
30684 +                       .dentry = d->d_parent,
30685 +                       .mnt    = a->path->mnt
30686 +               };
30687 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30688 +       }
30689 +
30690 +       if (!stop_sillyrename)
30691 +               dput(d);
30692 +       if (h_inode)
30693 +               iput(h_inode);
30694 +
30695 +       AuTraceErr(*a->errp);
30696 +}
30697 +
30698 +/*
30699 + * @dir: must be locked.
30700 + * @dentry: target dentry.
30701 + */
30702 +int vfsub_unlink(struct inode *dir, struct path *path,
30703 +                struct inode **delegated_inode, int force)
30704 +{
30705 +       int err;
30706 +       struct unlink_args args = {
30707 +               .errp                   = &err,
30708 +               .dir                    = dir,
30709 +               .path                   = path,
30710 +               .delegated_inode        = delegated_inode
30711 +       };
30712 +
30713 +       if (!force)
30714 +               call_unlink(&args);
30715 +       else {
30716 +               int wkq_err;
30717 +
30718 +               wkq_err = au_wkq_wait(call_unlink, &args);
30719 +               if (unlikely(wkq_err))
30720 +                       err = wkq_err;
30721 +       }
30722 +
30723 +       return err;
30724 +}
30725 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
30726 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
30727 +++ linux/fs/aufs/vfsub.h       2015-09-24 10:47:58.258053165 +0200
30728 @@ -0,0 +1,286 @@
30729 +/*
30730 + * Copyright (C) 2005-2015 Junjiro R. Okajima
30731 + *
30732 + * This program, aufs is free software; you can redistribute it and/or modify
30733 + * it under the terms of the GNU General Public License as published by
30734 + * the Free Software Foundation; either version 2 of the License, or
30735 + * (at your option) any later version.
30736 + *
30737 + * This program is distributed in the hope that it will be useful,
30738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30740 + * GNU General Public License for more details.
30741 + *
30742 + * You should have received a copy of the GNU General Public License
30743 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30744 + */
30745 +
30746 +/*
30747 + * sub-routines for VFS
30748 + */
30749 +
30750 +#ifndef __AUFS_VFSUB_H__
30751 +#define __AUFS_VFSUB_H__
30752 +
30753 +#ifdef __KERNEL__
30754 +
30755 +#include <linux/fs.h>
30756 +#include <linux/mount.h>
30757 +#include <linux/xattr.h>
30758 +#include "debug.h"
30759 +
30760 +/* copied from linux/fs/internal.h */
30761 +/* todo: BAD approach!! */
30762 +extern void __mnt_drop_write(struct vfsmount *);
30763 +extern spinlock_t inode_sb_list_lock;
30764 +extern int open_check_o_direct(struct file *f);
30765 +
30766 +/* ---------------------------------------------------------------------- */
30767 +
30768 +/* lock subclass for lower inode */
30769 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
30770 +/* reduce? gave up. */
30771 +enum {
30772 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
30773 +       AuLsc_I_PARENT,         /* lower inode, parent first */
30774 +       AuLsc_I_PARENT2,        /* copyup dirs */
30775 +       AuLsc_I_PARENT3,        /* copyup wh */
30776 +       AuLsc_I_CHILD,
30777 +       AuLsc_I_CHILD2,
30778 +       AuLsc_I_End
30779 +};
30780 +
30781 +/* to debug easier, do not make them inlined functions */
30782 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
30783 +#define IMustLock(i)           MtxMustLock(&(i)->i_mutex)
30784 +
30785 +/* ---------------------------------------------------------------------- */
30786 +
30787 +static inline void vfsub_drop_nlink(struct inode *inode)
30788 +{
30789 +       AuDebugOn(!inode->i_nlink);
30790 +       drop_nlink(inode);
30791 +}
30792 +
30793 +static inline void vfsub_dead_dir(struct inode *inode)
30794 +{
30795 +       AuDebugOn(!S_ISDIR(inode->i_mode));
30796 +       inode->i_flags |= S_DEAD;
30797 +       clear_nlink(inode);
30798 +}
30799 +
30800 +static inline int vfsub_native_ro(struct inode *inode)
30801 +{
30802 +       return (inode->i_sb->s_flags & MS_RDONLY)
30803 +               || IS_RDONLY(inode)
30804 +               /* || IS_APPEND(inode) */
30805 +               || IS_IMMUTABLE(inode);
30806 +}
30807 +
30808 +/* ---------------------------------------------------------------------- */
30809 +
30810 +int vfsub_update_h_iattr(struct path *h_path, int *did);
30811 +struct file *vfsub_dentry_open(struct path *path, int flags);
30812 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
30813 +struct vfsub_aopen_args {
30814 +       struct file     *file;
30815 +       unsigned int    open_flag;
30816 +       umode_t         create_mode;
30817 +       int             *opened;
30818 +};
30819 +struct au_branch;
30820 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30821 +                     struct vfsub_aopen_args *args, struct au_branch *br);
30822 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
30823 +
30824 +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30825 +                                   int len);
30826 +
30827 +struct vfsub_lkup_one_args {
30828 +       struct dentry **errp;
30829 +       struct qstr *name;
30830 +       struct dentry *parent;
30831 +};
30832 +
30833 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
30834 +                                           struct dentry *parent)
30835 +{
30836 +       return vfsub_lookup_one_len(name->name, parent, name->len);
30837 +}
30838 +
30839 +void vfsub_call_lkup_one(void *args);
30840 +
30841 +/* ---------------------------------------------------------------------- */
30842 +
30843 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
30844 +{
30845 +       int err;
30846 +
30847 +       lockdep_off();
30848 +       err = mnt_want_write(mnt);
30849 +       lockdep_on();
30850 +       return err;
30851 +}
30852 +
30853 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
30854 +{
30855 +       lockdep_off();
30856 +       mnt_drop_write(mnt);
30857 +       lockdep_on();
30858 +}
30859 +
30860 +#if 0 /* reserved */
30861 +static inline void vfsub_mnt_drop_write_file(struct file *file)
30862 +{
30863 +       lockdep_off();
30864 +       mnt_drop_write_file(file);
30865 +       lockdep_on();
30866 +}
30867 +#endif
30868 +
30869 +/* ---------------------------------------------------------------------- */
30870 +
30871 +struct au_hinode;
30872 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30873 +                                struct dentry *d2, struct au_hinode *hdir2);
30874 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30875 +                        struct dentry *d2, struct au_hinode *hdir2);
30876 +
30877 +int vfsub_create(struct inode *dir, struct path *path, int mode,
30878 +                bool want_excl);
30879 +int vfsub_symlink(struct inode *dir, struct path *path,
30880 +                 const char *symname);
30881 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
30882 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
30883 +              struct path *path, struct inode **delegated_inode);
30884 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
30885 +                struct inode *hdir, struct path *path,
30886 +                struct inode **delegated_inode);
30887 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
30888 +int vfsub_rmdir(struct inode *dir, struct path *path);
30889 +
30890 +/* ---------------------------------------------------------------------- */
30891 +
30892 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30893 +                    loff_t *ppos);
30894 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30895 +                       loff_t *ppos);
30896 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30897 +                     loff_t *ppos);
30898 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
30899 +                     loff_t *ppos);
30900 +int vfsub_flush(struct file *file, fl_owner_t id);
30901 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
30902 +
30903 +static inline loff_t vfsub_f_size_read(struct file *file)
30904 +{
30905 +       return i_size_read(file_inode(file));
30906 +}
30907 +
30908 +static inline unsigned int vfsub_file_flags(struct file *file)
30909 +{
30910 +       unsigned int flags;
30911 +
30912 +       spin_lock(&file->f_lock);
30913 +       flags = file->f_flags;
30914 +       spin_unlock(&file->f_lock);
30915 +
30916 +       return flags;
30917 +}
30918 +
30919 +#if 0 /* reserved */
30920 +static inline void vfsub_file_accessed(struct file *h_file)
30921 +{
30922 +       file_accessed(h_file);
30923 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
30924 +}
30925 +#endif
30926 +
30927 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
30928 +                                    struct dentry *h_dentry)
30929 +{
30930 +       struct path h_path = {
30931 +               .dentry = h_dentry,
30932 +               .mnt    = h_mnt
30933 +       };
30934 +       touch_atime(&h_path);
30935 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
30936 +}
30937 +
30938 +static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
30939 +                                   int flags)
30940 +{
30941 +       return generic_update_time(h_inode, ts, flags);
30942 +       /* no vfsub_update_h_iattr() since we don't have struct path */
30943 +}
30944 +
30945 +long vfsub_splice_to(struct file *in, loff_t *ppos,
30946 +                    struct pipe_inode_info *pipe, size_t len,
30947 +                    unsigned int flags);
30948 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30949 +                      loff_t *ppos, size_t len, unsigned int flags);
30950 +
30951 +static inline long vfsub_truncate(struct path *path, loff_t length)
30952 +{
30953 +       long err;
30954 +
30955 +       lockdep_off();
30956 +       err = vfs_truncate(path, length);
30957 +       lockdep_on();
30958 +       return err;
30959 +}
30960 +
30961 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30962 +               struct file *h_file);
30963 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
30964 +
30965 +/* ---------------------------------------------------------------------- */
30966 +
30967 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
30968 +{
30969 +       loff_t err;
30970 +
30971 +       lockdep_off();
30972 +       err = vfs_llseek(file, offset, origin);
30973 +       lockdep_on();
30974 +       return err;
30975 +}
30976 +
30977 +/* ---------------------------------------------------------------------- */
30978 +
30979 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
30980 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
30981 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30982 +                           struct inode **delegated_inode);
30983 +int vfsub_notify_change(struct path *path, struct iattr *ia,
30984 +                       struct inode **delegated_inode);
30985 +int vfsub_unlink(struct inode *dir, struct path *path,
30986 +                struct inode **delegated_inode, int force);
30987 +
30988 +/* ---------------------------------------------------------------------- */
30989 +
30990 +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
30991 +                                const void *value, size_t size, int flags)
30992 +{
30993 +       int err;
30994 +
30995 +       lockdep_off();
30996 +       err = vfs_setxattr(dentry, name, value, size, flags);
30997 +       lockdep_on();
30998 +
30999 +       return err;
31000 +}
31001 +
31002 +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31003 +{
31004 +       int err;
31005 +
31006 +       lockdep_off();
31007 +       err = vfs_removexattr(dentry, name);
31008 +       lockdep_on();
31009 +
31010 +       return err;
31011 +}
31012 +
31013 +#endif /* __KERNEL__ */
31014 +#endif /* __AUFS_VFSUB_H__ */
31015 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31016 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
31017 +++ linux/fs/aufs/wbr_policy.c  2015-09-24 10:47:58.258053165 +0200
31018 @@ -0,0 +1,765 @@
31019 +/*
31020 + * Copyright (C) 2005-2015 Junjiro R. Okajima
31021 + *
31022 + * This program, aufs is free software; you can redistribute it and/or modify
31023 + * it under the terms of the GNU General Public License as published by
31024 + * the Free Software Foundation; either version 2 of the License, or
31025 + * (at your option) any later version.
31026 + *
31027 + * This program is distributed in the hope that it will be useful,
31028 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31029 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31030 + * GNU General Public License for more details.
31031 + *
31032 + * You should have received a copy of the GNU General Public License
31033 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31034 + */
31035 +
31036 +/*
31037 + * policies for selecting one among multiple writable branches
31038 + */
31039 +
31040 +#include <linux/statfs.h>
31041 +#include "aufs.h"
31042 +
31043 +/* subset of cpup_attr() */
31044 +static noinline_for_stack
31045 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31046 +{
31047 +       int err, sbits;
31048 +       struct iattr ia;
31049 +       struct inode *h_isrc;
31050 +
31051 +       h_isrc = d_inode(h_src);
31052 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31053 +       ia.ia_mode = h_isrc->i_mode;
31054 +       ia.ia_uid = h_isrc->i_uid;
31055 +       ia.ia_gid = h_isrc->i_gid;
31056 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
31057 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
31058 +       /* no delegation since it is just created */
31059 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31060 +
31061 +       /* is this nfs only? */
31062 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31063 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31064 +               ia.ia_mode = h_isrc->i_mode;
31065 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
31066 +       }
31067 +
31068 +       return err;
31069 +}
31070 +
31071 +#define AuCpdown_PARENT_OPQ    1
31072 +#define AuCpdown_WHED          (1 << 1)
31073 +#define AuCpdown_MADE_DIR      (1 << 2)
31074 +#define AuCpdown_DIROPQ                (1 << 3)
31075 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
31076 +#define au_fset_cpdown(flags, name) \
31077 +       do { (flags) |= AuCpdown_##name; } while (0)
31078 +#define au_fclr_cpdown(flags, name) \
31079 +       do { (flags) &= ~AuCpdown_##name; } while (0)
31080 +
31081 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
31082 +                            unsigned int *flags)
31083 +{
31084 +       int err;
31085 +       struct dentry *opq_dentry;
31086 +
31087 +       opq_dentry = au_diropq_create(dentry, bdst);
31088 +       err = PTR_ERR(opq_dentry);
31089 +       if (IS_ERR(opq_dentry))
31090 +               goto out;
31091 +       dput(opq_dentry);
31092 +       au_fset_cpdown(*flags, DIROPQ);
31093 +
31094 +out:
31095 +       return err;
31096 +}
31097 +
31098 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31099 +                           struct inode *dir, aufs_bindex_t bdst)
31100 +{
31101 +       int err;
31102 +       struct path h_path;
31103 +       struct au_branch *br;
31104 +
31105 +       br = au_sbr(dentry->d_sb, bdst);
31106 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31107 +       err = PTR_ERR(h_path.dentry);
31108 +       if (IS_ERR(h_path.dentry))
31109 +               goto out;
31110 +
31111 +       err = 0;
31112 +       if (d_is_positive(h_path.dentry)) {
31113 +               h_path.mnt = au_br_mnt(br);
31114 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31115 +                                         dentry);
31116 +       }
31117 +       dput(h_path.dentry);
31118 +
31119 +out:
31120 +       return err;
31121 +}
31122 +
31123 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
31124 +                        struct au_pin *pin,
31125 +                        struct dentry *h_parent, void *arg)
31126 +{
31127 +       int err, rerr;
31128 +       aufs_bindex_t bopq, bstart;
31129 +       struct path h_path;
31130 +       struct dentry *parent;
31131 +       struct inode *h_dir, *h_inode, *inode, *dir;
31132 +       unsigned int *flags = arg;
31133 +
31134 +       bstart = au_dbstart(dentry);
31135 +       /* dentry is di-locked */
31136 +       parent = dget_parent(dentry);
31137 +       dir = d_inode(parent);
31138 +       h_dir = d_inode(h_parent);
31139 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31140 +       IMustLock(h_dir);
31141 +
31142 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
31143 +       if (unlikely(err < 0))
31144 +               goto out;
31145 +       h_path.dentry = au_h_dptr(dentry, bdst);
31146 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31147 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31148 +                             S_IRWXU | S_IRUGO | S_IXUGO);
31149 +       if (unlikely(err))
31150 +               goto out_put;
31151 +       au_fset_cpdown(*flags, MADE_DIR);
31152 +
31153 +       bopq = au_dbdiropq(dentry);
31154 +       au_fclr_cpdown(*flags, WHED);
31155 +       au_fclr_cpdown(*flags, DIROPQ);
31156 +       if (au_dbwh(dentry) == bdst)
31157 +               au_fset_cpdown(*flags, WHED);
31158 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31159 +               au_fset_cpdown(*flags, PARENT_OPQ);
31160 +       h_inode = d_inode(h_path.dentry);
31161 +       mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
31162 +       if (au_ftest_cpdown(*flags, WHED)) {
31163 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
31164 +               if (unlikely(err)) {
31165 +                       mutex_unlock(&h_inode->i_mutex);
31166 +                       goto out_dir;
31167 +               }
31168 +       }
31169 +
31170 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, bstart));
31171 +       mutex_unlock(&h_inode->i_mutex);
31172 +       if (unlikely(err))
31173 +               goto out_opq;
31174 +
31175 +       if (au_ftest_cpdown(*flags, WHED)) {
31176 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31177 +               if (unlikely(err))
31178 +                       goto out_opq;
31179 +       }
31180 +
31181 +       inode = d_inode(dentry);
31182 +       if (au_ibend(inode) < bdst)
31183 +               au_set_ibend(inode, bdst);
31184 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31185 +                     au_hi_flags(inode, /*isdir*/1));
31186 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
31187 +       goto out; /* success */
31188 +
31189 +       /* revert */
31190 +out_opq:
31191 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
31192 +               mutex_lock_nested(&h_inode->i_mutex, AuLsc_I_CHILD);
31193 +               rerr = au_diropq_remove(dentry, bdst);
31194 +               mutex_unlock(&h_inode->i_mutex);
31195 +               if (unlikely(rerr)) {
31196 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31197 +                               dentry, bdst, rerr);
31198 +                       err = -EIO;
31199 +                       goto out;
31200 +               }
31201 +       }
31202 +out_dir:
31203 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
31204 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31205 +               if (unlikely(rerr)) {
31206 +                       AuIOErr("failed removing %pd b%d (%d)\n",
31207 +                               dentry, bdst, rerr);
31208 +                       err = -EIO;
31209 +               }
31210 +       }
31211 +out_put:
31212 +       au_set_h_dptr(dentry, bdst, NULL);
31213 +       if (au_dbend(dentry) == bdst)
31214 +               au_update_dbend(dentry);
31215 +out:
31216 +       dput(parent);
31217 +       return err;
31218 +}
31219 +
31220 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31221 +{
31222 +       int err;
31223 +       unsigned int flags;
31224 +
31225 +       flags = 0;
31226 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
31227 +
31228 +       return err;
31229 +}
31230 +
31231 +/* ---------------------------------------------------------------------- */
31232 +
31233 +/* policies for create */
31234 +
31235 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
31236 +{
31237 +       int err, i, j, ndentry;
31238 +       aufs_bindex_t bopq;
31239 +       struct au_dcsub_pages dpages;
31240 +       struct au_dpage *dpage;
31241 +       struct dentry **dentries, *parent, *d;
31242 +
31243 +       err = au_dpages_init(&dpages, GFP_NOFS);
31244 +       if (unlikely(err))
31245 +               goto out;
31246 +       parent = dget_parent(dentry);
31247 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
31248 +       if (unlikely(err))
31249 +               goto out_free;
31250 +
31251 +       err = bindex;
31252 +       for (i = 0; i < dpages.ndpage; i++) {
31253 +               dpage = dpages.dpages + i;
31254 +               dentries = dpage->dentries;
31255 +               ndentry = dpage->ndentry;
31256 +               for (j = 0; j < ndentry; j++) {
31257 +                       d = dentries[j];
31258 +                       di_read_lock_parent2(d, !AuLock_IR);
31259 +                       bopq = au_dbdiropq(d);
31260 +                       di_read_unlock(d, !AuLock_IR);
31261 +                       if (bopq >= 0 && bopq < err)
31262 +                               err = bopq;
31263 +               }
31264 +       }
31265 +
31266 +out_free:
31267 +       dput(parent);
31268 +       au_dpages_free(&dpages);
31269 +out:
31270 +       return err;
31271 +}
31272 +
31273 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
31274 +{
31275 +       for (; bindex >= 0; bindex--)
31276 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
31277 +                       return bindex;
31278 +       return -EROFS;
31279 +}
31280 +
31281 +/* top down parent */
31282 +static int au_wbr_create_tdp(struct dentry *dentry,
31283 +                            unsigned int flags __maybe_unused)
31284 +{
31285 +       int err;
31286 +       aufs_bindex_t bstart, bindex;
31287 +       struct super_block *sb;
31288 +       struct dentry *parent, *h_parent;
31289 +
31290 +       sb = dentry->d_sb;
31291 +       bstart = au_dbstart(dentry);
31292 +       err = bstart;
31293 +       if (!au_br_rdonly(au_sbr(sb, bstart)))
31294 +               goto out;
31295 +
31296 +       err = -EROFS;
31297 +       parent = dget_parent(dentry);
31298 +       for (bindex = au_dbstart(parent); bindex < bstart; bindex++) {
31299 +               h_parent = au_h_dptr(parent, bindex);
31300 +               if (!h_parent || d_is_negative(h_parent))
31301 +                       continue;
31302 +
31303 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31304 +                       err = bindex;
31305 +                       break;
31306 +               }
31307 +       }
31308 +       dput(parent);
31309 +
31310 +       /* bottom up here */
31311 +       if (unlikely(err < 0)) {
31312 +               err = au_wbr_bu(sb, bstart - 1);
31313 +               if (err >= 0)
31314 +                       err = au_wbr_nonopq(dentry, err);
31315 +       }
31316 +
31317 +out:
31318 +       AuDbg("b%d\n", err);
31319 +       return err;
31320 +}
31321 +
31322 +/* ---------------------------------------------------------------------- */
31323 +
31324 +/* an exception for the policy other than tdp */
31325 +static int au_wbr_create_exp(struct dentry *dentry)
31326 +{
31327 +       int err;
31328 +       aufs_bindex_t bwh, bdiropq;
31329 +       struct dentry *parent;
31330 +
31331 +       err = -1;
31332 +       bwh = au_dbwh(dentry);
31333 +       parent = dget_parent(dentry);
31334 +       bdiropq = au_dbdiropq(parent);
31335 +       if (bwh >= 0) {
31336 +               if (bdiropq >= 0)
31337 +                       err = min(bdiropq, bwh);
31338 +               else
31339 +                       err = bwh;
31340 +               AuDbg("%d\n", err);
31341 +       } else if (bdiropq >= 0) {
31342 +               err = bdiropq;
31343 +               AuDbg("%d\n", err);
31344 +       }
31345 +       dput(parent);
31346 +
31347 +       if (err >= 0)
31348 +               err = au_wbr_nonopq(dentry, err);
31349 +
31350 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
31351 +               err = -1;
31352 +
31353 +       AuDbg("%d\n", err);
31354 +       return err;
31355 +}
31356 +
31357 +/* ---------------------------------------------------------------------- */
31358 +
31359 +/* round robin */
31360 +static int au_wbr_create_init_rr(struct super_block *sb)
31361 +{
31362 +       int err;
31363 +
31364 +       err = au_wbr_bu(sb, au_sbend(sb));
31365 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
31366 +       /* smp_mb(); */
31367 +
31368 +       AuDbg("b%d\n", err);
31369 +       return err;
31370 +}
31371 +
31372 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
31373 +{
31374 +       int err, nbr;
31375 +       unsigned int u;
31376 +       aufs_bindex_t bindex, bend;
31377 +       struct super_block *sb;
31378 +       atomic_t *next;
31379 +
31380 +       err = au_wbr_create_exp(dentry);
31381 +       if (err >= 0)
31382 +               goto out;
31383 +
31384 +       sb = dentry->d_sb;
31385 +       next = &au_sbi(sb)->si_wbr_rr_next;
31386 +       bend = au_sbend(sb);
31387 +       nbr = bend + 1;
31388 +       for (bindex = 0; bindex <= bend; bindex++) {
31389 +               if (!au_ftest_wbr(flags, DIR)) {
31390 +                       err = atomic_dec_return(next) + 1;
31391 +                       /* modulo for 0 is meaningless */
31392 +                       if (unlikely(!err))
31393 +                               err = atomic_dec_return(next) + 1;
31394 +               } else
31395 +                       err = atomic_read(next);
31396 +               AuDbg("%d\n", err);
31397 +               u = err;
31398 +               err = u % nbr;
31399 +               AuDbg("%d\n", err);
31400 +               if (!au_br_rdonly(au_sbr(sb, err)))
31401 +                       break;
31402 +               err = -EROFS;
31403 +       }
31404 +
31405 +       if (err >= 0)
31406 +               err = au_wbr_nonopq(dentry, err);
31407 +
31408 +out:
31409 +       AuDbg("%d\n", err);
31410 +       return err;
31411 +}
31412 +
31413 +/* ---------------------------------------------------------------------- */
31414 +
31415 +/* most free space */
31416 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
31417 +{
31418 +       struct super_block *sb;
31419 +       struct au_branch *br;
31420 +       struct au_wbr_mfs *mfs;
31421 +       struct dentry *h_parent;
31422 +       aufs_bindex_t bindex, bend;
31423 +       int err;
31424 +       unsigned long long b, bavail;
31425 +       struct path h_path;
31426 +       /* reduce the stack usage */
31427 +       struct kstatfs *st;
31428 +
31429 +       st = kmalloc(sizeof(*st), GFP_NOFS);
31430 +       if (unlikely(!st)) {
31431 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
31432 +               return;
31433 +       }
31434 +
31435 +       bavail = 0;
31436 +       sb = dentry->d_sb;
31437 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31438 +       MtxMustLock(&mfs->mfs_lock);
31439 +       mfs->mfs_bindex = -EROFS;
31440 +       mfs->mfsrr_bytes = 0;
31441 +       if (!parent) {
31442 +               bindex = 0;
31443 +               bend = au_sbend(sb);
31444 +       } else {
31445 +               bindex = au_dbstart(parent);
31446 +               bend = au_dbtaildir(parent);
31447 +       }
31448 +
31449 +       for (; bindex <= bend; bindex++) {
31450 +               if (parent) {
31451 +                       h_parent = au_h_dptr(parent, bindex);
31452 +                       if (!h_parent || d_is_negative(h_parent))
31453 +                               continue;
31454 +               }
31455 +               br = au_sbr(sb, bindex);
31456 +               if (au_br_rdonly(br))
31457 +                       continue;
31458 +
31459 +               /* sb->s_root for NFS is unreliable */
31460 +               h_path.mnt = au_br_mnt(br);
31461 +               h_path.dentry = h_path.mnt->mnt_root;
31462 +               err = vfs_statfs(&h_path, st);
31463 +               if (unlikely(err)) {
31464 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
31465 +                       continue;
31466 +               }
31467 +
31468 +               /* when the available size is equal, select the lower one */
31469 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
31470 +                            || sizeof(b) < sizeof(st->f_bsize));
31471 +               b = st->f_bavail * st->f_bsize;
31472 +               br->br_wbr->wbr_bytes = b;
31473 +               if (b >= bavail) {
31474 +                       bavail = b;
31475 +                       mfs->mfs_bindex = bindex;
31476 +                       mfs->mfs_jiffy = jiffies;
31477 +               }
31478 +       }
31479 +
31480 +       mfs->mfsrr_bytes = bavail;
31481 +       AuDbg("b%d\n", mfs->mfs_bindex);
31482 +       kfree(st);
31483 +}
31484 +
31485 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
31486 +{
31487 +       int err;
31488 +       struct dentry *parent;
31489 +       struct super_block *sb;
31490 +       struct au_wbr_mfs *mfs;
31491 +
31492 +       err = au_wbr_create_exp(dentry);
31493 +       if (err >= 0)
31494 +               goto out;
31495 +
31496 +       sb = dentry->d_sb;
31497 +       parent = NULL;
31498 +       if (au_ftest_wbr(flags, PARENT))
31499 +               parent = dget_parent(dentry);
31500 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31501 +       mutex_lock(&mfs->mfs_lock);
31502 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
31503 +           || mfs->mfs_bindex < 0
31504 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
31505 +               au_mfs(dentry, parent);
31506 +       mutex_unlock(&mfs->mfs_lock);
31507 +       err = mfs->mfs_bindex;
31508 +       dput(parent);
31509 +
31510 +       if (err >= 0)
31511 +               err = au_wbr_nonopq(dentry, err);
31512 +
31513 +out:
31514 +       AuDbg("b%d\n", err);
31515 +       return err;
31516 +}
31517 +
31518 +static int au_wbr_create_init_mfs(struct super_block *sb)
31519 +{
31520 +       struct au_wbr_mfs *mfs;
31521 +
31522 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31523 +       mutex_init(&mfs->mfs_lock);
31524 +       mfs->mfs_jiffy = 0;
31525 +       mfs->mfs_bindex = -EROFS;
31526 +
31527 +       return 0;
31528 +}
31529 +
31530 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
31531 +{
31532 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
31533 +       return 0;
31534 +}
31535 +
31536 +/* ---------------------------------------------------------------------- */
31537 +
31538 +/* most free space and then round robin */
31539 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
31540 +{
31541 +       int err;
31542 +       struct au_wbr_mfs *mfs;
31543 +
31544 +       err = au_wbr_create_mfs(dentry, flags);
31545 +       if (err >= 0) {
31546 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
31547 +               mutex_lock(&mfs->mfs_lock);
31548 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
31549 +                       err = au_wbr_create_rr(dentry, flags);
31550 +               mutex_unlock(&mfs->mfs_lock);
31551 +       }
31552 +
31553 +       AuDbg("b%d\n", err);
31554 +       return err;
31555 +}
31556 +
31557 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
31558 +{
31559 +       int err;
31560 +
31561 +       au_wbr_create_init_mfs(sb); /* ignore */
31562 +       err = au_wbr_create_init_rr(sb);
31563 +
31564 +       return err;
31565 +}
31566 +
31567 +/* ---------------------------------------------------------------------- */
31568 +
31569 +/* top down parent and most free space */
31570 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
31571 +{
31572 +       int err, e2;
31573 +       unsigned long long b;
31574 +       aufs_bindex_t bindex, bstart, bend;
31575 +       struct super_block *sb;
31576 +       struct dentry *parent, *h_parent;
31577 +       struct au_branch *br;
31578 +
31579 +       err = au_wbr_create_tdp(dentry, flags);
31580 +       if (unlikely(err < 0))
31581 +               goto out;
31582 +       parent = dget_parent(dentry);
31583 +       bstart = au_dbstart(parent);
31584 +       bend = au_dbtaildir(parent);
31585 +       if (bstart == bend)
31586 +               goto out_parent; /* success */
31587 +
31588 +       e2 = au_wbr_create_mfs(dentry, flags);
31589 +       if (e2 < 0)
31590 +               goto out_parent; /* success */
31591 +
31592 +       /* when the available size is equal, select upper one */
31593 +       sb = dentry->d_sb;
31594 +       br = au_sbr(sb, err);
31595 +       b = br->br_wbr->wbr_bytes;
31596 +       AuDbg("b%d, %llu\n", err, b);
31597 +
31598 +       for (bindex = bstart; bindex <= bend; bindex++) {
31599 +               h_parent = au_h_dptr(parent, bindex);
31600 +               if (!h_parent || d_is_negative(h_parent))
31601 +                       continue;
31602 +
31603 +               br = au_sbr(sb, bindex);
31604 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
31605 +                       b = br->br_wbr->wbr_bytes;
31606 +                       err = bindex;
31607 +                       AuDbg("b%d, %llu\n", err, b);
31608 +               }
31609 +       }
31610 +
31611 +       if (err >= 0)
31612 +               err = au_wbr_nonopq(dentry, err);
31613 +
31614 +out_parent:
31615 +       dput(parent);
31616 +out:
31617 +       AuDbg("b%d\n", err);
31618 +       return err;
31619 +}
31620 +
31621 +/* ---------------------------------------------------------------------- */
31622 +
31623 +/*
31624 + * - top down parent
31625 + * - most free space with parent
31626 + * - most free space round-robin regardless parent
31627 + */
31628 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
31629 +{
31630 +       int err;
31631 +       unsigned long long watermark;
31632 +       struct super_block *sb;
31633 +       struct au_branch *br;
31634 +       struct au_wbr_mfs *mfs;
31635 +
31636 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
31637 +       if (unlikely(err < 0))
31638 +               goto out;
31639 +
31640 +       sb = dentry->d_sb;
31641 +       br = au_sbr(sb, err);
31642 +       mfs = &au_sbi(sb)->si_wbr_mfs;
31643 +       mutex_lock(&mfs->mfs_lock);
31644 +       watermark = mfs->mfsrr_watermark;
31645 +       mutex_unlock(&mfs->mfs_lock);
31646 +       if (br->br_wbr->wbr_bytes < watermark)
31647 +               /* regardless the parent dir */
31648 +               err = au_wbr_create_mfsrr(dentry, flags);
31649 +
31650 +out:
31651 +       AuDbg("b%d\n", err);
31652 +       return err;
31653 +}
31654 +
31655 +/* ---------------------------------------------------------------------- */
31656 +
31657 +/* policies for copyup */
31658 +
31659 +/* top down parent */
31660 +static int au_wbr_copyup_tdp(struct dentry *dentry)
31661 +{
31662 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
31663 +}
31664 +
31665 +/* bottom up parent */
31666 +static int au_wbr_copyup_bup(struct dentry *dentry)
31667 +{
31668 +       int err;
31669 +       aufs_bindex_t bindex, bstart;
31670 +       struct dentry *parent, *h_parent;
31671 +       struct super_block *sb;
31672 +
31673 +       err = -EROFS;
31674 +       sb = dentry->d_sb;
31675 +       parent = dget_parent(dentry);
31676 +       bstart = au_dbstart(parent);
31677 +       for (bindex = au_dbstart(dentry); bindex >= bstart; bindex--) {
31678 +               h_parent = au_h_dptr(parent, bindex);
31679 +               if (!h_parent || d_is_negative(h_parent))
31680 +                       continue;
31681 +
31682 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
31683 +                       err = bindex;
31684 +                       break;
31685 +               }
31686 +       }
31687 +       dput(parent);
31688 +
31689 +       /* bottom up here */
31690 +       if (unlikely(err < 0))
31691 +               err = au_wbr_bu(sb, bstart - 1);
31692 +
31693 +       AuDbg("b%d\n", err);
31694 +       return err;
31695 +}
31696 +
31697 +/* bottom up */
31698 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t bstart)
31699 +{
31700 +       int err;
31701 +
31702 +       err = au_wbr_bu(dentry->d_sb, bstart);
31703 +       AuDbg("b%d\n", err);
31704 +       if (err > bstart)
31705 +               err = au_wbr_nonopq(dentry, err);
31706 +
31707 +       AuDbg("b%d\n", err);
31708 +       return err;
31709 +}
31710 +
31711 +static int au_wbr_copyup_bu(struct dentry *dentry)
31712 +{
31713 +       int err;
31714 +       aufs_bindex_t bstart;
31715 +
31716 +       bstart = au_dbstart(dentry);
31717 +       err = au_wbr_do_copyup_bu(dentry, bstart);
31718 +       return err;
31719 +}
31720 +
31721 +/* ---------------------------------------------------------------------- */
31722 +
31723 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
31724 +       [AuWbrCopyup_TDP] = {
31725 +               .copyup = au_wbr_copyup_tdp
31726 +       },
31727 +       [AuWbrCopyup_BUP] = {
31728 +               .copyup = au_wbr_copyup_bup
31729 +       },
31730 +       [AuWbrCopyup_BU] = {
31731 +               .copyup = au_wbr_copyup_bu
31732 +       }
31733 +};
31734 +
31735 +struct au_wbr_create_operations au_wbr_create_ops[] = {
31736 +       [AuWbrCreate_TDP] = {
31737 +               .create = au_wbr_create_tdp
31738 +       },
31739 +       [AuWbrCreate_RR] = {
31740 +               .create = au_wbr_create_rr,
31741 +               .init   = au_wbr_create_init_rr
31742 +       },
31743 +       [AuWbrCreate_MFS] = {
31744 +               .create = au_wbr_create_mfs,
31745 +               .init   = au_wbr_create_init_mfs,
31746 +               .fin    = au_wbr_create_fin_mfs
31747 +       },
31748 +       [AuWbrCreate_MFSV] = {
31749 +               .create = au_wbr_create_mfs,
31750 +               .init   = au_wbr_create_init_mfs,
31751 +               .fin    = au_wbr_create_fin_mfs
31752 +       },
31753 +       [AuWbrCreate_MFSRR] = {
31754 +               .create = au_wbr_create_mfsrr,
31755 +               .init   = au_wbr_create_init_mfsrr,
31756 +               .fin    = au_wbr_create_fin_mfs
31757 +       },
31758 +       [AuWbrCreate_MFSRRV] = {
31759 +               .create = au_wbr_create_mfsrr,
31760 +               .init   = au_wbr_create_init_mfsrr,
31761 +               .fin    = au_wbr_create_fin_mfs
31762 +       },
31763 +       [AuWbrCreate_PMFS] = {
31764 +               .create = au_wbr_create_pmfs,
31765 +               .init   = au_wbr_create_init_mfs,
31766 +               .fin    = au_wbr_create_fin_mfs
31767 +       },
31768 +       [AuWbrCreate_PMFSV] = {
31769 +               .create = au_wbr_create_pmfs,
31770 +               .init   = au_wbr_create_init_mfs,
31771 +               .fin    = au_wbr_create_fin_mfs
31772 +       },
31773 +       [AuWbrCreate_PMFSRR] = {
31774 +               .create = au_wbr_create_pmfsrr,
31775 +               .init   = au_wbr_create_init_mfsrr,
31776 +               .fin    = au_wbr_create_fin_mfs
31777 +       },
31778 +       [AuWbrCreate_PMFSRRV] = {
31779 +               .create = au_wbr_create_pmfsrr,
31780 +               .init   = au_wbr_create_init_mfsrr,
31781 +               .fin    = au_wbr_create_fin_mfs
31782 +       }
31783 +};
31784 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
31785 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
31786 +++ linux/fs/aufs/whout.c       2015-09-24 10:47:58.258053165 +0200
31787 @@ -0,0 +1,1063 @@
31788 +/*
31789 + * Copyright (C) 2005-2015 Junjiro R. Okajima
31790 + *
31791 + * This program, aufs is free software; you can redistribute it and/or modify
31792 + * it under the terms of the GNU General Public License as published by
31793 + * the Free Software Foundation; either version 2 of the License, or
31794 + * (at your option) any later version.
31795 + *
31796 + * This program is distributed in the hope that it will be useful,
31797 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31798 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31799 + * GNU General Public License for more details.
31800 + *
31801 + * You should have received a copy of the GNU General Public License
31802 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31803 + */
31804 +
31805 +/*
31806 + * whiteout for logical deletion and opaque directory
31807 + */
31808 +
31809 +#include "aufs.h"
31810 +
31811 +#define WH_MASK                        S_IRUGO
31812 +
31813 +/*
31814 + * If a directory contains this file, then it is opaque.  We start with the
31815 + * .wh. flag so that it is blocked by lookup.
31816 + */
31817 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
31818 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
31819 +
31820 +/*
31821 + * generate whiteout name, which is NOT terminated by NULL.
31822 + * @name: original d_name.name
31823 + * @len: original d_name.len
31824 + * @wh: whiteout qstr
31825 + * returns zero when succeeds, otherwise error.
31826 + * succeeded value as wh->name should be freed by kfree().
31827 + */
31828 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
31829 +{
31830 +       char *p;
31831 +
31832 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
31833 +               return -ENAMETOOLONG;
31834 +
31835 +       wh->len = name->len + AUFS_WH_PFX_LEN;
31836 +       p = kmalloc(wh->len, GFP_NOFS);
31837 +       wh->name = p;
31838 +       if (p) {
31839 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
31840 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
31841 +               /* smp_mb(); */
31842 +               return 0;
31843 +       }
31844 +       return -ENOMEM;
31845 +}
31846 +
31847 +/* ---------------------------------------------------------------------- */
31848 +
31849 +/*
31850 + * test if the @wh_name exists under @h_parent.
31851 + * @try_sio specifies the necessary of super-io.
31852 + */
31853 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
31854 +{
31855 +       int err;
31856 +       struct dentry *wh_dentry;
31857 +
31858 +       if (!try_sio)
31859 +               wh_dentry = vfsub_lkup_one(wh_name, h_parent);
31860 +       else
31861 +               wh_dentry = au_sio_lkup_one(wh_name, h_parent);
31862 +       err = PTR_ERR(wh_dentry);
31863 +       if (IS_ERR(wh_dentry)) {
31864 +               if (err == -ENAMETOOLONG)
31865 +                       err = 0;
31866 +               goto out;
31867 +       }
31868 +
31869 +       err = 0;
31870 +       if (d_is_negative(wh_dentry))
31871 +               goto out_wh; /* success */
31872 +
31873 +       err = 1;
31874 +       if (d_is_reg(wh_dentry))
31875 +               goto out_wh; /* success */
31876 +
31877 +       err = -EIO;
31878 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
31879 +               wh_dentry, d_inode(wh_dentry)->i_mode);
31880 +
31881 +out_wh:
31882 +       dput(wh_dentry);
31883 +out:
31884 +       return err;
31885 +}
31886 +
31887 +/*
31888 + * test if the @h_dentry sets opaque or not.
31889 + */
31890 +int au_diropq_test(struct dentry *h_dentry)
31891 +{
31892 +       int err;
31893 +       struct inode *h_dir;
31894 +
31895 +       h_dir = d_inode(h_dentry);
31896 +       err = au_wh_test(h_dentry, &diropq_name,
31897 +                        au_test_h_perm_sio(h_dir, MAY_EXEC));
31898 +       return err;
31899 +}
31900 +
31901 +/*
31902 + * returns a negative dentry whose name is unique and temporary.
31903 + */
31904 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
31905 +                            struct qstr *prefix)
31906 +{
31907 +       struct dentry *dentry;
31908 +       int i;
31909 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
31910 +               *name, *p;
31911 +       /* strict atomic_t is unnecessary here */
31912 +       static unsigned short cnt;
31913 +       struct qstr qs;
31914 +
31915 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
31916 +
31917 +       name = defname;
31918 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
31919 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
31920 +               dentry = ERR_PTR(-ENAMETOOLONG);
31921 +               if (unlikely(qs.len > NAME_MAX))
31922 +                       goto out;
31923 +               dentry = ERR_PTR(-ENOMEM);
31924 +               name = kmalloc(qs.len + 1, GFP_NOFS);
31925 +               if (unlikely(!name))
31926 +                       goto out;
31927 +       }
31928 +
31929 +       /* doubly whiteout-ed */
31930 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
31931 +       p = name + AUFS_WH_PFX_LEN * 2;
31932 +       memcpy(p, prefix->name, prefix->len);
31933 +       p += prefix->len;
31934 +       *p++ = '.';
31935 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
31936 +
31937 +       qs.name = name;
31938 +       for (i = 0; i < 3; i++) {
31939 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
31940 +               dentry = au_sio_lkup_one(&qs, h_parent);
31941 +               if (IS_ERR(dentry) || d_is_negative(dentry))
31942 +                       goto out_name;
31943 +               dput(dentry);
31944 +       }
31945 +       /* pr_warn("could not get random name\n"); */
31946 +       dentry = ERR_PTR(-EEXIST);
31947 +       AuDbg("%.*s\n", AuLNPair(&qs));
31948 +       BUG();
31949 +
31950 +out_name:
31951 +       if (name != defname)
31952 +               kfree(name);
31953 +out:
31954 +       AuTraceErrPtr(dentry);
31955 +       return dentry;
31956 +}
31957 +
31958 +/*
31959 + * rename the @h_dentry on @br to the whiteouted temporary name.
31960 + */
31961 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
31962 +{
31963 +       int err;
31964 +       struct path h_path = {
31965 +               .mnt = au_br_mnt(br)
31966 +       };
31967 +       struct inode *h_dir, *delegated;
31968 +       struct dentry *h_parent;
31969 +
31970 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
31971 +       h_dir = d_inode(h_parent);
31972 +       IMustLock(h_dir);
31973 +
31974 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
31975 +       err = PTR_ERR(h_path.dentry);
31976 +       if (IS_ERR(h_path.dentry))
31977 +               goto out;
31978 +
31979 +       /* under the same dir, no need to lock_rename() */
31980 +       delegated = NULL;
31981 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
31982 +       AuTraceErr(err);
31983 +       if (unlikely(err == -EWOULDBLOCK)) {
31984 +               pr_warn("cannot retry for NFSv4 delegation"
31985 +                       " for an internal rename\n");
31986 +               iput(delegated);
31987 +       }
31988 +       dput(h_path.dentry);
31989 +
31990 +out:
31991 +       AuTraceErr(err);
31992 +       return err;
31993 +}
31994 +
31995 +/* ---------------------------------------------------------------------- */
31996 +/*
31997 + * functions for removing a whiteout
31998 + */
31999 +
32000 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32001 +{
32002 +       int err, force;
32003 +       struct inode *delegated;
32004 +
32005 +       /*
32006 +        * forces superio when the dir has a sticky bit.
32007 +        * this may be a violation of unix fs semantics.
32008 +        */
32009 +       force = (h_dir->i_mode & S_ISVTX)
32010 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
32011 +       delegated = NULL;
32012 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
32013 +       if (unlikely(err == -EWOULDBLOCK)) {
32014 +               pr_warn("cannot retry for NFSv4 delegation"
32015 +                       " for an internal unlink\n");
32016 +               iput(delegated);
32017 +       }
32018 +       return err;
32019 +}
32020 +
32021 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32022 +                       struct dentry *dentry)
32023 +{
32024 +       int err;
32025 +
32026 +       err = do_unlink_wh(h_dir, h_path);
32027 +       if (!err && dentry)
32028 +               au_set_dbwh(dentry, -1);
32029 +
32030 +       return err;
32031 +}
32032 +
32033 +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32034 +                         struct au_branch *br)
32035 +{
32036 +       int err;
32037 +       struct path h_path = {
32038 +               .mnt = au_br_mnt(br)
32039 +       };
32040 +
32041 +       err = 0;
32042 +       h_path.dentry = vfsub_lkup_one(wh, h_parent);
32043 +       if (IS_ERR(h_path.dentry))
32044 +               err = PTR_ERR(h_path.dentry);
32045 +       else {
32046 +               if (d_is_reg(h_path.dentry))
32047 +                       err = do_unlink_wh(d_inode(h_parent), &h_path);
32048 +               dput(h_path.dentry);
32049 +       }
32050 +
32051 +       return err;
32052 +}
32053 +
32054 +/* ---------------------------------------------------------------------- */
32055 +/*
32056 + * initialize/clean whiteout for a branch
32057 + */
32058 +
32059 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32060 +                       const int isdir)
32061 +{
32062 +       int err;
32063 +       struct inode *delegated;
32064 +
32065 +       if (d_is_negative(whpath->dentry))
32066 +               return;
32067 +
32068 +       if (isdir)
32069 +               err = vfsub_rmdir(h_dir, whpath);
32070 +       else {
32071 +               delegated = NULL;
32072 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32073 +               if (unlikely(err == -EWOULDBLOCK)) {
32074 +                       pr_warn("cannot retry for NFSv4 delegation"
32075 +                               " for an internal unlink\n");
32076 +                       iput(delegated);
32077 +               }
32078 +       }
32079 +       if (unlikely(err))
32080 +               pr_warn("failed removing %pd (%d), ignored.\n",
32081 +                       whpath->dentry, err);
32082 +}
32083 +
32084 +static int test_linkable(struct dentry *h_root)
32085 +{
32086 +       struct inode *h_dir = d_inode(h_root);
32087 +
32088 +       if (h_dir->i_op->link)
32089 +               return 0;
32090 +
32091 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32092 +              h_root, au_sbtype(h_root->d_sb));
32093 +       return -ENOSYS;
32094 +}
32095 +
32096 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32097 +static int au_whdir(struct inode *h_dir, struct path *path)
32098 +{
32099 +       int err;
32100 +
32101 +       err = -EEXIST;
32102 +       if (d_is_negative(path->dentry)) {
32103 +               int mode = S_IRWXU;
32104 +
32105 +               if (au_test_nfs(path->dentry->d_sb))
32106 +                       mode |= S_IXUGO;
32107 +               err = vfsub_mkdir(h_dir, path, mode);
32108 +       } else if (d_is_dir(path->dentry))
32109 +               err = 0;
32110 +       else
32111 +               pr_err("unknown %pd exists\n", path->dentry);
32112 +
32113 +       return err;
32114 +}
32115 +
32116 +struct au_wh_base {
32117 +       const struct qstr *name;
32118 +       struct dentry *dentry;
32119 +};
32120 +
32121 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32122 +                         struct path *h_path)
32123 +{
32124 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32125 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32126 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32127 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32128 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32129 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
32130 +}
32131 +
32132 +/*
32133 + * returns tri-state,
32134 + * minus: error, caller should print the message
32135 + * zero: succuess
32136 + * plus: error, caller should NOT print the message
32137 + */
32138 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32139 +                               int do_plink, struct au_wh_base base[],
32140 +                               struct path *h_path)
32141 +{
32142 +       int err;
32143 +       struct inode *h_dir;
32144 +
32145 +       h_dir = d_inode(h_root);
32146 +       h_path->dentry = base[AuBrWh_BASE].dentry;
32147 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
32148 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32149 +       if (do_plink) {
32150 +               err = test_linkable(h_root);
32151 +               if (unlikely(err)) {
32152 +                       err = 1;
32153 +                       goto out;
32154 +               }
32155 +
32156 +               err = au_whdir(h_dir, h_path);
32157 +               if (unlikely(err))
32158 +                       goto out;
32159 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32160 +       } else
32161 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32162 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32163 +       err = au_whdir(h_dir, h_path);
32164 +       if (unlikely(err))
32165 +               goto out;
32166 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32167 +
32168 +out:
32169 +       return err;
32170 +}
32171 +
32172 +/*
32173 + * for the moment, aufs supports the branch filesystem which does not support
32174 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
32175 + * copyup failed. finally, such filesystem will not be used as the writable
32176 + * branch.
32177 + *
32178 + * returns tri-state, see above.
32179 + */
32180 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32181 +                        int do_plink, struct au_wh_base base[],
32182 +                        struct path *h_path)
32183 +{
32184 +       int err;
32185 +       struct inode *h_dir;
32186 +
32187 +       WbrWhMustWriteLock(wbr);
32188 +
32189 +       err = test_linkable(h_root);
32190 +       if (unlikely(err)) {
32191 +               err = 1;
32192 +               goto out;
32193 +       }
32194 +
32195 +       /*
32196 +        * todo: should this create be done in /sbin/mount.aufs helper?
32197 +        */
32198 +       err = -EEXIST;
32199 +       h_dir = d_inode(h_root);
32200 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
32201 +               h_path->dentry = base[AuBrWh_BASE].dentry;
32202 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
32203 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
32204 +               err = 0;
32205 +       else
32206 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
32207 +       if (unlikely(err))
32208 +               goto out;
32209 +
32210 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
32211 +       if (do_plink) {
32212 +               err = au_whdir(h_dir, h_path);
32213 +               if (unlikely(err))
32214 +                       goto out;
32215 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32216 +       } else
32217 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
32218 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
32219 +
32220 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
32221 +       err = au_whdir(h_dir, h_path);
32222 +       if (unlikely(err))
32223 +               goto out;
32224 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32225 +
32226 +out:
32227 +       return err;
32228 +}
32229 +
32230 +/*
32231 + * initialize the whiteout base file/dir for @br.
32232 + */
32233 +int au_wh_init(struct au_branch *br, struct super_block *sb)
32234 +{
32235 +       int err, i;
32236 +       const unsigned char do_plink
32237 +               = !!au_opt_test(au_mntflags(sb), PLINK);
32238 +       struct inode *h_dir;
32239 +       struct path path = br->br_path;
32240 +       struct dentry *h_root = path.dentry;
32241 +       struct au_wbr *wbr = br->br_wbr;
32242 +       static const struct qstr base_name[] = {
32243 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
32244 +                                         sizeof(AUFS_BASE_NAME) - 1),
32245 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
32246 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
32247 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
32248 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
32249 +       };
32250 +       struct au_wh_base base[] = {
32251 +               [AuBrWh_BASE] = {
32252 +                       .name   = base_name + AuBrWh_BASE,
32253 +                       .dentry = NULL
32254 +               },
32255 +               [AuBrWh_PLINK] = {
32256 +                       .name   = base_name + AuBrWh_PLINK,
32257 +                       .dentry = NULL
32258 +               },
32259 +               [AuBrWh_ORPH] = {
32260 +                       .name   = base_name + AuBrWh_ORPH,
32261 +                       .dentry = NULL
32262 +               }
32263 +       };
32264 +
32265 +       if (wbr)
32266 +               WbrWhMustWriteLock(wbr);
32267 +
32268 +       for (i = 0; i < AuBrWh_Last; i++) {
32269 +               /* doubly whiteouted */
32270 +               struct dentry *d;
32271 +
32272 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
32273 +               err = PTR_ERR(d);
32274 +               if (IS_ERR(d))
32275 +                       goto out;
32276 +
32277 +               base[i].dentry = d;
32278 +               AuDebugOn(wbr
32279 +                         && wbr->wbr_wh[i]
32280 +                         && wbr->wbr_wh[i] != base[i].dentry);
32281 +       }
32282 +
32283 +       if (wbr)
32284 +               for (i = 0; i < AuBrWh_Last; i++) {
32285 +                       dput(wbr->wbr_wh[i]);
32286 +                       wbr->wbr_wh[i] = NULL;
32287 +               }
32288 +
32289 +       err = 0;
32290 +       if (!au_br_writable(br->br_perm)) {
32291 +               h_dir = d_inode(h_root);
32292 +               au_wh_init_ro(h_dir, base, &path);
32293 +       } else if (!au_br_wh_linkable(br->br_perm)) {
32294 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
32295 +               if (err > 0)
32296 +                       goto out;
32297 +               else if (err)
32298 +                       goto out_err;
32299 +       } else {
32300 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
32301 +               if (err > 0)
32302 +                       goto out;
32303 +               else if (err)
32304 +                       goto out_err;
32305 +       }
32306 +       goto out; /* success */
32307 +
32308 +out_err:
32309 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
32310 +              err, h_root, au_sbtype(h_root->d_sb));
32311 +out:
32312 +       for (i = 0; i < AuBrWh_Last; i++)
32313 +               dput(base[i].dentry);
32314 +       return err;
32315 +}
32316 +
32317 +/* ---------------------------------------------------------------------- */
32318 +/*
32319 + * whiteouts are all hard-linked usually.
32320 + * when its link count reaches a ceiling, we create a new whiteout base
32321 + * asynchronously.
32322 + */
32323 +
32324 +struct reinit_br_wh {
32325 +       struct super_block *sb;
32326 +       struct au_branch *br;
32327 +};
32328 +
32329 +static void reinit_br_wh(void *arg)
32330 +{
32331 +       int err;
32332 +       aufs_bindex_t bindex;
32333 +       struct path h_path;
32334 +       struct reinit_br_wh *a = arg;
32335 +       struct au_wbr *wbr;
32336 +       struct inode *dir, *delegated;
32337 +       struct dentry *h_root;
32338 +       struct au_hinode *hdir;
32339 +
32340 +       err = 0;
32341 +       wbr = a->br->br_wbr;
32342 +       /* big aufs lock */
32343 +       si_noflush_write_lock(a->sb);
32344 +       if (!au_br_writable(a->br->br_perm))
32345 +               goto out;
32346 +       bindex = au_br_index(a->sb, a->br->br_id);
32347 +       if (unlikely(bindex < 0))
32348 +               goto out;
32349 +
32350 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
32351 +       dir = d_inode(a->sb->s_root);
32352 +       hdir = au_hi(dir, bindex);
32353 +       h_root = au_h_dptr(a->sb->s_root, bindex);
32354 +       AuDebugOn(h_root != au_br_dentry(a->br));
32355 +
32356 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32357 +       wbr_wh_write_lock(wbr);
32358 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
32359 +                         h_root, a->br);
32360 +       if (!err) {
32361 +               h_path.dentry = wbr->wbr_whbase;
32362 +               h_path.mnt = au_br_mnt(a->br);
32363 +               delegated = NULL;
32364 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
32365 +                                  /*force*/0);
32366 +               if (unlikely(err == -EWOULDBLOCK)) {
32367 +                       pr_warn("cannot retry for NFSv4 delegation"
32368 +                               " for an internal unlink\n");
32369 +                       iput(delegated);
32370 +               }
32371 +       } else {
32372 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
32373 +               err = 0;
32374 +       }
32375 +       dput(wbr->wbr_whbase);
32376 +       wbr->wbr_whbase = NULL;
32377 +       if (!err)
32378 +               err = au_wh_init(a->br, a->sb);
32379 +       wbr_wh_write_unlock(wbr);
32380 +       au_hn_imtx_unlock(hdir);
32381 +       di_read_unlock(a->sb->s_root, AuLock_IR);
32382 +       if (!err)
32383 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
32384 +
32385 +out:
32386 +       if (wbr)
32387 +               atomic_dec(&wbr->wbr_wh_running);
32388 +       atomic_dec(&a->br->br_count);
32389 +       si_write_unlock(a->sb);
32390 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
32391 +       kfree(arg);
32392 +       if (unlikely(err))
32393 +               AuIOErr("err %d\n", err);
32394 +}
32395 +
32396 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
32397 +{
32398 +       int do_dec, wkq_err;
32399 +       struct reinit_br_wh *arg;
32400 +
32401 +       do_dec = 1;
32402 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
32403 +               goto out;
32404 +
32405 +       /* ignore ENOMEM */
32406 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
32407 +       if (arg) {
32408 +               /*
32409 +                * dec(wh_running), kfree(arg) and dec(br_count)
32410 +                * in reinit function
32411 +                */
32412 +               arg->sb = sb;
32413 +               arg->br = br;
32414 +               atomic_inc(&br->br_count);
32415 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
32416 +               if (unlikely(wkq_err)) {
32417 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
32418 +                       atomic_dec(&br->br_count);
32419 +                       kfree(arg);
32420 +               }
32421 +               do_dec = 0;
32422 +       }
32423 +
32424 +out:
32425 +       if (do_dec)
32426 +               atomic_dec(&br->br_wbr->wbr_wh_running);
32427 +}
32428 +
32429 +/* ---------------------------------------------------------------------- */
32430 +
32431 +/*
32432 + * create the whiteout @wh.
32433 + */
32434 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
32435 +                            struct dentry *wh)
32436 +{
32437 +       int err;
32438 +       struct path h_path = {
32439 +               .dentry = wh
32440 +       };
32441 +       struct au_branch *br;
32442 +       struct au_wbr *wbr;
32443 +       struct dentry *h_parent;
32444 +       struct inode *h_dir, *delegated;
32445 +
32446 +       h_parent = wh->d_parent; /* dir inode is locked */
32447 +       h_dir = d_inode(h_parent);
32448 +       IMustLock(h_dir);
32449 +
32450 +       br = au_sbr(sb, bindex);
32451 +       h_path.mnt = au_br_mnt(br);
32452 +       wbr = br->br_wbr;
32453 +       wbr_wh_read_lock(wbr);
32454 +       if (wbr->wbr_whbase) {
32455 +               delegated = NULL;
32456 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
32457 +               if (unlikely(err == -EWOULDBLOCK)) {
32458 +                       pr_warn("cannot retry for NFSv4 delegation"
32459 +                               " for an internal link\n");
32460 +                       iput(delegated);
32461 +               }
32462 +               if (!err || err != -EMLINK)
32463 +                       goto out;
32464 +
32465 +               /* link count full. re-initialize br_whbase. */
32466 +               kick_reinit_br_wh(sb, br);
32467 +       }
32468 +
32469 +       /* return this error in this context */
32470 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
32471 +       if (!err)
32472 +               au_fhsm_wrote(sb, bindex, /*force*/0);
32473 +
32474 +out:
32475 +       wbr_wh_read_unlock(wbr);
32476 +       return err;
32477 +}
32478 +
32479 +/* ---------------------------------------------------------------------- */
32480 +
32481 +/*
32482 + * create or remove the diropq.
32483 + */
32484 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
32485 +                               unsigned int flags)
32486 +{
32487 +       struct dentry *opq_dentry, *h_dentry;
32488 +       struct super_block *sb;
32489 +       struct au_branch *br;
32490 +       int err;
32491 +
32492 +       sb = dentry->d_sb;
32493 +       br = au_sbr(sb, bindex);
32494 +       h_dentry = au_h_dptr(dentry, bindex);
32495 +       opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
32496 +       if (IS_ERR(opq_dentry))
32497 +               goto out;
32498 +
32499 +       if (au_ftest_diropq(flags, CREATE)) {
32500 +               err = link_or_create_wh(sb, bindex, opq_dentry);
32501 +               if (!err) {
32502 +                       au_set_dbdiropq(dentry, bindex);
32503 +                       goto out; /* success */
32504 +               }
32505 +       } else {
32506 +               struct path tmp = {
32507 +                       .dentry = opq_dentry,
32508 +                       .mnt    = au_br_mnt(br)
32509 +               };
32510 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
32511 +               if (!err)
32512 +                       au_set_dbdiropq(dentry, -1);
32513 +       }
32514 +       dput(opq_dentry);
32515 +       opq_dentry = ERR_PTR(err);
32516 +
32517 +out:
32518 +       return opq_dentry;
32519 +}
32520 +
32521 +struct do_diropq_args {
32522 +       struct dentry **errp;
32523 +       struct dentry *dentry;
32524 +       aufs_bindex_t bindex;
32525 +       unsigned int flags;
32526 +};
32527 +
32528 +static void call_do_diropq(void *args)
32529 +{
32530 +       struct do_diropq_args *a = args;
32531 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
32532 +}
32533 +
32534 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32535 +                            unsigned int flags)
32536 +{
32537 +       struct dentry *diropq, *h_dentry;
32538 +
32539 +       h_dentry = au_h_dptr(dentry, bindex);
32540 +       if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
32541 +               diropq = do_diropq(dentry, bindex, flags);
32542 +       else {
32543 +               int wkq_err;
32544 +               struct do_diropq_args args = {
32545 +                       .errp           = &diropq,
32546 +                       .dentry         = dentry,
32547 +                       .bindex         = bindex,
32548 +                       .flags          = flags
32549 +               };
32550 +
32551 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
32552 +               if (unlikely(wkq_err))
32553 +                       diropq = ERR_PTR(wkq_err);
32554 +       }
32555 +
32556 +       return diropq;
32557 +}
32558 +
32559 +/* ---------------------------------------------------------------------- */
32560 +
32561 +/*
32562 + * lookup whiteout dentry.
32563 + * @h_parent: lower parent dentry which must exist and be locked
32564 + * @base_name: name of dentry which will be whiteouted
32565 + * returns dentry for whiteout.
32566 + */
32567 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32568 +                         struct au_branch *br)
32569 +{
32570 +       int err;
32571 +       struct qstr wh_name;
32572 +       struct dentry *wh_dentry;
32573 +
32574 +       err = au_wh_name_alloc(&wh_name, base_name);
32575 +       wh_dentry = ERR_PTR(err);
32576 +       if (!err) {
32577 +               wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
32578 +               kfree(wh_name.name);
32579 +       }
32580 +       return wh_dentry;
32581 +}
32582 +
32583 +/*
32584 + * link/create a whiteout for @dentry on @bindex.
32585 + */
32586 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32587 +                           struct dentry *h_parent)
32588 +{
32589 +       struct dentry *wh_dentry;
32590 +       struct super_block *sb;
32591 +       int err;
32592 +
32593 +       sb = dentry->d_sb;
32594 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
32595 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
32596 +               err = link_or_create_wh(sb, bindex, wh_dentry);
32597 +               if (!err) {
32598 +                       au_set_dbwh(dentry, bindex);
32599 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
32600 +               } else {
32601 +                       dput(wh_dentry);
32602 +                       wh_dentry = ERR_PTR(err);
32603 +               }
32604 +       }
32605 +
32606 +       return wh_dentry;
32607 +}
32608 +
32609 +/* ---------------------------------------------------------------------- */
32610 +
32611 +/* Delete all whiteouts in this directory on branch bindex. */
32612 +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
32613 +                          aufs_bindex_t bindex, struct au_branch *br)
32614 +{
32615 +       int err;
32616 +       unsigned long ul, n;
32617 +       struct qstr wh_name;
32618 +       char *p;
32619 +       struct hlist_head *head;
32620 +       struct au_vdir_wh *pos;
32621 +       struct au_vdir_destr *str;
32622 +
32623 +       err = -ENOMEM;
32624 +       p = (void *)__get_free_page(GFP_NOFS);
32625 +       wh_name.name = p;
32626 +       if (unlikely(!wh_name.name))
32627 +               goto out;
32628 +
32629 +       err = 0;
32630 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32631 +       p += AUFS_WH_PFX_LEN;
32632 +       n = whlist->nh_num;
32633 +       head = whlist->nh_head;
32634 +       for (ul = 0; !err && ul < n; ul++, head++) {
32635 +               hlist_for_each_entry(pos, head, wh_hash) {
32636 +                       if (pos->wh_bindex != bindex)
32637 +                               continue;
32638 +
32639 +                       str = &pos->wh_str;
32640 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
32641 +                               memcpy(p, str->name, str->len);
32642 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
32643 +                               err = unlink_wh_name(h_dentry, &wh_name, br);
32644 +                               if (!err)
32645 +                                       continue;
32646 +                               break;
32647 +                       }
32648 +                       AuIOErr("whiteout name too long %.*s\n",
32649 +                               str->len, str->name);
32650 +                       err = -EIO;
32651 +                       break;
32652 +               }
32653 +       }
32654 +       free_page((unsigned long)wh_name.name);
32655 +
32656 +out:
32657 +       return err;
32658 +}
32659 +
32660 +struct del_wh_children_args {
32661 +       int *errp;
32662 +       struct dentry *h_dentry;
32663 +       struct au_nhash *whlist;
32664 +       aufs_bindex_t bindex;
32665 +       struct au_branch *br;
32666 +};
32667 +
32668 +static void call_del_wh_children(void *args)
32669 +{
32670 +       struct del_wh_children_args *a = args;
32671 +       *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
32672 +}
32673 +
32674 +/* ---------------------------------------------------------------------- */
32675 +
32676 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
32677 +{
32678 +       struct au_whtmp_rmdir *whtmp;
32679 +       int err;
32680 +       unsigned int rdhash;
32681 +
32682 +       SiMustAnyLock(sb);
32683 +
32684 +       whtmp = kmalloc(sizeof(*whtmp), gfp);
32685 +       if (unlikely(!whtmp)) {
32686 +               whtmp = ERR_PTR(-ENOMEM);
32687 +               goto out;
32688 +       }
32689 +
32690 +       whtmp->dir = NULL;
32691 +       whtmp->br = NULL;
32692 +       whtmp->wh_dentry = NULL;
32693 +       /* no estimation for dir size */
32694 +       rdhash = au_sbi(sb)->si_rdhash;
32695 +       if (!rdhash)
32696 +               rdhash = AUFS_RDHASH_DEF;
32697 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
32698 +       if (unlikely(err)) {
32699 +               kfree(whtmp);
32700 +               whtmp = ERR_PTR(err);
32701 +       }
32702 +
32703 +out:
32704 +       return whtmp;
32705 +}
32706 +
32707 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
32708 +{
32709 +       if (whtmp->br)
32710 +               atomic_dec(&whtmp->br->br_count);
32711 +       dput(whtmp->wh_dentry);
32712 +       iput(whtmp->dir);
32713 +       au_nhash_wh_free(&whtmp->whlist);
32714 +       kfree(whtmp);
32715 +}
32716 +
32717 +/*
32718 + * rmdir the whiteouted temporary named dir @h_dentry.
32719 + * @whlist: whiteouted children.
32720 + */
32721 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32722 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
32723 +{
32724 +       int err;
32725 +       unsigned int h_nlink;
32726 +       struct path h_tmp;
32727 +       struct inode *wh_inode, *h_dir;
32728 +       struct au_branch *br;
32729 +
32730 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
32731 +       IMustLock(h_dir);
32732 +
32733 +       br = au_sbr(dir->i_sb, bindex);
32734 +       wh_inode = d_inode(wh_dentry);
32735 +       mutex_lock_nested(&wh_inode->i_mutex, AuLsc_I_CHILD);
32736 +
32737 +       /*
32738 +        * someone else might change some whiteouts while we were sleeping.
32739 +        * it means this whlist may have an obsoleted entry.
32740 +        */
32741 +       if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
32742 +               err = del_wh_children(wh_dentry, whlist, bindex, br);
32743 +       else {
32744 +               int wkq_err;
32745 +               struct del_wh_children_args args = {
32746 +                       .errp           = &err,
32747 +                       .h_dentry       = wh_dentry,
32748 +                       .whlist         = whlist,
32749 +                       .bindex         = bindex,
32750 +                       .br             = br
32751 +               };
32752 +
32753 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
32754 +               if (unlikely(wkq_err))
32755 +                       err = wkq_err;
32756 +       }
32757 +       mutex_unlock(&wh_inode->i_mutex);
32758 +
32759 +       if (!err) {
32760 +               h_tmp.dentry = wh_dentry;
32761 +               h_tmp.mnt = au_br_mnt(br);
32762 +               h_nlink = h_dir->i_nlink;
32763 +               err = vfsub_rmdir(h_dir, &h_tmp);
32764 +               /* some fs doesn't change the parent nlink in some cases */
32765 +               h_nlink -= h_dir->i_nlink;
32766 +       }
32767 +
32768 +       if (!err) {
32769 +               if (au_ibstart(dir) == bindex) {
32770 +                       /* todo: dir->i_mutex is necessary */
32771 +                       au_cpup_attr_timesizes(dir);
32772 +                       if (h_nlink)
32773 +                               vfsub_drop_nlink(dir);
32774 +               }
32775 +               return 0; /* success */
32776 +       }
32777 +
32778 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
32779 +       return err;
32780 +}
32781 +
32782 +static void call_rmdir_whtmp(void *args)
32783 +{
32784 +       int err;
32785 +       aufs_bindex_t bindex;
32786 +       struct au_whtmp_rmdir *a = args;
32787 +       struct super_block *sb;
32788 +       struct dentry *h_parent;
32789 +       struct inode *h_dir;
32790 +       struct au_hinode *hdir;
32791 +
32792 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
32793 +       /* mutex_lock(&a->dir->i_mutex); */
32794 +       err = -EROFS;
32795 +       sb = a->dir->i_sb;
32796 +       si_read_lock(sb, !AuLock_FLUSH);
32797 +       if (!au_br_writable(a->br->br_perm))
32798 +               goto out;
32799 +       bindex = au_br_index(sb, a->br->br_id);
32800 +       if (unlikely(bindex < 0))
32801 +               goto out;
32802 +
32803 +       err = -EIO;
32804 +       ii_write_lock_parent(a->dir);
32805 +       h_parent = dget_parent(a->wh_dentry);
32806 +       h_dir = d_inode(h_parent);
32807 +       hdir = au_hi(a->dir, bindex);
32808 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
32809 +       if (unlikely(err))
32810 +               goto out_mnt;
32811 +       au_hn_imtx_lock_nested(hdir, AuLsc_I_PARENT);
32812 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
32813 +                         a->br);
32814 +       if (!err)
32815 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
32816 +       au_hn_imtx_unlock(hdir);
32817 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
32818 +
32819 +out_mnt:
32820 +       dput(h_parent);
32821 +       ii_write_unlock(a->dir);
32822 +out:
32823 +       /* mutex_unlock(&a->dir->i_mutex); */
32824 +       au_whtmp_rmdir_free(a);
32825 +       si_read_unlock(sb);
32826 +       au_nwt_done(&au_sbi(sb)->si_nowait);
32827 +       if (unlikely(err))
32828 +               AuIOErr("err %d\n", err);
32829 +}
32830 +
32831 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32832 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
32833 +{
32834 +       int wkq_err;
32835 +       struct super_block *sb;
32836 +
32837 +       IMustLock(dir);
32838 +
32839 +       /* all post-process will be done in do_rmdir_whtmp(). */
32840 +       sb = dir->i_sb;
32841 +       args->dir = au_igrab(dir);
32842 +       args->br = au_sbr(sb, bindex);
32843 +       atomic_inc(&args->br->br_count);
32844 +       args->wh_dentry = dget(wh_dentry);
32845 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
32846 +       if (unlikely(wkq_err)) {
32847 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
32848 +               au_whtmp_rmdir_free(args);
32849 +       }
32850 +}
32851 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
32852 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
32853 +++ linux/fs/aufs/whout.h       2015-09-24 10:47:58.258053165 +0200
32854 @@ -0,0 +1,85 @@
32855 +/*
32856 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32857 + *
32858 + * This program, aufs is free software; you can redistribute it and/or modify
32859 + * it under the terms of the GNU General Public License as published by
32860 + * the Free Software Foundation; either version 2 of the License, or
32861 + * (at your option) any later version.
32862 + *
32863 + * This program is distributed in the hope that it will be useful,
32864 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32865 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32866 + * GNU General Public License for more details.
32867 + *
32868 + * You should have received a copy of the GNU General Public License
32869 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32870 + */
32871 +
32872 +/*
32873 + * whiteout for logical deletion and opaque directory
32874 + */
32875 +
32876 +#ifndef __AUFS_WHOUT_H__
32877 +#define __AUFS_WHOUT_H__
32878 +
32879 +#ifdef __KERNEL__
32880 +
32881 +#include "dir.h"
32882 +
32883 +/* whout.c */
32884 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
32885 +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
32886 +int au_diropq_test(struct dentry *h_dentry);
32887 +struct au_branch;
32888 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32889 +                            struct qstr *prefix);
32890 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
32891 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32892 +                       struct dentry *dentry);
32893 +int au_wh_init(struct au_branch *br, struct super_block *sb);
32894 +
32895 +/* diropq flags */
32896 +#define AuDiropq_CREATE        1
32897 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
32898 +#define au_fset_diropq(flags, name) \
32899 +       do { (flags) |= AuDiropq_##name; } while (0)
32900 +#define au_fclr_diropq(flags, name) \
32901 +       do { (flags) &= ~AuDiropq_##name; } while (0)
32902 +
32903 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32904 +                            unsigned int flags);
32905 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32906 +                         struct au_branch *br);
32907 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32908 +                           struct dentry *h_parent);
32909 +
32910 +/* real rmdir for the whiteout-ed dir */
32911 +struct au_whtmp_rmdir {
32912 +       struct inode *dir;
32913 +       struct au_branch *br;
32914 +       struct dentry *wh_dentry;
32915 +       struct au_nhash whlist;
32916 +};
32917 +
32918 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
32919 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
32920 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
32921 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
32922 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
32923 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
32924 +
32925 +/* ---------------------------------------------------------------------- */
32926 +
32927 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
32928 +                                             aufs_bindex_t bindex)
32929 +{
32930 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
32931 +}
32932 +
32933 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
32934 +{
32935 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
32936 +}
32937 +
32938 +#endif /* __KERNEL__ */
32939 +#endif /* __AUFS_WHOUT_H__ */
32940 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
32941 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
32942 +++ linux/fs/aufs/wkq.c 2015-09-24 10:47:58.258053165 +0200
32943 @@ -0,0 +1,213 @@
32944 +/*
32945 + * Copyright (C) 2005-2015 Junjiro R. Okajima
32946 + *
32947 + * This program, aufs is free software; you can redistribute it and/or modify
32948 + * it under the terms of the GNU General Public License as published by
32949 + * the Free Software Foundation; either version 2 of the License, or
32950 + * (at your option) any later version.
32951 + *
32952 + * This program is distributed in the hope that it will be useful,
32953 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32954 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32955 + * GNU General Public License for more details.
32956 + *
32957 + * You should have received a copy of the GNU General Public License
32958 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32959 + */
32960 +
32961 +/*
32962 + * workqueue for asynchronous/super-io operations
32963 + * todo: try new dredential scheme
32964 + */
32965 +
32966 +#include <linux/module.h>
32967 +#include "aufs.h"
32968 +
32969 +/* internal workqueue named AUFS_WKQ_NAME */
32970 +
32971 +static struct workqueue_struct *au_wkq;
32972 +
32973 +struct au_wkinfo {
32974 +       struct work_struct wk;
32975 +       struct kobject *kobj;
32976 +
32977 +       unsigned int flags; /* see wkq.h */
32978 +
32979 +       au_wkq_func_t func;
32980 +       void *args;
32981 +
32982 +       struct completion *comp;
32983 +};
32984 +
32985 +/* ---------------------------------------------------------------------- */
32986 +
32987 +static void wkq_func(struct work_struct *wk)
32988 +{
32989 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
32990 +
32991 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
32992 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
32993 +
32994 +       wkinfo->func(wkinfo->args);
32995 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
32996 +               complete(wkinfo->comp);
32997 +       else {
32998 +               kobject_put(wkinfo->kobj);
32999 +               module_put(THIS_MODULE); /* todo: ?? */
33000 +               kfree(wkinfo);
33001 +       }
33002 +}
33003 +
33004 +/*
33005 + * Since struct completion is large, try allocating it dynamically.
33006 + */
33007 +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
33008 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
33009 +
33010 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33011 +{
33012 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33013 +       if (*comp) {
33014 +               init_completion(*comp);
33015 +               wkinfo->comp = *comp;
33016 +               return 0;
33017 +       }
33018 +       return -ENOMEM;
33019 +}
33020 +
33021 +static void au_wkq_comp_free(struct completion *comp)
33022 +{
33023 +       kfree(comp);
33024 +}
33025 +
33026 +#else
33027 +
33028 +/* no braces */
33029 +#define AuWkqCompDeclare(name) \
33030 +       DECLARE_COMPLETION_ONSTACK(_ ## name); \
33031 +       struct completion *comp = &_ ## name
33032 +
33033 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33034 +{
33035 +       wkinfo->comp = *comp;
33036 +       return 0;
33037 +}
33038 +
33039 +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33040 +{
33041 +       /* empty */
33042 +}
33043 +#endif /* 4KSTACKS */
33044 +
33045 +static void au_wkq_run(struct au_wkinfo *wkinfo)
33046 +{
33047 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
33048 +               if (au_wkq_test()) {
33049 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33050 +                               " due to a dead dir by UDBA?\n");
33051 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33052 +               }
33053 +       } else
33054 +               au_dbg_verify_kthread();
33055 +
33056 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
33057 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
33058 +               queue_work(au_wkq, &wkinfo->wk);
33059 +       } else {
33060 +               INIT_WORK(&wkinfo->wk, wkq_func);
33061 +               schedule_work(&wkinfo->wk);
33062 +       }
33063 +}
33064 +
33065 +/*
33066 + * Be careful. It is easy to make deadlock happen.
33067 + * processA: lock, wkq and wait
33068 + * processB: wkq and wait, lock in wkq
33069 + * --> deadlock
33070 + */
33071 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
33072 +{
33073 +       int err;
33074 +       AuWkqCompDeclare(comp);
33075 +       struct au_wkinfo wkinfo = {
33076 +               .flags  = flags,
33077 +               .func   = func,
33078 +               .args   = args
33079 +       };
33080 +
33081 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
33082 +       if (!err) {
33083 +               au_wkq_run(&wkinfo);
33084 +               /* no timeout, no interrupt */
33085 +               wait_for_completion(wkinfo.comp);
33086 +               au_wkq_comp_free(comp);
33087 +               destroy_work_on_stack(&wkinfo.wk);
33088 +       }
33089 +
33090 +       return err;
33091 +
33092 +}
33093 +
33094 +/*
33095 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33096 + * problem in a concurrent umounting.
33097 + */
33098 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33099 +                 unsigned int flags)
33100 +{
33101 +       int err;
33102 +       struct au_wkinfo *wkinfo;
33103 +
33104 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
33105 +
33106 +       /*
33107 +        * wkq_func() must free this wkinfo.
33108 +        * it highly depends upon the implementation of workqueue.
33109 +        */
33110 +       err = 0;
33111 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33112 +       if (wkinfo) {
33113 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
33114 +               wkinfo->flags = flags & ~AuWkq_WAIT;
33115 +               wkinfo->func = func;
33116 +               wkinfo->args = args;
33117 +               wkinfo->comp = NULL;
33118 +               kobject_get(wkinfo->kobj);
33119 +               __module_get(THIS_MODULE); /* todo: ?? */
33120 +
33121 +               au_wkq_run(wkinfo);
33122 +       } else {
33123 +               err = -ENOMEM;
33124 +               au_nwt_done(&au_sbi(sb)->si_nowait);
33125 +       }
33126 +
33127 +       return err;
33128 +}
33129 +
33130 +/* ---------------------------------------------------------------------- */
33131 +
33132 +void au_nwt_init(struct au_nowait_tasks *nwt)
33133 +{
33134 +       atomic_set(&nwt->nw_len, 0);
33135 +       /* smp_mb(); */ /* atomic_set */
33136 +       init_waitqueue_head(&nwt->nw_wq);
33137 +}
33138 +
33139 +void au_wkq_fin(void)
33140 +{
33141 +       destroy_workqueue(au_wkq);
33142 +}
33143 +
33144 +int __init au_wkq_init(void)
33145 +{
33146 +       int err;
33147 +
33148 +       err = 0;
33149 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
33150 +       if (IS_ERR(au_wkq))
33151 +               err = PTR_ERR(au_wkq);
33152 +       else if (!au_wkq)
33153 +               err = -ENOMEM;
33154 +
33155 +       return err;
33156 +}
33157 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33158 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
33159 +++ linux/fs/aufs/wkq.h 2015-09-24 10:47:58.258053165 +0200
33160 @@ -0,0 +1,91 @@
33161 +/*
33162 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33163 + *
33164 + * This program, aufs is free software; you can redistribute it and/or modify
33165 + * it under the terms of the GNU General Public License as published by
33166 + * the Free Software Foundation; either version 2 of the License, or
33167 + * (at your option) any later version.
33168 + *
33169 + * This program is distributed in the hope that it will be useful,
33170 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33171 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33172 + * GNU General Public License for more details.
33173 + *
33174 + * You should have received a copy of the GNU General Public License
33175 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33176 + */
33177 +
33178 +/*
33179 + * workqueue for asynchronous/super-io operations
33180 + * todo: try new credentials management scheme
33181 + */
33182 +
33183 +#ifndef __AUFS_WKQ_H__
33184 +#define __AUFS_WKQ_H__
33185 +
33186 +#ifdef __KERNEL__
33187 +
33188 +struct super_block;
33189 +
33190 +/* ---------------------------------------------------------------------- */
33191 +
33192 +/*
33193 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
33194 + */
33195 +struct au_nowait_tasks {
33196 +       atomic_t                nw_len;
33197 +       wait_queue_head_t       nw_wq;
33198 +};
33199 +
33200 +/* ---------------------------------------------------------------------- */
33201 +
33202 +typedef void (*au_wkq_func_t)(void *args);
33203 +
33204 +/* wkq flags */
33205 +#define AuWkq_WAIT     1
33206 +#define AuWkq_NEST     (1 << 1)
33207 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
33208 +#define au_fset_wkq(flags, name) \
33209 +       do { (flags) |= AuWkq_##name; } while (0)
33210 +#define au_fclr_wkq(flags, name) \
33211 +       do { (flags) &= ~AuWkq_##name; } while (0)
33212 +
33213 +#ifndef CONFIG_AUFS_HNOTIFY
33214 +#undef AuWkq_NEST
33215 +#define AuWkq_NEST     0
33216 +#endif
33217 +
33218 +/* wkq.c */
33219 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
33220 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33221 +                 unsigned int flags);
33222 +void au_nwt_init(struct au_nowait_tasks *nwt);
33223 +int __init au_wkq_init(void);
33224 +void au_wkq_fin(void);
33225 +
33226 +/* ---------------------------------------------------------------------- */
33227 +
33228 +static inline int au_wkq_test(void)
33229 +{
33230 +       return current->flags & PF_WQ_WORKER;
33231 +}
33232 +
33233 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
33234 +{
33235 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
33236 +}
33237 +
33238 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
33239 +{
33240 +       if (atomic_dec_and_test(&nwt->nw_len))
33241 +               wake_up_all(&nwt->nw_wq);
33242 +}
33243 +
33244 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
33245 +{
33246 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
33247 +       return 0;
33248 +}
33249 +
33250 +#endif /* __KERNEL__ */
33251 +#endif /* __AUFS_WKQ_H__ */
33252 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
33253 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
33254 +++ linux/fs/aufs/xattr.c       2015-09-24 10:47:58.258053165 +0200
33255 @@ -0,0 +1,344 @@
33256 +/*
33257 + * Copyright (C) 2014-2015 Junjiro R. Okajima
33258 + *
33259 + * This program, aufs is free software; you can redistribute it and/or modify
33260 + * it under the terms of the GNU General Public License as published by
33261 + * the Free Software Foundation; either version 2 of the License, or
33262 + * (at your option) any later version.
33263 + *
33264 + * This program is distributed in the hope that it will be useful,
33265 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33266 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33267 + * GNU General Public License for more details.
33268 + *
33269 + * You should have received a copy of the GNU General Public License
33270 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33271 + */
33272 +
33273 +/*
33274 + * handling xattr functions
33275 + */
33276 +
33277 +#include <linux/xattr.h>
33278 +#include "aufs.h"
33279 +
33280 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
33281 +{
33282 +       if (!ignore_flags)
33283 +               goto out;
33284 +       switch (err) {
33285 +       case -ENOMEM:
33286 +       case -EDQUOT:
33287 +               goto out;
33288 +       }
33289 +
33290 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
33291 +               err = 0;
33292 +               goto out;
33293 +       }
33294 +
33295 +#define cmp(brattr, prefix) do {                                       \
33296 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
33297 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
33298 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
33299 +                               err = 0;                                \
33300 +                       goto out;                                       \
33301 +               }                                                       \
33302 +       } while (0)
33303 +
33304 +       cmp(SEC, SECURITY);
33305 +       cmp(SYS, SYSTEM);
33306 +       cmp(TR, TRUSTED);
33307 +       cmp(USR, USER);
33308 +#undef cmp
33309 +
33310 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
33311 +               err = 0;
33312 +
33313 +out:
33314 +       return err;
33315 +}
33316 +
33317 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
33318 +
33319 +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
33320 +                           char *name, char **buf, unsigned int ignore_flags,
33321 +                           unsigned int verbose)
33322 +{
33323 +       int err;
33324 +       ssize_t ssz;
33325 +       struct inode *h_idst;
33326 +
33327 +       ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
33328 +       err = ssz;
33329 +       if (unlikely(err <= 0)) {
33330 +               if (err == -ENODATA
33331 +                   || (err == -EOPNOTSUPP
33332 +                       && ((ignore_flags & au_xattr_out_of_list)
33333 +                           || (au_test_nfs_noacl(d_inode(h_src))
33334 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
33335 +                                   || !strcmp(name,
33336 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
33337 +                           ))
33338 +                       err = 0;
33339 +               if (err && (verbose || au_debug_test()))
33340 +                       pr_err("%s, err %d\n", name, err);
33341 +               goto out;
33342 +       }
33343 +
33344 +       /* unlock it temporary */
33345 +       h_idst = d_inode(h_dst);
33346 +       mutex_unlock(&h_idst->i_mutex);
33347 +       err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
33348 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
33349 +       if (unlikely(err)) {
33350 +               if (verbose || au_debug_test())
33351 +                       pr_err("%s, err %d\n", name, err);
33352 +               err = au_xattr_ignore(err, name, ignore_flags);
33353 +       }
33354 +
33355 +out:
33356 +       return err;
33357 +}
33358 +
33359 +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
33360 +                 unsigned int verbose)
33361 +{
33362 +       int err, unlocked, acl_access, acl_default;
33363 +       ssize_t ssz;
33364 +       struct inode *h_isrc, *h_idst;
33365 +       char *value, *p, *o, *e;
33366 +
33367 +       /* try stopping to update the source inode while we are referencing */
33368 +       /* there should not be the parent-child relationship between them */
33369 +       h_isrc = d_inode(h_src);
33370 +       h_idst = d_inode(h_dst);
33371 +       mutex_unlock(&h_idst->i_mutex);
33372 +       mutex_lock_nested(&h_isrc->i_mutex, AuLsc_I_CHILD);
33373 +       mutex_lock_nested(&h_idst->i_mutex, AuLsc_I_CHILD2);
33374 +       unlocked = 0;
33375 +
33376 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
33377 +       ssz = vfs_listxattr(h_src, NULL, 0);
33378 +       err = ssz;
33379 +       if (unlikely(err < 0)) {
33380 +               AuTraceErr(err);
33381 +               if (err == -ENODATA
33382 +                   || err == -EOPNOTSUPP)
33383 +                       err = 0;        /* ignore */
33384 +               goto out;
33385 +       }
33386 +
33387 +       err = 0;
33388 +       p = NULL;
33389 +       o = NULL;
33390 +       if (ssz) {
33391 +               err = -ENOMEM;
33392 +               p = kmalloc(ssz, GFP_NOFS);
33393 +               o = p;
33394 +               if (unlikely(!p))
33395 +                       goto out;
33396 +               err = vfs_listxattr(h_src, p, ssz);
33397 +       }
33398 +       mutex_unlock(&h_isrc->i_mutex);
33399 +       unlocked = 1;
33400 +       AuDbg("err %d, ssz %zd\n", err, ssz);
33401 +       if (unlikely(err < 0))
33402 +               goto out_free;
33403 +
33404 +       err = 0;
33405 +       e = p + ssz;
33406 +       value = NULL;
33407 +       acl_access = 0;
33408 +       acl_default = 0;
33409 +       while (!err && p < e) {
33410 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
33411 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
33412 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
33413 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
33414 +                                       - 1);
33415 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
33416 +                                      verbose);
33417 +               p += strlen(p) + 1;
33418 +       }
33419 +       AuTraceErr(err);
33420 +       ignore_flags |= au_xattr_out_of_list;
33421 +       if (!err && !acl_access) {
33422 +               err = au_do_cpup_xattr(h_dst, h_src,
33423 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
33424 +                                      ignore_flags, verbose);
33425 +               AuTraceErr(err);
33426 +       }
33427 +       if (!err && !acl_default) {
33428 +               err = au_do_cpup_xattr(h_dst, h_src,
33429 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
33430 +                                      ignore_flags, verbose);
33431 +               AuTraceErr(err);
33432 +       }
33433 +
33434 +       kfree(value);
33435 +
33436 +out_free:
33437 +       kfree(o);
33438 +out:
33439 +       if (!unlocked)
33440 +               mutex_unlock(&h_isrc->i_mutex);
33441 +       AuTraceErr(err);
33442 +       return err;
33443 +}
33444 +
33445 +/* ---------------------------------------------------------------------- */
33446 +
33447 +enum {
33448 +       AU_XATTR_LIST,
33449 +       AU_XATTR_GET
33450 +};
33451 +
33452 +struct au_lgxattr {
33453 +       int type;
33454 +       union {
33455 +               struct {
33456 +                       char    *list;
33457 +                       size_t  size;
33458 +               } list;
33459 +               struct {
33460 +                       const char      *name;
33461 +                       void            *value;
33462 +                       size_t          size;
33463 +               } get;
33464 +       } u;
33465 +};
33466 +
33467 +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
33468 +{
33469 +       ssize_t err;
33470 +       struct path h_path;
33471 +       struct super_block *sb;
33472 +
33473 +       sb = dentry->d_sb;
33474 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
33475 +       if (unlikely(err))
33476 +               goto out;
33477 +       err = au_h_path_getattr(dentry, /*force*/1, &h_path);
33478 +       if (unlikely(err))
33479 +               goto out_si;
33480 +       if (unlikely(!h_path.dentry))
33481 +               /* illegally overlapped or something */
33482 +               goto out_di; /* pretending success */
33483 +
33484 +       /* always topmost entry only */
33485 +       switch (arg->type) {
33486 +       case AU_XATTR_LIST:
33487 +               err = vfs_listxattr(h_path.dentry,
33488 +                                   arg->u.list.list, arg->u.list.size);
33489 +               break;
33490 +       case AU_XATTR_GET:
33491 +               err = vfs_getxattr(h_path.dentry,
33492 +                                  arg->u.get.name, arg->u.get.value,
33493 +                                  arg->u.get.size);
33494 +               break;
33495 +       }
33496 +
33497 +out_di:
33498 +       di_read_unlock(dentry, AuLock_IR);
33499 +out_si:
33500 +       si_read_unlock(sb);
33501 +out:
33502 +       AuTraceErr(err);
33503 +       return err;
33504 +}
33505 +
33506 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
33507 +{
33508 +       struct au_lgxattr arg = {
33509 +               .type = AU_XATTR_LIST,
33510 +               .u.list = {
33511 +                       .list   = list,
33512 +                       .size   = size
33513 +               },
33514 +       };
33515 +
33516 +       return au_lgxattr(dentry, &arg);
33517 +}
33518 +
33519 +ssize_t aufs_getxattr(struct dentry *dentry, const char *name, void *value,
33520 +                     size_t size)
33521 +{
33522 +       struct au_lgxattr arg = {
33523 +               .type = AU_XATTR_GET,
33524 +               .u.get = {
33525 +                       .name   = name,
33526 +                       .value  = value,
33527 +                       .size   = size
33528 +               },
33529 +       };
33530 +
33531 +       return au_lgxattr(dentry, &arg);
33532 +}
33533 +
33534 +int aufs_setxattr(struct dentry *dentry, const char *name, const void *value,
33535 +                 size_t size, int flags)
33536 +{
33537 +       struct au_srxattr arg = {
33538 +               .type = AU_XATTR_SET,
33539 +               .u.set = {
33540 +                       .name   = name,
33541 +                       .value  = value,
33542 +                       .size   = size,
33543 +                       .flags  = flags
33544 +               },
33545 +       };
33546 +
33547 +       return au_srxattr(dentry, &arg);
33548 +}
33549 +
33550 +int aufs_removexattr(struct dentry *dentry, const char *name)
33551 +{
33552 +       struct au_srxattr arg = {
33553 +               .type = AU_XATTR_REMOVE,
33554 +               .u.remove = {
33555 +                       .name   = name
33556 +               },
33557 +       };
33558 +
33559 +       return au_srxattr(dentry, &arg);
33560 +}
33561 +
33562 +/* ---------------------------------------------------------------------- */
33563 +
33564 +#if 0
33565 +static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
33566 +                           const char *name, size_t name_len, int type)
33567 +{
33568 +       return aufs_listxattr(dentry, list, list_size);
33569 +}
33570 +
33571 +static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
33572 +                       size_t size, int type)
33573 +{
33574 +       return aufs_getxattr(dentry, name, buffer, size);
33575 +}
33576 +
33577 +static int au_xattr_set(struct dentry *dentry, const char *name,
33578 +                       const void *value, size_t size, int flags, int type)
33579 +{
33580 +       return aufs_setxattr(dentry, name, value, size, flags);
33581 +}
33582 +
33583 +static const struct xattr_handler au_xattr_handler = {
33584 +       /* no prefix, no flags */
33585 +       .list   = au_xattr_list,
33586 +       .get    = au_xattr_get,
33587 +       .set    = au_xattr_set
33588 +       /* why no remove? */
33589 +};
33590 +
33591 +static const struct xattr_handler *au_xattr_handlers[] = {
33592 +       &au_xattr_handler
33593 +};
33594 +
33595 +void au_xattr_init(struct super_block *sb)
33596 +{
33597 +       /* sb->s_xattr = au_xattr_handlers; */
33598 +}
33599 +#endif
33600 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
33601 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
33602 +++ linux/fs/aufs/xino.c        2015-09-24 10:47:58.258053165 +0200
33603 @@ -0,0 +1,1297 @@
33604 +/*
33605 + * Copyright (C) 2005-2015 Junjiro R. Okajima
33606 + *
33607 + * This program, aufs is free software; you can redistribute it and/or modify
33608 + * it under the terms of the GNU General Public License as published by
33609 + * the Free Software Foundation; either version 2 of the License, or
33610 + * (at your option) any later version.
33611 + *
33612 + * This program is distributed in the hope that it will be useful,
33613 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33614 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33615 + * GNU General Public License for more details.
33616 + *
33617 + * You should have received a copy of the GNU General Public License
33618 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33619 + */
33620 +
33621 +/*
33622 + * external inode number translation table and bitmap
33623 + */
33624 +
33625 +#include <linux/seq_file.h>
33626 +#include <linux/statfs.h>
33627 +#include "aufs.h"
33628 +
33629 +/* todo: unnecessary to support mmap_sem since kernel-space? */
33630 +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
33631 +                  loff_t *pos)
33632 +{
33633 +       ssize_t err;
33634 +       mm_segment_t oldfs;
33635 +       union {
33636 +               void *k;
33637 +               char __user *u;
33638 +       } buf;
33639 +
33640 +       buf.k = kbuf;
33641 +       oldfs = get_fs();
33642 +       set_fs(KERNEL_DS);
33643 +       do {
33644 +               /* todo: signal_pending? */
33645 +               err = func(file, buf.u, size, pos);
33646 +       } while (err == -EAGAIN || err == -EINTR);
33647 +       set_fs(oldfs);
33648 +
33649 +#if 0 /* reserved for future use */
33650 +       if (err > 0)
33651 +               fsnotify_access(file->f_path.dentry);
33652 +#endif
33653 +
33654 +       return err;
33655 +}
33656 +
33657 +/* ---------------------------------------------------------------------- */
33658 +
33659 +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
33660 +                             size_t size, loff_t *pos)
33661 +{
33662 +       ssize_t err;
33663 +       mm_segment_t oldfs;
33664 +       union {
33665 +               void *k;
33666 +               const char __user *u;
33667 +       } buf;
33668 +
33669 +       buf.k = kbuf;
33670 +       oldfs = get_fs();
33671 +       set_fs(KERNEL_DS);
33672 +       do {
33673 +               /* todo: signal_pending? */
33674 +               err = func(file, buf.u, size, pos);
33675 +       } while (err == -EAGAIN || err == -EINTR);
33676 +       set_fs(oldfs);
33677 +
33678 +#if 0 /* reserved for future use */
33679 +       if (err > 0)
33680 +               fsnotify_modify(file->f_path.dentry);
33681 +#endif
33682 +
33683 +       return err;
33684 +}
33685 +
33686 +struct do_xino_fwrite_args {
33687 +       ssize_t *errp;
33688 +       vfs_writef_t func;
33689 +       struct file *file;
33690 +       void *buf;
33691 +       size_t size;
33692 +       loff_t *pos;
33693 +};
33694 +
33695 +static void call_do_xino_fwrite(void *args)
33696 +{
33697 +       struct do_xino_fwrite_args *a = args;
33698 +       *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
33699 +}
33700 +
33701 +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
33702 +                   size_t size, loff_t *pos)
33703 +{
33704 +       ssize_t err;
33705 +
33706 +       /* todo: signal block and no wkq? */
33707 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
33708 +               lockdep_off();
33709 +               err = do_xino_fwrite(func, file, buf, size, pos);
33710 +               lockdep_on();
33711 +       } else {
33712 +               /*
33713 +                * it breaks RLIMIT_FSIZE and normal user's limit,
33714 +                * users should care about quota and real 'filesystem full.'
33715 +                */
33716 +               int wkq_err;
33717 +               struct do_xino_fwrite_args args = {
33718 +                       .errp   = &err,
33719 +                       .func   = func,
33720 +                       .file   = file,
33721 +                       .buf    = buf,
33722 +                       .size   = size,
33723 +                       .pos    = pos
33724 +               };
33725 +
33726 +               wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
33727 +               if (unlikely(wkq_err))
33728 +                       err = wkq_err;
33729 +       }
33730 +
33731 +       return err;
33732 +}
33733 +
33734 +/* ---------------------------------------------------------------------- */
33735 +
33736 +/*
33737 + * create a new xinofile at the same place/path as @base_file.
33738 + */
33739 +struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
33740 +{
33741 +       struct file *file;
33742 +       struct dentry *base, *parent;
33743 +       struct inode *dir, *delegated;
33744 +       struct qstr *name;
33745 +       struct path path;
33746 +       int err;
33747 +
33748 +       base = base_file->f_path.dentry;
33749 +       parent = base->d_parent; /* dir inode is locked */
33750 +       dir = d_inode(parent);
33751 +       IMustLock(dir);
33752 +
33753 +       file = ERR_PTR(-EINVAL);
33754 +       name = &base->d_name;
33755 +       path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
33756 +       if (IS_ERR(path.dentry)) {
33757 +               file = (void *)path.dentry;
33758 +               pr_err("%pd lookup err %ld\n",
33759 +                      base, PTR_ERR(path.dentry));
33760 +               goto out;
33761 +       }
33762 +
33763 +       /* no need to mnt_want_write() since we call dentry_open() later */
33764 +       err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
33765 +       if (unlikely(err)) {
33766 +               file = ERR_PTR(err);
33767 +               pr_err("%pd create err %d\n", base, err);
33768 +               goto out_dput;
33769 +       }
33770 +
33771 +       path.mnt = base_file->f_path.mnt;
33772 +       file = vfsub_dentry_open(&path,
33773 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
33774 +                                /* | __FMODE_NONOTIFY */);
33775 +       if (IS_ERR(file)) {
33776 +               pr_err("%pd open err %ld\n", base, PTR_ERR(file));
33777 +               goto out_dput;
33778 +       }
33779 +
33780 +       delegated = NULL;
33781 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
33782 +       if (unlikely(err == -EWOULDBLOCK)) {
33783 +               pr_warn("cannot retry for NFSv4 delegation"
33784 +                       " for an internal unlink\n");
33785 +               iput(delegated);
33786 +       }
33787 +       if (unlikely(err)) {
33788 +               pr_err("%pd unlink err %d\n", base, err);
33789 +               goto out_fput;
33790 +       }
33791 +
33792 +       if (copy_src) {
33793 +               /* no one can touch copy_src xino */
33794 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
33795 +               if (unlikely(err)) {
33796 +                       pr_err("%pd copy err %d\n", base, err);
33797 +                       goto out_fput;
33798 +               }
33799 +       }
33800 +       goto out_dput; /* success */
33801 +
33802 +out_fput:
33803 +       fput(file);
33804 +       file = ERR_PTR(err);
33805 +out_dput:
33806 +       dput(path.dentry);
33807 +out:
33808 +       return file;
33809 +}
33810 +
33811 +struct au_xino_lock_dir {
33812 +       struct au_hinode *hdir;
33813 +       struct dentry *parent;
33814 +       struct mutex *mtx;
33815 +};
33816 +
33817 +static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
33818 +                            struct au_xino_lock_dir *ldir)
33819 +{
33820 +       aufs_bindex_t brid, bindex;
33821 +
33822 +       ldir->hdir = NULL;
33823 +       bindex = -1;
33824 +       brid = au_xino_brid(sb);
33825 +       if (brid >= 0)
33826 +               bindex = au_br_index(sb, brid);
33827 +       if (bindex >= 0) {
33828 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
33829 +               au_hn_imtx_lock_nested(ldir->hdir, AuLsc_I_PARENT);
33830 +       } else {
33831 +               ldir->parent = dget_parent(xino->f_path.dentry);
33832 +               ldir->mtx = &d_inode(ldir->parent)->i_mutex;
33833 +               mutex_lock_nested(ldir->mtx, AuLsc_I_PARENT);
33834 +       }
33835 +}
33836 +
33837 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
33838 +{
33839 +       if (ldir->hdir)
33840 +               au_hn_imtx_unlock(ldir->hdir);
33841 +       else {
33842 +               mutex_unlock(ldir->mtx);
33843 +               dput(ldir->parent);
33844 +       }
33845 +}
33846 +
33847 +/* ---------------------------------------------------------------------- */
33848 +
33849 +/* trucate xino files asynchronously */
33850 +
33851 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
33852 +{
33853 +       int err;
33854 +       unsigned long jiffy;
33855 +       blkcnt_t blocks;
33856 +       aufs_bindex_t bi, bend;
33857 +       struct kstatfs *st;
33858 +       struct au_branch *br;
33859 +       struct file *new_xino, *file;
33860 +       struct super_block *h_sb;
33861 +       struct au_xino_lock_dir ldir;
33862 +
33863 +       err = -ENOMEM;
33864 +       st = kzalloc(sizeof(*st), GFP_NOFS);
33865 +       if (unlikely(!st))
33866 +               goto out;
33867 +
33868 +       err = -EINVAL;
33869 +       bend = au_sbend(sb);
33870 +       if (unlikely(bindex < 0 || bend < bindex))
33871 +               goto out_st;
33872 +       br = au_sbr(sb, bindex);
33873 +       file = br->br_xino.xi_file;
33874 +       if (!file)
33875 +               goto out_st;
33876 +
33877 +       err = vfs_statfs(&file->f_path, st);
33878 +       if (unlikely(err))
33879 +               AuErr1("statfs err %d, ignored\n", err);
33880 +       jiffy = jiffies;
33881 +       blocks = file_inode(file)->i_blocks;
33882 +       pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
33883 +               bindex, (u64)blocks, st->f_bfree, st->f_blocks);
33884 +
33885 +       au_xino_lock_dir(sb, file, &ldir);
33886 +       /* mnt_want_write() is unnecessary here */
33887 +       new_xino = au_xino_create2(file, file);
33888 +       au_xino_unlock_dir(&ldir);
33889 +       err = PTR_ERR(new_xino);
33890 +       if (IS_ERR(new_xino)) {
33891 +               pr_err("err %d, ignored\n", err);
33892 +               goto out_st;
33893 +       }
33894 +       err = 0;
33895 +       fput(file);
33896 +       br->br_xino.xi_file = new_xino;
33897 +
33898 +       h_sb = au_br_sb(br);
33899 +       for (bi = 0; bi <= bend; bi++) {
33900 +               if (unlikely(bi == bindex))
33901 +                       continue;
33902 +               br = au_sbr(sb, bi);
33903 +               if (au_br_sb(br) != h_sb)
33904 +                       continue;
33905 +
33906 +               fput(br->br_xino.xi_file);
33907 +               br->br_xino.xi_file = new_xino;
33908 +               get_file(new_xino);
33909 +       }
33910 +
33911 +       err = vfs_statfs(&new_xino->f_path, st);
33912 +       if (!err) {
33913 +               pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
33914 +                       bindex, (u64)file_inode(new_xino)->i_blocks,
33915 +                       st->f_bfree, st->f_blocks);
33916 +               if (file_inode(new_xino)->i_blocks < blocks)
33917 +                       au_sbi(sb)->si_xino_jiffy = jiffy;
33918 +       } else
33919 +               AuErr1("statfs err %d, ignored\n", err);
33920 +
33921 +out_st:
33922 +       kfree(st);
33923 +out:
33924 +       return err;
33925 +}
33926 +
33927 +struct xino_do_trunc_args {
33928 +       struct super_block *sb;
33929 +       struct au_branch *br;
33930 +};
33931 +
33932 +static void xino_do_trunc(void *_args)
33933 +{
33934 +       struct xino_do_trunc_args *args = _args;
33935 +       struct super_block *sb;
33936 +       struct au_branch *br;
33937 +       struct inode *dir;
33938 +       int err;
33939 +       aufs_bindex_t bindex;
33940 +
33941 +       err = 0;
33942 +       sb = args->sb;
33943 +       dir = d_inode(sb->s_root);
33944 +       br = args->br;
33945 +
33946 +       si_noflush_write_lock(sb);
33947 +       ii_read_lock_parent(dir);
33948 +       bindex = au_br_index(sb, br->br_id);
33949 +       err = au_xino_trunc(sb, bindex);
33950 +       ii_read_unlock(dir);
33951 +       if (unlikely(err))
33952 +               pr_warn("err b%d, (%d)\n", bindex, err);
33953 +       atomic_dec(&br->br_xino_running);
33954 +       atomic_dec(&br->br_count);
33955 +       si_write_unlock(sb);
33956 +       au_nwt_done(&au_sbi(sb)->si_nowait);
33957 +       kfree(args);
33958 +}
33959 +
33960 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
33961 +{
33962 +       int err;
33963 +       struct kstatfs st;
33964 +       struct au_sbinfo *sbinfo;
33965 +
33966 +       /* todo: si_xino_expire and the ratio should be customizable */
33967 +       sbinfo = au_sbi(sb);
33968 +       if (time_before(jiffies,
33969 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
33970 +               return 0;
33971 +
33972 +       /* truncation border */
33973 +       err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
33974 +       if (unlikely(err)) {
33975 +               AuErr1("statfs err %d, ignored\n", err);
33976 +               return 0;
33977 +       }
33978 +       if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
33979 +               return 0;
33980 +
33981 +       return 1;
33982 +}
33983 +
33984 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
33985 +{
33986 +       struct xino_do_trunc_args *args;
33987 +       int wkq_err;
33988 +
33989 +       if (!xino_trunc_test(sb, br))
33990 +               return;
33991 +
33992 +       if (atomic_inc_return(&br->br_xino_running) > 1)
33993 +               goto out;
33994 +
33995 +       /* lock and kfree() will be called in trunc_xino() */
33996 +       args = kmalloc(sizeof(*args), GFP_NOFS);
33997 +       if (unlikely(!args)) {
33998 +               AuErr1("no memory\n");
33999 +               goto out_args;
34000 +       }
34001 +
34002 +       atomic_inc(&br->br_count);
34003 +       args->sb = sb;
34004 +       args->br = br;
34005 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
34006 +       if (!wkq_err)
34007 +               return; /* success */
34008 +
34009 +       pr_err("wkq %d\n", wkq_err);
34010 +       atomic_dec(&br->br_count);
34011 +
34012 +out_args:
34013 +       kfree(args);
34014 +out:
34015 +       atomic_dec(&br->br_xino_running);
34016 +}
34017 +
34018 +/* ---------------------------------------------------------------------- */
34019 +
34020 +static int au_xino_do_write(vfs_writef_t write, struct file *file,
34021 +                           ino_t h_ino, ino_t ino)
34022 +{
34023 +       loff_t pos;
34024 +       ssize_t sz;
34025 +
34026 +       pos = h_ino;
34027 +       if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34028 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34029 +               return -EFBIG;
34030 +       }
34031 +       pos *= sizeof(ino);
34032 +       sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34033 +       if (sz == sizeof(ino))
34034 +               return 0; /* success */
34035 +
34036 +       AuIOErr("write failed (%zd)\n", sz);
34037 +       return -EIO;
34038 +}
34039 +
34040 +/*
34041 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
34042 + * at the position of @h_ino.
34043 + * even if @ino is zero, it is written to the xinofile and means no entry.
34044 + * if the size of the xino file on a specific filesystem exceeds the watermark,
34045 + * try truncating it.
34046 + */
34047 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34048 +                 ino_t ino)
34049 +{
34050 +       int err;
34051 +       unsigned int mnt_flags;
34052 +       struct au_branch *br;
34053 +
34054 +       BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34055 +                    || ((loff_t)-1) > 0);
34056 +       SiMustAnyLock(sb);
34057 +
34058 +       mnt_flags = au_mntflags(sb);
34059 +       if (!au_opt_test(mnt_flags, XINO))
34060 +               return 0;
34061 +
34062 +       br = au_sbr(sb, bindex);
34063 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34064 +                              h_ino, ino);
34065 +       if (!err) {
34066 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
34067 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34068 +                       xino_try_trunc(sb, br);
34069 +               return 0; /* success */
34070 +       }
34071 +
34072 +       AuIOErr("write failed (%d)\n", err);
34073 +       return -EIO;
34074 +}
34075 +
34076 +/* ---------------------------------------------------------------------- */
34077 +
34078 +/* aufs inode number bitmap */
34079 +
34080 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34081 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
34082 +{
34083 +       ino_t ino;
34084 +
34085 +       AuDebugOn(bit < 0 || page_bits <= bit);
34086 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34087 +       return ino;
34088 +}
34089 +
34090 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34091 +{
34092 +       AuDebugOn(ino < AUFS_FIRST_INO);
34093 +       ino -= AUFS_FIRST_INO;
34094 +       *pindex = ino / page_bits;
34095 +       *bit = ino % page_bits;
34096 +}
34097 +
34098 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
34099 +{
34100 +       int err;
34101 +       loff_t pos;
34102 +       ssize_t sz;
34103 +       struct au_sbinfo *sbinfo;
34104 +       struct file *xib;
34105 +       unsigned long *p;
34106 +
34107 +       sbinfo = au_sbi(sb);
34108 +       MtxMustLock(&sbinfo->si_xib_mtx);
34109 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34110 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
34111 +
34112 +       if (pindex == sbinfo->si_xib_last_pindex)
34113 +               return 0;
34114 +
34115 +       xib = sbinfo->si_xib;
34116 +       p = sbinfo->si_xib_buf;
34117 +       pos = sbinfo->si_xib_last_pindex;
34118 +       pos *= PAGE_SIZE;
34119 +       sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34120 +       if (unlikely(sz != PAGE_SIZE))
34121 +               goto out;
34122 +
34123 +       pos = pindex;
34124 +       pos *= PAGE_SIZE;
34125 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
34126 +               sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34127 +       else {
34128 +               memset(p, 0, PAGE_SIZE);
34129 +               sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34130 +       }
34131 +       if (sz == PAGE_SIZE) {
34132 +               sbinfo->si_xib_last_pindex = pindex;
34133 +               return 0; /* success */
34134 +       }
34135 +
34136 +out:
34137 +       AuIOErr1("write failed (%zd)\n", sz);
34138 +       err = sz;
34139 +       if (sz >= 0)
34140 +               err = -EIO;
34141 +       return err;
34142 +}
34143 +
34144 +/* ---------------------------------------------------------------------- */
34145 +
34146 +static void au_xib_clear_bit(struct inode *inode)
34147 +{
34148 +       int err, bit;
34149 +       unsigned long pindex;
34150 +       struct super_block *sb;
34151 +       struct au_sbinfo *sbinfo;
34152 +
34153 +       AuDebugOn(inode->i_nlink);
34154 +
34155 +       sb = inode->i_sb;
34156 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
34157 +       AuDebugOn(page_bits <= bit);
34158 +       sbinfo = au_sbi(sb);
34159 +       mutex_lock(&sbinfo->si_xib_mtx);
34160 +       err = xib_pindex(sb, pindex);
34161 +       if (!err) {
34162 +               clear_bit(bit, sbinfo->si_xib_buf);
34163 +               sbinfo->si_xib_next_bit = bit;
34164 +       }
34165 +       mutex_unlock(&sbinfo->si_xib_mtx);
34166 +}
34167 +
34168 +/* for s_op->delete_inode() */
34169 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
34170 +{
34171 +       int err;
34172 +       unsigned int mnt_flags;
34173 +       aufs_bindex_t bindex, bend, bi;
34174 +       unsigned char try_trunc;
34175 +       struct au_iinfo *iinfo;
34176 +       struct super_block *sb;
34177 +       struct au_hinode *hi;
34178 +       struct inode *h_inode;
34179 +       struct au_branch *br;
34180 +       vfs_writef_t xwrite;
34181 +
34182 +       sb = inode->i_sb;
34183 +       mnt_flags = au_mntflags(sb);
34184 +       if (!au_opt_test(mnt_flags, XINO)
34185 +           || inode->i_ino == AUFS_ROOT_INO)
34186 +               return;
34187 +
34188 +       if (unlinked) {
34189 +               au_xigen_inc(inode);
34190 +               au_xib_clear_bit(inode);
34191 +       }
34192 +
34193 +       iinfo = au_ii(inode);
34194 +       if (!iinfo)
34195 +               return;
34196 +
34197 +       bindex = iinfo->ii_bstart;
34198 +       if (bindex < 0)
34199 +               return;
34200 +
34201 +       xwrite = au_sbi(sb)->si_xwrite;
34202 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
34203 +       hi = iinfo->ii_hinode + bindex;
34204 +       bend = iinfo->ii_bend;
34205 +       for (; bindex <= bend; bindex++, hi++) {
34206 +               h_inode = hi->hi_inode;
34207 +               if (!h_inode
34208 +                   || (!unlinked && h_inode->i_nlink))
34209 +                       continue;
34210 +
34211 +               /* inode may not be revalidated */
34212 +               bi = au_br_index(sb, hi->hi_id);
34213 +               if (bi < 0)
34214 +                       continue;
34215 +
34216 +               br = au_sbr(sb, bi);
34217 +               err = au_xino_do_write(xwrite, br->br_xino.xi_file,
34218 +                                      h_inode->i_ino, /*ino*/0);
34219 +               if (!err && try_trunc
34220 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
34221 +                       xino_try_trunc(sb, br);
34222 +       }
34223 +}
34224 +
34225 +/* get an unused inode number from bitmap */
34226 +ino_t au_xino_new_ino(struct super_block *sb)
34227 +{
34228 +       ino_t ino;
34229 +       unsigned long *p, pindex, ul, pend;
34230 +       struct au_sbinfo *sbinfo;
34231 +       struct file *file;
34232 +       int free_bit, err;
34233 +
34234 +       if (!au_opt_test(au_mntflags(sb), XINO))
34235 +               return iunique(sb, AUFS_FIRST_INO);
34236 +
34237 +       sbinfo = au_sbi(sb);
34238 +       mutex_lock(&sbinfo->si_xib_mtx);
34239 +       p = sbinfo->si_xib_buf;
34240 +       free_bit = sbinfo->si_xib_next_bit;
34241 +       if (free_bit < page_bits && !test_bit(free_bit, p))
34242 +               goto out; /* success */
34243 +       free_bit = find_first_zero_bit(p, page_bits);
34244 +       if (free_bit < page_bits)
34245 +               goto out; /* success */
34246 +
34247 +       pindex = sbinfo->si_xib_last_pindex;
34248 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
34249 +               err = xib_pindex(sb, ul);
34250 +               if (unlikely(err))
34251 +                       goto out_err;
34252 +               free_bit = find_first_zero_bit(p, page_bits);
34253 +               if (free_bit < page_bits)
34254 +                       goto out; /* success */
34255 +       }
34256 +
34257 +       file = sbinfo->si_xib;
34258 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
34259 +       for (ul = pindex + 1; ul <= pend; ul++) {
34260 +               err = xib_pindex(sb, ul);
34261 +               if (unlikely(err))
34262 +                       goto out_err;
34263 +               free_bit = find_first_zero_bit(p, page_bits);
34264 +               if (free_bit < page_bits)
34265 +                       goto out; /* success */
34266 +       }
34267 +       BUG();
34268 +
34269 +out:
34270 +       set_bit(free_bit, p);
34271 +       sbinfo->si_xib_next_bit = free_bit + 1;
34272 +       pindex = sbinfo->si_xib_last_pindex;
34273 +       mutex_unlock(&sbinfo->si_xib_mtx);
34274 +       ino = xib_calc_ino(pindex, free_bit);
34275 +       AuDbg("i%lu\n", (unsigned long)ino);
34276 +       return ino;
34277 +out_err:
34278 +       mutex_unlock(&sbinfo->si_xib_mtx);
34279 +       AuDbg("i0\n");
34280 +       return 0;
34281 +}
34282 +
34283 +/*
34284 + * read @ino from xinofile for the specified branch{@sb, @bindex}
34285 + * at the position of @h_ino.
34286 + * if @ino does not exist and @do_new is true, get new one.
34287 + */
34288 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34289 +                ino_t *ino)
34290 +{
34291 +       int err;
34292 +       ssize_t sz;
34293 +       loff_t pos;
34294 +       struct file *file;
34295 +       struct au_sbinfo *sbinfo;
34296 +
34297 +       *ino = 0;
34298 +       if (!au_opt_test(au_mntflags(sb), XINO))
34299 +               return 0; /* no xino */
34300 +
34301 +       err = 0;
34302 +       sbinfo = au_sbi(sb);
34303 +       pos = h_ino;
34304 +       if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
34305 +               AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34306 +               return -EFBIG;
34307 +       }
34308 +       pos *= sizeof(*ino);
34309 +
34310 +       file = au_sbr(sb, bindex)->br_xino.xi_file;
34311 +       if (vfsub_f_size_read(file) < pos + sizeof(*ino))
34312 +               return 0; /* no ino */
34313 +
34314 +       sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
34315 +       if (sz == sizeof(*ino))
34316 +               return 0; /* success */
34317 +
34318 +       err = sz;
34319 +       if (unlikely(sz >= 0)) {
34320 +               err = -EIO;
34321 +               AuIOErr("xino read error (%zd)\n", sz);
34322 +       }
34323 +
34324 +       return err;
34325 +}
34326 +
34327 +/* ---------------------------------------------------------------------- */
34328 +
34329 +/* create and set a new xino file */
34330 +
34331 +struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
34332 +{
34333 +       struct file *file;
34334 +       struct dentry *h_parent, *d;
34335 +       struct inode *h_dir, *inode;
34336 +       int err;
34337 +
34338 +       /*
34339 +        * at mount-time, and the xino file is the default path,
34340 +        * hnotify is disabled so we have no notify events to ignore.
34341 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
34342 +        */
34343 +       file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
34344 +                              /* | __FMODE_NONOTIFY */,
34345 +                              S_IRUGO | S_IWUGO);
34346 +       if (IS_ERR(file)) {
34347 +               if (!silent)
34348 +                       pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
34349 +               return file;
34350 +       }
34351 +
34352 +       /* keep file count */
34353 +       err = 0;
34354 +       inode = file_inode(file);
34355 +       h_parent = dget_parent(file->f_path.dentry);
34356 +       h_dir = d_inode(h_parent);
34357 +       mutex_lock_nested(&h_dir->i_mutex, AuLsc_I_PARENT);
34358 +       /* mnt_want_write() is unnecessary here */
34359 +       /* no delegation since it is just created */
34360 +       if (inode->i_nlink)
34361 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
34362 +                                  /*force*/0);
34363 +       mutex_unlock(&h_dir->i_mutex);
34364 +       dput(h_parent);
34365 +       if (unlikely(err)) {
34366 +               if (!silent)
34367 +                       pr_err("unlink %s(%d)\n", fname, err);
34368 +               goto out;
34369 +       }
34370 +
34371 +       err = -EINVAL;
34372 +       d = file->f_path.dentry;
34373 +       if (unlikely(sb == d->d_sb)) {
34374 +               if (!silent)
34375 +                       pr_err("%s must be outside\n", fname);
34376 +               goto out;
34377 +       }
34378 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
34379 +               if (!silent)
34380 +                       pr_err("xino doesn't support %s(%s)\n",
34381 +                              fname, au_sbtype(d->d_sb));
34382 +               goto out;
34383 +       }
34384 +       return file; /* success */
34385 +
34386 +out:
34387 +       fput(file);
34388 +       file = ERR_PTR(err);
34389 +       return file;
34390 +}
34391 +
34392 +/*
34393 + * find another branch who is on the same filesystem of the specified
34394 + * branch{@btgt}. search until @bend.
34395 + */
34396 +static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
34397 +                       aufs_bindex_t bend)
34398 +{
34399 +       aufs_bindex_t bindex;
34400 +       struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
34401 +
34402 +       for (bindex = 0; bindex < btgt; bindex++)
34403 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34404 +                       return bindex;
34405 +       for (bindex++; bindex <= bend; bindex++)
34406 +               if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34407 +                       return bindex;
34408 +       return -1;
34409 +}
34410 +
34411 +/* ---------------------------------------------------------------------- */
34412 +
34413 +/*
34414 + * initialize the xinofile for the specified branch @br
34415 + * at the place/path where @base_file indicates.
34416 + * test whether another branch is on the same filesystem or not,
34417 + * if @do_test is true.
34418 + */
34419 +int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
34420 +              struct file *base_file, int do_test)
34421 +{
34422 +       int err;
34423 +       ino_t ino;
34424 +       aufs_bindex_t bend, bindex;
34425 +       struct au_branch *shared_br, *b;
34426 +       struct file *file;
34427 +       struct super_block *tgt_sb;
34428 +
34429 +       shared_br = NULL;
34430 +       bend = au_sbend(sb);
34431 +       if (do_test) {
34432 +               tgt_sb = au_br_sb(br);
34433 +               for (bindex = 0; bindex <= bend; bindex++) {
34434 +                       b = au_sbr(sb, bindex);
34435 +                       if (tgt_sb == au_br_sb(b)) {
34436 +                               shared_br = b;
34437 +                               break;
34438 +                       }
34439 +               }
34440 +       }
34441 +
34442 +       if (!shared_br || !shared_br->br_xino.xi_file) {
34443 +               struct au_xino_lock_dir ldir;
34444 +
34445 +               au_xino_lock_dir(sb, base_file, &ldir);
34446 +               /* mnt_want_write() is unnecessary here */
34447 +               file = au_xino_create2(base_file, NULL);
34448 +               au_xino_unlock_dir(&ldir);
34449 +               err = PTR_ERR(file);
34450 +               if (IS_ERR(file))
34451 +                       goto out;
34452 +               br->br_xino.xi_file = file;
34453 +       } else {
34454 +               br->br_xino.xi_file = shared_br->br_xino.xi_file;
34455 +               get_file(br->br_xino.xi_file);
34456 +       }
34457 +
34458 +       ino = AUFS_ROOT_INO;
34459 +       err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34460 +                              h_ino, ino);
34461 +       if (unlikely(err)) {
34462 +               fput(br->br_xino.xi_file);
34463 +               br->br_xino.xi_file = NULL;
34464 +       }
34465 +
34466 +out:
34467 +       return err;
34468 +}
34469 +
34470 +/* ---------------------------------------------------------------------- */
34471 +
34472 +/* trucate a xino bitmap file */
34473 +
34474 +/* todo: slow */
34475 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
34476 +{
34477 +       int err, bit;
34478 +       ssize_t sz;
34479 +       unsigned long pindex;
34480 +       loff_t pos, pend;
34481 +       struct au_sbinfo *sbinfo;
34482 +       vfs_readf_t func;
34483 +       ino_t *ino;
34484 +       unsigned long *p;
34485 +
34486 +       err = 0;
34487 +       sbinfo = au_sbi(sb);
34488 +       MtxMustLock(&sbinfo->si_xib_mtx);
34489 +       p = sbinfo->si_xib_buf;
34490 +       func = sbinfo->si_xread;
34491 +       pend = vfsub_f_size_read(file);
34492 +       pos = 0;
34493 +       while (pos < pend) {
34494 +               sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
34495 +               err = sz;
34496 +               if (unlikely(sz <= 0))
34497 +                       goto out;
34498 +
34499 +               err = 0;
34500 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
34501 +                       if (unlikely(*ino < AUFS_FIRST_INO))
34502 +                               continue;
34503 +
34504 +                       xib_calc_bit(*ino, &pindex, &bit);
34505 +                       AuDebugOn(page_bits <= bit);
34506 +                       err = xib_pindex(sb, pindex);
34507 +                       if (!err)
34508 +                               set_bit(bit, p);
34509 +                       else
34510 +                               goto out;
34511 +               }
34512 +       }
34513 +
34514 +out:
34515 +       return err;
34516 +}
34517 +
34518 +static int xib_restore(struct super_block *sb)
34519 +{
34520 +       int err;
34521 +       aufs_bindex_t bindex, bend;
34522 +       void *page;
34523 +
34524 +       err = -ENOMEM;
34525 +       page = (void *)__get_free_page(GFP_NOFS);
34526 +       if (unlikely(!page))
34527 +               goto out;
34528 +
34529 +       err = 0;
34530 +       bend = au_sbend(sb);
34531 +       for (bindex = 0; !err && bindex <= bend; bindex++)
34532 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
34533 +                       err = do_xib_restore
34534 +                               (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
34535 +               else
34536 +                       AuDbg("b%d\n", bindex);
34537 +       free_page((unsigned long)page);
34538 +
34539 +out:
34540 +       return err;
34541 +}
34542 +
34543 +int au_xib_trunc(struct super_block *sb)
34544 +{
34545 +       int err;
34546 +       ssize_t sz;
34547 +       loff_t pos;
34548 +       struct au_xino_lock_dir ldir;
34549 +       struct au_sbinfo *sbinfo;
34550 +       unsigned long *p;
34551 +       struct file *file;
34552 +
34553 +       SiMustWriteLock(sb);
34554 +
34555 +       err = 0;
34556 +       sbinfo = au_sbi(sb);
34557 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
34558 +               goto out;
34559 +
34560 +       file = sbinfo->si_xib;
34561 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
34562 +               goto out;
34563 +
34564 +       au_xino_lock_dir(sb, file, &ldir);
34565 +       /* mnt_want_write() is unnecessary here */
34566 +       file = au_xino_create2(sbinfo->si_xib, NULL);
34567 +       au_xino_unlock_dir(&ldir);
34568 +       err = PTR_ERR(file);
34569 +       if (IS_ERR(file))
34570 +               goto out;
34571 +       fput(sbinfo->si_xib);
34572 +       sbinfo->si_xib = file;
34573 +
34574 +       p = sbinfo->si_xib_buf;
34575 +       memset(p, 0, PAGE_SIZE);
34576 +       pos = 0;
34577 +       sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
34578 +       if (unlikely(sz != PAGE_SIZE)) {
34579 +               err = sz;
34580 +               AuIOErr("err %d\n", err);
34581 +               if (sz >= 0)
34582 +                       err = -EIO;
34583 +               goto out;
34584 +       }
34585 +
34586 +       mutex_lock(&sbinfo->si_xib_mtx);
34587 +       /* mnt_want_write() is unnecessary here */
34588 +       err = xib_restore(sb);
34589 +       mutex_unlock(&sbinfo->si_xib_mtx);
34590 +
34591 +out:
34592 +       return err;
34593 +}
34594 +
34595 +/* ---------------------------------------------------------------------- */
34596 +
34597 +/*
34598 + * xino mount option handlers
34599 + */
34600 +
34601 +/* xino bitmap */
34602 +static void xino_clear_xib(struct super_block *sb)
34603 +{
34604 +       struct au_sbinfo *sbinfo;
34605 +
34606 +       SiMustWriteLock(sb);
34607 +
34608 +       sbinfo = au_sbi(sb);
34609 +       sbinfo->si_xread = NULL;
34610 +       sbinfo->si_xwrite = NULL;
34611 +       if (sbinfo->si_xib)
34612 +               fput(sbinfo->si_xib);
34613 +       sbinfo->si_xib = NULL;
34614 +       free_page((unsigned long)sbinfo->si_xib_buf);
34615 +       sbinfo->si_xib_buf = NULL;
34616 +}
34617 +
34618 +static int au_xino_set_xib(struct super_block *sb, struct file *base)
34619 +{
34620 +       int err;
34621 +       loff_t pos;
34622 +       struct au_sbinfo *sbinfo;
34623 +       struct file *file;
34624 +
34625 +       SiMustWriteLock(sb);
34626 +
34627 +       sbinfo = au_sbi(sb);
34628 +       file = au_xino_create2(base, sbinfo->si_xib);
34629 +       err = PTR_ERR(file);
34630 +       if (IS_ERR(file))
34631 +               goto out;
34632 +       if (sbinfo->si_xib)
34633 +               fput(sbinfo->si_xib);
34634 +       sbinfo->si_xib = file;
34635 +       sbinfo->si_xread = vfs_readf(file);
34636 +       sbinfo->si_xwrite = vfs_writef(file);
34637 +
34638 +       err = -ENOMEM;
34639 +       if (!sbinfo->si_xib_buf)
34640 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
34641 +       if (unlikely(!sbinfo->si_xib_buf))
34642 +               goto out_unset;
34643 +
34644 +       sbinfo->si_xib_last_pindex = 0;
34645 +       sbinfo->si_xib_next_bit = 0;
34646 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
34647 +               pos = 0;
34648 +               err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
34649 +                                 PAGE_SIZE, &pos);
34650 +               if (unlikely(err != PAGE_SIZE))
34651 +                       goto out_free;
34652 +       }
34653 +       err = 0;
34654 +       goto out; /* success */
34655 +
34656 +out_free:
34657 +       free_page((unsigned long)sbinfo->si_xib_buf);
34658 +       sbinfo->si_xib_buf = NULL;
34659 +       if (err >= 0)
34660 +               err = -EIO;
34661 +out_unset:
34662 +       fput(sbinfo->si_xib);
34663 +       sbinfo->si_xib = NULL;
34664 +       sbinfo->si_xread = NULL;
34665 +       sbinfo->si_xwrite = NULL;
34666 +out:
34667 +       return err;
34668 +}
34669 +
34670 +/* xino for each branch */
34671 +static void xino_clear_br(struct super_block *sb)
34672 +{
34673 +       aufs_bindex_t bindex, bend;
34674 +       struct au_branch *br;
34675 +
34676 +       bend = au_sbend(sb);
34677 +       for (bindex = 0; bindex <= bend; bindex++) {
34678 +               br = au_sbr(sb, bindex);
34679 +               if (!br || !br->br_xino.xi_file)
34680 +                       continue;
34681 +
34682 +               fput(br->br_xino.xi_file);
34683 +               br->br_xino.xi_file = NULL;
34684 +       }
34685 +}
34686 +
34687 +static int au_xino_set_br(struct super_block *sb, struct file *base)
34688 +{
34689 +       int err;
34690 +       ino_t ino;
34691 +       aufs_bindex_t bindex, bend, bshared;
34692 +       struct {
34693 +               struct file *old, *new;
34694 +       } *fpair, *p;
34695 +       struct au_branch *br;
34696 +       struct inode *inode;
34697 +       vfs_writef_t writef;
34698 +
34699 +       SiMustWriteLock(sb);
34700 +
34701 +       err = -ENOMEM;
34702 +       bend = au_sbend(sb);
34703 +       fpair = kcalloc(bend + 1, sizeof(*fpair), GFP_NOFS);
34704 +       if (unlikely(!fpair))
34705 +               goto out;
34706 +
34707 +       inode = d_inode(sb->s_root);
34708 +       ino = AUFS_ROOT_INO;
34709 +       writef = au_sbi(sb)->si_xwrite;
34710 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34711 +               br = au_sbr(sb, bindex);
34712 +               bshared = is_sb_shared(sb, bindex, bindex - 1);
34713 +               if (bshared >= 0) {
34714 +                       /* shared xino */
34715 +                       *p = fpair[bshared];
34716 +                       get_file(p->new);
34717 +               }
34718 +
34719 +               if (!p->new) {
34720 +                       /* new xino */
34721 +                       p->old = br->br_xino.xi_file;
34722 +                       p->new = au_xino_create2(base, br->br_xino.xi_file);
34723 +                       err = PTR_ERR(p->new);
34724 +                       if (IS_ERR(p->new)) {
34725 +                               p->new = NULL;
34726 +                               goto out_pair;
34727 +                       }
34728 +               }
34729 +
34730 +               err = au_xino_do_write(writef, p->new,
34731 +                                      au_h_iptr(inode, bindex)->i_ino, ino);
34732 +               if (unlikely(err))
34733 +                       goto out_pair;
34734 +       }
34735 +
34736 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++) {
34737 +               br = au_sbr(sb, bindex);
34738 +               if (br->br_xino.xi_file)
34739 +                       fput(br->br_xino.xi_file);
34740 +               get_file(p->new);
34741 +               br->br_xino.xi_file = p->new;
34742 +       }
34743 +
34744 +out_pair:
34745 +       for (bindex = 0, p = fpair; bindex <= bend; bindex++, p++)
34746 +               if (p->new)
34747 +                       fput(p->new);
34748 +               else
34749 +                       break;
34750 +       kfree(fpair);
34751 +out:
34752 +       return err;
34753 +}
34754 +
34755 +void au_xino_clr(struct super_block *sb)
34756 +{
34757 +       struct au_sbinfo *sbinfo;
34758 +
34759 +       au_xigen_clr(sb);
34760 +       xino_clear_xib(sb);
34761 +       xino_clear_br(sb);
34762 +       sbinfo = au_sbi(sb);
34763 +       /* lvalue, do not call au_mntflags() */
34764 +       au_opt_clr(sbinfo->si_mntflags, XINO);
34765 +}
34766 +
34767 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
34768 +{
34769 +       int err, skip;
34770 +       struct dentry *parent, *cur_parent;
34771 +       struct qstr *dname, *cur_name;
34772 +       struct file *cur_xino;
34773 +       struct inode *dir;
34774 +       struct au_sbinfo *sbinfo;
34775 +
34776 +       SiMustWriteLock(sb);
34777 +
34778 +       err = 0;
34779 +       sbinfo = au_sbi(sb);
34780 +       parent = dget_parent(xino->file->f_path.dentry);
34781 +       if (remount) {
34782 +               skip = 0;
34783 +               dname = &xino->file->f_path.dentry->d_name;
34784 +               cur_xino = sbinfo->si_xib;
34785 +               if (cur_xino) {
34786 +                       cur_parent = dget_parent(cur_xino->f_path.dentry);
34787 +                       cur_name = &cur_xino->f_path.dentry->d_name;
34788 +                       skip = (cur_parent == parent
34789 +                               && au_qstreq(dname, cur_name));
34790 +                       dput(cur_parent);
34791 +               }
34792 +               if (skip)
34793 +                       goto out;
34794 +       }
34795 +
34796 +       au_opt_set(sbinfo->si_mntflags, XINO);
34797 +       dir = d_inode(parent);
34798 +       mutex_lock_nested(&dir->i_mutex, AuLsc_I_PARENT);
34799 +       /* mnt_want_write() is unnecessary here */
34800 +       err = au_xino_set_xib(sb, xino->file);
34801 +       if (!err)
34802 +               err = au_xigen_set(sb, xino->file);
34803 +       if (!err)
34804 +               err = au_xino_set_br(sb, xino->file);
34805 +       mutex_unlock(&dir->i_mutex);
34806 +       if (!err)
34807 +               goto out; /* success */
34808 +
34809 +       /* reset all */
34810 +       AuIOErr("failed creating xino(%d).\n", err);
34811 +       au_xigen_clr(sb);
34812 +       xino_clear_xib(sb);
34813 +
34814 +out:
34815 +       dput(parent);
34816 +       return err;
34817 +}
34818 +
34819 +/* ---------------------------------------------------------------------- */
34820 +
34821 +/*
34822 + * create a xinofile at the default place/path.
34823 + */
34824 +struct file *au_xino_def(struct super_block *sb)
34825 +{
34826 +       struct file *file;
34827 +       char *page, *p;
34828 +       struct au_branch *br;
34829 +       struct super_block *h_sb;
34830 +       struct path path;
34831 +       aufs_bindex_t bend, bindex, bwr;
34832 +
34833 +       br = NULL;
34834 +       bend = au_sbend(sb);
34835 +       bwr = -1;
34836 +       for (bindex = 0; bindex <= bend; bindex++) {
34837 +               br = au_sbr(sb, bindex);
34838 +               if (au_br_writable(br->br_perm)
34839 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
34840 +                       bwr = bindex;
34841 +                       break;
34842 +               }
34843 +       }
34844 +
34845 +       if (bwr >= 0) {
34846 +               file = ERR_PTR(-ENOMEM);
34847 +               page = (void *)__get_free_page(GFP_NOFS);
34848 +               if (unlikely(!page))
34849 +                       goto out;
34850 +               path.mnt = au_br_mnt(br);
34851 +               path.dentry = au_h_dptr(sb->s_root, bwr);
34852 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
34853 +               file = (void *)p;
34854 +               if (!IS_ERR(p)) {
34855 +                       strcat(p, "/" AUFS_XINO_FNAME);
34856 +                       AuDbg("%s\n", p);
34857 +                       file = au_xino_create(sb, p, /*silent*/0);
34858 +                       if (!IS_ERR(file))
34859 +                               au_xino_brid_set(sb, br->br_id);
34860 +               }
34861 +               free_page((unsigned long)page);
34862 +       } else {
34863 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
34864 +               if (IS_ERR(file))
34865 +                       goto out;
34866 +               h_sb = file->f_path.dentry->d_sb;
34867 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
34868 +                       pr_err("xino doesn't support %s(%s)\n",
34869 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
34870 +                       fput(file);
34871 +                       file = ERR_PTR(-EINVAL);
34872 +               }
34873 +               if (!IS_ERR(file))
34874 +                       au_xino_brid_set(sb, -1);
34875 +       }
34876 +
34877 +out:
34878 +       return file;
34879 +}
34880 +
34881 +/* ---------------------------------------------------------------------- */
34882 +
34883 +int au_xino_path(struct seq_file *seq, struct file *file)
34884 +{
34885 +       int err;
34886 +
34887 +       err = au_seq_path(seq, &file->f_path);
34888 +       if (unlikely(err < 0))
34889 +               goto out;
34890 +
34891 +       err = 0;
34892 +#define Deleted "\\040(deleted)"
34893 +       seq->count -= sizeof(Deleted) - 1;
34894 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
34895 +                        sizeof(Deleted) - 1));
34896 +#undef Deleted
34897 +
34898 +out:
34899 +       return err;
34900 +}
34901 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
34902 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
34903 +++ linux/include/uapi/linux/aufs_type.h        2015-09-24 10:47:58.258053165 +0200
34904 @@ -0,0 +1,419 @@
34905 +/*
34906 + * Copyright (C) 2005-2015 Junjiro R. Okajima
34907 + *
34908 + * This program, aufs is free software; you can redistribute it and/or modify
34909 + * it under the terms of the GNU General Public License as published by
34910 + * the Free Software Foundation; either version 2 of the License, or
34911 + * (at your option) any later version.
34912 + *
34913 + * This program is distributed in the hope that it will be useful,
34914 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34915 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34916 + * GNU General Public License for more details.
34917 + *
34918 + * You should have received a copy of the GNU General Public License
34919 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34920 + */
34921 +
34922 +#ifndef __AUFS_TYPE_H__
34923 +#define __AUFS_TYPE_H__
34924 +
34925 +#define AUFS_NAME      "aufs"
34926 +
34927 +#ifdef __KERNEL__
34928 +/*
34929 + * define it before including all other headers.
34930 + * sched.h may use pr_* macros before defining "current", so define the
34931 + * no-current version first, and re-define later.
34932 + */
34933 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
34934 +#include <linux/sched.h>
34935 +#undef pr_fmt
34936 +#define pr_fmt(fmt) \
34937 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
34938 +               (int)sizeof(current->comm), current->comm, current->pid
34939 +#else
34940 +#include <stdint.h>
34941 +#include <sys/types.h>
34942 +#endif /* __KERNEL__ */
34943 +
34944 +#include <linux/limits.h>
34945 +
34946 +#define AUFS_VERSION   "4.x-rcN-20150921"
34947 +
34948 +/* todo? move this to linux-2.6.19/include/magic.h */
34949 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
34950 +
34951 +/* ---------------------------------------------------------------------- */
34952 +
34953 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
34954 +typedef int8_t aufs_bindex_t;
34955 +#define AUFS_BRANCH_MAX 127
34956 +#else
34957 +typedef int16_t aufs_bindex_t;
34958 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
34959 +#define AUFS_BRANCH_MAX 511
34960 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
34961 +#define AUFS_BRANCH_MAX 1023
34962 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
34963 +#define AUFS_BRANCH_MAX 32767
34964 +#endif
34965 +#endif
34966 +
34967 +#ifdef __KERNEL__
34968 +#ifndef AUFS_BRANCH_MAX
34969 +#error unknown CONFIG_AUFS_BRANCH_MAX value
34970 +#endif
34971 +#endif /* __KERNEL__ */
34972 +
34973 +/* ---------------------------------------------------------------------- */
34974 +
34975 +#define AUFS_FSTYPE            AUFS_NAME
34976 +
34977 +#define AUFS_ROOT_INO          2
34978 +#define AUFS_FIRST_INO         11
34979 +
34980 +#define AUFS_WH_PFX            ".wh."
34981 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
34982 +#define AUFS_WH_TMP_LEN                4
34983 +/* a limit for rmdir/rename a dir and copyup */
34984 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
34985 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
34986 +                               - 1                     /* dot */\
34987 +                               - AUFS_WH_TMP_LEN)      /* hex */
34988 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
34989 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
34990 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
34991 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
34992 +#define AUFS_DIRWH_DEF         3
34993 +#define AUFS_RDCACHE_DEF       10 /* seconds */
34994 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
34995 +#define AUFS_RDBLK_DEF         512 /* bytes */
34996 +#define AUFS_RDHASH_DEF                32
34997 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
34998 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
34999 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
35000 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
35001 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
35002 +
35003 +/* pseudo-link maintenace under /proc */
35004 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
35005 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
35006 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35007 +
35008 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
35009 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
35010 +
35011 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
35012 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
35013 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
35014 +
35015 +/* doubly whiteouted */
35016 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
35017 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
35018 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
35019 +
35020 +/* branch permissions and attributes */
35021 +#define AUFS_BRPERM_RW         "rw"
35022 +#define AUFS_BRPERM_RO         "ro"
35023 +#define AUFS_BRPERM_RR         "rr"
35024 +#define AUFS_BRATTR_COO_REG    "coo_reg"
35025 +#define AUFS_BRATTR_COO_ALL    "coo_all"
35026 +#define AUFS_BRATTR_FHSM       "fhsm"
35027 +#define AUFS_BRATTR_UNPIN      "unpin"
35028 +#define AUFS_BRATTR_ICEX       "icex"
35029 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
35030 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
35031 +#define AUFS_BRATTR_ICEX_TR    "icextr"
35032 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
35033 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
35034 +#define AUFS_BRRATTR_WH                "wh"
35035 +#define AUFS_BRWATTR_NLWH      "nolwh"
35036 +#define AUFS_BRWATTR_MOO       "moo"
35037 +
35038 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
35039 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
35040 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
35041 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35042 +
35043 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
35044 +#define AuBrAttr_COO_ALL       (1 << 4)
35045 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35046 +
35047 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
35048 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
35049 +                                                  branch. meaningless since
35050 +                                                  linux-3.18-rc1 */
35051 +
35052 +/* ignore error in copying XATTR */
35053 +#define AuBrAttr_ICEX_SEC      (1 << 7)
35054 +#define AuBrAttr_ICEX_SYS      (1 << 8)
35055 +#define AuBrAttr_ICEX_TR       (1 << 9)
35056 +#define AuBrAttr_ICEX_USR      (1 << 10)
35057 +#define AuBrAttr_ICEX_OTH      (1 << 11)
35058 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
35059 +                                | AuBrAttr_ICEX_SYS    \
35060 +                                | AuBrAttr_ICEX_TR     \
35061 +                                | AuBrAttr_ICEX_USR    \
35062 +                                | AuBrAttr_ICEX_OTH)
35063 +
35064 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
35065 +#define AuBrRAttr_Mask         AuBrRAttr_WH
35066 +
35067 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
35068 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
35069 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35070 +
35071 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35072 +
35073 +/* #warning test userspace */
35074 +#ifdef __KERNEL__
35075 +#ifndef CONFIG_AUFS_FHSM
35076 +#undef AuBrAttr_FHSM
35077 +#define AuBrAttr_FHSM          0
35078 +#endif
35079 +#ifndef CONFIG_AUFS_XATTR
35080 +#undef AuBrAttr_ICEX
35081 +#define AuBrAttr_ICEX          0
35082 +#undef AuBrAttr_ICEX_SEC
35083 +#define AuBrAttr_ICEX_SEC      0
35084 +#undef AuBrAttr_ICEX_SYS
35085 +#define AuBrAttr_ICEX_SYS      0
35086 +#undef AuBrAttr_ICEX_TR
35087 +#define AuBrAttr_ICEX_TR       0
35088 +#undef AuBrAttr_ICEX_USR
35089 +#define AuBrAttr_ICEX_USR      0
35090 +#undef AuBrAttr_ICEX_OTH
35091 +#define AuBrAttr_ICEX_OTH      0
35092 +#endif
35093 +#endif
35094 +
35095 +/* the longest combination */
35096 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35097 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
35098 +                              "+" AUFS_BRATTR_COO_REG          \
35099 +                              "+" AUFS_BRATTR_FHSM             \
35100 +                              "+" AUFS_BRATTR_UNPIN            \
35101 +                              "+" AUFS_BRATTR_ICEX_SEC         \
35102 +                              "+" AUFS_BRATTR_ICEX_SYS         \
35103 +                              "+" AUFS_BRATTR_ICEX_USR         \
35104 +                              "+" AUFS_BRATTR_ICEX_OTH         \
35105 +                              "+" AUFS_BRWATTR_NLWH)
35106 +
35107 +typedef struct {
35108 +       char a[AuBrPermStrSz];
35109 +} au_br_perm_str_t;
35110 +
35111 +static inline int au_br_writable(int brperm)
35112 +{
35113 +       return brperm & AuBrPerm_RW;
35114 +}
35115 +
35116 +static inline int au_br_whable(int brperm)
35117 +{
35118 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35119 +}
35120 +
35121 +static inline int au_br_wh_linkable(int brperm)
35122 +{
35123 +       return !(brperm & AuBrWAttr_NoLinkWH);
35124 +}
35125 +
35126 +static inline int au_br_cmoo(int brperm)
35127 +{
35128 +       return brperm & AuBrAttr_CMOO_Mask;
35129 +}
35130 +
35131 +static inline int au_br_fhsm(int brperm)
35132 +{
35133 +       return brperm & AuBrAttr_FHSM;
35134 +}
35135 +
35136 +/* ---------------------------------------------------------------------- */
35137 +
35138 +/* ioctl */
35139 +enum {
35140 +       /* readdir in userspace */
35141 +       AuCtl_RDU,
35142 +       AuCtl_RDU_INO,
35143 +
35144 +       AuCtl_WBR_FD,   /* pathconf wrapper */
35145 +       AuCtl_IBUSY,    /* busy inode */
35146 +       AuCtl_MVDOWN,   /* move-down */
35147 +       AuCtl_BR,       /* info about branches */
35148 +       AuCtl_FHSM_FD   /* connection for fhsm */
35149 +};
35150 +
35151 +/* borrowed from linux/include/linux/kernel.h */
35152 +#ifndef ALIGN
35153 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
35154 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
35155 +#endif
35156 +
35157 +/* borrowed from linux/include/linux/compiler-gcc3.h */
35158 +#ifndef __aligned
35159 +#define __aligned(x)                   __attribute__((aligned(x)))
35160 +#endif
35161 +
35162 +#ifdef __KERNEL__
35163 +#ifndef __packed
35164 +#define __packed                       __attribute__((packed))
35165 +#endif
35166 +#endif
35167 +
35168 +struct au_rdu_cookie {
35169 +       uint64_t        h_pos;
35170 +       int16_t         bindex;
35171 +       uint8_t         flags;
35172 +       uint8_t         pad;
35173 +       uint32_t        generation;
35174 +} __aligned(8);
35175 +
35176 +struct au_rdu_ent {
35177 +       uint64_t        ino;
35178 +       int16_t         bindex;
35179 +       uint8_t         type;
35180 +       uint8_t         nlen;
35181 +       uint8_t         wh;
35182 +       char            name[0];
35183 +} __aligned(8);
35184 +
35185 +static inline int au_rdu_len(int nlen)
35186 +{
35187 +       /* include the terminating NULL */
35188 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
35189 +                    sizeof(uint64_t));
35190 +}
35191 +
35192 +union au_rdu_ent_ul {
35193 +       struct au_rdu_ent __user        *e;
35194 +       uint64_t                        ul;
35195 +};
35196 +
35197 +enum {
35198 +       AufsCtlRduV_SZ,
35199 +       AufsCtlRduV_End
35200 +};
35201 +
35202 +struct aufs_rdu {
35203 +       /* input */
35204 +       union {
35205 +               uint64_t        sz;     /* AuCtl_RDU */
35206 +               uint64_t        nent;   /* AuCtl_RDU_INO */
35207 +       };
35208 +       union au_rdu_ent_ul     ent;
35209 +       uint16_t                verify[AufsCtlRduV_End];
35210 +
35211 +       /* input/output */
35212 +       uint32_t                blk;
35213 +
35214 +       /* output */
35215 +       union au_rdu_ent_ul     tail;
35216 +       /* number of entries which were added in a single call */
35217 +       uint64_t                rent;
35218 +       uint8_t                 full;
35219 +       uint8_t                 shwh;
35220 +
35221 +       struct au_rdu_cookie    cookie;
35222 +} __aligned(8);
35223 +
35224 +/* ---------------------------------------------------------------------- */
35225 +
35226 +struct aufs_wbr_fd {
35227 +       uint32_t        oflags;
35228 +       int16_t         brid;
35229 +} __aligned(8);
35230 +
35231 +/* ---------------------------------------------------------------------- */
35232 +
35233 +struct aufs_ibusy {
35234 +       uint64_t        ino, h_ino;
35235 +       int16_t         bindex;
35236 +} __aligned(8);
35237 +
35238 +/* ---------------------------------------------------------------------- */
35239 +
35240 +/* error code for move-down */
35241 +/* the actual message strings are implemented in aufs-util.git */
35242 +enum {
35243 +       EAU_MVDOWN_OPAQUE = 1,
35244 +       EAU_MVDOWN_WHITEOUT,
35245 +       EAU_MVDOWN_UPPER,
35246 +       EAU_MVDOWN_BOTTOM,
35247 +       EAU_MVDOWN_NOUPPER,
35248 +       EAU_MVDOWN_NOLOWERBR,
35249 +       EAU_Last
35250 +};
35251 +
35252 +/* flags for move-down */
35253 +#define AUFS_MVDOWN_DMSG       1
35254 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
35255 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
35256 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
35257 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
35258 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
35259 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
35260 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
35261 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
35262 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
35263 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
35264 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
35265 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
35266 +
35267 +/* index for move-down */
35268 +enum {
35269 +       AUFS_MVDOWN_UPPER,
35270 +       AUFS_MVDOWN_LOWER,
35271 +       AUFS_MVDOWN_NARRAY
35272 +};
35273 +
35274 +/*
35275 + * additional info of move-down
35276 + * number of free blocks and inodes.
35277 + * subset of struct kstatfs, but smaller and always 64bit.
35278 + */
35279 +struct aufs_stfs {
35280 +       uint64_t        f_blocks;
35281 +       uint64_t        f_bavail;
35282 +       uint64_t        f_files;
35283 +       uint64_t        f_ffree;
35284 +};
35285 +
35286 +struct aufs_stbr {
35287 +       int16_t                 brid;   /* optional input */
35288 +       int16_t                 bindex; /* output */
35289 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
35290 +} __aligned(8);
35291 +
35292 +struct aufs_mvdown {
35293 +       uint32_t                flags;                  /* input/output */
35294 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
35295 +       int8_t                  au_errno;               /* output */
35296 +} __aligned(8);
35297 +
35298 +/* ---------------------------------------------------------------------- */
35299 +
35300 +union aufs_brinfo {
35301 +       /* PATH_MAX may differ between kernel-space and user-space */
35302 +       char    _spacer[4096];
35303 +       struct {
35304 +               int16_t id;
35305 +               int     perm;
35306 +               char    path[0];
35307 +       };
35308 +} __aligned(8);
35309 +
35310 +/* ---------------------------------------------------------------------- */
35311 +
35312 +#define AuCtlType              'A'
35313 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
35314 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
35315 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
35316 +                                    struct aufs_wbr_fd)
35317 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
35318 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
35319 +                                     struct aufs_mvdown)
35320 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
35321 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
35322 +
35323 +#endif /* __AUFS_TYPE_H__ */
35324 aufs4.x-rcN loopback patch
35325
35326 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
35327 index 23103ad..5c3263f 100644
35328 --- a/drivers/block/loop.c
35329 +++ b/drivers/block/loop.c
35330 @@ -417,7 +417,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
35331  }
35332  
35333  struct switch_request {
35334 -       struct file *file;
35335 +       struct file *file, *virt_file;
35336         struct completion wait;
35337  };
35338  
35339 @@ -437,6 +437,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
35340         mapping = file->f_mapping;
35341         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
35342         lo->lo_backing_file = file;
35343 +       lo->lo_backing_virt_file = p->virt_file;
35344         lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
35345                 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
35346         lo->old_gfp_mask = mapping_gfp_mask(mapping);
35347 @@ -448,11 +449,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
35348   * First it needs to flush existing IO, it does this by sending a magic
35349   * BIO down the pipe. The completion of this BIO does the actual switch.
35350   */
35351 -static int loop_switch(struct loop_device *lo, struct file *file)
35352 +static int loop_switch(struct loop_device *lo, struct file *file,
35353 +                      struct file *virt_file)
35354  {
35355         struct switch_request w;
35356  
35357         w.file = file;
35358 +       w.virt_file = virt_file;
35359  
35360         /* freeze queue and wait for completion of scheduled requests */
35361         blk_mq_freeze_queue(lo->lo_queue);
35362 @@ -471,7 +474,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
35363   */
35364  static int loop_flush(struct loop_device *lo)
35365  {
35366 -       return loop_switch(lo, NULL);
35367 +       return loop_switch(lo, NULL, NULL);
35368 +}
35369 +
35370 +static struct file *loop_real_file(struct file *file)
35371 +{
35372 +       struct file *f = NULL;
35373 +
35374 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
35375 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
35376 +       return f;
35377  }
35378  
35379  static void loop_reread_partitions(struct loop_device *lo,
35380 @@ -508,6 +520,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35381                           unsigned int arg)
35382  {
35383         struct file     *file, *old_file;
35384 +       struct file     *f, *virt_file = NULL, *old_virt_file;
35385         struct inode    *inode;
35386         int             error;
35387  
35388 @@ -524,9 +537,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35389         file = fget(arg);
35390         if (!file)
35391                 goto out;
35392 +       f = loop_real_file(file);
35393 +       if (f) {
35394 +               virt_file = file;
35395 +               file = f;
35396 +               get_file(file);
35397 +       }
35398  
35399         inode = file->f_mapping->host;
35400         old_file = lo->lo_backing_file;
35401 +       old_virt_file = lo->lo_backing_virt_file;
35402  
35403         error = -EINVAL;
35404  
35405 @@ -538,17 +558,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
35406                 goto out_putf;
35407  
35408         /* and ... switch */
35409 -       error = loop_switch(lo, file);
35410 +       error = loop_switch(lo, file, virt_file);
35411         if (error)
35412                 goto out_putf;
35413  
35414         fput(old_file);
35415 +       if (old_virt_file)
35416 +               fput(old_virt_file);
35417         if (lo->lo_flags & LO_FLAGS_PARTSCAN)
35418                 loop_reread_partitions(lo, bdev);
35419         return 0;
35420  
35421   out_putf:
35422         fput(file);
35423 +       if (virt_file)
35424 +               fput(virt_file);
35425   out:
35426         return error;
35427  }
35428 @@ -709,7 +733,7 @@ static void loop_config_discard(struct loop_device *lo)
35429  static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35430                        struct block_device *bdev, unsigned int arg)
35431  {
35432 -       struct file     *file, *f;
35433 +       struct file     *file, *f, *virt_file = NULL;
35434         struct inode    *inode;
35435         struct address_space *mapping;
35436         unsigned lo_blocksize;
35437 @@ -724,6 +748,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35438         file = fget(arg);
35439         if (!file)
35440                 goto out;
35441 +       f = loop_real_file(file);
35442 +       if (f) {
35443 +               virt_file = file;
35444 +               file = f;
35445 +               get_file(file);
35446 +       }
35447  
35448         error = -EBUSY;
35449         if (lo->lo_state != Lo_unbound)
35450 @@ -778,6 +808,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35451         lo->lo_device = bdev;
35452         lo->lo_flags = lo_flags;
35453         lo->lo_backing_file = file;
35454 +       lo->lo_backing_virt_file = virt_file;
35455         lo->transfer = NULL;
35456         lo->ioctl = NULL;
35457         lo->lo_sizelimit = 0;
35458 @@ -809,6 +840,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35459  
35460   out_putf:
35461         fput(file);
35462 +       if (virt_file)
35463 +               fput(virt_file);
35464   out:
35465         /* This is safe: open() is still holding a reference. */
35466         module_put(THIS_MODULE);
35467 @@ -855,6 +888,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
35468  static int loop_clr_fd(struct loop_device *lo)
35469  {
35470         struct file *filp = lo->lo_backing_file;
35471 +       struct file *virt_filp = lo->lo_backing_virt_file;
35472         gfp_t gfp = lo->old_gfp_mask;
35473         struct block_device *bdev = lo->lo_device;
35474  
35475 @@ -886,6 +920,7 @@ static int loop_clr_fd(struct loop_device *lo)
35476         spin_lock_irq(&lo->lo_lock);
35477         lo->lo_state = Lo_rundown;
35478         lo->lo_backing_file = NULL;
35479 +       lo->lo_backing_virt_file = NULL;
35480         spin_unlock_irq(&lo->lo_lock);
35481  
35482         loop_release_xfer(lo);
35483 @@ -931,6 +966,8 @@ static int loop_clr_fd(struct loop_device *lo)
35484          * bd_mutex which is usually taken before lo_ctl_mutex.
35485          */
35486         fput(filp);
35487 +       if (virt_filp)
35488 +               fput(virt_filp);
35489         return 0;
35490  }
35491  
35492 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
35493 index 25e8997..93b6fce 100644
35494 --- a/drivers/block/loop.h
35495 +++ b/drivers/block/loop.h
35496 @@ -46,7 +46,7 @@ struct loop_device {
35497         int             (*ioctl)(struct loop_device *, int cmd, 
35498                                  unsigned long arg); 
35499  
35500 -       struct file *   lo_backing_file;
35501 +       struct file *   lo_backing_file, *lo_backing_virt_file;
35502         struct block_device *lo_device;
35503         unsigned        lo_blocksize;
35504         void            *key_data; 
35505 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
35506 index 91c2ce7..d4ee5a7 100644
35507 --- a/fs/aufs/f_op.c
35508 +++ b/fs/aufs/f_op.c
35509 @@ -389,7 +389,7 @@ static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
35510         if (IS_ERR(h_file))
35511                 goto out;
35512  
35513 -       if (au_test_loopback_kthread()) {
35514 +       if (0 && au_test_loopback_kthread()) {
35515                 au_warn_loopback(h_file->f_path.dentry->d_sb);
35516                 if (file->f_mapping != h_file->f_mapping) {
35517                         file->f_mapping = h_file->f_mapping;
35518 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
35519 index 69f7e96..7941063 100644
35520 --- a/fs/aufs/loop.c
35521 +++ b/fs/aufs/loop.c
35522 @@ -130,3 +130,19 @@ void au_loopback_fin(void)
35523         symbol_put(loop_backing_file);
35524         kfree(au_warn_loopback_array);
35525  }
35526 +
35527 +/* ---------------------------------------------------------------------- */
35528 +
35529 +/* support the loopback block device insude aufs */
35530 +
35531 +struct file *aufs_real_loop(struct file *file)
35532 +{
35533 +       struct file *f;
35534 +
35535 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
35536 +       fi_read_lock(file);
35537 +       f = au_hf_top(file);
35538 +       fi_read_unlock(file);
35539 +       AuDebugOn(!f);
35540 +       return f;
35541 +}
35542 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
35543 index 6d9864d..3322557 100644
35544 --- a/fs/aufs/loop.h
35545 +++ b/fs/aufs/loop.h
35546 @@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
35547  
35548  int au_loopback_init(void);
35549  void au_loopback_fin(void);
35550 +
35551 +struct file *aufs_real_loop(struct file *file);
35552  #else
35553 +AuStub(struct file *, loop_backing_file, return NULL)
35554 +
35555  AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
35556            struct dentry *h_adding)
35557  AuStubInt0(au_test_loopback_kthread, void)
35558 @@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
35559  
35560  AuStubInt0(au_loopback_init, void)
35561  AuStubVoid(au_loopback_fin, void)
35562 +
35563 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
35564  #endif /* BLK_DEV_LOOP */
35565  
35566  #endif /* __KERNEL__ */
35567 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
35568 index ee5780d..da35759 100644
35569 --- a/fs/aufs/super.c
35570 +++ b/fs/aufs/super.c
35571 @@ -807,7 +807,10 @@ static const struct super_operations aufs_sop = {
35572         .statfs         = aufs_statfs,
35573         .put_super      = aufs_put_super,
35574         .sync_fs        = aufs_sync_fs,
35575 -       .remount_fs     = aufs_remount_fs
35576 +       .remount_fs     = aufs_remount_fs,
35577 +#ifdef CONFIG_AUFS_BDEV_LOOP
35578 +       .real_loop      = aufs_real_loop
35579 +#endif
35580  };
35581  
35582  /* ---------------------------------------------------------------------- */
35583 diff --git a/include/linux/fs.h b/include/linux/fs.h
35584 index 86080ea..5f8e0f2 100644
35585 --- a/include/linux/fs.h
35586 +++ b/include/linux/fs.h
35587 @@ -1708,6 +1708,10 @@ struct super_operations {
35588                                   struct shrink_control *);
35589         long (*free_cached_objects)(struct super_block *,
35590                                     struct shrink_control *);
35591 +#if defined(CONFIG_BLK_DEV_LOOP) ||  defined(CONFIG_BLK_DEV_LOOP_MODULE)
35592 +       /* and aufs */
35593 +       struct file *(*real_loop)(struct file *);
35594 +#endif
35595  };
35596  
35597  /*
This page took 4.646175 seconds and 4 git commands to generate.