]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- 4.8.1
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
f0c0a007 1aufs4.7 kbuild patch
7f207e10
AM
2
3diff --git a/fs/Kconfig b/fs/Kconfig
5afbbe0d 4index b8fcb41..78adefb 100644
7f207e10
AM
5--- a/fs/Kconfig
6+++ b/fs/Kconfig
5afbbe0d 7@@ -236,6 +236,7 @@ source "fs/pstore/Kconfig"
5527c038 8 source "fs/sysv/Kconfig"
7e9cd9fe 9 source "fs/ufs/Kconfig"
7f207e10
AM
10 source "fs/exofs/Kconfig"
11+source "fs/aufs/Kconfig"
12
13 endif # MISC_FILESYSTEMS
14
15diff --git a/fs/Makefile b/fs/Makefile
5afbbe0d 16index 85b6e13..e7bb164 100644
7f207e10
AM
17--- a/fs/Makefile
18+++ b/fs/Makefile
5afbbe0d 19@@ -128,3 +128,4 @@ obj-y += exofs/ # Multiple modules
7f207e10 20 obj-$(CONFIG_CEPH_FS) += ceph/
bf0370f2 21 obj-$(CONFIG_PSTORE) += pstore/
c06a8ce3 22 obj-$(CONFIG_EFIVAR_FS) += efivarfs/
86dc4139 23+obj-$(CONFIG_AUFS_FS) += aufs/
c06a8ce3 24diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild
f0c0a007 25index ec10cfe..800211b 100644
c06a8ce3
AM
26--- a/include/uapi/linux/Kbuild
27+++ b/include/uapi/linux/Kbuild
5527c038 28@@ -59,6 +59,7 @@ header-y += atmsvc.h
03673fb0
JR
29 header-y += atm_tcp.h
30 header-y += atm_zatm.h
c06a8ce3
AM
31 header-y += audit.h
32+header-y += aufs_type.h
c06a8ce3 33 header-y += auto_fs4.h
03673fb0 34 header-y += auto_fs.h
c06a8ce3 35 header-y += auxvec.h
f0c0a007 36aufs4.7 base patch
7f207e10 37
c1595e42 38diff --git a/MAINTAINERS b/MAINTAINERS
f0c0a007 39index 8c20323..d170184 100644
c1595e42
JR
40--- a/MAINTAINERS
41+++ b/MAINTAINERS
f0c0a007 42@@ -2213,6 +2213,19 @@ F: include/linux/audit.h
c1595e42
JR
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
5527c038 51+T: git://github.com/sfjro/aufs4-linux.git
c1595e42
JR
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
392086de 62diff --git a/drivers/block/loop.c b/drivers/block/loop.c
5afbbe0d 63index 1fa8cc2..7339e65 100644
392086de
AM
64--- a/drivers/block/loop.c
65+++ b/drivers/block/loop.c
5afbbe0d 66@@ -712,6 +712,24 @@ static inline int is_loop_device(struct file *file)
392086de
AM
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+}
febd17d6 86+EXPORT_SYMBOL_GPL(loop_backing_file);
392086de
AM
87+
88 /* loop sysfs attributes */
89
90 static ssize_t loop_attr_show(struct device *dev, char *page,
c1595e42 91diff --git a/fs/dcache.c b/fs/dcache.c
5afbbe0d 92index d6847d7..c3c0b6d 100644
c1595e42
JR
93--- a/fs/dcache.c
94+++ b/fs/dcache.c
5afbbe0d 95@@ -1202,7 +1202,7 @@ enum d_walk_ret {
c1595e42
JR
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 {
febd17d6
JR
104diff --git a/fs/fcntl.c b/fs/fcntl.c
105index 350a2c8..6f42279 100644
106--- a/fs/fcntl.c
107+++ b/fs/fcntl.c
108@@ -29,7 +29,7 @@
109
110 #define SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT | O_NOATIME)
111
112-static int setfl(int fd, struct file * filp, unsigned long arg)
113+int setfl(int fd, struct file * filp, unsigned long arg)
114 {
115 struct inode * inode = file_inode(filp);
116 int error = 0;
117@@ -60,6 +60,8 @@ static int setfl(int fd, struct file * filp, unsigned long arg)
118
119 if (filp->f_op->check_flags)
120 error = filp->f_op->check_flags(arg);
121+ if (!error && filp->f_op->setfl)
122+ error = filp->f_op->setfl(filp, arg);
123 if (error)
124 return error;
125
5afbbe0d
AM
126diff --git a/fs/inode.c b/fs/inode.c
127index 4ccbc21..aa6d071 100644
128--- a/fs/inode.c
129+++ b/fs/inode.c
130@@ -1591,7 +1591,7 @@ EXPORT_SYMBOL(generic_update_time);
131 * This does the actual work of updating an inodes time or version. Must have
132 * had called mnt_want_write() before calling this.
133 */
134-static int update_time(struct inode *inode, struct timespec *time, int flags)
135+int update_time(struct inode *inode, struct timespec *time, int flags)
136 {
137 int (*update_time)(struct inode *, struct timespec *, int);
138
5527c038 139diff --git a/fs/read_write.c b/fs/read_write.c
5afbbe0d 140index 933b53a..2d13282 100644
5527c038
JR
141--- a/fs/read_write.c
142+++ b/fs/read_write.c
5afbbe0d 143@@ -515,6 +515,28 @@ ssize_t __vfs_write(struct file *file, const char __user *p, size_t count,
5527c038
JR
144 }
145 EXPORT_SYMBOL(__vfs_write);
146
147+vfs_readf_t vfs_readf(struct file *file)
148+{
149+ const struct file_operations *fop = file->f_op;
150+
151+ if (fop->read)
152+ return fop->read;
153+ if (fop->read_iter)
154+ return new_sync_read;
155+ return ERR_PTR(-ENOSYS);
156+}
157+
158+vfs_writef_t vfs_writef(struct file *file)
159+{
160+ const struct file_operations *fop = file->f_op;
161+
162+ if (fop->write)
163+ return fop->write;
164+ if (fop->write_iter)
165+ return new_sync_write;
166+ return ERR_PTR(-ENOSYS);
167+}
168+
169 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
170 {
171 mm_segment_t old_fs;
7f207e10 172diff --git a/fs/splice.c b/fs/splice.c
5afbbe0d 173index dd9bf7e..9326c2a 100644
7f207e10
AM
174--- a/fs/splice.c
175+++ b/fs/splice.c
5afbbe0d 176@@ -1111,8 +1111,8 @@ EXPORT_SYMBOL(generic_splice_sendpage);
7f207e10
AM
177 /*
178 * Attempt to initiate a splice from pipe to file.
179 */
180-static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
181- loff_t *ppos, size_t len, unsigned int flags)
182+long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
183+ loff_t *ppos, size_t len, unsigned int flags)
184 {
185 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *,
186 loff_t *, size_t, unsigned int);
5afbbe0d 187@@ -1128,9 +1128,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
7f207e10
AM
188 /*
189 * Attempt to initiate a splice from a file to a pipe.
190 */
191-static long do_splice_to(struct file *in, loff_t *ppos,
192- struct pipe_inode_info *pipe, size_t len,
193- unsigned int flags)
194+long do_splice_to(struct file *in, loff_t *ppos,
195+ struct pipe_inode_info *pipe, size_t len,
196+ unsigned int flags)
197 {
198 ssize_t (*splice_read)(struct file *, loff_t *,
199 struct pipe_inode_info *, size_t, unsigned int);
b912730e 200diff --git a/include/linux/file.h b/include/linux/file.h
5afbbe0d 201index 7444f5f..bdac0be 100644
b912730e
AM
202--- a/include/linux/file.h
203+++ b/include/linux/file.h
204@@ -19,6 +19,7 @@ struct dentry;
205 struct path;
206 extern struct file *alloc_file(struct path *, fmode_t mode,
207 const struct file_operations *fop);
208+extern struct file *get_empty_filp(void);
209
210 static inline void fput_light(struct file *file, int fput_needed)
211 {
5527c038 212diff --git a/include/linux/fs.h b/include/linux/fs.h
5afbbe0d 213index dd28814..b689a48 100644
5527c038
JR
214--- a/include/linux/fs.h
215+++ b/include/linux/fs.h
5afbbe0d 216@@ -1306,6 +1306,7 @@ extern void fasync_free(struct fasync_struct *);
febd17d6
JR
217 /* can be called from interrupts */
218 extern void kill_fasync(struct fasync_struct **, int, int);
219
220+extern int setfl(int fd, struct file * filp, unsigned long arg);
221 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
222 extern void f_setown(struct file *filp, unsigned long arg, int force);
223 extern void f_delown(struct file *filp);
5afbbe0d 224@@ -1690,6 +1691,7 @@ struct file_operations {
febd17d6
JR
225 ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
226 unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
227 int (*check_flags)(int);
228+ int (*setfl)(struct file *, unsigned long);
229 int (*flock) (struct file *, int, struct file_lock *);
230 ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
231 ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
5afbbe0d 232@@ -1750,6 +1752,12 @@ ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
5527c038
JR
233 struct iovec *fast_pointer,
234 struct iovec **ret_pointer);
235
236+typedef ssize_t (*vfs_readf_t)(struct file *, char __user *, size_t, loff_t *);
237+typedef ssize_t (*vfs_writef_t)(struct file *, const char __user *, size_t,
238+ loff_t *);
239+vfs_readf_t vfs_readf(struct file *file);
240+vfs_writef_t vfs_writef(struct file *file);
241+
242 extern ssize_t __vfs_read(struct file *, char __user *, size_t, loff_t *);
243 extern ssize_t __vfs_write(struct file *, const char __user *, size_t, loff_t *);
244 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
5afbbe0d
AM
245@@ -2105,6 +2113,7 @@ extern int current_umask(void);
246 extern void ihold(struct inode * inode);
247 extern void iput(struct inode *);
248 extern int generic_update_time(struct inode *, struct timespec *, int);
249+extern int update_time(struct inode *, struct timespec *, int);
250
251 /* /sys/fs */
252 extern struct kobject *fs_kobj;
1e00d052 253diff --git a/include/linux/splice.h b/include/linux/splice.h
076b876e 254index da2751d..2e0fca6 100644
1e00d052
AM
255--- a/include/linux/splice.h
256+++ b/include/linux/splice.h
076b876e 257@@ -83,4 +83,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
4b3da204
AM
258 extern void spd_release_page(struct splice_pipe_desc *, unsigned int);
259
260 extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
1e00d052
AM
261+
262+extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
263+ loff_t *ppos, size_t len, unsigned int flags);
264+extern long do_splice_to(struct file *in, loff_t *ppos,
265+ struct pipe_inode_info *pipe, size_t len,
266+ unsigned int flags);
267 #endif
f0c0a007 268aufs4.7 mmap patch
fb47a38f 269
c1595e42 270diff --git a/fs/proc/base.c b/fs/proc/base.c
5afbbe0d 271index a11eb71..8f10865 100644
c1595e42
JR
272--- a/fs/proc/base.c
273+++ b/fs/proc/base.c
5afbbe0d 274@@ -1939,7 +1939,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
c1595e42
JR
275 down_read(&mm->mmap_sem);
276 vma = find_exact_vma(mm, vm_start, vm_end);
277 if (vma && vma->vm_file) {
278- *path = vma->vm_file->f_path;
279+ *path = vma_pr_or_file(vma)->f_path;
280 path_get(path);
281 rc = 0;
282 }
fb47a38f 283diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
c2c0f25c 284index f8595e8..cb8eda0 100644
fb47a38f
JR
285--- a/fs/proc/nommu.c
286+++ b/fs/proc/nommu.c
076b876e 287@@ -45,7 +45,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
fb47a38f
JR
288 file = region->vm_file;
289
290 if (file) {
291- struct inode *inode = file_inode(region->vm_file);
292+ struct inode *inode;
076b876e 293+
fb47a38f
JR
294+ file = vmr_pr_or_file(region);
295+ inode = file_inode(file);
296 dev = inode->i_sb->s_dev;
297 ino = inode->i_ino;
298 }
299diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
5afbbe0d 300index 4648c7f..061cb85 100644
fb47a38f
JR
301--- a/fs/proc/task_mmu.c
302+++ b/fs/proc/task_mmu.c
febd17d6 303@@ -298,7 +298,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma, int is_pid)
fb47a38f
JR
304 const char *name = NULL;
305
306 if (file) {
307- struct inode *inode = file_inode(vma->vm_file);
308+ struct inode *inode;
076b876e 309+
fb47a38f
JR
310+ file = vma_pr_or_file(vma);
311+ inode = file_inode(file);
312 dev = inode->i_sb->s_dev;
313 ino = inode->i_ino;
314 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
5afbbe0d 315@@ -1624,7 +1627,7 @@ static int show_numa_map(struct seq_file *m, void *v, int is_pid)
076b876e
AM
316 struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
317 struct vm_area_struct *vma = v;
318 struct numa_maps *md = &numa_priv->md;
319- struct file *file = vma->vm_file;
320+ struct file *file = vma_pr_or_file(vma);
076b876e 321 struct mm_struct *mm = vma->vm_mm;
7e9cd9fe
AM
322 struct mm_walk walk = {
323 .hugetlb_entry = gather_hugetlb_stats,
fb47a38f 324diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
febd17d6 325index faacb0c..17b43be 100644
fb47a38f
JR
326--- a/fs/proc/task_nommu.c
327+++ b/fs/proc/task_nommu.c
febd17d6 328@@ -163,7 +163,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma,
fb47a38f
JR
329 file = vma->vm_file;
330
331 if (file) {
332- struct inode *inode = file_inode(vma->vm_file);
333+ struct inode *inode;
076b876e 334+
b912730e 335+ file = vma_pr_or_file(vma);
fb47a38f
JR
336+ inode = file_inode(file);
337 dev = inode->i_sb->s_dev;
338 ino = inode->i_ino;
339 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
340diff --git a/include/linux/mm.h b/include/linux/mm.h
5afbbe0d 341index ece042d..1e24513 100644
fb47a38f
JR
342--- a/include/linux/mm.h
343+++ b/include/linux/mm.h
5afbbe0d 344@@ -1239,6 +1239,28 @@ static inline int fixup_user_fault(struct task_struct *tsk,
fb47a38f
JR
345 }
346 #endif
347
076b876e
AM
348+extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
349+extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
350+ int);
351+extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
352+extern void vma_do_fput(struct vm_area_struct *, const char[], int);
fb47a38f 353+
fb47a38f
JR
354+#define vma_file_update_time(vma) vma_do_file_update_time(vma, __func__, \
355+ __LINE__)
356+#define vma_pr_or_file(vma) vma_do_pr_or_file(vma, __func__, \
357+ __LINE__)
358+#define vma_get_file(vma) vma_do_get_file(vma, __func__, __LINE__)
359+#define vma_fput(vma) vma_do_fput(vma, __func__, __LINE__)
b912730e
AM
360+
361+#ifndef CONFIG_MMU
076b876e
AM
362+extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
363+extern void vmr_do_fput(struct vm_region *, const char[], int);
364+
365+#define vmr_pr_or_file(region) vmr_do_pr_or_file(region, __func__, \
366+ __LINE__)
367+#define vmr_fput(region) vmr_do_fput(region, __func__, __LINE__)
b912730e 368+#endif /* !CONFIG_MMU */
fb47a38f
JR
369+
370 extern int access_process_vm(struct task_struct *tsk, unsigned long addr, void *buf, int len, int write);
371 extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
372 void *buf, int len, int write);
373diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
5afbbe0d 374index ca3e517..10bc491 100644
fb47a38f
JR
375--- a/include/linux/mm_types.h
376+++ b/include/linux/mm_types.h
5afbbe0d 377@@ -274,6 +274,7 @@ struct vm_region {
fb47a38f
JR
378 unsigned long vm_top; /* region allocated to here */
379 unsigned long vm_pgoff; /* the offset in vm_file corresponding to vm_start */
380 struct file *vm_file; /* the backing file or NULL */
381+ struct file *vm_prfile; /* the virtual backing file or NULL */
382
383 int vm_usage; /* region usage count (access under nommu_region_sem) */
384 bool vm_icache_flushed : 1; /* true if the icache has been flushed for
5afbbe0d 385@@ -348,6 +349,7 @@ struct vm_area_struct {
fb47a38f 386 unsigned long vm_pgoff; /* Offset (within vm_file) in PAGE_SIZE
5afbbe0d 387 units */
fb47a38f
JR
388 struct file * vm_file; /* File we map to (can be NULL). */
389+ struct file *vm_prfile; /* shadow of vm_file */
390 void * vm_private_data; /* was vm_pte (shared mem) */
391
392 #ifndef CONFIG_MMU
393diff --git a/kernel/fork.c b/kernel/fork.c
5afbbe0d 394index 4a7ec0c..8c8f7ac 100644
fb47a38f
JR
395--- a/kernel/fork.c
396+++ b/kernel/fork.c
5afbbe0d 397@@ -479,7 +479,7 @@ static int dup_mmap(struct mm_struct *mm, struct mm_struct *oldmm)
fb47a38f
JR
398 struct inode *inode = file_inode(file);
399 struct address_space *mapping = file->f_mapping;
400
401- get_file(file);
402+ vma_get_file(tmp);
403 if (tmp->vm_flags & VM_DENYWRITE)
404 atomic_dec(&inode->i_writecount);
2000de60 405 i_mmap_lock_write(mapping);
076b876e 406diff --git a/mm/Makefile b/mm/Makefile
5afbbe0d 407index 78c6f7d..aea4230 100644
076b876e
AM
408--- a/mm/Makefile
409+++ b/mm/Makefile
5afbbe0d 410@@ -37,7 +37,7 @@ obj-y := filemap.o mempool.o oom_kill.o \
076b876e 411 mm_init.o mmu_context.o percpu.o slab_common.o \
c1595e42 412 compaction.o vmacache.o \
076b876e 413 interval_tree.o list_lru.o workingset.o \
7e9cd9fe
AM
414- debug.o $(mmu-y)
415+ prfile.o debug.o $(mmu-y)
076b876e
AM
416
417 obj-y += init-mm.o
418
fb47a38f 419diff --git a/mm/filemap.c b/mm/filemap.c
5afbbe0d 420index 20f3b1f..ee827ce 100644
fb47a38f
JR
421--- a/mm/filemap.c
422+++ b/mm/filemap.c
5afbbe0d 423@@ -2208,7 +2208,7 @@ int filemap_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
fb47a38f
JR
424 int ret = VM_FAULT_LOCKED;
425
426 sb_start_pagefault(inode->i_sb);
427- file_update_time(vma->vm_file);
428+ vma_file_update_time(vma);
429 lock_page(page);
430 if (page->mapping != inode->i_mapping) {
431 unlock_page(page);
fb47a38f 432diff --git a/mm/memory.c b/mm/memory.c
f0c0a007 433index 9e04681..06980d1 100644
fb47a38f
JR
434--- a/mm/memory.c
435+++ b/mm/memory.c
5afbbe0d 436@@ -2100,7 +2100,7 @@ static inline int wp_page_reuse(struct mm_struct *mm,
fb47a38f 437 }
7e9cd9fe 438
b912730e
AM
439 if (!page_mkwrite)
440- file_update_time(vma->vm_file);
441+ vma_file_update_time(vma);
442 }
443
444 return VM_FAULT_WRITE;
fb47a38f 445diff --git a/mm/mmap.c b/mm/mmap.c
5afbbe0d 446index de2c176..b7f391c 100644
fb47a38f
JR
447--- a/mm/mmap.c
448+++ b/mm/mmap.c
5afbbe0d 449@@ -162,7 +162,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
fb47a38f
JR
450 if (vma->vm_ops && vma->vm_ops->close)
451 vma->vm_ops->close(vma);
452 if (vma->vm_file)
453- fput(vma->vm_file);
454+ vma_fput(vma);
455 mpol_put(vma_policy(vma));
456 kmem_cache_free(vm_area_cachep, vma);
457 return next;
5afbbe0d 458@@ -782,7 +782,7 @@ again: remove_next = 1 + (end > next->vm_end);
fb47a38f
JR
459 if (remove_next) {
460 if (file) {
461 uprobe_munmap(next, next->vm_start, next->vm_end);
462- fput(file);
463+ vma_fput(vma);
464 }
465 if (next->anon_vma)
466 anon_vma_merge(vma, next);
5afbbe0d 467@@ -1563,8 +1563,8 @@ out:
35939ee7
JR
468 return addr;
469
fb47a38f 470 unmap_and_free_vma:
fb47a38f
JR
471+ vma_fput(vma);
472 vma->vm_file = NULL;
473- fput(file);
474
475 /* Undo any partial mapping done by a device driver. */
476 unmap_region(mm, vma, prev, vma->vm_start, vma->vm_end);
5afbbe0d 477@@ -2358,7 +2358,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
478 goto out_free_mpol;
479
480 if (new->vm_file)
481- get_file(new->vm_file);
482+ vma_get_file(new);
483
484 if (new->vm_ops && new->vm_ops->open)
485 new->vm_ops->open(new);
5afbbe0d 486@@ -2377,7 +2377,7 @@ static int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
fb47a38f
JR
487 if (new->vm_ops && new->vm_ops->close)
488 new->vm_ops->close(new);
489 if (new->vm_file)
490- fput(new->vm_file);
491+ vma_fput(new);
492 unlink_anon_vmas(new);
493 out_free_mpol:
494 mpol_put(vma_policy(new));
5afbbe0d 495@@ -2528,7 +2528,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
7e9cd9fe
AM
496 struct vm_area_struct *vma;
497 unsigned long populate = 0;
498 unsigned long ret = -EINVAL;
499- struct file *file;
5afbbe0d 500+ struct file *file, *prfile;
7e9cd9fe 501
5afbbe0d
AM
502 pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.txt.\n",
503 current->comm, current->pid);
504@@ -2597,10 +2597,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
febd17d6 505 }
7e9cd9fe
AM
506 }
507
508- file = get_file(vma->vm_file);
509+ vma_get_file(vma);
5afbbe0d
AM
510+ file = vma->vm_file;
511+ prfile = vma->vm_prfile;
7e9cd9fe
AM
512 ret = do_mmap_pgoff(vma->vm_file, start, size,
513 prot, flags, pgoff, &populate);
5afbbe0d
AM
514+ if (!IS_ERR_VALUE(ret) && file && prfile) {
515+ struct vm_area_struct *new_vma;
516+
517+ new_vma = find_vma(mm, ret);
518+ if (!new_vma->vm_prfile)
519+ new_vma->vm_prfile = prfile;
520+ if (new_vma != vma)
521+ get_file(prfile);
522+ }
523+ /*
524+ * two fput()s instead of vma_fput(vma),
525+ * coz vma may not be available anymore.
526+ */
527 fput(file);
528+ if (prfile)
529+ fput(prfile);
7e9cd9fe
AM
530 out:
531 up_write(&mm->mmap_sem);
532 if (populate)
5afbbe0d 533@@ -2873,7 +2890,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
79b8bda9
AM
534 if (anon_vma_clone(new_vma, vma))
535 goto out_free_mempol;
536 if (new_vma->vm_file)
537- get_file(new_vma->vm_file);
538+ vma_get_file(new_vma);
539 if (new_vma->vm_ops && new_vma->vm_ops->open)
540 new_vma->vm_ops->open(new_vma);
541 vma_link(mm, new_vma, prev, rb_link, rb_parent);
fb47a38f 542diff --git a/mm/nommu.c b/mm/nommu.c
5afbbe0d 543index c2e588802..c39edc4 100644
fb47a38f
JR
544--- a/mm/nommu.c
545+++ b/mm/nommu.c
5afbbe0d 546@@ -644,7 +644,7 @@ static void __put_nommu_region(struct vm_region *region)
fb47a38f
JR
547 up_write(&nommu_region_sem);
548
549 if (region->vm_file)
550- fput(region->vm_file);
551+ vmr_fput(region);
552
553 /* IO memory and memory shared directly out of the pagecache
554 * from ramfs/tmpfs mustn't be released here */
5afbbe0d 555@@ -802,7 +802,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
fb47a38f
JR
556 if (vma->vm_ops && vma->vm_ops->close)
557 vma->vm_ops->close(vma);
558 if (vma->vm_file)
559- fput(vma->vm_file);
560+ vma_fput(vma);
561 put_nommu_region(vma->vm_region);
562 kmem_cache_free(vm_area_cachep, vma);
563 }
5afbbe0d 564@@ -1328,7 +1328,7 @@ unsigned long do_mmap(struct file *file,
fb47a38f
JR
565 goto error_just_free;
566 }
567 }
568- fput(region->vm_file);
569+ vmr_fput(region);
570 kmem_cache_free(vm_region_jar, region);
571 region = pregion;
572 result = start;
5afbbe0d 573@@ -1403,10 +1403,10 @@ error_just_free:
fb47a38f
JR
574 up_write(&nommu_region_sem);
575 error:
576 if (region->vm_file)
577- fput(region->vm_file);
578+ vmr_fput(region);
579 kmem_cache_free(vm_region_jar, region);
580 if (vma->vm_file)
581- fput(vma->vm_file);
582+ vma_fput(vma);
583 kmem_cache_free(vm_area_cachep, vma);
fb47a38f 584 return ret;
c2c0f25c 585
076b876e
AM
586diff --git a/mm/prfile.c b/mm/prfile.c
587new file mode 100644
c2c0f25c 588index 0000000..b323b8a
076b876e
AM
589--- /dev/null
590+++ b/mm/prfile.c
591@@ -0,0 +1,86 @@
592+/*
593+ * Mainly for aufs which mmap(2) diffrent file and wants to print different path
594+ * in /proc/PID/maps.
595+ * Call these functions via macros defined in linux/mm.h.
596+ *
597+ * See Documentation/filesystems/aufs/design/06mmap.txt
598+ *
599+ * Copyright (c) 2014 Junjro R. Okajima
600+ * Copyright (c) 2014 Ian Campbell
601+ */
602+
603+#include <linux/mm.h>
604+#include <linux/file.h>
605+#include <linux/fs.h>
606+
607+/* #define PRFILE_TRACE */
608+static inline void prfile_trace(struct file *f, struct file *pr,
609+ const char func[], int line, const char func2[])
610+{
611+#ifdef PRFILE_TRACE
612+ if (pr)
c2c0f25c 613+ pr_info("%s:%d: %s, %s\n", func, line, func2,
7e9cd9fe 614+ f ? (char *)f->f_path.dentry->d_name.name : "(null)");
076b876e
AM
615+#endif
616+}
617+
076b876e
AM
618+void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
619+ int line)
620+{
621+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
622+
623+ prfile_trace(f, pr, func, line, __func__);
624+ file_update_time(f);
625+ if (f && pr)
626+ file_update_time(pr);
627+}
628+
629+struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
630+ int line)
631+{
632+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633+
634+ prfile_trace(f, pr, func, line, __func__);
635+ return (f && pr) ? pr : f;
636+}
637+
638+void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
639+{
640+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
641+
642+ prfile_trace(f, pr, func, line, __func__);
643+ get_file(f);
644+ if (f && pr)
645+ get_file(pr);
646+}
647+
648+void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
649+{
650+ struct file *f = vma->vm_file, *pr = vma->vm_prfile;
651+
652+ prfile_trace(f, pr, func, line, __func__);
653+ fput(f);
654+ if (f && pr)
655+ fput(pr);
656+}
b912730e
AM
657+
658+#ifndef CONFIG_MMU
076b876e
AM
659+struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
660+ int line)
661+{
662+ struct file *f = region->vm_file, *pr = region->vm_prfile;
663+
664+ prfile_trace(f, pr, func, line, __func__);
665+ return (f && pr) ? pr : f;
666+}
667+
668+void vmr_do_fput(struct vm_region *region, const char func[], int line)
669+{
670+ struct file *f = region->vm_file, *pr = region->vm_prfile;
671+
672+ prfile_trace(f, pr, func, line, __func__);
673+ fput(f);
674+ if (f && pr)
675+ fput(pr);
676+}
b912730e 677+#endif /* !CONFIG_MMU */
f0c0a007 678aufs4.7 standalone patch
7f207e10 679
c1595e42 680diff --git a/fs/dcache.c b/fs/dcache.c
5afbbe0d 681index c3c0b6d..c99d2d2 100644
c1595e42
JR
682--- a/fs/dcache.c
683+++ b/fs/dcache.c
5afbbe0d 684@@ -1310,6 +1310,7 @@ rename_retry:
c1595e42
JR
685 seq = 1;
686 goto again;
687 }
febd17d6 688+EXPORT_SYMBOL_GPL(d_walk);
c1595e42
JR
689
690 /*
691 * Search for at least 1 mount point in the dentry's subdirs.
79b8bda9 692diff --git a/fs/exec.c b/fs/exec.c
5afbbe0d 693index 887c1c9..40e8767 100644
79b8bda9
AM
694--- a/fs/exec.c
695+++ b/fs/exec.c
5afbbe0d 696@@ -104,6 +104,7 @@ bool path_noexec(const struct path *path)
79b8bda9
AM
697 return (path->mnt->mnt_flags & MNT_NOEXEC) ||
698 (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
699 }
febd17d6 700+EXPORT_SYMBOL_GPL(path_noexec);
79b8bda9
AM
701
702 #ifdef CONFIG_USELIB
703 /*
febd17d6
JR
704diff --git a/fs/fcntl.c b/fs/fcntl.c
705index 6f42279..04fd33c 100644
706--- a/fs/fcntl.c
707+++ b/fs/fcntl.c
708@@ -82,6 +82,7 @@ int setfl(int fd, struct file * filp, unsigned long arg)
709 out:
710 return error;
711 }
712+EXPORT_SYMBOL_GPL(setfl);
713
714 static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
715 int force)
b912730e 716diff --git a/fs/file_table.c b/fs/file_table.c
febd17d6 717index ad17e05..ae9f267 100644
b912730e
AM
718--- a/fs/file_table.c
719+++ b/fs/file_table.c
79b8bda9 720@@ -147,6 +147,7 @@ over:
b912730e
AM
721 }
722 return ERR_PTR(-ENFILE);
723 }
febd17d6 724+EXPORT_SYMBOL_GPL(get_empty_filp);
b912730e
AM
725
726 /**
727 * alloc_file - allocate and initialize a 'struct file'
8cdd5066
JR
728@@ -258,6 +259,7 @@ void flush_delayed_fput(void)
729 {
730 delayed_fput(NULL);
731 }
febd17d6 732+EXPORT_SYMBOL_GPL(flush_delayed_fput);
8cdd5066
JR
733
734 static DECLARE_DELAYED_WORK(delayed_fput_work, delayed_fput);
735
736@@ -300,6 +302,7 @@ void __fput_sync(struct file *file)
737 }
738
739 EXPORT_SYMBOL(fput);
febd17d6 740+EXPORT_SYMBOL_GPL(__fput_sync);
8cdd5066
JR
741
742 void put_filp(struct file *file)
743 {
744@@ -308,6 +311,7 @@ void put_filp(struct file *file)
b912730e
AM
745 file_free(file);
746 }
747 }
febd17d6 748+EXPORT_SYMBOL_GPL(put_filp);
b912730e 749
79b8bda9 750 void __init files_init(void)
b912730e 751 {
5afbbe0d
AM
752diff --git a/fs/inode.c b/fs/inode.c
753index aa6d071..f336032 100644
754--- a/fs/inode.c
755+++ b/fs/inode.c
756@@ -1600,6 +1600,7 @@ int update_time(struct inode *inode, struct timespec *time, int flags)
757
758 return update_time(inode, time, flags);
759 }
760+EXPORT_SYMBOL_GPL(update_time);
761
762 /**
763 * touch_atime - update the access time
7f207e10 764diff --git a/fs/namespace.c b/fs/namespace.c
f0c0a007 765index 419f746..9c0e0af 100644
7f207e10
AM
766--- a/fs/namespace.c
767+++ b/fs/namespace.c
7e9cd9fe 768@@ -463,6 +463,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
c06a8ce3
AM
769 mnt_dec_writers(real_mount(mnt));
770 preempt_enable();
771 }
772+EXPORT_SYMBOL_GPL(__mnt_drop_write);
773
774 /**
775 * mnt_drop_write - give up write access to a mount
f0c0a007 776@@ -1812,6 +1813,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
7f207e10
AM
777 }
778 return 0;
779 }
febd17d6 780+EXPORT_SYMBOL_GPL(iterate_mounts);
7f207e10 781
7eafdf33 782 static void cleanup_group_ids(struct mount *mnt, struct mount *end)
7f207e10
AM
783 {
784diff --git a/fs/notify/group.c b/fs/notify/group.c
5afbbe0d 785index 3e2dd85..b17cb4b 100644
7f207e10
AM
786--- a/fs/notify/group.c
787+++ b/fs/notify/group.c
788@@ -22,6 +22,7 @@
789 #include <linux/srcu.h>
790 #include <linux/rculist.h>
791 #include <linux/wait.h>
792+#include <linux/module.h>
793
794 #include <linux/fsnotify_backend.h>
795 #include "fsnotify.h"
5afbbe0d 796@@ -81,6 +82,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
1716fcea
AM
797 {
798 atomic_inc(&group->refcnt);
799 }
febd17d6 800+EXPORT_SYMBOL_GPL(fsnotify_get_group);
1716fcea
AM
801
802 /*
803 * Drop a reference to a group. Free it if it's through.
5afbbe0d 804@@ -90,6 +92,7 @@ void fsnotify_put_group(struct fsnotify_group *group)
7f207e10 805 if (atomic_dec_and_test(&group->refcnt))
1716fcea 806 fsnotify_final_destroy_group(group);
7f207e10 807 }
febd17d6 808+EXPORT_SYMBOL_GPL(fsnotify_put_group);
7f207e10
AM
809
810 /*
811 * Create a new fsnotify_group and hold a reference for the group returned.
5afbbe0d 812@@ -118,6 +121,7 @@ struct fsnotify_group *fsnotify_alloc_group(const struct fsnotify_ops *ops)
7f207e10
AM
813
814 return group;
815 }
febd17d6 816+EXPORT_SYMBOL_GPL(fsnotify_alloc_group);
1716fcea
AM
817
818 int fsnotify_fasync(int fd, struct file *file, int on)
819 {
7f207e10 820diff --git a/fs/notify/mark.c b/fs/notify/mark.c
5afbbe0d 821index d3fea0b..5fc06ad 100644
7f207e10
AM
822--- a/fs/notify/mark.c
823+++ b/fs/notify/mark.c
febd17d6 824@@ -113,6 +113,7 @@ void fsnotify_put_mark(struct fsnotify_mark *mark)
7f207e10 825 mark->free_mark(mark);
1716fcea 826 }
7f207e10 827 }
febd17d6 828+EXPORT_SYMBOL_GPL(fsnotify_put_mark);
7f207e10 829
2000de60
JR
830 /* Calculate mask of events for a list of marks */
831 u32 fsnotify_recalc_mask(struct hlist_head *head)
5afbbe0d 832@@ -230,6 +231,7 @@ void fsnotify_destroy_mark(struct fsnotify_mark *mark,
1716fcea 833 mutex_unlock(&group->mark_mutex);
79b8bda9 834 fsnotify_free_mark(mark);
7f207e10 835 }
febd17d6 836+EXPORT_SYMBOL_GPL(fsnotify_destroy_mark);
7f207e10 837
79b8bda9
AM
838 void fsnotify_destroy_marks(struct hlist_head *head, spinlock_t *lock)
839 {
5afbbe0d 840@@ -415,6 +417,7 @@ err:
7f207e10
AM
841
842 return ret;
843 }
febd17d6 844+EXPORT_SYMBOL_GPL(fsnotify_add_mark);
7f207e10 845
1716fcea
AM
846 int fsnotify_add_mark(struct fsnotify_mark *mark, struct fsnotify_group *group,
847 struct inode *inode, struct vfsmount *mnt, int allow_dups)
5afbbe0d 848@@ -533,6 +536,7 @@ void fsnotify_init_mark(struct fsnotify_mark *mark,
7f207e10
AM
849 atomic_set(&mark->refcnt, 1);
850 mark->free_mark = free_mark;
851 }
febd17d6 852+EXPORT_SYMBOL_GPL(fsnotify_init_mark);
7f207e10 853
5afbbe0d
AM
854 /*
855 * Destroy all marks in destroy_list, waits for SRCU period to finish before
7f207e10 856diff --git a/fs/open.c b/fs/open.c
5afbbe0d 857index 93ae3cd..d25b9bd 100644
7f207e10
AM
858--- a/fs/open.c
859+++ b/fs/open.c
c2c0f25c 860@@ -64,6 +64,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs,
febd17d6 861 inode_unlock(dentry->d_inode);
7f207e10
AM
862 return ret;
863 }
febd17d6 864+EXPORT_SYMBOL_GPL(do_truncate);
7f207e10 865
5afbbe0d 866 long vfs_truncate(const struct path *path, loff_t length)
7f207e10 867 {
c2c0f25c 868@@ -678,6 +679,7 @@ int open_check_o_direct(struct file *f)
b912730e
AM
869 }
870 return 0;
871 }
febd17d6 872+EXPORT_SYMBOL_GPL(open_check_o_direct);
b912730e
AM
873
874 static int do_dentry_open(struct file *f,
c2c0f25c 875 struct inode *inode,
5527c038 876diff --git a/fs/read_write.c b/fs/read_write.c
5afbbe0d 877index 2d13282..260c4a4 100644
5527c038
JR
878--- a/fs/read_write.c
879+++ b/fs/read_write.c
5afbbe0d 880@@ -525,6 +525,7 @@ vfs_readf_t vfs_readf(struct file *file)
5527c038
JR
881 return new_sync_read;
882 return ERR_PTR(-ENOSYS);
883 }
febd17d6 884+EXPORT_SYMBOL_GPL(vfs_readf);
5527c038
JR
885
886 vfs_writef_t vfs_writef(struct file *file)
887 {
5afbbe0d 888@@ -536,6 +537,7 @@ vfs_writef_t vfs_writef(struct file *file)
5527c038
JR
889 return new_sync_write;
890 return ERR_PTR(-ENOSYS);
891 }
febd17d6 892+EXPORT_SYMBOL_GPL(vfs_writef);
5527c038
JR
893
894 ssize_t __kernel_write(struct file *file, const char *buf, size_t count, loff_t *pos)
895 {
7f207e10 896diff --git a/fs/splice.c b/fs/splice.c
5afbbe0d 897index 9326c2a..0606690 100644
7f207e10
AM
898--- a/fs/splice.c
899+++ b/fs/splice.c
5afbbe0d 900@@ -1124,6 +1124,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
392086de
AM
901
902 return splice_write(pipe, out, ppos, len, flags);
7f207e10 903 }
febd17d6 904+EXPORT_SYMBOL_GPL(do_splice_from);
7f207e10
AM
905
906 /*
907 * Attempt to initiate a splice from a file to a pipe.
5afbbe0d 908@@ -1153,6 +1154,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
7f207e10
AM
909
910 return splice_read(in, ppos, pipe, len, flags);
911 }
febd17d6 912+EXPORT_SYMBOL_GPL(do_splice_to);
7f207e10
AM
913
914 /**
915 * splice_direct_to_actor - splices data directly between two non-pipes
c1595e42 916diff --git a/fs/xattr.c b/fs/xattr.c
5afbbe0d 917index 4beafc4..e118715 100644
c1595e42
JR
918--- a/fs/xattr.c
919+++ b/fs/xattr.c
920@@ -207,6 +207,7 @@ vfs_getxattr_alloc(struct dentry *dentry, const char *name, char **xattr_value,
921 *xattr_value = value;
922 return error;
923 }
febd17d6 924+EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
c1595e42 925
febd17d6
JR
926 ssize_t
927 vfs_getxattr(struct dentry *dentry, const char *name, void *value, size_t size)
8cdd5066 928diff --git a/kernel/task_work.c b/kernel/task_work.c
febd17d6 929index 53fa971..bce3211 100644
8cdd5066
JR
930--- a/kernel/task_work.c
931+++ b/kernel/task_work.c
932@@ -118,3 +118,4 @@ void task_work_run(void)
933 } while (work);
934 }
935 }
febd17d6 936+EXPORT_SYMBOL_GPL(task_work_run);
7f207e10 937diff --git a/security/commoncap.c b/security/commoncap.c
5afbbe0d 938index e7fadde..6423e53 100644
7f207e10
AM
939--- a/security/commoncap.c
940+++ b/security/commoncap.c
febd17d6 941@@ -1058,12 +1058,14 @@ int cap_mmap_addr(unsigned long addr)
94337f0d 942 }
7f207e10
AM
943 return ret;
944 }
febd17d6 945+EXPORT_SYMBOL_GPL(cap_mmap_addr);
0c3ec466
AM
946
947 int cap_mmap_file(struct file *file, unsigned long reqprot,
948 unsigned long prot, unsigned long flags)
949 {
950 return 0;
951 }
febd17d6 952+EXPORT_SYMBOL_GPL(cap_mmap_file);
c2c0f25c
AM
953
954 #ifdef CONFIG_SECURITY
955
7f207e10 956diff --git a/security/device_cgroup.c b/security/device_cgroup.c
febd17d6 957index 03c1652..f88c84b 100644
7f207e10
AM
958--- a/security/device_cgroup.c
959+++ b/security/device_cgroup.c
f6c5ef8b
AM
960@@ -7,6 +7,7 @@
961 #include <linux/device_cgroup.h>
962 #include <linux/cgroup.h>
963 #include <linux/ctype.h>
964+#include <linux/export.h>
965 #include <linux/list.h>
966 #include <linux/uaccess.h>
967 #include <linux/seq_file.h>
076b876e 968@@ -849,6 +850,7 @@ int __devcgroup_inode_permission(struct inode *inode, int mask)
537831f9
AM
969 return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
970 access);
7f207e10 971 }
febd17d6 972+EXPORT_SYMBOL_GPL(__devcgroup_inode_permission);
7f207e10
AM
973
974 int devcgroup_inode_mknod(int mode, dev_t dev)
975 {
976diff --git a/security/security.c b/security/security.c
5afbbe0d 977index 7095693..da7fe2c 100644
7f207e10
AM
978--- a/security/security.c
979+++ b/security/security.c
5afbbe0d 980@@ -434,6 +434,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
7f207e10 981 return 0;
c2c0f25c 982 return call_int_hook(path_rmdir, 0, dir, dentry);
7f207e10 983 }
febd17d6 984+EXPORT_SYMBOL_GPL(security_path_rmdir);
7f207e10 985
5afbbe0d 986 int security_path_unlink(const struct path *dir, struct dentry *dentry)
7f207e10 987 {
5afbbe0d 988@@ -450,6 +451,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
7f207e10 989 return 0;
c2c0f25c 990 return call_int_hook(path_symlink, 0, dir, dentry, old_name);
7f207e10 991 }
febd17d6 992+EXPORT_SYMBOL_GPL(security_path_symlink);
7f207e10 993
5afbbe0d 994 int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 995 struct dentry *new_dentry)
5afbbe0d 996@@ -458,6 +460,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
7f207e10 997 return 0;
c2c0f25c 998 return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
7f207e10 999 }
febd17d6 1000+EXPORT_SYMBOL_GPL(security_path_link);
7f207e10 1001
5afbbe0d
AM
1002 int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
1003 const struct path *new_dir, struct dentry *new_dentry,
1004@@ -485,6 +488,7 @@ int security_path_truncate(const struct path *path)
7f207e10 1005 return 0;
c2c0f25c 1006 return call_int_hook(path_truncate, 0, path);
7f207e10 1007 }
febd17d6 1008+EXPORT_SYMBOL_GPL(security_path_truncate);
7f207e10 1009
5afbbe0d 1010 int security_path_chmod(const struct path *path, umode_t mode)
7eafdf33 1011 {
5afbbe0d 1012@@ -492,6 +496,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
7f207e10 1013 return 0;
c2c0f25c 1014 return call_int_hook(path_chmod, 0, path, mode);
7f207e10 1015 }
febd17d6 1016+EXPORT_SYMBOL_GPL(security_path_chmod);
7f207e10 1017
5afbbe0d 1018 int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1019 {
5afbbe0d 1020@@ -499,6 +504,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
7f207e10 1021 return 0;
c2c0f25c 1022 return call_int_hook(path_chown, 0, path, uid, gid);
7f207e10 1023 }
febd17d6 1024+EXPORT_SYMBOL_GPL(security_path_chown);
7f207e10 1025
5afbbe0d 1026 int security_path_chroot(const struct path *path)
7f207e10 1027 {
5afbbe0d 1028@@ -584,6 +590,7 @@ int security_inode_readlink(struct dentry *dentry)
7f207e10 1029 return 0;
c2c0f25c 1030 return call_int_hook(inode_readlink, 0, dentry);
7f207e10 1031 }
febd17d6 1032+EXPORT_SYMBOL_GPL(security_inode_readlink);
7f207e10 1033
c2c0f25c
AM
1034 int security_inode_follow_link(struct dentry *dentry, struct inode *inode,
1035 bool rcu)
5afbbe0d 1036@@ -599,6 +606,7 @@ int security_inode_permission(struct inode *inode, int mask)
7f207e10 1037 return 0;
c2c0f25c 1038 return call_int_hook(inode_permission, 0, inode, mask);
7f207e10 1039 }
febd17d6 1040+EXPORT_SYMBOL_GPL(security_inode_permission);
7f207e10 1041
1e00d052 1042 int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
7f207e10 1043 {
5afbbe0d 1044@@ -737,6 +745,7 @@ int security_file_permission(struct file *file, int mask)
7f207e10
AM
1045
1046 return fsnotify_perm(file, mask);
1047 }
febd17d6 1048+EXPORT_SYMBOL_GPL(security_file_permission);
7f207e10
AM
1049
1050 int security_file_alloc(struct file *file)
1051 {
5afbbe0d 1052@@ -796,6 +805,7 @@ int security_mmap_file(struct file *file, unsigned long prot,
7f207e10
AM
1053 return ret;
1054 return ima_file_mmap(file, prot);
1055 }
febd17d6 1056+EXPORT_SYMBOL_GPL(security_mmap_file);
7f207e10 1057
0c3ec466
AM
1058 int security_mmap_addr(unsigned long addr)
1059 {
7f207e10
AM
1060diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
1061--- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1062+++ linux/Documentation/ABI/testing/debugfs-aufs 2015-09-24 10:47:58.244719488 +0200
86dc4139 1063@@ -0,0 +1,50 @@
7f207e10
AM
1064+What: /debug/aufs/si_<id>/
1065+Date: March 2009
f6b6e03d 1066+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1067+Description:
1068+ Under /debug/aufs, a directory named si_<id> is created
1069+ per aufs mount, where <id> is a unique id generated
1070+ internally.
1facf9fc 1071+
86dc4139
AM
1072+What: /debug/aufs/si_<id>/plink
1073+Date: Apr 2013
f6b6e03d 1074+Contact: J. R. Okajima <hooanon05g@gmail.com>
86dc4139
AM
1075+Description:
1076+ It has three lines and shows the information about the
1077+ pseudo-link. The first line is a single number
1078+ representing a number of buckets. The second line is a
1079+ number of pseudo-links per buckets (separated by a
1080+ blank). The last line is a single number representing a
1081+ total number of psedo-links.
1082+ When the aufs mount option 'noplink' is specified, it
1083+ will show "1\n0\n0\n".
1084+
7f207e10
AM
1085+What: /debug/aufs/si_<id>/xib
1086+Date: March 2009
f6b6e03d 1087+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1088+Description:
1089+ It shows the consumed blocks by xib (External Inode Number
1090+ Bitmap), its block size and file size.
1091+ When the aufs mount option 'noxino' is specified, it
1092+ will be empty. About XINO files, see the aufs manual.
1093+
1094+What: /debug/aufs/si_<id>/xino0, xino1 ... xinoN
1095+Date: March 2009
f6b6e03d 1096+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1097+Description:
1098+ It shows the consumed blocks by xino (External Inode Number
1099+ Translation Table), its link count, block size and file
1100+ size.
1101+ When the aufs mount option 'noxino' is specified, it
1102+ will be empty. About XINO files, see the aufs manual.
1103+
1104+What: /debug/aufs/si_<id>/xigen
1105+Date: March 2009
f6b6e03d 1106+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1107+Description:
1108+ It shows the consumed blocks by xigen (External Inode
1109+ Generation Table), its block size and file size.
1110+ If CONFIG_AUFS_EXPORT is disabled, this entry will not
1111+ be created.
1112+ When the aufs mount option 'noxino' is specified, it
1113+ will be empty. About XINO files, see the aufs manual.
1114diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1115--- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1116+++ linux/Documentation/ABI/testing/sysfs-aufs 2015-09-24 10:47:58.244719488 +0200
392086de 1117@@ -0,0 +1,31 @@
7f207e10
AM
1118+What: /sys/fs/aufs/si_<id>/
1119+Date: March 2009
f6b6e03d 1120+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1121+Description:
1122+ Under /sys/fs/aufs, a directory named si_<id> is created
1123+ per aufs mount, where <id> is a unique id generated
1124+ internally.
1125+
1126+What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
1127+Date: March 2009
f6b6e03d 1128+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1129+Description:
1130+ It shows the abolute path of a member directory (which
1131+ is called branch) in aufs, and its permission.
1132+
392086de
AM
1133+What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
1134+Date: July 2013
f6b6e03d 1135+Contact: J. R. Okajima <hooanon05g@gmail.com>
392086de
AM
1136+Description:
1137+ It shows the id of a member directory (which is called
1138+ branch) in aufs.
1139+
7f207e10
AM
1140+What: /sys/fs/aufs/si_<id>/xi_path
1141+Date: March 2009
f6b6e03d 1142+Contact: J. R. Okajima <hooanon05g@gmail.com>
7f207e10
AM
1143+Description:
1144+ It shows the abolute path of XINO (External Inode Number
1145+ Bitmap, Translation Table and Generation Table) file
1146+ even if it is the default path.
1147+ When the aufs mount option 'noxino' is specified, it
1148+ will be empty. About XINO files, see the aufs manual.
53392da6
AM
1149diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1150--- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1151+++ linux/Documentation/filesystems/aufs/design/01intro.txt 2016-07-25 19:05:34.807826401 +0200
7e9cd9fe 1152@@ -0,0 +1,170 @@
53392da6 1153+
8cdd5066 1154+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
1155+#
1156+# This program is free software; you can redistribute it and/or modify
1157+# it under the terms of the GNU General Public License as published by
1158+# the Free Software Foundation; either version 2 of the License, or
1159+# (at your option) any later version.
1160+#
1161+# This program is distributed in the hope that it will be useful,
1162+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1163+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1164+# GNU General Public License for more details.
1165+#
1166+# You should have received a copy of the GNU General Public License
523b37e3 1167+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1168+
1169+Introduction
1170+----------------------------------------
1171+
1172+aufs [ei ju: ef es] | [a u f s]
1173+1. abbrev. for "advanced multi-layered unification filesystem".
1174+2. abbrev. for "another unionfs".
1175+3. abbrev. for "auf das" in German which means "on the" in English.
1176+ Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1177+ But "Filesystem aufs Filesystem" is hard to understand.
1178+
1179+AUFS is a filesystem with features:
1180+- multi layered stackable unification filesystem, the member directory
1181+ is called as a branch.
1182+- branch permission and attribute, 'readonly', 'real-readonly',
7e9cd9fe 1183+ 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
53392da6
AM
1184+ combination.
1185+- internal "file copy-on-write".
1186+- logical deletion, whiteout.
1187+- dynamic branch manipulation, adding, deleting and changing permission.
1188+- allow bypassing aufs, user's direct branch access.
1189+- external inode number translation table and bitmap which maintains the
1190+ persistent aufs inode number.
1191+- seekable directory, including NFS readdir.
1192+- file mapping, mmap and sharing pages.
1193+- pseudo-link, hardlink over branches.
1194+- loopback mounted filesystem as a branch.
1195+- several policies to select one among multiple writable branches.
1196+- revert a single systemcall when an error occurs in aufs.
1197+- and more...
1198+
1199+
1200+Multi Layered Stackable Unification Filesystem
1201+----------------------------------------------------------------------
1202+Most people already knows what it is.
1203+It is a filesystem which unifies several directories and provides a
1204+merged single directory. When users access a file, the access will be
1205+passed/re-directed/converted (sorry, I am not sure which English word is
1206+correct) to the real file on the member filesystem. The member
1207+filesystem is called 'lower filesystem' or 'branch' and has a mode
1208+'readonly' and 'readwrite.' And the deletion for a file on the lower
1209+readonly branch is handled by creating 'whiteout' on the upper writable
1210+branch.
1211+
1212+On LKML, there have been discussions about UnionMount (Jan Blunck,
1213+Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1214+different approaches to implement the merged-view.
1215+The former tries putting it into VFS, and the latter implements as a
1216+separate filesystem.
1217+(If I misunderstand about these implementations, please let me know and
1218+I shall correct it. Because it is a long time ago when I read their
1219+source files last time).
1220+
1221+UnionMount's approach will be able to small, but may be hard to share
1222+branches between several UnionMount since the whiteout in it is
1223+implemented in the inode on branch filesystem and always
1224+shared. According to Bharata's post, readdir does not seems to be
1225+finished yet.
1226+There are several missing features known in this implementations such as
1227+- for users, the inode number may change silently. eg. copy-up.
1228+- link(2) may break by copy-up.
1229+- read(2) may get an obsoleted filedata (fstat(2) too).
1230+- fcntl(F_SETLK) may be broken by copy-up.
1231+- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1232+ open(O_RDWR).
1233+
7e9cd9fe
AM
1234+In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1235+merged into mainline. This is another implementation of UnionMount as a
1236+separated filesystem. All the limitations and known problems which
1237+UnionMount are equally inherited to "overlay" filesystem.
1238+
1239+Unionfs has a longer history. When I started implementing a stackable
1240+filesystem (Aug 2005), it already existed. It has virtual super_block,
1241+inode, dentry and file objects and they have an array pointing lower
1242+same kind objects. After contributing many patches for Unionfs, I
1243+re-started my project AUFS (Jun 2006).
53392da6
AM
1244+
1245+In AUFS, the structure of filesystem resembles to Unionfs, but I
1246+implemented my own ideas, approaches and enhancements and it became
1247+totally different one.
1248+
1249+Comparing DM snapshot and fs based implementation
1250+- the number of bytes to be copied between devices is much smaller.
1251+- the type of filesystem must be one and only.
1252+- the fs must be writable, no readonly fs, even for the lower original
1253+ device. so the compression fs will not be usable. but if we use
1254+ loopback mount, we may address this issue.
1255+ for instance,
1256+ mount /cdrom/squashfs.img /sq
1257+ losetup /sq/ext2.img
1258+ losetup /somewhere/cow
1259+ dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1260+- it will be difficult (or needs more operations) to extract the
1261+ difference between the original device and COW.
1262+- DM snapshot-merge may help a lot when users try merging. in the
1263+ fs-layer union, users will use rsync(1).
1264+
7e9cd9fe
AM
1265+You may want to read my old paper "Filesystems in LiveCD"
1266+(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
53392da6 1267+
7e9cd9fe
AM
1268+
1269+Several characters/aspects/persona of aufs
53392da6
AM
1270+----------------------------------------------------------------------
1271+
7e9cd9fe 1272+Aufs has several characters, aspects or persona.
53392da6
AM
1273+1. a filesystem, callee of VFS helper
1274+2. sub-VFS, caller of VFS helper for branches
1275+3. a virtual filesystem which maintains persistent inode number
1276+4. reader/writer of files on branches such like an application
1277+
1278+1. Callee of VFS Helper
1279+As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1280+unlink(2) from an application reaches sys_unlink() kernel function and
1281+then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1282+calls filesystem specific unlink operation. Actually aufs implements the
1283+unlink operation but it behaves like a redirector.
1284+
1285+2. Caller of VFS Helper for Branches
1286+aufs_unlink() passes the unlink request to the branch filesystem as if
1287+it were called from VFS. So the called unlink operation of the branch
1288+filesystem acts as usual. As a caller of VFS helper, aufs should handle
1289+every necessary pre/post operation for the branch filesystem.
1290+- acquire the lock for the parent dir on a branch
1291+- lookup in a branch
1292+- revalidate dentry on a branch
1293+- mnt_want_write() for a branch
1294+- vfs_unlink() for a branch
1295+- mnt_drop_write() for a branch
1296+- release the lock on a branch
1297+
1298+3. Persistent Inode Number
1299+One of the most important issue for a filesystem is to maintain inode
1300+numbers. This is particularly important to support exporting a
1301+filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1302+backend block device for its own. But some storage is necessary to
7e9cd9fe
AM
1303+keep and maintain the inode numbers. It may be a large space and may not
1304+suit to keep in memory. Aufs rents some space from its first writable
1305+branch filesystem (by default) and creates file(s) on it. These files
1306+are created by aufs internally and removed soon (currently) keeping
1307+opened.
53392da6
AM
1308+Note: Because these files are removed, they are totally gone after
1309+ unmounting aufs. It means the inode numbers are not persistent
1310+ across unmount or reboot. I have a plan to make them really
1311+ persistent which will be important for aufs on NFS server.
1312+
1313+4. Read/Write Files Internally (copy-on-write)
1314+Because a branch can be readonly, when you write a file on it, aufs will
1315+"copy-up" it to the upper writable branch internally. And then write the
1316+originally requested thing to the file. Generally kernel doesn't
1317+open/read/write file actively. In aufs, even a single write may cause a
1318+internal "file copy". This behaviour is very similar to cp(1) command.
1319+
1320+Some people may think it is better to pass such work to user space
1321+helper, instead of doing in kernel space. Actually I am still thinking
1322+about it. But currently I have implemented it in kernel space.
1323diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1324--- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1325+++ linux/Documentation/filesystems/aufs/design/02struct.txt 2016-07-25 19:05:34.807826401 +0200
7e9cd9fe 1326@@ -0,0 +1,258 @@
53392da6 1327+
8cdd5066 1328+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
1329+#
1330+# This program is free software; you can redistribute it and/or modify
1331+# it under the terms of the GNU General Public License as published by
1332+# the Free Software Foundation; either version 2 of the License, or
1333+# (at your option) any later version.
1334+#
1335+# This program is distributed in the hope that it will be useful,
1336+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1337+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1338+# GNU General Public License for more details.
1339+#
1340+# You should have received a copy of the GNU General Public License
523b37e3 1341+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1342+
1343+Basic Aufs Internal Structure
1344+
1345+Superblock/Inode/Dentry/File Objects
1346+----------------------------------------------------------------------
1347+As like an ordinary filesystem, aufs has its own
1348+superblock/inode/dentry/file objects. All these objects have a
1349+dynamically allocated array and store the same kind of pointers to the
1350+lower filesystem, branch.
1351+For example, when you build a union with one readwrite branch and one
1352+readonly, mounted /au, /rw and /ro respectively.
1353+- /au = /rw + /ro
1354+- /ro/fileA exists but /rw/fileA
1355+
1356+Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1357+pointers are stored in a aufs dentry. The array in aufs dentry will be,
7e9cd9fe 1358+- [0] = NULL (because /rw/fileA doesn't exist)
53392da6
AM
1359+- [1] = /ro/fileA
1360+
1361+This style of an array is essentially same to the aufs
1362+superblock/inode/dentry/file objects.
1363+
1364+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1365+branches dynamically, these objects has its own generation. When
1366+branches are changed, the generation in aufs superblock is
1367+incremented. And a generation in other object are compared when it is
1368+accessed. When a generation in other objects are obsoleted, aufs
1369+refreshes the internal array.
53392da6
AM
1370+
1371+
1372+Superblock
1373+----------------------------------------------------------------------
1374+Additionally aufs superblock has some data for policies to select one
1375+among multiple writable branches, XIB files, pseudo-links and kobject.
1376+See below in detail.
7e9cd9fe
AM
1377+About the policies which supports copy-down a directory, see
1378+wbr_policy.txt too.
53392da6
AM
1379+
1380+
1381+Branch and XINO(External Inode Number Translation Table)
1382+----------------------------------------------------------------------
1383+Every branch has its own xino (external inode number translation table)
1384+file. The xino file is created and unlinked by aufs internally. When two
1385+members of a union exist on the same filesystem, they share the single
1386+xino file.
1387+The struct of a xino file is simple, just a sequence of aufs inode
1388+numbers which is indexed by the lower inode number.
1389+In the above sample, assume the inode number of /ro/fileA is i111 and
1390+aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1391+4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1392+
1393+When the inode numbers are not contiguous, the xino file will be sparse
1394+which has a hole in it and doesn't consume as much disk space as it
1395+might appear. If your branch filesystem consumes disk space for such
1396+holes, then you should specify 'xino=' option at mounting aufs.
1397+
7e9cd9fe
AM
1398+Aufs has a mount option to free the disk blocks for such holes in XINO
1399+files on tmpfs or ramdisk. But it is not so effective actually. If you
1400+meet a problem of disk shortage due to XINO files, then you should try
1401+"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1402+The patch localizes the assignment inumbers per tmpfs-mount and avoid
1403+the holes in XINO files.
1404+
53392da6 1405+Also a writable branch has three kinds of "whiteout bases". All these
7e9cd9fe 1406+are existed when the branch is joined to aufs, and their names are
53392da6
AM
1407+whiteout-ed doubly, so that users will never see their names in aufs
1408+hierarchy.
7e9cd9fe 1409+1. a regular file which will be hardlinked to all whiteouts.
53392da6 1410+2. a directory to store a pseudo-link.
7e9cd9fe 1411+3. a directory to store an "orphan"-ed file temporary.
53392da6
AM
1412+
1413+1. Whiteout Base
1414+ When you remove a file on a readonly branch, aufs handles it as a
1415+ logical deletion and creates a whiteout on the upper writable branch
1416+ as a hardlink of this file in order not to consume inode on the
1417+ writable branch.
1418+2. Pseudo-link Dir
1419+ See below, Pseudo-link.
1420+3. Step-Parent Dir
1421+ When "fileC" exists on the lower readonly branch only and it is
1422+ opened and removed with its parent dir, and then user writes
1423+ something into it, then aufs copies-up fileC to this
1424+ directory. Because there is no other dir to store fileC. After
1425+ creating a file under this dir, the file is unlinked.
1426+
1427+Because aufs supports manipulating branches, ie. add/delete/change
7e9cd9fe
AM
1428+dynamically, a branch has its own id. When the branch order changes,
1429+aufs finds the new index by searching the branch id.
53392da6
AM
1430+
1431+
1432+Pseudo-link
1433+----------------------------------------------------------------------
1434+Assume "fileA" exists on the lower readonly branch only and it is
1435+hardlinked to "fileB" on the branch. When you write something to fileA,
1436+aufs copies-up it to the upper writable branch. Additionally aufs
1437+creates a hardlink under the Pseudo-link Directory of the writable
1438+branch. The inode of a pseudo-link is kept in aufs super_block as a
1439+simple list. If fileB is read after unlinking fileA, aufs returns
1440+filedata from the pseudo-link instead of the lower readonly
1441+branch. Because the pseudo-link is based upon the inode, to keep the
7e9cd9fe 1442+inode number by xino (see above) is essentially necessary.
53392da6
AM
1443+
1444+All the hardlinks under the Pseudo-link Directory of the writable branch
1445+should be restored in a proper location later. Aufs provides a utility
1446+to do this. The userspace helpers executed at remounting and unmounting
1447+aufs by default.
1448+During this utility is running, it puts aufs into the pseudo-link
1449+maintenance mode. In this mode, only the process which began the
1450+maintenance mode (and its child processes) is allowed to operate in
1451+aufs. Some other processes which are not related to the pseudo-link will
1452+be allowed to run too, but the rest have to return an error or wait
1453+until the maintenance mode ends. If a process already acquires an inode
1454+mutex (in VFS), it has to return an error.
1455+
1456+
1457+XIB(external inode number bitmap)
1458+----------------------------------------------------------------------
1459+Addition to the xino file per a branch, aufs has an external inode number
7e9cd9fe
AM
1460+bitmap in a superblock object. It is also an internal file such like a
1461+xino file.
53392da6
AM
1462+It is a simple bitmap to mark whether the aufs inode number is in-use or
1463+not.
1464+To reduce the file I/O, aufs prepares a single memory page to cache xib.
1465+
7e9cd9fe 1466+As well as XINO files, aufs has a feature to truncate/refresh XIB to
53392da6
AM
1467+reduce the number of consumed disk blocks for these files.
1468+
1469+
1470+Virtual or Vertical Dir, and Readdir in Userspace
1471+----------------------------------------------------------------------
1472+In order to support multiple layers (branches), aufs readdir operation
1473+constructs a virtual dir block on memory. For readdir, aufs calls
1474+vfs_readdir() internally for each dir on branches, merges their entries
1475+with eliminating the whiteout-ed ones, and sets it to file (dir)
1476+object. So the file object has its entry list until it is closed. The
1477+entry list will be updated when the file position is zero and becomes
7e9cd9fe 1478+obsoleted. This decision is made in aufs automatically.
53392da6
AM
1479+
1480+The dynamically allocated memory block for the name of entries has a
1481+unit of 512 bytes (by default) and stores the names contiguously (no
1482+padding). Another block for each entry is handled by kmem_cache too.
1483+During building dir blocks, aufs creates hash list and judging whether
1484+the entry is whiteouted by its upper branch or already listed.
1485+The merged result is cached in the corresponding inode object and
1486+maintained by a customizable life-time option.
1487+
1488+Some people may call it can be a security hole or invite DoS attack
1489+since the opened and once readdir-ed dir (file object) holds its entry
1490+list and becomes a pressure for system memory. But I'd say it is similar
1491+to files under /proc or /sys. The virtual files in them also holds a
1492+memory page (generally) while they are opened. When an idea to reduce
1493+memory for them is introduced, it will be applied to aufs too.
1494+For those who really hate this situation, I've developed readdir(3)
1495+library which operates this merging in userspace. You just need to set
1496+LD_PRELOAD environment variable, and aufs will not consume no memory in
1497+kernel space for readdir(3).
1498+
1499+
1500+Workqueue
1501+----------------------------------------------------------------------
1502+Aufs sometimes requires privilege access to a branch. For instance,
1503+in copy-up/down operation. When a user process is going to make changes
1504+to a file which exists in the lower readonly branch only, and the mode
1505+of one of ancestor directories may not be writable by a user
1506+process. Here aufs copy-up the file with its ancestors and they may
1507+require privilege to set its owner/group/mode/etc.
1508+This is a typical case of a application character of aufs (see
1509+Introduction).
1510+
1511+Aufs uses workqueue synchronously for this case. It creates its own
1512+workqueue. The workqueue is a kernel thread and has privilege. Aufs
1513+passes the request to call mkdir or write (for example), and wait for
1514+its completion. This approach solves a problem of a signal handler
1515+simply.
1516+If aufs didn't adopt the workqueue and changed the privilege of the
7e9cd9fe
AM
1517+process, then the process may receive the unexpected SIGXFSZ or other
1518+signals.
53392da6
AM
1519+
1520+Also aufs uses the system global workqueue ("events" kernel thread) too
1521+for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1522+whiteout base and etc. This is unrelated to a privilege.
1523+Most of aufs operation tries acquiring a rw_semaphore for aufs
1524+superblock at the beginning, at the same time waits for the completion
1525+of all queued asynchronous tasks.
1526+
1527+
1528+Whiteout
1529+----------------------------------------------------------------------
1530+The whiteout in aufs is very similar to Unionfs's. That is represented
1531+by its filename. UnionMount takes an approach of a file mode, but I am
1532+afraid several utilities (find(1) or something) will have to support it.
1533+
1534+Basically the whiteout represents "logical deletion" which stops aufs to
1535+lookup further, but also it represents "dir is opaque" which also stop
7e9cd9fe 1536+further lookup.
53392da6
AM
1537+
1538+In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1539+In order to make several functions in a single systemcall to be
1540+revertible, aufs adopts an approach to rename a directory to a temporary
1541+unique whiteouted name.
1542+For example, in rename(2) dir where the target dir already existed, aufs
1543+renames the target dir to a temporary unique whiteouted name before the
7e9cd9fe 1544+actual rename on a branch, and then handles other actions (make it opaque,
53392da6
AM
1545+update the attributes, etc). If an error happens in these actions, aufs
1546+simply renames the whiteouted name back and returns an error. If all are
1547+succeeded, aufs registers a function to remove the whiteouted unique
1548+temporary name completely and asynchronously to the system global
1549+workqueue.
1550+
1551+
1552+Copy-up
1553+----------------------------------------------------------------------
1554+It is a well-known feature or concept.
1555+When user modifies a file on a readonly branch, aufs operate "copy-up"
1556+internally and makes change to the new file on the upper writable branch.
1557+When the trigger systemcall does not update the timestamps of the parent
1558+dir, aufs reverts it after copy-up.
c2b27bf2
AM
1559+
1560+
1561+Move-down (aufs3.9 and later)
1562+----------------------------------------------------------------------
1563+"Copy-up" is one of the essential feature in aufs. It copies a file from
1564+the lower readonly branch to the upper writable branch when a user
1565+changes something about the file.
1566+"Move-down" is an opposite action of copy-up. Basically this action is
1567+ran manually instead of automatically and internally.
076b876e
AM
1568+For desgin and implementation, aufs has to consider these issues.
1569+- whiteout for the file may exist on the lower branch.
1570+- ancestor directories may not exist on the lower branch.
1571+- diropq for the ancestor directories may exist on the upper branch.
1572+- free space on the lower branch will reduce.
1573+- another access to the file may happen during moving-down, including
7e9cd9fe 1574+ UDBA (see "Revalidate Dentry and UDBA").
076b876e
AM
1575+- the file should not be hard-linked nor pseudo-linked. they should be
1576+ handled by auplink utility later.
c2b27bf2
AM
1577+
1578+Sometimes users want to move-down a file from the upper writable branch
1579+to the lower readonly or writable branch. For instance,
1580+- the free space of the upper writable branch is going to run out.
1581+- create a new intermediate branch between the upper and lower branch.
1582+- etc.
1583+
1584+For this purpose, use "aumvdown" command in aufs-util.git.
b912730e
AM
1585diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1586--- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1587+++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt 2016-07-25 19:05:34.807826401 +0200
b912730e
AM
1588@@ -0,0 +1,85 @@
1589+
8cdd5066 1590+# Copyright (C) 2015-2016 Junjiro R. Okajima
b912730e
AM
1591+#
1592+# This program is free software; you can redistribute it and/or modify
1593+# it under the terms of the GNU General Public License as published by
1594+# the Free Software Foundation; either version 2 of the License, or
1595+# (at your option) any later version.
1596+#
1597+# This program is distributed in the hope that it will be useful,
1598+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1599+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1600+# GNU General Public License for more details.
1601+#
1602+# You should have received a copy of the GNU General Public License
1603+# along with this program. If not, see <http://www.gnu.org/licenses/>.
1604+
1605+Support for a branch who has its ->atomic_open()
1606+----------------------------------------------------------------------
1607+The filesystems who implement its ->atomic_open() are not majority. For
1608+example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1609+particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1610+->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1611+sure whether all filesystems who have ->atomic_open() behave like this,
1612+but NFSv4 surely returns the error.
1613+
1614+In order to support ->atomic_open() for aufs, there are a few
1615+approaches.
1616+
1617+A. Introduce aufs_atomic_open()
1618+ - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1619+ branch fs.
1620+B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1621+ an aufs user Pip Cet's approach
1622+ - calls aufs_create(), VFS finish_open() and notify_change().
1623+ - pass fake-mode to finish_open(), and then correct the mode by
1624+ notify_change().
1625+C. Extend aufs_open() to call branch fs's ->atomic_open()
1626+ - no aufs_atomic_open().
1627+ - aufs_lookup() registers the TID to an aufs internal object.
1628+ - aufs_create() does nothing when the matching TID is registered, but
1629+ registers the mode.
1630+ - aufs_open() calls branch fs's ->atomic_open() when the matching
1631+ TID is registered.
1632+D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1633+ credential
1634+ - no aufs_atomic_open().
1635+ - aufs_create() registers the TID to an internal object. this info
1636+ represents "this process created this file just now."
1637+ - when aufs gets EACCES from branch fs's ->open(), then confirm the
1638+ registered TID and re-try open() with superuser's credential.
1639+
1640+Pros and cons for each approach.
1641+
1642+A.
1643+ - straightforward but highly depends upon VFS internal.
1644+ - the atomic behavaiour is kept.
1645+ - some of parameters such as nameidata are hard to reproduce for
1646+ branch fs.
1647+ - large overhead.
1648+B.
1649+ - easy to implement.
1650+ - the atomic behavaiour is lost.
1651+C.
1652+ - the atomic behavaiour is kept.
1653+ - dirty and tricky.
1654+ - VFS checks whether the file is created correctly after calling
1655+ ->create(), which means this approach doesn't work.
1656+D.
1657+ - easy to implement.
1658+ - the atomic behavaiour is lost.
1659+ - to open a file with superuser's credential and give it to a user
1660+ process is a bad idea, since the file object keeps the credential
1661+ in it. It may affect LSM or something. This approach doesn't work
1662+ either.
1663+
1664+The approach A is ideal, but it hard to implement. So here is a
1665+variation of A, which is to be implemented.
1666+
1667+A-1. Introduce aufs_atomic_open()
1668+ - calls branch fs ->atomic_open() if exists. otherwise calls
1669+ vfs_create() and finish_open().
1670+ - the demerit is that the several checks after branch fs
1671+ ->atomic_open() are lost. in the ordinary case, the checks are
1672+ done by VFS:do_last(), lookup_open() and atomic_open(). some can
1673+ be implemented in aufs, but not all I am afraid.
53392da6
AM
1674diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1675--- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1676+++ linux/Documentation/filesystems/aufs/design/03lookup.txt 2016-07-25 19:05:34.807826401 +0200
7e9cd9fe 1677@@ -0,0 +1,113 @@
53392da6 1678+
8cdd5066 1679+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
1680+#
1681+# This program is free software; you can redistribute it and/or modify
1682+# it under the terms of the GNU General Public License as published by
1683+# the Free Software Foundation; either version 2 of the License, or
1684+# (at your option) any later version.
1685+#
1686+# This program is distributed in the hope that it will be useful,
1687+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1688+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1689+# GNU General Public License for more details.
1690+#
1691+# You should have received a copy of the GNU General Public License
523b37e3 1692+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1693+
1694+Lookup in a Branch
1695+----------------------------------------------------------------------
1696+Since aufs has a character of sub-VFS (see Introduction), it operates
7e9cd9fe
AM
1697+lookup for branches as VFS does. It may be a heavy work. But almost all
1698+lookup operation in aufs is the simplest case, ie. lookup only an entry
1699+directly connected to its parent. Digging down the directory hierarchy
1700+is unnecessary. VFS has a function lookup_one_len() for that use, and
1701+aufs calls it.
1702+
1703+When a branch is a remote filesystem, aufs basically relies upon its
53392da6
AM
1704+->d_revalidate(), also aufs forces the hardest revalidate tests for
1705+them.
1706+For d_revalidate, aufs implements three levels of revalidate tests. See
1707+"Revalidate Dentry and UDBA" in detail.
1708+
1709+
076b876e
AM
1710+Test Only the Highest One for the Directory Permission (dirperm1 option)
1711+----------------------------------------------------------------------
1712+Let's try case study.
1713+- aufs has two branches, upper readwrite and lower readonly.
1714+ /au = /rw + /ro
1715+- "dirA" exists under /ro, but /rw. and its mode is 0700.
1716+- user invoked "chmod a+rx /au/dirA"
1717+- the internal copy-up is activated and "/rw/dirA" is created and its
7e9cd9fe 1718+ permission bits are set to world readable.
076b876e
AM
1719+- then "/au/dirA" becomes world readable?
1720+
1721+In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1722+or it may be a natively readonly filesystem. If aufs respects the lower
1723+branch, it should not respond readdir request from other users. But user
1724+allowed it by chmod. Should really aufs rejects showing the entries
1725+under /ro/dirA?
1726+
7e9cd9fe
AM
1727+To be honest, I don't have a good solution for this case. So aufs
1728+implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1729+users.
076b876e
AM
1730+When dirperm1 is specified, aufs checks only the highest one for the
1731+directory permission, and shows the entries. Otherwise, as usual, checks
1732+every dir existing on all branches and rejects the request.
1733+
1734+As a side effect, dirperm1 option improves the performance of aufs
1735+because the number of permission check is reduced when the number of
1736+branch is many.
1737+
1738+
53392da6
AM
1739+Revalidate Dentry and UDBA (User's Direct Branch Access)
1740+----------------------------------------------------------------------
1741+Generally VFS helpers re-validate a dentry as a part of lookup.
1742+0. digging down the directory hierarchy.
1743+1. lock the parent dir by its i_mutex.
1744+2. lookup the final (child) entry.
1745+3. revalidate it.
1746+4. call the actual operation (create, unlink, etc.)
1747+5. unlock the parent dir
1748+
1749+If the filesystem implements its ->d_revalidate() (step 3), then it is
1750+called. Actually aufs implements it and checks the dentry on a branch is
1751+still valid.
1752+But it is not enough. Because aufs has to release the lock for the
1753+parent dir on a branch at the end of ->lookup() (step 2) and
1754+->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1755+held by VFS.
1756+If the file on a branch is changed directly, eg. bypassing aufs, after
1757+aufs released the lock, then the subsequent operation may cause
1758+something unpleasant result.
1759+
1760+This situation is a result of VFS architecture, ->lookup() and
1761+->d_revalidate() is separated. But I never say it is wrong. It is a good
1762+design from VFS's point of view. It is just not suitable for sub-VFS
1763+character in aufs.
1764+
1765+Aufs supports such case by three level of revalidation which is
1766+selectable by user.
1767+1. Simple Revalidate
1768+ Addition to the native flow in VFS's, confirm the child-parent
1769+ relationship on the branch just after locking the parent dir on the
1770+ branch in the "actual operation" (step 4). When this validation
1771+ fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1772+ checks the validation of the dentry on branches.
1773+2. Monitor Changes Internally by Inotify/Fsnotify
1774+ Addition to above, in the "actual operation" (step 4) aufs re-lookup
1775+ the dentry on the branch, and returns EBUSY if it finds different
1776+ dentry.
1777+ Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1778+ during it is in cache. When the event is notified, aufs registers a
1779+ function to kernel 'events' thread by schedule_work(). And the
1780+ function sets some special status to the cached aufs dentry and inode
1781+ private data. If they are not cached, then aufs has nothing to
1782+ do. When the same file is accessed through aufs (step 0-3) later,
1783+ aufs will detect the status and refresh all necessary data.
1784+ In this mode, aufs has to ignore the event which is fired by aufs
1785+ itself.
1786+3. No Extra Validation
1787+ This is the simplest test and doesn't add any additional revalidation
7e9cd9fe 1788+ test, and skip the revalidation in step 4. It is useful and improves
53392da6
AM
1789+ aufs performance when system surely hide the aufs branches from user,
1790+ by over-mounting something (or another method).
1791diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1792--- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1793+++ linux/Documentation/filesystems/aufs/design/04branch.txt 2016-07-25 19:05:34.807826401 +0200
7e9cd9fe 1794@@ -0,0 +1,74 @@
53392da6 1795+
8cdd5066 1796+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
1797+#
1798+# This program is free software; you can redistribute it and/or modify
1799+# it under the terms of the GNU General Public License as published by
1800+# the Free Software Foundation; either version 2 of the License, or
1801+# (at your option) any later version.
1802+#
1803+# This program is distributed in the hope that it will be useful,
1804+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1805+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1806+# GNU General Public License for more details.
1807+#
1808+# You should have received a copy of the GNU General Public License
523b37e3 1809+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1810+
1811+Branch Manipulation
1812+
1813+Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1814+and changing its permission/attribute, there are a lot of works to do.
1815+
1816+
1817+Add a Branch
1818+----------------------------------------------------------------------
1819+o Confirm the adding dir exists outside of aufs, including loopback
7e9cd9fe 1820+ mount, and its various attributes.
53392da6
AM
1821+o Initialize the xino file and whiteout bases if necessary.
1822+ See struct.txt.
1823+
1824+o Check the owner/group/mode of the directory
1825+ When the owner/group/mode of the adding directory differs from the
1826+ existing branch, aufs issues a warning because it may impose a
1827+ security risk.
1828+ For example, when a upper writable branch has a world writable empty
1829+ top directory, a malicious user can create any files on the writable
1830+ branch directly, like copy-up and modify manually. If something like
1831+ /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1832+ writable branch, and the writable branch is world-writable, then a
1833+ malicious guy may create /etc/passwd on the writable branch directly
1834+ and the infected file will be valid in aufs.
7e9cd9fe 1835+ I am afraid it can be a security issue, but aufs can do nothing except
53392da6
AM
1836+ producing a warning.
1837+
1838+
1839+Delete a Branch
1840+----------------------------------------------------------------------
1841+o Confirm the deleting branch is not busy
1842+ To be general, there is one merit to adopt "remount" interface to
1843+ manipulate branches. It is to discard caches. At deleting a branch,
1844+ aufs checks the still cached (and connected) dentries and inodes. If
1845+ there are any, then they are all in-use. An inode without its
1846+ corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1847+
1848+ For the cached one, aufs checks whether the same named entry exists on
1849+ other branches.
1850+ If the cached one is a directory, because aufs provides a merged view
1851+ to users, as long as one dir is left on any branch aufs can show the
1852+ dir to users. In this case, the branch can be removed from aufs.
1853+ Otherwise aufs rejects deleting the branch.
1854+
1855+ If any file on the deleting branch is opened by aufs, then aufs
1856+ rejects deleting.
1857+
1858+
1859+Modify the Permission of a Branch
1860+----------------------------------------------------------------------
1861+o Re-initialize or remove the xino file and whiteout bases if necessary.
1862+ See struct.txt.
1863+
1864+o rw --> ro: Confirm the modifying branch is not busy
1865+ Aufs rejects the request if any of these conditions are true.
1866+ - a file on the branch is mmap-ed.
1867+ - a regular file on the branch is opened for write and there is no
1868+ same named entry on the upper branch.
1869diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1870--- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1871+++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt 2016-07-25 19:05:34.807826401 +0200
523b37e3 1872@@ -0,0 +1,64 @@
53392da6 1873+
8cdd5066 1874+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
1875+#
1876+# This program is free software; you can redistribute it and/or modify
1877+# it under the terms of the GNU General Public License as published by
1878+# the Free Software Foundation; either version 2 of the License, or
1879+# (at your option) any later version.
1880+#
1881+# This program is distributed in the hope that it will be useful,
1882+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1883+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1884+# GNU General Public License for more details.
1885+#
1886+# You should have received a copy of the GNU General Public License
523b37e3 1887+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
1888+
1889+Policies to Select One among Multiple Writable Branches
1890+----------------------------------------------------------------------
1891+When the number of writable branch is more than one, aufs has to decide
1892+the target branch for file creation or copy-up. By default, the highest
1893+writable branch which has the parent (or ancestor) dir of the target
1894+file is chosen (top-down-parent policy).
1895+By user's request, aufs implements some other policies to select the
7e9cd9fe
AM
1896+writable branch, for file creation several policies, round-robin,
1897+most-free-space, and other policies. For copy-up, top-down-parent,
1898+bottom-up-parent, bottom-up and others.
53392da6
AM
1899+
1900+As expected, the round-robin policy selects the branch in circular. When
1901+you have two writable branches and creates 10 new files, 5 files will be
1902+created for each branch. mkdir(2) systemcall is an exception. When you
1903+create 10 new directories, all will be created on the same branch.
1904+And the most-free-space policy selects the one which has most free
1905+space among the writable branches. The amount of free space will be
1906+checked by aufs internally, and users can specify its time interval.
1907+
1908+The policies for copy-up is more simple,
1909+top-down-parent is equivalent to the same named on in create policy,
1910+bottom-up-parent selects the writable branch where the parent dir
1911+exists and the nearest upper one from the copyup-source,
1912+bottom-up selects the nearest upper writable branch from the
1913+copyup-source, regardless the existence of the parent dir.
1914+
1915+There are some rules or exceptions to apply these policies.
1916+- If there is a readonly branch above the policy-selected branch and
1917+ the parent dir is marked as opaque (a variation of whiteout), or the
1918+ target (creating) file is whiteout-ed on the upper readonly branch,
1919+ then the result of the policy is ignored and the target file will be
1920+ created on the nearest upper writable branch than the readonly branch.
1921+- If there is a writable branch above the policy-selected branch and
1922+ the parent dir is marked as opaque or the target file is whiteouted
1923+ on the branch, then the result of the policy is ignored and the target
1924+ file will be created on the highest one among the upper writable
1925+ branches who has diropq or whiteout. In case of whiteout, aufs removes
1926+ it as usual.
1927+- link(2) and rename(2) systemcalls are exceptions in every policy.
1928+ They try selecting the branch where the source exists as possible
1929+ since copyup a large file will take long time. If it can't be,
1930+ ie. the branch where the source exists is readonly, then they will
1931+ follow the copyup policy.
1932+- There is an exception for rename(2) when the target exists.
1933+ If the rename target exists, aufs compares the index of the branches
1934+ where the source and the target exists and selects the higher
1935+ one. If the selected branch is readonly, then aufs follows the
1936+ copyup policy.
076b876e
AM
1937diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1938--- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 1939+++ linux/Documentation/filesystems/aufs/design/06fhsm.txt 2016-07-25 19:05:34.807826401 +0200
076b876e
AM
1940@@ -0,0 +1,120 @@
1941+
8cdd5066 1942+# Copyright (C) 2011-2016 Junjiro R. Okajima
076b876e
AM
1943+#
1944+# This program is free software; you can redistribute it and/or modify
1945+# it under the terms of the GNU General Public License as published by
1946+# the Free Software Foundation; either version 2 of the License, or
1947+# (at your option) any later version.
1948+#
1949+# This program is distributed in the hope that it will be useful,
1950+# but WITHOUT ANY WARRANTY; without even the implied warranty of
1951+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1952+# GNU General Public License for more details.
1953+#
1954+# You should have received a copy of the GNU General Public License
1955+# along with this program; if not, write to the Free Software
1956+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1957+
1958+
1959+File-based Hierarchical Storage Management (FHSM)
1960+----------------------------------------------------------------------
1961+Hierarchical Storage Management (or HSM) is a well-known feature in the
1962+storage world. Aufs provides this feature as file-based with multiple
7e9cd9fe 1963+writable branches, based upon the principle of "Colder, the Lower".
076b876e 1964+Here the word "colder" means that the less used files, and "lower" means
7e9cd9fe 1965+that the position in the order of the stacked branches vertically.
076b876e
AM
1966+These multiple writable branches are prioritized, ie. the topmost one
1967+should be the fastest drive and be used heavily.
1968+
1969+o Characters in aufs FHSM story
1970+- aufs itself and a new branch attribute.
1971+- a new ioctl interface to move-down and to establish a connection with
1972+ the daemon ("move-down" is a converse of "copy-up").
1973+- userspace tool and daemon.
1974+
1975+The userspace daemon establishes a connection with aufs and waits for
1976+the notification. The notified information is very similar to struct
1977+statfs containing the number of consumed blocks and inodes.
1978+When the consumed blocks/inodes of a branch exceeds the user-specified
1979+upper watermark, the daemon activates its move-down process until the
1980+consumed blocks/inodes reaches the user-specified lower watermark.
1981+
1982+The actual move-down is done by aufs based upon the request from
1983+user-space since we need to maintain the inode number and the internal
1984+pointer arrays in aufs.
1985+
1986+Currently aufs FHSM handles the regular files only. Additionally they
1987+must not be hard-linked nor pseudo-linked.
1988+
1989+
1990+o Cowork of aufs and the user-space daemon
1991+ During the userspace daemon established the connection, aufs sends a
1992+ small notification to it whenever aufs writes something into the
1993+ writable branch. But it may cost high since aufs issues statfs(2)
1994+ internally. So user can specify a new option to cache the
1995+ info. Actually the notification is controlled by these factors.
1996+ + the specified cache time.
1997+ + classified as "force" by aufs internally.
1998+ Until the specified time expires, aufs doesn't send the info
1999+ except the forced cases. When aufs decide forcing, the info is always
2000+ notified to userspace.
2001+ For example, the number of free inodes is generally large enough and
2002+ the shortage of it happens rarely. So aufs doesn't force the
2003+ notification when creating a new file, directory and others. This is
2004+ the typical case which aufs doesn't force.
2005+ When aufs writes the actual filedata and the files consumes any of new
2006+ blocks, the aufs forces notifying.
2007+
2008+
2009+o Interfaces in aufs
2010+- New branch attribute.
2011+ + fhsm
2012+ Specifies that the branch is managed by FHSM feature. In other word,
2013+ participant in the FHSM.
2014+ When nofhsm is set to the branch, it will not be the source/target
2015+ branch of the move-down operation. This attribute is set
2016+ independently from coo and moo attributes, and if you want full
2017+ FHSM, you should specify them as well.
2018+- New mount option.
2019+ + fhsm_sec
2020+ Specifies a second to suppress many less important info to be
2021+ notified.
2022+- New ioctl.
2023+ + AUFS_CTL_FHSM_FD
2024+ create a new file descriptor which userspace can read the notification
2025+ (a subset of struct statfs) from aufs.
2026+- Module parameter 'brs'
2027+ It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2028+ be set.
2029+- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2030+ When there are two or more branches with fhsm attributes,
2031+ /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2032+ terminates it. As a result of remounting and branch-manipulation, the
2033+ number of branches with fhsm attribute can be one. In this case,
2034+ /sbin/mount.aufs will terminate the user-space daemon.
2035+
2036+
2037+Finally the operation is done as these steps in kernel-space.
2038+- make sure that,
2039+ + no one else is using the file.
2040+ + the file is not hard-linked.
2041+ + the file is not pseudo-linked.
2042+ + the file is a regular file.
2043+ + the parent dir is not opaqued.
2044+- find the target writable branch.
2045+- make sure the file is not whiteout-ed by the upper (than the target)
2046+ branch.
2047+- make the parent dir on the target branch.
2048+- mutex lock the inode on the branch.
2049+- unlink the whiteout on the target branch (if exists).
2050+- lookup and create the whiteout-ed temporary name on the target branch.
2051+- copy the file as the whiteout-ed temporary name on the target branch.
2052+- rename the whiteout-ed temporary name to the original name.
2053+- unlink the file on the source branch.
2054+- maintain the internal pointer array and the external inode number
2055+ table (XINO).
2056+- maintain the timestamps and other attributes of the parent dir and the
2057+ file.
2058+
2059+And of course, in every step, an error may happen. So the operation
2060+should restore the original file state after an error happens.
53392da6
AM
2061diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2062--- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 2063+++ linux/Documentation/filesystems/aufs/design/06mmap.txt 2016-07-25 19:05:34.807826401 +0200
b912730e 2064@@ -0,0 +1,72 @@
53392da6 2065+
8cdd5066 2066+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
2067+#
2068+# This program is free software; you can redistribute it and/or modify
2069+# it under the terms of the GNU General Public License as published by
2070+# the Free Software Foundation; either version 2 of the License, or
2071+# (at your option) any later version.
2072+#
2073+# This program is distributed in the hope that it will be useful,
2074+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2075+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2076+# GNU General Public License for more details.
2077+#
2078+# You should have received a copy of the GNU General Public License
523b37e3 2079+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2080+
2081+mmap(2) -- File Memory Mapping
2082+----------------------------------------------------------------------
2083+In aufs, the file-mapped pages are handled by a branch fs directly, no
2084+interaction with aufs. It means aufs_mmap() calls the branch fs's
2085+->mmap().
2086+This approach is simple and good, but there is one problem.
7e9cd9fe 2087+Under /proc, several entries show the mmapped files by its path (with
53392da6
AM
2088+device and inode number), and the printed path will be the path on the
2089+branch fs's instead of virtual aufs's.
2090+This is not a problem in most cases, but some utilities lsof(1) (and its
2091+user) may expect the path on aufs.
2092+
2093+To address this issue, aufs adds a new member called vm_prfile in struct
2094+vm_area_struct (and struct vm_region). The original vm_file points to
2095+the file on the branch fs in order to handle everything correctly as
2096+usual. The new vm_prfile points to a virtual file in aufs, and the
2097+show-functions in procfs refers to vm_prfile if it is set.
2098+Also we need to maintain several other places where touching vm_file
2099+such like
2100+- fork()/clone() copies vma and the reference count of vm_file is
2101+ incremented.
2102+- merging vma maintains the ref count too.
2103+
7e9cd9fe 2104+This is not a good approach. It just fakes the printed path. But it
53392da6
AM
2105+leaves all behaviour around f_mapping unchanged. This is surely an
2106+advantage.
2107+Actually aufs had adopted another complicated approach which calls
2108+generic_file_mmap() and handles struct vm_operations_struct. In this
2109+approach, aufs met a hard problem and I could not solve it without
2110+switching the approach.
b912730e
AM
2111+
2112+There may be one more another approach which is
2113+- bind-mount the branch-root onto the aufs-root internally
2114+- grab the new vfsmount (ie. struct mount)
2115+- lazy-umount the branch-root internally
2116+- in open(2) the aufs-file, open the branch-file with the hidden
2117+ vfsmount (instead of the original branch's vfsmount)
2118+- ideally this "bind-mount and lazy-umount" should be done atomically,
2119+ but it may be possible from userspace by the mount helper.
2120+
2121+Adding the internal hidden vfsmount and using it in opening a file, the
2122+file path under /proc will be printed correctly. This approach looks
2123+smarter, but is not possible I am afraid.
2124+- aufs-root may be bind-mount later. when it happens, another hidden
2125+ vfsmount will be required.
2126+- it is hard to get the chance to bind-mount and lazy-umount
2127+ + in kernel-space, FS can have vfsmount in open(2) via
2128+ file->f_path, and aufs can know its vfsmount. But several locks are
2129+ already acquired, and if aufs tries to bind-mount and lazy-umount
2130+ here, then it may cause a deadlock.
2131+ + in user-space, bind-mount doesn't invoke the mount helper.
2132+- since /proc shows dev and ino, aufs has to give vma these info. it
2133+ means a new member vm_prinode will be necessary. this is essentially
2134+ equivalent to vm_prfile described above.
2135+
2136+I have to give up this "looks-smater" approach.
c1595e42
JR
2137diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2138--- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 2139+++ linux/Documentation/filesystems/aufs/design/06xattr.txt 2016-07-25 19:05:34.807826401 +0200
c1595e42
JR
2140@@ -0,0 +1,96 @@
2141+
8cdd5066 2142+# Copyright (C) 2014-2016 Junjiro R. Okajima
c1595e42
JR
2143+#
2144+# This program is free software; you can redistribute it and/or modify
2145+# it under the terms of the GNU General Public License as published by
2146+# the Free Software Foundation; either version 2 of the License, or
2147+# (at your option) any later version.
2148+#
2149+# This program is distributed in the hope that it will be useful,
2150+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2151+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2152+# GNU General Public License for more details.
2153+#
2154+# You should have received a copy of the GNU General Public License
2155+# along with this program; if not, write to the Free Software
2156+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2157+
2158+
2159+Listing XATTR/EA and getting the value
2160+----------------------------------------------------------------------
2161+For the inode standard attributes (owner, group, timestamps, etc.), aufs
2162+shows the values from the topmost existing file. This behaviour is good
7e9cd9fe 2163+for the non-dir entries since the bahaviour exactly matches the shown
c1595e42
JR
2164+information. But for the directories, aufs considers all the same named
2165+entries on the lower branches. Which means, if one of the lower entry
2166+rejects readdir call, then aufs returns an error even if the topmost
2167+entry allows it. This behaviour is necessary to respect the branch fs's
2168+security, but can make users confused since the user-visible standard
2169+attributes don't match the behaviour.
2170+To address this issue, aufs has a mount option called dirperm1 which
2171+checks the permission for the topmost entry only, and ignores the lower
2172+entry's permission.
2173+
2174+A similar issue can happen around XATTR.
2175+getxattr(2) and listxattr(2) families behave as if dirperm1 option is
7e9cd9fe
AM
2176+always set. Otherwise these very unpleasant situation would happen.
2177+- listxattr(2) may return the duplicated entries.
c1595e42
JR
2178+- users may not be able to remove or reset the XATTR forever,
2179+
2180+
2181+XATTR/EA support in the internal (copy,move)-(up,down)
2182+----------------------------------------------------------------------
7e9cd9fe 2183+Generally the extended attributes of inode are categorized as these.
c1595e42
JR
2184+- "security" for LSM and capability.
2185+- "system" for posix ACL, 'acl' mount option is required for the branch
2186+ fs generally.
2187+- "trusted" for userspace, CAP_SYS_ADMIN is required.
2188+- "user" for userspace, 'user_xattr' mount option is required for the
2189+ branch fs generally.
2190+
2191+Moreover there are some other categories. Aufs handles these rather
2192+unpopular categories as the ordinary ones, ie. there is no special
2193+condition nor exception.
2194+
2195+In copy-up, the support for XATTR on the dst branch may differ from the
2196+src branch. In this case, the copy-up operation will get an error and
7e9cd9fe
AM
2197+the original user operation which triggered the copy-up will fail. It
2198+can happen that even all copy-up will fail.
c1595e42
JR
2199+When both of src and dst branches support XATTR and if an error occurs
2200+during copying XATTR, then the copy-up should fail obviously. That is a
2201+good reason and aufs should return an error to userspace. But when only
7e9cd9fe 2202+the src branch support that XATTR, aufs should not return an error.
c1595e42
JR
2203+For example, the src branch supports ACL but the dst branch doesn't
2204+because the dst branch may natively un-support it or temporary
2205+un-support it due to "noacl" mount option. Of course, the dst branch fs
2206+may NOT return an error even if the XATTR is not supported. It is
2207+totally up to the branch fs.
2208+
2209+Anyway when the aufs internal copy-up gets an error from the dst branch
2210+fs, then aufs tries removing the just copied entry and returns the error
2211+to the userspace. The worst case of this situation will be all copy-up
2212+will fail.
2213+
2214+For the copy-up operation, there two basic approaches.
2215+- copy the specified XATTR only (by category above), and return the
7e9cd9fe 2216+ error unconditionally if it happens.
c1595e42
JR
2217+- copy all XATTR, and ignore the error on the specified category only.
2218+
2219+In order to support XATTR and to implement the correct behaviour, aufs
7e9cd9fe
AM
2220+chooses the latter approach and introduces some new branch attributes,
2221+"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
c1595e42 2222+They correspond to the XATTR namespaces (see above). Additionally, to be
7e9cd9fe
AM
2223+convenient, "icex" is also provided which means all "icex*" attributes
2224+are set (here the word "icex" stands for "ignore copy-error on XATTR").
c1595e42
JR
2225+
2226+The meaning of these attributes is to ignore the error from setting
2227+XATTR on that branch.
2228+Note that aufs tries copying all XATTR unconditionally, and ignores the
2229+error from the dst branch according to the specified attributes.
2230+
2231+Some XATTR may have its default value. The default value may come from
2232+the parent dir or the environment. If the default value is set at the
2233+file creating-time, it will be overwritten by copy-up.
2234+Some contradiction may happen I am afraid.
2235+Do we need another attribute to stop copying XATTR? I am unsure. For
2236+now, aufs implements the branch attributes to ignore the error.
53392da6
AM
2237diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2238--- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 2239+++ linux/Documentation/filesystems/aufs/design/07export.txt 2016-07-25 19:05:34.807826401 +0200
523b37e3 2240@@ -0,0 +1,58 @@
53392da6 2241+
8cdd5066 2242+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
2243+#
2244+# This program is free software; you can redistribute it and/or modify
2245+# it under the terms of the GNU General Public License as published by
2246+# the Free Software Foundation; either version 2 of the License, or
2247+# (at your option) any later version.
2248+#
2249+# This program is distributed in the hope that it will be useful,
2250+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2251+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2252+# GNU General Public License for more details.
2253+#
2254+# You should have received a copy of the GNU General Public License
523b37e3 2255+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2256+
2257+Export Aufs via NFS
2258+----------------------------------------------------------------------
2259+Here is an approach.
2260+- like xino/xib, add a new file 'xigen' which stores aufs inode
2261+ generation.
2262+- iget_locked(): initialize aufs inode generation for a new inode, and
2263+ store it in xigen file.
2264+- destroy_inode(): increment aufs inode generation and store it in xigen
2265+ file. it is necessary even if it is not unlinked, because any data of
2266+ inode may be changed by UDBA.
2267+- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2268+ build file handle by
2269+ + branch id (4 bytes)
2270+ + superblock generation (4 bytes)
2271+ + inode number (4 or 8 bytes)
2272+ + parent dir inode number (4 or 8 bytes)
2273+ + inode generation (4 bytes))
2274+ + return value of exportfs_encode_fh() for the parent on a branch (4
2275+ bytes)
2276+ + file handle for a branch (by exportfs_encode_fh())
2277+- fh_to_dentry():
2278+ + find the index of a branch from its id in handle, and check it is
2279+ still exist in aufs.
2280+ + 1st level: get the inode number from handle and search it in cache.
7e9cd9fe
AM
2281+ + 2nd level: if not found in cache, get the parent inode number from
2282+ the handle and search it in cache. and then open the found parent
2283+ dir, find the matching inode number by vfs_readdir() and get its
2284+ name, and call lookup_one_len() for the target dentry.
53392da6
AM
2285+ + 3rd level: if the parent dir is not cached, call
2286+ exportfs_decode_fh() for a branch and get the parent on a branch,
2287+ build a pathname of it, convert it a pathname in aufs, call
2288+ path_lookup(). now aufs gets a parent dir dentry, then handle it as
2289+ the 2nd level.
2290+ + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2291+ for every branch, but not itself. to get this, (currently) aufs
2292+ searches in current->nsproxy->mnt_ns list. it may not be a good
2293+ idea, but I didn't get other approach.
2294+ + test the generation of the gotten inode.
2295+- every inode operation: they may get EBUSY due to UDBA. in this case,
2296+ convert it into ESTALE for NFSD.
2297+- readdir(): call lockdep_on/off() because filldir in NFSD calls
2298+ lookup_one_len(), vfs_getattr(), encode_fh() and others.
2299diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2300--- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 2301+++ linux/Documentation/filesystems/aufs/design/08shwh.txt 2016-07-25 19:05:34.807826401 +0200
523b37e3 2302@@ -0,0 +1,52 @@
53392da6 2303+
8cdd5066 2304+# Copyright (C) 2005-2016 Junjiro R. Okajima
53392da6
AM
2305+#
2306+# This program is free software; you can redistribute it and/or modify
2307+# it under the terms of the GNU General Public License as published by
2308+# the Free Software Foundation; either version 2 of the License, or
2309+# (at your option) any later version.
2310+#
2311+# This program is distributed in the hope that it will be useful,
2312+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2313+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2314+# GNU General Public License for more details.
2315+#
2316+# You should have received a copy of the GNU General Public License
523b37e3 2317+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2318+
2319+Show Whiteout Mode (shwh)
2320+----------------------------------------------------------------------
2321+Generally aufs hides the name of whiteouts. But in some cases, to show
2322+them is very useful for users. For instance, creating a new middle layer
2323+(branch) by merging existing layers.
2324+
2325+(borrowing aufs1 HOW-TO from a user, Michael Towers)
2326+When you have three branches,
2327+- Bottom: 'system', squashfs (underlying base system), read-only
2328+- Middle: 'mods', squashfs, read-only
2329+- Top: 'overlay', ram (tmpfs), read-write
2330+
2331+The top layer is loaded at boot time and saved at shutdown, to preserve
2332+the changes made to the system during the session.
2333+When larger changes have been made, or smaller changes have accumulated,
2334+the size of the saved top layer data grows. At this point, it would be
2335+nice to be able to merge the two overlay branches ('mods' and 'overlay')
2336+and rewrite the 'mods' squashfs, clearing the top layer and thus
2337+restoring save and load speed.
2338+
2339+This merging is simplified by the use of another aufs mount, of just the
2340+two overlay branches using the 'shwh' option.
2341+# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2342+ aufs /livesys/merge_union
2343+
2344+A merged view of these two branches is then available at
2345+/livesys/merge_union, and the new feature is that the whiteouts are
2346+visible!
2347+Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2348+writing to all branches. Also the default mode for all branches is 'ro'.
2349+It is now possible to save the combined contents of the two overlay
2350+branches to a new squashfs, e.g.:
2351+# mksquashfs /livesys/merge_union /path/to/newmods.squash
2352+
2353+This new squashfs archive can be stored on the boot device and the
2354+initramfs will use it to replace the old one at the next boot.
2355diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2356--- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 2357+++ linux/Documentation/filesystems/aufs/design/10dynop.txt 2016-07-25 19:05:34.807826401 +0200
7e9cd9fe 2358@@ -0,0 +1,47 @@
53392da6 2359+
8cdd5066 2360+# Copyright (C) 2010-2016 Junjiro R. Okajima
53392da6
AM
2361+#
2362+# This program is free software; you can redistribute it and/or modify
2363+# it under the terms of the GNU General Public License as published by
2364+# the Free Software Foundation; either version 2 of the License, or
2365+# (at your option) any later version.
2366+#
2367+# This program is distributed in the hope that it will be useful,
2368+# but WITHOUT ANY WARRANTY; without even the implied warranty of
2369+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2370+# GNU General Public License for more details.
2371+#
2372+# You should have received a copy of the GNU General Public License
523b37e3 2373+# along with this program. If not, see <http://www.gnu.org/licenses/>.
53392da6
AM
2374+
2375+Dynamically customizable FS operations
2376+----------------------------------------------------------------------
2377+Generally FS operations (struct inode_operations, struct
2378+address_space_operations, struct file_operations, etc.) are defined as
2379+"static const", but it never means that FS have only one set of
2380+operation. Some FS have multiple sets of them. For instance, ext2 has
2381+three sets, one for XIP, for NOBH, and for normal.
2382+Since aufs overrides and redirects these operations, sometimes aufs has
7e9cd9fe 2383+to change its behaviour according to the branch FS type. More importantly
53392da6
AM
2384+VFS acts differently if a function (member in the struct) is set or
2385+not. It means aufs should have several sets of operations and select one
2386+among them according to the branch FS definition.
2387+
7e9cd9fe 2388+In order to solve this problem and not to affect the behaviour of VFS,
53392da6 2389+aufs defines these operations dynamically. For instance, aufs defines
7e9cd9fe
AM
2390+dummy direct_IO function for struct address_space_operations, but it may
2391+not be set to the address_space_operations actually. When the branch FS
2392+doesn't have it, aufs doesn't set it to its address_space_operations
2393+while the function definition itself is still alive. So the behaviour
2394+itself will not change, and it will return an error when direct_IO is
2395+not set.
53392da6
AM
2396+
2397+The lifetime of these dynamically generated operation object is
2398+maintained by aufs branch object. When the branch is removed from aufs,
2399+the reference counter of the object is decremented. When it reaches
2400+zero, the dynamically generated operation object will be freed.
2401+
7e9cd9fe
AM
2402+This approach is designed to support AIO (io_submit), Direct I/O and
2403+XIP (DAX) mainly.
2404+Currently this approach is applied to address_space_operations for
2405+regular files only.
53392da6
AM
2406diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2407--- /usr/share/empty/Documentation/filesystems/aufs/README 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
2408+++ linux/Documentation/filesystems/aufs/README 2016-07-25 19:05:34.807826401 +0200
2409@@ -0,0 +1,392 @@
53392da6 2410+
5527c038 2411+Aufs4 -- advanced multi layered unification filesystem version 4.x
53392da6
AM
2412+http://aufs.sf.net
2413+Junjiro R. Okajima
2414+
2415+
2416+0. Introduction
2417+----------------------------------------
2418+In the early days, aufs was entirely re-designed and re-implemented
7e9cd9fe 2419+Unionfs Version 1.x series. Adding many original ideas, approaches,
53392da6
AM
2420+improvements and implementations, it becomes totally different from
2421+Unionfs while keeping the basic features.
2422+Recently, Unionfs Version 2.x series begin taking some of the same
2423+approaches to aufs1's.
2424+Unionfs is being developed by Professor Erez Zadok at Stony Brook
2425+University and his team.
2426+
5527c038 2427+Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
53392da6
AM
2428+If you want older kernel version support, try aufs2-2.6.git or
2429+aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
2430+
2431+Note: it becomes clear that "Aufs was rejected. Let's give it up."
38d290e6
JR
2432+ According to Christoph Hellwig, linux rejects all union-type
2433+ filesystems but UnionMount.
53392da6
AM
2434+<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2435+
38d290e6
JR
2436+PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2437+ UnionMount, and he pointed out an issue around a directory mutex
2438+ lock and aufs addressed it. But it is still unsure whether aufs will
2439+ be merged (or any other union solution).
076b876e 2440+<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
38d290e6 2441+
53392da6
AM
2442+
2443+1. Features
2444+----------------------------------------
2445+- unite several directories into a single virtual filesystem. The member
2446+ directory is called as a branch.
2447+- you can specify the permission flags to the branch, which are 'readonly',
2448+ 'readwrite' and 'whiteout-able.'
2449+- by upper writable branch, internal copyup and whiteout, files/dirs on
2450+ readonly branch are modifiable logically.
2451+- dynamic branch manipulation, add, del.
2452+- etc...
2453+
7e9cd9fe
AM
2454+Also there are many enhancements in aufs, such as:
2455+- test only the highest one for the directory permission (dirperm1)
2456+- copyup on open (coo=)
2457+- 'move' policy for copy-up between two writable branches, after
2458+ checking free space.
2459+- xattr, acl
53392da6
AM
2460+- readdir(3) in userspace.
2461+- keep inode number by external inode number table
2462+- keep the timestamps of file/dir in internal copyup operation
2463+- seekable directory, supporting NFS readdir.
2464+- whiteout is hardlinked in order to reduce the consumption of inodes
2465+ on branch
2466+- do not copyup, nor create a whiteout when it is unnecessary
2467+- revert a single systemcall when an error occurs in aufs
2468+- remount interface instead of ioctl
2469+- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2470+- loopback mounted filesystem as a branch
2471+- kernel thread for removing the dir who has a plenty of whiteouts
2472+- support copyup sparse file (a file which has a 'hole' in it)
2473+- default permission flags for branches
2474+- selectable permission flags for ro branch, whether whiteout can
2475+ exist or not
2476+- export via NFS.
2477+- support <sysfs>/fs/aufs and <debugfs>/aufs.
2478+- support multiple writable branches, some policies to select one
2479+ among multiple writable branches.
2480+- a new semantics for link(2) and rename(2) to support multiple
2481+ writable branches.
2482+- no glibc changes are required.
2483+- pseudo hardlink (hardlink over branches)
2484+- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2485+ including NFS or remote filesystem branch.
2486+- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2487+- and more...
2488+
5527c038 2489+Currently these features are dropped temporary from aufs4.
53392da6 2490+See design/08plan.txt in detail.
53392da6
AM
2491+- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2492+ (robr)
2493+- statistics of aufs thread (/sys/fs/aufs/stat)
53392da6
AM
2494+
2495+Features or just an idea in the future (see also design/*.txt),
2496+- reorder the branch index without del/re-add.
2497+- permanent xino files for NFSD
2498+- an option for refreshing the opened files after add/del branches
53392da6
AM
2499+- light version, without branch manipulation. (unnecessary?)
2500+- copyup in userspace
2501+- inotify in userspace
2502+- readv/writev
53392da6
AM
2503+
2504+
2505+2. Download
2506+----------------------------------------
5527c038
JR
2507+There are three GIT trees for aufs4, aufs4-linux.git,
2508+aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
1e00d052 2509+"aufs-util.git."
5527c038
JR
2510+While the aufs-util is always necessary, you need either of aufs4-linux
2511+or aufs4-standalone.
1e00d052 2512+
5527c038 2513+The aufs4-linux tree includes the whole linux mainline GIT tree,
1e00d052
AM
2514+git://git.kernel.org/.../torvalds/linux.git.
2515+And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
5527c038 2516+build aufs4 as an external kernel module.
2000de60 2517+Several extra patches are not included in this tree. Only
be52b249 2518+aufs4-standalone tree contains them. They are described in the later
2000de60 2519+section "Configuration and Compilation."
1e00d052 2520+
5527c038 2521+On the other hand, the aufs4-standalone tree has only aufs source files
53392da6 2522+and necessary patches, and you can select CONFIG_AUFS_FS=m.
2000de60 2523+But you need to apply all aufs patches manually.
53392da6 2524+
5527c038
JR
2525+You will find GIT branches whose name is in form of "aufs4.x" where "x"
2526+represents the linux kernel version, "linux-4.x". For instance,
2527+"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
2528+"aufs4.x-rcN" branch.
1e00d052 2529+
5527c038 2530+o aufs4-linux tree
1e00d052 2531+$ git clone --reference /your/linux/git/tree \
5527c038 2532+ git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
1e00d052 2533+- if you don't have linux GIT tree, then remove "--reference ..."
5527c038
JR
2534+$ cd aufs4-linux.git
2535+$ git checkout origin/aufs4.0
53392da6 2536+
2000de60
JR
2537+Or You may want to directly git-pull aufs into your linux GIT tree, and
2538+leave the patch-work to GIT.
2539+$ cd /your/linux/git/tree
5527c038
JR
2540+$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
2541+$ git fetch aufs4
2542+$ git checkout -b my4.0 v4.0
2543+$ (add your local change...)
2544+$ git pull aufs4 aufs4.0
2545+- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
2000de60 2546+- you may need to solve some conflicts between your_changes and
5527c038
JR
2547+ aufs4.0. in this case, git-rerere is recommended so that you can
2548+ solve the similar conflicts automatically when you upgrade to 4.1 or
2000de60
JR
2549+ later in the future.
2550+
5527c038
JR
2551+o aufs4-standalone tree
2552+$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
2553+$ cd aufs4-standalone.git
2554+$ git checkout origin/aufs4.0
53392da6
AM
2555+
2556+o aufs-util tree
5527c038
JR
2557+$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2558+- note that the public aufs-util.git is on SourceForge instead of
2559+ GitHUB.
53392da6 2560+$ cd aufs-util.git
5527c038 2561+$ git checkout origin/aufs4.0
53392da6 2562+
5527c038
JR
2563+Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
2564+The minor version number, 'x' in '4.x', of aufs may not always
9dbd164d
AM
2565+follow the minor version number of the kernel.
2566+Because changes in the kernel that cause the use of a new
2567+minor version number do not always require changes to aufs-util.
2568+
2569+Since aufs-util has its own minor version number, you may not be
2570+able to find a GIT branch in aufs-util for your kernel's
2571+exact minor version number.
2572+In this case, you should git-checkout the branch for the
53392da6 2573+nearest lower number.
9dbd164d
AM
2574+
2575+For (an unreleased) example:
5527c038
JR
2576+If you are using "linux-4.10" and the "aufs4.10" branch
2577+does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
9dbd164d
AM
2578+or something numerically smaller is the branch for your kernel.
2579+
53392da6
AM
2580+Also you can view all branches by
2581+ $ git branch -a
2582+
2583+
2584+3. Configuration and Compilation
2585+----------------------------------------
2586+Make sure you have git-checkout'ed the correct branch.
2587+
5527c038 2588+For aufs4-linux tree,
c06a8ce3 2589+- enable CONFIG_AUFS_FS.
1e00d052
AM
2590+- set other aufs configurations if necessary.
2591+
5527c038 2592+For aufs4-standalone tree,
53392da6
AM
2593+There are several ways to build.
2594+
2595+1.
5527c038
JR
2596+- apply ./aufs4-kbuild.patch to your kernel source files.
2597+- apply ./aufs4-base.patch too.
2598+- apply ./aufs4-mmap.patch too.
2599+- apply ./aufs4-standalone.patch too, if you have a plan to set
2600+ CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
537831f9
AM
2601+- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2602+ kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
c06a8ce3 2603+- enable CONFIG_AUFS_FS, you can select either
53392da6
AM
2604+ =m or =y.
2605+- and build your kernel as usual.
2606+- install the built kernel.
c06a8ce3
AM
2607+ Note: Since linux-3.9, every filesystem module requires an alias
2608+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2609+ modules.aliases file if you set CONFIG_AUFS_FS=m.
7eafdf33
AM
2610+- install the header files too by "make headers_install" to the
2611+ directory where you specify. By default, it is $PWD/usr.
b4510431 2612+ "make help" shows a brief note for headers_install.
53392da6
AM
2613+- and reboot your system.
2614+
2615+2.
2616+- module only (CONFIG_AUFS_FS=m).
5527c038
JR
2617+- apply ./aufs4-base.patch to your kernel source files.
2618+- apply ./aufs4-mmap.patch too.
2619+- apply ./aufs4-standalone.patch too.
53392da6
AM
2620+- build your kernel, don't forget "make headers_install", and reboot.
2621+- edit ./config.mk and set other aufs configurations if necessary.
b4510431 2622+ Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
53392da6
AM
2623+ every aufs configurations.
2624+- build the module by simple "make".
c06a8ce3
AM
2625+ Note: Since linux-3.9, every filesystem module requires an alias
2626+ "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
2627+ modules.aliases file.
53392da6
AM
2628+- you can specify ${KDIR} make variable which points to your kernel
2629+ source tree.
2630+- install the files
2631+ + run "make install" to install the aufs module, or copy the built
b4510431
AM
2632+ $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2633+ + run "make install_headers" (instead of headers_install) to install
2634+ the modified aufs header file (you can specify DESTDIR which is
2635+ available in aufs standalone version's Makefile only), or copy
2636+ $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2637+ you like manually. By default, the target directory is $PWD/usr.
5527c038 2638+- no need to apply aufs4-kbuild.patch, nor copying source files to your
53392da6
AM
2639+ kernel source tree.
2640+
b4510431 2641+Note: The header file aufs_type.h is necessary to build aufs-util
53392da6
AM
2642+ as well as "make headers_install" in the kernel source tree.
2643+ headers_install is subject to be forgotten, but it is essentially
2644+ necessary, not only for building aufs-util.
2645+ You may not meet problems without headers_install in some older
2646+ version though.
2647+
2648+And then,
2649+- read README in aufs-util, build and install it
9dbd164d
AM
2650+- note that your distribution may contain an obsoleted version of
2651+ aufs_type.h in /usr/include/linux or something. When you build aufs
2652+ utilities, make sure that your compiler refers the correct aufs header
2653+ file which is built by "make headers_install."
53392da6
AM
2654+- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2655+ then run "make install_ulib" too. And refer to the aufs manual in
2656+ detail.
2657+
5527c038 2658+There several other patches in aufs4-standalone.git. They are all
38d290e6 2659+optional. When you meet some problems, they will help you.
5527c038 2660+- aufs4-loopback.patch
38d290e6
JR
2661+ Supports a nested loopback mount in a branch-fs. This patch is
2662+ unnecessary until aufs produces a message like "you may want to try
2663+ another patch for loopback file".
2664+- vfs-ino.patch
2665+ Modifies a system global kernel internal function get_next_ino() in
2666+ order to stop assigning 0 for an inode-number. Not directly related to
2667+ aufs, but recommended generally.
2668+- tmpfs-idr.patch
2669+ Keeps the tmpfs inode number as the lowest value. Effective to reduce
2670+ the size of aufs XINO files for tmpfs branch. Also it prevents the
2671+ duplication of inode number, which is important for backup tools and
2672+ other utilities. When you find aufs XINO files for tmpfs branch
2673+ growing too much, try this patch.
be52b249
AM
2674+- lockdep-debug.patch
2675+ Because aufs is not only an ordinary filesystem (callee of VFS), but
2676+ also a caller of VFS functions for branch filesystems, subclassing of
2677+ the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2678+ feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2679+ need to apply this debug patch to expand several constant values.
2680+ If don't know what LOCKDEP, then you don't have apply this patch.
38d290e6 2681+
53392da6
AM
2682+
2683+4. Usage
2684+----------------------------------------
2685+At first, make sure aufs-util are installed, and please read the aufs
2686+manual, aufs.5 in aufs-util.git tree.
2687+$ man -l aufs.5
2688+
2689+And then,
2690+$ mkdir /tmp/rw /tmp/aufs
2691+# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2692+
2693+Here is another example. The result is equivalent.
2694+# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2695+ Or
2696+# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2697+# mount -o remount,append:${HOME} /tmp/aufs
2698+
2699+Then, you can see whole tree of your home dir through /tmp/aufs. If
2700+you modify a file under /tmp/aufs, the one on your home directory is
2701+not affected, instead the same named file will be newly created under
2702+/tmp/rw. And all of your modification to a file will be applied to
2703+the one under /tmp/rw. This is called the file based Copy on Write
2704+(COW) method.
2705+Aufs mount options are described in aufs.5.
2706+If you run chroot or something and make your aufs as a root directory,
2707+then you need to customize the shutdown script. See the aufs manual in
2708+detail.
2709+
2710+Additionally, there are some sample usages of aufs which are a
2711+diskless system with network booting, and LiveCD over NFS.
2712+See sample dir in CVS tree on SourceForge.
2713+
2714+
2715+5. Contact
2716+----------------------------------------
2717+When you have any problems or strange behaviour in aufs, please let me
2718+know with:
2719+- /proc/mounts (instead of the output of mount(8))
2720+- /sys/module/aufs/*
2721+- /sys/fs/aufs/* (if you have them)
2722+- /debug/aufs/* (if you have them)
2723+- linux kernel version
2724+ if your kernel is not plain, for example modified by distributor,
2725+ the url where i can download its source is necessary too.
2726+- aufs version which was printed at loading the module or booting the
2727+ system, instead of the date you downloaded.
2728+- configuration (define/undefine CONFIG_AUFS_xxx)
2729+- kernel configuration or /proc/config.gz (if you have it)
2730+- behaviour which you think to be incorrect
2731+- actual operation, reproducible one is better
2732+- mailto: aufs-users at lists.sourceforge.net
2733+
2734+Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2735+and Feature Requests) on SourceForge. Please join and write to
2736+aufs-users ML.
2737+
2738+
2739+6. Acknowledgements
2740+----------------------------------------
2741+Thanks to everyone who have tried and are using aufs, whoever
2742+have reported a bug or any feedback.
2743+
2744+Especially donators:
2745+Tomas Matejicek(slax.org) made a donation (much more than once).
2746+ Since Apr 2010, Tomas M (the author of Slax and Linux Live
2747+ scripts) is making "doubling" donations.
2748+ Unfortunately I cannot list all of the donators, but I really
b4510431 2749+ appreciate.
53392da6
AM
2750+ It ends Aug 2010, but the ordinary donation URL is still available.
2751+ <http://sourceforge.net/donate/index.php?group_id=167503>
2752+Dai Itasaka made a donation (2007/8).
2753+Chuck Smith made a donation (2008/4, 10 and 12).
2754+Henk Schoneveld made a donation (2008/9).
2755+Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2756+Francois Dupoux made a donation (2008/11).
2757+Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2758+ aufs2 GIT tree (2009/2).
2759+William Grant made a donation (2009/3).
2760+Patrick Lane made a donation (2009/4).
2761+The Mail Archive (mail-archive.com) made donations (2009/5).
2762+Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2763+New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2764+Pavel Pronskiy made a donation (2011/2).
2765+Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2766+ Networks (Ed Wildgoose) made a donation for hardware (2011/3).
537831f9
AM
2767+Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2768+11).
1e00d052 2769+Sam Liddicott made a donation (2011/9).
86dc4139
AM
2770+Era Scarecrow made a donation (2013/4).
2771+Bor Ratajc made a donation (2013/4).
2772+Alessandro Gorreta made a donation (2013/4).
2773+POIRETTE Marc made a donation (2013/4).
2774+Alessandro Gorreta made a donation (2013/4).
2775+lauri kasvandik made a donation (2013/5).
392086de 2776+"pemasu from Finland" made a donation (2013/7).
523b37e3
AM
2777+The Parted Magic Project made a donation (2013/9 and 11).
2778+Pavel Barta made a donation (2013/10).
38d290e6 2779+Nikolay Pertsev made a donation (2014/5).
c2c0f25c 2780+James B made a donation (2014/7 and 2015/7).
076b876e 2781+Stefano Di Biase made a donation (2014/8).
2000de60 2782+Daniel Epellei made a donation (2015/1).
8cdd5066 2783+OmegaPhil made a donation (2016/1).
5afbbe0d 2784+Tomasz Szewczyk made a donation (2016/4).
53392da6
AM
2785+
2786+Thank you very much.
2787+Donations are always, including future donations, very important and
2788+helpful for me to keep on developing aufs.
2789+
2790+
2791+7.
2792+----------------------------------------
2793+If you are an experienced user, no explanation is needed. Aufs is
2794+just a linux filesystem.
2795+
2796+
2797+Enjoy!
2798+
2799+# Local variables: ;
2800+# mode: text;
2801+# End: ;
7f207e10
AM
2802diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2803--- /usr/share/empty/fs/aufs/aufs.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 2804+++ linux/fs/aufs/aufs.h 2016-07-25 19:05:34.811159821 +0200
523b37e3 2805@@ -0,0 +1,59 @@
7f207e10 2806+/*
8cdd5066 2807+ * Copyright (C) 2005-2016 Junjiro R. Okajima
7f207e10
AM
2808+ *
2809+ * This program, aufs is free software; you can redistribute it and/or modify
2810+ * it under the terms of the GNU General Public License as published by
2811+ * the Free Software Foundation; either version 2 of the License, or
2812+ * (at your option) any later version.
2813+ *
2814+ * This program is distributed in the hope that it will be useful,
2815+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2816+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2817+ * GNU General Public License for more details.
2818+ *
2819+ * You should have received a copy of the GNU General Public License
523b37e3 2820+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2821+ */
2822+
2823+/*
2824+ * all header files
2825+ */
2826+
2827+#ifndef __AUFS_H__
2828+#define __AUFS_H__
2829+
2830+#ifdef __KERNEL__
2831+
2832+#define AuStub(type, name, body, ...) \
2833+ static inline type name(__VA_ARGS__) { body; }
2834+
2835+#define AuStubVoid(name, ...) \
2836+ AuStub(void, name, , __VA_ARGS__)
2837+#define AuStubInt0(name, ...) \
2838+ AuStub(int, name, return 0, __VA_ARGS__)
2839+
2840+#include "debug.h"
2841+
2842+#include "branch.h"
2843+#include "cpup.h"
2844+#include "dcsub.h"
2845+#include "dbgaufs.h"
2846+#include "dentry.h"
2847+#include "dir.h"
2848+#include "dynop.h"
2849+#include "file.h"
2850+#include "fstype.h"
2851+#include "inode.h"
2852+#include "loop.h"
2853+#include "module.h"
7f207e10
AM
2854+#include "opts.h"
2855+#include "rwsem.h"
2856+#include "spl.h"
2857+#include "super.h"
2858+#include "sysaufs.h"
2859+#include "vfsub.h"
2860+#include "whout.h"
2861+#include "wkq.h"
2862+
2863+#endif /* __KERNEL__ */
2864+#endif /* __AUFS_H__ */
2865diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2866--- /usr/share/empty/fs/aufs/branch.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
2867+++ linux/fs/aufs/branch.c 2016-08-17 18:01:06.095221547 +0200
2868@@ -0,0 +1,1409 @@
7f207e10 2869+/*
8cdd5066 2870+ * Copyright (C) 2005-2016 Junjiro R. Okajima
7f207e10
AM
2871+ *
2872+ * This program, aufs is free software; you can redistribute it and/or modify
2873+ * it under the terms of the GNU General Public License as published by
2874+ * the Free Software Foundation; either version 2 of the License, or
2875+ * (at your option) any later version.
2876+ *
2877+ * This program is distributed in the hope that it will be useful,
2878+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
2879+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2880+ * GNU General Public License for more details.
2881+ *
2882+ * You should have received a copy of the GNU General Public License
523b37e3 2883+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
2884+ */
2885+
2886+/*
2887+ * branch management
2888+ */
2889+
027c5e7a 2890+#include <linux/compat.h>
7f207e10
AM
2891+#include <linux/statfs.h>
2892+#include "aufs.h"
2893+
2894+/*
2895+ * free a single branch
1facf9fc 2896+ */
2897+static void au_br_do_free(struct au_branch *br)
2898+{
2899+ int i;
2900+ struct au_wbr *wbr;
4a4d8108 2901+ struct au_dykey **key;
1facf9fc 2902+
027c5e7a
AM
2903+ au_hnotify_fin_br(br);
2904+
1facf9fc 2905+ if (br->br_xino.xi_file)
2906+ fput(br->br_xino.xi_file);
2907+ mutex_destroy(&br->br_xino.xi_nondir_mtx);
2908+
5afbbe0d
AM
2909+ AuDebugOn(au_br_count(br));
2910+ au_br_count_fin(br);
1facf9fc 2911+
2912+ wbr = br->br_wbr;
2913+ if (wbr) {
2914+ for (i = 0; i < AuBrWh_Last; i++)
2915+ dput(wbr->wbr_wh[i]);
2916+ AuDebugOn(atomic_read(&wbr->wbr_wh_running));
dece6358 2917+ AuRwDestroy(&wbr->wbr_wh_rwsem);
1facf9fc 2918+ }
2919+
076b876e
AM
2920+ if (br->br_fhsm) {
2921+ au_br_fhsm_fin(br->br_fhsm);
f0c0a007 2922+ au_delayed_kfree(br->br_fhsm);
076b876e
AM
2923+ }
2924+
4a4d8108
AM
2925+ key = br->br_dykey;
2926+ for (i = 0; i < AuBrDynOp; i++, key++)
2927+ if (*key)
2928+ au_dy_put(*key);
2929+ else
2930+ break;
2931+
537831f9
AM
2932+ /* recursive lock, s_umount of branch's */
2933+ lockdep_off();
86dc4139 2934+ path_put(&br->br_path);
537831f9 2935+ lockdep_on();
f0c0a007
AM
2936+ if (wbr)
2937+ au_delayed_kfree(wbr);
2938+ au_delayed_kfree(br);
1facf9fc 2939+}
2940+
2941+/*
2942+ * frees all branches
2943+ */
2944+void au_br_free(struct au_sbinfo *sbinfo)
2945+{
2946+ aufs_bindex_t bmax;
2947+ struct au_branch **br;
2948+
dece6358
AM
2949+ AuRwMustWriteLock(&sbinfo->si_rwsem);
2950+
5afbbe0d 2951+ bmax = sbinfo->si_bbot + 1;
1facf9fc 2952+ br = sbinfo->si_branch;
2953+ while (bmax--)
2954+ au_br_do_free(*br++);
2955+}
2956+
2957+/*
2958+ * find the index of a branch which is specified by @br_id.
2959+ */
2960+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2961+{
5afbbe0d 2962+ aufs_bindex_t bindex, bbot;
1facf9fc 2963+
5afbbe0d
AM
2964+ bbot = au_sbbot(sb);
2965+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 2966+ if (au_sbr_id(sb, bindex) == br_id)
2967+ return bindex;
2968+ return -1;
2969+}
2970+
2971+/* ---------------------------------------------------------------------- */
2972+
2973+/*
2974+ * add a branch
2975+ */
2976+
b752ccd1
AM
2977+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2978+ struct dentry *h_root)
1facf9fc 2979+{
b752ccd1
AM
2980+ if (unlikely(h_adding == h_root
2981+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 2982+ return 1;
b752ccd1
AM
2983+ if (h_adding->d_sb != h_root->d_sb)
2984+ return 0;
2985+ return au_test_subdir(h_adding, h_root)
2986+ || au_test_subdir(h_root, h_adding);
1facf9fc 2987+}
2988+
2989+/*
2990+ * returns a newly allocated branch. @new_nbranch is a number of branches
2991+ * after adding a branch.
2992+ */
2993+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2994+ int perm)
2995+{
2996+ struct au_branch *add_branch;
2997+ struct dentry *root;
5527c038 2998+ struct inode *inode;
4a4d8108 2999+ int err;
1facf9fc 3000+
4a4d8108 3001+ err = -ENOMEM;
1facf9fc 3002+ root = sb->s_root;
be52b249 3003+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3004+ if (unlikely(!add_branch))
3005+ goto out;
3006+
027c5e7a
AM
3007+ err = au_hnotify_init_br(add_branch, perm);
3008+ if (unlikely(err))
3009+ goto out_br;
3010+
1facf9fc 3011+ if (au_br_writable(perm)) {
3012+ /* may be freed separately at changing the branch permission */
be52b249 3013+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3014+ GFP_NOFS);
3015+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3016+ goto out_hnotify;
1facf9fc 3017+ }
3018+
076b876e
AM
3019+ if (au_br_fhsm(perm)) {
3020+ err = au_fhsm_br_alloc(add_branch);
3021+ if (unlikely(err))
3022+ goto out_wbr;
3023+ }
3024+
4a4d8108
AM
3025+ err = au_sbr_realloc(au_sbi(sb), new_nbranch);
3026+ if (!err)
3027+ err = au_di_realloc(au_di(root), new_nbranch);
5527c038
JR
3028+ if (!err) {
3029+ inode = d_inode(root);
5afbbe0d 3030+ err = au_hinode_realloc(au_ii(inode), new_nbranch);
5527c038 3031+ }
4a4d8108
AM
3032+ if (!err)
3033+ return add_branch; /* success */
1facf9fc 3034+
076b876e 3035+out_wbr:
f0c0a007
AM
3036+ if (add_branch->br_wbr)
3037+ au_delayed_kfree(add_branch->br_wbr);
027c5e7a
AM
3038+out_hnotify:
3039+ au_hnotify_fin_br(add_branch);
4f0767ce 3040+out_br:
f0c0a007 3041+ au_delayed_kfree(add_branch);
4f0767ce 3042+out:
4a4d8108 3043+ return ERR_PTR(err);
1facf9fc 3044+}
3045+
3046+/*
3047+ * test if the branch permission is legal or not.
3048+ */
3049+static int test_br(struct inode *inode, int brperm, char *path)
3050+{
3051+ int err;
3052+
4a4d8108
AM
3053+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3054+ if (!err)
3055+ goto out;
1facf9fc 3056+
4a4d8108
AM
3057+ err = -EINVAL;
3058+ pr_err("write permission for readonly mount or inode, %s\n", path);
3059+
4f0767ce 3060+out:
1facf9fc 3061+ return err;
3062+}
3063+
3064+/*
3065+ * returns:
3066+ * 0: success, the caller will add it
3067+ * plus: success, it is already unified, the caller should ignore it
3068+ * minus: error
3069+ */
3070+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3071+{
3072+ int err;
5afbbe0d 3073+ aufs_bindex_t bbot, bindex;
5527c038 3074+ struct dentry *root, *h_dentry;
1facf9fc 3075+ struct inode *inode, *h_inode;
3076+
3077+ root = sb->s_root;
5afbbe0d
AM
3078+ bbot = au_sbbot(sb);
3079+ if (unlikely(bbot >= 0
1facf9fc 3080+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3081+ err = 1;
3082+ if (!remount) {
3083+ err = -EINVAL;
4a4d8108 3084+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3085+ }
3086+ goto out;
3087+ }
3088+
3089+ err = -ENOSPC; /* -E2BIG; */
3090+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3091+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3092+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3093+ goto out;
3094+ }
3095+
3096+ err = -EDOM;
5afbbe0d 3097+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3098+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3099+ goto out;
3100+ }
3101+
5527c038 3102+ inode = d_inode(add->path.dentry);
1facf9fc 3103+ err = -ENOENT;
3104+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3105+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3106+ goto out;
3107+ }
3108+
3109+ err = -EINVAL;
3110+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3111+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3112+ goto out;
3113+ }
3114+
3115+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3116+ pr_err("unsupported filesystem, %s (%s)\n",
3117+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3118+ goto out;
3119+ }
3120+
c1595e42
JR
3121+ if (unlikely(inode->i_sb->s_stack_depth)) {
3122+ pr_err("already stacked, %s (%s)\n",
3123+ add->pathname, au_sbtype(inode->i_sb));
3124+ goto out;
3125+ }
3126+
5527c038 3127+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3128+ if (unlikely(err))
3129+ goto out;
3130+
5afbbe0d 3131+ if (bbot < 0)
1facf9fc 3132+ return 0; /* success */
3133+
3134+ err = -EINVAL;
5afbbe0d 3135+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3136+ if (unlikely(test_overlap(sb, add->path.dentry,
3137+ au_h_dptr(root, bindex)))) {
4a4d8108 3138+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3139+ goto out;
3140+ }
3141+
3142+ err = 0;
3143+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3144+ h_dentry = au_h_dptr(root, 0);
3145+ h_inode = d_inode(h_dentry);
1facf9fc 3146+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3147+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3148+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3149+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3150+ add->pathname,
3151+ i_uid_read(inode), i_gid_read(inode),
3152+ (inode->i_mode & S_IALLUGO),
3153+ i_uid_read(h_inode), i_gid_read(h_inode),
3154+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3155+ }
3156+
4f0767ce 3157+out:
1facf9fc 3158+ return err;
3159+}
3160+
3161+/*
3162+ * initialize or clean the whiteouts for an adding branch
3163+ */
3164+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3165+ int new_perm)
1facf9fc 3166+{
3167+ int err, old_perm;
3168+ aufs_bindex_t bindex;
febd17d6 3169+ struct inode *h_inode;
1facf9fc 3170+ struct au_wbr *wbr;
3171+ struct au_hinode *hdir;
5527c038 3172+ struct dentry *h_dentry;
1facf9fc 3173+
86dc4139
AM
3174+ err = vfsub_mnt_want_write(au_br_mnt(br));
3175+ if (unlikely(err))
3176+ goto out;
3177+
1facf9fc 3178+ wbr = br->br_wbr;
3179+ old_perm = br->br_perm;
3180+ br->br_perm = new_perm;
3181+ hdir = NULL;
febd17d6 3182+ h_inode = NULL;
1facf9fc 3183+ bindex = au_br_index(sb, br->br_id);
3184+ if (0 <= bindex) {
5527c038 3185+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3186+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3187+ } else {
5527c038 3188+ h_dentry = au_br_dentry(br);
febd17d6
JR
3189+ h_inode = d_inode(h_dentry);
3190+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3191+ }
3192+ if (!wbr)
86dc4139 3193+ err = au_wh_init(br, sb);
1facf9fc 3194+ else {
3195+ wbr_wh_write_lock(wbr);
86dc4139 3196+ err = au_wh_init(br, sb);
1facf9fc 3197+ wbr_wh_write_unlock(wbr);
3198+ }
3199+ if (hdir)
5afbbe0d 3200+ au_hn_inode_unlock(hdir);
1facf9fc 3201+ else
febd17d6 3202+ inode_unlock(h_inode);
86dc4139 3203+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3204+ br->br_perm = old_perm;
3205+
3206+ if (!err && wbr && !au_br_writable(new_perm)) {
f0c0a007 3207+ au_delayed_kfree(wbr);
1facf9fc 3208+ br->br_wbr = NULL;
3209+ }
3210+
86dc4139 3211+out:
1facf9fc 3212+ return err;
3213+}
3214+
3215+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3216+ int perm)
1facf9fc 3217+{
3218+ int err;
4a4d8108 3219+ struct kstatfs kst;
1facf9fc 3220+ struct au_wbr *wbr;
3221+
3222+ wbr = br->br_wbr;
dece6358 3223+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3224+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3225+
4a4d8108
AM
3226+ /*
3227+ * a limit for rmdir/rename a dir
523b37e3 3228+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3229+ */
86dc4139 3230+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3231+ if (unlikely(err))
3232+ goto out;
3233+ err = -EINVAL;
3234+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3235+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3236+ else
523b37e3
AM
3237+ pr_err("%pd(%s), unsupported namelen %ld\n",
3238+ au_br_dentry(br),
86dc4139 3239+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3240+
4f0767ce 3241+out:
1facf9fc 3242+ return err;
3243+}
3244+
c1595e42 3245+/* initialize a new branch */
1facf9fc 3246+static int au_br_init(struct au_branch *br, struct super_block *sb,
3247+ struct au_opt_add *add)
3248+{
3249+ int err;
5527c038 3250+ struct inode *h_inode;
1facf9fc 3251+
3252+ err = 0;
1facf9fc 3253+ mutex_init(&br->br_xino.xi_nondir_mtx);
3254+ br->br_perm = add->perm;
86dc4139 3255+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3256+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3257+ au_br_count_init(br);
1facf9fc 3258+ atomic_set(&br->br_xino_running, 0);
3259+ br->br_id = au_new_br_id(sb);
7f207e10 3260+ AuDebugOn(br->br_id < 0);
1facf9fc 3261+
3262+ if (au_br_writable(add->perm)) {
86dc4139 3263+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3264+ if (unlikely(err))
b752ccd1 3265+ goto out_err;
1facf9fc 3266+ }
3267+
3268+ if (au_opt_test(au_mntflags(sb), XINO)) {
5527c038
JR
3269+ h_inode = d_inode(add->path.dentry);
3270+ err = au_xino_br(sb, br, h_inode->i_ino,
1facf9fc 3271+ au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3272+ if (unlikely(err)) {
3273+ AuDebugOn(br->br_xino.xi_file);
b752ccd1 3274+ goto out_err;
1facf9fc 3275+ }
3276+ }
3277+
3278+ sysaufs_br_init(br);
86dc4139 3279+ path_get(&br->br_path);
b752ccd1 3280+ goto out; /* success */
1facf9fc 3281+
4f0767ce 3282+out_err:
86dc4139 3283+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3284+out:
1facf9fc 3285+ return err;
3286+}
3287+
3288+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3289+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3290+ aufs_bindex_t amount)
3291+{
3292+ struct au_branch **brp;
3293+
dece6358
AM
3294+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3295+
1facf9fc 3296+ brp = sbinfo->si_branch + bindex;
3297+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3298+ *brp = br;
5afbbe0d
AM
3299+ sbinfo->si_bbot++;
3300+ if (unlikely(bbot < 0))
3301+ sbinfo->si_bbot = 0;
1facf9fc 3302+}
3303+
3304+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3305+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3306+{
3307+ struct au_hdentry *hdp;
3308+
1308ab2a 3309+ AuRwMustWriteLock(&dinfo->di_rwsem);
3310+
5afbbe0d 3311+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3312+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3313+ au_h_dentry_init(hdp);
5afbbe0d
AM
3314+ dinfo->di_bbot++;
3315+ if (unlikely(bbot < 0))
3316+ dinfo->di_btop = 0;
1facf9fc 3317+}
3318+
3319+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3320+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3321+{
3322+ struct au_hinode *hip;
3323+
1308ab2a 3324+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3325+
5afbbe0d 3326+ hip = au_hinode(iinfo, bindex);
1facf9fc 3327+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3328+ au_hinode_init(hip);
3329+ iinfo->ii_bbot++;
3330+ if (unlikely(bbot < 0))
3331+ iinfo->ii_btop = 0;
1facf9fc 3332+}
3333+
86dc4139
AM
3334+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3335+ aufs_bindex_t bindex)
1facf9fc 3336+{
86dc4139 3337+ struct dentry *root, *h_dentry;
5527c038 3338+ struct inode *root_inode, *h_inode;
5afbbe0d 3339+ aufs_bindex_t bbot, amount;
1facf9fc 3340+
3341+ root = sb->s_root;
5527c038 3342+ root_inode = d_inode(root);
5afbbe0d
AM
3343+ bbot = au_sbbot(sb);
3344+ amount = bbot + 1 - bindex;
86dc4139 3345+ h_dentry = au_br_dentry(br);
53392da6 3346+ au_sbilist_lock();
5afbbe0d
AM
3347+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3348+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3349+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3350+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3351+ h_inode = d_inode(h_dentry);
3352+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3353+ au_sbilist_unlock();
1facf9fc 3354+}
3355+
3356+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3357+{
3358+ int err;
5afbbe0d 3359+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3360+ struct dentry *root, *h_dentry;
3361+ struct inode *root_inode;
3362+ struct au_branch *add_branch;
3363+
3364+ root = sb->s_root;
5527c038 3365+ root_inode = d_inode(root);
1facf9fc 3366+ IMustLock(root_inode);
5afbbe0d 3367+ IiMustWriteLock(root_inode);
1facf9fc 3368+ err = test_add(sb, add, remount);
3369+ if (unlikely(err < 0))
3370+ goto out;
3371+ if (err) {
3372+ err = 0;
3373+ goto out; /* success */
3374+ }
3375+
5afbbe0d
AM
3376+ bbot = au_sbbot(sb);
3377+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3378+ err = PTR_ERR(add_branch);
3379+ if (IS_ERR(add_branch))
3380+ goto out;
3381+
3382+ err = au_br_init(add_branch, sb, add);
3383+ if (unlikely(err)) {
3384+ au_br_do_free(add_branch);
3385+ goto out;
3386+ }
3387+
3388+ add_bindex = add->bindex;
1facf9fc 3389+ if (!remount)
86dc4139 3390+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3391+ else {
3392+ sysaufs_brs_del(sb, add_bindex);
86dc4139 3393+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3394+ sysaufs_brs_add(sb, add_bindex);
3395+ }
3396+
86dc4139 3397+ h_dentry = add->path.dentry;
1308ab2a 3398+ if (!add_bindex) {
1facf9fc 3399+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3400+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3401+ } else
5527c038 3402+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3403+
3404+ /*
4a4d8108 3405+ * this test/set prevents aufs from handling unnecesary notify events
027c5e7a 3406+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3407+ * once detached from aufs.
3408+ */
3409+ if (au_xino_brid(sb) < 0
3410+ && au_br_writable(add_branch->br_perm)
3411+ && !au_test_fs_bad_xino(h_dentry->d_sb)
3412+ && add_branch->br_xino.xi_file
2000de60 3413+ && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
1facf9fc 3414+ au_xino_brid_set(sb, add_branch->br_id);
3415+
4f0767ce 3416+out:
1facf9fc 3417+ return err;
3418+}
3419+
3420+/* ---------------------------------------------------------------------- */
3421+
79b8bda9 3422+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3423+ unsigned long long max __maybe_unused,
3424+ void *arg)
3425+{
3426+ unsigned long long n;
3427+ struct file **p, *f;
3428+ struct au_sphlhead *files;
3429+ struct au_finfo *finfo;
076b876e
AM
3430+
3431+ n = 0;
3432+ p = a;
3433+ files = &au_sbi(sb)->si_files;
3434+ spin_lock(&files->spin);
3435+ hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3436+ f = finfo->fi_file;
3437+ if (file_count(f)
3438+ && !special_file(file_inode(f)->i_mode)) {
3439+ get_file(f);
3440+ *p++ = f;
3441+ n++;
3442+ AuDebugOn(n > max);
3443+ }
3444+ }
3445+ spin_unlock(&files->spin);
3446+
3447+ return n;
3448+}
3449+
3450+static struct file **au_farray_alloc(struct super_block *sb,
3451+ unsigned long long *max)
3452+{
5afbbe0d 3453+ *max = au_nfiles(sb);
79b8bda9 3454+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3455+}
3456+
3457+static void au_farray_free(struct file **a, unsigned long long max)
3458+{
3459+ unsigned long long ull;
3460+
3461+ for (ull = 0; ull < max; ull++)
3462+ if (a[ull])
3463+ fput(a[ull]);
be52b249 3464+ kvfree(a);
076b876e
AM
3465+}
3466+
3467+/* ---------------------------------------------------------------------- */
3468+
1facf9fc 3469+/*
3470+ * delete a branch
3471+ */
3472+
3473+/* to show the line number, do not make it inlined function */
4a4d8108 3474+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3475+ if (do_info) \
4a4d8108 3476+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3477+} while (0)
3478+
5afbbe0d
AM
3479+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3480+ aufs_bindex_t bbot)
027c5e7a 3481+{
5afbbe0d 3482+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3483+}
3484+
5afbbe0d
AM
3485+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3486+ aufs_bindex_t bbot)
027c5e7a 3487+{
5afbbe0d 3488+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3489+}
3490+
1facf9fc 3491+/*
3492+ * test if the branch is deletable or not.
3493+ */
3494+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3495+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3496+{
3497+ int err, i, j, ndentry;
5afbbe0d 3498+ aufs_bindex_t btop, bbot;
1facf9fc 3499+ struct au_dcsub_pages dpages;
3500+ struct au_dpage *dpage;
3501+ struct dentry *d;
1facf9fc 3502+
3503+ err = au_dpages_init(&dpages, GFP_NOFS);
3504+ if (unlikely(err))
3505+ goto out;
3506+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3507+ if (unlikely(err))
3508+ goto out_dpages;
3509+
1facf9fc 3510+ for (i = 0; !err && i < dpages.ndpage; i++) {
3511+ dpage = dpages.dpages + i;
3512+ ndentry = dpage->ndentry;
3513+ for (j = 0; !err && j < ndentry; j++) {
3514+ d = dpage->dentries[j];
c1595e42 3515+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3516+ if (!au_digen_test(d, sigen)) {
1facf9fc 3517+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3518+ if (unlikely(au_dbrange_test(d))) {
3519+ di_read_unlock(d, AuLock_IR);
3520+ continue;
3521+ }
3522+ } else {
1facf9fc 3523+ di_write_lock_child(d);
027c5e7a
AM
3524+ if (unlikely(au_dbrange_test(d))) {
3525+ di_write_unlock(d);
3526+ continue;
3527+ }
1facf9fc 3528+ err = au_reval_dpath(d, sigen);
3529+ if (!err)
3530+ di_downgrade_lock(d, AuLock_IR);
3531+ else {
3532+ di_write_unlock(d);
3533+ break;
3534+ }
3535+ }
3536+
027c5e7a 3537+ /* AuDbgDentry(d); */
5afbbe0d
AM
3538+ btop = au_dbtop(d);
3539+ bbot = au_dbbot(d);
3540+ if (btop <= bindex
3541+ && bindex <= bbot
1facf9fc 3542+ && au_h_dptr(d, bindex)
5afbbe0d 3543+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3544+ err = -EBUSY;
523b37e3 3545+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3546+ AuDbgDentry(d);
1facf9fc 3547+ }
3548+ di_read_unlock(d, AuLock_IR);
3549+ }
3550+ }
3551+
4f0767ce 3552+out_dpages:
1facf9fc 3553+ au_dpages_free(&dpages);
4f0767ce 3554+out:
1facf9fc 3555+ return err;
3556+}
3557+
3558+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3559+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3560+{
3561+ int err;
7f207e10
AM
3562+ unsigned long long max, ull;
3563+ struct inode *i, **array;
5afbbe0d 3564+ aufs_bindex_t btop, bbot;
1facf9fc 3565+
7f207e10
AM
3566+ array = au_iarray_alloc(sb, &max);
3567+ err = PTR_ERR(array);
3568+ if (IS_ERR(array))
3569+ goto out;
3570+
1facf9fc 3571+ err = 0;
7f207e10
AM
3572+ AuDbg("b%d\n", bindex);
3573+ for (ull = 0; !err && ull < max; ull++) {
3574+ i = array[ull];
076b876e
AM
3575+ if (unlikely(!i))
3576+ break;
7f207e10 3577+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3578+ continue;
3579+
7f207e10 3580+ /* AuDbgInode(i); */
537831f9 3581+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3582+ ii_read_lock_child(i);
3583+ else {
3584+ ii_write_lock_child(i);
027c5e7a
AM
3585+ err = au_refresh_hinode_self(i);
3586+ au_iigen_dec(i);
1facf9fc 3587+ if (!err)
3588+ ii_downgrade_lock(i);
3589+ else {
3590+ ii_write_unlock(i);
3591+ break;
3592+ }
3593+ }
3594+
5afbbe0d
AM
3595+ btop = au_ibtop(i);
3596+ bbot = au_ibbot(i);
3597+ if (btop <= bindex
3598+ && bindex <= bbot
1facf9fc 3599+ && au_h_iptr(i, bindex)
5afbbe0d 3600+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3601+ err = -EBUSY;
3602+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3603+ AuDbgInode(i);
1facf9fc 3604+ }
3605+ ii_read_unlock(i);
3606+ }
7f207e10 3607+ au_iarray_free(array, max);
1facf9fc 3608+
7f207e10 3609+out:
1facf9fc 3610+ return err;
3611+}
3612+
b752ccd1
AM
3613+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3614+ const unsigned int verbose)
1facf9fc 3615+{
3616+ int err;
3617+ unsigned int sigen;
3618+
3619+ sigen = au_sigen(root->d_sb);
3620+ DiMustNoWaiters(root);
5527c038 3621+ IiMustNoWaiters(d_inode(root));
1facf9fc 3622+ di_write_unlock(root);
b752ccd1 3623+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3624+ if (!err)
b752ccd1 3625+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3626+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3627+
3628+ return err;
3629+}
3630+
076b876e
AM
3631+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3632+ struct file **to_free, int *idx)
3633+{
3634+ int err;
c1595e42 3635+ unsigned char matched, root;
5afbbe0d 3636+ aufs_bindex_t bindex, bbot;
076b876e
AM
3637+ struct au_fidir *fidir;
3638+ struct au_hfile *hfile;
3639+
3640+ err = 0;
2000de60 3641+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3642+ if (root) {
3643+ get_file(file);
3644+ to_free[*idx] = file;
3645+ (*idx)++;
3646+ goto out;
3647+ }
3648+
076b876e 3649+ matched = 0;
076b876e
AM
3650+ fidir = au_fi(file)->fi_hdir;
3651+ AuDebugOn(!fidir);
5afbbe0d
AM
3652+ bbot = au_fbbot_dir(file);
3653+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3654+ hfile = fidir->fd_hfile + bindex;
3655+ if (!hfile->hf_file)
3656+ continue;
3657+
c1595e42 3658+ if (hfile->hf_br->br_id == br_id) {
076b876e 3659+ matched = 1;
076b876e 3660+ break;
c1595e42 3661+ }
076b876e 3662+ }
c1595e42 3663+ if (matched)
076b876e
AM
3664+ err = -EBUSY;
3665+
3666+out:
3667+ return err;
3668+}
3669+
3670+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3671+ struct file **to_free, int opened)
3672+{
3673+ int err, idx;
3674+ unsigned long long ull, max;
5afbbe0d 3675+ aufs_bindex_t btop;
076b876e 3676+ struct file *file, **array;
076b876e
AM
3677+ struct dentry *root;
3678+ struct au_hfile *hfile;
3679+
3680+ array = au_farray_alloc(sb, &max);
3681+ err = PTR_ERR(array);
3682+ if (IS_ERR(array))
3683+ goto out;
3684+
3685+ err = 0;
3686+ idx = 0;
3687+ root = sb->s_root;
3688+ di_write_unlock(root);
3689+ for (ull = 0; ull < max; ull++) {
3690+ file = array[ull];
3691+ if (unlikely(!file))
3692+ break;
3693+
3694+ /* AuDbg("%pD\n", file); */
3695+ fi_read_lock(file);
5afbbe0d 3696+ btop = au_fbtop(file);
2000de60 3697+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3698+ hfile = &au_fi(file)->fi_htop;
3699+ if (hfile->hf_br->br_id == br_id)
3700+ err = -EBUSY;
3701+ } else
3702+ err = test_dir_busy(file, br_id, to_free, &idx);
3703+ fi_read_unlock(file);
3704+ if (unlikely(err))
3705+ break;
3706+ }
3707+ di_write_lock_child(root);
3708+ au_farray_free(array, max);
3709+ AuDebugOn(idx > opened);
3710+
3711+out:
3712+ return err;
3713+}
3714+
3715+static void br_del_file(struct file **to_free, unsigned long long opened,
3716+ aufs_bindex_t br_id)
3717+{
3718+ unsigned long long ull;
5afbbe0d 3719+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3720+ struct file *file;
3721+ struct au_fidir *fidir;
3722+ struct au_hfile *hfile;
3723+
3724+ for (ull = 0; ull < opened; ull++) {
3725+ file = to_free[ull];
3726+ if (unlikely(!file))
3727+ break;
3728+
3729+ /* AuDbg("%pD\n", file); */
2000de60 3730+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3731+ bfound = -1;
3732+ fidir = au_fi(file)->fi_hdir;
3733+ AuDebugOn(!fidir);
3734+ fi_write_lock(file);
5afbbe0d
AM
3735+ btop = au_fbtop(file);
3736+ bbot = au_fbbot_dir(file);
3737+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3738+ hfile = fidir->fd_hfile + bindex;
3739+ if (!hfile->hf_file)
3740+ continue;
3741+
3742+ if (hfile->hf_br->br_id == br_id) {
3743+ bfound = bindex;
3744+ break;
3745+ }
3746+ }
3747+ AuDebugOn(bfound < 0);
3748+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3749+ if (bfound == btop) {
3750+ for (btop++; btop <= bbot; btop++)
3751+ if (au_hf_dir(file, btop)) {
3752+ au_set_fbtop(file, btop);
076b876e
AM
3753+ break;
3754+ }
3755+ }
3756+ fi_write_unlock(file);
3757+ }
3758+}
3759+
1facf9fc 3760+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3761+ const aufs_bindex_t bindex,
5afbbe0d 3762+ const aufs_bindex_t bbot)
1facf9fc 3763+{
3764+ struct au_branch **brp, **p;
3765+
dece6358
AM
3766+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3767+
1facf9fc 3768+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3769+ if (bindex < bbot)
3770+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3771+ sbinfo->si_branch[0 + bbot] = NULL;
3772+ sbinfo->si_bbot--;
1facf9fc 3773+
5afbbe0d 3774+ p = krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST);
1facf9fc 3775+ if (p)
3776+ sbinfo->si_branch = p;
4a4d8108 3777+ /* harmless error */
1facf9fc 3778+}
3779+
3780+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3781+ const aufs_bindex_t bbot)
1facf9fc 3782+{
3783+ struct au_hdentry *hdp, *p;
3784+
1308ab2a 3785+ AuRwMustWriteLock(&dinfo->di_rwsem);
3786+
5afbbe0d
AM
3787+ hdp = au_hdentry(dinfo, bindex);
3788+ if (bindex < bbot)
3789+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3790+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3791+ dinfo->di_bbot--;
1facf9fc 3792+
5afbbe0d 3793+ p = krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST);
1facf9fc 3794+ if (p)
3795+ dinfo->di_hdentry = p;
4a4d8108 3796+ /* harmless error */
1facf9fc 3797+}
3798+
3799+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3800+ const aufs_bindex_t bbot)
1facf9fc 3801+{
3802+ struct au_hinode *hip, *p;
3803+
1308ab2a 3804+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3805+
5afbbe0d
AM
3806+ hip = au_hinode(iinfo, bindex);
3807+ if (bindex < bbot)
3808+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3809+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3810+ iinfo->ii_bbot--;
1facf9fc 3811+
5afbbe0d 3812+ p = krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST);
1facf9fc 3813+ if (p)
3814+ iinfo->ii_hinode = p;
4a4d8108 3815+ /* harmless error */
1facf9fc 3816+}
3817+
3818+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3819+ struct au_branch *br)
3820+{
5afbbe0d 3821+ aufs_bindex_t bbot;
1facf9fc 3822+ struct au_sbinfo *sbinfo;
53392da6
AM
3823+ struct dentry *root, *h_root;
3824+ struct inode *inode, *h_inode;
3825+ struct au_hinode *hinode;
1facf9fc 3826+
dece6358
AM
3827+ SiMustWriteLock(sb);
3828+
1facf9fc 3829+ root = sb->s_root;
5527c038 3830+ inode = d_inode(root);
1facf9fc 3831+ sbinfo = au_sbi(sb);
5afbbe0d 3832+ bbot = sbinfo->si_bbot;
1facf9fc 3833+
53392da6
AM
3834+ h_root = au_h_dptr(root, bindex);
3835+ hinode = au_hi(inode, bindex);
3836+ h_inode = au_igrab(hinode->hi_inode);
3837+ au_hiput(hinode);
1facf9fc 3838+
53392da6 3839+ au_sbilist_lock();
5afbbe0d
AM
3840+ au_br_do_del_brp(sbinfo, bindex, bbot);
3841+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3842+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
3843+ au_sbilist_unlock();
3844+
3845+ dput(h_root);
3846+ iput(h_inode);
3847+ au_br_do_free(br);
1facf9fc 3848+}
3849+
79b8bda9
AM
3850+static unsigned long long empty_cb(struct super_block *sb, void *array,
3851+ unsigned long long max, void *arg)
076b876e
AM
3852+{
3853+ return max;
3854+}
3855+
1facf9fc 3856+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3857+{
3858+ int err, rerr, i;
076b876e 3859+ unsigned long long opened;
1facf9fc 3860+ unsigned int mnt_flags;
5afbbe0d 3861+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 3862+ unsigned char do_wh, verbose;
3863+ struct au_branch *br;
3864+ struct au_wbr *wbr;
076b876e
AM
3865+ struct dentry *root;
3866+ struct file **to_free;
1facf9fc 3867+
3868+ err = 0;
076b876e
AM
3869+ opened = 0;
3870+ to_free = NULL;
3871+ root = sb->s_root;
3872+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 3873+ if (bindex < 0) {
3874+ if (remount)
3875+ goto out; /* success */
3876+ err = -ENOENT;
4a4d8108 3877+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 3878+ goto out;
3879+ }
3880+ AuDbg("bindex b%d\n", bindex);
3881+
3882+ err = -EBUSY;
3883+ mnt_flags = au_mntflags(sb);
3884+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
3885+ bbot = au_sbbot(sb);
3886+ if (unlikely(!bbot)) {
1facf9fc 3887+ AuVerbose(verbose, "no more branches left\n");
3888+ goto out;
3889+ }
3890+ br = au_sbr(sb, bindex);
86dc4139 3891+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
3892+
3893+ br_id = br->br_id;
5afbbe0d 3894+ opened = au_br_count(br);
076b876e 3895+ if (unlikely(opened)) {
79b8bda9 3896+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
3897+ err = PTR_ERR(to_free);
3898+ if (IS_ERR(to_free))
3899+ goto out;
3900+
3901+ err = test_file_busy(sb, br_id, to_free, opened);
3902+ if (unlikely(err)) {
3903+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3904+ goto out;
3905+ }
1facf9fc 3906+ }
3907+
3908+ wbr = br->br_wbr;
3909+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3910+ if (do_wh) {
1308ab2a 3911+ /* instead of WbrWhMustWriteLock(wbr) */
3912+ SiMustWriteLock(sb);
1facf9fc 3913+ for (i = 0; i < AuBrWh_Last; i++) {
3914+ dput(wbr->wbr_wh[i]);
3915+ wbr->wbr_wh[i] = NULL;
3916+ }
3917+ }
3918+
076b876e 3919+ err = test_children_busy(root, bindex, verbose);
1facf9fc 3920+ if (unlikely(err)) {
3921+ if (do_wh)
3922+ goto out_wh;
3923+ goto out;
3924+ }
3925+
3926+ err = 0;
076b876e
AM
3927+ if (to_free) {
3928+ /*
3929+ * now we confirmed the branch is deletable.
3930+ * let's free the remaining opened dirs on the branch.
3931+ */
3932+ di_write_unlock(root);
3933+ br_del_file(to_free, opened, br_id);
3934+ di_write_lock_child(root);
3935+ }
3936+
1facf9fc 3937+ if (!remount)
3938+ au_br_do_del(sb, bindex, br);
3939+ else {
3940+ sysaufs_brs_del(sb, bindex);
3941+ au_br_do_del(sb, bindex, br);
3942+ sysaufs_brs_add(sb, bindex);
3943+ }
3944+
1308ab2a 3945+ if (!bindex) {
5527c038 3946+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 3947+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3948+ } else
5527c038 3949+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 3950+ if (au_opt_test(mnt_flags, PLINK))
3951+ au_plink_half_refresh(sb, br_id);
3952+
b752ccd1 3953+ if (au_xino_brid(sb) == br_id)
1facf9fc 3954+ au_xino_brid_set(sb, -1);
3955+ goto out; /* success */
3956+
4f0767ce 3957+out_wh:
1facf9fc 3958+ /* revert */
86dc4139 3959+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 3960+ if (rerr)
0c3ec466
AM
3961+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3962+ del->pathname, rerr);
4f0767ce 3963+out:
076b876e
AM
3964+ if (to_free)
3965+ au_farray_free(to_free, opened);
1facf9fc 3966+ return err;
3967+}
3968+
3969+/* ---------------------------------------------------------------------- */
3970+
027c5e7a
AM
3971+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3972+{
3973+ int err;
5afbbe0d 3974+ aufs_bindex_t btop, bbot;
027c5e7a
AM
3975+ struct aufs_ibusy ibusy;
3976+ struct inode *inode, *h_inode;
3977+
3978+ err = -EPERM;
3979+ if (unlikely(!capable(CAP_SYS_ADMIN)))
3980+ goto out;
3981+
3982+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3983+ if (!err)
3984+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3985+ if (unlikely(err)) {
3986+ err = -EFAULT;
3987+ AuTraceErr(err);
3988+ goto out;
3989+ }
3990+
3991+ err = -EINVAL;
3992+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 3993+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
3994+ goto out_unlock;
3995+
3996+ err = 0;
3997+ ibusy.h_ino = 0; /* invalid */
3998+ inode = ilookup(sb, ibusy.ino);
3999+ if (!inode
4000+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 4001+ || au_is_bad_inode(inode))
027c5e7a
AM
4002+ goto out_unlock;
4003+
4004+ ii_read_lock_child(inode);
5afbbe0d
AM
4005+ btop = au_ibtop(inode);
4006+ bbot = au_ibbot(inode);
4007+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4008+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4009+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4010+ ibusy.h_ino = h_inode->i_ino;
4011+ }
4012+ ii_read_unlock(inode);
4013+ iput(inode);
4014+
4015+out_unlock:
4016+ si_read_unlock(sb);
4017+ if (!err) {
4018+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4019+ if (unlikely(err)) {
4020+ err = -EFAULT;
4021+ AuTraceErr(err);
4022+ }
4023+ }
4024+out:
4025+ return err;
4026+}
4027+
4028+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4029+{
2000de60 4030+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4031+}
4032+
4033+#ifdef CONFIG_COMPAT
4034+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4035+{
2000de60 4036+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4037+}
4038+#endif
4039+
4040+/* ---------------------------------------------------------------------- */
4041+
1facf9fc 4042+/*
4043+ * change a branch permission
4044+ */
4045+
dece6358
AM
4046+static void au_warn_ima(void)
4047+{
4048+#ifdef CONFIG_IMA
1308ab2a 4049+ /* since it doesn't support mark_files_ro() */
027c5e7a 4050+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4051+#endif
4052+}
4053+
1facf9fc 4054+static int do_need_sigen_inc(int a, int b)
4055+{
4056+ return au_br_whable(a) && !au_br_whable(b);
4057+}
4058+
4059+static int need_sigen_inc(int old, int new)
4060+{
4061+ return do_need_sigen_inc(old, new)
4062+ || do_need_sigen_inc(new, old);
4063+}
4064+
4065+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4066+{
7f207e10 4067+ int err, do_warn;
027c5e7a 4068+ unsigned int mnt_flags;
7f207e10 4069+ unsigned long long ull, max;
e49829fe 4070+ aufs_bindex_t br_id;
38d290e6 4071+ unsigned char verbose, writer;
7f207e10 4072+ struct file *file, *hf, **array;
e49829fe 4073+ struct au_hfile *hfile;
1facf9fc 4074+
027c5e7a
AM
4075+ mnt_flags = au_mntflags(sb);
4076+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4077+
7f207e10
AM
4078+ array = au_farray_alloc(sb, &max);
4079+ err = PTR_ERR(array);
4080+ if (IS_ERR(array))
1facf9fc 4081+ goto out;
4082+
7f207e10 4083+ do_warn = 0;
e49829fe 4084+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4085+ for (ull = 0; ull < max; ull++) {
4086+ file = array[ull];
076b876e
AM
4087+ if (unlikely(!file))
4088+ break;
1facf9fc 4089+
523b37e3 4090+ /* AuDbg("%pD\n", file); */
1facf9fc 4091+ fi_read_lock(file);
4092+ if (unlikely(au_test_mmapped(file))) {
4093+ err = -EBUSY;
523b37e3 4094+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4095+ AuDbgFile(file);
1facf9fc 4096+ FiMustNoWaiters(file);
4097+ fi_read_unlock(file);
7f207e10 4098+ goto out_array;
1facf9fc 4099+ }
4100+
e49829fe
JR
4101+ hfile = &au_fi(file)->fi_htop;
4102+ hf = hfile->hf_file;
7e9cd9fe 4103+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4104+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4105+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4106+ || !(hf->f_mode & FMODE_WRITE))
4107+ array[ull] = NULL;
4108+ else {
4109+ do_warn = 1;
4110+ get_file(file);
1facf9fc 4111+ }
4112+
1facf9fc 4113+ FiMustNoWaiters(file);
4114+ fi_read_unlock(file);
7f207e10
AM
4115+ fput(file);
4116+ }
1facf9fc 4117+
4118+ err = 0;
7f207e10 4119+ if (do_warn)
dece6358 4120+ au_warn_ima();
7f207e10
AM
4121+
4122+ for (ull = 0; ull < max; ull++) {
4123+ file = array[ull];
4124+ if (!file)
4125+ continue;
4126+
1facf9fc 4127+ /* todo: already flushed? */
523b37e3
AM
4128+ /*
4129+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4130+ * approach which resets f_mode and calls mnt_drop_write() and
4131+ * file_release_write() for each file, because the branch
4132+ * attribute in aufs world is totally different from the native
4133+ * fs rw/ro mode.
4134+ */
7f207e10
AM
4135+ /* fi_read_lock(file); */
4136+ hfile = &au_fi(file)->fi_htop;
4137+ hf = hfile->hf_file;
4138+ /* fi_read_unlock(file); */
027c5e7a 4139+ spin_lock(&hf->f_lock);
38d290e6
JR
4140+ writer = !!(hf->f_mode & FMODE_WRITER);
4141+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4142+ spin_unlock(&hf->f_lock);
38d290e6
JR
4143+ if (writer) {
4144+ put_write_access(file_inode(hf));
c06a8ce3 4145+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4146+ }
4147+ }
4148+
7f207e10
AM
4149+out_array:
4150+ au_farray_free(array, max);
4f0767ce 4151+out:
7f207e10 4152+ AuTraceErr(err);
1facf9fc 4153+ return err;
4154+}
4155+
4156+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4157+ int *do_refresh)
1facf9fc 4158+{
4159+ int err, rerr;
4160+ aufs_bindex_t bindex;
4161+ struct dentry *root;
4162+ struct au_branch *br;
076b876e 4163+ struct au_br_fhsm *bf;
1facf9fc 4164+
4165+ root = sb->s_root;
1facf9fc 4166+ bindex = au_find_dbindex(root, mod->h_root);
4167+ if (bindex < 0) {
4168+ if (remount)
4169+ return 0; /* success */
4170+ err = -ENOENT;
4a4d8108 4171+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4172+ goto out;
4173+ }
4174+ AuDbg("bindex b%d\n", bindex);
4175+
5527c038 4176+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4177+ if (unlikely(err))
4178+ goto out;
4179+
4180+ br = au_sbr(sb, bindex);
86dc4139 4181+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4182+ if (br->br_perm == mod->perm)
4183+ return 0; /* success */
4184+
076b876e
AM
4185+ /* pre-allocate for non-fhsm --> fhsm */
4186+ bf = NULL;
4187+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4188+ err = au_fhsm_br_alloc(br);
4189+ if (unlikely(err))
4190+ goto out;
4191+ bf = br->br_fhsm;
4192+ br->br_fhsm = NULL;
4193+ }
4194+
1facf9fc 4195+ if (au_br_writable(br->br_perm)) {
4196+ /* remove whiteout base */
86dc4139 4197+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4198+ if (unlikely(err))
076b876e 4199+ goto out_bf;
1facf9fc 4200+
4201+ if (!au_br_writable(mod->perm)) {
4202+ /* rw --> ro, file might be mmapped */
4203+ DiMustNoWaiters(root);
5527c038 4204+ IiMustNoWaiters(d_inode(root));
1facf9fc 4205+ di_write_unlock(root);
4206+ err = au_br_mod_files_ro(sb, bindex);
4207+ /* aufs_write_lock() calls ..._child() */
4208+ di_write_lock_child(root);
4209+
4210+ if (unlikely(err)) {
4211+ rerr = -ENOMEM;
be52b249 4212+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4213+ GFP_NOFS);
86dc4139
AM
4214+ if (br->br_wbr)
4215+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4216+ if (unlikely(rerr)) {
4217+ AuIOErr("nested error %d (%d)\n",
4218+ rerr, err);
4219+ br->br_perm = mod->perm;
4220+ }
4221+ }
4222+ }
4223+ } else if (au_br_writable(mod->perm)) {
4224+ /* ro --> rw */
4225+ err = -ENOMEM;
be52b249 4226+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4227+ if (br->br_wbr) {
86dc4139 4228+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4229+ if (unlikely(err)) {
f0c0a007 4230+ au_delayed_kfree(br->br_wbr);
1facf9fc 4231+ br->br_wbr = NULL;
4232+ }
4233+ }
4234+ }
076b876e
AM
4235+ if (unlikely(err))
4236+ goto out_bf;
4237+
4238+ if (au_br_fhsm(br->br_perm)) {
4239+ if (!au_br_fhsm(mod->perm)) {
4240+ /* fhsm --> non-fhsm */
4241+ au_br_fhsm_fin(br->br_fhsm);
f0c0a007 4242+ au_delayed_kfree(br->br_fhsm);
076b876e
AM
4243+ br->br_fhsm = NULL;
4244+ }
4245+ } else if (au_br_fhsm(mod->perm))
4246+ /* non-fhsm --> fhsm */
4247+ br->br_fhsm = bf;
4248+
076b876e
AM
4249+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4250+ br->br_perm = mod->perm;
4251+ goto out; /* success */
1facf9fc 4252+
076b876e 4253+out_bf:
f0c0a007
AM
4254+ if (bf)
4255+ au_delayed_kfree(bf);
076b876e
AM
4256+out:
4257+ AuTraceErr(err);
4258+ return err;
4259+}
4260+
4261+/* ---------------------------------------------------------------------- */
4262+
4263+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4264+{
4265+ int err;
4266+ struct kstatfs kstfs;
4267+
4268+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4269+ if (!err) {
076b876e
AM
4270+ stfs->f_blocks = kstfs.f_blocks;
4271+ stfs->f_bavail = kstfs.f_bavail;
4272+ stfs->f_files = kstfs.f_files;
4273+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4274+ }
4275+
1facf9fc 4276+ return err;
4277+}
7f207e10
AM
4278diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4279--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
4280+++ linux/fs/aufs/branch.h 2016-07-25 19:05:34.811159821 +0200
4281@@ -0,0 +1,309 @@
1facf9fc 4282+/*
8cdd5066 4283+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 4284+ *
4285+ * This program, aufs is free software; you can redistribute it and/or modify
4286+ * it under the terms of the GNU General Public License as published by
4287+ * the Free Software Foundation; either version 2 of the License, or
4288+ * (at your option) any later version.
dece6358
AM
4289+ *
4290+ * This program is distributed in the hope that it will be useful,
4291+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4292+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4293+ * GNU General Public License for more details.
4294+ *
4295+ * You should have received a copy of the GNU General Public License
523b37e3 4296+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4297+ */
4298+
4299+/*
4300+ * branch filesystems and xino for them
4301+ */
4302+
4303+#ifndef __AUFS_BRANCH_H__
4304+#define __AUFS_BRANCH_H__
4305+
4306+#ifdef __KERNEL__
4307+
1facf9fc 4308+#include <linux/mount.h>
4a4d8108 4309+#include "dynop.h"
1facf9fc 4310+#include "rwsem.h"
4311+#include "super.h"
4312+
4313+/* ---------------------------------------------------------------------- */
4314+
4315+/* a xino file */
4316+struct au_xino_file {
4317+ struct file *xi_file;
4318+ struct mutex xi_nondir_mtx;
4319+
4320+ /* todo: make xino files an array to support huge inode number */
4321+
4322+#ifdef CONFIG_DEBUG_FS
4323+ struct dentry *xi_dbgaufs;
4324+#endif
4325+};
4326+
076b876e
AM
4327+/* File-based Hierarchical Storage Management */
4328+struct au_br_fhsm {
4329+#ifdef CONFIG_AUFS_FHSM
4330+ struct mutex bf_lock;
4331+ unsigned long bf_jiffy;
4332+ struct aufs_stfs bf_stfs;
4333+ int bf_readable;
4334+#endif
4335+};
4336+
1facf9fc 4337+/* members for writable branch only */
4338+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4339+struct au_wbr {
dece6358 4340+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4341+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4342+ atomic_t wbr_wh_running;
1facf9fc 4343+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4344+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4345+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4346+
4347+ /* mfs mode */
4348+ unsigned long long wbr_bytes;
4349+};
4350+
4a4d8108
AM
4351+/* ext2 has 3 types of operations at least, ext3 has 4 */
4352+#define AuBrDynOp (AuDyLast * 4)
4353+
1716fcea
AM
4354+#ifdef CONFIG_AUFS_HFSNOTIFY
4355+/* support for asynchronous destruction */
4356+struct au_br_hfsnotify {
4357+ struct fsnotify_group *hfsn_group;
4358+};
4359+#endif
4360+
392086de
AM
4361+/* sysfs entries */
4362+struct au_brsysfs {
4363+ char name[16];
4364+ struct attribute attr;
4365+};
4366+
4367+enum {
4368+ AuBrSysfs_BR,
4369+ AuBrSysfs_BRID,
4370+ AuBrSysfs_Last
4371+};
4372+
1facf9fc 4373+/* protected by superblock rwsem */
4374+struct au_branch {
4375+ struct au_xino_file br_xino;
4376+
4377+ aufs_bindex_t br_id;
4378+
4379+ int br_perm;
86dc4139 4380+ struct path br_path;
4a4d8108
AM
4381+ spinlock_t br_dykey_lock;
4382+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4383+ struct percpu_counter br_count;
1facf9fc 4384+
4385+ struct au_wbr *br_wbr;
076b876e 4386+ struct au_br_fhsm *br_fhsm;
1facf9fc 4387+
4388+ /* xino truncation */
1facf9fc 4389+ atomic_t br_xino_running;
4390+
027c5e7a 4391+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4392+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4393+#endif
4394+
1facf9fc 4395+#ifdef CONFIG_SYSFS
392086de
AM
4396+ /* entries under sysfs per mount-point */
4397+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4398+#endif
4399+};
4400+
4401+/* ---------------------------------------------------------------------- */
4402+
86dc4139
AM
4403+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4404+{
4405+ return br->br_path.mnt;
4406+}
4407+
4408+static inline struct dentry *au_br_dentry(struct au_branch *br)
4409+{
4410+ return br->br_path.dentry;
4411+}
4412+
4413+static inline struct super_block *au_br_sb(struct au_branch *br)
4414+{
4415+ return au_br_mnt(br)->mnt_sb;
4416+}
4417+
5afbbe0d
AM
4418+static inline void au_br_get(struct au_branch *br)
4419+{
4420+ percpu_counter_inc(&br->br_count);
4421+}
4422+
4423+static inline void au_br_put(struct au_branch *br)
4424+{
4425+ percpu_counter_dec(&br->br_count);
4426+}
4427+
4428+static inline s64 au_br_count(struct au_branch *br)
4429+{
4430+ return percpu_counter_sum(&br->br_count);
4431+}
4432+
4433+static inline void au_br_count_init(struct au_branch *br)
4434+{
4435+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4436+}
4437+
4438+static inline void au_br_count_fin(struct au_branch *br)
4439+{
4440+ percpu_counter_destroy(&br->br_count);
4441+}
4442+
1facf9fc 4443+static inline int au_br_rdonly(struct au_branch *br)
4444+{
86dc4139 4445+ return ((au_br_sb(br)->s_flags & MS_RDONLY)
1facf9fc 4446+ || !au_br_writable(br->br_perm))
4447+ ? -EROFS : 0;
4448+}
4449+
4a4d8108 4450+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4451+{
4a4d8108 4452+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4453+ return !(brperm & AuBrPerm_RR);
1facf9fc 4454+#else
4455+ return 0;
4456+#endif
4457+}
4458+
b912730e
AM
4459+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4460+{
4461+ int err, exec_flag;
4462+
4463+ err = 0;
4464+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4465+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4466+ err = -EACCES;
4467+
4468+ return err;
4469+}
4470+
1facf9fc 4471+/* ---------------------------------------------------------------------- */
4472+
4473+/* branch.c */
4474+struct au_sbinfo;
4475+void au_br_free(struct au_sbinfo *sinfo);
4476+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4477+struct au_opt_add;
4478+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4479+struct au_opt_del;
4480+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4481+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4482+#ifdef CONFIG_COMPAT
4483+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4484+#endif
1facf9fc 4485+struct au_opt_mod;
4486+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4487+ int *do_refresh);
076b876e
AM
4488+struct aufs_stfs;
4489+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4490+
4491+/* xino.c */
4492+static const loff_t au_loff_max = LLONG_MAX;
4493+
4494+int au_xib_trunc(struct super_block *sb);
5527c038 4495+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4496+ loff_t *pos);
5527c038
JR
4497+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4498+ size_t size, loff_t *pos);
1facf9fc 4499+struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4500+struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4501+ino_t au_xino_new_ino(struct super_block *sb);
b752ccd1 4502+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4503+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4504+ ino_t ino);
4505+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4506+ ino_t *ino);
4507+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4508+ struct file *base_file, int do_test);
4509+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4510+
4511+struct au_opt_xino;
4512+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4513+void au_xino_clr(struct super_block *sb);
4514+struct file *au_xino_def(struct super_block *sb);
4515+int au_xino_path(struct seq_file *seq, struct file *file);
4516+
4517+/* ---------------------------------------------------------------------- */
4518+
4519+/* Superblock to branch */
4520+static inline
4521+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4522+{
4523+ return au_sbr(sb, bindex)->br_id;
4524+}
4525+
4526+static inline
4527+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4528+{
86dc4139 4529+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4530+}
4531+
4532+static inline
4533+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4534+{
86dc4139 4535+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4536+}
4537+
5afbbe0d
AM
4538+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4539+{
4540+ au_br_get(au_sbr(sb, bindex));
4541+}
4542+
1facf9fc 4543+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4544+{
5afbbe0d 4545+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4546+}
4547+
4548+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4549+{
4550+ return au_sbr(sb, bindex)->br_perm;
4551+}
4552+
4553+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4554+{
4555+ return au_br_whable(au_sbr_perm(sb, bindex));
4556+}
4557+
4558+/* ---------------------------------------------------------------------- */
4559+
4560+/*
4561+ * wbr_wh_read_lock, wbr_wh_write_lock
4562+ * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4563+ */
4564+AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4565+
dece6358
AM
4566+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4567+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4568+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4569+
076b876e
AM
4570+/* ---------------------------------------------------------------------- */
4571+
4572+#ifdef CONFIG_AUFS_FHSM
4573+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4574+{
4575+ mutex_init(&brfhsm->bf_lock);
4576+ brfhsm->bf_jiffy = 0;
4577+ brfhsm->bf_readable = 0;
4578+}
4579+
4580+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4581+{
4582+ mutex_destroy(&brfhsm->bf_lock);
4583+}
4584+#else
4585+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4586+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4587+#endif
4588+
1facf9fc 4589+#endif /* __KERNEL__ */
4590+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4591diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4592--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 4593+++ linux/fs/aufs/conf.mk 2015-09-24 10:47:58.248052907 +0200
c1595e42 4594@@ -0,0 +1,38 @@
4a4d8108
AM
4595+
4596+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4597+
4598+define AuConf
4599+ifdef ${1}
4600+AuConfStr += ${1}=${${1}}
4601+endif
4602+endef
4603+
b752ccd1 4604+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4605+ SBILIST \
7f207e10 4606+ HNOTIFY HFSNOTIFY \
4a4d8108 4607+ EXPORT INO_T_64 \
c1595e42 4608+ XATTR \
076b876e 4609+ FHSM \
4a4d8108 4610+ RDU \
4a4d8108
AM
4611+ SHWH \
4612+ BR_RAMFS \
4613+ BR_FUSE POLL \
4614+ BR_HFSPLUS \
4615+ BDEV_LOOP \
b752ccd1
AM
4616+ DEBUG MAGIC_SYSRQ
4617+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4618+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4619+
4620+AuConfName = ${obj}/conf.str
4621+${AuConfName}.tmp: FORCE
4622+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4623+${AuConfName}: ${AuConfName}.tmp
4624+ @diff -q $< $@ > /dev/null 2>&1 || { \
4625+ echo ' GEN ' $@; \
4626+ cp -p $< $@; \
4627+ }
4628+FORCE:
4629+clean-files += ${AuConfName} ${AuConfName}.tmp
4630+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4631+
4632+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4633diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4634--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 4635+++ linux/fs/aufs/cpup.c 2016-08-17 18:01:06.095221547 +0200
5afbbe0d 4636@@ -0,0 +1,1383 @@
1facf9fc 4637+/*
8cdd5066 4638+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 4639+ *
4640+ * This program, aufs is free software; you can redistribute it and/or modify
4641+ * it under the terms of the GNU General Public License as published by
4642+ * the Free Software Foundation; either version 2 of the License, or
4643+ * (at your option) any later version.
dece6358
AM
4644+ *
4645+ * This program is distributed in the hope that it will be useful,
4646+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4647+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4648+ * GNU General Public License for more details.
4649+ *
4650+ * You should have received a copy of the GNU General Public License
523b37e3 4651+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4652+ */
4653+
4654+/*
4655+ * copy-up functions, see wbr_policy.c for copy-down
4656+ */
4657+
4658+#include <linux/fs_stack.h>
dece6358 4659+#include <linux/mm.h>
8cdd5066 4660+#include <linux/task_work.h>
1facf9fc 4661+#include "aufs.h"
4662+
86dc4139 4663+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4664+{
4665+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4666+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4667+
86dc4139
AM
4668+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4669+
4670+ dst->i_flags |= iflags & ~mask;
1facf9fc 4671+ if (au_test_fs_notime(dst->i_sb))
4672+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4673+}
4674+
4675+void au_cpup_attr_timesizes(struct inode *inode)
4676+{
4677+ struct inode *h_inode;
4678+
5afbbe0d 4679+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4680+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4681+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4682+}
4683+
4684+void au_cpup_attr_nlink(struct inode *inode, int force)
4685+{
4686+ struct inode *h_inode;
4687+ struct super_block *sb;
5afbbe0d 4688+ aufs_bindex_t bindex, bbot;
1facf9fc 4689+
4690+ sb = inode->i_sb;
5afbbe0d 4691+ bindex = au_ibtop(inode);
1facf9fc 4692+ h_inode = au_h_iptr(inode, bindex);
4693+ if (!force
4694+ && !S_ISDIR(h_inode->i_mode)
4695+ && au_opt_test(au_mntflags(sb), PLINK)
4696+ && au_plink_test(inode))
4697+ return;
4698+
7eafdf33
AM
4699+ /*
4700+ * 0 can happen in revalidating.
38d290e6
JR
4701+ * h_inode->i_mutex may not be held here, but it is harmless since once
4702+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4703+ * case.
4704+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4705+ * the incorrect link count.
7eafdf33 4706+ */
92d182d2 4707+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4708+
4709+ /*
4710+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4711+ * it may includes whplink directory.
4712+ */
4713+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4714+ bbot = au_ibbot(inode);
4715+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4716+ h_inode = au_h_iptr(inode, bindex);
4717+ if (h_inode)
4718+ au_add_nlink(inode, h_inode);
4719+ }
4720+ }
4721+}
4722+
4723+void au_cpup_attr_changeable(struct inode *inode)
4724+{
4725+ struct inode *h_inode;
4726+
5afbbe0d 4727+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4728+ inode->i_mode = h_inode->i_mode;
4729+ inode->i_uid = h_inode->i_uid;
4730+ inode->i_gid = h_inode->i_gid;
4731+ au_cpup_attr_timesizes(inode);
86dc4139 4732+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4733+}
4734+
4735+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4736+{
4737+ struct au_iinfo *iinfo = au_ii(inode);
4738+
1308ab2a 4739+ IiMustWriteLock(inode);
4740+
1facf9fc 4741+ iinfo->ii_higen = h_inode->i_generation;
4742+ iinfo->ii_hsb1 = h_inode->i_sb;
4743+}
4744+
4745+void au_cpup_attr_all(struct inode *inode, int force)
4746+{
4747+ struct inode *h_inode;
4748+
5afbbe0d 4749+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4750+ au_cpup_attr_changeable(inode);
4751+ if (inode->i_nlink > 0)
4752+ au_cpup_attr_nlink(inode, force);
4753+ inode->i_rdev = h_inode->i_rdev;
4754+ inode->i_blkbits = h_inode->i_blkbits;
4755+ au_cpup_igen(inode, h_inode);
4756+}
4757+
4758+/* ---------------------------------------------------------------------- */
4759+
4760+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4761+
4762+/* keep the timestamps of the parent dir when cpup */
4763+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4764+ struct path *h_path)
4765+{
4766+ struct inode *h_inode;
4767+
4768+ dt->dt_dentry = dentry;
4769+ dt->dt_h_path = *h_path;
5527c038 4770+ h_inode = d_inode(h_path->dentry);
1facf9fc 4771+ dt->dt_atime = h_inode->i_atime;
4772+ dt->dt_mtime = h_inode->i_mtime;
4773+ /* smp_mb(); */
4774+}
4775+
4776+void au_dtime_revert(struct au_dtime *dt)
4777+{
4778+ struct iattr attr;
4779+ int err;
4780+
4781+ attr.ia_atime = dt->dt_atime;
4782+ attr.ia_mtime = dt->dt_mtime;
4783+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4784+ | ATTR_ATIME | ATTR_ATIME_SET;
4785+
523b37e3
AM
4786+ /* no delegation since this is a directory */
4787+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 4788+ if (unlikely(err))
0c3ec466 4789+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 4790+}
4791+
4792+/* ---------------------------------------------------------------------- */
4793+
86dc4139
AM
4794+/* internal use only */
4795+struct au_cpup_reg_attr {
4796+ int valid;
4797+ struct kstat st;
4798+ unsigned int iflags; /* inode->i_flags */
4799+};
4800+
1facf9fc 4801+static noinline_for_stack
86dc4139
AM
4802+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4803+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 4804+{
c1595e42 4805+ int err, sbits, icex;
7e9cd9fe
AM
4806+ unsigned int mnt_flags;
4807+ unsigned char verbose;
1facf9fc 4808+ struct iattr ia;
4809+ struct path h_path;
1308ab2a 4810+ struct inode *h_isrc, *h_idst;
86dc4139 4811+ struct kstat *h_st;
c1595e42 4812+ struct au_branch *br;
1facf9fc 4813+
4814+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 4815+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
4816+ br = au_sbr(dst->d_sb, bindex);
4817+ h_path.mnt = au_br_mnt(br);
5527c038 4818+ h_isrc = d_inode(h_src);
1308ab2a 4819+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 4820+ | ATTR_ATIME | ATTR_MTIME
4821+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
4822+ if (h_src_attr && h_src_attr->valid) {
4823+ h_st = &h_src_attr->st;
4824+ ia.ia_uid = h_st->uid;
4825+ ia.ia_gid = h_st->gid;
4826+ ia.ia_atime = h_st->atime;
4827+ ia.ia_mtime = h_st->mtime;
4828+ if (h_idst->i_mode != h_st->mode
4829+ && !S_ISLNK(h_idst->i_mode)) {
4830+ ia.ia_valid |= ATTR_MODE;
4831+ ia.ia_mode = h_st->mode;
4832+ }
4833+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4834+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4835+ } else {
4836+ ia.ia_uid = h_isrc->i_uid;
4837+ ia.ia_gid = h_isrc->i_gid;
4838+ ia.ia_atime = h_isrc->i_atime;
4839+ ia.ia_mtime = h_isrc->i_mtime;
4840+ if (h_idst->i_mode != h_isrc->i_mode
4841+ && !S_ISLNK(h_idst->i_mode)) {
4842+ ia.ia_valid |= ATTR_MODE;
4843+ ia.ia_mode = h_isrc->i_mode;
4844+ }
4845+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4846+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 4847+ }
523b37e3
AM
4848+ /* no delegation since it is just created */
4849+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4850+
4851+ /* is this nfs only? */
4852+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4853+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4854+ ia.ia_mode = h_isrc->i_mode;
523b37e3 4855+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4856+ }
4857+
c1595e42 4858+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
4859+ if (!err) {
4860+ mnt_flags = au_mntflags(dst->d_sb);
4861+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4862+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4863+ }
c1595e42 4864+
1facf9fc 4865+ return err;
4866+}
4867+
4868+/* ---------------------------------------------------------------------- */
4869+
4870+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4871+ char *buf, unsigned long blksize)
4872+{
4873+ int err;
4874+ size_t sz, rbytes, wbytes;
4875+ unsigned char all_zero;
4876+ char *p, *zp;
febd17d6 4877+ struct inode *h_inode;
1facf9fc 4878+ /* reduce stack usage */
4879+ struct iattr *ia;
4880+
4881+ zp = page_address(ZERO_PAGE(0));
4882+ if (unlikely(!zp))
4883+ return -ENOMEM; /* possible? */
4884+
4885+ err = 0;
4886+ all_zero = 0;
4887+ while (len) {
4888+ AuDbg("len %lld\n", len);
4889+ sz = blksize;
4890+ if (len < blksize)
4891+ sz = len;
4892+
4893+ rbytes = 0;
4894+ /* todo: signal_pending? */
4895+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
4896+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4897+ err = rbytes;
4898+ }
4899+ if (unlikely(err < 0))
4900+ break;
4901+
4902+ all_zero = 0;
4903+ if (len >= rbytes && rbytes == blksize)
4904+ all_zero = !memcmp(buf, zp, rbytes);
4905+ if (!all_zero) {
4906+ wbytes = rbytes;
4907+ p = buf;
4908+ while (wbytes) {
4909+ size_t b;
4910+
4911+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4912+ err = b;
4913+ /* todo: signal_pending? */
4914+ if (unlikely(err == -EAGAIN || err == -EINTR))
4915+ continue;
4916+ if (unlikely(err < 0))
4917+ break;
4918+ wbytes -= b;
4919+ p += b;
4920+ }
392086de
AM
4921+ if (unlikely(err < 0))
4922+ break;
1facf9fc 4923+ } else {
4924+ loff_t res;
4925+
4926+ AuLabel(hole);
4927+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4928+ err = res;
4929+ if (unlikely(res < 0))
4930+ break;
4931+ }
4932+ len -= rbytes;
4933+ err = 0;
4934+ }
4935+
4936+ /* the last block may be a hole */
4937+ if (!err && all_zero) {
4938+ AuLabel(last hole);
4939+
4940+ err = 1;
2000de60 4941+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 4942+ /* nfs requires this step to make last hole */
4943+ /* is this only nfs? */
4944+ do {
4945+ /* todo: signal_pending? */
4946+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4947+ } while (err == -EAGAIN || err == -EINTR);
4948+ if (err == 1)
4949+ dst->f_pos--;
4950+ }
4951+
4952+ if (err == 1) {
4953+ ia = (void *)buf;
4954+ ia->ia_size = dst->f_pos;
4955+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4956+ ia->ia_file = dst;
febd17d6
JR
4957+ h_inode = file_inode(dst);
4958+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
4959+ /* no delegation since it is just created */
4960+ err = vfsub_notify_change(&dst->f_path, ia,
4961+ /*delegated*/NULL);
febd17d6 4962+ inode_unlock(h_inode);
1facf9fc 4963+ }
4964+ }
4965+
4966+ return err;
4967+}
4968+
4969+int au_copy_file(struct file *dst, struct file *src, loff_t len)
4970+{
4971+ int err;
4972+ unsigned long blksize;
4973+ unsigned char do_kfree;
4974+ char *buf;
4975+
4976+ err = -ENOMEM;
2000de60 4977+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 4978+ if (!blksize || PAGE_SIZE < blksize)
4979+ blksize = PAGE_SIZE;
4980+ AuDbg("blksize %lu\n", blksize);
4981+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4982+ if (do_kfree)
4983+ buf = kmalloc(blksize, GFP_NOFS);
4984+ else
4985+ buf = (void *)__get_free_page(GFP_NOFS);
4986+ if (unlikely(!buf))
4987+ goto out;
4988+
4989+ if (len > (1 << 22))
4990+ AuDbg("copying a large file %lld\n", (long long)len);
4991+
4992+ src->f_pos = 0;
4993+ dst->f_pos = 0;
4994+ err = au_do_copy_file(dst, src, len, buf, blksize);
4995+ if (do_kfree)
f0c0a007 4996+ au_delayed_kfree(buf);
1facf9fc 4997+ else
f0c0a007 4998+ au_delayed_free_page((unsigned long)buf);
1facf9fc 4999+
4f0767ce 5000+out:
1facf9fc 5001+ return err;
5002+}
5003+
5004+/*
5005+ * to support a sparse file which is opened with O_APPEND,
5006+ * we need to close the file.
5007+ */
c2b27bf2 5008+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5009+{
5010+ int err, i;
5011+ enum { SRC, DST };
5012+ struct {
5013+ aufs_bindex_t bindex;
5014+ unsigned int flags;
5015+ struct dentry *dentry;
392086de 5016+ int force_wr;
1facf9fc 5017+ struct file *file;
523b37e3 5018+ void *label;
1facf9fc 5019+ } *f, file[] = {
5020+ {
c2b27bf2 5021+ .bindex = cpg->bsrc,
1facf9fc 5022+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5023+ .label = &&out
1facf9fc 5024+ },
5025+ {
c2b27bf2 5026+ .bindex = cpg->bdst,
1facf9fc 5027+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5028+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5029+ .label = &&out_src
1facf9fc 5030+ }
5031+ };
5032+ struct super_block *sb;
8cdd5066 5033+ struct task_struct *tsk = current;
1facf9fc 5034+
5035+ /* bsrc branch can be ro/rw. */
c2b27bf2 5036+ sb = cpg->dentry->d_sb;
1facf9fc 5037+ f = file;
5038+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5039+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5040+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5041+ /*file*/NULL, f->force_wr);
1facf9fc 5042+ err = PTR_ERR(f->file);
5043+ if (IS_ERR(f->file))
5044+ goto *f->label;
1facf9fc 5045+ }
5046+
5047+ /* try stopping to update while we copyup */
5527c038 5048+ IMustLock(d_inode(file[SRC].dentry));
c2b27bf2 5049+ err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5050+
8cdd5066
JR
5051+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5052+ if (tsk->flags & PF_KTHREAD)
5053+ __fput_sync(file[DST].file);
5054+ else {
5055+ WARN(1, "%pD\nPlease report this warning to aufs-users ML",
5056+ file[DST].file);
5057+ fput(file[DST].file);
5058+ /*
5059+ * too bad.
5060+ * we have to call both since we don't know which place the file
5061+ * was added to.
5062+ */
5063+ task_work_run();
5064+ flush_delayed_fput();
5065+ }
1facf9fc 5066+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5067+
4f0767ce 5068+out_src:
1facf9fc 5069+ fput(file[SRC].file);
5070+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5071+out:
1facf9fc 5072+ return err;
5073+}
5074+
c2b27bf2 5075+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5076+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5077+{
5078+ int err, rerr;
5079+ loff_t l;
86dc4139 5080+ struct path h_path;
38d290e6 5081+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5082+
5083+ err = 0;
5527c038 5084+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5085+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5086+ if (cpg->len == -1 || l < cpg->len)
5087+ cpg->len = l;
5088+ if (cpg->len) {
86dc4139 5089+ /* try stopping to update while we are referencing */
febd17d6 5090+ inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5091+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5092+
c2b27bf2
AM
5093+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5094+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5095+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038
JR
5096+ if (!au_test_nfs(h_src_inode->i_sb))
5097+ err = vfs_getattr(&h_path, &h_src_attr->st);
5098+ else {
febd17d6 5099+ inode_unlock(h_src_inode);
5527c038 5100+ err = vfs_getattr(&h_path, &h_src_attr->st);
febd17d6 5101+ inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5102+ }
86dc4139 5103+ if (unlikely(err)) {
febd17d6 5104+ inode_unlock(h_src_inode);
86dc4139
AM
5105+ goto out;
5106+ }
5107+ h_src_attr->valid = 1;
c2b27bf2 5108+ err = au_cp_regular(cpg);
febd17d6 5109+ inode_unlock(h_src_inode);
c2b27bf2 5110+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5111+ if (!err && rerr)
5112+ err = rerr;
1facf9fc 5113+ }
38d290e6
JR
5114+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5115+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5116+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5117+ spin_lock(&h_dst_inode->i_lock);
5118+ h_dst_inode->i_state |= I_LINKABLE;
5119+ spin_unlock(&h_dst_inode->i_lock);
5120+ }
1facf9fc 5121+
4f0767ce 5122+out:
1facf9fc 5123+ return err;
5124+}
5125+
5126+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5127+ struct inode *h_dir)
5128+{
5129+ int err, symlen;
5130+ mm_segment_t old_fs;
b752ccd1
AM
5131+ union {
5132+ char *k;
5133+ char __user *u;
5134+ } sym;
5527c038
JR
5135+ struct inode *h_inode = d_inode(h_src);
5136+ const struct inode_operations *h_iop = h_inode->i_op;
1facf9fc 5137+
5138+ err = -ENOSYS;
5527c038 5139+ if (unlikely(!h_iop->readlink))
1facf9fc 5140+ goto out;
5141+
5142+ err = -ENOMEM;
537831f9 5143+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5144+ if (unlikely(!sym.k))
1facf9fc 5145+ goto out;
5146+
9dbd164d 5147+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5148+ old_fs = get_fs();
5149+ set_fs(KERNEL_DS);
5527c038 5150+ symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5151+ err = symlen;
5152+ set_fs(old_fs);
5153+
5154+ if (symlen > 0) {
b752ccd1
AM
5155+ sym.k[symlen] = 0;
5156+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5157+ }
f0c0a007 5158+ au_delayed_free_page((unsigned long)sym.k);
1facf9fc 5159+
4f0767ce 5160+out:
1facf9fc 5161+ return err;
5162+}
5163+
8cdd5066
JR
5164+/*
5165+ * regardless 'acl' option, reset all ACL.
5166+ * All ACL will be copied up later from the original entry on the lower branch.
5167+ */
5168+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5169+{
5170+ int err;
5171+ struct dentry *h_dentry;
5172+ struct inode *h_inode;
5173+
5174+ h_dentry = h_path->dentry;
5175+ h_inode = d_inode(h_dentry);
5176+ /* forget_all_cached_acls(h_inode)); */
5177+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5178+ AuTraceErr(err);
5179+ if (err == -EOPNOTSUPP)
5180+ err = 0;
5181+ if (!err)
5182+ err = vfsub_acl_chmod(h_inode, mode);
5183+
5184+ AuTraceErr(err);
5185+ return err;
5186+}
5187+
5188+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5189+ struct inode *h_dir, struct path *h_path)
5190+{
5191+ int err;
5192+ struct inode *dir, *inode;
5193+
5194+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5195+ AuTraceErr(err);
5196+ if (err == -EOPNOTSUPP)
5197+ err = 0;
5198+ if (unlikely(err))
5199+ goto out;
5200+
5201+ /*
5202+ * strange behaviour from the users view,
5203+ * particularry setattr case
5204+ */
5205+ dir = d_inode(dst_parent);
5afbbe0d 5206+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5207+ au_cpup_attr_nlink(dir, /*force*/1);
5208+ inode = d_inode(cpg->dentry);
5209+ au_cpup_attr_nlink(inode, /*force*/1);
5210+
5211+out:
5212+ return err;
5213+}
5214+
1facf9fc 5215+static noinline_for_stack
c2b27bf2 5216+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5217+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5218+{
5219+ int err;
5220+ umode_t mode;
5221+ unsigned int mnt_flags;
076b876e 5222+ unsigned char isdir, isreg, force;
c2b27bf2 5223+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5224+ struct au_dtime dt;
5225+ struct path h_path;
5226+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5227+ struct inode *h_inode, *h_dir;
1facf9fc 5228+ struct super_block *sb;
5229+
5230+ /* bsrc branch can be ro/rw. */
c2b27bf2 5231+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5232+ h_inode = d_inode(h_src);
5233+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5234+
5235+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5236+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5237+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5238+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5239+ AUFS_WH_PFX_LEN));
1facf9fc 5240+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5241+ h_dir = d_inode(h_parent);
1facf9fc 5242+ IMustLock(h_dir);
5243+ AuDebugOn(h_parent != h_dst->d_parent);
5244+
c2b27bf2
AM
5245+ sb = cpg->dentry->d_sb;
5246+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5247+ if (do_dt) {
5248+ h_path.dentry = h_parent;
5249+ au_dtime_store(&dt, dst_parent, &h_path);
5250+ }
5251+ h_path.dentry = h_dst;
5252+
076b876e 5253+ isreg = 0;
1facf9fc 5254+ isdir = 0;
5255+ mode = h_inode->i_mode;
5256+ switch (mode & S_IFMT) {
5257+ case S_IFREG:
076b876e 5258+ isreg = 1;
8cdd5066 5259+ err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
b4510431 5260+ /*want_excl*/true);
1facf9fc 5261+ if (!err)
c2b27bf2 5262+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5263+ break;
5264+ case S_IFDIR:
5265+ isdir = 1;
5266+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5267+ if (!err)
5268+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5269+ break;
5270+ case S_IFLNK:
5271+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5272+ break;
5273+ case S_IFCHR:
5274+ case S_IFBLK:
5275+ AuDebugOn(!capable(CAP_MKNOD));
5276+ /*FALLTHROUGH*/
5277+ case S_IFIFO:
5278+ case S_IFSOCK:
5279+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5280+ break;
5281+ default:
5282+ AuIOErr("Unknown inode type 0%o\n", mode);
5283+ err = -EIO;
5284+ }
8cdd5066
JR
5285+ if (!err)
5286+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5287+
5288+ mnt_flags = au_mntflags(sb);
5289+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5290+ && !isdir
5291+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5292+ && (h_inode->i_nlink == 1
5293+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5294+ /* todo: unnecessary? */
5527c038 5295+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5296+ && cpg->bdst < cpg->bsrc
5297+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5298+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5299+ /* ignore this error */
5300+
076b876e
AM
5301+ if (!err) {
5302+ force = 0;
5303+ if (isreg) {
5304+ force = !!cpg->len;
5305+ if (cpg->len == -1)
5306+ force = !!i_size_read(h_inode);
5307+ }
5308+ au_fhsm_wrote(sb, cpg->bdst, force);
5309+ }
5310+
1facf9fc 5311+ if (do_dt)
5312+ au_dtime_revert(&dt);
5313+ return err;
5314+}
5315+
392086de 5316+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5317+{
5318+ int err;
392086de 5319+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5320+ struct inode *h_dir;
392086de 5321+ aufs_bindex_t bdst;
86dc4139 5322+
392086de
AM
5323+ dentry = cpg->dentry;
5324+ bdst = cpg->bdst;
5325+ h_dentry = au_h_dptr(dentry, bdst);
5326+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5327+ dget(h_dentry);
5328+ au_set_h_dptr(dentry, bdst, NULL);
5329+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5330+ if (!err)
5331+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5332+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5333+ } else {
5334+ err = 0;
5335+ parent = dget_parent(dentry);
5336+ h_parent = au_h_dptr(parent, bdst);
5337+ dput(parent);
5338+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5339+ if (IS_ERR(h_path->dentry))
5340+ err = PTR_ERR(h_path->dentry);
86dc4139 5341+ }
392086de
AM
5342+ if (unlikely(err))
5343+ goto out;
86dc4139 5344+
86dc4139 5345+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5346+ h_dir = d_inode(h_parent);
86dc4139 5347+ IMustLock(h_dir);
523b37e3
AM
5348+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5349+ /* no delegation since it is just created */
5350+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
86dc4139
AM
5351+ dput(h_path->dentry);
5352+
5353+out:
5354+ return err;
5355+}
5356+
1facf9fc 5357+/*
5358+ * copyup the @dentry from @bsrc to @bdst.
5359+ * the caller must set the both of lower dentries.
5360+ * @len is for truncating when it is -1 copyup the entire file.
5361+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5362+ * basic->bsrc can be larger than basic->bdst.
1facf9fc 5363+ */
c2b27bf2 5364+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5365+{
5366+ int err, rerr;
5afbbe0d 5367+ aufs_bindex_t old_ibtop;
1facf9fc 5368+ unsigned char isdir, plink;
1facf9fc 5369+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5370+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5371+ struct super_block *sb;
86dc4139 5372+ struct au_branch *br;
c2b27bf2
AM
5373+ /* to reuduce stack size */
5374+ struct {
5375+ struct au_dtime dt;
5376+ struct path h_path;
5377+ struct au_cpup_reg_attr h_src_attr;
5378+ } *a;
1facf9fc 5379+
c2b27bf2
AM
5380+ err = -ENOMEM;
5381+ a = kmalloc(sizeof(*a), GFP_NOFS);
5382+ if (unlikely(!a))
5383+ goto out;
5384+ a->h_src_attr.valid = 0;
1facf9fc 5385+
c2b27bf2
AM
5386+ sb = cpg->dentry->d_sb;
5387+ br = au_sbr(sb, cpg->bdst);
5388+ a->h_path.mnt = au_br_mnt(br);
5389+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5390+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5391+ h_dir = d_inode(h_parent);
1facf9fc 5392+ IMustLock(h_dir);
5393+
c2b27bf2 5394+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5395+ inode = d_inode(cpg->dentry);
1facf9fc 5396+
5397+ if (!dst_parent)
c2b27bf2 5398+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5399+ else
5400+ dget(dst_parent);
5401+
5402+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5403+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5404+ if (dst_inode) {
5405+ if (unlikely(!plink)) {
5406+ err = -EIO;
027c5e7a
AM
5407+ AuIOErr("hi%lu(i%lu) exists on b%d "
5408+ "but plink is disabled\n",
c2b27bf2
AM
5409+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5410+ goto out_parent;
1facf9fc 5411+ }
5412+
5413+ if (dst_inode->i_nlink) {
c2b27bf2 5414+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5415+
c2b27bf2 5416+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5417+ err = PTR_ERR(h_src);
5418+ if (IS_ERR(h_src))
c2b27bf2 5419+ goto out_parent;
5527c038 5420+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5421+ err = -EIO;
79b8bda9 5422+ AuIOErr("i%lu exists on b%d "
027c5e7a 5423+ "but not pseudo-linked\n",
79b8bda9 5424+ inode->i_ino, cpg->bdst);
1facf9fc 5425+ dput(h_src);
c2b27bf2 5426+ goto out_parent;
1facf9fc 5427+ }
5428+
5429+ if (do_dt) {
c2b27bf2
AM
5430+ a->h_path.dentry = h_parent;
5431+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5432+ }
86dc4139 5433+
c2b27bf2 5434+ a->h_path.dentry = h_dst;
523b37e3
AM
5435+ delegated = NULL;
5436+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5437+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5438+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5439+ if (do_dt)
c2b27bf2 5440+ au_dtime_revert(&a->dt);
523b37e3
AM
5441+ if (unlikely(err == -EWOULDBLOCK)) {
5442+ pr_warn("cannot retry for NFSv4 delegation"
5443+ " for an internal link\n");
5444+ iput(delegated);
5445+ }
1facf9fc 5446+ dput(h_src);
c2b27bf2 5447+ goto out_parent;
1facf9fc 5448+ } else
5449+ /* todo: cpup_wh_file? */
5450+ /* udba work */
4a4d8108 5451+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5452+ }
5453+
86dc4139 5454+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5455+ old_ibtop = au_ibtop(inode);
c2b27bf2 5456+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5457+ if (unlikely(err))
86dc4139 5458+ goto out_rev;
5527c038 5459+ dst_inode = d_inode(h_dst);
febd17d6 5460+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5461+ /* todo: necessary? */
c2b27bf2 5462+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5463+
c2b27bf2 5464+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5465+ if (unlikely(err)) {
5466+ /* todo: necessary? */
c2b27bf2 5467+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5468+ inode_unlock(dst_inode);
86dc4139
AM
5469+ goto out_rev;
5470+ }
5471+
5afbbe0d 5472+ if (cpg->bdst < old_ibtop) {
86dc4139 5473+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5474+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5475+ if (unlikely(err)) {
c2b27bf2
AM
5476+ /* ignore an error */
5477+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5478+ inode_unlock(dst_inode);
86dc4139 5479+ goto out_rev;
4a4d8108 5480+ }
4a4d8108 5481+ }
5afbbe0d 5482+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5483+ } else
5afbbe0d 5484+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5485+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5486+ au_hi_flags(inode, isdir));
5487+
5488+ /* todo: necessary? */
c2b27bf2 5489+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5490+ inode_unlock(dst_inode);
86dc4139
AM
5491+ if (unlikely(err))
5492+ goto out_rev;
5493+
5527c038 5494+ src_inode = d_inode(h_src);
86dc4139 5495+ if (!isdir
5527c038
JR
5496+ && (src_inode->i_nlink > 1
5497+ || src_inode->i_state & I_LINKABLE)
86dc4139 5498+ && plink)
c2b27bf2 5499+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5500+
c2b27bf2
AM
5501+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5502+ a->h_path.dentry = h_dst;
392086de 5503+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5504+ }
5505+ if (!err)
c2b27bf2 5506+ goto out_parent; /* success */
1facf9fc 5507+
5508+ /* revert */
4a4d8108 5509+out_rev:
c2b27bf2
AM
5510+ a->h_path.dentry = h_parent;
5511+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5512+ a->h_path.dentry = h_dst;
86dc4139 5513+ rerr = 0;
5527c038 5514+ if (d_is_positive(h_dst)) {
523b37e3
AM
5515+ if (!isdir) {
5516+ /* no delegation since it is just created */
5517+ rerr = vfsub_unlink(h_dir, &a->h_path,
5518+ /*delegated*/NULL, /*force*/0);
5519+ } else
c2b27bf2 5520+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5521+ }
c2b27bf2 5522+ au_dtime_revert(&a->dt);
1facf9fc 5523+ if (rerr) {
5524+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5525+ err = -EIO;
5526+ }
c2b27bf2 5527+out_parent:
1facf9fc 5528+ dput(dst_parent);
f0c0a007 5529+ au_delayed_kfree(a);
c2b27bf2 5530+out:
1facf9fc 5531+ return err;
5532+}
5533+
7e9cd9fe 5534+#if 0 /* reserved */
1facf9fc 5535+struct au_cpup_single_args {
5536+ int *errp;
c2b27bf2 5537+ struct au_cp_generic *cpg;
1facf9fc 5538+ struct dentry *dst_parent;
5539+};
5540+
5541+static void au_call_cpup_single(void *args)
5542+{
5543+ struct au_cpup_single_args *a = args;
86dc4139 5544+
c2b27bf2
AM
5545+ au_pin_hdir_acquire_nest(a->cpg->pin);
5546+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5547+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5548+}
c2b27bf2 5549+#endif
1facf9fc 5550+
53392da6
AM
5551+/*
5552+ * prevent SIGXFSZ in copy-up.
5553+ * testing CAP_MKNOD is for generic fs,
5554+ * but CAP_FSETID is for xfs only, currently.
5555+ */
86dc4139 5556+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5557+{
5558+ int do_sio;
86dc4139
AM
5559+ struct super_block *sb;
5560+ struct inode *h_dir;
53392da6
AM
5561+
5562+ do_sio = 0;
86dc4139 5563+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5564+ if (!au_wkq_test()
5565+ && (!au_sbi(sb)->si_plink_maint_pid
5566+ || au_plink_maint(sb, AuLock_NOPLM))) {
5567+ switch (mode & S_IFMT) {
5568+ case S_IFREG:
5569+ /* no condition about RLIMIT_FSIZE and the file size */
5570+ do_sio = 1;
5571+ break;
5572+ case S_IFCHR:
5573+ case S_IFBLK:
5574+ do_sio = !capable(CAP_MKNOD);
5575+ break;
5576+ }
5577+ if (!do_sio)
5578+ do_sio = ((mode & (S_ISUID | S_ISGID))
5579+ && !capable(CAP_FSETID));
86dc4139
AM
5580+ /* this workaround may be removed in the future */
5581+ if (!do_sio) {
5582+ h_dir = au_pinned_h_dir(pin);
5583+ do_sio = h_dir->i_mode & S_ISVTX;
5584+ }
53392da6
AM
5585+ }
5586+
5587+ return do_sio;
5588+}
5589+
7e9cd9fe 5590+#if 0 /* reserved */
c2b27bf2 5591+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5592+{
5593+ int err, wkq_err;
1facf9fc 5594+ struct dentry *h_dentry;
5595+
c2b27bf2 5596+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5597+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5598+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5599+ else {
5600+ struct au_cpup_single_args args = {
5601+ .errp = &err,
c2b27bf2
AM
5602+ .cpg = cpg,
5603+ .dst_parent = dst_parent
1facf9fc 5604+ };
5605+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5606+ if (unlikely(wkq_err))
5607+ err = wkq_err;
5608+ }
5609+
5610+ return err;
5611+}
c2b27bf2 5612+#endif
1facf9fc 5613+
5614+/*
5615+ * copyup the @dentry from the first active lower branch to @bdst,
5616+ * using au_cpup_single().
5617+ */
c2b27bf2 5618+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5619+{
5620+ int err;
c2b27bf2
AM
5621+ unsigned int flags_orig;
5622+ struct dentry *dentry;
5623+
5624+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5625+
c2b27bf2 5626+ dentry = cpg->dentry;
86dc4139 5627+ DiMustWriteLock(dentry);
1facf9fc 5628+
c2b27bf2 5629+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5630+ if (!err) {
c2b27bf2
AM
5631+ flags_orig = cpg->flags;
5632+ au_fset_cpup(cpg->flags, RENAME);
5633+ err = au_cpup_single(cpg, NULL);
5634+ cpg->flags = flags_orig;
1facf9fc 5635+ if (!err)
5636+ return 0; /* success */
5637+
5638+ /* revert */
c2b27bf2 5639+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5640+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5641+ }
5642+
5643+ return err;
5644+}
5645+
5646+struct au_cpup_simple_args {
5647+ int *errp;
c2b27bf2 5648+ struct au_cp_generic *cpg;
1facf9fc 5649+};
5650+
5651+static void au_call_cpup_simple(void *args)
5652+{
5653+ struct au_cpup_simple_args *a = args;
86dc4139 5654+
c2b27bf2
AM
5655+ au_pin_hdir_acquire_nest(a->cpg->pin);
5656+ *a->errp = au_cpup_simple(a->cpg);
5657+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5658+}
5659+
c2b27bf2 5660+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5661+{
5662+ int err, wkq_err;
c2b27bf2
AM
5663+ struct dentry *dentry, *parent;
5664+ struct file *h_file;
1facf9fc 5665+ struct inode *h_dir;
5666+
c2b27bf2
AM
5667+ dentry = cpg->dentry;
5668+ h_file = NULL;
5669+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5670+ AuDebugOn(cpg->bsrc < 0);
392086de 5671+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5672+ err = PTR_ERR(h_file);
5673+ if (IS_ERR(h_file))
5674+ goto out;
5675+ }
5676+
1facf9fc 5677+ parent = dget_parent(dentry);
5527c038 5678+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 5679+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5680+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5681+ err = au_cpup_simple(cpg);
1facf9fc 5682+ else {
5683+ struct au_cpup_simple_args args = {
5684+ .errp = &err,
c2b27bf2 5685+ .cpg = cpg
1facf9fc 5686+ };
5687+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5688+ if (unlikely(wkq_err))
5689+ err = wkq_err;
5690+ }
5691+
5692+ dput(parent);
c2b27bf2
AM
5693+ if (h_file)
5694+ au_h_open_post(dentry, cpg->bsrc, h_file);
5695+
5696+out:
1facf9fc 5697+ return err;
5698+}
5699+
c2b27bf2 5700+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 5701+{
5afbbe0d 5702+ aufs_bindex_t bsrc, bbot;
c2b27bf2 5703+ struct dentry *dentry, *h_dentry;
367653fa 5704+
c2b27bf2
AM
5705+ if (cpg->bsrc < 0) {
5706+ dentry = cpg->dentry;
5afbbe0d
AM
5707+ bbot = au_dbbot(dentry);
5708+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
5709+ h_dentry = au_h_dptr(dentry, bsrc);
5710+ if (h_dentry) {
5527c038 5711+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
5712+ break;
5713+ }
5714+ }
5afbbe0d 5715+ AuDebugOn(bsrc > bbot);
c2b27bf2 5716+ cpg->bsrc = bsrc;
367653fa 5717+ }
c2b27bf2
AM
5718+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5719+ return au_do_sio_cpup_simple(cpg);
5720+}
367653fa 5721+
c2b27bf2
AM
5722+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5723+{
5724+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5725+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
5726+}
5727+
1facf9fc 5728+/* ---------------------------------------------------------------------- */
5729+
5730+/*
5731+ * copyup the deleted file for writing.
5732+ */
c2b27bf2
AM
5733+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5734+ struct file *file)
1facf9fc 5735+{
5736+ int err;
c2b27bf2
AM
5737+ unsigned int flags_orig;
5738+ aufs_bindex_t bsrc_orig;
c2b27bf2 5739+ struct au_dinfo *dinfo;
5afbbe0d
AM
5740+ struct {
5741+ struct au_hdentry *hd;
5742+ struct dentry *h_dentry;
5743+ } hdst, hsrc;
1facf9fc 5744+
c2b27bf2 5745+ dinfo = au_di(cpg->dentry);
1308ab2a 5746+ AuRwMustWriteLock(&dinfo->di_rwsem);
5747+
c2b27bf2 5748+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
5749+ cpg->bsrc = dinfo->di_btop;
5750+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5751+ hdst.h_dentry = hdst.hd->hd_dentry;
5752+ hdst.hd->hd_dentry = wh_dentry;
5753+ dinfo->di_btop = cpg->bdst;
5754+
5755+ hsrc.h_dentry = NULL;
027c5e7a 5756+ if (file) {
5afbbe0d
AM
5757+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5758+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5759+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 5760+ }
c2b27bf2
AM
5761+ flags_orig = cpg->flags;
5762+ cpg->flags = !AuCpup_DTIME;
5763+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5764+ cpg->flags = flags_orig;
027c5e7a
AM
5765+ if (file) {
5766+ if (!err)
5767+ err = au_reopen_nondir(file);
5afbbe0d 5768+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 5769+ }
5afbbe0d
AM
5770+ hdst.hd->hd_dentry = hdst.h_dentry;
5771+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 5772+ cpg->bsrc = bsrc_orig;
1facf9fc 5773+
5774+ return err;
5775+}
5776+
c2b27bf2 5777+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 5778+{
5779+ int err;
c2b27bf2 5780+ aufs_bindex_t bdst;
1facf9fc 5781+ struct au_dtime dt;
c2b27bf2 5782+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 5783+ struct au_branch *br;
5784+ struct path h_path;
5785+
c2b27bf2
AM
5786+ dentry = cpg->dentry;
5787+ bdst = cpg->bdst;
1facf9fc 5788+ br = au_sbr(dentry->d_sb, bdst);
5789+ parent = dget_parent(dentry);
5790+ h_parent = au_h_dptr(parent, bdst);
5791+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5792+ err = PTR_ERR(wh_dentry);
5793+ if (IS_ERR(wh_dentry))
5794+ goto out;
5795+
5796+ h_path.dentry = h_parent;
86dc4139 5797+ h_path.mnt = au_br_mnt(br);
1facf9fc 5798+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 5799+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 5800+ if (unlikely(err))
5801+ goto out_wh;
5802+
5803+ dget(wh_dentry);
5804+ h_path.dentry = wh_dentry;
2000de60 5805+ if (!d_is_dir(wh_dentry)) {
523b37e3 5806+ /* no delegation since it is just created */
5527c038 5807+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
5808+ /*delegated*/NULL, /*force*/0);
5809+ } else
5527c038 5810+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 5811+ if (unlikely(err)) {
523b37e3
AM
5812+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5813+ wh_dentry, err);
1facf9fc 5814+ err = -EIO;
5815+ }
5816+ au_dtime_revert(&dt);
5527c038 5817+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 5818+
4f0767ce 5819+out_wh:
1facf9fc 5820+ dput(wh_dentry);
4f0767ce 5821+out:
1facf9fc 5822+ dput(parent);
5823+ return err;
5824+}
5825+
5826+struct au_cpup_wh_args {
5827+ int *errp;
c2b27bf2 5828+ struct au_cp_generic *cpg;
1facf9fc 5829+ struct file *file;
5830+};
5831+
5832+static void au_call_cpup_wh(void *args)
5833+{
5834+ struct au_cpup_wh_args *a = args;
86dc4139 5835+
c2b27bf2
AM
5836+ au_pin_hdir_acquire_nest(a->cpg->pin);
5837+ *a->errp = au_cpup_wh(a->cpg, a->file);
5838+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5839+}
5840+
c2b27bf2 5841+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 5842+{
5843+ int err, wkq_err;
c2b27bf2 5844+ aufs_bindex_t bdst;
c1595e42 5845+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 5846+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 5847+ struct au_wbr *wbr;
c2b27bf2 5848+ struct au_pin wh_pin, *pin_orig;
1facf9fc 5849+
c2b27bf2
AM
5850+ dentry = cpg->dentry;
5851+ bdst = cpg->bdst;
1facf9fc 5852+ parent = dget_parent(dentry);
5527c038 5853+ dir = d_inode(parent);
1facf9fc 5854+ h_orph = NULL;
5855+ h_parent = NULL;
5856+ h_dir = au_igrab(au_h_iptr(dir, bdst));
5857+ h_tmpdir = h_dir;
c2b27bf2 5858+ pin_orig = NULL;
1facf9fc 5859+ if (!h_dir->i_nlink) {
5860+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5861+ h_orph = wbr->wbr_orph;
5862+
5863+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 5864+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 5865+ h_tmpdir = d_inode(h_orph);
1facf9fc 5866+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5867+
febd17d6 5868+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 5869+ /* todo: au_h_open_pre()? */
86dc4139 5870+
c2b27bf2 5871+ pin_orig = cpg->pin;
86dc4139 5872+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
5873+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5874+ cpg->pin = &wh_pin;
1facf9fc 5875+ }
5876+
53392da6 5877+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 5878+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5879+ err = au_cpup_wh(cpg, file);
1facf9fc 5880+ else {
5881+ struct au_cpup_wh_args args = {
5882+ .errp = &err,
c2b27bf2
AM
5883+ .cpg = cpg,
5884+ .file = file
1facf9fc 5885+ };
5886+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5887+ if (unlikely(wkq_err))
5888+ err = wkq_err;
5889+ }
5890+
5891+ if (h_orph) {
febd17d6 5892+ inode_unlock(h_tmpdir);
4a4d8108 5893+ /* todo: au_h_open_post()? */
1facf9fc 5894+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 5895+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
5896+ AuDebugOn(!pin_orig);
5897+ cpg->pin = pin_orig;
1facf9fc 5898+ }
5899+ iput(h_dir);
5900+ dput(parent);
5901+
5902+ return err;
5903+}
5904+
5905+/* ---------------------------------------------------------------------- */
5906+
5907+/*
5908+ * generic routine for both of copy-up and copy-down.
5909+ */
5910+/* cf. revalidate function in file.c */
5911+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5912+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 5913+ struct au_pin *pin,
1facf9fc 5914+ struct dentry *h_parent, void *arg),
5915+ void *arg)
5916+{
5917+ int err;
5918+ struct au_pin pin;
5527c038 5919+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 5920+
5921+ err = 0;
5922+ parent = dget_parent(dentry);
5923+ if (IS_ROOT(parent))
5924+ goto out;
5925+
5926+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5927+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5928+
5929+ /* do not use au_dpage */
5930+ real_parent = parent;
5931+ while (1) {
5932+ dput(parent);
5933+ parent = dget_parent(dentry);
5934+ h_parent = au_h_dptr(parent, bdst);
5935+ if (h_parent)
5936+ goto out; /* success */
5937+
5938+ /* find top dir which is necessary to cpup */
5939+ do {
5940+ d = parent;
5941+ dput(parent);
5942+ parent = dget_parent(d);
5943+ di_read_lock_parent3(parent, !AuLock_IR);
5944+ h_parent = au_h_dptr(parent, bdst);
5945+ di_read_unlock(parent, !AuLock_IR);
5946+ } while (!h_parent);
5947+
5948+ if (d != real_parent)
5949+ di_write_lock_child3(d);
5950+
5951+ /* somebody else might create while we were sleeping */
5527c038
JR
5952+ h_dentry = au_h_dptr(d, bdst);
5953+ if (!h_dentry || d_is_negative(h_dentry)) {
5954+ if (h_dentry)
5afbbe0d 5955+ au_update_dbtop(d);
1facf9fc 5956+
5957+ au_pin_set_dentry(&pin, d);
5958+ err = au_do_pin(&pin);
5959+ if (!err) {
86dc4139 5960+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 5961+ au_unpin(&pin);
5962+ }
5963+ }
5964+
5965+ if (d != real_parent)
5966+ di_write_unlock(d);
5967+ if (unlikely(err))
5968+ break;
5969+ }
5970+
4f0767ce 5971+out:
1facf9fc 5972+ dput(parent);
5973+ return err;
5974+}
5975+
5976+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 5977+ struct au_pin *pin,
2000de60 5978+ struct dentry *h_parent __maybe_unused,
1facf9fc 5979+ void *arg __maybe_unused)
5980+{
c2b27bf2
AM
5981+ struct au_cp_generic cpg = {
5982+ .dentry = dentry,
5983+ .bdst = bdst,
5984+ .bsrc = -1,
5985+ .len = 0,
5986+ .pin = pin,
5987+ .flags = AuCpup_DTIME
5988+ };
5989+ return au_sio_cpup_simple(&cpg);
1facf9fc 5990+}
5991+
5992+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5993+{
5994+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5995+}
5996+
5997+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5998+{
5999+ int err;
6000+ struct dentry *parent;
6001+ struct inode *dir;
6002+
6003+ parent = dget_parent(dentry);
5527c038 6004+ dir = d_inode(parent);
1facf9fc 6005+ err = 0;
6006+ if (au_h_iptr(dir, bdst))
6007+ goto out;
6008+
6009+ di_read_unlock(parent, AuLock_IR);
6010+ di_write_lock_parent(parent);
6011+ /* someone else might change our inode while we were sleeping */
6012+ if (!au_h_iptr(dir, bdst))
6013+ err = au_cpup_dirs(dentry, bdst);
6014+ di_downgrade_lock(parent, AuLock_IR);
6015+
4f0767ce 6016+out:
1facf9fc 6017+ dput(parent);
6018+ return err;
6019+}
7f207e10
AM
6020diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6021--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6022+++ linux/fs/aufs/cpup.h 2016-07-25 19:05:34.811159821 +0200
523b37e3 6023@@ -0,0 +1,94 @@
1facf9fc 6024+/*
8cdd5066 6025+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6026+ *
6027+ * This program, aufs is free software; you can redistribute it and/or modify
6028+ * it under the terms of the GNU General Public License as published by
6029+ * the Free Software Foundation; either version 2 of the License, or
6030+ * (at your option) any later version.
dece6358
AM
6031+ *
6032+ * This program is distributed in the hope that it will be useful,
6033+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6034+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6035+ * GNU General Public License for more details.
6036+ *
6037+ * You should have received a copy of the GNU General Public License
523b37e3 6038+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6039+ */
6040+
6041+/*
6042+ * copy-up/down functions
6043+ */
6044+
6045+#ifndef __AUFS_CPUP_H__
6046+#define __AUFS_CPUP_H__
6047+
6048+#ifdef __KERNEL__
6049+
dece6358 6050+#include <linux/path.h>
1facf9fc 6051+
dece6358
AM
6052+struct inode;
6053+struct file;
86dc4139 6054+struct au_pin;
dece6358 6055+
86dc4139 6056+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6057+void au_cpup_attr_timesizes(struct inode *inode);
6058+void au_cpup_attr_nlink(struct inode *inode, int force);
6059+void au_cpup_attr_changeable(struct inode *inode);
6060+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6061+void au_cpup_attr_all(struct inode *inode, int force);
6062+
6063+/* ---------------------------------------------------------------------- */
6064+
c2b27bf2
AM
6065+struct au_cp_generic {
6066+ struct dentry *dentry;
6067+ aufs_bindex_t bdst, bsrc;
6068+ loff_t len;
6069+ struct au_pin *pin;
6070+ unsigned int flags;
6071+};
6072+
1facf9fc 6073+/* cpup flags */
392086de
AM
6074+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6075+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6076+ for link(2) */
6077+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6078+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6079+ cpup */
6080+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6081+ existing entry */
6082+#define AuCpup_RWDST (1 << 5) /* force write target even if
6083+ the branch is marked as RO */
c2b27bf2 6084+
1facf9fc 6085+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6086+#define au_fset_cpup(flags, name) \
6087+ do { (flags) |= AuCpup_##name; } while (0)
6088+#define au_fclr_cpup(flags, name) \
6089+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6090+
6091+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6092+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6093+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6094+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6095+
6096+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6097+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6098+ struct au_pin *pin,
1facf9fc 6099+ struct dentry *h_parent, void *arg),
6100+ void *arg);
6101+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6102+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6103+
6104+/* ---------------------------------------------------------------------- */
6105+
6106+/* keep timestamps when copyup */
6107+struct au_dtime {
6108+ struct dentry *dt_dentry;
6109+ struct path dt_h_path;
6110+ struct timespec dt_atime, dt_mtime;
6111+};
6112+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6113+ struct path *h_path);
6114+void au_dtime_revert(struct au_dtime *dt);
6115+
6116+#endif /* __KERNEL__ */
6117+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6118diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6119--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
6120+++ linux/fs/aufs/dbgaufs.c 2016-08-17 18:01:06.095221547 +0200
6121@@ -0,0 +1,435 @@
1facf9fc 6122+/*
8cdd5066 6123+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6124+ *
6125+ * This program, aufs is free software; you can redistribute it and/or modify
6126+ * it under the terms of the GNU General Public License as published by
6127+ * the Free Software Foundation; either version 2 of the License, or
6128+ * (at your option) any later version.
dece6358
AM
6129+ *
6130+ * This program is distributed in the hope that it will be useful,
6131+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6132+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6133+ * GNU General Public License for more details.
6134+ *
6135+ * You should have received a copy of the GNU General Public License
523b37e3 6136+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6137+ */
6138+
6139+/*
6140+ * debugfs interface
6141+ */
6142+
6143+#include <linux/debugfs.h>
6144+#include "aufs.h"
6145+
6146+#ifndef CONFIG_SYSFS
6147+#error DEBUG_FS depends upon SYSFS
6148+#endif
6149+
6150+static struct dentry *dbgaufs;
6151+static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6152+
6153+/* 20 is max digits length of ulong 64 */
6154+struct dbgaufs_arg {
6155+ int n;
6156+ char a[20 * 4];
6157+};
6158+
6159+/*
6160+ * common function for all XINO files
6161+ */
6162+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6163+ struct file *file)
6164+{
f0c0a007 6165+ au_delayed_kfree(file->private_data);
1facf9fc 6166+ return 0;
6167+}
6168+
6169+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6170+{
6171+ int err;
6172+ struct kstat st;
6173+ struct dbgaufs_arg *p;
6174+
6175+ err = -ENOMEM;
6176+ p = kmalloc(sizeof(*p), GFP_NOFS);
6177+ if (unlikely(!p))
6178+ goto out;
6179+
6180+ err = 0;
6181+ p->n = 0;
6182+ file->private_data = p;
6183+ if (!xf)
6184+ goto out;
6185+
c06a8ce3 6186+ err = vfs_getattr(&xf->f_path, &st);
1facf9fc 6187+ if (!err) {
6188+ if (do_fcnt)
6189+ p->n = snprintf
6190+ (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
6191+ (long)file_count(xf), st.blocks, st.blksize,
6192+ (long long)st.size);
6193+ else
6194+ p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
6195+ st.blocks, st.blksize,
6196+ (long long)st.size);
6197+ AuDebugOn(p->n >= sizeof(p->a));
6198+ } else {
6199+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6200+ err = 0;
6201+ }
6202+
4f0767ce 6203+out:
1facf9fc 6204+ return err;
6205+
6206+}
6207+
6208+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6209+ size_t count, loff_t *ppos)
6210+{
6211+ struct dbgaufs_arg *p;
6212+
6213+ p = file->private_data;
6214+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6215+}
6216+
6217+/* ---------------------------------------------------------------------- */
6218+
86dc4139
AM
6219+struct dbgaufs_plink_arg {
6220+ int n;
6221+ char a[];
6222+};
6223+
6224+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6225+ struct file *file)
6226+{
f0c0a007 6227+ au_delayed_free_page((unsigned long)file->private_data);
86dc4139
AM
6228+ return 0;
6229+}
6230+
6231+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6232+{
6233+ int err, i, limit;
6234+ unsigned long n, sum;
6235+ struct dbgaufs_plink_arg *p;
6236+ struct au_sbinfo *sbinfo;
6237+ struct super_block *sb;
6238+ struct au_sphlhead *sphl;
6239+
6240+ err = -ENOMEM;
6241+ p = (void *)get_zeroed_page(GFP_NOFS);
6242+ if (unlikely(!p))
6243+ goto out;
6244+
6245+ err = -EFBIG;
6246+ sbinfo = inode->i_private;
6247+ sb = sbinfo->si_sb;
6248+ si_noflush_read_lock(sb);
6249+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6250+ limit = PAGE_SIZE - sizeof(p->n);
6251+
6252+ /* the number of buckets */
6253+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6254+ p->n += n;
6255+ limit -= n;
6256+
6257+ sum = 0;
6258+ for (i = 0, sphl = sbinfo->si_plink;
6259+ i < AuPlink_NHASH;
6260+ i++, sphl++) {
6261+ n = au_sphl_count(sphl);
6262+ sum += n;
6263+
6264+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6265+ p->n += n;
6266+ limit -= n;
6267+ if (unlikely(limit <= 0))
6268+ goto out_free;
6269+ }
6270+ p->a[p->n - 1] = '\n';
6271+
6272+ /* the sum of plinks */
6273+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6274+ p->n += n;
6275+ limit -= n;
6276+ if (unlikely(limit <= 0))
6277+ goto out_free;
6278+ } else {
6279+#define str "1\n0\n0\n"
6280+ p->n = sizeof(str) - 1;
6281+ strcpy(p->a, str);
6282+#undef str
6283+ }
6284+ si_read_unlock(sb);
6285+
6286+ err = 0;
6287+ file->private_data = p;
6288+ goto out; /* success */
6289+
6290+out_free:
f0c0a007 6291+ au_delayed_free_page((unsigned long)p);
86dc4139
AM
6292+out:
6293+ return err;
6294+}
6295+
6296+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6297+ size_t count, loff_t *ppos)
6298+{
6299+ struct dbgaufs_plink_arg *p;
6300+
6301+ p = file->private_data;
6302+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6303+}
6304+
6305+static const struct file_operations dbgaufs_plink_fop = {
6306+ .owner = THIS_MODULE,
6307+ .open = dbgaufs_plink_open,
6308+ .release = dbgaufs_plink_release,
6309+ .read = dbgaufs_plink_read
6310+};
6311+
6312+/* ---------------------------------------------------------------------- */
6313+
1facf9fc 6314+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6315+{
6316+ int err;
6317+ struct au_sbinfo *sbinfo;
6318+ struct super_block *sb;
6319+
6320+ sbinfo = inode->i_private;
6321+ sb = sbinfo->si_sb;
6322+ si_noflush_read_lock(sb);
6323+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6324+ si_read_unlock(sb);
6325+ return err;
6326+}
6327+
6328+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6329+ .owner = THIS_MODULE,
1facf9fc 6330+ .open = dbgaufs_xib_open,
6331+ .release = dbgaufs_xi_release,
6332+ .read = dbgaufs_xi_read
6333+};
6334+
6335+/* ---------------------------------------------------------------------- */
6336+
6337+#define DbgaufsXi_PREFIX "xi"
6338+
6339+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6340+{
6341+ int err;
6342+ long l;
6343+ struct au_sbinfo *sbinfo;
6344+ struct super_block *sb;
6345+ struct file *xf;
6346+ struct qstr *name;
6347+
6348+ err = -ENOENT;
6349+ xf = NULL;
2000de60 6350+ name = &file->f_path.dentry->d_name;
1facf9fc 6351+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6352+ || memcmp(name->name, DbgaufsXi_PREFIX,
6353+ sizeof(DbgaufsXi_PREFIX) - 1)))
6354+ goto out;
9dbd164d 6355+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6356+ if (unlikely(err))
6357+ goto out;
6358+
6359+ sbinfo = inode->i_private;
6360+ sb = sbinfo->si_sb;
6361+ si_noflush_read_lock(sb);
5afbbe0d 6362+ if (l <= au_sbbot(sb)) {
1facf9fc 6363+ xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6364+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6365+ } else
6366+ err = -ENOENT;
6367+ si_read_unlock(sb);
6368+
4f0767ce 6369+out:
1facf9fc 6370+ return err;
6371+}
6372+
6373+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6374+ .owner = THIS_MODULE,
1facf9fc 6375+ .open = dbgaufs_xino_open,
6376+ .release = dbgaufs_xi_release,
6377+ .read = dbgaufs_xi_read
6378+};
6379+
6380+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6381+{
5afbbe0d 6382+ aufs_bindex_t bbot;
1facf9fc 6383+ struct au_branch *br;
6384+ struct au_xino_file *xi;
6385+
6386+ if (!au_sbi(sb)->si_dbgaufs)
6387+ return;
6388+
5afbbe0d
AM
6389+ bbot = au_sbbot(sb);
6390+ for (; bindex <= bbot; bindex++) {
1facf9fc 6391+ br = au_sbr(sb, bindex);
6392+ xi = &br->br_xino;
c06a8ce3
AM
6393+ debugfs_remove(xi->xi_dbgaufs);
6394+ xi->xi_dbgaufs = NULL;
1facf9fc 6395+ }
6396+}
6397+
6398+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6399+{
6400+ struct au_sbinfo *sbinfo;
6401+ struct dentry *parent;
6402+ struct au_branch *br;
6403+ struct au_xino_file *xi;
5afbbe0d 6404+ aufs_bindex_t bbot;
1facf9fc 6405+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6406+
6407+ sbinfo = au_sbi(sb);
6408+ parent = sbinfo->si_dbgaufs;
6409+ if (!parent)
6410+ return;
6411+
5afbbe0d
AM
6412+ bbot = au_sbbot(sb);
6413+ for (; bindex <= bbot; bindex++) {
1facf9fc 6414+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6415+ br = au_sbr(sb, bindex);
6416+ xi = &br->br_xino;
6417+ AuDebugOn(xi->xi_dbgaufs);
f0c0a007
AM
6418+ /* debugfs acquires the parent i_mutex */
6419+ lockdep_off();
1facf9fc 6420+ xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6421+ sbinfo, &dbgaufs_xino_fop);
f0c0a007 6422+ lockdep_on();
1facf9fc 6423+ /* ignore an error */
6424+ if (unlikely(!xi->xi_dbgaufs))
6425+ AuWarn1("failed %s under debugfs\n", name);
6426+ }
6427+}
6428+
6429+/* ---------------------------------------------------------------------- */
6430+
6431+#ifdef CONFIG_AUFS_EXPORT
6432+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6433+{
6434+ int err;
6435+ struct au_sbinfo *sbinfo;
6436+ struct super_block *sb;
6437+
6438+ sbinfo = inode->i_private;
6439+ sb = sbinfo->si_sb;
6440+ si_noflush_read_lock(sb);
6441+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6442+ si_read_unlock(sb);
6443+ return err;
6444+}
6445+
6446+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6447+ .owner = THIS_MODULE,
1facf9fc 6448+ .open = dbgaufs_xigen_open,
6449+ .release = dbgaufs_xi_release,
6450+ .read = dbgaufs_xi_read
6451+};
6452+
6453+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6454+{
6455+ int err;
6456+
dece6358 6457+ /*
c1595e42 6458+ * This function is a dynamic '__init' function actually,
dece6358
AM
6459+ * so the tiny check for si_rwsem is unnecessary.
6460+ */
6461+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6462+
1facf9fc 6463+ err = -EIO;
6464+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6465+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6466+ &dbgaufs_xigen_fop);
6467+ if (sbinfo->si_dbgaufs_xigen)
6468+ err = 0;
6469+
6470+ return err;
6471+}
6472+#else
6473+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6474+{
6475+ return 0;
6476+}
6477+#endif /* CONFIG_AUFS_EXPORT */
6478+
6479+/* ---------------------------------------------------------------------- */
6480+
6481+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6482+{
dece6358 6483+ /*
7e9cd9fe 6484+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6485+ * so the tiny check for si_rwsem is unnecessary.
6486+ */
6487+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6488+
1facf9fc 6489+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6490+ sbinfo->si_dbgaufs = NULL;
6491+ kobject_put(&sbinfo->si_kobj);
6492+}
6493+
6494+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6495+{
6496+ int err;
6497+ char name[SysaufsSiNameLen];
6498+
dece6358 6499+ /*
c1595e42 6500+ * This function is a dynamic '__init' function actually,
dece6358
AM
6501+ * so the tiny check for si_rwsem is unnecessary.
6502+ */
6503+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6504+
1facf9fc 6505+ err = -ENOENT;
6506+ if (!dbgaufs) {
6507+ AuErr1("/debug/aufs is uninitialized\n");
6508+ goto out;
6509+ }
6510+
6511+ err = -EIO;
6512+ sysaufs_name(sbinfo, name);
6513+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6514+ if (unlikely(!sbinfo->si_dbgaufs))
6515+ goto out;
6516+ kobject_get(&sbinfo->si_kobj);
6517+
6518+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6519+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6520+ &dbgaufs_xib_fop);
6521+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6522+ goto out_dir;
6523+
86dc4139
AM
6524+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6525+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6526+ &dbgaufs_plink_fop);
6527+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6528+ goto out_dir;
6529+
1facf9fc 6530+ err = dbgaufs_xigen_init(sbinfo);
6531+ if (!err)
6532+ goto out; /* success */
6533+
4f0767ce 6534+out_dir:
1facf9fc 6535+ dbgaufs_si_fin(sbinfo);
4f0767ce 6536+out:
1facf9fc 6537+ return err;
6538+}
6539+
6540+/* ---------------------------------------------------------------------- */
6541+
6542+void dbgaufs_fin(void)
6543+{
6544+ debugfs_remove(dbgaufs);
6545+}
6546+
6547+int __init dbgaufs_init(void)
6548+{
6549+ int err;
6550+
6551+ err = -EIO;
6552+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6553+ if (dbgaufs)
6554+ err = 0;
6555+ return err;
6556+}
7f207e10
AM
6557diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6558--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6559+++ linux/fs/aufs/dbgaufs.h 2016-07-25 19:05:34.811159821 +0200
523b37e3 6560@@ -0,0 +1,48 @@
1facf9fc 6561+/*
8cdd5066 6562+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6563+ *
6564+ * This program, aufs is free software; you can redistribute it and/or modify
6565+ * it under the terms of the GNU General Public License as published by
6566+ * the Free Software Foundation; either version 2 of the License, or
6567+ * (at your option) any later version.
dece6358
AM
6568+ *
6569+ * This program is distributed in the hope that it will be useful,
6570+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6571+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6572+ * GNU General Public License for more details.
6573+ *
6574+ * You should have received a copy of the GNU General Public License
523b37e3 6575+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6576+ */
6577+
6578+/*
6579+ * debugfs interface
6580+ */
6581+
6582+#ifndef __DBGAUFS_H__
6583+#define __DBGAUFS_H__
6584+
6585+#ifdef __KERNEL__
6586+
dece6358 6587+struct super_block;
1facf9fc 6588+struct au_sbinfo;
dece6358 6589+
1facf9fc 6590+#ifdef CONFIG_DEBUG_FS
6591+/* dbgaufs.c */
6592+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6593+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6594+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6595+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6596+void dbgaufs_fin(void);
6597+int __init dbgaufs_init(void);
1facf9fc 6598+#else
4a4d8108
AM
6599+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6600+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6601+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6602+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6603+AuStubVoid(dbgaufs_fin, void)
6604+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6605+#endif /* CONFIG_DEBUG_FS */
6606+
6607+#endif /* __KERNEL__ */
6608+#endif /* __DBGAUFS_H__ */
7f207e10
AM
6609diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6610--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 6611+++ linux/fs/aufs/dcsub.c 2016-08-17 18:01:06.101888388 +0200
c1595e42 6612@@ -0,0 +1,224 @@
1facf9fc 6613+/*
8cdd5066 6614+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6615+ *
6616+ * This program, aufs is free software; you can redistribute it and/or modify
6617+ * it under the terms of the GNU General Public License as published by
6618+ * the Free Software Foundation; either version 2 of the License, or
6619+ * (at your option) any later version.
dece6358
AM
6620+ *
6621+ * This program is distributed in the hope that it will be useful,
6622+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6623+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6624+ * GNU General Public License for more details.
6625+ *
6626+ * You should have received a copy of the GNU General Public License
523b37e3 6627+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6628+ */
6629+
6630+/*
6631+ * sub-routines for dentry cache
6632+ */
6633+
6634+#include "aufs.h"
6635+
6636+static void au_dpage_free(struct au_dpage *dpage)
6637+{
6638+ int i;
6639+ struct dentry **p;
6640+
6641+ p = dpage->dentries;
6642+ for (i = 0; i < dpage->ndentry; i++)
6643+ dput(*p++);
f0c0a007 6644+ au_delayed_free_page((unsigned long)dpage->dentries);
1facf9fc 6645+}
6646+
6647+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6648+{
6649+ int err;
6650+ void *p;
6651+
6652+ err = -ENOMEM;
6653+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6654+ if (unlikely(!dpages->dpages))
6655+ goto out;
6656+
6657+ p = (void *)__get_free_page(gfp);
6658+ if (unlikely(!p))
6659+ goto out_dpages;
6660+
6661+ dpages->dpages[0].ndentry = 0;
6662+ dpages->dpages[0].dentries = p;
6663+ dpages->ndpage = 1;
6664+ return 0; /* success */
6665+
4f0767ce 6666+out_dpages:
f0c0a007 6667+ au_delayed_kfree(dpages->dpages);
4f0767ce 6668+out:
1facf9fc 6669+ return err;
6670+}
6671+
6672+void au_dpages_free(struct au_dcsub_pages *dpages)
6673+{
6674+ int i;
6675+ struct au_dpage *p;
6676+
6677+ p = dpages->dpages;
6678+ for (i = 0; i < dpages->ndpage; i++)
6679+ au_dpage_free(p++);
f0c0a007 6680+ au_delayed_kfree(dpages->dpages);
1facf9fc 6681+}
6682+
6683+static int au_dpages_append(struct au_dcsub_pages *dpages,
6684+ struct dentry *dentry, gfp_t gfp)
6685+{
6686+ int err, sz;
6687+ struct au_dpage *dpage;
6688+ void *p;
6689+
6690+ dpage = dpages->dpages + dpages->ndpage - 1;
6691+ sz = PAGE_SIZE / sizeof(dentry);
6692+ if (unlikely(dpage->ndentry >= sz)) {
6693+ AuLabel(new dpage);
6694+ err = -ENOMEM;
6695+ sz = dpages->ndpage * sizeof(*dpages->dpages);
6696+ p = au_kzrealloc(dpages->dpages, sz,
6697+ sz + sizeof(*dpages->dpages), gfp);
6698+ if (unlikely(!p))
6699+ goto out;
6700+
6701+ dpages->dpages = p;
6702+ dpage = dpages->dpages + dpages->ndpage;
6703+ p = (void *)__get_free_page(gfp);
6704+ if (unlikely(!p))
6705+ goto out;
6706+
6707+ dpage->ndentry = 0;
6708+ dpage->dentries = p;
6709+ dpages->ndpage++;
6710+ }
6711+
c1595e42 6712+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 6713+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 6714+ return 0; /* success */
6715+
4f0767ce 6716+out:
1facf9fc 6717+ return err;
6718+}
6719+
c1595e42
JR
6720+/* todo: BAD approach */
6721+/* copied from linux/fs/dcache.c */
6722+enum d_walk_ret {
6723+ D_WALK_CONTINUE,
6724+ D_WALK_QUIT,
6725+ D_WALK_NORETRY,
6726+ D_WALK_SKIP,
6727+};
6728+
6729+extern void d_walk(struct dentry *parent, void *data,
6730+ enum d_walk_ret (*enter)(void *, struct dentry *),
6731+ void (*finish)(void *));
6732+
6733+struct ac_dpages_arg {
1facf9fc 6734+ int err;
c1595e42
JR
6735+ struct au_dcsub_pages *dpages;
6736+ struct super_block *sb;
6737+ au_dpages_test test;
6738+ void *arg;
6739+};
1facf9fc 6740+
c1595e42
JR
6741+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6742+{
6743+ enum d_walk_ret ret;
6744+ struct ac_dpages_arg *arg = _arg;
1facf9fc 6745+
c1595e42
JR
6746+ ret = D_WALK_CONTINUE;
6747+ if (dentry->d_sb == arg->sb
6748+ && !IS_ROOT(dentry)
6749+ && au_dcount(dentry) > 0
6750+ && au_di(dentry)
6751+ && (!arg->test || arg->test(dentry, arg->arg))) {
6752+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6753+ if (unlikely(arg->err))
6754+ ret = D_WALK_QUIT;
1facf9fc 6755+ }
6756+
c1595e42
JR
6757+ return ret;
6758+}
027c5e7a 6759+
c1595e42
JR
6760+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6761+ au_dpages_test test, void *arg)
6762+{
6763+ struct ac_dpages_arg args = {
6764+ .err = 0,
6765+ .dpages = dpages,
6766+ .sb = root->d_sb,
6767+ .test = test,
6768+ .arg = arg
6769+ };
027c5e7a 6770+
c1595e42
JR
6771+ d_walk(root, &args, au_call_dpages_append, NULL);
6772+
6773+ return args.err;
1facf9fc 6774+}
6775+
6776+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6777+ int do_include, au_dpages_test test, void *arg)
6778+{
6779+ int err;
6780+
6781+ err = 0;
027c5e7a
AM
6782+ write_seqlock(&rename_lock);
6783+ spin_lock(&dentry->d_lock);
6784+ if (do_include
c1595e42 6785+ && au_dcount(dentry) > 0
027c5e7a 6786+ && (!test || test(dentry, arg)))
1facf9fc 6787+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
6788+ spin_unlock(&dentry->d_lock);
6789+ if (unlikely(err))
6790+ goto out;
6791+
6792+ /*
523b37e3 6793+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
6794+ * mount
6795+ */
1facf9fc 6796+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
6797+ dentry = dentry->d_parent; /* rename_lock is locked */
6798+ spin_lock(&dentry->d_lock);
c1595e42 6799+ if (au_dcount(dentry) > 0
027c5e7a 6800+ && (!test || test(dentry, arg)))
1facf9fc 6801+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
6802+ spin_unlock(&dentry->d_lock);
6803+ if (unlikely(err))
6804+ break;
1facf9fc 6805+ }
6806+
4f0767ce 6807+out:
027c5e7a 6808+ write_sequnlock(&rename_lock);
1facf9fc 6809+ return err;
6810+}
6811+
027c5e7a
AM
6812+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6813+{
6814+ return au_di(dentry) && dentry->d_sb == arg;
6815+}
6816+
6817+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6818+ struct dentry *dentry, int do_include)
6819+{
6820+ return au_dcsub_pages_rev(dpages, dentry, do_include,
6821+ au_dcsub_dpages_aufs, dentry->d_sb);
6822+}
6823+
4a4d8108 6824+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 6825+{
4a4d8108
AM
6826+ struct path path[2] = {
6827+ {
6828+ .dentry = d1
6829+ },
6830+ {
6831+ .dentry = d2
6832+ }
6833+ };
1facf9fc 6834+
4a4d8108 6835+ return path_is_under(path + 0, path + 1);
1facf9fc 6836+}
7f207e10
AM
6837diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6838--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6839+++ linux/fs/aufs/dcsub.h 2016-07-25 19:05:34.811159821 +0200
5527c038 6840@@ -0,0 +1,136 @@
1facf9fc 6841+/*
8cdd5066 6842+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6843+ *
6844+ * This program, aufs is free software; you can redistribute it and/or modify
6845+ * it under the terms of the GNU General Public License as published by
6846+ * the Free Software Foundation; either version 2 of the License, or
6847+ * (at your option) any later version.
dece6358
AM
6848+ *
6849+ * This program is distributed in the hope that it will be useful,
6850+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6851+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6852+ * GNU General Public License for more details.
6853+ *
6854+ * You should have received a copy of the GNU General Public License
523b37e3 6855+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6856+ */
6857+
6858+/*
6859+ * sub-routines for dentry cache
6860+ */
6861+
6862+#ifndef __AUFS_DCSUB_H__
6863+#define __AUFS_DCSUB_H__
6864+
6865+#ifdef __KERNEL__
6866+
7f207e10 6867+#include <linux/dcache.h>
027c5e7a 6868+#include <linux/fs.h>
dece6358 6869+
1facf9fc 6870+struct au_dpage {
6871+ int ndentry;
6872+ struct dentry **dentries;
6873+};
6874+
6875+struct au_dcsub_pages {
6876+ int ndpage;
6877+ struct au_dpage *dpages;
6878+};
6879+
6880+/* ---------------------------------------------------------------------- */
6881+
7f207e10 6882+/* dcsub.c */
1facf9fc 6883+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6884+void au_dpages_free(struct au_dcsub_pages *dpages);
6885+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6886+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6887+ au_dpages_test test, void *arg);
6888+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6889+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
6890+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6891+ struct dentry *dentry, int do_include);
4a4d8108 6892+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 6893+
7f207e10
AM
6894+/* ---------------------------------------------------------------------- */
6895+
523b37e3
AM
6896+/*
6897+ * todo: in linux-3.13, several similar (but faster) helpers are added to
6898+ * include/linux/dcache.h. Try them (in the future).
6899+ */
6900+
027c5e7a
AM
6901+static inline int au_d_hashed_positive(struct dentry *d)
6902+{
6903+ int err;
5527c038 6904+ struct inode *inode = d_inode(d);
076b876e 6905+
027c5e7a 6906+ err = 0;
5527c038
JR
6907+ if (unlikely(d_unhashed(d)
6908+ || d_is_negative(d)
6909+ || !inode->i_nlink))
027c5e7a
AM
6910+ err = -ENOENT;
6911+ return err;
6912+}
6913+
38d290e6
JR
6914+static inline int au_d_linkable(struct dentry *d)
6915+{
6916+ int err;
5527c038 6917+ struct inode *inode = d_inode(d);
076b876e 6918+
38d290e6
JR
6919+ err = au_d_hashed_positive(d);
6920+ if (err
5527c038 6921+ && d_is_positive(d)
38d290e6
JR
6922+ && (inode->i_state & I_LINKABLE))
6923+ err = 0;
6924+ return err;
6925+}
6926+
027c5e7a
AM
6927+static inline int au_d_alive(struct dentry *d)
6928+{
6929+ int err;
6930+ struct inode *inode;
076b876e 6931+
027c5e7a
AM
6932+ err = 0;
6933+ if (!IS_ROOT(d))
6934+ err = au_d_hashed_positive(d);
6935+ else {
5527c038
JR
6936+ inode = d_inode(d);
6937+ if (unlikely(d_unlinked(d)
6938+ || d_is_negative(d)
6939+ || !inode->i_nlink))
027c5e7a
AM
6940+ err = -ENOENT;
6941+ }
6942+ return err;
6943+}
6944+
6945+static inline int au_alive_dir(struct dentry *d)
7f207e10 6946+{
027c5e7a 6947+ int err;
076b876e 6948+
027c5e7a 6949+ err = au_d_alive(d);
5527c038 6950+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
6951+ err = -ENOENT;
6952+ return err;
7f207e10
AM
6953+}
6954+
38d290e6
JR
6955+static inline int au_qstreq(struct qstr *a, struct qstr *b)
6956+{
6957+ return a->len == b->len
6958+ && !memcmp(a->name, b->name, a->len);
6959+}
6960+
7e9cd9fe
AM
6961+/*
6962+ * by the commit
6963+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6964+ * taking d_lock
6965+ * the type of d_lockref.count became int, but the inlined function d_count()
6966+ * still returns unsigned int.
6967+ * I don't know why. Maybe it is for every d_count() users?
6968+ * Anyway au_dcount() lives on.
6969+ */
c1595e42
JR
6970+static inline int au_dcount(struct dentry *d)
6971+{
6972+ return (int)d_count(d);
6973+}
6974+
1facf9fc 6975+#endif /* __KERNEL__ */
6976+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
6977diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6978--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
6979+++ linux/fs/aufs/debug.c 2016-08-17 18:01:06.101888388 +0200
6980@@ -0,0 +1,440 @@
1facf9fc 6981+/*
8cdd5066 6982+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6983+ *
6984+ * This program, aufs is free software; you can redistribute it and/or modify
6985+ * it under the terms of the GNU General Public License as published by
6986+ * the Free Software Foundation; either version 2 of the License, or
6987+ * (at your option) any later version.
dece6358
AM
6988+ *
6989+ * This program is distributed in the hope that it will be useful,
6990+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6991+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6992+ * GNU General Public License for more details.
6993+ *
6994+ * You should have received a copy of the GNU General Public License
523b37e3 6995+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6996+ */
6997+
6998+/*
6999+ * debug print functions
7000+ */
7001+
7002+#include "aufs.h"
7003+
392086de
AM
7004+/* Returns 0, or -errno. arg is in kp->arg. */
7005+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7006+{
7007+ int err, n;
7008+
7009+ err = kstrtoint(val, 0, &n);
7010+ if (!err) {
7011+ if (n > 0)
7012+ au_debug_on();
7013+ else
7014+ au_debug_off();
7015+ }
7016+ return err;
7017+}
7018+
7019+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7020+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7021+{
7022+ atomic_t *a;
7023+
7024+ a = kp->arg;
7025+ return sprintf(buffer, "%d", atomic_read(a));
7026+}
7027+
7028+static struct kernel_param_ops param_ops_atomic_t = {
7029+ .set = param_atomic_t_set,
7030+ .get = param_atomic_t_get
7031+ /* void (*free)(void *arg) */
7032+};
7033+
7034+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7035+MODULE_PARM_DESC(debug, "debug print");
392086de 7036+module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
1facf9fc 7037+
c1595e42 7038+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7039+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7040+#define dpri(fmt, ...) do { \
7041+ if ((au_plevel \
7042+ && strcmp(au_plevel, KERN_DEBUG)) \
7043+ || au_debug_test()) \
7044+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7045+} while (0)
7046+
7047+/* ---------------------------------------------------------------------- */
7048+
7049+void au_dpri_whlist(struct au_nhash *whlist)
7050+{
7051+ unsigned long ul, n;
7052+ struct hlist_head *head;
c06a8ce3 7053+ struct au_vdir_wh *pos;
1facf9fc 7054+
7055+ n = whlist->nh_num;
7056+ head = whlist->nh_head;
7057+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7058+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7059+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7060+ pos->wh_bindex,
7061+ pos->wh_str.len, pos->wh_str.name,
7062+ pos->wh_str.len);
1facf9fc 7063+ head++;
7064+ }
7065+}
7066+
7067+void au_dpri_vdir(struct au_vdir *vdir)
7068+{
7069+ unsigned long ul;
7070+ union au_vdir_deblk_p p;
7071+ unsigned char *o;
7072+
7073+ if (!vdir || IS_ERR(vdir)) {
7074+ dpri("err %ld\n", PTR_ERR(vdir));
7075+ return;
7076+ }
7077+
7078+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
7079+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7080+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7081+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7082+ p.deblk = vdir->vd_deblk[ul];
7083+ o = p.deblk;
7084+ dpri("[%lu]: %p\n", ul, o);
7085+ }
7086+}
7087+
53392da6 7088+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7089+ struct dentry *wh)
7090+{
7091+ char *n = NULL;
7092+ int l = 0;
7093+
7094+ if (!inode || IS_ERR(inode)) {
7095+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7096+ return -1;
7097+ }
7098+
c2b27bf2 7099+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7100+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7101+ && sizeof(inode->i_blocks) != sizeof(u64));
7102+ if (wh) {
7103+ n = (void *)wh->d_name.name;
7104+ l = wh->d_name.len;
7105+ }
7106+
53392da6
AM
7107+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7108+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7109+ bindex, inode,
1facf9fc 7110+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7111+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7112+ i_size_read(inode), (unsigned long long)inode->i_blocks,
53392da6 7113+ hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7114+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
b752ccd1
AM
7115+ inode->i_state, inode->i_flags, inode->i_version,
7116+ inode->i_generation,
1facf9fc 7117+ l ? ", wh " : "", l, n);
7118+ return 0;
7119+}
7120+
7121+void au_dpri_inode(struct inode *inode)
7122+{
7123+ struct au_iinfo *iinfo;
5afbbe0d 7124+ struct au_hinode *hi;
1facf9fc 7125+ aufs_bindex_t bindex;
53392da6 7126+ int err, hn;
1facf9fc 7127+
53392da6 7128+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7129+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7130+ return;
7131+
7132+ iinfo = au_ii(inode);
5afbbe0d
AM
7133+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7134+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7135+ if (iinfo->ii_btop < 0)
1facf9fc 7136+ return;
53392da6 7137+ hn = 0;
5afbbe0d
AM
7138+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7139+ hi = au_hinode(iinfo, bindex);
7140+ hn = !!au_hn(hi);
7141+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7142+ }
1facf9fc 7143+}
7144+
2cbb1c4b
JR
7145+void au_dpri_dalias(struct inode *inode)
7146+{
7147+ struct dentry *d;
7148+
7149+ spin_lock(&inode->i_lock);
c1595e42 7150+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7151+ au_dpri_dentry(d);
7152+ spin_unlock(&inode->i_lock);
7153+}
7154+
1facf9fc 7155+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7156+{
7157+ struct dentry *wh = NULL;
53392da6 7158+ int hn;
5afbbe0d 7159+ struct inode *inode;
076b876e 7160+ struct au_iinfo *iinfo;
5afbbe0d 7161+ struct au_hinode *hi;
1facf9fc 7162+
7163+ if (!dentry || IS_ERR(dentry)) {
7164+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7165+ return -1;
7166+ }
7167+ /* do not call dget_parent() here */
027c5e7a 7168+ /* note: access d_xxx without d_lock */
523b37e3
AM
7169+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7170+ bindex, dentry, dentry,
1facf9fc 7171+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7172+ au_dcount(dentry), dentry->d_flags,
523b37e3 7173+ d_unhashed(dentry) ? "un" : "");
53392da6 7174+ hn = -1;
5afbbe0d
AM
7175+ inode = NULL;
7176+ if (d_is_positive(dentry))
7177+ inode = d_inode(dentry);
7178+ if (inode
7179+ && au_test_aufs(dentry->d_sb)
7180+ && bindex >= 0
7181+ && !au_is_bad_inode(inode)) {
7182+ iinfo = au_ii(inode);
7183+ hi = au_hinode(iinfo, bindex);
7184+ hn = !!au_hn(hi);
7185+ wh = hi->hi_whdentry;
7186+ }
7187+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7188+ return 0;
7189+}
7190+
7191+void au_dpri_dentry(struct dentry *dentry)
7192+{
7193+ struct au_dinfo *dinfo;
7194+ aufs_bindex_t bindex;
7195+ int err;
7196+
7197+ err = do_pri_dentry(-1, dentry);
7198+ if (err || !au_test_aufs(dentry->d_sb))
7199+ return;
7200+
7201+ dinfo = au_di(dentry);
7202+ if (!dinfo)
7203+ return;
5afbbe0d
AM
7204+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7205+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7206+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7207+ dinfo->di_tmpfile);
5afbbe0d 7208+ if (dinfo->di_btop < 0)
1facf9fc 7209+ return;
5afbbe0d
AM
7210+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7211+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7212+}
7213+
7214+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7215+{
7216+ char a[32];
7217+
7218+ if (!file || IS_ERR(file)) {
7219+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7220+ return -1;
7221+ }
7222+ a[0] = 0;
7223+ if (bindex < 0
b912730e 7224+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7225+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7226+ && au_fi(file))
e49829fe 7227+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7228+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7229+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7230+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7231+ file->f_version, file->f_pos, a);
b912730e 7232+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7233+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7234+ return 0;
7235+}
7236+
7237+void au_dpri_file(struct file *file)
7238+{
7239+ struct au_finfo *finfo;
4a4d8108
AM
7240+ struct au_fidir *fidir;
7241+ struct au_hfile *hfile;
1facf9fc 7242+ aufs_bindex_t bindex;
7243+ int err;
7244+
7245+ err = do_pri_file(-1, file);
2000de60 7246+ if (err
b912730e 7247+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7248+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7249+ return;
7250+
7251+ finfo = au_fi(file);
7252+ if (!finfo)
7253+ return;
4a4d8108 7254+ if (finfo->fi_btop < 0)
1facf9fc 7255+ return;
4a4d8108
AM
7256+ fidir = finfo->fi_hdir;
7257+ if (!fidir)
7258+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7259+ else
e49829fe
JR
7260+ for (bindex = finfo->fi_btop;
7261+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7262+ bindex++) {
7263+ hfile = fidir->fd_hfile + bindex;
7264+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7265+ }
1facf9fc 7266+}
7267+
7268+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7269+{
7270+ struct vfsmount *mnt;
7271+ struct super_block *sb;
7272+
7273+ if (!br || IS_ERR(br))
7274+ goto out;
86dc4139 7275+ mnt = au_br_mnt(br);
1facf9fc 7276+ if (!mnt || IS_ERR(mnt))
7277+ goto out;
7278+ sb = mnt->mnt_sb;
7279+ if (!sb || IS_ERR(sb))
7280+ goto out;
7281+
5afbbe0d 7282+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7283+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7284+ "xino %d\n",
5afbbe0d 7285+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7286+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7287+ sb->s_flags, sb->s_count,
1facf9fc 7288+ atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7289+ return 0;
7290+
4f0767ce 7291+out:
1facf9fc 7292+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7293+ return -1;
7294+}
7295+
7296+void au_dpri_sb(struct super_block *sb)
7297+{
7298+ struct au_sbinfo *sbinfo;
7299+ aufs_bindex_t bindex;
7300+ int err;
7301+ /* to reuduce stack size */
7302+ struct {
7303+ struct vfsmount mnt;
7304+ struct au_branch fake;
7305+ } *a;
7306+
7307+ /* this function can be called from magic sysrq */
7308+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7309+ if (unlikely(!a)) {
7310+ dpri("no memory\n");
7311+ return;
7312+ }
7313+
7314+ a->mnt.mnt_sb = sb;
86dc4139 7315+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7316+ au_br_count_init(&a->fake);
1facf9fc 7317+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7318+ au_br_count_fin(&a->fake);
f0c0a007 7319+ au_delayed_kfree(a);
1facf9fc 7320+ dpri("dev 0x%x\n", sb->s_dev);
7321+ if (err || !au_test_aufs(sb))
7322+ return;
7323+
7324+ sbinfo = au_sbi(sb);
7325+ if (!sbinfo)
7326+ return;
f0c0a007
AM
7327+ dpri("nw %d, gen %u, kobj %d\n",
7328+ atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
1facf9fc 7329+ atomic_read(&sbinfo->si_kobj.kref.refcount));
5afbbe0d 7330+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7331+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7332+}
7333+
7334+/* ---------------------------------------------------------------------- */
7335+
027c5e7a
AM
7336+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7337+{
5527c038 7338+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7339+ struct dentry *h_dentry;
5afbbe0d 7340+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7341+
7342+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7343+ return;
7344+
5afbbe0d
AM
7345+ bbot = au_dbbot(dentry);
7346+ bi = au_ibbot(inode);
7347+ if (bi < bbot)
7348+ bbot = bi;
7349+ bindex = au_dbtop(dentry);
7350+ bi = au_ibtop(inode);
027c5e7a
AM
7351+ if (bi > bindex)
7352+ bindex = bi;
7353+
5afbbe0d 7354+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7355+ h_dentry = au_h_dptr(dentry, bindex);
7356+ if (!h_dentry)
7357+ continue;
7358+ h_inode = au_h_iptr(inode, bindex);
5527c038 7359+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7360+ au_debug_on();
027c5e7a
AM
7361+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7362+ AuDbgDentry(dentry);
7363+ AuDbgInode(inode);
392086de 7364+ au_debug_off();
027c5e7a
AM
7365+ BUG();
7366+ }
7367+ }
7368+}
7369+
1facf9fc 7370+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7371+{
7372+ int err, i, j;
7373+ struct au_dcsub_pages dpages;
7374+ struct au_dpage *dpage;
7375+ struct dentry **dentries;
7376+
7377+ err = au_dpages_init(&dpages, GFP_NOFS);
7378+ AuDebugOn(err);
027c5e7a 7379+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7380+ AuDebugOn(err);
7381+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7382+ dpage = dpages.dpages + i;
7383+ dentries = dpage->dentries;
7384+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7385+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7386+ }
7387+ au_dpages_free(&dpages);
7388+}
7389+
1facf9fc 7390+void au_dbg_verify_kthread(void)
7391+{
53392da6 7392+ if (au_wkq_test()) {
1facf9fc 7393+ au_dbg_blocked();
1e00d052
AM
7394+ /*
7395+ * It may be recursive, but udba=notify between two aufs mounts,
7396+ * where a single ro branch is shared, is not a problem.
7397+ */
7398+ /* WARN_ON(1); */
1facf9fc 7399+ }
7400+}
7401+
7402+/* ---------------------------------------------------------------------- */
7403+
1facf9fc 7404+int __init au_debug_init(void)
7405+{
7406+ aufs_bindex_t bindex;
7407+ struct au_vdir_destr destr;
7408+
7409+ bindex = -1;
7410+ AuDebugOn(bindex >= 0);
7411+
7412+ destr.len = -1;
7413+ AuDebugOn(destr.len < NAME_MAX);
7414+
7415+#ifdef CONFIG_4KSTACKS
0c3ec466 7416+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7417+#endif
7418+
1facf9fc 7419+ return 0;
7420+}
7f207e10
AM
7421diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7422--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 7423+++ linux/fs/aufs/debug.h 2016-07-25 19:05:34.811159821 +0200
5527c038 7424@@ -0,0 +1,225 @@
1facf9fc 7425+/*
8cdd5066 7426+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 7427+ *
7428+ * This program, aufs is free software; you can redistribute it and/or modify
7429+ * it under the terms of the GNU General Public License as published by
7430+ * the Free Software Foundation; either version 2 of the License, or
7431+ * (at your option) any later version.
dece6358
AM
7432+ *
7433+ * This program is distributed in the hope that it will be useful,
7434+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7435+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7436+ * GNU General Public License for more details.
7437+ *
7438+ * You should have received a copy of the GNU General Public License
523b37e3 7439+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7440+ */
7441+
7442+/*
7443+ * debug print functions
7444+ */
7445+
7446+#ifndef __AUFS_DEBUG_H__
7447+#define __AUFS_DEBUG_H__
7448+
7449+#ifdef __KERNEL__
7450+
392086de 7451+#include <linux/atomic.h>
4a4d8108
AM
7452+#include <linux/module.h>
7453+#include <linux/kallsyms.h>
1facf9fc 7454+#include <linux/sysrq.h>
4a4d8108 7455+
1facf9fc 7456+#ifdef CONFIG_AUFS_DEBUG
7457+#define AuDebugOn(a) BUG_ON(a)
7458+
7459+/* module parameter */
392086de
AM
7460+extern atomic_t aufs_debug;
7461+static inline void au_debug_on(void)
1facf9fc 7462+{
392086de
AM
7463+ atomic_inc(&aufs_debug);
7464+}
7465+static inline void au_debug_off(void)
7466+{
7467+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7468+}
7469+
7470+static inline int au_debug_test(void)
7471+{
392086de 7472+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7473+}
7474+#else
7475+#define AuDebugOn(a) do {} while (0)
392086de
AM
7476+AuStubVoid(au_debug_on, void)
7477+AuStubVoid(au_debug_off, void)
4a4d8108 7478+AuStubInt0(au_debug_test, void)
1facf9fc 7479+#endif /* CONFIG_AUFS_DEBUG */
7480+
392086de
AM
7481+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7482+
1facf9fc 7483+/* ---------------------------------------------------------------------- */
7484+
7485+/* debug print */
7486+
4a4d8108 7487+#define AuDbg(fmt, ...) do { \
1facf9fc 7488+ if (au_debug_test()) \
4a4d8108 7489+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7490+} while (0)
4a4d8108
AM
7491+#define AuLabel(l) AuDbg(#l "\n")
7492+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7493+#define AuWarn1(fmt, ...) do { \
1facf9fc 7494+ static unsigned char _c; \
7495+ if (!_c++) \
0c3ec466 7496+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7497+} while (0)
7498+
4a4d8108 7499+#define AuErr1(fmt, ...) do { \
1facf9fc 7500+ static unsigned char _c; \
7501+ if (!_c++) \
4a4d8108 7502+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7503+} while (0)
7504+
4a4d8108 7505+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7506+ static unsigned char _c; \
7507+ if (!_c++) \
4a4d8108 7508+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7509+} while (0)
7510+
7511+#define AuUnsupportMsg "This operation is not supported." \
7512+ " Please report this application to aufs-users ML."
4a4d8108
AM
7513+#define AuUnsupport(fmt, ...) do { \
7514+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7515+ dump_stack(); \
7516+} while (0)
7517+
7518+#define AuTraceErr(e) do { \
7519+ if (unlikely((e) < 0)) \
7520+ AuDbg("err %d\n", (int)(e)); \
7521+} while (0)
7522+
7523+#define AuTraceErrPtr(p) do { \
7524+ if (IS_ERR(p)) \
7525+ AuDbg("err %ld\n", PTR_ERR(p)); \
7526+} while (0)
7527+
7528+/* dirty macros for debug print, use with "%.*s" and caution */
7529+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7530+
7531+/* ---------------------------------------------------------------------- */
7532+
dece6358 7533+struct dentry;
1facf9fc 7534+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7535+extern struct mutex au_dbg_mtx;
1facf9fc 7536+extern char *au_plevel;
7537+struct au_nhash;
7538+void au_dpri_whlist(struct au_nhash *whlist);
7539+struct au_vdir;
7540+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7541+struct inode;
1facf9fc 7542+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7543+void au_dpri_dalias(struct inode *inode);
1facf9fc 7544+void au_dpri_dentry(struct dentry *dentry);
dece6358 7545+struct file;
1facf9fc 7546+void au_dpri_file(struct file *filp);
dece6358 7547+struct super_block;
1facf9fc 7548+void au_dpri_sb(struct super_block *sb);
7549+
027c5e7a
AM
7550+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7551+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7552+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7553+void au_dbg_verify_kthread(void);
7554+
7555+int __init au_debug_init(void);
7e9cd9fe 7556+
1facf9fc 7557+#define AuDbgWhlist(w) do { \
c1595e42 7558+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7559+ AuDbg(#w "\n"); \
7560+ au_dpri_whlist(w); \
c1595e42 7561+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7562+} while (0)
7563+
7564+#define AuDbgVdir(v) do { \
c1595e42 7565+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7566+ AuDbg(#v "\n"); \
7567+ au_dpri_vdir(v); \
c1595e42 7568+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7569+} while (0)
7570+
7571+#define AuDbgInode(i) do { \
c1595e42 7572+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7573+ AuDbg(#i "\n"); \
7574+ au_dpri_inode(i); \
c1595e42 7575+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7576+} while (0)
7577+
2cbb1c4b 7578+#define AuDbgDAlias(i) do { \
c1595e42 7579+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7580+ AuDbg(#i "\n"); \
7581+ au_dpri_dalias(i); \
c1595e42 7582+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7583+} while (0)
7584+
1facf9fc 7585+#define AuDbgDentry(d) do { \
c1595e42 7586+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7587+ AuDbg(#d "\n"); \
7588+ au_dpri_dentry(d); \
c1595e42 7589+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7590+} while (0)
7591+
7592+#define AuDbgFile(f) do { \
c1595e42 7593+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7594+ AuDbg(#f "\n"); \
7595+ au_dpri_file(f); \
c1595e42 7596+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7597+} while (0)
7598+
7599+#define AuDbgSb(sb) do { \
c1595e42 7600+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7601+ AuDbg(#sb "\n"); \
7602+ au_dpri_sb(sb); \
c1595e42 7603+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7604+} while (0)
7605+
4a4d8108
AM
7606+#define AuDbgSym(addr) do { \
7607+ char sym[KSYM_SYMBOL_LEN]; \
7608+ sprint_symbol(sym, (unsigned long)addr); \
7609+ AuDbg("%s\n", sym); \
7610+} while (0)
1facf9fc 7611+#else
027c5e7a 7612+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
7613+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7614+AuStubVoid(au_dbg_verify_kthread, void)
7615+AuStubInt0(__init au_debug_init, void)
1facf9fc 7616+
1facf9fc 7617+#define AuDbgWhlist(w) do {} while (0)
7618+#define AuDbgVdir(v) do {} while (0)
7619+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 7620+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 7621+#define AuDbgDentry(d) do {} while (0)
7622+#define AuDbgFile(f) do {} while (0)
7623+#define AuDbgSb(sb) do {} while (0)
4a4d8108 7624+#define AuDbgSym(addr) do {} while (0)
1facf9fc 7625+#endif /* CONFIG_AUFS_DEBUG */
7626+
7627+/* ---------------------------------------------------------------------- */
7628+
7629+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7630+int __init au_sysrq_init(void);
7631+void au_sysrq_fin(void);
7632+
7633+#ifdef CONFIG_HW_CONSOLE
7634+#define au_dbg_blocked() do { \
7635+ WARN_ON(1); \
0c5527e5 7636+ handle_sysrq('w'); \
1facf9fc 7637+} while (0)
7638+#else
4a4d8108 7639+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7640+#endif
7641+
7642+#else
4a4d8108
AM
7643+AuStubInt0(__init au_sysrq_init, void)
7644+AuStubVoid(au_sysrq_fin, void)
7645+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7646+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7647+
7648+#endif /* __KERNEL__ */
7649+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
7650diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7651--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 7652+++ linux/fs/aufs/dentry.c 2016-08-17 18:01:06.111888648 +0200
5afbbe0d 7653@@ -0,0 +1,1128 @@
1facf9fc 7654+/*
8cdd5066 7655+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 7656+ *
7657+ * This program, aufs is free software; you can redistribute it and/or modify
7658+ * it under the terms of the GNU General Public License as published by
7659+ * the Free Software Foundation; either version 2 of the License, or
7660+ * (at your option) any later version.
dece6358
AM
7661+ *
7662+ * This program is distributed in the hope that it will be useful,
7663+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7664+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7665+ * GNU General Public License for more details.
7666+ *
7667+ * You should have received a copy of the GNU General Public License
523b37e3 7668+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7669+ */
7670+
7671+/*
7672+ * lookup and dentry operations
7673+ */
7674+
dece6358 7675+#include <linux/namei.h>
1facf9fc 7676+#include "aufs.h"
7677+
1facf9fc 7678+struct au_do_lookup_args {
7679+ unsigned int flags;
7680+ mode_t type;
1facf9fc 7681+};
7682+
7683+/*
7684+ * returns positive/negative dentry, NULL or an error.
7685+ * NULL means whiteout-ed or not-found.
7686+ */
7687+static struct dentry*
7688+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7689+ aufs_bindex_t bindex, struct qstr *wh_name,
7690+ struct au_do_lookup_args *args)
7691+{
7692+ struct dentry *h_dentry;
2000de60 7693+ struct inode *h_inode;
1facf9fc 7694+ struct au_branch *br;
7695+ int wh_found, opq;
7696+ unsigned char wh_able;
7697+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
7698+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7699+ IGNORE_PERM);
1facf9fc 7700+
1facf9fc 7701+ wh_found = 0;
7702+ br = au_sbr(dentry->d_sb, bindex);
7703+ wh_able = !!au_br_whable(br->br_perm);
7704+ if (wh_able)
076b876e 7705+ wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
1facf9fc 7706+ h_dentry = ERR_PTR(wh_found);
7707+ if (!wh_found)
7708+ goto real_lookup;
7709+ if (unlikely(wh_found < 0))
7710+ goto out;
7711+
7712+ /* We found a whiteout */
5afbbe0d 7713+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 7714+ au_set_dbwh(dentry, bindex);
7715+ if (!allow_neg)
7716+ return NULL; /* success */
7717+
4f0767ce 7718+real_lookup:
076b876e
AM
7719+ if (!ignore_perm)
7720+ h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7721+ else
7722+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
2000de60
JR
7723+ if (IS_ERR(h_dentry)) {
7724+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7725+ && !allow_neg)
7726+ h_dentry = NULL;
1facf9fc 7727+ goto out;
2000de60 7728+ }
1facf9fc 7729+
5527c038
JR
7730+ h_inode = d_inode(h_dentry);
7731+ if (d_is_negative(h_dentry)) {
1facf9fc 7732+ if (!allow_neg)
7733+ goto out_neg;
7734+ } else if (wh_found
7735+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7736+ goto out_neg;
7737+
5afbbe0d
AM
7738+ if (au_dbbot(dentry) <= bindex)
7739+ au_set_dbbot(dentry, bindex);
7740+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
7741+ au_set_dbtop(dentry, bindex);
1facf9fc 7742+ au_set_h_dptr(dentry, bindex, h_dentry);
7743+
2000de60
JR
7744+ if (!d_is_dir(h_dentry)
7745+ || !wh_able
5527c038 7746+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 7747+ goto out; /* success */
7748+
febd17d6 7749+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
076b876e 7750+ opq = au_diropq_test(h_dentry);
febd17d6 7751+ inode_unlock(h_inode);
1facf9fc 7752+ if (opq > 0)
7753+ au_set_dbdiropq(dentry, bindex);
7754+ else if (unlikely(opq < 0)) {
7755+ au_set_h_dptr(dentry, bindex, NULL);
7756+ h_dentry = ERR_PTR(opq);
7757+ }
7758+ goto out;
7759+
4f0767ce 7760+out_neg:
1facf9fc 7761+ dput(h_dentry);
7762+ h_dentry = NULL;
4f0767ce 7763+out:
1facf9fc 7764+ return h_dentry;
7765+}
7766+
dece6358
AM
7767+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7768+{
7769+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7770+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7771+ return -EPERM;
7772+ return 0;
7773+}
7774+
1facf9fc 7775+/*
7776+ * returns the number of lower positive dentries,
7777+ * otherwise an error.
7778+ * can be called at unlinking with @type is zero.
7779+ */
5afbbe0d
AM
7780+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
7781+ unsigned int flags)
1facf9fc 7782+{
7783+ int npositive, err;
7784+ aufs_bindex_t bindex, btail, bdiropq;
076b876e 7785+ unsigned char isdir, dirperm1;
1facf9fc 7786+ struct qstr whname;
7787+ struct au_do_lookup_args args = {
5afbbe0d 7788+ .flags = flags
1facf9fc 7789+ };
7790+ const struct qstr *name = &dentry->d_name;
7791+ struct dentry *parent;
076b876e 7792+ struct super_block *sb;
1facf9fc 7793+
076b876e
AM
7794+ sb = dentry->d_sb;
7795+ err = au_test_shwh(sb, name);
dece6358 7796+ if (unlikely(err))
1facf9fc 7797+ goto out;
7798+
7799+ err = au_wh_name_alloc(&whname, name);
7800+ if (unlikely(err))
7801+ goto out;
7802+
2000de60 7803+ isdir = !!d_is_dir(dentry);
076b876e 7804+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
1facf9fc 7805+
7806+ npositive = 0;
4a4d8108 7807+ parent = dget_parent(dentry);
1facf9fc 7808+ btail = au_dbtaildir(parent);
5afbbe0d 7809+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 7810+ struct dentry *h_parent, *h_dentry;
7811+ struct inode *h_inode, *h_dir;
7812+
7813+ h_dentry = au_h_dptr(dentry, bindex);
7814+ if (h_dentry) {
5527c038 7815+ if (d_is_positive(h_dentry))
1facf9fc 7816+ npositive++;
5afbbe0d 7817+ break;
1facf9fc 7818+ }
7819+ h_parent = au_h_dptr(parent, bindex);
2000de60 7820+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 7821+ continue;
7822+
5527c038 7823+ h_dir = d_inode(h_parent);
febd17d6 7824+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 7825+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7826+ &args);
febd17d6 7827+ inode_unlock(h_dir);
1facf9fc 7828+ err = PTR_ERR(h_dentry);
7829+ if (IS_ERR(h_dentry))
4a4d8108 7830+ goto out_parent;
2000de60
JR
7831+ if (h_dentry)
7832+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
7833+ if (dirperm1)
7834+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 7835+
79b8bda9 7836+ if (au_dbwh(dentry) == bindex)
1facf9fc 7837+ break;
7838+ if (!h_dentry)
7839+ continue;
5527c038 7840+ if (d_is_negative(h_dentry))
1facf9fc 7841+ continue;
5527c038 7842+ h_inode = d_inode(h_dentry);
1facf9fc 7843+ npositive++;
7844+ if (!args.type)
7845+ args.type = h_inode->i_mode & S_IFMT;
7846+ if (args.type != S_IFDIR)
7847+ break;
7848+ else if (isdir) {
7849+ /* the type of lower may be different */
7850+ bdiropq = au_dbdiropq(dentry);
7851+ if (bdiropq >= 0 && bdiropq <= bindex)
7852+ break;
7853+ }
7854+ }
7855+
7856+ if (npositive) {
7857+ AuLabel(positive);
5afbbe0d 7858+ au_update_dbtop(dentry);
1facf9fc 7859+ }
7860+ err = npositive;
076b876e 7861+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 7862+ && au_dbtop(dentry) < 0)) {
1facf9fc 7863+ err = -EIO;
523b37e3
AM
7864+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7865+ dentry, err);
027c5e7a 7866+ }
1facf9fc 7867+
4f0767ce 7868+out_parent:
4a4d8108 7869+ dput(parent);
f0c0a007 7870+ au_delayed_kfree(whname.name);
4f0767ce 7871+out:
1facf9fc 7872+ return err;
7873+}
7874+
076b876e 7875+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 7876+{
7877+ struct dentry *dentry;
7878+ int wkq_err;
7879+
5527c038 7880+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 7881+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 7882+ else {
b4510431
AM
7883+ struct vfsub_lkup_one_args args = {
7884+ .errp = &dentry,
7885+ .name = name,
7886+ .parent = parent
1facf9fc 7887+ };
7888+
b4510431 7889+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 7890+ if (unlikely(wkq_err))
7891+ dentry = ERR_PTR(wkq_err);
7892+ }
7893+
7894+ return dentry;
7895+}
7896+
7897+/*
7898+ * lookup @dentry on @bindex which should be negative.
7899+ */
86dc4139 7900+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 7901+{
7902+ int err;
7903+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 7904+ struct au_branch *br;
1facf9fc 7905+
1facf9fc 7906+ parent = dget_parent(dentry);
7907+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
7908+ br = au_sbr(dentry->d_sb, bindex);
7909+ if (wh)
7910+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7911+ else
076b876e 7912+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 7913+ err = PTR_ERR(h_dentry);
7914+ if (IS_ERR(h_dentry))
7915+ goto out;
5527c038 7916+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 7917+ err = -EIO;
523b37e3 7918+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 7919+ dput(h_dentry);
7920+ goto out;
7921+ }
7922+
4a4d8108 7923+ err = 0;
5afbbe0d
AM
7924+ if (bindex < au_dbtop(dentry))
7925+ au_set_dbtop(dentry, bindex);
7926+ if (au_dbbot(dentry) < bindex)
7927+ au_set_dbbot(dentry, bindex);
1facf9fc 7928+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 7929+
4f0767ce 7930+out:
1facf9fc 7931+ dput(parent);
7932+ return err;
7933+}
7934+
7935+/* ---------------------------------------------------------------------- */
7936+
7937+/* subset of struct inode */
7938+struct au_iattr {
7939+ unsigned long i_ino;
7940+ /* unsigned int i_nlink; */
0c3ec466
AM
7941+ kuid_t i_uid;
7942+ kgid_t i_gid;
1facf9fc 7943+ u64 i_version;
7944+/*
7945+ loff_t i_size;
7946+ blkcnt_t i_blocks;
7947+*/
7948+ umode_t i_mode;
7949+};
7950+
7951+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7952+{
7953+ ia->i_ino = h_inode->i_ino;
7954+ /* ia->i_nlink = h_inode->i_nlink; */
7955+ ia->i_uid = h_inode->i_uid;
7956+ ia->i_gid = h_inode->i_gid;
7957+ ia->i_version = h_inode->i_version;
7958+/*
7959+ ia->i_size = h_inode->i_size;
7960+ ia->i_blocks = h_inode->i_blocks;
7961+*/
7962+ ia->i_mode = (h_inode->i_mode & S_IFMT);
7963+}
7964+
7965+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7966+{
7967+ return ia->i_ino != h_inode->i_ino
7968+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 7969+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 7970+ || !gid_eq(ia->i_gid, h_inode->i_gid)
1facf9fc 7971+ || ia->i_version != h_inode->i_version
7972+/*
7973+ || ia->i_size != h_inode->i_size
7974+ || ia->i_blocks != h_inode->i_blocks
7975+*/
7976+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
7977+}
7978+
7979+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7980+ struct au_branch *br)
7981+{
7982+ int err;
7983+ struct au_iattr ia;
7984+ struct inode *h_inode;
7985+ struct dentry *h_d;
7986+ struct super_block *h_sb;
7987+
7988+ err = 0;
7989+ memset(&ia, -1, sizeof(ia));
7990+ h_sb = h_dentry->d_sb;
5527c038
JR
7991+ h_inode = NULL;
7992+ if (d_is_positive(h_dentry)) {
7993+ h_inode = d_inode(h_dentry);
1facf9fc 7994+ au_iattr_save(&ia, h_inode);
5527c038 7995+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 7996+ /* nfs d_revalidate may return 0 for negative dentry */
7997+ /* fuse d_revalidate always return 0 for negative dentry */
7998+ goto out;
7999+
8000+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 8001+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 8002+ err = PTR_ERR(h_d);
8003+ if (IS_ERR(h_d))
8004+ goto out;
8005+
8006+ err = 0;
8007+ if (unlikely(h_d != h_dentry
5527c038 8008+ || d_inode(h_d) != h_inode
1facf9fc 8009+ || (h_inode && au_iattr_test(&ia, h_inode))))
8010+ err = au_busy_or_stale();
8011+ dput(h_d);
8012+
4f0767ce 8013+out:
1facf9fc 8014+ AuTraceErr(err);
8015+ return err;
8016+}
8017+
8018+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8019+ struct dentry *h_parent, struct au_branch *br)
8020+{
8021+ int err;
8022+
8023+ err = 0;
027c5e7a
AM
8024+ if (udba == AuOpt_UDBA_REVAL
8025+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8026+ IMustLock(h_dir);
5527c038 8027+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8028+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8029+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8030+
8031+ return err;
8032+}
8033+
8034+/* ---------------------------------------------------------------------- */
8035+
027c5e7a 8036+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8037+{
027c5e7a 8038+ int err;
5afbbe0d 8039+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8040+ struct au_hdentry tmp, *p, *q;
8041+ struct au_dinfo *dinfo;
8042+ struct super_block *sb;
1facf9fc 8043+
027c5e7a 8044+ DiMustWriteLock(dentry);
1308ab2a 8045+
027c5e7a
AM
8046+ sb = dentry->d_sb;
8047+ dinfo = au_di(dentry);
5afbbe0d 8048+ bbot = dinfo->di_bbot;
1facf9fc 8049+ bwh = dinfo->di_bwh;
8050+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8051+ bindex = dinfo->di_btop;
8052+ p = au_hdentry(dinfo, bindex);
8053+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8054+ if (!p->hd_dentry)
1facf9fc 8055+ continue;
8056+
027c5e7a
AM
8057+ new_bindex = au_br_index(sb, p->hd_id);
8058+ if (new_bindex == bindex)
1facf9fc 8059+ continue;
1facf9fc 8060+
1facf9fc 8061+ if (dinfo->di_bwh == bindex)
8062+ bwh = new_bindex;
8063+ if (dinfo->di_bdiropq == bindex)
8064+ bdiropq = new_bindex;
8065+ if (new_bindex < 0) {
8066+ au_hdput(p);
8067+ p->hd_dentry = NULL;
8068+ continue;
8069+ }
8070+
8071+ /* swap two lower dentries, and loop again */
5afbbe0d 8072+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8073+ tmp = *q;
8074+ *q = *p;
8075+ *p = tmp;
8076+ if (tmp.hd_dentry) {
8077+ bindex--;
8078+ p--;
8079+ }
8080+ }
8081+
1facf9fc 8082+ dinfo->di_bwh = -1;
5afbbe0d 8083+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8084+ dinfo->di_bwh = bwh;
8085+
8086+ dinfo->di_bdiropq = -1;
8087+ if (bdiropq >= 0
5afbbe0d 8088+ && bdiropq <= au_sbbot(sb)
1facf9fc 8089+ && au_sbr_whable(sb, bdiropq))
8090+ dinfo->di_bdiropq = bdiropq;
8091+
027c5e7a 8092+ err = -EIO;
5afbbe0d
AM
8093+ dinfo->di_btop = -1;
8094+ dinfo->di_bbot = -1;
8095+ bbot = au_dbbot(parent);
8096+ bindex = 0;
8097+ p = au_hdentry(dinfo, bindex);
8098+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8099+ if (p->hd_dentry) {
5afbbe0d 8100+ dinfo->di_btop = bindex;
1facf9fc 8101+ break;
8102+ }
8103+
5afbbe0d
AM
8104+ if (dinfo->di_btop >= 0) {
8105+ bindex = bbot;
8106+ p = au_hdentry(dinfo, bindex);
8107+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8108+ if (p->hd_dentry) {
5afbbe0d 8109+ dinfo->di_bbot = bindex;
027c5e7a
AM
8110+ err = 0;
8111+ break;
8112+ }
8113+ }
8114+
8115+ return err;
1facf9fc 8116+}
8117+
027c5e7a 8118+static void au_do_hide(struct dentry *dentry)
1facf9fc 8119+{
027c5e7a 8120+ struct inode *inode;
1facf9fc 8121+
5527c038
JR
8122+ if (d_really_is_positive(dentry)) {
8123+ inode = d_inode(dentry);
8124+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8125+ if (inode->i_nlink && !d_unhashed(dentry))
8126+ drop_nlink(inode);
8127+ } else {
8128+ clear_nlink(inode);
8129+ /* stop next lookup */
8130+ inode->i_flags |= S_DEAD;
8131+ }
8132+ smp_mb(); /* necessary? */
8133+ }
8134+ d_drop(dentry);
8135+}
1308ab2a 8136+
027c5e7a
AM
8137+static int au_hide_children(struct dentry *parent)
8138+{
8139+ int err, i, j, ndentry;
8140+ struct au_dcsub_pages dpages;
8141+ struct au_dpage *dpage;
8142+ struct dentry *dentry;
1facf9fc 8143+
027c5e7a 8144+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8145+ if (unlikely(err))
8146+ goto out;
027c5e7a
AM
8147+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8148+ if (unlikely(err))
8149+ goto out_dpages;
1facf9fc 8150+
027c5e7a
AM
8151+ /* in reverse order */
8152+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8153+ dpage = dpages.dpages + i;
8154+ ndentry = dpage->ndentry;
8155+ for (j = ndentry - 1; j >= 0; j--) {
8156+ dentry = dpage->dentries[j];
8157+ if (dentry != parent)
8158+ au_do_hide(dentry);
8159+ }
8160+ }
1facf9fc 8161+
027c5e7a
AM
8162+out_dpages:
8163+ au_dpages_free(&dpages);
4f0767ce 8164+out:
027c5e7a 8165+ return err;
1facf9fc 8166+}
8167+
027c5e7a 8168+static void au_hide(struct dentry *dentry)
1facf9fc 8169+{
027c5e7a 8170+ int err;
1facf9fc 8171+
027c5e7a 8172+ AuDbgDentry(dentry);
2000de60 8173+ if (d_is_dir(dentry)) {
027c5e7a
AM
8174+ /* shrink_dcache_parent(dentry); */
8175+ err = au_hide_children(dentry);
8176+ if (unlikely(err))
523b37e3
AM
8177+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8178+ dentry, err);
027c5e7a
AM
8179+ }
8180+ au_do_hide(dentry);
8181+}
1facf9fc 8182+
027c5e7a
AM
8183+/*
8184+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8185+ *
8186+ * a dirty branch is added
8187+ * - on the top of layers
8188+ * - in the middle of layers
8189+ * - to the bottom of layers
8190+ *
8191+ * on the added branch there exists
8192+ * - a whiteout
8193+ * - a diropq
8194+ * - a same named entry
8195+ * + exist
8196+ * * negative --> positive
8197+ * * positive --> positive
8198+ * - type is unchanged
8199+ * - type is changed
8200+ * + doesn't exist
8201+ * * negative --> negative
8202+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8203+ * - none
8204+ */
8205+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8206+ struct au_dinfo *tmp)
8207+{
8208+ int err;
5afbbe0d 8209+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8210+ struct {
8211+ struct dentry *dentry;
8212+ struct inode *inode;
8213+ mode_t mode;
be52b249
AM
8214+ } orig_h, tmp_h = {
8215+ .dentry = NULL
8216+ };
027c5e7a
AM
8217+ struct au_hdentry *hd;
8218+ struct inode *inode, *h_inode;
8219+ struct dentry *h_dentry;
8220+
8221+ err = 0;
5afbbe0d 8222+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8223+ orig_h.mode = 0;
5afbbe0d 8224+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8225+ orig_h.inode = NULL;
8226+ if (d_is_positive(orig_h.dentry)) {
8227+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8228+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8229+ }
5afbbe0d
AM
8230+ if (tmp->di_btop >= 0) {
8231+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8232+ if (d_is_positive(tmp_h.dentry)) {
8233+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8234+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8235+ }
027c5e7a
AM
8236+ }
8237+
5527c038
JR
8238+ inode = NULL;
8239+ if (d_really_is_positive(dentry))
8240+ inode = d_inode(dentry);
027c5e7a
AM
8241+ if (!orig_h.inode) {
8242+ AuDbg("nagative originally\n");
8243+ if (inode) {
8244+ au_hide(dentry);
8245+ goto out;
8246+ }
8247+ AuDebugOn(inode);
5afbbe0d 8248+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8249+ AuDebugOn(dinfo->di_bdiropq != -1);
8250+
8251+ if (!tmp_h.inode) {
8252+ AuDbg("negative --> negative\n");
8253+ /* should have only one negative lower */
5afbbe0d
AM
8254+ if (tmp->di_btop >= 0
8255+ && tmp->di_btop < dinfo->di_btop) {
8256+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8257+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8258+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8259+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8260+ hd = au_hdentry(tmp, tmp->di_btop);
8261+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8262+ dget(hd->hd_dentry));
8263+ }
8264+ au_dbg_verify_dinode(dentry);
8265+ } else {
8266+ AuDbg("negative --> positive\n");
8267+ /*
8268+ * similar to the behaviour of creating with bypassing
8269+ * aufs.
8270+ * unhash it in order to force an error in the
8271+ * succeeding create operation.
8272+ * we should not set S_DEAD here.
8273+ */
8274+ d_drop(dentry);
8275+ /* au_di_swap(tmp, dinfo); */
8276+ au_dbg_verify_dinode(dentry);
8277+ }
8278+ } else {
8279+ AuDbg("positive originally\n");
8280+ /* inode may be NULL */
8281+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8282+ if (!tmp_h.inode) {
8283+ AuDbg("positive --> negative\n");
8284+ /* or bypassing aufs */
8285+ au_hide(dentry);
5afbbe0d 8286+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8287+ dinfo->di_bwh = tmp->di_bwh;
8288+ if (inode)
8289+ err = au_refresh_hinode_self(inode);
8290+ au_dbg_verify_dinode(dentry);
8291+ } else if (orig_h.mode == tmp_h.mode) {
8292+ AuDbg("positive --> positive, same type\n");
8293+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8294+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8295+ /*
8296+ * similar to the behaviour of removing and
8297+ * creating.
8298+ */
8299+ au_hide(dentry);
8300+ if (inode)
8301+ err = au_refresh_hinode_self(inode);
8302+ au_dbg_verify_dinode(dentry);
8303+ } else {
8304+ /* fill empty slots */
5afbbe0d
AM
8305+ if (dinfo->di_btop > tmp->di_btop)
8306+ dinfo->di_btop = tmp->di_btop;
8307+ if (dinfo->di_bbot < tmp->di_bbot)
8308+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8309+ dinfo->di_bwh = tmp->di_bwh;
8310+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8311+ bbot = dinfo->di_bbot;
8312+ bindex = tmp->di_btop;
8313+ hd = au_hdentry(tmp, bindex);
8314+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8315+ if (au_h_dptr(dentry, bindex))
8316+ continue;
5afbbe0d 8317+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8318+ if (!h_dentry)
8319+ continue;
5527c038
JR
8320+ AuDebugOn(d_is_negative(h_dentry));
8321+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8322+ AuDebugOn(orig_h.mode
8323+ != (h_inode->i_mode
8324+ & S_IFMT));
8325+ au_set_h_dptr(dentry, bindex,
8326+ dget(h_dentry));
8327+ }
5afbbe0d
AM
8328+ if (inode)
8329+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8330+ au_dbg_verify_dinode(dentry);
8331+ }
8332+ } else {
8333+ AuDbg("positive --> positive, different type\n");
8334+ /* similar to the behaviour of removing and creating */
8335+ au_hide(dentry);
8336+ if (inode)
8337+ err = au_refresh_hinode_self(inode);
8338+ au_dbg_verify_dinode(dentry);
8339+ }
8340+ }
8341+
8342+out:
8343+ return err;
8344+}
8345+
79b8bda9
AM
8346+void au_refresh_dop(struct dentry *dentry, int force_reval)
8347+{
8348+ const struct dentry_operations *dop
8349+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8350+ static const unsigned int mask
8351+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8352+
8353+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8354+
8355+ if (dentry->d_op == dop)
8356+ return;
8357+
8358+ AuDbg("%pd\n", dentry);
8359+ spin_lock(&dentry->d_lock);
8360+ if (dop == &aufs_dop)
8361+ dentry->d_flags |= mask;
8362+ else
8363+ dentry->d_flags &= ~mask;
8364+ dentry->d_op = dop;
8365+ spin_unlock(&dentry->d_lock);
8366+}
8367+
027c5e7a
AM
8368+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8369+{
8370+ int err, ebrange;
8371+ unsigned int sigen;
8372+ struct au_dinfo *dinfo, *tmp;
8373+ struct super_block *sb;
8374+ struct inode *inode;
8375+
8376+ DiMustWriteLock(dentry);
8377+ AuDebugOn(IS_ROOT(dentry));
5527c038 8378+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8379+
8380+ sb = dentry->d_sb;
027c5e7a
AM
8381+ sigen = au_sigen(sb);
8382+ err = au_digen_test(parent, sigen);
8383+ if (unlikely(err))
8384+ goto out;
8385+
8386+ dinfo = au_di(dentry);
5afbbe0d 8387+ err = au_di_realloc(dinfo, au_sbbot(sb) + 1);
027c5e7a
AM
8388+ if (unlikely(err))
8389+ goto out;
8390+ ebrange = au_dbrange_test(dentry);
8391+ if (!ebrange)
8392+ ebrange = au_do_refresh_hdentry(dentry, parent);
8393+
38d290e6 8394+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8395+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8396+ if (d_really_is_positive(dentry)) {
8397+ inode = d_inode(dentry);
027c5e7a 8398+ err = au_refresh_hinode_self(inode);
5527c038 8399+ }
027c5e7a
AM
8400+ au_dbg_verify_dinode(dentry);
8401+ if (!err)
8402+ goto out_dgen; /* success */
8403+ goto out;
8404+ }
8405+
8406+ /* temporary dinfo */
8407+ AuDbgDentry(dentry);
8408+ err = -ENOMEM;
8409+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8410+ if (unlikely(!tmp))
8411+ goto out;
8412+ au_di_swap(tmp, dinfo);
8413+ /* returns the number of positive dentries */
8414+ /*
8415+ * if current working dir is removed, it returns an error.
8416+ * but the dentry is legal.
8417+ */
5afbbe0d 8418+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8419+ AuDbgDentry(dentry);
8420+ au_di_swap(tmp, dinfo);
8421+ if (err == -ENOENT)
8422+ err = 0;
8423+ if (err >= 0) {
8424+ /* compare/refresh by dinfo */
8425+ AuDbgDentry(dentry);
8426+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8427+ au_dbg_verify_dinode(dentry);
8428+ AuTraceErr(err);
8429+ }
8430+ au_rw_write_unlock(&tmp->di_rwsem);
8431+ au_di_free(tmp);
8432+ if (unlikely(err))
8433+ goto out;
8434+
8435+out_dgen:
8436+ au_update_digen(dentry);
8437+out:
8438+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8439+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8440+ AuDbgDentry(dentry);
8441+ }
8442+ AuTraceErr(err);
8443+ return err;
8444+}
8445+
b4510431
AM
8446+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8447+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8448+{
8449+ int err, valid;
027c5e7a
AM
8450+
8451+ err = 0;
8452+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8453+ goto out;
027c5e7a
AM
8454+
8455+ AuDbg("b%d\n", bindex);
b4510431
AM
8456+ /*
8457+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8458+ * due to whiteout and branch permission.
8459+ */
8460+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8461+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8462+ /* it may return tri-state */
8463+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8464+
8465+ if (unlikely(valid < 0))
8466+ err = valid;
8467+ else if (!valid)
8468+ err = -EINVAL;
8469+
4f0767ce 8470+out:
1facf9fc 8471+ AuTraceErr(err);
8472+ return err;
8473+}
8474+
8475+/* todo: remove this */
8476+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
b4510431 8477+ unsigned int flags, int do_udba)
1facf9fc 8478+{
8479+ int err;
8480+ umode_t mode, h_mode;
5afbbe0d 8481+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8482+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8483+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8484+ struct dentry *h_dentry;
8485+ struct qstr *name, *h_name;
8486+
8487+ err = 0;
8488+ plus = 0;
8489+ mode = 0;
1facf9fc 8490+ ibs = -1;
8491+ ibe = -1;
8492+ unhashed = !!d_unhashed(dentry);
8493+ is_root = !!IS_ROOT(dentry);
8494+ name = &dentry->d_name;
38d290e6 8495+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8496+
8497+ /*
7f207e10
AM
8498+ * Theoretically, REVAL test should be unnecessary in case of
8499+ * {FS,I}NOTIFY.
8500+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8501+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8502+ * Let's do REVAL test too.
8503+ */
8504+ if (do_udba && inode) {
8505+ mode = (inode->i_mode & S_IFMT);
8506+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8507+ ibs = au_ibtop(inode);
8508+ ibe = au_ibbot(inode);
1facf9fc 8509+ }
8510+
5afbbe0d
AM
8511+ btop = au_dbtop(dentry);
8512+ btail = btop;
1facf9fc 8513+ if (inode && S_ISDIR(inode->i_mode))
8514+ btail = au_dbtaildir(dentry);
5afbbe0d 8515+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8516+ h_dentry = au_h_dptr(dentry, bindex);
8517+ if (!h_dentry)
8518+ continue;
8519+
523b37e3
AM
8520+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8521+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8522+ spin_lock(&h_dentry->d_lock);
1facf9fc 8523+ h_name = &h_dentry->d_name;
8524+ if (unlikely(do_udba
8525+ && !is_root
523b37e3
AM
8526+ && ((!h_nfs
8527+ && (unhashed != !!d_unhashed(h_dentry)
38d290e6
JR
8528+ || (!tmpfile
8529+ && !au_qstreq(name, h_name))
8530+ ))
523b37e3
AM
8531+ || (h_nfs
8532+ && !(flags & LOOKUP_OPEN)
8533+ && (h_dentry->d_flags
8534+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8535+ )) {
38d290e6
JR
8536+ int h_unhashed;
8537+
8538+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8539+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8540+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8541+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8542+ goto err;
8543+ }
027c5e7a 8544+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8545+
b4510431 8546+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8547+ if (unlikely(err))
8548+ /* do not goto err, to keep the errno */
8549+ break;
8550+
8551+ /* todo: plink too? */
8552+ if (!do_udba)
8553+ continue;
8554+
8555+ /* UDBA tests */
5527c038 8556+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8557+ goto err;
8558+
5527c038
JR
8559+ h_inode = NULL;
8560+ if (d_is_positive(h_dentry))
8561+ h_inode = d_inode(h_dentry);
1facf9fc 8562+ h_plus = plus;
8563+ h_mode = mode;
8564+ h_cached_inode = h_inode;
8565+ if (h_inode) {
8566+ h_mode = (h_inode->i_mode & S_IFMT);
8567+ h_plus = (h_inode->i_nlink > 0);
8568+ }
8569+ if (inode && ibs <= bindex && bindex <= ibe)
8570+ h_cached_inode = au_h_iptr(inode, bindex);
8571+
523b37e3 8572+ if (!h_nfs) {
38d290e6 8573+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8574+ goto err;
8575+ } else {
8576+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8577+ && !is_root
8578+ && !IS_ROOT(h_dentry)
8579+ && unhashed != d_unhashed(h_dentry)))
8580+ goto err;
8581+ }
8582+ if (unlikely(mode != h_mode
1facf9fc 8583+ || h_cached_inode != h_inode))
8584+ goto err;
8585+ continue;
8586+
f6b6e03d 8587+err:
1facf9fc 8588+ err = -EINVAL;
8589+ break;
8590+ }
8591+
523b37e3 8592+ AuTraceErr(err);
1facf9fc 8593+ return err;
8594+}
8595+
027c5e7a 8596+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 8597+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8598+{
8599+ int err;
8600+ struct dentry *parent;
1facf9fc 8601+
027c5e7a 8602+ if (!au_digen_test(dentry, sigen))
1facf9fc 8603+ return 0;
8604+
8605+ parent = dget_parent(dentry);
8606+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8607+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 8608+ au_dbg_verify_gen(parent, sigen);
027c5e7a 8609+ err = au_refresh_dentry(dentry, parent);
1facf9fc 8610+ di_read_unlock(parent, AuLock_IR);
8611+ dput(parent);
027c5e7a 8612+ AuTraceErr(err);
1facf9fc 8613+ return err;
8614+}
8615+
8616+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8617+{
8618+ int err;
8619+ struct dentry *d, *parent;
1facf9fc 8620+
027c5e7a 8621+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 8622+ return simple_reval_dpath(dentry, sigen);
8623+
8624+ /* slow loop, keep it simple and stupid */
8625+ /* cf: au_cpup_dirs() */
8626+ err = 0;
8627+ parent = NULL;
027c5e7a 8628+ while (au_digen_test(dentry, sigen)) {
1facf9fc 8629+ d = dentry;
8630+ while (1) {
8631+ dput(parent);
8632+ parent = dget_parent(d);
027c5e7a 8633+ if (!au_digen_test(parent, sigen))
1facf9fc 8634+ break;
8635+ d = parent;
8636+ }
8637+
1facf9fc 8638+ if (d != dentry)
027c5e7a 8639+ di_write_lock_child2(d);
1facf9fc 8640+
8641+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
8642+ if (au_digen_test(d, sigen)) {
8643+ /*
8644+ * todo: consolidate with simple_reval_dpath(),
8645+ * do_refresh() and au_reval_for_attr().
8646+ */
1facf9fc 8647+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8648+ err = au_refresh_dentry(d, parent);
1facf9fc 8649+ di_read_unlock(parent, AuLock_IR);
8650+ }
8651+
8652+ if (d != dentry)
8653+ di_write_unlock(d);
8654+ dput(parent);
8655+ if (unlikely(err))
8656+ break;
8657+ }
8658+
8659+ return err;
8660+}
8661+
8662+/*
8663+ * if valid returns 1, otherwise 0.
8664+ */
b4510431 8665+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 8666+{
8667+ int valid, err;
8668+ unsigned int sigen;
8669+ unsigned char do_udba;
8670+ struct super_block *sb;
8671+ struct inode *inode;
8672+
027c5e7a 8673+ /* todo: support rcu-walk? */
b4510431 8674+ if (flags & LOOKUP_RCU)
027c5e7a
AM
8675+ return -ECHILD;
8676+
8677+ valid = 0;
8678+ if (unlikely(!au_di(dentry)))
8679+ goto out;
8680+
e49829fe 8681+ valid = 1;
1facf9fc 8682+ sb = dentry->d_sb;
e49829fe
JR
8683+ /*
8684+ * todo: very ugly
8685+ * i_mutex of parent dir may be held,
8686+ * but we should not return 'invalid' due to busy.
8687+ */
8688+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8689+ if (unlikely(err)) {
8690+ valid = err;
027c5e7a 8691+ AuTraceErr(err);
e49829fe
JR
8692+ goto out;
8693+ }
5527c038
JR
8694+ inode = NULL;
8695+ if (d_really_is_positive(dentry))
8696+ inode = d_inode(dentry);
5afbbe0d 8697+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
8698+ err = -EINVAL;
8699+ AuTraceErr(err);
8700+ goto out_dgrade;
8701+ }
027c5e7a
AM
8702+ if (unlikely(au_dbrange_test(dentry))) {
8703+ err = -EINVAL;
8704+ AuTraceErr(err);
8705+ goto out_dgrade;
1facf9fc 8706+ }
027c5e7a
AM
8707+
8708+ sigen = au_sigen(sb);
8709+ if (au_digen_test(dentry, sigen)) {
1facf9fc 8710+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
8711+ err = au_reval_dpath(dentry, sigen);
8712+ if (unlikely(err)) {
8713+ AuTraceErr(err);
1facf9fc 8714+ goto out_dgrade;
027c5e7a 8715+ }
1facf9fc 8716+ }
8717+ di_downgrade_lock(dentry, AuLock_IR);
8718+
1facf9fc 8719+ err = -EINVAL;
c1595e42 8720+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 8721+ && inode
38d290e6 8722+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
8723+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8724+ AuTraceErr(err);
027c5e7a 8725+ goto out_inval;
79b8bda9 8726+ }
027c5e7a 8727+
1facf9fc 8728+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8729+ if (do_udba && inode) {
5afbbe0d 8730+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 8731+ struct inode *h_inode;
1facf9fc 8732+
5afbbe0d
AM
8733+ if (btop >= 0) {
8734+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
8735+ if (h_inode && au_test_higen(inode, h_inode)) {
8736+ AuTraceErr(err);
027c5e7a 8737+ goto out_inval;
79b8bda9 8738+ }
027c5e7a 8739+ }
1facf9fc 8740+ }
8741+
b4510431 8742+ err = h_d_revalidate(dentry, inode, flags, do_udba);
5afbbe0d 8743+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 8744+ err = -EIO;
523b37e3
AM
8745+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
8746+ dentry, err);
027c5e7a 8747+ }
e49829fe 8748+ goto out_inval;
1facf9fc 8749+
4f0767ce 8750+out_dgrade:
1facf9fc 8751+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 8752+out_inval:
1facf9fc 8753+ aufs_read_unlock(dentry, AuLock_IR);
8754+ AuTraceErr(err);
8755+ valid = !err;
e49829fe 8756+out:
027c5e7a 8757+ if (!valid) {
523b37e3 8758+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
8759+ d_drop(dentry);
8760+ }
1facf9fc 8761+ return valid;
8762+}
8763+
8764+static void aufs_d_release(struct dentry *dentry)
8765+{
027c5e7a 8766+ if (au_di(dentry)) {
4a4d8108
AM
8767+ au_di_fin(dentry);
8768+ au_hn_di_reinit(dentry);
1facf9fc 8769+ }
1facf9fc 8770+}
8771+
4a4d8108 8772+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
8773+ .d_revalidate = aufs_d_revalidate,
8774+ .d_weak_revalidate = aufs_d_revalidate,
8775+ .d_release = aufs_d_release
1facf9fc 8776+};
79b8bda9
AM
8777+
8778+/* aufs_dop without d_revalidate */
8779+const struct dentry_operations aufs_dop_noreval = {
8780+ .d_release = aufs_d_release
8781+};
7f207e10
AM
8782diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8783--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
8784+++ linux/fs/aufs/dentry.h 2016-08-17 18:01:06.111888648 +0200
8785@@ -0,0 +1,255 @@
1facf9fc 8786+/*
8cdd5066 8787+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 8788+ *
8789+ * This program, aufs is free software; you can redistribute it and/or modify
8790+ * it under the terms of the GNU General Public License as published by
8791+ * the Free Software Foundation; either version 2 of the License, or
8792+ * (at your option) any later version.
dece6358
AM
8793+ *
8794+ * This program is distributed in the hope that it will be useful,
8795+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8796+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8797+ * GNU General Public License for more details.
8798+ *
8799+ * You should have received a copy of the GNU General Public License
523b37e3 8800+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8801+ */
8802+
8803+/*
8804+ * lookup and dentry operations
8805+ */
8806+
8807+#ifndef __AUFS_DENTRY_H__
8808+#define __AUFS_DENTRY_H__
8809+
8810+#ifdef __KERNEL__
8811+
dece6358 8812+#include <linux/dcache.h>
1facf9fc 8813+#include "rwsem.h"
8814+
1facf9fc 8815+struct au_hdentry {
8816+ struct dentry *hd_dentry;
027c5e7a 8817+ aufs_bindex_t hd_id;
1facf9fc 8818+};
8819+
8820+struct au_dinfo {
8821+ atomic_t di_generation;
8822+
dece6358 8823+ struct au_rwsem di_rwsem;
5afbbe0d 8824+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 8825+ unsigned char di_tmpfile; /* to allow the different name */
f0c0a007
AM
8826+ union {
8827+ struct au_hdentry *di_hdentry;
8828+ struct llist_node di_lnode; /* delayed free */
8829+ };
4a4d8108 8830+} ____cacheline_aligned_in_smp;
1facf9fc 8831+
8832+/* ---------------------------------------------------------------------- */
8833+
5afbbe0d
AM
8834+/* flags for au_lkup_dentry() */
8835+#define AuLkup_ALLOW_NEG 1
8836+#define AuLkup_IGNORE_PERM (1 << 1)
8837+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
8838+#define au_fset_lkup(flags, name) \
8839+ do { (flags) |= AuLkup_##name; } while (0)
8840+#define au_fclr_lkup(flags, name) \
8841+ do { (flags) &= ~AuLkup_##name; } while (0)
8842+
8843+/* ---------------------------------------------------------------------- */
8844+
1facf9fc 8845+/* dentry.c */
79b8bda9 8846+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 8847+struct au_branch;
076b876e 8848+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 8849+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8850+ struct dentry *h_parent, struct au_branch *br);
8851+
5afbbe0d
AM
8852+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8853+ unsigned int flags);
86dc4139 8854+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 8855+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 8856+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 8857+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 8858+
8859+/* dinfo.c */
4a4d8108 8860+void au_di_init_once(void *_di);
027c5e7a
AM
8861+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8862+void au_di_free(struct au_dinfo *dinfo);
8863+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8864+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
8865+int au_di_init(struct dentry *dentry);
8866+void au_di_fin(struct dentry *dentry);
1facf9fc 8867+int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8868+
8869+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8870+void di_read_unlock(struct dentry *d, int flags);
8871+void di_downgrade_lock(struct dentry *d, int flags);
8872+void di_write_lock(struct dentry *d, unsigned int lsc);
8873+void di_write_unlock(struct dentry *d);
8874+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8875+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8876+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8877+
8878+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 8879+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 8880+aufs_bindex_t au_dbtail(struct dentry *dentry);
8881+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8882+
8883+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8884+ struct dentry *h_dentry);
027c5e7a
AM
8885+int au_digen_test(struct dentry *dentry, unsigned int sigen);
8886+int au_dbrange_test(struct dentry *dentry);
1facf9fc 8887+void au_update_digen(struct dentry *dentry);
8888+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
8889+void au_update_dbtop(struct dentry *dentry);
8890+void au_update_dbbot(struct dentry *dentry);
1facf9fc 8891+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8892+
8893+/* ---------------------------------------------------------------------- */
8894+
8895+static inline struct au_dinfo *au_di(struct dentry *dentry)
8896+{
8897+ return dentry->d_fsdata;
8898+}
8899+
8900+/* ---------------------------------------------------------------------- */
8901+
8902+/* lock subclass for dinfo */
8903+enum {
8904+ AuLsc_DI_CHILD, /* child first */
4a4d8108 8905+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 8906+ AuLsc_DI_CHILD3, /* copyup dirs */
8907+ AuLsc_DI_PARENT,
8908+ AuLsc_DI_PARENT2,
027c5e7a
AM
8909+ AuLsc_DI_PARENT3,
8910+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 8911+};
8912+
8913+/*
8914+ * di_read_lock_child, di_write_lock_child,
8915+ * di_read_lock_child2, di_write_lock_child2,
8916+ * di_read_lock_child3, di_write_lock_child3,
8917+ * di_read_lock_parent, di_write_lock_parent,
8918+ * di_read_lock_parent2, di_write_lock_parent2,
8919+ * di_read_lock_parent3, di_write_lock_parent3,
8920+ */
8921+#define AuReadLockFunc(name, lsc) \
8922+static inline void di_read_lock_##name(struct dentry *d, int flags) \
8923+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8924+
8925+#define AuWriteLockFunc(name, lsc) \
8926+static inline void di_write_lock_##name(struct dentry *d) \
8927+{ di_write_lock(d, AuLsc_DI_##lsc); }
8928+
8929+#define AuRWLockFuncs(name, lsc) \
8930+ AuReadLockFunc(name, lsc) \
8931+ AuWriteLockFunc(name, lsc)
8932+
8933+AuRWLockFuncs(child, CHILD);
8934+AuRWLockFuncs(child2, CHILD2);
8935+AuRWLockFuncs(child3, CHILD3);
8936+AuRWLockFuncs(parent, PARENT);
8937+AuRWLockFuncs(parent2, PARENT2);
8938+AuRWLockFuncs(parent3, PARENT3);
8939+
8940+#undef AuReadLockFunc
8941+#undef AuWriteLockFunc
8942+#undef AuRWLockFuncs
8943+
8944+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
8945+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
8946+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 8947+
8948+/* ---------------------------------------------------------------------- */
8949+
8950+/* todo: memory barrier? */
8951+static inline unsigned int au_digen(struct dentry *d)
8952+{
8953+ return atomic_read(&au_di(d)->di_generation);
8954+}
8955+
8956+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8957+{
8958+ hdentry->hd_dentry = NULL;
8959+}
8960+
5afbbe0d
AM
8961+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
8962+ aufs_bindex_t bindex)
8963+{
8964+ return di->di_hdentry + bindex;
8965+}
8966+
1facf9fc 8967+static inline void au_hdput(struct au_hdentry *hd)
8968+{
4a4d8108
AM
8969+ if (hd)
8970+ dput(hd->hd_dentry);
1facf9fc 8971+}
8972+
5afbbe0d 8973+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 8974+{
1308ab2a 8975+ DiMustAnyLock(dentry);
5afbbe0d 8976+ return au_di(dentry)->di_btop;
1facf9fc 8977+}
8978+
5afbbe0d 8979+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 8980+{
1308ab2a 8981+ DiMustAnyLock(dentry);
5afbbe0d 8982+ return au_di(dentry)->di_bbot;
1facf9fc 8983+}
8984+
8985+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8986+{
1308ab2a 8987+ DiMustAnyLock(dentry);
1facf9fc 8988+ return au_di(dentry)->di_bwh;
8989+}
8990+
8991+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8992+{
1308ab2a 8993+ DiMustAnyLock(dentry);
1facf9fc 8994+ return au_di(dentry)->di_bdiropq;
8995+}
8996+
8997+/* todo: hard/soft set? */
5afbbe0d 8998+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 8999+{
1308ab2a 9000+ DiMustWriteLock(dentry);
5afbbe0d 9001+ au_di(dentry)->di_btop = bindex;
1facf9fc 9002+}
9003+
5afbbe0d 9004+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 9005+{
1308ab2a 9006+ DiMustWriteLock(dentry);
5afbbe0d 9007+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9008+}
9009+
9010+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9011+{
1308ab2a 9012+ DiMustWriteLock(dentry);
5afbbe0d 9013+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9014+ au_di(dentry)->di_bwh = bindex;
9015+}
9016+
9017+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9018+{
1308ab2a 9019+ DiMustWriteLock(dentry);
1facf9fc 9020+ au_di(dentry)->di_bdiropq = bindex;
9021+}
9022+
9023+/* ---------------------------------------------------------------------- */
9024+
4a4d8108 9025+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9026+static inline void au_digen_dec(struct dentry *d)
9027+{
e49829fe 9028+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9029+}
9030+
4a4d8108 9031+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9032+{
9033+ dentry->d_fsdata = NULL;
9034+}
9035+#else
4a4d8108
AM
9036+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9037+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9038+
9039+#endif /* __KERNEL__ */
9040+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9041diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9042--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 9043+++ linux/fs/aufs/dinfo.c 2016-08-17 18:01:06.111888648 +0200
5afbbe0d 9044@@ -0,0 +1,552 @@
1facf9fc 9045+/*
8cdd5066 9046+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 9047+ *
9048+ * This program, aufs is free software; you can redistribute it and/or modify
9049+ * it under the terms of the GNU General Public License as published by
9050+ * the Free Software Foundation; either version 2 of the License, or
9051+ * (at your option) any later version.
dece6358
AM
9052+ *
9053+ * This program is distributed in the hope that it will be useful,
9054+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9055+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9056+ * GNU General Public License for more details.
9057+ *
9058+ * You should have received a copy of the GNU General Public License
523b37e3 9059+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9060+ */
9061+
9062+/*
9063+ * dentry private data
9064+ */
9065+
9066+#include "aufs.h"
9067+
e49829fe 9068+void au_di_init_once(void *_dinfo)
4a4d8108 9069+{
e49829fe 9070+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9071+
e49829fe 9072+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9073+}
9074+
027c5e7a 9075+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9076+{
9077+ struct au_dinfo *dinfo;
027c5e7a 9078+ int nbr, i;
1facf9fc 9079+
9080+ dinfo = au_cache_alloc_dinfo();
9081+ if (unlikely(!dinfo))
9082+ goto out;
9083+
5afbbe0d 9084+ nbr = au_sbbot(sb) + 1;
1facf9fc 9085+ if (nbr <= 0)
9086+ nbr = 1;
9087+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9088+ if (dinfo->di_hdentry) {
9089+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9090+ dinfo->di_btop = -1;
9091+ dinfo->di_bbot = -1;
027c5e7a
AM
9092+ dinfo->di_bwh = -1;
9093+ dinfo->di_bdiropq = -1;
38d290e6 9094+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9095+ for (i = 0; i < nbr; i++)
9096+ dinfo->di_hdentry[i].hd_id = -1;
9097+ goto out;
9098+ }
1facf9fc 9099+
f0c0a007 9100+ au_cache_dfree_dinfo(dinfo);
027c5e7a
AM
9101+ dinfo = NULL;
9102+
4f0767ce 9103+out:
027c5e7a 9104+ return dinfo;
1facf9fc 9105+}
9106+
027c5e7a 9107+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9108+{
4a4d8108 9109+ struct au_hdentry *p;
5afbbe0d 9110+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9111+
9112+ /* dentry may not be revalidated */
5afbbe0d 9113+ bindex = dinfo->di_btop;
4a4d8108 9114+ if (bindex >= 0) {
5afbbe0d
AM
9115+ bbot = dinfo->di_bbot;
9116+ p = au_hdentry(dinfo, bindex);
9117+ while (bindex++ <= bbot)
4a4d8108
AM
9118+ au_hdput(p++);
9119+ }
f0c0a007
AM
9120+ au_delayed_kfree(dinfo->di_hdentry);
9121+ au_cache_dfree_dinfo(dinfo);
027c5e7a
AM
9122+}
9123+
9124+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9125+{
9126+ struct au_hdentry *p;
9127+ aufs_bindex_t bi;
9128+
9129+ AuRwMustWriteLock(&a->di_rwsem);
9130+ AuRwMustWriteLock(&b->di_rwsem);
9131+
9132+#define DiSwap(v, name) \
9133+ do { \
9134+ v = a->di_##name; \
9135+ a->di_##name = b->di_##name; \
9136+ b->di_##name = v; \
9137+ } while (0)
9138+
9139+ DiSwap(p, hdentry);
5afbbe0d
AM
9140+ DiSwap(bi, btop);
9141+ DiSwap(bi, bbot);
027c5e7a
AM
9142+ DiSwap(bi, bwh);
9143+ DiSwap(bi, bdiropq);
9144+ /* smp_mb(); */
9145+
9146+#undef DiSwap
9147+}
9148+
9149+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9150+{
9151+ AuRwMustWriteLock(&dst->di_rwsem);
9152+ AuRwMustWriteLock(&src->di_rwsem);
9153+
5afbbe0d
AM
9154+ dst->di_btop = src->di_btop;
9155+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9156+ dst->di_bwh = src->di_bwh;
9157+ dst->di_bdiropq = src->di_bdiropq;
9158+ /* smp_mb(); */
9159+}
9160+
9161+int au_di_init(struct dentry *dentry)
9162+{
9163+ int err;
9164+ struct super_block *sb;
9165+ struct au_dinfo *dinfo;
9166+
9167+ err = 0;
9168+ sb = dentry->d_sb;
9169+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9170+ if (dinfo) {
9171+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9172+ /* smp_mb(); */ /* atomic_set */
9173+ dentry->d_fsdata = dinfo;
9174+ } else
9175+ err = -ENOMEM;
9176+
9177+ return err;
9178+}
9179+
9180+void au_di_fin(struct dentry *dentry)
9181+{
9182+ struct au_dinfo *dinfo;
9183+
9184+ dinfo = au_di(dentry);
9185+ AuRwDestroy(&dinfo->di_rwsem);
9186+ au_di_free(dinfo);
4a4d8108
AM
9187+}
9188+
1facf9fc 9189+int au_di_realloc(struct au_dinfo *dinfo, int nbr)
9190+{
9191+ int err, sz;
9192+ struct au_hdentry *hdp;
9193+
1308ab2a 9194+ AuRwMustWriteLock(&dinfo->di_rwsem);
9195+
1facf9fc 9196+ err = -ENOMEM;
5afbbe0d 9197+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9198+ if (!sz)
9199+ sz = sizeof(*hdp);
9200+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
9201+ if (hdp) {
9202+ dinfo->di_hdentry = hdp;
9203+ err = 0;
9204+ }
9205+
9206+ return err;
9207+}
9208+
9209+/* ---------------------------------------------------------------------- */
9210+
9211+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9212+{
9213+ switch (lsc) {
9214+ case AuLsc_DI_CHILD:
9215+ ii_write_lock_child(inode);
9216+ break;
9217+ case AuLsc_DI_CHILD2:
9218+ ii_write_lock_child2(inode);
9219+ break;
9220+ case AuLsc_DI_CHILD3:
9221+ ii_write_lock_child3(inode);
9222+ break;
9223+ case AuLsc_DI_PARENT:
9224+ ii_write_lock_parent(inode);
9225+ break;
9226+ case AuLsc_DI_PARENT2:
9227+ ii_write_lock_parent2(inode);
9228+ break;
9229+ case AuLsc_DI_PARENT3:
9230+ ii_write_lock_parent3(inode);
9231+ break;
9232+ default:
9233+ BUG();
9234+ }
9235+}
9236+
9237+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9238+{
9239+ switch (lsc) {
9240+ case AuLsc_DI_CHILD:
9241+ ii_read_lock_child(inode);
9242+ break;
9243+ case AuLsc_DI_CHILD2:
9244+ ii_read_lock_child2(inode);
9245+ break;
9246+ case AuLsc_DI_CHILD3:
9247+ ii_read_lock_child3(inode);
9248+ break;
9249+ case AuLsc_DI_PARENT:
9250+ ii_read_lock_parent(inode);
9251+ break;
9252+ case AuLsc_DI_PARENT2:
9253+ ii_read_lock_parent2(inode);
9254+ break;
9255+ case AuLsc_DI_PARENT3:
9256+ ii_read_lock_parent3(inode);
9257+ break;
9258+ default:
9259+ BUG();
9260+ }
9261+}
9262+
9263+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9264+{
5527c038
JR
9265+ struct inode *inode;
9266+
dece6358 9267+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9268+ if (d_really_is_positive(d)) {
9269+ inode = d_inode(d);
1facf9fc 9270+ if (au_ftest_lock(flags, IW))
5527c038 9271+ do_ii_write_lock(inode, lsc);
1facf9fc 9272+ else if (au_ftest_lock(flags, IR))
5527c038 9273+ do_ii_read_lock(inode, lsc);
1facf9fc 9274+ }
9275+}
9276+
9277+void di_read_unlock(struct dentry *d, int flags)
9278+{
5527c038
JR
9279+ struct inode *inode;
9280+
9281+ if (d_really_is_positive(d)) {
9282+ inode = d_inode(d);
027c5e7a
AM
9283+ if (au_ftest_lock(flags, IW)) {
9284+ au_dbg_verify_dinode(d);
5527c038 9285+ ii_write_unlock(inode);
027c5e7a
AM
9286+ } else if (au_ftest_lock(flags, IR)) {
9287+ au_dbg_verify_dinode(d);
5527c038 9288+ ii_read_unlock(inode);
027c5e7a 9289+ }
1facf9fc 9290+ }
dece6358 9291+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9292+}
9293+
9294+void di_downgrade_lock(struct dentry *d, int flags)
9295+{
5527c038
JR
9296+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9297+ ii_downgrade_lock(d_inode(d));
dece6358 9298+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9299+}
9300+
9301+void di_write_lock(struct dentry *d, unsigned int lsc)
9302+{
dece6358 9303+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9304+ if (d_really_is_positive(d))
9305+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9306+}
9307+
9308+void di_write_unlock(struct dentry *d)
9309+{
027c5e7a 9310+ au_dbg_verify_dinode(d);
5527c038
JR
9311+ if (d_really_is_positive(d))
9312+ ii_write_unlock(d_inode(d));
dece6358 9313+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9314+}
9315+
9316+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9317+{
9318+ AuDebugOn(d1 == d2
5527c038 9319+ || d_inode(d1) == d_inode(d2)
1facf9fc 9320+ || d1->d_sb != d2->d_sb);
9321+
9322+ if (isdir && au_test_subdir(d1, d2)) {
9323+ di_write_lock_child(d1);
9324+ di_write_lock_child2(d2);
9325+ } else {
9326+ /* there should be no races */
9327+ di_write_lock_child(d2);
9328+ di_write_lock_child2(d1);
9329+ }
9330+}
9331+
9332+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9333+{
9334+ AuDebugOn(d1 == d2
5527c038 9335+ || d_inode(d1) == d_inode(d2)
1facf9fc 9336+ || d1->d_sb != d2->d_sb);
9337+
9338+ if (isdir && au_test_subdir(d1, d2)) {
9339+ di_write_lock_parent(d1);
9340+ di_write_lock_parent2(d2);
9341+ } else {
9342+ /* there should be no races */
9343+ di_write_lock_parent(d2);
9344+ di_write_lock_parent2(d1);
9345+ }
9346+}
9347+
9348+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9349+{
9350+ di_write_unlock(d1);
5527c038 9351+ if (d_inode(d1) == d_inode(d2))
dece6358 9352+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9353+ else
9354+ di_write_unlock(d2);
9355+}
9356+
9357+/* ---------------------------------------------------------------------- */
9358+
9359+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9360+{
9361+ struct dentry *d;
9362+
1308ab2a 9363+ DiMustAnyLock(dentry);
9364+
5afbbe0d 9365+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9366+ return NULL;
9367+ AuDebugOn(bindex < 0);
5afbbe0d 9368+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9369+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9370+ return d;
9371+}
9372+
2cbb1c4b
JR
9373+/*
9374+ * extended version of au_h_dptr().
38d290e6
JR
9375+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9376+ * error.
2cbb1c4b
JR
9377+ */
9378+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9379+{
9380+ struct dentry *h_dentry;
9381+ struct inode *inode, *h_inode;
9382+
5527c038 9383+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9384+
9385+ h_dentry = NULL;
5afbbe0d
AM
9386+ if (au_dbtop(dentry) <= bindex
9387+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9388+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9389+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9390+ dget(h_dentry);
9391+ goto out; /* success */
9392+ }
9393+
5527c038 9394+ inode = d_inode(dentry);
5afbbe0d
AM
9395+ AuDebugOn(bindex < au_ibtop(inode));
9396+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9397+ h_inode = au_h_iptr(inode, bindex);
9398+ h_dentry = d_find_alias(h_inode);
9399+ if (h_dentry) {
9400+ if (!IS_ERR(h_dentry)) {
38d290e6 9401+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9402+ goto out; /* success */
9403+ dput(h_dentry);
9404+ } else
9405+ goto out;
9406+ }
9407+
9408+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9409+ h_dentry = au_plink_lkup(inode, bindex);
9410+ AuDebugOn(!h_dentry);
9411+ if (!IS_ERR(h_dentry)) {
9412+ if (!au_d_hashed_positive(h_dentry))
9413+ goto out; /* success */
9414+ dput(h_dentry);
9415+ h_dentry = NULL;
9416+ }
9417+ }
9418+
9419+out:
9420+ AuDbgDentry(h_dentry);
9421+ return h_dentry;
9422+}
9423+
1facf9fc 9424+aufs_bindex_t au_dbtail(struct dentry *dentry)
9425+{
5afbbe0d 9426+ aufs_bindex_t bbot, bwh;
1facf9fc 9427+
5afbbe0d
AM
9428+ bbot = au_dbbot(dentry);
9429+ if (0 <= bbot) {
1facf9fc 9430+ bwh = au_dbwh(dentry);
9431+ if (!bwh)
9432+ return bwh;
5afbbe0d 9433+ if (0 < bwh && bwh < bbot)
1facf9fc 9434+ return bwh - 1;
9435+ }
5afbbe0d 9436+ return bbot;
1facf9fc 9437+}
9438+
9439+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9440+{
5afbbe0d 9441+ aufs_bindex_t bbot, bopq;
1facf9fc 9442+
5afbbe0d
AM
9443+ bbot = au_dbtail(dentry);
9444+ if (0 <= bbot) {
1facf9fc 9445+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9446+ if (0 <= bopq && bopq < bbot)
9447+ bbot = bopq;
1facf9fc 9448+ }
5afbbe0d 9449+ return bbot;
1facf9fc 9450+}
9451+
9452+/* ---------------------------------------------------------------------- */
9453+
9454+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9455+ struct dentry *h_dentry)
9456+{
5afbbe0d
AM
9457+ struct au_dinfo *dinfo;
9458+ struct au_hdentry *hd;
027c5e7a 9459+ struct au_branch *br;
1facf9fc 9460+
1308ab2a 9461+ DiMustWriteLock(dentry);
9462+
5afbbe0d
AM
9463+ dinfo = au_di(dentry);
9464+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9465+ au_hdput(hd);
1facf9fc 9466+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9467+ if (h_dentry) {
9468+ br = au_sbr(dentry->d_sb, bindex);
9469+ hd->hd_id = br->br_id;
9470+ }
9471+}
9472+
9473+int au_dbrange_test(struct dentry *dentry)
9474+{
9475+ int err;
5afbbe0d 9476+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9477+
9478+ err = 0;
5afbbe0d
AM
9479+ btop = au_dbtop(dentry);
9480+ bbot = au_dbbot(dentry);
9481+ if (btop >= 0)
9482+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9483+ else {
9484+ err = -EIO;
5afbbe0d 9485+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9486+ }
9487+
9488+ return err;
9489+}
9490+
9491+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9492+{
9493+ int err;
9494+
9495+ err = 0;
9496+ if (unlikely(au_digen(dentry) != sigen
5527c038 9497+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9498+ err = -EIO;
9499+
9500+ return err;
1facf9fc 9501+}
9502+
9503+void au_update_digen(struct dentry *dentry)
9504+{
9505+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9506+ /* smp_mb(); */ /* atomic_set */
9507+}
9508+
9509+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9510+{
9511+ struct au_dinfo *dinfo;
9512+ struct dentry *h_d;
4a4d8108 9513+ struct au_hdentry *hdp;
5afbbe0d 9514+ aufs_bindex_t bindex, bbot;
1facf9fc 9515+
1308ab2a 9516+ DiMustWriteLock(dentry);
9517+
1facf9fc 9518+ dinfo = au_di(dentry);
5afbbe0d 9519+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9520+ return;
9521+
9522+ if (do_put_zero) {
5afbbe0d
AM
9523+ bbot = dinfo->di_bbot;
9524+ bindex = dinfo->di_btop;
9525+ hdp = au_hdentry(dinfo, bindex);
9526+ for (; bindex <= bbot; bindex++, hdp++) {
9527+ h_d = hdp->hd_dentry;
5527c038 9528+ if (h_d && d_is_negative(h_d))
1facf9fc 9529+ au_set_h_dptr(dentry, bindex, NULL);
9530+ }
9531+ }
9532+
5afbbe0d
AM
9533+ dinfo->di_btop = 0;
9534+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9535+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9536+ if (hdp->hd_dentry)
1facf9fc 9537+ break;
5afbbe0d
AM
9538+ if (dinfo->di_btop > dinfo->di_bbot) {
9539+ dinfo->di_btop = -1;
9540+ dinfo->di_bbot = -1;
1facf9fc 9541+ return;
9542+ }
9543+
5afbbe0d
AM
9544+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9545+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9546+ if (hdp->hd_dentry)
1facf9fc 9547+ break;
5afbbe0d 9548+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9549+}
9550+
5afbbe0d 9551+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9552+{
5afbbe0d 9553+ aufs_bindex_t bindex, bbot;
1facf9fc 9554+ struct dentry *h_dentry;
9555+
5afbbe0d
AM
9556+ bbot = au_dbbot(dentry);
9557+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9558+ h_dentry = au_h_dptr(dentry, bindex);
9559+ if (!h_dentry)
9560+ continue;
5527c038 9561+ if (d_is_positive(h_dentry)) {
5afbbe0d 9562+ au_set_dbtop(dentry, bindex);
1facf9fc 9563+ return;
9564+ }
9565+ au_set_h_dptr(dentry, bindex, NULL);
9566+ }
9567+}
9568+
5afbbe0d 9569+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9570+{
5afbbe0d 9571+ aufs_bindex_t bindex, btop;
1facf9fc 9572+ struct dentry *h_dentry;
9573+
5afbbe0d
AM
9574+ btop = au_dbtop(dentry);
9575+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 9576+ h_dentry = au_h_dptr(dentry, bindex);
9577+ if (!h_dentry)
9578+ continue;
5527c038 9579+ if (d_is_positive(h_dentry)) {
5afbbe0d 9580+ au_set_dbbot(dentry, bindex);
1facf9fc 9581+ return;
9582+ }
9583+ au_set_h_dptr(dentry, bindex, NULL);
9584+ }
9585+}
9586+
9587+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9588+{
5afbbe0d 9589+ aufs_bindex_t bindex, bbot;
1facf9fc 9590+
5afbbe0d
AM
9591+ bbot = au_dbbot(dentry);
9592+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 9593+ if (au_h_dptr(dentry, bindex) == h_dentry)
9594+ return bindex;
9595+ return -1;
9596+}
7f207e10
AM
9597diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9598--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
9599+++ linux/fs/aufs/dir.c 2016-08-17 18:01:06.111888648 +0200
9600@@ -0,0 +1,762 @@
1facf9fc 9601+/*
8cdd5066 9602+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 9603+ *
9604+ * This program, aufs is free software; you can redistribute it and/or modify
9605+ * it under the terms of the GNU General Public License as published by
9606+ * the Free Software Foundation; either version 2 of the License, or
9607+ * (at your option) any later version.
dece6358
AM
9608+ *
9609+ * This program is distributed in the hope that it will be useful,
9610+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9611+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9612+ * GNU General Public License for more details.
9613+ *
9614+ * You should have received a copy of the GNU General Public License
523b37e3 9615+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9616+ */
9617+
9618+/*
9619+ * directory operations
9620+ */
9621+
9622+#include <linux/fs_stack.h>
9623+#include "aufs.h"
9624+
9625+void au_add_nlink(struct inode *dir, struct inode *h_dir)
9626+{
9dbd164d
AM
9627+ unsigned int nlink;
9628+
1facf9fc 9629+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9630+
9dbd164d
AM
9631+ nlink = dir->i_nlink;
9632+ nlink += h_dir->i_nlink - 2;
1facf9fc 9633+ if (h_dir->i_nlink < 2)
9dbd164d 9634+ nlink += 2;
f6b6e03d 9635+ smp_mb(); /* for i_nlink */
7eafdf33 9636+ /* 0 can happen in revaliding */
92d182d2 9637+ set_nlink(dir, nlink);
1facf9fc 9638+}
9639+
9640+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9641+{
9dbd164d
AM
9642+ unsigned int nlink;
9643+
1facf9fc 9644+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9645+
9dbd164d
AM
9646+ nlink = dir->i_nlink;
9647+ nlink -= h_dir->i_nlink - 2;
1facf9fc 9648+ if (h_dir->i_nlink < 2)
9dbd164d 9649+ nlink -= 2;
f6b6e03d 9650+ smp_mb(); /* for i_nlink */
92d182d2 9651+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 9652+ set_nlink(dir, nlink);
1facf9fc 9653+}
9654+
1308ab2a 9655+loff_t au_dir_size(struct file *file, struct dentry *dentry)
9656+{
9657+ loff_t sz;
5afbbe0d 9658+ aufs_bindex_t bindex, bbot;
1308ab2a 9659+ struct file *h_file;
9660+ struct dentry *h_dentry;
9661+
9662+ sz = 0;
9663+ if (file) {
2000de60 9664+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 9665+
5afbbe0d
AM
9666+ bbot = au_fbbot_dir(file);
9667+ for (bindex = au_fbtop(file);
9668+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 9669+ bindex++) {
4a4d8108 9670+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
9671+ if (h_file && file_inode(h_file))
9672+ sz += vfsub_f_size_read(h_file);
1308ab2a 9673+ }
9674+ } else {
9675+ AuDebugOn(!dentry);
2000de60 9676+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 9677+
5afbbe0d
AM
9678+ bbot = au_dbtaildir(dentry);
9679+ for (bindex = au_dbtop(dentry);
9680+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 9681+ bindex++) {
9682+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
9683+ if (h_dentry && d_is_positive(h_dentry))
9684+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 9685+ }
9686+ }
9687+ if (sz < KMALLOC_MAX_SIZE)
9688+ sz = roundup_pow_of_two(sz);
9689+ if (sz > KMALLOC_MAX_SIZE)
9690+ sz = KMALLOC_MAX_SIZE;
9691+ else if (sz < NAME_MAX) {
9692+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9693+ sz = AUFS_RDBLK_DEF;
9694+ }
9695+ return sz;
9696+}
9697+
b912730e
AM
9698+struct au_dir_ts_arg {
9699+ struct dentry *dentry;
9700+ aufs_bindex_t brid;
9701+};
9702+
9703+static void au_do_dir_ts(void *arg)
9704+{
9705+ struct au_dir_ts_arg *a = arg;
9706+ struct au_dtime dt;
9707+ struct path h_path;
9708+ struct inode *dir, *h_dir;
9709+ struct super_block *sb;
9710+ struct au_branch *br;
9711+ struct au_hinode *hdir;
9712+ int err;
5afbbe0d 9713+ aufs_bindex_t btop, bindex;
b912730e
AM
9714+
9715+ sb = a->dentry->d_sb;
5527c038 9716+ if (d_really_is_negative(a->dentry))
b912730e 9717+ goto out;
5527c038 9718+ /* no dir->i_mutex lock */
b95c5147
AM
9719+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9720+
5527c038 9721+ dir = d_inode(a->dentry);
5afbbe0d 9722+ btop = au_ibtop(dir);
b912730e 9723+ bindex = au_br_index(sb, a->brid);
5afbbe0d 9724+ if (bindex < btop)
b912730e
AM
9725+ goto out_unlock;
9726+
9727+ br = au_sbr(sb, bindex);
9728+ h_path.dentry = au_h_dptr(a->dentry, bindex);
9729+ if (!h_path.dentry)
9730+ goto out_unlock;
9731+ h_path.mnt = au_br_mnt(br);
9732+ au_dtime_store(&dt, a->dentry, &h_path);
9733+
5afbbe0d 9734+ br = au_sbr(sb, btop);
b912730e
AM
9735+ if (!au_br_writable(br->br_perm))
9736+ goto out_unlock;
5afbbe0d 9737+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
9738+ h_path.mnt = au_br_mnt(br);
9739+ err = vfsub_mnt_want_write(h_path.mnt);
9740+ if (err)
9741+ goto out_unlock;
5afbbe0d
AM
9742+ hdir = au_hi(dir, btop);
9743+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
9744+ h_dir = au_h_iptr(dir, btop);
b912730e
AM
9745+ if (h_dir->i_nlink
9746+ && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9747+ dt.dt_h_path = h_path;
9748+ au_dtime_revert(&dt);
9749+ }
5afbbe0d 9750+ au_hn_inode_unlock(hdir);
b912730e
AM
9751+ vfsub_mnt_drop_write(h_path.mnt);
9752+ au_cpup_attr_timesizes(dir);
9753+
9754+out_unlock:
9755+ aufs_read_unlock(a->dentry, AuLock_DW);
9756+out:
9757+ dput(a->dentry);
9758+ au_nwt_done(&au_sbi(sb)->si_nowait);
f0c0a007 9759+ au_delayed_kfree(arg);
b912730e
AM
9760+}
9761+
9762+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9763+{
9764+ int perm, wkq_err;
5afbbe0d 9765+ aufs_bindex_t btop;
b912730e
AM
9766+ struct au_dir_ts_arg *arg;
9767+ struct dentry *dentry;
9768+ struct super_block *sb;
9769+
9770+ IMustLock(dir);
9771+
9772+ dentry = d_find_any_alias(dir);
9773+ AuDebugOn(!dentry);
9774+ sb = dentry->d_sb;
5afbbe0d
AM
9775+ btop = au_ibtop(dir);
9776+ if (btop == bindex) {
b912730e
AM
9777+ au_cpup_attr_timesizes(dir);
9778+ goto out;
9779+ }
9780+
5afbbe0d 9781+ perm = au_sbr_perm(sb, btop);
b912730e
AM
9782+ if (!au_br_writable(perm))
9783+ goto out;
9784+
9785+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
9786+ if (!arg)
9787+ goto out;
9788+
9789+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9790+ arg->brid = au_sbr_id(sb, bindex);
9791+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9792+ if (unlikely(wkq_err)) {
9793+ pr_err("wkq %d\n", wkq_err);
9794+ dput(dentry);
f0c0a007 9795+ au_delayed_kfree(arg);
b912730e
AM
9796+ }
9797+
9798+out:
9799+ dput(dentry);
9800+}
9801+
1facf9fc 9802+/* ---------------------------------------------------------------------- */
9803+
9804+static int reopen_dir(struct file *file)
9805+{
9806+ int err;
9807+ unsigned int flags;
5afbbe0d 9808+ aufs_bindex_t bindex, btail, btop;
1facf9fc 9809+ struct dentry *dentry, *h_dentry;
9810+ struct file *h_file;
9811+
9812+ /* open all lower dirs */
2000de60 9813+ dentry = file->f_path.dentry;
5afbbe0d
AM
9814+ btop = au_dbtop(dentry);
9815+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 9816+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 9817+ au_set_fbtop(file, btop);
1facf9fc 9818+
9819+ btail = au_dbtaildir(dentry);
5afbbe0d 9820+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 9821+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 9822+ au_set_fbbot_dir(file, btail);
1facf9fc 9823+
4a4d8108 9824+ flags = vfsub_file_flags(file);
5afbbe0d 9825+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9826+ h_dentry = au_h_dptr(dentry, bindex);
9827+ if (!h_dentry)
9828+ continue;
4a4d8108 9829+ h_file = au_hf_dir(file, bindex);
1facf9fc 9830+ if (h_file)
9831+ continue;
9832+
392086de 9833+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 9834+ err = PTR_ERR(h_file);
9835+ if (IS_ERR(h_file))
9836+ goto out; /* close all? */
9837+ au_set_h_fptr(file, bindex, h_file);
9838+ }
9839+ au_update_figen(file);
9840+ /* todo: necessary? */
9841+ /* file->f_ra = h_file->f_ra; */
9842+ err = 0;
9843+
4f0767ce 9844+out:
1facf9fc 9845+ return err;
9846+}
9847+
b912730e 9848+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 9849+{
9850+ int err;
9851+ aufs_bindex_t bindex, btail;
9852+ struct dentry *dentry, *h_dentry;
8cdd5066 9853+ struct vfsmount *mnt;
1facf9fc 9854+
1308ab2a 9855+ FiMustWriteLock(file);
b912730e 9856+ AuDebugOn(h_file);
1308ab2a 9857+
523b37e3 9858+ err = 0;
8cdd5066 9859+ mnt = file->f_path.mnt;
2000de60 9860+ dentry = file->f_path.dentry;
5527c038 9861+ file->f_version = d_inode(dentry)->i_version;
5afbbe0d
AM
9862+ bindex = au_dbtop(dentry);
9863+ au_set_fbtop(file, bindex);
1facf9fc 9864+ btail = au_dbtaildir(dentry);
5afbbe0d 9865+ au_set_fbbot_dir(file, btail);
1facf9fc 9866+ for (; !err && bindex <= btail; bindex++) {
9867+ h_dentry = au_h_dptr(dentry, bindex);
9868+ if (!h_dentry)
9869+ continue;
9870+
8cdd5066
JR
9871+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
9872+ if (unlikely(err))
9873+ break;
392086de 9874+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 9875+ if (IS_ERR(h_file)) {
9876+ err = PTR_ERR(h_file);
9877+ break;
9878+ }
9879+ au_set_h_fptr(file, bindex, h_file);
9880+ }
9881+ au_update_figen(file);
9882+ /* todo: necessary? */
9883+ /* file->f_ra = h_file->f_ra; */
9884+ if (!err)
9885+ return 0; /* success */
9886+
9887+ /* close all */
5afbbe0d 9888+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 9889+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
9890+ au_set_fbtop(file, -1);
9891+ au_set_fbbot_dir(file, -1);
4a4d8108 9892+
1facf9fc 9893+ return err;
9894+}
9895+
9896+static int aufs_open_dir(struct inode *inode __maybe_unused,
9897+ struct file *file)
9898+{
4a4d8108
AM
9899+ int err;
9900+ struct super_block *sb;
9901+ struct au_fidir *fidir;
9902+
9903+ err = -ENOMEM;
2000de60 9904+ sb = file->f_path.dentry->d_sb;
4a4d8108 9905+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 9906+ fidir = au_fidir_alloc(sb);
4a4d8108 9907+ if (fidir) {
b912730e
AM
9908+ struct au_do_open_args args = {
9909+ .open = do_open_dir,
9910+ .fidir = fidir
9911+ };
9912+ err = au_do_open(file, &args);
4a4d8108 9913+ if (unlikely(err))
f0c0a007 9914+ au_delayed_kfree(fidir);
4a4d8108
AM
9915+ }
9916+ si_read_unlock(sb);
9917+ return err;
1facf9fc 9918+}
9919+
9920+static int aufs_release_dir(struct inode *inode __maybe_unused,
9921+ struct file *file)
9922+{
9923+ struct au_vdir *vdir_cache;
4a4d8108
AM
9924+ struct au_finfo *finfo;
9925+ struct au_fidir *fidir;
f0c0a007 9926+ struct au_hfile *hf;
5afbbe0d 9927+ aufs_bindex_t bindex, bbot;
f0c0a007 9928+ int execed, delayed;
1facf9fc 9929+
f0c0a007 9930+ delayed = (current->flags & PF_KTHREAD) || in_interrupt();
4a4d8108
AM
9931+ finfo = au_fi(file);
9932+ fidir = finfo->fi_hdir;
9933+ if (fidir) {
076b876e 9934+ au_sphl_del(&finfo->fi_hlist,
2000de60 9935+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
9936+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9937+ if (vdir_cache)
f0c0a007 9938+ au_vdir_free(vdir_cache, delayed);
4a4d8108
AM
9939+
9940+ bindex = finfo->fi_btop;
9941+ if (bindex >= 0) {
f0c0a007
AM
9942+ execed = vfsub_file_execed(file);
9943+ hf = fidir->fd_hfile + bindex;
4a4d8108
AM
9944+ /*
9945+ * calls fput() instead of filp_close(),
9946+ * since no dnotify or lock for the lower file.
9947+ */
5afbbe0d 9948+ bbot = fidir->fd_bbot;
f0c0a007
AM
9949+ for (; bindex <= bbot; bindex++, hf++)
9950+ if (hf->hf_file)
9951+ au_hfput(hf, execed);
4a4d8108 9952+ }
f0c0a007 9953+ au_delayed_kfree(fidir);
4a4d8108 9954+ finfo->fi_hdir = NULL;
1facf9fc 9955+ }
f0c0a007 9956+ au_finfo_fin(file, delayed);
1facf9fc 9957+ return 0;
9958+}
9959+
9960+/* ---------------------------------------------------------------------- */
9961+
4a4d8108
AM
9962+static int au_do_flush_dir(struct file *file, fl_owner_t id)
9963+{
9964+ int err;
5afbbe0d 9965+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
9966+ struct file *h_file;
9967+
9968+ err = 0;
5afbbe0d
AM
9969+ bbot = au_fbbot_dir(file);
9970+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
9971+ h_file = au_hf_dir(file, bindex);
9972+ if (h_file)
9973+ err = vfsub_flush(h_file, id);
9974+ }
9975+ return err;
9976+}
9977+
9978+static int aufs_flush_dir(struct file *file, fl_owner_t id)
9979+{
9980+ return au_do_flush(file, id, au_do_flush_dir);
9981+}
9982+
9983+/* ---------------------------------------------------------------------- */
9984+
1facf9fc 9985+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9986+{
9987+ int err;
5afbbe0d 9988+ aufs_bindex_t bbot, bindex;
1facf9fc 9989+ struct inode *inode;
9990+ struct super_block *sb;
9991+
9992+ err = 0;
9993+ sb = dentry->d_sb;
5527c038 9994+ inode = d_inode(dentry);
1facf9fc 9995+ IMustLock(inode);
5afbbe0d
AM
9996+ bbot = au_dbbot(dentry);
9997+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 9998+ struct path h_path;
1facf9fc 9999+
10000+ if (au_test_ro(sb, bindex, inode))
10001+ continue;
10002+ h_path.dentry = au_h_dptr(dentry, bindex);
10003+ if (!h_path.dentry)
10004+ continue;
1facf9fc 10005+
1facf9fc 10006+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 10007+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 10008+ }
10009+
10010+ return err;
10011+}
10012+
10013+static int au_do_fsync_dir(struct file *file, int datasync)
10014+{
10015+ int err;
5afbbe0d 10016+ aufs_bindex_t bbot, bindex;
1facf9fc 10017+ struct file *h_file;
10018+ struct super_block *sb;
10019+ struct inode *inode;
1facf9fc 10020+
10021+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
10022+ if (unlikely(err))
10023+ goto out;
10024+
c06a8ce3 10025+ inode = file_inode(file);
b912730e 10026+ sb = inode->i_sb;
5afbbe0d
AM
10027+ bbot = au_fbbot_dir(file);
10028+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10029+ h_file = au_hf_dir(file, bindex);
1facf9fc 10030+ if (!h_file || au_test_ro(sb, bindex, inode))
10031+ continue;
10032+
53392da6 10033+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10034+ }
10035+
4f0767ce 10036+out:
1facf9fc 10037+ return err;
10038+}
10039+
10040+/*
10041+ * @file may be NULL
10042+ */
1e00d052
AM
10043+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10044+ int datasync)
1facf9fc 10045+{
10046+ int err;
b752ccd1 10047+ struct dentry *dentry;
5527c038 10048+ struct inode *inode;
1facf9fc 10049+ struct super_block *sb;
1facf9fc 10050+
10051+ err = 0;
2000de60 10052+ dentry = file->f_path.dentry;
5527c038 10053+ inode = d_inode(dentry);
febd17d6 10054+ inode_lock(inode);
1facf9fc 10055+ sb = dentry->d_sb;
10056+ si_noflush_read_lock(sb);
10057+ if (file)
10058+ err = au_do_fsync_dir(file, datasync);
10059+ else {
10060+ di_write_lock_child(dentry);
10061+ err = au_do_fsync_dir_no_file(dentry, datasync);
10062+ }
5527c038 10063+ au_cpup_attr_timesizes(inode);
1facf9fc 10064+ di_write_unlock(dentry);
10065+ if (file)
10066+ fi_write_unlock(file);
10067+
10068+ si_read_unlock(sb);
febd17d6 10069+ inode_unlock(inode);
1facf9fc 10070+ return err;
10071+}
10072+
10073+/* ---------------------------------------------------------------------- */
10074+
5afbbe0d 10075+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10076+{
10077+ int err;
10078+ struct dentry *dentry;
9dbd164d 10079+ struct inode *inode, *h_inode;
1facf9fc 10080+ struct super_block *sb;
10081+
523b37e3 10082+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10083+
2000de60 10084+ dentry = file->f_path.dentry;
5527c038 10085+ inode = d_inode(dentry);
1facf9fc 10086+ IMustLock(inode);
10087+
10088+ sb = dentry->d_sb;
10089+ si_read_lock(sb, AuLock_FLUSH);
10090+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
10091+ if (unlikely(err))
10092+ goto out;
027c5e7a
AM
10093+ err = au_alive_dir(dentry);
10094+ if (!err)
10095+ err = au_vdir_init(file);
1facf9fc 10096+ di_downgrade_lock(dentry, AuLock_IR);
10097+ if (unlikely(err))
10098+ goto out_unlock;
10099+
5afbbe0d 10100+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10101+ if (!au_test_nfsd()) {
392086de 10102+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10103+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10104+ } else {
10105+ /*
10106+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10107+ * encode_fh() and others.
10108+ */
9dbd164d 10109+ atomic_inc(&h_inode->i_count);
1facf9fc 10110+ di_read_unlock(dentry, AuLock_IR);
10111+ si_read_unlock(sb);
392086de 10112+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10113+ fsstack_copy_attr_atime(inode, h_inode);
10114+ fi_write_unlock(file);
9dbd164d 10115+ iput(h_inode);
1facf9fc 10116+
10117+ AuTraceErr(err);
10118+ return err;
10119+ }
10120+
4f0767ce 10121+out_unlock:
1facf9fc 10122+ di_read_unlock(dentry, AuLock_IR);
10123+ fi_write_unlock(file);
4f0767ce 10124+out:
1facf9fc 10125+ si_read_unlock(sb);
10126+ return err;
10127+}
10128+
10129+/* ---------------------------------------------------------------------- */
10130+
10131+#define AuTestEmpty_WHONLY 1
dece6358
AM
10132+#define AuTestEmpty_CALLED (1 << 1)
10133+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10134+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10135+#define au_fset_testempty(flags, name) \
10136+ do { (flags) |= AuTestEmpty_##name; } while (0)
10137+#define au_fclr_testempty(flags, name) \
10138+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10139+
dece6358
AM
10140+#ifndef CONFIG_AUFS_SHWH
10141+#undef AuTestEmpty_SHWH
10142+#define AuTestEmpty_SHWH 0
10143+#endif
10144+
1facf9fc 10145+struct test_empty_arg {
392086de 10146+ struct dir_context ctx;
1308ab2a 10147+ struct au_nhash *whlist;
1facf9fc 10148+ unsigned int flags;
10149+ int err;
10150+ aufs_bindex_t bindex;
10151+};
10152+
392086de
AM
10153+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10154+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10155+ unsigned int d_type)
1facf9fc 10156+{
392086de
AM
10157+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10158+ ctx);
1facf9fc 10159+ char *name = (void *)__name;
10160+
10161+ arg->err = 0;
10162+ au_fset_testempty(arg->flags, CALLED);
10163+ /* smp_mb(); */
10164+ if (name[0] == '.'
10165+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10166+ goto out; /* success */
10167+
10168+ if (namelen <= AUFS_WH_PFX_LEN
10169+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10170+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10171+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10172+ arg->err = -ENOTEMPTY;
10173+ goto out;
10174+ }
10175+
10176+ name += AUFS_WH_PFX_LEN;
10177+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10178+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10179+ arg->err = au_nhash_append_wh
1308ab2a 10180+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10181+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10182+
4f0767ce 10183+out:
1facf9fc 10184+ /* smp_mb(); */
10185+ AuTraceErr(arg->err);
10186+ return arg->err;
10187+}
10188+
10189+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10190+{
10191+ int err;
10192+ struct file *h_file;
10193+
10194+ h_file = au_h_open(dentry, arg->bindex,
10195+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10196+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10197+ err = PTR_ERR(h_file);
10198+ if (IS_ERR(h_file))
10199+ goto out;
10200+
10201+ err = 0;
10202+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10203+ && !file_inode(h_file)->i_nlink)
1facf9fc 10204+ goto out_put;
10205+
10206+ do {
10207+ arg->err = 0;
10208+ au_fclr_testempty(arg->flags, CALLED);
10209+ /* smp_mb(); */
392086de 10210+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10211+ if (err >= 0)
10212+ err = arg->err;
10213+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10214+
4f0767ce 10215+out_put:
1facf9fc 10216+ fput(h_file);
10217+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10218+out:
1facf9fc 10219+ return err;
10220+}
10221+
10222+struct do_test_empty_args {
10223+ int *errp;
10224+ struct dentry *dentry;
10225+ struct test_empty_arg *arg;
10226+};
10227+
10228+static void call_do_test_empty(void *args)
10229+{
10230+ struct do_test_empty_args *a = args;
10231+ *a->errp = do_test_empty(a->dentry, a->arg);
10232+}
10233+
10234+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10235+{
10236+ int err, wkq_err;
10237+ struct dentry *h_dentry;
10238+ struct inode *h_inode;
10239+
10240+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10241+ h_inode = d_inode(h_dentry);
53392da6 10242+ /* todo: i_mode changes anytime? */
febd17d6 10243+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10244+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
febd17d6 10245+ inode_unlock(h_inode);
1facf9fc 10246+ if (!err)
10247+ err = do_test_empty(dentry, arg);
10248+ else {
10249+ struct do_test_empty_args args = {
10250+ .errp = &err,
10251+ .dentry = dentry,
10252+ .arg = arg
10253+ };
10254+ unsigned int flags = arg->flags;
10255+
10256+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10257+ if (unlikely(wkq_err))
10258+ err = wkq_err;
10259+ arg->flags = flags;
10260+ }
10261+
10262+ return err;
10263+}
10264+
10265+int au_test_empty_lower(struct dentry *dentry)
10266+{
10267+ int err;
1308ab2a 10268+ unsigned int rdhash;
5afbbe0d 10269+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10270+ struct au_nhash whlist;
392086de
AM
10271+ struct test_empty_arg arg = {
10272+ .ctx = {
2000de60 10273+ .actor = test_empty_cb
392086de
AM
10274+ }
10275+ };
076b876e 10276+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10277+
dece6358
AM
10278+ SiMustAnyLock(dentry->d_sb);
10279+
1308ab2a 10280+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10281+ if (!rdhash)
10282+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10283+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10284+ if (unlikely(err))
1facf9fc 10285+ goto out;
10286+
1facf9fc 10287+ arg.flags = 0;
1308ab2a 10288+ arg.whlist = &whlist;
5afbbe0d 10289+ btop = au_dbtop(dentry);
dece6358
AM
10290+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10291+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10292+ test_empty = do_test_empty;
10293+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10294+ test_empty = sio_test_empty;
5afbbe0d 10295+ arg.bindex = btop;
076b876e 10296+ err = test_empty(dentry, &arg);
1facf9fc 10297+ if (unlikely(err))
10298+ goto out_whlist;
10299+
10300+ au_fset_testempty(arg.flags, WHONLY);
10301+ btail = au_dbtaildir(dentry);
5afbbe0d 10302+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10303+ struct dentry *h_dentry;
10304+
10305+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10306+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10307+ arg.bindex = bindex;
076b876e 10308+ err = test_empty(dentry, &arg);
1facf9fc 10309+ }
10310+ }
10311+
4f0767ce 10312+out_whlist:
1308ab2a 10313+ au_nhash_wh_free(&whlist);
4f0767ce 10314+out:
1facf9fc 10315+ return err;
10316+}
10317+
10318+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10319+{
10320+ int err;
392086de
AM
10321+ struct test_empty_arg arg = {
10322+ .ctx = {
2000de60 10323+ .actor = test_empty_cb
392086de
AM
10324+ }
10325+ };
1facf9fc 10326+ aufs_bindex_t bindex, btail;
10327+
10328+ err = 0;
1308ab2a 10329+ arg.whlist = whlist;
1facf9fc 10330+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10331+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10332+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10333+ btail = au_dbtaildir(dentry);
5afbbe0d 10334+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10335+ struct dentry *h_dentry;
10336+
10337+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10338+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10339+ arg.bindex = bindex;
10340+ err = sio_test_empty(dentry, &arg);
10341+ }
10342+ }
10343+
10344+ return err;
10345+}
10346+
10347+/* ---------------------------------------------------------------------- */
10348+
10349+const struct file_operations aufs_dir_fop = {
4a4d8108 10350+ .owner = THIS_MODULE,
027c5e7a 10351+ .llseek = default_llseek,
1facf9fc 10352+ .read = generic_read_dir,
5afbbe0d 10353+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10354+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10355+#ifdef CONFIG_COMPAT
10356+ .compat_ioctl = aufs_compat_ioctl_dir,
10357+#endif
1facf9fc 10358+ .open = aufs_open_dir,
10359+ .release = aufs_release_dir,
4a4d8108 10360+ .flush = aufs_flush_dir,
1facf9fc 10361+ .fsync = aufs_fsync_dir
10362+};
7f207e10
AM
10363diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10364--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
10365+++ linux/fs/aufs/dir.h 2016-08-17 18:01:06.111888648 +0200
10366@@ -0,0 +1,137 @@
1facf9fc 10367+/*
8cdd5066 10368+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 10369+ *
10370+ * This program, aufs is free software; you can redistribute it and/or modify
10371+ * it under the terms of the GNU General Public License as published by
10372+ * the Free Software Foundation; either version 2 of the License, or
10373+ * (at your option) any later version.
dece6358
AM
10374+ *
10375+ * This program is distributed in the hope that it will be useful,
10376+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10377+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10378+ * GNU General Public License for more details.
10379+ *
10380+ * You should have received a copy of the GNU General Public License
523b37e3 10381+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10382+ */
10383+
10384+/*
10385+ * directory operations
10386+ */
10387+
10388+#ifndef __AUFS_DIR_H__
10389+#define __AUFS_DIR_H__
10390+
10391+#ifdef __KERNEL__
10392+
10393+#include <linux/fs.h>
1facf9fc 10394+
10395+/* ---------------------------------------------------------------------- */
10396+
10397+/* need to be faster and smaller */
10398+
10399+struct au_nhash {
dece6358
AM
10400+ unsigned int nh_num;
10401+ struct hlist_head *nh_head;
1facf9fc 10402+};
10403+
10404+struct au_vdir_destr {
10405+ unsigned char len;
10406+ unsigned char name[0];
10407+} __packed;
10408+
10409+struct au_vdir_dehstr {
10410+ struct hlist_node hash;
f0c0a007
AM
10411+ union {
10412+ struct au_vdir_destr *str;
10413+ struct llist_node lnode; /* delayed free */
10414+ };
4a4d8108 10415+} ____cacheline_aligned_in_smp;
1facf9fc 10416+
10417+struct au_vdir_de {
10418+ ino_t de_ino;
10419+ unsigned char de_type;
10420+ /* caution: packed */
10421+ struct au_vdir_destr de_str;
10422+} __packed;
10423+
10424+struct au_vdir_wh {
10425+ struct hlist_node wh_hash;
dece6358
AM
10426+#ifdef CONFIG_AUFS_SHWH
10427+ ino_t wh_ino;
1facf9fc 10428+ aufs_bindex_t wh_bindex;
dece6358
AM
10429+ unsigned char wh_type;
10430+#else
10431+ aufs_bindex_t wh_bindex;
10432+#endif
10433+ /* caution: packed */
1facf9fc 10434+ struct au_vdir_destr wh_str;
10435+} __packed;
10436+
10437+union au_vdir_deblk_p {
10438+ unsigned char *deblk;
10439+ struct au_vdir_de *de;
10440+};
10441+
10442+struct au_vdir {
10443+ unsigned char **vd_deblk;
10444+ unsigned long vd_nblk;
1facf9fc 10445+ struct {
10446+ unsigned long ul;
10447+ union au_vdir_deblk_p p;
10448+ } vd_last;
10449+
10450+ unsigned long vd_version;
dece6358 10451+ unsigned int vd_deblk_sz;
f0c0a007
AM
10452+ union {
10453+ unsigned long vd_jiffy;
10454+ struct llist_node vd_lnode; /* delayed free */
10455+ };
4a4d8108 10456+} ____cacheline_aligned_in_smp;
1facf9fc 10457+
10458+/* ---------------------------------------------------------------------- */
10459+
10460+/* dir.c */
10461+extern const struct file_operations aufs_dir_fop;
10462+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10463+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10464+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10465+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10466+int au_test_empty_lower(struct dentry *dentry);
10467+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10468+
10469+/* vdir.c */
1308ab2a 10470+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10471+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10472+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10473+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10474+ int limit);
dece6358
AM
10475+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10476+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10477+ unsigned int d_type, aufs_bindex_t bindex,
10478+ unsigned char shwh);
f0c0a007 10479+void au_vdir_free(struct au_vdir *vdir, int atonce);
1facf9fc 10480+int au_vdir_init(struct file *file);
392086de 10481+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10482+
10483+/* ioctl.c */
10484+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10485+
1308ab2a 10486+#ifdef CONFIG_AUFS_RDU
10487+/* rdu.c */
10488+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10489+#ifdef CONFIG_COMPAT
10490+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10491+ unsigned long arg);
10492+#endif
1308ab2a 10493+#else
c1595e42
JR
10494+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10495+ unsigned int cmd, unsigned long arg)
b752ccd1 10496+#ifdef CONFIG_COMPAT
c1595e42
JR
10497+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10498+ unsigned int cmd, unsigned long arg)
b752ccd1 10499+#endif
1308ab2a 10500+#endif
10501+
1facf9fc 10502+#endif /* __KERNEL__ */
10503+#endif /* __AUFS_DIR_H__ */
7f207e10
AM
10504diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10505--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 10506+++ linux/fs/aufs/dynop.c 2016-08-17 18:01:21.295617591 +0200
7e9cd9fe 10507@@ -0,0 +1,369 @@
1facf9fc 10508+/*
8cdd5066 10509+ * Copyright (C) 2010-2016 Junjiro R. Okajima
1facf9fc 10510+ *
10511+ * This program, aufs is free software; you can redistribute it and/or modify
10512+ * it under the terms of the GNU General Public License as published by
10513+ * the Free Software Foundation; either version 2 of the License, or
10514+ * (at your option) any later version.
dece6358
AM
10515+ *
10516+ * This program is distributed in the hope that it will be useful,
10517+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10518+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10519+ * GNU General Public License for more details.
10520+ *
10521+ * You should have received a copy of the GNU General Public License
523b37e3 10522+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10523+ */
10524+
10525+/*
4a4d8108 10526+ * dynamically customizable operations for regular files
1facf9fc 10527+ */
10528+
1facf9fc 10529+#include "aufs.h"
10530+
4a4d8108 10531+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 10532+
4a4d8108
AM
10533+/*
10534+ * How large will these lists be?
10535+ * Usually just a few elements, 20-30 at most for each, I guess.
10536+ */
f0c0a007 10537+static struct au_sphlhead dynop[AuDyLast];
4a4d8108 10538+
f0c0a007 10539+static struct au_dykey *dy_gfind_get(struct au_sphlhead *sphl, const void *h_op)
1facf9fc 10540+{
4a4d8108 10541+ struct au_dykey *key, *tmp;
f0c0a007 10542+ struct hlist_head *head;
1facf9fc 10543+
4a4d8108 10544+ key = NULL;
f0c0a007 10545+ head = &sphl->head;
4a4d8108 10546+ rcu_read_lock();
f0c0a007 10547+ hlist_for_each_entry_rcu(tmp, head, dk_hnode)
4a4d8108
AM
10548+ if (tmp->dk_op.dy_hop == h_op) {
10549+ key = tmp;
10550+ kref_get(&key->dk_kref);
10551+ break;
10552+ }
10553+ rcu_read_unlock();
10554+
10555+ return key;
1facf9fc 10556+}
10557+
4a4d8108 10558+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 10559+{
4a4d8108
AM
10560+ struct au_dykey **k, *found;
10561+ const void *h_op = key->dk_op.dy_hop;
10562+ int i;
1facf9fc 10563+
4a4d8108
AM
10564+ found = NULL;
10565+ k = br->br_dykey;
10566+ for (i = 0; i < AuBrDynOp; i++)
10567+ if (k[i]) {
10568+ if (k[i]->dk_op.dy_hop == h_op) {
10569+ found = k[i];
10570+ break;
10571+ }
10572+ } else
10573+ break;
10574+ if (!found) {
10575+ spin_lock(&br->br_dykey_lock);
10576+ for (; i < AuBrDynOp; i++)
10577+ if (k[i]) {
10578+ if (k[i]->dk_op.dy_hop == h_op) {
10579+ found = k[i];
10580+ break;
10581+ }
10582+ } else {
10583+ k[i] = key;
10584+ break;
10585+ }
10586+ spin_unlock(&br->br_dykey_lock);
10587+ BUG_ON(i == AuBrDynOp); /* expand the array */
10588+ }
10589+
10590+ return found;
1facf9fc 10591+}
10592+
4a4d8108 10593+/* kref_get() if @key is already added */
f0c0a007 10594+static struct au_dykey *dy_gadd(struct au_sphlhead *sphl, struct au_dykey *key)
4a4d8108
AM
10595+{
10596+ struct au_dykey *tmp, *found;
f0c0a007 10597+ struct hlist_head *head;
4a4d8108 10598+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 10599+
4a4d8108 10600+ found = NULL;
f0c0a007
AM
10601+ head = &sphl->head;
10602+ spin_lock(&sphl->spin);
10603+ hlist_for_each_entry(tmp, head, dk_hnode)
4a4d8108
AM
10604+ if (tmp->dk_op.dy_hop == h_op) {
10605+ kref_get(&tmp->dk_kref);
10606+ found = tmp;
10607+ break;
10608+ }
10609+ if (!found)
f0c0a007
AM
10610+ hlist_add_head_rcu(&key->dk_hnode, head);
10611+ spin_unlock(&sphl->spin);
1facf9fc 10612+
4a4d8108
AM
10613+ if (!found)
10614+ DyPrSym(key);
10615+ return found;
10616+}
10617+
10618+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 10619+{
4a4d8108
AM
10620+ struct au_dykey *key;
10621+
10622+ key = container_of(rcu, struct au_dykey, dk_rcu);
10623+ DyPrSym(key);
f0c0a007 10624+ kfree(key); /* not delayed */
1facf9fc 10625+}
10626+
4a4d8108
AM
10627+static void dy_free(struct kref *kref)
10628+{
10629+ struct au_dykey *key;
f0c0a007 10630+ struct au_sphlhead *sphl;
1facf9fc 10631+
4a4d8108 10632+ key = container_of(kref, struct au_dykey, dk_kref);
f0c0a007
AM
10633+ sphl = dynop + key->dk_op.dy_type;
10634+ au_sphl_del_rcu(&key->dk_hnode, sphl);
4a4d8108
AM
10635+ call_rcu(&key->dk_rcu, dy_free_rcu);
10636+}
10637+
10638+void au_dy_put(struct au_dykey *key)
1facf9fc 10639+{
4a4d8108
AM
10640+ kref_put(&key->dk_kref, dy_free);
10641+}
1facf9fc 10642+
4a4d8108
AM
10643+/* ---------------------------------------------------------------------- */
10644+
10645+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10646+
10647+#ifdef CONFIG_AUFS_DEBUG
10648+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 10649+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
10650+#else
10651+#define DyDbgDeclare(cnt) do {} while (0)
10652+#define DyDbgInc(cnt) do {} while (0)
10653+#endif
10654+
10655+#define DySet(func, dst, src, h_op, h_sb) do { \
10656+ DyDbgInc(cnt); \
10657+ if (h_op->func) { \
10658+ if (src.func) \
10659+ dst.func = src.func; \
10660+ else \
10661+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
10662+ } \
10663+} while (0)
10664+
10665+#define DySetForce(func, dst, src) do { \
10666+ AuDebugOn(!src.func); \
10667+ DyDbgInc(cnt); \
10668+ dst.func = src.func; \
10669+} while (0)
10670+
10671+#define DySetAop(func) \
10672+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10673+#define DySetAopForce(func) \
10674+ DySetForce(func, dyaop->da_op, aufs_aop)
10675+
10676+static void dy_aop(struct au_dykey *key, const void *h_op,
10677+ struct super_block *h_sb __maybe_unused)
10678+{
10679+ struct au_dyaop *dyaop = (void *)key;
10680+ const struct address_space_operations *h_aop = h_op;
10681+ DyDbgDeclare(cnt);
10682+
10683+ AuDbg("%s\n", au_sbtype(h_sb));
10684+
10685+ DySetAop(writepage);
10686+ DySetAopForce(readpage); /* force */
4a4d8108
AM
10687+ DySetAop(writepages);
10688+ DySetAop(set_page_dirty);
10689+ DySetAop(readpages);
10690+ DySetAop(write_begin);
10691+ DySetAop(write_end);
10692+ DySetAop(bmap);
10693+ DySetAop(invalidatepage);
10694+ DySetAop(releasepage);
027c5e7a 10695+ DySetAop(freepage);
7e9cd9fe 10696+ /* this one will be changed according to an aufs mount option */
4a4d8108 10697+ DySetAop(direct_IO);
4a4d8108
AM
10698+ DySetAop(migratepage);
10699+ DySetAop(launder_page);
10700+ DySetAop(is_partially_uptodate);
392086de 10701+ DySetAop(is_dirty_writeback);
4a4d8108 10702+ DySetAop(error_remove_page);
b4510431
AM
10703+ DySetAop(swap_activate);
10704+ DySetAop(swap_deactivate);
4a4d8108
AM
10705+
10706+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
10707+}
10708+
4a4d8108
AM
10709+/* ---------------------------------------------------------------------- */
10710+
10711+static void dy_bug(struct kref *kref)
10712+{
10713+ BUG();
10714+}
10715+
10716+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10717+{
10718+ struct au_dykey *key, *old;
f0c0a007 10719+ struct au_sphlhead *sphl;
b752ccd1 10720+ struct op {
4a4d8108 10721+ unsigned int sz;
b752ccd1
AM
10722+ void (*set)(struct au_dykey *key, const void *h_op,
10723+ struct super_block *h_sb __maybe_unused);
10724+ };
10725+ static const struct op a[] = {
4a4d8108
AM
10726+ [AuDy_AOP] = {
10727+ .sz = sizeof(struct au_dyaop),
b752ccd1 10728+ .set = dy_aop
4a4d8108 10729+ }
b752ccd1
AM
10730+ };
10731+ const struct op *p;
4a4d8108 10732+
f0c0a007
AM
10733+ sphl = dynop + op->dy_type;
10734+ key = dy_gfind_get(sphl, op->dy_hop);
4a4d8108
AM
10735+ if (key)
10736+ goto out_add; /* success */
10737+
10738+ p = a + op->dy_type;
10739+ key = kzalloc(p->sz, GFP_NOFS);
10740+ if (unlikely(!key)) {
10741+ key = ERR_PTR(-ENOMEM);
10742+ goto out;
10743+ }
10744+
10745+ key->dk_op.dy_hop = op->dy_hop;
10746+ kref_init(&key->dk_kref);
86dc4139 10747+ p->set(key, op->dy_hop, au_br_sb(br));
f0c0a007 10748+ old = dy_gadd(sphl, key);
4a4d8108 10749+ if (old) {
f0c0a007 10750+ au_delayed_kfree(key);
4a4d8108
AM
10751+ key = old;
10752+ }
10753+
10754+out_add:
10755+ old = dy_bradd(br, key);
10756+ if (old)
10757+ /* its ref-count should never be zero here */
10758+ kref_put(&key->dk_kref, dy_bug);
10759+out:
10760+ return key;
10761+}
10762+
10763+/* ---------------------------------------------------------------------- */
10764+/*
10765+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 10766+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
10767+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10768+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10769+ * See the aufs manual in detail.
4a4d8108
AM
10770+ */
10771+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10772+{
7e9cd9fe 10773+ if (!do_dx)
4a4d8108 10774+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 10775+ else
4a4d8108 10776+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
10777+}
10778+
10779+static struct au_dyaop *dy_aget(struct au_branch *br,
10780+ const struct address_space_operations *h_aop,
10781+ int do_dx)
10782+{
10783+ struct au_dyaop *dyaop;
10784+ struct au_dynop op;
10785+
10786+ op.dy_type = AuDy_AOP;
10787+ op.dy_haop = h_aop;
10788+ dyaop = (void *)dy_get(&op, br);
10789+ if (IS_ERR(dyaop))
10790+ goto out;
10791+ dy_adx(dyaop, do_dx);
10792+
10793+out:
10794+ return dyaop;
10795+}
10796+
10797+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10798+ struct inode *h_inode)
10799+{
10800+ int err, do_dx;
10801+ struct super_block *sb;
10802+ struct au_branch *br;
10803+ struct au_dyaop *dyaop;
10804+
10805+ AuDebugOn(!S_ISREG(h_inode->i_mode));
10806+ IiMustWriteLock(inode);
10807+
10808+ sb = inode->i_sb;
10809+ br = au_sbr(sb, bindex);
10810+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10811+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10812+ err = PTR_ERR(dyaop);
10813+ if (IS_ERR(dyaop))
10814+ /* unnecessary to call dy_fput() */
10815+ goto out;
10816+
10817+ err = 0;
10818+ inode->i_mapping->a_ops = &dyaop->da_op;
10819+
10820+out:
10821+ return err;
10822+}
10823+
b752ccd1
AM
10824+/*
10825+ * Is it safe to replace a_ops during the inode/file is in operation?
10826+ * Yes, I hope so.
10827+ */
10828+int au_dy_irefresh(struct inode *inode)
10829+{
10830+ int err;
5afbbe0d 10831+ aufs_bindex_t btop;
b752ccd1
AM
10832+ struct inode *h_inode;
10833+
10834+ err = 0;
10835+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
10836+ btop = au_ibtop(inode);
10837+ h_inode = au_h_iptr(inode, btop);
10838+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
10839+ }
10840+ return err;
10841+}
10842+
4a4d8108
AM
10843+void au_dy_arefresh(int do_dx)
10844+{
f0c0a007
AM
10845+ struct au_sphlhead *sphl;
10846+ struct hlist_head *head;
4a4d8108
AM
10847+ struct au_dykey *key;
10848+
f0c0a007
AM
10849+ sphl = dynop + AuDy_AOP;
10850+ head = &sphl->head;
10851+ spin_lock(&sphl->spin);
10852+ hlist_for_each_entry(key, head, dk_hnode)
4a4d8108 10853+ dy_adx((void *)key, do_dx);
f0c0a007 10854+ spin_unlock(&sphl->spin);
4a4d8108
AM
10855+}
10856+
4a4d8108
AM
10857+/* ---------------------------------------------------------------------- */
10858+
10859+void __init au_dy_init(void)
10860+{
10861+ int i;
10862+
10863+ /* make sure that 'struct au_dykey *' can be any type */
10864+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
10865+
10866+ for (i = 0; i < AuDyLast; i++)
f0c0a007 10867+ au_sphl_init(dynop + i);
4a4d8108
AM
10868+}
10869+
10870+void au_dy_fin(void)
10871+{
10872+ int i;
10873+
10874+ for (i = 0; i < AuDyLast; i++)
f0c0a007 10875+ WARN_ON(!hlist_empty(&dynop[i].head));
4a4d8108 10876+}
7f207e10
AM
10877diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10878--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007 10879+++ linux/fs/aufs/dynop.h 2016-08-17 18:01:06.118555489 +0200
7e9cd9fe 10880@@ -0,0 +1,74 @@
4a4d8108 10881+/*
8cdd5066 10882+ * Copyright (C) 2010-2016 Junjiro R. Okajima
4a4d8108
AM
10883+ *
10884+ * This program, aufs is free software; you can redistribute it and/or modify
10885+ * it under the terms of the GNU General Public License as published by
10886+ * the Free Software Foundation; either version 2 of the License, or
10887+ * (at your option) any later version.
10888+ *
10889+ * This program is distributed in the hope that it will be useful,
10890+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10891+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10892+ * GNU General Public License for more details.
10893+ *
10894+ * You should have received a copy of the GNU General Public License
523b37e3 10895+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
10896+ */
10897+
10898+/*
10899+ * dynamically customizable operations (for regular files only)
10900+ */
10901+
10902+#ifndef __AUFS_DYNOP_H__
10903+#define __AUFS_DYNOP_H__
10904+
10905+#ifdef __KERNEL__
10906+
7e9cd9fe
AM
10907+#include <linux/fs.h>
10908+#include <linux/kref.h>
4a4d8108 10909+
2cbb1c4b 10910+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
10911+
10912+struct au_dynop {
10913+ int dy_type;
10914+ union {
10915+ const void *dy_hop;
10916+ const struct address_space_operations *dy_haop;
4a4d8108
AM
10917+ };
10918+};
10919+
10920+struct au_dykey {
10921+ union {
f0c0a007 10922+ struct hlist_node dk_hnode;
4a4d8108
AM
10923+ struct rcu_head dk_rcu;
10924+ };
10925+ struct au_dynop dk_op;
10926+
10927+ /*
10928+ * during I am in the branch local array, kref is gotten. when the
10929+ * branch is removed, kref is put.
10930+ */
10931+ struct kref dk_kref;
10932+};
10933+
10934+/* stop unioning since their sizes are very different from each other */
10935+struct au_dyaop {
10936+ struct au_dykey da_key;
10937+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
10938+};
10939+
4a4d8108
AM
10940+/* ---------------------------------------------------------------------- */
10941+
10942+/* dynop.c */
10943+struct au_branch;
10944+void au_dy_put(struct au_dykey *key);
10945+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10946+ struct inode *h_inode);
b752ccd1 10947+int au_dy_irefresh(struct inode *inode);
4a4d8108 10948+void au_dy_arefresh(int do_dio);
4a4d8108
AM
10949+
10950+void __init au_dy_init(void);
10951+void au_dy_fin(void);
10952+
4a4d8108
AM
10953+#endif /* __KERNEL__ */
10954+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
10955diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10956--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 10957+++ linux/fs/aufs/export.c 2016-08-17 18:01:06.128555749 +0200
5afbbe0d 10958@@ -0,0 +1,837 @@
4a4d8108 10959+/*
8cdd5066 10960+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
10961+ *
10962+ * This program, aufs is free software; you can redistribute it and/or modify
10963+ * it under the terms of the GNU General Public License as published by
10964+ * the Free Software Foundation; either version 2 of the License, or
10965+ * (at your option) any later version.
10966+ *
10967+ * This program is distributed in the hope that it will be useful,
10968+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10969+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10970+ * GNU General Public License for more details.
10971+ *
10972+ * You should have received a copy of the GNU General Public License
523b37e3 10973+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
10974+ */
10975+
10976+/*
10977+ * export via nfs
10978+ */
10979+
10980+#include <linux/exportfs.h>
7eafdf33 10981+#include <linux/fs_struct.h>
4a4d8108
AM
10982+#include <linux/namei.h>
10983+#include <linux/nsproxy.h>
10984+#include <linux/random.h>
10985+#include <linux/writeback.h>
7eafdf33 10986+#include "../fs/mount.h"
4a4d8108
AM
10987+#include "aufs.h"
10988+
10989+union conv {
10990+#ifdef CONFIG_AUFS_INO_T_64
10991+ __u32 a[2];
10992+#else
10993+ __u32 a[1];
10994+#endif
10995+ ino_t ino;
10996+};
10997+
10998+static ino_t decode_ino(__u32 *a)
10999+{
11000+ union conv u;
11001+
11002+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
11003+ u.a[0] = a[0];
11004+#ifdef CONFIG_AUFS_INO_T_64
11005+ u.a[1] = a[1];
11006+#endif
11007+ return u.ino;
11008+}
11009+
11010+static void encode_ino(__u32 *a, ino_t ino)
11011+{
11012+ union conv u;
11013+
11014+ u.ino = ino;
11015+ a[0] = u.a[0];
11016+#ifdef CONFIG_AUFS_INO_T_64
11017+ a[1] = u.a[1];
11018+#endif
11019+}
11020+
11021+/* NFS file handle */
11022+enum {
11023+ Fh_br_id,
11024+ Fh_sigen,
11025+#ifdef CONFIG_AUFS_INO_T_64
11026+ /* support 64bit inode number */
11027+ Fh_ino1,
11028+ Fh_ino2,
11029+ Fh_dir_ino1,
11030+ Fh_dir_ino2,
11031+#else
11032+ Fh_ino1,
11033+ Fh_dir_ino1,
11034+#endif
11035+ Fh_igen,
11036+ Fh_h_type,
11037+ Fh_tail,
11038+
11039+ Fh_ino = Fh_ino1,
11040+ Fh_dir_ino = Fh_dir_ino1
11041+};
11042+
11043+static int au_test_anon(struct dentry *dentry)
11044+{
027c5e7a 11045+ /* note: read d_flags without d_lock */
4a4d8108
AM
11046+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
11047+}
11048+
a2a7ad62
AM
11049+int au_test_nfsd(void)
11050+{
11051+ int ret;
11052+ struct task_struct *tsk = current;
11053+ char comm[sizeof(tsk->comm)];
11054+
11055+ ret = 0;
11056+ if (tsk->flags & PF_KTHREAD) {
11057+ get_task_comm(comm, tsk);
11058+ ret = !strcmp(comm, "nfsd");
11059+ }
11060+
11061+ return ret;
11062+}
11063+
4a4d8108
AM
11064+/* ---------------------------------------------------------------------- */
11065+/* inode generation external table */
11066+
b752ccd1 11067+void au_xigen_inc(struct inode *inode)
4a4d8108 11068+{
4a4d8108
AM
11069+ loff_t pos;
11070+ ssize_t sz;
11071+ __u32 igen;
11072+ struct super_block *sb;
11073+ struct au_sbinfo *sbinfo;
11074+
4a4d8108 11075+ sb = inode->i_sb;
b752ccd1 11076+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 11077+
b752ccd1 11078+ sbinfo = au_sbi(sb);
1facf9fc 11079+ pos = inode->i_ino;
11080+ pos *= sizeof(igen);
11081+ igen = inode->i_generation + 1;
1facf9fc 11082+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
11083+ sizeof(igen), &pos);
11084+ if (sz == sizeof(igen))
b752ccd1 11085+ return; /* success */
1facf9fc 11086+
b752ccd1 11087+ if (unlikely(sz >= 0))
1facf9fc 11088+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 11089+}
11090+
11091+int au_xigen_new(struct inode *inode)
11092+{
11093+ int err;
11094+ loff_t pos;
11095+ ssize_t sz;
11096+ struct super_block *sb;
11097+ struct au_sbinfo *sbinfo;
11098+ struct file *file;
11099+
11100+ err = 0;
11101+ /* todo: dirty, at mount time */
11102+ if (inode->i_ino == AUFS_ROOT_INO)
11103+ goto out;
11104+ sb = inode->i_sb;
dece6358 11105+ SiMustAnyLock(sb);
1facf9fc 11106+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11107+ goto out;
11108+
11109+ err = -EFBIG;
11110+ pos = inode->i_ino;
11111+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
11112+ AuIOErr1("too large i%lld\n", pos);
11113+ goto out;
11114+ }
11115+ pos *= sizeof(inode->i_generation);
11116+
11117+ err = 0;
11118+ sbinfo = au_sbi(sb);
11119+ file = sbinfo->si_xigen;
11120+ BUG_ON(!file);
11121+
c06a8ce3 11122+ if (vfsub_f_size_read(file)
1facf9fc 11123+ < pos + sizeof(inode->i_generation)) {
11124+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
11125+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
11126+ sizeof(inode->i_generation), &pos);
11127+ } else
11128+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
11129+ sizeof(inode->i_generation), &pos);
11130+ if (sz == sizeof(inode->i_generation))
11131+ goto out; /* success */
11132+
11133+ err = sz;
11134+ if (unlikely(sz >= 0)) {
11135+ err = -EIO;
11136+ AuIOErr("xigen error (%zd)\n", sz);
11137+ }
11138+
4f0767ce 11139+out:
1facf9fc 11140+ return err;
11141+}
11142+
11143+int au_xigen_set(struct super_block *sb, struct file *base)
11144+{
11145+ int err;
11146+ struct au_sbinfo *sbinfo;
11147+ struct file *file;
11148+
dece6358
AM
11149+ SiMustWriteLock(sb);
11150+
1facf9fc 11151+ sbinfo = au_sbi(sb);
11152+ file = au_xino_create2(base, sbinfo->si_xigen);
11153+ err = PTR_ERR(file);
11154+ if (IS_ERR(file))
11155+ goto out;
11156+ err = 0;
11157+ if (sbinfo->si_xigen)
11158+ fput(sbinfo->si_xigen);
11159+ sbinfo->si_xigen = file;
11160+
4f0767ce 11161+out:
1facf9fc 11162+ return err;
11163+}
11164+
11165+void au_xigen_clr(struct super_block *sb)
11166+{
11167+ struct au_sbinfo *sbinfo;
11168+
dece6358
AM
11169+ SiMustWriteLock(sb);
11170+
1facf9fc 11171+ sbinfo = au_sbi(sb);
11172+ if (sbinfo->si_xigen) {
11173+ fput(sbinfo->si_xigen);
11174+ sbinfo->si_xigen = NULL;
11175+ }
11176+}
11177+
11178+/* ---------------------------------------------------------------------- */
11179+
11180+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
11181+ ino_t dir_ino)
11182+{
11183+ struct dentry *dentry, *d;
11184+ struct inode *inode;
11185+ unsigned int sigen;
11186+
11187+ dentry = NULL;
11188+ inode = ilookup(sb, ino);
11189+ if (!inode)
11190+ goto out;
11191+
11192+ dentry = ERR_PTR(-ESTALE);
11193+ sigen = au_sigen(sb);
5afbbe0d 11194+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 11195+ || IS_DEADDIR(inode)
537831f9 11196+ || sigen != au_iigen(inode, NULL)))
1facf9fc 11197+ goto out_iput;
11198+
11199+ dentry = NULL;
11200+ if (!dir_ino || S_ISDIR(inode->i_mode))
11201+ dentry = d_find_alias(inode);
11202+ else {
027c5e7a 11203+ spin_lock(&inode->i_lock);
c1595e42 11204+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 11205+ spin_lock(&d->d_lock);
1facf9fc 11206+ if (!au_test_anon(d)
5527c038 11207+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
11208+ dentry = dget_dlock(d);
11209+ spin_unlock(&d->d_lock);
1facf9fc 11210+ break;
11211+ }
027c5e7a
AM
11212+ spin_unlock(&d->d_lock);
11213+ }
11214+ spin_unlock(&inode->i_lock);
1facf9fc 11215+ }
027c5e7a 11216+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 11217+ /* need to refresh */
1facf9fc 11218+ dput(dentry);
2cbb1c4b 11219+ dentry = NULL;
1facf9fc 11220+ }
11221+
4f0767ce 11222+out_iput:
1facf9fc 11223+ iput(inode);
4f0767ce 11224+out:
2cbb1c4b 11225+ AuTraceErrPtr(dentry);
1facf9fc 11226+ return dentry;
11227+}
11228+
11229+/* ---------------------------------------------------------------------- */
11230+
11231+/* todo: dirty? */
11232+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
11233+
11234+struct au_compare_mnt_args {
11235+ /* input */
11236+ struct super_block *sb;
11237+
11238+ /* output */
11239+ struct vfsmount *mnt;
11240+};
11241+
11242+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
11243+{
11244+ struct au_compare_mnt_args *a = arg;
11245+
11246+ if (mnt->mnt_sb != a->sb)
11247+ return 0;
11248+ a->mnt = mntget(mnt);
11249+ return 1;
11250+}
11251+
1facf9fc 11252+static struct vfsmount *au_mnt_get(struct super_block *sb)
11253+{
4a4d8108 11254+ int err;
7eafdf33 11255+ struct path root;
4a4d8108
AM
11256+ struct au_compare_mnt_args args = {
11257+ .sb = sb
11258+ };
1facf9fc 11259+
7eafdf33 11260+ get_fs_root(current->fs, &root);
523b37e3 11261+ rcu_read_lock();
7eafdf33 11262+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 11263+ rcu_read_unlock();
7eafdf33 11264+ path_put(&root);
4a4d8108
AM
11265+ AuDebugOn(!err);
11266+ AuDebugOn(!args.mnt);
11267+ return args.mnt;
1facf9fc 11268+}
11269+
11270+struct au_nfsd_si_lock {
4a4d8108 11271+ unsigned int sigen;
027c5e7a 11272+ aufs_bindex_t bindex, br_id;
1facf9fc 11273+ unsigned char force_lock;
11274+};
11275+
027c5e7a
AM
11276+static int si_nfsd_read_lock(struct super_block *sb,
11277+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 11278+{
027c5e7a 11279+ int err;
1facf9fc 11280+ aufs_bindex_t bindex;
11281+
11282+ si_read_lock(sb, AuLock_FLUSH);
11283+
11284+ /* branch id may be wrapped around */
027c5e7a 11285+ err = 0;
1facf9fc 11286+ bindex = au_br_index(sb, nsi_lock->br_id);
11287+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11288+ goto out; /* success */
11289+
027c5e7a
AM
11290+ err = -ESTALE;
11291+ bindex = -1;
1facf9fc 11292+ if (!nsi_lock->force_lock)
11293+ si_read_unlock(sb);
1facf9fc 11294+
4f0767ce 11295+out:
027c5e7a
AM
11296+ nsi_lock->bindex = bindex;
11297+ return err;
1facf9fc 11298+}
11299+
11300+struct find_name_by_ino {
392086de 11301+ struct dir_context ctx;
1facf9fc 11302+ int called, found;
11303+ ino_t ino;
11304+ char *name;
11305+ int namelen;
11306+};
11307+
11308+static int
392086de
AM
11309+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11310+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 11311+{
392086de
AM
11312+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11313+ ctx);
1facf9fc 11314+
11315+ a->called++;
11316+ if (a->ino != ino)
11317+ return 0;
11318+
11319+ memcpy(a->name, name, namelen);
11320+ a->namelen = namelen;
11321+ a->found = 1;
11322+ return 1;
11323+}
11324+
11325+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11326+ struct au_nfsd_si_lock *nsi_lock)
11327+{
11328+ struct dentry *dentry, *parent;
11329+ struct file *file;
11330+ struct inode *dir;
392086de
AM
11331+ struct find_name_by_ino arg = {
11332+ .ctx = {
2000de60 11333+ .actor = find_name_by_ino
392086de
AM
11334+ }
11335+ };
1facf9fc 11336+ int err;
11337+
11338+ parent = path->dentry;
11339+ if (nsi_lock)
11340+ si_read_unlock(parent->d_sb);
4a4d8108 11341+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 11342+ dentry = (void *)file;
11343+ if (IS_ERR(file))
11344+ goto out;
11345+
11346+ dentry = ERR_PTR(-ENOMEM);
537831f9 11347+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 11348+ if (unlikely(!arg.name))
11349+ goto out_file;
11350+ arg.ino = ino;
11351+ arg.found = 0;
11352+ do {
11353+ arg.called = 0;
11354+ /* smp_mb(); */
392086de 11355+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 11356+ } while (!err && !arg.found && arg.called);
11357+ dentry = ERR_PTR(err);
11358+ if (unlikely(err))
11359+ goto out_name;
1716fcea
AM
11360+ /* instead of ENOENT */
11361+ dentry = ERR_PTR(-ESTALE);
1facf9fc 11362+ if (!arg.found)
11363+ goto out_name;
11364+
b4510431 11365+ /* do not call vfsub_lkup_one() */
5527c038 11366+ dir = d_inode(parent);
febd17d6 11367+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 11368+ AuTraceErrPtr(dentry);
11369+ if (IS_ERR(dentry))
11370+ goto out_name;
11371+ AuDebugOn(au_test_anon(dentry));
5527c038 11372+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 11373+ dput(dentry);
11374+ dentry = ERR_PTR(-ENOENT);
11375+ }
11376+
4f0767ce 11377+out_name:
f0c0a007 11378+ au_delayed_free_page((unsigned long)arg.name);
4f0767ce 11379+out_file:
1facf9fc 11380+ fput(file);
4f0767ce 11381+out:
1facf9fc 11382+ if (unlikely(nsi_lock
11383+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11384+ if (!IS_ERR(dentry)) {
11385+ dput(dentry);
11386+ dentry = ERR_PTR(-ESTALE);
11387+ }
11388+ AuTraceErrPtr(dentry);
11389+ return dentry;
11390+}
11391+
11392+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11393+ ino_t dir_ino,
11394+ struct au_nfsd_si_lock *nsi_lock)
11395+{
11396+ struct dentry *dentry;
11397+ struct path path;
11398+
11399+ if (dir_ino != AUFS_ROOT_INO) {
11400+ path.dentry = decode_by_ino(sb, dir_ino, 0);
11401+ dentry = path.dentry;
11402+ if (!path.dentry || IS_ERR(path.dentry))
11403+ goto out;
11404+ AuDebugOn(au_test_anon(path.dentry));
11405+ } else
11406+ path.dentry = dget(sb->s_root);
11407+
11408+ path.mnt = au_mnt_get(sb);
11409+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11410+ path_put(&path);
11411+
4f0767ce 11412+out:
1facf9fc 11413+ AuTraceErrPtr(dentry);
11414+ return dentry;
11415+}
11416+
11417+/* ---------------------------------------------------------------------- */
11418+
11419+static int h_acceptable(void *expv, struct dentry *dentry)
11420+{
11421+ return 1;
11422+}
11423+
11424+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11425+ char *buf, int len, struct super_block *sb)
11426+{
11427+ char *p;
11428+ int n;
11429+ struct path path;
11430+
11431+ p = d_path(h_rootpath, buf, len);
11432+ if (IS_ERR(p))
11433+ goto out;
11434+ n = strlen(p);
11435+
11436+ path.mnt = h_rootpath->mnt;
11437+ path.dentry = h_parent;
11438+ p = d_path(&path, buf, len);
11439+ if (IS_ERR(p))
11440+ goto out;
11441+ if (n != 1)
11442+ p += n;
11443+
11444+ path.mnt = au_mnt_get(sb);
11445+ path.dentry = sb->s_root;
11446+ p = d_path(&path, buf, len - strlen(p));
11447+ mntput(path.mnt);
11448+ if (IS_ERR(p))
11449+ goto out;
11450+ if (n != 1)
11451+ p[strlen(p)] = '/';
11452+
4f0767ce 11453+out:
1facf9fc 11454+ AuTraceErrPtr(p);
11455+ return p;
11456+}
11457+
11458+static
027c5e7a
AM
11459+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11460+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 11461+{
11462+ struct dentry *dentry, *h_parent, *root;
11463+ struct super_block *h_sb;
11464+ char *pathname, *p;
11465+ struct vfsmount *h_mnt;
11466+ struct au_branch *br;
11467+ int err;
11468+ struct path path;
11469+
027c5e7a 11470+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 11471+ h_mnt = au_br_mnt(br);
1facf9fc 11472+ h_sb = h_mnt->mnt_sb;
11473+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 11474+ lockdep_off();
1facf9fc 11475+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11476+ fh_len - Fh_tail, fh[Fh_h_type],
11477+ h_acceptable, /*context*/NULL);
5afbbe0d 11478+ lockdep_on();
1facf9fc 11479+ dentry = h_parent;
11480+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
11481+ AuWarn1("%s decode_fh failed, %ld\n",
11482+ au_sbtype(h_sb), PTR_ERR(h_parent));
11483+ goto out;
11484+ }
11485+ dentry = NULL;
11486+ if (unlikely(au_test_anon(h_parent))) {
11487+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
11488+ au_sbtype(h_sb));
11489+ goto out_h_parent;
11490+ }
11491+
11492+ dentry = ERR_PTR(-ENOMEM);
11493+ pathname = (void *)__get_free_page(GFP_NOFS);
11494+ if (unlikely(!pathname))
11495+ goto out_h_parent;
11496+
11497+ root = sb->s_root;
11498+ path.mnt = h_mnt;
11499+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 11500+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 11501+ di_read_unlock(root, !AuLock_IR);
11502+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11503+ dentry = (void *)p;
11504+ if (IS_ERR(p))
11505+ goto out_pathname;
11506+
11507+ si_read_unlock(sb);
11508+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11509+ dentry = ERR_PTR(err);
11510+ if (unlikely(err))
11511+ goto out_relock;
11512+
11513+ dentry = ERR_PTR(-ENOENT);
11514+ AuDebugOn(au_test_anon(path.dentry));
5527c038 11515+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 11516+ goto out_path;
11517+
5527c038 11518+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 11519+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11520+ else
11521+ dentry = dget(path.dentry);
11522+
4f0767ce 11523+out_path:
1facf9fc 11524+ path_put(&path);
4f0767ce 11525+out_relock:
1facf9fc 11526+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11527+ if (!IS_ERR(dentry)) {
11528+ dput(dentry);
11529+ dentry = ERR_PTR(-ESTALE);
11530+ }
4f0767ce 11531+out_pathname:
f0c0a007 11532+ au_delayed_free_page((unsigned long)pathname);
4f0767ce 11533+out_h_parent:
1facf9fc 11534+ dput(h_parent);
4f0767ce 11535+out:
1facf9fc 11536+ AuTraceErrPtr(dentry);
11537+ return dentry;
11538+}
11539+
11540+/* ---------------------------------------------------------------------- */
11541+
11542+static struct dentry *
11543+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11544+ int fh_type)
11545+{
11546+ struct dentry *dentry;
11547+ __u32 *fh = fid->raw;
027c5e7a 11548+ struct au_branch *br;
1facf9fc 11549+ ino_t ino, dir_ino;
1facf9fc 11550+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 11551+ .force_lock = 0
11552+ };
11553+
1facf9fc 11554+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
11555+ /* it should never happen, but the file handle is unreliable */
11556+ if (unlikely(fh_len < Fh_tail))
11557+ goto out;
11558+ nsi_lock.sigen = fh[Fh_sigen];
11559+ nsi_lock.br_id = fh[Fh_br_id];
11560+
1facf9fc 11561+ /* branch id may be wrapped around */
027c5e7a
AM
11562+ br = NULL;
11563+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 11564+ goto out;
11565+ nsi_lock.force_lock = 1;
11566+
11567+ /* is this inode still cached? */
11568+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
11569+ /* it should never happen */
11570+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 11571+ goto out_unlock;
4a4d8108 11572+
1facf9fc 11573+ dir_ino = decode_ino(fh + Fh_dir_ino);
11574+ dentry = decode_by_ino(sb, ino, dir_ino);
11575+ if (IS_ERR(dentry))
11576+ goto out_unlock;
11577+ if (dentry)
11578+ goto accept;
11579+
11580+ /* is the parent dir cached? */
027c5e7a 11581+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 11582+ au_br_get(br);
1facf9fc 11583+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11584+ if (IS_ERR(dentry))
11585+ goto out_unlock;
11586+ if (dentry)
11587+ goto accept;
11588+
11589+ /* lookup path */
027c5e7a 11590+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 11591+ if (IS_ERR(dentry))
11592+ goto out_unlock;
11593+ if (unlikely(!dentry))
11594+ /* todo?: make it ESTALE */
11595+ goto out_unlock;
11596+
4f0767ce 11597+accept:
027c5e7a 11598+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 11599+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 11600+ goto out_unlock; /* success */
11601+
11602+ dput(dentry);
11603+ dentry = ERR_PTR(-ESTALE);
4f0767ce 11604+out_unlock:
027c5e7a 11605+ if (br)
5afbbe0d 11606+ au_br_put(br);
1facf9fc 11607+ si_read_unlock(sb);
4f0767ce 11608+out:
1facf9fc 11609+ AuTraceErrPtr(dentry);
11610+ return dentry;
11611+}
11612+
11613+#if 0 /* reserved for future use */
11614+/* support subtreecheck option */
11615+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11616+ int fh_len, int fh_type)
11617+{
11618+ struct dentry *parent;
11619+ __u32 *fh = fid->raw;
11620+ ino_t dir_ino;
11621+
11622+ dir_ino = decode_ino(fh + Fh_dir_ino);
11623+ parent = decode_by_ino(sb, dir_ino, 0);
11624+ if (IS_ERR(parent))
11625+ goto out;
11626+ if (!parent)
11627+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11628+ dir_ino, fh, fh_len);
11629+
4f0767ce 11630+out:
1facf9fc 11631+ AuTraceErrPtr(parent);
11632+ return parent;
11633+}
11634+#endif
11635+
11636+/* ---------------------------------------------------------------------- */
11637+
0c3ec466
AM
11638+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11639+ struct inode *dir)
1facf9fc 11640+{
11641+ int err;
0c3ec466 11642+ aufs_bindex_t bindex;
1facf9fc 11643+ struct super_block *sb, *h_sb;
0c3ec466
AM
11644+ struct dentry *dentry, *parent, *h_parent;
11645+ struct inode *h_dir;
1facf9fc 11646+ struct au_branch *br;
11647+
1facf9fc 11648+ err = -ENOSPC;
11649+ if (unlikely(*max_len <= Fh_tail)) {
11650+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11651+ goto out;
11652+ }
11653+
11654+ err = FILEID_ROOT;
0c3ec466
AM
11655+ if (inode->i_ino == AUFS_ROOT_INO) {
11656+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 11657+ goto out;
11658+ }
11659+
1facf9fc 11660+ h_parent = NULL;
0c3ec466
AM
11661+ sb = inode->i_sb;
11662+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
11663+ if (unlikely(err))
11664+ goto out;
11665+
1facf9fc 11666+#ifdef CONFIG_AUFS_DEBUG
11667+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11668+ AuWarn1("NFS-exporting requires xino\n");
11669+#endif
027c5e7a 11670+ err = -EIO;
0c3ec466
AM
11671+ parent = NULL;
11672+ ii_read_lock_child(inode);
5afbbe0d 11673+ bindex = au_ibtop(inode);
0c3ec466 11674+ if (!dir) {
c1595e42 11675+ dentry = d_find_any_alias(inode);
0c3ec466
AM
11676+ if (unlikely(!dentry))
11677+ goto out_unlock;
11678+ AuDebugOn(au_test_anon(dentry));
11679+ parent = dget_parent(dentry);
11680+ dput(dentry);
11681+ if (unlikely(!parent))
11682+ goto out_unlock;
5527c038
JR
11683+ if (d_really_is_positive(parent))
11684+ dir = d_inode(parent);
1facf9fc 11685+ }
0c3ec466
AM
11686+
11687+ ii_read_lock_parent(dir);
11688+ h_dir = au_h_iptr(dir, bindex);
11689+ ii_read_unlock(dir);
11690+ if (unlikely(!h_dir))
11691+ goto out_parent;
c1595e42 11692+ h_parent = d_find_any_alias(h_dir);
1facf9fc 11693+ if (unlikely(!h_parent))
0c3ec466 11694+ goto out_hparent;
1facf9fc 11695+
11696+ err = -EPERM;
11697+ br = au_sbr(sb, bindex);
86dc4139 11698+ h_sb = au_br_sb(br);
1facf9fc 11699+ if (unlikely(!h_sb->s_export_op)) {
11700+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 11701+ goto out_hparent;
1facf9fc 11702+ }
11703+
11704+ fh[Fh_br_id] = br->br_id;
11705+ fh[Fh_sigen] = au_sigen(sb);
11706+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 11707+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 11708+ fh[Fh_igen] = inode->i_generation;
11709+
11710+ *max_len -= Fh_tail;
11711+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11712+ max_len,
11713+ /*connectable or subtreecheck*/0);
11714+ err = fh[Fh_h_type];
11715+ *max_len += Fh_tail;
11716+ /* todo: macros? */
1716fcea 11717+ if (err != FILEID_INVALID)
1facf9fc 11718+ err = 99;
11719+ else
11720+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11721+
0c3ec466 11722+out_hparent:
1facf9fc 11723+ dput(h_parent);
0c3ec466 11724+out_parent:
1facf9fc 11725+ dput(parent);
0c3ec466
AM
11726+out_unlock:
11727+ ii_read_unlock(inode);
11728+ si_read_unlock(sb);
4f0767ce 11729+out:
1facf9fc 11730+ if (unlikely(err < 0))
1716fcea 11731+ err = FILEID_INVALID;
1facf9fc 11732+ return err;
11733+}
11734+
11735+/* ---------------------------------------------------------------------- */
11736+
4a4d8108
AM
11737+static int aufs_commit_metadata(struct inode *inode)
11738+{
11739+ int err;
11740+ aufs_bindex_t bindex;
11741+ struct super_block *sb;
11742+ struct inode *h_inode;
11743+ int (*f)(struct inode *inode);
11744+
11745+ sb = inode->i_sb;
e49829fe 11746+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 11747+ ii_write_lock_child(inode);
5afbbe0d 11748+ bindex = au_ibtop(inode);
4a4d8108
AM
11749+ AuDebugOn(bindex < 0);
11750+ h_inode = au_h_iptr(inode, bindex);
11751+
11752+ f = h_inode->i_sb->s_export_op->commit_metadata;
11753+ if (f)
11754+ err = f(h_inode);
11755+ else {
11756+ struct writeback_control wbc = {
11757+ .sync_mode = WB_SYNC_ALL,
11758+ .nr_to_write = 0 /* metadata only */
11759+ };
11760+
11761+ err = sync_inode(h_inode, &wbc);
11762+ }
11763+
11764+ au_cpup_attr_timesizes(inode);
11765+ ii_write_unlock(inode);
11766+ si_read_unlock(sb);
11767+ return err;
11768+}
11769+
11770+/* ---------------------------------------------------------------------- */
11771+
1facf9fc 11772+static struct export_operations aufs_export_op = {
4a4d8108 11773+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 11774+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
11775+ .encode_fh = aufs_encode_fh,
11776+ .commit_metadata = aufs_commit_metadata
1facf9fc 11777+};
11778+
11779+void au_export_init(struct super_block *sb)
11780+{
11781+ struct au_sbinfo *sbinfo;
11782+ __u32 u;
11783+
5afbbe0d
AM
11784+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
11785+ && IS_MODULE(CONFIG_EXPORTFS),
11786+ AUFS_NAME ": unsupported configuration "
11787+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
11788+
1facf9fc 11789+ sb->s_export_op = &aufs_export_op;
11790+ sbinfo = au_sbi(sb);
11791+ sbinfo->si_xigen = NULL;
11792+ get_random_bytes(&u, sizeof(u));
11793+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
11794+ atomic_set(&sbinfo->si_xigen_next, u);
11795+}
076b876e
AM
11796diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11797--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 11798+++ linux/fs/aufs/fhsm.c 2016-07-25 19:05:34.814493242 +0200
c1595e42 11799@@ -0,0 +1,426 @@
076b876e 11800+/*
8cdd5066 11801+ * Copyright (C) 2011-2016 Junjiro R. Okajima
076b876e
AM
11802+ *
11803+ * This program, aufs is free software; you can redistribute it and/or modify
11804+ * it under the terms of the GNU General Public License as published by
11805+ * the Free Software Foundation; either version 2 of the License, or
11806+ * (at your option) any later version.
11807+ *
11808+ * This program is distributed in the hope that it will be useful,
11809+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11810+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11811+ * GNU General Public License for more details.
11812+ *
11813+ * You should have received a copy of the GNU General Public License
11814+ * along with this program; if not, write to the Free Software
11815+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
11816+ */
11817+
11818+/*
11819+ * File-based Hierarchy Storage Management
11820+ */
11821+
11822+#include <linux/anon_inodes.h>
11823+#include <linux/poll.h>
11824+#include <linux/seq_file.h>
11825+#include <linux/statfs.h>
11826+#include "aufs.h"
11827+
c1595e42
JR
11828+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11829+{
11830+ struct au_sbinfo *sbinfo;
11831+ struct au_fhsm *fhsm;
11832+
11833+ SiMustAnyLock(sb);
11834+
11835+ sbinfo = au_sbi(sb);
11836+ fhsm = &sbinfo->si_fhsm;
11837+ AuDebugOn(!fhsm);
11838+ return fhsm->fhsm_bottom;
11839+}
11840+
11841+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11842+{
11843+ struct au_sbinfo *sbinfo;
11844+ struct au_fhsm *fhsm;
11845+
11846+ SiMustWriteLock(sb);
11847+
11848+ sbinfo = au_sbi(sb);
11849+ fhsm = &sbinfo->si_fhsm;
11850+ AuDebugOn(!fhsm);
11851+ fhsm->fhsm_bottom = bindex;
11852+}
11853+
11854+/* ---------------------------------------------------------------------- */
11855+
076b876e
AM
11856+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11857+{
11858+ struct au_br_fhsm *bf;
11859+
11860+ bf = br->br_fhsm;
11861+ MtxMustLock(&bf->bf_lock);
11862+
11863+ return !bf->bf_readable
11864+ || time_after(jiffies,
11865+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11866+}
11867+
11868+/* ---------------------------------------------------------------------- */
11869+
11870+static void au_fhsm_notify(struct super_block *sb, int val)
11871+{
11872+ struct au_sbinfo *sbinfo;
11873+ struct au_fhsm *fhsm;
11874+
11875+ SiMustAnyLock(sb);
11876+
11877+ sbinfo = au_sbi(sb);
11878+ fhsm = &sbinfo->si_fhsm;
11879+ if (au_fhsm_pid(fhsm)
11880+ && atomic_read(&fhsm->fhsm_readable) != -1) {
11881+ atomic_set(&fhsm->fhsm_readable, val);
11882+ if (val)
11883+ wake_up(&fhsm->fhsm_wqh);
11884+ }
11885+}
11886+
11887+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11888+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
11889+{
11890+ int err;
11891+ struct au_branch *br;
11892+ struct au_br_fhsm *bf;
11893+
11894+ br = au_sbr(sb, bindex);
11895+ AuDebugOn(au_br_rdonly(br));
11896+ bf = br->br_fhsm;
11897+ AuDebugOn(!bf);
11898+
11899+ if (do_lock)
11900+ mutex_lock(&bf->bf_lock);
11901+ else
11902+ MtxMustLock(&bf->bf_lock);
11903+
11904+ /* sb->s_root for NFS is unreliable */
11905+ err = au_br_stfs(br, &bf->bf_stfs);
11906+ if (unlikely(err)) {
11907+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11908+ goto out;
11909+ }
11910+
11911+ bf->bf_jiffy = jiffies;
11912+ bf->bf_readable = 1;
11913+ if (do_notify)
11914+ au_fhsm_notify(sb, /*val*/1);
11915+ if (rstfs)
11916+ *rstfs = bf->bf_stfs;
11917+
11918+out:
11919+ if (do_lock)
11920+ mutex_unlock(&bf->bf_lock);
11921+ au_fhsm_notify(sb, /*val*/1);
11922+
11923+ return err;
11924+}
11925+
11926+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11927+{
11928+ int err;
076b876e
AM
11929+ struct au_sbinfo *sbinfo;
11930+ struct au_fhsm *fhsm;
11931+ struct au_branch *br;
11932+ struct au_br_fhsm *bf;
11933+
11934+ AuDbg("b%d, force %d\n", bindex, force);
11935+ SiMustAnyLock(sb);
11936+
11937+ sbinfo = au_sbi(sb);
11938+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
11939+ if (!au_ftest_si(sbinfo, FHSM)
11940+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
11941+ return;
11942+
11943+ br = au_sbr(sb, bindex);
11944+ bf = br->br_fhsm;
11945+ AuDebugOn(!bf);
11946+ mutex_lock(&bf->bf_lock);
11947+ if (force
11948+ || au_fhsm_pid(fhsm)
11949+ || au_fhsm_test_jiffy(sbinfo, br))
11950+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11951+ /*do_notify*/1);
11952+ mutex_unlock(&bf->bf_lock);
11953+}
11954+
11955+void au_fhsm_wrote_all(struct super_block *sb, int force)
11956+{
5afbbe0d 11957+ aufs_bindex_t bindex, bbot;
076b876e
AM
11958+ struct au_branch *br;
11959+
11960+ /* exclude the bottom */
5afbbe0d
AM
11961+ bbot = au_fhsm_bottom(sb);
11962+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
11963+ br = au_sbr(sb, bindex);
11964+ if (au_br_fhsm(br->br_perm))
11965+ au_fhsm_wrote(sb, bindex, force);
11966+ }
11967+}
11968+
11969+/* ---------------------------------------------------------------------- */
11970+
11971+static unsigned int au_fhsm_poll(struct file *file,
11972+ struct poll_table_struct *wait)
11973+{
11974+ unsigned int mask;
11975+ struct au_sbinfo *sbinfo;
11976+ struct au_fhsm *fhsm;
11977+
11978+ mask = 0;
11979+ sbinfo = file->private_data;
11980+ fhsm = &sbinfo->si_fhsm;
11981+ poll_wait(file, &fhsm->fhsm_wqh, wait);
11982+ if (atomic_read(&fhsm->fhsm_readable))
11983+ mask = POLLIN /* | POLLRDNORM */;
11984+
11985+ AuTraceErr((int)mask);
11986+ return mask;
11987+}
11988+
11989+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11990+ struct aufs_stfs *stfs, __s16 brid)
11991+{
11992+ int err;
11993+
11994+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11995+ if (!err)
11996+ err = __put_user(brid, &stbr->brid);
11997+ if (unlikely(err))
11998+ err = -EFAULT;
11999+
12000+ return err;
12001+}
12002+
12003+static ssize_t au_fhsm_do_read(struct super_block *sb,
12004+ struct aufs_stbr __user *stbr, size_t count)
12005+{
12006+ ssize_t err;
12007+ int nstbr;
5afbbe0d 12008+ aufs_bindex_t bindex, bbot;
076b876e
AM
12009+ struct au_branch *br;
12010+ struct au_br_fhsm *bf;
12011+
12012+ /* except the bottom branch */
12013+ err = 0;
12014+ nstbr = 0;
5afbbe0d
AM
12015+ bbot = au_fhsm_bottom(sb);
12016+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
12017+ br = au_sbr(sb, bindex);
12018+ if (!au_br_fhsm(br->br_perm))
12019+ continue;
12020+
12021+ bf = br->br_fhsm;
12022+ mutex_lock(&bf->bf_lock);
12023+ if (bf->bf_readable) {
12024+ err = -EFAULT;
12025+ if (count >= sizeof(*stbr))
12026+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
12027+ br->br_id);
12028+ if (!err) {
12029+ bf->bf_readable = 0;
12030+ count -= sizeof(*stbr);
12031+ nstbr++;
12032+ }
12033+ }
12034+ mutex_unlock(&bf->bf_lock);
12035+ }
12036+ if (!err)
12037+ err = sizeof(*stbr) * nstbr;
12038+
12039+ return err;
12040+}
12041+
12042+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
12043+ loff_t *pos)
12044+{
12045+ ssize_t err;
12046+ int readable;
5afbbe0d 12047+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
12048+ struct au_sbinfo *sbinfo;
12049+ struct au_fhsm *fhsm;
12050+ struct au_branch *br;
12051+ struct super_block *sb;
12052+
12053+ err = 0;
12054+ sbinfo = file->private_data;
12055+ fhsm = &sbinfo->si_fhsm;
12056+need_data:
12057+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
12058+ if (!atomic_read(&fhsm->fhsm_readable)) {
12059+ if (vfsub_file_flags(file) & O_NONBLOCK)
12060+ err = -EAGAIN;
12061+ else
12062+ err = wait_event_interruptible_locked_irq
12063+ (fhsm->fhsm_wqh,
12064+ atomic_read(&fhsm->fhsm_readable));
12065+ }
12066+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
12067+ if (unlikely(err))
12068+ goto out;
12069+
12070+ /* sb may already be dead */
12071+ au_rw_read_lock(&sbinfo->si_rwsem);
12072+ readable = atomic_read(&fhsm->fhsm_readable);
12073+ if (readable > 0) {
12074+ sb = sbinfo->si_sb;
12075+ AuDebugOn(!sb);
12076+ /* exclude the bottom branch */
12077+ nfhsm = 0;
5afbbe0d
AM
12078+ bbot = au_fhsm_bottom(sb);
12079+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
12080+ br = au_sbr(sb, bindex);
12081+ if (au_br_fhsm(br->br_perm))
12082+ nfhsm++;
12083+ }
12084+ err = -EMSGSIZE;
12085+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
12086+ atomic_set(&fhsm->fhsm_readable, 0);
12087+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
12088+ count);
12089+ }
12090+ }
12091+ au_rw_read_unlock(&sbinfo->si_rwsem);
12092+ if (!readable)
12093+ goto need_data;
12094+
12095+out:
12096+ return err;
12097+}
12098+
12099+static int au_fhsm_release(struct inode *inode, struct file *file)
12100+{
12101+ struct au_sbinfo *sbinfo;
12102+ struct au_fhsm *fhsm;
12103+
12104+ /* sb may already be dead */
12105+ sbinfo = file->private_data;
12106+ fhsm = &sbinfo->si_fhsm;
12107+ spin_lock(&fhsm->fhsm_spin);
12108+ fhsm->fhsm_pid = 0;
12109+ spin_unlock(&fhsm->fhsm_spin);
12110+ kobject_put(&sbinfo->si_kobj);
12111+
12112+ return 0;
12113+}
12114+
12115+static const struct file_operations au_fhsm_fops = {
12116+ .owner = THIS_MODULE,
12117+ .llseek = noop_llseek,
12118+ .read = au_fhsm_read,
12119+ .poll = au_fhsm_poll,
12120+ .release = au_fhsm_release
12121+};
12122+
12123+int au_fhsm_fd(struct super_block *sb, int oflags)
12124+{
12125+ int err, fd;
12126+ struct au_sbinfo *sbinfo;
12127+ struct au_fhsm *fhsm;
12128+
12129+ err = -EPERM;
12130+ if (unlikely(!capable(CAP_SYS_ADMIN)))
12131+ goto out;
12132+
12133+ err = -EINVAL;
12134+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
12135+ goto out;
12136+
12137+ err = 0;
12138+ sbinfo = au_sbi(sb);
12139+ fhsm = &sbinfo->si_fhsm;
12140+ spin_lock(&fhsm->fhsm_spin);
12141+ if (!fhsm->fhsm_pid)
12142+ fhsm->fhsm_pid = current->pid;
12143+ else
12144+ err = -EBUSY;
12145+ spin_unlock(&fhsm->fhsm_spin);
12146+ if (unlikely(err))
12147+ goto out;
12148+
12149+ oflags |= O_RDONLY;
12150+ /* oflags |= FMODE_NONOTIFY; */
12151+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
12152+ err = fd;
12153+ if (unlikely(fd < 0))
12154+ goto out_pid;
12155+
12156+ /* succeed reglardless 'fhsm' status */
12157+ kobject_get(&sbinfo->si_kobj);
12158+ si_noflush_read_lock(sb);
12159+ if (au_ftest_si(sbinfo, FHSM))
12160+ au_fhsm_wrote_all(sb, /*force*/0);
12161+ si_read_unlock(sb);
12162+ goto out; /* success */
12163+
12164+out_pid:
12165+ spin_lock(&fhsm->fhsm_spin);
12166+ fhsm->fhsm_pid = 0;
12167+ spin_unlock(&fhsm->fhsm_spin);
12168+out:
12169+ AuTraceErr(err);
12170+ return err;
12171+}
12172+
12173+/* ---------------------------------------------------------------------- */
12174+
12175+int au_fhsm_br_alloc(struct au_branch *br)
12176+{
12177+ int err;
12178+
12179+ err = 0;
12180+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
12181+ if (br->br_fhsm)
12182+ au_br_fhsm_init(br->br_fhsm);
12183+ else
12184+ err = -ENOMEM;
12185+
12186+ return err;
12187+}
12188+
12189+/* ---------------------------------------------------------------------- */
12190+
12191+void au_fhsm_fin(struct super_block *sb)
12192+{
12193+ au_fhsm_notify(sb, /*val*/-1);
12194+}
12195+
12196+void au_fhsm_init(struct au_sbinfo *sbinfo)
12197+{
12198+ struct au_fhsm *fhsm;
12199+
12200+ fhsm = &sbinfo->si_fhsm;
12201+ spin_lock_init(&fhsm->fhsm_spin);
12202+ init_waitqueue_head(&fhsm->fhsm_wqh);
12203+ atomic_set(&fhsm->fhsm_readable, 0);
12204+ fhsm->fhsm_expire
12205+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 12206+ fhsm->fhsm_bottom = -1;
076b876e
AM
12207+}
12208+
12209+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
12210+{
12211+ sbinfo->si_fhsm.fhsm_expire
12212+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
12213+}
12214+
12215+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
12216+{
12217+ unsigned int u;
12218+
12219+ if (!au_ftest_si(sbinfo, FHSM))
12220+ return;
12221+
12222+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
12223+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
12224+ seq_printf(seq, ",fhsm_sec=%u", u);
12225+}
7f207e10
AM
12226diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
12227--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
12228+++ linux/fs/aufs/file.c 2016-08-17 18:01:21.295617591 +0200
12229@@ -0,0 +1,845 @@
1facf9fc 12230+/*
8cdd5066 12231+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 12232+ *
12233+ * This program, aufs is free software; you can redistribute it and/or modify
12234+ * it under the terms of the GNU General Public License as published by
12235+ * the Free Software Foundation; either version 2 of the License, or
12236+ * (at your option) any later version.
dece6358
AM
12237+ *
12238+ * This program is distributed in the hope that it will be useful,
12239+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12240+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12241+ * GNU General Public License for more details.
12242+ *
12243+ * You should have received a copy of the GNU General Public License
523b37e3 12244+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12245+ */
12246+
12247+/*
4a4d8108 12248+ * handling file/dir, and address_space operation
1facf9fc 12249+ */
12250+
7eafdf33
AM
12251+#ifdef CONFIG_AUFS_DEBUG
12252+#include <linux/migrate.h>
12253+#endif
4a4d8108 12254+#include <linux/pagemap.h>
1facf9fc 12255+#include "aufs.h"
12256+
4a4d8108
AM
12257+/* drop flags for writing */
12258+unsigned int au_file_roflags(unsigned int flags)
12259+{
12260+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
12261+ flags |= O_RDONLY | O_NOATIME;
12262+ return flags;
12263+}
12264+
12265+/* common functions to regular file and dir */
12266+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 12267+ struct file *file, int force_wr)
1facf9fc 12268+{
1308ab2a 12269+ struct file *h_file;
4a4d8108
AM
12270+ struct dentry *h_dentry;
12271+ struct inode *h_inode;
12272+ struct super_block *sb;
12273+ struct au_branch *br;
12274+ struct path h_path;
b912730e 12275+ int err;
1facf9fc 12276+
4a4d8108
AM
12277+ /* a race condition can happen between open and unlink/rmdir */
12278+ h_file = ERR_PTR(-ENOENT);
12279+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 12280+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 12281+ goto out;
5527c038 12282+ h_inode = d_inode(h_dentry);
027c5e7a
AM
12283+ spin_lock(&h_dentry->d_lock);
12284+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 12285+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
12286+ ;
12287+ spin_unlock(&h_dentry->d_lock);
12288+ if (unlikely(err))
4a4d8108 12289+ goto out;
1facf9fc 12290+
4a4d8108
AM
12291+ sb = dentry->d_sb;
12292+ br = au_sbr(sb, bindex);
b912730e
AM
12293+ err = au_br_test_oflag(flags, br);
12294+ h_file = ERR_PTR(err);
12295+ if (unlikely(err))
027c5e7a 12296+ goto out;
1facf9fc 12297+
4a4d8108 12298+ /* drop flags for writing */
5527c038 12299+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
12300+ if (force_wr && !(flags & O_WRONLY))
12301+ force_wr = 0;
4a4d8108 12302+ flags = au_file_roflags(flags);
392086de
AM
12303+ if (force_wr) {
12304+ h_file = ERR_PTR(-EROFS);
12305+ flags = au_file_roflags(flags);
12306+ if (unlikely(vfsub_native_ro(h_inode)
12307+ || IS_APPEND(h_inode)))
12308+ goto out;
12309+ flags &= ~O_ACCMODE;
12310+ flags |= O_WRONLY;
12311+ }
12312+ }
4a4d8108 12313+ flags &= ~O_CREAT;
5afbbe0d 12314+ au_br_get(br);
4a4d8108 12315+ h_path.dentry = h_dentry;
86dc4139 12316+ h_path.mnt = au_br_mnt(br);
38d290e6 12317+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
12318+ if (IS_ERR(h_file))
12319+ goto out_br;
dece6358 12320+
b912730e 12321+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
12322+ err = deny_write_access(h_file);
12323+ if (unlikely(err)) {
12324+ fput(h_file);
12325+ h_file = ERR_PTR(err);
12326+ goto out_br;
12327+ }
12328+ }
953406b4 12329+ fsnotify_open(h_file);
4a4d8108 12330+ goto out; /* success */
1facf9fc 12331+
4f0767ce 12332+out_br:
5afbbe0d 12333+ au_br_put(br);
4f0767ce 12334+out:
4a4d8108
AM
12335+ return h_file;
12336+}
1308ab2a 12337+
076b876e
AM
12338+static int au_cmoo(struct dentry *dentry)
12339+{
12340+ int err, cmoo;
12341+ unsigned int udba;
12342+ struct path h_path;
12343+ struct au_pin pin;
12344+ struct au_cp_generic cpg = {
12345+ .dentry = dentry,
12346+ .bdst = -1,
12347+ .bsrc = -1,
12348+ .len = -1,
12349+ .pin = &pin,
12350+ .flags = AuCpup_DTIME | AuCpup_HOPEN
12351+ };
7e9cd9fe 12352+ struct inode *delegated;
076b876e
AM
12353+ struct super_block *sb;
12354+ struct au_sbinfo *sbinfo;
12355+ struct au_fhsm *fhsm;
12356+ pid_t pid;
12357+ struct au_branch *br;
12358+ struct dentry *parent;
12359+ struct au_hinode *hdir;
12360+
12361+ DiMustWriteLock(dentry);
5527c038 12362+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
12363+
12364+ err = 0;
12365+ if (IS_ROOT(dentry))
12366+ goto out;
5afbbe0d 12367+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
12368+ if (!cpg.bsrc)
12369+ goto out;
12370+
12371+ sb = dentry->d_sb;
12372+ sbinfo = au_sbi(sb);
12373+ fhsm = &sbinfo->si_fhsm;
12374+ pid = au_fhsm_pid(fhsm);
12375+ if (pid
12376+ && (current->pid == pid
12377+ || current->real_parent->pid == pid))
12378+ goto out;
12379+
12380+ br = au_sbr(sb, cpg.bsrc);
12381+ cmoo = au_br_cmoo(br->br_perm);
12382+ if (!cmoo)
12383+ goto out;
7e9cd9fe 12384+ if (!d_is_reg(dentry))
076b876e
AM
12385+ cmoo &= AuBrAttr_COO_ALL;
12386+ if (!cmoo)
12387+ goto out;
12388+
12389+ parent = dget_parent(dentry);
12390+ di_write_lock_parent(parent);
12391+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12392+ cpg.bdst = err;
12393+ if (unlikely(err < 0)) {
12394+ err = 0; /* there is no upper writable branch */
12395+ goto out_dgrade;
12396+ }
12397+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12398+
12399+ /* do not respect the coo attrib for the target branch */
12400+ err = au_cpup_dirs(dentry, cpg.bdst);
12401+ if (unlikely(err))
12402+ goto out_dgrade;
12403+
12404+ di_downgrade_lock(parent, AuLock_IR);
12405+ udba = au_opt_udba(sb);
12406+ err = au_pin(&pin, dentry, cpg.bdst, udba,
12407+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12408+ if (unlikely(err))
12409+ goto out_parent;
12410+
12411+ err = au_sio_cpup_simple(&cpg);
12412+ au_unpin(&pin);
12413+ if (unlikely(err))
12414+ goto out_parent;
12415+ if (!(cmoo & AuBrWAttr_MOO))
12416+ goto out_parent; /* success */
12417+
12418+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
12419+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12420+ if (unlikely(err))
12421+ goto out_parent;
12422+
12423+ h_path.mnt = au_br_mnt(br);
12424+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 12425+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
12426+ delegated = NULL;
12427+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12428+ au_unpin(&pin);
12429+ /* todo: keep h_dentry or not? */
12430+ if (unlikely(err == -EWOULDBLOCK)) {
12431+ pr_warn("cannot retry for NFSv4 delegation"
12432+ " for an internal unlink\n");
12433+ iput(delegated);
12434+ }
12435+ if (unlikely(err)) {
12436+ pr_err("unlink %pd after coo failed (%d), ignored\n",
12437+ dentry, err);
12438+ err = 0;
12439+ }
12440+ goto out_parent; /* success */
12441+
12442+out_dgrade:
12443+ di_downgrade_lock(parent, AuLock_IR);
12444+out_parent:
12445+ di_read_unlock(parent, AuLock_IR);
12446+ dput(parent);
12447+out:
12448+ AuTraceErr(err);
12449+ return err;
12450+}
12451+
b912730e 12452+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 12453+{
b912730e 12454+ int err, no_lock = args->no_lock;
1facf9fc 12455+ struct dentry *dentry;
076b876e 12456+ struct au_finfo *finfo;
1308ab2a 12457+
b912730e
AM
12458+ if (!no_lock)
12459+ err = au_finfo_init(file, args->fidir);
12460+ else {
12461+ lockdep_off();
12462+ err = au_finfo_init(file, args->fidir);
12463+ lockdep_on();
12464+ }
4a4d8108
AM
12465+ if (unlikely(err))
12466+ goto out;
1facf9fc 12467+
2000de60 12468+ dentry = file->f_path.dentry;
b912730e
AM
12469+ AuDebugOn(IS_ERR_OR_NULL(dentry));
12470+ if (!no_lock) {
12471+ di_write_lock_child(dentry);
12472+ err = au_cmoo(dentry);
12473+ di_downgrade_lock(dentry, AuLock_IR);
12474+ if (!err)
12475+ err = args->open(file, vfsub_file_flags(file), NULL);
12476+ di_read_unlock(dentry, AuLock_IR);
12477+ } else {
12478+ err = au_cmoo(dentry);
12479+ if (!err)
12480+ err = args->open(file, vfsub_file_flags(file),
12481+ args->h_file);
5afbbe0d 12482+ if (!err && au_fbtop(file) != au_dbtop(dentry))
b912730e
AM
12483+ /*
12484+ * cmoo happens after h_file was opened.
12485+ * need to refresh file later.
12486+ */
12487+ atomic_dec(&au_fi(file)->fi_generation);
12488+ }
1facf9fc 12489+
076b876e
AM
12490+ finfo = au_fi(file);
12491+ if (!err) {
12492+ finfo->fi_file = file;
12493+ au_sphl_add(&finfo->fi_hlist,
2000de60 12494+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 12495+ }
b912730e
AM
12496+ if (!no_lock)
12497+ fi_write_unlock(file);
12498+ else {
12499+ lockdep_off();
12500+ fi_write_unlock(file);
12501+ lockdep_on();
12502+ }
4a4d8108 12503+ if (unlikely(err)) {
076b876e 12504+ finfo->fi_hdir = NULL;
f0c0a007 12505+ au_finfo_fin(file, /*atonce*/0);
1308ab2a 12506+ }
4a4d8108 12507+
4f0767ce 12508+out:
1308ab2a 12509+ return err;
12510+}
dece6358 12511+
4a4d8108 12512+int au_reopen_nondir(struct file *file)
1308ab2a 12513+{
4a4d8108 12514+ int err;
5afbbe0d 12515+ aufs_bindex_t btop;
4a4d8108
AM
12516+ struct dentry *dentry;
12517+ struct file *h_file, *h_file_tmp;
1308ab2a 12518+
2000de60 12519+ dentry = file->f_path.dentry;
5afbbe0d 12520+ btop = au_dbtop(dentry);
4a4d8108 12521+ h_file_tmp = NULL;
5afbbe0d 12522+ if (au_fbtop(file) == btop) {
4a4d8108
AM
12523+ h_file = au_hf_top(file);
12524+ if (file->f_mode == h_file->f_mode)
12525+ return 0; /* success */
12526+ h_file_tmp = h_file;
12527+ get_file(h_file_tmp);
5afbbe0d 12528+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
12529+ }
12530+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
12531+ /*
12532+ * it can happen
12533+ * file exists on both of rw and ro
5afbbe0d 12534+ * open --> dbtop and fbtop are both 0
86dc4139
AM
12535+ * prepend a branch as rw, "rw" become ro
12536+ * remove rw/file
12537+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
12538+ * --> dbtop is 1, fbtop is still 0
12539+ * write --> fbtop is 0 but dbtop is 1
86dc4139 12540+ */
5afbbe0d 12541+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 12542+
5afbbe0d 12543+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 12544+ file, /*force_wr*/0);
4a4d8108 12545+ err = PTR_ERR(h_file);
86dc4139
AM
12546+ if (IS_ERR(h_file)) {
12547+ if (h_file_tmp) {
5afbbe0d
AM
12548+ au_sbr_get(dentry->d_sb, btop);
12549+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
12550+ h_file_tmp = NULL;
12551+ }
4a4d8108 12552+ goto out; /* todo: close all? */
86dc4139 12553+ }
4a4d8108
AM
12554+
12555+ err = 0;
5afbbe0d
AM
12556+ au_set_fbtop(file, btop);
12557+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
12558+ au_update_figen(file);
12559+ /* todo: necessary? */
12560+ /* file->f_ra = h_file->f_ra; */
12561+
4f0767ce 12562+out:
4a4d8108
AM
12563+ if (h_file_tmp)
12564+ fput(h_file_tmp);
12565+ return err;
1facf9fc 12566+}
12567+
1308ab2a 12568+/* ---------------------------------------------------------------------- */
12569+
4a4d8108
AM
12570+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12571+ struct dentry *hi_wh)
1facf9fc 12572+{
4a4d8108 12573+ int err;
5afbbe0d 12574+ aufs_bindex_t btop;
4a4d8108
AM
12575+ struct au_dinfo *dinfo;
12576+ struct dentry *h_dentry;
12577+ struct au_hdentry *hdp;
1facf9fc 12578+
2000de60 12579+ dinfo = au_di(file->f_path.dentry);
4a4d8108 12580+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 12581+
5afbbe0d
AM
12582+ btop = dinfo->di_btop;
12583+ dinfo->di_btop = btgt;
12584+ hdp = au_hdentry(dinfo, btgt);
12585+ h_dentry = hdp->hd_dentry;
12586+ hdp->hd_dentry = hi_wh;
4a4d8108 12587+ err = au_reopen_nondir(file);
5afbbe0d
AM
12588+ hdp->hd_dentry = h_dentry;
12589+ dinfo->di_btop = btop;
1facf9fc 12590+
1facf9fc 12591+ return err;
12592+}
12593+
4a4d8108 12594+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 12595+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 12596+{
4a4d8108 12597+ int err;
027c5e7a 12598+ struct inode *inode, *h_inode;
c2b27bf2
AM
12599+ struct dentry *h_dentry, *hi_wh;
12600+ struct au_cp_generic cpg = {
2000de60 12601+ .dentry = file->f_path.dentry,
c2b27bf2
AM
12602+ .bdst = bcpup,
12603+ .bsrc = -1,
12604+ .len = len,
12605+ .pin = pin
12606+ };
1facf9fc 12607+
5afbbe0d 12608+ au_update_dbtop(cpg.dentry);
5527c038 12609+ inode = d_inode(cpg.dentry);
027c5e7a 12610+ h_inode = NULL;
5afbbe0d
AM
12611+ if (au_dbtop(cpg.dentry) <= bcpup
12612+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 12613+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
12614+ if (h_dentry && d_is_positive(h_dentry))
12615+ h_inode = d_inode(h_dentry);
027c5e7a 12616+ }
4a4d8108 12617+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 12618+ if (!hi_wh && !h_inode)
c2b27bf2 12619+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
12620+ else
12621+ /* already copied-up after unlink */
12622+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 12623+
4a4d8108 12624+ if (!err
38d290e6
JR
12625+ && (inode->i_nlink > 1
12626+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
12627+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12628+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 12629+
dece6358 12630+ return err;
1facf9fc 12631+}
12632+
4a4d8108
AM
12633+/*
12634+ * prepare the @file for writing.
12635+ */
12636+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 12637+{
4a4d8108 12638+ int err;
5afbbe0d 12639+ aufs_bindex_t dbtop;
c1595e42 12640+ struct dentry *parent;
86dc4139 12641+ struct inode *inode;
1facf9fc 12642+ struct super_block *sb;
4a4d8108 12643+ struct file *h_file;
c2b27bf2 12644+ struct au_cp_generic cpg = {
2000de60 12645+ .dentry = file->f_path.dentry,
c2b27bf2
AM
12646+ .bdst = -1,
12647+ .bsrc = -1,
12648+ .len = len,
12649+ .pin = pin,
12650+ .flags = AuCpup_DTIME
12651+ };
1facf9fc 12652+
c2b27bf2 12653+ sb = cpg.dentry->d_sb;
5527c038 12654+ inode = d_inode(cpg.dentry);
5afbbe0d 12655+ cpg.bsrc = au_fbtop(file);
c2b27bf2 12656+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 12657+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
12658+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12659+ /*flags*/0);
1facf9fc 12660+ goto out;
4a4d8108 12661+ }
1facf9fc 12662+
027c5e7a 12663+ /* need to cpup or reopen */
c2b27bf2 12664+ parent = dget_parent(cpg.dentry);
4a4d8108 12665+ di_write_lock_parent(parent);
c2b27bf2
AM
12666+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12667+ cpg.bdst = err;
4a4d8108
AM
12668+ if (unlikely(err < 0))
12669+ goto out_dgrade;
12670+ err = 0;
12671+
c2b27bf2
AM
12672+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12673+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 12674+ if (unlikely(err))
4a4d8108
AM
12675+ goto out_dgrade;
12676+ }
12677+
c2b27bf2 12678+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
12679+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12680+ if (unlikely(err))
12681+ goto out_dgrade;
12682+
5afbbe0d
AM
12683+ dbtop = au_dbtop(cpg.dentry);
12684+ if (dbtop <= cpg.bdst)
c2b27bf2 12685+ cpg.bsrc = cpg.bdst;
027c5e7a 12686+
5afbbe0d 12687+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 12688+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 12689+ ) {
392086de 12690+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 12691+ if (IS_ERR(h_file))
027c5e7a 12692+ err = PTR_ERR(h_file);
86dc4139 12693+ else {
027c5e7a 12694+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 12695+ if (dbtop > cpg.bdst)
c2b27bf2 12696+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
12697+ if (!err)
12698+ err = au_reopen_nondir(file);
c2b27bf2 12699+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 12700+ }
027c5e7a
AM
12701+ } else { /* copyup as wh and reopen */
12702+ /*
12703+ * since writable hfsplus branch is not supported,
12704+ * h_open_pre/post() are unnecessary.
12705+ */
c2b27bf2 12706+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 12707+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 12708+ }
4a4d8108
AM
12709+
12710+ if (!err) {
12711+ au_pin_set_parent_lflag(pin, /*lflag*/0);
12712+ goto out_dput; /* success */
12713+ }
12714+ au_unpin(pin);
12715+ goto out_unlock;
1facf9fc 12716+
4f0767ce 12717+out_dgrade:
4a4d8108 12718+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 12719+out_unlock:
4a4d8108 12720+ di_read_unlock(parent, AuLock_IR);
4f0767ce 12721+out_dput:
4a4d8108 12722+ dput(parent);
4f0767ce 12723+out:
1facf9fc 12724+ return err;
12725+}
12726+
4a4d8108
AM
12727+/* ---------------------------------------------------------------------- */
12728+
12729+int au_do_flush(struct file *file, fl_owner_t id,
12730+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 12731+{
4a4d8108 12732+ int err;
1facf9fc 12733+ struct super_block *sb;
4a4d8108 12734+ struct inode *inode;
1facf9fc 12735+
c06a8ce3
AM
12736+ inode = file_inode(file);
12737+ sb = inode->i_sb;
4a4d8108
AM
12738+ si_noflush_read_lock(sb);
12739+ fi_read_lock(file);
b752ccd1 12740+ ii_read_lock_child(inode);
1facf9fc 12741+
4a4d8108
AM
12742+ err = flush(file, id);
12743+ au_cpup_attr_timesizes(inode);
1facf9fc 12744+
b752ccd1 12745+ ii_read_unlock(inode);
4a4d8108 12746+ fi_read_unlock(file);
1308ab2a 12747+ si_read_unlock(sb);
dece6358 12748+ return err;
1facf9fc 12749+}
12750+
4a4d8108
AM
12751+/* ---------------------------------------------------------------------- */
12752+
12753+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 12754+{
4a4d8108 12755+ int err;
4a4d8108
AM
12756+ struct au_pin pin;
12757+ struct au_finfo *finfo;
c2b27bf2 12758+ struct dentry *parent, *hi_wh;
4a4d8108 12759+ struct inode *inode;
1facf9fc 12760+ struct super_block *sb;
c2b27bf2 12761+ struct au_cp_generic cpg = {
2000de60 12762+ .dentry = file->f_path.dentry,
c2b27bf2
AM
12763+ .bdst = -1,
12764+ .bsrc = -1,
12765+ .len = -1,
12766+ .pin = &pin,
12767+ .flags = AuCpup_DTIME
12768+ };
1facf9fc 12769+
4a4d8108
AM
12770+ FiMustWriteLock(file);
12771+
12772+ err = 0;
12773+ finfo = au_fi(file);
c2b27bf2 12774+ sb = cpg.dentry->d_sb;
5527c038 12775+ inode = d_inode(cpg.dentry);
5afbbe0d 12776+ cpg.bdst = au_ibtop(inode);
c2b27bf2 12777+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 12778+ goto out;
dece6358 12779+
c2b27bf2
AM
12780+ parent = dget_parent(cpg.dentry);
12781+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 12782+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
12783+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12784+ cpg.bdst = err;
4a4d8108
AM
12785+ di_read_unlock(parent, !AuLock_IR);
12786+ if (unlikely(err < 0))
12787+ goto out_parent;
12788+ err = 0;
1facf9fc 12789+ }
1facf9fc 12790+
4a4d8108 12791+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 12792+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
12793+ if (!S_ISDIR(inode->i_mode)
12794+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 12795+ && au_plink_test(inode)
c2b27bf2 12796+ && !d_unhashed(cpg.dentry)
5afbbe0d 12797+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 12798+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
12799+ if (unlikely(err))
12800+ goto out_unlock;
12801+
12802+ /* always superio. */
c2b27bf2 12803+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 12804+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 12805+ if (!err) {
c2b27bf2 12806+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
12807+ au_unpin(&pin);
12808+ }
4a4d8108
AM
12809+ } else if (hi_wh) {
12810+ /* already copied-up after unlink */
c2b27bf2 12811+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
12812+ *need_reopen = 0;
12813+ }
1facf9fc 12814+
4f0767ce 12815+out_unlock:
4a4d8108 12816+ di_read_unlock(parent, AuLock_IR);
4f0767ce 12817+out_parent:
4a4d8108 12818+ dput(parent);
4f0767ce 12819+out:
1308ab2a 12820+ return err;
dece6358 12821+}
1facf9fc 12822+
4a4d8108 12823+static void au_do_refresh_dir(struct file *file)
dece6358 12824+{
f0c0a007 12825+ int execed;
5afbbe0d 12826+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
12827+ struct au_hfile *p, tmp, *q;
12828+ struct au_finfo *finfo;
1308ab2a 12829+ struct super_block *sb;
4a4d8108 12830+ struct au_fidir *fidir;
1facf9fc 12831+
4a4d8108 12832+ FiMustWriteLock(file);
1facf9fc 12833+
2000de60 12834+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
12835+ finfo = au_fi(file);
12836+ fidir = finfo->fi_hdir;
12837+ AuDebugOn(!fidir);
12838+ p = fidir->fd_hfile + finfo->fi_btop;
12839+ brid = p->hf_br->br_id;
5afbbe0d
AM
12840+ bbot = fidir->fd_bbot;
12841+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
12842+ if (!p->hf_file)
12843+ continue;
1308ab2a 12844+
4a4d8108
AM
12845+ new_bindex = au_br_index(sb, p->hf_br->br_id);
12846+ if (new_bindex == bindex)
12847+ continue;
12848+ if (new_bindex < 0) {
12849+ au_set_h_fptr(file, bindex, NULL);
12850+ continue;
12851+ }
1308ab2a 12852+
4a4d8108
AM
12853+ /* swap two lower inode, and loop again */
12854+ q = fidir->fd_hfile + new_bindex;
12855+ tmp = *q;
12856+ *q = *p;
12857+ *p = tmp;
12858+ if (tmp.hf_file) {
12859+ bindex--;
12860+ p--;
12861+ }
12862+ }
1308ab2a 12863+
f0c0a007 12864+ execed = vfsub_file_execed(file);
4a4d8108 12865+ p = fidir->fd_hfile;
2000de60 12866+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
12867+ bbot = au_sbbot(sb);
12868+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
12869+ finfo->fi_btop++, p++)
12870+ if (p->hf_file) {
c06a8ce3 12871+ if (file_inode(p->hf_file))
4a4d8108 12872+ break;
f0c0a007 12873+ au_hfput(p, execed);
4a4d8108
AM
12874+ }
12875+ } else {
5afbbe0d
AM
12876+ bbot = au_br_index(sb, brid);
12877+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
12878+ finfo->fi_btop++, p++)
12879+ if (p->hf_file)
f0c0a007 12880+ au_hfput(p, execed);
5afbbe0d 12881+ bbot = au_sbbot(sb);
4a4d8108 12882+ }
1308ab2a 12883+
5afbbe0d
AM
12884+ p = fidir->fd_hfile + bbot;
12885+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
12886+ fidir->fd_bbot--, p--)
12887+ if (p->hf_file) {
c06a8ce3 12888+ if (file_inode(p->hf_file))
4a4d8108 12889+ break;
f0c0a007 12890+ au_hfput(p, execed);
4a4d8108
AM
12891+ }
12892+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 12893+}
12894+
4a4d8108
AM
12895+/*
12896+ * after branch manipulating, refresh the file.
12897+ */
12898+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 12899+{
4a4d8108 12900+ int err, need_reopen;
5afbbe0d 12901+ aufs_bindex_t bbot, bindex;
4a4d8108 12902+ struct dentry *dentry;
1308ab2a 12903+ struct au_finfo *finfo;
4a4d8108 12904+ struct au_hfile *hfile;
1facf9fc 12905+
2000de60 12906+ dentry = file->f_path.dentry;
1308ab2a 12907+ finfo = au_fi(file);
4a4d8108
AM
12908+ if (!finfo->fi_hdir) {
12909+ hfile = &finfo->fi_htop;
12910+ AuDebugOn(!hfile->hf_file);
12911+ bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12912+ AuDebugOn(bindex < 0);
12913+ if (bindex != finfo->fi_btop)
5afbbe0d 12914+ au_set_fbtop(file, bindex);
4a4d8108 12915+ } else {
5afbbe0d 12916+ err = au_fidir_realloc(finfo, au_sbbot(dentry->d_sb) + 1);
4a4d8108
AM
12917+ if (unlikely(err))
12918+ goto out;
12919+ au_do_refresh_dir(file);
12920+ }
1facf9fc 12921+
4a4d8108
AM
12922+ err = 0;
12923+ need_reopen = 1;
12924+ if (!au_test_mmapped(file))
12925+ err = au_file_refresh_by_inode(file, &need_reopen);
027c5e7a 12926+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
12927+ err = reopen(file);
12928+ if (!err) {
12929+ au_update_figen(file);
12930+ goto out; /* success */
12931+ }
12932+
12933+ /* error, close all lower files */
12934+ if (finfo->fi_hdir) {
5afbbe0d
AM
12935+ bbot = au_fbbot_dir(file);
12936+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
12937+ au_set_h_fptr(file, bindex, NULL);
12938+ }
1facf9fc 12939+
4f0767ce 12940+out:
1facf9fc 12941+ return err;
12942+}
12943+
4a4d8108
AM
12944+/* common function to regular file and dir */
12945+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12946+ int wlock)
dece6358 12947+{
1308ab2a 12948+ int err;
4a4d8108 12949+ unsigned int sigen, figen;
5afbbe0d 12950+ aufs_bindex_t btop;
4a4d8108
AM
12951+ unsigned char pseudo_link;
12952+ struct dentry *dentry;
12953+ struct inode *inode;
1facf9fc 12954+
4a4d8108 12955+ err = 0;
2000de60 12956+ dentry = file->f_path.dentry;
5527c038 12957+ inode = d_inode(dentry);
4a4d8108
AM
12958+ sigen = au_sigen(dentry->d_sb);
12959+ fi_write_lock(file);
12960+ figen = au_figen(file);
12961+ di_write_lock_child(dentry);
5afbbe0d
AM
12962+ btop = au_dbtop(dentry);
12963+ pseudo_link = (btop != au_ibtop(inode));
12964+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
12965+ if (!wlock) {
12966+ di_downgrade_lock(dentry, AuLock_IR);
12967+ fi_downgrade_lock(file);
12968+ }
12969+ goto out; /* success */
12970+ }
dece6358 12971+
4a4d8108 12972+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 12973+ if (au_digen_test(dentry, sigen)) {
4a4d8108 12974+ err = au_reval_dpath(dentry, sigen);
027c5e7a 12975+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 12976+ }
dece6358 12977+
027c5e7a
AM
12978+ if (!err)
12979+ err = refresh_file(file, reopen);
4a4d8108
AM
12980+ if (!err) {
12981+ if (!wlock) {
12982+ di_downgrade_lock(dentry, AuLock_IR);
12983+ fi_downgrade_lock(file);
12984+ }
12985+ } else {
12986+ di_write_unlock(dentry);
12987+ fi_write_unlock(file);
12988+ }
1facf9fc 12989+
4f0767ce 12990+out:
1308ab2a 12991+ return err;
12992+}
1facf9fc 12993+
4a4d8108
AM
12994+/* ---------------------------------------------------------------------- */
12995+
12996+/* cf. aufs_nopage() */
12997+/* for madvise(2) */
12998+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 12999+{
4a4d8108
AM
13000+ unlock_page(page);
13001+ return 0;
13002+}
1facf9fc 13003+
4a4d8108 13004+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 13005+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 13006+{ BUG(); return 0; }
1facf9fc 13007+
4a4d8108
AM
13008+/* they will never be called. */
13009+#ifdef CONFIG_AUFS_DEBUG
13010+static int aufs_write_begin(struct file *file, struct address_space *mapping,
13011+ loff_t pos, unsigned len, unsigned flags,
13012+ struct page **pagep, void **fsdata)
13013+{ AuUnsupport(); return 0; }
13014+static int aufs_write_end(struct file *file, struct address_space *mapping,
13015+ loff_t pos, unsigned len, unsigned copied,
13016+ struct page *page, void *fsdata)
13017+{ AuUnsupport(); return 0; }
13018+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
13019+{ AuUnsupport(); return 0; }
1308ab2a 13020+
4a4d8108
AM
13021+static int aufs_set_page_dirty(struct page *page)
13022+{ AuUnsupport(); return 0; }
392086de
AM
13023+static void aufs_invalidatepage(struct page *page, unsigned int offset,
13024+ unsigned int length)
4a4d8108
AM
13025+{ AuUnsupport(); }
13026+static int aufs_releasepage(struct page *page, gfp_t gfp)
13027+{ AuUnsupport(); return 0; }
79b8bda9 13028+#if 0 /* called by memory compaction regardless file */
4a4d8108 13029+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 13030+ struct page *page, enum migrate_mode mode)
4a4d8108 13031+{ AuUnsupport(); return 0; }
79b8bda9 13032+#endif
4a4d8108
AM
13033+static int aufs_launder_page(struct page *page)
13034+{ AuUnsupport(); return 0; }
13035+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
13036+ unsigned long from,
13037+ unsigned long count)
4a4d8108 13038+{ AuUnsupport(); return 0; }
392086de
AM
13039+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
13040+ bool *writeback)
13041+{ AuUnsupport(); }
4a4d8108
AM
13042+static int aufs_error_remove_page(struct address_space *mapping,
13043+ struct page *page)
13044+{ AuUnsupport(); return 0; }
b4510431
AM
13045+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
13046+ sector_t *span)
13047+{ AuUnsupport(); return 0; }
13048+static void aufs_swap_deactivate(struct file *file)
13049+{ AuUnsupport(); }
4a4d8108
AM
13050+#endif /* CONFIG_AUFS_DEBUG */
13051+
13052+const struct address_space_operations aufs_aop = {
13053+ .readpage = aufs_readpage,
13054+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
13055+#ifdef CONFIG_AUFS_DEBUG
13056+ .writepage = aufs_writepage,
4a4d8108
AM
13057+ /* no writepages, because of writepage */
13058+ .set_page_dirty = aufs_set_page_dirty,
13059+ /* no readpages, because of readpage */
13060+ .write_begin = aufs_write_begin,
13061+ .write_end = aufs_write_end,
13062+ /* no bmap, no block device */
13063+ .invalidatepage = aufs_invalidatepage,
13064+ .releasepage = aufs_releasepage,
79b8bda9
AM
13065+ /* is fallback_migrate_page ok? */
13066+ /* .migratepage = aufs_migratepage, */
4a4d8108
AM
13067+ .launder_page = aufs_launder_page,
13068+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 13069+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
13070+ .error_remove_page = aufs_error_remove_page,
13071+ .swap_activate = aufs_swap_activate,
13072+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 13073+#endif /* CONFIG_AUFS_DEBUG */
dece6358 13074+};
7f207e10
AM
13075diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
13076--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
13077+++ linux/fs/aufs/file.h 2016-08-17 18:01:06.135222590 +0200
13078@@ -0,0 +1,294 @@
4a4d8108 13079+/*
8cdd5066 13080+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
13081+ *
13082+ * This program, aufs is free software; you can redistribute it and/or modify
13083+ * it under the terms of the GNU General Public License as published by
13084+ * the Free Software Foundation; either version 2 of the License, or
13085+ * (at your option) any later version.
13086+ *
13087+ * This program is distributed in the hope that it will be useful,
13088+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13089+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13090+ * GNU General Public License for more details.
13091+ *
13092+ * You should have received a copy of the GNU General Public License
523b37e3 13093+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 13094+ */
1facf9fc 13095+
4a4d8108
AM
13096+/*
13097+ * file operations
13098+ */
1facf9fc 13099+
4a4d8108
AM
13100+#ifndef __AUFS_FILE_H__
13101+#define __AUFS_FILE_H__
1facf9fc 13102+
4a4d8108 13103+#ifdef __KERNEL__
1facf9fc 13104+
2cbb1c4b 13105+#include <linux/file.h>
4a4d8108
AM
13106+#include <linux/fs.h>
13107+#include <linux/poll.h>
4a4d8108 13108+#include "rwsem.h"
1facf9fc 13109+
4a4d8108
AM
13110+struct au_branch;
13111+struct au_hfile {
13112+ struct file *hf_file;
13113+ struct au_branch *hf_br;
13114+};
1facf9fc 13115+
4a4d8108
AM
13116+struct au_vdir;
13117+struct au_fidir {
13118+ aufs_bindex_t fd_bbot;
13119+ aufs_bindex_t fd_nent;
13120+ struct au_vdir *fd_vdir_cache;
13121+ struct au_hfile fd_hfile[];
13122+};
1facf9fc 13123+
4a4d8108 13124+static inline int au_fidir_sz(int nent)
dece6358 13125+{
4f0767ce
JR
13126+ AuDebugOn(nent < 0);
13127+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 13128+}
1facf9fc 13129+
4a4d8108
AM
13130+struct au_finfo {
13131+ atomic_t fi_generation;
dece6358 13132+
4a4d8108
AM
13133+ struct au_rwsem fi_rwsem;
13134+ aufs_bindex_t fi_btop;
13135+
13136+ /* do not union them */
13137+ struct { /* for non-dir */
13138+ struct au_hfile fi_htop;
2cbb1c4b 13139+ atomic_t fi_mmapped;
4a4d8108
AM
13140+ };
13141+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3
AM
13142+
13143+ struct hlist_node fi_hlist;
f0c0a007
AM
13144+ union {
13145+ struct file *fi_file; /* very ugly */
13146+ struct llist_node fi_lnode; /* delayed free */
13147+ };
4a4d8108 13148+} ____cacheline_aligned_in_smp;
1facf9fc 13149+
4a4d8108 13150+/* ---------------------------------------------------------------------- */
1facf9fc 13151+
4a4d8108
AM
13152+/* file.c */
13153+extern const struct address_space_operations aufs_aop;
13154+unsigned int au_file_roflags(unsigned int flags);
13155+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 13156+ struct file *file, int force_wr);
b912730e
AM
13157+struct au_do_open_args {
13158+ int no_lock;
13159+ int (*open)(struct file *file, int flags,
13160+ struct file *h_file);
13161+ struct au_fidir *fidir;
13162+ struct file *h_file;
13163+};
13164+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
13165+int au_reopen_nondir(struct file *file);
13166+struct au_pin;
13167+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
13168+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
13169+ int wlock);
13170+int au_do_flush(struct file *file, fl_owner_t id,
13171+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 13172+
4a4d8108
AM
13173+/* poll.c */
13174+#ifdef CONFIG_AUFS_POLL
13175+unsigned int aufs_poll(struct file *file, poll_table *wait);
13176+#endif
1facf9fc 13177+
4a4d8108
AM
13178+#ifdef CONFIG_AUFS_BR_HFSPLUS
13179+/* hfsplus.c */
392086de
AM
13180+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
13181+ int force_wr);
4a4d8108
AM
13182+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13183+ struct file *h_file);
13184+#else
c1595e42
JR
13185+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
13186+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
13187+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
13188+ struct file *h_file);
13189+#endif
1facf9fc 13190+
4a4d8108
AM
13191+/* f_op.c */
13192+extern const struct file_operations aufs_file_fop;
b912730e 13193+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 13194+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
b912730e 13195+struct file *au_read_pre(struct file *file, int keep_fi);
4a4d8108 13196+
4a4d8108 13197+/* finfo.c */
f0c0a007 13198+void au_hfput(struct au_hfile *hf, int execed);
4a4d8108
AM
13199+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
13200+ struct file *h_file);
1facf9fc 13201+
4a4d8108 13202+void au_update_figen(struct file *file);
4a4d8108
AM
13203+struct au_fidir *au_fidir_alloc(struct super_block *sb);
13204+int au_fidir_realloc(struct au_finfo *finfo, int nbr);
1facf9fc 13205+
4a4d8108 13206+void au_fi_init_once(void *_fi);
f0c0a007 13207+void au_finfo_fin(struct file *file, int atonce);
4a4d8108 13208+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 13209+
4a4d8108
AM
13210+/* ioctl.c */
13211+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
13212+#ifdef CONFIG_COMPAT
13213+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
13214+ unsigned long arg);
c2b27bf2
AM
13215+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
13216+ unsigned long arg);
b752ccd1 13217+#endif
1facf9fc 13218+
4a4d8108 13219+/* ---------------------------------------------------------------------- */
1facf9fc 13220+
4a4d8108
AM
13221+static inline struct au_finfo *au_fi(struct file *file)
13222+{
38d290e6 13223+ return file->private_data;
4a4d8108 13224+}
1facf9fc 13225+
4a4d8108 13226+/* ---------------------------------------------------------------------- */
1facf9fc 13227+
4a4d8108
AM
13228+/*
13229+ * fi_read_lock, fi_write_lock,
13230+ * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
13231+ */
13232+AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
1308ab2a 13233+
4a4d8108
AM
13234+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
13235+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
13236+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 13237+
1308ab2a 13238+/* ---------------------------------------------------------------------- */
13239+
4a4d8108 13240+/* todo: hard/soft set? */
5afbbe0d 13241+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 13242+{
4a4d8108
AM
13243+ FiMustAnyLock(file);
13244+ return au_fi(file)->fi_btop;
13245+}
dece6358 13246+
5afbbe0d 13247+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
13248+{
13249+ FiMustAnyLock(file);
13250+ AuDebugOn(!au_fi(file)->fi_hdir);
13251+ return au_fi(file)->fi_hdir->fd_bbot;
13252+}
1facf9fc 13253+
4a4d8108
AM
13254+static inline struct au_vdir *au_fvdir_cache(struct file *file)
13255+{
13256+ FiMustAnyLock(file);
13257+ AuDebugOn(!au_fi(file)->fi_hdir);
13258+ return au_fi(file)->fi_hdir->fd_vdir_cache;
13259+}
1facf9fc 13260+
5afbbe0d 13261+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
13262+{
13263+ FiMustWriteLock(file);
13264+ au_fi(file)->fi_btop = bindex;
13265+}
1facf9fc 13266+
5afbbe0d 13267+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
13268+{
13269+ FiMustWriteLock(file);
13270+ AuDebugOn(!au_fi(file)->fi_hdir);
13271+ au_fi(file)->fi_hdir->fd_bbot = bindex;
13272+}
1308ab2a 13273+
4a4d8108
AM
13274+static inline void au_set_fvdir_cache(struct file *file,
13275+ struct au_vdir *vdir_cache)
13276+{
13277+ FiMustWriteLock(file);
13278+ AuDebugOn(!au_fi(file)->fi_hdir);
13279+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
13280+}
dece6358 13281+
4a4d8108
AM
13282+static inline struct file *au_hf_top(struct file *file)
13283+{
13284+ FiMustAnyLock(file);
13285+ AuDebugOn(au_fi(file)->fi_hdir);
13286+ return au_fi(file)->fi_htop.hf_file;
13287+}
1facf9fc 13288+
4a4d8108
AM
13289+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13290+{
13291+ FiMustAnyLock(file);
13292+ AuDebugOn(!au_fi(file)->fi_hdir);
13293+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
13294+}
13295+
4a4d8108
AM
13296+/* todo: memory barrier? */
13297+static inline unsigned int au_figen(struct file *f)
dece6358 13298+{
4a4d8108
AM
13299+ return atomic_read(&au_fi(f)->fi_generation);
13300+}
dece6358 13301+
2cbb1c4b
JR
13302+static inline void au_set_mmapped(struct file *f)
13303+{
13304+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13305+ return;
0c3ec466 13306+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
13307+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13308+ ;
13309+}
13310+
13311+static inline void au_unset_mmapped(struct file *f)
13312+{
13313+ atomic_dec(&au_fi(f)->fi_mmapped);
13314+}
13315+
4a4d8108
AM
13316+static inline int au_test_mmapped(struct file *f)
13317+{
2cbb1c4b
JR
13318+ return atomic_read(&au_fi(f)->fi_mmapped);
13319+}
13320+
13321+/* customize vma->vm_file */
13322+
13323+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13324+ struct file *file)
13325+{
53392da6
AM
13326+ struct file *f;
13327+
13328+ f = vma->vm_file;
2cbb1c4b
JR
13329+ get_file(file);
13330+ vma->vm_file = file;
53392da6 13331+ fput(f);
2cbb1c4b
JR
13332+}
13333+
13334+#ifdef CONFIG_MMU
13335+#define AuDbgVmRegion(file, vma) do {} while (0)
13336+
13337+static inline void au_vm_file_reset(struct vm_area_struct *vma,
13338+ struct file *file)
13339+{
13340+ au_do_vm_file_reset(vma, file);
13341+}
13342+#else
13343+#define AuDbgVmRegion(file, vma) \
13344+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13345+
13346+static inline void au_vm_file_reset(struct vm_area_struct *vma,
13347+ struct file *file)
13348+{
53392da6
AM
13349+ struct file *f;
13350+
2cbb1c4b 13351+ au_do_vm_file_reset(vma, file);
53392da6 13352+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
13353+ get_file(file);
13354+ vma->vm_region->vm_file = file;
53392da6 13355+ fput(f);
2cbb1c4b
JR
13356+}
13357+#endif /* CONFIG_MMU */
13358+
13359+/* handle vma->vm_prfile */
fb47a38f 13360+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
13361+ struct file *file)
13362+{
2cbb1c4b
JR
13363+ get_file(file);
13364+ vma->vm_prfile = file;
13365+#ifndef CONFIG_MMU
13366+ get_file(file);
13367+ vma->vm_region->vm_prfile = file;
13368+#endif
fb47a38f 13369+}
1308ab2a 13370+
4a4d8108
AM
13371+#endif /* __KERNEL__ */
13372+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
13373diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13374--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
13375+++ linux/fs/aufs/finfo.c 2016-08-17 18:01:06.135222590 +0200
13376@@ -0,0 +1,151 @@
4a4d8108 13377+/*
8cdd5066 13378+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
13379+ *
13380+ * This program, aufs is free software; you can redistribute it and/or modify
13381+ * it under the terms of the GNU General Public License as published by
13382+ * the Free Software Foundation; either version 2 of the License, or
13383+ * (at your option) any later version.
13384+ *
13385+ * This program is distributed in the hope that it will be useful,
13386+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13387+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13388+ * GNU General Public License for more details.
13389+ *
13390+ * You should have received a copy of the GNU General Public License
523b37e3 13391+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 13392+ */
1308ab2a 13393+
4a4d8108
AM
13394+/*
13395+ * file private data
13396+ */
1facf9fc 13397+
4a4d8108 13398+#include "aufs.h"
1facf9fc 13399+
f0c0a007 13400+void au_hfput(struct au_hfile *hf, int execed)
4a4d8108 13401+{
f0c0a007 13402+ if (execed)
4a4d8108
AM
13403+ allow_write_access(hf->hf_file);
13404+ fput(hf->hf_file);
13405+ hf->hf_file = NULL;
5afbbe0d 13406+ au_br_put(hf->hf_br);
4a4d8108
AM
13407+ hf->hf_br = NULL;
13408+}
1facf9fc 13409+
4a4d8108
AM
13410+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13411+{
13412+ struct au_finfo *finfo = au_fi(file);
13413+ struct au_hfile *hf;
13414+ struct au_fidir *fidir;
13415+
13416+ fidir = finfo->fi_hdir;
13417+ if (!fidir) {
13418+ AuDebugOn(finfo->fi_btop != bindex);
13419+ hf = &finfo->fi_htop;
13420+ } else
13421+ hf = fidir->fd_hfile + bindex;
13422+
13423+ if (hf && hf->hf_file)
f0c0a007 13424+ au_hfput(hf, vfsub_file_execed(file));
4a4d8108
AM
13425+ if (val) {
13426+ FiMustWriteLock(file);
b912730e 13427+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 13428+ hf->hf_file = val;
2000de60 13429+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 13430+ }
4a4d8108 13431+}
1facf9fc 13432+
4a4d8108
AM
13433+void au_update_figen(struct file *file)
13434+{
2000de60 13435+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 13436+ /* smp_mb(); */ /* atomic_set */
1facf9fc 13437+}
13438+
4a4d8108
AM
13439+/* ---------------------------------------------------------------------- */
13440+
4a4d8108
AM
13441+struct au_fidir *au_fidir_alloc(struct super_block *sb)
13442+{
13443+ struct au_fidir *fidir;
13444+ int nbr;
13445+
5afbbe0d 13446+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
13447+ if (nbr < 2)
13448+ nbr = 2; /* initial allocate for 2 branches */
13449+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13450+ if (fidir) {
13451+ fidir->fd_bbot = -1;
13452+ fidir->fd_nent = nbr;
4a4d8108
AM
13453+ }
13454+
13455+ return fidir;
13456+}
13457+
13458+int au_fidir_realloc(struct au_finfo *finfo, int nbr)
13459+{
13460+ int err;
13461+ struct au_fidir *fidir, *p;
13462+
13463+ AuRwMustWriteLock(&finfo->fi_rwsem);
13464+ fidir = finfo->fi_hdir;
13465+ AuDebugOn(!fidir);
13466+
13467+ err = -ENOMEM;
13468+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13469+ GFP_NOFS);
13470+ if (p) {
13471+ p->fd_nent = nbr;
13472+ finfo->fi_hdir = p;
13473+ err = 0;
13474+ }
1facf9fc 13475+
dece6358 13476+ return err;
1facf9fc 13477+}
1308ab2a 13478+
13479+/* ---------------------------------------------------------------------- */
13480+
f0c0a007 13481+void au_finfo_fin(struct file *file, int atonce)
1308ab2a 13482+{
4a4d8108
AM
13483+ struct au_finfo *finfo;
13484+
2000de60 13485+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 13486+
4a4d8108
AM
13487+ finfo = au_fi(file);
13488+ AuDebugOn(finfo->fi_hdir);
13489+ AuRwDestroy(&finfo->fi_rwsem);
f0c0a007
AM
13490+ if (!atonce)
13491+ au_cache_dfree_finfo(finfo);
13492+ else
13493+ au_cache_free_finfo(finfo);
1308ab2a 13494+}
1308ab2a 13495+
e49829fe 13496+void au_fi_init_once(void *_finfo)
4a4d8108 13497+{
e49829fe 13498+ struct au_finfo *finfo = _finfo;
1308ab2a 13499+
e49829fe 13500+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 13501+}
1308ab2a 13502+
4a4d8108
AM
13503+int au_finfo_init(struct file *file, struct au_fidir *fidir)
13504+{
1716fcea 13505+ int err;
4a4d8108
AM
13506+ struct au_finfo *finfo;
13507+ struct dentry *dentry;
13508+
13509+ err = -ENOMEM;
2000de60 13510+ dentry = file->f_path.dentry;
4a4d8108
AM
13511+ finfo = au_cache_alloc_finfo();
13512+ if (unlikely(!finfo))
13513+ goto out;
13514+
13515+ err = 0;
7f207e10 13516+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
13517+ au_rw_write_lock(&finfo->fi_rwsem);
13518+ finfo->fi_btop = -1;
13519+ finfo->fi_hdir = fidir;
13520+ atomic_set(&finfo->fi_generation, au_digen(dentry));
13521+ /* smp_mb(); */ /* atomic_set */
13522+
13523+ file->private_data = finfo;
13524+
13525+out:
13526+ return err;
13527+}
7f207e10
AM
13528diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13529--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
13530+++ linux/fs/aufs/f_op.c 2016-08-17 18:01:06.135222590 +0200
13531@@ -0,0 +1,772 @@
dece6358 13532+/*
8cdd5066 13533+ * Copyright (C) 2005-2016 Junjiro R. Okajima
dece6358
AM
13534+ *
13535+ * This program, aufs is free software; you can redistribute it and/or modify
13536+ * it under the terms of the GNU General Public License as published by
13537+ * the Free Software Foundation; either version 2 of the License, or
13538+ * (at your option) any later version.
13539+ *
13540+ * This program is distributed in the hope that it will be useful,
13541+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13542+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13543+ * GNU General Public License for more details.
13544+ *
13545+ * You should have received a copy of the GNU General Public License
523b37e3 13546+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 13547+ */
1facf9fc 13548+
13549+/*
4a4d8108 13550+ * file and vm operations
1facf9fc 13551+ */
dece6358 13552+
86dc4139 13553+#include <linux/aio.h>
4a4d8108
AM
13554+#include <linux/fs_stack.h>
13555+#include <linux/mman.h>
4a4d8108 13556+#include <linux/security.h>
dece6358
AM
13557+#include "aufs.h"
13558+
b912730e 13559+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 13560+{
4a4d8108
AM
13561+ int err;
13562+ aufs_bindex_t bindex;
8cdd5066 13563+ struct dentry *dentry, *h_dentry;
4a4d8108 13564+ struct au_finfo *finfo;
38d290e6 13565+ struct inode *h_inode;
4a4d8108
AM
13566+
13567+ FiMustWriteLock(file);
13568+
523b37e3 13569+ err = 0;
2000de60 13570+ dentry = file->f_path.dentry;
b912730e 13571+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
13572+ finfo = au_fi(file);
13573+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 13574+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 13575+ bindex = au_dbtop(dentry);
8cdd5066
JR
13576+ if (!h_file) {
13577+ h_dentry = au_h_dptr(dentry, bindex);
13578+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13579+ if (unlikely(err))
13580+ goto out;
b912730e 13581+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
13582+ } else {
13583+ h_dentry = h_file->f_path.dentry;
13584+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13585+ if (unlikely(err))
13586+ goto out;
b912730e 13587+ get_file(h_file);
8cdd5066 13588+ }
4a4d8108
AM
13589+ if (IS_ERR(h_file))
13590+ err = PTR_ERR(h_file);
13591+ else {
38d290e6
JR
13592+ if ((flags & __O_TMPFILE)
13593+ && !(flags & O_EXCL)) {
13594+ h_inode = file_inode(h_file);
13595+ spin_lock(&h_inode->i_lock);
13596+ h_inode->i_state |= I_LINKABLE;
13597+ spin_unlock(&h_inode->i_lock);
13598+ }
5afbbe0d 13599+ au_set_fbtop(file, bindex);
4a4d8108
AM
13600+ au_set_h_fptr(file, bindex, h_file);
13601+ au_update_figen(file);
13602+ /* todo: necessary? */
13603+ /* file->f_ra = h_file->f_ra; */
13604+ }
027c5e7a 13605+
8cdd5066 13606+out:
4a4d8108 13607+ return err;
1facf9fc 13608+}
13609+
4a4d8108
AM
13610+static int aufs_open_nondir(struct inode *inode __maybe_unused,
13611+ struct file *file)
1facf9fc 13612+{
4a4d8108 13613+ int err;
1308ab2a 13614+ struct super_block *sb;
b912730e
AM
13615+ struct au_do_open_args args = {
13616+ .open = au_do_open_nondir
13617+ };
1facf9fc 13618+
523b37e3
AM
13619+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13620+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 13621+
2000de60 13622+ sb = file->f_path.dentry->d_sb;
4a4d8108 13623+ si_read_lock(sb, AuLock_FLUSH);
b912730e 13624+ err = au_do_open(file, &args);
4a4d8108
AM
13625+ si_read_unlock(sb);
13626+ return err;
13627+}
1facf9fc 13628+
4a4d8108
AM
13629+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13630+{
13631+ struct au_finfo *finfo;
13632+ aufs_bindex_t bindex;
f0c0a007 13633+ int delayed;
1facf9fc 13634+
4a4d8108 13635+ finfo = au_fi(file);
2000de60
JR
13636+ au_sphl_del(&finfo->fi_hlist,
13637+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 13638+ bindex = finfo->fi_btop;
b4510431 13639+ if (bindex >= 0)
4a4d8108 13640+ au_set_h_fptr(file, bindex, NULL);
7f207e10 13641+
f0c0a007
AM
13642+ delayed = (current->flags & PF_KTHREAD) || in_interrupt();
13643+ au_finfo_fin(file, delayed);
4a4d8108 13644+ return 0;
1facf9fc 13645+}
13646+
4a4d8108
AM
13647+/* ---------------------------------------------------------------------- */
13648+
13649+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 13650+{
1308ab2a 13651+ int err;
4a4d8108
AM
13652+ struct file *h_file;
13653+
13654+ err = 0;
13655+ h_file = au_hf_top(file);
13656+ if (h_file)
13657+ err = vfsub_flush(h_file, id);
13658+ return err;
13659+}
13660+
13661+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13662+{
13663+ return au_do_flush(file, id, au_do_flush_nondir);
13664+}
13665+
13666+/* ---------------------------------------------------------------------- */
9dbd164d
AM
13667+/*
13668+ * read and write functions acquire [fdi]_rwsem once, but release before
13669+ * mmap_sem. This is because to stop a race condition between mmap(2).
13670+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13671+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13672+ * read functions after [fdi]_rwsem are released, but it should be harmless.
13673+ */
4a4d8108 13674+
b912730e
AM
13675+/* Callers should call au_read_post() or fput() in the end */
13676+struct file *au_read_pre(struct file *file, int keep_fi)
4a4d8108 13677+{
4a4d8108 13678+ struct file *h_file;
b912730e 13679+ int err;
1facf9fc 13680+
4a4d8108 13681+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
b912730e
AM
13682+ if (!err) {
13683+ di_read_unlock(file->f_path.dentry, AuLock_IR);
13684+ h_file = au_hf_top(file);
13685+ get_file(h_file);
13686+ if (!keep_fi)
13687+ fi_read_unlock(file);
13688+ } else
13689+ h_file = ERR_PTR(err);
13690+
13691+ return h_file;
13692+}
13693+
13694+static void au_read_post(struct inode *inode, struct file *h_file)
13695+{
13696+ /* update without lock, I don't think it a problem */
13697+ fsstack_copy_attr_atime(inode, file_inode(h_file));
13698+ fput(h_file);
13699+}
13700+
13701+struct au_write_pre {
13702+ blkcnt_t blks;
5afbbe0d 13703+ aufs_bindex_t btop;
b912730e
AM
13704+};
13705+
13706+/*
13707+ * return with iinfo is write-locked
13708+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13709+ * end
13710+ */
13711+static struct file *au_write_pre(struct file *file, int do_ready,
13712+ struct au_write_pre *wpre)
13713+{
13714+ struct file *h_file;
13715+ struct dentry *dentry;
13716+ int err;
13717+ struct au_pin pin;
13718+
13719+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13720+ h_file = ERR_PTR(err);
dece6358
AM
13721+ if (unlikely(err))
13722+ goto out;
1facf9fc 13723+
b912730e
AM
13724+ dentry = file->f_path.dentry;
13725+ if (do_ready) {
13726+ err = au_ready_to_write(file, -1, &pin);
13727+ if (unlikely(err)) {
13728+ h_file = ERR_PTR(err);
13729+ di_write_unlock(dentry);
13730+ goto out_fi;
13731+ }
13732+ }
13733+
13734+ di_downgrade_lock(dentry, /*flags*/0);
13735+ if (wpre)
5afbbe0d 13736+ wpre->btop = au_fbtop(file);
4a4d8108 13737+ h_file = au_hf_top(file);
9dbd164d 13738+ get_file(h_file);
b912730e
AM
13739+ if (wpre)
13740+ wpre->blks = file_inode(h_file)->i_blocks;
13741+ if (do_ready)
13742+ au_unpin(&pin);
13743+ di_read_unlock(dentry, /*flags*/0);
13744+
13745+out_fi:
13746+ fi_write_unlock(file);
13747+out:
13748+ return h_file;
13749+}
13750+
13751+static void au_write_post(struct inode *inode, struct file *h_file,
13752+ struct au_write_pre *wpre, ssize_t written)
13753+{
13754+ struct inode *h_inode;
13755+
13756+ au_cpup_attr_timesizes(inode);
5afbbe0d 13757+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
13758+ h_inode = file_inode(h_file);
13759+ inode->i_mode = h_inode->i_mode;
13760+ ii_write_unlock(inode);
13761+ fput(h_file);
13762+
13763+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13764+ if (written > 0)
5afbbe0d 13765+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e
AM
13766+ /*force*/h_inode->i_blocks > wpre->blks);
13767+}
13768+
13769+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13770+ loff_t *ppos)
13771+{
13772+ ssize_t err;
13773+ struct inode *inode;
13774+ struct file *h_file;
13775+ struct super_block *sb;
13776+
13777+ inode = file_inode(file);
13778+ sb = inode->i_sb;
13779+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13780+
13781+ h_file = au_read_pre(file, /*keep_fi*/0);
13782+ err = PTR_ERR(h_file);
13783+ if (IS_ERR(h_file))
13784+ goto out;
9dbd164d
AM
13785+
13786+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
13787+ err = vfsub_read_u(h_file, buf, count, ppos);
13788+ /* todo: necessary? */
13789+ /* file->f_ra = h_file->f_ra; */
b912730e 13790+ au_read_post(inode, h_file);
1308ab2a 13791+
4f0767ce 13792+out:
dece6358
AM
13793+ si_read_unlock(sb);
13794+ return err;
13795+}
1facf9fc 13796+
e49829fe
JR
13797+/*
13798+ * todo: very ugly
13799+ * it locks both of i_mutex and si_rwsem for read in safe.
13800+ * if the plink maintenance mode continues forever (that is the problem),
13801+ * may loop forever.
13802+ */
13803+static void au_mtx_and_read_lock(struct inode *inode)
13804+{
13805+ int err;
13806+ struct super_block *sb = inode->i_sb;
13807+
13808+ while (1) {
febd17d6 13809+ inode_lock(inode);
e49829fe
JR
13810+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13811+ if (!err)
13812+ break;
febd17d6 13813+ inode_unlock(inode);
e49829fe
JR
13814+ si_read_lock(sb, AuLock_NOPLMW);
13815+ si_read_unlock(sb);
13816+ }
13817+}
13818+
4a4d8108
AM
13819+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13820+ size_t count, loff_t *ppos)
dece6358 13821+{
4a4d8108 13822+ ssize_t err;
b912730e
AM
13823+ struct au_write_pre wpre;
13824+ struct inode *inode;
4a4d8108
AM
13825+ struct file *h_file;
13826+ char __user *buf = (char __user *)ubuf;
1facf9fc 13827+
b912730e 13828+ inode = file_inode(file);
e49829fe 13829+ au_mtx_and_read_lock(inode);
1facf9fc 13830+
b912730e
AM
13831+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13832+ err = PTR_ERR(h_file);
13833+ if (IS_ERR(h_file))
9dbd164d 13834+ goto out;
9dbd164d 13835+
4a4d8108 13836+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 13837+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 13838+
4f0767ce 13839+out:
b912730e 13840+ si_read_unlock(inode->i_sb);
febd17d6 13841+ inode_unlock(inode);
dece6358
AM
13842+ return err;
13843+}
1facf9fc 13844+
076b876e
AM
13845+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13846+ struct iov_iter *iov_iter)
dece6358 13847+{
4a4d8108
AM
13848+ ssize_t err;
13849+ struct file *file;
076b876e 13850+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 13851+
4a4d8108
AM
13852+ err = security_file_permission(h_file, rw);
13853+ if (unlikely(err))
13854+ goto out;
1facf9fc 13855+
4a4d8108 13856+ err = -ENOSYS;
076b876e 13857+ iter = NULL;
5527c038 13858+ if (rw == MAY_READ)
076b876e 13859+ iter = h_file->f_op->read_iter;
5527c038 13860+ else if (rw == MAY_WRITE)
076b876e 13861+ iter = h_file->f_op->write_iter;
076b876e
AM
13862+
13863+ file = kio->ki_filp;
13864+ kio->ki_filp = h_file;
13865+ if (iter) {
2cbb1c4b 13866+ lockdep_off();
076b876e
AM
13867+ err = iter(kio, iov_iter);
13868+ lockdep_on();
4a4d8108
AM
13869+ } else
13870+ /* currently there is no such fs */
13871+ WARN_ON_ONCE(1);
076b876e 13872+ kio->ki_filp = file;
1facf9fc 13873+
4f0767ce 13874+out:
dece6358
AM
13875+ return err;
13876+}
1facf9fc 13877+
076b876e 13878+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 13879+{
4a4d8108
AM
13880+ ssize_t err;
13881+ struct file *file, *h_file;
b912730e 13882+ struct inode *inode;
dece6358 13883+ struct super_block *sb;
1facf9fc 13884+
4a4d8108 13885+ file = kio->ki_filp;
b912730e
AM
13886+ inode = file_inode(file);
13887+ sb = inode->i_sb;
e49829fe 13888+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13889+
5afbbe0d 13890+ h_file = au_read_pre(file, /*keep_fi*/1);
b912730e
AM
13891+ err = PTR_ERR(h_file);
13892+ if (IS_ERR(h_file))
13893+ goto out;
9dbd164d 13894+
5afbbe0d
AM
13895+ if (au_test_loopback_kthread()) {
13896+ au_warn_loopback(h_file->f_path.dentry->d_sb);
13897+ if (file->f_mapping != h_file->f_mapping) {
13898+ file->f_mapping = h_file->f_mapping;
13899+ smp_mb(); /* unnecessary? */
13900+ }
13901+ }
13902+ fi_read_unlock(file);
13903+
076b876e 13904+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
13905+ /* todo: necessary? */
13906+ /* file->f_ra = h_file->f_ra; */
b912730e 13907+ au_read_post(inode, h_file);
1facf9fc 13908+
4f0767ce 13909+out:
4a4d8108 13910+ si_read_unlock(sb);
1308ab2a 13911+ return err;
13912+}
1facf9fc 13913+
076b876e 13914+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 13915+{
4a4d8108 13916+ ssize_t err;
b912730e
AM
13917+ struct au_write_pre wpre;
13918+ struct inode *inode;
4a4d8108 13919+ struct file *file, *h_file;
1308ab2a 13920+
4a4d8108 13921+ file = kio->ki_filp;
b912730e 13922+ inode = file_inode(file);
e49829fe
JR
13923+ au_mtx_and_read_lock(inode);
13924+
b912730e
AM
13925+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13926+ err = PTR_ERR(h_file);
13927+ if (IS_ERR(h_file))
9dbd164d 13928+ goto out;
9dbd164d 13929+
076b876e 13930+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 13931+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 13932+
4f0767ce 13933+out:
b912730e 13934+ si_read_unlock(inode->i_sb);
febd17d6 13935+ inode_unlock(inode);
dece6358 13936+ return err;
1facf9fc 13937+}
13938+
4a4d8108
AM
13939+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13940+ struct pipe_inode_info *pipe, size_t len,
13941+ unsigned int flags)
1facf9fc 13942+{
4a4d8108
AM
13943+ ssize_t err;
13944+ struct file *h_file;
b912730e 13945+ struct inode *inode;
dece6358 13946+ struct super_block *sb;
1facf9fc 13947+
b912730e
AM
13948+ inode = file_inode(file);
13949+ sb = inode->i_sb;
e49829fe 13950+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 13951+
5afbbe0d 13952+ h_file = au_read_pre(file, /*keep_fi*/0);
b912730e
AM
13953+ err = PTR_ERR(h_file);
13954+ if (IS_ERR(h_file))
dece6358 13955+ goto out;
1facf9fc 13956+
4a4d8108
AM
13957+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13958+ /* todo: necessasry? */
13959+ /* file->f_ra = h_file->f_ra; */
b912730e 13960+ au_read_post(inode, h_file);
1facf9fc 13961+
4f0767ce 13962+out:
4a4d8108 13963+ si_read_unlock(sb);
dece6358 13964+ return err;
1facf9fc 13965+}
13966+
4a4d8108
AM
13967+static ssize_t
13968+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13969+ size_t len, unsigned int flags)
1facf9fc 13970+{
4a4d8108 13971+ ssize_t err;
b912730e
AM
13972+ struct au_write_pre wpre;
13973+ struct inode *inode;
076b876e 13974+ struct file *h_file;
1facf9fc 13975+
b912730e 13976+ inode = file_inode(file);
e49829fe 13977+ au_mtx_and_read_lock(inode);
9dbd164d 13978+
b912730e
AM
13979+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13980+ err = PTR_ERR(h_file);
13981+ if (IS_ERR(h_file))
9dbd164d 13982+ goto out;
9dbd164d 13983+
4a4d8108 13984+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 13985+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 13986+
4f0767ce 13987+out:
b912730e 13988+ si_read_unlock(inode->i_sb);
febd17d6 13989+ inode_unlock(inode);
4a4d8108
AM
13990+ return err;
13991+}
1facf9fc 13992+
38d290e6
JR
13993+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13994+ loff_t len)
13995+{
13996+ long err;
b912730e 13997+ struct au_write_pre wpre;
38d290e6
JR
13998+ struct inode *inode;
13999+ struct file *h_file;
14000+
b912730e 14001+ inode = file_inode(file);
38d290e6
JR
14002+ au_mtx_and_read_lock(inode);
14003+
b912730e
AM
14004+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14005+ err = PTR_ERR(h_file);
14006+ if (IS_ERR(h_file))
38d290e6 14007+ goto out;
38d290e6
JR
14008+
14009+ lockdep_off();
03673fb0 14010+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 14011+ lockdep_on();
b912730e 14012+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
14013+
14014+out:
b912730e 14015+ si_read_unlock(inode->i_sb);
febd17d6 14016+ inode_unlock(inode);
38d290e6
JR
14017+ return err;
14018+}
14019+
4a4d8108
AM
14020+/* ---------------------------------------------------------------------- */
14021+
9dbd164d
AM
14022+/*
14023+ * The locking order around current->mmap_sem.
14024+ * - in most and regular cases
14025+ * file I/O syscall -- aufs_read() or something
14026+ * -- si_rwsem for read -- mmap_sem
14027+ * (Note that [fdi]i_rwsem are released before mmap_sem).
14028+ * - in mmap case
14029+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
14030+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
14031+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
14032+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
14033+ * It means that when aufs acquires si_rwsem for write, the process should never
14034+ * acquire mmap_sem.
14035+ *
392086de 14036+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
14037+ * problem either since any directory is not able to be mmap-ed.
14038+ * The similar scenario is applied to aufs_readlink() too.
14039+ */
14040+
38d290e6 14041+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
14042+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
14043+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
14044+
14045+static unsigned long au_arch_prot_conv(unsigned long flags)
14046+{
14047+ /* currently ppc64 only */
14048+#ifdef CONFIG_PPC64
14049+ /* cf. linux/arch/powerpc/include/asm/mman.h */
14050+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
14051+ return AuConv_VM_PROT(flags, SAO);
14052+#else
14053+ AuDebugOn(arch_calc_vm_prot_bits(-1));
14054+ return 0;
14055+#endif
14056+}
14057+
14058+static unsigned long au_prot_conv(unsigned long flags)
14059+{
14060+ return AuConv_VM_PROT(flags, READ)
14061+ | AuConv_VM_PROT(flags, WRITE)
14062+ | AuConv_VM_PROT(flags, EXEC)
14063+ | au_arch_prot_conv(flags);
14064+}
14065+
14066+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
14067+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
14068+
14069+static unsigned long au_flag_conv(unsigned long flags)
14070+{
14071+ return AuConv_VM_MAP(flags, GROWSDOWN)
14072+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
14073+ | AuConv_VM_MAP(flags, LOCKED);
14074+}
38d290e6 14075+#endif
2dfbb274 14076+
9dbd164d 14077+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 14078+{
4a4d8108 14079+ int err;
4a4d8108 14080+ const unsigned char wlock
9dbd164d 14081+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 14082+ struct super_block *sb;
9dbd164d 14083+ struct file *h_file;
b912730e 14084+ struct inode *inode;
9dbd164d
AM
14085+
14086+ AuDbgVmRegion(file, vma);
1308ab2a 14087+
b912730e
AM
14088+ inode = file_inode(file);
14089+ sb = inode->i_sb;
9dbd164d 14090+ lockdep_off();
e49829fe 14091+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 14092+
b912730e 14093+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 14094+ lockdep_on();
b912730e
AM
14095+ err = PTR_ERR(h_file);
14096+ if (IS_ERR(h_file))
14097+ goto out;
1308ab2a 14098+
b912730e
AM
14099+ err = 0;
14100+ au_set_mmapped(file);
9dbd164d 14101+ au_vm_file_reset(vma, h_file);
38d290e6
JR
14102+ /*
14103+ * we cannot call security_mmap_file() here since it may acquire
14104+ * mmap_sem or i_mutex.
14105+ *
14106+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
14107+ * au_flag_conv(vma->vm_flags));
14108+ */
9dbd164d
AM
14109+ if (!err)
14110+ err = h_file->f_op->mmap(h_file, vma);
b912730e
AM
14111+ if (!err) {
14112+ au_vm_prfile_set(vma, file);
14113+ fsstack_copy_attr_atime(inode, file_inode(h_file));
14114+ goto out_fput; /* success */
14115+ }
2cbb1c4b
JR
14116+ au_unset_mmapped(file);
14117+ au_vm_file_reset(vma, file);
b912730e 14118+
2cbb1c4b 14119+out_fput:
9dbd164d 14120+ lockdep_off();
b912730e
AM
14121+ ii_write_unlock(inode);
14122+ lockdep_on();
14123+ fput(h_file);
4f0767ce 14124+out:
b912730e 14125+ lockdep_off();
9dbd164d
AM
14126+ si_read_unlock(sb);
14127+ lockdep_on();
14128+ AuTraceErr(err);
4a4d8108
AM
14129+ return err;
14130+}
14131+
14132+/* ---------------------------------------------------------------------- */
14133+
1e00d052
AM
14134+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
14135+ int datasync)
4a4d8108
AM
14136+{
14137+ int err;
b912730e 14138+ struct au_write_pre wpre;
4a4d8108
AM
14139+ struct inode *inode;
14140+ struct file *h_file;
4a4d8108
AM
14141+
14142+ err = 0; /* -EBADF; */ /* posix? */
14143+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 14144+ goto out;
4a4d8108 14145+
b912730e
AM
14146+ inode = file_inode(file);
14147+ au_mtx_and_read_lock(inode);
14148+
14149+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14150+ err = PTR_ERR(h_file);
14151+ if (IS_ERR(h_file))
4a4d8108 14152+ goto out_unlock;
4a4d8108 14153+
53392da6 14154+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 14155+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 14156+
4f0767ce 14157+out_unlock:
b912730e 14158+ si_read_unlock(inode->i_sb);
febd17d6 14159+ inode_unlock(inode);
b912730e 14160+out:
4a4d8108 14161+ return err;
dece6358
AM
14162+}
14163+
4a4d8108
AM
14164+/* no one supports this operation, currently */
14165+#if 0
14166+static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
dece6358 14167+{
4a4d8108 14168+ int err;
b912730e 14169+ struct au_write_pre wpre;
febd17d6 14170+ struct inode *inode, *h_inode;
4a4d8108 14171+ struct file *file, *h_file;
1308ab2a 14172+
4a4d8108
AM
14173+ err = 0; /* -EBADF; */ /* posix? */
14174+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
14175+ goto out;
1308ab2a 14176+
b912730e
AM
14177+ file = kio->ki_filp;
14178+ inode = file_inode(file);
14179+ au_mtx_and_read_lock(inode);
14180+
14181+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14182+ err = PTR_ERR(h_file);
14183+ if (IS_ERR(h_file))
4a4d8108 14184+ goto out_unlock;
1308ab2a 14185+
4a4d8108
AM
14186+ err = -ENOSYS;
14187+ h_file = au_hf_top(file);
523b37e3 14188+ if (h_file->f_op->aio_fsync) {
febd17d6 14189+ h_inode = file_inode(h_file);
4a4d8108
AM
14190+ if (!is_sync_kiocb(kio)) {
14191+ get_file(h_file);
14192+ fput(file);
14193+ }
14194+ kio->ki_filp = h_file;
14195+ err = h_file->f_op->aio_fsync(kio, datasync);
febd17d6 14196+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108
AM
14197+ if (!err)
14198+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
14199+ /*ignore*/
febd17d6 14200+ inode_unlock(h_inode);
4a4d8108 14201+ }
b912730e 14202+ au_write_post(inode, h_file, &wpre, /*written*/0);
1308ab2a 14203+
4f0767ce 14204+out_unlock:
e49829fe 14205+ si_read_unlock(inode->sb);
febd17d6 14206+ inode_unlock(inode);
b912730e 14207+out:
4a4d8108 14208+ return err;
dece6358 14209+}
4a4d8108 14210+#endif
dece6358 14211+
4a4d8108 14212+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 14213+{
4a4d8108
AM
14214+ int err;
14215+ struct file *h_file;
4a4d8108 14216+ struct super_block *sb;
1308ab2a 14217+
b912730e 14218+ sb = file->f_path.dentry->d_sb;
e49829fe 14219+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e
AM
14220+
14221+ h_file = au_read_pre(file, /*keep_fi*/0);
14222+ err = PTR_ERR(h_file);
14223+ if (IS_ERR(h_file))
4a4d8108
AM
14224+ goto out;
14225+
523b37e3 14226+ if (h_file->f_op->fasync)
4a4d8108 14227+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 14228+ fput(h_file); /* instead of au_read_post() */
1308ab2a 14229+
4f0767ce 14230+out:
4a4d8108 14231+ si_read_unlock(sb);
1308ab2a 14232+ return err;
dece6358 14233+}
4a4d8108 14234+
febd17d6
JR
14235+static int aufs_setfl(struct file *file, unsigned long arg)
14236+{
14237+ int err;
14238+ struct file *h_file;
14239+ struct super_block *sb;
14240+
14241+ sb = file->f_path.dentry->d_sb;
14242+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14243+
14244+ h_file = au_read_pre(file, /*keep_fi*/0);
14245+ err = PTR_ERR(h_file);
14246+ if (IS_ERR(h_file))
14247+ goto out;
14248+
14249+ arg |= vfsub_file_flags(file) & FASYNC; /* stop calling h_file->fasync */
14250+ err = setfl(/*unused fd*/-1, h_file, arg);
14251+ fput(h_file); /* instead of au_read_post() */
14252+
14253+out:
14254+ si_read_unlock(sb);
14255+ return err;
14256+}
14257+
4a4d8108
AM
14258+/* ---------------------------------------------------------------------- */
14259+
14260+/* no one supports this operation, currently */
14261+#if 0
14262+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 14263+ size_t len, loff_t *pos, int more)
4a4d8108
AM
14264+{
14265+}
14266+#endif
14267+
14268+/* ---------------------------------------------------------------------- */
14269+
14270+const struct file_operations aufs_file_fop = {
14271+ .owner = THIS_MODULE,
2cbb1c4b 14272+
027c5e7a 14273+ .llseek = default_llseek,
4a4d8108
AM
14274+
14275+ .read = aufs_read,
14276+ .write = aufs_write,
076b876e
AM
14277+ .read_iter = aufs_read_iter,
14278+ .write_iter = aufs_write_iter,
14279+
4a4d8108
AM
14280+#ifdef CONFIG_AUFS_POLL
14281+ .poll = aufs_poll,
14282+#endif
14283+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 14284+#ifdef CONFIG_COMPAT
c2b27bf2 14285+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 14286+#endif
4a4d8108
AM
14287+ .mmap = aufs_mmap,
14288+ .open = aufs_open_nondir,
14289+ .flush = aufs_flush_nondir,
14290+ .release = aufs_release_nondir,
14291+ .fsync = aufs_fsync_nondir,
14292+ /* .aio_fsync = aufs_aio_fsync_nondir, */
14293+ .fasync = aufs_fasync,
14294+ /* .sendpage = aufs_sendpage, */
febd17d6 14295+ .setfl = aufs_setfl,
4a4d8108
AM
14296+ .splice_write = aufs_splice_write,
14297+ .splice_read = aufs_splice_read,
14298+#if 0
14299+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 14300+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 14301+#endif
38d290e6 14302+ .fallocate = aufs_fallocate
4a4d8108 14303+};
7f207e10
AM
14304diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
14305--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007 14306+++ linux/fs/aufs/fstype.h 2016-08-17 18:01:06.145222850 +0200
b912730e 14307@@ -0,0 +1,400 @@
4a4d8108 14308+/*
8cdd5066 14309+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
14310+ *
14311+ * This program, aufs is free software; you can redistribute it and/or modify
14312+ * it under the terms of the GNU General Public License as published by
14313+ * the Free Software Foundation; either version 2 of the License, or
14314+ * (at your option) any later version.
14315+ *
14316+ * This program is distributed in the hope that it will be useful,
14317+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14318+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14319+ * GNU General Public License for more details.
14320+ *
14321+ * You should have received a copy of the GNU General Public License
523b37e3 14322+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
14323+ */
14324+
14325+/*
14326+ * judging filesystem type
14327+ */
14328+
14329+#ifndef __AUFS_FSTYPE_H__
14330+#define __AUFS_FSTYPE_H__
14331+
14332+#ifdef __KERNEL__
14333+
14334+#include <linux/fs.h>
14335+#include <linux/magic.h>
b912730e 14336+#include <linux/nfs_fs.h>
b95c5147 14337+#include <linux/romfs_fs.h>
4a4d8108
AM
14338+
14339+static inline int au_test_aufs(struct super_block *sb)
14340+{
14341+ return sb->s_magic == AUFS_SUPER_MAGIC;
14342+}
14343+
14344+static inline const char *au_sbtype(struct super_block *sb)
14345+{
14346+ return sb->s_type->name;
14347+}
1308ab2a 14348+
14349+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14350+{
f0c0a007 14351+#if IS_ENABLED(CONFIG_ISO9660_FS)
2000de60 14352+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
14353+#else
14354+ return 0;
14355+#endif
14356+}
14357+
1308ab2a 14358+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 14359+{
f0c0a007 14360+#if IS_ENABLED(CONFIG_ROMFS_FS)
2000de60 14361+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
14362+#else
14363+ return 0;
14364+#endif
14365+}
14366+
1308ab2a 14367+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 14368+{
f0c0a007 14369+#if IS_ENABLED(CONFIG_CRAMFS)
1308ab2a 14370+ return sb->s_magic == CRAMFS_MAGIC;
14371+#endif
14372+ return 0;
14373+}
14374+
14375+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14376+{
f0c0a007 14377+#if IS_ENABLED(CONFIG_NFS_FS)
1308ab2a 14378+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
14379+#else
14380+ return 0;
14381+#endif
14382+}
14383+
1308ab2a 14384+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 14385+{
f0c0a007 14386+#if IS_ENABLED(CONFIG_FUSE_FS)
1308ab2a 14387+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
14388+#else
14389+ return 0;
14390+#endif
14391+}
14392+
1308ab2a 14393+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 14394+{
f0c0a007 14395+#if IS_ENABLED(CONFIG_XFS_FS)
1308ab2a 14396+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
14397+#else
14398+ return 0;
14399+#endif
14400+}
14401+
1308ab2a 14402+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 14403+{
1308ab2a 14404+#ifdef CONFIG_TMPFS
14405+ return sb->s_magic == TMPFS_MAGIC;
14406+#else
14407+ return 0;
dece6358 14408+#endif
dece6358
AM
14409+}
14410+
1308ab2a 14411+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 14412+{
f0c0a007 14413+#if IS_ENABLED(CONFIG_ECRYPT_FS)
1308ab2a 14414+ return !strcmp(au_sbtype(sb), "ecryptfs");
14415+#else
14416+ return 0;
14417+#endif
1facf9fc 14418+}
14419+
1308ab2a 14420+static inline int au_test_ramfs(struct super_block *sb)
14421+{
14422+ return sb->s_magic == RAMFS_MAGIC;
14423+}
14424+
14425+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14426+{
f0c0a007 14427+#if IS_ENABLED(CONFIG_UBIFS_FS)
1308ab2a 14428+ return sb->s_magic == UBIFS_SUPER_MAGIC;
14429+#else
14430+ return 0;
14431+#endif
14432+}
14433+
14434+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14435+{
14436+#ifdef CONFIG_PROC_FS
14437+ return sb->s_magic == PROC_SUPER_MAGIC;
14438+#else
14439+ return 0;
14440+#endif
14441+}
14442+
14443+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14444+{
14445+#ifdef CONFIG_SYSFS
14446+ return sb->s_magic == SYSFS_MAGIC;
14447+#else
14448+ return 0;
14449+#endif
14450+}
14451+
14452+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14453+{
f0c0a007 14454+#if IS_ENABLED(CONFIG_CONFIGFS_FS)
1308ab2a 14455+ return sb->s_magic == CONFIGFS_MAGIC;
14456+#else
14457+ return 0;
14458+#endif
14459+}
14460+
14461+static inline int au_test_minix(struct super_block *sb __maybe_unused)
14462+{
f0c0a007 14463+#if IS_ENABLED(CONFIG_MINIX_FS)
1308ab2a 14464+ return sb->s_magic == MINIX3_SUPER_MAGIC
14465+ || sb->s_magic == MINIX2_SUPER_MAGIC
14466+ || sb->s_magic == MINIX2_SUPER_MAGIC2
14467+ || sb->s_magic == MINIX_SUPER_MAGIC
14468+ || sb->s_magic == MINIX_SUPER_MAGIC2;
14469+#else
14470+ return 0;
14471+#endif
14472+}
14473+
1308ab2a 14474+static inline int au_test_fat(struct super_block *sb __maybe_unused)
14475+{
f0c0a007 14476+#if IS_ENABLED(CONFIG_FAT_FS)
1308ab2a 14477+ return sb->s_magic == MSDOS_SUPER_MAGIC;
14478+#else
14479+ return 0;
14480+#endif
14481+}
14482+
14483+static inline int au_test_msdos(struct super_block *sb)
14484+{
14485+ return au_test_fat(sb);
14486+}
14487+
14488+static inline int au_test_vfat(struct super_block *sb)
14489+{
14490+ return au_test_fat(sb);
14491+}
14492+
14493+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14494+{
14495+#ifdef CONFIG_SECURITYFS
14496+ return sb->s_magic == SECURITYFS_MAGIC;
14497+#else
14498+ return 0;
14499+#endif
14500+}
14501+
14502+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14503+{
f0c0a007 14504+#if IS_ENABLED(CONFIG_SQUASHFS)
1308ab2a 14505+ return sb->s_magic == SQUASHFS_MAGIC;
14506+#else
14507+ return 0;
14508+#endif
14509+}
14510+
14511+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14512+{
f0c0a007 14513+#if IS_ENABLED(CONFIG_BTRFS_FS)
1308ab2a 14514+ return sb->s_magic == BTRFS_SUPER_MAGIC;
14515+#else
14516+ return 0;
14517+#endif
14518+}
14519+
14520+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14521+{
f0c0a007 14522+#if IS_ENABLED(CONFIG_XENFS)
1308ab2a 14523+ return sb->s_magic == XENFS_SUPER_MAGIC;
14524+#else
14525+ return 0;
14526+#endif
14527+}
14528+
14529+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14530+{
14531+#ifdef CONFIG_DEBUG_FS
14532+ return sb->s_magic == DEBUGFS_MAGIC;
14533+#else
14534+ return 0;
14535+#endif
14536+}
14537+
14538+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14539+{
f0c0a007 14540+#if IS_ENABLED(CONFIG_NILFS)
1308ab2a 14541+ return sb->s_magic == NILFS_SUPER_MAGIC;
14542+#else
14543+ return 0;
14544+#endif
14545+}
14546+
4a4d8108
AM
14547+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14548+{
f0c0a007 14549+#if IS_ENABLED(CONFIG_HFSPLUS_FS)
4a4d8108
AM
14550+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14551+#else
14552+ return 0;
14553+#endif
14554+}
14555+
1308ab2a 14556+/* ---------------------------------------------------------------------- */
14557+/*
14558+ * they can't be an aufs branch.
14559+ */
14560+static inline int au_test_fs_unsuppoted(struct super_block *sb)
14561+{
14562+ return
14563+#ifndef CONFIG_AUFS_BR_RAMFS
14564+ au_test_ramfs(sb) ||
14565+#endif
14566+ au_test_procfs(sb)
14567+ || au_test_sysfs(sb)
14568+ || au_test_configfs(sb)
14569+ || au_test_debugfs(sb)
14570+ || au_test_securityfs(sb)
14571+ || au_test_xenfs(sb)
14572+ || au_test_ecryptfs(sb)
14573+ /* || !strcmp(au_sbtype(sb), "unionfs") */
14574+ || au_test_aufs(sb); /* will be supported in next version */
14575+}
14576+
1308ab2a 14577+static inline int au_test_fs_remote(struct super_block *sb)
14578+{
14579+ return !au_test_tmpfs(sb)
14580+#ifdef CONFIG_AUFS_BR_RAMFS
14581+ && !au_test_ramfs(sb)
14582+#endif
14583+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14584+}
14585+
14586+/* ---------------------------------------------------------------------- */
14587+
14588+/*
14589+ * Note: these functions (below) are created after reading ->getattr() in all
14590+ * filesystems under linux/fs. it means we have to do so in every update...
14591+ */
14592+
14593+/*
14594+ * some filesystems require getattr to refresh the inode attributes before
14595+ * referencing.
14596+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14597+ * and leave the work for d_revalidate()
14598+ */
14599+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14600+{
14601+ return au_test_nfs(sb)
14602+ || au_test_fuse(sb)
1308ab2a 14603+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 14604+ ;
14605+}
14606+
14607+/*
14608+ * filesystems which don't maintain i_size or i_blocks.
14609+ */
14610+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14611+{
14612+ return au_test_xfs(sb)
4a4d8108
AM
14613+ || au_test_btrfs(sb)
14614+ || au_test_ubifs(sb)
14615+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 14616+ /* || au_test_minix(sb) */ /* untested */
14617+ ;
14618+}
14619+
14620+/*
14621+ * filesystems which don't store the correct value in some of their inode
14622+ * attributes.
14623+ */
14624+static inline int au_test_fs_bad_iattr(struct super_block *sb)
14625+{
14626+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 14627+ || au_test_fat(sb)
14628+ || au_test_msdos(sb)
14629+ || au_test_vfat(sb);
1facf9fc 14630+}
14631+
14632+/* they don't check i_nlink in link(2) */
14633+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14634+{
14635+ return au_test_tmpfs(sb)
14636+#ifdef CONFIG_AUFS_BR_RAMFS
14637+ || au_test_ramfs(sb)
14638+#endif
4a4d8108 14639+ || au_test_ubifs(sb)
4a4d8108 14640+ || au_test_hfsplus(sb);
1facf9fc 14641+}
14642+
14643+/*
14644+ * filesystems which sets S_NOATIME and S_NOCMTIME.
14645+ */
14646+static inline int au_test_fs_notime(struct super_block *sb)
14647+{
14648+ return au_test_nfs(sb)
14649+ || au_test_fuse(sb)
dece6358 14650+ || au_test_ubifs(sb)
1facf9fc 14651+ ;
14652+}
14653+
1facf9fc 14654+/* temporary support for i#1 in cramfs */
14655+static inline int au_test_fs_unique_ino(struct inode *inode)
14656+{
14657+ if (au_test_cramfs(inode->i_sb))
14658+ return inode->i_ino != 1;
14659+ return 1;
14660+}
14661+
14662+/* ---------------------------------------------------------------------- */
14663+
14664+/*
14665+ * the filesystem where the xino files placed must support i/o after unlink and
14666+ * maintain i_size and i_blocks.
14667+ */
14668+static inline int au_test_fs_bad_xino(struct super_block *sb)
14669+{
14670+ return au_test_fs_remote(sb)
14671+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 14672+ /* don't want unnecessary work for xino */
14673+ || au_test_aufs(sb)
1308ab2a 14674+ || au_test_ecryptfs(sb)
14675+ || au_test_nilfs(sb);
1facf9fc 14676+}
14677+
14678+static inline int au_test_fs_trunc_xino(struct super_block *sb)
14679+{
14680+ return au_test_tmpfs(sb)
14681+ || au_test_ramfs(sb);
14682+}
14683+
14684+/*
14685+ * test if the @sb is real-readonly.
14686+ */
14687+static inline int au_test_fs_rr(struct super_block *sb)
14688+{
14689+ return au_test_squashfs(sb)
14690+ || au_test_iso9660(sb)
14691+ || au_test_cramfs(sb)
14692+ || au_test_romfs(sb);
14693+}
14694+
b912730e
AM
14695+/*
14696+ * test if the @inode is nfs with 'noacl' option
14697+ * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14698+ */
14699+static inline int au_test_nfs_noacl(struct inode *inode)
14700+{
14701+ return au_test_nfs(inode->i_sb)
14702+ /* && IS_POSIXACL(inode) */
14703+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
14704+}
14705+
1facf9fc 14706+#endif /* __KERNEL__ */
14707+#endif /* __AUFS_FSTYPE_H__ */
7f207e10
AM
14708diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14709--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 14710+++ linux/fs/aufs/hfsnotify.c 2016-08-17 18:01:06.145222850 +0200
5afbbe0d 14711@@ -0,0 +1,287 @@
1facf9fc 14712+/*
8cdd5066 14713+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 14714+ *
14715+ * This program, aufs is free software; you can redistribute it and/or modify
14716+ * it under the terms of the GNU General Public License as published by
14717+ * the Free Software Foundation; either version 2 of the License, or
14718+ * (at your option) any later version.
dece6358
AM
14719+ *
14720+ * This program is distributed in the hope that it will be useful,
14721+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14722+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14723+ * GNU General Public License for more details.
14724+ *
14725+ * You should have received a copy of the GNU General Public License
523b37e3 14726+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14727+ */
14728+
14729+/*
4a4d8108 14730+ * fsnotify for the lower directories
1facf9fc 14731+ */
14732+
14733+#include "aufs.h"
14734+
4a4d8108
AM
14735+/* FS_IN_IGNORED is unnecessary */
14736+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14737+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 14738+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 14739+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 14740+
0c5527e5 14741+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 14742+{
0c5527e5
AM
14743+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14744+ hn_mark);
5afbbe0d 14745+ /* AuDbg("here\n"); */
f0c0a007 14746+ au_cache_dfree_hnotify(hn);
076b876e 14747+ smp_mb__before_atomic();
1716fcea
AM
14748+ if (atomic64_dec_and_test(&au_hfsn_ifree))
14749+ wake_up(&au_hfsn_wq);
4a4d8108 14750+}
1facf9fc 14751+
027c5e7a 14752+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 14753+{
1716fcea 14754+ int err;
027c5e7a
AM
14755+ struct au_hnotify *hn;
14756+ struct super_block *sb;
14757+ struct au_branch *br;
0c5527e5 14758+ struct fsnotify_mark *mark;
027c5e7a 14759+ aufs_bindex_t bindex;
1facf9fc 14760+
027c5e7a
AM
14761+ hn = hinode->hi_notify;
14762+ sb = hn->hn_aufs_inode->i_sb;
14763+ bindex = au_br_index(sb, hinode->hi_id);
14764+ br = au_sbr(sb, bindex);
1716fcea
AM
14765+ AuDebugOn(!br->br_hfsn);
14766+
0c5527e5
AM
14767+ mark = &hn->hn_mark;
14768+ fsnotify_init_mark(mark, au_hfsn_free_mark);
14769+ mark->mask = AuHfsnMask;
7f207e10
AM
14770+ /*
14771+ * by udba rename or rmdir, aufs assign a new inode to the known
14772+ * h_inode, so specify 1 to allow dups.
14773+ */
c1595e42 14774+ lockdep_off();
1716fcea 14775+ err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
027c5e7a 14776+ /*mnt*/NULL, /*allow_dups*/1);
c1595e42 14777+ lockdep_on();
1716fcea
AM
14778+
14779+ return err;
1facf9fc 14780+}
14781+
7eafdf33 14782+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 14783+{
0c5527e5 14784+ struct fsnotify_mark *mark;
7eafdf33 14785+ unsigned long long ull;
1716fcea 14786+ struct fsnotify_group *group;
7eafdf33
AM
14787+
14788+ ull = atomic64_inc_return(&au_hfsn_ifree);
14789+ BUG_ON(!ull);
953406b4 14790+
0c5527e5 14791+ mark = &hn->hn_mark;
1716fcea
AM
14792+ spin_lock(&mark->lock);
14793+ group = mark->group;
14794+ fsnotify_get_group(group);
14795+ spin_unlock(&mark->lock);
c1595e42 14796+ lockdep_off();
1716fcea 14797+ fsnotify_destroy_mark(mark, group);
5afbbe0d 14798+ fsnotify_put_mark(mark);
1716fcea 14799+ fsnotify_put_group(group);
c1595e42 14800+ lockdep_on();
7f207e10 14801+
7eafdf33
AM
14802+ /* free hn by myself */
14803+ return 0;
1facf9fc 14804+}
14805+
14806+/* ---------------------------------------------------------------------- */
14807+
4a4d8108 14808+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 14809+{
0c5527e5 14810+ struct fsnotify_mark *mark;
1facf9fc 14811+
0c5527e5
AM
14812+ mark = &hinode->hi_notify->hn_mark;
14813+ spin_lock(&mark->lock);
1facf9fc 14814+ if (do_set) {
0c5527e5
AM
14815+ AuDebugOn(mark->mask & AuHfsnMask);
14816+ mark->mask |= AuHfsnMask;
1facf9fc 14817+ } else {
0c5527e5
AM
14818+ AuDebugOn(!(mark->mask & AuHfsnMask));
14819+ mark->mask &= ~AuHfsnMask;
1facf9fc 14820+ }
0c5527e5 14821+ spin_unlock(&mark->lock);
4a4d8108 14822+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 14823+}
14824+
4a4d8108 14825+/* ---------------------------------------------------------------------- */
1facf9fc 14826+
4a4d8108
AM
14827+/* #define AuDbgHnotify */
14828+#ifdef AuDbgHnotify
14829+static char *au_hfsn_name(u32 mask)
14830+{
14831+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
14832+#define test_ret(flag) \
14833+ do { \
14834+ if (mask & flag) \
14835+ return #flag; \
14836+ } while (0)
4a4d8108
AM
14837+ test_ret(FS_ACCESS);
14838+ test_ret(FS_MODIFY);
14839+ test_ret(FS_ATTRIB);
14840+ test_ret(FS_CLOSE_WRITE);
14841+ test_ret(FS_CLOSE_NOWRITE);
14842+ test_ret(FS_OPEN);
14843+ test_ret(FS_MOVED_FROM);
14844+ test_ret(FS_MOVED_TO);
14845+ test_ret(FS_CREATE);
14846+ test_ret(FS_DELETE);
14847+ test_ret(FS_DELETE_SELF);
14848+ test_ret(FS_MOVE_SELF);
14849+ test_ret(FS_UNMOUNT);
14850+ test_ret(FS_Q_OVERFLOW);
14851+ test_ret(FS_IN_IGNORED);
b912730e 14852+ test_ret(FS_ISDIR);
4a4d8108
AM
14853+ test_ret(FS_IN_ONESHOT);
14854+ test_ret(FS_EVENT_ON_CHILD);
14855+ return "";
14856+#undef test_ret
14857+#else
14858+ return "??";
14859+#endif
1facf9fc 14860+}
4a4d8108 14861+#endif
1facf9fc 14862+
14863+/* ---------------------------------------------------------------------- */
14864+
1716fcea
AM
14865+static void au_hfsn_free_group(struct fsnotify_group *group)
14866+{
14867+ struct au_br_hfsnotify *hfsn = group->private;
14868+
5afbbe0d 14869+ /* AuDbg("here\n"); */
f0c0a007 14870+ au_delayed_kfree(hfsn);
1716fcea
AM
14871+}
14872+
4a4d8108 14873+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 14874+ struct inode *inode,
0c5527e5
AM
14875+ struct fsnotify_mark *inode_mark,
14876+ struct fsnotify_mark *vfsmount_mark,
fb47a38f
JR
14877+ u32 mask, void *data, int data_type,
14878+ const unsigned char *file_name, u32 cookie)
1facf9fc 14879+{
14880+ int err;
4a4d8108
AM
14881+ struct au_hnotify *hnotify;
14882+ struct inode *h_dir, *h_inode;
fb47a38f 14883+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
4a4d8108 14884+
fb47a38f 14885+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 14886+
14887+ err = 0;
0c5527e5 14888+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 14889+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 14890+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 14891+ goto out;
1facf9fc 14892+
fb47a38f
JR
14893+ h_dir = inode;
14894+ h_inode = NULL;
4a4d8108 14895+#ifdef AuDbgHnotify
392086de 14896+ au_debug_on();
4a4d8108
AM
14897+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14898+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14899+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14900+ h_dir->i_ino, mask, au_hfsn_name(mask),
14901+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14902+ /* WARN_ON(1); */
1facf9fc 14903+ }
392086de 14904+ au_debug_off();
1facf9fc 14905+#endif
4a4d8108 14906+
0c5527e5
AM
14907+ AuDebugOn(!inode_mark);
14908+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14909+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 14910+
4a4d8108
AM
14911+out:
14912+ return err;
14913+}
1facf9fc 14914+
4a4d8108 14915+static struct fsnotify_ops au_hfsn_ops = {
1716fcea
AM
14916+ .handle_event = au_hfsn_handle_event,
14917+ .free_group_priv = au_hfsn_free_group
4a4d8108
AM
14918+};
14919+
14920+/* ---------------------------------------------------------------------- */
14921+
027c5e7a
AM
14922+static void au_hfsn_fin_br(struct au_branch *br)
14923+{
1716fcea 14924+ struct au_br_hfsnotify *hfsn;
027c5e7a 14925+
1716fcea 14926+ hfsn = br->br_hfsn;
c1595e42
JR
14927+ if (hfsn) {
14928+ lockdep_off();
1716fcea 14929+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
14930+ lockdep_on();
14931+ }
027c5e7a
AM
14932+}
14933+
1716fcea 14934+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
14935+{
14936+ int err;
1716fcea
AM
14937+ struct fsnotify_group *group;
14938+ struct au_br_hfsnotify *hfsn;
1facf9fc 14939+
4a4d8108 14940+ err = 0;
1716fcea
AM
14941+ br->br_hfsn = NULL;
14942+ if (!au_br_hnotifyable(perm))
027c5e7a 14943+ goto out;
027c5e7a 14944+
1716fcea
AM
14945+ err = -ENOMEM;
14946+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14947+ if (unlikely(!hfsn))
027c5e7a
AM
14948+ goto out;
14949+
1716fcea
AM
14950+ err = 0;
14951+ group = fsnotify_alloc_group(&au_hfsn_ops);
14952+ if (IS_ERR(group)) {
14953+ err = PTR_ERR(group);
0c5527e5 14954+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 14955+ goto out_hfsn;
4a4d8108 14956+ }
1facf9fc 14957+
1716fcea
AM
14958+ group->private = hfsn;
14959+ hfsn->hfsn_group = group;
14960+ br->br_hfsn = hfsn;
14961+ goto out; /* success */
14962+
14963+out_hfsn:
f0c0a007 14964+ au_delayed_kfree(hfsn);
027c5e7a 14965+out:
1716fcea
AM
14966+ return err;
14967+}
14968+
14969+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14970+{
14971+ int err;
14972+
14973+ err = 0;
14974+ if (!br->br_hfsn)
14975+ err = au_hfsn_init_br(br, perm);
14976+
1facf9fc 14977+ return err;
14978+}
14979+
7eafdf33
AM
14980+/* ---------------------------------------------------------------------- */
14981+
14982+static void au_hfsn_fin(void)
14983+{
14984+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14985+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14986+}
14987+
4a4d8108
AM
14988+const struct au_hnotify_op au_hnotify_op = {
14989+ .ctl = au_hfsn_ctl,
14990+ .alloc = au_hfsn_alloc,
14991+ .free = au_hfsn_free,
1facf9fc 14992+
7eafdf33
AM
14993+ .fin = au_hfsn_fin,
14994+
027c5e7a
AM
14995+ .reset_br = au_hfsn_reset_br,
14996+ .fin_br = au_hfsn_fin_br,
14997+ .init_br = au_hfsn_init_br
4a4d8108 14998+};
7f207e10
AM
14999diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
15000--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 15001+++ linux/fs/aufs/hfsplus.c 2016-07-25 19:05:34.814493242 +0200
523b37e3 15002@@ -0,0 +1,56 @@
4a4d8108 15003+/*
8cdd5066 15004+ * Copyright (C) 2010-2016 Junjiro R. Okajima
4a4d8108
AM
15005+ *
15006+ * This program, aufs is free software; you can redistribute it and/or modify
15007+ * it under the terms of the GNU General Public License as published by
15008+ * the Free Software Foundation; either version 2 of the License, or
15009+ * (at your option) any later version.
15010+ *
15011+ * This program is distributed in the hope that it will be useful,
15012+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15013+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15014+ * GNU General Public License for more details.
15015+ *
15016+ * You should have received a copy of the GNU General Public License
523b37e3 15017+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 15018+ */
1facf9fc 15019+
4a4d8108
AM
15020+/*
15021+ * special support for filesystems which aqucires an inode mutex
15022+ * at final closing a file, eg, hfsplus.
15023+ *
15024+ * This trick is very simple and stupid, just to open the file before really
15025+ * neceeary open to tell hfsplus that this is not the final closing.
15026+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
15027+ * and au_h_open_post() after releasing it.
15028+ */
1facf9fc 15029+
4a4d8108 15030+#include "aufs.h"
1facf9fc 15031+
392086de
AM
15032+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15033+ int force_wr)
4a4d8108
AM
15034+{
15035+ struct file *h_file;
15036+ struct dentry *h_dentry;
1facf9fc 15037+
4a4d8108
AM
15038+ h_dentry = au_h_dptr(dentry, bindex);
15039+ AuDebugOn(!h_dentry);
5527c038 15040+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
15041+
15042+ h_file = NULL;
15043+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 15044+ && d_is_reg(h_dentry))
4a4d8108
AM
15045+ h_file = au_h_open(dentry, bindex,
15046+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 15047+ /*file*/NULL, force_wr);
4a4d8108 15048+ return h_file;
1facf9fc 15049+}
15050+
4a4d8108
AM
15051+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15052+ struct file *h_file)
15053+{
15054+ if (h_file) {
15055+ fput(h_file);
15056+ au_sbr_put(dentry->d_sb, bindex);
15057+ }
15058+}
7f207e10
AM
15059diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
15060--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
15061+++ linux/fs/aufs/hnotify.c 2016-08-17 18:01:06.148556271 +0200
15062@@ -0,0 +1,723 @@
e49829fe 15063+/*
8cdd5066 15064+ * Copyright (C) 2005-2016 Junjiro R. Okajima
e49829fe
JR
15065+ *
15066+ * This program, aufs is free software; you can redistribute it and/or modify
15067+ * it under the terms of the GNU General Public License as published by
15068+ * the Free Software Foundation; either version 2 of the License, or
15069+ * (at your option) any later version.
15070+ *
15071+ * This program is distributed in the hope that it will be useful,
15072+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15073+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15074+ * GNU General Public License for more details.
15075+ *
15076+ * You should have received a copy of the GNU General Public License
523b37e3 15077+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
15078+ */
15079+
15080+/*
7f207e10 15081+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
15082+ */
15083+
15084+#include "aufs.h"
15085+
027c5e7a 15086+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
15087+{
15088+ int err;
7f207e10 15089+ struct au_hnotify *hn;
1facf9fc 15090+
4a4d8108
AM
15091+ err = -ENOMEM;
15092+ hn = au_cache_alloc_hnotify();
15093+ if (hn) {
15094+ hn->hn_aufs_inode = inode;
027c5e7a
AM
15095+ hinode->hi_notify = hn;
15096+ err = au_hnotify_op.alloc(hinode);
15097+ AuTraceErr(err);
15098+ if (unlikely(err)) {
15099+ hinode->hi_notify = NULL;
f0c0a007 15100+ au_cache_dfree_hnotify(hn);
4a4d8108
AM
15101+ /*
15102+ * The upper dir was removed by udba, but the same named
15103+ * dir left. In this case, aufs assignes a new inode
15104+ * number and set the monitor again.
15105+ * For the lower dir, the old monitnor is still left.
15106+ */
15107+ if (err == -EEXIST)
15108+ err = 0;
15109+ }
1308ab2a 15110+ }
1308ab2a 15111+
027c5e7a 15112+ AuTraceErr(err);
1308ab2a 15113+ return err;
dece6358 15114+}
1facf9fc 15115+
4a4d8108 15116+void au_hn_free(struct au_hinode *hinode)
dece6358 15117+{
4a4d8108 15118+ struct au_hnotify *hn;
1facf9fc 15119+
4a4d8108
AM
15120+ hn = hinode->hi_notify;
15121+ if (hn) {
4a4d8108 15122+ hinode->hi_notify = NULL;
7eafdf33 15123+ if (au_hnotify_op.free(hinode, hn))
f0c0a007 15124+ au_cache_dfree_hnotify(hn);
4a4d8108
AM
15125+ }
15126+}
dece6358 15127+
4a4d8108 15128+/* ---------------------------------------------------------------------- */
dece6358 15129+
4a4d8108
AM
15130+void au_hn_ctl(struct au_hinode *hinode, int do_set)
15131+{
15132+ if (hinode->hi_notify)
15133+ au_hnotify_op.ctl(hinode, do_set);
15134+}
15135+
15136+void au_hn_reset(struct inode *inode, unsigned int flags)
15137+{
5afbbe0d 15138+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
15139+ struct inode *hi;
15140+ struct dentry *iwhdentry;
1facf9fc 15141+
5afbbe0d
AM
15142+ bbot = au_ibbot(inode);
15143+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
15144+ hi = au_h_iptr(inode, bindex);
15145+ if (!hi)
15146+ continue;
1308ab2a 15147+
febd17d6 15148+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
15149+ iwhdentry = au_hi_wh(inode, bindex);
15150+ if (iwhdentry)
15151+ dget(iwhdentry);
15152+ au_igrab(hi);
15153+ au_set_h_iptr(inode, bindex, NULL, 0);
15154+ au_set_h_iptr(inode, bindex, au_igrab(hi),
15155+ flags & ~AuHi_XINO);
15156+ iput(hi);
15157+ dput(iwhdentry);
febd17d6 15158+ /* inode_unlock(hi); */
1facf9fc 15159+ }
1facf9fc 15160+}
15161+
1308ab2a 15162+/* ---------------------------------------------------------------------- */
1facf9fc 15163+
4a4d8108 15164+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 15165+{
4a4d8108 15166+ int err;
5afbbe0d 15167+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 15168+ struct inode *h_i;
1facf9fc 15169+
4a4d8108
AM
15170+ err = 0;
15171+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 15172+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
15173+ goto out;
15174+ }
1facf9fc 15175+
4a4d8108 15176+ bfound = -1;
5afbbe0d
AM
15177+ bbot = au_ibbot(inode);
15178+ btop = au_ibtop(inode);
4a4d8108 15179+#if 0 /* reserved for future use */
5afbbe0d 15180+ if (bindex == bbot) {
4a4d8108
AM
15181+ /* keep this ino in rename case */
15182+ goto out;
15183+ }
15184+#endif
5afbbe0d 15185+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
15186+ if (au_h_iptr(inode, bindex) == h_inode) {
15187+ bfound = bindex;
15188+ break;
15189+ }
15190+ if (bfound < 0)
1308ab2a 15191+ goto out;
1facf9fc 15192+
5afbbe0d 15193+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
15194+ h_i = au_h_iptr(inode, bindex);
15195+ if (!h_i)
15196+ continue;
1facf9fc 15197+
4a4d8108
AM
15198+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
15199+ /* ignore this error */
15200+ /* bad action? */
1facf9fc 15201+ }
1facf9fc 15202+
4a4d8108 15203+ /* children inode number will be broken */
1facf9fc 15204+
4f0767ce 15205+out:
4a4d8108
AM
15206+ AuTraceErr(err);
15207+ return err;
1facf9fc 15208+}
15209+
4a4d8108 15210+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 15211+{
4a4d8108
AM
15212+ int err, i, j, ndentry;
15213+ struct au_dcsub_pages dpages;
15214+ struct au_dpage *dpage;
15215+ struct dentry **dentries;
1facf9fc 15216+
4a4d8108
AM
15217+ err = au_dpages_init(&dpages, GFP_NOFS);
15218+ if (unlikely(err))
15219+ goto out;
15220+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
15221+ if (unlikely(err))
15222+ goto out_dpages;
1facf9fc 15223+
4a4d8108
AM
15224+ for (i = 0; i < dpages.ndpage; i++) {
15225+ dpage = dpages.dpages + i;
15226+ dentries = dpage->dentries;
15227+ ndentry = dpage->ndentry;
15228+ for (j = 0; j < ndentry; j++) {
15229+ struct dentry *d;
15230+
15231+ d = dentries[j];
15232+ if (IS_ROOT(d))
15233+ continue;
15234+
4a4d8108 15235+ au_digen_dec(d);
5527c038 15236+ if (d_really_is_positive(d))
4a4d8108
AM
15237+ /* todo: reset children xino?
15238+ cached children only? */
5527c038 15239+ au_iigen_dec(d_inode(d));
1308ab2a 15240+ }
dece6358 15241+ }
1facf9fc 15242+
4f0767ce 15243+out_dpages:
4a4d8108 15244+ au_dpages_free(&dpages);
dece6358 15245+
027c5e7a 15246+#if 0
4a4d8108
AM
15247+ /* discard children */
15248+ dentry_unhash(dentry);
15249+ dput(dentry);
027c5e7a 15250+#endif
4f0767ce 15251+out:
dece6358
AM
15252+ return err;
15253+}
15254+
1308ab2a 15255+/*
4a4d8108 15256+ * return 0 if processed.
1308ab2a 15257+ */
4a4d8108
AM
15258+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
15259+ const unsigned int isdir)
dece6358 15260+{
1308ab2a 15261+ int err;
4a4d8108
AM
15262+ struct dentry *d;
15263+ struct qstr *dname;
1facf9fc 15264+
4a4d8108
AM
15265+ err = 1;
15266+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 15267+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
15268+ err = 0;
15269+ goto out;
15270+ }
dece6358 15271+
4a4d8108
AM
15272+ if (!isdir) {
15273+ AuDebugOn(!name);
15274+ au_iigen_dec(inode);
027c5e7a 15275+ spin_lock(&inode->i_lock);
c1595e42 15276+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 15277+ spin_lock(&d->d_lock);
4a4d8108
AM
15278+ dname = &d->d_name;
15279+ if (dname->len != nlen
027c5e7a
AM
15280+ && memcmp(dname->name, name, nlen)) {
15281+ spin_unlock(&d->d_lock);
4a4d8108 15282+ continue;
027c5e7a 15283+ }
4a4d8108 15284+ err = 0;
4a4d8108
AM
15285+ au_digen_dec(d);
15286+ spin_unlock(&d->d_lock);
15287+ break;
1facf9fc 15288+ }
027c5e7a 15289+ spin_unlock(&inode->i_lock);
1308ab2a 15290+ } else {
027c5e7a 15291+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 15292+ d = d_find_any_alias(inode);
4a4d8108
AM
15293+ if (!d) {
15294+ au_iigen_dec(inode);
15295+ goto out;
15296+ }
1facf9fc 15297+
027c5e7a 15298+ spin_lock(&d->d_lock);
4a4d8108 15299+ dname = &d->d_name;
027c5e7a
AM
15300+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
15301+ spin_unlock(&d->d_lock);
4a4d8108 15302+ err = hn_gen_tree(d);
027c5e7a
AM
15303+ spin_lock(&d->d_lock);
15304+ }
15305+ spin_unlock(&d->d_lock);
4a4d8108
AM
15306+ dput(d);
15307+ }
1facf9fc 15308+
4f0767ce 15309+out:
4a4d8108 15310+ AuTraceErr(err);
1308ab2a 15311+ return err;
15312+}
dece6358 15313+
4a4d8108 15314+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 15315+{
4a4d8108 15316+ int err;
1facf9fc 15317+
5527c038 15318+ if (IS_ROOT(dentry)) {
0c3ec466 15319+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
15320+ return 0;
15321+ }
1308ab2a 15322+
4a4d8108
AM
15323+ err = 0;
15324+ if (!isdir) {
4a4d8108 15325+ au_digen_dec(dentry);
5527c038
JR
15326+ if (d_really_is_positive(dentry))
15327+ au_iigen_dec(d_inode(dentry));
4a4d8108 15328+ } else {
027c5e7a 15329+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 15330+ if (d_really_is_positive(dentry))
4a4d8108
AM
15331+ err = hn_gen_tree(dentry);
15332+ }
15333+
15334+ AuTraceErr(err);
15335+ return err;
1facf9fc 15336+}
15337+
4a4d8108 15338+/* ---------------------------------------------------------------------- */
1facf9fc 15339+
4a4d8108
AM
15340+/* hnotify job flags */
15341+#define AuHnJob_XINO0 1
15342+#define AuHnJob_GEN (1 << 1)
15343+#define AuHnJob_DIRENT (1 << 2)
15344+#define AuHnJob_ISDIR (1 << 3)
15345+#define AuHnJob_TRYXINO0 (1 << 4)
15346+#define AuHnJob_MNTPNT (1 << 5)
15347+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
15348+#define au_fset_hnjob(flags, name) \
15349+ do { (flags) |= AuHnJob_##name; } while (0)
15350+#define au_fclr_hnjob(flags, name) \
15351+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 15352+
4a4d8108
AM
15353+enum {
15354+ AuHn_CHILD,
15355+ AuHn_PARENT,
15356+ AuHnLast
15357+};
1facf9fc 15358+
4a4d8108
AM
15359+struct au_hnotify_args {
15360+ struct inode *h_dir, *dir, *h_child_inode;
15361+ u32 mask;
15362+ unsigned int flags[AuHnLast];
15363+ unsigned int h_child_nlen;
15364+ char h_child_name[];
15365+};
1facf9fc 15366+
4a4d8108
AM
15367+struct hn_job_args {
15368+ unsigned int flags;
15369+ struct inode *inode, *h_inode, *dir, *h_dir;
15370+ struct dentry *dentry;
15371+ char *h_name;
15372+ int h_nlen;
15373+};
1308ab2a 15374+
4a4d8108
AM
15375+static int hn_job(struct hn_job_args *a)
15376+{
15377+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 15378+ int e;
1308ab2a 15379+
4a4d8108
AM
15380+ /* reset xino */
15381+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15382+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 15383+
4a4d8108
AM
15384+ if (au_ftest_hnjob(a->flags, TRYXINO0)
15385+ && a->inode
15386+ && a->h_inode) {
febd17d6 15387+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
15388+ if (!a->h_inode->i_nlink
15389+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 15390+ hn_xino(a->inode, a->h_inode); /* ignore this error */
febd17d6 15391+ inode_unlock(a->h_inode);
1308ab2a 15392+ }
1facf9fc 15393+
4a4d8108
AM
15394+ /* make the generation obsolete */
15395+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 15396+ e = -1;
4a4d8108 15397+ if (a->inode)
076b876e 15398+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 15399+ isdir);
076b876e 15400+ if (e && a->dentry)
4a4d8108
AM
15401+ hn_gen_by_name(a->dentry, isdir);
15402+ /* ignore this error */
1facf9fc 15403+ }
1facf9fc 15404+
4a4d8108
AM
15405+ /* make dir entries obsolete */
15406+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15407+ struct au_vdir *vdir;
1facf9fc 15408+
4a4d8108
AM
15409+ vdir = au_ivdir(a->inode);
15410+ if (vdir)
15411+ vdir->vd_jiffy = 0;
15412+ /* IMustLock(a->inode); */
15413+ /* a->inode->i_version++; */
15414+ }
1facf9fc 15415+
4a4d8108
AM
15416+ /* can do nothing but warn */
15417+ if (au_ftest_hnjob(a->flags, MNTPNT)
15418+ && a->dentry
15419+ && d_mountpoint(a->dentry))
523b37e3 15420+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 15421+
4a4d8108 15422+ return 0;
1308ab2a 15423+}
1facf9fc 15424+
1308ab2a 15425+/* ---------------------------------------------------------------------- */
1facf9fc 15426+
4a4d8108
AM
15427+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15428+ struct inode *dir)
1308ab2a 15429+{
4a4d8108
AM
15430+ struct dentry *dentry, *d, *parent;
15431+ struct qstr *dname;
1308ab2a 15432+
c1595e42 15433+ parent = d_find_any_alias(dir);
4a4d8108
AM
15434+ if (!parent)
15435+ return NULL;
1308ab2a 15436+
4a4d8108 15437+ dentry = NULL;
027c5e7a 15438+ spin_lock(&parent->d_lock);
c1595e42 15439+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 15440+ /* AuDbg("%pd\n", d); */
027c5e7a 15441+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
15442+ dname = &d->d_name;
15443+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
15444+ goto cont_unlock;
15445+ if (au_di(d))
15446+ au_digen_dec(d);
15447+ else
15448+ goto cont_unlock;
c1595e42 15449+ if (au_dcount(d) > 0) {
027c5e7a 15450+ dentry = dget_dlock(d);
4a4d8108 15451+ spin_unlock(&d->d_lock);
027c5e7a 15452+ break;
dece6358 15453+ }
1facf9fc 15454+
f6b6e03d 15455+cont_unlock:
027c5e7a 15456+ spin_unlock(&d->d_lock);
1308ab2a 15457+ }
027c5e7a 15458+ spin_unlock(&parent->d_lock);
4a4d8108 15459+ dput(parent);
1facf9fc 15460+
4a4d8108
AM
15461+ if (dentry)
15462+ di_write_lock_child(dentry);
1308ab2a 15463+
4a4d8108
AM
15464+ return dentry;
15465+}
dece6358 15466+
4a4d8108
AM
15467+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15468+ aufs_bindex_t bindex, ino_t h_ino)
15469+{
15470+ struct inode *inode;
15471+ ino_t ino;
15472+ int err;
15473+
15474+ inode = NULL;
15475+ err = au_xino_read(sb, bindex, h_ino, &ino);
15476+ if (!err && ino)
15477+ inode = ilookup(sb, ino);
15478+ if (!inode)
15479+ goto out;
15480+
15481+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 15482+ pr_warn("wrong root branch\n");
4a4d8108
AM
15483+ iput(inode);
15484+ inode = NULL;
15485+ goto out;
1308ab2a 15486+ }
15487+
4a4d8108 15488+ ii_write_lock_child(inode);
1308ab2a 15489+
4f0767ce 15490+out:
4a4d8108 15491+ return inode;
dece6358
AM
15492+}
15493+
4a4d8108 15494+static void au_hn_bh(void *_args)
1facf9fc 15495+{
4a4d8108
AM
15496+ struct au_hnotify_args *a = _args;
15497+ struct super_block *sb;
5afbbe0d 15498+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 15499+ unsigned char xino, try_iput;
1facf9fc 15500+ int err;
1308ab2a 15501+ struct inode *inode;
4a4d8108
AM
15502+ ino_t h_ino;
15503+ struct hn_job_args args;
15504+ struct dentry *dentry;
15505+ struct au_sbinfo *sbinfo;
1facf9fc 15506+
4a4d8108
AM
15507+ AuDebugOn(!_args);
15508+ AuDebugOn(!a->h_dir);
15509+ AuDebugOn(!a->dir);
15510+ AuDebugOn(!a->mask);
15511+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15512+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
15513+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 15514+
4a4d8108
AM
15515+ inode = NULL;
15516+ dentry = NULL;
15517+ /*
15518+ * do not lock a->dir->i_mutex here
15519+ * because of d_revalidate() may cause a deadlock.
15520+ */
15521+ sb = a->dir->i_sb;
15522+ AuDebugOn(!sb);
15523+ sbinfo = au_sbi(sb);
15524+ AuDebugOn(!sbinfo);
7f207e10 15525+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 15526+
4a4d8108
AM
15527+ ii_read_lock_parent(a->dir);
15528+ bfound = -1;
5afbbe0d
AM
15529+ bbot = au_ibbot(a->dir);
15530+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
15531+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15532+ bfound = bindex;
15533+ break;
15534+ }
15535+ ii_read_unlock(a->dir);
15536+ if (unlikely(bfound < 0))
15537+ goto out;
1facf9fc 15538+
4a4d8108
AM
15539+ xino = !!au_opt_test(au_mntflags(sb), XINO);
15540+ h_ino = 0;
15541+ if (a->h_child_inode)
15542+ h_ino = a->h_child_inode->i_ino;
1facf9fc 15543+
4a4d8108
AM
15544+ if (a->h_child_nlen
15545+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15546+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15547+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15548+ a->dir);
15549+ try_iput = 0;
5527c038
JR
15550+ if (dentry && d_really_is_positive(dentry))
15551+ inode = d_inode(dentry);
4a4d8108
AM
15552+ if (xino && !inode && h_ino
15553+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15554+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15555+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15556+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15557+ try_iput = 1;
f0c0a007 15558+ }
1facf9fc 15559+
4a4d8108
AM
15560+ args.flags = a->flags[AuHn_CHILD];
15561+ args.dentry = dentry;
15562+ args.inode = inode;
15563+ args.h_inode = a->h_child_inode;
15564+ args.dir = a->dir;
15565+ args.h_dir = a->h_dir;
15566+ args.h_name = a->h_child_name;
15567+ args.h_nlen = a->h_child_nlen;
15568+ err = hn_job(&args);
15569+ if (dentry) {
027c5e7a 15570+ if (au_di(dentry))
4a4d8108
AM
15571+ di_write_unlock(dentry);
15572+ dput(dentry);
15573+ }
15574+ if (inode && try_iput) {
15575+ ii_write_unlock(inode);
15576+ iput(inode);
15577+ }
1facf9fc 15578+
4a4d8108
AM
15579+ ii_write_lock_parent(a->dir);
15580+ args.flags = a->flags[AuHn_PARENT];
15581+ args.dentry = NULL;
15582+ args.inode = a->dir;
15583+ args.h_inode = a->h_dir;
15584+ args.dir = NULL;
15585+ args.h_dir = NULL;
15586+ args.h_name = NULL;
15587+ args.h_nlen = 0;
15588+ err = hn_job(&args);
15589+ ii_write_unlock(a->dir);
1facf9fc 15590+
4f0767ce 15591+out:
4a4d8108
AM
15592+ iput(a->h_child_inode);
15593+ iput(a->h_dir);
15594+ iput(a->dir);
027c5e7a
AM
15595+ si_write_unlock(sb);
15596+ au_nwt_done(&sbinfo->si_nowait);
f0c0a007 15597+ au_delayed_kfree(a);
dece6358 15598+}
1facf9fc 15599+
4a4d8108
AM
15600+/* ---------------------------------------------------------------------- */
15601+
15602+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15603+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 15604+{
4a4d8108 15605+ int err, len;
53392da6 15606+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
15607+ unsigned char isdir, isroot, wh;
15608+ struct inode *dir;
15609+ struct au_hnotify_args *args;
15610+ char *p, *h_child_name;
dece6358 15611+
1308ab2a 15612+ err = 0;
4a4d8108
AM
15613+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15614+ dir = igrab(hnotify->hn_aufs_inode);
15615+ if (!dir)
15616+ goto out;
1facf9fc 15617+
4a4d8108
AM
15618+ isroot = (dir->i_ino == AUFS_ROOT_INO);
15619+ wh = 0;
15620+ h_child_name = (void *)h_child_qstr->name;
15621+ len = h_child_qstr->len;
15622+ if (h_child_name) {
15623+ if (len > AUFS_WH_PFX_LEN
15624+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15625+ h_child_name += AUFS_WH_PFX_LEN;
15626+ len -= AUFS_WH_PFX_LEN;
15627+ wh = 1;
15628+ }
1facf9fc 15629+ }
dece6358 15630+
4a4d8108
AM
15631+ isdir = 0;
15632+ if (h_child_inode)
15633+ isdir = !!S_ISDIR(h_child_inode->i_mode);
15634+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
15635+ flags[AuHn_CHILD] = 0;
15636+ if (isdir)
15637+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
15638+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15639+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
15640+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15641+ case FS_MOVED_FROM:
15642+ case FS_MOVED_TO:
15643+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15644+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15645+ /*FALLTHROUGH*/
15646+ case FS_CREATE:
fb47a38f 15647+ AuDebugOn(!h_child_name);
4a4d8108 15648+ break;
1facf9fc 15649+
4a4d8108
AM
15650+ case FS_DELETE:
15651+ /*
15652+ * aufs never be able to get this child inode.
15653+ * revalidation should be in d_revalidate()
15654+ * by checking i_nlink, i_generation or d_unhashed().
15655+ */
15656+ AuDebugOn(!h_child_name);
15657+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15658+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15659+ break;
dece6358 15660+
4a4d8108
AM
15661+ default:
15662+ AuDebugOn(1);
15663+ }
1308ab2a 15664+
4a4d8108
AM
15665+ if (wh)
15666+ h_child_inode = NULL;
1308ab2a 15667+
4a4d8108
AM
15668+ err = -ENOMEM;
15669+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 15670+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
15671+ if (unlikely(!args)) {
15672+ AuErr1("no memory\n");
15673+ iput(dir);
15674+ goto out;
15675+ }
15676+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15677+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15678+ args->mask = mask;
15679+ args->dir = dir;
15680+ args->h_dir = igrab(h_dir);
15681+ if (h_child_inode)
15682+ h_child_inode = igrab(h_child_inode); /* can be NULL */
15683+ args->h_child_inode = h_child_inode;
15684+ args->h_child_nlen = len;
15685+ if (len) {
15686+ p = (void *)args;
15687+ p += sizeof(*args);
15688+ memcpy(p, h_child_name, len);
15689+ p[len] = 0;
1308ab2a 15690+ }
1308ab2a 15691+
38d290e6 15692+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 15693+ f = 0;
38d290e6
JR
15694+ if (!dir->i_nlink
15695+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
15696+ f = AuWkq_NEST;
15697+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
15698+ if (unlikely(err)) {
15699+ pr_err("wkq %d\n", err);
15700+ iput(args->h_child_inode);
15701+ iput(args->h_dir);
15702+ iput(args->dir);
f0c0a007 15703+ au_delayed_kfree(args);
1facf9fc 15704+ }
1facf9fc 15705+
4a4d8108 15706+out:
1facf9fc 15707+ return err;
15708+}
15709+
027c5e7a
AM
15710+/* ---------------------------------------------------------------------- */
15711+
15712+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15713+{
15714+ int err;
15715+
15716+ AuDebugOn(!(udba & AuOptMask_UDBA));
15717+
15718+ err = 0;
15719+ if (au_hnotify_op.reset_br)
15720+ err = au_hnotify_op.reset_br(udba, br, perm);
15721+
15722+ return err;
15723+}
15724+
15725+int au_hnotify_init_br(struct au_branch *br, int perm)
15726+{
15727+ int err;
15728+
15729+ err = 0;
15730+ if (au_hnotify_op.init_br)
15731+ err = au_hnotify_op.init_br(br, perm);
15732+
15733+ return err;
15734+}
15735+
15736+void au_hnotify_fin_br(struct au_branch *br)
15737+{
15738+ if (au_hnotify_op.fin_br)
15739+ au_hnotify_op.fin_br(br);
15740+}
15741+
4a4d8108
AM
15742+static void au_hn_destroy_cache(void)
15743+{
f0c0a007
AM
15744+ struct au_cache *cp;
15745+
15746+ flush_delayed_work(&au_dfree.dwork);
15747+ cp = au_dfree.cache + AuCache_HNOTIFY;
15748+ AuDebugOn(!llist_empty(&cp->llist));
15749+ kmem_cache_destroy(cp->cache);
15750+ cp->cache = NULL;
4a4d8108 15751+}
1308ab2a 15752+
f0c0a007
AM
15753+AU_CACHE_DFREE_FUNC(hnotify, HNOTIFY, hn_lnode);
15754+
4a4d8108 15755+int __init au_hnotify_init(void)
1facf9fc 15756+{
1308ab2a 15757+ int err;
f0c0a007 15758+ struct au_cache *cp;
1308ab2a 15759+
4a4d8108 15760+ err = -ENOMEM;
f0c0a007
AM
15761+ cp = au_dfree.cache + AuCache_HNOTIFY;
15762+ cp->cache = AuCache(au_hnotify);
15763+ if (cp->cache) {
027c5e7a
AM
15764+ err = 0;
15765+ if (au_hnotify_op.init)
15766+ err = au_hnotify_op.init();
4a4d8108
AM
15767+ if (unlikely(err))
15768+ au_hn_destroy_cache();
1308ab2a 15769+ }
1308ab2a 15770+ AuTraceErr(err);
4a4d8108 15771+ return err;
1308ab2a 15772+}
15773+
4a4d8108 15774+void au_hnotify_fin(void)
1308ab2a 15775+{
f0c0a007
AM
15776+ struct au_cache *cp;
15777+
027c5e7a
AM
15778+ if (au_hnotify_op.fin)
15779+ au_hnotify_op.fin();
f0c0a007 15780+
4a4d8108 15781+ /* cf. au_cache_fin() */
f0c0a007
AM
15782+ cp = au_dfree.cache + AuCache_HNOTIFY;
15783+ if (cp->cache)
4a4d8108 15784+ au_hn_destroy_cache();
dece6358 15785+}
7f207e10
AM
15786diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15787--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 15788+++ linux/fs/aufs/iinfo.c 2016-08-17 18:01:06.148556271 +0200
5afbbe0d 15789@@ -0,0 +1,284 @@
dece6358 15790+/*
8cdd5066 15791+ * Copyright (C) 2005-2016 Junjiro R. Okajima
dece6358
AM
15792+ *
15793+ * This program, aufs is free software; you can redistribute it and/or modify
15794+ * it under the terms of the GNU General Public License as published by
15795+ * the Free Software Foundation; either version 2 of the License, or
15796+ * (at your option) any later version.
15797+ *
15798+ * This program is distributed in the hope that it will be useful,
15799+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15800+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15801+ * GNU General Public License for more details.
15802+ *
15803+ * You should have received a copy of the GNU General Public License
523b37e3 15804+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15805+ */
1facf9fc 15806+
dece6358 15807+/*
4a4d8108 15808+ * inode private data
dece6358 15809+ */
1facf9fc 15810+
1308ab2a 15811+#include "aufs.h"
1facf9fc 15812+
4a4d8108 15813+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 15814+{
4a4d8108 15815+ struct inode *h_inode;
5afbbe0d 15816+ struct au_hinode *hinode;
1facf9fc 15817+
4a4d8108 15818+ IiMustAnyLock(inode);
1facf9fc 15819+
5afbbe0d
AM
15820+ hinode = au_hinode(au_ii(inode), bindex);
15821+ h_inode = hinode->hi_inode;
4a4d8108
AM
15822+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15823+ return h_inode;
15824+}
1facf9fc 15825+
4a4d8108
AM
15826+/* todo: hard/soft set? */
15827+void au_hiput(struct au_hinode *hinode)
15828+{
15829+ au_hn_free(hinode);
15830+ dput(hinode->hi_whdentry);
15831+ iput(hinode->hi_inode);
15832+}
1facf9fc 15833+
4a4d8108
AM
15834+unsigned int au_hi_flags(struct inode *inode, int isdir)
15835+{
15836+ unsigned int flags;
15837+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 15838+
4a4d8108
AM
15839+ flags = 0;
15840+ if (au_opt_test(mnt_flags, XINO))
15841+ au_fset_hi(flags, XINO);
15842+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15843+ au_fset_hi(flags, HNOTIFY);
15844+ return flags;
1facf9fc 15845+}
15846+
4a4d8108
AM
15847+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15848+ struct inode *h_inode, unsigned int flags)
1308ab2a 15849+{
4a4d8108
AM
15850+ struct au_hinode *hinode;
15851+ struct inode *hi;
15852+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 15853+
4a4d8108 15854+ IiMustWriteLock(inode);
dece6358 15855+
5afbbe0d 15856+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
15857+ hi = hinode->hi_inode;
15858+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15859+
15860+ if (hi)
15861+ au_hiput(hinode);
15862+ hinode->hi_inode = h_inode;
15863+ if (h_inode) {
15864+ int err;
15865+ struct super_block *sb = inode->i_sb;
15866+ struct au_branch *br;
15867+
027c5e7a
AM
15868+ AuDebugOn(inode->i_mode
15869+ && (h_inode->i_mode & S_IFMT)
15870+ != (inode->i_mode & S_IFMT));
5afbbe0d 15871+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
15872+ au_cpup_igen(inode, h_inode);
15873+ br = au_sbr(sb, bindex);
15874+ hinode->hi_id = br->br_id;
15875+ if (au_ftest_hi(flags, XINO)) {
15876+ err = au_xino_write(sb, bindex, h_inode->i_ino,
15877+ inode->i_ino);
15878+ if (unlikely(err))
15879+ AuIOErr1("failed au_xino_write() %d\n", err);
15880+ }
15881+
15882+ if (au_ftest_hi(flags, HNOTIFY)
15883+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 15884+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
15885+ if (unlikely(err))
15886+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 15887+ }
15888+ }
4a4d8108 15889+}
dece6358 15890+
4a4d8108
AM
15891+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15892+ struct dentry *h_wh)
15893+{
15894+ struct au_hinode *hinode;
dece6358 15895+
4a4d8108
AM
15896+ IiMustWriteLock(inode);
15897+
5afbbe0d 15898+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
15899+ AuDebugOn(hinode->hi_whdentry);
15900+ hinode->hi_whdentry = h_wh;
1facf9fc 15901+}
15902+
537831f9 15903+void au_update_iigen(struct inode *inode, int half)
1308ab2a 15904+{
537831f9
AM
15905+ struct au_iinfo *iinfo;
15906+ struct au_iigen *iigen;
15907+ unsigned int sigen;
15908+
15909+ sigen = au_sigen(inode->i_sb);
15910+ iinfo = au_ii(inode);
15911+ iigen = &iinfo->ii_generation;
be52b249 15912+ spin_lock(&iigen->ig_spin);
537831f9
AM
15913+ iigen->ig_generation = sigen;
15914+ if (half)
15915+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15916+ else
15917+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 15918+ spin_unlock(&iigen->ig_spin);
4a4d8108 15919+}
1facf9fc 15920+
4a4d8108
AM
15921+/* it may be called at remount time, too */
15922+void au_update_ibrange(struct inode *inode, int do_put_zero)
15923+{
15924+ struct au_iinfo *iinfo;
5afbbe0d 15925+ aufs_bindex_t bindex, bbot;
1facf9fc 15926+
5afbbe0d 15927+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 15928+ IiMustWriteLock(inode);
1facf9fc 15929+
5afbbe0d
AM
15930+ iinfo = au_ii(inode);
15931+ if (do_put_zero && iinfo->ii_btop >= 0) {
15932+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
15933+ bindex++) {
15934+ struct inode *h_i;
1facf9fc 15935+
5afbbe0d 15936+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
15937+ if (h_i
15938+ && !h_i->i_nlink
15939+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
15940+ au_set_h_iptr(inode, bindex, NULL, 0);
15941+ }
4a4d8108
AM
15942+ }
15943+
5afbbe0d
AM
15944+ iinfo->ii_btop = -1;
15945+ iinfo->ii_bbot = -1;
15946+ bbot = au_sbbot(inode->i_sb);
15947+ for (bindex = 0; bindex <= bbot; bindex++)
15948+ if (au_hinode(iinfo, bindex)->hi_inode) {
15949+ iinfo->ii_btop = bindex;
4a4d8108 15950+ break;
027c5e7a 15951+ }
5afbbe0d
AM
15952+ if (iinfo->ii_btop >= 0)
15953+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
15954+ if (au_hinode(iinfo, bindex)->hi_inode) {
15955+ iinfo->ii_bbot = bindex;
027c5e7a
AM
15956+ break;
15957+ }
5afbbe0d 15958+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 15959+}
1facf9fc 15960+
dece6358 15961+/* ---------------------------------------------------------------------- */
1facf9fc 15962+
4a4d8108 15963+void au_icntnr_init_once(void *_c)
dece6358 15964+{
4a4d8108
AM
15965+ struct au_icntnr *c = _c;
15966+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 15967+
be52b249 15968+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
15969+ au_rw_init(&iinfo->ii_rwsem);
15970+ inode_init_once(&c->vfs_inode);
15971+}
1facf9fc 15972+
5afbbe0d
AM
15973+void au_hinode_init(struct au_hinode *hinode)
15974+{
15975+ hinode->hi_inode = NULL;
15976+ hinode->hi_id = -1;
15977+ au_hn_init(hinode);
15978+ hinode->hi_whdentry = NULL;
15979+}
15980+
4a4d8108
AM
15981+int au_iinfo_init(struct inode *inode)
15982+{
15983+ struct au_iinfo *iinfo;
15984+ struct super_block *sb;
5afbbe0d 15985+ struct au_hinode *hi;
4a4d8108 15986+ int nbr, i;
1facf9fc 15987+
4a4d8108
AM
15988+ sb = inode->i_sb;
15989+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 15990+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15991+ if (unlikely(nbr <= 0))
15992+ nbr = 1;
5afbbe0d
AM
15993+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15994+ if (hi) {
7f207e10 15995+ au_ninodes_inc(sb);
5afbbe0d
AM
15996+
15997+ iinfo->ii_hinode = hi;
15998+ for (i = 0; i < nbr; i++, hi++)
15999+ au_hinode_init(hi);
1facf9fc 16000+
537831f9 16001+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
16002+ iinfo->ii_btop = -1;
16003+ iinfo->ii_bbot = -1;
4a4d8108
AM
16004+ iinfo->ii_vdir = NULL;
16005+ return 0;
1308ab2a 16006+ }
4a4d8108
AM
16007+ return -ENOMEM;
16008+}
1facf9fc 16009+
5afbbe0d 16010+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr)
4a4d8108 16011+{
5afbbe0d 16012+ int err, i;
4a4d8108 16013+ struct au_hinode *hip;
1facf9fc 16014+
4a4d8108
AM
16015+ AuRwMustWriteLock(&iinfo->ii_rwsem);
16016+
16017+ err = -ENOMEM;
5afbbe0d 16018+ hip = krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS);
4a4d8108
AM
16019+ if (hip) {
16020+ iinfo->ii_hinode = hip;
5afbbe0d
AM
16021+ i = iinfo->ii_bbot + 1;
16022+ hip += i;
16023+ for (; i < nbr; i++, hip++)
16024+ au_hinode_init(hip);
4a4d8108 16025+ err = 0;
1308ab2a 16026+ }
4a4d8108 16027+
1308ab2a 16028+ return err;
1facf9fc 16029+}
16030+
4a4d8108 16031+void au_iinfo_fin(struct inode *inode)
1facf9fc 16032+{
4a4d8108
AM
16033+ struct au_iinfo *iinfo;
16034+ struct au_hinode *hi;
16035+ struct super_block *sb;
5afbbe0d 16036+ aufs_bindex_t bindex, bbot;
b752ccd1 16037+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 16038+
5afbbe0d 16039+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 16040+
b752ccd1 16041+ sb = inode->i_sb;
7f207e10 16042+ au_ninodes_dec(sb);
b752ccd1
AM
16043+ if (si_pid_test(sb))
16044+ au_xino_delete_inode(inode, unlinked);
16045+ else {
16046+ /*
16047+ * it is safe to hide the dependency between sbinfo and
16048+ * sb->s_umount.
16049+ */
16050+ lockdep_off();
16051+ si_noflush_read_lock(sb);
16052+ au_xino_delete_inode(inode, unlinked);
16053+ si_read_unlock(sb);
16054+ lockdep_on();
16055+ }
16056+
5afbbe0d 16057+ iinfo = au_ii(inode);
4a4d8108 16058+ if (iinfo->ii_vdir)
f0c0a007 16059+ au_vdir_free(iinfo->ii_vdir, /*atonce*/0);
1308ab2a 16060+
5afbbe0d 16061+ bindex = iinfo->ii_btop;
b752ccd1 16062+ if (bindex >= 0) {
5afbbe0d
AM
16063+ hi = au_hinode(iinfo, bindex);
16064+ bbot = iinfo->ii_bbot;
16065+ while (bindex++ <= bbot) {
b752ccd1 16066+ if (hi->hi_inode)
4a4d8108 16067+ au_hiput(hi);
4a4d8108
AM
16068+ hi++;
16069+ }
16070+ }
f0c0a007 16071+ au_delayed_kfree(iinfo->ii_hinode);
4a4d8108 16072+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 16073+}
7f207e10
AM
16074diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
16075--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
16076+++ linux/fs/aufs/inode.c 2016-07-25 19:05:34.814493242 +0200
16077@@ -0,0 +1,517 @@
4a4d8108 16078+/*
8cdd5066 16079+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
16080+ *
16081+ * This program, aufs is free software; you can redistribute it and/or modify
16082+ * it under the terms of the GNU General Public License as published by
16083+ * the Free Software Foundation; either version 2 of the License, or
16084+ * (at your option) any later version.
16085+ *
16086+ * This program is distributed in the hope that it will be useful,
16087+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16088+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16089+ * GNU General Public License for more details.
16090+ *
16091+ * You should have received a copy of the GNU General Public License
523b37e3 16092+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 16093+ */
1facf9fc 16094+
4a4d8108
AM
16095+/*
16096+ * inode functions
16097+ */
1facf9fc 16098+
4a4d8108 16099+#include "aufs.h"
1308ab2a 16100+
4a4d8108
AM
16101+struct inode *au_igrab(struct inode *inode)
16102+{
16103+ if (inode) {
16104+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 16105+ ihold(inode);
1facf9fc 16106+ }
4a4d8108
AM
16107+ return inode;
16108+}
1facf9fc 16109+
4a4d8108
AM
16110+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
16111+{
16112+ au_cpup_attr_all(inode, /*force*/0);
537831f9 16113+ au_update_iigen(inode, /*half*/1);
4a4d8108
AM
16114+ if (do_version)
16115+ inode->i_version++;
dece6358 16116+}
1facf9fc 16117+
027c5e7a 16118+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 16119+{
4a4d8108 16120+ int err, e;
027c5e7a 16121+ umode_t type;
4a4d8108 16122+ aufs_bindex_t bindex, new_bindex;
1308ab2a 16123+ struct super_block *sb;
4a4d8108 16124+ struct au_iinfo *iinfo;
027c5e7a 16125+ struct au_hinode *p, *q, tmp;
1facf9fc 16126+
5afbbe0d 16127+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 16128+ IiMustWriteLock(inode);
1facf9fc 16129+
027c5e7a 16130+ *update = 0;
4a4d8108 16131+ sb = inode->i_sb;
027c5e7a 16132+ type = inode->i_mode & S_IFMT;
4a4d8108 16133+ iinfo = au_ii(inode);
5afbbe0d 16134+ err = au_hinode_realloc(iinfo, au_sbbot(sb) + 1);
4a4d8108 16135+ if (unlikely(err))
1308ab2a 16136+ goto out;
1facf9fc 16137+
5afbbe0d
AM
16138+ AuDebugOn(iinfo->ii_btop < 0);
16139+ p = au_hinode(iinfo, iinfo->ii_btop);
16140+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
16141+ bindex++, p++) {
16142+ if (!p->hi_inode)
16143+ continue;
1facf9fc 16144+
027c5e7a 16145+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
16146+ new_bindex = au_br_index(sb, p->hi_id);
16147+ if (new_bindex == bindex)
16148+ continue;
1facf9fc 16149+
4a4d8108 16150+ if (new_bindex < 0) {
027c5e7a 16151+ *update = 1;
4a4d8108
AM
16152+ au_hiput(p);
16153+ p->hi_inode = NULL;
16154+ continue;
1308ab2a 16155+ }
4a4d8108 16156+
5afbbe0d
AM
16157+ if (new_bindex < iinfo->ii_btop)
16158+ iinfo->ii_btop = new_bindex;
16159+ if (iinfo->ii_bbot < new_bindex)
16160+ iinfo->ii_bbot = new_bindex;
4a4d8108 16161+ /* swap two lower inode, and loop again */
5afbbe0d 16162+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
16163+ tmp = *q;
16164+ *q = *p;
16165+ *p = tmp;
16166+ if (tmp.hi_inode) {
16167+ bindex--;
16168+ p--;
1308ab2a 16169+ }
16170+ }
4a4d8108
AM
16171+ au_update_ibrange(inode, /*do_put_zero*/0);
16172+ e = au_dy_irefresh(inode);
16173+ if (unlikely(e && !err))
16174+ err = e;
1facf9fc 16175+
4f0767ce 16176+out:
027c5e7a
AM
16177+ AuTraceErr(err);
16178+ return err;
16179+}
16180+
b95c5147
AM
16181+void au_refresh_iop(struct inode *inode, int force_getattr)
16182+{
16183+ int type;
16184+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
16185+ const struct inode_operations *iop
16186+ = force_getattr ? aufs_iop : sbi->si_iop_array;
16187+
16188+ if (inode->i_op == iop)
16189+ return;
16190+
16191+ switch (inode->i_mode & S_IFMT) {
16192+ case S_IFDIR:
16193+ type = AuIop_DIR;
16194+ break;
16195+ case S_IFLNK:
16196+ type = AuIop_SYMLINK;
16197+ break;
16198+ default:
16199+ type = AuIop_OTHER;
16200+ break;
16201+ }
16202+
16203+ inode->i_op = iop + type;
16204+ /* unnecessary smp_wmb() */
16205+}
16206+
027c5e7a
AM
16207+int au_refresh_hinode_self(struct inode *inode)
16208+{
16209+ int err, update;
16210+
16211+ err = au_ii_refresh(inode, &update);
16212+ if (!err)
16213+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
16214+
16215+ AuTraceErr(err);
4a4d8108
AM
16216+ return err;
16217+}
1facf9fc 16218+
4a4d8108
AM
16219+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
16220+{
027c5e7a 16221+ int err, e, update;
4a4d8108 16222+ unsigned int flags;
027c5e7a 16223+ umode_t mode;
5afbbe0d 16224+ aufs_bindex_t bindex, bbot;
027c5e7a 16225+ unsigned char isdir;
4a4d8108
AM
16226+ struct au_hinode *p;
16227+ struct au_iinfo *iinfo;
1facf9fc 16228+
027c5e7a 16229+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
16230+ if (unlikely(err))
16231+ goto out;
16232+
16233+ update = 0;
16234+ iinfo = au_ii(inode);
5afbbe0d 16235+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
16236+ mode = (inode->i_mode & S_IFMT);
16237+ isdir = S_ISDIR(mode);
4a4d8108 16238+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
16239+ bbot = au_dbbot(dentry);
16240+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 16241+ struct inode *h_i, *h_inode;
4a4d8108
AM
16242+ struct dentry *h_d;
16243+
16244+ h_d = au_h_dptr(dentry, bindex);
5527c038 16245+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
16246+ continue;
16247+
5527c038
JR
16248+ h_inode = d_inode(h_d);
16249+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 16250+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
16251+ h_i = au_h_iptr(inode, bindex);
16252+ if (h_i) {
5527c038 16253+ if (h_i == h_inode)
4a4d8108
AM
16254+ continue;
16255+ err = -EIO;
16256+ break;
16257+ }
16258+ }
5afbbe0d
AM
16259+ if (bindex < iinfo->ii_btop)
16260+ iinfo->ii_btop = bindex;
16261+ if (iinfo->ii_bbot < bindex)
16262+ iinfo->ii_bbot = bindex;
5527c038 16263+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 16264+ update = 1;
1308ab2a 16265+ }
4a4d8108
AM
16266+ au_update_ibrange(inode, /*do_put_zero*/0);
16267+ e = au_dy_irefresh(inode);
16268+ if (unlikely(e && !err))
16269+ err = e;
027c5e7a
AM
16270+ if (!err)
16271+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 16272+
4f0767ce 16273+out:
4a4d8108 16274+ AuTraceErr(err);
1308ab2a 16275+ return err;
dece6358
AM
16276+}
16277+
4a4d8108 16278+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 16279+{
4a4d8108
AM
16280+ int err;
16281+ unsigned int flags;
16282+ umode_t mode;
5afbbe0d 16283+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
16284+ unsigned char isdir;
16285+ struct dentry *h_dentry;
16286+ struct inode *h_inode;
16287+ struct au_iinfo *iinfo;
b95c5147 16288+ struct inode_operations *iop;
dece6358 16289+
4a4d8108 16290+ IiMustWriteLock(inode);
dece6358 16291+
4a4d8108
AM
16292+ err = 0;
16293+ isdir = 0;
b95c5147 16294+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
16295+ btop = au_dbtop(dentry);
16296+ h_dentry = au_h_dptr(dentry, btop);
5527c038 16297+ h_inode = d_inode(h_dentry);
4a4d8108
AM
16298+ mode = h_inode->i_mode;
16299+ switch (mode & S_IFMT) {
16300+ case S_IFREG:
16301+ btail = au_dbtail(dentry);
b95c5147 16302+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 16303+ inode->i_fop = &aufs_file_fop;
5afbbe0d 16304+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
16305+ if (unlikely(err))
16306+ goto out;
16307+ break;
16308+ case S_IFDIR:
16309+ isdir = 1;
16310+ btail = au_dbtaildir(dentry);
b95c5147 16311+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
16312+ inode->i_fop = &aufs_dir_fop;
16313+ break;
16314+ case S_IFLNK:
16315+ btail = au_dbtail(dentry);
b95c5147 16316+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
16317+ break;
16318+ case S_IFBLK:
16319+ case S_IFCHR:
16320+ case S_IFIFO:
16321+ case S_IFSOCK:
16322+ btail = au_dbtail(dentry);
b95c5147 16323+ inode->i_op = iop + AuIop_OTHER;
38d290e6 16324+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
16325+ break;
16326+ default:
16327+ AuIOErr("Unknown file type 0%o\n", mode);
16328+ err = -EIO;
1308ab2a 16329+ goto out;
4a4d8108 16330+ }
dece6358 16331+
4a4d8108
AM
16332+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
16333+ flags = au_hi_flags(inode, isdir);
16334+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
16335+ && au_ftest_hi(flags, HNOTIFY)
16336+ && dentry->d_name.len > AUFS_WH_PFX_LEN
16337+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
16338+ au_fclr_hi(flags, HNOTIFY);
16339+ iinfo = au_ii(inode);
5afbbe0d
AM
16340+ iinfo->ii_btop = btop;
16341+ iinfo->ii_bbot = btail;
16342+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
16343+ h_dentry = au_h_dptr(dentry, bindex);
16344+ if (h_dentry)
16345+ au_set_h_iptr(inode, bindex,
5527c038 16346+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
16347+ }
16348+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
16349+ /*
16350+ * to force calling aufs_get_acl() every time,
16351+ * do not call cache_no_acl() for aufs inode.
16352+ */
dece6358 16353+
4f0767ce 16354+out:
4a4d8108
AM
16355+ return err;
16356+}
dece6358 16357+
027c5e7a
AM
16358+/*
16359+ * successful returns with iinfo write_locked
16360+ * minus: errno
16361+ * zero: success, matched
16362+ * plus: no error, but unmatched
16363+ */
16364+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
16365+{
16366+ int err;
cfc41e69 16367+ unsigned int gen, igflags;
5afbbe0d 16368+ aufs_bindex_t bindex, bbot;
4a4d8108 16369+ struct inode *h_inode, *h_dinode;
5527c038 16370+ struct dentry *h_dentry;
dece6358 16371+
4a4d8108
AM
16372+ /*
16373+ * before this function, if aufs got any iinfo lock, it must be only
16374+ * one, the parent dir.
16375+ * it can happen by UDBA and the obsoleted inode number.
16376+ */
16377+ err = -EIO;
16378+ if (unlikely(inode->i_ino == parent_ino(dentry)))
16379+ goto out;
16380+
027c5e7a 16381+ err = 1;
4a4d8108 16382+ ii_write_lock_new_child(inode);
5afbbe0d 16383+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 16384+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
16385+ bbot = au_ibbot(inode);
16386+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 16387+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
16388+ if (!h_inode || h_inode != h_dinode)
16389+ continue;
16390+
16391+ err = 0;
cfc41e69 16392+ gen = au_iigen(inode, &igflags);
537831f9 16393+ if (gen == au_digen(dentry)
cfc41e69 16394+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 16395+ break;
537831f9
AM
16396+
16397+ /* fully refresh inode using dentry */
16398+ err = au_refresh_hinode(inode, dentry);
16399+ if (!err)
16400+ au_update_iigen(inode, /*half*/0);
16401+ break;
1facf9fc 16402+ }
dece6358 16403+
4a4d8108
AM
16404+ if (unlikely(err))
16405+ ii_write_unlock(inode);
4f0767ce 16406+out:
1facf9fc 16407+ return err;
16408+}
1facf9fc 16409+
4a4d8108
AM
16410+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16411+ unsigned int d_type, ino_t *ino)
1facf9fc 16412+{
4a4d8108
AM
16413+ int err;
16414+ struct mutex *mtx;
1facf9fc 16415+
b752ccd1 16416+ /* prevent hardlinked inode number from race condition */
4a4d8108 16417+ mtx = NULL;
b752ccd1 16418+ if (d_type != DT_DIR) {
4a4d8108
AM
16419+ mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16420+ mutex_lock(mtx);
16421+ }
16422+ err = au_xino_read(sb, bindex, h_ino, ino);
16423+ if (unlikely(err))
16424+ goto out;
1308ab2a 16425+
4a4d8108
AM
16426+ if (!*ino) {
16427+ err = -EIO;
16428+ *ino = au_xino_new_ino(sb);
16429+ if (unlikely(!*ino))
1facf9fc 16430+ goto out;
4a4d8108
AM
16431+ err = au_xino_write(sb, bindex, h_ino, *ino);
16432+ if (unlikely(err))
1308ab2a 16433+ goto out;
1308ab2a 16434+ }
1facf9fc 16435+
4f0767ce 16436+out:
b752ccd1 16437+ if (mtx)
4a4d8108 16438+ mutex_unlock(mtx);
1facf9fc 16439+ return err;
16440+}
16441+
4a4d8108
AM
16442+/* successful returns with iinfo write_locked */
16443+/* todo: return with unlocked? */
16444+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 16445+{
5527c038 16446+ struct inode *inode, *h_inode;
4a4d8108
AM
16447+ struct dentry *h_dentry;
16448+ struct super_block *sb;
b752ccd1 16449+ struct mutex *mtx;
4a4d8108 16450+ ino_t h_ino, ino;
1716fcea 16451+ int err;
5afbbe0d 16452+ aufs_bindex_t btop;
1facf9fc 16453+
4a4d8108 16454+ sb = dentry->d_sb;
5afbbe0d
AM
16455+ btop = au_dbtop(dentry);
16456+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
16457+ h_inode = d_inode(h_dentry);
16458+ h_ino = h_inode->i_ino;
b752ccd1
AM
16459+
16460+ /*
16461+ * stop 'race'-ing between hardlinks under different
16462+ * parents.
16463+ */
16464+ mtx = NULL;
2000de60 16465+ if (!d_is_dir(h_dentry))
5afbbe0d 16466+ mtx = &au_sbr(sb, btop)->br_xino.xi_nondir_mtx;
b752ccd1 16467+
4f0767ce 16468+new_ino:
b752ccd1
AM
16469+ if (mtx)
16470+ mutex_lock(mtx);
5afbbe0d 16471+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
16472+ inode = ERR_PTR(err);
16473+ if (unlikely(err))
16474+ goto out;
b752ccd1 16475+
4a4d8108
AM
16476+ if (!ino) {
16477+ ino = au_xino_new_ino(sb);
16478+ if (unlikely(!ino)) {
16479+ inode = ERR_PTR(-EIO);
dece6358
AM
16480+ goto out;
16481+ }
16482+ }
1facf9fc 16483+
4a4d8108
AM
16484+ AuDbg("i%lu\n", (unsigned long)ino);
16485+ inode = au_iget_locked(sb, ino);
16486+ err = PTR_ERR(inode);
16487+ if (IS_ERR(inode))
1facf9fc 16488+ goto out;
1facf9fc 16489+
4a4d8108
AM
16490+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16491+ if (inode->i_state & I_NEW) {
16492+ ii_write_lock_new_child(inode);
16493+ err = set_inode(inode, dentry);
16494+ if (!err) {
16495+ unlock_new_inode(inode);
16496+ goto out; /* success */
16497+ }
1308ab2a 16498+
027c5e7a
AM
16499+ /*
16500+ * iget_failed() calls iput(), but we need to call
16501+ * ii_write_unlock() after iget_failed(). so dirty hack for
16502+ * i_count.
16503+ */
16504+ atomic_inc(&inode->i_count);
4a4d8108 16505+ iget_failed(inode);
027c5e7a 16506+ ii_write_unlock(inode);
5afbbe0d 16507+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
16508+ /* ignore this error */
16509+ goto out_iput;
16510+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
16511+ /*
16512+ * horrible race condition between lookup, readdir and copyup
16513+ * (or something).
16514+ */
16515+ if (mtx)
16516+ mutex_unlock(mtx);
027c5e7a
AM
16517+ err = reval_inode(inode, dentry);
16518+ if (unlikely(err < 0)) {
16519+ mtx = NULL;
16520+ goto out_iput;
16521+ }
16522+
b752ccd1
AM
16523+ if (!err) {
16524+ mtx = NULL;
4a4d8108 16525+ goto out; /* success */
b752ccd1
AM
16526+ } else if (mtx)
16527+ mutex_lock(mtx);
4a4d8108
AM
16528+ }
16529+
5527c038 16530+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 16531+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 16532+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 16533+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
16534+ (unsigned long)h_ino, (unsigned long)ino);
16535+ ino = 0;
5afbbe0d 16536+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
16537+ if (!err) {
16538+ iput(inode);
b752ccd1
AM
16539+ if (mtx)
16540+ mutex_unlock(mtx);
4a4d8108
AM
16541+ goto new_ino;
16542+ }
1308ab2a 16543+
4f0767ce 16544+out_iput:
4a4d8108 16545+ iput(inode);
4a4d8108 16546+ inode = ERR_PTR(err);
4f0767ce 16547+out:
b752ccd1
AM
16548+ if (mtx)
16549+ mutex_unlock(mtx);
4a4d8108 16550+ return inode;
1facf9fc 16551+}
16552+
4a4d8108 16553+/* ---------------------------------------------------------------------- */
1facf9fc 16554+
4a4d8108
AM
16555+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16556+ struct inode *inode)
16557+{
16558+ int err;
076b876e 16559+ struct inode *hi;
1facf9fc 16560+
4a4d8108 16561+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 16562+
4a4d8108
AM
16563+ /* pseudo-link after flushed may happen out of bounds */
16564+ if (!err
16565+ && inode
5afbbe0d
AM
16566+ && au_ibtop(inode) <= bindex
16567+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
16568+ /*
16569+ * permission check is unnecessary since vfsub routine
16570+ * will be called later
16571+ */
076b876e 16572+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
16573+ if (hi)
16574+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 16575+ }
16576+
4a4d8108
AM
16577+ return err;
16578+}
dece6358 16579+
4a4d8108
AM
16580+int au_test_h_perm(struct inode *h_inode, int mask)
16581+{
2dfbb274 16582+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
16583+ return 0;
16584+ return inode_permission(h_inode, mask);
16585+}
1facf9fc 16586+
4a4d8108
AM
16587+int au_test_h_perm_sio(struct inode *h_inode, int mask)
16588+{
16589+ if (au_test_nfs(h_inode->i_sb)
16590+ && (mask & MAY_WRITE)
16591+ && S_ISDIR(h_inode->i_mode))
16592+ mask |= MAY_READ; /* force permission check */
16593+ return au_test_h_perm(h_inode, mask);
1facf9fc 16594+}
7f207e10
AM
16595diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16596--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
16597+++ linux/fs/aufs/inode.h 2016-08-17 18:01:06.151889691 +0200
16598@@ -0,0 +1,700 @@
4a4d8108 16599+/*
8cdd5066 16600+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
16601+ *
16602+ * This program, aufs is free software; you can redistribute it and/or modify
16603+ * it under the terms of the GNU General Public License as published by
16604+ * the Free Software Foundation; either version 2 of the License, or
16605+ * (at your option) any later version.
16606+ *
16607+ * This program is distributed in the hope that it will be useful,
16608+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16609+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16610+ * GNU General Public License for more details.
16611+ *
16612+ * You should have received a copy of the GNU General Public License
523b37e3 16613+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 16614+ */
1facf9fc 16615+
1308ab2a 16616+/*
4a4d8108 16617+ * inode operations
1308ab2a 16618+ */
dece6358 16619+
4a4d8108
AM
16620+#ifndef __AUFS_INODE_H__
16621+#define __AUFS_INODE_H__
dece6358 16622+
4a4d8108 16623+#ifdef __KERNEL__
1308ab2a 16624+
4a4d8108 16625+#include <linux/fsnotify.h>
4a4d8108 16626+#include "rwsem.h"
1308ab2a 16627+
4a4d8108 16628+struct vfsmount;
1facf9fc 16629+
4a4d8108
AM
16630+struct au_hnotify {
16631+#ifdef CONFIG_AUFS_HNOTIFY
16632+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 16633+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 16634+ struct fsnotify_mark hn_mark;
4a4d8108 16635+#endif
f0c0a007
AM
16636+ union {
16637+ struct inode *hn_aufs_inode; /* no get/put */
16638+ struct llist_node hn_lnode; /* delayed free */
16639+ };
4a4d8108
AM
16640+#endif
16641+} ____cacheline_aligned_in_smp;
1facf9fc 16642+
4a4d8108
AM
16643+struct au_hinode {
16644+ struct inode *hi_inode;
16645+ aufs_bindex_t hi_id;
16646+#ifdef CONFIG_AUFS_HNOTIFY
16647+ struct au_hnotify *hi_notify;
16648+#endif
dece6358 16649+
4a4d8108
AM
16650+ /* reference to the copied-up whiteout with get/put */
16651+ struct dentry *hi_whdentry;
16652+};
dece6358 16653+
537831f9
AM
16654+/* ig_flags */
16655+#define AuIG_HALF_REFRESHED 1
16656+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
16657+#define au_ig_fset(flags, name) \
16658+ do { (flags) |= AuIG_##name; } while (0)
16659+#define au_ig_fclr(flags, name) \
16660+ do { (flags) &= ~AuIG_##name; } while (0)
16661+
16662+struct au_iigen {
be52b249 16663+ spinlock_t ig_spin;
537831f9
AM
16664+ __u32 ig_generation, ig_flags;
16665+};
16666+
4a4d8108
AM
16667+struct au_vdir;
16668+struct au_iinfo {
7a9e40b8 16669+ struct au_iigen ii_generation;
4a4d8108 16670+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 16671+
4a4d8108 16672+ struct au_rwsem ii_rwsem;
5afbbe0d 16673+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
16674+ __u32 ii_higen;
16675+ struct au_hinode *ii_hinode;
16676+ struct au_vdir *ii_vdir;
16677+};
1facf9fc 16678+
4a4d8108
AM
16679+struct au_icntnr {
16680+ struct au_iinfo iinfo;
16681+ struct inode vfs_inode;
f0c0a007
AM
16682+ union {
16683+ struct hlist_node plink;
16684+ struct llist_node lnode; /* delayed free */
16685+ };
4a4d8108 16686+} ____cacheline_aligned_in_smp;
1308ab2a 16687+
4a4d8108
AM
16688+/* au_pin flags */
16689+#define AuPin_DI_LOCKED 1
16690+#define AuPin_MNT_WRITE (1 << 1)
16691+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
16692+#define au_fset_pin(flags, name) \
16693+ do { (flags) |= AuPin_##name; } while (0)
16694+#define au_fclr_pin(flags, name) \
16695+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
16696+
16697+struct au_pin {
16698+ /* input */
16699+ struct dentry *dentry;
16700+ unsigned int udba;
16701+ unsigned char lsc_di, lsc_hi, flags;
16702+ aufs_bindex_t bindex;
16703+
16704+ /* output */
16705+ struct dentry *parent;
16706+ struct au_hinode *hdir;
16707+ struct vfsmount *h_mnt;
86dc4139
AM
16708+
16709+ /* temporary unlock/relock for copyup */
16710+ struct dentry *h_dentry, *h_parent;
16711+ struct au_branch *br;
16712+ struct task_struct *task;
4a4d8108 16713+};
1facf9fc 16714+
86dc4139 16715+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 16716+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 16717+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
16718+void au_pin_hdir_acquire_nest(struct au_pin *p);
16719+void au_pin_hdir_release(struct au_pin *p);
16720+
1308ab2a 16721+/* ---------------------------------------------------------------------- */
16722+
4a4d8108 16723+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 16724+{
5afbbe0d
AM
16725+ BUG_ON(is_bad_inode(inode));
16726+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 16727+}
1facf9fc 16728+
4a4d8108 16729+/* ---------------------------------------------------------------------- */
1facf9fc 16730+
4a4d8108
AM
16731+/* inode.c */
16732+struct inode *au_igrab(struct inode *inode);
b95c5147 16733+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 16734+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
16735+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16736+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16737+ unsigned int d_type, ino_t *ino);
16738+struct inode *au_new_inode(struct dentry *dentry, int must_new);
16739+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16740+ struct inode *inode);
16741+int au_test_h_perm(struct inode *h_inode, int mask);
16742+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 16743+
4a4d8108
AM
16744+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16745+ ino_t h_ino, unsigned int d_type, ino_t *ino)
16746+{
16747+#ifdef CONFIG_AUFS_SHWH
16748+ return au_ino(sb, bindex, h_ino, d_type, ino);
16749+#else
16750+ return 0;
16751+#endif
16752+}
1facf9fc 16753+
4a4d8108 16754+/* i_op.c */
b95c5147
AM
16755+enum {
16756+ AuIop_SYMLINK,
16757+ AuIop_DIR,
16758+ AuIop_OTHER,
16759+ AuIop_Last
16760+};
16761+extern struct inode_operations aufs_iop[AuIop_Last],
16762+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 16763+
4a4d8108
AM
16764+/* au_wr_dir flags */
16765+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
16766+#define AuWrDir_ISDIR (1 << 1)
16767+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 16768+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
16769+#define au_fset_wrdir(flags, name) \
16770+ do { (flags) |= AuWrDir_##name; } while (0)
16771+#define au_fclr_wrdir(flags, name) \
16772+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 16773+
4a4d8108
AM
16774+struct au_wr_dir_args {
16775+ aufs_bindex_t force_btgt;
16776+ unsigned char flags;
16777+};
16778+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16779+ struct au_wr_dir_args *args);
dece6358 16780+
4a4d8108
AM
16781+struct dentry *au_pinned_h_parent(struct au_pin *pin);
16782+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16783+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16784+ unsigned int udba, unsigned char flags);
16785+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16786+ unsigned int udba, unsigned char flags) __must_check;
16787+int au_do_pin(struct au_pin *pin) __must_check;
16788+void au_unpin(struct au_pin *pin);
c1595e42
JR
16789+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16790+
16791+#define AuIcpup_DID_CPUP 1
16792+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
16793+#define au_fset_icpup(flags, name) \
16794+ do { (flags) |= AuIcpup_##name; } while (0)
16795+#define au_fclr_icpup(flags, name) \
16796+ do { (flags) &= ~AuIcpup_##name; } while (0)
16797+
16798+struct au_icpup_args {
16799+ unsigned char flags;
16800+ unsigned char pin_flags;
16801+ aufs_bindex_t btgt;
16802+ unsigned int udba;
16803+ struct au_pin pin;
16804+ struct path h_path;
16805+ struct inode *h_inode;
16806+};
16807+
16808+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16809+ struct au_icpup_args *a);
16810+
16811+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
1facf9fc 16812+
4a4d8108
AM
16813+/* i_op_add.c */
16814+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16815+ struct dentry *h_parent, int isdir);
7eafdf33
AM
16816+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16817+ dev_t dev);
4a4d8108 16818+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 16819+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 16820+ bool want_excl);
b912730e
AM
16821+struct vfsub_aopen_args;
16822+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16823+ struct vfsub_aopen_args *args);
38d290e6 16824+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
16825+int aufs_link(struct dentry *src_dentry, struct inode *dir,
16826+ struct dentry *dentry);
7eafdf33 16827+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 16828+
4a4d8108
AM
16829+/* i_op_del.c */
16830+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16831+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16832+ struct dentry *h_parent, int isdir);
16833+int aufs_unlink(struct inode *dir, struct dentry *dentry);
16834+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 16835+
4a4d8108
AM
16836+/* i_op_ren.c */
16837+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16838+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16839+ struct inode *dir, struct dentry *dentry);
1facf9fc 16840+
4a4d8108
AM
16841+/* iinfo.c */
16842+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16843+void au_hiput(struct au_hinode *hinode);
16844+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16845+ struct dentry *h_wh);
16846+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 16847+
4a4d8108
AM
16848+/* hinode flags */
16849+#define AuHi_XINO 1
16850+#define AuHi_HNOTIFY (1 << 1)
16851+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
16852+#define au_fset_hi(flags, name) \
16853+ do { (flags) |= AuHi_##name; } while (0)
16854+#define au_fclr_hi(flags, name) \
16855+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 16856+
4a4d8108
AM
16857+#ifndef CONFIG_AUFS_HNOTIFY
16858+#undef AuHi_HNOTIFY
16859+#define AuHi_HNOTIFY 0
16860+#endif
1facf9fc 16861+
4a4d8108
AM
16862+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16863+ struct inode *h_inode, unsigned int flags);
1facf9fc 16864+
537831f9 16865+void au_update_iigen(struct inode *inode, int half);
4a4d8108 16866+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 16867+
4a4d8108 16868+void au_icntnr_init_once(void *_c);
5afbbe0d 16869+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
16870+int au_iinfo_init(struct inode *inode);
16871+void au_iinfo_fin(struct inode *inode);
5afbbe0d 16872+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr);
1308ab2a 16873+
e49829fe 16874+#ifdef CONFIG_PROC_FS
4a4d8108 16875+/* plink.c */
e49829fe 16876+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 16877+struct au_sbinfo;
e49829fe
JR
16878+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16879+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
16880+#ifdef CONFIG_AUFS_DEBUG
16881+void au_plink_list(struct super_block *sb);
16882+#else
16883+AuStubVoid(au_plink_list, struct super_block *sb)
16884+#endif
16885+int au_plink_test(struct inode *inode);
16886+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16887+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16888+ struct dentry *h_dentry);
e49829fe
JR
16889+void au_plink_put(struct super_block *sb, int verbose);
16890+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 16891+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
16892+#else
16893+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16894+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16895+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16896+AuStubVoid(au_plink_list, struct super_block *sb);
16897+AuStubInt0(au_plink_test, struct inode *inode);
16898+AuStub(struct dentry *, au_plink_lkup, return NULL,
16899+ struct inode *inode, aufs_bindex_t bindex);
16900+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16901+ struct dentry *h_dentry);
16902+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16903+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16904+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16905+#endif /* CONFIG_PROC_FS */
1facf9fc 16906+
c1595e42
JR
16907+#ifdef CONFIG_AUFS_XATTR
16908+/* xattr.c */
7e9cd9fe
AM
16909+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16910+ unsigned int verbose);
c1595e42 16911+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
5afbbe0d
AM
16912+ssize_t aufs_getxattr(struct dentry *dentry, struct inode *inode,
16913+ const char *name, void *value, size_t size);
16914+int aufs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
16915+ const void *value, size_t size, int flags);
c1595e42
JR
16916+int aufs_removexattr(struct dentry *dentry, const char *name);
16917+
16918+/* void au_xattr_init(struct super_block *sb); */
16919+#else
16920+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 16921+ int ignore_flags, unsigned int verbose);
c1595e42
JR
16922+/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16923+#endif
16924+
16925+#ifdef CONFIG_FS_POSIX_ACL
16926+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16927+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16928+#endif
16929+
16930+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16931+enum {
16932+ AU_XATTR_SET,
16933+ AU_XATTR_REMOVE,
16934+ AU_ACL_SET
16935+};
16936+
16937+struct au_srxattr {
16938+ int type;
16939+ union {
16940+ struct {
16941+ const char *name;
16942+ const void *value;
16943+ size_t size;
16944+ int flags;
16945+ } set;
16946+ struct {
16947+ const char *name;
16948+ } remove;
16949+ struct {
16950+ struct posix_acl *acl;
16951+ int type;
16952+ } acl_set;
16953+ } u;
16954+};
5afbbe0d
AM
16955+ssize_t au_srxattr(struct dentry *dentry, struct inode *inode,
16956+ struct au_srxattr *arg);
c1595e42
JR
16957+#endif
16958+
4a4d8108 16959+/* ---------------------------------------------------------------------- */
1308ab2a 16960+
4a4d8108
AM
16961+/* lock subclass for iinfo */
16962+enum {
16963+ AuLsc_II_CHILD, /* child first */
16964+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
16965+ AuLsc_II_CHILD3, /* copyup dirs */
16966+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
16967+ AuLsc_II_PARENT2,
16968+ AuLsc_II_PARENT3, /* copyup dirs */
16969+ AuLsc_II_NEW_CHILD
16970+};
1308ab2a 16971+
1facf9fc 16972+/*
4a4d8108
AM
16973+ * ii_read_lock_child, ii_write_lock_child,
16974+ * ii_read_lock_child2, ii_write_lock_child2,
16975+ * ii_read_lock_child3, ii_write_lock_child3,
16976+ * ii_read_lock_parent, ii_write_lock_parent,
16977+ * ii_read_lock_parent2, ii_write_lock_parent2,
16978+ * ii_read_lock_parent3, ii_write_lock_parent3,
16979+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 16980+ */
4a4d8108
AM
16981+#define AuReadLockFunc(name, lsc) \
16982+static inline void ii_read_lock_##name(struct inode *i) \
16983+{ \
16984+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16985+}
16986+
16987+#define AuWriteLockFunc(name, lsc) \
16988+static inline void ii_write_lock_##name(struct inode *i) \
16989+{ \
16990+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16991+}
16992+
16993+#define AuRWLockFuncs(name, lsc) \
16994+ AuReadLockFunc(name, lsc) \
16995+ AuWriteLockFunc(name, lsc)
16996+
16997+AuRWLockFuncs(child, CHILD);
16998+AuRWLockFuncs(child2, CHILD2);
16999+AuRWLockFuncs(child3, CHILD3);
17000+AuRWLockFuncs(parent, PARENT);
17001+AuRWLockFuncs(parent2, PARENT2);
17002+AuRWLockFuncs(parent3, PARENT3);
17003+AuRWLockFuncs(new_child, NEW_CHILD);
17004+
17005+#undef AuReadLockFunc
17006+#undef AuWriteLockFunc
17007+#undef AuRWLockFuncs
1facf9fc 17008+
17009+/*
4a4d8108 17010+ * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
1facf9fc 17011+ */
4a4d8108 17012+AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
1facf9fc 17013+
4a4d8108
AM
17014+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
17015+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
17016+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 17017+
4a4d8108 17018+/* ---------------------------------------------------------------------- */
1308ab2a 17019+
027c5e7a
AM
17020+static inline void au_icntnr_init(struct au_icntnr *c)
17021+{
17022+#ifdef CONFIG_AUFS_DEBUG
17023+ c->vfs_inode.i_mode = 0;
17024+#endif
17025+}
17026+
cfc41e69 17027+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 17028+{
537831f9
AM
17029+ unsigned int gen;
17030+ struct au_iinfo *iinfo;
be52b249 17031+ struct au_iigen *iigen;
537831f9
AM
17032+
17033+ iinfo = au_ii(inode);
be52b249
AM
17034+ iigen = &iinfo->ii_generation;
17035+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
17036+ if (igflags)
17037+ *igflags = iigen->ig_flags;
be52b249
AM
17038+ gen = iigen->ig_generation;
17039+ spin_unlock(&iigen->ig_spin);
537831f9
AM
17040+
17041+ return gen;
4a4d8108 17042+}
1308ab2a 17043+
4a4d8108
AM
17044+/* tiny test for inode number */
17045+/* tmpfs generation is too rough */
17046+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
17047+{
17048+ struct au_iinfo *iinfo;
1308ab2a 17049+
4a4d8108
AM
17050+ iinfo = au_ii(inode);
17051+ AuRwMustAnyLock(&iinfo->ii_rwsem);
17052+ return !(iinfo->ii_hsb1 == h_inode->i_sb
17053+ && iinfo->ii_higen == h_inode->i_generation);
17054+}
1308ab2a 17055+
4a4d8108
AM
17056+static inline void au_iigen_dec(struct inode *inode)
17057+{
537831f9 17058+ struct au_iinfo *iinfo;
be52b249 17059+ struct au_iigen *iigen;
537831f9
AM
17060+
17061+ iinfo = au_ii(inode);
be52b249
AM
17062+ iigen = &iinfo->ii_generation;
17063+ spin_lock(&iigen->ig_spin);
17064+ iigen->ig_generation--;
17065+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
17066+}
17067+
17068+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
17069+{
17070+ int err;
17071+
17072+ err = 0;
537831f9 17073+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
17074+ err = -EIO;
17075+
17076+ return err;
4a4d8108 17077+}
1308ab2a 17078+
4a4d8108 17079+/* ---------------------------------------------------------------------- */
1308ab2a 17080+
5afbbe0d
AM
17081+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
17082+ aufs_bindex_t bindex)
17083+{
17084+ return iinfo->ii_hinode + bindex;
17085+}
17086+
17087+static inline int au_is_bad_inode(struct inode *inode)
17088+{
17089+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
17090+}
17091+
4a4d8108
AM
17092+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
17093+ aufs_bindex_t bindex)
17094+{
17095+ IiMustAnyLock(inode);
5afbbe0d 17096+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 17097+}
1308ab2a 17098+
5afbbe0d 17099+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
17100+{
17101+ IiMustAnyLock(inode);
5afbbe0d 17102+ return au_ii(inode)->ii_btop;
4a4d8108 17103+}
1308ab2a 17104+
5afbbe0d 17105+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
17106+{
17107+ IiMustAnyLock(inode);
5afbbe0d 17108+ return au_ii(inode)->ii_bbot;
4a4d8108 17109+}
1308ab2a 17110+
4a4d8108
AM
17111+static inline struct au_vdir *au_ivdir(struct inode *inode)
17112+{
17113+ IiMustAnyLock(inode);
17114+ return au_ii(inode)->ii_vdir;
17115+}
1308ab2a 17116+
4a4d8108
AM
17117+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
17118+{
17119+ IiMustAnyLock(inode);
5afbbe0d 17120+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 17121+}
1308ab2a 17122+
5afbbe0d 17123+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17124+{
4a4d8108 17125+ IiMustWriteLock(inode);
5afbbe0d 17126+ au_ii(inode)->ii_btop = bindex;
4a4d8108 17127+}
1308ab2a 17128+
5afbbe0d 17129+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
17130+{
17131+ IiMustWriteLock(inode);
5afbbe0d 17132+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 17133+}
17134+
4a4d8108
AM
17135+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
17136+{
17137+ IiMustWriteLock(inode);
17138+ au_ii(inode)->ii_vdir = vdir;
17139+}
1facf9fc 17140+
4a4d8108 17141+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17142+{
4a4d8108 17143+ IiMustAnyLock(inode);
5afbbe0d 17144+ return au_hinode(au_ii(inode), bindex);
4a4d8108 17145+}
dece6358 17146+
4a4d8108 17147+/* ---------------------------------------------------------------------- */
1facf9fc 17148+
4a4d8108
AM
17149+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
17150+{
17151+ if (pin)
17152+ return pin->parent;
17153+ return NULL;
1facf9fc 17154+}
17155+
4a4d8108 17156+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 17157+{
4a4d8108
AM
17158+ if (pin && pin->hdir)
17159+ return pin->hdir->hi_inode;
17160+ return NULL;
1308ab2a 17161+}
1facf9fc 17162+
4a4d8108
AM
17163+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
17164+{
17165+ if (pin)
17166+ return pin->hdir;
17167+ return NULL;
17168+}
1facf9fc 17169+
4a4d8108 17170+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 17171+{
4a4d8108
AM
17172+ if (pin)
17173+ pin->dentry = dentry;
17174+}
1308ab2a 17175+
4a4d8108
AM
17176+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
17177+ unsigned char lflag)
17178+{
17179+ if (pin) {
7f207e10 17180+ if (lflag)
4a4d8108 17181+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 17182+ else
4a4d8108 17183+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 17184+ }
4a4d8108
AM
17185+}
17186+
7e9cd9fe 17187+#if 0 /* reserved */
4a4d8108
AM
17188+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
17189+{
17190+ if (pin) {
17191+ dput(pin->parent);
17192+ pin->parent = dget(parent);
1facf9fc 17193+ }
4a4d8108 17194+}
7e9cd9fe 17195+#endif
1facf9fc 17196+
4a4d8108
AM
17197+/* ---------------------------------------------------------------------- */
17198+
027c5e7a 17199+struct au_branch;
4a4d8108
AM
17200+#ifdef CONFIG_AUFS_HNOTIFY
17201+struct au_hnotify_op {
17202+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 17203+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
17204+
17205+ /*
17206+ * if it returns true, the the caller should free hinode->hi_notify,
17207+ * otherwise ->free() frees it.
17208+ */
17209+ int (*free)(struct au_hinode *hinode,
17210+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
17211+
17212+ void (*fin)(void);
17213+ int (*init)(void);
027c5e7a
AM
17214+
17215+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
17216+ void (*fin_br)(struct au_branch *br);
17217+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
17218+};
17219+
17220+/* hnotify.c */
027c5e7a 17221+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
17222+void au_hn_free(struct au_hinode *hinode);
17223+void au_hn_ctl(struct au_hinode *hinode, int do_set);
17224+void au_hn_reset(struct inode *inode, unsigned int flags);
17225+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17226+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
17227+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
17228+int au_hnotify_init_br(struct au_branch *br, int perm);
17229+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
17230+int __init au_hnotify_init(void);
17231+void au_hnotify_fin(void);
17232+
7f207e10 17233+/* hfsnotify.c */
4a4d8108
AM
17234+extern const struct au_hnotify_op au_hnotify_op;
17235+
17236+static inline
17237+void au_hn_init(struct au_hinode *hinode)
17238+{
17239+ hinode->hi_notify = NULL;
1308ab2a 17240+}
17241+
53392da6
AM
17242+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
17243+{
17244+ return hinode->hi_notify;
17245+}
17246+
4a4d8108 17247+#else
c1595e42
JR
17248+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
17249+ struct au_hinode *hinode __maybe_unused,
17250+ struct inode *inode __maybe_unused)
17251+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
17252+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
17253+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
17254+ int do_set __maybe_unused)
17255+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
17256+ unsigned int flags __maybe_unused)
027c5e7a
AM
17257+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
17258+ struct au_branch *br __maybe_unused,
17259+ int perm __maybe_unused)
17260+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
17261+ int perm __maybe_unused)
17262+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
17263+AuStubInt0(__init au_hnotify_init, void)
17264+AuStubVoid(au_hnotify_fin, void)
17265+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
17266+#endif /* CONFIG_AUFS_HNOTIFY */
17267+
17268+static inline void au_hn_suspend(struct au_hinode *hdir)
17269+{
17270+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 17271+}
17272+
4a4d8108 17273+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 17274+{
4a4d8108
AM
17275+ au_hn_ctl(hdir, /*do_set*/1);
17276+}
1308ab2a 17277+
5afbbe0d 17278+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 17279+{
febd17d6 17280+ inode_lock(hdir->hi_inode);
4a4d8108
AM
17281+ au_hn_suspend(hdir);
17282+}
dece6358 17283+
5afbbe0d 17284+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
17285+ unsigned int sc __maybe_unused)
17286+{
febd17d6 17287+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 17288+ au_hn_suspend(hdir);
1facf9fc 17289+}
1facf9fc 17290+
5afbbe0d 17291+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
17292+{
17293+ au_hn_resume(hdir);
febd17d6 17294+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
17295+}
17296+
17297+#endif /* __KERNEL__ */
17298+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
17299diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
17300--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 17301+++ linux/fs/aufs/ioctl.c 2016-07-25 19:05:34.814493242 +0200
c1595e42 17302@@ -0,0 +1,219 @@
4a4d8108 17303+/*
8cdd5066 17304+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
17305+ *
17306+ * This program, aufs is free software; you can redistribute it and/or modify
17307+ * it under the terms of the GNU General Public License as published by
17308+ * the Free Software Foundation; either version 2 of the License, or
17309+ * (at your option) any later version.
17310+ *
17311+ * This program is distributed in the hope that it will be useful,
17312+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17313+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17314+ * GNU General Public License for more details.
17315+ *
17316+ * You should have received a copy of the GNU General Public License
523b37e3 17317+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
17318+ */
17319+
17320+/*
17321+ * ioctl
17322+ * plink-management and readdir in userspace.
17323+ * assist the pathconf(3) wrapper library.
c2b27bf2 17324+ * move-down
076b876e 17325+ * File-based Hierarchical Storage Management.
4a4d8108
AM
17326+ */
17327+
c2b27bf2
AM
17328+#include <linux/compat.h>
17329+#include <linux/file.h>
4a4d8108
AM
17330+#include "aufs.h"
17331+
1e00d052 17332+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
17333+{
17334+ int err, fd;
5afbbe0d 17335+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
17336+ struct file *h_file;
17337+ struct super_block *sb;
17338+ struct dentry *root;
1e00d052
AM
17339+ struct au_branch *br;
17340+ struct aufs_wbr_fd wbrfd = {
17341+ .oflags = au_dir_roflags,
17342+ .brid = -1
17343+ };
17344+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
17345+ | O_NOATIME | O_CLOEXEC;
4a4d8108 17346+
1e00d052
AM
17347+ AuDebugOn(wbrfd.oflags & ~valid);
17348+
17349+ if (arg) {
17350+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
17351+ if (unlikely(err)) {
17352+ err = -EFAULT;
17353+ goto out;
17354+ }
17355+
17356+ err = -EINVAL;
17357+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
17358+ wbrfd.oflags |= au_dir_roflags;
17359+ AuDbg("0%o\n", wbrfd.oflags);
17360+ if (unlikely(wbrfd.oflags & ~valid))
17361+ goto out;
17362+ }
17363+
2000de60 17364+ fd = get_unused_fd_flags(0);
1e00d052
AM
17365+ err = fd;
17366+ if (unlikely(fd < 0))
4a4d8108 17367+ goto out;
4a4d8108 17368+
1e00d052 17369+ h_file = ERR_PTR(-EINVAL);
4a4d8108 17370+ wbi = 0;
1e00d052 17371+ br = NULL;
4a4d8108
AM
17372+ sb = path->dentry->d_sb;
17373+ root = sb->s_root;
17374+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 17375+ bbot = au_sbbot(sb);
1e00d052
AM
17376+ if (wbrfd.brid >= 0) {
17377+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 17378+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
17379+ goto out_unlock;
17380+ }
17381+
17382+ h_file = ERR_PTR(-ENOENT);
17383+ br = au_sbr(sb, wbi);
17384+ if (!au_br_writable(br->br_perm)) {
17385+ if (arg)
17386+ goto out_unlock;
17387+
17388+ bindex = wbi + 1;
17389+ wbi = -1;
5afbbe0d 17390+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
17391+ br = au_sbr(sb, bindex);
17392+ if (au_br_writable(br->br_perm)) {
4a4d8108 17393+ wbi = bindex;
1e00d052 17394+ br = au_sbr(sb, wbi);
4a4d8108
AM
17395+ break;
17396+ }
17397+ }
4a4d8108
AM
17398+ }
17399+ AuDbg("wbi %d\n", wbi);
1e00d052 17400+ if (wbi >= 0)
392086de
AM
17401+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17402+ /*force_wr*/0);
1e00d052
AM
17403+
17404+out_unlock:
4a4d8108
AM
17405+ aufs_read_unlock(root, AuLock_IR);
17406+ err = PTR_ERR(h_file);
17407+ if (IS_ERR(h_file))
17408+ goto out_fd;
17409+
5afbbe0d 17410+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
17411+ fd_install(fd, h_file);
17412+ err = fd;
17413+ goto out; /* success */
17414+
4f0767ce 17415+out_fd:
4a4d8108 17416+ put_unused_fd(fd);
4f0767ce 17417+out:
1e00d052 17418+ AuTraceErr(err);
4a4d8108
AM
17419+ return err;
17420+}
17421+
17422+/* ---------------------------------------------------------------------- */
17423+
17424+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17425+{
17426+ long err;
c1595e42 17427+ struct dentry *dentry;
4a4d8108
AM
17428+
17429+ switch (cmd) {
4a4d8108
AM
17430+ case AUFS_CTL_RDU:
17431+ case AUFS_CTL_RDU_INO:
17432+ err = au_rdu_ioctl(file, cmd, arg);
17433+ break;
17434+
17435+ case AUFS_CTL_WBR_FD:
1e00d052 17436+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
17437+ break;
17438+
027c5e7a
AM
17439+ case AUFS_CTL_IBUSY:
17440+ err = au_ibusy_ioctl(file, arg);
17441+ break;
17442+
076b876e
AM
17443+ case AUFS_CTL_BRINFO:
17444+ err = au_brinfo_ioctl(file, arg);
17445+ break;
17446+
17447+ case AUFS_CTL_FHSM_FD:
2000de60 17448+ dentry = file->f_path.dentry;
c1595e42
JR
17449+ if (IS_ROOT(dentry))
17450+ err = au_fhsm_fd(dentry->d_sb, arg);
17451+ else
17452+ err = -ENOTTY;
076b876e
AM
17453+ break;
17454+
4a4d8108
AM
17455+ default:
17456+ /* do not call the lower */
17457+ AuDbg("0x%x\n", cmd);
17458+ err = -ENOTTY;
17459+ }
17460+
17461+ AuTraceErr(err);
17462+ return err;
17463+}
17464+
17465+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17466+{
17467+ long err;
17468+
17469+ switch (cmd) {
c2b27bf2 17470+ case AUFS_CTL_MVDOWN:
2000de60 17471+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
17472+ break;
17473+
4a4d8108 17474+ case AUFS_CTL_WBR_FD:
1e00d052 17475+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
17476+ break;
17477+
17478+ default:
17479+ /* do not call the lower */
17480+ AuDbg("0x%x\n", cmd);
17481+ err = -ENOTTY;
17482+ }
17483+
17484+ AuTraceErr(err);
17485+ return err;
17486+}
b752ccd1
AM
17487+
17488+#ifdef CONFIG_COMPAT
17489+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17490+ unsigned long arg)
17491+{
17492+ long err;
17493+
17494+ switch (cmd) {
17495+ case AUFS_CTL_RDU:
17496+ case AUFS_CTL_RDU_INO:
17497+ err = au_rdu_compat_ioctl(file, cmd, arg);
17498+ break;
17499+
027c5e7a
AM
17500+ case AUFS_CTL_IBUSY:
17501+ err = au_ibusy_compat_ioctl(file, arg);
17502+ break;
17503+
076b876e
AM
17504+ case AUFS_CTL_BRINFO:
17505+ err = au_brinfo_compat_ioctl(file, arg);
17506+ break;
17507+
b752ccd1
AM
17508+ default:
17509+ err = aufs_ioctl_dir(file, cmd, arg);
17510+ }
17511+
17512+ AuTraceErr(err);
17513+ return err;
17514+}
17515+
b752ccd1
AM
17516+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17517+ unsigned long arg)
17518+{
17519+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17520+}
17521+#endif
7f207e10
AM
17522diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17523--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 17524+++ linux/fs/aufs/i_op_add.c 2016-08-17 18:01:06.148556271 +0200
5afbbe0d 17525@@ -0,0 +1,924 @@
4a4d8108 17526+/*
8cdd5066 17527+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
17528+ *
17529+ * This program, aufs is free software; you can redistribute it and/or modify
17530+ * it under the terms of the GNU General Public License as published by
17531+ * the Free Software Foundation; either version 2 of the License, or
17532+ * (at your option) any later version.
17533+ *
17534+ * This program is distributed in the hope that it will be useful,
17535+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17536+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17537+ * GNU General Public License for more details.
17538+ *
17539+ * You should have received a copy of the GNU General Public License
523b37e3 17540+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
17541+ */
17542+
17543+/*
17544+ * inode operations (add entry)
17545+ */
17546+
17547+#include "aufs.h"
17548+
17549+/*
17550+ * final procedure of adding a new entry, except link(2).
17551+ * remove whiteout, instantiate, copyup the parent dir's times and size
17552+ * and update version.
17553+ * if it failed, re-create the removed whiteout.
17554+ */
17555+static int epilog(struct inode *dir, aufs_bindex_t bindex,
17556+ struct dentry *wh_dentry, struct dentry *dentry)
17557+{
17558+ int err, rerr;
17559+ aufs_bindex_t bwh;
17560+ struct path h_path;
076b876e 17561+ struct super_block *sb;
4a4d8108
AM
17562+ struct inode *inode, *h_dir;
17563+ struct dentry *wh;
17564+
17565+ bwh = -1;
076b876e 17566+ sb = dir->i_sb;
4a4d8108 17567+ if (wh_dentry) {
5527c038 17568+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
17569+ IMustLock(h_dir);
17570+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17571+ bwh = au_dbwh(dentry);
17572+ h_path.dentry = wh_dentry;
076b876e 17573+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
17574+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17575+ dentry);
17576+ if (unlikely(err))
17577+ goto out;
17578+ }
17579+
17580+ inode = au_new_inode(dentry, /*must_new*/1);
17581+ if (!IS_ERR(inode)) {
17582+ d_instantiate(dentry, inode);
5527c038 17583+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 17584+ IMustLock(dir);
b912730e 17585+ au_dir_ts(dir, bindex);
4a4d8108 17586+ dir->i_version++;
076b876e 17587+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
17588+ return 0; /* success */
17589+ }
17590+
17591+ err = PTR_ERR(inode);
17592+ if (!wh_dentry)
17593+ goto out;
17594+
17595+ /* revert */
17596+ /* dir inode is locked */
17597+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17598+ rerr = PTR_ERR(wh);
17599+ if (IS_ERR(wh)) {
523b37e3
AM
17600+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17601+ dentry, err, rerr);
4a4d8108
AM
17602+ err = -EIO;
17603+ } else
17604+ dput(wh);
17605+
4f0767ce 17606+out:
4a4d8108
AM
17607+ return err;
17608+}
17609+
027c5e7a
AM
17610+static int au_d_may_add(struct dentry *dentry)
17611+{
17612+ int err;
17613+
17614+ err = 0;
17615+ if (unlikely(d_unhashed(dentry)))
17616+ err = -ENOENT;
5527c038 17617+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
17618+ err = -EEXIST;
17619+ return err;
17620+}
17621+
4a4d8108
AM
17622+/*
17623+ * simple tests for the adding inode operations.
17624+ * following the checks in vfs, plus the parent-child relationship.
17625+ */
17626+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17627+ struct dentry *h_parent, int isdir)
17628+{
17629+ int err;
17630+ umode_t h_mode;
17631+ struct dentry *h_dentry;
17632+ struct inode *h_inode;
17633+
17634+ err = -ENAMETOOLONG;
17635+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17636+ goto out;
17637+
17638+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 17639+ if (d_really_is_negative(dentry)) {
4a4d8108 17640+ err = -EEXIST;
5527c038 17641+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
17642+ goto out;
17643+ } else {
17644+ /* rename(2) case */
17645+ err = -EIO;
5527c038
JR
17646+ if (unlikely(d_is_negative(h_dentry)))
17647+ goto out;
17648+ h_inode = d_inode(h_dentry);
17649+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
17650+ goto out;
17651+
17652+ h_mode = h_inode->i_mode;
17653+ if (!isdir) {
17654+ err = -EISDIR;
17655+ if (unlikely(S_ISDIR(h_mode)))
17656+ goto out;
17657+ } else if (unlikely(!S_ISDIR(h_mode))) {
17658+ err = -ENOTDIR;
17659+ goto out;
17660+ }
17661+ }
17662+
17663+ err = 0;
17664+ /* expected parent dir is locked */
17665+ if (unlikely(h_parent != h_dentry->d_parent))
17666+ err = -EIO;
17667+
4f0767ce 17668+out:
4a4d8108
AM
17669+ AuTraceErr(err);
17670+ return err;
17671+}
17672+
17673+/*
17674+ * initial procedure of adding a new entry.
17675+ * prepare writable branch and the parent dir, lock it,
17676+ * and lookup whiteout for the new entry.
17677+ */
17678+static struct dentry*
17679+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17680+ struct dentry *src_dentry, struct au_pin *pin,
17681+ struct au_wr_dir_args *wr_dir_args)
17682+{
17683+ struct dentry *wh_dentry, *h_parent;
17684+ struct super_block *sb;
17685+ struct au_branch *br;
17686+ int err;
17687+ unsigned int udba;
17688+ aufs_bindex_t bcpup;
17689+
523b37e3 17690+ AuDbg("%pd\n", dentry);
4a4d8108
AM
17691+
17692+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17693+ bcpup = err;
17694+ wh_dentry = ERR_PTR(err);
17695+ if (unlikely(err < 0))
17696+ goto out;
17697+
17698+ sb = dentry->d_sb;
17699+ udba = au_opt_udba(sb);
17700+ err = au_pin(pin, dentry, bcpup, udba,
17701+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17702+ wh_dentry = ERR_PTR(err);
17703+ if (unlikely(err))
17704+ goto out;
17705+
17706+ h_parent = au_pinned_h_parent(pin);
17707+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 17708+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
17709+ err = au_may_add(dentry, bcpup, h_parent,
17710+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17711+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17712+ err = -ENAMETOOLONG;
17713+ wh_dentry = ERR_PTR(err);
17714+ if (unlikely(err))
17715+ goto out_unpin;
17716+
17717+ br = au_sbr(sb, bcpup);
17718+ if (dt) {
17719+ struct path tmp = {
17720+ .dentry = h_parent,
86dc4139 17721+ .mnt = au_br_mnt(br)
4a4d8108
AM
17722+ };
17723+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17724+ }
17725+
17726+ wh_dentry = NULL;
17727+ if (bcpup != au_dbwh(dentry))
17728+ goto out; /* success */
17729+
2000de60
JR
17730+ /*
17731+ * ENAMETOOLONG here means that if we allowed create such name, then it
17732+ * would not be able to removed in the future. So we don't allow such
17733+ * name here and we don't handle ENAMETOOLONG differently here.
17734+ */
4a4d8108
AM
17735+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17736+
4f0767ce 17737+out_unpin:
4a4d8108
AM
17738+ if (IS_ERR(wh_dentry))
17739+ au_unpin(pin);
4f0767ce 17740+out:
4a4d8108
AM
17741+ return wh_dentry;
17742+}
17743+
17744+/* ---------------------------------------------------------------------- */
17745+
17746+enum { Mknod, Symlink, Creat };
17747+struct simple_arg {
17748+ int type;
17749+ union {
17750+ struct {
b912730e
AM
17751+ umode_t mode;
17752+ bool want_excl;
17753+ bool try_aopen;
17754+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
17755+ } c;
17756+ struct {
17757+ const char *symname;
17758+ } s;
17759+ struct {
7eafdf33 17760+ umode_t mode;
4a4d8108
AM
17761+ dev_t dev;
17762+ } m;
17763+ } u;
17764+};
17765+
17766+static int add_simple(struct inode *dir, struct dentry *dentry,
17767+ struct simple_arg *arg)
17768+{
076b876e 17769+ int err, rerr;
5afbbe0d 17770+ aufs_bindex_t btop;
4a4d8108 17771+ unsigned char created;
b912730e
AM
17772+ const unsigned char try_aopen
17773+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
17774+ struct dentry *wh_dentry, *parent;
17775+ struct inode *h_dir;
b912730e
AM
17776+ struct super_block *sb;
17777+ struct au_branch *br;
c2b27bf2
AM
17778+ /* to reuduce stack size */
17779+ struct {
17780+ struct au_dtime dt;
17781+ struct au_pin pin;
17782+ struct path h_path;
17783+ struct au_wr_dir_args wr_dir_args;
17784+ } *a;
4a4d8108 17785+
523b37e3 17786+ AuDbg("%pd\n", dentry);
4a4d8108
AM
17787+ IMustLock(dir);
17788+
c2b27bf2
AM
17789+ err = -ENOMEM;
17790+ a = kmalloc(sizeof(*a), GFP_NOFS);
17791+ if (unlikely(!a))
17792+ goto out;
17793+ a->wr_dir_args.force_btgt = -1;
17794+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17795+
4a4d8108 17796+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
17797+ if (!try_aopen) {
17798+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17799+ if (unlikely(err))
17800+ goto out_free;
17801+ }
027c5e7a
AM
17802+ err = au_d_may_add(dentry);
17803+ if (unlikely(err))
17804+ goto out_unlock;
b912730e
AM
17805+ if (!try_aopen)
17806+ di_write_lock_parent(parent);
c2b27bf2
AM
17807+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17808+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
17809+ err = PTR_ERR(wh_dentry);
17810+ if (IS_ERR(wh_dentry))
027c5e7a 17811+ goto out_parent;
4a4d8108 17812+
5afbbe0d 17813+ btop = au_dbtop(dentry);
b912730e 17814+ sb = dentry->d_sb;
5afbbe0d
AM
17815+ br = au_sbr(sb, btop);
17816+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 17817+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 17818+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
17819+ switch (arg->type) {
17820+ case Creat:
b912730e
AM
17821+ err = 0;
17822+ if (!try_aopen || !h_dir->i_op->atomic_open)
17823+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17824+ arg->u.c.want_excl);
17825+ else
17826+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17827+ arg->u.c.aopen, br);
4a4d8108
AM
17828+ break;
17829+ case Symlink:
c2b27bf2 17830+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
17831+ break;
17832+ case Mknod:
c2b27bf2
AM
17833+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17834+ arg->u.m.dev);
4a4d8108
AM
17835+ break;
17836+ default:
17837+ BUG();
17838+ }
17839+ created = !err;
17840+ if (!err)
5afbbe0d 17841+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
17842+
17843+ /* revert */
5527c038 17844+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
17845+ /* no delegation since it is just created */
17846+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17847+ /*force*/0);
4a4d8108 17848+ if (rerr) {
523b37e3
AM
17849+ AuIOErr("%pd revert failure(%d, %d)\n",
17850+ dentry, err, rerr);
4a4d8108
AM
17851+ err = -EIO;
17852+ }
c2b27bf2 17853+ au_dtime_revert(&a->dt);
4a4d8108
AM
17854+ }
17855+
b912730e
AM
17856+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
17857+ *arg->u.c.aopen->opened |= FILE_CREATED;
17858+
c2b27bf2 17859+ au_unpin(&a->pin);
4a4d8108
AM
17860+ dput(wh_dentry);
17861+
027c5e7a 17862+out_parent:
b912730e
AM
17863+ if (!try_aopen)
17864+ di_write_unlock(parent);
027c5e7a 17865+out_unlock:
4a4d8108 17866+ if (unlikely(err)) {
5afbbe0d 17867+ au_update_dbtop(dentry);
4a4d8108
AM
17868+ d_drop(dentry);
17869+ }
b912730e
AM
17870+ if (!try_aopen)
17871+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 17872+out_free:
f0c0a007 17873+ au_delayed_kfree(a);
027c5e7a 17874+out:
4a4d8108
AM
17875+ return err;
17876+}
17877+
7eafdf33
AM
17878+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17879+ dev_t dev)
4a4d8108
AM
17880+{
17881+ struct simple_arg arg = {
17882+ .type = Mknod,
17883+ .u.m = {
17884+ .mode = mode,
17885+ .dev = dev
17886+ }
17887+ };
17888+ return add_simple(dir, dentry, &arg);
17889+}
17890+
17891+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17892+{
17893+ struct simple_arg arg = {
17894+ .type = Symlink,
17895+ .u.s.symname = symname
17896+ };
17897+ return add_simple(dir, dentry, &arg);
17898+}
17899+
7eafdf33 17900+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 17901+ bool want_excl)
4a4d8108
AM
17902+{
17903+ struct simple_arg arg = {
17904+ .type = Creat,
17905+ .u.c = {
b4510431
AM
17906+ .mode = mode,
17907+ .want_excl = want_excl
4a4d8108
AM
17908+ }
17909+ };
17910+ return add_simple(dir, dentry, &arg);
17911+}
17912+
b912730e
AM
17913+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17914+ struct vfsub_aopen_args *aopen_args)
17915+{
17916+ struct simple_arg arg = {
17917+ .type = Creat,
17918+ .u.c = {
17919+ .mode = aopen_args->create_mode,
17920+ .want_excl = aopen_args->open_flag & O_EXCL,
17921+ .try_aopen = true,
17922+ .aopen = aopen_args
17923+ }
17924+ };
17925+ return add_simple(dir, dentry, &arg);
17926+}
17927+
38d290e6
JR
17928+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17929+{
17930+ int err;
17931+ aufs_bindex_t bindex;
17932+ struct super_block *sb;
17933+ struct dentry *parent, *h_parent, *h_dentry;
17934+ struct inode *h_dir, *inode;
17935+ struct vfsmount *h_mnt;
17936+ struct au_wr_dir_args wr_dir_args = {
17937+ .force_btgt = -1,
17938+ .flags = AuWrDir_TMPFILE
17939+ };
17940+
17941+ /* copy-up may happen */
febd17d6 17942+ inode_lock(dir);
38d290e6
JR
17943+
17944+ sb = dir->i_sb;
17945+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17946+ if (unlikely(err))
17947+ goto out;
17948+
17949+ err = au_di_init(dentry);
17950+ if (unlikely(err))
17951+ goto out_si;
17952+
17953+ err = -EBUSY;
17954+ parent = d_find_any_alias(dir);
17955+ AuDebugOn(!parent);
17956+ di_write_lock_parent(parent);
5527c038 17957+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
17958+ goto out_parent;
17959+
17960+ err = au_digen_test(parent, au_sigen(sb));
17961+ if (unlikely(err))
17962+ goto out_parent;
17963+
5afbbe0d
AM
17964+ bindex = au_dbtop(parent);
17965+ au_set_dbtop(dentry, bindex);
17966+ au_set_dbbot(dentry, bindex);
38d290e6
JR
17967+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17968+ bindex = err;
17969+ if (unlikely(err < 0))
17970+ goto out_parent;
17971+
17972+ err = -EOPNOTSUPP;
17973+ h_dir = au_h_iptr(dir, bindex);
17974+ if (unlikely(!h_dir->i_op->tmpfile))
17975+ goto out_parent;
17976+
17977+ h_mnt = au_sbr_mnt(sb, bindex);
17978+ err = vfsub_mnt_want_write(h_mnt);
17979+ if (unlikely(err))
17980+ goto out_parent;
17981+
17982+ h_parent = au_h_dptr(parent, bindex);
5527c038 17983+ err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
38d290e6
JR
17984+ if (unlikely(err))
17985+ goto out_mnt;
17986+
17987+ err = -ENOMEM;
17988+ h_dentry = d_alloc(h_parent, &dentry->d_name);
17989+ if (unlikely(!h_dentry))
17990+ goto out_mnt;
17991+
17992+ err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17993+ if (unlikely(err))
17994+ goto out_dentry;
17995+
5afbbe0d
AM
17996+ au_set_dbtop(dentry, bindex);
17997+ au_set_dbbot(dentry, bindex);
38d290e6
JR
17998+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
17999+ inode = au_new_inode(dentry, /*must_new*/1);
18000+ if (IS_ERR(inode)) {
18001+ err = PTR_ERR(inode);
18002+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
18003+ au_set_dbtop(dentry, -1);
18004+ au_set_dbbot(dentry, -1);
38d290e6
JR
18005+ } else {
18006+ if (!inode->i_nlink)
18007+ set_nlink(inode, 1);
18008+ d_tmpfile(dentry, inode);
18009+ au_di(dentry)->di_tmpfile = 1;
18010+
18011+ /* update without i_mutex */
5afbbe0d 18012+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
18013+ au_cpup_attr_timesizes(dir);
18014+ }
18015+
18016+out_dentry:
18017+ dput(h_dentry);
18018+out_mnt:
18019+ vfsub_mnt_drop_write(h_mnt);
18020+out_parent:
18021+ di_write_unlock(parent);
18022+ dput(parent);
18023+ di_write_unlock(dentry);
5afbbe0d 18024+ if (unlikely(err)) {
38d290e6
JR
18025+ au_di_fin(dentry);
18026+ dentry->d_fsdata = NULL;
18027+ }
18028+out_si:
18029+ si_read_unlock(sb);
18030+out:
febd17d6 18031+ inode_unlock(dir);
38d290e6
JR
18032+ return err;
18033+}
18034+
4a4d8108
AM
18035+/* ---------------------------------------------------------------------- */
18036+
18037+struct au_link_args {
18038+ aufs_bindex_t bdst, bsrc;
18039+ struct au_pin pin;
18040+ struct path h_path;
18041+ struct dentry *src_parent, *parent;
18042+};
18043+
18044+static int au_cpup_before_link(struct dentry *src_dentry,
18045+ struct au_link_args *a)
18046+{
18047+ int err;
18048+ struct dentry *h_src_dentry;
c2b27bf2
AM
18049+ struct au_cp_generic cpg = {
18050+ .dentry = src_dentry,
18051+ .bdst = a->bdst,
18052+ .bsrc = a->bsrc,
18053+ .len = -1,
18054+ .pin = &a->pin,
18055+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
18056+ };
4a4d8108
AM
18057+
18058+ di_read_lock_parent(a->src_parent, AuLock_IR);
18059+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
18060+ if (unlikely(err))
18061+ goto out;
18062+
18063+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
18064+ err = au_pin(&a->pin, src_dentry, a->bdst,
18065+ au_opt_udba(src_dentry->d_sb),
18066+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18067+ if (unlikely(err))
18068+ goto out;
367653fa 18069+
c2b27bf2 18070+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
18071+ au_unpin(&a->pin);
18072+
4f0767ce 18073+out:
4a4d8108
AM
18074+ di_read_unlock(a->src_parent, AuLock_IR);
18075+ return err;
18076+}
18077+
86dc4139
AM
18078+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
18079+ struct au_link_args *a)
4a4d8108
AM
18080+{
18081+ int err;
18082+ unsigned char plink;
5afbbe0d 18083+ aufs_bindex_t bbot;
4a4d8108 18084+ struct dentry *h_src_dentry;
523b37e3 18085+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
18086+ struct super_block *sb;
18087+ struct file *h_file;
18088+
18089+ plink = 0;
18090+ h_inode = NULL;
18091+ sb = src_dentry->d_sb;
5527c038 18092+ inode = d_inode(src_dentry);
5afbbe0d 18093+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
18094+ h_inode = au_h_iptr(inode, a->bdst);
18095+ if (!h_inode || !h_inode->i_nlink) {
18096+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
18097+ bbot = au_dbbot(dentry);
18098+ if (bbot < a->bsrc)
18099+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
18100+ au_set_h_dptr(dentry, a->bsrc,
18101+ dget(au_h_dptr(src_dentry, a->bsrc)));
18102+ dget(a->h_path.dentry);
18103+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
18104+ AuDbg("temporary d_inode...\n");
18105+ spin_lock(&dentry->d_lock);
5527c038 18106+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 18107+ spin_unlock(&dentry->d_lock);
392086de 18108+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 18109+ if (IS_ERR(h_file))
4a4d8108 18110+ err = PTR_ERR(h_file);
86dc4139 18111+ else {
c2b27bf2
AM
18112+ struct au_cp_generic cpg = {
18113+ .dentry = dentry,
18114+ .bdst = a->bdst,
18115+ .bsrc = -1,
18116+ .len = -1,
18117+ .pin = &a->pin,
18118+ .flags = AuCpup_KEEPLINO
18119+ };
18120+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
18121+ au_h_open_post(dentry, a->bsrc, h_file);
18122+ if (!err) {
18123+ dput(a->h_path.dentry);
18124+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18125+ } else
18126+ au_set_h_dptr(dentry, a->bdst,
18127+ a->h_path.dentry);
18128+ }
c1595e42 18129+ spin_lock(&dentry->d_lock);
86dc4139 18130+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
18131+ spin_unlock(&dentry->d_lock);
18132+ AuDbg("temporary d_inode...done\n");
86dc4139 18133+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 18134+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
18135+ } else {
18136+ /* the inode of src_dentry already exists on a.bdst branch */
18137+ h_src_dentry = d_find_alias(h_inode);
18138+ if (!h_src_dentry && au_plink_test(inode)) {
18139+ plink = 1;
18140+ h_src_dentry = au_plink_lkup(inode, a->bdst);
18141+ err = PTR_ERR(h_src_dentry);
18142+ if (IS_ERR(h_src_dentry))
18143+ goto out;
18144+
5527c038 18145+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
18146+ dput(h_src_dentry);
18147+ h_src_dentry = NULL;
18148+ }
18149+
18150+ }
18151+ if (h_src_dentry) {
523b37e3 18152+ delegated = NULL;
4a4d8108 18153+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
18154+ &a->h_path, &delegated);
18155+ if (unlikely(err == -EWOULDBLOCK)) {
18156+ pr_warn("cannot retry for NFSv4 delegation"
18157+ " for an internal link\n");
18158+ iput(delegated);
18159+ }
4a4d8108
AM
18160+ dput(h_src_dentry);
18161+ } else {
18162+ AuIOErr("no dentry found for hi%lu on b%d\n",
18163+ h_inode->i_ino, a->bdst);
18164+ err = -EIO;
18165+ }
18166+ }
18167+
18168+ if (!err && !plink)
18169+ au_plink_append(inode, a->bdst, a->h_path.dentry);
18170+
18171+out:
2cbb1c4b 18172+ AuTraceErr(err);
4a4d8108
AM
18173+ return err;
18174+}
18175+
18176+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18177+ struct dentry *dentry)
18178+{
18179+ int err, rerr;
18180+ struct au_dtime dt;
18181+ struct au_link_args *a;
18182+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 18183+ struct inode *inode, *delegated;
4a4d8108
AM
18184+ struct super_block *sb;
18185+ struct au_wr_dir_args wr_dir_args = {
18186+ /* .force_btgt = -1, */
18187+ .flags = AuWrDir_ADD_ENTRY
18188+ };
18189+
18190+ IMustLock(dir);
5527c038 18191+ inode = d_inode(src_dentry);
4a4d8108
AM
18192+ IMustLock(inode);
18193+
4a4d8108
AM
18194+ err = -ENOMEM;
18195+ a = kzalloc(sizeof(*a), GFP_NOFS);
18196+ if (unlikely(!a))
18197+ goto out;
18198+
18199+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
18200+ err = aufs_read_and_write_lock2(dentry, src_dentry,
18201+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
18202+ if (unlikely(err))
18203+ goto out_kfree;
38d290e6 18204+ err = au_d_linkable(src_dentry);
027c5e7a
AM
18205+ if (unlikely(err))
18206+ goto out_unlock;
18207+ err = au_d_may_add(dentry);
18208+ if (unlikely(err))
18209+ goto out_unlock;
e49829fe 18210+
4a4d8108 18211+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 18212+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
18213+
18214+ di_write_lock_parent(a->parent);
18215+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
18216+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
18217+ &wr_dir_args);
18218+ err = PTR_ERR(wh_dentry);
18219+ if (IS_ERR(wh_dentry))
027c5e7a 18220+ goto out_parent;
4a4d8108
AM
18221+
18222+ err = 0;
18223+ sb = dentry->d_sb;
5afbbe0d 18224+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
18225+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18226+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 18227+ a->bsrc = au_ibtop(inode);
2cbb1c4b 18228+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
18229+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
18230+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 18231+ if (!h_src_dentry) {
5afbbe0d 18232+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
18233+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18234+ AuDebugOn(!h_src_dentry);
38d290e6
JR
18235+ } else if (IS_ERR(h_src_dentry)) {
18236+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 18237+ goto out_parent;
38d290e6 18238+ }
2cbb1c4b 18239+
4a4d8108
AM
18240+ if (au_opt_test(au_mntflags(sb), PLINK)) {
18241+ if (a->bdst < a->bsrc
18242+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 18243+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
18244+ else {
18245+ delegated = NULL;
4a4d8108 18246+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
18247+ &a->h_path, &delegated);
18248+ if (unlikely(err == -EWOULDBLOCK)) {
18249+ pr_warn("cannot retry for NFSv4 delegation"
18250+ " for an internal link\n");
18251+ iput(delegated);
18252+ }
18253+ }
2cbb1c4b 18254+ dput(h_src_dentry);
4a4d8108
AM
18255+ } else {
18256+ /*
18257+ * copyup src_dentry to the branch we process,
18258+ * and then link(2) to it.
18259+ */
2cbb1c4b 18260+ dput(h_src_dentry);
4a4d8108
AM
18261+ if (a->bdst < a->bsrc
18262+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
18263+ au_unpin(&a->pin);
18264+ di_write_unlock(a->parent);
18265+ err = au_cpup_before_link(src_dentry, a);
18266+ di_write_lock_parent(a->parent);
18267+ if (!err)
18268+ err = au_pin(&a->pin, dentry, a->bdst,
18269+ au_opt_udba(sb),
18270+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18271+ if (unlikely(err))
18272+ goto out_wh;
18273+ }
18274+ if (!err) {
18275+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
18276+ err = -ENOENT;
5527c038 18277+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 18278+ delegated = NULL;
4a4d8108
AM
18279+ err = vfsub_link(h_src_dentry,
18280+ au_pinned_h_dir(&a->pin),
523b37e3
AM
18281+ &a->h_path, &delegated);
18282+ if (unlikely(err == -EWOULDBLOCK)) {
18283+ pr_warn("cannot retry"
18284+ " for NFSv4 delegation"
18285+ " for an internal link\n");
18286+ iput(delegated);
18287+ }
18288+ }
4a4d8108
AM
18289+ }
18290+ }
18291+ if (unlikely(err))
18292+ goto out_unpin;
18293+
18294+ if (wh_dentry) {
18295+ a->h_path.dentry = wh_dentry;
18296+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
18297+ dentry);
18298+ if (unlikely(err))
18299+ goto out_revert;
18300+ }
18301+
b912730e 18302+ au_dir_ts(dir, a->bdst);
4a4d8108 18303+ dir->i_version++;
4a4d8108
AM
18304+ inc_nlink(inode);
18305+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
18306+ d_instantiate(dentry, au_igrab(inode));
18307+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
18308+ /* some filesystem calls d_drop() */
18309+ d_drop(dentry);
076b876e
AM
18310+ /* some filesystems consume an inode even hardlink */
18311+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
18312+ goto out_unpin; /* success */
18313+
4f0767ce 18314+out_revert:
523b37e3
AM
18315+ /* no delegation since it is just created */
18316+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
18317+ /*delegated*/NULL, /*force*/0);
027c5e7a 18318+ if (unlikely(rerr)) {
523b37e3 18319+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
18320+ err = -EIO;
18321+ }
4a4d8108 18322+ au_dtime_revert(&dt);
4f0767ce 18323+out_unpin:
4a4d8108 18324+ au_unpin(&a->pin);
4f0767ce 18325+out_wh:
4a4d8108 18326+ dput(wh_dentry);
027c5e7a
AM
18327+out_parent:
18328+ di_write_unlock(a->parent);
18329+ dput(a->src_parent);
4f0767ce 18330+out_unlock:
4a4d8108 18331+ if (unlikely(err)) {
5afbbe0d 18332+ au_update_dbtop(dentry);
4a4d8108
AM
18333+ d_drop(dentry);
18334+ }
4a4d8108 18335+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 18336+out_kfree:
f0c0a007 18337+ au_delayed_kfree(a);
4f0767ce 18338+out:
86dc4139 18339+ AuTraceErr(err);
4a4d8108
AM
18340+ return err;
18341+}
18342+
7eafdf33 18343+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
18344+{
18345+ int err, rerr;
18346+ aufs_bindex_t bindex;
18347+ unsigned char diropq;
18348+ struct path h_path;
18349+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 18350+ struct inode *h_inode;
4a4d8108
AM
18351+ struct super_block *sb;
18352+ struct {
18353+ struct au_pin pin;
18354+ struct au_dtime dt;
18355+ } *a; /* reduce the stack usage */
18356+ struct au_wr_dir_args wr_dir_args = {
18357+ .force_btgt = -1,
18358+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
18359+ };
18360+
18361+ IMustLock(dir);
18362+
18363+ err = -ENOMEM;
18364+ a = kmalloc(sizeof(*a), GFP_NOFS);
18365+ if (unlikely(!a))
18366+ goto out;
18367+
027c5e7a
AM
18368+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18369+ if (unlikely(err))
18370+ goto out_free;
18371+ err = au_d_may_add(dentry);
18372+ if (unlikely(err))
18373+ goto out_unlock;
18374+
4a4d8108
AM
18375+ parent = dentry->d_parent; /* dir inode is locked */
18376+ di_write_lock_parent(parent);
18377+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18378+ &a->pin, &wr_dir_args);
18379+ err = PTR_ERR(wh_dentry);
18380+ if (IS_ERR(wh_dentry))
027c5e7a 18381+ goto out_parent;
4a4d8108
AM
18382+
18383+ sb = dentry->d_sb;
5afbbe0d 18384+ bindex = au_dbtop(dentry);
4a4d8108
AM
18385+ h_path.dentry = au_h_dptr(dentry, bindex);
18386+ h_path.mnt = au_sbr_mnt(sb, bindex);
18387+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18388+ if (unlikely(err))
027c5e7a 18389+ goto out_unpin;
4a4d8108
AM
18390+
18391+ /* make the dir opaque */
18392+ diropq = 0;
febd17d6 18393+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
18394+ if (wh_dentry
18395+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 18396+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 18397+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 18398+ inode_unlock(h_inode);
4a4d8108
AM
18399+ err = PTR_ERR(opq_dentry);
18400+ if (IS_ERR(opq_dentry))
18401+ goto out_dir;
18402+ dput(opq_dentry);
18403+ diropq = 1;
18404+ }
18405+
18406+ err = epilog(dir, bindex, wh_dentry, dentry);
18407+ if (!err) {
18408+ inc_nlink(dir);
027c5e7a 18409+ goto out_unpin; /* success */
4a4d8108
AM
18410+ }
18411+
18412+ /* revert */
18413+ if (diropq) {
18414+ AuLabel(revert opq);
febd17d6 18415+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 18416+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 18417+ inode_unlock(h_inode);
4a4d8108 18418+ if (rerr) {
523b37e3
AM
18419+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18420+ dentry, err, rerr);
4a4d8108
AM
18421+ err = -EIO;
18422+ }
18423+ }
18424+
4f0767ce 18425+out_dir:
4a4d8108
AM
18426+ AuLabel(revert dir);
18427+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18428+ if (rerr) {
523b37e3
AM
18429+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
18430+ dentry, err, rerr);
4a4d8108
AM
18431+ err = -EIO;
18432+ }
4a4d8108 18433+ au_dtime_revert(&a->dt);
027c5e7a 18434+out_unpin:
4a4d8108
AM
18435+ au_unpin(&a->pin);
18436+ dput(wh_dentry);
027c5e7a
AM
18437+out_parent:
18438+ di_write_unlock(parent);
18439+out_unlock:
4a4d8108 18440+ if (unlikely(err)) {
5afbbe0d 18441+ au_update_dbtop(dentry);
4a4d8108
AM
18442+ d_drop(dentry);
18443+ }
4a4d8108 18444+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 18445+out_free:
f0c0a007 18446+ au_delayed_kfree(a);
4f0767ce 18447+out:
4a4d8108
AM
18448+ return err;
18449+}
7f207e10
AM
18450diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18451--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
18452+++ linux/fs/aufs/i_op.c 2016-08-17 18:01:06.148556271 +0200
18453@@ -0,0 +1,1413 @@
4a4d8108 18454+/*
8cdd5066 18455+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
18456+ *
18457+ * This program, aufs is free software; you can redistribute it and/or modify
18458+ * it under the terms of the GNU General Public License as published by
18459+ * the Free Software Foundation; either version 2 of the License, or
18460+ * (at your option) any later version.
18461+ *
18462+ * This program is distributed in the hope that it will be useful,
18463+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18464+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18465+ * GNU General Public License for more details.
18466+ *
18467+ * You should have received a copy of the GNU General Public License
523b37e3 18468+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18469+ */
1facf9fc 18470+
1308ab2a 18471+/*
4a4d8108 18472+ * inode operations (except add/del/rename)
1308ab2a 18473+ */
4a4d8108
AM
18474+
18475+#include <linux/device_cgroup.h>
18476+#include <linux/fs_stack.h>
4a4d8108
AM
18477+#include <linux/namei.h>
18478+#include <linux/security.h>
4a4d8108
AM
18479+#include "aufs.h"
18480+
1e00d052 18481+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 18482+ struct path *h_path, int brperm)
1facf9fc 18483+{
1308ab2a 18484+ int err;
4a4d8108 18485+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 18486+
4a4d8108
AM
18487+ err = -EACCES;
18488+ if ((write_mask && IS_IMMUTABLE(h_inode))
18489+ || ((mask & MAY_EXEC)
18490+ && S_ISREG(h_inode->i_mode)
79b8bda9 18491+ && (path_noexec(h_path)
4a4d8108
AM
18492+ || !(h_inode->i_mode & S_IXUGO))))
18493+ goto out;
18494+
18495+ /*
18496+ * - skip the lower fs test in the case of write to ro branch.
18497+ * - nfs dir permission write check is optimized, but a policy for
18498+ * link/rename requires a real check.
b912730e
AM
18499+ * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18500+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
18501+ */
18502+ if ((write_mask && !au_br_writable(brperm))
18503+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18504+ && write_mask && !(mask & MAY_READ))
18505+ || !h_inode->i_op->permission) {
18506+ /* AuLabel(generic_permission); */
b912730e 18507+ /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
1e00d052 18508+ err = generic_permission(h_inode, mask);
b912730e
AM
18509+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18510+ err = h_inode->i_op->permission(h_inode, mask);
18511+ AuTraceErr(err);
1308ab2a 18512+ } else {
4a4d8108 18513+ /* AuLabel(h_inode->permission); */
1e00d052 18514+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
18515+ AuTraceErr(err);
18516+ }
1facf9fc 18517+
4a4d8108
AM
18518+ if (!err)
18519+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 18520+ if (!err)
4a4d8108 18521+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
18522+
18523+#if 0
18524+ if (!err) {
18525+ /* todo: do we need to call ima_path_check()? */
18526+ struct path h_path = {
18527+ .dentry =
18528+ .mnt = h_mnt
18529+ };
18530+ err = ima_path_check(&h_path,
18531+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18532+ IMA_COUNT_LEAVE);
1308ab2a 18533+ }
4a4d8108 18534+#endif
dece6358 18535+
4f0767ce 18536+out:
1308ab2a 18537+ return err;
18538+}
dece6358 18539+
1e00d052 18540+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 18541+{
18542+ int err;
5afbbe0d 18543+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
18544+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18545+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18546+ struct inode *h_inode;
18547+ struct super_block *sb;
18548+ struct au_branch *br;
1facf9fc 18549+
027c5e7a 18550+ /* todo: support rcu-walk? */
1e00d052 18551+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
18552+ return -ECHILD;
18553+
4a4d8108
AM
18554+ sb = inode->i_sb;
18555+ si_read_lock(sb, AuLock_FLUSH);
18556+ ii_read_lock_child(inode);
027c5e7a
AM
18557+#if 0
18558+ err = au_iigen_test(inode, au_sigen(sb));
18559+ if (unlikely(err))
18560+ goto out;
18561+#endif
dece6358 18562+
076b876e
AM
18563+ if (!isdir
18564+ || write_mask
18565+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 18566+ err = au_busy_or_stale();
5afbbe0d 18567+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
18568+ if (unlikely(!h_inode
18569+ || (h_inode->i_mode & S_IFMT)
18570+ != (inode->i_mode & S_IFMT)))
18571+ goto out;
1facf9fc 18572+
4a4d8108 18573+ err = 0;
5afbbe0d 18574+ bindex = au_ibtop(inode);
4a4d8108 18575+ br = au_sbr(sb, bindex);
79b8bda9 18576+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
18577+ if (write_mask
18578+ && !err
18579+ && !special_file(h_inode->i_mode)) {
18580+ /* test whether the upper writable branch exists */
18581+ err = -EROFS;
18582+ for (; bindex >= 0; bindex--)
18583+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
18584+ err = 0;
18585+ break;
18586+ }
18587+ }
18588+ goto out;
18589+ }
dece6358 18590+
4a4d8108 18591+ /* non-write to dir */
1308ab2a 18592+ err = 0;
5afbbe0d
AM
18593+ bbot = au_ibbot(inode);
18594+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
18595+ h_inode = au_h_iptr(inode, bindex);
18596+ if (h_inode) {
18597+ err = au_busy_or_stale();
18598+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
18599+ break;
18600+
18601+ br = au_sbr(sb, bindex);
79b8bda9 18602+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
18603+ br->br_perm);
18604+ }
18605+ }
1308ab2a 18606+
4f0767ce 18607+out:
4a4d8108
AM
18608+ ii_read_unlock(inode);
18609+ si_read_unlock(sb);
1308ab2a 18610+ return err;
18611+}
18612+
4a4d8108 18613+/* ---------------------------------------------------------------------- */
1facf9fc 18614+
4a4d8108 18615+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 18616+ unsigned int flags)
4a4d8108
AM
18617+{
18618+ struct dentry *ret, *parent;
b752ccd1 18619+ struct inode *inode;
4a4d8108 18620+ struct super_block *sb;
1716fcea 18621+ int err, npositive;
dece6358 18622+
4a4d8108 18623+ IMustLock(dir);
1308ab2a 18624+
537831f9
AM
18625+ /* todo: support rcu-walk? */
18626+ ret = ERR_PTR(-ECHILD);
18627+ if (flags & LOOKUP_RCU)
18628+ goto out;
18629+
18630+ ret = ERR_PTR(-ENAMETOOLONG);
18631+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18632+ goto out;
18633+
4a4d8108 18634+ sb = dir->i_sb;
7f207e10
AM
18635+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18636+ ret = ERR_PTR(err);
18637+ if (unlikely(err))
18638+ goto out;
18639+
4a4d8108
AM
18640+ err = au_di_init(dentry);
18641+ ret = ERR_PTR(err);
18642+ if (unlikely(err))
7f207e10 18643+ goto out_si;
1308ab2a 18644+
9dbd164d 18645+ inode = NULL;
027c5e7a 18646+ npositive = 0; /* suppress a warning */
4a4d8108
AM
18647+ parent = dentry->d_parent; /* dir inode is locked */
18648+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
18649+ err = au_alive_dir(parent);
18650+ if (!err)
18651+ err = au_digen_test(parent, au_sigen(sb));
18652+ if (!err) {
5afbbe0d
AM
18653+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
18654+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
18655+ AuLkup_ALLOW_NEG);
027c5e7a
AM
18656+ err = npositive;
18657+ }
4a4d8108 18658+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
18659+ ret = ERR_PTR(err);
18660+ if (unlikely(err < 0))
18661+ goto out_unlock;
1308ab2a 18662+
4a4d8108 18663+ if (npositive) {
b752ccd1 18664+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
18665+ if (IS_ERR(inode)) {
18666+ ret = (void *)inode;
18667+ inode = NULL;
18668+ goto out_unlock;
18669+ }
9dbd164d 18670+ }
4a4d8108 18671+
c1595e42
JR
18672+ if (inode)
18673+ atomic_inc(&inode->i_count);
4a4d8108 18674+ ret = d_splice_alias(inode, dentry);
537831f9
AM
18675+#if 0
18676+ if (unlikely(d_need_lookup(dentry))) {
18677+ spin_lock(&dentry->d_lock);
18678+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18679+ spin_unlock(&dentry->d_lock);
18680+ } else
18681+#endif
c1595e42 18682+ if (inode) {
2000de60 18683+ if (!IS_ERR(ret)) {
c1595e42 18684+ iput(inode);
2000de60
JR
18685+ if (ret && ret != dentry)
18686+ ii_write_unlock(inode);
18687+ } else {
c1595e42
JR
18688+ ii_write_unlock(inode);
18689+ iput(inode);
18690+ inode = NULL;
18691+ }
7f207e10 18692+ }
1facf9fc 18693+
4f0767ce 18694+out_unlock:
4a4d8108 18695+ di_write_unlock(dentry);
7f207e10 18696+out_si:
4a4d8108 18697+ si_read_unlock(sb);
7f207e10 18698+out:
4a4d8108
AM
18699+ return ret;
18700+}
1facf9fc 18701+
4a4d8108 18702+/* ---------------------------------------------------------------------- */
1facf9fc 18703+
b912730e
AM
18704+struct aopen_node {
18705+ struct hlist_node hlist;
18706+ struct file *file, *h_file;
18707+};
18708+
18709+static int au_do_aopen(struct inode *inode, struct file *file)
18710+{
18711+ struct au_sphlhead *aopen;
18712+ struct aopen_node *node;
18713+ struct au_do_open_args args = {
18714+ .no_lock = 1,
18715+ .open = au_do_open_nondir
18716+ };
18717+
18718+ aopen = &au_sbi(inode->i_sb)->si_aopen;
18719+ spin_lock(&aopen->spin);
18720+ hlist_for_each_entry(node, &aopen->head, hlist)
18721+ if (node->file == file) {
18722+ args.h_file = node->h_file;
18723+ break;
18724+ }
18725+ spin_unlock(&aopen->spin);
18726+ /* AuDebugOn(!args.h_file); */
18727+
18728+ return au_do_open(file, &args);
18729+}
18730+
18731+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18732+ struct file *file, unsigned int open_flag,
18733+ umode_t create_mode, int *opened)
18734+{
18735+ int err, h_opened = *opened;
5afbbe0d 18736+ unsigned int lkup_flags;
f0c0a007 18737+ struct dentry *parent, *d;
b912730e
AM
18738+ struct au_sphlhead *aopen;
18739+ struct vfsub_aopen_args args = {
18740+ .open_flag = open_flag,
18741+ .create_mode = create_mode,
18742+ .opened = &h_opened
18743+ };
18744+ struct aopen_node aopen_node = {
18745+ .file = file
18746+ };
18747+
18748+ IMustLock(dir);
5afbbe0d 18749+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
18750+ AuDbgDentry(dentry);
18751+
18752+ err = 0;
18753+ if (!au_di(dentry)) {
5afbbe0d
AM
18754+ lkup_flags = LOOKUP_OPEN;
18755+ if (open_flag & O_CREAT)
18756+ lkup_flags |= LOOKUP_CREATE;
18757+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
18758+ if (IS_ERR(d)) {
18759+ err = PTR_ERR(d);
5afbbe0d 18760+ AuTraceErr(err);
b912730e
AM
18761+ goto out;
18762+ } else if (d) {
18763+ /*
18764+ * obsoleted dentry found.
18765+ * another error will be returned later.
18766+ */
18767+ d_drop(d);
b912730e 18768+ AuDbgDentry(d);
5afbbe0d 18769+ dput(d);
b912730e
AM
18770+ }
18771+ AuDbgDentry(dentry);
18772+ }
18773+
18774+ if (d_is_positive(dentry)
18775+ || d_unhashed(dentry)
18776+ || d_unlinked(dentry)
18777+ || !(open_flag & O_CREAT))
18778+ goto out_no_open;
18779+
18780+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18781+ if (unlikely(err))
18782+ goto out;
18783+
18784+ parent = dentry->d_parent; /* dir is locked */
18785+ di_write_lock_parent(parent);
5afbbe0d 18786+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
18787+ if (unlikely(err))
18788+ goto out_unlock;
18789+
18790+ AuDbgDentry(dentry);
18791+ if (d_is_positive(dentry))
18792+ goto out_unlock;
18793+
18794+ args.file = get_empty_filp();
18795+ err = PTR_ERR(args.file);
18796+ if (IS_ERR(args.file))
18797+ goto out_unlock;
18798+
18799+ args.file->f_flags = file->f_flags;
18800+ err = au_aopen_or_create(dir, dentry, &args);
18801+ AuTraceErr(err);
18802+ AuDbgFile(args.file);
18803+ if (unlikely(err < 0)) {
18804+ if (h_opened & FILE_OPENED)
18805+ fput(args.file);
18806+ else
18807+ put_filp(args.file);
18808+ goto out_unlock;
18809+ }
18810+
18811+ /* some filesystems don't set FILE_CREATED while succeeded? */
18812+ *opened |= FILE_CREATED;
18813+ if (h_opened & FILE_OPENED)
18814+ aopen_node.h_file = args.file;
18815+ else {
18816+ put_filp(args.file);
18817+ args.file = NULL;
18818+ }
18819+ aopen = &au_sbi(dir->i_sb)->si_aopen;
18820+ au_sphl_add(&aopen_node.hlist, aopen);
18821+ err = finish_open(file, dentry, au_do_aopen, opened);
18822+ au_sphl_del(&aopen_node.hlist, aopen);
18823+ AuTraceErr(err);
18824+ AuDbgFile(file);
18825+ if (aopen_node.h_file)
18826+ fput(aopen_node.h_file);
18827+
18828+out_unlock:
18829+ di_write_unlock(parent);
18830+ aufs_read_unlock(dentry, AuLock_DW);
18831+ AuDbgDentry(dentry);
f0c0a007 18832+ if (unlikely(err < 0))
b912730e
AM
18833+ goto out;
18834+out_no_open:
f0c0a007 18835+ if (err >= 0 && !(*opened & FILE_CREATED)) {
b912730e
AM
18836+ AuLabel(out_no_open);
18837+ dget(dentry);
18838+ err = finish_no_open(file, dentry);
18839+ }
18840+out:
18841+ AuDbg("%pd%s%s\n", dentry,
18842+ (*opened & FILE_CREATED) ? " created" : "",
18843+ (*opened & FILE_OPENED) ? " opened" : "");
18844+ AuTraceErr(err);
18845+ return err;
18846+}
18847+
18848+
18849+/* ---------------------------------------------------------------------- */
18850+
4a4d8108
AM
18851+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18852+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 18853+ aufs_bindex_t btop)
4a4d8108
AM
18854+{
18855+ int err;
18856+ struct dentry *h_parent;
18857+ struct inode *h_dir;
1facf9fc 18858+
027c5e7a 18859+ if (add_entry)
5527c038 18860+ IMustLock(d_inode(parent));
027c5e7a 18861+ else
4a4d8108
AM
18862+ di_write_lock_parent(parent);
18863+
18864+ err = 0;
18865+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 18866+ if (btop > bcpup)
c2b27bf2 18867+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 18868+ else if (btop < bcpup)
4a4d8108
AM
18869+ err = au_cpdown_dirs(dentry, bcpup);
18870+ else
c2b27bf2 18871+ BUG();
4a4d8108 18872+ }
38d290e6 18873+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 18874+ h_parent = au_h_dptr(parent, bcpup);
5527c038 18875+ h_dir = d_inode(h_parent);
febd17d6 18876+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 18877+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 18878+ /* todo: no unlock here */
febd17d6 18879+ inode_unlock(h_dir);
027c5e7a
AM
18880+
18881+ AuDbg("bcpup %d\n", bcpup);
18882+ if (!err) {
5527c038 18883+ if (d_really_is_negative(dentry))
5afbbe0d 18884+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
18885+ au_update_dbrange(dentry, /*do_put_zero*/0);
18886+ }
1308ab2a 18887+ }
1facf9fc 18888+
4a4d8108
AM
18889+ if (!add_entry)
18890+ di_write_unlock(parent);
18891+ if (!err)
18892+ err = bcpup; /* success */
1308ab2a 18893+
027c5e7a 18894+ AuTraceErr(err);
4a4d8108
AM
18895+ return err;
18896+}
1facf9fc 18897+
4a4d8108
AM
18898+/*
18899+ * decide the branch and the parent dir where we will create a new entry.
18900+ * returns new bindex or an error.
18901+ * copyup the parent dir if needed.
18902+ */
18903+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18904+ struct au_wr_dir_args *args)
18905+{
18906+ int err;
392086de 18907+ unsigned int flags;
5afbbe0d 18908+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
18909+ const unsigned char add_entry
18910+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 18911+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
18912+ struct super_block *sb;
18913+ struct dentry *parent;
18914+ struct au_sbinfo *sbinfo;
1facf9fc 18915+
4a4d8108
AM
18916+ sb = dentry->d_sb;
18917+ sbinfo = au_sbi(sb);
18918+ parent = dget_parent(dentry);
5afbbe0d
AM
18919+ btop = au_dbtop(dentry);
18920+ bcpup = btop;
4a4d8108
AM
18921+ if (args->force_btgt < 0) {
18922+ if (src_dentry) {
5afbbe0d
AM
18923+ src_btop = au_dbtop(src_dentry);
18924+ if (src_btop < btop)
18925+ bcpup = src_btop;
4a4d8108 18926+ } else if (add_entry) {
392086de
AM
18927+ flags = 0;
18928+ if (au_ftest_wrdir(args->flags, ISDIR))
18929+ au_fset_wbr(flags, DIR);
18930+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
18931+ bcpup = err;
18932+ }
1facf9fc 18933+
5527c038 18934+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
18935+ if (add_entry)
18936+ err = AuWbrCopyup(sbinfo, dentry);
18937+ else {
18938+ if (!IS_ROOT(dentry)) {
18939+ di_read_lock_parent(parent, !AuLock_IR);
18940+ err = AuWbrCopyup(sbinfo, dentry);
18941+ di_read_unlock(parent, !AuLock_IR);
18942+ } else
18943+ err = AuWbrCopyup(sbinfo, dentry);
18944+ }
18945+ bcpup = err;
18946+ if (unlikely(err < 0))
18947+ goto out;
18948+ }
18949+ } else {
18950+ bcpup = args->force_btgt;
5527c038 18951+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 18952+ }
027c5e7a 18953+
5afbbe0d 18954+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 18955+ err = bcpup;
5afbbe0d 18956+ if (bcpup == btop)
4a4d8108 18957+ goto out; /* success */
4a4d8108
AM
18958+
18959+ /* copyup the new parent into the branch we process */
5afbbe0d 18960+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 18961+ if (err >= 0) {
5527c038 18962+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
18963+ au_set_h_dptr(dentry, btop, NULL);
18964+ au_set_dbtop(dentry, bcpup);
18965+ au_set_dbbot(dentry, bcpup);
027c5e7a 18966+ }
38d290e6
JR
18967+ AuDebugOn(add_entry
18968+ && !au_ftest_wrdir(args->flags, TMPFILE)
18969+ && !au_h_dptr(dentry, bcpup));
027c5e7a 18970+ }
86dc4139
AM
18971+
18972+out:
18973+ dput(parent);
18974+ return err;
18975+}
18976+
18977+/* ---------------------------------------------------------------------- */
18978+
18979+void au_pin_hdir_unlock(struct au_pin *p)
18980+{
18981+ if (p->hdir)
5afbbe0d 18982+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
18983+}
18984+
c1595e42 18985+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
18986+{
18987+ int err;
18988+
18989+ err = 0;
18990+ if (!p->hdir)
18991+ goto out;
18992+
18993+ /* even if an error happens later, keep this lock */
5afbbe0d 18994+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
18995+
18996+ err = -EBUSY;
5527c038 18997+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
18998+ goto out;
18999+
19000+ err = 0;
19001+ if (p->h_dentry)
19002+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
19003+ p->h_parent, p->br);
19004+
19005+out:
19006+ return err;
19007+}
19008+
19009+int au_pin_hdir_relock(struct au_pin *p)
19010+{
19011+ int err, i;
19012+ struct inode *h_i;
19013+ struct dentry *h_d[] = {
19014+ p->h_dentry,
19015+ p->h_parent
19016+ };
19017+
19018+ err = au_pin_hdir_lock(p);
19019+ if (unlikely(err))
19020+ goto out;
19021+
19022+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
19023+ if (!h_d[i])
19024+ continue;
5527c038
JR
19025+ if (d_is_positive(h_d[i])) {
19026+ h_i = d_inode(h_d[i]);
86dc4139 19027+ err = !h_i->i_nlink;
5527c038 19028+ }
86dc4139
AM
19029+ }
19030+
19031+out:
19032+ return err;
19033+}
19034+
5afbbe0d 19035+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 19036+{
5afbbe0d
AM
19037+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
19038+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
19039+#endif
19040+}
19041+
19042+void au_pin_hdir_acquire_nest(struct au_pin *p)
19043+{
19044+ if (p->hdir) {
5afbbe0d 19045+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
19046+ p->lsc_hi, 0, NULL, _RET_IP_);
19047+ au_pin_hdir_set_owner(p, current);
19048+ }
dece6358 19049+}
1facf9fc 19050+
86dc4139
AM
19051+void au_pin_hdir_release(struct au_pin *p)
19052+{
19053+ if (p->hdir) {
19054+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 19055+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
19056+ }
19057+}
1308ab2a 19058+
4a4d8108 19059+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 19060+{
4a4d8108
AM
19061+ if (pin && pin->parent)
19062+ return au_h_dptr(pin->parent, pin->bindex);
19063+ return NULL;
dece6358 19064+}
1facf9fc 19065+
4a4d8108 19066+void au_unpin(struct au_pin *p)
dece6358 19067+{
86dc4139
AM
19068+ if (p->hdir)
19069+ au_pin_hdir_unlock(p);
e49829fe 19070+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 19071+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
19072+ if (!p->hdir)
19073+ return;
1facf9fc 19074+
4a4d8108
AM
19075+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19076+ di_read_unlock(p->parent, AuLock_IR);
19077+ iput(p->hdir->hi_inode);
19078+ dput(p->parent);
19079+ p->parent = NULL;
19080+ p->hdir = NULL;
19081+ p->h_mnt = NULL;
86dc4139 19082+ /* do not clear p->task */
4a4d8108 19083+}
1308ab2a 19084+
4a4d8108
AM
19085+int au_do_pin(struct au_pin *p)
19086+{
19087+ int err;
19088+ struct super_block *sb;
4a4d8108
AM
19089+ struct inode *h_dir;
19090+
19091+ err = 0;
19092+ sb = p->dentry->d_sb;
86dc4139 19093+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
19094+ if (IS_ROOT(p->dentry)) {
19095+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 19096+ p->h_mnt = au_br_mnt(p->br);
b4510431 19097+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
19098+ if (unlikely(err)) {
19099+ au_fclr_pin(p->flags, MNT_WRITE);
19100+ goto out_err;
19101+ }
19102+ }
dece6358 19103+ goto out;
1facf9fc 19104+ }
19105+
86dc4139 19106+ p->h_dentry = NULL;
5afbbe0d 19107+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 19108+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 19109+
4a4d8108
AM
19110+ p->parent = dget_parent(p->dentry);
19111+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19112+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 19113+
4a4d8108 19114+ h_dir = NULL;
86dc4139 19115+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 19116+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
19117+ if (p->hdir)
19118+ h_dir = p->hdir->hi_inode;
dece6358 19119+
b752ccd1
AM
19120+ /*
19121+ * udba case, or
19122+ * if DI_LOCKED is not set, then p->parent may be different
19123+ * and h_parent can be NULL.
19124+ */
86dc4139 19125+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 19126+ err = -EBUSY;
4a4d8108
AM
19127+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19128+ di_read_unlock(p->parent, AuLock_IR);
19129+ dput(p->parent);
19130+ p->parent = NULL;
19131+ goto out_err;
19132+ }
1308ab2a 19133+
4a4d8108 19134+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 19135+ p->h_mnt = au_br_mnt(p->br);
b4510431 19136+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 19137+ if (unlikely(err)) {
4a4d8108 19138+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
19139+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19140+ di_read_unlock(p->parent, AuLock_IR);
19141+ dput(p->parent);
19142+ p->parent = NULL;
19143+ goto out_err;
dece6358
AM
19144+ }
19145+ }
4a4d8108 19146+
86dc4139
AM
19147+ au_igrab(h_dir);
19148+ err = au_pin_hdir_lock(p);
19149+ if (!err)
19150+ goto out; /* success */
19151+
076b876e
AM
19152+ au_unpin(p);
19153+
4f0767ce 19154+out_err:
4a4d8108
AM
19155+ pr_err("err %d\n", err);
19156+ err = au_busy_or_stale();
4f0767ce 19157+out:
1facf9fc 19158+ return err;
19159+}
19160+
4a4d8108
AM
19161+void au_pin_init(struct au_pin *p, struct dentry *dentry,
19162+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19163+ unsigned int udba, unsigned char flags)
19164+{
19165+ p->dentry = dentry;
19166+ p->udba = udba;
19167+ p->lsc_di = lsc_di;
19168+ p->lsc_hi = lsc_hi;
19169+ p->flags = flags;
19170+ p->bindex = bindex;
19171+
19172+ p->parent = NULL;
19173+ p->hdir = NULL;
19174+ p->h_mnt = NULL;
86dc4139
AM
19175+
19176+ p->h_dentry = NULL;
19177+ p->h_parent = NULL;
19178+ p->br = NULL;
19179+ p->task = current;
4a4d8108
AM
19180+}
19181+
19182+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19183+ unsigned int udba, unsigned char flags)
19184+{
19185+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
19186+ udba, flags);
19187+ return au_do_pin(pin);
19188+}
19189+
dece6358
AM
19190+/* ---------------------------------------------------------------------- */
19191+
1308ab2a 19192+/*
4a4d8108
AM
19193+ * ->setattr() and ->getattr() are called in various cases.
19194+ * chmod, stat: dentry is revalidated.
19195+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
19196+ * unhashed.
19197+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 19198+ */
027c5e7a 19199+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 19200+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 19201+{
4a4d8108 19202+ int err;
4a4d8108 19203+ struct dentry *parent;
1facf9fc 19204+
1308ab2a 19205+ err = 0;
027c5e7a 19206+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
19207+ parent = dget_parent(dentry);
19208+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 19209+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
19210+ di_read_unlock(parent, AuLock_IR);
19211+ dput(parent);
dece6358 19212+ }
1facf9fc 19213+
4a4d8108 19214+ AuTraceErr(err);
1308ab2a 19215+ return err;
19216+}
dece6358 19217+
c1595e42
JR
19218+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19219+ struct au_icpup_args *a)
1308ab2a 19220+{
19221+ int err;
4a4d8108 19222+ loff_t sz;
5afbbe0d 19223+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
19224+ struct dentry *hi_wh, *parent;
19225+ struct inode *inode;
4a4d8108
AM
19226+ struct au_wr_dir_args wr_dir_args = {
19227+ .force_btgt = -1,
19228+ .flags = 0
19229+ };
19230+
2000de60 19231+ if (d_is_dir(dentry))
4a4d8108
AM
19232+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
19233+ /* plink or hi_wh() case */
5afbbe0d 19234+ btop = au_dbtop(dentry);
5527c038 19235+ inode = d_inode(dentry);
5afbbe0d
AM
19236+ ibtop = au_ibtop(inode);
19237+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
19238+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
19239+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19240+ if (unlikely(err < 0))
19241+ goto out;
19242+ a->btgt = err;
5afbbe0d 19243+ if (err != btop)
4a4d8108
AM
19244+ au_fset_icpup(a->flags, DID_CPUP);
19245+
19246+ err = 0;
19247+ a->pin_flags = AuPin_MNT_WRITE;
19248+ parent = NULL;
19249+ if (!IS_ROOT(dentry)) {
19250+ au_fset_pin(a->pin_flags, DI_LOCKED);
19251+ parent = dget_parent(dentry);
19252+ di_write_lock_parent(parent);
19253+ }
19254+
19255+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
19256+ if (unlikely(err))
19257+ goto out_parent;
19258+
4a4d8108 19259+ sz = -1;
5afbbe0d 19260+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 19261+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 19262+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
febd17d6 19263+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
19264+ if (ia->ia_size < i_size_read(a->h_inode))
19265+ sz = ia->ia_size;
febd17d6 19266+ inode_unlock(a->h_inode);
c1595e42 19267+ }
4a4d8108 19268+
4a4d8108 19269+ hi_wh = NULL;
027c5e7a 19270+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
19271+ hi_wh = au_hi_wh(inode, a->btgt);
19272+ if (!hi_wh) {
c2b27bf2
AM
19273+ struct au_cp_generic cpg = {
19274+ .dentry = dentry,
19275+ .bdst = a->btgt,
19276+ .bsrc = -1,
19277+ .len = sz,
19278+ .pin = &a->pin
19279+ };
19280+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
19281+ if (unlikely(err))
19282+ goto out_unlock;
19283+ hi_wh = au_hi_wh(inode, a->btgt);
19284+ /* todo: revalidate hi_wh? */
19285+ }
19286+ }
19287+
19288+ if (parent) {
19289+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
19290+ di_downgrade_lock(parent, AuLock_IR);
19291+ dput(parent);
19292+ parent = NULL;
19293+ }
19294+ if (!au_ftest_icpup(a->flags, DID_CPUP))
19295+ goto out; /* success */
19296+
19297+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
19298+ struct au_cp_generic cpg = {
19299+ .dentry = dentry,
19300+ .bdst = a->btgt,
5afbbe0d 19301+ .bsrc = btop,
c2b27bf2
AM
19302+ .len = sz,
19303+ .pin = &a->pin,
19304+ .flags = AuCpup_DTIME | AuCpup_HOPEN
19305+ };
19306+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
19307+ if (!err)
19308+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19309+ } else if (!hi_wh)
19310+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19311+ else
19312+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 19313+
4f0767ce 19314+out_unlock:
5527c038 19315+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 19316+ if (!err)
dece6358 19317+ goto out; /* success */
4a4d8108 19318+ au_unpin(&a->pin);
4f0767ce 19319+out_parent:
4a4d8108
AM
19320+ if (parent) {
19321+ di_write_unlock(parent);
19322+ dput(parent);
19323+ }
4f0767ce 19324+out:
86dc4139 19325+ if (!err)
febd17d6 19326+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 19327+ return err;
19328+}
19329+
4a4d8108 19330+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 19331+{
4a4d8108 19332+ int err;
523b37e3 19333+ struct inode *inode, *delegated;
4a4d8108
AM
19334+ struct super_block *sb;
19335+ struct file *file;
19336+ struct au_icpup_args *a;
1facf9fc 19337+
5527c038 19338+ inode = d_inode(dentry);
4a4d8108 19339+ IMustLock(inode);
dece6358 19340+
4a4d8108
AM
19341+ err = -ENOMEM;
19342+ a = kzalloc(sizeof(*a), GFP_NOFS);
19343+ if (unlikely(!a))
19344+ goto out;
1facf9fc 19345+
4a4d8108
AM
19346+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19347+ ia->ia_valid &= ~ATTR_MODE;
dece6358 19348+
4a4d8108
AM
19349+ file = NULL;
19350+ sb = dentry->d_sb;
e49829fe
JR
19351+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19352+ if (unlikely(err))
19353+ goto out_kfree;
19354+
4a4d8108
AM
19355+ if (ia->ia_valid & ATTR_FILE) {
19356+ /* currently ftruncate(2) only */
7e9cd9fe 19357+ AuDebugOn(!d_is_reg(dentry));
4a4d8108
AM
19358+ file = ia->ia_file;
19359+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
19360+ if (unlikely(err))
19361+ goto out_si;
19362+ ia->ia_file = au_hf_top(file);
19363+ a->udba = AuOpt_UDBA_NONE;
19364+ } else {
19365+ /* fchmod() doesn't pass ia_file */
19366+ a->udba = au_opt_udba(sb);
027c5e7a
AM
19367+ di_write_lock_child(dentry);
19368+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
19369+ if (d_unhashed(dentry))
19370+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
19371+ if (a->udba != AuOpt_UDBA_NONE) {
19372+ AuDebugOn(IS_ROOT(dentry));
19373+ err = au_reval_for_attr(dentry, au_sigen(sb));
19374+ if (unlikely(err))
19375+ goto out_dentry;
19376+ }
dece6358 19377+ }
dece6358 19378+
4a4d8108
AM
19379+ err = au_pin_and_icpup(dentry, ia, a);
19380+ if (unlikely(err < 0))
19381+ goto out_dentry;
19382+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
19383+ ia->ia_file = NULL;
19384+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 19385+ }
dece6358 19386+
4a4d8108
AM
19387+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19388+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19389+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 19390+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
19391+ if (unlikely(err))
19392+ goto out_unlock;
19393+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19394+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 19395+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
19396+ if (unlikely(err))
19397+ goto out_unlock;
19398+ }
dece6358 19399+
4a4d8108
AM
19400+ if (ia->ia_valid & ATTR_SIZE) {
19401+ struct file *f;
1308ab2a 19402+
953406b4 19403+ if (ia->ia_size < i_size_read(inode))
4a4d8108 19404+ /* unmap only */
953406b4 19405+ truncate_setsize(inode, ia->ia_size);
1308ab2a 19406+
4a4d8108
AM
19407+ f = NULL;
19408+ if (ia->ia_valid & ATTR_FILE)
19409+ f = ia->ia_file;
febd17d6 19410+ inode_unlock(a->h_inode);
4a4d8108 19411+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 19412+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
19413+ } else {
19414+ delegated = NULL;
19415+ while (1) {
19416+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
19417+ if (delegated) {
19418+ err = break_deleg_wait(&delegated);
19419+ if (!err)
19420+ continue;
19421+ }
19422+ break;
19423+ }
19424+ }
8cdd5066
JR
19425+ /*
19426+ * regardless aufs 'acl' option setting.
19427+ * why don't all acl-aware fs call this func from their ->setattr()?
19428+ */
19429+ if (!err && (ia->ia_valid & ATTR_MODE))
19430+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
19431+ if (!err)
19432+ au_cpup_attr_changeable(inode);
1308ab2a 19433+
4f0767ce 19434+out_unlock:
febd17d6 19435+ inode_unlock(a->h_inode);
4a4d8108 19436+ au_unpin(&a->pin);
027c5e7a 19437+ if (unlikely(err))
5afbbe0d 19438+ au_update_dbtop(dentry);
4f0767ce 19439+out_dentry:
4a4d8108
AM
19440+ di_write_unlock(dentry);
19441+ if (file) {
19442+ fi_write_unlock(file);
19443+ ia->ia_file = file;
19444+ ia->ia_valid |= ATTR_FILE;
19445+ }
4f0767ce 19446+out_si:
4a4d8108 19447+ si_read_unlock(sb);
e49829fe 19448+out_kfree:
f0c0a007 19449+ au_delayed_kfree(a);
4f0767ce 19450+out:
4a4d8108
AM
19451+ AuTraceErr(err);
19452+ return err;
1facf9fc 19453+}
19454+
c1595e42
JR
19455+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19456+static int au_h_path_to_set_attr(struct dentry *dentry,
19457+ struct au_icpup_args *a, struct path *h_path)
19458+{
19459+ int err;
19460+ struct super_block *sb;
19461+
19462+ sb = dentry->d_sb;
19463+ a->udba = au_opt_udba(sb);
19464+ /* no d_unlinked(), to set UDBA_NONE for root */
19465+ if (d_unhashed(dentry))
19466+ a->udba = AuOpt_UDBA_NONE;
19467+ if (a->udba != AuOpt_UDBA_NONE) {
19468+ AuDebugOn(IS_ROOT(dentry));
19469+ err = au_reval_for_attr(dentry, au_sigen(sb));
19470+ if (unlikely(err))
19471+ goto out;
19472+ }
19473+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19474+ if (unlikely(err < 0))
19475+ goto out;
19476+
19477+ h_path->dentry = a->h_path.dentry;
19478+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
19479+
19480+out:
19481+ return err;
19482+}
19483+
5afbbe0d
AM
19484+ssize_t au_srxattr(struct dentry *dentry, struct inode *inode,
19485+ struct au_srxattr *arg)
c1595e42
JR
19486+{
19487+ int err;
19488+ struct path h_path;
19489+ struct super_block *sb;
19490+ struct au_icpup_args *a;
5afbbe0d 19491+ struct inode *h_inode;
c1595e42 19492+
c1595e42
JR
19493+ IMustLock(inode);
19494+
19495+ err = -ENOMEM;
19496+ a = kzalloc(sizeof(*a), GFP_NOFS);
19497+ if (unlikely(!a))
19498+ goto out;
19499+
19500+ sb = dentry->d_sb;
19501+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19502+ if (unlikely(err))
19503+ goto out_kfree;
19504+
19505+ h_path.dentry = NULL; /* silence gcc */
19506+ di_write_lock_child(dentry);
19507+ err = au_h_path_to_set_attr(dentry, a, &h_path);
19508+ if (unlikely(err))
19509+ goto out_di;
19510+
febd17d6 19511+ inode_unlock(a->h_inode);
c1595e42
JR
19512+ switch (arg->type) {
19513+ case AU_XATTR_SET:
5afbbe0d 19514+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
19515+ err = vfsub_setxattr(h_path.dentry,
19516+ arg->u.set.name, arg->u.set.value,
19517+ arg->u.set.size, arg->u.set.flags);
19518+ break;
19519+ case AU_XATTR_REMOVE:
19520+ err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
19521+ break;
19522+ case AU_ACL_SET:
19523+ err = -EOPNOTSUPP;
5527c038 19524+ h_inode = d_inode(h_path.dentry);
c1595e42
JR
19525+ if (h_inode->i_op->set_acl)
19526+ err = h_inode->i_op->set_acl(h_inode,
19527+ arg->u.acl_set.acl,
19528+ arg->u.acl_set.type);
19529+ break;
19530+ }
19531+ if (!err)
19532+ au_cpup_attr_timesizes(inode);
19533+
19534+ au_unpin(&a->pin);
19535+ if (unlikely(err))
5afbbe0d 19536+ au_update_dbtop(dentry);
c1595e42
JR
19537+
19538+out_di:
19539+ di_write_unlock(dentry);
19540+ si_read_unlock(sb);
19541+out_kfree:
f0c0a007 19542+ au_delayed_kfree(a);
c1595e42
JR
19543+out:
19544+ AuTraceErr(err);
19545+ return err;
19546+}
19547+#endif
19548+
4a4d8108
AM
19549+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19550+ unsigned int nlink)
1facf9fc 19551+{
9dbd164d
AM
19552+ unsigned int n;
19553+
4a4d8108 19554+ inode->i_mode = st->mode;
86dc4139
AM
19555+ /* don't i_[ug]id_write() here */
19556+ inode->i_uid = st->uid;
19557+ inode->i_gid = st->gid;
4a4d8108
AM
19558+ inode->i_atime = st->atime;
19559+ inode->i_mtime = st->mtime;
19560+ inode->i_ctime = st->ctime;
1facf9fc 19561+
4a4d8108
AM
19562+ au_cpup_attr_nlink(inode, /*force*/0);
19563+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
19564+ n = inode->i_nlink;
19565+ n -= nlink;
19566+ n += st->nlink;
f6b6e03d 19567+ smp_mb(); /* for i_nlink */
7eafdf33 19568+ /* 0 can happen */
92d182d2 19569+ set_nlink(inode, n);
4a4d8108 19570+ }
1facf9fc 19571+
4a4d8108
AM
19572+ spin_lock(&inode->i_lock);
19573+ inode->i_blocks = st->blocks;
19574+ i_size_write(inode, st->size);
19575+ spin_unlock(&inode->i_lock);
1facf9fc 19576+}
19577+
c1595e42
JR
19578+/*
19579+ * common routine for aufs_getattr() and aufs_getxattr().
19580+ * returns zero or negative (an error).
19581+ * @dentry will be read-locked in success.
19582+ */
19583+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
1facf9fc 19584+{
4a4d8108 19585+ int err;
076b876e 19586+ unsigned int mnt_flags, sigen;
c1595e42 19587+ unsigned char udba_none;
4a4d8108 19588+ aufs_bindex_t bindex;
4a4d8108
AM
19589+ struct super_block *sb, *h_sb;
19590+ struct inode *inode;
1facf9fc 19591+
c1595e42
JR
19592+ h_path->mnt = NULL;
19593+ h_path->dentry = NULL;
19594+
19595+ err = 0;
4a4d8108 19596+ sb = dentry->d_sb;
4a4d8108
AM
19597+ mnt_flags = au_mntflags(sb);
19598+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 19599+
4a4d8108 19600+ /* support fstat(2) */
027c5e7a 19601+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 19602+ sigen = au_sigen(sb);
027c5e7a
AM
19603+ err = au_digen_test(dentry, sigen);
19604+ if (!err) {
4a4d8108 19605+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 19606+ err = au_dbrange_test(dentry);
c1595e42
JR
19607+ if (unlikely(err)) {
19608+ di_read_unlock(dentry, AuLock_IR);
19609+ goto out;
19610+ }
027c5e7a 19611+ } else {
4a4d8108
AM
19612+ AuDebugOn(IS_ROOT(dentry));
19613+ di_write_lock_child(dentry);
027c5e7a
AM
19614+ err = au_dbrange_test(dentry);
19615+ if (!err)
19616+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
19617+ if (!err)
19618+ di_downgrade_lock(dentry, AuLock_IR);
19619+ else {
19620+ di_write_unlock(dentry);
19621+ goto out;
19622+ }
4a4d8108
AM
19623+ }
19624+ } else
19625+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 19626+
5527c038 19627+ inode = d_inode(dentry);
5afbbe0d 19628+ bindex = au_ibtop(inode);
c1595e42
JR
19629+ h_path->mnt = au_sbr_mnt(sb, bindex);
19630+ h_sb = h_path->mnt->mnt_sb;
19631+ if (!force
19632+ && !au_test_fs_bad_iattr(h_sb)
19633+ && udba_none)
19634+ goto out; /* success */
1facf9fc 19635+
5afbbe0d 19636+ if (au_dbtop(dentry) == bindex)
c1595e42 19637+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 19638+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
19639+ h_path->dentry = au_plink_lkup(inode, bindex);
19640+ if (IS_ERR(h_path->dentry))
19641+ /* pretending success */
19642+ h_path->dentry = NULL;
19643+ else
19644+ dput(h_path->dentry);
4a4d8108 19645+ }
c1595e42
JR
19646+
19647+out:
19648+ return err;
19649+}
19650+
19651+static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19652+ struct dentry *dentry, struct kstat *st)
19653+{
19654+ int err;
19655+ unsigned char positive;
19656+ struct path h_path;
19657+ struct inode *inode;
19658+ struct super_block *sb;
19659+
5527c038 19660+ inode = d_inode(dentry);
c1595e42
JR
19661+ sb = dentry->d_sb;
19662+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19663+ if (unlikely(err))
19664+ goto out;
19665+ err = au_h_path_getattr(dentry, /*force*/0, &h_path);
19666+ if (unlikely(err))
19667+ goto out_si;
c06a8ce3 19668+ if (unlikely(!h_path.dentry))
c1595e42 19669+ /* illegally overlapped or something */
4a4d8108
AM
19670+ goto out_fill; /* pretending success */
19671+
5527c038 19672+ positive = d_is_positive(h_path.dentry);
4a4d8108 19673+ if (positive)
c06a8ce3 19674+ err = vfs_getattr(&h_path, st);
4a4d8108
AM
19675+ if (!err) {
19676+ if (positive)
c06a8ce3 19677+ au_refresh_iattr(inode, st,
5527c038 19678+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 19679+ goto out_fill; /* success */
1facf9fc 19680+ }
7f207e10 19681+ AuTraceErr(err);
c1595e42 19682+ goto out_di;
4a4d8108 19683+
4f0767ce 19684+out_fill:
4a4d8108 19685+ generic_fillattr(inode, st);
c1595e42 19686+out_di:
4a4d8108 19687+ di_read_unlock(dentry, AuLock_IR);
c1595e42 19688+out_si:
4a4d8108 19689+ si_read_unlock(sb);
7f207e10
AM
19690+out:
19691+ AuTraceErr(err);
4a4d8108 19692+ return err;
1facf9fc 19693+}
19694+
19695+/* ---------------------------------------------------------------------- */
19696+
febd17d6
JR
19697+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
19698+ struct delayed_call *done)
4a4d8108 19699+{
c2c0f25c 19700+ const char *ret;
c2c0f25c 19701+ struct dentry *h_dentry;
febd17d6 19702+ struct inode *h_inode;
4a4d8108 19703+ int err;
c2c0f25c 19704+ aufs_bindex_t bindex;
1facf9fc 19705+
79b8bda9 19706+ ret = NULL; /* suppress a warning */
febd17d6
JR
19707+ err = -ECHILD;
19708+ if (!dentry)
19709+ goto out;
19710+
027c5e7a
AM
19711+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19712+ if (unlikely(err))
c2c0f25c 19713+ goto out;
027c5e7a
AM
19714+
19715+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
19716+ if (unlikely(err))
19717+ goto out_unlock;
19718+
19719+ err = -EINVAL;
19720+ inode = d_inode(dentry);
5afbbe0d 19721+ bindex = au_ibtop(inode);
c2c0f25c 19722+ h_inode = au_h_iptr(inode, bindex);
febd17d6 19723+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
19724+ goto out_unlock;
19725+
19726+ err = -EBUSY;
19727+ h_dentry = NULL;
5afbbe0d 19728+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
19729+ h_dentry = au_h_dptr(dentry, bindex);
19730+ if (h_dentry)
19731+ dget(h_dentry);
027c5e7a 19732+ }
c2c0f25c
AM
19733+ if (!h_dentry) {
19734+ h_dentry = d_find_any_alias(h_inode);
19735+ if (IS_ERR(h_dentry)) {
19736+ err = PTR_ERR(h_dentry);
febd17d6 19737+ goto out_unlock;
c2c0f25c
AM
19738+ }
19739+ }
19740+ if (unlikely(!h_dentry))
febd17d6 19741+ goto out_unlock;
1facf9fc 19742+
c2c0f25c 19743+ err = 0;
febd17d6 19744+ AuDbg("%pf\n", h_inode->i_op->get_link);
c2c0f25c 19745+ AuDbgDentry(h_dentry);
febd17d6 19746+ ret = h_inode->i_op->get_link(h_dentry, h_inode, done);
c2c0f25c 19747+ dput(h_dentry);
febd17d6
JR
19748+ if (IS_ERR(ret))
19749+ err = PTR_ERR(ret);
c2c0f25c 19750+
c2c0f25c
AM
19751+out_unlock:
19752+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 19753+out:
c2c0f25c
AM
19754+ if (unlikely(err))
19755+ ret = ERR_PTR(err);
19756+ AuTraceErrPtr(ret);
19757+ return ret;
4a4d8108 19758+}
1facf9fc 19759+
4a4d8108 19760+/* ---------------------------------------------------------------------- */
1facf9fc 19761+
0c3ec466 19762+static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
4a4d8108 19763+{
0c3ec466
AM
19764+ int err;
19765+ struct super_block *sb;
19766+ struct inode *h_inode;
19767+
19768+ sb = inode->i_sb;
19769+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
19770+ lockdep_off();
19771+ si_read_lock(sb, AuLock_FLUSH);
19772+ ii_write_lock_child(inode);
19773+ lockdep_on();
5afbbe0d 19774+ h_inode = au_h_iptr(inode, au_ibtop(inode));
0c3ec466
AM
19775+ err = vfsub_update_time(h_inode, ts, flags);
19776+ lockdep_off();
38d290e6
JR
19777+ if (!err)
19778+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
19779+ ii_write_unlock(inode);
19780+ si_read_unlock(sb);
19781+ lockdep_on();
38d290e6
JR
19782+
19783+ if (!err && (flags & S_VERSION))
19784+ inode_inc_iversion(inode);
19785+
0c3ec466 19786+ return err;
4a4d8108 19787+}
1facf9fc 19788+
4a4d8108 19789+/* ---------------------------------------------------------------------- */
1308ab2a 19790+
b95c5147
AM
19791+/* no getattr version will be set by module.c:aufs_init() */
19792+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
19793+ aufs_iop[] = {
19794+ [AuIop_SYMLINK] = {
19795+ .permission = aufs_permission,
c1595e42 19796+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
19797+ .get_acl = aufs_get_acl,
19798+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
19799+#endif
19800+
b95c5147
AM
19801+ .setattr = aufs_setattr,
19802+ .getattr = aufs_getattr,
0c3ec466 19803+
c1595e42 19804+#ifdef CONFIG_AUFS_XATTR
b95c5147
AM
19805+ .setxattr = aufs_setxattr,
19806+ .getxattr = aufs_getxattr,
19807+ .listxattr = aufs_listxattr,
19808+ .removexattr = aufs_removexattr,
c1595e42
JR
19809+#endif
19810+
b95c5147 19811+ .readlink = generic_readlink,
febd17d6 19812+ .get_link = aufs_get_link,
0c3ec466 19813+
b95c5147
AM
19814+ /* .update_time = aufs_update_time */
19815+ },
19816+ [AuIop_DIR] = {
19817+ .create = aufs_create,
19818+ .lookup = aufs_lookup,
19819+ .link = aufs_link,
19820+ .unlink = aufs_unlink,
19821+ .symlink = aufs_symlink,
19822+ .mkdir = aufs_mkdir,
19823+ .rmdir = aufs_rmdir,
19824+ .mknod = aufs_mknod,
19825+ .rename = aufs_rename,
19826+
19827+ .permission = aufs_permission,
c1595e42 19828+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
19829+ .get_acl = aufs_get_acl,
19830+ .set_acl = aufs_set_acl,
c1595e42
JR
19831+#endif
19832+
b95c5147
AM
19833+ .setattr = aufs_setattr,
19834+ .getattr = aufs_getattr,
0c3ec466 19835+
c1595e42 19836+#ifdef CONFIG_AUFS_XATTR
b95c5147
AM
19837+ .setxattr = aufs_setxattr,
19838+ .getxattr = aufs_getxattr,
19839+ .listxattr = aufs_listxattr,
19840+ .removexattr = aufs_removexattr,
c1595e42
JR
19841+#endif
19842+
b95c5147
AM
19843+ .update_time = aufs_update_time,
19844+ .atomic_open = aufs_atomic_open,
19845+ .tmpfile = aufs_tmpfile
19846+ },
19847+ [AuIop_OTHER] = {
19848+ .permission = aufs_permission,
c1595e42 19849+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
19850+ .get_acl = aufs_get_acl,
19851+ .set_acl = aufs_set_acl,
c1595e42
JR
19852+#endif
19853+
b95c5147
AM
19854+ .setattr = aufs_setattr,
19855+ .getattr = aufs_getattr,
0c3ec466 19856+
c1595e42 19857+#ifdef CONFIG_AUFS_XATTR
b95c5147
AM
19858+ .setxattr = aufs_setxattr,
19859+ .getxattr = aufs_getxattr,
19860+ .listxattr = aufs_listxattr,
19861+ .removexattr = aufs_removexattr,
c1595e42
JR
19862+#endif
19863+
b95c5147
AM
19864+ .update_time = aufs_update_time
19865+ }
4a4d8108 19866+};
7f207e10
AM
19867diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19868--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 19869+++ linux/fs/aufs/i_op_del.c 2016-08-17 18:01:06.148556271 +0200
5afbbe0d 19870@@ -0,0 +1,511 @@
1facf9fc 19871+/*
8cdd5066 19872+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 19873+ *
19874+ * This program, aufs is free software; you can redistribute it and/or modify
19875+ * it under the terms of the GNU General Public License as published by
19876+ * the Free Software Foundation; either version 2 of the License, or
19877+ * (at your option) any later version.
dece6358
AM
19878+ *
19879+ * This program is distributed in the hope that it will be useful,
19880+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19881+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19882+ * GNU General Public License for more details.
19883+ *
19884+ * You should have received a copy of the GNU General Public License
523b37e3 19885+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 19886+ */
19887+
19888+/*
4a4d8108 19889+ * inode operations (del entry)
1308ab2a 19890+ */
dece6358 19891+
1308ab2a 19892+#include "aufs.h"
dece6358 19893+
4a4d8108
AM
19894+/*
19895+ * decide if a new whiteout for @dentry is necessary or not.
19896+ * when it is necessary, prepare the parent dir for the upper branch whose
19897+ * branch index is @bcpup for creation. the actual creation of the whiteout will
19898+ * be done by caller.
19899+ * return value:
19900+ * 0: wh is unnecessary
19901+ * plus: wh is necessary
19902+ * minus: error
19903+ */
19904+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 19905+{
4a4d8108 19906+ int need_wh, err;
5afbbe0d 19907+ aufs_bindex_t btop;
4a4d8108 19908+ struct super_block *sb;
dece6358 19909+
4a4d8108 19910+ sb = dentry->d_sb;
5afbbe0d 19911+ btop = au_dbtop(dentry);
4a4d8108 19912+ if (*bcpup < 0) {
5afbbe0d
AM
19913+ *bcpup = btop;
19914+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
19915+ err = AuWbrCopyup(au_sbi(sb), dentry);
19916+ *bcpup = err;
19917+ if (unlikely(err < 0))
19918+ goto out;
19919+ }
19920+ } else
5afbbe0d 19921+ AuDebugOn(btop < *bcpup
5527c038 19922+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 19923+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 19924+
5afbbe0d 19925+ if (*bcpup != btop) {
4a4d8108
AM
19926+ err = au_cpup_dirs(dentry, *bcpup);
19927+ if (unlikely(err))
19928+ goto out;
19929+ need_wh = 1;
19930+ } else {
027c5e7a 19931+ struct au_dinfo *dinfo, *tmp;
4a4d8108 19932+
027c5e7a
AM
19933+ need_wh = -ENOMEM;
19934+ dinfo = au_di(dentry);
19935+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19936+ if (tmp) {
19937+ au_di_cp(tmp, dinfo);
19938+ au_di_swap(tmp, dinfo);
19939+ /* returns the number of positive dentries */
5afbbe0d
AM
19940+ need_wh = au_lkup_dentry(dentry, btop + 1,
19941+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
19942+ au_di_swap(tmp, dinfo);
19943+ au_rw_write_unlock(&tmp->di_rwsem);
19944+ au_di_free(tmp);
4a4d8108
AM
19945+ }
19946+ }
19947+ AuDbg("need_wh %d\n", need_wh);
19948+ err = need_wh;
19949+
4f0767ce 19950+out:
4a4d8108 19951+ return err;
1facf9fc 19952+}
19953+
4a4d8108
AM
19954+/*
19955+ * simple tests for the del-entry operations.
19956+ * following the checks in vfs, plus the parent-child relationship.
19957+ */
19958+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19959+ struct dentry *h_parent, int isdir)
1facf9fc 19960+{
4a4d8108
AM
19961+ int err;
19962+ umode_t h_mode;
19963+ struct dentry *h_dentry, *h_latest;
1308ab2a 19964+ struct inode *h_inode;
1facf9fc 19965+
4a4d8108 19966+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19967+ if (d_really_is_positive(dentry)) {
4a4d8108 19968+ err = -ENOENT;
5527c038
JR
19969+ if (unlikely(d_is_negative(h_dentry)))
19970+ goto out;
19971+ h_inode = d_inode(h_dentry);
19972+ if (unlikely(!h_inode->i_nlink))
4a4d8108 19973+ goto out;
1facf9fc 19974+
4a4d8108
AM
19975+ h_mode = h_inode->i_mode;
19976+ if (!isdir) {
19977+ err = -EISDIR;
19978+ if (unlikely(S_ISDIR(h_mode)))
19979+ goto out;
19980+ } else if (unlikely(!S_ISDIR(h_mode))) {
19981+ err = -ENOTDIR;
19982+ goto out;
19983+ }
19984+ } else {
19985+ /* rename(2) case */
19986+ err = -EIO;
5527c038 19987+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19988+ goto out;
19989+ }
1facf9fc 19990+
4a4d8108
AM
19991+ err = -ENOENT;
19992+ /* expected parent dir is locked */
19993+ if (unlikely(h_parent != h_dentry->d_parent))
19994+ goto out;
19995+ err = 0;
19996+
19997+ /*
19998+ * rmdir a dir may break the consistency on some filesystem.
19999+ * let's try heavy test.
20000+ */
20001+ err = -EACCES;
076b876e 20002+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 20003+ && au_test_h_perm(d_inode(h_parent),
076b876e 20004+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
20005+ goto out;
20006+
076b876e 20007+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
20008+ err = -EIO;
20009+ if (IS_ERR(h_latest))
20010+ goto out;
20011+ if (h_latest == h_dentry)
20012+ err = 0;
20013+ dput(h_latest);
20014+
4f0767ce 20015+out:
4a4d8108 20016+ return err;
1308ab2a 20017+}
1facf9fc 20018+
4a4d8108
AM
20019+/*
20020+ * decide the branch where we operate for @dentry. the branch index will be set
20021+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
20022+ * dir for reverting.
20023+ * when a new whiteout is necessary, create it.
20024+ */
20025+static struct dentry*
20026+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
20027+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 20028+{
4a4d8108
AM
20029+ struct dentry *wh_dentry;
20030+ struct super_block *sb;
20031+ struct path h_path;
20032+ int err, need_wh;
20033+ unsigned int udba;
20034+ aufs_bindex_t bcpup;
dece6358 20035+
4a4d8108
AM
20036+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
20037+ wh_dentry = ERR_PTR(need_wh);
20038+ if (unlikely(need_wh < 0))
20039+ goto out;
20040+
20041+ sb = dentry->d_sb;
20042+ udba = au_opt_udba(sb);
20043+ bcpup = *rbcpup;
20044+ err = au_pin(pin, dentry, bcpup, udba,
20045+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20046+ wh_dentry = ERR_PTR(err);
20047+ if (unlikely(err))
20048+ goto out;
20049+
20050+ h_path.dentry = au_pinned_h_parent(pin);
20051+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 20052+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
20053+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
20054+ wh_dentry = ERR_PTR(err);
20055+ if (unlikely(err))
20056+ goto out_unpin;
20057+ }
20058+
20059+ h_path.mnt = au_sbr_mnt(sb, bcpup);
20060+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
20061+ wh_dentry = NULL;
20062+ if (!need_wh)
20063+ goto out; /* success, no need to create whiteout */
20064+
20065+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
20066+ if (IS_ERR(wh_dentry))
20067+ goto out_unpin;
20068+
20069+ /* returns with the parent is locked and wh_dentry is dget-ed */
20070+ goto out; /* success */
20071+
4f0767ce 20072+out_unpin:
4a4d8108 20073+ au_unpin(pin);
4f0767ce 20074+out:
4a4d8108 20075+ return wh_dentry;
1facf9fc 20076+}
20077+
4a4d8108
AM
20078+/*
20079+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
20080+ * in order to be revertible and save time for removing many child whiteouts
20081+ * under the dir.
20082+ * returns 1 when there are too many child whiteout and caller should remove
20083+ * them asynchronously. returns 0 when the number of children is enough small to
20084+ * remove now or the branch fs is a remote fs.
20085+ * otherwise return an error.
20086+ */
20087+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
20088+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 20089+{
4a4d8108
AM
20090+ int rmdir_later, err, dirwh;
20091+ struct dentry *h_dentry;
20092+ struct super_block *sb;
5527c038 20093+ struct inode *inode;
4a4d8108
AM
20094+
20095+ sb = dentry->d_sb;
20096+ SiMustAnyLock(sb);
20097+ h_dentry = au_h_dptr(dentry, bindex);
20098+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
20099+ if (unlikely(err))
20100+ goto out;
20101+
20102+ /* stop monitoring */
5527c038
JR
20103+ inode = d_inode(dentry);
20104+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
20105+
20106+ if (!au_test_fs_remote(h_dentry->d_sb)) {
20107+ dirwh = au_sbi(sb)->si_dirwh;
20108+ rmdir_later = (dirwh <= 1);
20109+ if (!rmdir_later)
20110+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
20111+ dirwh);
20112+ if (rmdir_later)
20113+ return rmdir_later;
20114+ }
1facf9fc 20115+
4a4d8108
AM
20116+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
20117+ if (unlikely(err)) {
523b37e3
AM
20118+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
20119+ h_dentry, bindex, err);
4a4d8108
AM
20120+ err = 0;
20121+ }
dece6358 20122+
4f0767ce 20123+out:
4a4d8108
AM
20124+ AuTraceErr(err);
20125+ return err;
20126+}
1308ab2a 20127+
4a4d8108
AM
20128+/*
20129+ * final procedure for deleting a entry.
20130+ * maintain dentry and iattr.
20131+ */
20132+static void epilog(struct inode *dir, struct dentry *dentry,
20133+ aufs_bindex_t bindex)
20134+{
20135+ struct inode *inode;
1308ab2a 20136+
5527c038 20137+ inode = d_inode(dentry);
4a4d8108
AM
20138+ d_drop(dentry);
20139+ inode->i_ctime = dir->i_ctime;
1308ab2a 20140+
b912730e 20141+ au_dir_ts(dir, bindex);
4a4d8108 20142+ dir->i_version++;
1facf9fc 20143+}
20144+
4a4d8108
AM
20145+/*
20146+ * when an error happened, remove the created whiteout and revert everything.
20147+ */
7f207e10
AM
20148+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
20149+ aufs_bindex_t bwh, struct dentry *wh_dentry,
20150+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 20151+{
4a4d8108
AM
20152+ int rerr;
20153+ struct path h_path = {
20154+ .dentry = wh_dentry,
7f207e10 20155+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 20156+ };
dece6358 20157+
7f207e10 20158+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
20159+ if (!rerr) {
20160+ au_set_dbwh(dentry, bwh);
20161+ au_dtime_revert(dt);
20162+ return 0;
20163+ }
dece6358 20164+
523b37e3 20165+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 20166+ return -EIO;
1facf9fc 20167+}
20168+
4a4d8108 20169+/* ---------------------------------------------------------------------- */
1facf9fc 20170+
4a4d8108 20171+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 20172+{
4a4d8108 20173+ int err;
5afbbe0d 20174+ aufs_bindex_t bwh, bindex, btop;
523b37e3 20175+ struct inode *inode, *h_dir, *delegated;
4a4d8108 20176+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
20177+ /* to reuduce stack size */
20178+ struct {
20179+ struct au_dtime dt;
20180+ struct au_pin pin;
20181+ struct path h_path;
20182+ } *a;
1facf9fc 20183+
4a4d8108 20184+ IMustLock(dir);
027c5e7a 20185+
c2b27bf2
AM
20186+ err = -ENOMEM;
20187+ a = kmalloc(sizeof(*a), GFP_NOFS);
20188+ if (unlikely(!a))
20189+ goto out;
20190+
027c5e7a
AM
20191+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20192+ if (unlikely(err))
c2b27bf2 20193+ goto out_free;
027c5e7a
AM
20194+ err = au_d_hashed_positive(dentry);
20195+ if (unlikely(err))
20196+ goto out_unlock;
5527c038 20197+ inode = d_inode(dentry);
4a4d8108 20198+ IMustLock(inode);
027c5e7a 20199+ err = -EISDIR;
2000de60 20200+ if (unlikely(d_is_dir(dentry)))
027c5e7a 20201+ goto out_unlock; /* possible? */
1facf9fc 20202+
5afbbe0d 20203+ btop = au_dbtop(dentry);
4a4d8108
AM
20204+ bwh = au_dbwh(dentry);
20205+ bindex = -1;
027c5e7a
AM
20206+ parent = dentry->d_parent; /* dir inode is locked */
20207+ di_write_lock_parent(parent);
c2b27bf2
AM
20208+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
20209+ &a->pin);
4a4d8108
AM
20210+ err = PTR_ERR(wh_dentry);
20211+ if (IS_ERR(wh_dentry))
027c5e7a 20212+ goto out_parent;
1facf9fc 20213+
5afbbe0d
AM
20214+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
20215+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 20216+ dget(a->h_path.dentry);
5afbbe0d 20217+ if (bindex == btop) {
c2b27bf2 20218+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
20219+ delegated = NULL;
20220+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
20221+ if (unlikely(err == -EWOULDBLOCK)) {
20222+ pr_warn("cannot retry for NFSv4 delegation"
20223+ " for an internal unlink\n");
20224+ iput(delegated);
20225+ }
4a4d8108
AM
20226+ } else {
20227+ /* dir inode is locked */
5527c038 20228+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
20229+ IMustLock(h_dir);
20230+ err = 0;
20231+ }
dece6358 20232+
4a4d8108 20233+ if (!err) {
7f207e10 20234+ vfsub_drop_nlink(inode);
4a4d8108
AM
20235+ epilog(dir, dentry, bindex);
20236+
20237+ /* update target timestamps */
5afbbe0d 20238+ if (bindex == btop) {
c2b27bf2
AM
20239+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
20240+ /*ignore*/
5527c038 20241+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
20242+ } else
20243+ /* todo: this timestamp may be reverted later */
20244+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 20245+ goto out_unpin; /* success */
1facf9fc 20246+ }
20247+
4a4d8108
AM
20248+ /* revert */
20249+ if (wh_dentry) {
20250+ int rerr;
20251+
c2b27bf2
AM
20252+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20253+ &a->dt);
4a4d8108
AM
20254+ if (rerr)
20255+ err = rerr;
dece6358 20256+ }
1facf9fc 20257+
027c5e7a 20258+out_unpin:
c2b27bf2 20259+ au_unpin(&a->pin);
4a4d8108 20260+ dput(wh_dentry);
c2b27bf2 20261+ dput(a->h_path.dentry);
027c5e7a 20262+out_parent:
4a4d8108 20263+ di_write_unlock(parent);
027c5e7a 20264+out_unlock:
4a4d8108 20265+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20266+out_free:
f0c0a007 20267+ au_delayed_kfree(a);
027c5e7a 20268+out:
4a4d8108 20269+ return err;
dece6358
AM
20270+}
20271+
4a4d8108 20272+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 20273+{
4a4d8108 20274+ int err, rmdir_later;
5afbbe0d 20275+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
20276+ struct inode *inode;
20277+ struct dentry *parent, *wh_dentry, *h_dentry;
20278+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
20279+ /* to reuduce stack size */
20280+ struct {
20281+ struct au_dtime dt;
20282+ struct au_pin pin;
20283+ } *a;
1facf9fc 20284+
4a4d8108 20285+ IMustLock(dir);
027c5e7a 20286+
c2b27bf2
AM
20287+ err = -ENOMEM;
20288+ a = kmalloc(sizeof(*a), GFP_NOFS);
20289+ if (unlikely(!a))
20290+ goto out;
20291+
027c5e7a
AM
20292+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20293+ if (unlikely(err))
c2b27bf2 20294+ goto out_free;
53392da6
AM
20295+ err = au_alive_dir(dentry);
20296+ if (unlikely(err))
027c5e7a 20297+ goto out_unlock;
5527c038 20298+ inode = d_inode(dentry);
4a4d8108 20299+ IMustLock(inode);
027c5e7a 20300+ err = -ENOTDIR;
2000de60 20301+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 20302+ goto out_unlock; /* possible? */
dece6358 20303+
4a4d8108
AM
20304+ err = -ENOMEM;
20305+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20306+ if (unlikely(!args))
20307+ goto out_unlock;
dece6358 20308+
4a4d8108
AM
20309+ parent = dentry->d_parent; /* dir inode is locked */
20310+ di_write_lock_parent(parent);
20311+ err = au_test_empty(dentry, &args->whlist);
20312+ if (unlikely(err))
027c5e7a 20313+ goto out_parent;
1facf9fc 20314+
5afbbe0d 20315+ btop = au_dbtop(dentry);
4a4d8108
AM
20316+ bwh = au_dbwh(dentry);
20317+ bindex = -1;
c2b27bf2
AM
20318+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20319+ &a->pin);
4a4d8108
AM
20320+ err = PTR_ERR(wh_dentry);
20321+ if (IS_ERR(wh_dentry))
027c5e7a 20322+ goto out_parent;
1facf9fc 20323+
5afbbe0d 20324+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
20325+ dget(h_dentry);
20326+ rmdir_later = 0;
5afbbe0d
AM
20327+ if (bindex == btop) {
20328+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
20329+ if (err > 0) {
20330+ rmdir_later = err;
20331+ err = 0;
20332+ }
20333+ } else {
20334+ /* stop monitoring */
5afbbe0d 20335+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
20336+
20337+ /* dir inode is locked */
5527c038 20338+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 20339+ err = 0;
20340+ }
20341+
4a4d8108 20342+ if (!err) {
027c5e7a 20343+ vfsub_dead_dir(inode);
4a4d8108
AM
20344+ au_set_dbdiropq(dentry, -1);
20345+ epilog(dir, dentry, bindex);
1308ab2a 20346+
4a4d8108 20347+ if (rmdir_later) {
5afbbe0d 20348+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
20349+ args = NULL;
20350+ }
1308ab2a 20351+
4a4d8108 20352+ goto out_unpin; /* success */
1facf9fc 20353+ }
20354+
4a4d8108
AM
20355+ /* revert */
20356+ AuLabel(revert);
20357+ if (wh_dentry) {
20358+ int rerr;
1308ab2a 20359+
c2b27bf2
AM
20360+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20361+ &a->dt);
4a4d8108
AM
20362+ if (rerr)
20363+ err = rerr;
1facf9fc 20364+ }
20365+
4f0767ce 20366+out_unpin:
c2b27bf2 20367+ au_unpin(&a->pin);
4a4d8108
AM
20368+ dput(wh_dentry);
20369+ dput(h_dentry);
027c5e7a 20370+out_parent:
4a4d8108
AM
20371+ di_write_unlock(parent);
20372+ if (args)
20373+ au_whtmp_rmdir_free(args);
4f0767ce 20374+out_unlock:
4a4d8108 20375+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2 20376+out_free:
f0c0a007 20377+ au_delayed_kfree(a);
4f0767ce 20378+out:
4a4d8108
AM
20379+ AuTraceErr(err);
20380+ return err;
dece6358 20381+}
7f207e10
AM
20382diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20383--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 20384+++ linux/fs/aufs/i_op_ren.c 2016-08-17 18:01:06.148556271 +0200
b95c5147 20385@@ -0,0 +1,1015 @@
1facf9fc 20386+/*
8cdd5066 20387+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 20388+ *
20389+ * This program, aufs is free software; you can redistribute it and/or modify
20390+ * it under the terms of the GNU General Public License as published by
20391+ * the Free Software Foundation; either version 2 of the License, or
20392+ * (at your option) any later version.
dece6358
AM
20393+ *
20394+ * This program is distributed in the hope that it will be useful,
20395+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20396+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20397+ * GNU General Public License for more details.
20398+ *
20399+ * You should have received a copy of the GNU General Public License
523b37e3 20400+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 20401+ */
20402+
20403+/*
4a4d8108
AM
20404+ * inode operation (rename entry)
20405+ * todo: this is crazy monster
1facf9fc 20406+ */
20407+
20408+#include "aufs.h"
20409+
4a4d8108
AM
20410+enum { AuSRC, AuDST, AuSrcDst };
20411+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 20412+
4a4d8108
AM
20413+#define AuRen_ISDIR 1
20414+#define AuRen_ISSAMEDIR (1 << 1)
20415+#define AuRen_WHSRC (1 << 2)
20416+#define AuRen_WHDST (1 << 3)
20417+#define AuRen_MNT_WRITE (1 << 4)
20418+#define AuRen_DT_DSTDIR (1 << 5)
20419+#define AuRen_DIROPQ (1 << 6)
4a4d8108 20420+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
20421+#define au_fset_ren(flags, name) \
20422+ do { (flags) |= AuRen_##name; } while (0)
20423+#define au_fclr_ren(flags, name) \
20424+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 20425+
4a4d8108
AM
20426+struct au_ren_args {
20427+ struct {
20428+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
20429+ *wh_dentry;
20430+ struct inode *dir, *inode;
20431+ struct au_hinode *hdir;
20432+ struct au_dtime dt[AuParentChild];
5afbbe0d 20433+ aufs_bindex_t btop;
4a4d8108 20434+ } sd[AuSrcDst];
1facf9fc 20435+
4a4d8108
AM
20436+#define src_dentry sd[AuSRC].dentry
20437+#define src_dir sd[AuSRC].dir
20438+#define src_inode sd[AuSRC].inode
20439+#define src_h_dentry sd[AuSRC].h_dentry
20440+#define src_parent sd[AuSRC].parent
20441+#define src_h_parent sd[AuSRC].h_parent
20442+#define src_wh_dentry sd[AuSRC].wh_dentry
20443+#define src_hdir sd[AuSRC].hdir
20444+#define src_h_dir sd[AuSRC].hdir->hi_inode
20445+#define src_dt sd[AuSRC].dt
5afbbe0d 20446+#define src_btop sd[AuSRC].btop
1facf9fc 20447+
4a4d8108
AM
20448+#define dst_dentry sd[AuDST].dentry
20449+#define dst_dir sd[AuDST].dir
20450+#define dst_inode sd[AuDST].inode
20451+#define dst_h_dentry sd[AuDST].h_dentry
20452+#define dst_parent sd[AuDST].parent
20453+#define dst_h_parent sd[AuDST].h_parent
20454+#define dst_wh_dentry sd[AuDST].wh_dentry
20455+#define dst_hdir sd[AuDST].hdir
20456+#define dst_h_dir sd[AuDST].hdir->hi_inode
20457+#define dst_dt sd[AuDST].dt
5afbbe0d 20458+#define dst_btop sd[AuDST].btop
4a4d8108
AM
20459+
20460+ struct dentry *h_trap;
20461+ struct au_branch *br;
20462+ struct au_hinode *src_hinode;
20463+ struct path h_path;
20464+ struct au_nhash whlist;
027c5e7a 20465+ aufs_bindex_t btgt, src_bwh, src_bdiropq;
1facf9fc 20466+
1308ab2a 20467+ unsigned int flags;
1facf9fc 20468+
4a4d8108
AM
20469+ struct au_whtmp_rmdir *thargs;
20470+ struct dentry *h_dst;
20471+};
1308ab2a 20472+
4a4d8108 20473+/* ---------------------------------------------------------------------- */
1308ab2a 20474+
4a4d8108
AM
20475+/*
20476+ * functions for reverting.
20477+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 20478+ * everything as if nothing happened.
4a4d8108
AM
20479+ * we don't need to revert the copied-up/down the parent dir since they are
20480+ * harmless.
20481+ */
1facf9fc 20482+
4a4d8108
AM
20483+#define RevertFailure(fmt, ...) do { \
20484+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20485+ ##__VA_ARGS__, err, rerr); \
20486+ err = -EIO; \
20487+} while (0)
1facf9fc 20488+
4a4d8108 20489+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
1facf9fc 20490+{
4a4d8108 20491+ int rerr;
1facf9fc 20492+
5afbbe0d 20493+ au_hn_inode_lock_nested(a->src_hinode, AuLsc_I_CHILD);
4a4d8108 20494+ rerr = au_diropq_remove(a->src_dentry, a->btgt);
5afbbe0d 20495+ au_hn_inode_unlock(a->src_hinode);
027c5e7a 20496+ au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
4a4d8108 20497+ if (rerr)
523b37e3 20498+ RevertFailure("remove diropq %pd", a->src_dentry);
4a4d8108 20499+}
1facf9fc 20500+
4a4d8108
AM
20501+static void au_ren_rev_rename(int err, struct au_ren_args *a)
20502+{
20503+ int rerr;
523b37e3 20504+ struct inode *delegated;
1facf9fc 20505+
b4510431
AM
20506+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20507+ a->src_h_parent);
4a4d8108
AM
20508+ rerr = PTR_ERR(a->h_path.dentry);
20509+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 20510+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 20511+ return;
1facf9fc 20512+ }
20513+
523b37e3 20514+ delegated = NULL;
4a4d8108
AM
20515+ rerr = vfsub_rename(a->dst_h_dir,
20516+ au_h_dptr(a->src_dentry, a->btgt),
523b37e3
AM
20517+ a->src_h_dir, &a->h_path, &delegated);
20518+ if (unlikely(rerr == -EWOULDBLOCK)) {
20519+ pr_warn("cannot retry for NFSv4 delegation"
20520+ " for an internal rename\n");
20521+ iput(delegated);
20522+ }
4a4d8108
AM
20523+ d_drop(a->h_path.dentry);
20524+ dput(a->h_path.dentry);
20525+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20526+ if (rerr)
523b37e3 20527+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 20528+}
20529+
4a4d8108 20530+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 20531+{
4a4d8108 20532+ int rerr;
523b37e3 20533+ struct inode *delegated;
dece6358 20534+
b4510431
AM
20535+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20536+ a->dst_h_parent);
4a4d8108
AM
20537+ rerr = PTR_ERR(a->h_path.dentry);
20538+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 20539+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
20540+ return;
20541+ }
5527c038 20542+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
20543+ d_drop(a->h_path.dentry);
20544+ dput(a->h_path.dentry);
20545+ return;
dece6358
AM
20546+ }
20547+
523b37e3
AM
20548+ delegated = NULL;
20549+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20550+ &delegated);
20551+ if (unlikely(rerr == -EWOULDBLOCK)) {
20552+ pr_warn("cannot retry for NFSv4 delegation"
20553+ " for an internal rename\n");
20554+ iput(delegated);
20555+ }
4a4d8108
AM
20556+ d_drop(a->h_path.dentry);
20557+ dput(a->h_path.dentry);
20558+ if (!rerr)
20559+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20560+ else
523b37e3 20561+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 20562+}
1308ab2a 20563+
4a4d8108
AM
20564+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20565+{
20566+ int rerr;
1308ab2a 20567+
4a4d8108
AM
20568+ a->h_path.dentry = a->src_wh_dentry;
20569+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 20570+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 20571+ if (rerr)
523b37e3 20572+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 20573+}
4a4d8108 20574+#undef RevertFailure
1facf9fc 20575+
1308ab2a 20576+/* ---------------------------------------------------------------------- */
20577+
4a4d8108
AM
20578+/*
20579+ * when we have to copyup the renaming entry, do it with the rename-target name
20580+ * in order to minimize the cost (the later actual rename is unnecessary).
20581+ * otherwise rename it on the target branch.
20582+ */
20583+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 20584+{
dece6358 20585+ int err;
4a4d8108 20586+ struct dentry *d;
523b37e3 20587+ struct inode *delegated;
1facf9fc 20588+
4a4d8108 20589+ d = a->src_dentry;
5afbbe0d 20590+ if (au_dbtop(d) == a->btgt) {
4a4d8108
AM
20591+ a->h_path.dentry = a->dst_h_dentry;
20592+ if (au_ftest_ren(a->flags, DIROPQ)
20593+ && au_dbdiropq(d) == a->btgt)
20594+ au_fclr_ren(a->flags, DIROPQ);
5afbbe0d 20595+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 20596+ delegated = NULL;
4a4d8108 20597+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
523b37e3
AM
20598+ a->dst_h_dir, &a->h_path, &delegated);
20599+ if (unlikely(err == -EWOULDBLOCK)) {
20600+ pr_warn("cannot retry for NFSv4 delegation"
20601+ " for an internal rename\n");
20602+ iput(delegated);
20603+ }
c2b27bf2 20604+ } else
86dc4139 20605+ BUG();
1308ab2a 20606+
027c5e7a
AM
20607+ if (!err && a->h_dst)
20608+ /* it will be set to dinfo later */
20609+ dget(a->h_dst);
1facf9fc 20610+
dece6358
AM
20611+ return err;
20612+}
1facf9fc 20613+
4a4d8108
AM
20614+/* cf. aufs_rmdir() */
20615+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 20616+{
4a4d8108
AM
20617+ int err;
20618+ struct inode *dir;
1facf9fc 20619+
4a4d8108
AM
20620+ dir = a->dst_dir;
20621+ SiMustAnyLock(dir->i_sb);
20622+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20623+ au_sbi(dir->i_sb)->si_dirwh)
20624+ || au_test_fs_remote(a->h_dst->d_sb)) {
20625+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20626+ if (unlikely(err))
523b37e3
AM
20627+ pr_warn("failed removing whtmp dir %pd (%d), "
20628+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
20629+ } else {
20630+ au_nhash_wh_free(&a->thargs->whlist);
20631+ a->thargs->whlist = a->whlist;
20632+ a->whlist.nh_num = 0;
20633+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20634+ dput(a->h_dst);
20635+ a->thargs = NULL;
20636+ }
20637+
20638+ return 0;
1308ab2a 20639+}
1facf9fc 20640+
4a4d8108
AM
20641+/* make it 'opaque' dir. */
20642+static int au_ren_diropq(struct au_ren_args *a)
20643+{
20644+ int err;
20645+ struct dentry *diropq;
1facf9fc 20646+
4a4d8108 20647+ err = 0;
027c5e7a 20648+ a->src_bdiropq = au_dbdiropq(a->src_dentry);
4a4d8108 20649+ a->src_hinode = au_hi(a->src_inode, a->btgt);
5afbbe0d 20650+ au_hn_inode_lock_nested(a->src_hinode, AuLsc_I_CHILD);
4a4d8108 20651+ diropq = au_diropq_create(a->src_dentry, a->btgt);
5afbbe0d 20652+ au_hn_inode_unlock(a->src_hinode);
4a4d8108
AM
20653+ if (IS_ERR(diropq))
20654+ err = PTR_ERR(diropq);
076b876e
AM
20655+ else
20656+ dput(diropq);
1facf9fc 20657+
4a4d8108
AM
20658+ return err;
20659+}
1facf9fc 20660+
4a4d8108
AM
20661+static int do_rename(struct au_ren_args *a)
20662+{
20663+ int err;
20664+ struct dentry *d, *h_d;
1facf9fc 20665+
4a4d8108
AM
20666+ /* prepare workqueue args for asynchronous rmdir */
20667+ h_d = a->dst_h_dentry;
5527c038 20668+ if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
4a4d8108
AM
20669+ err = -ENOMEM;
20670+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
20671+ if (unlikely(!a->thargs))
20672+ goto out;
20673+ a->h_dst = dget(h_d);
20674+ }
1facf9fc 20675+
4a4d8108
AM
20676+ /* create whiteout for src_dentry */
20677+ if (au_ftest_ren(a->flags, WHSRC)) {
027c5e7a
AM
20678+ a->src_bwh = au_dbwh(a->src_dentry);
20679+ AuDebugOn(a->src_bwh >= 0);
4a4d8108
AM
20680+ a->src_wh_dentry
20681+ = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
20682+ err = PTR_ERR(a->src_wh_dentry);
20683+ if (IS_ERR(a->src_wh_dentry))
20684+ goto out_thargs;
20685+ }
1facf9fc 20686+
4a4d8108
AM
20687+ /* lookup whiteout for dentry */
20688+ if (au_ftest_ren(a->flags, WHDST)) {
20689+ h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
20690+ a->br);
20691+ err = PTR_ERR(h_d);
20692+ if (IS_ERR(h_d))
20693+ goto out_whsrc;
5527c038 20694+ if (d_is_negative(h_d))
4a4d8108
AM
20695+ dput(h_d);
20696+ else
20697+ a->dst_wh_dentry = h_d;
20698+ }
1facf9fc 20699+
4a4d8108
AM
20700+ /* rename dentry to tmpwh */
20701+ if (a->thargs) {
20702+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
20703+ if (unlikely(err))
20704+ goto out_whdst;
dece6358 20705+
4a4d8108
AM
20706+ d = a->dst_dentry;
20707+ au_set_h_dptr(d, a->btgt, NULL);
86dc4139 20708+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
4a4d8108
AM
20709+ if (unlikely(err))
20710+ goto out_whtmp;
20711+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
20712+ }
1facf9fc 20713+
5afbbe0d 20714+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
1facf9fc 20715+
4a4d8108
AM
20716+ /* rename by vfs_rename or cpup */
20717+ d = a->dst_dentry;
20718+ if (au_ftest_ren(a->flags, ISDIR)
20719+ && (a->dst_wh_dentry
20720+ || au_dbdiropq(d) == a->btgt
20721+ /* hide the lower to keep xino */
5afbbe0d 20722+ || a->btgt < au_dbbot(d)
4a4d8108
AM
20723+ || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
20724+ au_fset_ren(a->flags, DIROPQ);
20725+ err = au_ren_or_cpup(a);
20726+ if (unlikely(err))
20727+ /* leave the copied-up one */
20728+ goto out_whtmp;
1308ab2a 20729+
4a4d8108
AM
20730+ /* make dir opaque */
20731+ if (au_ftest_ren(a->flags, DIROPQ)) {
20732+ err = au_ren_diropq(a);
20733+ if (unlikely(err))
20734+ goto out_rename;
20735+ }
1308ab2a 20736+
4a4d8108 20737+ /* update target timestamps */
5afbbe0d 20738+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
20739+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20740+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 20741+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 20742+
4a4d8108
AM
20743+ /* remove whiteout for dentry */
20744+ if (a->dst_wh_dentry) {
20745+ a->h_path.dentry = a->dst_wh_dentry;
20746+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20747+ a->dst_dentry);
20748+ if (unlikely(err))
20749+ goto out_diropq;
20750+ }
1facf9fc 20751+
4a4d8108
AM
20752+ /* remove whtmp */
20753+ if (a->thargs)
20754+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 20755+
076b876e 20756+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
4a4d8108
AM
20757+ err = 0;
20758+ goto out_success;
20759+
4f0767ce 20760+out_diropq:
4a4d8108
AM
20761+ if (au_ftest_ren(a->flags, DIROPQ))
20762+ au_ren_rev_diropq(err, a);
4f0767ce 20763+out_rename:
7e9cd9fe 20764+ au_ren_rev_rename(err, a);
027c5e7a 20765+ dput(a->h_dst);
4f0767ce 20766+out_whtmp:
4a4d8108
AM
20767+ if (a->thargs)
20768+ au_ren_rev_whtmp(err, a);
4f0767ce 20769+out_whdst:
4a4d8108
AM
20770+ dput(a->dst_wh_dentry);
20771+ a->dst_wh_dentry = NULL;
4f0767ce 20772+out_whsrc:
4a4d8108
AM
20773+ if (a->src_wh_dentry)
20774+ au_ren_rev_whsrc(err, a);
4f0767ce 20775+out_success:
4a4d8108
AM
20776+ dput(a->src_wh_dentry);
20777+ dput(a->dst_wh_dentry);
4f0767ce 20778+out_thargs:
4a4d8108
AM
20779+ if (a->thargs) {
20780+ dput(a->h_dst);
20781+ au_whtmp_rmdir_free(a->thargs);
20782+ a->thargs = NULL;
20783+ }
4f0767ce 20784+out:
4a4d8108 20785+ return err;
dece6358 20786+}
1facf9fc 20787+
1308ab2a 20788+/* ---------------------------------------------------------------------- */
1facf9fc 20789+
4a4d8108
AM
20790+/*
20791+ * test if @dentry dir can be rename destination or not.
20792+ * success means, it is a logically empty dir.
20793+ */
20794+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 20795+{
4a4d8108 20796+ return au_test_empty(dentry, whlist);
1308ab2a 20797+}
1facf9fc 20798+
4a4d8108
AM
20799+/*
20800+ * test if @dentry dir can be rename source or not.
20801+ * if it can, return 0 and @children is filled.
20802+ * success means,
20803+ * - it is a logically empty dir.
20804+ * - or, it exists on writable branch and has no children including whiteouts
20805+ * on the lower branch.
20806+ */
20807+static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20808+{
20809+ int err;
20810+ unsigned int rdhash;
5afbbe0d 20811+ aufs_bindex_t btop;
1facf9fc 20812+
5afbbe0d
AM
20813+ btop = au_dbtop(dentry);
20814+ if (btop != btgt) {
4a4d8108 20815+ struct au_nhash whlist;
dece6358 20816+
4a4d8108
AM
20817+ SiMustAnyLock(dentry->d_sb);
20818+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20819+ if (!rdhash)
20820+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20821+ dentry));
20822+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20823+ if (unlikely(err))
20824+ goto out;
20825+ err = au_test_empty(dentry, &whlist);
20826+ au_nhash_wh_free(&whlist);
20827+ goto out;
20828+ }
dece6358 20829+
5afbbe0d 20830+ if (btop == au_dbtaildir(dentry))
4a4d8108 20831+ return 0; /* success */
dece6358 20832+
4a4d8108 20833+ err = au_test_empty_lower(dentry);
1facf9fc 20834+
4f0767ce 20835+out:
4a4d8108
AM
20836+ if (err == -ENOTEMPTY) {
20837+ AuWarn1("renaming dir who has child(ren) on multiple branches,"
20838+ " is not supported\n");
20839+ err = -EXDEV;
20840+ }
20841+ return err;
20842+}
1308ab2a 20843+
4a4d8108
AM
20844+/* side effect: sets whlist and h_dentry */
20845+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 20846+{
4a4d8108
AM
20847+ int err;
20848+ unsigned int rdhash;
20849+ struct dentry *d;
1facf9fc 20850+
4a4d8108
AM
20851+ d = a->dst_dentry;
20852+ SiMustAnyLock(d->d_sb);
1facf9fc 20853+
4a4d8108
AM
20854+ err = 0;
20855+ if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20856+ rdhash = au_sbi(d->d_sb)->si_rdhash;
20857+ if (!rdhash)
20858+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20859+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20860+ if (unlikely(err))
20861+ goto out;
1308ab2a 20862+
5afbbe0d 20863+ au_set_dbtop(d, a->dst_btop);
4a4d8108 20864+ err = may_rename_dstdir(d, &a->whlist);
5afbbe0d 20865+ au_set_dbtop(d, a->btgt);
4a4d8108 20866+ }
5afbbe0d 20867+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
20868+ if (unlikely(err))
20869+ goto out;
20870+
20871+ d = a->src_dentry;
5afbbe0d 20872+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
20873+ if (au_ftest_ren(a->flags, ISDIR)) {
20874+ err = may_rename_srcdir(d, a->btgt);
20875+ if (unlikely(err)) {
20876+ au_nhash_wh_free(&a->whlist);
20877+ a->whlist.nh_num = 0;
20878+ }
20879+ }
4f0767ce 20880+out:
4a4d8108 20881+ return err;
1facf9fc 20882+}
20883+
4a4d8108 20884+/* ---------------------------------------------------------------------- */
1facf9fc 20885+
4a4d8108
AM
20886+/*
20887+ * simple tests for rename.
20888+ * following the checks in vfs, plus the parent-child relationship.
20889+ */
20890+static int au_may_ren(struct au_ren_args *a)
20891+{
20892+ int err, isdir;
20893+ struct inode *h_inode;
1facf9fc 20894+
5afbbe0d 20895+ if (a->src_btop == a->btgt) {
4a4d8108
AM
20896+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20897+ au_ftest_ren(a->flags, ISDIR));
20898+ if (unlikely(err))
20899+ goto out;
20900+ err = -EINVAL;
20901+ if (unlikely(a->src_h_dentry == a->h_trap))
20902+ goto out;
20903+ }
1facf9fc 20904+
4a4d8108 20905+ err = 0;
5afbbe0d 20906+ if (a->dst_btop != a->btgt)
4a4d8108 20907+ goto out;
1facf9fc 20908+
027c5e7a
AM
20909+ err = -ENOTEMPTY;
20910+ if (unlikely(a->dst_h_dentry == a->h_trap))
20911+ goto out;
20912+
4a4d8108 20913+ err = -EIO;
4a4d8108 20914+ isdir = !!au_ftest_ren(a->flags, ISDIR);
5527c038
JR
20915+ if (d_really_is_negative(a->dst_dentry)) {
20916+ if (d_is_negative(a->dst_h_dentry))
20917+ err = au_may_add(a->dst_dentry, a->btgt,
20918+ a->dst_h_parent, isdir);
4a4d8108 20919+ } else {
5527c038 20920+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 20921+ goto out;
5527c038
JR
20922+ h_inode = d_inode(a->dst_h_dentry);
20923+ if (h_inode->i_nlink)
20924+ err = au_may_del(a->dst_dentry, a->btgt,
20925+ a->dst_h_parent, isdir);
4a4d8108 20926+ }
1facf9fc 20927+
4f0767ce 20928+out:
4a4d8108
AM
20929+ if (unlikely(err == -ENOENT || err == -EEXIST))
20930+ err = -EIO;
20931+ AuTraceErr(err);
20932+ return err;
20933+}
1facf9fc 20934+
1308ab2a 20935+/* ---------------------------------------------------------------------- */
1facf9fc 20936+
4a4d8108
AM
20937+/*
20938+ * locking order
20939+ * (VFS)
20940+ * - src_dir and dir by lock_rename()
20941+ * - inode if exitsts
20942+ * (aufs)
20943+ * - lock all
20944+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20945+ * + si_read_lock
20946+ * + di_write_lock2_child()
20947+ * + di_write_lock_child()
20948+ * + ii_write_lock_child()
20949+ * + di_write_lock_child2()
20950+ * + ii_write_lock_child2()
20951+ * + src_parent and parent
20952+ * + di_write_lock_parent()
20953+ * + ii_write_lock_parent()
20954+ * + di_write_lock_parent2()
20955+ * + ii_write_lock_parent2()
20956+ * + lower src_dir and dir by vfsub_lock_rename()
20957+ * + verify the every relationships between child and parent. if any
20958+ * of them failed, unlock all and return -EBUSY.
20959+ */
20960+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 20961+{
4a4d8108
AM
20962+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20963+ a->dst_h_parent, a->dst_hdir);
86dc4139
AM
20964+ if (au_ftest_ren(a->flags, MNT_WRITE))
20965+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 20966+}
20967+
4a4d8108 20968+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 20969+{
4a4d8108
AM
20970+ int err;
20971+ unsigned int udba;
1308ab2a 20972+
4a4d8108
AM
20973+ err = 0;
20974+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20975+ a->src_hdir = au_hi(a->src_dir, a->btgt);
20976+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20977+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
20978+
20979+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
20980+ if (unlikely(err))
20981+ goto out;
20982+ au_fset_ren(a->flags, MNT_WRITE);
4a4d8108
AM
20983+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20984+ a->dst_h_parent, a->dst_hdir);
20985+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
20986+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
20987+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 20988+ err = au_busy_or_stale();
5afbbe0d 20989+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 20990+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 20991+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 20992+ a->br);
5afbbe0d 20993+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 20994+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 20995+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 20996+ a->br);
86dc4139 20997+ if (!err)
4a4d8108 20998+ goto out; /* success */
4a4d8108
AM
20999+
21000+ err = au_busy_or_stale();
4a4d8108 21001+ au_ren_unlock(a);
86dc4139 21002+
4f0767ce 21003+out:
4a4d8108 21004+ return err;
1facf9fc 21005+}
21006+
21007+/* ---------------------------------------------------------------------- */
21008+
4a4d8108 21009+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 21010+{
4a4d8108 21011+ struct inode *dir;
dece6358 21012+
4a4d8108
AM
21013+ dir = a->dst_dir;
21014+ dir->i_version++;
21015+ if (au_ftest_ren(a->flags, ISDIR)) {
21016+ /* is this updating defined in POSIX? */
21017+ au_cpup_attr_timesizes(a->src_inode);
21018+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 21019+ }
027c5e7a 21020+
b912730e 21021+ au_dir_ts(dir, a->btgt);
dece6358 21022+
4a4d8108
AM
21023+ if (au_ftest_ren(a->flags, ISSAMEDIR))
21024+ return;
dece6358 21025+
4a4d8108
AM
21026+ dir = a->src_dir;
21027+ dir->i_version++;
21028+ if (au_ftest_ren(a->flags, ISDIR))
21029+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 21030+ au_dir_ts(dir, a->btgt);
1facf9fc 21031+}
21032+
4a4d8108 21033+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 21034+{
5afbbe0d 21035+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
21036+ struct dentry *d, *h_d;
21037+ struct inode *i, *h_i;
21038+ struct super_block *sb;
dece6358 21039+
027c5e7a
AM
21040+ d = a->dst_dentry;
21041+ d_drop(d);
21042+ if (a->h_dst)
21043+ /* already dget-ed by au_ren_or_cpup() */
21044+ au_set_h_dptr(d, a->btgt, a->h_dst);
21045+
21046+ i = a->dst_inode;
21047+ if (i) {
21048+ if (!au_ftest_ren(a->flags, ISDIR))
21049+ vfsub_drop_nlink(i);
21050+ else {
21051+ vfsub_dead_dir(i);
21052+ au_cpup_attr_timesizes(i);
21053+ }
21054+ au_update_dbrange(d, /*do_put_zero*/1);
21055+ } else {
5afbbe0d
AM
21056+ bbot = a->btgt;
21057+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 21058+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
21059+ bbot = au_dbbot(d);
21060+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
21061+ au_set_h_dptr(d, bindex, NULL);
21062+ au_update_dbrange(d, /*do_put_zero*/0);
21063+ }
21064+
4a4d8108
AM
21065+ d = a->src_dentry;
21066+ au_set_dbwh(d, -1);
5afbbe0d
AM
21067+ bbot = au_dbbot(d);
21068+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
4a4d8108
AM
21069+ h_d = au_h_dptr(d, bindex);
21070+ if (h_d)
21071+ au_set_h_dptr(d, bindex, NULL);
21072+ }
5afbbe0d 21073+ au_set_dbbot(d, a->btgt);
4a4d8108
AM
21074+
21075+ sb = d->d_sb;
21076+ i = a->src_inode;
21077+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
21078+ return; /* success */
21079+
5afbbe0d
AM
21080+ bbot = au_ibbot(i);
21081+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
4a4d8108
AM
21082+ h_i = au_h_iptr(i, bindex);
21083+ if (h_i) {
21084+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
21085+ /* ignore this error */
21086+ au_set_h_iptr(i, bindex, NULL, 0);
21087+ }
21088+ }
5afbbe0d 21089+ au_set_ibbot(i, a->btgt);
1308ab2a 21090+}
dece6358 21091+
4a4d8108
AM
21092+/* ---------------------------------------------------------------------- */
21093+
21094+/* mainly for link(2) and rename(2) */
21095+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 21096+{
4a4d8108
AM
21097+ aufs_bindex_t bdiropq, bwh;
21098+ struct dentry *parent;
21099+ struct au_branch *br;
21100+
21101+ parent = dentry->d_parent;
5527c038 21102+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
21103+
21104+ bdiropq = au_dbdiropq(parent);
21105+ bwh = au_dbwh(dentry);
21106+ br = au_sbr(dentry->d_sb, btgt);
21107+ if (au_br_rdonly(br)
21108+ || (0 <= bdiropq && bdiropq < btgt)
21109+ || (0 <= bwh && bwh < btgt))
21110+ btgt = -1;
21111+
21112+ AuDbg("btgt %d\n", btgt);
21113+ return btgt;
1facf9fc 21114+}
21115+
5afbbe0d 21116+/* sets src_btop, dst_btop and btgt */
4a4d8108 21117+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 21118+{
4a4d8108
AM
21119+ int err;
21120+ struct au_wr_dir_args wr_dir_args = {
21121+ /* .force_btgt = -1, */
21122+ .flags = AuWrDir_ADD_ENTRY
21123+ };
dece6358 21124+
5afbbe0d
AM
21125+ a->src_btop = au_dbtop(a->src_dentry);
21126+ a->dst_btop = au_dbtop(a->dst_dentry);
4a4d8108
AM
21127+ if (au_ftest_ren(a->flags, ISDIR))
21128+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
21129+ wr_dir_args.force_btgt = a->src_btop;
21130+ if (a->dst_inode && a->dst_btop < a->src_btop)
21131+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
21132+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
21133+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
21134+ a->btgt = err;
dece6358 21135+
4a4d8108 21136+ return err;
1facf9fc 21137+}
21138+
4a4d8108 21139+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 21140+{
4a4d8108
AM
21141+ a->h_path.dentry = a->src_h_parent;
21142+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
21143+ if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
21144+ a->h_path.dentry = a->dst_h_parent;
21145+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
21146+ }
1facf9fc 21147+
4a4d8108
AM
21148+ au_fclr_ren(a->flags, DT_DSTDIR);
21149+ if (!au_ftest_ren(a->flags, ISDIR))
21150+ return;
dece6358 21151+
4a4d8108
AM
21152+ a->h_path.dentry = a->src_h_dentry;
21153+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 21154+ if (d_is_positive(a->dst_h_dentry)) {
4a4d8108
AM
21155+ au_fset_ren(a->flags, DT_DSTDIR);
21156+ a->h_path.dentry = a->dst_h_dentry;
21157+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
21158+ }
1308ab2a 21159+}
dece6358 21160+
4a4d8108 21161+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 21162+{
4a4d8108 21163+ struct dentry *h_d;
febd17d6 21164+ struct inode *h_inode;
4a4d8108
AM
21165+
21166+ au_dtime_revert(a->src_dt + AuPARENT);
21167+ if (!au_ftest_ren(a->flags, ISSAMEDIR))
21168+ au_dtime_revert(a->dst_dt + AuPARENT);
21169+
21170+ if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
21171+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
21172+ h_inode = d_inode(h_d);
21173+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21174+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 21175+ inode_unlock(h_inode);
4a4d8108
AM
21176+
21177+ if (au_ftest_ren(a->flags, DT_DSTDIR)) {
21178+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
21179+ h_inode = d_inode(h_d);
21180+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21181+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 21182+ inode_unlock(h_inode);
1facf9fc 21183+ }
21184+ }
21185+}
21186+
4a4d8108
AM
21187+/* ---------------------------------------------------------------------- */
21188+
21189+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
21190+ struct inode *_dst_dir, struct dentry *_dst_dentry)
1facf9fc 21191+{
e49829fe 21192+ int err, flags;
4a4d8108
AM
21193+ /* reduce stack space */
21194+ struct au_ren_args *a;
21195+
523b37e3 21196+ AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
4a4d8108
AM
21197+ IMustLock(_src_dir);
21198+ IMustLock(_dst_dir);
21199+
21200+ err = -ENOMEM;
21201+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
21202+ a = kzalloc(sizeof(*a), GFP_NOFS);
21203+ if (unlikely(!a))
21204+ goto out;
21205+
21206+ a->src_dir = _src_dir;
21207+ a->src_dentry = _src_dentry;
5527c038
JR
21208+ a->src_inode = NULL;
21209+ if (d_really_is_positive(a->src_dentry))
21210+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
21211+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
21212+ a->dst_dir = _dst_dir;
21213+ a->dst_dentry = _dst_dentry;
5527c038
JR
21214+ a->dst_inode = NULL;
21215+ if (d_really_is_positive(a->dst_dentry))
21216+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
21217+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
21218+ if (a->dst_inode) {
21219+ IMustLock(a->dst_inode);
21220+ au_igrab(a->dst_inode);
1facf9fc 21221+ }
1facf9fc 21222+
4a4d8108 21223+ err = -ENOTDIR;
027c5e7a 21224+ flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 21225+ if (d_is_dir(a->src_dentry)) {
4a4d8108 21226+ au_fset_ren(a->flags, ISDIR);
5527c038 21227+ if (unlikely(d_really_is_positive(a->dst_dentry)
2000de60 21228+ && !d_is_dir(a->dst_dentry)))
4a4d8108 21229+ goto out_free;
b95c5147
AM
21230+ flags |= AuLock_DIRS;
21231+ }
21232+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, flags);
e49829fe
JR
21233+ if (unlikely(err))
21234+ goto out_free;
1facf9fc 21235+
027c5e7a
AM
21236+ err = au_d_hashed_positive(a->src_dentry);
21237+ if (unlikely(err))
21238+ goto out_unlock;
21239+ err = -ENOENT;
21240+ if (a->dst_inode) {
21241+ /*
21242+ * If it is a dir, VFS unhash dst_dentry before this
21243+ * function. It means we cannot rely upon d_unhashed().
21244+ */
21245+ if (unlikely(!a->dst_inode->i_nlink))
21246+ goto out_unlock;
21247+ if (!S_ISDIR(a->dst_inode->i_mode)) {
21248+ err = au_d_hashed_positive(a->dst_dentry);
21249+ if (unlikely(err))
21250+ goto out_unlock;
21251+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
21252+ goto out_unlock;
21253+ } else if (unlikely(d_unhashed(a->dst_dentry)))
21254+ goto out_unlock;
21255+
7eafdf33
AM
21256+ /*
21257+ * is it possible?
79b8bda9 21258+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
21259+ * there may exist a problem somewhere else.
21260+ */
21261+ err = -EINVAL;
5527c038 21262+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
21263+ goto out_unlock;
21264+
4a4d8108
AM
21265+ au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
21266+ di_write_lock_parent(a->dst_parent);
1facf9fc 21267+
4a4d8108
AM
21268+ /* which branch we process */
21269+ err = au_ren_wbr(a);
21270+ if (unlikely(err < 0))
027c5e7a 21271+ goto out_parent;
4a4d8108 21272+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 21273+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 21274+
4a4d8108
AM
21275+ /* are they available to be renamed */
21276+ err = au_ren_may_dir(a);
21277+ if (unlikely(err))
21278+ goto out_children;
1facf9fc 21279+
4a4d8108 21280+ /* prepare the writable parent dir on the same branch */
5afbbe0d 21281+ if (a->dst_btop == a->btgt) {
4a4d8108
AM
21282+ au_fset_ren(a->flags, WHDST);
21283+ } else {
21284+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
21285+ if (unlikely(err))
21286+ goto out_children;
21287+ }
1facf9fc 21288+
4a4d8108
AM
21289+ if (a->src_dir != a->dst_dir) {
21290+ /*
21291+ * this temporary unlock is safe,
21292+ * because both dir->i_mutex are locked.
21293+ */
21294+ di_write_unlock(a->dst_parent);
21295+ di_write_lock_parent(a->src_parent);
21296+ err = au_wr_dir_need_wh(a->src_dentry,
21297+ au_ftest_ren(a->flags, ISDIR),
21298+ &a->btgt);
21299+ di_write_unlock(a->src_parent);
21300+ di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
21301+ au_fclr_ren(a->flags, ISSAMEDIR);
21302+ } else
21303+ err = au_wr_dir_need_wh(a->src_dentry,
21304+ au_ftest_ren(a->flags, ISDIR),
21305+ &a->btgt);
21306+ if (unlikely(err < 0))
21307+ goto out_children;
21308+ if (err)
21309+ au_fset_ren(a->flags, WHSRC);
1facf9fc 21310+
86dc4139 21311+ /* cpup src */
5afbbe0d 21312+ if (a->src_btop != a->btgt) {
86dc4139
AM
21313+ struct au_pin pin;
21314+
21315+ err = au_pin(&pin, a->src_dentry, a->btgt,
21316+ au_opt_udba(a->src_dentry->d_sb),
21317+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 21318+ if (!err) {
c2b27bf2
AM
21319+ struct au_cp_generic cpg = {
21320+ .dentry = a->src_dentry,
21321+ .bdst = a->btgt,
5afbbe0d 21322+ .bsrc = a->src_btop,
c2b27bf2
AM
21323+ .len = -1,
21324+ .pin = &pin,
21325+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21326+ };
5afbbe0d 21327+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 21328+ err = au_sio_cpup_simple(&cpg);
367653fa 21329+ au_unpin(&pin);
86dc4139 21330+ }
86dc4139
AM
21331+ if (unlikely(err))
21332+ goto out_children;
5afbbe0d 21333+ a->src_btop = a->btgt;
86dc4139
AM
21334+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21335+ au_fset_ren(a->flags, WHSRC);
21336+ }
21337+
4a4d8108
AM
21338+ /* lock them all */
21339+ err = au_ren_lock(a);
21340+ if (unlikely(err))
86dc4139 21341+ /* leave the copied-up one */
4a4d8108 21342+ goto out_children;
1facf9fc 21343+
4a4d8108
AM
21344+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21345+ err = au_may_ren(a);
21346+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21347+ err = -ENAMETOOLONG;
21348+ if (unlikely(err))
21349+ goto out_hdir;
1facf9fc 21350+
4a4d8108
AM
21351+ /* store timestamps to be revertible */
21352+ au_ren_dt(a);
1facf9fc 21353+
4a4d8108
AM
21354+ /* here we go */
21355+ err = do_rename(a);
21356+ if (unlikely(err))
21357+ goto out_dt;
21358+
21359+ /* update dir attributes */
21360+ au_ren_refresh_dir(a);
21361+
21362+ /* dput/iput all lower dentries */
21363+ au_ren_refresh(a);
21364+
21365+ goto out_hdir; /* success */
21366+
4f0767ce 21367+out_dt:
4a4d8108 21368+ au_ren_rev_dt(err, a);
4f0767ce 21369+out_hdir:
4a4d8108 21370+ au_ren_unlock(a);
4f0767ce 21371+out_children:
4a4d8108 21372+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
21373+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
21374+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 21375+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 21376+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 21377+ }
027c5e7a 21378+out_parent:
4a4d8108
AM
21379+ if (!err)
21380+ d_move(a->src_dentry, a->dst_dentry);
027c5e7a 21381+ else {
5afbbe0d 21382+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
21383+ if (!a->dst_inode)
21384+ d_drop(a->dst_dentry);
21385+ }
4a4d8108
AM
21386+ if (au_ftest_ren(a->flags, ISSAMEDIR))
21387+ di_write_unlock(a->dst_parent);
21388+ else
21389+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 21390+out_unlock:
4a4d8108 21391+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 21392+out_free:
4a4d8108
AM
21393+ iput(a->dst_inode);
21394+ if (a->thargs)
21395+ au_whtmp_rmdir_free(a->thargs);
f0c0a007 21396+ au_delayed_kfree(a);
4f0767ce 21397+out:
4a4d8108
AM
21398+ AuTraceErr(err);
21399+ return err;
1308ab2a 21400+}
7f207e10
AM
21401diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21402--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21403+++ linux/fs/aufs/Kconfig 2015-09-24 10:47:58.248052907 +0200
c1595e42 21404@@ -0,0 +1,185 @@
4a4d8108
AM
21405+config AUFS_FS
21406+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
21407+ help
21408+ Aufs is a stackable unification filesystem such as Unionfs,
21409+ which unifies several directories and provides a merged single
21410+ directory.
21411+ In the early days, aufs was entirely re-designed and
21412+ re-implemented Unionfs Version 1.x series. Introducing many
21413+ original ideas, approaches and improvements, it becomes totally
21414+ different from Unionfs while keeping the basic features.
1facf9fc 21415+
4a4d8108
AM
21416+if AUFS_FS
21417+choice
21418+ prompt "Maximum number of branches"
21419+ default AUFS_BRANCH_MAX_127
21420+ help
21421+ Specifies the maximum number of branches (or member directories)
21422+ in a single aufs. The larger value consumes more system
21423+ resources and has a minor impact to performance.
21424+config AUFS_BRANCH_MAX_127
21425+ bool "127"
21426+ help
21427+ Specifies the maximum number of branches (or member directories)
21428+ in a single aufs. The larger value consumes more system
21429+ resources and has a minor impact to performance.
21430+config AUFS_BRANCH_MAX_511
21431+ bool "511"
21432+ help
21433+ Specifies the maximum number of branches (or member directories)
21434+ in a single aufs. The larger value consumes more system
21435+ resources and has a minor impact to performance.
21436+config AUFS_BRANCH_MAX_1023
21437+ bool "1023"
21438+ help
21439+ Specifies the maximum number of branches (or member directories)
21440+ in a single aufs. The larger value consumes more system
21441+ resources and has a minor impact to performance.
21442+config AUFS_BRANCH_MAX_32767
21443+ bool "32767"
21444+ help
21445+ Specifies the maximum number of branches (or member directories)
21446+ in a single aufs. The larger value consumes more system
21447+ resources and has a minor impact to performance.
21448+endchoice
1facf9fc 21449+
e49829fe
JR
21450+config AUFS_SBILIST
21451+ bool
21452+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
21453+ default y
21454+ help
21455+ Automatic configuration for internal use.
21456+ When aufs supports Magic SysRq or /proc, enabled automatically.
21457+
4a4d8108
AM
21458+config AUFS_HNOTIFY
21459+ bool "Detect direct branch access (bypassing aufs)"
21460+ help
21461+ If you want to modify files on branches directly, eg. bypassing aufs,
21462+ and want aufs to detect the changes of them fully, then enable this
21463+ option and use 'udba=notify' mount option.
7f207e10 21464+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
21465+ It will have a negative impact to the performance.
21466+ See detail in aufs.5.
dece6358 21467+
4a4d8108
AM
21468+choice
21469+ prompt "method" if AUFS_HNOTIFY
21470+ default AUFS_HFSNOTIFY
21471+config AUFS_HFSNOTIFY
21472+ bool "fsnotify"
21473+ select FSNOTIFY
4a4d8108 21474+endchoice
1facf9fc 21475+
4a4d8108
AM
21476+config AUFS_EXPORT
21477+ bool "NFS-exportable aufs"
2cbb1c4b 21478+ depends on EXPORTFS
4a4d8108
AM
21479+ help
21480+ If you want to export your mounted aufs via NFS, then enable this
21481+ option. There are several requirements for this configuration.
21482+ See detail in aufs.5.
1facf9fc 21483+
4a4d8108
AM
21484+config AUFS_INO_T_64
21485+ bool
21486+ depends on AUFS_EXPORT
21487+ depends on 64BIT && !(ALPHA || S390)
21488+ default y
21489+ help
21490+ Automatic configuration for internal use.
21491+ /* typedef unsigned long/int __kernel_ino_t */
21492+ /* alpha and s390x are int */
1facf9fc 21493+
c1595e42
JR
21494+config AUFS_XATTR
21495+ bool "support for XATTR/EA (including Security Labels)"
21496+ help
21497+ If your branch fs supports XATTR/EA and you want to make them
21498+ available in aufs too, then enable this opsion and specify the
21499+ branch attributes for EA.
21500+ See detail in aufs.5.
21501+
076b876e
AM
21502+config AUFS_FHSM
21503+ bool "File-based Hierarchical Storage Management"
21504+ help
21505+ Hierarchical Storage Management (or HSM) is a well-known feature
21506+ in the storage world. Aufs provides this feature as file-based.
21507+ with multiple branches.
21508+ These multiple branches are prioritized, ie. the topmost one
21509+ should be the fastest drive and be used heavily.
21510+
4a4d8108
AM
21511+config AUFS_RDU
21512+ bool "Readdir in userspace"
21513+ help
21514+ Aufs has two methods to provide a merged view for a directory,
21515+ by a user-space library and by kernel-space natively. The latter
21516+ is always enabled but sometimes large and slow.
21517+ If you enable this option, install the library in aufs2-util
21518+ package, and set some environment variables for your readdir(3),
21519+ then the work will be handled in user-space which generally
21520+ shows better performance in most cases.
21521+ See detail in aufs.5.
1facf9fc 21522+
4a4d8108
AM
21523+config AUFS_SHWH
21524+ bool "Show whiteouts"
21525+ help
21526+ If you want to make the whiteouts in aufs visible, then enable
21527+ this option and specify 'shwh' mount option. Although it may
21528+ sounds like philosophy or something, but in technically it
21529+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 21530+
4a4d8108
AM
21531+config AUFS_BR_RAMFS
21532+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
21533+ help
21534+ If you want to use ramfs as an aufs branch fs, then enable this
21535+ option. Generally tmpfs is recommended.
21536+ Aufs prohibited them to be a branch fs by default, because
21537+ initramfs becomes unusable after switch_root or something
21538+ generally. If you sets initramfs as an aufs branch and boot your
21539+ system by switch_root, you will meet a problem easily since the
21540+ files in initramfs may be inaccessible.
21541+ Unless you are going to use ramfs as an aufs branch fs without
21542+ switch_root or something, leave it N.
1facf9fc 21543+
4a4d8108
AM
21544+config AUFS_BR_FUSE
21545+ bool "Fuse fs as an aufs branch"
21546+ depends on FUSE_FS
21547+ select AUFS_POLL
21548+ help
21549+ If you want to use fuse-based userspace filesystem as an aufs
21550+ branch fs, then enable this option.
21551+ It implements the internal poll(2) operation which is
21552+ implemented by fuse only (curretnly).
1facf9fc 21553+
4a4d8108
AM
21554+config AUFS_POLL
21555+ bool
21556+ help
21557+ Automatic configuration for internal use.
1facf9fc 21558+
4a4d8108
AM
21559+config AUFS_BR_HFSPLUS
21560+ bool "Hfsplus as an aufs branch"
21561+ depends on HFSPLUS_FS
21562+ default y
21563+ help
21564+ If you want to use hfsplus fs as an aufs branch fs, then enable
21565+ this option. This option introduces a small overhead at
21566+ copying-up a file on hfsplus.
1facf9fc 21567+
4a4d8108
AM
21568+config AUFS_BDEV_LOOP
21569+ bool
21570+ depends on BLK_DEV_LOOP
21571+ default y
21572+ help
21573+ Automatic configuration for internal use.
21574+ Convert =[ym] into =y.
1308ab2a 21575+
4a4d8108
AM
21576+config AUFS_DEBUG
21577+ bool "Debug aufs"
21578+ help
21579+ Enable this to compile aufs internal debug code.
21580+ It will have a negative impact to the performance.
21581+
21582+config AUFS_MAGIC_SYSRQ
21583+ bool
21584+ depends on AUFS_DEBUG && MAGIC_SYSRQ
21585+ default y
21586+ help
21587+ Automatic configuration for internal use.
21588+ When aufs supports Magic SysRq, enabled automatically.
21589+endif
7f207e10
AM
21590diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21591--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 21592+++ linux/fs/aufs/loop.c 2016-08-17 18:01:06.151889691 +0200
79b8bda9 21593@@ -0,0 +1,146 @@
1facf9fc 21594+/*
8cdd5066 21595+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 21596+ *
21597+ * This program, aufs is free software; you can redistribute it and/or modify
21598+ * it under the terms of the GNU General Public License as published by
21599+ * the Free Software Foundation; either version 2 of the License, or
21600+ * (at your option) any later version.
dece6358
AM
21601+ *
21602+ * This program is distributed in the hope that it will be useful,
21603+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21604+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21605+ * GNU General Public License for more details.
21606+ *
21607+ * You should have received a copy of the GNU General Public License
523b37e3 21608+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21609+ */
21610+
21611+/*
21612+ * support for loopback block device as a branch
21613+ */
21614+
1facf9fc 21615+#include "aufs.h"
21616+
392086de
AM
21617+/* added into drivers/block/loop.c */
21618+static struct file *(*backing_file_func)(struct super_block *sb);
21619+
1facf9fc 21620+/*
21621+ * test if two lower dentries have overlapping branches.
21622+ */
b752ccd1 21623+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 21624+{
b752ccd1 21625+ struct super_block *h_sb;
392086de
AM
21626+ struct file *backing_file;
21627+
21628+ if (unlikely(!backing_file_func)) {
21629+ /* don't load "loop" module here */
21630+ backing_file_func = symbol_get(loop_backing_file);
21631+ if (unlikely(!backing_file_func))
21632+ /* "loop" module is not loaded */
21633+ return 0;
21634+ }
1facf9fc 21635+
b752ccd1 21636+ h_sb = h_adding->d_sb;
392086de
AM
21637+ backing_file = backing_file_func(h_sb);
21638+ if (!backing_file)
1facf9fc 21639+ return 0;
21640+
2000de60 21641+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
21642+ /*
21643+ * h_adding can be local NFS.
21644+ * in this case aufs cannot detect the loop.
21645+ */
21646+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 21647+ return 1;
b752ccd1 21648+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 21649+}
21650+
21651+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21652+int au_test_loopback_kthread(void)
21653+{
b752ccd1
AM
21654+ int ret;
21655+ struct task_struct *tsk = current;
a2a7ad62 21656+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
21657+
21658+ ret = 0;
21659+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
21660+ get_task_comm(comm, tsk);
21661+ c = comm[4];
b752ccd1 21662+ ret = ('0' <= c && c <= '9'
a2a7ad62 21663+ && !strncmp(comm, "loop", 4));
b752ccd1 21664+ }
1facf9fc 21665+
b752ccd1 21666+ return ret;
1facf9fc 21667+}
87a755f4
AM
21668+
21669+/* ---------------------------------------------------------------------- */
21670+
21671+#define au_warn_loopback_step 16
21672+static int au_warn_loopback_nelem = au_warn_loopback_step;
21673+static unsigned long *au_warn_loopback_array;
21674+
21675+void au_warn_loopback(struct super_block *h_sb)
21676+{
21677+ int i, new_nelem;
21678+ unsigned long *a, magic;
21679+ static DEFINE_SPINLOCK(spin);
21680+
21681+ magic = h_sb->s_magic;
21682+ spin_lock(&spin);
21683+ a = au_warn_loopback_array;
21684+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21685+ if (a[i] == magic) {
21686+ spin_unlock(&spin);
21687+ return;
21688+ }
21689+
21690+ /* h_sb is new to us, print it */
21691+ if (i < au_warn_loopback_nelem) {
21692+ a[i] = magic;
21693+ goto pr;
21694+ }
21695+
21696+ /* expand the array */
21697+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21698+ a = au_kzrealloc(au_warn_loopback_array,
21699+ au_warn_loopback_nelem * sizeof(unsigned long),
21700+ new_nelem * sizeof(unsigned long), GFP_ATOMIC);
21701+ if (a) {
21702+ au_warn_loopback_nelem = new_nelem;
21703+ au_warn_loopback_array = a;
21704+ a[i] = magic;
21705+ goto pr;
21706+ }
21707+
21708+ spin_unlock(&spin);
21709+ AuWarn1("realloc failed, ignored\n");
21710+ return;
21711+
21712+pr:
21713+ spin_unlock(&spin);
0c3ec466
AM
21714+ pr_warn("you may want to try another patch for loopback file "
21715+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
21716+}
21717+
21718+int au_loopback_init(void)
21719+{
21720+ int err;
21721+ struct super_block *sb __maybe_unused;
21722+
79b8bda9 21723+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
21724+
21725+ err = 0;
21726+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21727+ sizeof(unsigned long), GFP_NOFS);
21728+ if (unlikely(!au_warn_loopback_array))
21729+ err = -ENOMEM;
21730+
21731+ return err;
21732+}
21733+
21734+void au_loopback_fin(void)
21735+{
79b8bda9
AM
21736+ if (backing_file_func)
21737+ symbol_put(loop_backing_file);
f0c0a007 21738+ au_delayed_kfree(au_warn_loopback_array);
87a755f4 21739+}
7f207e10
AM
21740diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21741--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21742+++ linux/fs/aufs/loop.h 2016-07-25 19:05:34.814493242 +0200
523b37e3 21743@@ -0,0 +1,52 @@
1facf9fc 21744+/*
8cdd5066 21745+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 21746+ *
21747+ * This program, aufs is free software; you can redistribute it and/or modify
21748+ * it under the terms of the GNU General Public License as published by
21749+ * the Free Software Foundation; either version 2 of the License, or
21750+ * (at your option) any later version.
dece6358
AM
21751+ *
21752+ * This program is distributed in the hope that it will be useful,
21753+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21754+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21755+ * GNU General Public License for more details.
21756+ *
21757+ * You should have received a copy of the GNU General Public License
523b37e3 21758+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21759+ */
21760+
21761+/*
21762+ * support for loopback mount as a branch
21763+ */
21764+
21765+#ifndef __AUFS_LOOP_H__
21766+#define __AUFS_LOOP_H__
21767+
21768+#ifdef __KERNEL__
21769+
dece6358
AM
21770+struct dentry;
21771+struct super_block;
1facf9fc 21772+
21773+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
21774+/* drivers/block/loop.c */
21775+struct file *loop_backing_file(struct super_block *sb);
21776+
1facf9fc 21777+/* loop.c */
b752ccd1 21778+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 21779+int au_test_loopback_kthread(void);
87a755f4
AM
21780+void au_warn_loopback(struct super_block *h_sb);
21781+
21782+int au_loopback_init(void);
21783+void au_loopback_fin(void);
1facf9fc 21784+#else
4a4d8108 21785+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 21786+ struct dentry *h_adding)
4a4d8108 21787+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
21788+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21789+
21790+AuStubInt0(au_loopback_init, void)
21791+AuStubVoid(au_loopback_fin, void)
1facf9fc 21792+#endif /* BLK_DEV_LOOP */
21793+
21794+#endif /* __KERNEL__ */
21795+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
21796diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21797--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21798+++ linux/fs/aufs/magic.mk 2015-09-24 10:47:58.254719746 +0200
7e9cd9fe 21799@@ -0,0 +1,30 @@
1facf9fc 21800+
21801+# defined in ${srctree}/fs/fuse/inode.c
21802+# tristate
21803+ifdef CONFIG_FUSE_FS
21804+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21805+endif
21806+
1facf9fc 21807+# defined in ${srctree}/fs/xfs/xfs_sb.h
21808+# tristate
21809+ifdef CONFIG_XFS_FS
21810+ccflags-y += -DXFS_SB_MAGIC=0x58465342
21811+endif
21812+
21813+# defined in ${srctree}/fs/configfs/mount.c
21814+# tristate
21815+ifdef CONFIG_CONFIGFS_FS
21816+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21817+endif
21818+
1facf9fc 21819+# defined in ${srctree}/fs/ubifs/ubifs.h
21820+# tristate
21821+ifdef CONFIG_UBIFS_FS
21822+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21823+endif
4a4d8108
AM
21824+
21825+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21826+# tristate
21827+ifdef CONFIG_HFSPLUS_FS
21828+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21829+endif
7f207e10
AM
21830diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21831--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21832+++ linux/fs/aufs/Makefile 2015-09-24 10:47:58.248052907 +0200
c1595e42 21833@@ -0,0 +1,44 @@
4a4d8108
AM
21834+
21835+include ${src}/magic.mk
21836+ifeq (${CONFIG_AUFS_FS},m)
21837+include ${src}/conf.mk
21838+endif
21839+-include ${src}/priv_def.mk
21840+
21841+# cf. include/linux/kernel.h
21842+# enable pr_debug
21843+ccflags-y += -DDEBUG
f6c5ef8b
AM
21844+# sparse requires the full pathname
21845+ifdef M
523b37e3 21846+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 21847+else
523b37e3 21848+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 21849+endif
4a4d8108
AM
21850+
21851+obj-$(CONFIG_AUFS_FS) += aufs.o
21852+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21853+ wkq.o vfsub.o dcsub.o \
e49829fe 21854+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
21855+ dinfo.o dentry.o \
21856+ dynop.o \
21857+ finfo.o file.o f_op.o \
21858+ dir.o vdir.o \
21859+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 21860+ mvdown.o ioctl.o
4a4d8108
AM
21861+
21862+# all are boolean
e49829fe 21863+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
21864+aufs-$(CONFIG_SYSFS) += sysfs.o
21865+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21866+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21867+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21868+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 21869+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
21870+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21871+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
076b876e 21872+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
21873+aufs-$(CONFIG_AUFS_POLL) += poll.o
21874+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
21875+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21876+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21877+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
21878diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21879--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
21880+++ linux/fs/aufs/module.c 2016-08-17 18:01:06.151889691 +0200
21881@@ -0,0 +1,289 @@
1facf9fc 21882+/*
8cdd5066 21883+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 21884+ *
21885+ * This program, aufs is free software; you can redistribute it and/or modify
21886+ * it under the terms of the GNU General Public License as published by
21887+ * the Free Software Foundation; either version 2 of the License, or
21888+ * (at your option) any later version.
dece6358
AM
21889+ *
21890+ * This program is distributed in the hope that it will be useful,
21891+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21892+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21893+ * GNU General Public License for more details.
21894+ *
21895+ * You should have received a copy of the GNU General Public License
523b37e3 21896+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21897+ */
21898+
21899+/*
21900+ * module global variables and operations
21901+ */
21902+
21903+#include <linux/module.h>
21904+#include <linux/seq_file.h>
21905+#include "aufs.h"
21906+
21907+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21908+{
21909+ if (new_sz <= nused)
21910+ return p;
21911+
21912+ p = krealloc(p, new_sz, gfp);
21913+ if (p)
21914+ memset(p + nused, 0, new_sz - nused);
21915+ return p;
21916+}
21917+
21918+/* ---------------------------------------------------------------------- */
1facf9fc 21919+/*
21920+ * aufs caches
21921+ */
f0c0a007
AM
21922+
21923+struct au_dfree au_dfree;
21924+
21925+/* delayed free */
21926+static void au_do_dfree(struct work_struct *work __maybe_unused)
21927+{
21928+ struct llist_head *head;
21929+ struct llist_node *node, *next;
21930+
21931+#define AU_CACHE_DFREE_DO_BODY(name, idx, lnode) do { \
21932+ head = &au_dfree.cache[AuCache_##idx].llist; \
21933+ node = llist_del_all(head); \
21934+ for (; node; node = next) { \
21935+ struct au_##name *p = \
21936+ p = llist_entry(node, struct au_##name, \
21937+ lnode); \
21938+ next = llist_next(node); \
21939+ au_cache_free_##name(p); \
21940+ } \
21941+ } while (0)
21942+
21943+ AU_CACHE_DFREE_DO_BODY(dinfo, DINFO, di_lnode);
21944+ AU_CACHE_DFREE_DO_BODY(icntnr, ICNTNR, lnode);
21945+ AU_CACHE_DFREE_DO_BODY(finfo, FINFO, fi_lnode);
21946+ AU_CACHE_DFREE_DO_BODY(vdir, VDIR, vd_lnode);
21947+ AU_CACHE_DFREE_DO_BODY(vdir_dehstr, DEHSTR, lnode);
21948+#ifdef CONFIG_AUFS_HNOTIFY
21949+ AU_CACHE_DFREE_DO_BODY(hnotify, HNOTIFY, hn_lnode);
21950+#endif
21951+
21952+#define AU_DFREE_DO_BODY(llist, func) do { \
21953+ node = llist_del_all(llist); \
21954+ for (; node; node = next) { \
21955+ next = llist_next(node); \
21956+ func(node); \
21957+ } \
21958+ } while (0)
21959+
21960+ AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_KFREE, kfree);
21961+ AU_DFREE_DO_BODY(au_dfree.llist + AU_DFREE_FREE_PAGE, au_free_page);
21962+
21963+#undef AU_CACHE_DFREE_DO_BODY
21964+#undef AU_DFREE_DO_BODY
21965+}
21966+
21967+AU_CACHE_DFREE_FUNC(dinfo, DINFO, di_lnode);
21968+AU_CACHE_DFREE_FUNC(icntnr, ICNTNR, lnode);
21969+AU_CACHE_DFREE_FUNC(finfo, FINFO, fi_lnode);
21970+AU_CACHE_DFREE_FUNC(vdir, VDIR, vd_lnode);
21971+AU_CACHE_DFREE_FUNC(vdir_dehstr, DEHSTR, lnode);
5afbbe0d
AM
21972+
21973+static void au_cache_fin(void)
21974+{
21975+ int i;
f0c0a007 21976+ struct au_cache *cp;
5afbbe0d
AM
21977+
21978+ /*
21979+ * Make sure all delayed rcu free inodes are flushed before we
21980+ * destroy cache.
21981+ */
21982+ rcu_barrier();
21983+
21984+ /* excluding AuCache_HNOTIFY */
21985+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
f0c0a007 21986+ flush_delayed_work(&au_dfree.dwork);
5afbbe0d 21987+ for (i = 0; i < AuCache_HNOTIFY; i++) {
f0c0a007
AM
21988+ cp = au_dfree.cache + i;
21989+ AuDebugOn(!llist_empty(&cp->llist));
21990+ kmem_cache_destroy(cp->cache);
21991+ cp->cache = NULL;
5afbbe0d
AM
21992+ }
21993+}
21994+
1facf9fc 21995+static int __init au_cache_init(void)
21996+{
f0c0a007
AM
21997+ struct au_cache *cp;
21998+
21999+ cp = au_dfree.cache;
22000+ cp[AuCache_DINFO].cache = AuCacheCtor(au_dinfo, au_di_init_once);
22001+ if (cp[AuCache_DINFO].cache)
027c5e7a 22002+ /* SLAB_DESTROY_BY_RCU */
f0c0a007
AM
22003+ cp[AuCache_ICNTNR].cache = AuCacheCtor(au_icntnr,
22004+ au_icntnr_init_once);
22005+ if (cp[AuCache_ICNTNR].cache)
22006+ cp[AuCache_FINFO].cache = AuCacheCtor(au_finfo,
22007+ au_fi_init_once);
22008+ if (cp[AuCache_FINFO].cache)
22009+ cp[AuCache_VDIR].cache = AuCache(au_vdir);
22010+ if (cp[AuCache_VDIR].cache)
22011+ cp[AuCache_DEHSTR].cache = AuCache(au_vdir_dehstr);
22012+ if (cp[AuCache_DEHSTR].cache)
1facf9fc 22013+ return 0;
22014+
5afbbe0d 22015+ au_cache_fin();
1facf9fc 22016+ return -ENOMEM;
22017+}
22018+
1facf9fc 22019+/* ---------------------------------------------------------------------- */
22020+
22021+int au_dir_roflags;
22022+
e49829fe 22023+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
22024+/*
22025+ * iterate_supers_type() doesn't protect us from
22026+ * remounting (branch management)
22027+ */
5afbbe0d 22028+struct au_sphlhead au_sbilist;
e49829fe
JR
22029+#endif
22030+
1facf9fc 22031+/*
22032+ * functions for module interface.
22033+ */
22034+MODULE_LICENSE("GPL");
22035+/* MODULE_LICENSE("GPL v2"); */
dece6358 22036+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 22037+MODULE_DESCRIPTION(AUFS_NAME
22038+ " -- Advanced multi layered unification filesystem");
22039+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 22040+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 22041+
1facf9fc 22042+/* this module parameter has no meaning when SYSFS is disabled */
22043+int sysaufs_brs = 1;
22044+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
22045+module_param_named(brs, sysaufs_brs, int, S_IRUGO);
22046+
076b876e 22047+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 22048+bool au_userns;
076b876e
AM
22049+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
22050+module_param_named(allow_userns, au_userns, bool, S_IRUGO);
22051+
1facf9fc 22052+/* ---------------------------------------------------------------------- */
22053+
22054+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
22055+
22056+int au_seq_path(struct seq_file *seq, struct path *path)
22057+{
79b8bda9
AM
22058+ int err;
22059+
22060+ err = seq_path(seq, path, au_esc_chars);
22061+ if (err > 0)
22062+ err = 0;
22063+ else if (err < 0)
22064+ err = -ENOMEM;
22065+
22066+ return err;
1facf9fc 22067+}
22068+
22069+/* ---------------------------------------------------------------------- */
22070+
22071+static int __init aufs_init(void)
22072+{
22073+ int err, i;
22074+ char *p;
f0c0a007 22075+ struct au_cache *cp;
1facf9fc 22076+
22077+ p = au_esc_chars;
22078+ for (i = 1; i <= ' '; i++)
22079+ *p++ = i;
22080+ *p++ = '\\';
22081+ *p++ = '\x7f';
22082+ *p = 0;
22083+
22084+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
22085+
b95c5147
AM
22086+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
22087+ for (i = 0; i < AuIop_Last; i++)
22088+ aufs_iop_nogetattr[i].getattr = NULL;
22089+
f0c0a007
AM
22090+ /* First, initialize au_dfree */
22091+ for (i = 0; i < AuCache_Last; i++) { /* including hnotify */
22092+ cp = au_dfree.cache + i;
22093+ cp->cache = NULL;
22094+ init_llist_head(&cp->llist);
22095+ }
22096+ for (i = 0; i < AU_DFREE_Last; i++)
22097+ init_llist_head(au_dfree.llist + i);
22098+ INIT_DELAYED_WORK(&au_dfree.dwork, au_do_dfree);
22099+
e49829fe 22100+ au_sbilist_init();
1facf9fc 22101+ sysaufs_brs_init();
22102+ au_debug_init();
4a4d8108 22103+ au_dy_init();
1facf9fc 22104+ err = sysaufs_init();
22105+ if (unlikely(err))
22106+ goto out;
e49829fe 22107+ err = au_procfs_init();
4f0767ce 22108+ if (unlikely(err))
953406b4 22109+ goto out_sysaufs;
e49829fe
JR
22110+ err = au_wkq_init();
22111+ if (unlikely(err))
22112+ goto out_procfs;
87a755f4 22113+ err = au_loopback_init();
1facf9fc 22114+ if (unlikely(err))
22115+ goto out_wkq;
87a755f4
AM
22116+ err = au_hnotify_init();
22117+ if (unlikely(err))
22118+ goto out_loopback;
1facf9fc 22119+ err = au_sysrq_init();
22120+ if (unlikely(err))
22121+ goto out_hin;
22122+ err = au_cache_init();
22123+ if (unlikely(err))
22124+ goto out_sysrq;
076b876e
AM
22125+
22126+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 22127+ err = register_filesystem(&aufs_fs_type);
22128+ if (unlikely(err))
22129+ goto out_cache;
076b876e 22130+
4a4d8108
AM
22131+ /* since we define pr_fmt, call printk directly */
22132+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 22133+ goto out; /* success */
22134+
4f0767ce 22135+out_cache:
1facf9fc 22136+ au_cache_fin();
4f0767ce 22137+out_sysrq:
1facf9fc 22138+ au_sysrq_fin();
4f0767ce 22139+out_hin:
4a4d8108 22140+ au_hnotify_fin();
87a755f4
AM
22141+out_loopback:
22142+ au_loopback_fin();
4f0767ce 22143+out_wkq:
1facf9fc 22144+ au_wkq_fin();
e49829fe
JR
22145+out_procfs:
22146+ au_procfs_fin();
4f0767ce 22147+out_sysaufs:
1facf9fc 22148+ sysaufs_fin();
4a4d8108 22149+ au_dy_fin();
f0c0a007 22150+ flush_delayed_work(&au_dfree.dwork);
4f0767ce 22151+out:
1facf9fc 22152+ return err;
22153+}
22154+
22155+static void __exit aufs_exit(void)
22156+{
22157+ unregister_filesystem(&aufs_fs_type);
22158+ au_cache_fin();
22159+ au_sysrq_fin();
4a4d8108 22160+ au_hnotify_fin();
87a755f4 22161+ au_loopback_fin();
1facf9fc 22162+ au_wkq_fin();
e49829fe 22163+ au_procfs_fin();
1facf9fc 22164+ sysaufs_fin();
4a4d8108 22165+ au_dy_fin();
f0c0a007 22166+ flush_delayed_work(&au_dfree.dwork);
1facf9fc 22167+}
22168+
22169+module_init(aufs_init);
22170+module_exit(aufs_exit);
7f207e10
AM
22171diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
22172--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
22173+++ linux/fs/aufs/module.h 2016-08-17 18:01:06.158556531 +0200
22174@@ -0,0 +1,144 @@
1facf9fc 22175+/*
8cdd5066 22176+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 22177+ *
22178+ * This program, aufs is free software; you can redistribute it and/or modify
22179+ * it under the terms of the GNU General Public License as published by
22180+ * the Free Software Foundation; either version 2 of the License, or
22181+ * (at your option) any later version.
dece6358
AM
22182+ *
22183+ * This program is distributed in the hope that it will be useful,
22184+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22185+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22186+ * GNU General Public License for more details.
22187+ *
22188+ * You should have received a copy of the GNU General Public License
523b37e3 22189+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22190+ */
22191+
22192+/*
22193+ * module initialization and module-global
22194+ */
22195+
22196+#ifndef __AUFS_MODULE_H__
22197+#define __AUFS_MODULE_H__
22198+
22199+#ifdef __KERNEL__
22200+
22201+#include <linux/slab.h>
f0c0a007 22202+#include "debug.h"
1facf9fc 22203+
dece6358
AM
22204+struct path;
22205+struct seq_file;
22206+
1facf9fc 22207+/* module parameters */
1facf9fc 22208+extern int sysaufs_brs;
8cdd5066 22209+extern bool au_userns;
1facf9fc 22210+
22211+/* ---------------------------------------------------------------------- */
22212+
22213+extern int au_dir_roflags;
22214+
22215+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
22216+int au_seq_path(struct seq_file *seq, struct path *path);
22217+
e49829fe
JR
22218+#ifdef CONFIG_PROC_FS
22219+/* procfs.c */
22220+int __init au_procfs_init(void);
22221+void au_procfs_fin(void);
22222+#else
22223+AuStubInt0(au_procfs_init, void);
22224+AuStubVoid(au_procfs_fin, void);
22225+#endif
22226+
4f0767ce
JR
22227+/* ---------------------------------------------------------------------- */
22228+
f0c0a007 22229+/* kmem cache and delayed free */
1facf9fc 22230+enum {
22231+ AuCache_DINFO,
22232+ AuCache_ICNTNR,
22233+ AuCache_FINFO,
22234+ AuCache_VDIR,
22235+ AuCache_DEHSTR,
7eafdf33 22236+ AuCache_HNOTIFY, /* must be last */
1facf9fc 22237+ AuCache_Last
22238+};
22239+
f0c0a007
AM
22240+enum {
22241+ AU_DFREE_KFREE,
22242+ AU_DFREE_FREE_PAGE,
22243+ AU_DFREE_Last
22244+};
22245+
22246+struct au_cache {
22247+ struct kmem_cache *cache;
22248+ struct llist_head llist; /* delayed free */
22249+};
22250+
22251+/*
22252+ * in order to reduce the cost of the internal timer, consolidate all the
22253+ * delayed free works into a single delayed_work.
22254+ */
22255+struct au_dfree {
22256+ struct au_cache cache[AuCache_Last];
22257+ struct llist_head llist[AU_DFREE_Last];
22258+ struct delayed_work dwork;
22259+};
22260+
22261+extern struct au_dfree au_dfree;
22262+
4a4d8108
AM
22263+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
22264+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
22265+#define AuCacheCtor(type, ctor) \
22266+ kmem_cache_create(#type, sizeof(struct type), \
22267+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 22268+
f0c0a007
AM
22269+#define AU_DFREE_DELAY msecs_to_jiffies(10)
22270+#define AU_DFREE_BODY(lnode, llist) do { \
22271+ if (llist_add(lnode, llist)) \
22272+ schedule_delayed_work(&au_dfree.dwork, \
22273+ AU_DFREE_DELAY); \
22274+ } while (0)
22275+#define AU_CACHE_DFREE_FUNC(name, idx, lnode) \
22276+ void au_cache_dfree_##name(struct au_##name *p) \
22277+ { \
22278+ struct au_cache *cp = au_dfree.cache + AuCache_##idx; \
22279+ AU_DFREE_BODY(&p->lnode, &cp->llist); \
22280+ }
1facf9fc 22281+
22282+#define AuCacheFuncs(name, index) \
4a4d8108 22283+static inline struct au_##name *au_cache_alloc_##name(void) \
f0c0a007 22284+{ return kmem_cache_alloc(au_dfree.cache[AuCache_##index].cache, GFP_NOFS); } \
4a4d8108 22285+static inline void au_cache_free_##name(struct au_##name *p) \
f0c0a007
AM
22286+{ kmem_cache_free(au_dfree.cache[AuCache_##index].cache, p); } \
22287+void au_cache_dfree_##name(struct au_##name *p)
1facf9fc 22288+
22289+AuCacheFuncs(dinfo, DINFO);
22290+AuCacheFuncs(icntnr, ICNTNR);
22291+AuCacheFuncs(finfo, FINFO);
22292+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
22293+AuCacheFuncs(vdir_dehstr, DEHSTR);
22294+#ifdef CONFIG_AUFS_HNOTIFY
22295+AuCacheFuncs(hnotify, HNOTIFY);
22296+#endif
1facf9fc 22297+
f0c0a007
AM
22298+static inline void au_delayed_kfree(const void *p)
22299+{
22300+ AuDebugOn(!p);
22301+ AuDebugOn(ksize(p) < sizeof(struct llist_node));
22302+
22303+ AU_DFREE_BODY((void *)p, au_dfree.llist + AU_DFREE_KFREE);
22304+}
22305+
22306+/* cast only */
22307+static inline void au_free_page(void *p)
22308+{
22309+ free_page((unsigned long)p);
22310+}
22311+
22312+static inline void au_delayed_free_page(unsigned long addr)
22313+{
22314+ AU_DFREE_BODY((void *)addr, au_dfree.llist + AU_DFREE_FREE_PAGE);
22315+}
22316+
4a4d8108
AM
22317+#endif /* __KERNEL__ */
22318+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
22319diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
22320--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 22321+++ linux/fs/aufs/mvdown.c 2016-08-17 18:01:06.158556531 +0200
5afbbe0d 22322@@ -0,0 +1,704 @@
c2b27bf2 22323+/*
8cdd5066 22324+ * Copyright (C) 2011-2016 Junjiro R. Okajima
c2b27bf2
AM
22325+ *
22326+ * This program, aufs is free software; you can redistribute it and/or modify
22327+ * it under the terms of the GNU General Public License as published by
22328+ * the Free Software Foundation; either version 2 of the License, or
22329+ * (at your option) any later version.
22330+ *
22331+ * This program is distributed in the hope that it will be useful,
22332+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22333+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22334+ * GNU General Public License for more details.
22335+ *
22336+ * You should have received a copy of the GNU General Public License
523b37e3
AM
22337+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22338+ */
22339+
22340+/*
22341+ * move-down, opposite of copy-up
c2b27bf2
AM
22342+ */
22343+
22344+#include "aufs.h"
22345+
c2b27bf2
AM
22346+struct au_mvd_args {
22347+ struct {
c2b27bf2
AM
22348+ struct super_block *h_sb;
22349+ struct dentry *h_parent;
22350+ struct au_hinode *hdir;
392086de 22351+ struct inode *h_dir, *h_inode;
c1595e42 22352+ struct au_pin pin;
c2b27bf2
AM
22353+ } info[AUFS_MVDOWN_NARRAY];
22354+
22355+ struct aufs_mvdown mvdown;
22356+ struct dentry *dentry, *parent;
22357+ struct inode *inode, *dir;
22358+ struct super_block *sb;
22359+ aufs_bindex_t bopq, bwh, bfound;
22360+ unsigned char rename_lock;
c2b27bf2
AM
22361+};
22362+
392086de 22363+#define mvd_errno mvdown.au_errno
076b876e
AM
22364+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
22365+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
22366+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
22367+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 22368+
392086de
AM
22369+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
22370+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
22371+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
22372+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
22373+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 22374+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
22375+
22376+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
22377+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
22378+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
22379+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
22380+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 22381+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
22382+
22383+#define AU_MVD_PR(flag, ...) do { \
22384+ if (flag) \
22385+ pr_err(__VA_ARGS__); \
22386+ } while (0)
22387+
076b876e
AM
22388+static int find_lower_writable(struct au_mvd_args *a)
22389+{
22390+ struct super_block *sb;
5afbbe0d 22391+ aufs_bindex_t bindex, bbot;
076b876e
AM
22392+ struct au_branch *br;
22393+
22394+ sb = a->sb;
22395+ bindex = a->mvd_bsrc;
5afbbe0d 22396+ bbot = au_sbbot(sb);
076b876e 22397+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 22398+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
22399+ br = au_sbr(sb, bindex);
22400+ if (au_br_fhsm(br->br_perm)
22401+ && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
22402+ return bindex;
22403+ }
22404+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 22405+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
22406+ br = au_sbr(sb, bindex);
22407+ if (!au_br_rdonly(br))
22408+ return bindex;
22409+ }
22410+ else
5afbbe0d 22411+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
22412+ br = au_sbr(sb, bindex);
22413+ if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
22414+ if (au_br_rdonly(br))
22415+ a->mvdown.flags
22416+ |= AUFS_MVDOWN_ROLOWER_R;
22417+ return bindex;
22418+ }
22419+ }
22420+
22421+ return -1;
22422+}
22423+
c2b27bf2 22424+/* make the parent dir on bdst */
392086de 22425+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22426+{
22427+ int err;
22428+
22429+ err = 0;
22430+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22431+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22432+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22433+ a->mvd_h_dst_parent = NULL;
5afbbe0d 22434+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
22435+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22436+ if (!a->mvd_h_dst_parent) {
22437+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22438+ if (unlikely(err)) {
392086de 22439+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
22440+ goto out;
22441+ }
22442+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22443+ }
22444+
22445+out:
22446+ AuTraceErr(err);
22447+ return err;
22448+}
22449+
22450+/* lock them all */
392086de 22451+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22452+{
22453+ int err;
22454+ struct dentry *h_trap;
22455+
22456+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22457+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
22458+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22459+ au_opt_udba(a->sb),
22460+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22461+ AuTraceErr(err);
22462+ if (unlikely(err)) {
22463+ AU_MVD_PR(dmsg, "pin_dst failed\n");
22464+ goto out;
22465+ }
22466+
c2b27bf2
AM
22467+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22468+ a->rename_lock = 0;
c1595e42
JR
22469+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22470+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22471+ au_opt_udba(a->sb),
22472+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22473+ err = au_do_pin(&a->mvd_pin_src);
22474+ AuTraceErr(err);
5527c038 22475+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
22476+ if (unlikely(err)) {
22477+ AU_MVD_PR(dmsg, "pin_src failed\n");
22478+ goto out_dst;
22479+ }
22480+ goto out; /* success */
c2b27bf2
AM
22481+ }
22482+
c2b27bf2 22483+ a->rename_lock = 1;
c1595e42
JR
22484+ au_pin_hdir_unlock(&a->mvd_pin_dst);
22485+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22486+ au_opt_udba(a->sb),
22487+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22488+ AuTraceErr(err);
5527c038 22489+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
22490+ if (unlikely(err)) {
22491+ AU_MVD_PR(dmsg, "pin_src failed\n");
22492+ au_pin_hdir_lock(&a->mvd_pin_dst);
22493+ goto out_dst;
22494+ }
22495+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
22496+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22497+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
22498+ if (h_trap) {
22499+ err = (h_trap != a->mvd_h_src_parent);
22500+ if (err)
22501+ err = (h_trap != a->mvd_h_dst_parent);
22502+ }
22503+ BUG_ON(err); /* it should never happen */
c1595e42
JR
22504+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22505+ err = -EBUSY;
22506+ AuTraceErr(err);
22507+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22508+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
22509+ au_pin_hdir_lock(&a->mvd_pin_src);
22510+ au_unpin(&a->mvd_pin_src);
22511+ au_pin_hdir_lock(&a->mvd_pin_dst);
22512+ goto out_dst;
22513+ }
22514+ goto out; /* success */
c2b27bf2 22515+
c1595e42
JR
22516+out_dst:
22517+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
22518+out:
22519+ AuTraceErr(err);
22520+ return err;
22521+}
22522+
392086de 22523+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 22524+{
c1595e42
JR
22525+ if (!a->rename_lock)
22526+ au_unpin(&a->mvd_pin_src);
22527+ else {
c2b27bf2
AM
22528+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22529+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
22530+ au_pin_hdir_lock(&a->mvd_pin_src);
22531+ au_unpin(&a->mvd_pin_src);
22532+ au_pin_hdir_lock(&a->mvd_pin_dst);
22533+ }
22534+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
22535+}
22536+
22537+/* copy-down the file */
392086de 22538+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22539+{
22540+ int err;
22541+ struct au_cp_generic cpg = {
22542+ .dentry = a->dentry,
22543+ .bdst = a->mvd_bdst,
22544+ .bsrc = a->mvd_bsrc,
22545+ .len = -1,
c1595e42 22546+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
22547+ .flags = AuCpup_DTIME | AuCpup_HOPEN
22548+ };
22549+
22550+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
22551+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22552+ au_fset_cpup(cpg.flags, OVERWRITE);
22553+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22554+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
22555+ err = au_sio_cpdown_simple(&cpg);
22556+ if (unlikely(err))
392086de 22557+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
22558+
22559+ AuTraceErr(err);
22560+ return err;
22561+}
22562+
22563+/*
22564+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
22565+ * were sleeping
22566+ */
392086de 22567+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22568+{
22569+ int err;
22570+ struct path h_path;
22571+ struct au_branch *br;
523b37e3 22572+ struct inode *delegated;
c2b27bf2
AM
22573+
22574+ br = au_sbr(a->sb, a->mvd_bdst);
22575+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22576+ err = PTR_ERR(h_path.dentry);
22577+ if (IS_ERR(h_path.dentry)) {
392086de 22578+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
22579+ goto out;
22580+ }
22581+
22582+ err = 0;
5527c038 22583+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 22584+ h_path.mnt = au_br_mnt(br);
523b37e3 22585+ delegated = NULL;
5527c038 22586+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
22587+ &delegated, /*force*/0);
22588+ if (unlikely(err == -EWOULDBLOCK)) {
22589+ pr_warn("cannot retry for NFSv4 delegation"
22590+ " for an internal unlink\n");
22591+ iput(delegated);
22592+ }
c2b27bf2 22593+ if (unlikely(err))
392086de 22594+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
22595+ }
22596+ dput(h_path.dentry);
22597+
22598+out:
22599+ AuTraceErr(err);
22600+ return err;
22601+}
22602+
22603+/*
22604+ * unlink the topmost h_dentry
c2b27bf2 22605+ */
392086de 22606+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22607+{
22608+ int err;
22609+ struct path h_path;
523b37e3 22610+ struct inode *delegated;
c2b27bf2
AM
22611+
22612+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22613+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
22614+ delegated = NULL;
22615+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22616+ if (unlikely(err == -EWOULDBLOCK)) {
22617+ pr_warn("cannot retry for NFSv4 delegation"
22618+ " for an internal unlink\n");
22619+ iput(delegated);
22620+ }
c2b27bf2 22621+ if (unlikely(err))
392086de 22622+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
22623+
22624+ AuTraceErr(err);
22625+ return err;
22626+}
22627+
076b876e
AM
22628+/* Since mvdown succeeded, we ignore an error of this function */
22629+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22630+{
22631+ int err;
22632+ struct au_branch *br;
22633+
22634+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22635+ br = au_sbr(a->sb, a->mvd_bsrc);
22636+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22637+ if (!err) {
22638+ br = au_sbr(a->sb, a->mvd_bdst);
22639+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22640+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22641+ }
22642+ if (!err)
22643+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22644+ else
22645+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22646+}
22647+
c2b27bf2
AM
22648+/*
22649+ * copy-down the file and unlink the bsrc file.
22650+ * - unlink the bdst whout if exist
22651+ * - copy-down the file (with whtmp name and rename)
22652+ * - unlink the bsrc file
22653+ */
392086de 22654+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22655+{
22656+ int err;
22657+
392086de 22658+ err = au_do_mkdir(dmsg, a);
c2b27bf2 22659+ if (!err)
392086de 22660+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
22661+ if (unlikely(err))
22662+ goto out;
22663+
22664+ /*
22665+ * do not revert the activities we made on bdst since they should be
22666+ * harmless in aufs.
22667+ */
22668+
392086de 22669+ err = au_do_cpdown(dmsg, a);
c2b27bf2 22670+ if (!err)
392086de
AM
22671+ err = au_do_unlink_wh(dmsg, a);
22672+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22673+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
22674+ if (unlikely(err))
22675+ goto out_unlock;
22676+
c1595e42
JR
22677+ AuDbg("%pd2, 0x%x, %d --> %d\n",
22678+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
22679+ if (find_lower_writable(a) < 0)
22680+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22681+
22682+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22683+ au_do_stfs(dmsg, a);
22684+
c2b27bf2 22685+ /* maintain internal array */
392086de
AM
22686+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22687+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 22688+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 22689+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 22690+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
22691+ } else {
22692+ /* hide the lower */
22693+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 22694+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 22695+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 22696+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 22697+ }
5afbbe0d
AM
22698+ if (au_dbbot(a->dentry) < a->mvd_bdst)
22699+ au_set_dbbot(a->dentry, a->mvd_bdst);
22700+ if (au_ibbot(a->inode) < a->mvd_bdst)
22701+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
22702+
22703+out_unlock:
392086de 22704+ au_do_unlock(dmsg, a);
c2b27bf2
AM
22705+out:
22706+ AuTraceErr(err);
22707+ return err;
22708+}
22709+
22710+/* ---------------------------------------------------------------------- */
22711+
c2b27bf2 22712+/* make sure the file is idle */
392086de 22713+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22714+{
22715+ int err, plinked;
c2b27bf2
AM
22716+
22717+ err = 0;
c2b27bf2 22718+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 22719+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 22720+ && au_dcount(a->dentry) == 1
c2b27bf2 22721+ && atomic_read(&a->inode->i_count) == 1
392086de 22722+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
22723+ && (!plinked || !au_plink_test(a->inode))
22724+ && a->inode->i_nlink == 1)
22725+ goto out;
22726+
22727+ err = -EBUSY;
392086de 22728+ AU_MVD_PR(dmsg,
c1595e42 22729+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 22730+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 22731+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 22732+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
22733+ plinked, plinked ? au_plink_test(a->inode) : 0);
22734+
22735+out:
22736+ AuTraceErr(err);
22737+ return err;
22738+}
22739+
22740+/* make sure the parent dir is fine */
392086de 22741+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
22742+ struct au_mvd_args *a)
22743+{
22744+ int err;
22745+ aufs_bindex_t bindex;
22746+
22747+ err = 0;
22748+ if (unlikely(au_alive_dir(a->parent))) {
22749+ err = -ENOENT;
392086de 22750+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
22751+ goto out;
22752+ }
22753+
22754+ a->bopq = au_dbdiropq(a->parent);
22755+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
22756+ AuDbg("b%d\n", bindex);
22757+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
22758+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
22759+ err = -EINVAL;
392086de
AM
22760+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
22761+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
22762+ a->bopq, a->mvd_bdst);
22763+ }
22764+
22765+out:
22766+ AuTraceErr(err);
22767+ return err;
22768+}
22769+
392086de 22770+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
22771+ struct au_mvd_args *a)
22772+{
22773+ int err;
22774+ struct au_dinfo *dinfo, *tmp;
22775+
22776+ /* lookup the next lower positive entry */
22777+ err = -ENOMEM;
22778+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
22779+ if (unlikely(!tmp))
22780+ goto out;
22781+
22782+ a->bfound = -1;
22783+ a->bwh = -1;
22784+ dinfo = au_di(a->dentry);
22785+ au_di_cp(tmp, dinfo);
22786+ au_di_swap(tmp, dinfo);
22787+
22788+ /* returns the number of positive dentries */
5afbbe0d
AM
22789+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
22790+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
22791+ if (!err)
22792+ a->bwh = au_dbwh(a->dentry);
22793+ else if (err > 0)
5afbbe0d 22794+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
22795+
22796+ au_di_swap(tmp, dinfo);
22797+ au_rw_write_unlock(&tmp->di_rwsem);
22798+ au_di_free(tmp);
22799+ if (unlikely(err < 0))
392086de 22800+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
22801+
22802+ /*
22803+ * here, we have these cases.
22804+ * bfound == -1
22805+ * no positive dentry under bsrc. there are more sub-cases.
22806+ * bwh < 0
22807+ * there no whiteout, we can safely move-down.
22808+ * bwh <= bsrc
22809+ * impossible
22810+ * bsrc < bwh && bwh < bdst
22811+ * there is a whiteout on RO branch. cannot proceed.
22812+ * bwh == bdst
22813+ * there is a whiteout on the RW target branch. it should
22814+ * be removed.
22815+ * bdst < bwh
22816+ * there is a whiteout somewhere unrelated branch.
22817+ * -1 < bfound && bfound <= bsrc
22818+ * impossible.
22819+ * bfound < bdst
22820+ * found, but it is on RO branch between bsrc and bdst. cannot
22821+ * proceed.
22822+ * bfound == bdst
22823+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
22824+ * error.
22825+ * bdst < bfound
22826+ * found, after we create the file on bdst, it will be hidden.
22827+ */
22828+
22829+ AuDebugOn(a->bfound == -1
22830+ && a->bwh != -1
22831+ && a->bwh <= a->mvd_bsrc);
22832+ AuDebugOn(-1 < a->bfound
22833+ && a->bfound <= a->mvd_bsrc);
22834+
22835+ err = -EINVAL;
22836+ if (a->bfound == -1
22837+ && a->mvd_bsrc < a->bwh
22838+ && a->bwh != -1
22839+ && a->bwh < a->mvd_bdst) {
392086de
AM
22840+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
22841+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
22842+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
22843+ goto out;
22844+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
22845+ a->mvd_errno = EAU_MVDOWN_UPPER;
22846+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
22847+ a->mvd_bdst, a->bfound);
22848+ goto out;
22849+ }
22850+
22851+ err = 0; /* success */
22852+
22853+out:
22854+ AuTraceErr(err);
22855+ return err;
22856+}
22857+
392086de 22858+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22859+{
22860+ int err;
22861+
392086de
AM
22862+ err = 0;
22863+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22864+ && a->bfound == a->mvd_bdst)
22865+ err = -EEXIST;
c2b27bf2
AM
22866+ AuTraceErr(err);
22867+ return err;
22868+}
22869+
392086de 22870+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22871+{
22872+ int err;
22873+ struct au_branch *br;
22874+
22875+ err = -EISDIR;
22876+ if (unlikely(S_ISDIR(a->inode->i_mode)))
22877+ goto out;
22878+
22879+ err = -EINVAL;
392086de 22880+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 22881+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
22882+ else {
22883+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
22884+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
22885+ || (a->mvd_bsrc < au_dbtop(a->dentry)
22886+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 22887+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
22888+ || (a->mvd_bsrc < au_ibtop(a->inode)
22889+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
22890+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
22891+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
22892+ AU_MVD_PR(dmsg, "no upper\n");
22893+ goto out;
22894+ }
22895+ }
5afbbe0d 22896+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
22897+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
22898+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
22899+ goto out;
22900+ }
392086de 22901+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
22902+ br = au_sbr(a->sb, a->mvd_bsrc);
22903+ err = au_br_rdonly(br);
392086de
AM
22904+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
22905+ if (unlikely(err))
22906+ goto out;
22907+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
22908+ || IS_APPEND(a->mvd_h_src_inode))) {
22909+ if (err)
22910+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22911+ /* go on */
22912+ } else
c2b27bf2
AM
22913+ goto out;
22914+
22915+ err = -EINVAL;
392086de
AM
22916+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22917+ a->mvd_bdst = find_lower_writable(a);
22918+ if (unlikely(a->mvd_bdst < 0)) {
22919+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
22920+ AU_MVD_PR(dmsg, "no writable lower branch\n");
22921+ goto out;
22922+ }
22923+ } else {
22924+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22925+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 22926+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
22927+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22928+ AU_MVD_PR(dmsg, "no lower brid\n");
22929+ goto out;
22930+ }
c2b27bf2
AM
22931+ }
22932+
392086de 22933+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 22934+ if (!err)
392086de 22935+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 22936+ if (!err)
392086de 22937+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 22938+ if (!err)
392086de 22939+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
22940+ if (!err)
22941+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22942+
22943+out:
22944+ AuTraceErr(err);
22945+ return err;
22946+}
22947+
22948+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22949+{
392086de
AM
22950+ int err, e;
22951+ unsigned char dmsg;
22952+ struct au_mvd_args *args;
79b8bda9 22953+ struct inode *inode;
c2b27bf2 22954+
79b8bda9 22955+ inode = d_inode(dentry);
c2b27bf2
AM
22956+ err = -EPERM;
22957+ if (unlikely(!capable(CAP_SYS_ADMIN)))
22958+ goto out;
22959+
392086de
AM
22960+ err = -ENOMEM;
22961+ args = kmalloc(sizeof(*args), GFP_NOFS);
22962+ if (unlikely(!args))
22963+ goto out;
22964+
22965+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22966+ if (!err)
22967+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
22968+ if (unlikely(err)) {
22969+ err = -EFAULT;
392086de
AM
22970+ AuTraceErr(err);
22971+ goto out_free;
c2b27bf2 22972+ }
392086de
AM
22973+ AuDbg("flags 0x%x\n", args->mvdown.flags);
22974+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22975+ args->mvdown.au_errno = 0;
22976+ args->dentry = dentry;
79b8bda9 22977+ args->inode = inode;
392086de 22978+ args->sb = dentry->d_sb;
c2b27bf2 22979+
392086de
AM
22980+ err = -ENOENT;
22981+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22982+ args->parent = dget_parent(dentry);
5527c038 22983+ args->dir = d_inode(args->parent);
febd17d6 22984+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
22985+ dput(args->parent);
22986+ if (unlikely(args->parent != dentry->d_parent)) {
22987+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
22988+ goto out_dir;
22989+ }
22990+
febd17d6 22991+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 22992+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
22993+ if (unlikely(err))
22994+ goto out_inode;
22995+
392086de
AM
22996+ di_write_lock_parent(args->parent);
22997+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
22998+ if (unlikely(err))
22999+ goto out_parent;
23000+
392086de 23001+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
23002+ if (unlikely(err))
23003+ goto out_parent;
c2b27bf2 23004+
392086de 23005+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
23006+ au_cpup_attr_timesizes(inode);
23007+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
23008+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
23009+ /* au_digen_dec(dentry); */
23010+
23011+out_parent:
392086de 23012+ di_write_unlock(args->parent);
c2b27bf2
AM
23013+ aufs_read_unlock(dentry, AuLock_DW);
23014+out_inode:
febd17d6 23015+ inode_unlock(inode);
c2b27bf2 23016+out_dir:
febd17d6 23017+ inode_unlock(args->dir);
392086de
AM
23018+out_free:
23019+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
23020+ if (unlikely(e))
23021+ err = -EFAULT;
f0c0a007 23022+ au_delayed_kfree(args);
c2b27bf2
AM
23023+out:
23024+ AuTraceErr(err);
23025+ return err;
23026+}
23027diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
23028--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
23029+++ linux/fs/aufs/opts.c 2016-08-17 18:01:06.161889951 +0200
23030@@ -0,0 +1,1860 @@
1facf9fc 23031+/*
8cdd5066 23032+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 23033+ *
23034+ * This program, aufs is free software; you can redistribute it and/or modify
23035+ * it under the terms of the GNU General Public License as published by
23036+ * the Free Software Foundation; either version 2 of the License, or
23037+ * (at your option) any later version.
dece6358
AM
23038+ *
23039+ * This program is distributed in the hope that it will be useful,
23040+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
23041+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23042+ * GNU General Public License for more details.
23043+ *
23044+ * You should have received a copy of the GNU General Public License
523b37e3 23045+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 23046+ */
23047+
23048+/*
23049+ * mount options/flags
23050+ */
23051+
dece6358 23052+#include <linux/namei.h>
1facf9fc 23053+#include <linux/types.h> /* a distribution requires */
23054+#include <linux/parser.h>
23055+#include "aufs.h"
23056+
23057+/* ---------------------------------------------------------------------- */
23058+
23059+enum {
23060+ Opt_br,
7e9cd9fe
AM
23061+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
23062+ Opt_idel, Opt_imod,
23063+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 23064+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 23065+ Opt_xino, Opt_noxino,
1facf9fc 23066+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
23067+ Opt_trunc_xino_path, Opt_itrunc_xino,
23068+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 23069+ Opt_shwh, Opt_noshwh,
1facf9fc 23070+ Opt_plink, Opt_noplink, Opt_list_plink,
23071+ Opt_udba,
4a4d8108 23072+ Opt_dio, Opt_nodio,
1facf9fc 23073+ Opt_diropq_a, Opt_diropq_w,
23074+ Opt_warn_perm, Opt_nowarn_perm,
23075+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 23076+ Opt_fhsm_sec,
1facf9fc 23077+ Opt_verbose, Opt_noverbose,
23078+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 23079+ Opt_dirperm1, Opt_nodirperm1,
c1595e42 23080+ Opt_acl, Opt_noacl,
1facf9fc 23081+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
23082+};
23083+
23084+static match_table_t options = {
23085+ {Opt_br, "br=%s"},
23086+ {Opt_br, "br:%s"},
23087+
23088+ {Opt_add, "add=%d:%s"},
23089+ {Opt_add, "add:%d:%s"},
23090+ {Opt_add, "ins=%d:%s"},
23091+ {Opt_add, "ins:%d:%s"},
23092+ {Opt_append, "append=%s"},
23093+ {Opt_append, "append:%s"},
23094+ {Opt_prepend, "prepend=%s"},
23095+ {Opt_prepend, "prepend:%s"},
23096+
23097+ {Opt_del, "del=%s"},
23098+ {Opt_del, "del:%s"},
23099+ /* {Opt_idel, "idel:%d"}, */
23100+ {Opt_mod, "mod=%s"},
23101+ {Opt_mod, "mod:%s"},
23102+ /* {Opt_imod, "imod:%d:%s"}, */
23103+
23104+ {Opt_dirwh, "dirwh=%d"},
23105+
23106+ {Opt_xino, "xino=%s"},
23107+ {Opt_noxino, "noxino"},
23108+ {Opt_trunc_xino, "trunc_xino"},
23109+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
23110+ {Opt_notrunc_xino, "notrunc_xino"},
23111+ {Opt_trunc_xino_path, "trunc_xino=%s"},
23112+ {Opt_itrunc_xino, "itrunc_xino=%d"},
23113+ /* {Opt_zxino, "zxino=%s"}, */
23114+ {Opt_trunc_xib, "trunc_xib"},
23115+ {Opt_notrunc_xib, "notrunc_xib"},
23116+
e49829fe 23117+#ifdef CONFIG_PROC_FS
1facf9fc 23118+ {Opt_plink, "plink"},
e49829fe
JR
23119+#else
23120+ {Opt_ignore_silent, "plink"},
23121+#endif
23122+
1facf9fc 23123+ {Opt_noplink, "noplink"},
e49829fe 23124+
1facf9fc 23125+#ifdef CONFIG_AUFS_DEBUG
23126+ {Opt_list_plink, "list_plink"},
23127+#endif
23128+
23129+ {Opt_udba, "udba=%s"},
23130+
4a4d8108
AM
23131+ {Opt_dio, "dio"},
23132+ {Opt_nodio, "nodio"},
23133+
076b876e
AM
23134+#ifdef CONFIG_AUFS_FHSM
23135+ {Opt_fhsm_sec, "fhsm_sec=%d"},
23136+#else
23137+ {Opt_ignore_silent, "fhsm_sec=%d"},
23138+#endif
23139+
1facf9fc 23140+ {Opt_diropq_a, "diropq=always"},
23141+ {Opt_diropq_a, "diropq=a"},
23142+ {Opt_diropq_w, "diropq=whiteouted"},
23143+ {Opt_diropq_w, "diropq=w"},
23144+
23145+ {Opt_warn_perm, "warn_perm"},
23146+ {Opt_nowarn_perm, "nowarn_perm"},
23147+
23148+ /* keep them temporary */
1facf9fc 23149+ {Opt_ignore_silent, "nodlgt"},
1facf9fc 23150+ {Opt_ignore_silent, "clean_plink"},
23151+
dece6358
AM
23152+#ifdef CONFIG_AUFS_SHWH
23153+ {Opt_shwh, "shwh"},
23154+#endif
23155+ {Opt_noshwh, "noshwh"},
23156+
076b876e
AM
23157+ {Opt_dirperm1, "dirperm1"},
23158+ {Opt_nodirperm1, "nodirperm1"},
23159+
1facf9fc 23160+ {Opt_verbose, "verbose"},
23161+ {Opt_verbose, "v"},
23162+ {Opt_noverbose, "noverbose"},
23163+ {Opt_noverbose, "quiet"},
23164+ {Opt_noverbose, "q"},
23165+ {Opt_noverbose, "silent"},
23166+
23167+ {Opt_sum, "sum"},
23168+ {Opt_nosum, "nosum"},
23169+ {Opt_wsum, "wsum"},
23170+
23171+ {Opt_rdcache, "rdcache=%d"},
23172+ {Opt_rdblk, "rdblk=%d"},
dece6358 23173+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 23174+ {Opt_rdhash, "rdhash=%d"},
dece6358 23175+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 23176+
23177+ {Opt_wbr_create, "create=%s"},
23178+ {Opt_wbr_create, "create_policy=%s"},
23179+ {Opt_wbr_copyup, "cpup=%s"},
23180+ {Opt_wbr_copyup, "copyup=%s"},
23181+ {Opt_wbr_copyup, "copyup_policy=%s"},
23182+
c1595e42
JR
23183+ /* generic VFS flag */
23184+#ifdef CONFIG_FS_POSIX_ACL
23185+ {Opt_acl, "acl"},
23186+ {Opt_noacl, "noacl"},
23187+#else
23188+ {Opt_ignore_silent, "acl"},
23189+ {Opt_ignore_silent, "noacl"},
23190+#endif
23191+
1facf9fc 23192+ /* internal use for the scripts */
23193+ {Opt_ignore_silent, "si=%s"},
23194+
23195+ {Opt_br, "dirs=%s"},
23196+ {Opt_ignore, "debug=%d"},
23197+ {Opt_ignore, "delete=whiteout"},
23198+ {Opt_ignore, "delete=all"},
23199+ {Opt_ignore, "imap=%s"},
23200+
1308ab2a 23201+ /* temporary workaround, due to old mount(8)? */
23202+ {Opt_ignore_silent, "relatime"},
23203+
1facf9fc 23204+ {Opt_err, NULL}
23205+};
23206+
23207+/* ---------------------------------------------------------------------- */
23208+
076b876e 23209+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 23210+{
076b876e
AM
23211+ struct match_token *p;
23212+
23213+ p = tbl;
23214+ while (p->pattern) {
23215+ if (p->token == val)
23216+ return p->pattern;
23217+ p++;
1facf9fc 23218+ }
23219+ BUG();
23220+ return "??";
23221+}
23222+
076b876e
AM
23223+static const char *au_optstr(int *val, match_table_t tbl)
23224+{
23225+ struct match_token *p;
23226+ int v;
23227+
23228+ v = *val;
2000de60
JR
23229+ if (!v)
23230+ goto out;
076b876e 23231+ p = tbl;
2000de60
JR
23232+ while (p->pattern) {
23233+ if (p->token
23234+ && (v & p->token) == p->token) {
076b876e
AM
23235+ *val &= ~p->token;
23236+ return p->pattern;
23237+ }
23238+ p++;
23239+ }
2000de60
JR
23240+
23241+out:
076b876e
AM
23242+ return NULL;
23243+}
23244+
1facf9fc 23245+/* ---------------------------------------------------------------------- */
23246+
1e00d052 23247+static match_table_t brperm = {
1facf9fc 23248+ {AuBrPerm_RO, AUFS_BRPERM_RO},
23249+ {AuBrPerm_RR, AUFS_BRPERM_RR},
23250+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
23251+ {0, NULL}
23252+};
1facf9fc 23253+
86dc4139 23254+static match_table_t brattr = {
076b876e
AM
23255+ /* general */
23256+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
23257+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 23258+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 23259+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 23260+#ifdef CONFIG_AUFS_FHSM
076b876e 23261+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
23262+#endif
23263+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
23264+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
23265+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
23266+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
23267+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
23268+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
23269+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 23270+#endif
076b876e
AM
23271+
23272+ /* ro/rr branch */
1e00d052 23273+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
23274+
23275+ /* rw branch */
23276+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 23277+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 23278+
1e00d052 23279+ {0, NULL}
1facf9fc 23280+};
23281+
1e00d052
AM
23282+static int br_attr_val(char *str, match_table_t table, substring_t args[])
23283+{
23284+ int attr, v;
23285+ char *p;
23286+
23287+ attr = 0;
23288+ do {
23289+ p = strchr(str, '+');
23290+ if (p)
23291+ *p = 0;
23292+ v = match_token(str, table, args);
076b876e
AM
23293+ if (v) {
23294+ if (v & AuBrAttr_CMOO_Mask)
23295+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 23296+ attr |= v;
076b876e 23297+ } else {
1e00d052
AM
23298+ if (p)
23299+ *p = '+';
0c3ec466 23300+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
23301+ break;
23302+ }
23303+ if (p)
23304+ str = p + 1;
23305+ } while (p);
23306+
23307+ return attr;
23308+}
23309+
076b876e
AM
23310+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
23311+{
23312+ int sz;
23313+ const char *p;
23314+ char *q;
23315+
076b876e
AM
23316+ q = str->a;
23317+ *q = 0;
23318+ p = au_optstr(&perm, brattr);
23319+ if (p) {
23320+ sz = strlen(p);
23321+ memcpy(q, p, sz + 1);
23322+ q += sz;
23323+ } else
23324+ goto out;
23325+
23326+ do {
23327+ p = au_optstr(&perm, brattr);
23328+ if (p) {
23329+ *q++ = '+';
23330+ sz = strlen(p);
23331+ memcpy(q, p, sz + 1);
23332+ q += sz;
23333+ }
23334+ } while (p);
23335+
23336+out:
c1595e42 23337+ return q - str->a;
076b876e
AM
23338+}
23339+
4a4d8108 23340+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 23341+{
076b876e
AM
23342+ int val, bad, sz;
23343+ char *p;
1facf9fc 23344+ substring_t args[MAX_OPT_ARGS];
076b876e 23345+ au_br_perm_str_t attr;
1facf9fc 23346+
1e00d052
AM
23347+ p = strchr(perm, '+');
23348+ if (p)
23349+ *p = 0;
23350+ val = match_token(perm, brperm, args);
23351+ if (!val) {
23352+ if (p)
23353+ *p = '+';
0c3ec466 23354+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
23355+ val = AuBrPerm_RO;
23356+ goto out;
23357+ }
23358+ if (!p)
23359+ goto out;
23360+
076b876e
AM
23361+ val |= br_attr_val(p + 1, brattr, args);
23362+
23363+ bad = 0;
86dc4139 23364+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
23365+ case AuBrPerm_RO:
23366+ case AuBrPerm_RR:
076b876e
AM
23367+ bad = val & AuBrWAttr_Mask;
23368+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
23369+ break;
23370+ case AuBrPerm_RW:
076b876e
AM
23371+ bad = val & AuBrRAttr_Mask;
23372+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
23373+ break;
23374+ }
c1595e42
JR
23375+
23376+ /*
23377+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
23378+ * does not treat it as an error, just warning.
23379+ * this is a tiny guard for the user operation.
23380+ */
23381+ if (val & AuBrAttr_UNPIN) {
23382+ bad |= AuBrAttr_UNPIN;
23383+ val &= ~AuBrAttr_UNPIN;
23384+ }
23385+
076b876e
AM
23386+ if (unlikely(bad)) {
23387+ sz = au_do_optstr_br_attr(&attr, bad);
23388+ AuDebugOn(!sz);
23389+ pr_warn("ignored branch attribute %s\n", attr.a);
23390+ }
1e00d052
AM
23391+
23392+out:
1facf9fc 23393+ return val;
23394+}
23395+
076b876e 23396+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 23397+{
076b876e
AM
23398+ au_br_perm_str_t attr;
23399+ const char *p;
23400+ char *q;
1e00d052
AM
23401+ int sz;
23402+
076b876e
AM
23403+ q = str->a;
23404+ p = au_optstr(&perm, brperm);
23405+ AuDebugOn(!p || !*p);
23406+ sz = strlen(p);
23407+ memcpy(q, p, sz + 1);
23408+ q += sz;
1e00d052 23409+
076b876e
AM
23410+ sz = au_do_optstr_br_attr(&attr, perm);
23411+ if (sz) {
23412+ *q++ = '+';
23413+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
23414+ }
23415+
076b876e 23416+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 23417+}
23418+
23419+/* ---------------------------------------------------------------------- */
23420+
23421+static match_table_t udbalevel = {
23422+ {AuOpt_UDBA_REVAL, "reval"},
23423+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
23424+#ifdef CONFIG_AUFS_HNOTIFY
23425+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
23426+#ifdef CONFIG_AUFS_HFSNOTIFY
23427+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 23428+#endif
1facf9fc 23429+#endif
23430+ {-1, NULL}
23431+};
23432+
4a4d8108 23433+static int noinline_for_stack udba_val(char *str)
1facf9fc 23434+{
23435+ substring_t args[MAX_OPT_ARGS];
23436+
7f207e10 23437+ return match_token(str, udbalevel, args);
1facf9fc 23438+}
23439+
23440+const char *au_optstr_udba(int udba)
23441+{
076b876e 23442+ return au_parser_pattern(udba, udbalevel);
1facf9fc 23443+}
23444+
23445+/* ---------------------------------------------------------------------- */
23446+
23447+static match_table_t au_wbr_create_policy = {
23448+ {AuWbrCreate_TDP, "tdp"},
23449+ {AuWbrCreate_TDP, "top-down-parent"},
23450+ {AuWbrCreate_RR, "rr"},
23451+ {AuWbrCreate_RR, "round-robin"},
23452+ {AuWbrCreate_MFS, "mfs"},
23453+ {AuWbrCreate_MFS, "most-free-space"},
23454+ {AuWbrCreate_MFSV, "mfs:%d"},
23455+ {AuWbrCreate_MFSV, "most-free-space:%d"},
23456+
23457+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
23458+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23459+ {AuWbrCreate_PMFS, "pmfs"},
23460+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
23461+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23462+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 23463+
23464+ {-1, NULL}
23465+};
23466+
dece6358
AM
23467+/*
23468+ * cf. linux/lib/parser.c and cmdline.c
23469+ * gave up calling memparse() since it uses simple_strtoull() instead of
9dbd164d 23470+ * kstrto...().
dece6358 23471+ */
4a4d8108
AM
23472+static int noinline_for_stack
23473+au_match_ull(substring_t *s, unsigned long long *result)
1facf9fc 23474+{
23475+ int err;
23476+ unsigned int len;
23477+ char a[32];
23478+
23479+ err = -ERANGE;
23480+ len = s->to - s->from;
23481+ if (len + 1 <= sizeof(a)) {
23482+ memcpy(a, s->from, len);
23483+ a[len] = '\0';
9dbd164d 23484+ err = kstrtoull(a, 0, result);
1facf9fc 23485+ }
23486+ return err;
23487+}
23488+
23489+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23490+ struct au_opt_wbr_create *create)
23491+{
23492+ int err;
23493+ unsigned long long ull;
23494+
23495+ err = 0;
23496+ if (!au_match_ull(arg, &ull))
23497+ create->mfsrr_watermark = ull;
23498+ else {
4a4d8108 23499+ pr_err("bad integer in %s\n", str);
1facf9fc 23500+ err = -EINVAL;
23501+ }
23502+
23503+ return err;
23504+}
23505+
23506+static int au_wbr_mfs_sec(substring_t *arg, char *str,
23507+ struct au_opt_wbr_create *create)
23508+{
23509+ int n, err;
23510+
23511+ err = 0;
027c5e7a 23512+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 23513+ create->mfs_second = n;
23514+ else {
4a4d8108 23515+ pr_err("bad integer in %s\n", str);
1facf9fc 23516+ err = -EINVAL;
23517+ }
23518+
23519+ return err;
23520+}
23521+
4a4d8108
AM
23522+static int noinline_for_stack
23523+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 23524+{
23525+ int err, e;
23526+ substring_t args[MAX_OPT_ARGS];
23527+
23528+ err = match_token(str, au_wbr_create_policy, args);
23529+ create->wbr_create = err;
23530+ switch (err) {
23531+ case AuWbrCreate_MFSRRV:
392086de 23532+ case AuWbrCreate_PMFSRRV:
1facf9fc 23533+ e = au_wbr_mfs_wmark(&args[0], str, create);
23534+ if (!e)
23535+ e = au_wbr_mfs_sec(&args[1], str, create);
23536+ if (unlikely(e))
23537+ err = e;
23538+ break;
23539+ case AuWbrCreate_MFSRR:
392086de 23540+ case AuWbrCreate_PMFSRR:
1facf9fc 23541+ e = au_wbr_mfs_wmark(&args[0], str, create);
23542+ if (unlikely(e)) {
23543+ err = e;
23544+ break;
23545+ }
23546+ /*FALLTHROUGH*/
23547+ case AuWbrCreate_MFS:
23548+ case AuWbrCreate_PMFS:
027c5e7a 23549+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 23550+ break;
23551+ case AuWbrCreate_MFSV:
23552+ case AuWbrCreate_PMFSV:
23553+ e = au_wbr_mfs_sec(&args[0], str, create);
23554+ if (unlikely(e))
23555+ err = e;
23556+ break;
23557+ }
23558+
23559+ return err;
23560+}
23561+
23562+const char *au_optstr_wbr_create(int wbr_create)
23563+{
076b876e 23564+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 23565+}
23566+
23567+static match_table_t au_wbr_copyup_policy = {
23568+ {AuWbrCopyup_TDP, "tdp"},
23569+ {AuWbrCopyup_TDP, "top-down-parent"},
23570+ {AuWbrCopyup_BUP, "bup"},
23571+ {AuWbrCopyup_BUP, "bottom-up-parent"},
23572+ {AuWbrCopyup_BU, "bu"},
23573+ {AuWbrCopyup_BU, "bottom-up"},
23574+ {-1, NULL}
23575+};
23576+
4a4d8108 23577+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 23578+{
23579+ substring_t args[MAX_OPT_ARGS];
23580+
23581+ return match_token(str, au_wbr_copyup_policy, args);
23582+}
23583+
23584+const char *au_optstr_wbr_copyup(int wbr_copyup)
23585+{
076b876e 23586+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 23587+}
23588+
23589+/* ---------------------------------------------------------------------- */
23590+
23591+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23592+
23593+static void dump_opts(struct au_opts *opts)
23594+{
23595+#ifdef CONFIG_AUFS_DEBUG
23596+ /* reduce stack space */
23597+ union {
23598+ struct au_opt_add *add;
23599+ struct au_opt_del *del;
23600+ struct au_opt_mod *mod;
23601+ struct au_opt_xino *xino;
23602+ struct au_opt_xino_itrunc *xino_itrunc;
23603+ struct au_opt_wbr_create *create;
23604+ } u;
23605+ struct au_opt *opt;
23606+
23607+ opt = opts->opt;
23608+ while (opt->type != Opt_tail) {
23609+ switch (opt->type) {
23610+ case Opt_add:
23611+ u.add = &opt->add;
23612+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
23613+ u.add->bindex, u.add->pathname, u.add->perm,
23614+ u.add->path.dentry);
23615+ break;
23616+ case Opt_del:
23617+ case Opt_idel:
23618+ u.del = &opt->del;
23619+ AuDbg("del {%s, %p}\n",
23620+ u.del->pathname, u.del->h_path.dentry);
23621+ break;
23622+ case Opt_mod:
23623+ case Opt_imod:
23624+ u.mod = &opt->mod;
23625+ AuDbg("mod {%s, 0x%x, %p}\n",
23626+ u.mod->path, u.mod->perm, u.mod->h_root);
23627+ break;
23628+ case Opt_append:
23629+ u.add = &opt->add;
23630+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
23631+ u.add->bindex, u.add->pathname, u.add->perm,
23632+ u.add->path.dentry);
23633+ break;
23634+ case Opt_prepend:
23635+ u.add = &opt->add;
23636+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23637+ u.add->bindex, u.add->pathname, u.add->perm,
23638+ u.add->path.dentry);
23639+ break;
23640+ case Opt_dirwh:
23641+ AuDbg("dirwh %d\n", opt->dirwh);
23642+ break;
23643+ case Opt_rdcache:
23644+ AuDbg("rdcache %d\n", opt->rdcache);
23645+ break;
23646+ case Opt_rdblk:
23647+ AuDbg("rdblk %u\n", opt->rdblk);
23648+ break;
dece6358
AM
23649+ case Opt_rdblk_def:
23650+ AuDbg("rdblk_def\n");
23651+ break;
1facf9fc 23652+ case Opt_rdhash:
23653+ AuDbg("rdhash %u\n", opt->rdhash);
23654+ break;
dece6358
AM
23655+ case Opt_rdhash_def:
23656+ AuDbg("rdhash_def\n");
23657+ break;
1facf9fc 23658+ case Opt_xino:
23659+ u.xino = &opt->xino;
523b37e3 23660+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 23661+ break;
23662+ case Opt_trunc_xino:
23663+ AuLabel(trunc_xino);
23664+ break;
23665+ case Opt_notrunc_xino:
23666+ AuLabel(notrunc_xino);
23667+ break;
23668+ case Opt_trunc_xino_path:
23669+ case Opt_itrunc_xino:
23670+ u.xino_itrunc = &opt->xino_itrunc;
23671+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23672+ break;
1facf9fc 23673+ case Opt_noxino:
23674+ AuLabel(noxino);
23675+ break;
23676+ case Opt_trunc_xib:
23677+ AuLabel(trunc_xib);
23678+ break;
23679+ case Opt_notrunc_xib:
23680+ AuLabel(notrunc_xib);
23681+ break;
dece6358
AM
23682+ case Opt_shwh:
23683+ AuLabel(shwh);
23684+ break;
23685+ case Opt_noshwh:
23686+ AuLabel(noshwh);
23687+ break;
076b876e
AM
23688+ case Opt_dirperm1:
23689+ AuLabel(dirperm1);
23690+ break;
23691+ case Opt_nodirperm1:
23692+ AuLabel(nodirperm1);
23693+ break;
1facf9fc 23694+ case Opt_plink:
23695+ AuLabel(plink);
23696+ break;
23697+ case Opt_noplink:
23698+ AuLabel(noplink);
23699+ break;
23700+ case Opt_list_plink:
23701+ AuLabel(list_plink);
23702+ break;
23703+ case Opt_udba:
23704+ AuDbg("udba %d, %s\n",
23705+ opt->udba, au_optstr_udba(opt->udba));
23706+ break;
4a4d8108
AM
23707+ case Opt_dio:
23708+ AuLabel(dio);
23709+ break;
23710+ case Opt_nodio:
23711+ AuLabel(nodio);
23712+ break;
1facf9fc 23713+ case Opt_diropq_a:
23714+ AuLabel(diropq_a);
23715+ break;
23716+ case Opt_diropq_w:
23717+ AuLabel(diropq_w);
23718+ break;
23719+ case Opt_warn_perm:
23720+ AuLabel(warn_perm);
23721+ break;
23722+ case Opt_nowarn_perm:
23723+ AuLabel(nowarn_perm);
23724+ break;
1facf9fc 23725+ case Opt_verbose:
23726+ AuLabel(verbose);
23727+ break;
23728+ case Opt_noverbose:
23729+ AuLabel(noverbose);
23730+ break;
23731+ case Opt_sum:
23732+ AuLabel(sum);
23733+ break;
23734+ case Opt_nosum:
23735+ AuLabel(nosum);
23736+ break;
23737+ case Opt_wsum:
23738+ AuLabel(wsum);
23739+ break;
23740+ case Opt_wbr_create:
23741+ u.create = &opt->wbr_create;
23742+ AuDbg("create %d, %s\n", u.create->wbr_create,
23743+ au_optstr_wbr_create(u.create->wbr_create));
23744+ switch (u.create->wbr_create) {
23745+ case AuWbrCreate_MFSV:
23746+ case AuWbrCreate_PMFSV:
23747+ AuDbg("%d sec\n", u.create->mfs_second);
23748+ break;
23749+ case AuWbrCreate_MFSRR:
23750+ AuDbg("%llu watermark\n",
23751+ u.create->mfsrr_watermark);
23752+ break;
23753+ case AuWbrCreate_MFSRRV:
392086de 23754+ case AuWbrCreate_PMFSRRV:
1facf9fc 23755+ AuDbg("%llu watermark, %d sec\n",
23756+ u.create->mfsrr_watermark,
23757+ u.create->mfs_second);
23758+ break;
23759+ }
23760+ break;
23761+ case Opt_wbr_copyup:
23762+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23763+ au_optstr_wbr_copyup(opt->wbr_copyup));
23764+ break;
076b876e
AM
23765+ case Opt_fhsm_sec:
23766+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
23767+ break;
c1595e42
JR
23768+ case Opt_acl:
23769+ AuLabel(acl);
23770+ break;
23771+ case Opt_noacl:
23772+ AuLabel(noacl);
23773+ break;
1facf9fc 23774+ default:
23775+ BUG();
23776+ }
23777+ opt++;
23778+ }
23779+#endif
23780+}
23781+
23782+void au_opts_free(struct au_opts *opts)
23783+{
23784+ struct au_opt *opt;
23785+
23786+ opt = opts->opt;
23787+ while (opt->type != Opt_tail) {
23788+ switch (opt->type) {
23789+ case Opt_add:
23790+ case Opt_append:
23791+ case Opt_prepend:
23792+ path_put(&opt->add.path);
23793+ break;
23794+ case Opt_del:
23795+ case Opt_idel:
23796+ path_put(&opt->del.h_path);
23797+ break;
23798+ case Opt_mod:
23799+ case Opt_imod:
23800+ dput(opt->mod.h_root);
23801+ break;
23802+ case Opt_xino:
23803+ fput(opt->xino.file);
23804+ break;
23805+ }
23806+ opt++;
23807+ }
23808+}
23809+
23810+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
23811+ aufs_bindex_t bindex)
23812+{
23813+ int err;
23814+ struct au_opt_add *add = &opt->add;
23815+ char *p;
23816+
23817+ add->bindex = bindex;
1e00d052 23818+ add->perm = AuBrPerm_RO;
1facf9fc 23819+ add->pathname = opt_str;
23820+ p = strchr(opt_str, '=');
23821+ if (p) {
23822+ *p++ = 0;
23823+ if (*p)
23824+ add->perm = br_perm_val(p);
23825+ }
23826+
23827+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
23828+ if (!err) {
23829+ if (!p) {
23830+ add->perm = AuBrPerm_RO;
23831+ if (au_test_fs_rr(add->path.dentry->d_sb))
23832+ add->perm = AuBrPerm_RR;
23833+ else if (!bindex && !(sb_flags & MS_RDONLY))
23834+ add->perm = AuBrPerm_RW;
23835+ }
23836+ opt->type = Opt_add;
23837+ goto out;
23838+ }
4a4d8108 23839+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 23840+ err = -EINVAL;
23841+
4f0767ce 23842+out:
1facf9fc 23843+ return err;
23844+}
23845+
23846+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
23847+{
23848+ int err;
23849+
23850+ del->pathname = args[0].from;
23851+ AuDbg("del path %s\n", del->pathname);
23852+
23853+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
23854+ if (unlikely(err))
4a4d8108 23855+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 23856+
23857+ return err;
23858+}
23859+
23860+#if 0 /* reserved for future use */
23861+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
23862+ struct au_opt_del *del, substring_t args[])
23863+{
23864+ int err;
23865+ struct dentry *root;
23866+
23867+ err = -EINVAL;
23868+ root = sb->s_root;
23869+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 23870+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 23871+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 23872+ goto out;
23873+ }
23874+
23875+ err = 0;
23876+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
23877+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
23878+
4f0767ce 23879+out:
1facf9fc 23880+ aufs_read_unlock(root, !AuLock_IR);
23881+ return err;
23882+}
23883+#endif
23884+
4a4d8108
AM
23885+static int noinline_for_stack
23886+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 23887+{
23888+ int err;
23889+ struct path path;
23890+ char *p;
23891+
23892+ err = -EINVAL;
23893+ mod->path = args[0].from;
23894+ p = strchr(mod->path, '=');
23895+ if (unlikely(!p)) {
4a4d8108 23896+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 23897+ goto out;
23898+ }
23899+
23900+ *p++ = 0;
23901+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
23902+ if (unlikely(err)) {
4a4d8108 23903+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 23904+ goto out;
23905+ }
23906+
23907+ mod->perm = br_perm_val(p);
23908+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23909+ mod->h_root = dget(path.dentry);
23910+ path_put(&path);
23911+
4f0767ce 23912+out:
1facf9fc 23913+ return err;
23914+}
23915+
23916+#if 0 /* reserved for future use */
23917+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23918+ struct au_opt_mod *mod, substring_t args[])
23919+{
23920+ int err;
23921+ struct dentry *root;
23922+
23923+ err = -EINVAL;
23924+ root = sb->s_root;
23925+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 23926+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 23927+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 23928+ goto out;
23929+ }
23930+
23931+ err = 0;
23932+ mod->perm = br_perm_val(args[1].from);
23933+ AuDbg("mod path %s, perm 0x%x, %s\n",
23934+ mod->path, mod->perm, args[1].from);
23935+ mod->h_root = dget(au_h_dptr(root, bindex));
23936+
4f0767ce 23937+out:
1facf9fc 23938+ aufs_read_unlock(root, !AuLock_IR);
23939+ return err;
23940+}
23941+#endif
23942+
23943+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23944+ substring_t args[])
23945+{
23946+ int err;
23947+ struct file *file;
23948+
23949+ file = au_xino_create(sb, args[0].from, /*silent*/0);
23950+ err = PTR_ERR(file);
23951+ if (IS_ERR(file))
23952+ goto out;
23953+
23954+ err = -EINVAL;
2000de60 23955+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 23956+ fput(file);
4a4d8108 23957+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 23958+ goto out;
23959+ }
23960+
23961+ err = 0;
23962+ xino->file = file;
23963+ xino->path = args[0].from;
23964+
4f0767ce 23965+out:
1facf9fc 23966+ return err;
23967+}
23968+
4a4d8108
AM
23969+static int noinline_for_stack
23970+au_opts_parse_xino_itrunc_path(struct super_block *sb,
23971+ struct au_opt_xino_itrunc *xino_itrunc,
23972+ substring_t args[])
1facf9fc 23973+{
23974+ int err;
5afbbe0d 23975+ aufs_bindex_t bbot, bindex;
1facf9fc 23976+ struct path path;
23977+ struct dentry *root;
23978+
23979+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23980+ if (unlikely(err)) {
4a4d8108 23981+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 23982+ goto out;
23983+ }
23984+
23985+ xino_itrunc->bindex = -1;
23986+ root = sb->s_root;
23987+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
23988+ bbot = au_sbbot(sb);
23989+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 23990+ if (au_h_dptr(root, bindex) == path.dentry) {
23991+ xino_itrunc->bindex = bindex;
23992+ break;
23993+ }
23994+ }
23995+ aufs_read_unlock(root, !AuLock_IR);
23996+ path_put(&path);
23997+
23998+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 23999+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 24000+ err = -EINVAL;
24001+ }
24002+
4f0767ce 24003+out:
1facf9fc 24004+ return err;
24005+}
24006+
24007+/* called without aufs lock */
24008+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
24009+{
24010+ int err, n, token;
24011+ aufs_bindex_t bindex;
24012+ unsigned char skipped;
24013+ struct dentry *root;
24014+ struct au_opt *opt, *opt_tail;
24015+ char *opt_str;
24016+ /* reduce the stack space */
24017+ union {
24018+ struct au_opt_xino_itrunc *xino_itrunc;
24019+ struct au_opt_wbr_create *create;
24020+ } u;
24021+ struct {
24022+ substring_t args[MAX_OPT_ARGS];
24023+ } *a;
24024+
24025+ err = -ENOMEM;
24026+ a = kmalloc(sizeof(*a), GFP_NOFS);
24027+ if (unlikely(!a))
24028+ goto out;
24029+
24030+ root = sb->s_root;
24031+ err = 0;
24032+ bindex = 0;
24033+ opt = opts->opt;
24034+ opt_tail = opt + opts->max_opt - 1;
24035+ opt->type = Opt_tail;
24036+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
24037+ err = -EINVAL;
24038+ skipped = 0;
24039+ token = match_token(opt_str, options, a->args);
24040+ switch (token) {
24041+ case Opt_br:
24042+ err = 0;
24043+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
24044+ && *opt_str) {
24045+ err = opt_add(opt, opt_str, opts->sb_flags,
24046+ bindex++);
24047+ if (unlikely(!err && ++opt > opt_tail)) {
24048+ err = -E2BIG;
24049+ break;
24050+ }
24051+ opt->type = Opt_tail;
24052+ skipped = 1;
24053+ }
24054+ break;
24055+ case Opt_add:
24056+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 24057+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24058+ break;
24059+ }
24060+ bindex = n;
24061+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
24062+ bindex);
24063+ if (!err)
24064+ opt->type = token;
24065+ break;
24066+ case Opt_append:
24067+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
24068+ /*dummy bindex*/1);
24069+ if (!err)
24070+ opt->type = token;
24071+ break;
24072+ case Opt_prepend:
24073+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
24074+ /*bindex*/0);
24075+ if (!err)
24076+ opt->type = token;
24077+ break;
24078+ case Opt_del:
24079+ err = au_opts_parse_del(&opt->del, a->args);
24080+ if (!err)
24081+ opt->type = token;
24082+ break;
24083+#if 0 /* reserved for future use */
24084+ case Opt_idel:
24085+ del->pathname = "(indexed)";
24086+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 24087+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24088+ break;
24089+ }
24090+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
24091+ if (!err)
24092+ opt->type = token;
24093+ break;
24094+#endif
24095+ case Opt_mod:
24096+ err = au_opts_parse_mod(&opt->mod, a->args);
24097+ if (!err)
24098+ opt->type = token;
24099+ break;
24100+#ifdef IMOD /* reserved for future use */
24101+ case Opt_imod:
24102+ u.mod->path = "(indexed)";
24103+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 24104+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24105+ break;
24106+ }
24107+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
24108+ if (!err)
24109+ opt->type = token;
24110+ break;
24111+#endif
24112+ case Opt_xino:
24113+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
24114+ if (!err)
24115+ opt->type = token;
24116+ break;
24117+
24118+ case Opt_trunc_xino_path:
24119+ err = au_opts_parse_xino_itrunc_path
24120+ (sb, &opt->xino_itrunc, a->args);
24121+ if (!err)
24122+ opt->type = token;
24123+ break;
24124+
24125+ case Opt_itrunc_xino:
24126+ u.xino_itrunc = &opt->xino_itrunc;
24127+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 24128+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24129+ break;
24130+ }
24131+ u.xino_itrunc->bindex = n;
24132+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 24133+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 24134+ pr_err("out of bounds, %d\n", n);
1facf9fc 24135+ aufs_read_unlock(root, !AuLock_IR);
24136+ break;
24137+ }
24138+ aufs_read_unlock(root, !AuLock_IR);
24139+ err = 0;
24140+ opt->type = token;
24141+ break;
24142+
24143+ case Opt_dirwh:
24144+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
24145+ break;
24146+ err = 0;
24147+ opt->type = token;
24148+ break;
24149+
24150+ case Opt_rdcache:
027c5e7a
AM
24151+ if (unlikely(match_int(&a->args[0], &n))) {
24152+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24153+ break;
027c5e7a
AM
24154+ }
24155+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
24156+ pr_err("rdcache must be smaller than %d\n",
24157+ AUFS_RDCACHE_MAX);
24158+ break;
24159+ }
24160+ opt->rdcache = n;
1facf9fc 24161+ err = 0;
24162+ opt->type = token;
24163+ break;
24164+ case Opt_rdblk:
24165+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 24166+ || n < 0
1facf9fc 24167+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 24168+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24169+ break;
24170+ }
1308ab2a 24171+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
24172+ pr_err("rdblk must be larger than %d\n",
24173+ NAME_MAX);
1facf9fc 24174+ break;
24175+ }
24176+ opt->rdblk = n;
24177+ err = 0;
24178+ opt->type = token;
24179+ break;
24180+ case Opt_rdhash:
24181+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 24182+ || n < 0
1facf9fc 24183+ || n * sizeof(struct hlist_head)
24184+ > KMALLOC_MAX_SIZE)) {
4a4d8108 24185+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24186+ break;
24187+ }
24188+ opt->rdhash = n;
24189+ err = 0;
24190+ opt->type = token;
24191+ break;
24192+
24193+ case Opt_trunc_xino:
24194+ case Opt_notrunc_xino:
24195+ case Opt_noxino:
24196+ case Opt_trunc_xib:
24197+ case Opt_notrunc_xib:
dece6358
AM
24198+ case Opt_shwh:
24199+ case Opt_noshwh:
076b876e
AM
24200+ case Opt_dirperm1:
24201+ case Opt_nodirperm1:
1facf9fc 24202+ case Opt_plink:
24203+ case Opt_noplink:
24204+ case Opt_list_plink:
4a4d8108
AM
24205+ case Opt_dio:
24206+ case Opt_nodio:
1facf9fc 24207+ case Opt_diropq_a:
24208+ case Opt_diropq_w:
24209+ case Opt_warn_perm:
24210+ case Opt_nowarn_perm:
1facf9fc 24211+ case Opt_verbose:
24212+ case Opt_noverbose:
24213+ case Opt_sum:
24214+ case Opt_nosum:
24215+ case Opt_wsum:
dece6358
AM
24216+ case Opt_rdblk_def:
24217+ case Opt_rdhash_def:
c1595e42
JR
24218+ case Opt_acl:
24219+ case Opt_noacl:
1facf9fc 24220+ err = 0;
24221+ opt->type = token;
24222+ break;
24223+
24224+ case Opt_udba:
24225+ opt->udba = udba_val(a->args[0].from);
24226+ if (opt->udba >= 0) {
24227+ err = 0;
24228+ opt->type = token;
24229+ } else
4a4d8108 24230+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 24231+ break;
24232+
24233+ case Opt_wbr_create:
24234+ u.create = &opt->wbr_create;
24235+ u.create->wbr_create
24236+ = au_wbr_create_val(a->args[0].from, u.create);
24237+ if (u.create->wbr_create >= 0) {
24238+ err = 0;
24239+ opt->type = token;
24240+ } else
4a4d8108 24241+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 24242+ break;
24243+ case Opt_wbr_copyup:
24244+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
24245+ if (opt->wbr_copyup >= 0) {
24246+ err = 0;
24247+ opt->type = token;
24248+ } else
4a4d8108 24249+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 24250+ break;
24251+
076b876e
AM
24252+ case Opt_fhsm_sec:
24253+ if (unlikely(match_int(&a->args[0], &n)
24254+ || n < 0)) {
24255+ pr_err("bad integer in %s\n", opt_str);
24256+ break;
24257+ }
24258+ if (sysaufs_brs) {
24259+ opt->fhsm_second = n;
24260+ opt->type = token;
24261+ } else
24262+ pr_warn("ignored %s\n", opt_str);
24263+ err = 0;
24264+ break;
24265+
1facf9fc 24266+ case Opt_ignore:
0c3ec466 24267+ pr_warn("ignored %s\n", opt_str);
1facf9fc 24268+ /*FALLTHROUGH*/
24269+ case Opt_ignore_silent:
24270+ skipped = 1;
24271+ err = 0;
24272+ break;
24273+ case Opt_err:
4a4d8108 24274+ pr_err("unknown option %s\n", opt_str);
1facf9fc 24275+ break;
24276+ }
24277+
24278+ if (!err && !skipped) {
24279+ if (unlikely(++opt > opt_tail)) {
24280+ err = -E2BIG;
24281+ opt--;
24282+ opt->type = Opt_tail;
24283+ break;
24284+ }
24285+ opt->type = Opt_tail;
24286+ }
24287+ }
24288+
f0c0a007 24289+ au_delayed_kfree(a);
1facf9fc 24290+ dump_opts(opts);
24291+ if (unlikely(err))
24292+ au_opts_free(opts);
24293+
4f0767ce 24294+out:
1facf9fc 24295+ return err;
24296+}
24297+
24298+static int au_opt_wbr_create(struct super_block *sb,
24299+ struct au_opt_wbr_create *create)
24300+{
24301+ int err;
24302+ struct au_sbinfo *sbinfo;
24303+
dece6358
AM
24304+ SiMustWriteLock(sb);
24305+
1facf9fc 24306+ err = 1; /* handled */
24307+ sbinfo = au_sbi(sb);
24308+ if (sbinfo->si_wbr_create_ops->fin) {
24309+ err = sbinfo->si_wbr_create_ops->fin(sb);
24310+ if (!err)
24311+ err = 1;
24312+ }
24313+
24314+ sbinfo->si_wbr_create = create->wbr_create;
24315+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
24316+ switch (create->wbr_create) {
24317+ case AuWbrCreate_MFSRRV:
24318+ case AuWbrCreate_MFSRR:
392086de
AM
24319+ case AuWbrCreate_PMFSRR:
24320+ case AuWbrCreate_PMFSRRV:
1facf9fc 24321+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
24322+ /*FALLTHROUGH*/
24323+ case AuWbrCreate_MFS:
24324+ case AuWbrCreate_MFSV:
24325+ case AuWbrCreate_PMFS:
24326+ case AuWbrCreate_PMFSV:
e49829fe
JR
24327+ sbinfo->si_wbr_mfs.mfs_expire
24328+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 24329+ break;
24330+ }
24331+
24332+ if (sbinfo->si_wbr_create_ops->init)
24333+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
24334+
24335+ return err;
24336+}
24337+
24338+/*
24339+ * returns,
24340+ * plus: processed without an error
24341+ * zero: unprocessed
24342+ */
24343+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
24344+ struct au_opts *opts)
24345+{
24346+ int err;
24347+ struct au_sbinfo *sbinfo;
24348+
dece6358
AM
24349+ SiMustWriteLock(sb);
24350+
1facf9fc 24351+ err = 1; /* handled */
24352+ sbinfo = au_sbi(sb);
24353+ switch (opt->type) {
24354+ case Opt_udba:
24355+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24356+ sbinfo->si_mntflags |= opt->udba;
24357+ opts->given_udba |= opt->udba;
24358+ break;
24359+
24360+ case Opt_plink:
24361+ au_opt_set(sbinfo->si_mntflags, PLINK);
24362+ break;
24363+ case Opt_noplink:
24364+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 24365+ au_plink_put(sb, /*verbose*/1);
1facf9fc 24366+ au_opt_clr(sbinfo->si_mntflags, PLINK);
24367+ break;
24368+ case Opt_list_plink:
24369+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
24370+ au_plink_list(sb);
24371+ break;
24372+
4a4d8108
AM
24373+ case Opt_dio:
24374+ au_opt_set(sbinfo->si_mntflags, DIO);
24375+ au_fset_opts(opts->flags, REFRESH_DYAOP);
24376+ break;
24377+ case Opt_nodio:
24378+ au_opt_clr(sbinfo->si_mntflags, DIO);
24379+ au_fset_opts(opts->flags, REFRESH_DYAOP);
24380+ break;
24381+
076b876e
AM
24382+ case Opt_fhsm_sec:
24383+ au_fhsm_set(sbinfo, opt->fhsm_second);
24384+ break;
24385+
1facf9fc 24386+ case Opt_diropq_a:
24387+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24388+ break;
24389+ case Opt_diropq_w:
24390+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24391+ break;
24392+
24393+ case Opt_warn_perm:
24394+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
24395+ break;
24396+ case Opt_nowarn_perm:
24397+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
24398+ break;
24399+
1facf9fc 24400+ case Opt_verbose:
24401+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
24402+ break;
24403+ case Opt_noverbose:
24404+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
24405+ break;
24406+
24407+ case Opt_sum:
24408+ au_opt_set(sbinfo->si_mntflags, SUM);
24409+ break;
24410+ case Opt_wsum:
24411+ au_opt_clr(sbinfo->si_mntflags, SUM);
24412+ au_opt_set(sbinfo->si_mntflags, SUM_W);
24413+ case Opt_nosum:
24414+ au_opt_clr(sbinfo->si_mntflags, SUM);
24415+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
24416+ break;
24417+
24418+ case Opt_wbr_create:
24419+ err = au_opt_wbr_create(sb, &opt->wbr_create);
24420+ break;
24421+ case Opt_wbr_copyup:
24422+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
24423+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
24424+ break;
24425+
24426+ case Opt_dirwh:
24427+ sbinfo->si_dirwh = opt->dirwh;
24428+ break;
24429+
24430+ case Opt_rdcache:
e49829fe
JR
24431+ sbinfo->si_rdcache
24432+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 24433+ break;
24434+ case Opt_rdblk:
24435+ sbinfo->si_rdblk = opt->rdblk;
24436+ break;
dece6358
AM
24437+ case Opt_rdblk_def:
24438+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24439+ break;
1facf9fc 24440+ case Opt_rdhash:
24441+ sbinfo->si_rdhash = opt->rdhash;
24442+ break;
dece6358
AM
24443+ case Opt_rdhash_def:
24444+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24445+ break;
24446+
24447+ case Opt_shwh:
24448+ au_opt_set(sbinfo->si_mntflags, SHWH);
24449+ break;
24450+ case Opt_noshwh:
24451+ au_opt_clr(sbinfo->si_mntflags, SHWH);
24452+ break;
1facf9fc 24453+
076b876e
AM
24454+ case Opt_dirperm1:
24455+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24456+ break;
24457+ case Opt_nodirperm1:
24458+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24459+ break;
24460+
1facf9fc 24461+ case Opt_trunc_xino:
24462+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24463+ break;
24464+ case Opt_notrunc_xino:
24465+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24466+ break;
24467+
24468+ case Opt_trunc_xino_path:
24469+ case Opt_itrunc_xino:
24470+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24471+ if (!err)
24472+ err = 1;
24473+ break;
24474+
24475+ case Opt_trunc_xib:
24476+ au_fset_opts(opts->flags, TRUNC_XIB);
24477+ break;
24478+ case Opt_notrunc_xib:
24479+ au_fclr_opts(opts->flags, TRUNC_XIB);
24480+ break;
24481+
c1595e42
JR
24482+ case Opt_acl:
24483+ sb->s_flags |= MS_POSIXACL;
24484+ break;
24485+ case Opt_noacl:
24486+ sb->s_flags &= ~MS_POSIXACL;
24487+ break;
24488+
1facf9fc 24489+ default:
24490+ err = 0;
24491+ break;
24492+ }
24493+
24494+ return err;
24495+}
24496+
24497+/*
24498+ * returns tri-state.
24499+ * plus: processed without an error
24500+ * zero: unprocessed
24501+ * minus: error
24502+ */
24503+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24504+ struct au_opts *opts)
24505+{
24506+ int err, do_refresh;
24507+
24508+ err = 0;
24509+ switch (opt->type) {
24510+ case Opt_append:
5afbbe0d 24511+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 24512+ if (opt->add.bindex < 0)
24513+ opt->add.bindex = 0;
24514+ goto add;
24515+ case Opt_prepend:
24516+ opt->add.bindex = 0;
f6b6e03d 24517+ add: /* indented label */
1facf9fc 24518+ case Opt_add:
24519+ err = au_br_add(sb, &opt->add,
24520+ au_ftest_opts(opts->flags, REMOUNT));
24521+ if (!err) {
24522+ err = 1;
027c5e7a 24523+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24524+ }
24525+ break;
24526+
24527+ case Opt_del:
24528+ case Opt_idel:
24529+ err = au_br_del(sb, &opt->del,
24530+ au_ftest_opts(opts->flags, REMOUNT));
24531+ if (!err) {
24532+ err = 1;
24533+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 24534+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24535+ }
24536+ break;
24537+
24538+ case Opt_mod:
24539+ case Opt_imod:
24540+ err = au_br_mod(sb, &opt->mod,
24541+ au_ftest_opts(opts->flags, REMOUNT),
24542+ &do_refresh);
24543+ if (!err) {
24544+ err = 1;
027c5e7a
AM
24545+ if (do_refresh)
24546+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24547+ }
24548+ break;
24549+ }
24550+
24551+ return err;
24552+}
24553+
24554+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24555+ struct au_opt_xino **opt_xino,
24556+ struct au_opts *opts)
24557+{
24558+ int err;
5afbbe0d 24559+ aufs_bindex_t bbot, bindex;
1facf9fc 24560+ struct dentry *root, *parent, *h_root;
24561+
24562+ err = 0;
24563+ switch (opt->type) {
24564+ case Opt_xino:
24565+ err = au_xino_set(sb, &opt->xino,
24566+ !!au_ftest_opts(opts->flags, REMOUNT));
24567+ if (unlikely(err))
24568+ break;
24569+
24570+ *opt_xino = &opt->xino;
24571+ au_xino_brid_set(sb, -1);
24572+
24573+ /* safe d_parent access */
2000de60 24574+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 24575+ root = sb->s_root;
5afbbe0d
AM
24576+ bbot = au_sbbot(sb);
24577+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 24578+ h_root = au_h_dptr(root, bindex);
24579+ if (h_root == parent) {
24580+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24581+ break;
24582+ }
24583+ }
24584+ break;
24585+
24586+ case Opt_noxino:
24587+ au_xino_clr(sb);
24588+ au_xino_brid_set(sb, -1);
24589+ *opt_xino = (void *)-1;
24590+ break;
24591+ }
24592+
24593+ return err;
24594+}
24595+
24596+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24597+ unsigned int pending)
24598+{
076b876e 24599+ int err, fhsm;
5afbbe0d 24600+ aufs_bindex_t bindex, bbot;
79b8bda9 24601+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 24602+ struct au_branch *br;
24603+ struct au_wbr *wbr;
79b8bda9 24604+ struct dentry *root, *dentry;
1facf9fc 24605+ struct inode *dir, *h_dir;
24606+ struct au_sbinfo *sbinfo;
24607+ struct au_hinode *hdir;
24608+
dece6358
AM
24609+ SiMustAnyLock(sb);
24610+
1facf9fc 24611+ sbinfo = au_sbi(sb);
24612+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24613+
dece6358
AM
24614+ if (!(sb_flags & MS_RDONLY)) {
24615+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 24616+ pr_warn("first branch should be rw\n");
dece6358 24617+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 24618+ pr_warn_once("shwh should be used with ro\n");
dece6358 24619+ }
1facf9fc 24620+
4a4d8108 24621+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 24622+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 24623+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 24624+
076b876e 24625+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
24626+ pr_warn_once("dirperm1 breaks the protection"
24627+ " by the permission bits on the lower branch\n");
076b876e 24628+
1facf9fc 24629+ err = 0;
076b876e 24630+ fhsm = 0;
1facf9fc 24631+ root = sb->s_root;
5527c038 24632+ dir = d_inode(root);
1facf9fc 24633+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
24634+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
24635+ UDBA_NONE);
5afbbe0d
AM
24636+ bbot = au_sbbot(sb);
24637+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 24638+ skip = 0;
24639+ h_dir = au_h_iptr(dir, bindex);
24640+ br = au_sbr(sb, bindex);
1facf9fc 24641+
c1595e42
JR
24642+ if ((br->br_perm & AuBrAttr_ICEX)
24643+ && !h_dir->i_op->listxattr)
24644+ br->br_perm &= ~AuBrAttr_ICEX;
24645+#if 0
24646+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
24647+ && (au_br_sb(br)->s_flags & MS_NOSEC))
24648+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
24649+#endif
24650+
24651+ do_free = 0;
1facf9fc 24652+ wbr = br->br_wbr;
24653+ if (wbr)
24654+ wbr_wh_read_lock(wbr);
24655+
1e00d052 24656+ if (!au_br_writable(br->br_perm)) {
1facf9fc 24657+ do_free = !!wbr;
24658+ skip = (!wbr
24659+ || (!wbr->wbr_whbase
24660+ && !wbr->wbr_plink
24661+ && !wbr->wbr_orph));
1e00d052 24662+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 24663+ /* skip = (!br->br_whbase && !br->br_orph); */
24664+ skip = (!wbr || !wbr->wbr_whbase);
24665+ if (skip && wbr) {
24666+ if (do_plink)
24667+ skip = !!wbr->wbr_plink;
24668+ else
24669+ skip = !wbr->wbr_plink;
24670+ }
1e00d052 24671+ } else {
1facf9fc 24672+ /* skip = (br->br_whbase && br->br_ohph); */
24673+ skip = (wbr && wbr->wbr_whbase);
24674+ if (skip) {
24675+ if (do_plink)
24676+ skip = !!wbr->wbr_plink;
24677+ else
24678+ skip = !wbr->wbr_plink;
24679+ }
1facf9fc 24680+ }
24681+ if (wbr)
24682+ wbr_wh_read_unlock(wbr);
24683+
79b8bda9
AM
24684+ if (can_no_dreval) {
24685+ dentry = br->br_path.dentry;
24686+ spin_lock(&dentry->d_lock);
24687+ if (dentry->d_flags &
24688+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
24689+ can_no_dreval = 0;
24690+ spin_unlock(&dentry->d_lock);
24691+ }
24692+
076b876e
AM
24693+ if (au_br_fhsm(br->br_perm)) {
24694+ fhsm++;
24695+ AuDebugOn(!br->br_fhsm);
24696+ }
24697+
1facf9fc 24698+ if (skip)
24699+ continue;
24700+
24701+ hdir = au_hi(dir, bindex);
5afbbe0d 24702+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 24703+ if (wbr)
24704+ wbr_wh_write_lock(wbr);
86dc4139 24705+ err = au_wh_init(br, sb);
1facf9fc 24706+ if (wbr)
24707+ wbr_wh_write_unlock(wbr);
5afbbe0d 24708+ au_hn_inode_unlock(hdir);
1facf9fc 24709+
24710+ if (!err && do_free) {
f0c0a007
AM
24711+ if (wbr)
24712+ au_delayed_kfree(wbr);
1facf9fc 24713+ br->br_wbr = NULL;
24714+ }
24715+ }
24716+
79b8bda9
AM
24717+ if (can_no_dreval)
24718+ au_fset_si(sbinfo, NO_DREVAL);
24719+ else
24720+ au_fclr_si(sbinfo, NO_DREVAL);
24721+
c1595e42 24722+ if (fhsm >= 2) {
076b876e 24723+ au_fset_si(sbinfo, FHSM);
5afbbe0d 24724+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
24725+ br = au_sbr(sb, bindex);
24726+ if (au_br_fhsm(br->br_perm)) {
24727+ au_fhsm_set_bottom(sb, bindex);
24728+ break;
24729+ }
24730+ }
24731+ } else {
076b876e 24732+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
24733+ au_fhsm_set_bottom(sb, -1);
24734+ }
076b876e 24735+
1facf9fc 24736+ return err;
24737+}
24738+
24739+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24740+{
24741+ int err;
24742+ unsigned int tmp;
5afbbe0d 24743+ aufs_bindex_t bindex, bbot;
1facf9fc 24744+ struct au_opt *opt;
24745+ struct au_opt_xino *opt_xino, xino;
24746+ struct au_sbinfo *sbinfo;
027c5e7a 24747+ struct au_branch *br;
076b876e 24748+ struct inode *dir;
1facf9fc 24749+
dece6358
AM
24750+ SiMustWriteLock(sb);
24751+
1facf9fc 24752+ err = 0;
24753+ opt_xino = NULL;
24754+ opt = opts->opt;
24755+ while (err >= 0 && opt->type != Opt_tail)
24756+ err = au_opt_simple(sb, opt++, opts);
24757+ if (err > 0)
24758+ err = 0;
24759+ else if (unlikely(err < 0))
24760+ goto out;
24761+
24762+ /* disable xino and udba temporary */
24763+ sbinfo = au_sbi(sb);
24764+ tmp = sbinfo->si_mntflags;
24765+ au_opt_clr(sbinfo->si_mntflags, XINO);
24766+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
24767+
24768+ opt = opts->opt;
24769+ while (err >= 0 && opt->type != Opt_tail)
24770+ err = au_opt_br(sb, opt++, opts);
24771+ if (err > 0)
24772+ err = 0;
24773+ else if (unlikely(err < 0))
24774+ goto out;
24775+
5afbbe0d
AM
24776+ bbot = au_sbbot(sb);
24777+ if (unlikely(bbot < 0)) {
1facf9fc 24778+ err = -EINVAL;
4a4d8108 24779+ pr_err("no branches\n");
1facf9fc 24780+ goto out;
24781+ }
24782+
24783+ if (au_opt_test(tmp, XINO))
24784+ au_opt_set(sbinfo->si_mntflags, XINO);
24785+ opt = opts->opt;
24786+ while (!err && opt->type != Opt_tail)
24787+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
24788+ if (unlikely(err))
24789+ goto out;
24790+
24791+ err = au_opts_verify(sb, sb->s_flags, tmp);
24792+ if (unlikely(err))
24793+ goto out;
24794+
24795+ /* restore xino */
24796+ if (au_opt_test(tmp, XINO) && !opt_xino) {
24797+ xino.file = au_xino_def(sb);
24798+ err = PTR_ERR(xino.file);
24799+ if (IS_ERR(xino.file))
24800+ goto out;
24801+
24802+ err = au_xino_set(sb, &xino, /*remount*/0);
24803+ fput(xino.file);
24804+ if (unlikely(err))
24805+ goto out;
24806+ }
24807+
24808+ /* restore udba */
027c5e7a 24809+ tmp &= AuOptMask_UDBA;
1facf9fc 24810+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 24811+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
24812+ bbot = au_sbbot(sb);
24813+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
24814+ br = au_sbr(sb, bindex);
24815+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
24816+ if (unlikely(err))
24817+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
24818+ bindex, err);
24819+ /* go on even if err */
24820+ }
4a4d8108 24821+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 24822+ dir = d_inode(sb->s_root);
4a4d8108 24823+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 24824+ }
24825+
4f0767ce 24826+out:
1facf9fc 24827+ return err;
24828+}
24829+
24830+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
24831+{
24832+ int err, rerr;
79b8bda9 24833+ unsigned char no_dreval;
1facf9fc 24834+ struct inode *dir;
24835+ struct au_opt_xino *opt_xino;
24836+ struct au_opt *opt;
24837+ struct au_sbinfo *sbinfo;
24838+
dece6358
AM
24839+ SiMustWriteLock(sb);
24840+
79b8bda9 24841+ err = 0;
5527c038 24842+ dir = d_inode(sb->s_root);
1facf9fc 24843+ sbinfo = au_sbi(sb);
1facf9fc 24844+ opt_xino = NULL;
24845+ opt = opts->opt;
24846+ while (err >= 0 && opt->type != Opt_tail) {
24847+ err = au_opt_simple(sb, opt, opts);
24848+ if (!err)
24849+ err = au_opt_br(sb, opt, opts);
24850+ if (!err)
24851+ err = au_opt_xino(sb, opt, &opt_xino, opts);
24852+ opt++;
24853+ }
24854+ if (err > 0)
24855+ err = 0;
24856+ AuTraceErr(err);
24857+ /* go on even err */
24858+
79b8bda9 24859+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 24860+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
24861+ if (unlikely(rerr && !err))
24862+ err = rerr;
24863+
79b8bda9 24864+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 24865+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 24866+
1facf9fc 24867+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
24868+ rerr = au_xib_trunc(sb);
24869+ if (unlikely(rerr && !err))
24870+ err = rerr;
24871+ }
24872+
24873+ /* will be handled by the caller */
027c5e7a 24874+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
24875+ && (opts->given_udba
24876+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 24877+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 24878+ ))
027c5e7a 24879+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24880+
24881+ AuDbg("status 0x%x\n", opts->flags);
24882+ return err;
24883+}
24884+
24885+/* ---------------------------------------------------------------------- */
24886+
24887+unsigned int au_opt_udba(struct super_block *sb)
24888+{
24889+ return au_mntflags(sb) & AuOptMask_UDBA;
24890+}
7f207e10
AM
24891diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
24892--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 24893+++ linux/fs/aufs/opts.h 2016-07-25 19:05:34.814493242 +0200
79b8bda9 24894@@ -0,0 +1,211 @@
1facf9fc 24895+/*
8cdd5066 24896+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 24897+ *
24898+ * This program, aufs is free software; you can redistribute it and/or modify
24899+ * it under the terms of the GNU General Public License as published by
24900+ * the Free Software Foundation; either version 2 of the License, or
24901+ * (at your option) any later version.
dece6358
AM
24902+ *
24903+ * This program is distributed in the hope that it will be useful,
24904+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24905+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24906+ * GNU General Public License for more details.
24907+ *
24908+ * You should have received a copy of the GNU General Public License
523b37e3 24909+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24910+ */
24911+
24912+/*
24913+ * mount options/flags
24914+ */
24915+
24916+#ifndef __AUFS_OPTS_H__
24917+#define __AUFS_OPTS_H__
24918+
24919+#ifdef __KERNEL__
24920+
dece6358 24921+#include <linux/path.h>
1facf9fc 24922+
dece6358
AM
24923+struct file;
24924+struct super_block;
24925+
1facf9fc 24926+/* ---------------------------------------------------------------------- */
24927+
24928+/* mount flags */
24929+#define AuOpt_XINO 1 /* external inode number bitmap
24930+ and translation table */
24931+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
24932+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
24933+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 24934+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
24935+#define AuOpt_SHWH (1 << 5) /* show whiteout */
24936+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
24937+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
24938+ bits */
dece6358
AM
24939+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
24940+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
24941+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
24942+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
24943+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 24944+#define AuOpt_DIO (1 << 14) /* direct io */
1facf9fc 24945+
4a4d8108
AM
24946+#ifndef CONFIG_AUFS_HNOTIFY
24947+#undef AuOpt_UDBA_HNOTIFY
24948+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 24949+#endif
dece6358
AM
24950+#ifndef CONFIG_AUFS_SHWH
24951+#undef AuOpt_SHWH
24952+#define AuOpt_SHWH 0
24953+#endif
1facf9fc 24954+
24955+#define AuOpt_Def (AuOpt_XINO \
24956+ | AuOpt_UDBA_REVAL \
24957+ | AuOpt_PLINK \
24958+ /* | AuOpt_DIRPERM1 */ \
24959+ | AuOpt_WARN_PERM)
24960+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24961+ | AuOpt_UDBA_REVAL \
4a4d8108 24962+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 24963+
24964+#define au_opt_test(flags, name) (flags & AuOpt_##name)
24965+#define au_opt_set(flags, name) do { \
24966+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24967+ ((flags) |= AuOpt_##name); \
24968+} while (0)
24969+#define au_opt_set_udba(flags, name) do { \
24970+ (flags) &= ~AuOptMask_UDBA; \
24971+ ((flags) |= AuOpt_##name); \
24972+} while (0)
7f207e10
AM
24973+#define au_opt_clr(flags, name) do { \
24974+ ((flags) &= ~AuOpt_##name); \
24975+} while (0)
1facf9fc 24976+
e49829fe
JR
24977+static inline unsigned int au_opts_plink(unsigned int mntflags)
24978+{
24979+#ifdef CONFIG_PROC_FS
24980+ return mntflags;
24981+#else
24982+ return mntflags & ~AuOpt_PLINK;
24983+#endif
24984+}
24985+
1facf9fc 24986+/* ---------------------------------------------------------------------- */
24987+
24988+/* policies to select one among multiple writable branches */
24989+enum {
24990+ AuWbrCreate_TDP, /* top down parent */
24991+ AuWbrCreate_RR, /* round robin */
24992+ AuWbrCreate_MFS, /* most free space */
24993+ AuWbrCreate_MFSV, /* mfs with seconds */
24994+ AuWbrCreate_MFSRR, /* mfs then rr */
24995+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
24996+ AuWbrCreate_PMFS, /* parent and mfs */
24997+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
24998+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
24999+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 25000+
25001+ AuWbrCreate_Def = AuWbrCreate_TDP
25002+};
25003+
25004+enum {
25005+ AuWbrCopyup_TDP, /* top down parent */
25006+ AuWbrCopyup_BUP, /* bottom up parent */
25007+ AuWbrCopyup_BU, /* bottom up */
25008+
25009+ AuWbrCopyup_Def = AuWbrCopyup_TDP
25010+};
25011+
25012+/* ---------------------------------------------------------------------- */
25013+
25014+struct au_opt_add {
25015+ aufs_bindex_t bindex;
25016+ char *pathname;
25017+ int perm;
25018+ struct path path;
25019+};
25020+
25021+struct au_opt_del {
25022+ char *pathname;
25023+ struct path h_path;
25024+};
25025+
25026+struct au_opt_mod {
25027+ char *path;
25028+ int perm;
25029+ struct dentry *h_root;
25030+};
25031+
25032+struct au_opt_xino {
25033+ char *path;
25034+ struct file *file;
25035+};
25036+
25037+struct au_opt_xino_itrunc {
25038+ aufs_bindex_t bindex;
25039+};
25040+
25041+struct au_opt_wbr_create {
25042+ int wbr_create;
25043+ int mfs_second;
25044+ unsigned long long mfsrr_watermark;
25045+};
25046+
25047+struct au_opt {
25048+ int type;
25049+ union {
25050+ struct au_opt_xino xino;
25051+ struct au_opt_xino_itrunc xino_itrunc;
25052+ struct au_opt_add add;
25053+ struct au_opt_del del;
25054+ struct au_opt_mod mod;
25055+ int dirwh;
25056+ int rdcache;
25057+ unsigned int rdblk;
25058+ unsigned int rdhash;
25059+ int udba;
25060+ struct au_opt_wbr_create wbr_create;
25061+ int wbr_copyup;
076b876e 25062+ unsigned int fhsm_second;
1facf9fc 25063+ };
25064+};
25065+
25066+/* opts flags */
25067+#define AuOpts_REMOUNT 1
027c5e7a
AM
25068+#define AuOpts_REFRESH (1 << 1)
25069+#define AuOpts_TRUNC_XIB (1 << 2)
25070+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 25071+#define AuOpts_REFRESH_IDOP (1 << 4)
1facf9fc 25072+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
25073+#define au_fset_opts(flags, name) \
25074+ do { (flags) |= AuOpts_##name; } while (0)
25075+#define au_fclr_opts(flags, name) \
25076+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 25077+
25078+struct au_opts {
25079+ struct au_opt *opt;
25080+ int max_opt;
25081+
25082+ unsigned int given_udba;
25083+ unsigned int flags;
25084+ unsigned long sb_flags;
25085+};
25086+
25087+/* ---------------------------------------------------------------------- */
25088+
7e9cd9fe 25089+/* opts.c */
076b876e 25090+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 25091+const char *au_optstr_udba(int udba);
25092+const char *au_optstr_wbr_copyup(int wbr_copyup);
25093+const char *au_optstr_wbr_create(int wbr_create);
25094+
25095+void au_opts_free(struct au_opts *opts);
25096+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
25097+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
25098+ unsigned int pending);
25099+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
25100+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
25101+
25102+unsigned int au_opt_udba(struct super_block *sb);
25103+
1facf9fc 25104+#endif /* __KERNEL__ */
25105+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
25106diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
25107--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
25108+++ linux/fs/aufs/plink.c 2016-08-17 18:01:06.161889951 +0200
25109@@ -0,0 +1,514 @@
1facf9fc 25110+/*
8cdd5066 25111+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 25112+ *
25113+ * This program, aufs is free software; you can redistribute it and/or modify
25114+ * it under the terms of the GNU General Public License as published by
25115+ * the Free Software Foundation; either version 2 of the License, or
25116+ * (at your option) any later version.
dece6358
AM
25117+ *
25118+ * This program is distributed in the hope that it will be useful,
25119+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25120+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25121+ * GNU General Public License for more details.
25122+ *
25123+ * You should have received a copy of the GNU General Public License
523b37e3 25124+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 25125+ */
25126+
25127+/*
25128+ * pseudo-link
25129+ */
25130+
25131+#include "aufs.h"
25132+
25133+/*
e49829fe 25134+ * the pseudo-link maintenance mode.
1facf9fc 25135+ * during a user process maintains the pseudo-links,
25136+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
25137+ *
25138+ * Flags
25139+ * NOPLM:
25140+ * For entry functions which will handle plink, and i_mutex is already held
25141+ * in VFS.
25142+ * They cannot wait and should return an error at once.
25143+ * Callers has to check the error.
25144+ * NOPLMW:
25145+ * For entry functions which will handle plink, but i_mutex is not held
25146+ * in VFS.
25147+ * They can wait the plink maintenance mode to finish.
25148+ *
25149+ * They behave like F_SETLK and F_SETLKW.
25150+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 25151+ */
e49829fe
JR
25152+
25153+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 25154+{
e49829fe
JR
25155+ int err;
25156+ pid_t pid, ppid;
f0c0a007 25157+ struct task_struct *parent, *prev;
e49829fe 25158+ struct au_sbinfo *sbi;
dece6358
AM
25159+
25160+ SiMustAnyLock(sb);
25161+
e49829fe
JR
25162+ err = 0;
25163+ if (!au_opt_test(au_mntflags(sb), PLINK))
25164+ goto out;
25165+
25166+ sbi = au_sbi(sb);
25167+ pid = sbi->si_plink_maint_pid;
25168+ if (!pid || pid == current->pid)
25169+ goto out;
25170+
25171+ /* todo: it highly depends upon /sbin/mount.aufs */
f0c0a007
AM
25172+ prev = NULL;
25173+ parent = current;
25174+ ppid = 0;
e49829fe 25175+ rcu_read_lock();
f0c0a007
AM
25176+ while (1) {
25177+ parent = rcu_dereference(parent->real_parent);
25178+ if (parent == prev)
25179+ break;
25180+ ppid = task_pid_vnr(parent);
25181+ if (pid == ppid) {
25182+ rcu_read_unlock();
25183+ goto out;
25184+ }
25185+ prev = parent;
25186+ }
e49829fe 25187+ rcu_read_unlock();
e49829fe
JR
25188+
25189+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
25190+ /* if there is no i_mutex lock in VFS, we don't need to wait */
25191+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
25192+ while (sbi->si_plink_maint_pid) {
25193+ si_read_unlock(sb);
25194+ /* gave up wake_up_bit() */
25195+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
25196+
25197+ if (au_ftest_lock(flags, FLUSH))
25198+ au_nwt_flush(&sbi->si_nowait);
25199+ si_noflush_read_lock(sb);
25200+ }
25201+ } else if (au_ftest_lock(flags, NOPLM)) {
25202+ AuDbg("ppid %d, pid %d\n", ppid, pid);
25203+ err = -EAGAIN;
25204+ }
25205+
25206+out:
25207+ return err;
4a4d8108
AM
25208+}
25209+
e49829fe 25210+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 25211+{
4a4d8108 25212+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 25213+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 25214+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 25215+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
25216+}
25217+
e49829fe 25218+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
25219+{
25220+ int err;
4a4d8108
AM
25221+ struct au_sbinfo *sbinfo;
25222+
25223+ err = 0;
4a4d8108
AM
25224+ sbinfo = au_sbi(sb);
25225+ /* make sure i am the only one in this fs */
e49829fe
JR
25226+ si_write_lock(sb, AuLock_FLUSH);
25227+ if (au_opt_test(au_mntflags(sb), PLINK)) {
25228+ spin_lock(&sbinfo->si_plink_maint_lock);
25229+ if (!sbinfo->si_plink_maint_pid)
25230+ sbinfo->si_plink_maint_pid = current->pid;
25231+ else
25232+ err = -EBUSY;
25233+ spin_unlock(&sbinfo->si_plink_maint_lock);
25234+ }
4a4d8108
AM
25235+ si_write_unlock(sb);
25236+
25237+ return err;
1facf9fc 25238+}
25239+
25240+/* ---------------------------------------------------------------------- */
25241+
1facf9fc 25242+#ifdef CONFIG_AUFS_DEBUG
25243+void au_plink_list(struct super_block *sb)
25244+{
86dc4139 25245+ int i;
1facf9fc 25246+ struct au_sbinfo *sbinfo;
86dc4139 25247+ struct hlist_head *plink_hlist;
5afbbe0d 25248+ struct au_icntnr *icntnr;
1facf9fc 25249+
dece6358
AM
25250+ SiMustAnyLock(sb);
25251+
1facf9fc 25252+ sbinfo = au_sbi(sb);
25253+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25254+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25255+
86dc4139
AM
25256+ for (i = 0; i < AuPlink_NHASH; i++) {
25257+ plink_hlist = &sbinfo->si_plink[i].head;
25258+ rcu_read_lock();
5afbbe0d
AM
25259+ hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25260+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
86dc4139
AM
25261+ rcu_read_unlock();
25262+ }
1facf9fc 25263+}
25264+#endif
25265+
25266+/* is the inode pseudo-linked? */
25267+int au_plink_test(struct inode *inode)
25268+{
86dc4139 25269+ int found, i;
1facf9fc 25270+ struct au_sbinfo *sbinfo;
86dc4139 25271+ struct hlist_head *plink_hlist;
5afbbe0d 25272+ struct au_icntnr *icntnr;
1facf9fc 25273+
25274+ sbinfo = au_sbi(inode->i_sb);
dece6358 25275+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 25276+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 25277+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 25278+
25279+ found = 0;
86dc4139
AM
25280+ i = au_plink_hash(inode->i_ino);
25281+ plink_hlist = &sbinfo->si_plink[i].head;
4a4d8108 25282+ rcu_read_lock();
5afbbe0d
AM
25283+ hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25284+ if (&icntnr->vfs_inode == inode) {
1facf9fc 25285+ found = 1;
25286+ break;
25287+ }
4a4d8108 25288+ rcu_read_unlock();
1facf9fc 25289+ return found;
25290+}
25291+
25292+/* ---------------------------------------------------------------------- */
25293+
25294+/*
25295+ * generate a name for plink.
25296+ * the file will be stored under AUFS_WH_PLINKDIR.
25297+ */
25298+/* 20 is max digits length of ulong 64 */
25299+#define PLINK_NAME_LEN ((20 + 1) * 2)
25300+
25301+static int plink_name(char *name, int len, struct inode *inode,
25302+ aufs_bindex_t bindex)
25303+{
25304+ int rlen;
25305+ struct inode *h_inode;
25306+
25307+ h_inode = au_h_iptr(inode, bindex);
25308+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
25309+ return rlen;
25310+}
25311+
7f207e10
AM
25312+struct au_do_plink_lkup_args {
25313+ struct dentry **errp;
25314+ struct qstr *tgtname;
25315+ struct dentry *h_parent;
25316+ struct au_branch *br;
25317+};
25318+
25319+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
25320+ struct dentry *h_parent,
25321+ struct au_branch *br)
25322+{
25323+ struct dentry *h_dentry;
febd17d6 25324+ struct inode *h_inode;
7f207e10 25325+
febd17d6
JR
25326+ h_inode = d_inode(h_parent);
25327+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
b4510431 25328+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
febd17d6 25329+ inode_unlock(h_inode);
7f207e10
AM
25330+ return h_dentry;
25331+}
25332+
25333+static void au_call_do_plink_lkup(void *args)
25334+{
25335+ struct au_do_plink_lkup_args *a = args;
25336+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
25337+}
25338+
1facf9fc 25339+/* lookup the plink-ed @inode under the branch at @bindex */
25340+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
25341+{
25342+ struct dentry *h_dentry, *h_parent;
25343+ struct au_branch *br;
7f207e10 25344+ int wkq_err;
1facf9fc 25345+ char a[PLINK_NAME_LEN];
0c3ec466 25346+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 25347+
e49829fe
JR
25348+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25349+
1facf9fc 25350+ br = au_sbr(inode->i_sb, bindex);
25351+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 25352+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25353+
2dfbb274 25354+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
25355+ struct au_do_plink_lkup_args args = {
25356+ .errp = &h_dentry,
25357+ .tgtname = &tgtname,
25358+ .h_parent = h_parent,
25359+ .br = br
25360+ };
25361+
25362+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
25363+ if (unlikely(wkq_err))
25364+ h_dentry = ERR_PTR(wkq_err);
25365+ } else
25366+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
25367+
1facf9fc 25368+ return h_dentry;
25369+}
25370+
25371+/* create a pseudo-link */
25372+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
25373+ struct dentry *h_dentry, struct au_branch *br)
25374+{
25375+ int err;
25376+ struct path h_path = {
86dc4139 25377+ .mnt = au_br_mnt(br)
1facf9fc 25378+ };
523b37e3 25379+ struct inode *h_dir, *delegated;
1facf9fc 25380+
5527c038 25381+ h_dir = d_inode(h_parent);
febd17d6 25382+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 25383+again:
b4510431 25384+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 25385+ err = PTR_ERR(h_path.dentry);
25386+ if (IS_ERR(h_path.dentry))
25387+ goto out;
25388+
25389+ err = 0;
25390+ /* wh.plink dir is not monitored */
7f207e10 25391+ /* todo: is it really safe? */
5527c038
JR
25392+ if (d_is_positive(h_path.dentry)
25393+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
25394+ delegated = NULL;
25395+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
25396+ if (unlikely(err == -EWOULDBLOCK)) {
25397+ pr_warn("cannot retry for NFSv4 delegation"
25398+ " for an internal unlink\n");
25399+ iput(delegated);
25400+ }
1facf9fc 25401+ dput(h_path.dentry);
25402+ h_path.dentry = NULL;
25403+ if (!err)
25404+ goto again;
25405+ }
5527c038 25406+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
25407+ delegated = NULL;
25408+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
25409+ if (unlikely(err == -EWOULDBLOCK)) {
25410+ pr_warn("cannot retry for NFSv4 delegation"
25411+ " for an internal link\n");
25412+ iput(delegated);
25413+ }
25414+ }
1facf9fc 25415+ dput(h_path.dentry);
25416+
4f0767ce 25417+out:
febd17d6 25418+ inode_unlock(h_dir);
1facf9fc 25419+ return err;
25420+}
25421+
25422+struct do_whplink_args {
25423+ int *errp;
25424+ struct qstr *tgt;
25425+ struct dentry *h_parent;
25426+ struct dentry *h_dentry;
25427+ struct au_branch *br;
25428+};
25429+
25430+static void call_do_whplink(void *args)
25431+{
25432+ struct do_whplink_args *a = args;
25433+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
25434+}
25435+
25436+static int whplink(struct dentry *h_dentry, struct inode *inode,
25437+ aufs_bindex_t bindex, struct au_branch *br)
25438+{
25439+ int err, wkq_err;
25440+ struct au_wbr *wbr;
25441+ struct dentry *h_parent;
1facf9fc 25442+ char a[PLINK_NAME_LEN];
0c3ec466 25443+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 25444+
25445+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
25446+ h_parent = wbr->wbr_plink;
1facf9fc 25447+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25448+
25449+ /* always superio. */
2dfbb274 25450+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 25451+ struct do_whplink_args args = {
25452+ .errp = &err,
25453+ .tgt = &tgtname,
25454+ .h_parent = h_parent,
25455+ .h_dentry = h_dentry,
25456+ .br = br
25457+ };
25458+ wkq_err = au_wkq_wait(call_do_whplink, &args);
25459+ if (unlikely(wkq_err))
25460+ err = wkq_err;
25461+ } else
25462+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 25463+
25464+ return err;
25465+}
25466+
1facf9fc 25467+/*
25468+ * create a new pseudo-link for @h_dentry on @bindex.
25469+ * the linked inode is held in aufs @inode.
25470+ */
25471+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25472+ struct dentry *h_dentry)
25473+{
25474+ struct super_block *sb;
25475+ struct au_sbinfo *sbinfo;
86dc4139 25476+ struct hlist_head *plink_hlist;
5afbbe0d 25477+ struct au_icntnr *icntnr;
86dc4139
AM
25478+ struct au_sphlhead *sphl;
25479+ int found, err, cnt, i;
1facf9fc 25480+
25481+ sb = inode->i_sb;
25482+ sbinfo = au_sbi(sb);
25483+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25484+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25485+
86dc4139 25486+ found = au_plink_test(inode);
4a4d8108 25487+ if (found)
1facf9fc 25488+ return;
4a4d8108 25489+
86dc4139
AM
25490+ i = au_plink_hash(inode->i_ino);
25491+ sphl = sbinfo->si_plink + i;
25492+ plink_hlist = &sphl->head;
5afbbe0d 25493+ au_igrab(inode);
1facf9fc 25494+
86dc4139 25495+ spin_lock(&sphl->spin);
5afbbe0d
AM
25496+ hlist_for_each_entry(icntnr, plink_hlist, plink) {
25497+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
25498+ found = 1;
25499+ break;
25500+ }
1facf9fc 25501+ }
5afbbe0d
AM
25502+ if (!found) {
25503+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
25504+ hlist_add_head_rcu(&icntnr->plink, plink_hlist);
25505+ }
86dc4139 25506+ spin_unlock(&sphl->spin);
4a4d8108 25507+ if (!found) {
86dc4139
AM
25508+ cnt = au_sphl_count(sphl);
25509+#define msg "unexpectedly unblanced or too many pseudo-links"
25510+ if (cnt > AUFS_PLINK_WARN)
25511+ AuWarn1(msg ", %d\n", cnt);
25512+#undef msg
1facf9fc 25513+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
25514+ if (unlikely(err)) {
25515+ pr_warn("err %d, damaged pseudo link.\n", err);
25516+ au_sphl_del_rcu(&icntnr->plink, sphl);
25517+ iput(&icntnr->vfs_inode);
4a4d8108 25518+ }
5afbbe0d
AM
25519+ } else
25520+ iput(&icntnr->vfs_inode);
1facf9fc 25521+}
25522+
25523+/* free all plinks */
e49829fe 25524+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 25525+{
86dc4139 25526+ int i, warned;
1facf9fc 25527+ struct au_sbinfo *sbinfo;
86dc4139
AM
25528+ struct hlist_head *plink_hlist;
25529+ struct hlist_node *tmp;
5afbbe0d 25530+ struct au_icntnr *icntnr;
1facf9fc 25531+
dece6358
AM
25532+ SiMustWriteLock(sb);
25533+
1facf9fc 25534+ sbinfo = au_sbi(sb);
25535+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25536+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25537+
1facf9fc 25538+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
25539+ warned = 0;
25540+ for (i = 0; i < AuPlink_NHASH; i++) {
25541+ plink_hlist = &sbinfo->si_plink[i].head;
25542+ if (!warned && verbose && !hlist_empty(plink_hlist)) {
25543+ pr_warn("pseudo-link is not flushed");
25544+ warned = 1;
25545+ }
5afbbe0d
AM
25546+ hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink)
25547+ iput(&icntnr->vfs_inode);
86dc4139
AM
25548+ INIT_HLIST_HEAD(plink_hlist);
25549+ }
1facf9fc 25550+}
25551+
e49829fe
JR
25552+void au_plink_clean(struct super_block *sb, int verbose)
25553+{
25554+ struct dentry *root;
25555+
25556+ root = sb->s_root;
25557+ aufs_write_lock(root);
25558+ if (au_opt_test(au_mntflags(sb), PLINK))
25559+ au_plink_put(sb, verbose);
25560+ aufs_write_unlock(root);
25561+}
25562+
86dc4139
AM
25563+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25564+{
25565+ int do_put;
5afbbe0d 25566+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
25567+
25568+ do_put = 0;
5afbbe0d
AM
25569+ btop = au_ibtop(inode);
25570+ bbot = au_ibbot(inode);
25571+ if (btop >= 0) {
25572+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
25573+ if (!au_h_iptr(inode, bindex)
25574+ || au_ii_br_id(inode, bindex) != br_id)
25575+ continue;
25576+ au_set_h_iptr(inode, bindex, NULL, 0);
25577+ do_put = 1;
25578+ break;
25579+ }
25580+ if (do_put)
5afbbe0d 25581+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
25582+ if (au_h_iptr(inode, bindex)) {
25583+ do_put = 0;
25584+ break;
25585+ }
25586+ } else
25587+ do_put = 1;
25588+
25589+ return do_put;
25590+}
25591+
1facf9fc 25592+/* free the plinks on a branch specified by @br_id */
25593+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25594+{
25595+ struct au_sbinfo *sbinfo;
86dc4139
AM
25596+ struct hlist_head *plink_hlist;
25597+ struct hlist_node *tmp;
5afbbe0d 25598+ struct au_icntnr *icntnr;
1facf9fc 25599+ struct inode *inode;
86dc4139 25600+ int i, do_put;
1facf9fc 25601+
dece6358
AM
25602+ SiMustWriteLock(sb);
25603+
1facf9fc 25604+ sbinfo = au_sbi(sb);
25605+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25606+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25607+
1facf9fc 25608+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
25609+ for (i = 0; i < AuPlink_NHASH; i++) {
25610+ plink_hlist = &sbinfo->si_plink[i].head;
5afbbe0d
AM
25611+ hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink) {
25612+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
25613+ ii_write_lock_child(inode);
25614+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d
AM
25615+ if (do_put) {
25616+ hlist_del(&icntnr->plink);
25617+ iput(inode);
25618+ }
86dc4139
AM
25619+ ii_write_unlock(inode);
25620+ iput(inode);
dece6358 25621+ }
dece6358
AM
25622+ }
25623+}
7f207e10
AM
25624diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25625--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 25626+++ linux/fs/aufs/poll.c 2016-07-25 19:05:34.817826663 +0200
b912730e 25627@@ -0,0 +1,52 @@
dece6358 25628+/*
8cdd5066 25629+ * Copyright (C) 2005-2016 Junjiro R. Okajima
dece6358
AM
25630+ *
25631+ * This program, aufs is free software; you can redistribute it and/or modify
25632+ * it under the terms of the GNU General Public License as published by
25633+ * the Free Software Foundation; either version 2 of the License, or
25634+ * (at your option) any later version.
25635+ *
25636+ * This program is distributed in the hope that it will be useful,
25637+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25638+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25639+ * GNU General Public License for more details.
25640+ *
25641+ * You should have received a copy of the GNU General Public License
523b37e3 25642+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
25643+ */
25644+
1308ab2a 25645+/*
25646+ * poll operation
25647+ * There is only one filesystem which implements ->poll operation, currently.
25648+ */
25649+
25650+#include "aufs.h"
25651+
25652+unsigned int aufs_poll(struct file *file, poll_table *wait)
25653+{
25654+ unsigned int mask;
25655+ int err;
25656+ struct file *h_file;
1308ab2a 25657+ struct super_block *sb;
25658+
25659+ /* We should pretend an error happened. */
25660+ mask = POLLERR /* | POLLIN | POLLOUT */;
b912730e 25661+ sb = file->f_path.dentry->d_sb;
e49829fe 25662+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e
AM
25663+
25664+ h_file = au_read_pre(file, /*keep_fi*/0);
25665+ err = PTR_ERR(h_file);
25666+ if (IS_ERR(h_file))
1308ab2a 25667+ goto out;
25668+
25669+ /* it is not an error if h_file has no operation */
25670+ mask = DEFAULT_POLLMASK;
523b37e3 25671+ if (h_file->f_op->poll)
1308ab2a 25672+ mask = h_file->f_op->poll(h_file, wait);
b912730e 25673+ fput(h_file); /* instead of au_read_post() */
1308ab2a 25674+
4f0767ce 25675+out:
1308ab2a 25676+ si_read_unlock(sb);
25677+ AuTraceErr((int)mask);
25678+ return mask;
25679+}
c1595e42
JR
25680diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25681--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 25682+++ linux/fs/aufs/posix_acl.c 2016-07-25 19:05:34.817826663 +0200
8cdd5066 25683@@ -0,0 +1,98 @@
c1595e42 25684+/*
8cdd5066 25685+ * Copyright (C) 2014-2016 Junjiro R. Okajima
c1595e42
JR
25686+ *
25687+ * This program, aufs is free software; you can redistribute it and/or modify
25688+ * it under the terms of the GNU General Public License as published by
25689+ * the Free Software Foundation; either version 2 of the License, or
25690+ * (at your option) any later version.
25691+ *
25692+ * This program is distributed in the hope that it will be useful,
25693+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25694+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25695+ * GNU General Public License for more details.
25696+ *
25697+ * You should have received a copy of the GNU General Public License
25698+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25699+ */
25700+
25701+/*
25702+ * posix acl operations
25703+ */
25704+
25705+#include <linux/fs.h>
c1595e42
JR
25706+#include "aufs.h"
25707+
25708+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25709+{
25710+ struct posix_acl *acl;
25711+ int err;
25712+ aufs_bindex_t bindex;
25713+ struct inode *h_inode;
25714+ struct super_block *sb;
25715+
25716+ acl = NULL;
25717+ sb = inode->i_sb;
25718+ si_read_lock(sb, AuLock_FLUSH);
25719+ ii_read_lock_child(inode);
25720+ if (!(sb->s_flags & MS_POSIXACL))
25721+ goto out;
25722+
5afbbe0d 25723+ bindex = au_ibtop(inode);
c1595e42
JR
25724+ h_inode = au_h_iptr(inode, bindex);
25725+ if (unlikely(!h_inode
25726+ || ((h_inode->i_mode & S_IFMT)
25727+ != (inode->i_mode & S_IFMT)))) {
25728+ err = au_busy_or_stale();
25729+ acl = ERR_PTR(err);
25730+ goto out;
25731+ }
25732+
25733+ /* always topmost only */
25734+ acl = get_acl(h_inode, type);
25735+
25736+out:
25737+ ii_read_unlock(inode);
25738+ si_read_unlock(sb);
25739+
25740+ AuTraceErrPtr(acl);
25741+ return acl;
25742+}
25743+
25744+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25745+{
25746+ int err;
25747+ ssize_t ssz;
25748+ struct dentry *dentry;
25749+ struct au_srxattr arg = {
25750+ .type = AU_ACL_SET,
25751+ .u.acl_set = {
25752+ .acl = acl,
25753+ .type = type
25754+ },
25755+ };
25756+
5afbbe0d
AM
25757+ IMustLock(inode);
25758+
c1595e42
JR
25759+ if (inode->i_ino == AUFS_ROOT_INO)
25760+ dentry = dget(inode->i_sb->s_root);
25761+ else {
25762+ dentry = d_find_alias(inode);
25763+ if (!dentry)
25764+ dentry = d_find_any_alias(inode);
25765+ if (!dentry) {
25766+ pr_warn("cannot handle this inode, "
25767+ "please report to aufs-users ML\n");
25768+ err = -ENOENT;
25769+ goto out;
25770+ }
25771+ }
25772+
5afbbe0d 25773+ ssz = au_srxattr(dentry, inode, &arg);
c1595e42
JR
25774+ dput(dentry);
25775+ err = ssz;
25776+ if (ssz >= 0)
25777+ err = 0;
25778+
25779+out:
c1595e42
JR
25780+ return err;
25781+}
7f207e10
AM
25782diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
25783--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 25784+++ linux/fs/aufs/procfs.c 2016-07-25 19:05:34.817826663 +0200
523b37e3 25785@@ -0,0 +1,169 @@
e49829fe 25786+/*
8cdd5066 25787+ * Copyright (C) 2010-2016 Junjiro R. Okajima
e49829fe
JR
25788+ *
25789+ * This program, aufs is free software; you can redistribute it and/or modify
25790+ * it under the terms of the GNU General Public License as published by
25791+ * the Free Software Foundation; either version 2 of the License, or
25792+ * (at your option) any later version.
25793+ *
25794+ * This program is distributed in the hope that it will be useful,
25795+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25796+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25797+ * GNU General Public License for more details.
25798+ *
25799+ * You should have received a copy of the GNU General Public License
523b37e3 25800+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
25801+ */
25802+
25803+/*
25804+ * procfs interfaces
25805+ */
25806+
25807+#include <linux/proc_fs.h>
25808+#include "aufs.h"
25809+
25810+static int au_procfs_plm_release(struct inode *inode, struct file *file)
25811+{
25812+ struct au_sbinfo *sbinfo;
25813+
25814+ sbinfo = file->private_data;
25815+ if (sbinfo) {
25816+ au_plink_maint_leave(sbinfo);
25817+ kobject_put(&sbinfo->si_kobj);
25818+ }
25819+
25820+ return 0;
25821+}
25822+
25823+static void au_procfs_plm_write_clean(struct file *file)
25824+{
25825+ struct au_sbinfo *sbinfo;
25826+
25827+ sbinfo = file->private_data;
25828+ if (sbinfo)
25829+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
25830+}
25831+
25832+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
25833+{
25834+ int err;
25835+ struct super_block *sb;
25836+ struct au_sbinfo *sbinfo;
25837+
25838+ err = -EBUSY;
25839+ if (unlikely(file->private_data))
25840+ goto out;
25841+
25842+ sb = NULL;
53392da6 25843+ /* don't use au_sbilist_lock() here */
e49829fe 25844+ spin_lock(&au_sbilist.spin);
5afbbe0d 25845+ hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
e49829fe
JR
25846+ if (id == sysaufs_si_id(sbinfo)) {
25847+ kobject_get(&sbinfo->si_kobj);
25848+ sb = sbinfo->si_sb;
25849+ break;
25850+ }
25851+ spin_unlock(&au_sbilist.spin);
25852+
25853+ err = -EINVAL;
25854+ if (unlikely(!sb))
25855+ goto out;
25856+
25857+ err = au_plink_maint_enter(sb);
25858+ if (!err)
25859+ /* keep kobject_get() */
25860+ file->private_data = sbinfo;
25861+ else
25862+ kobject_put(&sbinfo->si_kobj);
25863+out:
25864+ return err;
25865+}
25866+
25867+/*
25868+ * Accept a valid "si=xxxx" only.
25869+ * Once it is accepted successfully, accept "clean" too.
25870+ */
25871+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
25872+ size_t count, loff_t *ppos)
25873+{
25874+ ssize_t err;
25875+ unsigned long id;
25876+ /* last newline is allowed */
25877+ char buf[3 + sizeof(unsigned long) * 2 + 1];
25878+
25879+ err = -EACCES;
25880+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25881+ goto out;
25882+
25883+ err = -EINVAL;
25884+ if (unlikely(count > sizeof(buf)))
25885+ goto out;
25886+
25887+ err = copy_from_user(buf, ubuf, count);
25888+ if (unlikely(err)) {
25889+ err = -EFAULT;
25890+ goto out;
25891+ }
25892+ buf[count] = 0;
25893+
25894+ err = -EINVAL;
25895+ if (!strcmp("clean", buf)) {
25896+ au_procfs_plm_write_clean(file);
25897+ goto out_success;
25898+ } else if (unlikely(strncmp("si=", buf, 3)))
25899+ goto out;
25900+
9dbd164d 25901+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
25902+ if (unlikely(err))
25903+ goto out;
25904+
25905+ err = au_procfs_plm_write_si(file, id);
25906+ if (unlikely(err))
25907+ goto out;
25908+
25909+out_success:
25910+ err = count; /* success */
25911+out:
25912+ return err;
25913+}
25914+
25915+static const struct file_operations au_procfs_plm_fop = {
25916+ .write = au_procfs_plm_write,
25917+ .release = au_procfs_plm_release,
25918+ .owner = THIS_MODULE
25919+};
25920+
25921+/* ---------------------------------------------------------------------- */
25922+
25923+static struct proc_dir_entry *au_procfs_dir;
25924+
25925+void au_procfs_fin(void)
25926+{
25927+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25928+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25929+}
25930+
25931+int __init au_procfs_init(void)
25932+{
25933+ int err;
25934+ struct proc_dir_entry *entry;
25935+
25936+ err = -ENOMEM;
25937+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25938+ if (unlikely(!au_procfs_dir))
25939+ goto out;
25940+
25941+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25942+ au_procfs_dir, &au_procfs_plm_fop);
25943+ if (unlikely(!entry))
25944+ goto out_dir;
25945+
25946+ err = 0;
25947+ goto out; /* success */
25948+
25949+
25950+out_dir:
25951+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25952+out:
25953+ return err;
25954+}
7f207e10
AM
25955diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25956--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
25957+++ linux/fs/aufs/rdu.c 2016-07-25 19:05:34.817826663 +0200
25958@@ -0,0 +1,381 @@
1308ab2a 25959+/*
8cdd5066 25960+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1308ab2a 25961+ *
25962+ * This program, aufs is free software; you can redistribute it and/or modify
25963+ * it under the terms of the GNU General Public License as published by
25964+ * the Free Software Foundation; either version 2 of the License, or
25965+ * (at your option) any later version.
25966+ *
25967+ * This program is distributed in the hope that it will be useful,
25968+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25969+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25970+ * GNU General Public License for more details.
25971+ *
25972+ * You should have received a copy of the GNU General Public License
523b37e3 25973+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 25974+ */
25975+
25976+/*
25977+ * readdir in userspace.
25978+ */
25979+
b752ccd1 25980+#include <linux/compat.h>
4a4d8108 25981+#include <linux/fs_stack.h>
1308ab2a 25982+#include <linux/security.h>
1308ab2a 25983+#include "aufs.h"
25984+
25985+/* bits for struct aufs_rdu.flags */
25986+#define AuRdu_CALLED 1
25987+#define AuRdu_CONT (1 << 1)
25988+#define AuRdu_FULL (1 << 2)
25989+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
25990+#define au_fset_rdu(flags, name) \
25991+ do { (flags) |= AuRdu_##name; } while (0)
25992+#define au_fclr_rdu(flags, name) \
25993+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 25994+
25995+struct au_rdu_arg {
392086de 25996+ struct dir_context ctx;
1308ab2a 25997+ struct aufs_rdu *rdu;
25998+ union au_rdu_ent_ul ent;
25999+ unsigned long end;
26000+
26001+ struct super_block *sb;
26002+ int err;
26003+};
26004+
392086de 26005+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 26006+ loff_t offset, u64 h_ino, unsigned int d_type)
26007+{
26008+ int err, len;
392086de 26009+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 26010+ struct aufs_rdu *rdu = arg->rdu;
26011+ struct au_rdu_ent ent;
26012+
26013+ err = 0;
26014+ arg->err = 0;
26015+ au_fset_rdu(rdu->cookie.flags, CALLED);
26016+ len = au_rdu_len(nlen);
26017+ if (arg->ent.ul + len < arg->end) {
26018+ ent.ino = h_ino;
26019+ ent.bindex = rdu->cookie.bindex;
26020+ ent.type = d_type;
26021+ ent.nlen = nlen;
4a4d8108
AM
26022+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
26023+ ent.type = DT_UNKNOWN;
1308ab2a 26024+
9dbd164d 26025+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 26026+ err = -EFAULT;
26027+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
26028+ goto out;
26029+ if (copy_to_user(arg->ent.e->name, name, nlen))
26030+ goto out;
26031+ /* the terminating NULL */
26032+ if (__put_user(0, arg->ent.e->name + nlen))
26033+ goto out;
26034+ err = 0;
26035+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
26036+ arg->ent.ul += len;
26037+ rdu->rent++;
26038+ } else {
26039+ err = -EFAULT;
26040+ au_fset_rdu(rdu->cookie.flags, FULL);
26041+ rdu->full = 1;
26042+ rdu->tail = arg->ent;
26043+ }
26044+
4f0767ce 26045+out:
1308ab2a 26046+ /* AuTraceErr(err); */
26047+ return err;
26048+}
26049+
26050+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
26051+{
26052+ int err;
26053+ loff_t offset;
26054+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
26055+
92d182d2 26056+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 26057+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
26058+ err = offset;
26059+ if (unlikely(offset != cookie->h_pos))
26060+ goto out;
26061+
26062+ err = 0;
26063+ do {
26064+ arg->err = 0;
26065+ au_fclr_rdu(cookie->flags, CALLED);
26066+ /* smp_mb(); */
392086de 26067+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 26068+ if (err >= 0)
26069+ err = arg->err;
26070+ } while (!err
26071+ && au_ftest_rdu(cookie->flags, CALLED)
26072+ && !au_ftest_rdu(cookie->flags, FULL));
26073+ cookie->h_pos = h_file->f_pos;
26074+
4f0767ce 26075+out:
1308ab2a 26076+ AuTraceErr(err);
26077+ return err;
26078+}
26079+
26080+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
26081+{
26082+ int err;
5afbbe0d 26083+ aufs_bindex_t bbot;
392086de
AM
26084+ struct au_rdu_arg arg = {
26085+ .ctx = {
2000de60 26086+ .actor = au_rdu_fill
392086de
AM
26087+ }
26088+ };
1308ab2a 26089+ struct dentry *dentry;
26090+ struct inode *inode;
26091+ struct file *h_file;
26092+ struct au_rdu_cookie *cookie = &rdu->cookie;
26093+
26094+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
26095+ if (unlikely(err)) {
26096+ err = -EFAULT;
26097+ AuTraceErr(err);
26098+ goto out;
26099+ }
26100+ rdu->rent = 0;
26101+ rdu->tail = rdu->ent;
26102+ rdu->full = 0;
26103+ arg.rdu = rdu;
26104+ arg.ent = rdu->ent;
26105+ arg.end = arg.ent.ul;
26106+ arg.end += rdu->sz;
26107+
26108+ err = -ENOTDIR;
5afbbe0d 26109+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 26110+ goto out;
26111+
26112+ err = security_file_permission(file, MAY_READ);
26113+ AuTraceErr(err);
26114+ if (unlikely(err))
26115+ goto out;
26116+
2000de60 26117+ dentry = file->f_path.dentry;
5527c038 26118+ inode = d_inode(dentry);
5afbbe0d 26119+ inode_lock_shared(inode);
1308ab2a 26120+
26121+ arg.sb = inode->i_sb;
e49829fe
JR
26122+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
26123+ if (unlikely(err))
26124+ goto out_mtx;
027c5e7a
AM
26125+ err = au_alive_dir(dentry);
26126+ if (unlikely(err))
26127+ goto out_si;
e49829fe 26128+ /* todo: reval? */
1308ab2a 26129+ fi_read_lock(file);
26130+
26131+ err = -EAGAIN;
26132+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
26133+ && cookie->generation != au_figen(file)))
26134+ goto out_unlock;
26135+
26136+ err = 0;
26137+ if (!rdu->blk) {
26138+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
26139+ if (!rdu->blk)
26140+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
26141+ }
5afbbe0d
AM
26142+ bbot = au_fbtop(file);
26143+ if (cookie->bindex < bbot)
26144+ cookie->bindex = bbot;
26145+ bbot = au_fbbot_dir(file);
26146+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
26147+ for (; !err && cookie->bindex <= bbot;
1308ab2a 26148+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 26149+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 26150+ if (!h_file)
26151+ continue;
26152+
26153+ au_fclr_rdu(cookie->flags, FULL);
26154+ err = au_rdu_do(h_file, &arg);
26155+ AuTraceErr(err);
26156+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
26157+ break;
26158+ }
26159+ AuDbg("rent %llu\n", rdu->rent);
26160+
26161+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
26162+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
26163+ au_fset_rdu(cookie->flags, CONT);
26164+ cookie->generation = au_figen(file);
26165+ }
26166+
26167+ ii_read_lock_child(inode);
5afbbe0d 26168+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 26169+ ii_read_unlock(inode);
26170+
4f0767ce 26171+out_unlock:
1308ab2a 26172+ fi_read_unlock(file);
027c5e7a 26173+out_si:
1308ab2a 26174+ si_read_unlock(arg.sb);
4f0767ce 26175+out_mtx:
5afbbe0d 26176+ inode_unlock_shared(inode);
4f0767ce 26177+out:
1308ab2a 26178+ AuTraceErr(err);
26179+ return err;
26180+}
26181+
26182+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
26183+{
26184+ int err;
26185+ ino_t ino;
26186+ unsigned long long nent;
26187+ union au_rdu_ent_ul *u;
26188+ struct au_rdu_ent ent;
26189+ struct super_block *sb;
26190+
26191+ err = 0;
26192+ nent = rdu->nent;
26193+ u = &rdu->ent;
2000de60 26194+ sb = file->f_path.dentry->d_sb;
1308ab2a 26195+ si_read_lock(sb, AuLock_FLUSH);
26196+ while (nent-- > 0) {
9dbd164d 26197+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 26198+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
26199+ if (!err)
26200+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 26201+ if (unlikely(err)) {
26202+ err = -EFAULT;
26203+ AuTraceErr(err);
26204+ break;
26205+ }
26206+
26207+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
26208+ if (!ent.wh)
26209+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
26210+ else
26211+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
26212+ &ino);
26213+ if (unlikely(err)) {
26214+ AuTraceErr(err);
26215+ break;
26216+ }
26217+
26218+ err = __put_user(ino, &u->e->ino);
26219+ if (unlikely(err)) {
26220+ err = -EFAULT;
26221+ AuTraceErr(err);
26222+ break;
26223+ }
26224+ u->ul += au_rdu_len(ent.nlen);
26225+ }
26226+ si_read_unlock(sb);
26227+
26228+ return err;
26229+}
26230+
26231+/* ---------------------------------------------------------------------- */
26232+
26233+static int au_rdu_verify(struct aufs_rdu *rdu)
26234+{
b752ccd1 26235+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 26236+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 26237+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 26238+ rdu->blk,
26239+ rdu->rent, rdu->shwh, rdu->full,
26240+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
26241+ rdu->cookie.generation);
dece6358 26242+
b752ccd1 26243+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 26244+ return 0;
dece6358 26245+
b752ccd1
AM
26246+ AuDbg("%u:%u\n",
26247+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 26248+ return -EINVAL;
26249+}
26250+
26251+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 26252+{
1308ab2a 26253+ long err, e;
26254+ struct aufs_rdu rdu;
26255+ void __user *p = (void __user *)arg;
dece6358 26256+
1308ab2a 26257+ err = copy_from_user(&rdu, p, sizeof(rdu));
26258+ if (unlikely(err)) {
26259+ err = -EFAULT;
26260+ AuTraceErr(err);
26261+ goto out;
26262+ }
26263+ err = au_rdu_verify(&rdu);
dece6358
AM
26264+ if (unlikely(err))
26265+ goto out;
26266+
1308ab2a 26267+ switch (cmd) {
26268+ case AUFS_CTL_RDU:
26269+ err = au_rdu(file, &rdu);
26270+ if (unlikely(err))
26271+ break;
dece6358 26272+
1308ab2a 26273+ e = copy_to_user(p, &rdu, sizeof(rdu));
26274+ if (unlikely(e)) {
26275+ err = -EFAULT;
26276+ AuTraceErr(err);
26277+ }
26278+ break;
26279+ case AUFS_CTL_RDU_INO:
26280+ err = au_rdu_ino(file, &rdu);
26281+ break;
26282+
26283+ default:
4a4d8108 26284+ /* err = -ENOTTY; */
1308ab2a 26285+ err = -EINVAL;
26286+ }
dece6358 26287+
4f0767ce 26288+out:
1308ab2a 26289+ AuTraceErr(err);
26290+ return err;
1facf9fc 26291+}
b752ccd1
AM
26292+
26293+#ifdef CONFIG_COMPAT
26294+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26295+{
26296+ long err, e;
26297+ struct aufs_rdu rdu;
26298+ void __user *p = compat_ptr(arg);
26299+
26300+ /* todo: get_user()? */
26301+ err = copy_from_user(&rdu, p, sizeof(rdu));
26302+ if (unlikely(err)) {
26303+ err = -EFAULT;
26304+ AuTraceErr(err);
26305+ goto out;
26306+ }
26307+ rdu.ent.e = compat_ptr(rdu.ent.ul);
26308+ err = au_rdu_verify(&rdu);
26309+ if (unlikely(err))
26310+ goto out;
26311+
26312+ switch (cmd) {
26313+ case AUFS_CTL_RDU:
26314+ err = au_rdu(file, &rdu);
26315+ if (unlikely(err))
26316+ break;
26317+
26318+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
26319+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
26320+ e = copy_to_user(p, &rdu, sizeof(rdu));
26321+ if (unlikely(e)) {
26322+ err = -EFAULT;
26323+ AuTraceErr(err);
26324+ }
26325+ break;
26326+ case AUFS_CTL_RDU_INO:
26327+ err = au_rdu_ino(file, &rdu);
26328+ break;
26329+
26330+ default:
26331+ /* err = -ENOTTY; */
26332+ err = -EINVAL;
26333+ }
26334+
4f0767ce 26335+out:
b752ccd1
AM
26336+ AuTraceErr(err);
26337+ return err;
26338+}
26339+#endif
7f207e10
AM
26340diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
26341--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
26342+++ linux/fs/aufs/rwsem.h 2016-07-25 19:05:34.817826663 +0200
26343@@ -0,0 +1,198 @@
1facf9fc 26344+/*
8cdd5066 26345+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26346+ *
26347+ * This program, aufs is free software; you can redistribute it and/or modify
26348+ * it under the terms of the GNU General Public License as published by
26349+ * the Free Software Foundation; either version 2 of the License, or
26350+ * (at your option) any later version.
dece6358
AM
26351+ *
26352+ * This program is distributed in the hope that it will be useful,
26353+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26354+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26355+ * GNU General Public License for more details.
26356+ *
26357+ * You should have received a copy of the GNU General Public License
523b37e3 26358+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26359+ */
26360+
26361+/*
26362+ * simple read-write semaphore wrappers
26363+ */
26364+
26365+#ifndef __AUFS_RWSEM_H__
26366+#define __AUFS_RWSEM_H__
26367+
26368+#ifdef __KERNEL__
26369+
4a4d8108 26370+#include "debug.h"
dece6358
AM
26371+
26372+struct au_rwsem {
26373+ struct rw_semaphore rwsem;
26374+#ifdef CONFIG_AUFS_DEBUG
26375+ /* just for debugging, not almighty counter */
26376+ atomic_t rcnt, wcnt;
26377+#endif
26378+};
26379+
5afbbe0d
AM
26380+#ifdef CONFIG_LOCKDEP
26381+#define au_lockdep_set_name(rw) \
26382+ lockdep_set_class_and_name(&(rw)->rwsem, \
26383+ /*original key*/(rw)->rwsem.dep_map.key, \
26384+ /*name*/#rw)
26385+#else
26386+#define au_lockdep_set_name(rw) do {} while (0)
26387+#endif
26388+
dece6358
AM
26389+#ifdef CONFIG_AUFS_DEBUG
26390+#define AuDbgCntInit(rw) do { \
26391+ atomic_set(&(rw)->rcnt, 0); \
26392+ atomic_set(&(rw)->wcnt, 0); \
26393+ smp_mb(); /* atomic set */ \
26394+} while (0)
26395+
5afbbe0d
AM
26396+#define AuDbgCnt(rw, cnt) atomic_read(&(rw)->cnt)
26397+#define AuDbgCntInc(rw, cnt) atomic_inc(&(rw)->cnt)
26398+#define AuDbgCntDec(rw, cnt) WARN_ON(atomic_dec_return(&(rw)->cnt) < 0)
26399+#define AuDbgRcntInc(rw) AuDbgCntInc(rw, rcnt)
26400+#define AuDbgRcntDec(rw) AuDbgCntDec(rw, rcnt)
26401+#define AuDbgWcntInc(rw) AuDbgCntInc(rw, wcnt)
26402+#define AuDbgWcntDec(rw) AuDbgCntDec(rw, wcnt)
dece6358 26403+#else
5afbbe0d 26404+#define AuDbgCnt(rw, cnt) 0
dece6358
AM
26405+#define AuDbgCntInit(rw) do {} while (0)
26406+#define AuDbgRcntInc(rw) do {} while (0)
26407+#define AuDbgRcntDec(rw) do {} while (0)
26408+#define AuDbgWcntInc(rw) do {} while (0)
26409+#define AuDbgWcntDec(rw) do {} while (0)
26410+#endif /* CONFIG_AUFS_DEBUG */
26411+
26412+/* to debug easier, do not make them inlined functions */
5afbbe0d 26413+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(&(rw)->rwsem))
dece6358 26414+/* rwsem_is_locked() is unusable */
5afbbe0d
AM
26415+#define AuRwMustReadLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0)
26416+#define AuRwMustWriteLock(rw) AuDebugOn(AuDbgCnt(rw, wcnt) <= 0)
26417+#define AuRwMustAnyLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0 \
26418+ && AuDbgCnt(rw, wcnt) <= 0)
26419+#define AuRwDestroy(rw) AuDebugOn(AuDbgCnt(rw, rcnt) \
26420+ || AuDbgCnt(rw, wcnt))
26421+
26422+#define au_rw_init(rw) do { \
26423+ AuDbgCntInit(rw); \
26424+ init_rwsem(&(rw)->rwsem); \
26425+ au_lockdep_set_name(rw); \
26426+ } while (0)
dece6358 26427+
5afbbe0d
AM
26428+#define au_rw_init_wlock(rw) do { \
26429+ au_rw_init(rw); \
26430+ down_write(&(rw)->rwsem); \
26431+ AuDbgWcntInc(rw); \
26432+ } while (0)
dece6358 26433+
5afbbe0d
AM
26434+#define au_rw_init_wlock_nested(rw, lsc) do { \
26435+ au_rw_init(rw); \
26436+ down_write_nested(&(rw)->rwsem, lsc); \
26437+ AuDbgWcntInc(rw); \
26438+ } while (0)
dece6358
AM
26439+
26440+static inline void au_rw_read_lock(struct au_rwsem *rw)
26441+{
26442+ down_read(&rw->rwsem);
26443+ AuDbgRcntInc(rw);
26444+}
26445+
26446+static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26447+{
26448+ down_read_nested(&rw->rwsem, lsc);
26449+ AuDbgRcntInc(rw);
26450+}
26451+
26452+static inline void au_rw_read_unlock(struct au_rwsem *rw)
26453+{
26454+ AuRwMustReadLock(rw);
26455+ AuDbgRcntDec(rw);
26456+ up_read(&rw->rwsem);
26457+}
26458+
26459+static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26460+{
26461+ AuRwMustWriteLock(rw);
26462+ AuDbgRcntInc(rw);
26463+ AuDbgWcntDec(rw);
26464+ downgrade_write(&rw->rwsem);
26465+}
26466+
26467+static inline void au_rw_write_lock(struct au_rwsem *rw)
26468+{
26469+ down_write(&rw->rwsem);
26470+ AuDbgWcntInc(rw);
26471+}
26472+
26473+static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26474+ unsigned int lsc)
26475+{
26476+ down_write_nested(&rw->rwsem, lsc);
26477+ AuDbgWcntInc(rw);
26478+}
1facf9fc 26479+
dece6358
AM
26480+static inline void au_rw_write_unlock(struct au_rwsem *rw)
26481+{
26482+ AuRwMustWriteLock(rw);
26483+ AuDbgWcntDec(rw);
26484+ up_write(&rw->rwsem);
26485+}
26486+
26487+/* why is not _nested version defined */
26488+static inline int au_rw_read_trylock(struct au_rwsem *rw)
26489+{
076b876e
AM
26490+ int ret;
26491+
26492+ ret = down_read_trylock(&rw->rwsem);
dece6358
AM
26493+ if (ret)
26494+ AuDbgRcntInc(rw);
26495+ return ret;
26496+}
26497+
26498+static inline int au_rw_write_trylock(struct au_rwsem *rw)
26499+{
076b876e
AM
26500+ int ret;
26501+
26502+ ret = down_write_trylock(&rw->rwsem);
dece6358
AM
26503+ if (ret)
26504+ AuDbgWcntInc(rw);
26505+ return ret;
26506+}
26507+
5afbbe0d 26508+#undef AuDbgCntDec
dece6358
AM
26509+#undef AuDbgRcntInc
26510+#undef AuDbgRcntDec
dece6358 26511+#undef AuDbgWcntDec
1facf9fc 26512+
26513+#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26514+static inline void prefix##_read_lock(param) \
dece6358 26515+{ au_rw_read_lock(rwsem); } \
1facf9fc 26516+static inline void prefix##_write_lock(param) \
dece6358 26517+{ au_rw_write_lock(rwsem); } \
1facf9fc 26518+static inline int prefix##_read_trylock(param) \
dece6358 26519+{ return au_rw_read_trylock(rwsem); } \
1facf9fc 26520+static inline int prefix##_write_trylock(param) \
dece6358 26521+{ return au_rw_write_trylock(rwsem); }
1facf9fc 26522+/* why is not _nested version defined */
26523+/* static inline void prefix##_read_trylock_nested(param, lsc)
dece6358 26524+{ au_rw_read_trylock_nested(rwsem, lsc)); }
1facf9fc 26525+static inline void prefix##_write_trylock_nestd(param, lsc)
dece6358 26526+{ au_rw_write_trylock_nested(rwsem, lsc); } */
1facf9fc 26527+
26528+#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26529+static inline void prefix##_read_unlock(param) \
dece6358 26530+{ au_rw_read_unlock(rwsem); } \
1facf9fc 26531+static inline void prefix##_write_unlock(param) \
dece6358 26532+{ au_rw_write_unlock(rwsem); } \
1facf9fc 26533+static inline void prefix##_downgrade_lock(param) \
dece6358 26534+{ au_rw_dgrade_lock(rwsem); }
1facf9fc 26535+
26536+#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26537+ AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26538+ AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26539+
26540+#endif /* __KERNEL__ */
26541+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
26542diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26543--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
26544+++ linux/fs/aufs/sbinfo.c 2016-08-17 18:01:06.161889951 +0200
26545@@ -0,0 +1,354 @@
1facf9fc 26546+/*
8cdd5066 26547+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26548+ *
26549+ * This program, aufs is free software; you can redistribute it and/or modify
26550+ * it under the terms of the GNU General Public License as published by
26551+ * the Free Software Foundation; either version 2 of the License, or
26552+ * (at your option) any later version.
dece6358
AM
26553+ *
26554+ * This program is distributed in the hope that it will be useful,
26555+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26556+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26557+ * GNU General Public License for more details.
26558+ *
26559+ * You should have received a copy of the GNU General Public License
523b37e3 26560+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26561+ */
26562+
26563+/*
26564+ * superblock private data
26565+ */
26566+
26567+#include "aufs.h"
26568+
26569+/*
26570+ * they are necessary regardless sysfs is disabled.
26571+ */
26572+void au_si_free(struct kobject *kobj)
26573+{
86dc4139 26574+ int i;
1facf9fc 26575+ struct au_sbinfo *sbinfo;
b752ccd1 26576+ char *locked __maybe_unused; /* debug only */
1facf9fc 26577+
26578+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139
AM
26579+ for (i = 0; i < AuPlink_NHASH; i++)
26580+ AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
f0c0a007 26581+ AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
5afbbe0d
AM
26582+
26583+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
26584+ percpu_counter_destroy(&sbinfo->si_ninodes);
26585+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
26586+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 26587+
e49829fe 26588+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 26589+ au_br_free(sbinfo);
e49829fe 26590+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 26591+
f0c0a007 26592+ au_delayed_kfree(sbinfo->si_branch);
febd17d6 26593+ for (i = 0; i < AU_NPIDMAP; i++)
f0c0a007
AM
26594+ if (sbinfo->au_si_pid.pid_bitmap[i])
26595+ au_delayed_kfree(sbinfo->au_si_pid.pid_bitmap[i]);
febd17d6 26596+ mutex_destroy(&sbinfo->au_si_pid.pid_mtx);
1facf9fc 26597+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 26598+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 26599+
f0c0a007 26600+ au_delayed_kfree(sbinfo);
1facf9fc 26601+}
26602+
26603+int au_si_alloc(struct super_block *sb)
26604+{
86dc4139 26605+ int err, i;
1facf9fc 26606+ struct au_sbinfo *sbinfo;
26607+
26608+ err = -ENOMEM;
4a4d8108 26609+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 26610+ if (unlikely(!sbinfo))
26611+ goto out;
26612+
26613+ /* will be reallocated separately */
26614+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26615+ if (unlikely(!sbinfo->si_branch))
febd17d6 26616+ goto out_sbinfo;
1facf9fc 26617+
1facf9fc 26618+ err = sysaufs_si_init(sbinfo);
26619+ if (unlikely(err))
26620+ goto out_br;
26621+
26622+ au_nwt_init(&sbinfo->si_nowait);
dece6358 26623+ au_rw_init_wlock(&sbinfo->si_rwsem);
febd17d6 26624+ mutex_init(&sbinfo->au_si_pid.pid_mtx);
b752ccd1 26625+
5afbbe0d
AM
26626+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
26627+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 26628+
5afbbe0d 26629+ sbinfo->si_bbot = -1;
392086de 26630+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 26631+
26632+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26633+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
26634+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26635+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 26636+
076b876e
AM
26637+ au_fhsm_init(sbinfo);
26638+
e49829fe 26639+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 26640+
392086de
AM
26641+ sbinfo->si_xino_jiffy = jiffies;
26642+ sbinfo->si_xino_expire
26643+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 26644+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 26645+ sbinfo->si_xino_brid = -1;
26646+ /* leave si_xib_last_pindex and si_xib_next_bit */
26647+
b912730e
AM
26648+ au_sphl_init(&sbinfo->si_aopen);
26649+
e49829fe 26650+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 26651+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26652+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26653+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26654+
86dc4139
AM
26655+ for (i = 0; i < AuPlink_NHASH; i++)
26656+ au_sphl_init(sbinfo->si_plink + i);
1facf9fc 26657+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 26658+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 26659+
523b37e3
AM
26660+ au_sphl_init(&sbinfo->si_files);
26661+
b95c5147
AM
26662+ /* with getattr by default */
26663+ sbinfo->si_iop_array = aufs_iop;
26664+
1facf9fc 26665+ /* leave other members for sysaufs and si_mnt. */
26666+ sbinfo->si_sb = sb;
26667+ sb->s_fs_info = sbinfo;
b752ccd1 26668+ si_pid_set(sb);
1facf9fc 26669+ return 0; /* success */
26670+
4f0767ce 26671+out_br:
f0c0a007 26672+ au_delayed_kfree(sbinfo->si_branch);
4f0767ce 26673+out_sbinfo:
f0c0a007 26674+ au_delayed_kfree(sbinfo);
4f0767ce 26675+out:
1facf9fc 26676+ return err;
26677+}
26678+
26679+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
26680+{
26681+ int err, sz;
26682+ struct au_branch **brp;
26683+
dece6358
AM
26684+ AuRwMustWriteLock(&sbinfo->si_rwsem);
26685+
1facf9fc 26686+ err = -ENOMEM;
5afbbe0d 26687+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 26688+ if (unlikely(!sz))
26689+ sz = sizeof(*brp);
26690+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
26691+ if (brp) {
26692+ sbinfo->si_branch = brp;
26693+ err = 0;
26694+ }
26695+
26696+ return err;
26697+}
26698+
26699+/* ---------------------------------------------------------------------- */
26700+
26701+unsigned int au_sigen_inc(struct super_block *sb)
26702+{
26703+ unsigned int gen;
5527c038 26704+ struct inode *inode;
1facf9fc 26705+
dece6358
AM
26706+ SiMustWriteLock(sb);
26707+
1facf9fc 26708+ gen = ++au_sbi(sb)->si_generation;
26709+ au_update_digen(sb->s_root);
5527c038
JR
26710+ inode = d_inode(sb->s_root);
26711+ au_update_iigen(inode, /*half*/0);
26712+ inode->i_version++;
1facf9fc 26713+ return gen;
26714+}
26715+
26716+aufs_bindex_t au_new_br_id(struct super_block *sb)
26717+{
26718+ aufs_bindex_t br_id;
26719+ int i;
26720+ struct au_sbinfo *sbinfo;
26721+
dece6358
AM
26722+ SiMustWriteLock(sb);
26723+
1facf9fc 26724+ sbinfo = au_sbi(sb);
26725+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26726+ br_id = ++sbinfo->si_last_br_id;
7f207e10 26727+ AuDebugOn(br_id < 0);
1facf9fc 26728+ if (br_id && au_br_index(sb, br_id) < 0)
26729+ return br_id;
26730+ }
26731+
26732+ return -1;
26733+}
26734+
26735+/* ---------------------------------------------------------------------- */
26736+
e49829fe
JR
26737+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26738+int si_read_lock(struct super_block *sb, int flags)
26739+{
26740+ int err;
26741+
26742+ err = 0;
26743+ if (au_ftest_lock(flags, FLUSH))
26744+ au_nwt_flush(&au_sbi(sb)->si_nowait);
26745+
26746+ si_noflush_read_lock(sb);
26747+ err = au_plink_maint(sb, flags);
26748+ if (unlikely(err))
26749+ si_read_unlock(sb);
26750+
26751+ return err;
26752+}
26753+
26754+int si_write_lock(struct super_block *sb, int flags)
26755+{
26756+ int err;
26757+
26758+ if (au_ftest_lock(flags, FLUSH))
26759+ au_nwt_flush(&au_sbi(sb)->si_nowait);
26760+
26761+ si_noflush_write_lock(sb);
26762+ err = au_plink_maint(sb, flags);
26763+ if (unlikely(err))
26764+ si_write_unlock(sb);
26765+
26766+ return err;
26767+}
26768+
1facf9fc 26769+/* dentry and super_block lock. call at entry point */
e49829fe 26770+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 26771+{
e49829fe 26772+ int err;
027c5e7a 26773+ struct super_block *sb;
e49829fe 26774+
027c5e7a
AM
26775+ sb = dentry->d_sb;
26776+ err = si_read_lock(sb, flags);
26777+ if (unlikely(err))
26778+ goto out;
26779+
26780+ if (au_ftest_lock(flags, DW))
26781+ di_write_lock_child(dentry);
26782+ else
26783+ di_read_lock_child(dentry, flags);
26784+
26785+ if (au_ftest_lock(flags, GEN)) {
26786+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
26787+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
26788+ AuDebugOn(!err && au_dbrange_test(dentry));
26789+ else if (!err)
26790+ err = au_dbrange_test(dentry);
027c5e7a
AM
26791+ if (unlikely(err))
26792+ aufs_read_unlock(dentry, flags);
e49829fe
JR
26793+ }
26794+
027c5e7a 26795+out:
e49829fe 26796+ return err;
1facf9fc 26797+}
26798+
26799+void aufs_read_unlock(struct dentry *dentry, int flags)
26800+{
26801+ if (au_ftest_lock(flags, DW))
26802+ di_write_unlock(dentry);
26803+ else
26804+ di_read_unlock(dentry, flags);
26805+ si_read_unlock(dentry->d_sb);
26806+}
26807+
26808+void aufs_write_lock(struct dentry *dentry)
26809+{
e49829fe 26810+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 26811+ di_write_lock_child(dentry);
26812+}
26813+
26814+void aufs_write_unlock(struct dentry *dentry)
26815+{
26816+ di_write_unlock(dentry);
26817+ si_write_unlock(dentry->d_sb);
26818+}
26819+
e49829fe 26820+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 26821+{
e49829fe 26822+ int err;
027c5e7a
AM
26823+ unsigned int sigen;
26824+ struct super_block *sb;
e49829fe 26825+
027c5e7a
AM
26826+ sb = d1->d_sb;
26827+ err = si_read_lock(sb, flags);
26828+ if (unlikely(err))
26829+ goto out;
26830+
b95c5147 26831+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
26832+
26833+ if (au_ftest_lock(flags, GEN)) {
26834+ sigen = au_sigen(sb);
26835+ err = au_digen_test(d1, sigen);
26836+ AuDebugOn(!err && au_dbrange_test(d1));
26837+ if (!err) {
26838+ err = au_digen_test(d2, sigen);
26839+ AuDebugOn(!err && au_dbrange_test(d2));
26840+ }
26841+ if (unlikely(err))
26842+ aufs_read_and_write_unlock2(d1, d2);
26843+ }
26844+
26845+out:
e49829fe 26846+ return err;
1facf9fc 26847+}
26848+
26849+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
26850+{
26851+ di_write_unlock2(d1, d2);
26852+ si_read_unlock(d1->d_sb);
26853+}
b752ccd1
AM
26854+
26855+/* ---------------------------------------------------------------------- */
26856+
febd17d6 26857+static void si_pid_alloc(struct au_si_pid *au_si_pid, int idx)
b752ccd1 26858+{
febd17d6 26859+ unsigned long *p;
b752ccd1 26860+
febd17d6
JR
26861+ BUILD_BUG_ON(sizeof(unsigned long) !=
26862+ sizeof(*au_si_pid->pid_bitmap));
b752ccd1 26863+
febd17d6
JR
26864+ mutex_lock(&au_si_pid->pid_mtx);
26865+ p = au_si_pid->pid_bitmap[idx];
26866+ while (!p) {
26867+ /*
26868+ * bad approach.
26869+ * but keeping 'si_pid_set()' void is more important.
26870+ */
26871+ p = kcalloc(BITS_TO_LONGS(AU_PIDSTEP),
26872+ sizeof(*au_si_pid->pid_bitmap),
26873+ GFP_NOFS);
26874+ if (p)
26875+ break;
26876+ cond_resched();
26877+ }
26878+ au_si_pid->pid_bitmap[idx] = p;
26879+ mutex_unlock(&au_si_pid->pid_mtx);
b752ccd1
AM
26880+}
26881+
febd17d6 26882+void si_pid_set(struct super_block *sb)
b752ccd1 26883+{
febd17d6
JR
26884+ pid_t bit;
26885+ int idx;
26886+ unsigned long *bitmap;
26887+ struct au_si_pid *au_si_pid;
26888+
26889+ si_pid_idx_bit(&idx, &bit);
26890+ au_si_pid = &au_sbi(sb)->au_si_pid;
26891+ bitmap = au_si_pid->pid_bitmap[idx];
26892+ if (!bitmap) {
26893+ si_pid_alloc(au_si_pid, idx);
26894+ bitmap = au_si_pid->pid_bitmap[idx];
26895+ }
26896+ AuDebugOn(test_bit(bit, bitmap));
26897+ set_bit(bit, bitmap);
26898+ /* smp_mb(); */
b752ccd1 26899+}
7f207e10
AM
26900diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26901--- /usr/share/empty/fs/aufs/spl.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
26902+++ linux/fs/aufs/spl.h 2016-08-17 18:01:06.161889951 +0200
26903@@ -0,0 +1,113 @@
1facf9fc 26904+/*
8cdd5066 26905+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26906+ *
26907+ * This program, aufs is free software; you can redistribute it and/or modify
26908+ * it under the terms of the GNU General Public License as published by
26909+ * the Free Software Foundation; either version 2 of the License, or
26910+ * (at your option) any later version.
dece6358
AM
26911+ *
26912+ * This program is distributed in the hope that it will be useful,
26913+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26914+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26915+ * GNU General Public License for more details.
26916+ *
26917+ * You should have received a copy of the GNU General Public License
523b37e3 26918+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26919+ */
26920+
26921+/*
26922+ * simple list protected by a spinlock
26923+ */
26924+
26925+#ifndef __AUFS_SPL_H__
26926+#define __AUFS_SPL_H__
26927+
26928+#ifdef __KERNEL__
26929+
f0c0a007 26930+#if 0
1facf9fc 26931+struct au_splhead {
26932+ spinlock_t spin;
26933+ struct list_head head;
26934+};
26935+
26936+static inline void au_spl_init(struct au_splhead *spl)
26937+{
26938+ spin_lock_init(&spl->spin);
26939+ INIT_LIST_HEAD(&spl->head);
26940+}
26941+
26942+static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26943+{
26944+ spin_lock(&spl->spin);
26945+ list_add(list, &spl->head);
26946+ spin_unlock(&spl->spin);
26947+}
26948+
26949+static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26950+{
26951+ spin_lock(&spl->spin);
26952+ list_del(list);
26953+ spin_unlock(&spl->spin);
26954+}
26955+
4a4d8108
AM
26956+static inline void au_spl_del_rcu(struct list_head *list,
26957+ struct au_splhead *spl)
26958+{
26959+ spin_lock(&spl->spin);
26960+ list_del_rcu(list);
26961+ spin_unlock(&spl->spin);
26962+}
f0c0a007 26963+#endif
4a4d8108 26964+
86dc4139
AM
26965+/* ---------------------------------------------------------------------- */
26966+
26967+struct au_sphlhead {
26968+ spinlock_t spin;
26969+ struct hlist_head head;
26970+};
26971+
26972+static inline void au_sphl_init(struct au_sphlhead *sphl)
26973+{
26974+ spin_lock_init(&sphl->spin);
26975+ INIT_HLIST_HEAD(&sphl->head);
26976+}
26977+
26978+static inline void au_sphl_add(struct hlist_node *hlist,
26979+ struct au_sphlhead *sphl)
26980+{
26981+ spin_lock(&sphl->spin);
26982+ hlist_add_head(hlist, &sphl->head);
26983+ spin_unlock(&sphl->spin);
26984+}
26985+
26986+static inline void au_sphl_del(struct hlist_node *hlist,
26987+ struct au_sphlhead *sphl)
26988+{
26989+ spin_lock(&sphl->spin);
26990+ hlist_del(hlist);
26991+ spin_unlock(&sphl->spin);
26992+}
26993+
26994+static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26995+ struct au_sphlhead *sphl)
26996+{
26997+ spin_lock(&sphl->spin);
26998+ hlist_del_rcu(hlist);
26999+ spin_unlock(&sphl->spin);
27000+}
27001+
27002+static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
27003+{
27004+ unsigned long cnt;
27005+ struct hlist_node *pos;
27006+
27007+ cnt = 0;
27008+ spin_lock(&sphl->spin);
27009+ hlist_for_each(pos, &sphl->head)
27010+ cnt++;
27011+ spin_unlock(&sphl->spin);
27012+ return cnt;
27013+}
27014+
1facf9fc 27015+#endif /* __KERNEL__ */
27016+#endif /* __AUFS_SPL_H__ */
7f207e10
AM
27017diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
27018--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
27019+++ linux/fs/aufs/super.c 2016-08-17 18:01:06.161889951 +0200
27020@@ -0,0 +1,1038 @@
1facf9fc 27021+/*
8cdd5066 27022+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 27023+ *
27024+ * This program, aufs is free software; you can redistribute it and/or modify
27025+ * it under the terms of the GNU General Public License as published by
27026+ * the Free Software Foundation; either version 2 of the License, or
27027+ * (at your option) any later version.
dece6358
AM
27028+ *
27029+ * This program is distributed in the hope that it will be useful,
27030+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27031+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27032+ * GNU General Public License for more details.
27033+ *
27034+ * You should have received a copy of the GNU General Public License
523b37e3 27035+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27036+ */
27037+
27038+/*
27039+ * mount and super_block operations
27040+ */
27041+
f6c5ef8b 27042+#include <linux/mm.h>
1facf9fc 27043+#include <linux/seq_file.h>
27044+#include <linux/statfs.h>
7f207e10 27045+#include <linux/vmalloc.h>
1facf9fc 27046+#include "aufs.h"
27047+
27048+/*
27049+ * super_operations
27050+ */
27051+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
27052+{
27053+ struct au_icntnr *c;
27054+
27055+ c = au_cache_alloc_icntnr();
27056+ if (c) {
027c5e7a 27057+ au_icntnr_init(c);
1facf9fc 27058+ c->vfs_inode.i_version = 1; /* sigen(sb); */
27059+ c->iinfo.ii_hinode = NULL;
27060+ return &c->vfs_inode;
27061+ }
27062+ return NULL;
27063+}
27064+
027c5e7a
AM
27065+static void aufs_destroy_inode_cb(struct rcu_head *head)
27066+{
27067+ struct inode *inode = container_of(head, struct inode, i_rcu);
27068+
f0c0a007 27069+ au_cache_dfree_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
027c5e7a
AM
27070+}
27071+
1facf9fc 27072+static void aufs_destroy_inode(struct inode *inode)
27073+{
5afbbe0d
AM
27074+ if (!au_is_bad_inode(inode))
27075+ au_iinfo_fin(inode);
027c5e7a 27076+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 27077+}
27078+
27079+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
27080+{
27081+ struct inode *inode;
27082+ int err;
27083+
27084+ inode = iget_locked(sb, ino);
27085+ if (unlikely(!inode)) {
27086+ inode = ERR_PTR(-ENOMEM);
27087+ goto out;
27088+ }
27089+ if (!(inode->i_state & I_NEW))
27090+ goto out;
27091+
27092+ err = au_xigen_new(inode);
27093+ if (!err)
27094+ err = au_iinfo_init(inode);
27095+ if (!err)
27096+ inode->i_version++;
27097+ else {
27098+ iget_failed(inode);
27099+ inode = ERR_PTR(err);
27100+ }
27101+
4f0767ce 27102+out:
1facf9fc 27103+ /* never return NULL */
27104+ AuDebugOn(!inode);
27105+ AuTraceErrPtr(inode);
27106+ return inode;
27107+}
27108+
27109+/* lock free root dinfo */
27110+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
27111+{
27112+ int err;
5afbbe0d 27113+ aufs_bindex_t bindex, bbot;
1facf9fc 27114+ struct path path;
4a4d8108 27115+ struct au_hdentry *hdp;
1facf9fc 27116+ struct au_branch *br;
076b876e 27117+ au_br_perm_str_t perm;
1facf9fc 27118+
27119+ err = 0;
5afbbe0d
AM
27120+ bbot = au_sbbot(sb);
27121+ bindex = 0;
27122+ hdp = au_hdentry(au_di(sb->s_root), bindex);
27123+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 27124+ br = au_sbr(sb, bindex);
86dc4139 27125+ path.mnt = au_br_mnt(br);
5afbbe0d 27126+ path.dentry = hdp->hd_dentry;
1facf9fc 27127+ err = au_seq_path(seq, &path);
79b8bda9 27128+ if (!err) {
076b876e 27129+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 27130+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 27131+ if (bindex != bbot)
79b8bda9 27132+ seq_putc(seq, ':');
1e00d052 27133+ }
1facf9fc 27134+ }
79b8bda9
AM
27135+ if (unlikely(err || seq_has_overflowed(seq)))
27136+ err = -E2BIG;
1facf9fc 27137+
27138+ return err;
27139+}
27140+
27141+static void au_show_wbr_create(struct seq_file *m, int v,
27142+ struct au_sbinfo *sbinfo)
27143+{
27144+ const char *pat;
27145+
dece6358
AM
27146+ AuRwMustAnyLock(&sbinfo->si_rwsem);
27147+
c2b27bf2 27148+ seq_puts(m, ",create=");
1facf9fc 27149+ pat = au_optstr_wbr_create(v);
27150+ switch (v) {
27151+ case AuWbrCreate_TDP:
27152+ case AuWbrCreate_RR:
27153+ case AuWbrCreate_MFS:
27154+ case AuWbrCreate_PMFS:
c2b27bf2 27155+ seq_puts(m, pat);
1facf9fc 27156+ break;
27157+ case AuWbrCreate_MFSV:
27158+ seq_printf(m, /*pat*/"mfs:%lu",
e49829fe
JR
27159+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
27160+ / MSEC_PER_SEC);
1facf9fc 27161+ break;
27162+ case AuWbrCreate_PMFSV:
27163+ seq_printf(m, /*pat*/"pmfs:%lu",
e49829fe
JR
27164+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
27165+ / MSEC_PER_SEC);
1facf9fc 27166+ break;
27167+ case AuWbrCreate_MFSRR:
27168+ seq_printf(m, /*pat*/"mfsrr:%llu",
27169+ sbinfo->si_wbr_mfs.mfsrr_watermark);
27170+ break;
27171+ case AuWbrCreate_MFSRRV:
27172+ seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
27173+ sbinfo->si_wbr_mfs.mfsrr_watermark,
e49829fe
JR
27174+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
27175+ / MSEC_PER_SEC);
1facf9fc 27176+ break;
392086de
AM
27177+ case AuWbrCreate_PMFSRR:
27178+ seq_printf(m, /*pat*/"pmfsrr:%llu",
27179+ sbinfo->si_wbr_mfs.mfsrr_watermark);
27180+ break;
27181+ case AuWbrCreate_PMFSRRV:
27182+ seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
27183+ sbinfo->si_wbr_mfs.mfsrr_watermark,
27184+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
27185+ / MSEC_PER_SEC);
27186+ break;
1facf9fc 27187+ }
27188+}
27189+
7eafdf33 27190+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 27191+{
27192+#ifdef CONFIG_SYSFS
27193+ return 0;
27194+#else
27195+ int err;
27196+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
27197+ aufs_bindex_t bindex, brid;
1facf9fc 27198+ struct qstr *name;
27199+ struct file *f;
27200+ struct dentry *d, *h_root;
27201+
dece6358
AM
27202+ AuRwMustAnyLock(&sbinfo->si_rwsem);
27203+
1facf9fc 27204+ err = 0;
1facf9fc 27205+ f = au_sbi(sb)->si_xib;
27206+ if (!f)
27207+ goto out;
27208+
27209+ /* stop printing the default xino path on the first writable branch */
27210+ h_root = NULL;
27211+ brid = au_xino_brid(sb);
27212+ if (brid >= 0) {
27213+ bindex = au_br_index(sb, brid);
5afbbe0d 27214+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 27215+ }
2000de60 27216+ d = f->f_path.dentry;
1facf9fc 27217+ name = &d->d_name;
27218+ /* safe ->d_parent because the file is unlinked */
27219+ if (d->d_parent == h_root
27220+ && name->len == len
27221+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
27222+ goto out;
27223+
27224+ seq_puts(seq, ",xino=");
27225+ err = au_xino_path(seq, f);
27226+
4f0767ce 27227+out:
1facf9fc 27228+ return err;
27229+#endif
27230+}
27231+
27232+/* seq_file will re-call me in case of too long string */
7eafdf33 27233+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 27234+{
027c5e7a 27235+ int err;
1facf9fc 27236+ unsigned int mnt_flags, v;
27237+ struct super_block *sb;
27238+ struct au_sbinfo *sbinfo;
27239+
27240+#define AuBool(name, str) do { \
27241+ v = au_opt_test(mnt_flags, name); \
27242+ if (v != au_opt_test(AuOpt_Def, name)) \
27243+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
27244+} while (0)
27245+
27246+#define AuStr(name, str) do { \
27247+ v = mnt_flags & AuOptMask_##name; \
27248+ if (v != (AuOpt_Def & AuOptMask_##name)) \
27249+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
27250+} while (0)
27251+
27252+#define AuUInt(name, str, val) do { \
27253+ if (val != AUFS_##name##_DEF) \
27254+ seq_printf(m, "," #str "=%u", val); \
27255+} while (0)
27256+
7eafdf33 27257+ sb = dentry->d_sb;
c1595e42
JR
27258+ if (sb->s_flags & MS_POSIXACL)
27259+ seq_puts(m, ",acl");
27260+
27261+ /* lock free root dinfo */
1facf9fc 27262+ si_noflush_read_lock(sb);
27263+ sbinfo = au_sbi(sb);
27264+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
27265+
27266+ mnt_flags = au_mntflags(sb);
27267+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 27268+ err = au_show_xino(m, sb);
1facf9fc 27269+ if (unlikely(err))
27270+ goto out;
27271+ } else
27272+ seq_puts(m, ",noxino");
27273+
27274+ AuBool(TRUNC_XINO, trunc_xino);
27275+ AuStr(UDBA, udba);
dece6358 27276+ AuBool(SHWH, shwh);
1facf9fc 27277+ AuBool(PLINK, plink);
4a4d8108 27278+ AuBool(DIO, dio);
076b876e 27279+ AuBool(DIRPERM1, dirperm1);
1facf9fc 27280+
27281+ v = sbinfo->si_wbr_create;
27282+ if (v != AuWbrCreate_Def)
27283+ au_show_wbr_create(m, v, sbinfo);
27284+
27285+ v = sbinfo->si_wbr_copyup;
27286+ if (v != AuWbrCopyup_Def)
27287+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
27288+
27289+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
27290+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
27291+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
27292+
27293+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
27294+
027c5e7a
AM
27295+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
27296+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 27297+
27298+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
27299+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
27300+
076b876e
AM
27301+ au_fhsm_show(m, sbinfo);
27302+
1facf9fc 27303+ AuBool(SUM, sum);
27304+ /* AuBool(SUM_W, wsum); */
27305+ AuBool(WARN_PERM, warn_perm);
27306+ AuBool(VERBOSE, verbose);
27307+
4f0767ce 27308+out:
1facf9fc 27309+ /* be sure to print "br:" last */
27310+ if (!sysaufs_brs) {
27311+ seq_puts(m, ",br:");
27312+ au_show_brs(m, sb);
27313+ }
27314+ si_read_unlock(sb);
27315+ return 0;
27316+
1facf9fc 27317+#undef AuBool
27318+#undef AuStr
4a4d8108 27319+#undef AuUInt
1facf9fc 27320+}
27321+
27322+/* ---------------------------------------------------------------------- */
27323+
27324+/* sum mode which returns the summation for statfs(2) */
27325+
27326+static u64 au_add_till_max(u64 a, u64 b)
27327+{
27328+ u64 old;
27329+
27330+ old = a;
27331+ a += b;
92d182d2
AM
27332+ if (old <= a)
27333+ return a;
27334+ return ULLONG_MAX;
27335+}
27336+
27337+static u64 au_mul_till_max(u64 a, long mul)
27338+{
27339+ u64 old;
27340+
27341+ old = a;
27342+ a *= mul;
27343+ if (old <= a)
1facf9fc 27344+ return a;
27345+ return ULLONG_MAX;
27346+}
27347+
27348+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
27349+{
27350+ int err;
92d182d2 27351+ long bsize, factor;
1facf9fc 27352+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 27353+ aufs_bindex_t bbot, bindex, i;
1facf9fc 27354+ unsigned char shared;
7f207e10 27355+ struct path h_path;
1facf9fc 27356+ struct super_block *h_sb;
27357+
92d182d2
AM
27358+ err = 0;
27359+ bsize = LONG_MAX;
27360+ files = 0;
27361+ ffree = 0;
1facf9fc 27362+ blocks = 0;
27363+ bfree = 0;
27364+ bavail = 0;
5afbbe0d
AM
27365+ bbot = au_sbbot(sb);
27366+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
27367+ h_path.mnt = au_sbr_mnt(sb, bindex);
27368+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 27369+ shared = 0;
92d182d2 27370+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 27371+ shared = (au_sbr_sb(sb, i) == h_sb);
27372+ if (shared)
27373+ continue;
27374+
27375+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
27376+ h_path.dentry = h_path.mnt->mnt_root;
27377+ err = vfs_statfs(&h_path, buf);
1facf9fc 27378+ if (unlikely(err))
27379+ goto out;
27380+
92d182d2
AM
27381+ if (bsize > buf->f_bsize) {
27382+ /*
27383+ * we will reduce bsize, so we have to expand blocks
27384+ * etc. to match them again
27385+ */
27386+ factor = (bsize / buf->f_bsize);
27387+ blocks = au_mul_till_max(blocks, factor);
27388+ bfree = au_mul_till_max(bfree, factor);
27389+ bavail = au_mul_till_max(bavail, factor);
27390+ bsize = buf->f_bsize;
27391+ }
27392+
27393+ factor = (buf->f_bsize / bsize);
27394+ blocks = au_add_till_max(blocks,
27395+ au_mul_till_max(buf->f_blocks, factor));
27396+ bfree = au_add_till_max(bfree,
27397+ au_mul_till_max(buf->f_bfree, factor));
27398+ bavail = au_add_till_max(bavail,
27399+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 27400+ files = au_add_till_max(files, buf->f_files);
27401+ ffree = au_add_till_max(ffree, buf->f_ffree);
27402+ }
27403+
92d182d2 27404+ buf->f_bsize = bsize;
1facf9fc 27405+ buf->f_blocks = blocks;
27406+ buf->f_bfree = bfree;
27407+ buf->f_bavail = bavail;
27408+ buf->f_files = files;
27409+ buf->f_ffree = ffree;
92d182d2 27410+ buf->f_frsize = 0;
1facf9fc 27411+
4f0767ce 27412+out:
1facf9fc 27413+ return err;
27414+}
27415+
27416+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
27417+{
27418+ int err;
7f207e10 27419+ struct path h_path;
1facf9fc 27420+ struct super_block *sb;
27421+
27422+ /* lock free root dinfo */
27423+ sb = dentry->d_sb;
27424+ si_noflush_read_lock(sb);
7f207e10 27425+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 27426+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
27427+ h_path.mnt = au_sbr_mnt(sb, 0);
27428+ h_path.dentry = h_path.mnt->mnt_root;
27429+ err = vfs_statfs(&h_path, buf);
27430+ } else
1facf9fc 27431+ err = au_statfs_sum(sb, buf);
27432+ si_read_unlock(sb);
27433+
27434+ if (!err) {
27435+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 27436+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 27437+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27438+ }
27439+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27440+
27441+ return err;
27442+}
27443+
27444+/* ---------------------------------------------------------------------- */
27445+
537831f9
AM
27446+static int aufs_sync_fs(struct super_block *sb, int wait)
27447+{
27448+ int err, e;
5afbbe0d 27449+ aufs_bindex_t bbot, bindex;
537831f9
AM
27450+ struct au_branch *br;
27451+ struct super_block *h_sb;
27452+
27453+ err = 0;
27454+ si_noflush_read_lock(sb);
5afbbe0d
AM
27455+ bbot = au_sbbot(sb);
27456+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
27457+ br = au_sbr(sb, bindex);
27458+ if (!au_br_writable(br->br_perm))
27459+ continue;
27460+
27461+ h_sb = au_sbr_sb(sb, bindex);
27462+ if (h_sb->s_op->sync_fs) {
27463+ e = h_sb->s_op->sync_fs(h_sb, wait);
27464+ if (unlikely(e && !err))
27465+ err = e;
27466+ /* go on even if an error happens */
27467+ }
27468+ }
27469+ si_read_unlock(sb);
27470+
27471+ return err;
27472+}
27473+
27474+/* ---------------------------------------------------------------------- */
27475+
1facf9fc 27476+/* final actions when unmounting a file system */
27477+static void aufs_put_super(struct super_block *sb)
27478+{
27479+ struct au_sbinfo *sbinfo;
27480+
27481+ sbinfo = au_sbi(sb);
27482+ if (!sbinfo)
27483+ return;
27484+
1facf9fc 27485+ dbgaufs_si_fin(sbinfo);
27486+ kobject_put(&sbinfo->si_kobj);
27487+}
27488+
27489+/* ---------------------------------------------------------------------- */
27490+
79b8bda9
AM
27491+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
27492+ struct super_block *sb, void *arg)
7f207e10
AM
27493+{
27494+ void *array;
076b876e 27495+ unsigned long long n, sz;
7f207e10
AM
27496+
27497+ array = NULL;
27498+ n = 0;
27499+ if (!*hint)
27500+ goto out;
27501+
27502+ if (*hint > ULLONG_MAX / sizeof(array)) {
27503+ array = ERR_PTR(-EMFILE);
27504+ pr_err("hint %llu\n", *hint);
27505+ goto out;
27506+ }
27507+
076b876e
AM
27508+ sz = sizeof(array) * *hint;
27509+ array = kzalloc(sz, GFP_NOFS);
7f207e10 27510+ if (unlikely(!array))
076b876e 27511+ array = vzalloc(sz);
7f207e10
AM
27512+ if (unlikely(!array)) {
27513+ array = ERR_PTR(-ENOMEM);
27514+ goto out;
27515+ }
27516+
79b8bda9 27517+ n = cb(sb, array, *hint, arg);
7f207e10
AM
27518+ AuDebugOn(n > *hint);
27519+
27520+out:
27521+ *hint = n;
27522+ return array;
27523+}
27524+
79b8bda9 27525+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
27526+ unsigned long long max __maybe_unused,
27527+ void *arg)
27528+{
27529+ unsigned long long n;
27530+ struct inode **p, *inode;
27531+ struct list_head *head;
27532+
27533+ n = 0;
27534+ p = a;
27535+ head = arg;
79b8bda9 27536+ spin_lock(&sb->s_inode_list_lock);
7f207e10 27537+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
27538+ if (!au_is_bad_inode(inode)
27539+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
27540+ spin_lock(&inode->i_lock);
27541+ if (atomic_read(&inode->i_count)) {
27542+ au_igrab(inode);
27543+ *p++ = inode;
27544+ n++;
27545+ AuDebugOn(n > max);
27546+ }
27547+ spin_unlock(&inode->i_lock);
7f207e10
AM
27548+ }
27549+ }
79b8bda9 27550+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
27551+
27552+ return n;
27553+}
27554+
27555+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27556+{
5afbbe0d 27557+ *max = au_ninodes(sb);
79b8bda9 27558+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
27559+}
27560+
27561+void au_iarray_free(struct inode **a, unsigned long long max)
27562+{
27563+ unsigned long long ull;
27564+
27565+ for (ull = 0; ull < max; ull++)
27566+ iput(a[ull]);
be52b249 27567+ kvfree(a);
7f207e10
AM
27568+}
27569+
27570+/* ---------------------------------------------------------------------- */
27571+
1facf9fc 27572+/*
27573+ * refresh dentry and inode at remount time.
27574+ */
027c5e7a
AM
27575+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27576+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27577+ struct dentry *parent)
1facf9fc 27578+{
27579+ int err;
1facf9fc 27580+
27581+ di_write_lock_child(dentry);
1facf9fc 27582+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
27583+ err = au_refresh_dentry(dentry, parent);
27584+ if (!err && dir_flags)
5527c038 27585+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 27586+ di_read_unlock(parent, AuLock_IR);
1facf9fc 27587+ di_write_unlock(dentry);
27588+
27589+ return err;
27590+}
27591+
027c5e7a
AM
27592+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27593+ struct au_sbinfo *sbinfo,
b95c5147 27594+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 27595+{
027c5e7a
AM
27596+ int err;
27597+ struct dentry *parent;
027c5e7a
AM
27598+
27599+ err = 0;
27600+ parent = dget_parent(dentry);
27601+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
27602+ if (d_really_is_positive(dentry)) {
27603+ if (!d_is_dir(dentry))
027c5e7a
AM
27604+ err = au_do_refresh(dentry, /*dir_flags*/0,
27605+ parent);
27606+ else {
27607+ err = au_do_refresh(dentry, dir_flags, parent);
27608+ if (unlikely(err))
27609+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27610+ }
27611+ } else
27612+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27613+ AuDbgDentry(dentry);
27614+ }
27615+ dput(parent);
27616+
79b8bda9 27617+ if (!err) {
b95c5147 27618+ if (do_idop)
79b8bda9
AM
27619+ au_refresh_dop(dentry, /*force_reval*/0);
27620+ } else
27621+ au_refresh_dop(dentry, /*force_reval*/1);
27622+
027c5e7a
AM
27623+ AuTraceErr(err);
27624+ return err;
1facf9fc 27625+}
27626+
b95c5147 27627+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 27628+{
27629+ int err, i, j, ndentry, e;
027c5e7a 27630+ unsigned int sigen;
1facf9fc 27631+ struct au_dcsub_pages dpages;
27632+ struct au_dpage *dpage;
027c5e7a
AM
27633+ struct dentry **dentries, *d;
27634+ struct au_sbinfo *sbinfo;
27635+ struct dentry *root = sb->s_root;
5527c038 27636+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 27637+
b95c5147 27638+ if (do_idop)
79b8bda9
AM
27639+ au_refresh_dop(root, /*force_reval*/0);
27640+
027c5e7a
AM
27641+ err = au_dpages_init(&dpages, GFP_NOFS);
27642+ if (unlikely(err))
1facf9fc 27643+ goto out;
027c5e7a
AM
27644+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
27645+ if (unlikely(err))
1facf9fc 27646+ goto out_dpages;
1facf9fc 27647+
027c5e7a
AM
27648+ sigen = au_sigen(sb);
27649+ sbinfo = au_sbi(sb);
27650+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 27651+ dpage = dpages.dpages + i;
27652+ dentries = dpage->dentries;
27653+ ndentry = dpage->ndentry;
027c5e7a 27654+ for (j = 0; j < ndentry; j++) {
1facf9fc 27655+ d = dentries[j];
79b8bda9 27656+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 27657+ do_idop);
027c5e7a
AM
27658+ if (unlikely(e && !err))
27659+ err = e;
27660+ /* go on even err */
1facf9fc 27661+ }
27662+ }
27663+
4f0767ce 27664+out_dpages:
1facf9fc 27665+ au_dpages_free(&dpages);
4f0767ce 27666+out:
1facf9fc 27667+ return err;
27668+}
27669+
b95c5147 27670+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 27671+{
027c5e7a
AM
27672+ int err, e;
27673+ unsigned int sigen;
27674+ unsigned long long max, ull;
27675+ struct inode *inode, **array;
1facf9fc 27676+
027c5e7a
AM
27677+ array = au_iarray_alloc(sb, &max);
27678+ err = PTR_ERR(array);
27679+ if (IS_ERR(array))
27680+ goto out;
1facf9fc 27681+
27682+ err = 0;
027c5e7a
AM
27683+ sigen = au_sigen(sb);
27684+ for (ull = 0; ull < max; ull++) {
27685+ inode = array[ull];
076b876e
AM
27686+ if (unlikely(!inode))
27687+ break;
b95c5147
AM
27688+
27689+ e = 0;
27690+ ii_write_lock_child(inode);
537831f9 27691+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 27692+ e = au_refresh_hinode_self(inode);
1facf9fc 27693+ if (unlikely(e)) {
b95c5147 27694+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 27695+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 27696+ if (!err)
27697+ err = e;
27698+ /* go on even if err */
27699+ }
27700+ }
b95c5147
AM
27701+ if (!e && do_idop)
27702+ au_refresh_iop(inode, /*force_getattr*/0);
27703+ ii_write_unlock(inode);
1facf9fc 27704+ }
27705+
027c5e7a 27706+ au_iarray_free(array, max);
1facf9fc 27707+
4f0767ce 27708+out:
1facf9fc 27709+ return err;
27710+}
27711+
b95c5147 27712+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 27713+{
027c5e7a
AM
27714+ int err, e;
27715+ unsigned int udba;
5afbbe0d 27716+ aufs_bindex_t bindex, bbot;
1facf9fc 27717+ struct dentry *root;
27718+ struct inode *inode;
027c5e7a 27719+ struct au_branch *br;
79b8bda9 27720+ struct au_sbinfo *sbi;
1facf9fc 27721+
27722+ au_sigen_inc(sb);
79b8bda9
AM
27723+ sbi = au_sbi(sb);
27724+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 27725+
27726+ root = sb->s_root;
27727+ DiMustNoWaiters(root);
5527c038 27728+ inode = d_inode(root);
1facf9fc 27729+ IiMustNoWaiters(inode);
1facf9fc 27730+
027c5e7a 27731+ udba = au_opt_udba(sb);
5afbbe0d
AM
27732+ bbot = au_sbbot(sb);
27733+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27734+ br = au_sbr(sb, bindex);
27735+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 27736+ if (unlikely(err))
027c5e7a
AM
27737+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27738+ bindex, err);
27739+ /* go on even if err */
1facf9fc 27740+ }
027c5e7a 27741+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 27742+
b95c5147 27743+ if (do_idop) {
79b8bda9
AM
27744+ if (au_ftest_si(sbi, NO_DREVAL)) {
27745+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
27746+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
27747+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
27748+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
27749+ } else {
27750+ AuDebugOn(sb->s_d_op == &aufs_dop);
27751+ sb->s_d_op = &aufs_dop;
b95c5147
AM
27752+ AuDebugOn(sbi->si_iop_array == aufs_iop);
27753+ sbi->si_iop_array = aufs_iop;
79b8bda9 27754+ }
b95c5147
AM
27755+ pr_info("reset to %pf and %pf\n",
27756+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
27757+ }
27758+
027c5e7a 27759+ di_write_unlock(root);
b95c5147
AM
27760+ err = au_refresh_d(sb, do_idop);
27761+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
27762+ if (unlikely(e && !err))
27763+ err = e;
1facf9fc 27764+ /* aufs_write_lock() calls ..._child() */
27765+ di_write_lock_child(root);
027c5e7a
AM
27766+
27767+ au_cpup_attr_all(inode, /*force*/1);
27768+
27769+ if (unlikely(err))
27770+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 27771+}
27772+
27773+/* stop extra interpretation of errno in mount(8), and strange error messages */
27774+static int cvt_err(int err)
27775+{
27776+ AuTraceErr(err);
27777+
27778+ switch (err) {
27779+ case -ENOENT:
27780+ case -ENOTDIR:
27781+ case -EEXIST:
27782+ case -EIO:
27783+ err = -EINVAL;
27784+ }
27785+ return err;
27786+}
27787+
27788+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27789+{
4a4d8108
AM
27790+ int err, do_dx;
27791+ unsigned int mntflags;
be52b249
AM
27792+ struct au_opts opts = {
27793+ .opt = NULL
27794+ };
1facf9fc 27795+ struct dentry *root;
27796+ struct inode *inode;
27797+ struct au_sbinfo *sbinfo;
27798+
27799+ err = 0;
27800+ root = sb->s_root;
27801+ if (!data || !*data) {
e49829fe
JR
27802+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27803+ if (!err) {
27804+ di_write_lock_child(root);
27805+ err = au_opts_verify(sb, *flags, /*pending*/0);
27806+ aufs_write_unlock(root);
27807+ }
1facf9fc 27808+ goto out;
27809+ }
27810+
27811+ err = -ENOMEM;
1facf9fc 27812+ opts.opt = (void *)__get_free_page(GFP_NOFS);
27813+ if (unlikely(!opts.opt))
27814+ goto out;
27815+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27816+ opts.flags = AuOpts_REMOUNT;
27817+ opts.sb_flags = *flags;
27818+
27819+ /* parse it before aufs lock */
27820+ err = au_opts_parse(sb, data, &opts);
27821+ if (unlikely(err))
27822+ goto out_opts;
27823+
27824+ sbinfo = au_sbi(sb);
5527c038 27825+ inode = d_inode(root);
febd17d6 27826+ inode_lock(inode);
e49829fe
JR
27827+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27828+ if (unlikely(err))
27829+ goto out_mtx;
27830+ di_write_lock_child(root);
1facf9fc 27831+
27832+ /* au_opts_remount() may return an error */
27833+ err = au_opts_remount(sb, &opts);
27834+ au_opts_free(&opts);
27835+
027c5e7a 27836+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 27837+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 27838+
4a4d8108
AM
27839+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
27840+ mntflags = au_mntflags(sb);
27841+ do_dx = !!au_opt_test(mntflags, DIO);
27842+ au_dy_arefresh(do_dx);
27843+ }
27844+
076b876e 27845+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 27846+ aufs_write_unlock(root);
953406b4 27847+
e49829fe 27848+out_mtx:
febd17d6 27849+ inode_unlock(inode);
4f0767ce 27850+out_opts:
f0c0a007 27851+ au_delayed_free_page((unsigned long)opts.opt);
4f0767ce 27852+out:
1facf9fc 27853+ err = cvt_err(err);
27854+ AuTraceErr(err);
27855+ return err;
27856+}
27857+
4a4d8108 27858+static const struct super_operations aufs_sop = {
1facf9fc 27859+ .alloc_inode = aufs_alloc_inode,
27860+ .destroy_inode = aufs_destroy_inode,
b752ccd1 27861+ /* always deleting, no clearing */
1facf9fc 27862+ .drop_inode = generic_delete_inode,
27863+ .show_options = aufs_show_options,
27864+ .statfs = aufs_statfs,
27865+ .put_super = aufs_put_super,
537831f9 27866+ .sync_fs = aufs_sync_fs,
1facf9fc 27867+ .remount_fs = aufs_remount_fs
27868+};
27869+
27870+/* ---------------------------------------------------------------------- */
27871+
27872+static int alloc_root(struct super_block *sb)
27873+{
27874+ int err;
27875+ struct inode *inode;
27876+ struct dentry *root;
27877+
27878+ err = -ENOMEM;
27879+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
27880+ err = PTR_ERR(inode);
27881+ if (IS_ERR(inode))
27882+ goto out;
27883+
b95c5147 27884+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 27885+ inode->i_fop = &aufs_dir_fop;
27886+ inode->i_mode = S_IFDIR;
9dbd164d 27887+ set_nlink(inode, 2);
1facf9fc 27888+ unlock_new_inode(inode);
27889+
92d182d2 27890+ root = d_make_root(inode);
1facf9fc 27891+ if (unlikely(!root))
92d182d2 27892+ goto out;
1facf9fc 27893+ err = PTR_ERR(root);
27894+ if (IS_ERR(root))
92d182d2 27895+ goto out;
1facf9fc 27896+
4a4d8108 27897+ err = au_di_init(root);
1facf9fc 27898+ if (!err) {
27899+ sb->s_root = root;
27900+ return 0; /* success */
27901+ }
27902+ dput(root);
1facf9fc 27903+
4f0767ce 27904+out:
1facf9fc 27905+ return err;
1facf9fc 27906+}
27907+
27908+static int aufs_fill_super(struct super_block *sb, void *raw_data,
27909+ int silent __maybe_unused)
27910+{
27911+ int err;
be52b249
AM
27912+ struct au_opts opts = {
27913+ .opt = NULL
27914+ };
79b8bda9 27915+ struct au_sbinfo *sbinfo;
1facf9fc 27916+ struct dentry *root;
27917+ struct inode *inode;
27918+ char *arg = raw_data;
27919+
27920+ if (unlikely(!arg || !*arg)) {
27921+ err = -EINVAL;
4a4d8108 27922+ pr_err("no arg\n");
1facf9fc 27923+ goto out;
27924+ }
27925+
27926+ err = -ENOMEM;
1facf9fc 27927+ opts.opt = (void *)__get_free_page(GFP_NOFS);
27928+ if (unlikely(!opts.opt))
27929+ goto out;
27930+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27931+ opts.sb_flags = sb->s_flags;
27932+
27933+ err = au_si_alloc(sb);
27934+ if (unlikely(err))
27935+ goto out_opts;
79b8bda9 27936+ sbinfo = au_sbi(sb);
1facf9fc 27937+
27938+ /* all timestamps always follow the ones on the branch */
27939+ sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27940+ sb->s_op = &aufs_sop;
027c5e7a 27941+ sb->s_d_op = &aufs_dop;
1facf9fc 27942+ sb->s_magic = AUFS_SUPER_MAGIC;
27943+ sb->s_maxbytes = 0;
c1595e42 27944+ sb->s_stack_depth = 1;
1facf9fc 27945+ au_export_init(sb);
c1595e42 27946+ /* au_xattr_init(sb); */
1facf9fc 27947+
27948+ err = alloc_root(sb);
27949+ if (unlikely(err)) {
27950+ si_write_unlock(sb);
27951+ goto out_info;
27952+ }
27953+ root = sb->s_root;
5527c038 27954+ inode = d_inode(root);
1facf9fc 27955+
27956+ /*
27957+ * actually we can parse options regardless aufs lock here.
27958+ * but at remount time, parsing must be done before aufs lock.
27959+ * so we follow the same rule.
27960+ */
27961+ ii_write_lock_parent(inode);
27962+ aufs_write_unlock(root);
27963+ err = au_opts_parse(sb, arg, &opts);
27964+ if (unlikely(err))
27965+ goto out_root;
27966+
27967+ /* lock vfs_inode first, then aufs. */
febd17d6 27968+ inode_lock(inode);
1facf9fc 27969+ aufs_write_lock(root);
27970+ err = au_opts_mount(sb, &opts);
27971+ au_opts_free(&opts);
79b8bda9
AM
27972+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
27973+ sb->s_d_op = &aufs_dop_noreval;
27974+ pr_info("%pf\n", sb->s_d_op);
27975+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
27976+ sbinfo->si_iop_array = aufs_iop_nogetattr;
27977+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 27978+ }
1facf9fc 27979+ aufs_write_unlock(root);
febd17d6 27980+ inode_unlock(inode);
4a4d8108
AM
27981+ if (!err)
27982+ goto out_opts; /* success */
1facf9fc 27983+
4f0767ce 27984+out_root:
1facf9fc 27985+ dput(root);
27986+ sb->s_root = NULL;
4f0767ce 27987+out_info:
79b8bda9
AM
27988+ dbgaufs_si_fin(sbinfo);
27989+ kobject_put(&sbinfo->si_kobj);
1facf9fc 27990+ sb->s_fs_info = NULL;
4f0767ce 27991+out_opts:
f0c0a007 27992+ au_delayed_free_page((unsigned long)opts.opt);
4f0767ce 27993+out:
1facf9fc 27994+ AuTraceErr(err);
27995+ err = cvt_err(err);
27996+ AuTraceErr(err);
27997+ return err;
27998+}
27999+
28000+/* ---------------------------------------------------------------------- */
28001+
027c5e7a
AM
28002+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
28003+ const char *dev_name __maybe_unused,
28004+ void *raw_data)
1facf9fc 28005+{
027c5e7a 28006+ struct dentry *root;
1facf9fc 28007+ struct super_block *sb;
28008+
28009+ /* all timestamps always follow the ones on the branch */
28010+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
28011+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
28012+ if (IS_ERR(root))
28013+ goto out;
28014+
28015+ sb = root->d_sb;
28016+ si_write_lock(sb, !AuLock_FLUSH);
28017+ sysaufs_brs_add(sb, 0);
28018+ si_write_unlock(sb);
28019+ au_sbilist_add(sb);
28020+
28021+out:
28022+ return root;
1facf9fc 28023+}
28024+
e49829fe
JR
28025+static void aufs_kill_sb(struct super_block *sb)
28026+{
28027+ struct au_sbinfo *sbinfo;
28028+
28029+ sbinfo = au_sbi(sb);
28030+ if (sbinfo) {
28031+ au_sbilist_del(sb);
28032+ aufs_write_lock(sb->s_root);
076b876e 28033+ au_fhsm_fin(sb);
e49829fe
JR
28034+ if (sbinfo->si_wbr_create_ops->fin)
28035+ sbinfo->si_wbr_create_ops->fin(sb);
28036+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
28037+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 28038+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
28039+ }
28040+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
28041+ au_plink_put(sb, /*verbose*/1);
28042+ au_xino_clr(sb);
1e00d052 28043+ sbinfo->si_sb = NULL;
e49829fe 28044+ aufs_write_unlock(sb->s_root);
e49829fe
JR
28045+ au_nwt_flush(&sbinfo->si_nowait);
28046+ }
98d9a5b1 28047+ kill_anon_super(sb);
e49829fe
JR
28048+}
28049+
1facf9fc 28050+struct file_system_type aufs_fs_type = {
28051+ .name = AUFS_FSTYPE,
c06a8ce3
AM
28052+ /* a race between rename and others */
28053+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 28054+ .mount = aufs_mount,
e49829fe 28055+ .kill_sb = aufs_kill_sb,
1facf9fc 28056+ /* no need to __module_get() and module_put(). */
28057+ .owner = THIS_MODULE,
28058+};
7f207e10
AM
28059diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
28060--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
28061+++ linux/fs/aufs/super.h 2016-07-25 19:05:34.817826663 +0200
28062@@ -0,0 +1,638 @@
1facf9fc 28063+/*
8cdd5066 28064+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28065+ *
28066+ * This program, aufs is free software; you can redistribute it and/or modify
28067+ * it under the terms of the GNU General Public License as published by
28068+ * the Free Software Foundation; either version 2 of the License, or
28069+ * (at your option) any later version.
dece6358
AM
28070+ *
28071+ * This program is distributed in the hope that it will be useful,
28072+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28073+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28074+ * GNU General Public License for more details.
28075+ *
28076+ * You should have received a copy of the GNU General Public License
523b37e3 28077+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28078+ */
28079+
28080+/*
28081+ * super_block operations
28082+ */
28083+
28084+#ifndef __AUFS_SUPER_H__
28085+#define __AUFS_SUPER_H__
28086+
28087+#ifdef __KERNEL__
28088+
28089+#include <linux/fs.h>
5527c038 28090+#include <linux/kobject.h>
1facf9fc 28091+#include "rwsem.h"
28092+#include "spl.h"
28093+#include "wkq.h"
28094+
1facf9fc 28095+/* policies to select one among multiple writable branches */
28096+struct au_wbr_copyup_operations {
28097+ int (*copyup)(struct dentry *dentry);
28098+};
28099+
392086de
AM
28100+#define AuWbr_DIR 1 /* target is a dir */
28101+#define AuWbr_PARENT (1 << 1) /* always require a parent */
28102+
28103+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
28104+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
28105+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
28106+
1facf9fc 28107+struct au_wbr_create_operations {
392086de 28108+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 28109+ int (*init)(struct super_block *sb);
28110+ int (*fin)(struct super_block *sb);
28111+};
28112+
28113+struct au_wbr_mfs {
28114+ struct mutex mfs_lock; /* protect this structure */
28115+ unsigned long mfs_jiffy;
28116+ unsigned long mfs_expire;
28117+ aufs_bindex_t mfs_bindex;
28118+
28119+ unsigned long long mfsrr_bytes;
28120+ unsigned long long mfsrr_watermark;
28121+};
28122+
86dc4139
AM
28123+#define AuPlink_NHASH 100
28124+static inline int au_plink_hash(ino_t ino)
28125+{
28126+ return ino % AuPlink_NHASH;
28127+}
28128+
076b876e
AM
28129+/* File-based Hierarchical Storage Management */
28130+struct au_fhsm {
28131+#ifdef CONFIG_AUFS_FHSM
28132+ /* allow only one process who can receive the notification */
28133+ spinlock_t fhsm_spin;
28134+ pid_t fhsm_pid;
28135+ wait_queue_head_t fhsm_wqh;
28136+ atomic_t fhsm_readable;
28137+
c1595e42 28138+ /* these are protected by si_rwsem */
076b876e 28139+ unsigned long fhsm_expire;
c1595e42 28140+ aufs_bindex_t fhsm_bottom;
076b876e
AM
28141+#endif
28142+};
28143+
febd17d6
JR
28144+#define AU_PIDSTEP (int)(BITS_TO_LONGS(PID_MAX_DEFAULT) * BITS_PER_LONG)
28145+#define AU_NPIDMAP (int)DIV_ROUND_UP(PID_MAX_LIMIT, AU_PIDSTEP)
28146+struct au_si_pid {
28147+ unsigned long *pid_bitmap[AU_NPIDMAP];
28148+ struct mutex pid_mtx;
28149+};
28150+
1facf9fc 28151+struct au_branch;
28152+struct au_sbinfo {
28153+ /* nowait tasks in the system-wide workqueue */
28154+ struct au_nowait_tasks si_nowait;
28155+
b752ccd1
AM
28156+ /*
28157+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
28158+ * rwsem for au_sbinfo is necessary.
28159+ */
dece6358 28160+ struct au_rwsem si_rwsem;
1facf9fc 28161+
b752ccd1 28162+ /* prevent recursive locking in deleting inode */
febd17d6 28163+ struct au_si_pid au_si_pid;
b752ccd1 28164+
7f207e10 28165+ /*
523b37e3
AM
28166+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
28167+ * remount.
7f207e10 28168+ */
5afbbe0d 28169+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 28170+
1facf9fc 28171+ /* branch management */
28172+ unsigned int si_generation;
28173+
2000de60 28174+ /* see AuSi_ flags */
1facf9fc 28175+ unsigned char au_si_status;
28176+
5afbbe0d 28177+ aufs_bindex_t si_bbot;
7f207e10
AM
28178+
28179+ /* dirty trick to keep br_id plus */
28180+ unsigned int si_last_br_id :
28181+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 28182+ struct au_branch **si_branch;
28183+
28184+ /* policy to select a writable branch */
28185+ unsigned char si_wbr_copyup;
28186+ unsigned char si_wbr_create;
28187+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
28188+ struct au_wbr_create_operations *si_wbr_create_ops;
28189+
28190+ /* round robin */
28191+ atomic_t si_wbr_rr_next;
28192+
28193+ /* most free space */
28194+ struct au_wbr_mfs si_wbr_mfs;
28195+
076b876e
AM
28196+ /* File-based Hierarchical Storage Management */
28197+ struct au_fhsm si_fhsm;
28198+
1facf9fc 28199+ /* mount flags */
28200+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
28201+ unsigned int si_mntflags;
28202+
28203+ /* external inode number (bitmap and translation table) */
5527c038
JR
28204+ vfs_readf_t si_xread;
28205+ vfs_writef_t si_xwrite;
1facf9fc 28206+ struct file *si_xib;
28207+ struct mutex si_xib_mtx; /* protect xib members */
28208+ unsigned long *si_xib_buf;
28209+ unsigned long si_xib_last_pindex;
28210+ int si_xib_next_bit;
28211+ aufs_bindex_t si_xino_brid;
392086de
AM
28212+ unsigned long si_xino_jiffy;
28213+ unsigned long si_xino_expire;
1facf9fc 28214+ /* reserved for future use */
28215+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
28216+
28217+#ifdef CONFIG_AUFS_EXPORT
28218+ /* i_generation */
28219+ struct file *si_xigen;
28220+ atomic_t si_xigen_next;
28221+#endif
28222+
b912730e
AM
28223+ /* dirty trick to suppoer atomic_open */
28224+ struct au_sphlhead si_aopen;
28225+
1facf9fc 28226+ /* vdir parameters */
e49829fe 28227+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 28228+ unsigned int si_rdblk; /* deblk size */
28229+ unsigned int si_rdhash; /* hash size */
28230+
28231+ /*
28232+ * If the number of whiteouts are larger than si_dirwh, leave all of
28233+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
28234+ * future fsck.aufs or kernel thread will remove them later.
28235+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
28236+ */
28237+ unsigned int si_dirwh;
28238+
1facf9fc 28239+ /* pseudo_link list */
86dc4139 28240+ struct au_sphlhead si_plink[AuPlink_NHASH];
1facf9fc 28241+ wait_queue_head_t si_plink_wq;
4a4d8108 28242+ spinlock_t si_plink_maint_lock;
e49829fe 28243+ pid_t si_plink_maint_pid;
1facf9fc 28244+
523b37e3
AM
28245+ /* file list */
28246+ struct au_sphlhead si_files;
28247+
b95c5147
AM
28248+ /* with/without getattr, brother of sb->s_d_op */
28249+ struct inode_operations *si_iop_array;
28250+
1facf9fc 28251+ /*
28252+ * sysfs and lifetime management.
28253+ * this is not a small structure and it may be a waste of memory in case
28254+ * of sysfs is disabled, particulary when many aufs-es are mounted.
28255+ * but using sysfs is majority.
28256+ */
28257+ struct kobject si_kobj;
28258+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
28259+ struct dentry *si_dbgaufs;
28260+ struct dentry *si_dbgaufs_plink;
28261+ struct dentry *si_dbgaufs_xib;
1facf9fc 28262+#ifdef CONFIG_AUFS_EXPORT
28263+ struct dentry *si_dbgaufs_xigen;
28264+#endif
28265+#endif
28266+
e49829fe 28267+#ifdef CONFIG_AUFS_SBILIST
5afbbe0d 28268+ struct hlist_node si_list;
e49829fe
JR
28269+#endif
28270+
1facf9fc 28271+ /* dirty, necessary for unmounting, sysfs and sysrq */
28272+ struct super_block *si_sb;
28273+};
28274+
dece6358
AM
28275+/* sbinfo status flags */
28276+/*
28277+ * set true when refresh_dirs() failed at remount time.
28278+ * then try refreshing dirs at access time again.
28279+ * if it is false, refreshing dirs at access time is unnecesary
28280+ */
027c5e7a 28281+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 28282+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 28283+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
28284+
28285+#ifndef CONFIG_AUFS_FHSM
28286+#undef AuSi_FHSM
28287+#define AuSi_FHSM 0
28288+#endif
28289+
dece6358
AM
28290+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
28291+ unsigned int flag)
28292+{
28293+ AuRwMustAnyLock(&sbi->si_rwsem);
28294+ return sbi->au_si_status & flag;
28295+}
28296+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
28297+#define au_fset_si(sbinfo, name) do { \
28298+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28299+ (sbinfo)->au_si_status |= AuSi_##name; \
28300+} while (0)
28301+#define au_fclr_si(sbinfo, name) do { \
28302+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28303+ (sbinfo)->au_si_status &= ~AuSi_##name; \
28304+} while (0)
28305+
1facf9fc 28306+/* ---------------------------------------------------------------------- */
28307+
28308+/* policy to select one among writable branches */
4a4d8108
AM
28309+#define AuWbrCopyup(sbinfo, ...) \
28310+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
28311+#define AuWbrCreate(sbinfo, ...) \
28312+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 28313+
28314+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
28315+#define AuLock_DW 1 /* write-lock dentry */
28316+#define AuLock_IR (1 << 1) /* read-lock inode */
28317+#define AuLock_IW (1 << 2) /* write-lock inode */
28318+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 28319+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
e49829fe
JR
28320+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
28321+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 28322+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 28323+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
28324+#define au_fset_lock(flags, name) \
28325+ do { (flags) |= AuLock_##name; } while (0)
28326+#define au_fclr_lock(flags, name) \
28327+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 28328+
28329+/* ---------------------------------------------------------------------- */
28330+
28331+/* super.c */
28332+extern struct file_system_type aufs_fs_type;
28333+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
28334+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
28335+ unsigned long long max, void *arg);
79b8bda9
AM
28336+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
28337+ struct super_block *sb, void *arg);
7f207e10
AM
28338+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
28339+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 28340+
28341+/* sbinfo.c */
28342+void au_si_free(struct kobject *kobj);
28343+int au_si_alloc(struct super_block *sb);
28344+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
28345+
28346+unsigned int au_sigen_inc(struct super_block *sb);
28347+aufs_bindex_t au_new_br_id(struct super_block *sb);
28348+
e49829fe
JR
28349+int si_read_lock(struct super_block *sb, int flags);
28350+int si_write_lock(struct super_block *sb, int flags);
28351+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 28352+void aufs_read_unlock(struct dentry *dentry, int flags);
28353+void aufs_write_lock(struct dentry *dentry);
28354+void aufs_write_unlock(struct dentry *dentry);
e49829fe 28355+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 28356+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
28357+
28358+/* wbr_policy.c */
28359+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
28360+extern struct au_wbr_create_operations au_wbr_create_ops[];
28361+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 28362+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 28363+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
28364+
28365+/* mvdown.c */
28366+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 28367+
076b876e
AM
28368+#ifdef CONFIG_AUFS_FHSM
28369+/* fhsm.c */
28370+
28371+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
28372+{
28373+ pid_t pid;
28374+
28375+ spin_lock(&fhsm->fhsm_spin);
28376+ pid = fhsm->fhsm_pid;
28377+ spin_unlock(&fhsm->fhsm_spin);
28378+
28379+ return pid;
28380+}
28381+
28382+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
28383+void au_fhsm_wrote_all(struct super_block *sb, int force);
28384+int au_fhsm_fd(struct super_block *sb, int oflags);
28385+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 28386+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
28387+void au_fhsm_fin(struct super_block *sb);
28388+void au_fhsm_init(struct au_sbinfo *sbinfo);
28389+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
28390+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
28391+#else
28392+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
28393+ int force)
28394+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
28395+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
28396+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
28397+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
28398+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
28399+AuStubVoid(au_fhsm_fin, struct super_block *sb)
28400+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
28401+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
28402+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
28403+#endif
28404+
1facf9fc 28405+/* ---------------------------------------------------------------------- */
28406+
28407+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
28408+{
28409+ return sb->s_fs_info;
28410+}
28411+
28412+/* ---------------------------------------------------------------------- */
28413+
28414+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 28415+int au_test_nfsd(void);
1facf9fc 28416+void au_export_init(struct super_block *sb);
b752ccd1 28417+void au_xigen_inc(struct inode *inode);
1facf9fc 28418+int au_xigen_new(struct inode *inode);
28419+int au_xigen_set(struct super_block *sb, struct file *base);
28420+void au_xigen_clr(struct super_block *sb);
28421+
28422+static inline int au_busy_or_stale(void)
28423+{
b752ccd1 28424+ if (!au_test_nfsd())
1facf9fc 28425+ return -EBUSY;
28426+ return -ESTALE;
28427+}
28428+#else
b752ccd1 28429+AuStubInt0(au_test_nfsd, void)
a2a7ad62 28430+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 28431+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108
AM
28432+AuStubInt0(au_xigen_new, struct inode *inode)
28433+AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
28434+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 28435+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 28436+#endif /* CONFIG_AUFS_EXPORT */
28437+
28438+/* ---------------------------------------------------------------------- */
28439+
e49829fe
JR
28440+#ifdef CONFIG_AUFS_SBILIST
28441+/* module.c */
5afbbe0d 28442+extern struct au_sphlhead au_sbilist;
e49829fe
JR
28443+
28444+static inline void au_sbilist_init(void)
28445+{
5afbbe0d 28446+ au_sphl_init(&au_sbilist);
e49829fe
JR
28447+}
28448+
28449+static inline void au_sbilist_add(struct super_block *sb)
28450+{
5afbbe0d 28451+ au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
28452+}
28453+
28454+static inline void au_sbilist_del(struct super_block *sb)
28455+{
5afbbe0d 28456+ au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 28457+}
53392da6
AM
28458+
28459+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
28460+static inline void au_sbilist_lock(void)
28461+{
28462+ spin_lock(&au_sbilist.spin);
28463+}
28464+
28465+static inline void au_sbilist_unlock(void)
28466+{
28467+ spin_unlock(&au_sbilist.spin);
28468+}
28469+#define AuGFP_SBILIST GFP_ATOMIC
28470+#else
28471+AuStubVoid(au_sbilist_lock, void)
28472+AuStubVoid(au_sbilist_unlock, void)
28473+#define AuGFP_SBILIST GFP_NOFS
28474+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
28475+#else
28476+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
28477+AuStubVoid(au_sbilist_add, struct super_block *sb)
28478+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
28479+AuStubVoid(au_sbilist_lock, void)
28480+AuStubVoid(au_sbilist_unlock, void)
28481+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
28482+#endif
28483+
28484+/* ---------------------------------------------------------------------- */
28485+
1facf9fc 28486+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28487+{
dece6358 28488+ /*
c1595e42 28489+ * This function is a dynamic '__init' function actually,
dece6358
AM
28490+ * so the tiny check for si_rwsem is unnecessary.
28491+ */
28492+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 28493+#ifdef CONFIG_DEBUG_FS
28494+ sbinfo->si_dbgaufs = NULL;
86dc4139 28495+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 28496+ sbinfo->si_dbgaufs_xib = NULL;
28497+#ifdef CONFIG_AUFS_EXPORT
28498+ sbinfo->si_dbgaufs_xigen = NULL;
28499+#endif
28500+#endif
28501+}
28502+
28503+/* ---------------------------------------------------------------------- */
28504+
febd17d6 28505+static inline void si_pid_idx_bit(int *idx, pid_t *bit)
b752ccd1
AM
28506+{
28507+ /* the origin of pid is 1, but the bitmap's is 0 */
febd17d6
JR
28508+ *bit = current->pid - 1;
28509+ *idx = *bit / AU_PIDSTEP;
28510+ *bit %= AU_PIDSTEP;
b752ccd1
AM
28511+}
28512+
28513+static inline int si_pid_test(struct super_block *sb)
28514+{
076b876e 28515+ pid_t bit;
febd17d6
JR
28516+ int idx;
28517+ unsigned long *bitmap;
076b876e 28518+
febd17d6
JR
28519+ si_pid_idx_bit(&idx, &bit);
28520+ bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
28521+ if (bitmap)
28522+ return test_bit(bit, bitmap);
28523+ return 0;
b752ccd1
AM
28524+}
28525+
28526+static inline void si_pid_clr(struct super_block *sb)
28527+{
076b876e 28528+ pid_t bit;
febd17d6
JR
28529+ int idx;
28530+ unsigned long *bitmap;
076b876e 28531+
febd17d6
JR
28532+ si_pid_idx_bit(&idx, &bit);
28533+ bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
28534+ BUG_ON(!bitmap);
28535+ AuDebugOn(!test_bit(bit, bitmap));
28536+ clear_bit(bit, bitmap);
28537+ /* smp_mb(); */
b752ccd1
AM
28538+}
28539+
febd17d6
JR
28540+void si_pid_set(struct super_block *sb);
28541+
b752ccd1
AM
28542+/* ---------------------------------------------------------------------- */
28543+
1facf9fc 28544+/* lock superblock. mainly for entry point functions */
28545+/*
b752ccd1
AM
28546+ * __si_read_lock, __si_write_lock,
28547+ * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
1facf9fc 28548+ */
b752ccd1 28549+AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
1facf9fc 28550+
dece6358
AM
28551+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28552+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28553+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28554+
b752ccd1
AM
28555+static inline void si_noflush_read_lock(struct super_block *sb)
28556+{
28557+ __si_read_lock(sb);
28558+ si_pid_set(sb);
28559+}
28560+
28561+static inline int si_noflush_read_trylock(struct super_block *sb)
28562+{
076b876e
AM
28563+ int locked;
28564+
28565+ locked = __si_read_trylock(sb);
b752ccd1
AM
28566+ if (locked)
28567+ si_pid_set(sb);
28568+ return locked;
28569+}
28570+
28571+static inline void si_noflush_write_lock(struct super_block *sb)
28572+{
28573+ __si_write_lock(sb);
28574+ si_pid_set(sb);
28575+}
28576+
28577+static inline int si_noflush_write_trylock(struct super_block *sb)
28578+{
076b876e
AM
28579+ int locked;
28580+
28581+ locked = __si_write_trylock(sb);
b752ccd1
AM
28582+ if (locked)
28583+ si_pid_set(sb);
28584+ return locked;
28585+}
28586+
7e9cd9fe 28587+#if 0 /* reserved */
1facf9fc 28588+static inline int si_read_trylock(struct super_block *sb, int flags)
28589+{
28590+ if (au_ftest_lock(flags, FLUSH))
28591+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28592+ return si_noflush_read_trylock(sb);
28593+}
e49829fe 28594+#endif
1facf9fc 28595+
b752ccd1
AM
28596+static inline void si_read_unlock(struct super_block *sb)
28597+{
28598+ si_pid_clr(sb);
28599+ __si_read_unlock(sb);
28600+}
28601+
7e9cd9fe 28602+#if 0 /* reserved */
1facf9fc 28603+static inline int si_write_trylock(struct super_block *sb, int flags)
28604+{
28605+ if (au_ftest_lock(flags, FLUSH))
28606+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28607+ return si_noflush_write_trylock(sb);
28608+}
b752ccd1
AM
28609+#endif
28610+
28611+static inline void si_write_unlock(struct super_block *sb)
28612+{
28613+ si_pid_clr(sb);
28614+ __si_write_unlock(sb);
28615+}
28616+
7e9cd9fe 28617+#if 0 /* reserved */
b752ccd1
AM
28618+static inline void si_downgrade_lock(struct super_block *sb)
28619+{
28620+ __si_downgrade_lock(sb);
28621+}
28622+#endif
1facf9fc 28623+
28624+/* ---------------------------------------------------------------------- */
28625+
5afbbe0d 28626+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 28627+{
dece6358 28628+ SiMustAnyLock(sb);
5afbbe0d 28629+ return au_sbi(sb)->si_bbot;
1facf9fc 28630+}
28631+
28632+static inline unsigned int au_mntflags(struct super_block *sb)
28633+{
dece6358 28634+ SiMustAnyLock(sb);
1facf9fc 28635+ return au_sbi(sb)->si_mntflags;
28636+}
28637+
28638+static inline unsigned int au_sigen(struct super_block *sb)
28639+{
dece6358 28640+ SiMustAnyLock(sb);
1facf9fc 28641+ return au_sbi(sb)->si_generation;
28642+}
28643+
5afbbe0d
AM
28644+static inline unsigned long long au_ninodes(struct super_block *sb)
28645+{
28646+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
28647+
28648+ BUG_ON(n < 0);
28649+ return n;
28650+}
28651+
7f207e10
AM
28652+static inline void au_ninodes_inc(struct super_block *sb)
28653+{
5afbbe0d 28654+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
28655+}
28656+
28657+static inline void au_ninodes_dec(struct super_block *sb)
28658+{
5afbbe0d
AM
28659+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
28660+}
28661+
28662+static inline unsigned long long au_nfiles(struct super_block *sb)
28663+{
28664+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
28665+
28666+ BUG_ON(n < 0);
28667+ return n;
7f207e10
AM
28668+}
28669+
28670+static inline void au_nfiles_inc(struct super_block *sb)
28671+{
5afbbe0d 28672+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
28673+}
28674+
28675+static inline void au_nfiles_dec(struct super_block *sb)
28676+{
5afbbe0d 28677+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
28678+}
28679+
1facf9fc 28680+static inline struct au_branch *au_sbr(struct super_block *sb,
28681+ aufs_bindex_t bindex)
28682+{
dece6358 28683+ SiMustAnyLock(sb);
1facf9fc 28684+ return au_sbi(sb)->si_branch[0 + bindex];
28685+}
28686+
28687+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28688+{
dece6358 28689+ SiMustWriteLock(sb);
1facf9fc 28690+ au_sbi(sb)->si_xino_brid = brid;
28691+}
28692+
28693+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28694+{
dece6358 28695+ SiMustAnyLock(sb);
1facf9fc 28696+ return au_sbi(sb)->si_xino_brid;
28697+}
28698+
28699+#endif /* __KERNEL__ */
28700+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
28701diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28702--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 28703+++ linux/fs/aufs/sysaufs.c 2016-07-25 19:05:34.817826663 +0200
523b37e3 28704@@ -0,0 +1,104 @@
1facf9fc 28705+/*
8cdd5066 28706+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28707+ *
28708+ * This program, aufs is free software; you can redistribute it and/or modify
28709+ * it under the terms of the GNU General Public License as published by
28710+ * the Free Software Foundation; either version 2 of the License, or
28711+ * (at your option) any later version.
dece6358
AM
28712+ *
28713+ * This program is distributed in the hope that it will be useful,
28714+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28715+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28716+ * GNU General Public License for more details.
28717+ *
28718+ * You should have received a copy of the GNU General Public License
523b37e3 28719+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28720+ */
28721+
28722+/*
28723+ * sysfs interface and lifetime management
28724+ * they are necessary regardless sysfs is disabled.
28725+ */
28726+
1facf9fc 28727+#include <linux/random.h>
1facf9fc 28728+#include "aufs.h"
28729+
28730+unsigned long sysaufs_si_mask;
e49829fe 28731+struct kset *sysaufs_kset;
1facf9fc 28732+
28733+#define AuSiAttr(_name) { \
28734+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
28735+ .show = sysaufs_si_##_name, \
28736+}
28737+
28738+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28739+struct attribute *sysaufs_si_attrs[] = {
28740+ &sysaufs_si_attr_xi_path.attr,
28741+ NULL,
28742+};
28743+
4a4d8108 28744+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 28745+ .show = sysaufs_si_show
28746+};
28747+
28748+static struct kobj_type au_sbi_ktype = {
28749+ .release = au_si_free,
28750+ .sysfs_ops = &au_sbi_ops,
28751+ .default_attrs = sysaufs_si_attrs
28752+};
28753+
28754+/* ---------------------------------------------------------------------- */
28755+
28756+int sysaufs_si_init(struct au_sbinfo *sbinfo)
28757+{
28758+ int err;
28759+
e49829fe 28760+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 28761+ /* cf. sysaufs_name() */
28762+ err = kobject_init_and_add
e49829fe 28763+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 28764+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28765+
28766+ dbgaufs_si_null(sbinfo);
28767+ if (!err) {
28768+ err = dbgaufs_si_init(sbinfo);
28769+ if (unlikely(err))
28770+ kobject_put(&sbinfo->si_kobj);
28771+ }
28772+ return err;
28773+}
28774+
28775+void sysaufs_fin(void)
28776+{
28777+ dbgaufs_fin();
e49829fe
JR
28778+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28779+ kset_unregister(sysaufs_kset);
1facf9fc 28780+}
28781+
28782+int __init sysaufs_init(void)
28783+{
28784+ int err;
28785+
28786+ do {
28787+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28788+ } while (!sysaufs_si_mask);
28789+
4a4d8108 28790+ err = -EINVAL;
e49829fe
JR
28791+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28792+ if (unlikely(!sysaufs_kset))
4a4d8108 28793+ goto out;
e49829fe
JR
28794+ err = PTR_ERR(sysaufs_kset);
28795+ if (IS_ERR(sysaufs_kset))
1facf9fc 28796+ goto out;
e49829fe 28797+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
1facf9fc 28798+ if (unlikely(err)) {
e49829fe 28799+ kset_unregister(sysaufs_kset);
1facf9fc 28800+ goto out;
28801+ }
28802+
28803+ err = dbgaufs_init();
28804+ if (unlikely(err))
28805+ sysaufs_fin();
4f0767ce 28806+out:
1facf9fc 28807+ return err;
28808+}
7f207e10
AM
28809diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28810--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 28811+++ linux/fs/aufs/sysaufs.h 2016-07-25 19:05:34.817826663 +0200
c1595e42 28812@@ -0,0 +1,101 @@
1facf9fc 28813+/*
8cdd5066 28814+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28815+ *
28816+ * This program, aufs is free software; you can redistribute it and/or modify
28817+ * it under the terms of the GNU General Public License as published by
28818+ * the Free Software Foundation; either version 2 of the License, or
28819+ * (at your option) any later version.
dece6358
AM
28820+ *
28821+ * This program is distributed in the hope that it will be useful,
28822+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28823+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28824+ * GNU General Public License for more details.
28825+ *
28826+ * You should have received a copy of the GNU General Public License
523b37e3 28827+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28828+ */
28829+
28830+/*
28831+ * sysfs interface and mount lifetime management
28832+ */
28833+
28834+#ifndef __SYSAUFS_H__
28835+#define __SYSAUFS_H__
28836+
28837+#ifdef __KERNEL__
28838+
1facf9fc 28839+#include <linux/sysfs.h>
1facf9fc 28840+#include "module.h"
28841+
dece6358
AM
28842+struct super_block;
28843+struct au_sbinfo;
28844+
1facf9fc 28845+struct sysaufs_si_attr {
28846+ struct attribute attr;
28847+ int (*show)(struct seq_file *seq, struct super_block *sb);
28848+};
28849+
28850+/* ---------------------------------------------------------------------- */
28851+
28852+/* sysaufs.c */
28853+extern unsigned long sysaufs_si_mask;
e49829fe 28854+extern struct kset *sysaufs_kset;
1facf9fc 28855+extern struct attribute *sysaufs_si_attrs[];
28856+int sysaufs_si_init(struct au_sbinfo *sbinfo);
28857+int __init sysaufs_init(void);
28858+void sysaufs_fin(void);
28859+
28860+/* ---------------------------------------------------------------------- */
28861+
28862+/* some people doesn't like to show a pointer in kernel */
28863+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
28864+{
28865+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
28866+}
28867+
28868+#define SysaufsSiNamePrefix "si_"
28869+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
28870+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
28871+{
28872+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
28873+ sysaufs_si_id(sbinfo));
28874+}
28875+
28876+struct au_branch;
28877+#ifdef CONFIG_SYSFS
28878+/* sysfs.c */
28879+extern struct attribute_group *sysaufs_attr_group;
28880+
28881+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
28882+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28883+ char *buf);
076b876e
AM
28884+long au_brinfo_ioctl(struct file *file, unsigned long arg);
28885+#ifdef CONFIG_COMPAT
28886+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
28887+#endif
1facf9fc 28888+
28889+void sysaufs_br_init(struct au_branch *br);
28890+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
28891+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
28892+
28893+#define sysaufs_brs_init() do {} while (0)
28894+
28895+#else
28896+#define sysaufs_attr_group NULL
28897+
4a4d8108 28898+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
28899+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
28900+ struct attribute *attr, char *buf)
4a4d8108
AM
28901+AuStubVoid(sysaufs_br_init, struct au_branch *br)
28902+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
28903+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 28904+
28905+static inline void sysaufs_brs_init(void)
28906+{
28907+ sysaufs_brs = 0;
28908+}
28909+
28910+#endif /* CONFIG_SYSFS */
28911+
28912+#endif /* __KERNEL__ */
28913+#endif /* __SYSAUFS_H__ */
7f207e10
AM
28914diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
28915--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 28916+++ linux/fs/aufs/sysfs.c 2016-08-17 18:01:06.161889951 +0200
79b8bda9 28917@@ -0,0 +1,376 @@
1facf9fc 28918+/*
8cdd5066 28919+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28920+ *
28921+ * This program, aufs is free software; you can redistribute it and/or modify
28922+ * it under the terms of the GNU General Public License as published by
28923+ * the Free Software Foundation; either version 2 of the License, or
28924+ * (at your option) any later version.
dece6358
AM
28925+ *
28926+ * This program is distributed in the hope that it will be useful,
28927+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28928+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28929+ * GNU General Public License for more details.
28930+ *
28931+ * You should have received a copy of the GNU General Public License
523b37e3 28932+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28933+ */
28934+
28935+/*
28936+ * sysfs interface
28937+ */
28938+
076b876e 28939+#include <linux/compat.h>
1facf9fc 28940+#include <linux/seq_file.h>
1facf9fc 28941+#include "aufs.h"
28942+
4a4d8108
AM
28943+#ifdef CONFIG_AUFS_FS_MODULE
28944+/* this entry violates the "one line per file" policy of sysfs */
28945+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28946+ char *buf)
28947+{
28948+ ssize_t err;
28949+ static char *conf =
28950+/* this file is generated at compiling */
28951+#include "conf.str"
28952+ ;
28953+
28954+ err = snprintf(buf, PAGE_SIZE, conf);
28955+ if (unlikely(err >= PAGE_SIZE))
28956+ err = -EFBIG;
28957+ return err;
28958+}
28959+
28960+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28961+#endif
28962+
1facf9fc 28963+static struct attribute *au_attr[] = {
4a4d8108
AM
28964+#ifdef CONFIG_AUFS_FS_MODULE
28965+ &au_config_attr.attr,
28966+#endif
1facf9fc 28967+ NULL, /* need to NULL terminate the list of attributes */
28968+};
28969+
28970+static struct attribute_group sysaufs_attr_group_body = {
28971+ .attrs = au_attr
28972+};
28973+
28974+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28975+
28976+/* ---------------------------------------------------------------------- */
28977+
28978+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28979+{
28980+ int err;
28981+
dece6358
AM
28982+ SiMustAnyLock(sb);
28983+
1facf9fc 28984+ err = 0;
28985+ if (au_opt_test(au_mntflags(sb), XINO)) {
28986+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
28987+ seq_putc(seq, '\n');
28988+ }
28989+ return err;
28990+}
28991+
28992+/*
28993+ * the lifetime of branch is independent from the entry under sysfs.
28994+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
28995+ * unlinked.
28996+ */
28997+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 28998+ aufs_bindex_t bindex, int idx)
1facf9fc 28999+{
1e00d052 29000+ int err;
1facf9fc 29001+ struct path path;
29002+ struct dentry *root;
29003+ struct au_branch *br;
076b876e 29004+ au_br_perm_str_t perm;
1facf9fc 29005+
29006+ AuDbg("b%d\n", bindex);
29007+
1e00d052 29008+ err = 0;
1facf9fc 29009+ root = sb->s_root;
29010+ di_read_lock_parent(root, !AuLock_IR);
29011+ br = au_sbr(sb, bindex);
392086de
AM
29012+
29013+ switch (idx) {
29014+ case AuBrSysfs_BR:
29015+ path.mnt = au_br_mnt(br);
29016+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
29017+ err = au_seq_path(seq, &path);
29018+ if (!err) {
29019+ au_optstr_br_perm(&perm, br->br_perm);
29020+ seq_printf(seq, "=%s\n", perm.a);
29021+ }
392086de
AM
29022+ break;
29023+ case AuBrSysfs_BRID:
79b8bda9 29024+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
29025+ break;
29026+ }
076b876e 29027+ di_read_unlock(root, !AuLock_IR);
79b8bda9 29028+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 29029+ err = -E2BIG;
392086de 29030+
1e00d052 29031+ return err;
1facf9fc 29032+}
29033+
29034+/* ---------------------------------------------------------------------- */
29035+
29036+static struct seq_file *au_seq(char *p, ssize_t len)
29037+{
29038+ struct seq_file *seq;
29039+
29040+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
29041+ if (seq) {
29042+ /* mutex_init(&seq.lock); */
29043+ seq->buf = p;
29044+ seq->size = len;
29045+ return seq; /* success */
29046+ }
29047+
29048+ seq = ERR_PTR(-ENOMEM);
29049+ return seq;
29050+}
29051+
392086de
AM
29052+#define SysaufsBr_PREFIX "br"
29053+#define SysaufsBrid_PREFIX "brid"
1facf9fc 29054+
29055+/* todo: file size may exceed PAGE_SIZE */
29056+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 29057+ char *buf)
1facf9fc 29058+{
29059+ ssize_t err;
392086de 29060+ int idx;
1facf9fc 29061+ long l;
5afbbe0d 29062+ aufs_bindex_t bbot;
1facf9fc 29063+ struct au_sbinfo *sbinfo;
29064+ struct super_block *sb;
29065+ struct seq_file *seq;
29066+ char *name;
29067+ struct attribute **cattr;
29068+
29069+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29070+ sb = sbinfo->si_sb;
1308ab2a 29071+
29072+ /*
29073+ * prevent a race condition between sysfs and aufs.
29074+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
29075+ * prohibits maintaining the sysfs entries.
29076+ * hew we acquire read lock after sysfs_get_active_two().
29077+ * on the other hand, the remount process may maintain the sysfs/aufs
29078+ * entries after acquiring write lock.
29079+ * it can cause a deadlock.
29080+ * simply we gave up processing read here.
29081+ */
29082+ err = -EBUSY;
29083+ if (unlikely(!si_noflush_read_trylock(sb)))
29084+ goto out;
1facf9fc 29085+
29086+ seq = au_seq(buf, PAGE_SIZE);
29087+ err = PTR_ERR(seq);
29088+ if (IS_ERR(seq))
1308ab2a 29089+ goto out_unlock;
1facf9fc 29090+
29091+ name = (void *)attr->name;
29092+ cattr = sysaufs_si_attrs;
29093+ while (*cattr) {
29094+ if (!strcmp(name, (*cattr)->name)) {
29095+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
29096+ ->show(seq, sb);
29097+ goto out_seq;
29098+ }
29099+ cattr++;
29100+ }
29101+
392086de
AM
29102+ if (!strncmp(name, SysaufsBrid_PREFIX,
29103+ sizeof(SysaufsBrid_PREFIX) - 1)) {
29104+ idx = AuBrSysfs_BRID;
29105+ name += sizeof(SysaufsBrid_PREFIX) - 1;
29106+ } else if (!strncmp(name, SysaufsBr_PREFIX,
29107+ sizeof(SysaufsBr_PREFIX) - 1)) {
29108+ idx = AuBrSysfs_BR;
1facf9fc 29109+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
29110+ } else
29111+ BUG();
29112+
29113+ err = kstrtol(name, 10, &l);
29114+ if (!err) {
5afbbe0d
AM
29115+ bbot = au_sbbot(sb);
29116+ if (l <= bbot)
392086de
AM
29117+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
29118+ else
29119+ err = -ENOENT;
1facf9fc 29120+ }
1facf9fc 29121+
4f0767ce 29122+out_seq:
1facf9fc 29123+ if (!err) {
29124+ err = seq->count;
29125+ /* sysfs limit */
29126+ if (unlikely(err == PAGE_SIZE))
29127+ err = -EFBIG;
29128+ }
f0c0a007 29129+ au_delayed_kfree(seq);
4f0767ce 29130+out_unlock:
1facf9fc 29131+ si_read_unlock(sb);
4f0767ce 29132+out:
1facf9fc 29133+ return err;
29134+}
29135+
29136+/* ---------------------------------------------------------------------- */
29137+
076b876e
AM
29138+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
29139+{
29140+ int err;
29141+ int16_t brid;
5afbbe0d 29142+ aufs_bindex_t bindex, bbot;
076b876e
AM
29143+ size_t sz;
29144+ char *buf;
29145+ struct seq_file *seq;
29146+ struct au_branch *br;
29147+
29148+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
29149+ bbot = au_sbbot(sb);
29150+ err = bbot + 1;
076b876e
AM
29151+ if (!arg)
29152+ goto out;
29153+
29154+ err = -ENOMEM;
29155+ buf = (void *)__get_free_page(GFP_NOFS);
29156+ if (unlikely(!buf))
29157+ goto out;
29158+
29159+ seq = au_seq(buf, PAGE_SIZE);
29160+ err = PTR_ERR(seq);
29161+ if (IS_ERR(seq))
29162+ goto out_buf;
29163+
29164+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 29165+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
29166+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
29167+ if (unlikely(err))
29168+ break;
29169+
29170+ br = au_sbr(sb, bindex);
29171+ brid = br->br_id;
29172+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
29173+ err = __put_user(brid, &arg->id);
29174+ if (unlikely(err))
29175+ break;
29176+
29177+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
29178+ err = __put_user(br->br_perm, &arg->perm);
29179+ if (unlikely(err))
29180+ break;
29181+
79b8bda9
AM
29182+ err = au_seq_path(seq, &br->br_path);
29183+ if (unlikely(err))
29184+ break;
29185+ seq_putc(seq, '\0');
29186+ if (!seq_has_overflowed(seq)) {
076b876e
AM
29187+ err = copy_to_user(arg->path, seq->buf, seq->count);
29188+ seq->count = 0;
29189+ if (unlikely(err))
29190+ break;
29191+ } else {
29192+ err = -E2BIG;
29193+ goto out_seq;
29194+ }
29195+ }
29196+ if (unlikely(err))
29197+ err = -EFAULT;
29198+
29199+out_seq:
f0c0a007 29200+ au_delayed_kfree(seq);
076b876e 29201+out_buf:
f0c0a007 29202+ au_delayed_free_page((unsigned long)buf);
076b876e
AM
29203+out:
29204+ si_read_unlock(sb);
29205+ return err;
29206+}
29207+
29208+long au_brinfo_ioctl(struct file *file, unsigned long arg)
29209+{
2000de60 29210+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
29211+}
29212+
29213+#ifdef CONFIG_COMPAT
29214+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
29215+{
2000de60 29216+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
29217+}
29218+#endif
29219+
29220+/* ---------------------------------------------------------------------- */
29221+
1facf9fc 29222+void sysaufs_br_init(struct au_branch *br)
29223+{
392086de
AM
29224+ int i;
29225+ struct au_brsysfs *br_sysfs;
29226+ struct attribute *attr;
4a4d8108 29227+
392086de
AM
29228+ br_sysfs = br->br_sysfs;
29229+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29230+ attr = &br_sysfs->attr;
29231+ sysfs_attr_init(attr);
29232+ attr->name = br_sysfs->name;
29233+ attr->mode = S_IRUGO;
29234+ br_sysfs++;
29235+ }
1facf9fc 29236+}
29237+
29238+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
29239+{
29240+ struct au_branch *br;
29241+ struct kobject *kobj;
392086de
AM
29242+ struct au_brsysfs *br_sysfs;
29243+ int i;
5afbbe0d 29244+ aufs_bindex_t bbot;
1facf9fc 29245+
29246+ dbgaufs_brs_del(sb, bindex);
29247+
29248+ if (!sysaufs_brs)
29249+ return;
29250+
29251+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
29252+ bbot = au_sbbot(sb);
29253+ for (; bindex <= bbot; bindex++) {
1facf9fc 29254+ br = au_sbr(sb, bindex);
392086de
AM
29255+ br_sysfs = br->br_sysfs;
29256+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29257+ sysfs_remove_file(kobj, &br_sysfs->attr);
29258+ br_sysfs++;
29259+ }
1facf9fc 29260+ }
29261+}
29262+
29263+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
29264+{
392086de 29265+ int err, i;
5afbbe0d 29266+ aufs_bindex_t bbot;
1facf9fc 29267+ struct kobject *kobj;
29268+ struct au_branch *br;
392086de 29269+ struct au_brsysfs *br_sysfs;
1facf9fc 29270+
29271+ dbgaufs_brs_add(sb, bindex);
29272+
29273+ if (!sysaufs_brs)
29274+ return;
29275+
29276+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
29277+ bbot = au_sbbot(sb);
29278+ for (; bindex <= bbot; bindex++) {
1facf9fc 29279+ br = au_sbr(sb, bindex);
392086de
AM
29280+ br_sysfs = br->br_sysfs;
29281+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
29282+ SysaufsBr_PREFIX "%d", bindex);
29283+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
29284+ SysaufsBrid_PREFIX "%d", bindex);
29285+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29286+ err = sysfs_create_file(kobj, &br_sysfs->attr);
29287+ if (unlikely(err))
29288+ pr_warn("failed %s under sysfs(%d)\n",
29289+ br_sysfs->name, err);
29290+ br_sysfs++;
29291+ }
1facf9fc 29292+ }
29293+}
7f207e10
AM
29294diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
29295--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 29296+++ linux/fs/aufs/sysrq.c 2016-07-25 19:05:34.817826663 +0200
076b876e 29297@@ -0,0 +1,157 @@
1facf9fc 29298+/*
8cdd5066 29299+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 29300+ *
29301+ * This program, aufs is free software; you can redistribute it and/or modify
29302+ * it under the terms of the GNU General Public License as published by
29303+ * the Free Software Foundation; either version 2 of the License, or
29304+ * (at your option) any later version.
dece6358
AM
29305+ *
29306+ * This program is distributed in the hope that it will be useful,
29307+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29308+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29309+ * GNU General Public License for more details.
29310+ *
29311+ * You should have received a copy of the GNU General Public License
523b37e3 29312+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29313+ */
29314+
29315+/*
29316+ * magic sysrq hanlder
29317+ */
29318+
1facf9fc 29319+/* #include <linux/sysrq.h> */
027c5e7a 29320+#include <linux/writeback.h>
1facf9fc 29321+#include "aufs.h"
29322+
29323+/* ---------------------------------------------------------------------- */
29324+
29325+static void sysrq_sb(struct super_block *sb)
29326+{
29327+ char *plevel;
29328+ struct au_sbinfo *sbinfo;
29329+ struct file *file;
523b37e3
AM
29330+ struct au_sphlhead *files;
29331+ struct au_finfo *finfo;
1facf9fc 29332+
29333+ plevel = au_plevel;
29334+ au_plevel = KERN_WARNING;
1facf9fc 29335+
4a4d8108 29336+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
29337+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
29338+
29339+ sbinfo = au_sbi(sb);
4a4d8108 29340+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 29341+ pr("superblock\n");
1facf9fc 29342+ au_dpri_sb(sb);
027c5e7a
AM
29343+
29344+#if 0
c06a8ce3 29345+ pr("root dentry\n");
1facf9fc 29346+ au_dpri_dentry(sb->s_root);
c06a8ce3 29347+ pr("root inode\n");
5527c038 29348+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
29349+#endif
29350+
1facf9fc 29351+#if 0
027c5e7a
AM
29352+ do {
29353+ int err, i, j, ndentry;
29354+ struct au_dcsub_pages dpages;
29355+ struct au_dpage *dpage;
29356+
29357+ err = au_dpages_init(&dpages, GFP_ATOMIC);
29358+ if (unlikely(err))
29359+ break;
29360+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
29361+ if (!err)
29362+ for (i = 0; i < dpages.ndpage; i++) {
29363+ dpage = dpages.dpages + i;
29364+ ndentry = dpage->ndentry;
29365+ for (j = 0; j < ndentry; j++)
29366+ au_dpri_dentry(dpage->dentries[j]);
29367+ }
29368+ au_dpages_free(&dpages);
29369+ } while (0);
29370+#endif
29371+
29372+#if 1
29373+ {
29374+ struct inode *i;
076b876e 29375+
c06a8ce3 29376+ pr("isolated inode\n");
79b8bda9 29377+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
29378+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
29379+ spin_lock(&i->i_lock);
b4510431 29380+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 29381+ au_dpri_inode(i);
2cbb1c4b
JR
29382+ spin_unlock(&i->i_lock);
29383+ }
79b8bda9 29384+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 29385+ }
1facf9fc 29386+#endif
c06a8ce3 29387+ pr("files\n");
523b37e3
AM
29388+ files = &au_sbi(sb)->si_files;
29389+ spin_lock(&files->spin);
29390+ hlist_for_each_entry(finfo, &files->head, fi_hlist) {
4a4d8108 29391+ umode_t mode;
076b876e 29392+
523b37e3 29393+ file = finfo->fi_file;
c06a8ce3 29394+ mode = file_inode(file)->i_mode;
38d290e6 29395+ if (!special_file(mode))
1facf9fc 29396+ au_dpri_file(file);
523b37e3
AM
29397+ }
29398+ spin_unlock(&files->spin);
c06a8ce3 29399+ pr("done\n");
1facf9fc 29400+
c06a8ce3 29401+#undef pr
1facf9fc 29402+ au_plevel = plevel;
1facf9fc 29403+}
29404+
29405+/* ---------------------------------------------------------------------- */
29406+
29407+/* module parameter */
29408+static char *aufs_sysrq_key = "a";
29409+module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
29410+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
29411+
0c5527e5 29412+static void au_sysrq(int key __maybe_unused)
1facf9fc 29413+{
1facf9fc 29414+ struct au_sbinfo *sbinfo;
29415+
027c5e7a 29416+ lockdep_off();
53392da6 29417+ au_sbilist_lock();
5afbbe0d 29418+ hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
1facf9fc 29419+ sysrq_sb(sbinfo->si_sb);
53392da6 29420+ au_sbilist_unlock();
027c5e7a 29421+ lockdep_on();
1facf9fc 29422+}
29423+
29424+static struct sysrq_key_op au_sysrq_op = {
29425+ .handler = au_sysrq,
29426+ .help_msg = "Aufs",
29427+ .action_msg = "Aufs",
29428+ .enable_mask = SYSRQ_ENABLE_DUMP
29429+};
29430+
29431+/* ---------------------------------------------------------------------- */
29432+
29433+int __init au_sysrq_init(void)
29434+{
29435+ int err;
29436+ char key;
29437+
29438+ err = -1;
29439+ key = *aufs_sysrq_key;
29440+ if ('a' <= key && key <= 'z')
29441+ err = register_sysrq_key(key, &au_sysrq_op);
29442+ if (unlikely(err))
4a4d8108 29443+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 29444+ return err;
29445+}
29446+
29447+void au_sysrq_fin(void)
29448+{
29449+ int err;
076b876e 29450+
1facf9fc 29451+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
29452+ if (unlikely(err))
4a4d8108 29453+ pr_err("err %d (ignored)\n", err);
1facf9fc 29454+}
7f207e10
AM
29455diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
29456--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
29457+++ linux/fs/aufs/vdir.c 2016-08-17 18:01:06.161889951 +0200
29458@@ -0,0 +1,899 @@
1facf9fc 29459+/*
8cdd5066 29460+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 29461+ *
29462+ * This program, aufs is free software; you can redistribute it and/or modify
29463+ * it under the terms of the GNU General Public License as published by
29464+ * the Free Software Foundation; either version 2 of the License, or
29465+ * (at your option) any later version.
dece6358
AM
29466+ *
29467+ * This program is distributed in the hope that it will be useful,
29468+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29469+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29470+ * GNU General Public License for more details.
29471+ *
29472+ * You should have received a copy of the GNU General Public License
523b37e3 29473+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29474+ */
29475+
29476+/*
29477+ * virtual or vertical directory
29478+ */
29479+
29480+#include "aufs.h"
29481+
dece6358 29482+static unsigned int calc_size(int nlen)
1facf9fc 29483+{
dece6358 29484+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 29485+}
29486+
29487+static int set_deblk_end(union au_vdir_deblk_p *p,
29488+ union au_vdir_deblk_p *deblk_end)
29489+{
29490+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29491+ p->de->de_str.len = 0;
29492+ /* smp_mb(); */
29493+ return 0;
29494+ }
29495+ return -1; /* error */
29496+}
29497+
29498+/* returns true or false */
29499+static int is_deblk_end(union au_vdir_deblk_p *p,
29500+ union au_vdir_deblk_p *deblk_end)
29501+{
29502+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
29503+ return !p->de->de_str.len;
29504+ return 1;
29505+}
29506+
29507+static unsigned char *last_deblk(struct au_vdir *vdir)
29508+{
29509+ return vdir->vd_deblk[vdir->vd_nblk - 1];
29510+}
29511+
29512+/* ---------------------------------------------------------------------- */
29513+
79b8bda9 29514+/* estimate the appropriate size for name hash table */
1308ab2a 29515+unsigned int au_rdhash_est(loff_t sz)
29516+{
29517+ unsigned int n;
29518+
29519+ n = UINT_MAX;
29520+ sz >>= 10;
29521+ if (sz < n)
29522+ n = sz;
29523+ if (sz < AUFS_RDHASH_DEF)
29524+ n = AUFS_RDHASH_DEF;
4a4d8108 29525+ /* pr_info("n %u\n", n); */
1308ab2a 29526+ return n;
29527+}
29528+
1facf9fc 29529+/*
29530+ * the allocated memory has to be freed by
dece6358 29531+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 29532+ */
dece6358 29533+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 29534+{
1facf9fc 29535+ struct hlist_head *head;
dece6358 29536+ unsigned int u;
076b876e 29537+ size_t sz;
1facf9fc 29538+
076b876e
AM
29539+ sz = sizeof(*nhash->nh_head) * num_hash;
29540+ head = kmalloc(sz, gfp);
dece6358
AM
29541+ if (head) {
29542+ nhash->nh_num = num_hash;
29543+ nhash->nh_head = head;
29544+ for (u = 0; u < num_hash; u++)
1facf9fc 29545+ INIT_HLIST_HEAD(head++);
dece6358 29546+ return 0; /* success */
1facf9fc 29547+ }
1facf9fc 29548+
dece6358 29549+ return -ENOMEM;
1facf9fc 29550+}
29551+
dece6358
AM
29552+static void nhash_count(struct hlist_head *head)
29553+{
29554+#if 0
29555+ unsigned long n;
29556+ struct hlist_node *pos;
29557+
29558+ n = 0;
29559+ hlist_for_each(pos, head)
29560+ n++;
4a4d8108 29561+ pr_info("%lu\n", n);
dece6358
AM
29562+#endif
29563+}
29564+
29565+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 29566+{
c06a8ce3
AM
29567+ struct au_vdir_wh *pos;
29568+ struct hlist_node *node;
1facf9fc 29569+
c06a8ce3 29570+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
f0c0a007 29571+ au_delayed_kfree(pos);
1facf9fc 29572+}
29573+
dece6358 29574+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 29575+{
c06a8ce3
AM
29576+ struct au_vdir_dehstr *pos;
29577+ struct hlist_node *node;
1facf9fc 29578+
c06a8ce3 29579+ hlist_for_each_entry_safe(pos, node, head, hash)
f0c0a007 29580+ au_cache_dfree_vdir_dehstr(pos);
1facf9fc 29581+}
29582+
dece6358
AM
29583+static void au_nhash_do_free(struct au_nhash *nhash,
29584+ void (*free)(struct hlist_head *head))
1facf9fc 29585+{
1308ab2a 29586+ unsigned int n;
1facf9fc 29587+ struct hlist_head *head;
1facf9fc 29588+
dece6358 29589+ n = nhash->nh_num;
1308ab2a 29590+ if (!n)
29591+ return;
29592+
dece6358 29593+ head = nhash->nh_head;
1308ab2a 29594+ while (n-- > 0) {
dece6358
AM
29595+ nhash_count(head);
29596+ free(head++);
1facf9fc 29597+ }
f0c0a007 29598+ au_delayed_kfree(nhash->nh_head);
1facf9fc 29599+}
29600+
dece6358 29601+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 29602+{
dece6358
AM
29603+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
29604+}
1facf9fc 29605+
dece6358
AM
29606+static void au_nhash_de_free(struct au_nhash *delist)
29607+{
29608+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 29609+}
29610+
29611+/* ---------------------------------------------------------------------- */
29612+
29613+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29614+ int limit)
29615+{
29616+ int num;
29617+ unsigned int u, n;
29618+ struct hlist_head *head;
c06a8ce3 29619+ struct au_vdir_wh *pos;
1facf9fc 29620+
29621+ num = 0;
29622+ n = whlist->nh_num;
29623+ head = whlist->nh_head;
1308ab2a 29624+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
29625+ hlist_for_each_entry(pos, head, wh_hash)
29626+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 29627+ return 1;
1facf9fc 29628+ return 0;
29629+}
29630+
29631+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 29632+ unsigned char *name,
1facf9fc 29633+ unsigned int len)
29634+{
dece6358
AM
29635+ unsigned int v;
29636+ /* const unsigned int magic_bit = 12; */
29637+
1308ab2a 29638+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29639+
dece6358 29640+ v = 0;
f0c0a007
AM
29641+ if (len > 8)
29642+ len = 8;
dece6358
AM
29643+ while (len--)
29644+ v += *name++;
29645+ /* v = hash_long(v, magic_bit); */
29646+ v %= nhash->nh_num;
29647+ return nhash->nh_head + v;
29648+}
29649+
29650+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29651+ int nlen)
29652+{
29653+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 29654+}
29655+
29656+/* returns found or not */
dece6358 29657+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 29658+{
29659+ struct hlist_head *head;
c06a8ce3 29660+ struct au_vdir_wh *pos;
1facf9fc 29661+ struct au_vdir_destr *str;
29662+
dece6358 29663+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
29664+ hlist_for_each_entry(pos, head, wh_hash) {
29665+ str = &pos->wh_str;
1facf9fc 29666+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
29667+ if (au_nhash_test_name(str, name, nlen))
29668+ return 1;
29669+ }
29670+ return 0;
29671+}
29672+
29673+/* returns found(true) or not */
29674+static int test_known(struct au_nhash *delist, char *name, int nlen)
29675+{
29676+ struct hlist_head *head;
c06a8ce3 29677+ struct au_vdir_dehstr *pos;
dece6358
AM
29678+ struct au_vdir_destr *str;
29679+
29680+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
29681+ hlist_for_each_entry(pos, head, hash) {
29682+ str = pos->str;
dece6358
AM
29683+ AuDbg("%.*s\n", str->len, str->name);
29684+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 29685+ return 1;
29686+ }
29687+ return 0;
29688+}
29689+
dece6358
AM
29690+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29691+ unsigned char d_type)
29692+{
29693+#ifdef CONFIG_AUFS_SHWH
29694+ wh->wh_ino = ino;
29695+ wh->wh_type = d_type;
29696+#endif
29697+}
29698+
29699+/* ---------------------------------------------------------------------- */
29700+
29701+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29702+ unsigned int d_type, aufs_bindex_t bindex,
29703+ unsigned char shwh)
1facf9fc 29704+{
29705+ int err;
29706+ struct au_vdir_destr *str;
29707+ struct au_vdir_wh *wh;
29708+
dece6358 29709+ AuDbg("%.*s\n", nlen, name);
1308ab2a 29710+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29711+
1facf9fc 29712+ err = -ENOMEM;
dece6358 29713+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 29714+ if (unlikely(!wh))
29715+ goto out;
29716+
29717+ err = 0;
29718+ wh->wh_bindex = bindex;
dece6358
AM
29719+ if (shwh)
29720+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 29721+ str = &wh->wh_str;
dece6358
AM
29722+ str->len = nlen;
29723+ memcpy(str->name, name, nlen);
29724+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 29725+ /* smp_mb(); */
29726+
4f0767ce 29727+out:
1facf9fc 29728+ return err;
29729+}
29730+
1facf9fc 29731+static int append_deblk(struct au_vdir *vdir)
29732+{
29733+ int err;
dece6358 29734+ unsigned long ul;
1facf9fc 29735+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
29736+ union au_vdir_deblk_p p, deblk_end;
29737+ unsigned char **o;
29738+
29739+ err = -ENOMEM;
dece6358
AM
29740+ o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29741+ GFP_NOFS);
1facf9fc 29742+ if (unlikely(!o))
29743+ goto out;
29744+
29745+ vdir->vd_deblk = o;
29746+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29747+ if (p.deblk) {
29748+ ul = vdir->vd_nblk++;
29749+ vdir->vd_deblk[ul] = p.deblk;
29750+ vdir->vd_last.ul = ul;
29751+ vdir->vd_last.p.deblk = p.deblk;
29752+ deblk_end.deblk = p.deblk + deblk_sz;
29753+ err = set_deblk_end(&p, &deblk_end);
29754+ }
29755+
4f0767ce 29756+out:
1facf9fc 29757+ return err;
29758+}
29759+
dece6358
AM
29760+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29761+ unsigned int d_type, struct au_nhash *delist)
29762+{
29763+ int err;
29764+ unsigned int sz;
29765+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
29766+ union au_vdir_deblk_p p, *room, deblk_end;
29767+ struct au_vdir_dehstr *dehstr;
29768+
29769+ p.deblk = last_deblk(vdir);
29770+ deblk_end.deblk = p.deblk + deblk_sz;
29771+ room = &vdir->vd_last.p;
29772+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29773+ || !is_deblk_end(room, &deblk_end));
29774+
29775+ sz = calc_size(nlen);
29776+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29777+ err = append_deblk(vdir);
29778+ if (unlikely(err))
29779+ goto out;
29780+
29781+ p.deblk = last_deblk(vdir);
29782+ deblk_end.deblk = p.deblk + deblk_sz;
29783+ /* smp_mb(); */
29784+ AuDebugOn(room->deblk != p.deblk);
29785+ }
29786+
29787+ err = -ENOMEM;
4a4d8108 29788+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
29789+ if (unlikely(!dehstr))
29790+ goto out;
29791+
29792+ dehstr->str = &room->de->de_str;
29793+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29794+ room->de->de_ino = ino;
29795+ room->de->de_type = d_type;
29796+ room->de->de_str.len = nlen;
29797+ memcpy(room->de->de_str.name, name, nlen);
29798+
29799+ err = 0;
29800+ room->deblk += sz;
29801+ if (unlikely(set_deblk_end(room, &deblk_end)))
29802+ err = append_deblk(vdir);
29803+ /* smp_mb(); */
29804+
4f0767ce 29805+out:
dece6358
AM
29806+ return err;
29807+}
29808+
29809+/* ---------------------------------------------------------------------- */
29810+
f0c0a007 29811+void au_vdir_free(struct au_vdir *vdir, int atonce)
dece6358
AM
29812+{
29813+ unsigned char **deblk;
29814+
29815+ deblk = vdir->vd_deblk;
f0c0a007
AM
29816+ if (!atonce) {
29817+ while (vdir->vd_nblk--)
29818+ au_delayed_kfree(*deblk++);
29819+ au_delayed_kfree(vdir->vd_deblk);
29820+ au_cache_dfree_vdir(vdir);
29821+ } else {
29822+ /* not delayed */
29823+ while (vdir->vd_nblk--)
29824+ kfree(*deblk++);
29825+ kfree(vdir->vd_deblk);
29826+ au_cache_free_vdir(vdir);
29827+ }
dece6358
AM
29828+}
29829+
1308ab2a 29830+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 29831+{
29832+ struct au_vdir *vdir;
1308ab2a 29833+ struct super_block *sb;
1facf9fc 29834+ int err;
29835+
2000de60 29836+ sb = file->f_path.dentry->d_sb;
dece6358
AM
29837+ SiMustAnyLock(sb);
29838+
1facf9fc 29839+ err = -ENOMEM;
29840+ vdir = au_cache_alloc_vdir();
29841+ if (unlikely(!vdir))
29842+ goto out;
29843+
29844+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
29845+ if (unlikely(!vdir->vd_deblk))
29846+ goto out_free;
29847+
29848+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 29849+ if (!vdir->vd_deblk_sz) {
79b8bda9 29850+ /* estimate the appropriate size for deblk */
1308ab2a 29851+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 29852+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 29853+ }
1facf9fc 29854+ vdir->vd_nblk = 0;
29855+ vdir->vd_version = 0;
29856+ vdir->vd_jiffy = 0;
29857+ err = append_deblk(vdir);
29858+ if (!err)
29859+ return vdir; /* success */
29860+
f0c0a007 29861+ au_delayed_kfree(vdir->vd_deblk);
1facf9fc 29862+
4f0767ce 29863+out_free:
f0c0a007 29864+ au_cache_dfree_vdir(vdir);
4f0767ce 29865+out:
1facf9fc 29866+ vdir = ERR_PTR(err);
29867+ return vdir;
29868+}
29869+
29870+static int reinit_vdir(struct au_vdir *vdir)
29871+{
29872+ int err;
29873+ union au_vdir_deblk_p p, deblk_end;
29874+
29875+ while (vdir->vd_nblk > 1) {
f0c0a007 29876+ au_delayed_kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
1facf9fc 29877+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
29878+ vdir->vd_nblk--;
29879+ }
29880+ p.deblk = vdir->vd_deblk[0];
29881+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
29882+ err = set_deblk_end(&p, &deblk_end);
29883+ /* keep vd_dblk_sz */
29884+ vdir->vd_last.ul = 0;
29885+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29886+ vdir->vd_version = 0;
29887+ vdir->vd_jiffy = 0;
29888+ /* smp_mb(); */
29889+ return err;
29890+}
29891+
29892+/* ---------------------------------------------------------------------- */
29893+
1facf9fc 29894+#define AuFillVdir_CALLED 1
29895+#define AuFillVdir_WHABLE (1 << 1)
dece6358 29896+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 29897+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
29898+#define au_fset_fillvdir(flags, name) \
29899+ do { (flags) |= AuFillVdir_##name; } while (0)
29900+#define au_fclr_fillvdir(flags, name) \
29901+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 29902+
dece6358
AM
29903+#ifndef CONFIG_AUFS_SHWH
29904+#undef AuFillVdir_SHWH
29905+#define AuFillVdir_SHWH 0
29906+#endif
29907+
1facf9fc 29908+struct fillvdir_arg {
392086de 29909+ struct dir_context ctx;
1facf9fc 29910+ struct file *file;
29911+ struct au_vdir *vdir;
dece6358
AM
29912+ struct au_nhash delist;
29913+ struct au_nhash whlist;
1facf9fc 29914+ aufs_bindex_t bindex;
29915+ unsigned int flags;
29916+ int err;
29917+};
29918+
392086de 29919+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 29920+ loff_t offset __maybe_unused, u64 h_ino,
29921+ unsigned int d_type)
29922+{
392086de 29923+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 29924+ char *name = (void *)__name;
29925+ struct super_block *sb;
1facf9fc 29926+ ino_t ino;
dece6358 29927+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 29928+
1facf9fc 29929+ arg->err = 0;
2000de60 29930+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 29931+ au_fset_fillvdir(arg->flags, CALLED);
29932+ /* smp_mb(); */
dece6358 29933+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 29934+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
29935+ if (test_known(&arg->delist, name, nlen)
29936+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
29937+ goto out; /* already exists or whiteouted */
1facf9fc 29938+
dece6358 29939+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
29940+ if (!arg->err) {
29941+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
29942+ d_type = DT_UNKNOWN;
dece6358
AM
29943+ arg->err = append_de(arg->vdir, name, nlen, ino,
29944+ d_type, &arg->delist);
4a4d8108 29945+ }
1facf9fc 29946+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
29947+ name += AUFS_WH_PFX_LEN;
dece6358
AM
29948+ nlen -= AUFS_WH_PFX_LEN;
29949+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29950+ goto out; /* already whiteouted */
1facf9fc 29951+
dece6358
AM
29952+ if (shwh)
29953+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29954+ &ino);
4a4d8108
AM
29955+ if (!arg->err) {
29956+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29957+ d_type = DT_UNKNOWN;
1facf9fc 29958+ arg->err = au_nhash_append_wh
dece6358
AM
29959+ (&arg->whlist, name, nlen, ino, d_type,
29960+ arg->bindex, shwh);
4a4d8108 29961+ }
1facf9fc 29962+ }
29963+
4f0767ce 29964+out:
1facf9fc 29965+ if (!arg->err)
29966+ arg->vdir->vd_jiffy = jiffies;
29967+ /* smp_mb(); */
29968+ AuTraceErr(arg->err);
29969+ return arg->err;
29970+}
29971+
dece6358
AM
29972+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29973+ struct au_nhash *whlist, struct au_nhash *delist)
29974+{
29975+#ifdef CONFIG_AUFS_SHWH
29976+ int err;
29977+ unsigned int nh, u;
29978+ struct hlist_head *head;
c06a8ce3
AM
29979+ struct au_vdir_wh *pos;
29980+ struct hlist_node *n;
dece6358
AM
29981+ char *p, *o;
29982+ struct au_vdir_destr *destr;
29983+
29984+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29985+
29986+ err = -ENOMEM;
537831f9 29987+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
29988+ if (unlikely(!p))
29989+ goto out;
29990+
29991+ err = 0;
29992+ nh = whlist->nh_num;
29993+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29994+ p += AUFS_WH_PFX_LEN;
29995+ for (u = 0; u < nh; u++) {
29996+ head = whlist->nh_head + u;
c06a8ce3
AM
29997+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29998+ destr = &pos->wh_str;
dece6358
AM
29999+ memcpy(p, destr->name, destr->len);
30000+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 30001+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
30002+ if (unlikely(err))
30003+ break;
30004+ }
30005+ }
30006+
f0c0a007 30007+ au_delayed_free_page((unsigned long)o);
dece6358 30008+
4f0767ce 30009+out:
dece6358
AM
30010+ AuTraceErr(err);
30011+ return err;
30012+#else
30013+ return 0;
30014+#endif
30015+}
30016+
1facf9fc 30017+static int au_do_read_vdir(struct fillvdir_arg *arg)
30018+{
30019+ int err;
dece6358 30020+ unsigned int rdhash;
1facf9fc 30021+ loff_t offset;
5afbbe0d 30022+ aufs_bindex_t bbot, bindex, btop;
dece6358 30023+ unsigned char shwh;
1facf9fc 30024+ struct file *hf, *file;
30025+ struct super_block *sb;
30026+
1facf9fc 30027+ file = arg->file;
2000de60 30028+ sb = file->f_path.dentry->d_sb;
dece6358
AM
30029+ SiMustAnyLock(sb);
30030+
30031+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 30032+ if (!rdhash)
30033+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
30034+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
30035+ if (unlikely(err))
1facf9fc 30036+ goto out;
dece6358
AM
30037+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
30038+ if (unlikely(err))
1facf9fc 30039+ goto out_delist;
30040+
30041+ err = 0;
30042+ arg->flags = 0;
dece6358
AM
30043+ shwh = 0;
30044+ if (au_opt_test(au_mntflags(sb), SHWH)) {
30045+ shwh = 1;
30046+ au_fset_fillvdir(arg->flags, SHWH);
30047+ }
5afbbe0d
AM
30048+ btop = au_fbtop(file);
30049+ bbot = au_fbbot_dir(file);
30050+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 30051+ hf = au_hf_dir(file, bindex);
1facf9fc 30052+ if (!hf)
30053+ continue;
30054+
30055+ offset = vfsub_llseek(hf, 0, SEEK_SET);
30056+ err = offset;
30057+ if (unlikely(offset))
30058+ break;
30059+
30060+ arg->bindex = bindex;
30061+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 30062+ if (shwh
5afbbe0d 30063+ || (bindex != bbot
dece6358 30064+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 30065+ au_fset_fillvdir(arg->flags, WHABLE);
30066+ do {
30067+ arg->err = 0;
30068+ au_fclr_fillvdir(arg->flags, CALLED);
30069+ /* smp_mb(); */
392086de 30070+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 30071+ if (err >= 0)
30072+ err = arg->err;
30073+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
30074+
30075+ /*
30076+ * dir_relax() may be good for concurrency, but aufs should not
30077+ * use it since it will cause a lockdep problem.
30078+ */
1facf9fc 30079+ }
dece6358
AM
30080+
30081+ if (!err && shwh)
30082+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
30083+
30084+ au_nhash_wh_free(&arg->whlist);
1facf9fc 30085+
4f0767ce 30086+out_delist:
dece6358 30087+ au_nhash_de_free(&arg->delist);
4f0767ce 30088+out:
1facf9fc 30089+ return err;
30090+}
30091+
30092+static int read_vdir(struct file *file, int may_read)
30093+{
30094+ int err;
30095+ unsigned long expire;
30096+ unsigned char do_read;
392086de
AM
30097+ struct fillvdir_arg arg = {
30098+ .ctx = {
2000de60 30099+ .actor = fillvdir
392086de
AM
30100+ }
30101+ };
1facf9fc 30102+ struct inode *inode;
30103+ struct au_vdir *vdir, *allocated;
30104+
30105+ err = 0;
c06a8ce3 30106+ inode = file_inode(file);
1facf9fc 30107+ IMustLock(inode);
5afbbe0d 30108+ IiMustWriteLock(inode);
dece6358
AM
30109+ SiMustAnyLock(inode->i_sb);
30110+
1facf9fc 30111+ allocated = NULL;
30112+ do_read = 0;
30113+ expire = au_sbi(inode->i_sb)->si_rdcache;
30114+ vdir = au_ivdir(inode);
30115+ if (!vdir) {
30116+ do_read = 1;
1308ab2a 30117+ vdir = alloc_vdir(file);
1facf9fc 30118+ err = PTR_ERR(vdir);
30119+ if (IS_ERR(vdir))
30120+ goto out;
30121+ err = 0;
30122+ allocated = vdir;
30123+ } else if (may_read
30124+ && (inode->i_version != vdir->vd_version
30125+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
30126+ do_read = 1;
30127+ err = reinit_vdir(vdir);
30128+ if (unlikely(err))
30129+ goto out;
30130+ }
30131+
30132+ if (!do_read)
30133+ return 0; /* success */
30134+
30135+ arg.file = file;
30136+ arg.vdir = vdir;
30137+ err = au_do_read_vdir(&arg);
30138+ if (!err) {
392086de 30139+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
1facf9fc 30140+ vdir->vd_version = inode->i_version;
30141+ vdir->vd_last.ul = 0;
30142+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
30143+ if (allocated)
30144+ au_set_ivdir(inode, allocated);
30145+ } else if (allocated)
f0c0a007 30146+ au_vdir_free(allocated, /*atonce*/0);
1facf9fc 30147+
4f0767ce 30148+out:
1facf9fc 30149+ return err;
30150+}
30151+
30152+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
30153+{
30154+ int err, rerr;
30155+ unsigned long ul, n;
30156+ const unsigned int deblk_sz = src->vd_deblk_sz;
30157+
30158+ AuDebugOn(tgt->vd_nblk != 1);
30159+
30160+ err = -ENOMEM;
30161+ if (tgt->vd_nblk < src->vd_nblk) {
30162+ unsigned char **p;
30163+
dece6358
AM
30164+ p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
30165+ GFP_NOFS);
1facf9fc 30166+ if (unlikely(!p))
30167+ goto out;
30168+ tgt->vd_deblk = p;
30169+ }
30170+
1308ab2a 30171+ if (tgt->vd_deblk_sz != deblk_sz) {
30172+ unsigned char *p;
30173+
30174+ tgt->vd_deblk_sz = deblk_sz;
30175+ p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
30176+ if (unlikely(!p))
30177+ goto out;
30178+ tgt->vd_deblk[0] = p;
30179+ }
1facf9fc 30180+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 30181+ tgt->vd_version = src->vd_version;
30182+ tgt->vd_jiffy = src->vd_jiffy;
30183+
30184+ n = src->vd_nblk;
30185+ for (ul = 1; ul < n; ul++) {
dece6358
AM
30186+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
30187+ GFP_NOFS);
30188+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 30189+ goto out;
1308ab2a 30190+ tgt->vd_nblk++;
1facf9fc 30191+ }
1308ab2a 30192+ tgt->vd_nblk = n;
30193+ tgt->vd_last.ul = tgt->vd_last.ul;
30194+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
30195+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
30196+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 30197+ /* smp_mb(); */
30198+ return 0; /* success */
30199+
4f0767ce 30200+out:
1facf9fc 30201+ rerr = reinit_vdir(tgt);
30202+ BUG_ON(rerr);
30203+ return err;
30204+}
30205+
30206+int au_vdir_init(struct file *file)
30207+{
30208+ int err;
30209+ struct inode *inode;
30210+ struct au_vdir *vdir_cache, *allocated;
30211+
392086de 30212+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 30213+ err = read_vdir(file, !file->f_pos);
30214+ if (unlikely(err))
30215+ goto out;
30216+
30217+ allocated = NULL;
30218+ vdir_cache = au_fvdir_cache(file);
30219+ if (!vdir_cache) {
1308ab2a 30220+ vdir_cache = alloc_vdir(file);
1facf9fc 30221+ err = PTR_ERR(vdir_cache);
30222+ if (IS_ERR(vdir_cache))
30223+ goto out;
30224+ allocated = vdir_cache;
30225+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 30226+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 30227+ err = reinit_vdir(vdir_cache);
30228+ if (unlikely(err))
30229+ goto out;
30230+ } else
30231+ return 0; /* success */
30232+
c06a8ce3 30233+ inode = file_inode(file);
1facf9fc 30234+ err = copy_vdir(vdir_cache, au_ivdir(inode));
30235+ if (!err) {
30236+ file->f_version = inode->i_version;
30237+ if (allocated)
30238+ au_set_fvdir_cache(file, allocated);
30239+ } else if (allocated)
f0c0a007 30240+ au_vdir_free(allocated, /*atonce*/0);
1facf9fc 30241+
4f0767ce 30242+out:
1facf9fc 30243+ return err;
30244+}
30245+
30246+static loff_t calc_offset(struct au_vdir *vdir)
30247+{
30248+ loff_t offset;
30249+ union au_vdir_deblk_p p;
30250+
30251+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
30252+ offset = vdir->vd_last.p.deblk - p.deblk;
30253+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
30254+ return offset;
30255+}
30256+
30257+/* returns true or false */
392086de 30258+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 30259+{
30260+ int valid;
30261+ unsigned int deblk_sz;
30262+ unsigned long ul, n;
30263+ loff_t offset;
30264+ union au_vdir_deblk_p p, deblk_end;
30265+ struct au_vdir *vdir_cache;
30266+
30267+ valid = 1;
30268+ vdir_cache = au_fvdir_cache(file);
30269+ offset = calc_offset(vdir_cache);
30270+ AuDbg("offset %lld\n", offset);
392086de 30271+ if (ctx->pos == offset)
1facf9fc 30272+ goto out;
30273+
30274+ vdir_cache->vd_last.ul = 0;
30275+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 30276+ if (!ctx->pos)
1facf9fc 30277+ goto out;
30278+
30279+ valid = 0;
30280+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 30281+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 30282+ AuDbg("ul %lu\n", ul);
30283+ if (ul >= vdir_cache->vd_nblk)
30284+ goto out;
30285+
30286+ n = vdir_cache->vd_nblk;
30287+ for (; ul < n; ul++) {
30288+ p.deblk = vdir_cache->vd_deblk[ul];
30289+ deblk_end.deblk = p.deblk + deblk_sz;
30290+ offset = ul;
30291+ offset *= deblk_sz;
392086de 30292+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 30293+ unsigned int l;
30294+
30295+ l = calc_size(p.de->de_str.len);
30296+ offset += l;
30297+ p.deblk += l;
30298+ }
30299+ if (!is_deblk_end(&p, &deblk_end)) {
30300+ valid = 1;
30301+ vdir_cache->vd_last.ul = ul;
30302+ vdir_cache->vd_last.p = p;
30303+ break;
30304+ }
30305+ }
30306+
4f0767ce 30307+out:
1facf9fc 30308+ /* smp_mb(); */
30309+ AuTraceErr(!valid);
30310+ return valid;
30311+}
30312+
392086de 30313+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 30314+{
1facf9fc 30315+ unsigned int l, deblk_sz;
30316+ union au_vdir_deblk_p deblk_end;
30317+ struct au_vdir *vdir_cache;
30318+ struct au_vdir_de *de;
30319+
30320+ vdir_cache = au_fvdir_cache(file);
392086de 30321+ if (!seek_vdir(file, ctx))
1facf9fc 30322+ return 0;
30323+
30324+ deblk_sz = vdir_cache->vd_deblk_sz;
30325+ while (1) {
30326+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30327+ deblk_end.deblk += deblk_sz;
30328+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
30329+ de = vdir_cache->vd_last.p.de;
30330+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 30331+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 30332+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
30333+ if (unlikely(!dir_emit(ctx, de->de_str.name,
30334+ de->de_str.len, de->de_ino,
30335+ de->de_type))) {
1facf9fc 30336+ /* todo: ignore the error caused by udba? */
30337+ /* return err; */
30338+ return 0;
30339+ }
30340+
30341+ l = calc_size(de->de_str.len);
30342+ vdir_cache->vd_last.p.deblk += l;
392086de 30343+ ctx->pos += l;
1facf9fc 30344+ }
30345+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
30346+ vdir_cache->vd_last.ul++;
30347+ vdir_cache->vd_last.p.deblk
30348+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 30349+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 30350+ continue;
30351+ }
30352+ break;
30353+ }
30354+
30355+ /* smp_mb(); */
30356+ return 0;
30357+}
7f207e10
AM
30358diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
30359--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 30360+++ linux/fs/aufs/vfsub.c 2016-07-25 19:05:34.817826663 +0200
febd17d6 30361@@ -0,0 +1,884 @@
1facf9fc 30362+/*
8cdd5066 30363+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 30364+ *
30365+ * This program, aufs is free software; you can redistribute it and/or modify
30366+ * it under the terms of the GNU General Public License as published by
30367+ * the Free Software Foundation; either version 2 of the License, or
30368+ * (at your option) any later version.
dece6358
AM
30369+ *
30370+ * This program is distributed in the hope that it will be useful,
30371+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30372+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30373+ * GNU General Public License for more details.
30374+ *
30375+ * You should have received a copy of the GNU General Public License
523b37e3 30376+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30377+ */
30378+
30379+/*
30380+ * sub-routines for VFS
30381+ */
30382+
dece6358 30383+#include <linux/namei.h>
8cdd5066 30384+#include <linux/nsproxy.h>
dece6358
AM
30385+#include <linux/security.h>
30386+#include <linux/splice.h>
8cdd5066 30387+#include "../fs/mount.h"
1facf9fc 30388+#include "aufs.h"
30389+
8cdd5066
JR
30390+#ifdef CONFIG_AUFS_BR_FUSE
30391+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
30392+{
30393+ struct nsproxy *ns;
30394+
30395+ if (!au_test_fuse(h_sb) || !au_userns)
30396+ return 0;
30397+
30398+ ns = current->nsproxy;
30399+ /* no {get,put}_nsproxy(ns) */
30400+ return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
30401+}
30402+#endif
30403+
30404+/* ---------------------------------------------------------------------- */
30405+
1facf9fc 30406+int vfsub_update_h_iattr(struct path *h_path, int *did)
30407+{
30408+ int err;
30409+ struct kstat st;
30410+ struct super_block *h_sb;
30411+
30412+ /* for remote fs, leave work for its getattr or d_revalidate */
30413+ /* for bad i_attr fs, handle them in aufs_getattr() */
30414+ /* still some fs may acquire i_mutex. we need to skip them */
30415+ err = 0;
30416+ if (!did)
30417+ did = &err;
30418+ h_sb = h_path->dentry->d_sb;
30419+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
30420+ if (*did)
c06a8ce3 30421+ err = vfs_getattr(h_path, &st);
1facf9fc 30422+
30423+ return err;
30424+}
30425+
30426+/* ---------------------------------------------------------------------- */
30427+
4a4d8108 30428+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 30429+{
30430+ struct file *file;
30431+
b4510431 30432+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 30433+ current_cred());
2cbb1c4b
JR
30434+ if (!IS_ERR_OR_NULL(file)
30435+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 30436+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 30437+
1308ab2a 30438+ return file;
30439+}
30440+
1facf9fc 30441+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
30442+{
30443+ struct file *file;
30444+
2cbb1c4b 30445+ lockdep_off();
7f207e10 30446+ file = filp_open(path,
2cbb1c4b 30447+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 30448+ mode);
2cbb1c4b 30449+ lockdep_on();
1facf9fc 30450+ if (IS_ERR(file))
30451+ goto out;
30452+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30453+
4f0767ce 30454+out:
1facf9fc 30455+ return file;
30456+}
30457+
b912730e
AM
30458+/*
30459+ * Ideally this function should call VFS:do_last() in order to keep all its
30460+ * checkings. But it is very hard for aufs to regenerate several VFS internal
30461+ * structure such as nameidata. This is a second (or third) best approach.
30462+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
30463+ */
30464+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30465+ struct vfsub_aopen_args *args, struct au_branch *br)
30466+{
30467+ int err;
30468+ struct file *file = args->file;
30469+ /* copied from linux/fs/namei.c:atomic_open() */
30470+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
30471+
30472+ IMustLock(dir);
30473+ AuDebugOn(!dir->i_op->atomic_open);
30474+
30475+ err = au_br_test_oflag(args->open_flag, br);
30476+ if (unlikely(err))
30477+ goto out;
30478+
30479+ args->file->f_path.dentry = DENTRY_NOT_SET;
30480+ args->file->f_path.mnt = au_br_mnt(br);
30481+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
30482+ args->create_mode, args->opened);
30483+ if (err >= 0) {
30484+ /* some filesystems don't set FILE_CREATED while succeeded? */
30485+ if (*args->opened & FILE_CREATED)
30486+ fsnotify_create(dir, dentry);
30487+ } else
30488+ goto out;
30489+
30490+
30491+ if (!err) {
30492+ /* todo: call VFS:may_open() here */
30493+ err = open_check_o_direct(file);
30494+ /* todo: ima_file_check() too? */
30495+ if (!err && (args->open_flag & __FMODE_EXEC))
30496+ err = deny_write_access(file);
30497+ if (unlikely(err))
30498+ /* note that the file is created and still opened */
30499+ goto out;
30500+ }
30501+
5afbbe0d 30502+ au_br_get(br);
b912730e
AM
30503+ fsnotify_open(file);
30504+
30505+out:
30506+ return err;
30507+}
30508+
1facf9fc 30509+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30510+{
30511+ int err;
30512+
1facf9fc 30513+ err = kern_path(name, flags, path);
5527c038 30514+ if (!err && d_is_positive(path->dentry))
1facf9fc 30515+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30516+ return err;
30517+}
30518+
febd17d6
JR
30519+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
30520+ struct dentry *parent, int len)
30521+{
30522+ struct path path = {
30523+ .mnt = NULL
30524+ };
30525+
30526+ path.dentry = lookup_one_len_unlocked(name, parent, len);
30527+ if (IS_ERR(path.dentry))
30528+ goto out;
30529+ if (d_is_positive(path.dentry))
30530+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30531+
30532+out:
30533+ AuTraceErrPtr(path.dentry);
30534+ return path.dentry;
30535+}
30536+
1facf9fc 30537+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30538+ int len)
30539+{
30540+ struct path path = {
30541+ .mnt = NULL
30542+ };
30543+
1308ab2a 30544+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 30545+ IMustLock(d_inode(parent));
1facf9fc 30546+
30547+ path.dentry = lookup_one_len(name, parent, len);
30548+ if (IS_ERR(path.dentry))
30549+ goto out;
5527c038 30550+ if (d_is_positive(path.dentry))
1facf9fc 30551+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30552+
4f0767ce 30553+out:
4a4d8108 30554+ AuTraceErrPtr(path.dentry);
1facf9fc 30555+ return path.dentry;
30556+}
30557+
b4510431 30558+void vfsub_call_lkup_one(void *args)
2cbb1c4b 30559+{
b4510431
AM
30560+ struct vfsub_lkup_one_args *a = args;
30561+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
30562+}
30563+
1facf9fc 30564+/* ---------------------------------------------------------------------- */
30565+
30566+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30567+ struct dentry *d2, struct au_hinode *hdir2)
30568+{
30569+ struct dentry *d;
30570+
2cbb1c4b 30571+ lockdep_off();
1facf9fc 30572+ d = lock_rename(d1, d2);
2cbb1c4b 30573+ lockdep_on();
4a4d8108 30574+ au_hn_suspend(hdir1);
1facf9fc 30575+ if (hdir1 != hdir2)
4a4d8108 30576+ au_hn_suspend(hdir2);
1facf9fc 30577+
30578+ return d;
30579+}
30580+
30581+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30582+ struct dentry *d2, struct au_hinode *hdir2)
30583+{
4a4d8108 30584+ au_hn_resume(hdir1);
1facf9fc 30585+ if (hdir1 != hdir2)
4a4d8108 30586+ au_hn_resume(hdir2);
2cbb1c4b 30587+ lockdep_off();
1facf9fc 30588+ unlock_rename(d1, d2);
2cbb1c4b 30589+ lockdep_on();
1facf9fc 30590+}
30591+
30592+/* ---------------------------------------------------------------------- */
30593+
b4510431 30594+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 30595+{
30596+ int err;
30597+ struct dentry *d;
30598+
30599+ IMustLock(dir);
30600+
30601+ d = path->dentry;
30602+ path->dentry = d->d_parent;
b752ccd1 30603+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 30604+ path->dentry = d;
30605+ if (unlikely(err))
30606+ goto out;
30607+
c1595e42 30608+ lockdep_off();
b4510431 30609+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 30610+ lockdep_on();
1facf9fc 30611+ if (!err) {
30612+ struct path tmp = *path;
30613+ int did;
30614+
30615+ vfsub_update_h_iattr(&tmp, &did);
30616+ if (did) {
30617+ tmp.dentry = path->dentry->d_parent;
30618+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30619+ }
30620+ /*ignore*/
30621+ }
30622+
4f0767ce 30623+out:
1facf9fc 30624+ return err;
30625+}
30626+
30627+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30628+{
30629+ int err;
30630+ struct dentry *d;
30631+
30632+ IMustLock(dir);
30633+
30634+ d = path->dentry;
30635+ path->dentry = d->d_parent;
b752ccd1 30636+ err = security_path_symlink(path, d, symname);
1facf9fc 30637+ path->dentry = d;
30638+ if (unlikely(err))
30639+ goto out;
30640+
c1595e42 30641+ lockdep_off();
1facf9fc 30642+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 30643+ lockdep_on();
1facf9fc 30644+ if (!err) {
30645+ struct path tmp = *path;
30646+ int did;
30647+
30648+ vfsub_update_h_iattr(&tmp, &did);
30649+ if (did) {
30650+ tmp.dentry = path->dentry->d_parent;
30651+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30652+ }
30653+ /*ignore*/
30654+ }
30655+
4f0767ce 30656+out:
1facf9fc 30657+ return err;
30658+}
30659+
30660+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30661+{
30662+ int err;
30663+ struct dentry *d;
30664+
30665+ IMustLock(dir);
30666+
30667+ d = path->dentry;
30668+ path->dentry = d->d_parent;
027c5e7a 30669+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 30670+ path->dentry = d;
30671+ if (unlikely(err))
30672+ goto out;
30673+
c1595e42 30674+ lockdep_off();
1facf9fc 30675+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 30676+ lockdep_on();
1facf9fc 30677+ if (!err) {
30678+ struct path tmp = *path;
30679+ int did;
30680+
30681+ vfsub_update_h_iattr(&tmp, &did);
30682+ if (did) {
30683+ tmp.dentry = path->dentry->d_parent;
30684+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30685+ }
30686+ /*ignore*/
30687+ }
30688+
4f0767ce 30689+out:
1facf9fc 30690+ return err;
30691+}
30692+
30693+static int au_test_nlink(struct inode *inode)
30694+{
30695+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30696+
30697+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
30698+ || inode->i_nlink < link_max)
30699+ return 0;
30700+ return -EMLINK;
30701+}
30702+
523b37e3
AM
30703+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30704+ struct inode **delegated_inode)
1facf9fc 30705+{
30706+ int err;
30707+ struct dentry *d;
30708+
30709+ IMustLock(dir);
30710+
5527c038 30711+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 30712+ if (unlikely(err))
30713+ return err;
30714+
b4510431 30715+ /* we don't call may_linkat() */
1facf9fc 30716+ d = path->dentry;
30717+ path->dentry = d->d_parent;
b752ccd1 30718+ err = security_path_link(src_dentry, path, d);
1facf9fc 30719+ path->dentry = d;
30720+ if (unlikely(err))
30721+ goto out;
30722+
2cbb1c4b 30723+ lockdep_off();
523b37e3 30724+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 30725+ lockdep_on();
1facf9fc 30726+ if (!err) {
30727+ struct path tmp = *path;
30728+ int did;
30729+
30730+ /* fuse has different memory inode for the same inumber */
30731+ vfsub_update_h_iattr(&tmp, &did);
30732+ if (did) {
30733+ tmp.dentry = path->dentry->d_parent;
30734+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30735+ tmp.dentry = src_dentry;
30736+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30737+ }
30738+ /*ignore*/
30739+ }
30740+
4f0767ce 30741+out:
1facf9fc 30742+ return err;
30743+}
30744+
30745+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3
AM
30746+ struct inode *dir, struct path *path,
30747+ struct inode **delegated_inode)
1facf9fc 30748+{
30749+ int err;
30750+ struct path tmp = {
30751+ .mnt = path->mnt
30752+ };
30753+ struct dentry *d;
30754+
30755+ IMustLock(dir);
30756+ IMustLock(src_dir);
30757+
30758+ d = path->dentry;
30759+ path->dentry = d->d_parent;
30760+ tmp.dentry = src_dentry->d_parent;
38d290e6 30761+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 30762+ path->dentry = d;
30763+ if (unlikely(err))
30764+ goto out;
30765+
2cbb1c4b 30766+ lockdep_off();
523b37e3 30767+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
38d290e6 30768+ delegated_inode, /*flags*/0);
2cbb1c4b 30769+ lockdep_on();
1facf9fc 30770+ if (!err) {
30771+ int did;
30772+
30773+ tmp.dentry = d->d_parent;
30774+ vfsub_update_h_iattr(&tmp, &did);
30775+ if (did) {
30776+ tmp.dentry = src_dentry;
30777+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30778+ tmp.dentry = src_dentry->d_parent;
30779+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30780+ }
30781+ /*ignore*/
30782+ }
30783+
4f0767ce 30784+out:
1facf9fc 30785+ return err;
30786+}
30787+
30788+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30789+{
30790+ int err;
30791+ struct dentry *d;
30792+
30793+ IMustLock(dir);
30794+
30795+ d = path->dentry;
30796+ path->dentry = d->d_parent;
b752ccd1 30797+ err = security_path_mkdir(path, d, mode);
1facf9fc 30798+ path->dentry = d;
30799+ if (unlikely(err))
30800+ goto out;
30801+
c1595e42 30802+ lockdep_off();
1facf9fc 30803+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 30804+ lockdep_on();
1facf9fc 30805+ if (!err) {
30806+ struct path tmp = *path;
30807+ int did;
30808+
30809+ vfsub_update_h_iattr(&tmp, &did);
30810+ if (did) {
30811+ tmp.dentry = path->dentry->d_parent;
30812+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30813+ }
30814+ /*ignore*/
30815+ }
30816+
4f0767ce 30817+out:
1facf9fc 30818+ return err;
30819+}
30820+
30821+int vfsub_rmdir(struct inode *dir, struct path *path)
30822+{
30823+ int err;
30824+ struct dentry *d;
30825+
30826+ IMustLock(dir);
30827+
30828+ d = path->dentry;
30829+ path->dentry = d->d_parent;
b752ccd1 30830+ err = security_path_rmdir(path, d);
1facf9fc 30831+ path->dentry = d;
30832+ if (unlikely(err))
30833+ goto out;
30834+
2cbb1c4b 30835+ lockdep_off();
1facf9fc 30836+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 30837+ lockdep_on();
1facf9fc 30838+ if (!err) {
30839+ struct path tmp = {
30840+ .dentry = path->dentry->d_parent,
30841+ .mnt = path->mnt
30842+ };
30843+
30844+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30845+ }
30846+
4f0767ce 30847+out:
1facf9fc 30848+ return err;
30849+}
30850+
30851+/* ---------------------------------------------------------------------- */
30852+
9dbd164d 30853+/* todo: support mmap_sem? */
1facf9fc 30854+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30855+ loff_t *ppos)
30856+{
30857+ ssize_t err;
30858+
2cbb1c4b 30859+ lockdep_off();
1facf9fc 30860+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 30861+ lockdep_on();
1facf9fc 30862+ if (err >= 0)
30863+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30864+ return err;
30865+}
30866+
30867+/* todo: kernel_read()? */
30868+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30869+ loff_t *ppos)
30870+{
30871+ ssize_t err;
30872+ mm_segment_t oldfs;
b752ccd1
AM
30873+ union {
30874+ void *k;
30875+ char __user *u;
30876+ } buf;
1facf9fc 30877+
b752ccd1 30878+ buf.k = kbuf;
1facf9fc 30879+ oldfs = get_fs();
30880+ set_fs(KERNEL_DS);
b752ccd1 30881+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 30882+ set_fs(oldfs);
30883+ return err;
30884+}
30885+
30886+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30887+ loff_t *ppos)
30888+{
30889+ ssize_t err;
30890+
2cbb1c4b 30891+ lockdep_off();
1facf9fc 30892+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 30893+ lockdep_on();
1facf9fc 30894+ if (err >= 0)
30895+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30896+ return err;
30897+}
30898+
30899+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
30900+{
30901+ ssize_t err;
30902+ mm_segment_t oldfs;
b752ccd1
AM
30903+ union {
30904+ void *k;
30905+ const char __user *u;
30906+ } buf;
1facf9fc 30907+
b752ccd1 30908+ buf.k = kbuf;
1facf9fc 30909+ oldfs = get_fs();
30910+ set_fs(KERNEL_DS);
b752ccd1 30911+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 30912+ set_fs(oldfs);
30913+ return err;
30914+}
30915+
4a4d8108
AM
30916+int vfsub_flush(struct file *file, fl_owner_t id)
30917+{
30918+ int err;
30919+
30920+ err = 0;
523b37e3 30921+ if (file->f_op->flush) {
2000de60 30922+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
30923+ err = file->f_op->flush(file, id);
30924+ else {
30925+ lockdep_off();
30926+ err = file->f_op->flush(file, id);
30927+ lockdep_on();
30928+ }
4a4d8108
AM
30929+ if (!err)
30930+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
30931+ /*ignore*/
30932+ }
30933+ return err;
30934+}
30935+
392086de 30936+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 30937+{
30938+ int err;
30939+
523b37e3 30940+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 30941+
2cbb1c4b 30942+ lockdep_off();
392086de 30943+ err = iterate_dir(file, ctx);
2cbb1c4b 30944+ lockdep_on();
1facf9fc 30945+ if (err >= 0)
30946+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30947+ return err;
30948+}
30949+
30950+long vfsub_splice_to(struct file *in, loff_t *ppos,
30951+ struct pipe_inode_info *pipe, size_t len,
30952+ unsigned int flags)
30953+{
30954+ long err;
30955+
2cbb1c4b 30956+ lockdep_off();
0fc653ad 30957+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 30958+ lockdep_on();
4a4d8108 30959+ file_accessed(in);
1facf9fc 30960+ if (err >= 0)
30961+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
30962+ return err;
30963+}
30964+
30965+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30966+ loff_t *ppos, size_t len, unsigned int flags)
30967+{
30968+ long err;
30969+
2cbb1c4b 30970+ lockdep_off();
0fc653ad 30971+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 30972+ lockdep_on();
1facf9fc 30973+ if (err >= 0)
30974+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
30975+ return err;
30976+}
30977+
53392da6
AM
30978+int vfsub_fsync(struct file *file, struct path *path, int datasync)
30979+{
30980+ int err;
30981+
30982+ /* file can be NULL */
30983+ lockdep_off();
30984+ err = vfs_fsync(file, datasync);
30985+ lockdep_on();
30986+ if (!err) {
30987+ if (!path) {
30988+ AuDebugOn(!file);
30989+ path = &file->f_path;
30990+ }
30991+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30992+ }
30993+ return err;
30994+}
30995+
1facf9fc 30996+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
30997+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30998+ struct file *h_file)
30999+{
31000+ int err;
31001+ struct inode *h_inode;
c06a8ce3 31002+ struct super_block *h_sb;
1facf9fc 31003+
1facf9fc 31004+ if (!h_file) {
c06a8ce3
AM
31005+ err = vfsub_truncate(h_path, length);
31006+ goto out;
1facf9fc 31007+ }
31008+
5527c038 31009+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
31010+ h_sb = h_inode->i_sb;
31011+ lockdep_off();
31012+ sb_start_write(h_sb);
31013+ lockdep_on();
1facf9fc 31014+ err = locks_verify_truncate(h_inode, h_file, length);
31015+ if (!err)
953406b4 31016+ err = security_path_truncate(h_path);
2cbb1c4b
JR
31017+ if (!err) {
31018+ lockdep_off();
1facf9fc 31019+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
31020+ lockdep_on();
31021+ }
c06a8ce3
AM
31022+ lockdep_off();
31023+ sb_end_write(h_sb);
31024+ lockdep_on();
1facf9fc 31025+
4f0767ce 31026+out:
1facf9fc 31027+ return err;
31028+}
31029+
31030+/* ---------------------------------------------------------------------- */
31031+
31032+struct au_vfsub_mkdir_args {
31033+ int *errp;
31034+ struct inode *dir;
31035+ struct path *path;
31036+ int mode;
31037+};
31038+
31039+static void au_call_vfsub_mkdir(void *args)
31040+{
31041+ struct au_vfsub_mkdir_args *a = args;
31042+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
31043+}
31044+
31045+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
31046+{
31047+ int err, do_sio, wkq_err;
31048+
31049+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
31050+ if (!do_sio) {
31051+ lockdep_off();
1facf9fc 31052+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
31053+ lockdep_on();
31054+ } else {
1facf9fc 31055+ struct au_vfsub_mkdir_args args = {
31056+ .errp = &err,
31057+ .dir = dir,
31058+ .path = path,
31059+ .mode = mode
31060+ };
31061+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
31062+ if (unlikely(wkq_err))
31063+ err = wkq_err;
31064+ }
31065+
31066+ return err;
31067+}
31068+
31069+struct au_vfsub_rmdir_args {
31070+ int *errp;
31071+ struct inode *dir;
31072+ struct path *path;
31073+};
31074+
31075+static void au_call_vfsub_rmdir(void *args)
31076+{
31077+ struct au_vfsub_rmdir_args *a = args;
31078+ *a->errp = vfsub_rmdir(a->dir, a->path);
31079+}
31080+
31081+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
31082+{
31083+ int err, do_sio, wkq_err;
31084+
31085+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
31086+ if (!do_sio) {
31087+ lockdep_off();
1facf9fc 31088+ err = vfsub_rmdir(dir, path);
c1595e42
JR
31089+ lockdep_on();
31090+ } else {
1facf9fc 31091+ struct au_vfsub_rmdir_args args = {
31092+ .errp = &err,
31093+ .dir = dir,
31094+ .path = path
31095+ };
31096+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
31097+ if (unlikely(wkq_err))
31098+ err = wkq_err;
31099+ }
31100+
31101+ return err;
31102+}
31103+
31104+/* ---------------------------------------------------------------------- */
31105+
31106+struct notify_change_args {
31107+ int *errp;
31108+ struct path *path;
31109+ struct iattr *ia;
523b37e3 31110+ struct inode **delegated_inode;
1facf9fc 31111+};
31112+
31113+static void call_notify_change(void *args)
31114+{
31115+ struct notify_change_args *a = args;
31116+ struct inode *h_inode;
31117+
5527c038 31118+ h_inode = d_inode(a->path->dentry);
1facf9fc 31119+ IMustLock(h_inode);
31120+
31121+ *a->errp = -EPERM;
31122+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 31123+ lockdep_off();
523b37e3
AM
31124+ *a->errp = notify_change(a->path->dentry, a->ia,
31125+ a->delegated_inode);
c1595e42 31126+ lockdep_on();
1facf9fc 31127+ if (!*a->errp)
31128+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
31129+ }
31130+ AuTraceErr(*a->errp);
31131+}
31132+
523b37e3
AM
31133+int vfsub_notify_change(struct path *path, struct iattr *ia,
31134+ struct inode **delegated_inode)
1facf9fc 31135+{
31136+ int err;
31137+ struct notify_change_args args = {
523b37e3
AM
31138+ .errp = &err,
31139+ .path = path,
31140+ .ia = ia,
31141+ .delegated_inode = delegated_inode
1facf9fc 31142+ };
31143+
31144+ call_notify_change(&args);
31145+
31146+ return err;
31147+}
31148+
523b37e3
AM
31149+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31150+ struct inode **delegated_inode)
1facf9fc 31151+{
31152+ int err, wkq_err;
31153+ struct notify_change_args args = {
523b37e3
AM
31154+ .errp = &err,
31155+ .path = path,
31156+ .ia = ia,
31157+ .delegated_inode = delegated_inode
1facf9fc 31158+ };
31159+
31160+ wkq_err = au_wkq_wait(call_notify_change, &args);
31161+ if (unlikely(wkq_err))
31162+ err = wkq_err;
31163+
31164+ return err;
31165+}
31166+
31167+/* ---------------------------------------------------------------------- */
31168+
31169+struct unlink_args {
31170+ int *errp;
31171+ struct inode *dir;
31172+ struct path *path;
523b37e3 31173+ struct inode **delegated_inode;
1facf9fc 31174+};
31175+
31176+static void call_unlink(void *args)
31177+{
31178+ struct unlink_args *a = args;
31179+ struct dentry *d = a->path->dentry;
31180+ struct inode *h_inode;
31181+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 31182+ && au_dcount(d) == 1);
1facf9fc 31183+
31184+ IMustLock(a->dir);
31185+
31186+ a->path->dentry = d->d_parent;
31187+ *a->errp = security_path_unlink(a->path, d);
31188+ a->path->dentry = d;
31189+ if (unlikely(*a->errp))
31190+ return;
31191+
31192+ if (!stop_sillyrename)
31193+ dget(d);
5527c038
JR
31194+ h_inode = NULL;
31195+ if (d_is_positive(d)) {
31196+ h_inode = d_inode(d);
027c5e7a 31197+ ihold(h_inode);
5527c038 31198+ }
1facf9fc 31199+
2cbb1c4b 31200+ lockdep_off();
523b37e3 31201+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 31202+ lockdep_on();
1facf9fc 31203+ if (!*a->errp) {
31204+ struct path tmp = {
31205+ .dentry = d->d_parent,
31206+ .mnt = a->path->mnt
31207+ };
31208+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
31209+ }
31210+
31211+ if (!stop_sillyrename)
31212+ dput(d);
31213+ if (h_inode)
31214+ iput(h_inode);
31215+
31216+ AuTraceErr(*a->errp);
31217+}
31218+
31219+/*
31220+ * @dir: must be locked.
31221+ * @dentry: target dentry.
31222+ */
523b37e3
AM
31223+int vfsub_unlink(struct inode *dir, struct path *path,
31224+ struct inode **delegated_inode, int force)
1facf9fc 31225+{
31226+ int err;
31227+ struct unlink_args args = {
523b37e3
AM
31228+ .errp = &err,
31229+ .dir = dir,
31230+ .path = path,
31231+ .delegated_inode = delegated_inode
1facf9fc 31232+ };
31233+
31234+ if (!force)
31235+ call_unlink(&args);
31236+ else {
31237+ int wkq_err;
31238+
31239+ wkq_err = au_wkq_wait(call_unlink, &args);
31240+ if (unlikely(wkq_err))
31241+ err = wkq_err;
31242+ }
31243+
31244+ return err;
31245+}
7f207e10
AM
31246diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
31247--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
31248+++ linux/fs/aufs/vfsub.h 2016-08-17 18:01:06.161889951 +0200
31249@@ -0,0 +1,316 @@
1facf9fc 31250+/*
8cdd5066 31251+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 31252+ *
31253+ * This program, aufs is free software; you can redistribute it and/or modify
31254+ * it under the terms of the GNU General Public License as published by
31255+ * the Free Software Foundation; either version 2 of the License, or
31256+ * (at your option) any later version.
dece6358
AM
31257+ *
31258+ * This program is distributed in the hope that it will be useful,
31259+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31260+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31261+ * GNU General Public License for more details.
31262+ *
31263+ * You should have received a copy of the GNU General Public License
523b37e3 31264+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31265+ */
31266+
31267+/*
31268+ * sub-routines for VFS
31269+ */
31270+
31271+#ifndef __AUFS_VFSUB_H__
31272+#define __AUFS_VFSUB_H__
31273+
31274+#ifdef __KERNEL__
31275+
31276+#include <linux/fs.h>
b4510431 31277+#include <linux/mount.h>
8cdd5066 31278+#include <linux/posix_acl.h>
c1595e42 31279+#include <linux/xattr.h>
7f207e10 31280+#include "debug.h"
1facf9fc 31281+
7f207e10 31282+/* copied from linux/fs/internal.h */
2cbb1c4b 31283+/* todo: BAD approach!! */
c06a8ce3 31284+extern void __mnt_drop_write(struct vfsmount *);
b912730e 31285+extern int open_check_o_direct(struct file *f);
7f207e10
AM
31286+
31287+/* ---------------------------------------------------------------------- */
1facf9fc 31288+
31289+/* lock subclass for lower inode */
31290+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
31291+/* reduce? gave up. */
31292+enum {
c1595e42 31293+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 31294+ AuLsc_I_PARENT, /* lower inode, parent first */
31295+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 31296+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 31297+ AuLsc_I_CHILD,
31298+ AuLsc_I_CHILD2,
31299+ AuLsc_I_End
31300+};
31301+
31302+/* to debug easier, do not make them inlined functions */
31303+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 31304+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 31305+
31306+/* ---------------------------------------------------------------------- */
31307+
7f207e10
AM
31308+static inline void vfsub_drop_nlink(struct inode *inode)
31309+{
31310+ AuDebugOn(!inode->i_nlink);
31311+ drop_nlink(inode);
31312+}
31313+
027c5e7a
AM
31314+static inline void vfsub_dead_dir(struct inode *inode)
31315+{
31316+ AuDebugOn(!S_ISDIR(inode->i_mode));
31317+ inode->i_flags |= S_DEAD;
31318+ clear_nlink(inode);
31319+}
31320+
392086de
AM
31321+static inline int vfsub_native_ro(struct inode *inode)
31322+{
31323+ return (inode->i_sb->s_flags & MS_RDONLY)
31324+ || IS_RDONLY(inode)
31325+ /* || IS_APPEND(inode) */
31326+ || IS_IMMUTABLE(inode);
31327+}
31328+
8cdd5066
JR
31329+#ifdef CONFIG_AUFS_BR_FUSE
31330+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
31331+#else
31332+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
31333+#endif
31334+
7f207e10
AM
31335+/* ---------------------------------------------------------------------- */
31336+
31337+int vfsub_update_h_iattr(struct path *h_path, int *did);
31338+struct file *vfsub_dentry_open(struct path *path, int flags);
31339+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
31340+struct vfsub_aopen_args {
31341+ struct file *file;
31342+ unsigned int open_flag;
31343+ umode_t create_mode;
31344+ int *opened;
31345+};
31346+struct au_branch;
31347+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
31348+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 31349+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 31350+
febd17d6
JR
31351+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
31352+ struct dentry *parent, int len);
1facf9fc 31353+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
31354+ int len);
b4510431
AM
31355+
31356+struct vfsub_lkup_one_args {
31357+ struct dentry **errp;
31358+ struct qstr *name;
31359+ struct dentry *parent;
31360+};
31361+
31362+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
31363+ struct dentry *parent)
31364+{
31365+ return vfsub_lookup_one_len(name->name, parent, name->len);
31366+}
31367+
31368+void vfsub_call_lkup_one(void *args);
31369+
31370+/* ---------------------------------------------------------------------- */
31371+
31372+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
31373+{
31374+ int err;
076b876e 31375+
b4510431
AM
31376+ lockdep_off();
31377+ err = mnt_want_write(mnt);
31378+ lockdep_on();
31379+ return err;
31380+}
31381+
31382+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
31383+{
31384+ lockdep_off();
31385+ mnt_drop_write(mnt);
31386+ lockdep_on();
31387+}
1facf9fc 31388+
7e9cd9fe 31389+#if 0 /* reserved */
c06a8ce3
AM
31390+static inline void vfsub_mnt_drop_write_file(struct file *file)
31391+{
31392+ lockdep_off();
31393+ mnt_drop_write_file(file);
31394+ lockdep_on();
31395+}
7e9cd9fe 31396+#endif
c06a8ce3 31397+
1facf9fc 31398+/* ---------------------------------------------------------------------- */
31399+
31400+struct au_hinode;
31401+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
31402+ struct dentry *d2, struct au_hinode *hdir2);
31403+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
31404+ struct dentry *d2, struct au_hinode *hdir2);
31405+
537831f9
AM
31406+int vfsub_create(struct inode *dir, struct path *path, int mode,
31407+ bool want_excl);
1facf9fc 31408+int vfsub_symlink(struct inode *dir, struct path *path,
31409+ const char *symname);
31410+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
31411+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 31412+ struct path *path, struct inode **delegated_inode);
1facf9fc 31413+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3
AM
31414+ struct inode *hdir, struct path *path,
31415+ struct inode **delegated_inode);
1facf9fc 31416+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
31417+int vfsub_rmdir(struct inode *dir, struct path *path);
31418+
31419+/* ---------------------------------------------------------------------- */
31420+
31421+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31422+ loff_t *ppos);
31423+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31424+ loff_t *ppos);
31425+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31426+ loff_t *ppos);
31427+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
31428+ loff_t *ppos);
4a4d8108 31429+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
31430+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
31431+
c06a8ce3
AM
31432+static inline loff_t vfsub_f_size_read(struct file *file)
31433+{
31434+ return i_size_read(file_inode(file));
31435+}
31436+
4a4d8108
AM
31437+static inline unsigned int vfsub_file_flags(struct file *file)
31438+{
31439+ unsigned int flags;
31440+
31441+ spin_lock(&file->f_lock);
31442+ flags = file->f_flags;
31443+ spin_unlock(&file->f_lock);
31444+
31445+ return flags;
31446+}
1308ab2a 31447+
f0c0a007
AM
31448+static inline int vfsub_file_execed(struct file *file)
31449+{
31450+ /* todo: direct access f_flags */
31451+ return !!(vfsub_file_flags(file) & __FMODE_EXEC);
31452+}
31453+
7e9cd9fe 31454+#if 0 /* reserved */
1facf9fc 31455+static inline void vfsub_file_accessed(struct file *h_file)
31456+{
31457+ file_accessed(h_file);
31458+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
31459+}
7e9cd9fe 31460+#endif
1facf9fc 31461+
79b8bda9 31462+#if 0 /* reserved */
1facf9fc 31463+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
31464+ struct dentry *h_dentry)
31465+{
31466+ struct path h_path = {
31467+ .dentry = h_dentry,
31468+ .mnt = h_mnt
31469+ };
92d182d2 31470+ touch_atime(&h_path);
1facf9fc 31471+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
31472+}
79b8bda9 31473+#endif
1facf9fc 31474+
0c3ec466
AM
31475+static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
31476+ int flags)
31477+{
5afbbe0d 31478+ return update_time(h_inode, ts, flags);
0c3ec466
AM
31479+ /* no vfsub_update_h_iattr() since we don't have struct path */
31480+}
31481+
8cdd5066
JR
31482+#ifdef CONFIG_FS_POSIX_ACL
31483+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
31484+{
31485+ int err;
31486+
31487+ err = posix_acl_chmod(h_inode, h_mode);
31488+ if (err == -EOPNOTSUPP)
31489+ err = 0;
31490+ return err;
31491+}
31492+#else
31493+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
31494+#endif
31495+
4a4d8108
AM
31496+long vfsub_splice_to(struct file *in, loff_t *ppos,
31497+ struct pipe_inode_info *pipe, size_t len,
31498+ unsigned int flags);
31499+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31500+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
31501+
31502+static inline long vfsub_truncate(struct path *path, loff_t length)
31503+{
31504+ long err;
076b876e 31505+
c06a8ce3
AM
31506+ lockdep_off();
31507+ err = vfs_truncate(path, length);
31508+ lockdep_on();
31509+ return err;
31510+}
31511+
4a4d8108
AM
31512+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31513+ struct file *h_file);
53392da6 31514+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 31515+
1facf9fc 31516+/* ---------------------------------------------------------------------- */
31517+
31518+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
31519+{
31520+ loff_t err;
31521+
2cbb1c4b 31522+ lockdep_off();
1facf9fc 31523+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 31524+ lockdep_on();
1facf9fc 31525+ return err;
31526+}
31527+
31528+/* ---------------------------------------------------------------------- */
31529+
4a4d8108
AM
31530+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
31531+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
31532+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31533+ struct inode **delegated_inode);
31534+int vfsub_notify_change(struct path *path, struct iattr *ia,
31535+ struct inode **delegated_inode);
31536+int vfsub_unlink(struct inode *dir, struct path *path,
31537+ struct inode **delegated_inode, int force);
4a4d8108 31538+
c1595e42
JR
31539+/* ---------------------------------------------------------------------- */
31540+
31541+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
31542+ const void *value, size_t size, int flags)
31543+{
31544+ int err;
31545+
31546+ lockdep_off();
31547+ err = vfs_setxattr(dentry, name, value, size, flags);
31548+ lockdep_on();
31549+
31550+ return err;
31551+}
31552+
31553+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31554+{
31555+ int err;
31556+
31557+ lockdep_off();
31558+ err = vfs_removexattr(dentry, name);
31559+ lockdep_on();
31560+
31561+ return err;
31562+}
31563+
1facf9fc 31564+#endif /* __KERNEL__ */
31565+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
31566diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31567--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 31568+++ linux/fs/aufs/wbr_policy.c 2016-08-17 18:01:06.161889951 +0200
076b876e 31569@@ -0,0 +1,765 @@
1facf9fc 31570+/*
8cdd5066 31571+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 31572+ *
31573+ * This program, aufs is free software; you can redistribute it and/or modify
31574+ * it under the terms of the GNU General Public License as published by
31575+ * the Free Software Foundation; either version 2 of the License, or
31576+ * (at your option) any later version.
dece6358
AM
31577+ *
31578+ * This program is distributed in the hope that it will be useful,
31579+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31580+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31581+ * GNU General Public License for more details.
31582+ *
31583+ * You should have received a copy of the GNU General Public License
523b37e3 31584+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31585+ */
31586+
31587+/*
31588+ * policies for selecting one among multiple writable branches
31589+ */
31590+
31591+#include <linux/statfs.h>
31592+#include "aufs.h"
31593+
31594+/* subset of cpup_attr() */
31595+static noinline_for_stack
31596+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31597+{
31598+ int err, sbits;
31599+ struct iattr ia;
31600+ struct inode *h_isrc;
31601+
5527c038 31602+ h_isrc = d_inode(h_src);
1facf9fc 31603+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31604+ ia.ia_mode = h_isrc->i_mode;
31605+ ia.ia_uid = h_isrc->i_uid;
31606+ ia.ia_gid = h_isrc->i_gid;
31607+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 31608+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
31609+ /* no delegation since it is just created */
31610+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 31611+
31612+ /* is this nfs only? */
31613+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31614+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31615+ ia.ia_mode = h_isrc->i_mode;
523b37e3 31616+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 31617+ }
31618+
31619+ return err;
31620+}
31621+
31622+#define AuCpdown_PARENT_OPQ 1
31623+#define AuCpdown_WHED (1 << 1)
31624+#define AuCpdown_MADE_DIR (1 << 2)
31625+#define AuCpdown_DIROPQ (1 << 3)
31626+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
31627+#define au_fset_cpdown(flags, name) \
31628+ do { (flags) |= AuCpdown_##name; } while (0)
31629+#define au_fclr_cpdown(flags, name) \
31630+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 31631+
1facf9fc 31632+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 31633+ unsigned int *flags)
1facf9fc 31634+{
31635+ int err;
31636+ struct dentry *opq_dentry;
31637+
31638+ opq_dentry = au_diropq_create(dentry, bdst);
31639+ err = PTR_ERR(opq_dentry);
31640+ if (IS_ERR(opq_dentry))
31641+ goto out;
31642+ dput(opq_dentry);
c2b27bf2 31643+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 31644+
4f0767ce 31645+out:
1facf9fc 31646+ return err;
31647+}
31648+
31649+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31650+ struct inode *dir, aufs_bindex_t bdst)
31651+{
31652+ int err;
31653+ struct path h_path;
31654+ struct au_branch *br;
31655+
31656+ br = au_sbr(dentry->d_sb, bdst);
31657+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31658+ err = PTR_ERR(h_path.dentry);
31659+ if (IS_ERR(h_path.dentry))
31660+ goto out;
31661+
31662+ err = 0;
5527c038 31663+ if (d_is_positive(h_path.dentry)) {
86dc4139 31664+ h_path.mnt = au_br_mnt(br);
1facf9fc 31665+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31666+ dentry);
31667+ }
31668+ dput(h_path.dentry);
31669+
4f0767ce 31670+out:
1facf9fc 31671+ return err;
31672+}
31673+
31674+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 31675+ struct au_pin *pin,
1facf9fc 31676+ struct dentry *h_parent, void *arg)
31677+{
31678+ int err, rerr;
5afbbe0d 31679+ aufs_bindex_t bopq, btop;
1facf9fc 31680+ struct path h_path;
31681+ struct dentry *parent;
31682+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 31683+ unsigned int *flags = arg;
1facf9fc 31684+
5afbbe0d 31685+ btop = au_dbtop(dentry);
1facf9fc 31686+ /* dentry is di-locked */
31687+ parent = dget_parent(dentry);
5527c038
JR
31688+ dir = d_inode(parent);
31689+ h_dir = d_inode(h_parent);
1facf9fc 31690+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31691+ IMustLock(h_dir);
31692+
86dc4139 31693+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 31694+ if (unlikely(err < 0))
31695+ goto out;
31696+ h_path.dentry = au_h_dptr(dentry, bdst);
31697+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31698+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31699+ S_IRWXU | S_IRUGO | S_IXUGO);
31700+ if (unlikely(err))
31701+ goto out_put;
c2b27bf2 31702+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 31703+
1facf9fc 31704+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
31705+ au_fclr_cpdown(*flags, WHED);
31706+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 31707+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
31708+ au_fset_cpdown(*flags, WHED);
31709+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31710+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 31711+ h_inode = d_inode(h_path.dentry);
febd17d6 31712+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
31713+ if (au_ftest_cpdown(*flags, WHED)) {
31714+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 31715+ if (unlikely(err)) {
febd17d6 31716+ inode_unlock(h_inode);
1facf9fc 31717+ goto out_dir;
31718+ }
31719+ }
31720+
5afbbe0d 31721+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 31722+ inode_unlock(h_inode);
1facf9fc 31723+ if (unlikely(err))
31724+ goto out_opq;
31725+
c2b27bf2 31726+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 31727+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31728+ if (unlikely(err))
31729+ goto out_opq;
31730+ }
31731+
5527c038 31732+ inode = d_inode(dentry);
5afbbe0d
AM
31733+ if (au_ibbot(inode) < bdst)
31734+ au_set_ibbot(inode, bdst);
1facf9fc 31735+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31736+ au_hi_flags(inode, /*isdir*/1));
076b876e 31737+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 31738+ goto out; /* success */
31739+
31740+ /* revert */
4f0767ce 31741+out_opq:
c2b27bf2 31742+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 31743+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 31744+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 31745+ inode_unlock(h_inode);
1facf9fc 31746+ if (unlikely(rerr)) {
523b37e3
AM
31747+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31748+ dentry, bdst, rerr);
1facf9fc 31749+ err = -EIO;
31750+ goto out;
31751+ }
31752+ }
4f0767ce 31753+out_dir:
c2b27bf2 31754+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 31755+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31756+ if (unlikely(rerr)) {
523b37e3
AM
31757+ AuIOErr("failed removing %pd b%d (%d)\n",
31758+ dentry, bdst, rerr);
1facf9fc 31759+ err = -EIO;
31760+ }
31761+ }
4f0767ce 31762+out_put:
1facf9fc 31763+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
31764+ if (au_dbbot(dentry) == bdst)
31765+ au_update_dbbot(dentry);
4f0767ce 31766+out:
1facf9fc 31767+ dput(parent);
31768+ return err;
31769+}
31770+
31771+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31772+{
31773+ int err;
c2b27bf2 31774+ unsigned int flags;
1facf9fc 31775+
c2b27bf2
AM
31776+ flags = 0;
31777+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 31778+
31779+ return err;
31780+}
31781+
31782+/* ---------------------------------------------------------------------- */
31783+
31784+/* policies for create */
31785+
c2b27bf2 31786+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
31787+{
31788+ int err, i, j, ndentry;
31789+ aufs_bindex_t bopq;
31790+ struct au_dcsub_pages dpages;
31791+ struct au_dpage *dpage;
31792+ struct dentry **dentries, *parent, *d;
31793+
31794+ err = au_dpages_init(&dpages, GFP_NOFS);
31795+ if (unlikely(err))
31796+ goto out;
31797+ parent = dget_parent(dentry);
027c5e7a 31798+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
31799+ if (unlikely(err))
31800+ goto out_free;
31801+
31802+ err = bindex;
31803+ for (i = 0; i < dpages.ndpage; i++) {
31804+ dpage = dpages.dpages + i;
31805+ dentries = dpage->dentries;
31806+ ndentry = dpage->ndentry;
31807+ for (j = 0; j < ndentry; j++) {
31808+ d = dentries[j];
31809+ di_read_lock_parent2(d, !AuLock_IR);
31810+ bopq = au_dbdiropq(d);
31811+ di_read_unlock(d, !AuLock_IR);
31812+ if (bopq >= 0 && bopq < err)
31813+ err = bopq;
31814+ }
31815+ }
31816+
31817+out_free:
31818+ dput(parent);
31819+ au_dpages_free(&dpages);
31820+out:
31821+ return err;
31822+}
31823+
1facf9fc 31824+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
31825+{
31826+ for (; bindex >= 0; bindex--)
31827+ if (!au_br_rdonly(au_sbr(sb, bindex)))
31828+ return bindex;
31829+ return -EROFS;
31830+}
31831+
31832+/* top down parent */
392086de
AM
31833+static int au_wbr_create_tdp(struct dentry *dentry,
31834+ unsigned int flags __maybe_unused)
1facf9fc 31835+{
31836+ int err;
5afbbe0d 31837+ aufs_bindex_t btop, bindex;
1facf9fc 31838+ struct super_block *sb;
31839+ struct dentry *parent, *h_parent;
31840+
31841+ sb = dentry->d_sb;
5afbbe0d
AM
31842+ btop = au_dbtop(dentry);
31843+ err = btop;
31844+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 31845+ goto out;
31846+
31847+ err = -EROFS;
31848+ parent = dget_parent(dentry);
5afbbe0d 31849+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 31850+ h_parent = au_h_dptr(parent, bindex);
5527c038 31851+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 31852+ continue;
31853+
31854+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
31855+ err = bindex;
31856+ break;
31857+ }
31858+ }
31859+ dput(parent);
31860+
31861+ /* bottom up here */
4a4d8108 31862+ if (unlikely(err < 0)) {
5afbbe0d 31863+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
31864+ if (err >= 0)
31865+ err = au_wbr_nonopq(dentry, err);
31866+ }
1facf9fc 31867+
4f0767ce 31868+out:
1facf9fc 31869+ AuDbg("b%d\n", err);
31870+ return err;
31871+}
31872+
31873+/* ---------------------------------------------------------------------- */
31874+
31875+/* an exception for the policy other than tdp */
31876+static int au_wbr_create_exp(struct dentry *dentry)
31877+{
31878+ int err;
31879+ aufs_bindex_t bwh, bdiropq;
31880+ struct dentry *parent;
31881+
31882+ err = -1;
31883+ bwh = au_dbwh(dentry);
31884+ parent = dget_parent(dentry);
31885+ bdiropq = au_dbdiropq(parent);
31886+ if (bwh >= 0) {
31887+ if (bdiropq >= 0)
31888+ err = min(bdiropq, bwh);
31889+ else
31890+ err = bwh;
31891+ AuDbg("%d\n", err);
31892+ } else if (bdiropq >= 0) {
31893+ err = bdiropq;
31894+ AuDbg("%d\n", err);
31895+ }
31896+ dput(parent);
31897+
4a4d8108
AM
31898+ if (err >= 0)
31899+ err = au_wbr_nonopq(dentry, err);
31900+
1facf9fc 31901+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
31902+ err = -1;
31903+
31904+ AuDbg("%d\n", err);
31905+ return err;
31906+}
31907+
31908+/* ---------------------------------------------------------------------- */
31909+
31910+/* round robin */
31911+static int au_wbr_create_init_rr(struct super_block *sb)
31912+{
31913+ int err;
31914+
5afbbe0d 31915+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 31916+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 31917+ /* smp_mb(); */
1facf9fc 31918+
31919+ AuDbg("b%d\n", err);
31920+ return err;
31921+}
31922+
392086de 31923+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 31924+{
31925+ int err, nbr;
31926+ unsigned int u;
5afbbe0d 31927+ aufs_bindex_t bindex, bbot;
1facf9fc 31928+ struct super_block *sb;
31929+ atomic_t *next;
31930+
31931+ err = au_wbr_create_exp(dentry);
31932+ if (err >= 0)
31933+ goto out;
31934+
31935+ sb = dentry->d_sb;
31936+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
31937+ bbot = au_sbbot(sb);
31938+ nbr = bbot + 1;
31939+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 31940+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 31941+ err = atomic_dec_return(next) + 1;
31942+ /* modulo for 0 is meaningless */
31943+ if (unlikely(!err))
31944+ err = atomic_dec_return(next) + 1;
31945+ } else
31946+ err = atomic_read(next);
31947+ AuDbg("%d\n", err);
31948+ u = err;
31949+ err = u % nbr;
31950+ AuDbg("%d\n", err);
31951+ if (!au_br_rdonly(au_sbr(sb, err)))
31952+ break;
31953+ err = -EROFS;
31954+ }
31955+
4a4d8108
AM
31956+ if (err >= 0)
31957+ err = au_wbr_nonopq(dentry, err);
31958+
4f0767ce 31959+out:
1facf9fc 31960+ AuDbg("%d\n", err);
31961+ return err;
31962+}
31963+
31964+/* ---------------------------------------------------------------------- */
31965+
31966+/* most free space */
392086de 31967+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 31968+{
31969+ struct super_block *sb;
31970+ struct au_branch *br;
31971+ struct au_wbr_mfs *mfs;
392086de 31972+ struct dentry *h_parent;
5afbbe0d 31973+ aufs_bindex_t bindex, bbot;
1facf9fc 31974+ int err;
31975+ unsigned long long b, bavail;
7f207e10 31976+ struct path h_path;
1facf9fc 31977+ /* reduce the stack usage */
31978+ struct kstatfs *st;
31979+
31980+ st = kmalloc(sizeof(*st), GFP_NOFS);
31981+ if (unlikely(!st)) {
31982+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
31983+ return;
31984+ }
31985+
31986+ bavail = 0;
31987+ sb = dentry->d_sb;
31988+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 31989+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 31990+ mfs->mfs_bindex = -EROFS;
31991+ mfs->mfsrr_bytes = 0;
392086de
AM
31992+ if (!parent) {
31993+ bindex = 0;
5afbbe0d 31994+ bbot = au_sbbot(sb);
392086de 31995+ } else {
5afbbe0d
AM
31996+ bindex = au_dbtop(parent);
31997+ bbot = au_dbtaildir(parent);
392086de
AM
31998+ }
31999+
5afbbe0d 32000+ for (; bindex <= bbot; bindex++) {
392086de
AM
32001+ if (parent) {
32002+ h_parent = au_h_dptr(parent, bindex);
5527c038 32003+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
32004+ continue;
32005+ }
1facf9fc 32006+ br = au_sbr(sb, bindex);
32007+ if (au_br_rdonly(br))
32008+ continue;
32009+
32010+ /* sb->s_root for NFS is unreliable */
86dc4139 32011+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
32012+ h_path.dentry = h_path.mnt->mnt_root;
32013+ err = vfs_statfs(&h_path, st);
1facf9fc 32014+ if (unlikely(err)) {
32015+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
32016+ continue;
32017+ }
32018+
32019+ /* when the available size is equal, select the lower one */
32020+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
32021+ || sizeof(b) < sizeof(st->f_bsize));
32022+ b = st->f_bavail * st->f_bsize;
32023+ br->br_wbr->wbr_bytes = b;
32024+ if (b >= bavail) {
32025+ bavail = b;
32026+ mfs->mfs_bindex = bindex;
32027+ mfs->mfs_jiffy = jiffies;
32028+ }
32029+ }
32030+
32031+ mfs->mfsrr_bytes = bavail;
32032+ AuDbg("b%d\n", mfs->mfs_bindex);
f0c0a007 32033+ au_delayed_kfree(st);
1facf9fc 32034+}
32035+
392086de 32036+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 32037+{
32038+ int err;
392086de 32039+ struct dentry *parent;
1facf9fc 32040+ struct super_block *sb;
32041+ struct au_wbr_mfs *mfs;
32042+
32043+ err = au_wbr_create_exp(dentry);
32044+ if (err >= 0)
32045+ goto out;
32046+
32047+ sb = dentry->d_sb;
392086de
AM
32048+ parent = NULL;
32049+ if (au_ftest_wbr(flags, PARENT))
32050+ parent = dget_parent(dentry);
1facf9fc 32051+ mfs = &au_sbi(sb)->si_wbr_mfs;
32052+ mutex_lock(&mfs->mfs_lock);
32053+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
32054+ || mfs->mfs_bindex < 0
32055+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 32056+ au_mfs(dentry, parent);
1facf9fc 32057+ mutex_unlock(&mfs->mfs_lock);
32058+ err = mfs->mfs_bindex;
392086de 32059+ dput(parent);
1facf9fc 32060+
4a4d8108
AM
32061+ if (err >= 0)
32062+ err = au_wbr_nonopq(dentry, err);
32063+
4f0767ce 32064+out:
1facf9fc 32065+ AuDbg("b%d\n", err);
32066+ return err;
32067+}
32068+
32069+static int au_wbr_create_init_mfs(struct super_block *sb)
32070+{
32071+ struct au_wbr_mfs *mfs;
32072+
32073+ mfs = &au_sbi(sb)->si_wbr_mfs;
32074+ mutex_init(&mfs->mfs_lock);
32075+ mfs->mfs_jiffy = 0;
32076+ mfs->mfs_bindex = -EROFS;
32077+
32078+ return 0;
32079+}
32080+
32081+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
32082+{
32083+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
32084+ return 0;
32085+}
32086+
32087+/* ---------------------------------------------------------------------- */
32088+
32089+/* most free space and then round robin */
392086de 32090+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 32091+{
32092+ int err;
32093+ struct au_wbr_mfs *mfs;
32094+
392086de 32095+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 32096+ if (err >= 0) {
32097+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 32098+ mutex_lock(&mfs->mfs_lock);
1facf9fc 32099+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 32100+ err = au_wbr_create_rr(dentry, flags);
dece6358 32101+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 32102+ }
32103+
32104+ AuDbg("b%d\n", err);
32105+ return err;
32106+}
32107+
32108+static int au_wbr_create_init_mfsrr(struct super_block *sb)
32109+{
32110+ int err;
32111+
32112+ au_wbr_create_init_mfs(sb); /* ignore */
32113+ err = au_wbr_create_init_rr(sb);
32114+
32115+ return err;
32116+}
32117+
32118+/* ---------------------------------------------------------------------- */
32119+
32120+/* top down parent and most free space */
392086de 32121+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 32122+{
32123+ int err, e2;
32124+ unsigned long long b;
5afbbe0d 32125+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 32126+ struct super_block *sb;
32127+ struct dentry *parent, *h_parent;
32128+ struct au_branch *br;
32129+
392086de 32130+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 32131+ if (unlikely(err < 0))
32132+ goto out;
32133+ parent = dget_parent(dentry);
5afbbe0d
AM
32134+ btop = au_dbtop(parent);
32135+ bbot = au_dbtaildir(parent);
32136+ if (btop == bbot)
1facf9fc 32137+ goto out_parent; /* success */
32138+
392086de 32139+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 32140+ if (e2 < 0)
32141+ goto out_parent; /* success */
32142+
32143+ /* when the available size is equal, select upper one */
32144+ sb = dentry->d_sb;
32145+ br = au_sbr(sb, err);
32146+ b = br->br_wbr->wbr_bytes;
32147+ AuDbg("b%d, %llu\n", err, b);
32148+
5afbbe0d 32149+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 32150+ h_parent = au_h_dptr(parent, bindex);
5527c038 32151+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 32152+ continue;
32153+
32154+ br = au_sbr(sb, bindex);
32155+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
32156+ b = br->br_wbr->wbr_bytes;
32157+ err = bindex;
32158+ AuDbg("b%d, %llu\n", err, b);
32159+ }
32160+ }
32161+
4a4d8108
AM
32162+ if (err >= 0)
32163+ err = au_wbr_nonopq(dentry, err);
32164+
4f0767ce 32165+out_parent:
1facf9fc 32166+ dput(parent);
4f0767ce 32167+out:
1facf9fc 32168+ AuDbg("b%d\n", err);
32169+ return err;
32170+}
32171+
32172+/* ---------------------------------------------------------------------- */
32173+
392086de
AM
32174+/*
32175+ * - top down parent
32176+ * - most free space with parent
32177+ * - most free space round-robin regardless parent
32178+ */
32179+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
32180+{
32181+ int err;
32182+ unsigned long long watermark;
32183+ struct super_block *sb;
32184+ struct au_branch *br;
32185+ struct au_wbr_mfs *mfs;
32186+
32187+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
32188+ if (unlikely(err < 0))
32189+ goto out;
32190+
32191+ sb = dentry->d_sb;
32192+ br = au_sbr(sb, err);
32193+ mfs = &au_sbi(sb)->si_wbr_mfs;
32194+ mutex_lock(&mfs->mfs_lock);
32195+ watermark = mfs->mfsrr_watermark;
32196+ mutex_unlock(&mfs->mfs_lock);
32197+ if (br->br_wbr->wbr_bytes < watermark)
32198+ /* regardless the parent dir */
32199+ err = au_wbr_create_mfsrr(dentry, flags);
32200+
32201+out:
32202+ AuDbg("b%d\n", err);
32203+ return err;
32204+}
32205+
32206+/* ---------------------------------------------------------------------- */
32207+
1facf9fc 32208+/* policies for copyup */
32209+
32210+/* top down parent */
32211+static int au_wbr_copyup_tdp(struct dentry *dentry)
32212+{
392086de 32213+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 32214+}
32215+
32216+/* bottom up parent */
32217+static int au_wbr_copyup_bup(struct dentry *dentry)
32218+{
32219+ int err;
5afbbe0d 32220+ aufs_bindex_t bindex, btop;
1facf9fc 32221+ struct dentry *parent, *h_parent;
32222+ struct super_block *sb;
32223+
32224+ err = -EROFS;
32225+ sb = dentry->d_sb;
32226+ parent = dget_parent(dentry);
5afbbe0d
AM
32227+ btop = au_dbtop(parent);
32228+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 32229+ h_parent = au_h_dptr(parent, bindex);
5527c038 32230+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 32231+ continue;
32232+
32233+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
32234+ err = bindex;
32235+ break;
32236+ }
32237+ }
32238+ dput(parent);
32239+
32240+ /* bottom up here */
32241+ if (unlikely(err < 0))
5afbbe0d 32242+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 32243+
32244+ AuDbg("b%d\n", err);
32245+ return err;
32246+}
32247+
32248+/* bottom up */
5afbbe0d 32249+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 32250+{
32251+ int err;
32252+
5afbbe0d 32253+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 32254+ AuDbg("b%d\n", err);
5afbbe0d 32255+ if (err > btop)
4a4d8108 32256+ err = au_wbr_nonopq(dentry, err);
1facf9fc 32257+
32258+ AuDbg("b%d\n", err);
32259+ return err;
32260+}
32261+
076b876e
AM
32262+static int au_wbr_copyup_bu(struct dentry *dentry)
32263+{
32264+ int err;
5afbbe0d 32265+ aufs_bindex_t btop;
076b876e 32266+
5afbbe0d
AM
32267+ btop = au_dbtop(dentry);
32268+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
32269+ return err;
32270+}
32271+
1facf9fc 32272+/* ---------------------------------------------------------------------- */
32273+
32274+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
32275+ [AuWbrCopyup_TDP] = {
32276+ .copyup = au_wbr_copyup_tdp
32277+ },
32278+ [AuWbrCopyup_BUP] = {
32279+ .copyup = au_wbr_copyup_bup
32280+ },
32281+ [AuWbrCopyup_BU] = {
32282+ .copyup = au_wbr_copyup_bu
32283+ }
32284+};
32285+
32286+struct au_wbr_create_operations au_wbr_create_ops[] = {
32287+ [AuWbrCreate_TDP] = {
32288+ .create = au_wbr_create_tdp
32289+ },
32290+ [AuWbrCreate_RR] = {
32291+ .create = au_wbr_create_rr,
32292+ .init = au_wbr_create_init_rr
32293+ },
32294+ [AuWbrCreate_MFS] = {
32295+ .create = au_wbr_create_mfs,
32296+ .init = au_wbr_create_init_mfs,
32297+ .fin = au_wbr_create_fin_mfs
32298+ },
32299+ [AuWbrCreate_MFSV] = {
32300+ .create = au_wbr_create_mfs,
32301+ .init = au_wbr_create_init_mfs,
32302+ .fin = au_wbr_create_fin_mfs
32303+ },
32304+ [AuWbrCreate_MFSRR] = {
32305+ .create = au_wbr_create_mfsrr,
32306+ .init = au_wbr_create_init_mfsrr,
32307+ .fin = au_wbr_create_fin_mfs
32308+ },
32309+ [AuWbrCreate_MFSRRV] = {
32310+ .create = au_wbr_create_mfsrr,
32311+ .init = au_wbr_create_init_mfsrr,
32312+ .fin = au_wbr_create_fin_mfs
32313+ },
32314+ [AuWbrCreate_PMFS] = {
32315+ .create = au_wbr_create_pmfs,
32316+ .init = au_wbr_create_init_mfs,
32317+ .fin = au_wbr_create_fin_mfs
32318+ },
32319+ [AuWbrCreate_PMFSV] = {
32320+ .create = au_wbr_create_pmfs,
32321+ .init = au_wbr_create_init_mfs,
32322+ .fin = au_wbr_create_fin_mfs
392086de
AM
32323+ },
32324+ [AuWbrCreate_PMFSRR] = {
32325+ .create = au_wbr_create_pmfsrr,
32326+ .init = au_wbr_create_init_mfsrr,
32327+ .fin = au_wbr_create_fin_mfs
32328+ },
32329+ [AuWbrCreate_PMFSRRV] = {
32330+ .create = au_wbr_create_pmfsrr,
32331+ .init = au_wbr_create_init_mfsrr,
32332+ .fin = au_wbr_create_fin_mfs
1facf9fc 32333+ }
32334+};
7f207e10
AM
32335diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
32336--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007 32337+++ linux/fs/aufs/whout.c 2016-08-17 18:01:06.161889951 +0200
be52b249 32338@@ -0,0 +1,1060 @@
1facf9fc 32339+/*
8cdd5066 32340+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 32341+ *
32342+ * This program, aufs is free software; you can redistribute it and/or modify
32343+ * it under the terms of the GNU General Public License as published by
32344+ * the Free Software Foundation; either version 2 of the License, or
32345+ * (at your option) any later version.
dece6358
AM
32346+ *
32347+ * This program is distributed in the hope that it will be useful,
32348+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32349+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32350+ * GNU General Public License for more details.
32351+ *
32352+ * You should have received a copy of the GNU General Public License
523b37e3 32353+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32354+ */
32355+
32356+/*
32357+ * whiteout for logical deletion and opaque directory
32358+ */
32359+
1facf9fc 32360+#include "aufs.h"
32361+
32362+#define WH_MASK S_IRUGO
32363+
32364+/*
32365+ * If a directory contains this file, then it is opaque. We start with the
32366+ * .wh. flag so that it is blocked by lookup.
32367+ */
0c3ec466
AM
32368+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
32369+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 32370+
32371+/*
32372+ * generate whiteout name, which is NOT terminated by NULL.
32373+ * @name: original d_name.name
32374+ * @len: original d_name.len
32375+ * @wh: whiteout qstr
32376+ * returns zero when succeeds, otherwise error.
32377+ * succeeded value as wh->name should be freed by kfree().
32378+ */
32379+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
32380+{
32381+ char *p;
32382+
32383+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
32384+ return -ENAMETOOLONG;
32385+
32386+ wh->len = name->len + AUFS_WH_PFX_LEN;
32387+ p = kmalloc(wh->len, GFP_NOFS);
32388+ wh->name = p;
32389+ if (p) {
32390+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32391+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
32392+ /* smp_mb(); */
32393+ return 0;
32394+ }
32395+ return -ENOMEM;
32396+}
32397+
32398+/* ---------------------------------------------------------------------- */
32399+
32400+/*
32401+ * test if the @wh_name exists under @h_parent.
32402+ * @try_sio specifies the necessary of super-io.
32403+ */
076b876e 32404+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 32405+{
32406+ int err;
32407+ struct dentry *wh_dentry;
1facf9fc 32408+
1facf9fc 32409+ if (!try_sio)
b4510431 32410+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 32411+ else
076b876e 32412+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 32413+ err = PTR_ERR(wh_dentry);
2000de60
JR
32414+ if (IS_ERR(wh_dentry)) {
32415+ if (err == -ENAMETOOLONG)
32416+ err = 0;
1facf9fc 32417+ goto out;
2000de60 32418+ }
1facf9fc 32419+
32420+ err = 0;
5527c038 32421+ if (d_is_negative(wh_dentry))
1facf9fc 32422+ goto out_wh; /* success */
32423+
32424+ err = 1;
7e9cd9fe 32425+ if (d_is_reg(wh_dentry))
1facf9fc 32426+ goto out_wh; /* success */
32427+
32428+ err = -EIO;
523b37e3 32429+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 32430+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 32431+
4f0767ce 32432+out_wh:
1facf9fc 32433+ dput(wh_dentry);
4f0767ce 32434+out:
1facf9fc 32435+ return err;
32436+}
32437+
32438+/*
32439+ * test if the @h_dentry sets opaque or not.
32440+ */
076b876e 32441+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 32442+{
32443+ int err;
32444+ struct inode *h_dir;
32445+
5527c038 32446+ h_dir = d_inode(h_dentry);
076b876e 32447+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 32448+ au_test_h_perm_sio(h_dir, MAY_EXEC));
32449+ return err;
32450+}
32451+
32452+/*
32453+ * returns a negative dentry whose name is unique and temporary.
32454+ */
32455+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32456+ struct qstr *prefix)
32457+{
1facf9fc 32458+ struct dentry *dentry;
32459+ int i;
027c5e7a 32460+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 32461+ *name, *p;
027c5e7a 32462+ /* strict atomic_t is unnecessary here */
1facf9fc 32463+ static unsigned short cnt;
32464+ struct qstr qs;
32465+
4a4d8108
AM
32466+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
32467+
1facf9fc 32468+ name = defname;
027c5e7a
AM
32469+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
32470+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 32471+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 32472+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 32473+ goto out;
32474+ dentry = ERR_PTR(-ENOMEM);
32475+ name = kmalloc(qs.len + 1, GFP_NOFS);
32476+ if (unlikely(!name))
32477+ goto out;
32478+ }
32479+
32480+ /* doubly whiteout-ed */
32481+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
32482+ p = name + AUFS_WH_PFX_LEN * 2;
32483+ memcpy(p, prefix->name, prefix->len);
32484+ p += prefix->len;
32485+ *p++ = '.';
4a4d8108 32486+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 32487+
32488+ qs.name = name;
32489+ for (i = 0; i < 3; i++) {
b752ccd1 32490+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 32491+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 32492+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 32493+ goto out_name;
32494+ dput(dentry);
32495+ }
0c3ec466 32496+ /* pr_warn("could not get random name\n"); */
1facf9fc 32497+ dentry = ERR_PTR(-EEXIST);
32498+ AuDbg("%.*s\n", AuLNPair(&qs));
32499+ BUG();
32500+
4f0767ce 32501+out_name:
1facf9fc 32502+ if (name != defname)
f0c0a007 32503+ au_delayed_kfree(name);
4f0767ce 32504+out:
4a4d8108 32505+ AuTraceErrPtr(dentry);
1facf9fc 32506+ return dentry;
1facf9fc 32507+}
32508+
32509+/*
32510+ * rename the @h_dentry on @br to the whiteouted temporary name.
32511+ */
32512+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
32513+{
32514+ int err;
32515+ struct path h_path = {
86dc4139 32516+ .mnt = au_br_mnt(br)
1facf9fc 32517+ };
523b37e3 32518+ struct inode *h_dir, *delegated;
1facf9fc 32519+ struct dentry *h_parent;
32520+
32521+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 32522+ h_dir = d_inode(h_parent);
1facf9fc 32523+ IMustLock(h_dir);
32524+
32525+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
32526+ err = PTR_ERR(h_path.dentry);
32527+ if (IS_ERR(h_path.dentry))
32528+ goto out;
32529+
32530+ /* under the same dir, no need to lock_rename() */
523b37e3
AM
32531+ delegated = NULL;
32532+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
1facf9fc 32533+ AuTraceErr(err);
523b37e3
AM
32534+ if (unlikely(err == -EWOULDBLOCK)) {
32535+ pr_warn("cannot retry for NFSv4 delegation"
32536+ " for an internal rename\n");
32537+ iput(delegated);
32538+ }
1facf9fc 32539+ dput(h_path.dentry);
32540+
4f0767ce 32541+out:
4a4d8108 32542+ AuTraceErr(err);
1facf9fc 32543+ return err;
32544+}
32545+
32546+/* ---------------------------------------------------------------------- */
32547+/*
32548+ * functions for removing a whiteout
32549+ */
32550+
32551+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32552+{
523b37e3
AM
32553+ int err, force;
32554+ struct inode *delegated;
1facf9fc 32555+
32556+ /*
32557+ * forces superio when the dir has a sticky bit.
32558+ * this may be a violation of unix fs semantics.
32559+ */
32560+ force = (h_dir->i_mode & S_ISVTX)
5527c038 32561+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
32562+ delegated = NULL;
32563+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
32564+ if (unlikely(err == -EWOULDBLOCK)) {
32565+ pr_warn("cannot retry for NFSv4 delegation"
32566+ " for an internal unlink\n");
32567+ iput(delegated);
32568+ }
32569+ return err;
1facf9fc 32570+}
32571+
32572+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32573+ struct dentry *dentry)
32574+{
32575+ int err;
32576+
32577+ err = do_unlink_wh(h_dir, h_path);
32578+ if (!err && dentry)
32579+ au_set_dbwh(dentry, -1);
32580+
32581+ return err;
32582+}
32583+
32584+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32585+ struct au_branch *br)
32586+{
32587+ int err;
32588+ struct path h_path = {
86dc4139 32589+ .mnt = au_br_mnt(br)
1facf9fc 32590+ };
32591+
32592+ err = 0;
b4510431 32593+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 32594+ if (IS_ERR(h_path.dentry))
32595+ err = PTR_ERR(h_path.dentry);
32596+ else {
5527c038
JR
32597+ if (d_is_reg(h_path.dentry))
32598+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 32599+ dput(h_path.dentry);
32600+ }
32601+
32602+ return err;
32603+}
32604+
32605+/* ---------------------------------------------------------------------- */
32606+/*
32607+ * initialize/clean whiteout for a branch
32608+ */
32609+
32610+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32611+ const int isdir)
32612+{
32613+ int err;
523b37e3 32614+ struct inode *delegated;
1facf9fc 32615+
5527c038 32616+ if (d_is_negative(whpath->dentry))
1facf9fc 32617+ return;
32618+
86dc4139
AM
32619+ if (isdir)
32620+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
32621+ else {
32622+ delegated = NULL;
32623+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32624+ if (unlikely(err == -EWOULDBLOCK)) {
32625+ pr_warn("cannot retry for NFSv4 delegation"
32626+ " for an internal unlink\n");
32627+ iput(delegated);
32628+ }
32629+ }
1facf9fc 32630+ if (unlikely(err))
523b37e3
AM
32631+ pr_warn("failed removing %pd (%d), ignored.\n",
32632+ whpath->dentry, err);
1facf9fc 32633+}
32634+
32635+static int test_linkable(struct dentry *h_root)
32636+{
5527c038 32637+ struct inode *h_dir = d_inode(h_root);
1facf9fc 32638+
32639+ if (h_dir->i_op->link)
32640+ return 0;
32641+
523b37e3
AM
32642+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32643+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 32644+ return -ENOSYS;
32645+}
32646+
32647+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32648+static int au_whdir(struct inode *h_dir, struct path *path)
32649+{
32650+ int err;
32651+
32652+ err = -EEXIST;
5527c038 32653+ if (d_is_negative(path->dentry)) {
1facf9fc 32654+ int mode = S_IRWXU;
32655+
32656+ if (au_test_nfs(path->dentry->d_sb))
32657+ mode |= S_IXUGO;
86dc4139 32658+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 32659+ } else if (d_is_dir(path->dentry))
1facf9fc 32660+ err = 0;
32661+ else
523b37e3 32662+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 32663+
32664+ return err;
32665+}
32666+
32667+struct au_wh_base {
32668+ const struct qstr *name;
32669+ struct dentry *dentry;
32670+};
32671+
32672+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32673+ struct path *h_path)
32674+{
32675+ h_path->dentry = base[AuBrWh_BASE].dentry;
32676+ au_wh_clean(h_dir, h_path, /*isdir*/0);
32677+ h_path->dentry = base[AuBrWh_PLINK].dentry;
32678+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32679+ h_path->dentry = base[AuBrWh_ORPH].dentry;
32680+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32681+}
32682+
32683+/*
32684+ * returns tri-state,
c1595e42 32685+ * minus: error, caller should print the message
1facf9fc 32686+ * zero: succuess
c1595e42 32687+ * plus: error, caller should NOT print the message
1facf9fc 32688+ */
32689+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32690+ int do_plink, struct au_wh_base base[],
32691+ struct path *h_path)
32692+{
32693+ int err;
32694+ struct inode *h_dir;
32695+
5527c038 32696+ h_dir = d_inode(h_root);
1facf9fc 32697+ h_path->dentry = base[AuBrWh_BASE].dentry;
32698+ au_wh_clean(h_dir, h_path, /*isdir*/0);
32699+ h_path->dentry = base[AuBrWh_PLINK].dentry;
32700+ if (do_plink) {
32701+ err = test_linkable(h_root);
32702+ if (unlikely(err)) {
32703+ err = 1;
32704+ goto out;
32705+ }
32706+
32707+ err = au_whdir(h_dir, h_path);
32708+ if (unlikely(err))
32709+ goto out;
32710+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32711+ } else
32712+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32713+ h_path->dentry = base[AuBrWh_ORPH].dentry;
32714+ err = au_whdir(h_dir, h_path);
32715+ if (unlikely(err))
32716+ goto out;
32717+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32718+
4f0767ce 32719+out:
1facf9fc 32720+ return err;
32721+}
32722+
32723+/*
32724+ * for the moment, aufs supports the branch filesystem which does not support
32725+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
32726+ * copyup failed. finally, such filesystem will not be used as the writable
32727+ * branch.
32728+ *
32729+ * returns tri-state, see above.
32730+ */
32731+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32732+ int do_plink, struct au_wh_base base[],
32733+ struct path *h_path)
32734+{
32735+ int err;
32736+ struct inode *h_dir;
32737+
1308ab2a 32738+ WbrWhMustWriteLock(wbr);
32739+
1facf9fc 32740+ err = test_linkable(h_root);
32741+ if (unlikely(err)) {
32742+ err = 1;
32743+ goto out;
32744+ }
32745+
32746+ /*
32747+ * todo: should this create be done in /sbin/mount.aufs helper?
32748+ */
32749+ err = -EEXIST;
5527c038
JR
32750+ h_dir = d_inode(h_root);
32751+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
32752+ h_path->dentry = base[AuBrWh_BASE].dentry;
32753+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 32754+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 32755+ err = 0;
32756+ else
523b37e3 32757+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 32758+ if (unlikely(err))
32759+ goto out;
32760+
32761+ h_path->dentry = base[AuBrWh_PLINK].dentry;
32762+ if (do_plink) {
32763+ err = au_whdir(h_dir, h_path);
32764+ if (unlikely(err))
32765+ goto out;
32766+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32767+ } else
32768+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32769+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
32770+
32771+ h_path->dentry = base[AuBrWh_ORPH].dentry;
32772+ err = au_whdir(h_dir, h_path);
32773+ if (unlikely(err))
32774+ goto out;
32775+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32776+
4f0767ce 32777+out:
1facf9fc 32778+ return err;
32779+}
32780+
32781+/*
32782+ * initialize the whiteout base file/dir for @br.
32783+ */
86dc4139 32784+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 32785+{
32786+ int err, i;
32787+ const unsigned char do_plink
32788+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 32789+ struct inode *h_dir;
86dc4139
AM
32790+ struct path path = br->br_path;
32791+ struct dentry *h_root = path.dentry;
1facf9fc 32792+ struct au_wbr *wbr = br->br_wbr;
32793+ static const struct qstr base_name[] = {
0c3ec466
AM
32794+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
32795+ sizeof(AUFS_BASE_NAME) - 1),
32796+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
32797+ sizeof(AUFS_PLINKDIR_NAME) - 1),
32798+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
32799+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 32800+ };
32801+ struct au_wh_base base[] = {
32802+ [AuBrWh_BASE] = {
32803+ .name = base_name + AuBrWh_BASE,
32804+ .dentry = NULL
32805+ },
32806+ [AuBrWh_PLINK] = {
32807+ .name = base_name + AuBrWh_PLINK,
32808+ .dentry = NULL
32809+ },
32810+ [AuBrWh_ORPH] = {
32811+ .name = base_name + AuBrWh_ORPH,
32812+ .dentry = NULL
32813+ }
32814+ };
32815+
1308ab2a 32816+ if (wbr)
32817+ WbrWhMustWriteLock(wbr);
1facf9fc 32818+
1facf9fc 32819+ for (i = 0; i < AuBrWh_Last; i++) {
32820+ /* doubly whiteouted */
32821+ struct dentry *d;
32822+
32823+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
32824+ err = PTR_ERR(d);
32825+ if (IS_ERR(d))
32826+ goto out;
32827+
32828+ base[i].dentry = d;
32829+ AuDebugOn(wbr
32830+ && wbr->wbr_wh[i]
32831+ && wbr->wbr_wh[i] != base[i].dentry);
32832+ }
32833+
32834+ if (wbr)
32835+ for (i = 0; i < AuBrWh_Last; i++) {
32836+ dput(wbr->wbr_wh[i]);
32837+ wbr->wbr_wh[i] = NULL;
32838+ }
32839+
32840+ err = 0;
1e00d052 32841+ if (!au_br_writable(br->br_perm)) {
5527c038 32842+ h_dir = d_inode(h_root);
1facf9fc 32843+ au_wh_init_ro(h_dir, base, &path);
1e00d052 32844+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 32845+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
32846+ if (err > 0)
32847+ goto out;
32848+ else if (err)
32849+ goto out_err;
1e00d052 32850+ } else {
1facf9fc 32851+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
32852+ if (err > 0)
32853+ goto out;
32854+ else if (err)
32855+ goto out_err;
1facf9fc 32856+ }
32857+ goto out; /* success */
32858+
4f0767ce 32859+out_err:
523b37e3
AM
32860+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
32861+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 32862+out:
1facf9fc 32863+ for (i = 0; i < AuBrWh_Last; i++)
32864+ dput(base[i].dentry);
32865+ return err;
32866+}
32867+
32868+/* ---------------------------------------------------------------------- */
32869+/*
32870+ * whiteouts are all hard-linked usually.
32871+ * when its link count reaches a ceiling, we create a new whiteout base
32872+ * asynchronously.
32873+ */
32874+
32875+struct reinit_br_wh {
32876+ struct super_block *sb;
32877+ struct au_branch *br;
32878+};
32879+
32880+static void reinit_br_wh(void *arg)
32881+{
32882+ int err;
32883+ aufs_bindex_t bindex;
32884+ struct path h_path;
32885+ struct reinit_br_wh *a = arg;
32886+ struct au_wbr *wbr;
523b37e3 32887+ struct inode *dir, *delegated;
1facf9fc 32888+ struct dentry *h_root;
32889+ struct au_hinode *hdir;
32890+
32891+ err = 0;
32892+ wbr = a->br->br_wbr;
32893+ /* big aufs lock */
32894+ si_noflush_write_lock(a->sb);
32895+ if (!au_br_writable(a->br->br_perm))
32896+ goto out;
32897+ bindex = au_br_index(a->sb, a->br->br_id);
32898+ if (unlikely(bindex < 0))
32899+ goto out;
32900+
1308ab2a 32901+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 32902+ dir = d_inode(a->sb->s_root);
1facf9fc 32903+ hdir = au_hi(dir, bindex);
32904+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 32905+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 32906+
5afbbe0d 32907+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 32908+ wbr_wh_write_lock(wbr);
32909+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
32910+ h_root, a->br);
32911+ if (!err) {
86dc4139
AM
32912+ h_path.dentry = wbr->wbr_whbase;
32913+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
32914+ delegated = NULL;
32915+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
32916+ /*force*/0);
32917+ if (unlikely(err == -EWOULDBLOCK)) {
32918+ pr_warn("cannot retry for NFSv4 delegation"
32919+ " for an internal unlink\n");
32920+ iput(delegated);
32921+ }
1facf9fc 32922+ } else {
523b37e3 32923+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 32924+ err = 0;
32925+ }
32926+ dput(wbr->wbr_whbase);
32927+ wbr->wbr_whbase = NULL;
32928+ if (!err)
86dc4139 32929+ err = au_wh_init(a->br, a->sb);
1facf9fc 32930+ wbr_wh_write_unlock(wbr);
5afbbe0d 32931+ au_hn_inode_unlock(hdir);
1308ab2a 32932+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
32933+ if (!err)
32934+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 32935+
4f0767ce 32936+out:
1facf9fc 32937+ if (wbr)
32938+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 32939+ au_br_put(a->br);
1facf9fc 32940+ si_write_unlock(a->sb);
027c5e7a 32941+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
f0c0a007 32942+ au_delayed_kfree(arg);
1facf9fc 32943+ if (unlikely(err))
32944+ AuIOErr("err %d\n", err);
32945+}
32946+
32947+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
32948+{
32949+ int do_dec, wkq_err;
32950+ struct reinit_br_wh *arg;
32951+
32952+ do_dec = 1;
32953+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
32954+ goto out;
32955+
32956+ /* ignore ENOMEM */
32957+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
32958+ if (arg) {
32959+ /*
32960+ * dec(wh_running), kfree(arg) and dec(br_count)
32961+ * in reinit function
32962+ */
32963+ arg->sb = sb;
32964+ arg->br = br;
5afbbe0d 32965+ au_br_get(br);
53392da6 32966+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 32967+ if (unlikely(wkq_err)) {
32968+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 32969+ au_br_put(br);
f0c0a007 32970+ au_delayed_kfree(arg);
1facf9fc 32971+ }
32972+ do_dec = 0;
32973+ }
32974+
4f0767ce 32975+out:
1facf9fc 32976+ if (do_dec)
32977+ atomic_dec(&br->br_wbr->wbr_wh_running);
32978+}
32979+
32980+/* ---------------------------------------------------------------------- */
32981+
32982+/*
32983+ * create the whiteout @wh.
32984+ */
32985+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
32986+ struct dentry *wh)
32987+{
32988+ int err;
32989+ struct path h_path = {
32990+ .dentry = wh
32991+ };
32992+ struct au_branch *br;
32993+ struct au_wbr *wbr;
32994+ struct dentry *h_parent;
523b37e3 32995+ struct inode *h_dir, *delegated;
1facf9fc 32996+
32997+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 32998+ h_dir = d_inode(h_parent);
1facf9fc 32999+ IMustLock(h_dir);
33000+
33001+ br = au_sbr(sb, bindex);
86dc4139 33002+ h_path.mnt = au_br_mnt(br);
1facf9fc 33003+ wbr = br->br_wbr;
33004+ wbr_wh_read_lock(wbr);
33005+ if (wbr->wbr_whbase) {
523b37e3
AM
33006+ delegated = NULL;
33007+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
33008+ if (unlikely(err == -EWOULDBLOCK)) {
33009+ pr_warn("cannot retry for NFSv4 delegation"
33010+ " for an internal link\n");
33011+ iput(delegated);
33012+ }
1facf9fc 33013+ if (!err || err != -EMLINK)
33014+ goto out;
33015+
33016+ /* link count full. re-initialize br_whbase. */
33017+ kick_reinit_br_wh(sb, br);
33018+ }
33019+
33020+ /* return this error in this context */
b4510431 33021+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
33022+ if (!err)
33023+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 33024+
4f0767ce 33025+out:
1facf9fc 33026+ wbr_wh_read_unlock(wbr);
33027+ return err;
33028+}
33029+
33030+/* ---------------------------------------------------------------------- */
33031+
33032+/*
33033+ * create or remove the diropq.
33034+ */
33035+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
33036+ unsigned int flags)
33037+{
33038+ struct dentry *opq_dentry, *h_dentry;
33039+ struct super_block *sb;
33040+ struct au_branch *br;
33041+ int err;
33042+
33043+ sb = dentry->d_sb;
33044+ br = au_sbr(sb, bindex);
33045+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 33046+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 33047+ if (IS_ERR(opq_dentry))
33048+ goto out;
33049+
33050+ if (au_ftest_diropq(flags, CREATE)) {
33051+ err = link_or_create_wh(sb, bindex, opq_dentry);
33052+ if (!err) {
33053+ au_set_dbdiropq(dentry, bindex);
33054+ goto out; /* success */
33055+ }
33056+ } else {
33057+ struct path tmp = {
33058+ .dentry = opq_dentry,
86dc4139 33059+ .mnt = au_br_mnt(br)
1facf9fc 33060+ };
5527c038 33061+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 33062+ if (!err)
33063+ au_set_dbdiropq(dentry, -1);
33064+ }
33065+ dput(opq_dentry);
33066+ opq_dentry = ERR_PTR(err);
33067+
4f0767ce 33068+out:
1facf9fc 33069+ return opq_dentry;
33070+}
33071+
33072+struct do_diropq_args {
33073+ struct dentry **errp;
33074+ struct dentry *dentry;
33075+ aufs_bindex_t bindex;
33076+ unsigned int flags;
33077+};
33078+
33079+static void call_do_diropq(void *args)
33080+{
33081+ struct do_diropq_args *a = args;
33082+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
33083+}
33084+
33085+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33086+ unsigned int flags)
33087+{
33088+ struct dentry *diropq, *h_dentry;
33089+
33090+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 33091+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 33092+ diropq = do_diropq(dentry, bindex, flags);
33093+ else {
33094+ int wkq_err;
33095+ struct do_diropq_args args = {
33096+ .errp = &diropq,
33097+ .dentry = dentry,
33098+ .bindex = bindex,
33099+ .flags = flags
33100+ };
33101+
33102+ wkq_err = au_wkq_wait(call_do_diropq, &args);
33103+ if (unlikely(wkq_err))
33104+ diropq = ERR_PTR(wkq_err);
33105+ }
33106+
33107+ return diropq;
33108+}
33109+
33110+/* ---------------------------------------------------------------------- */
33111+
33112+/*
33113+ * lookup whiteout dentry.
33114+ * @h_parent: lower parent dentry which must exist and be locked
33115+ * @base_name: name of dentry which will be whiteouted
33116+ * returns dentry for whiteout.
33117+ */
33118+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33119+ struct au_branch *br)
33120+{
33121+ int err;
33122+ struct qstr wh_name;
33123+ struct dentry *wh_dentry;
33124+
33125+ err = au_wh_name_alloc(&wh_name, base_name);
33126+ wh_dentry = ERR_PTR(err);
33127+ if (!err) {
b4510431 33128+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
f0c0a007 33129+ au_delayed_kfree(wh_name.name);
1facf9fc 33130+ }
33131+ return wh_dentry;
33132+}
33133+
33134+/*
33135+ * link/create a whiteout for @dentry on @bindex.
33136+ */
33137+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33138+ struct dentry *h_parent)
33139+{
33140+ struct dentry *wh_dentry;
33141+ struct super_block *sb;
33142+ int err;
33143+
33144+ sb = dentry->d_sb;
33145+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 33146+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 33147+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 33148+ if (!err) {
1facf9fc 33149+ au_set_dbwh(dentry, bindex);
076b876e
AM
33150+ au_fhsm_wrote(sb, bindex, /*force*/0);
33151+ } else {
1facf9fc 33152+ dput(wh_dentry);
33153+ wh_dentry = ERR_PTR(err);
33154+ }
33155+ }
33156+
33157+ return wh_dentry;
33158+}
33159+
33160+/* ---------------------------------------------------------------------- */
33161+
33162+/* Delete all whiteouts in this directory on branch bindex. */
33163+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
33164+ aufs_bindex_t bindex, struct au_branch *br)
33165+{
33166+ int err;
33167+ unsigned long ul, n;
33168+ struct qstr wh_name;
33169+ char *p;
33170+ struct hlist_head *head;
c06a8ce3 33171+ struct au_vdir_wh *pos;
1facf9fc 33172+ struct au_vdir_destr *str;
33173+
33174+ err = -ENOMEM;
537831f9 33175+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 33176+ wh_name.name = p;
33177+ if (unlikely(!wh_name.name))
33178+ goto out;
33179+
33180+ err = 0;
33181+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
33182+ p += AUFS_WH_PFX_LEN;
33183+ n = whlist->nh_num;
33184+ head = whlist->nh_head;
33185+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
33186+ hlist_for_each_entry(pos, head, wh_hash) {
33187+ if (pos->wh_bindex != bindex)
1facf9fc 33188+ continue;
33189+
c06a8ce3 33190+ str = &pos->wh_str;
1facf9fc 33191+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
33192+ memcpy(p, str->name, str->len);
33193+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
33194+ err = unlink_wh_name(h_dentry, &wh_name, br);
33195+ if (!err)
33196+ continue;
33197+ break;
33198+ }
33199+ AuIOErr("whiteout name too long %.*s\n",
33200+ str->len, str->name);
33201+ err = -EIO;
33202+ break;
33203+ }
33204+ }
f0c0a007 33205+ au_delayed_free_page((unsigned long)wh_name.name);
1facf9fc 33206+
4f0767ce 33207+out:
1facf9fc 33208+ return err;
33209+}
33210+
33211+struct del_wh_children_args {
33212+ int *errp;
33213+ struct dentry *h_dentry;
1308ab2a 33214+ struct au_nhash *whlist;
1facf9fc 33215+ aufs_bindex_t bindex;
33216+ struct au_branch *br;
33217+};
33218+
33219+static void call_del_wh_children(void *args)
33220+{
33221+ struct del_wh_children_args *a = args;
1308ab2a 33222+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 33223+}
33224+
33225+/* ---------------------------------------------------------------------- */
33226+
33227+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
33228+{
33229+ struct au_whtmp_rmdir *whtmp;
dece6358 33230+ int err;
1308ab2a 33231+ unsigned int rdhash;
dece6358
AM
33232+
33233+ SiMustAnyLock(sb);
1facf9fc 33234+
be52b249 33235+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
33236+ if (unlikely(!whtmp)) {
33237+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 33238+ goto out;
dece6358 33239+ }
1facf9fc 33240+
1308ab2a 33241+ /* no estimation for dir size */
33242+ rdhash = au_sbi(sb)->si_rdhash;
33243+ if (!rdhash)
33244+ rdhash = AUFS_RDHASH_DEF;
33245+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
33246+ if (unlikely(err)) {
f0c0a007 33247+ au_delayed_kfree(whtmp);
1308ab2a 33248+ whtmp = ERR_PTR(err);
33249+ }
dece6358 33250+
4f0767ce 33251+out:
dece6358 33252+ return whtmp;
1facf9fc 33253+}
33254+
33255+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
33256+{
027c5e7a 33257+ if (whtmp->br)
5afbbe0d 33258+ au_br_put(whtmp->br);
1facf9fc 33259+ dput(whtmp->wh_dentry);
33260+ iput(whtmp->dir);
dece6358 33261+ au_nhash_wh_free(&whtmp->whlist);
f0c0a007 33262+ au_delayed_kfree(whtmp);
1facf9fc 33263+}
33264+
33265+/*
33266+ * rmdir the whiteouted temporary named dir @h_dentry.
33267+ * @whlist: whiteouted children.
33268+ */
33269+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33270+ struct dentry *wh_dentry, struct au_nhash *whlist)
33271+{
33272+ int err;
2000de60 33273+ unsigned int h_nlink;
1facf9fc 33274+ struct path h_tmp;
33275+ struct inode *wh_inode, *h_dir;
33276+ struct au_branch *br;
33277+
5527c038 33278+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 33279+ IMustLock(h_dir);
33280+
33281+ br = au_sbr(dir->i_sb, bindex);
5527c038 33282+ wh_inode = d_inode(wh_dentry);
febd17d6 33283+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 33284+
33285+ /*
33286+ * someone else might change some whiteouts while we were sleeping.
33287+ * it means this whlist may have an obsoleted entry.
33288+ */
33289+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
33290+ err = del_wh_children(wh_dentry, whlist, bindex, br);
33291+ else {
33292+ int wkq_err;
33293+ struct del_wh_children_args args = {
33294+ .errp = &err,
33295+ .h_dentry = wh_dentry,
1308ab2a 33296+ .whlist = whlist,
1facf9fc 33297+ .bindex = bindex,
33298+ .br = br
33299+ };
33300+
33301+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
33302+ if (unlikely(wkq_err))
33303+ err = wkq_err;
33304+ }
febd17d6 33305+ inode_unlock(wh_inode);
1facf9fc 33306+
33307+ if (!err) {
33308+ h_tmp.dentry = wh_dentry;
86dc4139 33309+ h_tmp.mnt = au_br_mnt(br);
2000de60 33310+ h_nlink = h_dir->i_nlink;
1facf9fc 33311+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
33312+ /* some fs doesn't change the parent nlink in some cases */
33313+ h_nlink -= h_dir->i_nlink;
1facf9fc 33314+ }
33315+
33316+ if (!err) {
5afbbe0d 33317+ if (au_ibtop(dir) == bindex) {
7f207e10 33318+ /* todo: dir->i_mutex is necessary */
1facf9fc 33319+ au_cpup_attr_timesizes(dir);
2000de60
JR
33320+ if (h_nlink)
33321+ vfsub_drop_nlink(dir);
1facf9fc 33322+ }
33323+ return 0; /* success */
33324+ }
33325+
523b37e3 33326+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 33327+ return err;
33328+}
33329+
33330+static void call_rmdir_whtmp(void *args)
33331+{
33332+ int err;
e49829fe 33333+ aufs_bindex_t bindex;
1facf9fc 33334+ struct au_whtmp_rmdir *a = args;
33335+ struct super_block *sb;
33336+ struct dentry *h_parent;
33337+ struct inode *h_dir;
1facf9fc 33338+ struct au_hinode *hdir;
33339+
33340+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 33341+ /* inode_lock(a->dir); */
e49829fe 33342+ err = -EROFS;
1facf9fc 33343+ sb = a->dir->i_sb;
e49829fe
JR
33344+ si_read_lock(sb, !AuLock_FLUSH);
33345+ if (!au_br_writable(a->br->br_perm))
33346+ goto out;
33347+ bindex = au_br_index(sb, a->br->br_id);
33348+ if (unlikely(bindex < 0))
1facf9fc 33349+ goto out;
33350+
33351+ err = -EIO;
1facf9fc 33352+ ii_write_lock_parent(a->dir);
33353+ h_parent = dget_parent(a->wh_dentry);
5527c038 33354+ h_dir = d_inode(h_parent);
e49829fe 33355+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
33356+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
33357+ if (unlikely(err))
33358+ goto out_mnt;
5afbbe0d 33359+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
33360+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
33361+ a->br);
86dc4139
AM
33362+ if (!err)
33363+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 33364+ au_hn_inode_unlock(hdir);
86dc4139
AM
33365+ vfsub_mnt_drop_write(au_br_mnt(a->br));
33366+
33367+out_mnt:
1facf9fc 33368+ dput(h_parent);
33369+ ii_write_unlock(a->dir);
4f0767ce 33370+out:
febd17d6 33371+ /* inode_unlock(a->dir); */
1facf9fc 33372+ au_whtmp_rmdir_free(a);
027c5e7a
AM
33373+ si_read_unlock(sb);
33374+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 33375+ if (unlikely(err))
33376+ AuIOErr("err %d\n", err);
33377+}
33378+
33379+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33380+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
33381+{
33382+ int wkq_err;
e49829fe 33383+ struct super_block *sb;
1facf9fc 33384+
33385+ IMustLock(dir);
33386+
33387+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 33388+ sb = dir->i_sb;
1facf9fc 33389+ args->dir = au_igrab(dir);
e49829fe 33390+ args->br = au_sbr(sb, bindex);
5afbbe0d 33391+ au_br_get(args->br);
1facf9fc 33392+ args->wh_dentry = dget(wh_dentry);
53392da6 33393+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 33394+ if (unlikely(wkq_err)) {
523b37e3 33395+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 33396+ au_whtmp_rmdir_free(args);
33397+ }
33398+}
7f207e10
AM
33399diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
33400--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 33401+++ linux/fs/aufs/whout.h 2016-07-25 19:05:34.817826663 +0200
076b876e 33402@@ -0,0 +1,85 @@
1facf9fc 33403+/*
8cdd5066 33404+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33405+ *
33406+ * This program, aufs is free software; you can redistribute it and/or modify
33407+ * it under the terms of the GNU General Public License as published by
33408+ * the Free Software Foundation; either version 2 of the License, or
33409+ * (at your option) any later version.
dece6358
AM
33410+ *
33411+ * This program is distributed in the hope that it will be useful,
33412+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33413+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33414+ * GNU General Public License for more details.
33415+ *
33416+ * You should have received a copy of the GNU General Public License
523b37e3 33417+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33418+ */
33419+
33420+/*
33421+ * whiteout for logical deletion and opaque directory
33422+ */
33423+
33424+#ifndef __AUFS_WHOUT_H__
33425+#define __AUFS_WHOUT_H__
33426+
33427+#ifdef __KERNEL__
33428+
1facf9fc 33429+#include "dir.h"
33430+
33431+/* whout.c */
33432+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
33433+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
33434+int au_diropq_test(struct dentry *h_dentry);
7e9cd9fe 33435+struct au_branch;
1facf9fc 33436+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
33437+ struct qstr *prefix);
33438+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
33439+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
33440+ struct dentry *dentry);
86dc4139 33441+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 33442+
33443+/* diropq flags */
33444+#define AuDiropq_CREATE 1
33445+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
33446+#define au_fset_diropq(flags, name) \
33447+ do { (flags) |= AuDiropq_##name; } while (0)
33448+#define au_fclr_diropq(flags, name) \
33449+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 33450+
33451+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33452+ unsigned int flags);
33453+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33454+ struct au_branch *br);
33455+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33456+ struct dentry *h_parent);
33457+
33458+/* real rmdir for the whiteout-ed dir */
33459+struct au_whtmp_rmdir {
33460+ struct inode *dir;
e49829fe 33461+ struct au_branch *br;
1facf9fc 33462+ struct dentry *wh_dentry;
dece6358 33463+ struct au_nhash whlist;
1facf9fc 33464+};
33465+
33466+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
33467+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
33468+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33469+ struct dentry *wh_dentry, struct au_nhash *whlist);
33470+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33471+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
33472+
33473+/* ---------------------------------------------------------------------- */
33474+
33475+static inline struct dentry *au_diropq_create(struct dentry *dentry,
33476+ aufs_bindex_t bindex)
33477+{
33478+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
33479+}
33480+
33481+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
33482+{
33483+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
33484+}
33485+
33486+#endif /* __KERNEL__ */
33487+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
33488diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
33489--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
33490+++ linux/fs/aufs/wkq.c 2016-08-17 18:01:06.161889951 +0200
33491@@ -0,0 +1,213 @@
1facf9fc 33492+/*
8cdd5066 33493+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33494+ *
33495+ * This program, aufs is free software; you can redistribute it and/or modify
33496+ * it under the terms of the GNU General Public License as published by
33497+ * the Free Software Foundation; either version 2 of the License, or
33498+ * (at your option) any later version.
dece6358
AM
33499+ *
33500+ * This program is distributed in the hope that it will be useful,
33501+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33502+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33503+ * GNU General Public License for more details.
33504+ *
33505+ * You should have received a copy of the GNU General Public License
523b37e3 33506+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33507+ */
33508+
33509+/*
33510+ * workqueue for asynchronous/super-io operations
33511+ * todo: try new dredential scheme
33512+ */
33513+
dece6358 33514+#include <linux/module.h>
1facf9fc 33515+#include "aufs.h"
33516+
9dbd164d 33517+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 33518+
9dbd164d 33519+static struct workqueue_struct *au_wkq;
1facf9fc 33520+
33521+struct au_wkinfo {
33522+ struct work_struct wk;
7f207e10 33523+ struct kobject *kobj;
1facf9fc 33524+
33525+ unsigned int flags; /* see wkq.h */
33526+
33527+ au_wkq_func_t func;
33528+ void *args;
33529+
1facf9fc 33530+ struct completion *comp;
33531+};
33532+
33533+/* ---------------------------------------------------------------------- */
33534+
1facf9fc 33535+static void wkq_func(struct work_struct *wk)
33536+{
33537+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
33538+
2dfbb274 33539+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
33540+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
33541+
1facf9fc 33542+ wkinfo->func(wkinfo->args);
1facf9fc 33543+ if (au_ftest_wkq(wkinfo->flags, WAIT))
33544+ complete(wkinfo->comp);
33545+ else {
7f207e10 33546+ kobject_put(wkinfo->kobj);
9dbd164d 33547+ module_put(THIS_MODULE); /* todo: ?? */
f0c0a007 33548+ au_delayed_kfree(wkinfo);
1facf9fc 33549+ }
33550+}
33551+
33552+/*
33553+ * Since struct completion is large, try allocating it dynamically.
33554+ */
c2b27bf2 33555+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 33556+#define AuWkqCompDeclare(name) struct completion *comp = NULL
33557+
33558+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33559+{
33560+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33561+ if (*comp) {
33562+ init_completion(*comp);
33563+ wkinfo->comp = *comp;
33564+ return 0;
33565+ }
33566+ return -ENOMEM;
33567+}
33568+
33569+static void au_wkq_comp_free(struct completion *comp)
33570+{
f0c0a007 33571+ au_delayed_kfree(comp);
1facf9fc 33572+}
33573+
33574+#else
33575+
33576+/* no braces */
33577+#define AuWkqCompDeclare(name) \
33578+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
33579+ struct completion *comp = &_ ## name
33580+
33581+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33582+{
33583+ wkinfo->comp = *comp;
33584+ return 0;
33585+}
33586+
33587+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33588+{
33589+ /* empty */
33590+}
33591+#endif /* 4KSTACKS */
33592+
53392da6 33593+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 33594+{
53392da6
AM
33595+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
33596+ if (au_wkq_test()) {
38d290e6
JR
33597+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33598+ " due to a dead dir by UDBA?\n");
53392da6
AM
33599+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33600+ }
33601+ } else
33602+ au_dbg_verify_kthread();
33603+
33604+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 33605+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 33606+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
33607+ } else {
33608+ INIT_WORK(&wkinfo->wk, wkq_func);
33609+ schedule_work(&wkinfo->wk);
33610+ }
1facf9fc 33611+}
33612+
7f207e10
AM
33613+/*
33614+ * Be careful. It is easy to make deadlock happen.
33615+ * processA: lock, wkq and wait
33616+ * processB: wkq and wait, lock in wkq
33617+ * --> deadlock
33618+ */
b752ccd1 33619+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 33620+{
33621+ int err;
33622+ AuWkqCompDeclare(comp);
33623+ struct au_wkinfo wkinfo = {
b752ccd1 33624+ .flags = flags,
1facf9fc 33625+ .func = func,
33626+ .args = args
33627+ };
33628+
33629+ err = au_wkq_comp_alloc(&wkinfo, &comp);
33630+ if (!err) {
53392da6 33631+ au_wkq_run(&wkinfo);
1facf9fc 33632+ /* no timeout, no interrupt */
33633+ wait_for_completion(wkinfo.comp);
33634+ au_wkq_comp_free(comp);
4a4d8108 33635+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 33636+ }
33637+
33638+ return err;
33639+
33640+}
33641+
027c5e7a
AM
33642+/*
33643+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33644+ * problem in a concurrent umounting.
33645+ */
53392da6
AM
33646+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33647+ unsigned int flags)
1facf9fc 33648+{
33649+ int err;
33650+ struct au_wkinfo *wkinfo;
33651+
f0c0a007 33652+ atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 33653+
33654+ /*
33655+ * wkq_func() must free this wkinfo.
33656+ * it highly depends upon the implementation of workqueue.
33657+ */
33658+ err = 0;
33659+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33660+ if (wkinfo) {
7f207e10 33661+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 33662+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 33663+ wkinfo->func = func;
33664+ wkinfo->args = args;
33665+ wkinfo->comp = NULL;
7f207e10 33666+ kobject_get(wkinfo->kobj);
9dbd164d 33667+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 33668+
53392da6 33669+ au_wkq_run(wkinfo);
1facf9fc 33670+ } else {
33671+ err = -ENOMEM;
e49829fe 33672+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 33673+ }
33674+
33675+ return err;
33676+}
33677+
33678+/* ---------------------------------------------------------------------- */
33679+
33680+void au_nwt_init(struct au_nowait_tasks *nwt)
33681+{
f0c0a007
AM
33682+ atomic_set(&nwt->nw_len, 0);
33683+ /* smp_mb(); */ /* atomic_set */
1facf9fc 33684+ init_waitqueue_head(&nwt->nw_wq);
33685+}
33686+
33687+void au_wkq_fin(void)
33688+{
9dbd164d 33689+ destroy_workqueue(au_wkq);
1facf9fc 33690+}
33691+
33692+int __init au_wkq_init(void)
33693+{
9dbd164d 33694+ int err;
b752ccd1
AM
33695+
33696+ err = 0;
86dc4139 33697+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
33698+ if (IS_ERR(au_wkq))
33699+ err = PTR_ERR(au_wkq);
33700+ else if (!au_wkq)
33701+ err = -ENOMEM;
b752ccd1
AM
33702+
33703+ return err;
1facf9fc 33704+}
7f207e10
AM
33705diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33706--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
33707+++ linux/fs/aufs/wkq.h 2016-08-17 18:01:06.161889951 +0200
33708@@ -0,0 +1,93 @@
1facf9fc 33709+/*
8cdd5066 33710+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33711+ *
33712+ * This program, aufs is free software; you can redistribute it and/or modify
33713+ * it under the terms of the GNU General Public License as published by
33714+ * the Free Software Foundation; either version 2 of the License, or
33715+ * (at your option) any later version.
dece6358
AM
33716+ *
33717+ * This program is distributed in the hope that it will be useful,
33718+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33719+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33720+ * GNU General Public License for more details.
33721+ *
33722+ * You should have received a copy of the GNU General Public License
523b37e3 33723+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33724+ */
33725+
33726+/*
33727+ * workqueue for asynchronous/super-io operations
33728+ * todo: try new credentials management scheme
33729+ */
33730+
33731+#ifndef __AUFS_WKQ_H__
33732+#define __AUFS_WKQ_H__
33733+
33734+#ifdef __KERNEL__
33735+
5afbbe0d
AM
33736+#include <linux/percpu_counter.h>
33737+
dece6358
AM
33738+struct super_block;
33739+
1facf9fc 33740+/* ---------------------------------------------------------------------- */
33741+
33742+/*
33743+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
33744+ */
33745+struct au_nowait_tasks {
f0c0a007 33746+ atomic_t nw_len;
1facf9fc 33747+ wait_queue_head_t nw_wq;
33748+};
33749+
33750+/* ---------------------------------------------------------------------- */
33751+
33752+typedef void (*au_wkq_func_t)(void *args);
33753+
33754+/* wkq flags */
33755+#define AuWkq_WAIT 1
9dbd164d 33756+#define AuWkq_NEST (1 << 1)
1facf9fc 33757+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
33758+#define au_fset_wkq(flags, name) \
33759+ do { (flags) |= AuWkq_##name; } while (0)
33760+#define au_fclr_wkq(flags, name) \
33761+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 33762+
9dbd164d
AM
33763+#ifndef CONFIG_AUFS_HNOTIFY
33764+#undef AuWkq_NEST
33765+#define AuWkq_NEST 0
33766+#endif
33767+
1facf9fc 33768+/* wkq.c */
b752ccd1 33769+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
33770+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33771+ unsigned int flags);
1facf9fc 33772+void au_nwt_init(struct au_nowait_tasks *nwt);
33773+int __init au_wkq_init(void);
33774+void au_wkq_fin(void);
33775+
33776+/* ---------------------------------------------------------------------- */
33777+
53392da6
AM
33778+static inline int au_wkq_test(void)
33779+{
33780+ return current->flags & PF_WQ_WORKER;
33781+}
33782+
b752ccd1 33783+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 33784+{
b752ccd1 33785+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 33786+}
33787+
33788+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
33789+{
f0c0a007 33790+ if (atomic_dec_and_test(&nwt->nw_len))
1facf9fc 33791+ wake_up_all(&nwt->nw_wq);
33792+}
33793+
33794+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
33795+{
f0c0a007 33796+ wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
1facf9fc 33797+ return 0;
33798+}
33799+
33800+#endif /* __KERNEL__ */
33801+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
33802diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
33803--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
33804+++ linux/fs/aufs/xattr.c 2016-08-17 18:01:06.165223371 +0200
33805@@ -0,0 +1,347 @@
c1595e42 33806+/*
8cdd5066 33807+ * Copyright (C) 2014-2016 Junjiro R. Okajima
c1595e42
JR
33808+ *
33809+ * This program, aufs is free software; you can redistribute it and/or modify
33810+ * it under the terms of the GNU General Public License as published by
33811+ * the Free Software Foundation; either version 2 of the License, or
33812+ * (at your option) any later version.
33813+ *
33814+ * This program is distributed in the hope that it will be useful,
33815+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33816+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33817+ * GNU General Public License for more details.
33818+ *
33819+ * You should have received a copy of the GNU General Public License
33820+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33821+ */
33822+
33823+/*
33824+ * handling xattr functions
33825+ */
33826+
33827+#include <linux/xattr.h>
33828+#include "aufs.h"
33829+
33830+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
33831+{
33832+ if (!ignore_flags)
33833+ goto out;
33834+ switch (err) {
33835+ case -ENOMEM:
33836+ case -EDQUOT:
33837+ goto out;
33838+ }
33839+
33840+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
33841+ err = 0;
33842+ goto out;
33843+ }
33844+
33845+#define cmp(brattr, prefix) do { \
33846+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
33847+ XATTR_##prefix##_PREFIX_LEN)) { \
33848+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
33849+ err = 0; \
33850+ goto out; \
33851+ } \
33852+ } while (0)
33853+
33854+ cmp(SEC, SECURITY);
33855+ cmp(SYS, SYSTEM);
33856+ cmp(TR, TRUSTED);
33857+ cmp(USR, USER);
33858+#undef cmp
33859+
33860+ if (ignore_flags & AuBrAttr_ICEX_OTH)
33861+ err = 0;
33862+
33863+out:
33864+ return err;
33865+}
33866+
33867+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
33868+
33869+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
33870+ char *name, char **buf, unsigned int ignore_flags,
33871+ unsigned int verbose)
c1595e42
JR
33872+{
33873+ int err;
33874+ ssize_t ssz;
33875+ struct inode *h_idst;
33876+
33877+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
33878+ err = ssz;
33879+ if (unlikely(err <= 0)) {
c1595e42
JR
33880+ if (err == -ENODATA
33881+ || (err == -EOPNOTSUPP
b912730e 33882+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 33883+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
33884+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
33885+ || !strcmp(name,
33886+ XATTR_NAME_POSIX_ACL_DEFAULT))))
33887+ ))
c1595e42 33888+ err = 0;
b912730e
AM
33889+ if (err && (verbose || au_debug_test()))
33890+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
33891+ goto out;
33892+ }
33893+
33894+ /* unlock it temporary */
5527c038 33895+ h_idst = d_inode(h_dst);
febd17d6 33896+ inode_unlock(h_idst);
c1595e42 33897+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 33898+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 33899+ if (unlikely(err)) {
7e9cd9fe
AM
33900+ if (verbose || au_debug_test())
33901+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
33902+ err = au_xattr_ignore(err, name, ignore_flags);
33903+ }
33904+
33905+out:
33906+ return err;
33907+}
33908+
7e9cd9fe
AM
33909+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
33910+ unsigned int verbose)
c1595e42
JR
33911+{
33912+ int err, unlocked, acl_access, acl_default;
33913+ ssize_t ssz;
33914+ struct inode *h_isrc, *h_idst;
33915+ char *value, *p, *o, *e;
33916+
33917+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 33918+ /* there should not be the parent-child relationship between them */
5527c038
JR
33919+ h_isrc = d_inode(h_src);
33920+ h_idst = d_inode(h_dst);
febd17d6
JR
33921+ inode_unlock(h_idst);
33922+ inode_lock_nested(h_isrc, AuLsc_I_CHILD);
33923+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
33924+ unlocked = 0;
33925+
33926+ /* some filesystems don't list POSIX ACL, for example tmpfs */
33927+ ssz = vfs_listxattr(h_src, NULL, 0);
33928+ err = ssz;
33929+ if (unlikely(err < 0)) {
33930+ AuTraceErr(err);
33931+ if (err == -ENODATA
33932+ || err == -EOPNOTSUPP)
33933+ err = 0; /* ignore */
33934+ goto out;
33935+ }
33936+
33937+ err = 0;
33938+ p = NULL;
33939+ o = NULL;
33940+ if (ssz) {
33941+ err = -ENOMEM;
33942+ p = kmalloc(ssz, GFP_NOFS);
33943+ o = p;
33944+ if (unlikely(!p))
33945+ goto out;
33946+ err = vfs_listxattr(h_src, p, ssz);
33947+ }
febd17d6 33948+ inode_unlock(h_isrc);
c1595e42
JR
33949+ unlocked = 1;
33950+ AuDbg("err %d, ssz %zd\n", err, ssz);
33951+ if (unlikely(err < 0))
33952+ goto out_free;
33953+
33954+ err = 0;
33955+ e = p + ssz;
33956+ value = NULL;
33957+ acl_access = 0;
33958+ acl_default = 0;
33959+ while (!err && p < e) {
33960+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
33961+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
33962+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
33963+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
33964+ - 1);
7e9cd9fe
AM
33965+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
33966+ verbose);
c1595e42
JR
33967+ p += strlen(p) + 1;
33968+ }
33969+ AuTraceErr(err);
33970+ ignore_flags |= au_xattr_out_of_list;
33971+ if (!err && !acl_access) {
33972+ err = au_do_cpup_xattr(h_dst, h_src,
33973+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 33974+ ignore_flags, verbose);
c1595e42
JR
33975+ AuTraceErr(err);
33976+ }
33977+ if (!err && !acl_default) {
33978+ err = au_do_cpup_xattr(h_dst, h_src,
33979+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 33980+ ignore_flags, verbose);
c1595e42
JR
33981+ AuTraceErr(err);
33982+ }
33983+
f0c0a007
AM
33984+ if (value)
33985+ au_delayed_kfree(value);
c1595e42
JR
33986+
33987+out_free:
f0c0a007
AM
33988+ if (o)
33989+ au_delayed_kfree(o);
c1595e42
JR
33990+out:
33991+ if (!unlocked)
febd17d6 33992+ inode_unlock(h_isrc);
c1595e42
JR
33993+ AuTraceErr(err);
33994+ return err;
33995+}
33996+
33997+/* ---------------------------------------------------------------------- */
33998+
33999+enum {
34000+ AU_XATTR_LIST,
34001+ AU_XATTR_GET
34002+};
34003+
34004+struct au_lgxattr {
34005+ int type;
34006+ union {
34007+ struct {
34008+ char *list;
34009+ size_t size;
34010+ } list;
34011+ struct {
34012+ const char *name;
34013+ void *value;
34014+ size_t size;
34015+ } get;
34016+ } u;
34017+};
34018+
34019+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
34020+{
34021+ ssize_t err;
34022+ struct path h_path;
34023+ struct super_block *sb;
34024+
34025+ sb = dentry->d_sb;
34026+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
34027+ if (unlikely(err))
34028+ goto out;
34029+ err = au_h_path_getattr(dentry, /*force*/1, &h_path);
34030+ if (unlikely(err))
34031+ goto out_si;
34032+ if (unlikely(!h_path.dentry))
34033+ /* illegally overlapped or something */
34034+ goto out_di; /* pretending success */
34035+
34036+ /* always topmost entry only */
34037+ switch (arg->type) {
34038+ case AU_XATTR_LIST:
34039+ err = vfs_listxattr(h_path.dentry,
34040+ arg->u.list.list, arg->u.list.size);
34041+ break;
34042+ case AU_XATTR_GET:
5afbbe0d 34043+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
34044+ err = vfs_getxattr(h_path.dentry,
34045+ arg->u.get.name, arg->u.get.value,
34046+ arg->u.get.size);
34047+ break;
34048+ }
34049+
34050+out_di:
34051+ di_read_unlock(dentry, AuLock_IR);
34052+out_si:
34053+ si_read_unlock(sb);
34054+out:
34055+ AuTraceErr(err);
34056+ return err;
34057+}
34058+
34059+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
34060+{
34061+ struct au_lgxattr arg = {
34062+ .type = AU_XATTR_LIST,
34063+ .u.list = {
34064+ .list = list,
34065+ .size = size
34066+ },
34067+ };
34068+
34069+ return au_lgxattr(dentry, &arg);
34070+}
34071+
5afbbe0d
AM
34072+ssize_t aufs_getxattr(struct dentry *dentry, struct inode *inode __maybe_unused,
34073+ const char *name, void *value, size_t size)
c1595e42
JR
34074+{
34075+ struct au_lgxattr arg = {
34076+ .type = AU_XATTR_GET,
34077+ .u.get = {
34078+ .name = name,
34079+ .value = value,
34080+ .size = size
34081+ },
34082+ };
34083+
34084+ return au_lgxattr(dentry, &arg);
34085+}
34086+
5afbbe0d
AM
34087+int aufs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
34088+ const void *value, size_t size, int flags)
c1595e42
JR
34089+{
34090+ struct au_srxattr arg = {
34091+ .type = AU_XATTR_SET,
34092+ .u.set = {
34093+ .name = name,
34094+ .value = value,
34095+ .size = size,
34096+ .flags = flags
34097+ },
34098+ };
34099+
5afbbe0d 34100+ return au_srxattr(dentry, inode, &arg);
c1595e42
JR
34101+}
34102+
34103+int aufs_removexattr(struct dentry *dentry, const char *name)
34104+{
34105+ struct au_srxattr arg = {
34106+ .type = AU_XATTR_REMOVE,
34107+ .u.remove = {
34108+ .name = name
34109+ },
34110+ };
34111+
5afbbe0d 34112+ return au_srxattr(dentry, d_inode(dentry), &arg);
c1595e42
JR
34113+}
34114+
34115+/* ---------------------------------------------------------------------- */
34116+
34117+#if 0
34118+static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
34119+ const char *name, size_t name_len, int type)
34120+{
34121+ return aufs_listxattr(dentry, list, list_size);
34122+}
34123+
34124+static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
34125+ size_t size, int type)
34126+{
34127+ return aufs_getxattr(dentry, name, buffer, size);
34128+}
34129+
34130+static int au_xattr_set(struct dentry *dentry, const char *name,
34131+ const void *value, size_t size, int flags, int type)
34132+{
34133+ return aufs_setxattr(dentry, name, value, size, flags);
34134+}
34135+
34136+static const struct xattr_handler au_xattr_handler = {
34137+ /* no prefix, no flags */
34138+ .list = au_xattr_list,
34139+ .get = au_xattr_get,
34140+ .set = au_xattr_set
34141+ /* why no remove? */
34142+};
34143+
34144+static const struct xattr_handler *au_xattr_handlers[] = {
34145+ &au_xattr_handler
34146+};
34147+
34148+void au_xattr_init(struct super_block *sb)
34149+{
34150+ /* sb->s_xattr = au_xattr_handlers; */
34151+}
34152+#endif
7f207e10
AM
34153diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
34154--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
f0c0a007
AM
34155+++ linux/fs/aufs/xino.c 2016-08-17 18:01:06.165223371 +0200
34156@@ -0,0 +1,1318 @@
1facf9fc 34157+/*
8cdd5066 34158+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 34159+ *
34160+ * This program, aufs is free software; you can redistribute it and/or modify
34161+ * it under the terms of the GNU General Public License as published by
34162+ * the Free Software Foundation; either version 2 of the License, or
34163+ * (at your option) any later version.
dece6358
AM
34164+ *
34165+ * This program is distributed in the hope that it will be useful,
34166+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
34167+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34168+ * GNU General Public License for more details.
34169+ *
34170+ * You should have received a copy of the GNU General Public License
523b37e3 34171+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 34172+ */
34173+
34174+/*
34175+ * external inode number translation table and bitmap
34176+ */
34177+
34178+#include <linux/seq_file.h>
392086de 34179+#include <linux/statfs.h>
1facf9fc 34180+#include "aufs.h"
34181+
9dbd164d 34182+/* todo: unnecessary to support mmap_sem since kernel-space? */
5527c038 34183+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
1facf9fc 34184+ loff_t *pos)
34185+{
34186+ ssize_t err;
34187+ mm_segment_t oldfs;
b752ccd1
AM
34188+ union {
34189+ void *k;
34190+ char __user *u;
34191+ } buf;
1facf9fc 34192+
b752ccd1 34193+ buf.k = kbuf;
1facf9fc 34194+ oldfs = get_fs();
34195+ set_fs(KERNEL_DS);
34196+ do {
34197+ /* todo: signal_pending? */
b752ccd1 34198+ err = func(file, buf.u, size, pos);
1facf9fc 34199+ } while (err == -EAGAIN || err == -EINTR);
34200+ set_fs(oldfs);
34201+
34202+#if 0 /* reserved for future use */
34203+ if (err > 0)
2000de60 34204+ fsnotify_access(file->f_path.dentry);
1facf9fc 34205+#endif
34206+
34207+ return err;
34208+}
34209+
34210+/* ---------------------------------------------------------------------- */
34211+
be52b249
AM
34212+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34213+ size_t size, loff_t *pos);
34214+
5527c038 34215+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
1facf9fc 34216+ size_t size, loff_t *pos)
34217+{
34218+ ssize_t err;
34219+ mm_segment_t oldfs;
b752ccd1
AM
34220+ union {
34221+ void *k;
34222+ const char __user *u;
34223+ } buf;
be52b249
AM
34224+ int i;
34225+ const int prevent_endless = 10;
1facf9fc 34226+
be52b249 34227+ i = 0;
b752ccd1 34228+ buf.k = kbuf;
1facf9fc 34229+ oldfs = get_fs();
34230+ set_fs(KERNEL_DS);
1facf9fc 34231+ do {
b752ccd1 34232+ err = func(file, buf.u, size, pos);
be52b249
AM
34233+ if (err == -EINTR
34234+ && !au_wkq_test()
34235+ && fatal_signal_pending(current)) {
34236+ set_fs(oldfs);
34237+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
34238+ BUG_ON(err == -EINTR);
34239+ oldfs = get_fs();
34240+ set_fs(KERNEL_DS);
34241+ }
34242+ } while (i++ < prevent_endless
34243+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 34244+ set_fs(oldfs);
34245+
34246+#if 0 /* reserved for future use */
34247+ if (err > 0)
2000de60 34248+ fsnotify_modify(file->f_path.dentry);
1facf9fc 34249+#endif
34250+
34251+ return err;
34252+}
34253+
34254+struct do_xino_fwrite_args {
34255+ ssize_t *errp;
5527c038 34256+ vfs_writef_t func;
1facf9fc 34257+ struct file *file;
34258+ void *buf;
34259+ size_t size;
34260+ loff_t *pos;
34261+};
34262+
34263+static void call_do_xino_fwrite(void *args)
34264+{
34265+ struct do_xino_fwrite_args *a = args;
34266+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
34267+}
34268+
be52b249
AM
34269+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34270+ size_t size, loff_t *pos)
34271+{
34272+ ssize_t err;
34273+ int wkq_err;
34274+ struct do_xino_fwrite_args args = {
34275+ .errp = &err,
34276+ .func = func,
34277+ .file = file,
34278+ .buf = buf,
34279+ .size = size,
34280+ .pos = pos
34281+ };
34282+
34283+ /*
34284+ * it breaks RLIMIT_FSIZE and normal user's limit,
34285+ * users should care about quota and real 'filesystem full.'
34286+ */
34287+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
34288+ if (unlikely(wkq_err))
34289+ err = wkq_err;
34290+
34291+ return err;
34292+}
34293+
5527c038
JR
34294+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
34295+ size_t size, loff_t *pos)
1facf9fc 34296+{
34297+ ssize_t err;
34298+
b752ccd1
AM
34299+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
34300+ lockdep_off();
34301+ err = do_xino_fwrite(func, file, buf, size, pos);
34302+ lockdep_on();
be52b249
AM
34303+ } else
34304+ err = xino_fwrite_wkq(func, file, buf, size, pos);
1facf9fc 34305+
34306+ return err;
34307+}
34308+
34309+/* ---------------------------------------------------------------------- */
34310+
34311+/*
34312+ * create a new xinofile at the same place/path as @base_file.
34313+ */
34314+struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
34315+{
34316+ struct file *file;
4a4d8108 34317+ struct dentry *base, *parent;
523b37e3 34318+ struct inode *dir, *delegated;
1facf9fc 34319+ struct qstr *name;
1308ab2a 34320+ struct path path;
4a4d8108 34321+ int err;
1facf9fc 34322+
2000de60 34323+ base = base_file->f_path.dentry;
1facf9fc 34324+ parent = base->d_parent; /* dir inode is locked */
5527c038 34325+ dir = d_inode(parent);
1facf9fc 34326+ IMustLock(dir);
34327+
34328+ file = ERR_PTR(-EINVAL);
34329+ name = &base->d_name;
4a4d8108
AM
34330+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
34331+ if (IS_ERR(path.dentry)) {
34332+ file = (void *)path.dentry;
523b37e3
AM
34333+ pr_err("%pd lookup err %ld\n",
34334+ base, PTR_ERR(path.dentry));
1facf9fc 34335+ goto out;
34336+ }
34337+
34338+ /* no need to mnt_want_write() since we call dentry_open() later */
4a4d8108 34339+ err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
1facf9fc 34340+ if (unlikely(err)) {
34341+ file = ERR_PTR(err);
523b37e3 34342+ pr_err("%pd create err %d\n", base, err);
1facf9fc 34343+ goto out_dput;
34344+ }
34345+
c06a8ce3 34346+ path.mnt = base_file->f_path.mnt;
4a4d8108 34347+ file = vfsub_dentry_open(&path,
7f207e10 34348+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 34349+ /* | __FMODE_NONOTIFY */);
1facf9fc 34350+ if (IS_ERR(file)) {
523b37e3 34351+ pr_err("%pd open err %ld\n", base, PTR_ERR(file));
1facf9fc 34352+ goto out_dput;
34353+ }
34354+
523b37e3
AM
34355+ delegated = NULL;
34356+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
34357+ if (unlikely(err == -EWOULDBLOCK)) {
34358+ pr_warn("cannot retry for NFSv4 delegation"
34359+ " for an internal unlink\n");
34360+ iput(delegated);
34361+ }
1facf9fc 34362+ if (unlikely(err)) {
523b37e3 34363+ pr_err("%pd unlink err %d\n", base, err);
1facf9fc 34364+ goto out_fput;
34365+ }
34366+
34367+ if (copy_src) {
34368+ /* no one can touch copy_src xino */
c06a8ce3 34369+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
1facf9fc 34370+ if (unlikely(err)) {
523b37e3 34371+ pr_err("%pd copy err %d\n", base, err);
1facf9fc 34372+ goto out_fput;
34373+ }
34374+ }
34375+ goto out_dput; /* success */
34376+
4f0767ce 34377+out_fput:
1facf9fc 34378+ fput(file);
34379+ file = ERR_PTR(err);
4f0767ce 34380+out_dput:
4a4d8108 34381+ dput(path.dentry);
4f0767ce 34382+out:
1facf9fc 34383+ return file;
34384+}
34385+
34386+struct au_xino_lock_dir {
34387+ struct au_hinode *hdir;
34388+ struct dentry *parent;
febd17d6 34389+ struct inode *dir;
1facf9fc 34390+};
34391+
34392+static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
34393+ struct au_xino_lock_dir *ldir)
34394+{
34395+ aufs_bindex_t brid, bindex;
34396+
34397+ ldir->hdir = NULL;
34398+ bindex = -1;
34399+ brid = au_xino_brid(sb);
34400+ if (brid >= 0)
34401+ bindex = au_br_index(sb, brid);
34402+ if (bindex >= 0) {
5527c038 34403+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 34404+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 34405+ } else {
2000de60 34406+ ldir->parent = dget_parent(xino->f_path.dentry);
febd17d6
JR
34407+ ldir->dir = d_inode(ldir->parent);
34408+ inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
1facf9fc 34409+ }
34410+}
34411+
34412+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
34413+{
34414+ if (ldir->hdir)
5afbbe0d 34415+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 34416+ else {
febd17d6 34417+ inode_unlock(ldir->dir);
1facf9fc 34418+ dput(ldir->parent);
34419+ }
34420+}
34421+
34422+/* ---------------------------------------------------------------------- */
34423+
34424+/* trucate xino files asynchronously */
34425+
34426+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
34427+{
34428+ int err;
392086de
AM
34429+ unsigned long jiffy;
34430+ blkcnt_t blocks;
5afbbe0d 34431+ aufs_bindex_t bi, bbot;
392086de 34432+ struct kstatfs *st;
1facf9fc 34433+ struct au_branch *br;
34434+ struct file *new_xino, *file;
34435+ struct super_block *h_sb;
34436+ struct au_xino_lock_dir ldir;
34437+
392086de 34438+ err = -ENOMEM;
be52b249 34439+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
34440+ if (unlikely(!st))
34441+ goto out;
34442+
1facf9fc 34443+ err = -EINVAL;
5afbbe0d
AM
34444+ bbot = au_sbbot(sb);
34445+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 34446+ goto out_st;
1facf9fc 34447+ br = au_sbr(sb, bindex);
34448+ file = br->br_xino.xi_file;
34449+ if (!file)
392086de
AM
34450+ goto out_st;
34451+
34452+ err = vfs_statfs(&file->f_path, st);
34453+ if (unlikely(err))
34454+ AuErr1("statfs err %d, ignored\n", err);
34455+ jiffy = jiffies;
34456+ blocks = file_inode(file)->i_blocks;
34457+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34458+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 34459+
34460+ au_xino_lock_dir(sb, file, &ldir);
34461+ /* mnt_want_write() is unnecessary here */
34462+ new_xino = au_xino_create2(file, file);
34463+ au_xino_unlock_dir(&ldir);
34464+ err = PTR_ERR(new_xino);
392086de
AM
34465+ if (IS_ERR(new_xino)) {
34466+ pr_err("err %d, ignored\n", err);
34467+ goto out_st;
34468+ }
1facf9fc 34469+ err = 0;
34470+ fput(file);
34471+ br->br_xino.xi_file = new_xino;
34472+
86dc4139 34473+ h_sb = au_br_sb(br);
5afbbe0d 34474+ for (bi = 0; bi <= bbot; bi++) {
1facf9fc 34475+ if (unlikely(bi == bindex))
34476+ continue;
34477+ br = au_sbr(sb, bi);
86dc4139 34478+ if (au_br_sb(br) != h_sb)
1facf9fc 34479+ continue;
34480+
34481+ fput(br->br_xino.xi_file);
34482+ br->br_xino.xi_file = new_xino;
34483+ get_file(new_xino);
34484+ }
34485+
392086de
AM
34486+ err = vfs_statfs(&new_xino->f_path, st);
34487+ if (!err) {
34488+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34489+ bindex, (u64)file_inode(new_xino)->i_blocks,
34490+ st->f_bfree, st->f_blocks);
34491+ if (file_inode(new_xino)->i_blocks < blocks)
34492+ au_sbi(sb)->si_xino_jiffy = jiffy;
34493+ } else
34494+ AuErr1("statfs err %d, ignored\n", err);
34495+
34496+out_st:
f0c0a007 34497+ au_delayed_kfree(st);
4f0767ce 34498+out:
1facf9fc 34499+ return err;
34500+}
34501+
34502+struct xino_do_trunc_args {
34503+ struct super_block *sb;
34504+ struct au_branch *br;
34505+};
34506+
34507+static void xino_do_trunc(void *_args)
34508+{
34509+ struct xino_do_trunc_args *args = _args;
34510+ struct super_block *sb;
34511+ struct au_branch *br;
34512+ struct inode *dir;
34513+ int err;
34514+ aufs_bindex_t bindex;
34515+
34516+ err = 0;
34517+ sb = args->sb;
5527c038 34518+ dir = d_inode(sb->s_root);
1facf9fc 34519+ br = args->br;
34520+
34521+ si_noflush_write_lock(sb);
34522+ ii_read_lock_parent(dir);
34523+ bindex = au_br_index(sb, br->br_id);
34524+ err = au_xino_trunc(sb, bindex);
1facf9fc 34525+ ii_read_unlock(dir);
34526+ if (unlikely(err))
392086de 34527+ pr_warn("err b%d, (%d)\n", bindex, err);
1facf9fc 34528+ atomic_dec(&br->br_xino_running);
5afbbe0d 34529+ au_br_put(br);
1facf9fc 34530+ si_write_unlock(sb);
027c5e7a 34531+ au_nwt_done(&au_sbi(sb)->si_nowait);
f0c0a007 34532+ au_delayed_kfree(args);
1facf9fc 34533+}
34534+
392086de
AM
34535+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
34536+{
34537+ int err;
34538+ struct kstatfs st;
34539+ struct au_sbinfo *sbinfo;
34540+
34541+ /* todo: si_xino_expire and the ratio should be customizable */
34542+ sbinfo = au_sbi(sb);
34543+ if (time_before(jiffies,
34544+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
34545+ return 0;
34546+
34547+ /* truncation border */
34548+ err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
34549+ if (unlikely(err)) {
34550+ AuErr1("statfs err %d, ignored\n", err);
34551+ return 0;
34552+ }
34553+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
34554+ return 0;
34555+
34556+ return 1;
34557+}
34558+
1facf9fc 34559+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
34560+{
34561+ struct xino_do_trunc_args *args;
34562+ int wkq_err;
34563+
392086de 34564+ if (!xino_trunc_test(sb, br))
1facf9fc 34565+ return;
34566+
34567+ if (atomic_inc_return(&br->br_xino_running) > 1)
34568+ goto out;
34569+
34570+ /* lock and kfree() will be called in trunc_xino() */
34571+ args = kmalloc(sizeof(*args), GFP_NOFS);
34572+ if (unlikely(!args)) {
34573+ AuErr1("no memory\n");
f0c0a007 34574+ goto out;
1facf9fc 34575+ }
34576+
5afbbe0d 34577+ au_br_get(br);
1facf9fc 34578+ args->sb = sb;
34579+ args->br = br;
53392da6 34580+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 34581+ if (!wkq_err)
34582+ return; /* success */
34583+
4a4d8108 34584+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 34585+ au_br_put(br);
f0c0a007 34586+ au_delayed_kfree(args);
1facf9fc 34587+
4f0767ce 34588+out:
e49829fe 34589+ atomic_dec(&br->br_xino_running);
1facf9fc 34590+}
34591+
34592+/* ---------------------------------------------------------------------- */
34593+
5527c038 34594+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 34595+ ino_t h_ino, ino_t ino)
34596+{
34597+ loff_t pos;
34598+ ssize_t sz;
34599+
34600+ pos = h_ino;
34601+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34602+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34603+ return -EFBIG;
34604+ }
34605+ pos *= sizeof(ino);
34606+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34607+ if (sz == sizeof(ino))
34608+ return 0; /* success */
34609+
34610+ AuIOErr("write failed (%zd)\n", sz);
34611+ return -EIO;
34612+}
34613+
34614+/*
34615+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
34616+ * at the position of @h_ino.
34617+ * even if @ino is zero, it is written to the xinofile and means no entry.
34618+ * if the size of the xino file on a specific filesystem exceeds the watermark,
34619+ * try truncating it.
34620+ */
34621+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34622+ ino_t ino)
34623+{
34624+ int err;
34625+ unsigned int mnt_flags;
34626+ struct au_branch *br;
34627+
34628+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34629+ || ((loff_t)-1) > 0);
dece6358 34630+ SiMustAnyLock(sb);
1facf9fc 34631+
34632+ mnt_flags = au_mntflags(sb);
34633+ if (!au_opt_test(mnt_flags, XINO))
34634+ return 0;
34635+
34636+ br = au_sbr(sb, bindex);
34637+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34638+ h_ino, ino);
34639+ if (!err) {
34640+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 34641+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 34642+ xino_try_trunc(sb, br);
34643+ return 0; /* success */
34644+ }
34645+
34646+ AuIOErr("write failed (%d)\n", err);
34647+ return -EIO;
34648+}
34649+
34650+/* ---------------------------------------------------------------------- */
34651+
34652+/* aufs inode number bitmap */
34653+
34654+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34655+static ino_t xib_calc_ino(unsigned long pindex, int bit)
34656+{
34657+ ino_t ino;
34658+
34659+ AuDebugOn(bit < 0 || page_bits <= bit);
34660+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34661+ return ino;
34662+}
34663+
34664+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34665+{
34666+ AuDebugOn(ino < AUFS_FIRST_INO);
34667+ ino -= AUFS_FIRST_INO;
34668+ *pindex = ino / page_bits;
34669+ *bit = ino % page_bits;
34670+}
34671+
34672+static int xib_pindex(struct super_block *sb, unsigned long pindex)
34673+{
34674+ int err;
34675+ loff_t pos;
34676+ ssize_t sz;
34677+ struct au_sbinfo *sbinfo;
34678+ struct file *xib;
34679+ unsigned long *p;
34680+
34681+ sbinfo = au_sbi(sb);
34682+ MtxMustLock(&sbinfo->si_xib_mtx);
34683+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34684+ || !au_opt_test(sbinfo->si_mntflags, XINO));
34685+
34686+ if (pindex == sbinfo->si_xib_last_pindex)
34687+ return 0;
34688+
34689+ xib = sbinfo->si_xib;
34690+ p = sbinfo->si_xib_buf;
34691+ pos = sbinfo->si_xib_last_pindex;
34692+ pos *= PAGE_SIZE;
34693+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34694+ if (unlikely(sz != PAGE_SIZE))
34695+ goto out;
34696+
34697+ pos = pindex;
34698+ pos *= PAGE_SIZE;
c06a8ce3 34699+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 34700+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34701+ else {
34702+ memset(p, 0, PAGE_SIZE);
34703+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34704+ }
34705+ if (sz == PAGE_SIZE) {
34706+ sbinfo->si_xib_last_pindex = pindex;
34707+ return 0; /* success */
34708+ }
34709+
4f0767ce 34710+out:
b752ccd1
AM
34711+ AuIOErr1("write failed (%zd)\n", sz);
34712+ err = sz;
34713+ if (sz >= 0)
34714+ err = -EIO;
34715+ return err;
34716+}
34717+
34718+/* ---------------------------------------------------------------------- */
34719+
34720+static void au_xib_clear_bit(struct inode *inode)
34721+{
34722+ int err, bit;
34723+ unsigned long pindex;
34724+ struct super_block *sb;
34725+ struct au_sbinfo *sbinfo;
34726+
34727+ AuDebugOn(inode->i_nlink);
34728+
34729+ sb = inode->i_sb;
34730+ xib_calc_bit(inode->i_ino, &pindex, &bit);
34731+ AuDebugOn(page_bits <= bit);
34732+ sbinfo = au_sbi(sb);
34733+ mutex_lock(&sbinfo->si_xib_mtx);
34734+ err = xib_pindex(sb, pindex);
34735+ if (!err) {
34736+ clear_bit(bit, sbinfo->si_xib_buf);
34737+ sbinfo->si_xib_next_bit = bit;
34738+ }
34739+ mutex_unlock(&sbinfo->si_xib_mtx);
34740+}
34741+
34742+/* for s_op->delete_inode() */
34743+void au_xino_delete_inode(struct inode *inode, const int unlinked)
34744+{
34745+ int err;
34746+ unsigned int mnt_flags;
5afbbe0d 34747+ aufs_bindex_t bindex, bbot, bi;
b752ccd1
AM
34748+ unsigned char try_trunc;
34749+ struct au_iinfo *iinfo;
34750+ struct super_block *sb;
34751+ struct au_hinode *hi;
34752+ struct inode *h_inode;
34753+ struct au_branch *br;
5527c038 34754+ vfs_writef_t xwrite;
b752ccd1 34755+
5afbbe0d
AM
34756+ AuDebugOn(au_is_bad_inode(inode));
34757+
b752ccd1
AM
34758+ sb = inode->i_sb;
34759+ mnt_flags = au_mntflags(sb);
34760+ if (!au_opt_test(mnt_flags, XINO)
34761+ || inode->i_ino == AUFS_ROOT_INO)
34762+ return;
34763+
34764+ if (unlinked) {
34765+ au_xigen_inc(inode);
34766+ au_xib_clear_bit(inode);
34767+ }
34768+
34769+ iinfo = au_ii(inode);
5afbbe0d 34770+ bindex = iinfo->ii_btop;
b752ccd1
AM
34771+ if (bindex < 0)
34772+ return;
1facf9fc 34773+
b752ccd1
AM
34774+ xwrite = au_sbi(sb)->si_xwrite;
34775+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
5afbbe0d
AM
34776+ hi = au_hinode(iinfo, bindex);
34777+ bbot = iinfo->ii_bbot;
34778+ for (; bindex <= bbot; bindex++, hi++) {
b752ccd1
AM
34779+ h_inode = hi->hi_inode;
34780+ if (!h_inode
34781+ || (!unlinked && h_inode->i_nlink))
34782+ continue;
1facf9fc 34783+
b752ccd1
AM
34784+ /* inode may not be revalidated */
34785+ bi = au_br_index(sb, hi->hi_id);
34786+ if (bi < 0)
34787+ continue;
1facf9fc 34788+
b752ccd1
AM
34789+ br = au_sbr(sb, bi);
34790+ err = au_xino_do_write(xwrite, br->br_xino.xi_file,
34791+ h_inode->i_ino, /*ino*/0);
34792+ if (!err && try_trunc
86dc4139 34793+ && au_test_fs_trunc_xino(au_br_sb(br)))
b752ccd1 34794+ xino_try_trunc(sb, br);
1facf9fc 34795+ }
1facf9fc 34796+}
34797+
34798+/* get an unused inode number from bitmap */
34799+ino_t au_xino_new_ino(struct super_block *sb)
34800+{
34801+ ino_t ino;
34802+ unsigned long *p, pindex, ul, pend;
34803+ struct au_sbinfo *sbinfo;
34804+ struct file *file;
34805+ int free_bit, err;
34806+
34807+ if (!au_opt_test(au_mntflags(sb), XINO))
34808+ return iunique(sb, AUFS_FIRST_INO);
34809+
34810+ sbinfo = au_sbi(sb);
34811+ mutex_lock(&sbinfo->si_xib_mtx);
34812+ p = sbinfo->si_xib_buf;
34813+ free_bit = sbinfo->si_xib_next_bit;
34814+ if (free_bit < page_bits && !test_bit(free_bit, p))
34815+ goto out; /* success */
34816+ free_bit = find_first_zero_bit(p, page_bits);
34817+ if (free_bit < page_bits)
34818+ goto out; /* success */
34819+
34820+ pindex = sbinfo->si_xib_last_pindex;
34821+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
34822+ err = xib_pindex(sb, ul);
34823+ if (unlikely(err))
34824+ goto out_err;
34825+ free_bit = find_first_zero_bit(p, page_bits);
34826+ if (free_bit < page_bits)
34827+ goto out; /* success */
34828+ }
34829+
34830+ file = sbinfo->si_xib;
c06a8ce3 34831+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
1facf9fc 34832+ for (ul = pindex + 1; ul <= pend; ul++) {
34833+ err = xib_pindex(sb, ul);
34834+ if (unlikely(err))
34835+ goto out_err;
34836+ free_bit = find_first_zero_bit(p, page_bits);
34837+ if (free_bit < page_bits)
34838+ goto out; /* success */
34839+ }
34840+ BUG();
34841+
4f0767ce 34842+out:
1facf9fc 34843+ set_bit(free_bit, p);
7f207e10 34844+ sbinfo->si_xib_next_bit = free_bit + 1;
1facf9fc 34845+ pindex = sbinfo->si_xib_last_pindex;
34846+ mutex_unlock(&sbinfo->si_xib_mtx);
34847+ ino = xib_calc_ino(pindex, free_bit);
34848+ AuDbg("i%lu\n", (unsigned long)ino);
34849+ return ino;
4f0767ce 34850+out_err:
1facf9fc 34851+ mutex_unlock(&sbinfo->si_xib_mtx);
34852+ AuDbg("i0\n");
34853+ return 0;
34854+}
34855+
34856+/*
34857+ * read @ino from xinofile for the specified branch{@sb, @bindex}
34858+ * at the position of @h_ino.
34859+ * if @ino does not exist and @do_new is true, get new one.
34860+ */
34861+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34862+ ino_t *ino)
34863+{
34864+ int err;
34865+ ssize_t sz;
34866+ loff_t pos;
34867+ struct file *file;
34868+ struct au_sbinfo *sbinfo;
34869+
34870+ *ino = 0;
34871+ if (!au_opt_test(au_mntflags(sb), XINO))
34872+ return 0; /* no xino */
34873+
34874+ err = 0;
34875+ sbinfo = au_sbi(sb);
34876+ pos = h_ino;
34877+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
34878+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34879+ return -EFBIG;
34880+ }
34881+ pos *= sizeof(*ino);
34882+
34883+ file = au_sbr(sb, bindex)->br_xino.xi_file;
c06a8ce3 34884+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
1facf9fc 34885+ return 0; /* no ino */
34886+
34887+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
34888+ if (sz == sizeof(*ino))
34889+ return 0; /* success */
34890+
34891+ err = sz;
34892+ if (unlikely(sz >= 0)) {
34893+ err = -EIO;
34894+ AuIOErr("xino read error (%zd)\n", sz);
34895+ }
34896+
34897+ return err;
34898+}
34899+
34900+/* ---------------------------------------------------------------------- */
34901+
34902+/* create and set a new xino file */
34903+
34904+struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
34905+{
34906+ struct file *file;
34907+ struct dentry *h_parent, *d;
b912730e 34908+ struct inode *h_dir, *inode;
1facf9fc 34909+ int err;
34910+
34911+ /*
34912+ * at mount-time, and the xino file is the default path,
4a4d8108 34913+ * hnotify is disabled so we have no notify events to ignore.
1facf9fc 34914+ * when a user specified the xino, we cannot get au_hdir to be ignored.
34915+ */
7f207e10 34916+ file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 34917+ /* | __FMODE_NONOTIFY */,
1facf9fc 34918+ S_IRUGO | S_IWUGO);
34919+ if (IS_ERR(file)) {
34920+ if (!silent)
4a4d8108 34921+ pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
1facf9fc 34922+ return file;
34923+ }
34924+
34925+ /* keep file count */
b912730e
AM
34926+ err = 0;
34927+ inode = file_inode(file);
2000de60 34928+ h_parent = dget_parent(file->f_path.dentry);
5527c038 34929+ h_dir = d_inode(h_parent);
febd17d6 34930+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 34931+ /* mnt_want_write() is unnecessary here */
523b37e3 34932+ /* no delegation since it is just created */
b912730e
AM
34933+ if (inode->i_nlink)
34934+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
34935+ /*force*/0);
febd17d6 34936+ inode_unlock(h_dir);
1facf9fc 34937+ dput(h_parent);
34938+ if (unlikely(err)) {
34939+ if (!silent)
4a4d8108 34940+ pr_err("unlink %s(%d)\n", fname, err);
1facf9fc 34941+ goto out;
34942+ }
34943+
34944+ err = -EINVAL;
2000de60 34945+ d = file->f_path.dentry;
1facf9fc 34946+ if (unlikely(sb == d->d_sb)) {
34947+ if (!silent)
4a4d8108 34948+ pr_err("%s must be outside\n", fname);
1facf9fc 34949+ goto out;
34950+ }
34951+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
34952+ if (!silent)
4a4d8108
AM
34953+ pr_err("xino doesn't support %s(%s)\n",
34954+ fname, au_sbtype(d->d_sb));
1facf9fc 34955+ goto out;
34956+ }
34957+ return file; /* success */
34958+
4f0767ce 34959+out:
1facf9fc 34960+ fput(file);
34961+ file = ERR_PTR(err);
34962+ return file;
34963+}
34964+
34965+/*
34966+ * find another branch who is on the same filesystem of the specified
5afbbe0d 34967+ * branch{@btgt}. search until @bbot.
1facf9fc 34968+ */
34969+static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
5afbbe0d 34970+ aufs_bindex_t bbot)
1facf9fc 34971+{
34972+ aufs_bindex_t bindex;
34973+ struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
34974+
34975+ for (bindex = 0; bindex < btgt; bindex++)
34976+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34977+ return bindex;
5afbbe0d 34978+ for (bindex++; bindex <= bbot; bindex++)
1facf9fc 34979+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34980+ return bindex;
34981+ return -1;
34982+}
34983+
34984+/* ---------------------------------------------------------------------- */
34985+
34986+/*
34987+ * initialize the xinofile for the specified branch @br
34988+ * at the place/path where @base_file indicates.
34989+ * test whether another branch is on the same filesystem or not,
34990+ * if @do_test is true.
34991+ */
34992+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
34993+ struct file *base_file, int do_test)
34994+{
34995+ int err;
34996+ ino_t ino;
5afbbe0d 34997+ aufs_bindex_t bbot, bindex;
1facf9fc 34998+ struct au_branch *shared_br, *b;
34999+ struct file *file;
35000+ struct super_block *tgt_sb;
35001+
35002+ shared_br = NULL;
5afbbe0d 35003+ bbot = au_sbbot(sb);
1facf9fc 35004+ if (do_test) {
86dc4139 35005+ tgt_sb = au_br_sb(br);
5afbbe0d 35006+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 35007+ b = au_sbr(sb, bindex);
86dc4139 35008+ if (tgt_sb == au_br_sb(b)) {
1facf9fc 35009+ shared_br = b;
35010+ break;
35011+ }
35012+ }
35013+ }
35014+
35015+ if (!shared_br || !shared_br->br_xino.xi_file) {
35016+ struct au_xino_lock_dir ldir;
35017+
35018+ au_xino_lock_dir(sb, base_file, &ldir);
35019+ /* mnt_want_write() is unnecessary here */
35020+ file = au_xino_create2(base_file, NULL);
35021+ au_xino_unlock_dir(&ldir);
35022+ err = PTR_ERR(file);
35023+ if (IS_ERR(file))
35024+ goto out;
35025+ br->br_xino.xi_file = file;
35026+ } else {
35027+ br->br_xino.xi_file = shared_br->br_xino.xi_file;
35028+ get_file(br->br_xino.xi_file);
35029+ }
35030+
35031+ ino = AUFS_ROOT_INO;
35032+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
35033+ h_ino, ino);
b752ccd1
AM
35034+ if (unlikely(err)) {
35035+ fput(br->br_xino.xi_file);
35036+ br->br_xino.xi_file = NULL;
35037+ }
1facf9fc 35038+
4f0767ce 35039+out:
1facf9fc 35040+ return err;
35041+}
35042+
35043+/* ---------------------------------------------------------------------- */
35044+
35045+/* trucate a xino bitmap file */
35046+
35047+/* todo: slow */
35048+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
35049+{
35050+ int err, bit;
35051+ ssize_t sz;
35052+ unsigned long pindex;
35053+ loff_t pos, pend;
35054+ struct au_sbinfo *sbinfo;
5527c038 35055+ vfs_readf_t func;
1facf9fc 35056+ ino_t *ino;
35057+ unsigned long *p;
35058+
35059+ err = 0;
35060+ sbinfo = au_sbi(sb);
dece6358 35061+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 35062+ p = sbinfo->si_xib_buf;
35063+ func = sbinfo->si_xread;
c06a8ce3 35064+ pend = vfsub_f_size_read(file);
1facf9fc 35065+ pos = 0;
35066+ while (pos < pend) {
35067+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
35068+ err = sz;
35069+ if (unlikely(sz <= 0))
35070+ goto out;
35071+
35072+ err = 0;
35073+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
35074+ if (unlikely(*ino < AUFS_FIRST_INO))
35075+ continue;
35076+
35077+ xib_calc_bit(*ino, &pindex, &bit);
35078+ AuDebugOn(page_bits <= bit);
35079+ err = xib_pindex(sb, pindex);
35080+ if (!err)
35081+ set_bit(bit, p);
35082+ else
35083+ goto out;
35084+ }
35085+ }
35086+
4f0767ce 35087+out:
1facf9fc 35088+ return err;
35089+}
35090+
35091+static int xib_restore(struct super_block *sb)
35092+{
35093+ int err;
5afbbe0d 35094+ aufs_bindex_t bindex, bbot;
1facf9fc 35095+ void *page;
35096+
35097+ err = -ENOMEM;
35098+ page = (void *)__get_free_page(GFP_NOFS);
35099+ if (unlikely(!page))
35100+ goto out;
35101+
35102+ err = 0;
5afbbe0d
AM
35103+ bbot = au_sbbot(sb);
35104+ for (bindex = 0; !err && bindex <= bbot; bindex++)
1facf9fc 35105+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
35106+ err = do_xib_restore
35107+ (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
35108+ else
35109+ AuDbg("b%d\n", bindex);
f0c0a007 35110+ au_delayed_free_page((unsigned long)page);
1facf9fc 35111+
4f0767ce 35112+out:
1facf9fc 35113+ return err;
35114+}
35115+
35116+int au_xib_trunc(struct super_block *sb)
35117+{
35118+ int err;
35119+ ssize_t sz;
35120+ loff_t pos;
35121+ struct au_xino_lock_dir ldir;
35122+ struct au_sbinfo *sbinfo;
35123+ unsigned long *p;
35124+ struct file *file;
35125+
dece6358
AM
35126+ SiMustWriteLock(sb);
35127+
1facf9fc 35128+ err = 0;
35129+ sbinfo = au_sbi(sb);
35130+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
35131+ goto out;
35132+
35133+ file = sbinfo->si_xib;
c06a8ce3 35134+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 35135+ goto out;
35136+
35137+ au_xino_lock_dir(sb, file, &ldir);
35138+ /* mnt_want_write() is unnecessary here */
35139+ file = au_xino_create2(sbinfo->si_xib, NULL);
35140+ au_xino_unlock_dir(&ldir);
35141+ err = PTR_ERR(file);
35142+ if (IS_ERR(file))
35143+ goto out;
35144+ fput(sbinfo->si_xib);
35145+ sbinfo->si_xib = file;
35146+
35147+ p = sbinfo->si_xib_buf;
35148+ memset(p, 0, PAGE_SIZE);
35149+ pos = 0;
35150+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
35151+ if (unlikely(sz != PAGE_SIZE)) {
35152+ err = sz;
35153+ AuIOErr("err %d\n", err);
35154+ if (sz >= 0)
35155+ err = -EIO;
35156+ goto out;
35157+ }
35158+
35159+ mutex_lock(&sbinfo->si_xib_mtx);
35160+ /* mnt_want_write() is unnecessary here */
35161+ err = xib_restore(sb);
35162+ mutex_unlock(&sbinfo->si_xib_mtx);
35163+
35164+out:
35165+ return err;
35166+}
35167+
35168+/* ---------------------------------------------------------------------- */
35169+
35170+/*
35171+ * xino mount option handlers
35172+ */
1facf9fc 35173+
35174+/* xino bitmap */
35175+static void xino_clear_xib(struct super_block *sb)
35176+{
35177+ struct au_sbinfo *sbinfo;
35178+
dece6358
AM
35179+ SiMustWriteLock(sb);
35180+
1facf9fc 35181+ sbinfo = au_sbi(sb);
35182+ sbinfo->si_xread = NULL;
35183+ sbinfo->si_xwrite = NULL;
35184+ if (sbinfo->si_xib)
35185+ fput(sbinfo->si_xib);
35186+ sbinfo->si_xib = NULL;
f0c0a007
AM
35187+ if (sbinfo->si_xib_buf)
35188+ au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
1facf9fc 35189+ sbinfo->si_xib_buf = NULL;
35190+}
35191+
35192+static int au_xino_set_xib(struct super_block *sb, struct file *base)
35193+{
35194+ int err;
35195+ loff_t pos;
35196+ struct au_sbinfo *sbinfo;
35197+ struct file *file;
35198+
dece6358
AM
35199+ SiMustWriteLock(sb);
35200+
1facf9fc 35201+ sbinfo = au_sbi(sb);
35202+ file = au_xino_create2(base, sbinfo->si_xib);
35203+ err = PTR_ERR(file);
35204+ if (IS_ERR(file))
35205+ goto out;
35206+ if (sbinfo->si_xib)
35207+ fput(sbinfo->si_xib);
35208+ sbinfo->si_xib = file;
5527c038
JR
35209+ sbinfo->si_xread = vfs_readf(file);
35210+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 35211+
35212+ err = -ENOMEM;
35213+ if (!sbinfo->si_xib_buf)
35214+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
35215+ if (unlikely(!sbinfo->si_xib_buf))
35216+ goto out_unset;
35217+
35218+ sbinfo->si_xib_last_pindex = 0;
35219+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 35220+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 35221+ pos = 0;
35222+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
35223+ PAGE_SIZE, &pos);
35224+ if (unlikely(err != PAGE_SIZE))
35225+ goto out_free;
35226+ }
35227+ err = 0;
35228+ goto out; /* success */
35229+
4f0767ce 35230+out_free:
f0c0a007
AM
35231+ if (sbinfo->si_xib_buf)
35232+ au_delayed_free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
35233+ sbinfo->si_xib_buf = NULL;
35234+ if (err >= 0)
35235+ err = -EIO;
4f0767ce 35236+out_unset:
b752ccd1
AM
35237+ fput(sbinfo->si_xib);
35238+ sbinfo->si_xib = NULL;
35239+ sbinfo->si_xread = NULL;
35240+ sbinfo->si_xwrite = NULL;
4f0767ce 35241+out:
b752ccd1 35242+ return err;
1facf9fc 35243+}
35244+
b752ccd1
AM
35245+/* xino for each branch */
35246+static void xino_clear_br(struct super_block *sb)
35247+{
5afbbe0d 35248+ aufs_bindex_t bindex, bbot;
b752ccd1 35249+ struct au_branch *br;
1facf9fc 35250+
5afbbe0d
AM
35251+ bbot = au_sbbot(sb);
35252+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
35253+ br = au_sbr(sb, bindex);
35254+ if (!br || !br->br_xino.xi_file)
35255+ continue;
35256+
35257+ fput(br->br_xino.xi_file);
35258+ br->br_xino.xi_file = NULL;
35259+ }
35260+}
35261+
35262+static int au_xino_set_br(struct super_block *sb, struct file *base)
1facf9fc 35263+{
35264+ int err;
b752ccd1 35265+ ino_t ino;
5afbbe0d 35266+ aufs_bindex_t bindex, bbot, bshared;
b752ccd1
AM
35267+ struct {
35268+ struct file *old, *new;
35269+ } *fpair, *p;
35270+ struct au_branch *br;
35271+ struct inode *inode;
5527c038 35272+ vfs_writef_t writef;
1facf9fc 35273+
b752ccd1
AM
35274+ SiMustWriteLock(sb);
35275+
35276+ err = -ENOMEM;
5afbbe0d
AM
35277+ bbot = au_sbbot(sb);
35278+ fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
b752ccd1 35279+ if (unlikely(!fpair))
1facf9fc 35280+ goto out;
35281+
5527c038 35282+ inode = d_inode(sb->s_root);
b752ccd1
AM
35283+ ino = AUFS_ROOT_INO;
35284+ writef = au_sbi(sb)->si_xwrite;
5afbbe0d 35285+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
35286+ bshared = is_sb_shared(sb, bindex, bindex - 1);
35287+ if (bshared >= 0) {
35288+ /* shared xino */
35289+ *p = fpair[bshared];
35290+ get_file(p->new);
35291+ }
35292+
35293+ if (!p->new) {
35294+ /* new xino */
5afbbe0d 35295+ br = au_sbr(sb, bindex);
b752ccd1
AM
35296+ p->old = br->br_xino.xi_file;
35297+ p->new = au_xino_create2(base, br->br_xino.xi_file);
35298+ err = PTR_ERR(p->new);
35299+ if (IS_ERR(p->new)) {
35300+ p->new = NULL;
35301+ goto out_pair;
35302+ }
35303+ }
35304+
35305+ err = au_xino_do_write(writef, p->new,
35306+ au_h_iptr(inode, bindex)->i_ino, ino);
35307+ if (unlikely(err))
35308+ goto out_pair;
35309+ }
35310+
5afbbe0d 35311+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
35312+ br = au_sbr(sb, bindex);
35313+ if (br->br_xino.xi_file)
35314+ fput(br->br_xino.xi_file);
35315+ get_file(p->new);
35316+ br->br_xino.xi_file = p->new;
35317+ }
1facf9fc 35318+
4f0767ce 35319+out_pair:
5afbbe0d 35320+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
b752ccd1
AM
35321+ if (p->new)
35322+ fput(p->new);
35323+ else
35324+ break;
f0c0a007 35325+ au_delayed_kfree(fpair);
4f0767ce 35326+out:
1facf9fc 35327+ return err;
35328+}
b752ccd1
AM
35329+
35330+void au_xino_clr(struct super_block *sb)
35331+{
35332+ struct au_sbinfo *sbinfo;
35333+
35334+ au_xigen_clr(sb);
35335+ xino_clear_xib(sb);
35336+ xino_clear_br(sb);
35337+ sbinfo = au_sbi(sb);
35338+ /* lvalue, do not call au_mntflags() */
35339+ au_opt_clr(sbinfo->si_mntflags, XINO);
35340+}
35341+
35342+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
35343+{
35344+ int err, skip;
35345+ struct dentry *parent, *cur_parent;
35346+ struct qstr *dname, *cur_name;
35347+ struct file *cur_xino;
35348+ struct inode *dir;
35349+ struct au_sbinfo *sbinfo;
35350+
35351+ SiMustWriteLock(sb);
35352+
35353+ err = 0;
35354+ sbinfo = au_sbi(sb);
2000de60 35355+ parent = dget_parent(xino->file->f_path.dentry);
b752ccd1
AM
35356+ if (remount) {
35357+ skip = 0;
2000de60 35358+ dname = &xino->file->f_path.dentry->d_name;
b752ccd1
AM
35359+ cur_xino = sbinfo->si_xib;
35360+ if (cur_xino) {
2000de60
JR
35361+ cur_parent = dget_parent(cur_xino->f_path.dentry);
35362+ cur_name = &cur_xino->f_path.dentry->d_name;
b752ccd1 35363+ skip = (cur_parent == parent
38d290e6 35364+ && au_qstreq(dname, cur_name));
b752ccd1
AM
35365+ dput(cur_parent);
35366+ }
35367+ if (skip)
35368+ goto out;
35369+ }
35370+
35371+ au_opt_set(sbinfo->si_mntflags, XINO);
5527c038 35372+ dir = d_inode(parent);
febd17d6 35373+ inode_lock_nested(dir, AuLsc_I_PARENT);
b752ccd1
AM
35374+ /* mnt_want_write() is unnecessary here */
35375+ err = au_xino_set_xib(sb, xino->file);
35376+ if (!err)
35377+ err = au_xigen_set(sb, xino->file);
35378+ if (!err)
35379+ err = au_xino_set_br(sb, xino->file);
febd17d6 35380+ inode_unlock(dir);
b752ccd1
AM
35381+ if (!err)
35382+ goto out; /* success */
35383+
35384+ /* reset all */
35385+ AuIOErr("failed creating xino(%d).\n", err);
c1595e42
JR
35386+ au_xigen_clr(sb);
35387+ xino_clear_xib(sb);
b752ccd1 35388+
4f0767ce 35389+out:
b752ccd1
AM
35390+ dput(parent);
35391+ return err;
35392+}
35393+
35394+/* ---------------------------------------------------------------------- */
35395+
35396+/*
35397+ * create a xinofile at the default place/path.
35398+ */
35399+struct file *au_xino_def(struct super_block *sb)
35400+{
35401+ struct file *file;
35402+ char *page, *p;
35403+ struct au_branch *br;
35404+ struct super_block *h_sb;
35405+ struct path path;
5afbbe0d 35406+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
35407+
35408+ br = NULL;
5afbbe0d 35409+ bbot = au_sbbot(sb);
b752ccd1 35410+ bwr = -1;
5afbbe0d 35411+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
35412+ br = au_sbr(sb, bindex);
35413+ if (au_br_writable(br->br_perm)
86dc4139 35414+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
35415+ bwr = bindex;
35416+ break;
35417+ }
35418+ }
35419+
7f207e10
AM
35420+ if (bwr >= 0) {
35421+ file = ERR_PTR(-ENOMEM);
537831f9 35422+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
35423+ if (unlikely(!page))
35424+ goto out;
86dc4139 35425+ path.mnt = au_br_mnt(br);
7f207e10
AM
35426+ path.dentry = au_h_dptr(sb->s_root, bwr);
35427+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
35428+ file = (void *)p;
35429+ if (!IS_ERR(p)) {
35430+ strcat(p, "/" AUFS_XINO_FNAME);
35431+ AuDbg("%s\n", p);
35432+ file = au_xino_create(sb, p, /*silent*/0);
35433+ if (!IS_ERR(file))
35434+ au_xino_brid_set(sb, br->br_id);
35435+ }
f0c0a007 35436+ au_delayed_free_page((unsigned long)page);
7f207e10
AM
35437+ } else {
35438+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
35439+ if (IS_ERR(file))
35440+ goto out;
2000de60 35441+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
35442+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
35443+ pr_err("xino doesn't support %s(%s)\n",
35444+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
35445+ fput(file);
35446+ file = ERR_PTR(-EINVAL);
35447+ }
35448+ if (!IS_ERR(file))
35449+ au_xino_brid_set(sb, -1);
35450+ }
0c5527e5 35451+
7f207e10
AM
35452+out:
35453+ return file;
35454+}
35455+
35456+/* ---------------------------------------------------------------------- */
35457+
35458+int au_xino_path(struct seq_file *seq, struct file *file)
35459+{
35460+ int err;
35461+
35462+ err = au_seq_path(seq, &file->f_path);
79b8bda9 35463+ if (unlikely(err))
7f207e10
AM
35464+ goto out;
35465+
7f207e10
AM
35466+#define Deleted "\\040(deleted)"
35467+ seq->count -= sizeof(Deleted) - 1;
35468+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
35469+ sizeof(Deleted) - 1));
35470+#undef Deleted
35471+
35472+out:
35473+ return err;
35474+}
537831f9
AM
35475diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
35476--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
f0c0a007 35477+++ linux/include/uapi/linux/aufs_type.h 2016-08-17 18:01:21.295617591 +0200
c1595e42 35478@@ -0,0 +1,419 @@
7f207e10 35479+/*
8cdd5066 35480+ * Copyright (C) 2005-2016 Junjiro R. Okajima
7f207e10
AM
35481+ *
35482+ * This program, aufs is free software; you can redistribute it and/or modify
35483+ * it under the terms of the GNU General Public License as published by
35484+ * the Free Software Foundation; either version 2 of the License, or
35485+ * (at your option) any later version.
35486+ *
35487+ * This program is distributed in the hope that it will be useful,
35488+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35489+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35490+ * GNU General Public License for more details.
35491+ *
35492+ * You should have received a copy of the GNU General Public License
523b37e3 35493+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
35494+ */
35495+
35496+#ifndef __AUFS_TYPE_H__
35497+#define __AUFS_TYPE_H__
35498+
f6c5ef8b
AM
35499+#define AUFS_NAME "aufs"
35500+
9dbd164d 35501+#ifdef __KERNEL__
f6c5ef8b
AM
35502+/*
35503+ * define it before including all other headers.
35504+ * sched.h may use pr_* macros before defining "current", so define the
35505+ * no-current version first, and re-define later.
35506+ */
35507+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
35508+#include <linux/sched.h>
35509+#undef pr_fmt
a2a7ad62
AM
35510+#define pr_fmt(fmt) \
35511+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
35512+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
35513+#else
35514+#include <stdint.h>
35515+#include <sys/types.h>
f6c5ef8b 35516+#endif /* __KERNEL__ */
7f207e10 35517+
f6c5ef8b
AM
35518+#include <linux/limits.h>
35519+
f0c0a007 35520+#define AUFS_VERSION "4.7-20160815"
7f207e10
AM
35521+
35522+/* todo? move this to linux-2.6.19/include/magic.h */
35523+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
35524+
35525+/* ---------------------------------------------------------------------- */
35526+
35527+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 35528+typedef int8_t aufs_bindex_t;
7f207e10
AM
35529+#define AUFS_BRANCH_MAX 127
35530+#else
9dbd164d 35531+typedef int16_t aufs_bindex_t;
7f207e10
AM
35532+#ifdef CONFIG_AUFS_BRANCH_MAX_511
35533+#define AUFS_BRANCH_MAX 511
35534+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
35535+#define AUFS_BRANCH_MAX 1023
35536+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
35537+#define AUFS_BRANCH_MAX 32767
35538+#endif
35539+#endif
35540+
35541+#ifdef __KERNEL__
35542+#ifndef AUFS_BRANCH_MAX
35543+#error unknown CONFIG_AUFS_BRANCH_MAX value
35544+#endif
35545+#endif /* __KERNEL__ */
35546+
35547+/* ---------------------------------------------------------------------- */
35548+
7f207e10
AM
35549+#define AUFS_FSTYPE AUFS_NAME
35550+
35551+#define AUFS_ROOT_INO 2
35552+#define AUFS_FIRST_INO 11
35553+
35554+#define AUFS_WH_PFX ".wh."
35555+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
35556+#define AUFS_WH_TMP_LEN 4
86dc4139 35557+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
35558+#define AUFS_MAX_NAMELEN (NAME_MAX \
35559+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
35560+ - 1 /* dot */\
35561+ - AUFS_WH_TMP_LEN) /* hex */
35562+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
35563+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
35564+#define AUFS_XINO_DEF_SEC 30 /* seconds */
35565+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
35566+#define AUFS_DIRWH_DEF 3
35567+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 35568+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
35569+#define AUFS_RDBLK_DEF 512 /* bytes */
35570+#define AUFS_RDHASH_DEF 32
35571+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
35572+#define AUFS_MFS_DEF_SEC 30 /* seconds */
35573+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 35574+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 35575+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
35576+
35577+/* pseudo-link maintenace under /proc */
35578+#define AUFS_PLINK_MAINT_NAME "plink_maint"
35579+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
35580+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35581+
35582+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
35583+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
35584+
35585+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
35586+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
35587+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
35588+
35589+/* doubly whiteouted */
35590+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
35591+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
35592+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
35593+
1e00d052 35594+/* branch permissions and attributes */
7f207e10
AM
35595+#define AUFS_BRPERM_RW "rw"
35596+#define AUFS_BRPERM_RO "ro"
35597+#define AUFS_BRPERM_RR "rr"
076b876e
AM
35598+#define AUFS_BRATTR_COO_REG "coo_reg"
35599+#define AUFS_BRATTR_COO_ALL "coo_all"
35600+#define AUFS_BRATTR_FHSM "fhsm"
35601+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
35602+#define AUFS_BRATTR_ICEX "icex"
35603+#define AUFS_BRATTR_ICEX_SEC "icexsec"
35604+#define AUFS_BRATTR_ICEX_SYS "icexsys"
35605+#define AUFS_BRATTR_ICEX_TR "icextr"
35606+#define AUFS_BRATTR_ICEX_USR "icexusr"
35607+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
35608+#define AUFS_BRRATTR_WH "wh"
35609+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
35610+#define AUFS_BRWATTR_MOO "moo"
35611+
35612+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
35613+#define AuBrPerm_RO (1 << 1) /* readonly */
35614+#define AuBrPerm_RR (1 << 2) /* natively readonly */
35615+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35616+
35617+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
35618+#define AuBrAttr_COO_ALL (1 << 4)
35619+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35620+
35621+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
35622+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
35623+ branch. meaningless since
35624+ linux-3.18-rc1 */
35625+
35626+/* ignore error in copying XATTR */
35627+#define AuBrAttr_ICEX_SEC (1 << 7)
35628+#define AuBrAttr_ICEX_SYS (1 << 8)
35629+#define AuBrAttr_ICEX_TR (1 << 9)
35630+#define AuBrAttr_ICEX_USR (1 << 10)
35631+#define AuBrAttr_ICEX_OTH (1 << 11)
35632+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
35633+ | AuBrAttr_ICEX_SYS \
35634+ | AuBrAttr_ICEX_TR \
35635+ | AuBrAttr_ICEX_USR \
35636+ | AuBrAttr_ICEX_OTH)
35637+
35638+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
35639+#define AuBrRAttr_Mask AuBrRAttr_WH
35640+
c1595e42
JR
35641+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
35642+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
35643+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35644+
35645+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35646+
c1595e42 35647+/* #warning test userspace */
076b876e
AM
35648+#ifdef __KERNEL__
35649+#ifndef CONFIG_AUFS_FHSM
35650+#undef AuBrAttr_FHSM
35651+#define AuBrAttr_FHSM 0
35652+#endif
c1595e42
JR
35653+#ifndef CONFIG_AUFS_XATTR
35654+#undef AuBrAttr_ICEX
35655+#define AuBrAttr_ICEX 0
35656+#undef AuBrAttr_ICEX_SEC
35657+#define AuBrAttr_ICEX_SEC 0
35658+#undef AuBrAttr_ICEX_SYS
35659+#define AuBrAttr_ICEX_SYS 0
35660+#undef AuBrAttr_ICEX_TR
35661+#define AuBrAttr_ICEX_TR 0
35662+#undef AuBrAttr_ICEX_USR
35663+#define AuBrAttr_ICEX_USR 0
35664+#undef AuBrAttr_ICEX_OTH
35665+#define AuBrAttr_ICEX_OTH 0
35666+#endif
076b876e
AM
35667+#endif
35668+
35669+/* the longest combination */
c1595e42
JR
35670+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35671+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
35672+ "+" AUFS_BRATTR_COO_REG \
35673+ "+" AUFS_BRATTR_FHSM \
35674+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
35675+ "+" AUFS_BRATTR_ICEX_SEC \
35676+ "+" AUFS_BRATTR_ICEX_SYS \
35677+ "+" AUFS_BRATTR_ICEX_USR \
35678+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
35679+ "+" AUFS_BRWATTR_NLWH)
35680+
35681+typedef struct {
35682+ char a[AuBrPermStrSz];
35683+} au_br_perm_str_t;
35684+
35685+static inline int au_br_writable(int brperm)
35686+{
35687+ return brperm & AuBrPerm_RW;
35688+}
35689+
35690+static inline int au_br_whable(int brperm)
35691+{
35692+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35693+}
35694+
35695+static inline int au_br_wh_linkable(int brperm)
35696+{
35697+ return !(brperm & AuBrWAttr_NoLinkWH);
35698+}
35699+
35700+static inline int au_br_cmoo(int brperm)
35701+{
35702+ return brperm & AuBrAttr_CMOO_Mask;
35703+}
35704+
35705+static inline int au_br_fhsm(int brperm)
35706+{
35707+ return brperm & AuBrAttr_FHSM;
35708+}
7f207e10
AM
35709+
35710+/* ---------------------------------------------------------------------- */
35711+
35712+/* ioctl */
35713+enum {
35714+ /* readdir in userspace */
35715+ AuCtl_RDU,
35716+ AuCtl_RDU_INO,
35717+
076b876e
AM
35718+ AuCtl_WBR_FD, /* pathconf wrapper */
35719+ AuCtl_IBUSY, /* busy inode */
35720+ AuCtl_MVDOWN, /* move-down */
35721+ AuCtl_BR, /* info about branches */
35722+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
35723+};
35724+
35725+/* borrowed from linux/include/linux/kernel.h */
35726+#ifndef ALIGN
35727+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
35728+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
35729+#endif
35730+
35731+/* borrowed from linux/include/linux/compiler-gcc3.h */
35732+#ifndef __aligned
35733+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
35734+#endif
35735+
35736+#ifdef __KERNEL__
35737+#ifndef __packed
7f207e10
AM
35738+#define __packed __attribute__((packed))
35739+#endif
53392da6 35740+#endif
7f207e10
AM
35741+
35742+struct au_rdu_cookie {
9dbd164d
AM
35743+ uint64_t h_pos;
35744+ int16_t bindex;
35745+ uint8_t flags;
35746+ uint8_t pad;
35747+ uint32_t generation;
7f207e10
AM
35748+} __aligned(8);
35749+
35750+struct au_rdu_ent {
9dbd164d
AM
35751+ uint64_t ino;
35752+ int16_t bindex;
35753+ uint8_t type;
35754+ uint8_t nlen;
35755+ uint8_t wh;
7f207e10
AM
35756+ char name[0];
35757+} __aligned(8);
35758+
35759+static inline int au_rdu_len(int nlen)
35760+{
35761+ /* include the terminating NULL */
35762+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 35763+ sizeof(uint64_t));
7f207e10
AM
35764+}
35765+
35766+union au_rdu_ent_ul {
35767+ struct au_rdu_ent __user *e;
9dbd164d 35768+ uint64_t ul;
7f207e10
AM
35769+};
35770+
35771+enum {
35772+ AufsCtlRduV_SZ,
35773+ AufsCtlRduV_End
35774+};
35775+
35776+struct aufs_rdu {
35777+ /* input */
35778+ union {
9dbd164d
AM
35779+ uint64_t sz; /* AuCtl_RDU */
35780+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
35781+ };
35782+ union au_rdu_ent_ul ent;
9dbd164d 35783+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
35784+
35785+ /* input/output */
9dbd164d 35786+ uint32_t blk;
7f207e10
AM
35787+
35788+ /* output */
35789+ union au_rdu_ent_ul tail;
35790+ /* number of entries which were added in a single call */
9dbd164d
AM
35791+ uint64_t rent;
35792+ uint8_t full;
35793+ uint8_t shwh;
7f207e10
AM
35794+
35795+ struct au_rdu_cookie cookie;
35796+} __aligned(8);
35797+
1e00d052
AM
35798+/* ---------------------------------------------------------------------- */
35799+
35800+struct aufs_wbr_fd {
9dbd164d
AM
35801+ uint32_t oflags;
35802+ int16_t brid;
1e00d052
AM
35803+} __aligned(8);
35804+
35805+/* ---------------------------------------------------------------------- */
35806+
027c5e7a 35807+struct aufs_ibusy {
9dbd164d
AM
35808+ uint64_t ino, h_ino;
35809+ int16_t bindex;
027c5e7a
AM
35810+} __aligned(8);
35811+
1e00d052
AM
35812+/* ---------------------------------------------------------------------- */
35813+
392086de
AM
35814+/* error code for move-down */
35815+/* the actual message strings are implemented in aufs-util.git */
35816+enum {
35817+ EAU_MVDOWN_OPAQUE = 1,
35818+ EAU_MVDOWN_WHITEOUT,
35819+ EAU_MVDOWN_UPPER,
35820+ EAU_MVDOWN_BOTTOM,
35821+ EAU_MVDOWN_NOUPPER,
35822+ EAU_MVDOWN_NOLOWERBR,
35823+ EAU_Last
35824+};
35825+
c2b27bf2 35826+/* flags for move-down */
392086de
AM
35827+#define AUFS_MVDOWN_DMSG 1
35828+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
35829+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
35830+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
35831+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
35832+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
35833+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
35834+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
35835+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
35836+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
35837+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
35838+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
35839+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 35840+
076b876e 35841+/* index for move-down */
392086de
AM
35842+enum {
35843+ AUFS_MVDOWN_UPPER,
35844+ AUFS_MVDOWN_LOWER,
35845+ AUFS_MVDOWN_NARRAY
35846+};
35847+
076b876e
AM
35848+/*
35849+ * additional info of move-down
35850+ * number of free blocks and inodes.
35851+ * subset of struct kstatfs, but smaller and always 64bit.
35852+ */
35853+struct aufs_stfs {
35854+ uint64_t f_blocks;
35855+ uint64_t f_bavail;
35856+ uint64_t f_files;
35857+ uint64_t f_ffree;
35858+};
35859+
35860+struct aufs_stbr {
35861+ int16_t brid; /* optional input */
35862+ int16_t bindex; /* output */
35863+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
35864+} __aligned(8);
35865+
c2b27bf2 35866+struct aufs_mvdown {
076b876e
AM
35867+ uint32_t flags; /* input/output */
35868+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
35869+ int8_t au_errno; /* output */
35870+} __aligned(8);
35871+
35872+/* ---------------------------------------------------------------------- */
35873+
35874+union aufs_brinfo {
35875+ /* PATH_MAX may differ between kernel-space and user-space */
35876+ char _spacer[4096];
392086de 35877+ struct {
076b876e
AM
35878+ int16_t id;
35879+ int perm;
35880+ char path[0];
35881+ };
c2b27bf2
AM
35882+} __aligned(8);
35883+
35884+/* ---------------------------------------------------------------------- */
35885+
7f207e10
AM
35886+#define AuCtlType 'A'
35887+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
35888+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
35889+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
35890+ struct aufs_wbr_fd)
027c5e7a 35891+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
35892+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
35893+ struct aufs_mvdown)
076b876e
AM
35894+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
35895+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
35896+
35897+#endif /* __AUFS_TYPE_H__ */
f0c0a007 35898aufs4.7 loopback patch
5527c038
JR
35899
35900diff --git a/drivers/block/loop.c b/drivers/block/loop.c
5afbbe0d 35901index 7339e65..76e5da4 100644
5527c038
JR
35902--- a/drivers/block/loop.c
35903+++ b/drivers/block/loop.c
5afbbe0d 35904@@ -562,7 +562,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
5527c038
JR
35905 }
35906
35907 struct switch_request {
35908- struct file *file;
35909+ struct file *file, *virt_file;
35910 struct completion wait;
35911 };
35912
5afbbe0d 35913@@ -588,6 +588,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
5527c038
JR
35914 mapping = file->f_mapping;
35915 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
35916 lo->lo_backing_file = file;
35917+ lo->lo_backing_virt_file = p->virt_file;
35918 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
35919 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
35920 lo->old_gfp_mask = mapping_gfp_mask(mapping);
5afbbe0d 35921@@ -600,11 +601,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
5527c038
JR
35922 * First it needs to flush existing IO, it does this by sending a magic
35923 * BIO down the pipe. The completion of this BIO does the actual switch.
35924 */
35925-static int loop_switch(struct loop_device *lo, struct file *file)
35926+static int loop_switch(struct loop_device *lo, struct file *file,
35927+ struct file *virt_file)
35928 {
35929 struct switch_request w;
35930
35931 w.file = file;
35932+ w.virt_file = virt_file;
35933
35934 /* freeze queue and wait for completion of scheduled requests */
35935 blk_mq_freeze_queue(lo->lo_queue);
5afbbe0d 35936@@ -623,7 +626,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
5527c038
JR
35937 */
35938 static int loop_flush(struct loop_device *lo)
35939 {
35940- return loop_switch(lo, NULL);
35941+ return loop_switch(lo, NULL, NULL);
35942+}
35943+
35944+static struct file *loop_real_file(struct file *file)
35945+{
35946+ struct file *f = NULL;
35947+
35948+ if (file->f_path.dentry->d_sb->s_op->real_loop)
35949+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
35950+ return f;
35951 }
35952
c2c0f25c 35953 static void loop_reread_partitions(struct loop_device *lo,
5afbbe0d 35954@@ -660,6 +672,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
35955 unsigned int arg)
35956 {
35957 struct file *file, *old_file;
35958+ struct file *f, *virt_file = NULL, *old_virt_file;
35959 struct inode *inode;
35960 int error;
35961
5afbbe0d 35962@@ -676,9 +689,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
35963 file = fget(arg);
35964 if (!file)
35965 goto out;
35966+ f = loop_real_file(file);
35967+ if (f) {
35968+ virt_file = file;
35969+ file = f;
35970+ get_file(file);
35971+ }
35972
35973 inode = file->f_mapping->host;
35974 old_file = lo->lo_backing_file;
35975+ old_virt_file = lo->lo_backing_virt_file;
35976
35977 error = -EINVAL;
35978
5afbbe0d 35979@@ -690,17 +710,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
35980 goto out_putf;
35981
35982 /* and ... switch */
35983- error = loop_switch(lo, file);
35984+ error = loop_switch(lo, file, virt_file);
35985 if (error)
35986 goto out_putf;
35987
35988 fput(old_file);
35989+ if (old_virt_file)
35990+ fput(old_virt_file);
35991 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 35992 loop_reread_partitions(lo, bdev);
5527c038
JR
35993 return 0;
35994
35995 out_putf:
35996 fput(file);
35997+ if (virt_file)
35998+ fput(virt_file);
35999 out:
36000 return error;
36001 }
5afbbe0d 36002@@ -887,7 +911,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
36003 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
36004 struct block_device *bdev, unsigned int arg)
36005 {
36006- struct file *file, *f;
36007+ struct file *file, *f, *virt_file = NULL;
36008 struct inode *inode;
36009 struct address_space *mapping;
36010 unsigned lo_blocksize;
5afbbe0d 36011@@ -902,6 +926,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
36012 file = fget(arg);
36013 if (!file)
36014 goto out;
36015+ f = loop_real_file(file);
36016+ if (f) {
36017+ virt_file = file;
36018+ file = f;
36019+ get_file(file);
36020+ }
36021
36022 error = -EBUSY;
36023 if (lo->lo_state != Lo_unbound)
5afbbe0d 36024@@ -954,6 +984,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
36025 lo->lo_device = bdev;
36026 lo->lo_flags = lo_flags;
36027 lo->lo_backing_file = file;
36028+ lo->lo_backing_virt_file = virt_file;
36029 lo->transfer = NULL;
36030 lo->ioctl = NULL;
36031 lo->lo_sizelimit = 0;
5afbbe0d 36032@@ -986,6 +1017,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
36033
36034 out_putf:
36035 fput(file);
36036+ if (virt_file)
36037+ fput(virt_file);
36038 out:
36039 /* This is safe: open() is still holding a reference. */
36040 module_put(THIS_MODULE);
5afbbe0d 36041@@ -1032,6 +1065,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
36042 static int loop_clr_fd(struct loop_device *lo)
36043 {
36044 struct file *filp = lo->lo_backing_file;
36045+ struct file *virt_filp = lo->lo_backing_virt_file;
36046 gfp_t gfp = lo->old_gfp_mask;
36047 struct block_device *bdev = lo->lo_device;
36048
5afbbe0d 36049@@ -1063,6 +1097,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
36050 spin_lock_irq(&lo->lo_lock);
36051 lo->lo_state = Lo_rundown;
36052 lo->lo_backing_file = NULL;
36053+ lo->lo_backing_virt_file = NULL;
36054 spin_unlock_irq(&lo->lo_lock);
36055
36056 loop_release_xfer(lo);
5afbbe0d 36057@@ -1107,6 +1142,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
36058 * bd_mutex which is usually taken before lo_ctl_mutex.
36059 */
36060 fput(filp);
36061+ if (virt_filp)
36062+ fput(virt_filp);
36063 return 0;
36064 }
36065
36066diff --git a/drivers/block/loop.h b/drivers/block/loop.h
be52b249 36067index fb2237c..c3888c5 100644
5527c038
JR
36068--- a/drivers/block/loop.h
36069+++ b/drivers/block/loop.h
36070@@ -46,7 +46,7 @@ struct loop_device {
36071 int (*ioctl)(struct loop_device *, int cmd,
36072 unsigned long arg);
36073
36074- struct file * lo_backing_file;
36075+ struct file * lo_backing_file, *lo_backing_virt_file;
36076 struct block_device *lo_device;
36077 unsigned lo_blocksize;
36078 void *key_data;
36079diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
f0c0a007 36080index 00475fb..01390e1 100644
5527c038
JR
36081--- a/fs/aufs/f_op.c
36082+++ b/fs/aufs/f_op.c
f0c0a007 36083@@ -348,7 +348,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
36084 if (IS_ERR(h_file))
36085 goto out;
36086
36087- if (au_test_loopback_kthread()) {
36088+ if (0 && au_test_loopback_kthread()) {
36089 au_warn_loopback(h_file->f_path.dentry->d_sb);
36090 if (file->f_mapping != h_file->f_mapping) {
36091 file->f_mapping = h_file->f_mapping;
36092diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
f0c0a007 36093index e92a345..35f4d48 100644
5527c038
JR
36094--- a/fs/aufs/loop.c
36095+++ b/fs/aufs/loop.c
79b8bda9
AM
36096@@ -131,3 +131,19 @@ void au_loopback_fin(void)
36097 symbol_put(loop_backing_file);
f0c0a007 36098 au_delayed_kfree(au_warn_loopback_array);
5527c038
JR
36099 }
36100+
36101+/* ---------------------------------------------------------------------- */
36102+
36103+/* support the loopback block device insude aufs */
36104+
36105+struct file *aufs_real_loop(struct file *file)
36106+{
36107+ struct file *f;
36108+
36109+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
36110+ fi_read_lock(file);
36111+ f = au_hf_top(file);
36112+ fi_read_unlock(file);
36113+ AuDebugOn(!f);
36114+ return f;
36115+}
36116diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
8cdd5066 36117index 48bf070..66afec7 100644
5527c038
JR
36118--- a/fs/aufs/loop.h
36119+++ b/fs/aufs/loop.h
36120@@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
36121
36122 int au_loopback_init(void);
36123 void au_loopback_fin(void);
36124+
36125+struct file *aufs_real_loop(struct file *file);
36126 #else
36127+AuStub(struct file *, loop_backing_file, return NULL)
36128+
36129 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
36130 struct dentry *h_adding)
36131 AuStubInt0(au_test_loopback_kthread, void)
36132@@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
36133
36134 AuStubInt0(au_loopback_init, void)
36135 AuStubVoid(au_loopback_fin, void)
36136+
36137+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
36138 #endif /* BLK_DEV_LOOP */
36139
36140 #endif /* __KERNEL__ */
36141diff --git a/fs/aufs/super.c b/fs/aufs/super.c
f0c0a007 36142index 58a773c..75f212c 100644
5527c038
JR
36143--- a/fs/aufs/super.c
36144+++ b/fs/aufs/super.c
f0c0a007 36145@@ -831,7 +831,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
36146 .statfs = aufs_statfs,
36147 .put_super = aufs_put_super,
36148 .sync_fs = aufs_sync_fs,
36149- .remount_fs = aufs_remount_fs
36150+ .remount_fs = aufs_remount_fs,
36151+#ifdef CONFIG_AUFS_BDEV_LOOP
36152+ .real_loop = aufs_real_loop
36153+#endif
36154 };
36155
36156 /* ---------------------------------------------------------------------- */
36157diff --git a/include/linux/fs.h b/include/linux/fs.h
5afbbe0d 36158index 33c6953..f9b06a2 100644
5527c038
JR
36159--- a/include/linux/fs.h
36160+++ b/include/linux/fs.h
5afbbe0d 36161@@ -1805,6 +1805,10 @@ struct super_operations {
5527c038
JR
36162 struct shrink_control *);
36163 long (*free_cached_objects)(struct super_block *,
36164 struct shrink_control *);
36165+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
36166+ /* and aufs */
36167+ struct file *(*real_loop)(struct file *);
36168+#endif
36169 };
36170
36171 /*
This page took 7.088799 seconds and 4 git commands to generate.