]> git.pld-linux.org Git - packages/kernel.git/blame - kernel-aufs4.patch
- disable multiqueue by default; mq layer doesn't support I/O schedulers and lack...
[packages/kernel.git] / kernel-aufs4.patch
CommitLineData
5afbbe0d 1aufs4.x-rcN 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
5afbbe0d 25index 8bdae34..65dbd5f 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
5afbbe0d 36aufs4.x-rcN base patch
7f207e10 37
c1595e42 38diff --git a/MAINTAINERS b/MAINTAINERS
5afbbe0d 39index 952fd2a..6a8f0f8 100644
c1595e42
JR
40--- a/MAINTAINERS
41+++ b/MAINTAINERS
5afbbe0d 42@@ -2210,6 +2210,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
5afbbe0d 268aufs4.x-rcN 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
5afbbe0d 433index cd1f29e..f0c204c 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 */
5afbbe0d 678aufs4.x-rcN 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
5afbbe0d 765index 783004a..44abb2d 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
febd17d6 776@@ -1811,6 +1812,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
5afbbe0d
AM
2867+++ linux/fs/aufs/branch.c 2016-07-25 19:05:34.811159821 +0200
2868@@ -0,0 +1,1406 @@
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);
2922+ kfree(br->br_fhsm);
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();
1facf9fc 2936+ kfree(wbr);
2937+ kfree(br);
2938+}
2939+
2940+/*
2941+ * frees all branches
2942+ */
2943+void au_br_free(struct au_sbinfo *sbinfo)
2944+{
2945+ aufs_bindex_t bmax;
2946+ struct au_branch **br;
2947+
dece6358
AM
2948+ AuRwMustWriteLock(&sbinfo->si_rwsem);
2949+
5afbbe0d 2950+ bmax = sbinfo->si_bbot + 1;
1facf9fc 2951+ br = sbinfo->si_branch;
2952+ while (bmax--)
2953+ au_br_do_free(*br++);
2954+}
2955+
2956+/*
2957+ * find the index of a branch which is specified by @br_id.
2958+ */
2959+int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2960+{
5afbbe0d 2961+ aufs_bindex_t bindex, bbot;
1facf9fc 2962+
5afbbe0d
AM
2963+ bbot = au_sbbot(sb);
2964+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 2965+ if (au_sbr_id(sb, bindex) == br_id)
2966+ return bindex;
2967+ return -1;
2968+}
2969+
2970+/* ---------------------------------------------------------------------- */
2971+
2972+/*
2973+ * add a branch
2974+ */
2975+
b752ccd1
AM
2976+static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2977+ struct dentry *h_root)
1facf9fc 2978+{
b752ccd1
AM
2979+ if (unlikely(h_adding == h_root
2980+ || au_test_loopback_overlap(sb, h_adding)))
1facf9fc 2981+ return 1;
b752ccd1
AM
2982+ if (h_adding->d_sb != h_root->d_sb)
2983+ return 0;
2984+ return au_test_subdir(h_adding, h_root)
2985+ || au_test_subdir(h_root, h_adding);
1facf9fc 2986+}
2987+
2988+/*
2989+ * returns a newly allocated branch. @new_nbranch is a number of branches
2990+ * after adding a branch.
2991+ */
2992+static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
2993+ int perm)
2994+{
2995+ struct au_branch *add_branch;
2996+ struct dentry *root;
5527c038 2997+ struct inode *inode;
4a4d8108 2998+ int err;
1facf9fc 2999+
4a4d8108 3000+ err = -ENOMEM;
1facf9fc 3001+ root = sb->s_root;
be52b249 3002+ add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
1facf9fc 3003+ if (unlikely(!add_branch))
3004+ goto out;
3005+
027c5e7a
AM
3006+ err = au_hnotify_init_br(add_branch, perm);
3007+ if (unlikely(err))
3008+ goto out_br;
3009+
1facf9fc 3010+ if (au_br_writable(perm)) {
3011+ /* may be freed separately at changing the branch permission */
be52b249 3012+ add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
1facf9fc 3013+ GFP_NOFS);
3014+ if (unlikely(!add_branch->br_wbr))
027c5e7a 3015+ goto out_hnotify;
1facf9fc 3016+ }
3017+
076b876e
AM
3018+ if (au_br_fhsm(perm)) {
3019+ err = au_fhsm_br_alloc(add_branch);
3020+ if (unlikely(err))
3021+ goto out_wbr;
3022+ }
3023+
4a4d8108
AM
3024+ err = au_sbr_realloc(au_sbi(sb), new_nbranch);
3025+ if (!err)
3026+ err = au_di_realloc(au_di(root), new_nbranch);
5527c038
JR
3027+ if (!err) {
3028+ inode = d_inode(root);
5afbbe0d 3029+ err = au_hinode_realloc(au_ii(inode), new_nbranch);
5527c038 3030+ }
4a4d8108
AM
3031+ if (!err)
3032+ return add_branch; /* success */
1facf9fc 3033+
076b876e 3034+out_wbr:
1facf9fc 3035+ kfree(add_branch->br_wbr);
027c5e7a
AM
3036+out_hnotify:
3037+ au_hnotify_fin_br(add_branch);
4f0767ce 3038+out_br:
1facf9fc 3039+ kfree(add_branch);
4f0767ce 3040+out:
4a4d8108 3041+ return ERR_PTR(err);
1facf9fc 3042+}
3043+
3044+/*
3045+ * test if the branch permission is legal or not.
3046+ */
3047+static int test_br(struct inode *inode, int brperm, char *path)
3048+{
3049+ int err;
3050+
4a4d8108
AM
3051+ err = (au_br_writable(brperm) && IS_RDONLY(inode));
3052+ if (!err)
3053+ goto out;
1facf9fc 3054+
4a4d8108
AM
3055+ err = -EINVAL;
3056+ pr_err("write permission for readonly mount or inode, %s\n", path);
3057+
4f0767ce 3058+out:
1facf9fc 3059+ return err;
3060+}
3061+
3062+/*
3063+ * returns:
3064+ * 0: success, the caller will add it
3065+ * plus: success, it is already unified, the caller should ignore it
3066+ * minus: error
3067+ */
3068+static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3069+{
3070+ int err;
5afbbe0d 3071+ aufs_bindex_t bbot, bindex;
5527c038 3072+ struct dentry *root, *h_dentry;
1facf9fc 3073+ struct inode *inode, *h_inode;
3074+
3075+ root = sb->s_root;
5afbbe0d
AM
3076+ bbot = au_sbbot(sb);
3077+ if (unlikely(bbot >= 0
1facf9fc 3078+ && au_find_dbindex(root, add->path.dentry) >= 0)) {
3079+ err = 1;
3080+ if (!remount) {
3081+ err = -EINVAL;
4a4d8108 3082+ pr_err("%s duplicated\n", add->pathname);
1facf9fc 3083+ }
3084+ goto out;
3085+ }
3086+
3087+ err = -ENOSPC; /* -E2BIG; */
3088+ if (unlikely(AUFS_BRANCH_MAX <= add->bindex
5afbbe0d 3089+ || AUFS_BRANCH_MAX - 1 <= bbot)) {
4a4d8108 3090+ pr_err("number of branches exceeded %s\n", add->pathname);
1facf9fc 3091+ goto out;
3092+ }
3093+
3094+ err = -EDOM;
5afbbe0d 3095+ if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
4a4d8108 3096+ pr_err("bad index %d\n", add->bindex);
1facf9fc 3097+ goto out;
3098+ }
3099+
5527c038 3100+ inode = d_inode(add->path.dentry);
1facf9fc 3101+ err = -ENOENT;
3102+ if (unlikely(!inode->i_nlink)) {
4a4d8108 3103+ pr_err("no existence %s\n", add->pathname);
1facf9fc 3104+ goto out;
3105+ }
3106+
3107+ err = -EINVAL;
3108+ if (unlikely(inode->i_sb == sb)) {
4a4d8108 3109+ pr_err("%s must be outside\n", add->pathname);
1facf9fc 3110+ goto out;
3111+ }
3112+
3113+ if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
4a4d8108
AM
3114+ pr_err("unsupported filesystem, %s (%s)\n",
3115+ add->pathname, au_sbtype(inode->i_sb));
1facf9fc 3116+ goto out;
3117+ }
3118+
c1595e42
JR
3119+ if (unlikely(inode->i_sb->s_stack_depth)) {
3120+ pr_err("already stacked, %s (%s)\n",
3121+ add->pathname, au_sbtype(inode->i_sb));
3122+ goto out;
3123+ }
3124+
5527c038 3125+ err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
1facf9fc 3126+ if (unlikely(err))
3127+ goto out;
3128+
5afbbe0d 3129+ if (bbot < 0)
1facf9fc 3130+ return 0; /* success */
3131+
3132+ err = -EINVAL;
5afbbe0d 3133+ for (bindex = 0; bindex <= bbot; bindex++)
1facf9fc 3134+ if (unlikely(test_overlap(sb, add->path.dentry,
3135+ au_h_dptr(root, bindex)))) {
4a4d8108 3136+ pr_err("%s is overlapped\n", add->pathname);
1facf9fc 3137+ goto out;
3138+ }
3139+
3140+ err = 0;
3141+ if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
5527c038
JR
3142+ h_dentry = au_h_dptr(root, 0);
3143+ h_inode = d_inode(h_dentry);
1facf9fc 3144+ if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
0c3ec466
AM
3145+ || !uid_eq(h_inode->i_uid, inode->i_uid)
3146+ || !gid_eq(h_inode->i_gid, inode->i_gid))
3147+ pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3148+ add->pathname,
3149+ i_uid_read(inode), i_gid_read(inode),
3150+ (inode->i_mode & S_IALLUGO),
3151+ i_uid_read(h_inode), i_gid_read(h_inode),
3152+ (h_inode->i_mode & S_IALLUGO));
1facf9fc 3153+ }
3154+
4f0767ce 3155+out:
1facf9fc 3156+ return err;
3157+}
3158+
3159+/*
3160+ * initialize or clean the whiteouts for an adding branch
3161+ */
3162+static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
86dc4139 3163+ int new_perm)
1facf9fc 3164+{
3165+ int err, old_perm;
3166+ aufs_bindex_t bindex;
febd17d6 3167+ struct inode *h_inode;
1facf9fc 3168+ struct au_wbr *wbr;
3169+ struct au_hinode *hdir;
5527c038 3170+ struct dentry *h_dentry;
1facf9fc 3171+
86dc4139
AM
3172+ err = vfsub_mnt_want_write(au_br_mnt(br));
3173+ if (unlikely(err))
3174+ goto out;
3175+
1facf9fc 3176+ wbr = br->br_wbr;
3177+ old_perm = br->br_perm;
3178+ br->br_perm = new_perm;
3179+ hdir = NULL;
febd17d6 3180+ h_inode = NULL;
1facf9fc 3181+ bindex = au_br_index(sb, br->br_id);
3182+ if (0 <= bindex) {
5527c038 3183+ hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 3184+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 3185+ } else {
5527c038 3186+ h_dentry = au_br_dentry(br);
febd17d6
JR
3187+ h_inode = d_inode(h_dentry);
3188+ inode_lock_nested(h_inode, AuLsc_I_PARENT);
1facf9fc 3189+ }
3190+ if (!wbr)
86dc4139 3191+ err = au_wh_init(br, sb);
1facf9fc 3192+ else {
3193+ wbr_wh_write_lock(wbr);
86dc4139 3194+ err = au_wh_init(br, sb);
1facf9fc 3195+ wbr_wh_write_unlock(wbr);
3196+ }
3197+ if (hdir)
5afbbe0d 3198+ au_hn_inode_unlock(hdir);
1facf9fc 3199+ else
febd17d6 3200+ inode_unlock(h_inode);
86dc4139 3201+ vfsub_mnt_drop_write(au_br_mnt(br));
1facf9fc 3202+ br->br_perm = old_perm;
3203+
3204+ if (!err && wbr && !au_br_writable(new_perm)) {
3205+ kfree(wbr);
3206+ br->br_wbr = NULL;
3207+ }
3208+
86dc4139 3209+out:
1facf9fc 3210+ return err;
3211+}
3212+
3213+static int au_wbr_init(struct au_branch *br, struct super_block *sb,
86dc4139 3214+ int perm)
1facf9fc 3215+{
3216+ int err;
4a4d8108 3217+ struct kstatfs kst;
1facf9fc 3218+ struct au_wbr *wbr;
3219+
3220+ wbr = br->br_wbr;
dece6358 3221+ au_rw_init(&wbr->wbr_wh_rwsem);
1facf9fc 3222+ atomic_set(&wbr->wbr_wh_running, 0);
1facf9fc 3223+
4a4d8108
AM
3224+ /*
3225+ * a limit for rmdir/rename a dir
523b37e3 3226+ * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
4a4d8108 3227+ */
86dc4139 3228+ err = vfs_statfs(&br->br_path, &kst);
4a4d8108
AM
3229+ if (unlikely(err))
3230+ goto out;
3231+ err = -EINVAL;
3232+ if (kst.f_namelen >= NAME_MAX)
86dc4139 3233+ err = au_br_init_wh(sb, br, perm);
4a4d8108 3234+ else
523b37e3
AM
3235+ pr_err("%pd(%s), unsupported namelen %ld\n",
3236+ au_br_dentry(br),
86dc4139 3237+ au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
1facf9fc 3238+
4f0767ce 3239+out:
1facf9fc 3240+ return err;
3241+}
3242+
c1595e42 3243+/* initialize a new branch */
1facf9fc 3244+static int au_br_init(struct au_branch *br, struct super_block *sb,
3245+ struct au_opt_add *add)
3246+{
3247+ int err;
5527c038 3248+ struct inode *h_inode;
1facf9fc 3249+
3250+ err = 0;
1facf9fc 3251+ mutex_init(&br->br_xino.xi_nondir_mtx);
3252+ br->br_perm = add->perm;
86dc4139 3253+ br->br_path = add->path; /* set first, path_get() later */
4a4d8108 3254+ spin_lock_init(&br->br_dykey_lock);
5afbbe0d 3255+ au_br_count_init(br);
1facf9fc 3256+ atomic_set(&br->br_xino_running, 0);
3257+ br->br_id = au_new_br_id(sb);
7f207e10 3258+ AuDebugOn(br->br_id < 0);
1facf9fc 3259+
3260+ if (au_br_writable(add->perm)) {
86dc4139 3261+ err = au_wbr_init(br, sb, add->perm);
1facf9fc 3262+ if (unlikely(err))
b752ccd1 3263+ goto out_err;
1facf9fc 3264+ }
3265+
3266+ if (au_opt_test(au_mntflags(sb), XINO)) {
5527c038
JR
3267+ h_inode = d_inode(add->path.dentry);
3268+ err = au_xino_br(sb, br, h_inode->i_ino,
1facf9fc 3269+ au_sbr(sb, 0)->br_xino.xi_file, /*do_test*/1);
3270+ if (unlikely(err)) {
3271+ AuDebugOn(br->br_xino.xi_file);
b752ccd1 3272+ goto out_err;
1facf9fc 3273+ }
3274+ }
3275+
3276+ sysaufs_br_init(br);
86dc4139 3277+ path_get(&br->br_path);
b752ccd1 3278+ goto out; /* success */
1facf9fc 3279+
4f0767ce 3280+out_err:
86dc4139 3281+ memset(&br->br_path, 0, sizeof(br->br_path));
4f0767ce 3282+out:
1facf9fc 3283+ return err;
3284+}
3285+
3286+static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
5afbbe0d 3287+ struct au_branch *br, aufs_bindex_t bbot,
1facf9fc 3288+ aufs_bindex_t amount)
3289+{
3290+ struct au_branch **brp;
3291+
dece6358
AM
3292+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3293+
1facf9fc 3294+ brp = sbinfo->si_branch + bindex;
3295+ memmove(brp + 1, brp, sizeof(*brp) * amount);
3296+ *brp = br;
5afbbe0d
AM
3297+ sbinfo->si_bbot++;
3298+ if (unlikely(bbot < 0))
3299+ sbinfo->si_bbot = 0;
1facf9fc 3300+}
3301+
3302+static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
5afbbe0d 3303+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3304+{
3305+ struct au_hdentry *hdp;
3306+
1308ab2a 3307+ AuRwMustWriteLock(&dinfo->di_rwsem);
3308+
5afbbe0d 3309+ hdp = au_hdentry(dinfo, bindex);
1facf9fc 3310+ memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3311+ au_h_dentry_init(hdp);
5afbbe0d
AM
3312+ dinfo->di_bbot++;
3313+ if (unlikely(bbot < 0))
3314+ dinfo->di_btop = 0;
1facf9fc 3315+}
3316+
3317+static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
5afbbe0d 3318+ aufs_bindex_t bbot, aufs_bindex_t amount)
1facf9fc 3319+{
3320+ struct au_hinode *hip;
3321+
1308ab2a 3322+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3323+
5afbbe0d 3324+ hip = au_hinode(iinfo, bindex);
1facf9fc 3325+ memmove(hip + 1, hip, sizeof(*hip) * amount);
5afbbe0d
AM
3326+ au_hinode_init(hip);
3327+ iinfo->ii_bbot++;
3328+ if (unlikely(bbot < 0))
3329+ iinfo->ii_btop = 0;
1facf9fc 3330+}
3331+
86dc4139
AM
3332+static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3333+ aufs_bindex_t bindex)
1facf9fc 3334+{
86dc4139 3335+ struct dentry *root, *h_dentry;
5527c038 3336+ struct inode *root_inode, *h_inode;
5afbbe0d 3337+ aufs_bindex_t bbot, amount;
1facf9fc 3338+
3339+ root = sb->s_root;
5527c038 3340+ root_inode = d_inode(root);
5afbbe0d
AM
3341+ bbot = au_sbbot(sb);
3342+ amount = bbot + 1 - bindex;
86dc4139 3343+ h_dentry = au_br_dentry(br);
53392da6 3344+ au_sbilist_lock();
5afbbe0d
AM
3345+ au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3346+ au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3347+ au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
1facf9fc 3348+ au_set_h_dptr(root, bindex, dget(h_dentry));
5527c038
JR
3349+ h_inode = d_inode(h_dentry);
3350+ au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
53392da6 3351+ au_sbilist_unlock();
1facf9fc 3352+}
3353+
3354+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3355+{
3356+ int err;
5afbbe0d 3357+ aufs_bindex_t bbot, add_bindex;
1facf9fc 3358+ struct dentry *root, *h_dentry;
3359+ struct inode *root_inode;
3360+ struct au_branch *add_branch;
3361+
3362+ root = sb->s_root;
5527c038 3363+ root_inode = d_inode(root);
1facf9fc 3364+ IMustLock(root_inode);
5afbbe0d 3365+ IiMustWriteLock(root_inode);
1facf9fc 3366+ err = test_add(sb, add, remount);
3367+ if (unlikely(err < 0))
3368+ goto out;
3369+ if (err) {
3370+ err = 0;
3371+ goto out; /* success */
3372+ }
3373+
5afbbe0d
AM
3374+ bbot = au_sbbot(sb);
3375+ add_branch = au_br_alloc(sb, bbot + 2, add->perm);
1facf9fc 3376+ err = PTR_ERR(add_branch);
3377+ if (IS_ERR(add_branch))
3378+ goto out;
3379+
3380+ err = au_br_init(add_branch, sb, add);
3381+ if (unlikely(err)) {
3382+ au_br_do_free(add_branch);
3383+ goto out;
3384+ }
3385+
3386+ add_bindex = add->bindex;
1facf9fc 3387+ if (!remount)
86dc4139 3388+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3389+ else {
3390+ sysaufs_brs_del(sb, add_bindex);
86dc4139 3391+ au_br_do_add(sb, add_branch, add_bindex);
1facf9fc 3392+ sysaufs_brs_add(sb, add_bindex);
3393+ }
3394+
86dc4139 3395+ h_dentry = add->path.dentry;
1308ab2a 3396+ if (!add_bindex) {
1facf9fc 3397+ au_cpup_attr_all(root_inode, /*force*/1);
1308ab2a 3398+ sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3399+ } else
5527c038 3400+ au_add_nlink(root_inode, d_inode(h_dentry));
1facf9fc 3401+
3402+ /*
4a4d8108 3403+ * this test/set prevents aufs from handling unnecesary notify events
027c5e7a 3404+ * of xino files, in case of re-adding a writable branch which was
1facf9fc 3405+ * once detached from aufs.
3406+ */
3407+ if (au_xino_brid(sb) < 0
3408+ && au_br_writable(add_branch->br_perm)
3409+ && !au_test_fs_bad_xino(h_dentry->d_sb)
3410+ && add_branch->br_xino.xi_file
2000de60 3411+ && add_branch->br_xino.xi_file->f_path.dentry->d_parent == h_dentry)
1facf9fc 3412+ au_xino_brid_set(sb, add_branch->br_id);
3413+
4f0767ce 3414+out:
1facf9fc 3415+ return err;
3416+}
3417+
3418+/* ---------------------------------------------------------------------- */
3419+
79b8bda9 3420+static unsigned long long au_farray_cb(struct super_block *sb, void *a,
076b876e
AM
3421+ unsigned long long max __maybe_unused,
3422+ void *arg)
3423+{
3424+ unsigned long long n;
3425+ struct file **p, *f;
3426+ struct au_sphlhead *files;
3427+ struct au_finfo *finfo;
076b876e
AM
3428+
3429+ n = 0;
3430+ p = a;
3431+ files = &au_sbi(sb)->si_files;
3432+ spin_lock(&files->spin);
3433+ hlist_for_each_entry(finfo, &files->head, fi_hlist) {
3434+ f = finfo->fi_file;
3435+ if (file_count(f)
3436+ && !special_file(file_inode(f)->i_mode)) {
3437+ get_file(f);
3438+ *p++ = f;
3439+ n++;
3440+ AuDebugOn(n > max);
3441+ }
3442+ }
3443+ spin_unlock(&files->spin);
3444+
3445+ return n;
3446+}
3447+
3448+static struct file **au_farray_alloc(struct super_block *sb,
3449+ unsigned long long *max)
3450+{
5afbbe0d 3451+ *max = au_nfiles(sb);
79b8bda9 3452+ return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
076b876e
AM
3453+}
3454+
3455+static void au_farray_free(struct file **a, unsigned long long max)
3456+{
3457+ unsigned long long ull;
3458+
3459+ for (ull = 0; ull < max; ull++)
3460+ if (a[ull])
3461+ fput(a[ull]);
be52b249 3462+ kvfree(a);
076b876e
AM
3463+}
3464+
3465+/* ---------------------------------------------------------------------- */
3466+
1facf9fc 3467+/*
3468+ * delete a branch
3469+ */
3470+
3471+/* to show the line number, do not make it inlined function */
4a4d8108 3472+#define AuVerbose(do_info, fmt, ...) do { \
1facf9fc 3473+ if (do_info) \
4a4d8108 3474+ pr_info(fmt, ##__VA_ARGS__); \
1facf9fc 3475+} while (0)
3476+
5afbbe0d
AM
3477+static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3478+ aufs_bindex_t bbot)
027c5e7a 3479+{
5afbbe0d 3480+ return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
027c5e7a
AM
3481+}
3482+
5afbbe0d
AM
3483+static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3484+ aufs_bindex_t bbot)
027c5e7a 3485+{
5afbbe0d 3486+ return au_test_ibusy(d_inode(dentry), btop, bbot);
027c5e7a
AM
3487+}
3488+
1facf9fc 3489+/*
3490+ * test if the branch is deletable or not.
3491+ */
3492+static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
b752ccd1 3493+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3494+{
3495+ int err, i, j, ndentry;
5afbbe0d 3496+ aufs_bindex_t btop, bbot;
1facf9fc 3497+ struct au_dcsub_pages dpages;
3498+ struct au_dpage *dpage;
3499+ struct dentry *d;
1facf9fc 3500+
3501+ err = au_dpages_init(&dpages, GFP_NOFS);
3502+ if (unlikely(err))
3503+ goto out;
3504+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
3505+ if (unlikely(err))
3506+ goto out_dpages;
3507+
1facf9fc 3508+ for (i = 0; !err && i < dpages.ndpage; i++) {
3509+ dpage = dpages.dpages + i;
3510+ ndentry = dpage->ndentry;
3511+ for (j = 0; !err && j < ndentry; j++) {
3512+ d = dpage->dentries[j];
c1595e42 3513+ AuDebugOn(au_dcount(d) <= 0);
027c5e7a 3514+ if (!au_digen_test(d, sigen)) {
1facf9fc 3515+ di_read_lock_child(d, AuLock_IR);
027c5e7a
AM
3516+ if (unlikely(au_dbrange_test(d))) {
3517+ di_read_unlock(d, AuLock_IR);
3518+ continue;
3519+ }
3520+ } else {
1facf9fc 3521+ di_write_lock_child(d);
027c5e7a
AM
3522+ if (unlikely(au_dbrange_test(d))) {
3523+ di_write_unlock(d);
3524+ continue;
3525+ }
1facf9fc 3526+ err = au_reval_dpath(d, sigen);
3527+ if (!err)
3528+ di_downgrade_lock(d, AuLock_IR);
3529+ else {
3530+ di_write_unlock(d);
3531+ break;
3532+ }
3533+ }
3534+
027c5e7a 3535+ /* AuDbgDentry(d); */
5afbbe0d
AM
3536+ btop = au_dbtop(d);
3537+ bbot = au_dbbot(d);
3538+ if (btop <= bindex
3539+ && bindex <= bbot
1facf9fc 3540+ && au_h_dptr(d, bindex)
5afbbe0d 3541+ && au_test_dbusy(d, btop, bbot)) {
1facf9fc 3542+ err = -EBUSY;
523b37e3 3543+ AuVerbose(verbose, "busy %pd\n", d);
027c5e7a 3544+ AuDbgDentry(d);
1facf9fc 3545+ }
3546+ di_read_unlock(d, AuLock_IR);
3547+ }
3548+ }
3549+
4f0767ce 3550+out_dpages:
1facf9fc 3551+ au_dpages_free(&dpages);
4f0767ce 3552+out:
1facf9fc 3553+ return err;
3554+}
3555+
3556+static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
b752ccd1 3557+ unsigned int sigen, const unsigned int verbose)
1facf9fc 3558+{
3559+ int err;
7f207e10
AM
3560+ unsigned long long max, ull;
3561+ struct inode *i, **array;
5afbbe0d 3562+ aufs_bindex_t btop, bbot;
1facf9fc 3563+
7f207e10
AM
3564+ array = au_iarray_alloc(sb, &max);
3565+ err = PTR_ERR(array);
3566+ if (IS_ERR(array))
3567+ goto out;
3568+
1facf9fc 3569+ err = 0;
7f207e10
AM
3570+ AuDbg("b%d\n", bindex);
3571+ for (ull = 0; !err && ull < max; ull++) {
3572+ i = array[ull];
076b876e
AM
3573+ if (unlikely(!i))
3574+ break;
7f207e10 3575+ if (i->i_ino == AUFS_ROOT_INO)
1facf9fc 3576+ continue;
3577+
7f207e10 3578+ /* AuDbgInode(i); */
537831f9 3579+ if (au_iigen(i, NULL) == sigen)
1facf9fc 3580+ ii_read_lock_child(i);
3581+ else {
3582+ ii_write_lock_child(i);
027c5e7a
AM
3583+ err = au_refresh_hinode_self(i);
3584+ au_iigen_dec(i);
1facf9fc 3585+ if (!err)
3586+ ii_downgrade_lock(i);
3587+ else {
3588+ ii_write_unlock(i);
3589+ break;
3590+ }
3591+ }
3592+
5afbbe0d
AM
3593+ btop = au_ibtop(i);
3594+ bbot = au_ibbot(i);
3595+ if (btop <= bindex
3596+ && bindex <= bbot
1facf9fc 3597+ && au_h_iptr(i, bindex)
5afbbe0d 3598+ && au_test_ibusy(i, btop, bbot)) {
1facf9fc 3599+ err = -EBUSY;
3600+ AuVerbose(verbose, "busy i%lu\n", i->i_ino);
7f207e10 3601+ AuDbgInode(i);
1facf9fc 3602+ }
3603+ ii_read_unlock(i);
3604+ }
7f207e10 3605+ au_iarray_free(array, max);
1facf9fc 3606+
7f207e10 3607+out:
1facf9fc 3608+ return err;
3609+}
3610+
b752ccd1
AM
3611+static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3612+ const unsigned int verbose)
1facf9fc 3613+{
3614+ int err;
3615+ unsigned int sigen;
3616+
3617+ sigen = au_sigen(root->d_sb);
3618+ DiMustNoWaiters(root);
5527c038 3619+ IiMustNoWaiters(d_inode(root));
1facf9fc 3620+ di_write_unlock(root);
b752ccd1 3621+ err = test_dentry_busy(root, bindex, sigen, verbose);
1facf9fc 3622+ if (!err)
b752ccd1 3623+ err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
1facf9fc 3624+ di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3625+
3626+ return err;
3627+}
3628+
076b876e
AM
3629+static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3630+ struct file **to_free, int *idx)
3631+{
3632+ int err;
c1595e42 3633+ unsigned char matched, root;
5afbbe0d 3634+ aufs_bindex_t bindex, bbot;
076b876e
AM
3635+ struct au_fidir *fidir;
3636+ struct au_hfile *hfile;
3637+
3638+ err = 0;
2000de60 3639+ root = IS_ROOT(file->f_path.dentry);
c1595e42
JR
3640+ if (root) {
3641+ get_file(file);
3642+ to_free[*idx] = file;
3643+ (*idx)++;
3644+ goto out;
3645+ }
3646+
076b876e 3647+ matched = 0;
076b876e
AM
3648+ fidir = au_fi(file)->fi_hdir;
3649+ AuDebugOn(!fidir);
5afbbe0d
AM
3650+ bbot = au_fbbot_dir(file);
3651+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
076b876e
AM
3652+ hfile = fidir->fd_hfile + bindex;
3653+ if (!hfile->hf_file)
3654+ continue;
3655+
c1595e42 3656+ if (hfile->hf_br->br_id == br_id) {
076b876e 3657+ matched = 1;
076b876e 3658+ break;
c1595e42 3659+ }
076b876e 3660+ }
c1595e42 3661+ if (matched)
076b876e
AM
3662+ err = -EBUSY;
3663+
3664+out:
3665+ return err;
3666+}
3667+
3668+static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3669+ struct file **to_free, int opened)
3670+{
3671+ int err, idx;
3672+ unsigned long long ull, max;
5afbbe0d 3673+ aufs_bindex_t btop;
076b876e 3674+ struct file *file, **array;
076b876e
AM
3675+ struct dentry *root;
3676+ struct au_hfile *hfile;
3677+
3678+ array = au_farray_alloc(sb, &max);
3679+ err = PTR_ERR(array);
3680+ if (IS_ERR(array))
3681+ goto out;
3682+
3683+ err = 0;
3684+ idx = 0;
3685+ root = sb->s_root;
3686+ di_write_unlock(root);
3687+ for (ull = 0; ull < max; ull++) {
3688+ file = array[ull];
3689+ if (unlikely(!file))
3690+ break;
3691+
3692+ /* AuDbg("%pD\n", file); */
3693+ fi_read_lock(file);
5afbbe0d 3694+ btop = au_fbtop(file);
2000de60 3695+ if (!d_is_dir(file->f_path.dentry)) {
076b876e
AM
3696+ hfile = &au_fi(file)->fi_htop;
3697+ if (hfile->hf_br->br_id == br_id)
3698+ err = -EBUSY;
3699+ } else
3700+ err = test_dir_busy(file, br_id, to_free, &idx);
3701+ fi_read_unlock(file);
3702+ if (unlikely(err))
3703+ break;
3704+ }
3705+ di_write_lock_child(root);
3706+ au_farray_free(array, max);
3707+ AuDebugOn(idx > opened);
3708+
3709+out:
3710+ return err;
3711+}
3712+
3713+static void br_del_file(struct file **to_free, unsigned long long opened,
3714+ aufs_bindex_t br_id)
3715+{
3716+ unsigned long long ull;
5afbbe0d 3717+ aufs_bindex_t bindex, btop, bbot, bfound;
076b876e
AM
3718+ struct file *file;
3719+ struct au_fidir *fidir;
3720+ struct au_hfile *hfile;
3721+
3722+ for (ull = 0; ull < opened; ull++) {
3723+ file = to_free[ull];
3724+ if (unlikely(!file))
3725+ break;
3726+
3727+ /* AuDbg("%pD\n", file); */
2000de60 3728+ AuDebugOn(!d_is_dir(file->f_path.dentry));
076b876e
AM
3729+ bfound = -1;
3730+ fidir = au_fi(file)->fi_hdir;
3731+ AuDebugOn(!fidir);
3732+ fi_write_lock(file);
5afbbe0d
AM
3733+ btop = au_fbtop(file);
3734+ bbot = au_fbbot_dir(file);
3735+ for (bindex = btop; bindex <= bbot; bindex++) {
076b876e
AM
3736+ hfile = fidir->fd_hfile + bindex;
3737+ if (!hfile->hf_file)
3738+ continue;
3739+
3740+ if (hfile->hf_br->br_id == br_id) {
3741+ bfound = bindex;
3742+ break;
3743+ }
3744+ }
3745+ AuDebugOn(bfound < 0);
3746+ au_set_h_fptr(file, bfound, NULL);
5afbbe0d
AM
3747+ if (bfound == btop) {
3748+ for (btop++; btop <= bbot; btop++)
3749+ if (au_hf_dir(file, btop)) {
3750+ au_set_fbtop(file, btop);
076b876e
AM
3751+ break;
3752+ }
3753+ }
3754+ fi_write_unlock(file);
3755+ }
3756+}
3757+
1facf9fc 3758+static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3759+ const aufs_bindex_t bindex,
5afbbe0d 3760+ const aufs_bindex_t bbot)
1facf9fc 3761+{
3762+ struct au_branch **brp, **p;
3763+
dece6358
AM
3764+ AuRwMustWriteLock(&sbinfo->si_rwsem);
3765+
1facf9fc 3766+ brp = sbinfo->si_branch + bindex;
5afbbe0d
AM
3767+ if (bindex < bbot)
3768+ memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3769+ sbinfo->si_branch[0 + bbot] = NULL;
3770+ sbinfo->si_bbot--;
1facf9fc 3771+
5afbbe0d 3772+ p = krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST);
1facf9fc 3773+ if (p)
3774+ sbinfo->si_branch = p;
4a4d8108 3775+ /* harmless error */
1facf9fc 3776+}
3777+
3778+static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
5afbbe0d 3779+ const aufs_bindex_t bbot)
1facf9fc 3780+{
3781+ struct au_hdentry *hdp, *p;
3782+
1308ab2a 3783+ AuRwMustWriteLock(&dinfo->di_rwsem);
3784+
5afbbe0d
AM
3785+ hdp = au_hdentry(dinfo, bindex);
3786+ if (bindex < bbot)
3787+ memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3788+ /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3789+ dinfo->di_bbot--;
1facf9fc 3790+
5afbbe0d 3791+ p = krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST);
1facf9fc 3792+ if (p)
3793+ dinfo->di_hdentry = p;
4a4d8108 3794+ /* harmless error */
1facf9fc 3795+}
3796+
3797+static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
5afbbe0d 3798+ const aufs_bindex_t bbot)
1facf9fc 3799+{
3800+ struct au_hinode *hip, *p;
3801+
1308ab2a 3802+ AuRwMustWriteLock(&iinfo->ii_rwsem);
3803+
5afbbe0d
AM
3804+ hip = au_hinode(iinfo, bindex);
3805+ if (bindex < bbot)
3806+ memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3807+ /* au_hinode_init(au_hinode(iinfo, bbot)); */
3808+ iinfo->ii_bbot--;
1facf9fc 3809+
5afbbe0d 3810+ p = krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST);
1facf9fc 3811+ if (p)
3812+ iinfo->ii_hinode = p;
4a4d8108 3813+ /* harmless error */
1facf9fc 3814+}
3815+
3816+static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3817+ struct au_branch *br)
3818+{
5afbbe0d 3819+ aufs_bindex_t bbot;
1facf9fc 3820+ struct au_sbinfo *sbinfo;
53392da6
AM
3821+ struct dentry *root, *h_root;
3822+ struct inode *inode, *h_inode;
3823+ struct au_hinode *hinode;
1facf9fc 3824+
dece6358
AM
3825+ SiMustWriteLock(sb);
3826+
1facf9fc 3827+ root = sb->s_root;
5527c038 3828+ inode = d_inode(root);
1facf9fc 3829+ sbinfo = au_sbi(sb);
5afbbe0d 3830+ bbot = sbinfo->si_bbot;
1facf9fc 3831+
53392da6
AM
3832+ h_root = au_h_dptr(root, bindex);
3833+ hinode = au_hi(inode, bindex);
3834+ h_inode = au_igrab(hinode->hi_inode);
3835+ au_hiput(hinode);
1facf9fc 3836+
53392da6 3837+ au_sbilist_lock();
5afbbe0d
AM
3838+ au_br_do_del_brp(sbinfo, bindex, bbot);
3839+ au_br_do_del_hdp(au_di(root), bindex, bbot);
3840+ au_br_do_del_hip(au_ii(inode), bindex, bbot);
53392da6
AM
3841+ au_sbilist_unlock();
3842+
3843+ dput(h_root);
3844+ iput(h_inode);
3845+ au_br_do_free(br);
1facf9fc 3846+}
3847+
79b8bda9
AM
3848+static unsigned long long empty_cb(struct super_block *sb, void *array,
3849+ unsigned long long max, void *arg)
076b876e
AM
3850+{
3851+ return max;
3852+}
3853+
1facf9fc 3854+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3855+{
3856+ int err, rerr, i;
076b876e 3857+ unsigned long long opened;
1facf9fc 3858+ unsigned int mnt_flags;
5afbbe0d 3859+ aufs_bindex_t bindex, bbot, br_id;
1facf9fc 3860+ unsigned char do_wh, verbose;
3861+ struct au_branch *br;
3862+ struct au_wbr *wbr;
076b876e
AM
3863+ struct dentry *root;
3864+ struct file **to_free;
1facf9fc 3865+
3866+ err = 0;
076b876e
AM
3867+ opened = 0;
3868+ to_free = NULL;
3869+ root = sb->s_root;
3870+ bindex = au_find_dbindex(root, del->h_path.dentry);
1facf9fc 3871+ if (bindex < 0) {
3872+ if (remount)
3873+ goto out; /* success */
3874+ err = -ENOENT;
4a4d8108 3875+ pr_err("%s no such branch\n", del->pathname);
1facf9fc 3876+ goto out;
3877+ }
3878+ AuDbg("bindex b%d\n", bindex);
3879+
3880+ err = -EBUSY;
3881+ mnt_flags = au_mntflags(sb);
3882+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
5afbbe0d
AM
3883+ bbot = au_sbbot(sb);
3884+ if (unlikely(!bbot)) {
1facf9fc 3885+ AuVerbose(verbose, "no more branches left\n");
3886+ goto out;
3887+ }
3888+ br = au_sbr(sb, bindex);
86dc4139 3889+ AuDebugOn(!path_equal(&br->br_path, &del->h_path));
076b876e
AM
3890+
3891+ br_id = br->br_id;
5afbbe0d 3892+ opened = au_br_count(br);
076b876e 3893+ if (unlikely(opened)) {
79b8bda9 3894+ to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
076b876e
AM
3895+ err = PTR_ERR(to_free);
3896+ if (IS_ERR(to_free))
3897+ goto out;
3898+
3899+ err = test_file_busy(sb, br_id, to_free, opened);
3900+ if (unlikely(err)) {
3901+ AuVerbose(verbose, "%llu file(s) opened\n", opened);
3902+ goto out;
3903+ }
1facf9fc 3904+ }
3905+
3906+ wbr = br->br_wbr;
3907+ do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3908+ if (do_wh) {
1308ab2a 3909+ /* instead of WbrWhMustWriteLock(wbr) */
3910+ SiMustWriteLock(sb);
1facf9fc 3911+ for (i = 0; i < AuBrWh_Last; i++) {
3912+ dput(wbr->wbr_wh[i]);
3913+ wbr->wbr_wh[i] = NULL;
3914+ }
3915+ }
3916+
076b876e 3917+ err = test_children_busy(root, bindex, verbose);
1facf9fc 3918+ if (unlikely(err)) {
3919+ if (do_wh)
3920+ goto out_wh;
3921+ goto out;
3922+ }
3923+
3924+ err = 0;
076b876e
AM
3925+ if (to_free) {
3926+ /*
3927+ * now we confirmed the branch is deletable.
3928+ * let's free the remaining opened dirs on the branch.
3929+ */
3930+ di_write_unlock(root);
3931+ br_del_file(to_free, opened, br_id);
3932+ di_write_lock_child(root);
3933+ }
3934+
1facf9fc 3935+ if (!remount)
3936+ au_br_do_del(sb, bindex, br);
3937+ else {
3938+ sysaufs_brs_del(sb, bindex);
3939+ au_br_do_del(sb, bindex, br);
3940+ sysaufs_brs_add(sb, bindex);
3941+ }
3942+
1308ab2a 3943+ if (!bindex) {
5527c038 3944+ au_cpup_attr_all(d_inode(root), /*force*/1);
1308ab2a 3945+ sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3946+ } else
5527c038 3947+ au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
1facf9fc 3948+ if (au_opt_test(mnt_flags, PLINK))
3949+ au_plink_half_refresh(sb, br_id);
3950+
b752ccd1 3951+ if (au_xino_brid(sb) == br_id)
1facf9fc 3952+ au_xino_brid_set(sb, -1);
3953+ goto out; /* success */
3954+
4f0767ce 3955+out_wh:
1facf9fc 3956+ /* revert */
86dc4139 3957+ rerr = au_br_init_wh(sb, br, br->br_perm);
1facf9fc 3958+ if (rerr)
0c3ec466
AM
3959+ pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3960+ del->pathname, rerr);
4f0767ce 3961+out:
076b876e
AM
3962+ if (to_free)
3963+ au_farray_free(to_free, opened);
1facf9fc 3964+ return err;
3965+}
3966+
3967+/* ---------------------------------------------------------------------- */
3968+
027c5e7a
AM
3969+static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3970+{
3971+ int err;
5afbbe0d 3972+ aufs_bindex_t btop, bbot;
027c5e7a
AM
3973+ struct aufs_ibusy ibusy;
3974+ struct inode *inode, *h_inode;
3975+
3976+ err = -EPERM;
3977+ if (unlikely(!capable(CAP_SYS_ADMIN)))
3978+ goto out;
3979+
3980+ err = copy_from_user(&ibusy, arg, sizeof(ibusy));
3981+ if (!err)
3982+ err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
3983+ if (unlikely(err)) {
3984+ err = -EFAULT;
3985+ AuTraceErr(err);
3986+ goto out;
3987+ }
3988+
3989+ err = -EINVAL;
3990+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d 3991+ if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
027c5e7a
AM
3992+ goto out_unlock;
3993+
3994+ err = 0;
3995+ ibusy.h_ino = 0; /* invalid */
3996+ inode = ilookup(sb, ibusy.ino);
3997+ if (!inode
3998+ || inode->i_ino == AUFS_ROOT_INO
5afbbe0d 3999+ || au_is_bad_inode(inode))
027c5e7a
AM
4000+ goto out_unlock;
4001+
4002+ ii_read_lock_child(inode);
5afbbe0d
AM
4003+ btop = au_ibtop(inode);
4004+ bbot = au_ibbot(inode);
4005+ if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
027c5e7a 4006+ h_inode = au_h_iptr(inode, ibusy.bindex);
5afbbe0d 4007+ if (h_inode && au_test_ibusy(inode, btop, bbot))
027c5e7a
AM
4008+ ibusy.h_ino = h_inode->i_ino;
4009+ }
4010+ ii_read_unlock(inode);
4011+ iput(inode);
4012+
4013+out_unlock:
4014+ si_read_unlock(sb);
4015+ if (!err) {
4016+ err = __put_user(ibusy.h_ino, &arg->h_ino);
4017+ if (unlikely(err)) {
4018+ err = -EFAULT;
4019+ AuTraceErr(err);
4020+ }
4021+ }
4022+out:
4023+ return err;
4024+}
4025+
4026+long au_ibusy_ioctl(struct file *file, unsigned long arg)
4027+{
2000de60 4028+ return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
027c5e7a
AM
4029+}
4030+
4031+#ifdef CONFIG_COMPAT
4032+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4033+{
2000de60 4034+ return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
027c5e7a
AM
4035+}
4036+#endif
4037+
4038+/* ---------------------------------------------------------------------- */
4039+
1facf9fc 4040+/*
4041+ * change a branch permission
4042+ */
4043+
dece6358
AM
4044+static void au_warn_ima(void)
4045+{
4046+#ifdef CONFIG_IMA
1308ab2a 4047+ /* since it doesn't support mark_files_ro() */
027c5e7a 4048+ AuWarn1("RW -> RO makes IMA to produce wrong message\n");
dece6358
AM
4049+#endif
4050+}
4051+
1facf9fc 4052+static int do_need_sigen_inc(int a, int b)
4053+{
4054+ return au_br_whable(a) && !au_br_whable(b);
4055+}
4056+
4057+static int need_sigen_inc(int old, int new)
4058+{
4059+ return do_need_sigen_inc(old, new)
4060+ || do_need_sigen_inc(new, old);
4061+}
4062+
4063+static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4064+{
7f207e10 4065+ int err, do_warn;
027c5e7a 4066+ unsigned int mnt_flags;
7f207e10 4067+ unsigned long long ull, max;
e49829fe 4068+ aufs_bindex_t br_id;
38d290e6 4069+ unsigned char verbose, writer;
7f207e10 4070+ struct file *file, *hf, **array;
e49829fe 4071+ struct au_hfile *hfile;
1facf9fc 4072+
027c5e7a
AM
4073+ mnt_flags = au_mntflags(sb);
4074+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4075+
7f207e10
AM
4076+ array = au_farray_alloc(sb, &max);
4077+ err = PTR_ERR(array);
4078+ if (IS_ERR(array))
1facf9fc 4079+ goto out;
4080+
7f207e10 4081+ do_warn = 0;
e49829fe 4082+ br_id = au_sbr_id(sb, bindex);
7f207e10
AM
4083+ for (ull = 0; ull < max; ull++) {
4084+ file = array[ull];
076b876e
AM
4085+ if (unlikely(!file))
4086+ break;
1facf9fc 4087+
523b37e3 4088+ /* AuDbg("%pD\n", file); */
1facf9fc 4089+ fi_read_lock(file);
4090+ if (unlikely(au_test_mmapped(file))) {
4091+ err = -EBUSY;
523b37e3 4092+ AuVerbose(verbose, "mmapped %pD\n", file);
7f207e10 4093+ AuDbgFile(file);
1facf9fc 4094+ FiMustNoWaiters(file);
4095+ fi_read_unlock(file);
7f207e10 4096+ goto out_array;
1facf9fc 4097+ }
4098+
e49829fe
JR
4099+ hfile = &au_fi(file)->fi_htop;
4100+ hf = hfile->hf_file;
7e9cd9fe 4101+ if (!d_is_reg(file->f_path.dentry)
1facf9fc 4102+ || !(file->f_mode & FMODE_WRITE)
e49829fe 4103+ || hfile->hf_br->br_id != br_id
7f207e10
AM
4104+ || !(hf->f_mode & FMODE_WRITE))
4105+ array[ull] = NULL;
4106+ else {
4107+ do_warn = 1;
4108+ get_file(file);
1facf9fc 4109+ }
4110+
1facf9fc 4111+ FiMustNoWaiters(file);
4112+ fi_read_unlock(file);
7f207e10
AM
4113+ fput(file);
4114+ }
1facf9fc 4115+
4116+ err = 0;
7f207e10 4117+ if (do_warn)
dece6358 4118+ au_warn_ima();
7f207e10
AM
4119+
4120+ for (ull = 0; ull < max; ull++) {
4121+ file = array[ull];
4122+ if (!file)
4123+ continue;
4124+
1facf9fc 4125+ /* todo: already flushed? */
523b37e3
AM
4126+ /*
4127+ * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4128+ * approach which resets f_mode and calls mnt_drop_write() and
4129+ * file_release_write() for each file, because the branch
4130+ * attribute in aufs world is totally different from the native
4131+ * fs rw/ro mode.
4132+ */
7f207e10
AM
4133+ /* fi_read_lock(file); */
4134+ hfile = &au_fi(file)->fi_htop;
4135+ hf = hfile->hf_file;
4136+ /* fi_read_unlock(file); */
027c5e7a 4137+ spin_lock(&hf->f_lock);
38d290e6
JR
4138+ writer = !!(hf->f_mode & FMODE_WRITER);
4139+ hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
027c5e7a 4140+ spin_unlock(&hf->f_lock);
38d290e6
JR
4141+ if (writer) {
4142+ put_write_access(file_inode(hf));
c06a8ce3 4143+ __mnt_drop_write(hf->f_path.mnt);
1facf9fc 4144+ }
4145+ }
4146+
7f207e10
AM
4147+out_array:
4148+ au_farray_free(array, max);
4f0767ce 4149+out:
7f207e10 4150+ AuTraceErr(err);
1facf9fc 4151+ return err;
4152+}
4153+
4154+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4155+ int *do_refresh)
1facf9fc 4156+{
4157+ int err, rerr;
4158+ aufs_bindex_t bindex;
4159+ struct dentry *root;
4160+ struct au_branch *br;
076b876e 4161+ struct au_br_fhsm *bf;
1facf9fc 4162+
4163+ root = sb->s_root;
1facf9fc 4164+ bindex = au_find_dbindex(root, mod->h_root);
4165+ if (bindex < 0) {
4166+ if (remount)
4167+ return 0; /* success */
4168+ err = -ENOENT;
4a4d8108 4169+ pr_err("%s no such branch\n", mod->path);
1facf9fc 4170+ goto out;
4171+ }
4172+ AuDbg("bindex b%d\n", bindex);
4173+
5527c038 4174+ err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
1facf9fc 4175+ if (unlikely(err))
4176+ goto out;
4177+
4178+ br = au_sbr(sb, bindex);
86dc4139 4179+ AuDebugOn(mod->h_root != au_br_dentry(br));
1facf9fc 4180+ if (br->br_perm == mod->perm)
4181+ return 0; /* success */
4182+
076b876e
AM
4183+ /* pre-allocate for non-fhsm --> fhsm */
4184+ bf = NULL;
4185+ if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4186+ err = au_fhsm_br_alloc(br);
4187+ if (unlikely(err))
4188+ goto out;
4189+ bf = br->br_fhsm;
4190+ br->br_fhsm = NULL;
4191+ }
4192+
1facf9fc 4193+ if (au_br_writable(br->br_perm)) {
4194+ /* remove whiteout base */
86dc4139 4195+ err = au_br_init_wh(sb, br, mod->perm);
1facf9fc 4196+ if (unlikely(err))
076b876e 4197+ goto out_bf;
1facf9fc 4198+
4199+ if (!au_br_writable(mod->perm)) {
4200+ /* rw --> ro, file might be mmapped */
4201+ DiMustNoWaiters(root);
5527c038 4202+ IiMustNoWaiters(d_inode(root));
1facf9fc 4203+ di_write_unlock(root);
4204+ err = au_br_mod_files_ro(sb, bindex);
4205+ /* aufs_write_lock() calls ..._child() */
4206+ di_write_lock_child(root);
4207+
4208+ if (unlikely(err)) {
4209+ rerr = -ENOMEM;
be52b249 4210+ br->br_wbr = kzalloc(sizeof(*br->br_wbr),
1facf9fc 4211+ GFP_NOFS);
86dc4139
AM
4212+ if (br->br_wbr)
4213+ rerr = au_wbr_init(br, sb, br->br_perm);
1facf9fc 4214+ if (unlikely(rerr)) {
4215+ AuIOErr("nested error %d (%d)\n",
4216+ rerr, err);
4217+ br->br_perm = mod->perm;
4218+ }
4219+ }
4220+ }
4221+ } else if (au_br_writable(mod->perm)) {
4222+ /* ro --> rw */
4223+ err = -ENOMEM;
be52b249 4224+ br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
1facf9fc 4225+ if (br->br_wbr) {
86dc4139 4226+ err = au_wbr_init(br, sb, mod->perm);
1facf9fc 4227+ if (unlikely(err)) {
4228+ kfree(br->br_wbr);
4229+ br->br_wbr = NULL;
4230+ }
4231+ }
4232+ }
076b876e
AM
4233+ if (unlikely(err))
4234+ goto out_bf;
4235+
4236+ if (au_br_fhsm(br->br_perm)) {
4237+ if (!au_br_fhsm(mod->perm)) {
4238+ /* fhsm --> non-fhsm */
4239+ au_br_fhsm_fin(br->br_fhsm);
4240+ kfree(br->br_fhsm);
4241+ br->br_fhsm = NULL;
4242+ }
4243+ } else if (au_br_fhsm(mod->perm))
4244+ /* non-fhsm --> fhsm */
4245+ br->br_fhsm = bf;
4246+
076b876e
AM
4247+ *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4248+ br->br_perm = mod->perm;
4249+ goto out; /* success */
1facf9fc 4250+
076b876e
AM
4251+out_bf:
4252+ kfree(bf);
4253+out:
4254+ AuTraceErr(err);
4255+ return err;
4256+}
4257+
4258+/* ---------------------------------------------------------------------- */
4259+
4260+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4261+{
4262+ int err;
4263+ struct kstatfs kstfs;
4264+
4265+ err = vfs_statfs(&br->br_path, &kstfs);
1facf9fc 4266+ if (!err) {
076b876e
AM
4267+ stfs->f_blocks = kstfs.f_blocks;
4268+ stfs->f_bavail = kstfs.f_bavail;
4269+ stfs->f_files = kstfs.f_files;
4270+ stfs->f_ffree = kstfs.f_ffree;
1facf9fc 4271+ }
4272+
1facf9fc 4273+ return err;
4274+}
7f207e10
AM
4275diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4276--- /usr/share/empty/fs/aufs/branch.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
4277+++ linux/fs/aufs/branch.h 2016-07-25 19:05:34.811159821 +0200
4278@@ -0,0 +1,309 @@
1facf9fc 4279+/*
8cdd5066 4280+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 4281+ *
4282+ * This program, aufs is free software; you can redistribute it and/or modify
4283+ * it under the terms of the GNU General Public License as published by
4284+ * the Free Software Foundation; either version 2 of the License, or
4285+ * (at your option) any later version.
dece6358
AM
4286+ *
4287+ * This program is distributed in the hope that it will be useful,
4288+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4289+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4290+ * GNU General Public License for more details.
4291+ *
4292+ * You should have received a copy of the GNU General Public License
523b37e3 4293+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4294+ */
4295+
4296+/*
4297+ * branch filesystems and xino for them
4298+ */
4299+
4300+#ifndef __AUFS_BRANCH_H__
4301+#define __AUFS_BRANCH_H__
4302+
4303+#ifdef __KERNEL__
4304+
1facf9fc 4305+#include <linux/mount.h>
4a4d8108 4306+#include "dynop.h"
1facf9fc 4307+#include "rwsem.h"
4308+#include "super.h"
4309+
4310+/* ---------------------------------------------------------------------- */
4311+
4312+/* a xino file */
4313+struct au_xino_file {
4314+ struct file *xi_file;
4315+ struct mutex xi_nondir_mtx;
4316+
4317+ /* todo: make xino files an array to support huge inode number */
4318+
4319+#ifdef CONFIG_DEBUG_FS
4320+ struct dentry *xi_dbgaufs;
4321+#endif
4322+};
4323+
076b876e
AM
4324+/* File-based Hierarchical Storage Management */
4325+struct au_br_fhsm {
4326+#ifdef CONFIG_AUFS_FHSM
4327+ struct mutex bf_lock;
4328+ unsigned long bf_jiffy;
4329+ struct aufs_stfs bf_stfs;
4330+ int bf_readable;
4331+#endif
4332+};
4333+
1facf9fc 4334+/* members for writable branch only */
4335+enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4336+struct au_wbr {
dece6358 4337+ struct au_rwsem wbr_wh_rwsem;
1facf9fc 4338+ struct dentry *wbr_wh[AuBrWh_Last];
4a4d8108 4339+ atomic_t wbr_wh_running;
1facf9fc 4340+#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
4341+#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
4342+#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
4343+
4344+ /* mfs mode */
4345+ unsigned long long wbr_bytes;
4346+};
4347+
4a4d8108
AM
4348+/* ext2 has 3 types of operations at least, ext3 has 4 */
4349+#define AuBrDynOp (AuDyLast * 4)
4350+
1716fcea
AM
4351+#ifdef CONFIG_AUFS_HFSNOTIFY
4352+/* support for asynchronous destruction */
4353+struct au_br_hfsnotify {
4354+ struct fsnotify_group *hfsn_group;
4355+};
4356+#endif
4357+
392086de
AM
4358+/* sysfs entries */
4359+struct au_brsysfs {
4360+ char name[16];
4361+ struct attribute attr;
4362+};
4363+
4364+enum {
4365+ AuBrSysfs_BR,
4366+ AuBrSysfs_BRID,
4367+ AuBrSysfs_Last
4368+};
4369+
1facf9fc 4370+/* protected by superblock rwsem */
4371+struct au_branch {
4372+ struct au_xino_file br_xino;
4373+
4374+ aufs_bindex_t br_id;
4375+
4376+ int br_perm;
86dc4139 4377+ struct path br_path;
4a4d8108
AM
4378+ spinlock_t br_dykey_lock;
4379+ struct au_dykey *br_dykey[AuBrDynOp];
5afbbe0d 4380+ struct percpu_counter br_count;
1facf9fc 4381+
4382+ struct au_wbr *br_wbr;
076b876e 4383+ struct au_br_fhsm *br_fhsm;
1facf9fc 4384+
4385+ /* xino truncation */
1facf9fc 4386+ atomic_t br_xino_running;
4387+
027c5e7a 4388+#ifdef CONFIG_AUFS_HFSNOTIFY
1716fcea 4389+ struct au_br_hfsnotify *br_hfsn;
027c5e7a
AM
4390+#endif
4391+
1facf9fc 4392+#ifdef CONFIG_SYSFS
392086de
AM
4393+ /* entries under sysfs per mount-point */
4394+ struct au_brsysfs br_sysfs[AuBrSysfs_Last];
1facf9fc 4395+#endif
4396+};
4397+
4398+/* ---------------------------------------------------------------------- */
4399+
86dc4139
AM
4400+static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4401+{
4402+ return br->br_path.mnt;
4403+}
4404+
4405+static inline struct dentry *au_br_dentry(struct au_branch *br)
4406+{
4407+ return br->br_path.dentry;
4408+}
4409+
4410+static inline struct super_block *au_br_sb(struct au_branch *br)
4411+{
4412+ return au_br_mnt(br)->mnt_sb;
4413+}
4414+
5afbbe0d
AM
4415+static inline void au_br_get(struct au_branch *br)
4416+{
4417+ percpu_counter_inc(&br->br_count);
4418+}
4419+
4420+static inline void au_br_put(struct au_branch *br)
4421+{
4422+ percpu_counter_dec(&br->br_count);
4423+}
4424+
4425+static inline s64 au_br_count(struct au_branch *br)
4426+{
4427+ return percpu_counter_sum(&br->br_count);
4428+}
4429+
4430+static inline void au_br_count_init(struct au_branch *br)
4431+{
4432+ percpu_counter_init(&br->br_count, 0, GFP_NOFS);
4433+}
4434+
4435+static inline void au_br_count_fin(struct au_branch *br)
4436+{
4437+ percpu_counter_destroy(&br->br_count);
4438+}
4439+
1facf9fc 4440+static inline int au_br_rdonly(struct au_branch *br)
4441+{
86dc4139 4442+ return ((au_br_sb(br)->s_flags & MS_RDONLY)
1facf9fc 4443+ || !au_br_writable(br->br_perm))
4444+ ? -EROFS : 0;
4445+}
4446+
4a4d8108 4447+static inline int au_br_hnotifyable(int brperm __maybe_unused)
1facf9fc 4448+{
4a4d8108 4449+#ifdef CONFIG_AUFS_HNOTIFY
1e00d052 4450+ return !(brperm & AuBrPerm_RR);
1facf9fc 4451+#else
4452+ return 0;
4453+#endif
4454+}
4455+
b912730e
AM
4456+static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4457+{
4458+ int err, exec_flag;
4459+
4460+ err = 0;
4461+ exec_flag = oflag & __FMODE_EXEC;
79b8bda9 4462+ if (unlikely(exec_flag && path_noexec(&br->br_path)))
b912730e
AM
4463+ err = -EACCES;
4464+
4465+ return err;
4466+}
4467+
1facf9fc 4468+/* ---------------------------------------------------------------------- */
4469+
4470+/* branch.c */
4471+struct au_sbinfo;
4472+void au_br_free(struct au_sbinfo *sinfo);
4473+int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4474+struct au_opt_add;
4475+int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4476+struct au_opt_del;
4477+int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
027c5e7a
AM
4478+long au_ibusy_ioctl(struct file *file, unsigned long arg);
4479+#ifdef CONFIG_COMPAT
4480+long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4481+#endif
1facf9fc 4482+struct au_opt_mod;
4483+int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
7f207e10 4484+ int *do_refresh);
076b876e
AM
4485+struct aufs_stfs;
4486+int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
1facf9fc 4487+
4488+/* xino.c */
4489+static const loff_t au_loff_max = LLONG_MAX;
4490+
4491+int au_xib_trunc(struct super_block *sb);
5527c038 4492+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
1facf9fc 4493+ loff_t *pos);
5527c038
JR
4494+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
4495+ size_t size, loff_t *pos);
1facf9fc 4496+struct file *au_xino_create2(struct file *base_file, struct file *copy_src);
4497+struct file *au_xino_create(struct super_block *sb, char *fname, int silent);
4498+ino_t au_xino_new_ino(struct super_block *sb);
b752ccd1 4499+void au_xino_delete_inode(struct inode *inode, const int unlinked);
1facf9fc 4500+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4501+ ino_t ino);
4502+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4503+ ino_t *ino);
4504+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4505+ struct file *base_file, int do_test);
4506+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex);
4507+
4508+struct au_opt_xino;
4509+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount);
4510+void au_xino_clr(struct super_block *sb);
4511+struct file *au_xino_def(struct super_block *sb);
4512+int au_xino_path(struct seq_file *seq, struct file *file);
4513+
4514+/* ---------------------------------------------------------------------- */
4515+
4516+/* Superblock to branch */
4517+static inline
4518+aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4519+{
4520+ return au_sbr(sb, bindex)->br_id;
4521+}
4522+
4523+static inline
4524+struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4525+{
86dc4139 4526+ return au_br_mnt(au_sbr(sb, bindex));
1facf9fc 4527+}
4528+
4529+static inline
4530+struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4531+{
86dc4139 4532+ return au_br_sb(au_sbr(sb, bindex));
1facf9fc 4533+}
4534+
5afbbe0d
AM
4535+static inline void au_sbr_get(struct super_block *sb, aufs_bindex_t bindex)
4536+{
4537+ au_br_get(au_sbr(sb, bindex));
4538+}
4539+
1facf9fc 4540+static inline void au_sbr_put(struct super_block *sb, aufs_bindex_t bindex)
4541+{
5afbbe0d 4542+ au_br_put(au_sbr(sb, bindex));
1facf9fc 4543+}
4544+
4545+static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4546+{
4547+ return au_sbr(sb, bindex)->br_perm;
4548+}
4549+
4550+static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4551+{
4552+ return au_br_whable(au_sbr_perm(sb, bindex));
4553+}
4554+
4555+/* ---------------------------------------------------------------------- */
4556+
4557+/*
4558+ * wbr_wh_read_lock, wbr_wh_write_lock
4559+ * wbr_wh_read_unlock, wbr_wh_write_unlock, wbr_wh_downgrade_lock
4560+ */
4561+AuSimpleRwsemFuncs(wbr_wh, struct au_wbr *wbr, &wbr->wbr_wh_rwsem);
4562+
dece6358
AM
4563+#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&wbr->wbr_wh_rwsem)
4564+#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&wbr->wbr_wh_rwsem)
4565+#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&wbr->wbr_wh_rwsem)
4566+
076b876e
AM
4567+/* ---------------------------------------------------------------------- */
4568+
4569+#ifdef CONFIG_AUFS_FHSM
4570+static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4571+{
4572+ mutex_init(&brfhsm->bf_lock);
4573+ brfhsm->bf_jiffy = 0;
4574+ brfhsm->bf_readable = 0;
4575+}
4576+
4577+static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4578+{
4579+ mutex_destroy(&brfhsm->bf_lock);
4580+}
4581+#else
4582+AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4583+AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4584+#endif
4585+
1facf9fc 4586+#endif /* __KERNEL__ */
4587+#endif /* __AUFS_BRANCH_H__ */
7f207e10
AM
4588diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4589--- /usr/share/empty/fs/aufs/conf.mk 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 4590+++ linux/fs/aufs/conf.mk 2015-09-24 10:47:58.248052907 +0200
c1595e42 4591@@ -0,0 +1,38 @@
4a4d8108
AM
4592+
4593+AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4594+
4595+define AuConf
4596+ifdef ${1}
4597+AuConfStr += ${1}=${${1}}
4598+endif
4599+endef
4600+
b752ccd1 4601+AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
e49829fe 4602+ SBILIST \
7f207e10 4603+ HNOTIFY HFSNOTIFY \
4a4d8108 4604+ EXPORT INO_T_64 \
c1595e42 4605+ XATTR \
076b876e 4606+ FHSM \
4a4d8108 4607+ RDU \
4a4d8108
AM
4608+ SHWH \
4609+ BR_RAMFS \
4610+ BR_FUSE POLL \
4611+ BR_HFSPLUS \
4612+ BDEV_LOOP \
b752ccd1
AM
4613+ DEBUG MAGIC_SYSRQ
4614+$(foreach i, ${AuConfAll}, \
4a4d8108
AM
4615+ $(eval $(call AuConf,CONFIG_AUFS_${i})))
4616+
4617+AuConfName = ${obj}/conf.str
4618+${AuConfName}.tmp: FORCE
4619+ @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4620+${AuConfName}: ${AuConfName}.tmp
4621+ @diff -q $< $@ > /dev/null 2>&1 || { \
4622+ echo ' GEN ' $@; \
4623+ cp -p $< $@; \
4624+ }
4625+FORCE:
4626+clean-files += ${AuConfName} ${AuConfName}.tmp
4627+${obj}/sysfs.o: ${AuConfName}
b752ccd1
AM
4628+
4629+-include ${srctree}/${src}/conf_priv.mk
7f207e10
AM
4630diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4631--- /usr/share/empty/fs/aufs/cpup.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
4632+++ linux/fs/aufs/cpup.c 2016-07-25 19:05:34.811159821 +0200
4633@@ -0,0 +1,1383 @@
1facf9fc 4634+/*
8cdd5066 4635+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 4636+ *
4637+ * This program, aufs is free software; you can redistribute it and/or modify
4638+ * it under the terms of the GNU General Public License as published by
4639+ * the Free Software Foundation; either version 2 of the License, or
4640+ * (at your option) any later version.
dece6358
AM
4641+ *
4642+ * This program is distributed in the hope that it will be useful,
4643+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
4644+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4645+ * GNU General Public License for more details.
4646+ *
4647+ * You should have received a copy of the GNU General Public License
523b37e3 4648+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 4649+ */
4650+
4651+/*
4652+ * copy-up functions, see wbr_policy.c for copy-down
4653+ */
4654+
4655+#include <linux/fs_stack.h>
dece6358 4656+#include <linux/mm.h>
8cdd5066 4657+#include <linux/task_work.h>
1facf9fc 4658+#include "aufs.h"
4659+
86dc4139 4660+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
1facf9fc 4661+{
4662+ const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
367653fa 4663+ | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
1facf9fc 4664+
86dc4139
AM
4665+ BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4666+
4667+ dst->i_flags |= iflags & ~mask;
1facf9fc 4668+ if (au_test_fs_notime(dst->i_sb))
4669+ dst->i_flags |= S_NOATIME | S_NOCMTIME;
4670+}
4671+
4672+void au_cpup_attr_timesizes(struct inode *inode)
4673+{
4674+ struct inode *h_inode;
4675+
5afbbe0d 4676+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4677+ fsstack_copy_attr_times(inode, h_inode);
4a4d8108 4678+ fsstack_copy_inode_size(inode, h_inode);
1facf9fc 4679+}
4680+
4681+void au_cpup_attr_nlink(struct inode *inode, int force)
4682+{
4683+ struct inode *h_inode;
4684+ struct super_block *sb;
5afbbe0d 4685+ aufs_bindex_t bindex, bbot;
1facf9fc 4686+
4687+ sb = inode->i_sb;
5afbbe0d 4688+ bindex = au_ibtop(inode);
1facf9fc 4689+ h_inode = au_h_iptr(inode, bindex);
4690+ if (!force
4691+ && !S_ISDIR(h_inode->i_mode)
4692+ && au_opt_test(au_mntflags(sb), PLINK)
4693+ && au_plink_test(inode))
4694+ return;
4695+
7eafdf33
AM
4696+ /*
4697+ * 0 can happen in revalidating.
38d290e6
JR
4698+ * h_inode->i_mutex may not be held here, but it is harmless since once
4699+ * i_nlink reaches 0, it will never become positive except O_TMPFILE
4700+ * case.
4701+ * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4702+ * the incorrect link count.
7eafdf33 4703+ */
92d182d2 4704+ set_nlink(inode, h_inode->i_nlink);
1facf9fc 4705+
4706+ /*
4707+ * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4708+ * it may includes whplink directory.
4709+ */
4710+ if (S_ISDIR(h_inode->i_mode)) {
5afbbe0d
AM
4711+ bbot = au_ibbot(inode);
4712+ for (bindex++; bindex <= bbot; bindex++) {
1facf9fc 4713+ h_inode = au_h_iptr(inode, bindex);
4714+ if (h_inode)
4715+ au_add_nlink(inode, h_inode);
4716+ }
4717+ }
4718+}
4719+
4720+void au_cpup_attr_changeable(struct inode *inode)
4721+{
4722+ struct inode *h_inode;
4723+
5afbbe0d 4724+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4725+ inode->i_mode = h_inode->i_mode;
4726+ inode->i_uid = h_inode->i_uid;
4727+ inode->i_gid = h_inode->i_gid;
4728+ au_cpup_attr_timesizes(inode);
86dc4139 4729+ au_cpup_attr_flags(inode, h_inode->i_flags);
1facf9fc 4730+}
4731+
4732+void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4733+{
4734+ struct au_iinfo *iinfo = au_ii(inode);
4735+
1308ab2a 4736+ IiMustWriteLock(inode);
4737+
1facf9fc 4738+ iinfo->ii_higen = h_inode->i_generation;
4739+ iinfo->ii_hsb1 = h_inode->i_sb;
4740+}
4741+
4742+void au_cpup_attr_all(struct inode *inode, int force)
4743+{
4744+ struct inode *h_inode;
4745+
5afbbe0d 4746+ h_inode = au_h_iptr(inode, au_ibtop(inode));
1facf9fc 4747+ au_cpup_attr_changeable(inode);
4748+ if (inode->i_nlink > 0)
4749+ au_cpup_attr_nlink(inode, force);
4750+ inode->i_rdev = h_inode->i_rdev;
4751+ inode->i_blkbits = h_inode->i_blkbits;
4752+ au_cpup_igen(inode, h_inode);
4753+}
4754+
4755+/* ---------------------------------------------------------------------- */
4756+
4757+/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4758+
4759+/* keep the timestamps of the parent dir when cpup */
4760+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4761+ struct path *h_path)
4762+{
4763+ struct inode *h_inode;
4764+
4765+ dt->dt_dentry = dentry;
4766+ dt->dt_h_path = *h_path;
5527c038 4767+ h_inode = d_inode(h_path->dentry);
1facf9fc 4768+ dt->dt_atime = h_inode->i_atime;
4769+ dt->dt_mtime = h_inode->i_mtime;
4770+ /* smp_mb(); */
4771+}
4772+
4773+void au_dtime_revert(struct au_dtime *dt)
4774+{
4775+ struct iattr attr;
4776+ int err;
4777+
4778+ attr.ia_atime = dt->dt_atime;
4779+ attr.ia_mtime = dt->dt_mtime;
4780+ attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4781+ | ATTR_ATIME | ATTR_ATIME_SET;
4782+
523b37e3
AM
4783+ /* no delegation since this is a directory */
4784+ err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
1facf9fc 4785+ if (unlikely(err))
0c3ec466 4786+ pr_warn("restoring timestamps failed(%d). ignored\n", err);
1facf9fc 4787+}
4788+
4789+/* ---------------------------------------------------------------------- */
4790+
86dc4139
AM
4791+/* internal use only */
4792+struct au_cpup_reg_attr {
4793+ int valid;
4794+ struct kstat st;
4795+ unsigned int iflags; /* inode->i_flags */
4796+};
4797+
1facf9fc 4798+static noinline_for_stack
86dc4139
AM
4799+int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
4800+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 4801+{
c1595e42 4802+ int err, sbits, icex;
7e9cd9fe
AM
4803+ unsigned int mnt_flags;
4804+ unsigned char verbose;
1facf9fc 4805+ struct iattr ia;
4806+ struct path h_path;
1308ab2a 4807+ struct inode *h_isrc, *h_idst;
86dc4139 4808+ struct kstat *h_st;
c1595e42 4809+ struct au_branch *br;
1facf9fc 4810+
4811+ h_path.dentry = au_h_dptr(dst, bindex);
5527c038 4812+ h_idst = d_inode(h_path.dentry);
c1595e42
JR
4813+ br = au_sbr(dst->d_sb, bindex);
4814+ h_path.mnt = au_br_mnt(br);
5527c038 4815+ h_isrc = d_inode(h_src);
1308ab2a 4816+ ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
1facf9fc 4817+ | ATTR_ATIME | ATTR_MTIME
4818+ | ATTR_ATIME_SET | ATTR_MTIME_SET;
86dc4139
AM
4819+ if (h_src_attr && h_src_attr->valid) {
4820+ h_st = &h_src_attr->st;
4821+ ia.ia_uid = h_st->uid;
4822+ ia.ia_gid = h_st->gid;
4823+ ia.ia_atime = h_st->atime;
4824+ ia.ia_mtime = h_st->mtime;
4825+ if (h_idst->i_mode != h_st->mode
4826+ && !S_ISLNK(h_idst->i_mode)) {
4827+ ia.ia_valid |= ATTR_MODE;
4828+ ia.ia_mode = h_st->mode;
4829+ }
4830+ sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4831+ au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4832+ } else {
4833+ ia.ia_uid = h_isrc->i_uid;
4834+ ia.ia_gid = h_isrc->i_gid;
4835+ ia.ia_atime = h_isrc->i_atime;
4836+ ia.ia_mtime = h_isrc->i_mtime;
4837+ if (h_idst->i_mode != h_isrc->i_mode
4838+ && !S_ISLNK(h_idst->i_mode)) {
4839+ ia.ia_valid |= ATTR_MODE;
4840+ ia.ia_mode = h_isrc->i_mode;
4841+ }
4842+ sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4843+ au_cpup_attr_flags(h_idst, h_isrc->i_flags);
1308ab2a 4844+ }
523b37e3
AM
4845+ /* no delegation since it is just created */
4846+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4847+
4848+ /* is this nfs only? */
4849+ if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4850+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4851+ ia.ia_mode = h_isrc->i_mode;
523b37e3 4852+ err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
1facf9fc 4853+ }
4854+
c1595e42 4855+ icex = br->br_perm & AuBrAttr_ICEX;
7e9cd9fe
AM
4856+ if (!err) {
4857+ mnt_flags = au_mntflags(dst->d_sb);
4858+ verbose = !!au_opt_test(mnt_flags, VERBOSE);
4859+ err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
4860+ }
c1595e42 4861+
1facf9fc 4862+ return err;
4863+}
4864+
4865+/* ---------------------------------------------------------------------- */
4866+
4867+static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4868+ char *buf, unsigned long blksize)
4869+{
4870+ int err;
4871+ size_t sz, rbytes, wbytes;
4872+ unsigned char all_zero;
4873+ char *p, *zp;
febd17d6 4874+ struct inode *h_inode;
1facf9fc 4875+ /* reduce stack usage */
4876+ struct iattr *ia;
4877+
4878+ zp = page_address(ZERO_PAGE(0));
4879+ if (unlikely(!zp))
4880+ return -ENOMEM; /* possible? */
4881+
4882+ err = 0;
4883+ all_zero = 0;
4884+ while (len) {
4885+ AuDbg("len %lld\n", len);
4886+ sz = blksize;
4887+ if (len < blksize)
4888+ sz = len;
4889+
4890+ rbytes = 0;
4891+ /* todo: signal_pending? */
4892+ while (!rbytes || err == -EAGAIN || err == -EINTR) {
4893+ rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4894+ err = rbytes;
4895+ }
4896+ if (unlikely(err < 0))
4897+ break;
4898+
4899+ all_zero = 0;
4900+ if (len >= rbytes && rbytes == blksize)
4901+ all_zero = !memcmp(buf, zp, rbytes);
4902+ if (!all_zero) {
4903+ wbytes = rbytes;
4904+ p = buf;
4905+ while (wbytes) {
4906+ size_t b;
4907+
4908+ b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
4909+ err = b;
4910+ /* todo: signal_pending? */
4911+ if (unlikely(err == -EAGAIN || err == -EINTR))
4912+ continue;
4913+ if (unlikely(err < 0))
4914+ break;
4915+ wbytes -= b;
4916+ p += b;
4917+ }
392086de
AM
4918+ if (unlikely(err < 0))
4919+ break;
1facf9fc 4920+ } else {
4921+ loff_t res;
4922+
4923+ AuLabel(hole);
4924+ res = vfsub_llseek(dst, rbytes, SEEK_CUR);
4925+ err = res;
4926+ if (unlikely(res < 0))
4927+ break;
4928+ }
4929+ len -= rbytes;
4930+ err = 0;
4931+ }
4932+
4933+ /* the last block may be a hole */
4934+ if (!err && all_zero) {
4935+ AuLabel(last hole);
4936+
4937+ err = 1;
2000de60 4938+ if (au_test_nfs(dst->f_path.dentry->d_sb)) {
1facf9fc 4939+ /* nfs requires this step to make last hole */
4940+ /* is this only nfs? */
4941+ do {
4942+ /* todo: signal_pending? */
4943+ err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
4944+ } while (err == -EAGAIN || err == -EINTR);
4945+ if (err == 1)
4946+ dst->f_pos--;
4947+ }
4948+
4949+ if (err == 1) {
4950+ ia = (void *)buf;
4951+ ia->ia_size = dst->f_pos;
4952+ ia->ia_valid = ATTR_SIZE | ATTR_FILE;
4953+ ia->ia_file = dst;
febd17d6
JR
4954+ h_inode = file_inode(dst);
4955+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
523b37e3
AM
4956+ /* no delegation since it is just created */
4957+ err = vfsub_notify_change(&dst->f_path, ia,
4958+ /*delegated*/NULL);
febd17d6 4959+ inode_unlock(h_inode);
1facf9fc 4960+ }
4961+ }
4962+
4963+ return err;
4964+}
4965+
4966+int au_copy_file(struct file *dst, struct file *src, loff_t len)
4967+{
4968+ int err;
4969+ unsigned long blksize;
4970+ unsigned char do_kfree;
4971+ char *buf;
4972+
4973+ err = -ENOMEM;
2000de60 4974+ blksize = dst->f_path.dentry->d_sb->s_blocksize;
1facf9fc 4975+ if (!blksize || PAGE_SIZE < blksize)
4976+ blksize = PAGE_SIZE;
4977+ AuDbg("blksize %lu\n", blksize);
4978+ do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
4979+ if (do_kfree)
4980+ buf = kmalloc(blksize, GFP_NOFS);
4981+ else
4982+ buf = (void *)__get_free_page(GFP_NOFS);
4983+ if (unlikely(!buf))
4984+ goto out;
4985+
4986+ if (len > (1 << 22))
4987+ AuDbg("copying a large file %lld\n", (long long)len);
4988+
4989+ src->f_pos = 0;
4990+ dst->f_pos = 0;
4991+ err = au_do_copy_file(dst, src, len, buf, blksize);
4992+ if (do_kfree)
4993+ kfree(buf);
4994+ else
4995+ free_page((unsigned long)buf);
4996+
4f0767ce 4997+out:
1facf9fc 4998+ return err;
4999+}
5000+
5001+/*
5002+ * to support a sparse file which is opened with O_APPEND,
5003+ * we need to close the file.
5004+ */
c2b27bf2 5005+static int au_cp_regular(struct au_cp_generic *cpg)
1facf9fc 5006+{
5007+ int err, i;
5008+ enum { SRC, DST };
5009+ struct {
5010+ aufs_bindex_t bindex;
5011+ unsigned int flags;
5012+ struct dentry *dentry;
392086de 5013+ int force_wr;
1facf9fc 5014+ struct file *file;
523b37e3 5015+ void *label;
1facf9fc 5016+ } *f, file[] = {
5017+ {
c2b27bf2 5018+ .bindex = cpg->bsrc,
1facf9fc 5019+ .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
523b37e3 5020+ .label = &&out
1facf9fc 5021+ },
5022+ {
c2b27bf2 5023+ .bindex = cpg->bdst,
1facf9fc 5024+ .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
392086de 5025+ .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
523b37e3 5026+ .label = &&out_src
1facf9fc 5027+ }
5028+ };
5029+ struct super_block *sb;
8cdd5066 5030+ struct task_struct *tsk = current;
1facf9fc 5031+
5032+ /* bsrc branch can be ro/rw. */
c2b27bf2 5033+ sb = cpg->dentry->d_sb;
1facf9fc 5034+ f = file;
5035+ for (i = 0; i < 2; i++, f++) {
c2b27bf2
AM
5036+ f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5037+ f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
392086de 5038+ /*file*/NULL, f->force_wr);
1facf9fc 5039+ err = PTR_ERR(f->file);
5040+ if (IS_ERR(f->file))
5041+ goto *f->label;
1facf9fc 5042+ }
5043+
5044+ /* try stopping to update while we copyup */
5527c038 5045+ IMustLock(d_inode(file[SRC].dentry));
c2b27bf2 5046+ err = au_copy_file(file[DST].file, file[SRC].file, cpg->len);
1facf9fc 5047+
8cdd5066
JR
5048+ /* i wonder if we had O_NO_DELAY_FPUT flag */
5049+ if (tsk->flags & PF_KTHREAD)
5050+ __fput_sync(file[DST].file);
5051+ else {
5052+ WARN(1, "%pD\nPlease report this warning to aufs-users ML",
5053+ file[DST].file);
5054+ fput(file[DST].file);
5055+ /*
5056+ * too bad.
5057+ * we have to call both since we don't know which place the file
5058+ * was added to.
5059+ */
5060+ task_work_run();
5061+ flush_delayed_fput();
5062+ }
1facf9fc 5063+ au_sbr_put(sb, file[DST].bindex);
523b37e3 5064+
4f0767ce 5065+out_src:
1facf9fc 5066+ fput(file[SRC].file);
5067+ au_sbr_put(sb, file[SRC].bindex);
4f0767ce 5068+out:
1facf9fc 5069+ return err;
5070+}
5071+
c2b27bf2 5072+static int au_do_cpup_regular(struct au_cp_generic *cpg,
86dc4139 5073+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5074+{
5075+ int err, rerr;
5076+ loff_t l;
86dc4139 5077+ struct path h_path;
38d290e6 5078+ struct inode *h_src_inode, *h_dst_inode;
1facf9fc 5079+
5080+ err = 0;
5527c038 5081+ h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
86dc4139 5082+ l = i_size_read(h_src_inode);
c2b27bf2
AM
5083+ if (cpg->len == -1 || l < cpg->len)
5084+ cpg->len = l;
5085+ if (cpg->len) {
86dc4139 5086+ /* try stopping to update while we are referencing */
febd17d6 5087+ inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
c2b27bf2 5088+ au_pin_hdir_unlock(cpg->pin);
1facf9fc 5089+
c2b27bf2
AM
5090+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5091+ h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
86dc4139 5092+ h_src_attr->iflags = h_src_inode->i_flags;
5527c038
JR
5093+ if (!au_test_nfs(h_src_inode->i_sb))
5094+ err = vfs_getattr(&h_path, &h_src_attr->st);
5095+ else {
febd17d6 5096+ inode_unlock(h_src_inode);
5527c038 5097+ err = vfs_getattr(&h_path, &h_src_attr->st);
febd17d6 5098+ inode_lock_nested(h_src_inode, AuLsc_I_CHILD);
5527c038 5099+ }
86dc4139 5100+ if (unlikely(err)) {
febd17d6 5101+ inode_unlock(h_src_inode);
86dc4139
AM
5102+ goto out;
5103+ }
5104+ h_src_attr->valid = 1;
c2b27bf2 5105+ err = au_cp_regular(cpg);
febd17d6 5106+ inode_unlock(h_src_inode);
c2b27bf2 5107+ rerr = au_pin_hdir_relock(cpg->pin);
86dc4139
AM
5108+ if (!err && rerr)
5109+ err = rerr;
1facf9fc 5110+ }
38d290e6
JR
5111+ if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5112+ h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5527c038 5113+ h_dst_inode = d_inode(h_path.dentry);
38d290e6
JR
5114+ spin_lock(&h_dst_inode->i_lock);
5115+ h_dst_inode->i_state |= I_LINKABLE;
5116+ spin_unlock(&h_dst_inode->i_lock);
5117+ }
1facf9fc 5118+
4f0767ce 5119+out:
1facf9fc 5120+ return err;
5121+}
5122+
5123+static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5124+ struct inode *h_dir)
5125+{
5126+ int err, symlen;
5127+ mm_segment_t old_fs;
b752ccd1
AM
5128+ union {
5129+ char *k;
5130+ char __user *u;
5131+ } sym;
5527c038
JR
5132+ struct inode *h_inode = d_inode(h_src);
5133+ const struct inode_operations *h_iop = h_inode->i_op;
1facf9fc 5134+
5135+ err = -ENOSYS;
5527c038 5136+ if (unlikely(!h_iop->readlink))
1facf9fc 5137+ goto out;
5138+
5139+ err = -ENOMEM;
537831f9 5140+ sym.k = (void *)__get_free_page(GFP_NOFS);
b752ccd1 5141+ if (unlikely(!sym.k))
1facf9fc 5142+ goto out;
5143+
9dbd164d 5144+ /* unnecessary to support mmap_sem since symlink is not mmap-able */
1facf9fc 5145+ old_fs = get_fs();
5146+ set_fs(KERNEL_DS);
5527c038 5147+ symlen = h_iop->readlink(h_src, sym.u, PATH_MAX);
1facf9fc 5148+ err = symlen;
5149+ set_fs(old_fs);
5150+
5151+ if (symlen > 0) {
b752ccd1
AM
5152+ sym.k[symlen] = 0;
5153+ err = vfsub_symlink(h_dir, h_path, sym.k);
1facf9fc 5154+ }
537831f9 5155+ free_page((unsigned long)sym.k);
1facf9fc 5156+
4f0767ce 5157+out:
1facf9fc 5158+ return err;
5159+}
5160+
8cdd5066
JR
5161+/*
5162+ * regardless 'acl' option, reset all ACL.
5163+ * All ACL will be copied up later from the original entry on the lower branch.
5164+ */
5165+static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5166+{
5167+ int err;
5168+ struct dentry *h_dentry;
5169+ struct inode *h_inode;
5170+
5171+ h_dentry = h_path->dentry;
5172+ h_inode = d_inode(h_dentry);
5173+ /* forget_all_cached_acls(h_inode)); */
5174+ err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5175+ AuTraceErr(err);
5176+ if (err == -EOPNOTSUPP)
5177+ err = 0;
5178+ if (!err)
5179+ err = vfsub_acl_chmod(h_inode, mode);
5180+
5181+ AuTraceErr(err);
5182+ return err;
5183+}
5184+
5185+static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5186+ struct inode *h_dir, struct path *h_path)
5187+{
5188+ int err;
5189+ struct inode *dir, *inode;
5190+
5191+ err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
5192+ AuTraceErr(err);
5193+ if (err == -EOPNOTSUPP)
5194+ err = 0;
5195+ if (unlikely(err))
5196+ goto out;
5197+
5198+ /*
5199+ * strange behaviour from the users view,
5200+ * particularry setattr case
5201+ */
5202+ dir = d_inode(dst_parent);
5afbbe0d 5203+ if (au_ibtop(dir) == cpg->bdst)
8cdd5066
JR
5204+ au_cpup_attr_nlink(dir, /*force*/1);
5205+ inode = d_inode(cpg->dentry);
5206+ au_cpup_attr_nlink(inode, /*force*/1);
5207+
5208+out:
5209+ return err;
5210+}
5211+
1facf9fc 5212+static noinline_for_stack
c2b27bf2 5213+int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
86dc4139 5214+ struct au_cpup_reg_attr *h_src_attr)
1facf9fc 5215+{
5216+ int err;
5217+ umode_t mode;
5218+ unsigned int mnt_flags;
076b876e 5219+ unsigned char isdir, isreg, force;
c2b27bf2 5220+ const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5221+ struct au_dtime dt;
5222+ struct path h_path;
5223+ struct dentry *h_src, *h_dst, *h_parent;
8cdd5066 5224+ struct inode *h_inode, *h_dir;
1facf9fc 5225+ struct super_block *sb;
5226+
5227+ /* bsrc branch can be ro/rw. */
c2b27bf2 5228+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038
JR
5229+ h_inode = d_inode(h_src);
5230+ AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
1facf9fc 5231+
5232+ /* try stopping to be referenced while we are creating */
c2b27bf2
AM
5233+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5234+ if (au_ftest_cpup(cpg->flags, RENAME))
86dc4139
AM
5235+ AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5236+ AUFS_WH_PFX_LEN));
1facf9fc 5237+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5238+ h_dir = d_inode(h_parent);
1facf9fc 5239+ IMustLock(h_dir);
5240+ AuDebugOn(h_parent != h_dst->d_parent);
5241+
c2b27bf2
AM
5242+ sb = cpg->dentry->d_sb;
5243+ h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
1facf9fc 5244+ if (do_dt) {
5245+ h_path.dentry = h_parent;
5246+ au_dtime_store(&dt, dst_parent, &h_path);
5247+ }
5248+ h_path.dentry = h_dst;
5249+
076b876e 5250+ isreg = 0;
1facf9fc 5251+ isdir = 0;
5252+ mode = h_inode->i_mode;
5253+ switch (mode & S_IFMT) {
5254+ case S_IFREG:
076b876e 5255+ isreg = 1;
8cdd5066 5256+ err = vfsub_create(h_dir, &h_path, S_IRUSR | S_IWUSR,
b4510431 5257+ /*want_excl*/true);
1facf9fc 5258+ if (!err)
c2b27bf2 5259+ err = au_do_cpup_regular(cpg, h_src_attr);
1facf9fc 5260+ break;
5261+ case S_IFDIR:
5262+ isdir = 1;
5263+ err = vfsub_mkdir(h_dir, &h_path, mode);
8cdd5066
JR
5264+ if (!err)
5265+ err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
1facf9fc 5266+ break;
5267+ case S_IFLNK:
5268+ err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5269+ break;
5270+ case S_IFCHR:
5271+ case S_IFBLK:
5272+ AuDebugOn(!capable(CAP_MKNOD));
5273+ /*FALLTHROUGH*/
5274+ case S_IFIFO:
5275+ case S_IFSOCK:
5276+ err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5277+ break;
5278+ default:
5279+ AuIOErr("Unknown inode type 0%o\n", mode);
5280+ err = -EIO;
5281+ }
8cdd5066
JR
5282+ if (!err)
5283+ err = au_reset_acl(h_dir, &h_path, mode);
1facf9fc 5284+
5285+ mnt_flags = au_mntflags(sb);
5286+ if (!au_opt_test(mnt_flags, UDBA_NONE)
5287+ && !isdir
5288+ && au_opt_test(mnt_flags, XINO)
38d290e6
JR
5289+ && (h_inode->i_nlink == 1
5290+ || (h_inode->i_state & I_LINKABLE))
1facf9fc 5291+ /* todo: unnecessary? */
5527c038 5292+ /* && d_inode(cpg->dentry)->i_nlink == 1 */
c2b27bf2
AM
5293+ && cpg->bdst < cpg->bsrc
5294+ && !au_ftest_cpup(cpg->flags, KEEPLINO))
5295+ au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
1facf9fc 5296+ /* ignore this error */
5297+
076b876e
AM
5298+ if (!err) {
5299+ force = 0;
5300+ if (isreg) {
5301+ force = !!cpg->len;
5302+ if (cpg->len == -1)
5303+ force = !!i_size_read(h_inode);
5304+ }
5305+ au_fhsm_wrote(sb, cpg->bdst, force);
5306+ }
5307+
1facf9fc 5308+ if (do_dt)
5309+ au_dtime_revert(&dt);
5310+ return err;
5311+}
5312+
392086de 5313+static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
86dc4139
AM
5314+{
5315+ int err;
392086de 5316+ struct dentry *dentry, *h_dentry, *h_parent, *parent;
86dc4139 5317+ struct inode *h_dir;
392086de 5318+ aufs_bindex_t bdst;
86dc4139 5319+
392086de
AM
5320+ dentry = cpg->dentry;
5321+ bdst = cpg->bdst;
5322+ h_dentry = au_h_dptr(dentry, bdst);
5323+ if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5324+ dget(h_dentry);
5325+ au_set_h_dptr(dentry, bdst, NULL);
5326+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
5327+ if (!err)
5328+ h_path->dentry = dget(au_h_dptr(dentry, bdst));
86dc4139 5329+ au_set_h_dptr(dentry, bdst, h_dentry);
392086de
AM
5330+ } else {
5331+ err = 0;
5332+ parent = dget_parent(dentry);
5333+ h_parent = au_h_dptr(parent, bdst);
5334+ dput(parent);
5335+ h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
5336+ if (IS_ERR(h_path->dentry))
5337+ err = PTR_ERR(h_path->dentry);
86dc4139 5338+ }
392086de
AM
5339+ if (unlikely(err))
5340+ goto out;
86dc4139 5341+
86dc4139 5342+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 5343+ h_dir = d_inode(h_parent);
86dc4139 5344+ IMustLock(h_dir);
523b37e3
AM
5345+ AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5346+ /* no delegation since it is just created */
5347+ err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL);
86dc4139
AM
5348+ dput(h_path->dentry);
5349+
5350+out:
5351+ return err;
5352+}
5353+
1facf9fc 5354+/*
5355+ * copyup the @dentry from @bsrc to @bdst.
5356+ * the caller must set the both of lower dentries.
5357+ * @len is for truncating when it is -1 copyup the entire file.
5358+ * in link/rename cases, @dst_parent may be different from the real one.
c2b27bf2 5359+ * basic->bsrc can be larger than basic->bdst.
1facf9fc 5360+ */
c2b27bf2 5361+static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5362+{
5363+ int err, rerr;
5afbbe0d 5364+ aufs_bindex_t old_ibtop;
1facf9fc 5365+ unsigned char isdir, plink;
1facf9fc 5366+ struct dentry *h_src, *h_dst, *h_parent;
5527c038 5367+ struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
1facf9fc 5368+ struct super_block *sb;
86dc4139 5369+ struct au_branch *br;
c2b27bf2
AM
5370+ /* to reuduce stack size */
5371+ struct {
5372+ struct au_dtime dt;
5373+ struct path h_path;
5374+ struct au_cpup_reg_attr h_src_attr;
5375+ } *a;
1facf9fc 5376+
c2b27bf2
AM
5377+ err = -ENOMEM;
5378+ a = kmalloc(sizeof(*a), GFP_NOFS);
5379+ if (unlikely(!a))
5380+ goto out;
5381+ a->h_src_attr.valid = 0;
1facf9fc 5382+
c2b27bf2
AM
5383+ sb = cpg->dentry->d_sb;
5384+ br = au_sbr(sb, cpg->bdst);
5385+ a->h_path.mnt = au_br_mnt(br);
5386+ h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
1facf9fc 5387+ h_parent = h_dst->d_parent; /* dir inode is locked */
5527c038 5388+ h_dir = d_inode(h_parent);
1facf9fc 5389+ IMustLock(h_dir);
5390+
c2b27bf2 5391+ h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5392+ inode = d_inode(cpg->dentry);
1facf9fc 5393+
5394+ if (!dst_parent)
c2b27bf2 5395+ dst_parent = dget_parent(cpg->dentry);
1facf9fc 5396+ else
5397+ dget(dst_parent);
5398+
5399+ plink = !!au_opt_test(au_mntflags(sb), PLINK);
c2b27bf2 5400+ dst_inode = au_h_iptr(inode, cpg->bdst);
1facf9fc 5401+ if (dst_inode) {
5402+ if (unlikely(!plink)) {
5403+ err = -EIO;
027c5e7a
AM
5404+ AuIOErr("hi%lu(i%lu) exists on b%d "
5405+ "but plink is disabled\n",
c2b27bf2
AM
5406+ dst_inode->i_ino, inode->i_ino, cpg->bdst);
5407+ goto out_parent;
1facf9fc 5408+ }
5409+
5410+ if (dst_inode->i_nlink) {
c2b27bf2 5411+ const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
1facf9fc 5412+
c2b27bf2 5413+ h_src = au_plink_lkup(inode, cpg->bdst);
1facf9fc 5414+ err = PTR_ERR(h_src);
5415+ if (IS_ERR(h_src))
c2b27bf2 5416+ goto out_parent;
5527c038 5417+ if (unlikely(d_is_negative(h_src))) {
1facf9fc 5418+ err = -EIO;
79b8bda9 5419+ AuIOErr("i%lu exists on b%d "
027c5e7a 5420+ "but not pseudo-linked\n",
79b8bda9 5421+ inode->i_ino, cpg->bdst);
1facf9fc 5422+ dput(h_src);
c2b27bf2 5423+ goto out_parent;
1facf9fc 5424+ }
5425+
5426+ if (do_dt) {
c2b27bf2
AM
5427+ a->h_path.dentry = h_parent;
5428+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
1facf9fc 5429+ }
86dc4139 5430+
c2b27bf2 5431+ a->h_path.dentry = h_dst;
523b37e3
AM
5432+ delegated = NULL;
5433+ err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
c2b27bf2 5434+ if (!err && au_ftest_cpup(cpg->flags, RENAME))
392086de 5435+ err = au_do_ren_after_cpup(cpg, &a->h_path);
1facf9fc 5436+ if (do_dt)
c2b27bf2 5437+ au_dtime_revert(&a->dt);
523b37e3
AM
5438+ if (unlikely(err == -EWOULDBLOCK)) {
5439+ pr_warn("cannot retry for NFSv4 delegation"
5440+ " for an internal link\n");
5441+ iput(delegated);
5442+ }
1facf9fc 5443+ dput(h_src);
c2b27bf2 5444+ goto out_parent;
1facf9fc 5445+ } else
5446+ /* todo: cpup_wh_file? */
5447+ /* udba work */
4a4d8108 5448+ au_update_ibrange(inode, /*do_put_zero*/1);
1facf9fc 5449+ }
5450+
86dc4139 5451+ isdir = S_ISDIR(inode->i_mode);
5afbbe0d 5452+ old_ibtop = au_ibtop(inode);
c2b27bf2 5453+ err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
1facf9fc 5454+ if (unlikely(err))
86dc4139 5455+ goto out_rev;
5527c038 5456+ dst_inode = d_inode(h_dst);
febd17d6 5457+ inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
86dc4139 5458+ /* todo: necessary? */
c2b27bf2 5459+ /* au_pin_hdir_unlock(cpg->pin); */
1facf9fc 5460+
c2b27bf2 5461+ err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
86dc4139
AM
5462+ if (unlikely(err)) {
5463+ /* todo: necessary? */
c2b27bf2 5464+ /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
febd17d6 5465+ inode_unlock(dst_inode);
86dc4139
AM
5466+ goto out_rev;
5467+ }
5468+
5afbbe0d 5469+ if (cpg->bdst < old_ibtop) {
86dc4139 5470+ if (S_ISREG(inode->i_mode)) {
c2b27bf2 5471+ err = au_dy_iaop(inode, cpg->bdst, dst_inode);
86dc4139 5472+ if (unlikely(err)) {
c2b27bf2
AM
5473+ /* ignore an error */
5474+ /* au_pin_hdir_relock(cpg->pin); */
febd17d6 5475+ inode_unlock(dst_inode);
86dc4139 5476+ goto out_rev;
4a4d8108 5477+ }
4a4d8108 5478+ }
5afbbe0d 5479+ au_set_ibtop(inode, cpg->bdst);
c2b27bf2 5480+ } else
5afbbe0d 5481+ au_set_ibbot(inode, cpg->bdst);
c2b27bf2 5482+ au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
86dc4139
AM
5483+ au_hi_flags(inode, isdir));
5484+
5485+ /* todo: necessary? */
c2b27bf2 5486+ /* err = au_pin_hdir_relock(cpg->pin); */
febd17d6 5487+ inode_unlock(dst_inode);
86dc4139
AM
5488+ if (unlikely(err))
5489+ goto out_rev;
5490+
5527c038 5491+ src_inode = d_inode(h_src);
86dc4139 5492+ if (!isdir
5527c038
JR
5493+ && (src_inode->i_nlink > 1
5494+ || src_inode->i_state & I_LINKABLE)
86dc4139 5495+ && plink)
c2b27bf2 5496+ au_plink_append(inode, cpg->bdst, h_dst);
86dc4139 5497+
c2b27bf2
AM
5498+ if (au_ftest_cpup(cpg->flags, RENAME)) {
5499+ a->h_path.dentry = h_dst;
392086de 5500+ err = au_do_ren_after_cpup(cpg, &a->h_path);
86dc4139
AM
5501+ }
5502+ if (!err)
c2b27bf2 5503+ goto out_parent; /* success */
1facf9fc 5504+
5505+ /* revert */
4a4d8108 5506+out_rev:
c2b27bf2
AM
5507+ a->h_path.dentry = h_parent;
5508+ au_dtime_store(&a->dt, dst_parent, &a->h_path);
5509+ a->h_path.dentry = h_dst;
86dc4139 5510+ rerr = 0;
5527c038 5511+ if (d_is_positive(h_dst)) {
523b37e3
AM
5512+ if (!isdir) {
5513+ /* no delegation since it is just created */
5514+ rerr = vfsub_unlink(h_dir, &a->h_path,
5515+ /*delegated*/NULL, /*force*/0);
5516+ } else
c2b27bf2 5517+ rerr = vfsub_rmdir(h_dir, &a->h_path);
86dc4139 5518+ }
c2b27bf2 5519+ au_dtime_revert(&a->dt);
1facf9fc 5520+ if (rerr) {
5521+ AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5522+ err = -EIO;
5523+ }
c2b27bf2 5524+out_parent:
1facf9fc 5525+ dput(dst_parent);
c2b27bf2
AM
5526+ kfree(a);
5527+out:
1facf9fc 5528+ return err;
5529+}
5530+
7e9cd9fe 5531+#if 0 /* reserved */
1facf9fc 5532+struct au_cpup_single_args {
5533+ int *errp;
c2b27bf2 5534+ struct au_cp_generic *cpg;
1facf9fc 5535+ struct dentry *dst_parent;
5536+};
5537+
5538+static void au_call_cpup_single(void *args)
5539+{
5540+ struct au_cpup_single_args *a = args;
86dc4139 5541+
c2b27bf2
AM
5542+ au_pin_hdir_acquire_nest(a->cpg->pin);
5543+ *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5544+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5545+}
c2b27bf2 5546+#endif
1facf9fc 5547+
53392da6
AM
5548+/*
5549+ * prevent SIGXFSZ in copy-up.
5550+ * testing CAP_MKNOD is for generic fs,
5551+ * but CAP_FSETID is for xfs only, currently.
5552+ */
86dc4139 5553+static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
53392da6
AM
5554+{
5555+ int do_sio;
86dc4139
AM
5556+ struct super_block *sb;
5557+ struct inode *h_dir;
53392da6
AM
5558+
5559+ do_sio = 0;
86dc4139 5560+ sb = au_pinned_parent(pin)->d_sb;
53392da6
AM
5561+ if (!au_wkq_test()
5562+ && (!au_sbi(sb)->si_plink_maint_pid
5563+ || au_plink_maint(sb, AuLock_NOPLM))) {
5564+ switch (mode & S_IFMT) {
5565+ case S_IFREG:
5566+ /* no condition about RLIMIT_FSIZE and the file size */
5567+ do_sio = 1;
5568+ break;
5569+ case S_IFCHR:
5570+ case S_IFBLK:
5571+ do_sio = !capable(CAP_MKNOD);
5572+ break;
5573+ }
5574+ if (!do_sio)
5575+ do_sio = ((mode & (S_ISUID | S_ISGID))
5576+ && !capable(CAP_FSETID));
86dc4139
AM
5577+ /* this workaround may be removed in the future */
5578+ if (!do_sio) {
5579+ h_dir = au_pinned_h_dir(pin);
5580+ do_sio = h_dir->i_mode & S_ISVTX;
5581+ }
53392da6
AM
5582+ }
5583+
5584+ return do_sio;
5585+}
5586+
7e9cd9fe 5587+#if 0 /* reserved */
c2b27bf2 5588+int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
1facf9fc 5589+{
5590+ int err, wkq_err;
1facf9fc 5591+ struct dentry *h_dentry;
5592+
c2b27bf2 5593+ h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5527c038 5594+ if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
c2b27bf2 5595+ err = au_cpup_single(cpg, dst_parent);
1facf9fc 5596+ else {
5597+ struct au_cpup_single_args args = {
5598+ .errp = &err,
c2b27bf2
AM
5599+ .cpg = cpg,
5600+ .dst_parent = dst_parent
1facf9fc 5601+ };
5602+ wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5603+ if (unlikely(wkq_err))
5604+ err = wkq_err;
5605+ }
5606+
5607+ return err;
5608+}
c2b27bf2 5609+#endif
1facf9fc 5610+
5611+/*
5612+ * copyup the @dentry from the first active lower branch to @bdst,
5613+ * using au_cpup_single().
5614+ */
c2b27bf2 5615+static int au_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5616+{
5617+ int err;
c2b27bf2
AM
5618+ unsigned int flags_orig;
5619+ struct dentry *dentry;
5620+
5621+ AuDebugOn(cpg->bsrc < 0);
1facf9fc 5622+
c2b27bf2 5623+ dentry = cpg->dentry;
86dc4139 5624+ DiMustWriteLock(dentry);
1facf9fc 5625+
c2b27bf2 5626+ err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
1facf9fc 5627+ if (!err) {
c2b27bf2
AM
5628+ flags_orig = cpg->flags;
5629+ au_fset_cpup(cpg->flags, RENAME);
5630+ err = au_cpup_single(cpg, NULL);
5631+ cpg->flags = flags_orig;
1facf9fc 5632+ if (!err)
5633+ return 0; /* success */
5634+
5635+ /* revert */
c2b27bf2 5636+ au_set_h_dptr(dentry, cpg->bdst, NULL);
5afbbe0d 5637+ au_set_dbtop(dentry, cpg->bsrc);
1facf9fc 5638+ }
5639+
5640+ return err;
5641+}
5642+
5643+struct au_cpup_simple_args {
5644+ int *errp;
c2b27bf2 5645+ struct au_cp_generic *cpg;
1facf9fc 5646+};
5647+
5648+static void au_call_cpup_simple(void *args)
5649+{
5650+ struct au_cpup_simple_args *a = args;
86dc4139 5651+
c2b27bf2
AM
5652+ au_pin_hdir_acquire_nest(a->cpg->pin);
5653+ *a->errp = au_cpup_simple(a->cpg);
5654+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5655+}
5656+
c2b27bf2 5657+static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
1facf9fc 5658+{
5659+ int err, wkq_err;
c2b27bf2
AM
5660+ struct dentry *dentry, *parent;
5661+ struct file *h_file;
1facf9fc 5662+ struct inode *h_dir;
5663+
c2b27bf2
AM
5664+ dentry = cpg->dentry;
5665+ h_file = NULL;
5666+ if (au_ftest_cpup(cpg->flags, HOPEN)) {
5667+ AuDebugOn(cpg->bsrc < 0);
392086de 5668+ h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
c2b27bf2
AM
5669+ err = PTR_ERR(h_file);
5670+ if (IS_ERR(h_file))
5671+ goto out;
5672+ }
5673+
1facf9fc 5674+ parent = dget_parent(dentry);
5527c038 5675+ h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
53392da6 5676+ if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
5527c038 5677+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5678+ err = au_cpup_simple(cpg);
1facf9fc 5679+ else {
5680+ struct au_cpup_simple_args args = {
5681+ .errp = &err,
c2b27bf2 5682+ .cpg = cpg
1facf9fc 5683+ };
5684+ wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5685+ if (unlikely(wkq_err))
5686+ err = wkq_err;
5687+ }
5688+
5689+ dput(parent);
c2b27bf2
AM
5690+ if (h_file)
5691+ au_h_open_post(dentry, cpg->bsrc, h_file);
5692+
5693+out:
1facf9fc 5694+ return err;
5695+}
5696+
c2b27bf2 5697+int au_sio_cpup_simple(struct au_cp_generic *cpg)
367653fa 5698+{
5afbbe0d 5699+ aufs_bindex_t bsrc, bbot;
c2b27bf2 5700+ struct dentry *dentry, *h_dentry;
367653fa 5701+
c2b27bf2
AM
5702+ if (cpg->bsrc < 0) {
5703+ dentry = cpg->dentry;
5afbbe0d
AM
5704+ bbot = au_dbbot(dentry);
5705+ for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
c2b27bf2
AM
5706+ h_dentry = au_h_dptr(dentry, bsrc);
5707+ if (h_dentry) {
5527c038 5708+ AuDebugOn(d_is_negative(h_dentry));
c2b27bf2
AM
5709+ break;
5710+ }
5711+ }
5afbbe0d 5712+ AuDebugOn(bsrc > bbot);
c2b27bf2 5713+ cpg->bsrc = bsrc;
367653fa 5714+ }
c2b27bf2
AM
5715+ AuDebugOn(cpg->bsrc <= cpg->bdst);
5716+ return au_do_sio_cpup_simple(cpg);
5717+}
367653fa 5718+
c2b27bf2
AM
5719+int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5720+{
5721+ AuDebugOn(cpg->bdst <= cpg->bsrc);
5722+ return au_do_sio_cpup_simple(cpg);
367653fa
AM
5723+}
5724+
1facf9fc 5725+/* ---------------------------------------------------------------------- */
5726+
5727+/*
5728+ * copyup the deleted file for writing.
5729+ */
c2b27bf2
AM
5730+static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5731+ struct file *file)
1facf9fc 5732+{
5733+ int err;
c2b27bf2
AM
5734+ unsigned int flags_orig;
5735+ aufs_bindex_t bsrc_orig;
c2b27bf2 5736+ struct au_dinfo *dinfo;
5afbbe0d
AM
5737+ struct {
5738+ struct au_hdentry *hd;
5739+ struct dentry *h_dentry;
5740+ } hdst, hsrc;
1facf9fc 5741+
c2b27bf2 5742+ dinfo = au_di(cpg->dentry);
1308ab2a 5743+ AuRwMustWriteLock(&dinfo->di_rwsem);
5744+
c2b27bf2 5745+ bsrc_orig = cpg->bsrc;
5afbbe0d
AM
5746+ cpg->bsrc = dinfo->di_btop;
5747+ hdst.hd = au_hdentry(dinfo, cpg->bdst);
5748+ hdst.h_dentry = hdst.hd->hd_dentry;
5749+ hdst.hd->hd_dentry = wh_dentry;
5750+ dinfo->di_btop = cpg->bdst;
5751+
5752+ hsrc.h_dentry = NULL;
027c5e7a 5753+ if (file) {
5afbbe0d
AM
5754+ hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5755+ hsrc.h_dentry = hsrc.hd->hd_dentry;
5756+ hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
027c5e7a 5757+ }
c2b27bf2
AM
5758+ flags_orig = cpg->flags;
5759+ cpg->flags = !AuCpup_DTIME;
5760+ err = au_cpup_single(cpg, /*h_parent*/NULL);
5761+ cpg->flags = flags_orig;
027c5e7a
AM
5762+ if (file) {
5763+ if (!err)
5764+ err = au_reopen_nondir(file);
5afbbe0d 5765+ hsrc.hd->hd_dentry = hsrc.h_dentry;
1facf9fc 5766+ }
5afbbe0d
AM
5767+ hdst.hd->hd_dentry = hdst.h_dentry;
5768+ dinfo->di_btop = cpg->bsrc;
c2b27bf2 5769+ cpg->bsrc = bsrc_orig;
1facf9fc 5770+
5771+ return err;
5772+}
5773+
c2b27bf2 5774+static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 5775+{
5776+ int err;
c2b27bf2 5777+ aufs_bindex_t bdst;
1facf9fc 5778+ struct au_dtime dt;
c2b27bf2 5779+ struct dentry *dentry, *parent, *h_parent, *wh_dentry;
1facf9fc 5780+ struct au_branch *br;
5781+ struct path h_path;
5782+
c2b27bf2
AM
5783+ dentry = cpg->dentry;
5784+ bdst = cpg->bdst;
1facf9fc 5785+ br = au_sbr(dentry->d_sb, bdst);
5786+ parent = dget_parent(dentry);
5787+ h_parent = au_h_dptr(parent, bdst);
5788+ wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5789+ err = PTR_ERR(wh_dentry);
5790+ if (IS_ERR(wh_dentry))
5791+ goto out;
5792+
5793+ h_path.dentry = h_parent;
86dc4139 5794+ h_path.mnt = au_br_mnt(br);
1facf9fc 5795+ au_dtime_store(&dt, parent, &h_path);
c2b27bf2 5796+ err = au_do_cpup_wh(cpg, wh_dentry, file);
1facf9fc 5797+ if (unlikely(err))
5798+ goto out_wh;
5799+
5800+ dget(wh_dentry);
5801+ h_path.dentry = wh_dentry;
2000de60 5802+ if (!d_is_dir(wh_dentry)) {
523b37e3 5803+ /* no delegation since it is just created */
5527c038 5804+ err = vfsub_unlink(d_inode(h_parent), &h_path,
523b37e3
AM
5805+ /*delegated*/NULL, /*force*/0);
5806+ } else
5527c038 5807+ err = vfsub_rmdir(d_inode(h_parent), &h_path);
1facf9fc 5808+ if (unlikely(err)) {
523b37e3
AM
5809+ AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5810+ wh_dentry, err);
1facf9fc 5811+ err = -EIO;
5812+ }
5813+ au_dtime_revert(&dt);
5527c038 5814+ au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
1facf9fc 5815+
4f0767ce 5816+out_wh:
1facf9fc 5817+ dput(wh_dentry);
4f0767ce 5818+out:
1facf9fc 5819+ dput(parent);
5820+ return err;
5821+}
5822+
5823+struct au_cpup_wh_args {
5824+ int *errp;
c2b27bf2 5825+ struct au_cp_generic *cpg;
1facf9fc 5826+ struct file *file;
5827+};
5828+
5829+static void au_call_cpup_wh(void *args)
5830+{
5831+ struct au_cpup_wh_args *a = args;
86dc4139 5832+
c2b27bf2
AM
5833+ au_pin_hdir_acquire_nest(a->cpg->pin);
5834+ *a->errp = au_cpup_wh(a->cpg, a->file);
5835+ au_pin_hdir_release(a->cpg->pin);
1facf9fc 5836+}
5837+
c2b27bf2 5838+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
1facf9fc 5839+{
5840+ int err, wkq_err;
c2b27bf2 5841+ aufs_bindex_t bdst;
c1595e42 5842+ struct dentry *dentry, *parent, *h_orph, *h_parent;
86dc4139 5843+ struct inode *dir, *h_dir, *h_tmpdir;
1facf9fc 5844+ struct au_wbr *wbr;
c2b27bf2 5845+ struct au_pin wh_pin, *pin_orig;
1facf9fc 5846+
c2b27bf2
AM
5847+ dentry = cpg->dentry;
5848+ bdst = cpg->bdst;
1facf9fc 5849+ parent = dget_parent(dentry);
5527c038 5850+ dir = d_inode(parent);
1facf9fc 5851+ h_orph = NULL;
5852+ h_parent = NULL;
5853+ h_dir = au_igrab(au_h_iptr(dir, bdst));
5854+ h_tmpdir = h_dir;
c2b27bf2 5855+ pin_orig = NULL;
1facf9fc 5856+ if (!h_dir->i_nlink) {
5857+ wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
5858+ h_orph = wbr->wbr_orph;
5859+
5860+ h_parent = dget(au_h_dptr(parent, bdst));
1facf9fc 5861+ au_set_h_dptr(parent, bdst, dget(h_orph));
5527c038 5862+ h_tmpdir = d_inode(h_orph);
1facf9fc 5863+ au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
5864+
febd17d6 5865+ inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
4a4d8108 5866+ /* todo: au_h_open_pre()? */
86dc4139 5867+
c2b27bf2 5868+ pin_orig = cpg->pin;
86dc4139 5869+ au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
c2b27bf2
AM
5870+ AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
5871+ cpg->pin = &wh_pin;
1facf9fc 5872+ }
5873+
53392da6 5874+ if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
5527c038 5875+ && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
c2b27bf2 5876+ err = au_cpup_wh(cpg, file);
1facf9fc 5877+ else {
5878+ struct au_cpup_wh_args args = {
5879+ .errp = &err,
c2b27bf2
AM
5880+ .cpg = cpg,
5881+ .file = file
1facf9fc 5882+ };
5883+ wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
5884+ if (unlikely(wkq_err))
5885+ err = wkq_err;
5886+ }
5887+
5888+ if (h_orph) {
febd17d6 5889+ inode_unlock(h_tmpdir);
4a4d8108 5890+ /* todo: au_h_open_post()? */
1facf9fc 5891+ au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
1facf9fc 5892+ au_set_h_dptr(parent, bdst, h_parent);
c2b27bf2
AM
5893+ AuDebugOn(!pin_orig);
5894+ cpg->pin = pin_orig;
1facf9fc 5895+ }
5896+ iput(h_dir);
5897+ dput(parent);
5898+
5899+ return err;
5900+}
5901+
5902+/* ---------------------------------------------------------------------- */
5903+
5904+/*
5905+ * generic routine for both of copy-up and copy-down.
5906+ */
5907+/* cf. revalidate function in file.c */
5908+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
5909+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 5910+ struct au_pin *pin,
1facf9fc 5911+ struct dentry *h_parent, void *arg),
5912+ void *arg)
5913+{
5914+ int err;
5915+ struct au_pin pin;
5527c038 5916+ struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
1facf9fc 5917+
5918+ err = 0;
5919+ parent = dget_parent(dentry);
5920+ if (IS_ROOT(parent))
5921+ goto out;
5922+
5923+ au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
5924+ au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
5925+
5926+ /* do not use au_dpage */
5927+ real_parent = parent;
5928+ while (1) {
5929+ dput(parent);
5930+ parent = dget_parent(dentry);
5931+ h_parent = au_h_dptr(parent, bdst);
5932+ if (h_parent)
5933+ goto out; /* success */
5934+
5935+ /* find top dir which is necessary to cpup */
5936+ do {
5937+ d = parent;
5938+ dput(parent);
5939+ parent = dget_parent(d);
5940+ di_read_lock_parent3(parent, !AuLock_IR);
5941+ h_parent = au_h_dptr(parent, bdst);
5942+ di_read_unlock(parent, !AuLock_IR);
5943+ } while (!h_parent);
5944+
5945+ if (d != real_parent)
5946+ di_write_lock_child3(d);
5947+
5948+ /* somebody else might create while we were sleeping */
5527c038
JR
5949+ h_dentry = au_h_dptr(d, bdst);
5950+ if (!h_dentry || d_is_negative(h_dentry)) {
5951+ if (h_dentry)
5afbbe0d 5952+ au_update_dbtop(d);
1facf9fc 5953+
5954+ au_pin_set_dentry(&pin, d);
5955+ err = au_do_pin(&pin);
5956+ if (!err) {
86dc4139 5957+ err = cp(d, bdst, &pin, h_parent, arg);
1facf9fc 5958+ au_unpin(&pin);
5959+ }
5960+ }
5961+
5962+ if (d != real_parent)
5963+ di_write_unlock(d);
5964+ if (unlikely(err))
5965+ break;
5966+ }
5967+
4f0767ce 5968+out:
1facf9fc 5969+ dput(parent);
5970+ return err;
5971+}
5972+
5973+static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 5974+ struct au_pin *pin,
2000de60 5975+ struct dentry *h_parent __maybe_unused,
1facf9fc 5976+ void *arg __maybe_unused)
5977+{
c2b27bf2
AM
5978+ struct au_cp_generic cpg = {
5979+ .dentry = dentry,
5980+ .bdst = bdst,
5981+ .bsrc = -1,
5982+ .len = 0,
5983+ .pin = pin,
5984+ .flags = AuCpup_DTIME
5985+ };
5986+ return au_sio_cpup_simple(&cpg);
1facf9fc 5987+}
5988+
5989+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5990+{
5991+ return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
5992+}
5993+
5994+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
5995+{
5996+ int err;
5997+ struct dentry *parent;
5998+ struct inode *dir;
5999+
6000+ parent = dget_parent(dentry);
5527c038 6001+ dir = d_inode(parent);
1facf9fc 6002+ err = 0;
6003+ if (au_h_iptr(dir, bdst))
6004+ goto out;
6005+
6006+ di_read_unlock(parent, AuLock_IR);
6007+ di_write_lock_parent(parent);
6008+ /* someone else might change our inode while we were sleeping */
6009+ if (!au_h_iptr(dir, bdst))
6010+ err = au_cpup_dirs(dentry, bdst);
6011+ di_downgrade_lock(parent, AuLock_IR);
6012+
4f0767ce 6013+out:
1facf9fc 6014+ dput(parent);
6015+ return err;
6016+}
7f207e10
AM
6017diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6018--- /usr/share/empty/fs/aufs/cpup.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6019+++ linux/fs/aufs/cpup.h 2016-07-25 19:05:34.811159821 +0200
523b37e3 6020@@ -0,0 +1,94 @@
1facf9fc 6021+/*
8cdd5066 6022+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6023+ *
6024+ * This program, aufs is free software; you can redistribute it and/or modify
6025+ * it under the terms of the GNU General Public License as published by
6026+ * the Free Software Foundation; either version 2 of the License, or
6027+ * (at your option) any later version.
dece6358
AM
6028+ *
6029+ * This program is distributed in the hope that it will be useful,
6030+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6031+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6032+ * GNU General Public License for more details.
6033+ *
6034+ * You should have received a copy of the GNU General Public License
523b37e3 6035+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6036+ */
6037+
6038+/*
6039+ * copy-up/down functions
6040+ */
6041+
6042+#ifndef __AUFS_CPUP_H__
6043+#define __AUFS_CPUP_H__
6044+
6045+#ifdef __KERNEL__
6046+
dece6358 6047+#include <linux/path.h>
1facf9fc 6048+
dece6358
AM
6049+struct inode;
6050+struct file;
86dc4139 6051+struct au_pin;
dece6358 6052+
86dc4139 6053+void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
1facf9fc 6054+void au_cpup_attr_timesizes(struct inode *inode);
6055+void au_cpup_attr_nlink(struct inode *inode, int force);
6056+void au_cpup_attr_changeable(struct inode *inode);
6057+void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6058+void au_cpup_attr_all(struct inode *inode, int force);
6059+
6060+/* ---------------------------------------------------------------------- */
6061+
c2b27bf2
AM
6062+struct au_cp_generic {
6063+ struct dentry *dentry;
6064+ aufs_bindex_t bdst, bsrc;
6065+ loff_t len;
6066+ struct au_pin *pin;
6067+ unsigned int flags;
6068+};
6069+
1facf9fc 6070+/* cpup flags */
392086de
AM
6071+#define AuCpup_DTIME 1 /* do dtime_store/revert */
6072+#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
6073+ for link(2) */
6074+#define AuCpup_RENAME (1 << 2) /* rename after cpup */
6075+#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
6076+ cpup */
6077+#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
6078+ existing entry */
6079+#define AuCpup_RWDST (1 << 5) /* force write target even if
6080+ the branch is marked as RO */
c2b27bf2 6081+
1facf9fc 6082+#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
7f207e10
AM
6083+#define au_fset_cpup(flags, name) \
6084+ do { (flags) |= AuCpup_##name; } while (0)
6085+#define au_fclr_cpup(flags, name) \
6086+ do { (flags) &= ~AuCpup_##name; } while (0)
1facf9fc 6087+
6088+int au_copy_file(struct file *dst, struct file *src, loff_t len);
c2b27bf2
AM
6089+int au_sio_cpup_simple(struct au_cp_generic *cpg);
6090+int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6091+int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
1facf9fc 6092+
6093+int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6094+ int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 6095+ struct au_pin *pin,
1facf9fc 6096+ struct dentry *h_parent, void *arg),
6097+ void *arg);
6098+int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6099+int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6100+
6101+/* ---------------------------------------------------------------------- */
6102+
6103+/* keep timestamps when copyup */
6104+struct au_dtime {
6105+ struct dentry *dt_dentry;
6106+ struct path dt_h_path;
6107+ struct timespec dt_atime, dt_mtime;
6108+};
6109+void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6110+ struct path *h_path);
6111+void au_dtime_revert(struct au_dtime *dt);
6112+
6113+#endif /* __KERNEL__ */
6114+#endif /* __AUFS_CPUP_H__ */
7f207e10
AM
6115diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6116--- /usr/share/empty/fs/aufs/dbgaufs.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6117+++ linux/fs/aufs/dbgaufs.c 2016-07-25 19:05:34.811159821 +0200
523b37e3 6118@@ -0,0 +1,432 @@
1facf9fc 6119+/*
8cdd5066 6120+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6121+ *
6122+ * This program, aufs is free software; you can redistribute it and/or modify
6123+ * it under the terms of the GNU General Public License as published by
6124+ * the Free Software Foundation; either version 2 of the License, or
6125+ * (at your option) any later version.
dece6358
AM
6126+ *
6127+ * This program is distributed in the hope that it will be useful,
6128+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6129+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6130+ * GNU General Public License for more details.
6131+ *
6132+ * You should have received a copy of the GNU General Public License
523b37e3 6133+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6134+ */
6135+
6136+/*
6137+ * debugfs interface
6138+ */
6139+
6140+#include <linux/debugfs.h>
6141+#include "aufs.h"
6142+
6143+#ifndef CONFIG_SYSFS
6144+#error DEBUG_FS depends upon SYSFS
6145+#endif
6146+
6147+static struct dentry *dbgaufs;
6148+static const mode_t dbgaufs_mode = S_IRUSR | S_IRGRP | S_IROTH;
6149+
6150+/* 20 is max digits length of ulong 64 */
6151+struct dbgaufs_arg {
6152+ int n;
6153+ char a[20 * 4];
6154+};
6155+
6156+/*
6157+ * common function for all XINO files
6158+ */
6159+static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6160+ struct file *file)
6161+{
6162+ kfree(file->private_data);
6163+ return 0;
6164+}
6165+
6166+static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt)
6167+{
6168+ int err;
6169+ struct kstat st;
6170+ struct dbgaufs_arg *p;
6171+
6172+ err = -ENOMEM;
6173+ p = kmalloc(sizeof(*p), GFP_NOFS);
6174+ if (unlikely(!p))
6175+ goto out;
6176+
6177+ err = 0;
6178+ p->n = 0;
6179+ file->private_data = p;
6180+ if (!xf)
6181+ goto out;
6182+
c06a8ce3 6183+ err = vfs_getattr(&xf->f_path, &st);
1facf9fc 6184+ if (!err) {
6185+ if (do_fcnt)
6186+ p->n = snprintf
6187+ (p->a, sizeof(p->a), "%ld, %llux%lu %lld\n",
6188+ (long)file_count(xf), st.blocks, st.blksize,
6189+ (long long)st.size);
6190+ else
6191+ p->n = snprintf(p->a, sizeof(p->a), "%llux%lu %lld\n",
6192+ st.blocks, st.blksize,
6193+ (long long)st.size);
6194+ AuDebugOn(p->n >= sizeof(p->a));
6195+ } else {
6196+ p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6197+ err = 0;
6198+ }
6199+
4f0767ce 6200+out:
1facf9fc 6201+ return err;
6202+
6203+}
6204+
6205+static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6206+ size_t count, loff_t *ppos)
6207+{
6208+ struct dbgaufs_arg *p;
6209+
6210+ p = file->private_data;
6211+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6212+}
6213+
6214+/* ---------------------------------------------------------------------- */
6215+
86dc4139
AM
6216+struct dbgaufs_plink_arg {
6217+ int n;
6218+ char a[];
6219+};
6220+
6221+static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6222+ struct file *file)
6223+{
6224+ free_page((unsigned long)file->private_data);
6225+ return 0;
6226+}
6227+
6228+static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6229+{
6230+ int err, i, limit;
6231+ unsigned long n, sum;
6232+ struct dbgaufs_plink_arg *p;
6233+ struct au_sbinfo *sbinfo;
6234+ struct super_block *sb;
6235+ struct au_sphlhead *sphl;
6236+
6237+ err = -ENOMEM;
6238+ p = (void *)get_zeroed_page(GFP_NOFS);
6239+ if (unlikely(!p))
6240+ goto out;
6241+
6242+ err = -EFBIG;
6243+ sbinfo = inode->i_private;
6244+ sb = sbinfo->si_sb;
6245+ si_noflush_read_lock(sb);
6246+ if (au_opt_test(au_mntflags(sb), PLINK)) {
6247+ limit = PAGE_SIZE - sizeof(p->n);
6248+
6249+ /* the number of buckets */
6250+ n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6251+ p->n += n;
6252+ limit -= n;
6253+
6254+ sum = 0;
6255+ for (i = 0, sphl = sbinfo->si_plink;
6256+ i < AuPlink_NHASH;
6257+ i++, sphl++) {
6258+ n = au_sphl_count(sphl);
6259+ sum += n;
6260+
6261+ n = snprintf(p->a + p->n, limit, "%lu ", n);
6262+ p->n += n;
6263+ limit -= n;
6264+ if (unlikely(limit <= 0))
6265+ goto out_free;
6266+ }
6267+ p->a[p->n - 1] = '\n';
6268+
6269+ /* the sum of plinks */
6270+ n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6271+ p->n += n;
6272+ limit -= n;
6273+ if (unlikely(limit <= 0))
6274+ goto out_free;
6275+ } else {
6276+#define str "1\n0\n0\n"
6277+ p->n = sizeof(str) - 1;
6278+ strcpy(p->a, str);
6279+#undef str
6280+ }
6281+ si_read_unlock(sb);
6282+
6283+ err = 0;
6284+ file->private_data = p;
6285+ goto out; /* success */
6286+
6287+out_free:
6288+ free_page((unsigned long)p);
6289+out:
6290+ return err;
6291+}
6292+
6293+static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6294+ size_t count, loff_t *ppos)
6295+{
6296+ struct dbgaufs_plink_arg *p;
6297+
6298+ p = file->private_data;
6299+ return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6300+}
6301+
6302+static const struct file_operations dbgaufs_plink_fop = {
6303+ .owner = THIS_MODULE,
6304+ .open = dbgaufs_plink_open,
6305+ .release = dbgaufs_plink_release,
6306+ .read = dbgaufs_plink_read
6307+};
6308+
6309+/* ---------------------------------------------------------------------- */
6310+
1facf9fc 6311+static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6312+{
6313+ int err;
6314+ struct au_sbinfo *sbinfo;
6315+ struct super_block *sb;
6316+
6317+ sbinfo = inode->i_private;
6318+ sb = sbinfo->si_sb;
6319+ si_noflush_read_lock(sb);
6320+ err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0);
6321+ si_read_unlock(sb);
6322+ return err;
6323+}
6324+
6325+static const struct file_operations dbgaufs_xib_fop = {
4a4d8108 6326+ .owner = THIS_MODULE,
1facf9fc 6327+ .open = dbgaufs_xib_open,
6328+ .release = dbgaufs_xi_release,
6329+ .read = dbgaufs_xi_read
6330+};
6331+
6332+/* ---------------------------------------------------------------------- */
6333+
6334+#define DbgaufsXi_PREFIX "xi"
6335+
6336+static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6337+{
6338+ int err;
6339+ long l;
6340+ struct au_sbinfo *sbinfo;
6341+ struct super_block *sb;
6342+ struct file *xf;
6343+ struct qstr *name;
6344+
6345+ err = -ENOENT;
6346+ xf = NULL;
2000de60 6347+ name = &file->f_path.dentry->d_name;
1facf9fc 6348+ if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6349+ || memcmp(name->name, DbgaufsXi_PREFIX,
6350+ sizeof(DbgaufsXi_PREFIX) - 1)))
6351+ goto out;
9dbd164d 6352+ err = kstrtol(name->name + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
1facf9fc 6353+ if (unlikely(err))
6354+ goto out;
6355+
6356+ sbinfo = inode->i_private;
6357+ sb = sbinfo->si_sb;
6358+ si_noflush_read_lock(sb);
5afbbe0d 6359+ if (l <= au_sbbot(sb)) {
1facf9fc 6360+ xf = au_sbr(sb, (aufs_bindex_t)l)->br_xino.xi_file;
6361+ err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1);
6362+ } else
6363+ err = -ENOENT;
6364+ si_read_unlock(sb);
6365+
4f0767ce 6366+out:
1facf9fc 6367+ return err;
6368+}
6369+
6370+static const struct file_operations dbgaufs_xino_fop = {
4a4d8108 6371+ .owner = THIS_MODULE,
1facf9fc 6372+ .open = dbgaufs_xino_open,
6373+ .release = dbgaufs_xi_release,
6374+ .read = dbgaufs_xi_read
6375+};
6376+
6377+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6378+{
5afbbe0d 6379+ aufs_bindex_t bbot;
1facf9fc 6380+ struct au_branch *br;
6381+ struct au_xino_file *xi;
6382+
6383+ if (!au_sbi(sb)->si_dbgaufs)
6384+ return;
6385+
5afbbe0d
AM
6386+ bbot = au_sbbot(sb);
6387+ for (; bindex <= bbot; bindex++) {
1facf9fc 6388+ br = au_sbr(sb, bindex);
6389+ xi = &br->br_xino;
c06a8ce3
AM
6390+ debugfs_remove(xi->xi_dbgaufs);
6391+ xi->xi_dbgaufs = NULL;
1facf9fc 6392+ }
6393+}
6394+
6395+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
6396+{
6397+ struct au_sbinfo *sbinfo;
6398+ struct dentry *parent;
6399+ struct au_branch *br;
6400+ struct au_xino_file *xi;
5afbbe0d 6401+ aufs_bindex_t bbot;
1facf9fc 6402+ char name[sizeof(DbgaufsXi_PREFIX) + 5]; /* "xi" bindex NULL */
6403+
6404+ sbinfo = au_sbi(sb);
6405+ parent = sbinfo->si_dbgaufs;
6406+ if (!parent)
6407+ return;
6408+
5afbbe0d
AM
6409+ bbot = au_sbbot(sb);
6410+ for (; bindex <= bbot; bindex++) {
1facf9fc 6411+ snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6412+ br = au_sbr(sb, bindex);
6413+ xi = &br->br_xino;
6414+ AuDebugOn(xi->xi_dbgaufs);
6415+ xi->xi_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6416+ sbinfo, &dbgaufs_xino_fop);
6417+ /* ignore an error */
6418+ if (unlikely(!xi->xi_dbgaufs))
6419+ AuWarn1("failed %s under debugfs\n", name);
6420+ }
6421+}
6422+
6423+/* ---------------------------------------------------------------------- */
6424+
6425+#ifdef CONFIG_AUFS_EXPORT
6426+static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6427+{
6428+ int err;
6429+ struct au_sbinfo *sbinfo;
6430+ struct super_block *sb;
6431+
6432+ sbinfo = inode->i_private;
6433+ sb = sbinfo->si_sb;
6434+ si_noflush_read_lock(sb);
6435+ err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0);
6436+ si_read_unlock(sb);
6437+ return err;
6438+}
6439+
6440+static const struct file_operations dbgaufs_xigen_fop = {
4a4d8108 6441+ .owner = THIS_MODULE,
1facf9fc 6442+ .open = dbgaufs_xigen_open,
6443+ .release = dbgaufs_xi_release,
6444+ .read = dbgaufs_xi_read
6445+};
6446+
6447+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6448+{
6449+ int err;
6450+
dece6358 6451+ /*
c1595e42 6452+ * This function is a dynamic '__init' function actually,
dece6358
AM
6453+ * so the tiny check for si_rwsem is unnecessary.
6454+ */
6455+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6456+
1facf9fc 6457+ err = -EIO;
6458+ sbinfo->si_dbgaufs_xigen = debugfs_create_file
6459+ ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6460+ &dbgaufs_xigen_fop);
6461+ if (sbinfo->si_dbgaufs_xigen)
6462+ err = 0;
6463+
6464+ return err;
6465+}
6466+#else
6467+static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6468+{
6469+ return 0;
6470+}
6471+#endif /* CONFIG_AUFS_EXPORT */
6472+
6473+/* ---------------------------------------------------------------------- */
6474+
6475+void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6476+{
dece6358 6477+ /*
7e9cd9fe 6478+ * This function is a dynamic '__fin' function actually,
dece6358
AM
6479+ * so the tiny check for si_rwsem is unnecessary.
6480+ */
6481+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6482+
1facf9fc 6483+ debugfs_remove_recursive(sbinfo->si_dbgaufs);
6484+ sbinfo->si_dbgaufs = NULL;
6485+ kobject_put(&sbinfo->si_kobj);
6486+}
6487+
6488+int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6489+{
6490+ int err;
6491+ char name[SysaufsSiNameLen];
6492+
dece6358 6493+ /*
c1595e42 6494+ * This function is a dynamic '__init' function actually,
dece6358
AM
6495+ * so the tiny check for si_rwsem is unnecessary.
6496+ */
6497+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6498+
1facf9fc 6499+ err = -ENOENT;
6500+ if (!dbgaufs) {
6501+ AuErr1("/debug/aufs is uninitialized\n");
6502+ goto out;
6503+ }
6504+
6505+ err = -EIO;
6506+ sysaufs_name(sbinfo, name);
6507+ sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6508+ if (unlikely(!sbinfo->si_dbgaufs))
6509+ goto out;
6510+ kobject_get(&sbinfo->si_kobj);
6511+
6512+ sbinfo->si_dbgaufs_xib = debugfs_create_file
6513+ ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6514+ &dbgaufs_xib_fop);
6515+ if (unlikely(!sbinfo->si_dbgaufs_xib))
6516+ goto out_dir;
6517+
86dc4139
AM
6518+ sbinfo->si_dbgaufs_plink = debugfs_create_file
6519+ ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6520+ &dbgaufs_plink_fop);
6521+ if (unlikely(!sbinfo->si_dbgaufs_plink))
6522+ goto out_dir;
6523+
1facf9fc 6524+ err = dbgaufs_xigen_init(sbinfo);
6525+ if (!err)
6526+ goto out; /* success */
6527+
4f0767ce 6528+out_dir:
1facf9fc 6529+ dbgaufs_si_fin(sbinfo);
4f0767ce 6530+out:
1facf9fc 6531+ return err;
6532+}
6533+
6534+/* ---------------------------------------------------------------------- */
6535+
6536+void dbgaufs_fin(void)
6537+{
6538+ debugfs_remove(dbgaufs);
6539+}
6540+
6541+int __init dbgaufs_init(void)
6542+{
6543+ int err;
6544+
6545+ err = -EIO;
6546+ dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6547+ if (dbgaufs)
6548+ err = 0;
6549+ return err;
6550+}
7f207e10
AM
6551diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6552--- /usr/share/empty/fs/aufs/dbgaufs.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6553+++ linux/fs/aufs/dbgaufs.h 2016-07-25 19:05:34.811159821 +0200
523b37e3 6554@@ -0,0 +1,48 @@
1facf9fc 6555+/*
8cdd5066 6556+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6557+ *
6558+ * This program, aufs is free software; you can redistribute it and/or modify
6559+ * it under the terms of the GNU General Public License as published by
6560+ * the Free Software Foundation; either version 2 of the License, or
6561+ * (at your option) any later version.
dece6358
AM
6562+ *
6563+ * This program is distributed in the hope that it will be useful,
6564+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6565+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6566+ * GNU General Public License for more details.
6567+ *
6568+ * You should have received a copy of the GNU General Public License
523b37e3 6569+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6570+ */
6571+
6572+/*
6573+ * debugfs interface
6574+ */
6575+
6576+#ifndef __DBGAUFS_H__
6577+#define __DBGAUFS_H__
6578+
6579+#ifdef __KERNEL__
6580+
dece6358 6581+struct super_block;
1facf9fc 6582+struct au_sbinfo;
dece6358 6583+
1facf9fc 6584+#ifdef CONFIG_DEBUG_FS
6585+/* dbgaufs.c */
6586+void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6587+void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
6588+void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6589+int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6590+void dbgaufs_fin(void);
6591+int __init dbgaufs_init(void);
1facf9fc 6592+#else
4a4d8108
AM
6593+AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6594+AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
6595+AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6596+AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6597+AuStubVoid(dbgaufs_fin, void)
6598+AuStubInt0(__init dbgaufs_init, void)
1facf9fc 6599+#endif /* CONFIG_DEBUG_FS */
6600+
6601+#endif /* __KERNEL__ */
6602+#endif /* __DBGAUFS_H__ */
7f207e10
AM
6603diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6604--- /usr/share/empty/fs/aufs/dcsub.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6605+++ linux/fs/aufs/dcsub.c 2016-07-25 19:05:34.811159821 +0200
c1595e42 6606@@ -0,0 +1,224 @@
1facf9fc 6607+/*
8cdd5066 6608+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6609+ *
6610+ * This program, aufs is free software; you can redistribute it and/or modify
6611+ * it under the terms of the GNU General Public License as published by
6612+ * the Free Software Foundation; either version 2 of the License, or
6613+ * (at your option) any later version.
dece6358
AM
6614+ *
6615+ * This program is distributed in the hope that it will be useful,
6616+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6617+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6618+ * GNU General Public License for more details.
6619+ *
6620+ * You should have received a copy of the GNU General Public License
523b37e3 6621+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6622+ */
6623+
6624+/*
6625+ * sub-routines for dentry cache
6626+ */
6627+
6628+#include "aufs.h"
6629+
6630+static void au_dpage_free(struct au_dpage *dpage)
6631+{
6632+ int i;
6633+ struct dentry **p;
6634+
6635+ p = dpage->dentries;
6636+ for (i = 0; i < dpage->ndentry; i++)
6637+ dput(*p++);
6638+ free_page((unsigned long)dpage->dentries);
6639+}
6640+
6641+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6642+{
6643+ int err;
6644+ void *p;
6645+
6646+ err = -ENOMEM;
6647+ dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6648+ if (unlikely(!dpages->dpages))
6649+ goto out;
6650+
6651+ p = (void *)__get_free_page(gfp);
6652+ if (unlikely(!p))
6653+ goto out_dpages;
6654+
6655+ dpages->dpages[0].ndentry = 0;
6656+ dpages->dpages[0].dentries = p;
6657+ dpages->ndpage = 1;
6658+ return 0; /* success */
6659+
4f0767ce 6660+out_dpages:
1facf9fc 6661+ kfree(dpages->dpages);
4f0767ce 6662+out:
1facf9fc 6663+ return err;
6664+}
6665+
6666+void au_dpages_free(struct au_dcsub_pages *dpages)
6667+{
6668+ int i;
6669+ struct au_dpage *p;
6670+
6671+ p = dpages->dpages;
6672+ for (i = 0; i < dpages->ndpage; i++)
6673+ au_dpage_free(p++);
6674+ kfree(dpages->dpages);
6675+}
6676+
6677+static int au_dpages_append(struct au_dcsub_pages *dpages,
6678+ struct dentry *dentry, gfp_t gfp)
6679+{
6680+ int err, sz;
6681+ struct au_dpage *dpage;
6682+ void *p;
6683+
6684+ dpage = dpages->dpages + dpages->ndpage - 1;
6685+ sz = PAGE_SIZE / sizeof(dentry);
6686+ if (unlikely(dpage->ndentry >= sz)) {
6687+ AuLabel(new dpage);
6688+ err = -ENOMEM;
6689+ sz = dpages->ndpage * sizeof(*dpages->dpages);
6690+ p = au_kzrealloc(dpages->dpages, sz,
6691+ sz + sizeof(*dpages->dpages), gfp);
6692+ if (unlikely(!p))
6693+ goto out;
6694+
6695+ dpages->dpages = p;
6696+ dpage = dpages->dpages + dpages->ndpage;
6697+ p = (void *)__get_free_page(gfp);
6698+ if (unlikely(!p))
6699+ goto out;
6700+
6701+ dpage->ndentry = 0;
6702+ dpage->dentries = p;
6703+ dpages->ndpage++;
6704+ }
6705+
c1595e42 6706+ AuDebugOn(au_dcount(dentry) <= 0);
027c5e7a 6707+ dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
1facf9fc 6708+ return 0; /* success */
6709+
4f0767ce 6710+out:
1facf9fc 6711+ return err;
6712+}
6713+
c1595e42
JR
6714+/* todo: BAD approach */
6715+/* copied from linux/fs/dcache.c */
6716+enum d_walk_ret {
6717+ D_WALK_CONTINUE,
6718+ D_WALK_QUIT,
6719+ D_WALK_NORETRY,
6720+ D_WALK_SKIP,
6721+};
6722+
6723+extern void d_walk(struct dentry *parent, void *data,
6724+ enum d_walk_ret (*enter)(void *, struct dentry *),
6725+ void (*finish)(void *));
6726+
6727+struct ac_dpages_arg {
1facf9fc 6728+ int err;
c1595e42
JR
6729+ struct au_dcsub_pages *dpages;
6730+ struct super_block *sb;
6731+ au_dpages_test test;
6732+ void *arg;
6733+};
1facf9fc 6734+
c1595e42
JR
6735+static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
6736+{
6737+ enum d_walk_ret ret;
6738+ struct ac_dpages_arg *arg = _arg;
1facf9fc 6739+
c1595e42
JR
6740+ ret = D_WALK_CONTINUE;
6741+ if (dentry->d_sb == arg->sb
6742+ && !IS_ROOT(dentry)
6743+ && au_dcount(dentry) > 0
6744+ && au_di(dentry)
6745+ && (!arg->test || arg->test(dentry, arg->arg))) {
6746+ arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
6747+ if (unlikely(arg->err))
6748+ ret = D_WALK_QUIT;
1facf9fc 6749+ }
6750+
c1595e42
JR
6751+ return ret;
6752+}
027c5e7a 6753+
c1595e42
JR
6754+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6755+ au_dpages_test test, void *arg)
6756+{
6757+ struct ac_dpages_arg args = {
6758+ .err = 0,
6759+ .dpages = dpages,
6760+ .sb = root->d_sb,
6761+ .test = test,
6762+ .arg = arg
6763+ };
027c5e7a 6764+
c1595e42
JR
6765+ d_walk(root, &args, au_call_dpages_append, NULL);
6766+
6767+ return args.err;
1facf9fc 6768+}
6769+
6770+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6771+ int do_include, au_dpages_test test, void *arg)
6772+{
6773+ int err;
6774+
6775+ err = 0;
027c5e7a
AM
6776+ write_seqlock(&rename_lock);
6777+ spin_lock(&dentry->d_lock);
6778+ if (do_include
c1595e42 6779+ && au_dcount(dentry) > 0
027c5e7a 6780+ && (!test || test(dentry, arg)))
1facf9fc 6781+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
6782+ spin_unlock(&dentry->d_lock);
6783+ if (unlikely(err))
6784+ goto out;
6785+
6786+ /*
523b37e3 6787+ * RCU for vfsmount is unnecessary since this is a traverse in a single
027c5e7a
AM
6788+ * mount
6789+ */
1facf9fc 6790+ while (!IS_ROOT(dentry)) {
027c5e7a
AM
6791+ dentry = dentry->d_parent; /* rename_lock is locked */
6792+ spin_lock(&dentry->d_lock);
c1595e42 6793+ if (au_dcount(dentry) > 0
027c5e7a 6794+ && (!test || test(dentry, arg)))
1facf9fc 6795+ err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
027c5e7a
AM
6796+ spin_unlock(&dentry->d_lock);
6797+ if (unlikely(err))
6798+ break;
1facf9fc 6799+ }
6800+
4f0767ce 6801+out:
027c5e7a 6802+ write_sequnlock(&rename_lock);
1facf9fc 6803+ return err;
6804+}
6805+
027c5e7a
AM
6806+static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
6807+{
6808+ return au_di(dentry) && dentry->d_sb == arg;
6809+}
6810+
6811+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6812+ struct dentry *dentry, int do_include)
6813+{
6814+ return au_dcsub_pages_rev(dpages, dentry, do_include,
6815+ au_dcsub_dpages_aufs, dentry->d_sb);
6816+}
6817+
4a4d8108 6818+int au_test_subdir(struct dentry *d1, struct dentry *d2)
1facf9fc 6819+{
4a4d8108
AM
6820+ struct path path[2] = {
6821+ {
6822+ .dentry = d1
6823+ },
6824+ {
6825+ .dentry = d2
6826+ }
6827+ };
1facf9fc 6828+
4a4d8108 6829+ return path_is_under(path + 0, path + 1);
1facf9fc 6830+}
7f207e10
AM
6831diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
6832--- /usr/share/empty/fs/aufs/dcsub.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 6833+++ linux/fs/aufs/dcsub.h 2016-07-25 19:05:34.811159821 +0200
5527c038 6834@@ -0,0 +1,136 @@
1facf9fc 6835+/*
8cdd5066 6836+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6837+ *
6838+ * This program, aufs is free software; you can redistribute it and/or modify
6839+ * it under the terms of the GNU General Public License as published by
6840+ * the Free Software Foundation; either version 2 of the License, or
6841+ * (at your option) any later version.
dece6358
AM
6842+ *
6843+ * This program is distributed in the hope that it will be useful,
6844+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6845+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6846+ * GNU General Public License for more details.
6847+ *
6848+ * You should have received a copy of the GNU General Public License
523b37e3 6849+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6850+ */
6851+
6852+/*
6853+ * sub-routines for dentry cache
6854+ */
6855+
6856+#ifndef __AUFS_DCSUB_H__
6857+#define __AUFS_DCSUB_H__
6858+
6859+#ifdef __KERNEL__
6860+
7f207e10 6861+#include <linux/dcache.h>
027c5e7a 6862+#include <linux/fs.h>
dece6358 6863+
1facf9fc 6864+struct au_dpage {
6865+ int ndentry;
6866+ struct dentry **dentries;
6867+};
6868+
6869+struct au_dcsub_pages {
6870+ int ndpage;
6871+ struct au_dpage *dpages;
6872+};
6873+
6874+/* ---------------------------------------------------------------------- */
6875+
7f207e10 6876+/* dcsub.c */
1facf9fc 6877+int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
6878+void au_dpages_free(struct au_dcsub_pages *dpages);
6879+typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
6880+int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
6881+ au_dpages_test test, void *arg);
6882+int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
6883+ int do_include, au_dpages_test test, void *arg);
027c5e7a
AM
6884+int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
6885+ struct dentry *dentry, int do_include);
4a4d8108 6886+int au_test_subdir(struct dentry *d1, struct dentry *d2);
1facf9fc 6887+
7f207e10
AM
6888+/* ---------------------------------------------------------------------- */
6889+
523b37e3
AM
6890+/*
6891+ * todo: in linux-3.13, several similar (but faster) helpers are added to
6892+ * include/linux/dcache.h. Try them (in the future).
6893+ */
6894+
027c5e7a
AM
6895+static inline int au_d_hashed_positive(struct dentry *d)
6896+{
6897+ int err;
5527c038 6898+ struct inode *inode = d_inode(d);
076b876e 6899+
027c5e7a 6900+ err = 0;
5527c038
JR
6901+ if (unlikely(d_unhashed(d)
6902+ || d_is_negative(d)
6903+ || !inode->i_nlink))
027c5e7a
AM
6904+ err = -ENOENT;
6905+ return err;
6906+}
6907+
38d290e6
JR
6908+static inline int au_d_linkable(struct dentry *d)
6909+{
6910+ int err;
5527c038 6911+ struct inode *inode = d_inode(d);
076b876e 6912+
38d290e6
JR
6913+ err = au_d_hashed_positive(d);
6914+ if (err
5527c038 6915+ && d_is_positive(d)
38d290e6
JR
6916+ && (inode->i_state & I_LINKABLE))
6917+ err = 0;
6918+ return err;
6919+}
6920+
027c5e7a
AM
6921+static inline int au_d_alive(struct dentry *d)
6922+{
6923+ int err;
6924+ struct inode *inode;
076b876e 6925+
027c5e7a
AM
6926+ err = 0;
6927+ if (!IS_ROOT(d))
6928+ err = au_d_hashed_positive(d);
6929+ else {
5527c038
JR
6930+ inode = d_inode(d);
6931+ if (unlikely(d_unlinked(d)
6932+ || d_is_negative(d)
6933+ || !inode->i_nlink))
027c5e7a
AM
6934+ err = -ENOENT;
6935+ }
6936+ return err;
6937+}
6938+
6939+static inline int au_alive_dir(struct dentry *d)
7f207e10 6940+{
027c5e7a 6941+ int err;
076b876e 6942+
027c5e7a 6943+ err = au_d_alive(d);
5527c038 6944+ if (unlikely(err || IS_DEADDIR(d_inode(d))))
027c5e7a
AM
6945+ err = -ENOENT;
6946+ return err;
7f207e10
AM
6947+}
6948+
38d290e6
JR
6949+static inline int au_qstreq(struct qstr *a, struct qstr *b)
6950+{
6951+ return a->len == b->len
6952+ && !memcmp(a->name, b->name, a->len);
6953+}
6954+
7e9cd9fe
AM
6955+/*
6956+ * by the commit
6957+ * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
6958+ * taking d_lock
6959+ * the type of d_lockref.count became int, but the inlined function d_count()
6960+ * still returns unsigned int.
6961+ * I don't know why. Maybe it is for every d_count() users?
6962+ * Anyway au_dcount() lives on.
6963+ */
c1595e42
JR
6964+static inline int au_dcount(struct dentry *d)
6965+{
6966+ return (int)d_count(d);
6967+}
6968+
1facf9fc 6969+#endif /* __KERNEL__ */
6970+#endif /* __AUFS_DCSUB_H__ */
7f207e10
AM
6971diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
6972--- /usr/share/empty/fs/aufs/debug.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
6973+++ linux/fs/aufs/debug.c 2016-07-25 19:05:34.811159821 +0200
6974@@ -0,0 +1,441 @@
1facf9fc 6975+/*
8cdd5066 6976+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 6977+ *
6978+ * This program, aufs is free software; you can redistribute it and/or modify
6979+ * it under the terms of the GNU General Public License as published by
6980+ * the Free Software Foundation; either version 2 of the License, or
6981+ * (at your option) any later version.
dece6358
AM
6982+ *
6983+ * This program is distributed in the hope that it will be useful,
6984+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
6985+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6986+ * GNU General Public License for more details.
6987+ *
6988+ * You should have received a copy of the GNU General Public License
523b37e3 6989+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 6990+ */
6991+
6992+/*
6993+ * debug print functions
6994+ */
6995+
6996+#include "aufs.h"
6997+
392086de
AM
6998+/* Returns 0, or -errno. arg is in kp->arg. */
6999+static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7000+{
7001+ int err, n;
7002+
7003+ err = kstrtoint(val, 0, &n);
7004+ if (!err) {
7005+ if (n > 0)
7006+ au_debug_on();
7007+ else
7008+ au_debug_off();
7009+ }
7010+ return err;
7011+}
7012+
7013+/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
7014+static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7015+{
7016+ atomic_t *a;
7017+
7018+ a = kp->arg;
7019+ return sprintf(buffer, "%d", atomic_read(a));
7020+}
7021+
7022+static struct kernel_param_ops param_ops_atomic_t = {
7023+ .set = param_atomic_t_set,
7024+ .get = param_atomic_t_get
7025+ /* void (*free)(void *arg) */
7026+};
7027+
7028+atomic_t aufs_debug = ATOMIC_INIT(0);
1facf9fc 7029+MODULE_PARM_DESC(debug, "debug print");
392086de 7030+module_param_named(debug, aufs_debug, atomic_t, S_IRUGO | S_IWUSR | S_IWGRP);
1facf9fc 7031+
c1595e42 7032+DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
1facf9fc 7033+char *au_plevel = KERN_DEBUG;
e49829fe
JR
7034+#define dpri(fmt, ...) do { \
7035+ if ((au_plevel \
7036+ && strcmp(au_plevel, KERN_DEBUG)) \
7037+ || au_debug_test()) \
7038+ printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
1facf9fc 7039+} while (0)
7040+
7041+/* ---------------------------------------------------------------------- */
7042+
7043+void au_dpri_whlist(struct au_nhash *whlist)
7044+{
7045+ unsigned long ul, n;
7046+ struct hlist_head *head;
c06a8ce3 7047+ struct au_vdir_wh *pos;
1facf9fc 7048+
7049+ n = whlist->nh_num;
7050+ head = whlist->nh_head;
7051+ for (ul = 0; ul < n; ul++) {
c06a8ce3 7052+ hlist_for_each_entry(pos, head, wh_hash)
1facf9fc 7053+ dpri("b%d, %.*s, %d\n",
c06a8ce3
AM
7054+ pos->wh_bindex,
7055+ pos->wh_str.len, pos->wh_str.name,
7056+ pos->wh_str.len);
1facf9fc 7057+ head++;
7058+ }
7059+}
7060+
7061+void au_dpri_vdir(struct au_vdir *vdir)
7062+{
7063+ unsigned long ul;
7064+ union au_vdir_deblk_p p;
7065+ unsigned char *o;
7066+
7067+ if (!vdir || IS_ERR(vdir)) {
7068+ dpri("err %ld\n", PTR_ERR(vdir));
7069+ return;
7070+ }
7071+
7072+ dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %lu\n",
7073+ vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7074+ vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7075+ for (ul = 0; ul < vdir->vd_nblk; ul++) {
7076+ p.deblk = vdir->vd_deblk[ul];
7077+ o = p.deblk;
7078+ dpri("[%lu]: %p\n", ul, o);
7079+ }
7080+}
7081+
53392da6 7082+static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
1facf9fc 7083+ struct dentry *wh)
7084+{
7085+ char *n = NULL;
7086+ int l = 0;
7087+
7088+ if (!inode || IS_ERR(inode)) {
7089+ dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7090+ return -1;
7091+ }
7092+
c2b27bf2 7093+ /* the type of i_blocks depends upon CONFIG_LBDAF */
1facf9fc 7094+ BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7095+ && sizeof(inode->i_blocks) != sizeof(u64));
7096+ if (wh) {
7097+ n = (void *)wh->d_name.name;
7098+ l = wh->d_name.len;
7099+ }
7100+
53392da6
AM
7101+ dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7102+ " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7103+ bindex, inode,
1facf9fc 7104+ inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7105+ atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7106+ i_size_read(inode), (unsigned long long)inode->i_blocks,
53392da6 7107+ hn, (long long)timespec_to_ns(&inode->i_ctime) & 0x0ffff,
1facf9fc 7108+ inode->i_mapping ? inode->i_mapping->nrpages : 0,
b752ccd1
AM
7109+ inode->i_state, inode->i_flags, inode->i_version,
7110+ inode->i_generation,
1facf9fc 7111+ l ? ", wh " : "", l, n);
7112+ return 0;
7113+}
7114+
7115+void au_dpri_inode(struct inode *inode)
7116+{
7117+ struct au_iinfo *iinfo;
5afbbe0d 7118+ struct au_hinode *hi;
1facf9fc 7119+ aufs_bindex_t bindex;
53392da6 7120+ int err, hn;
1facf9fc 7121+
53392da6 7122+ err = do_pri_inode(-1, inode, -1, NULL);
5afbbe0d 7123+ if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
1facf9fc 7124+ return;
7125+
7126+ iinfo = au_ii(inode);
5afbbe0d
AM
7127+ dpri("i-1: btop %d, bbot %d, gen %d\n",
7128+ iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7129+ if (iinfo->ii_btop < 0)
1facf9fc 7130+ return;
53392da6 7131+ hn = 0;
5afbbe0d
AM
7132+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7133+ hi = au_hinode(iinfo, bindex);
7134+ hn = !!au_hn(hi);
7135+ do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
53392da6 7136+ }
1facf9fc 7137+}
7138+
2cbb1c4b
JR
7139+void au_dpri_dalias(struct inode *inode)
7140+{
7141+ struct dentry *d;
7142+
7143+ spin_lock(&inode->i_lock);
c1595e42 7144+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
2cbb1c4b
JR
7145+ au_dpri_dentry(d);
7146+ spin_unlock(&inode->i_lock);
7147+}
7148+
1facf9fc 7149+static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7150+{
7151+ struct dentry *wh = NULL;
53392da6 7152+ int hn;
5afbbe0d 7153+ struct inode *inode;
076b876e 7154+ struct au_iinfo *iinfo;
5afbbe0d 7155+ struct au_hinode *hi;
1facf9fc 7156+
7157+ if (!dentry || IS_ERR(dentry)) {
7158+ dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7159+ return -1;
7160+ }
7161+ /* do not call dget_parent() here */
027c5e7a 7162+ /* note: access d_xxx without d_lock */
523b37e3
AM
7163+ dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7164+ bindex, dentry, dentry,
1facf9fc 7165+ dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
c1595e42 7166+ au_dcount(dentry), dentry->d_flags,
523b37e3 7167+ d_unhashed(dentry) ? "un" : "");
53392da6 7168+ hn = -1;
5afbbe0d
AM
7169+ inode = NULL;
7170+ if (d_is_positive(dentry))
7171+ inode = d_inode(dentry);
7172+ if (inode
7173+ && au_test_aufs(dentry->d_sb)
7174+ && bindex >= 0
7175+ && !au_is_bad_inode(inode)) {
7176+ iinfo = au_ii(inode);
7177+ hi = au_hinode(iinfo, bindex);
7178+ hn = !!au_hn(hi);
7179+ wh = hi->hi_whdentry;
7180+ }
7181+ do_pri_inode(bindex, inode, hn, wh);
1facf9fc 7182+ return 0;
7183+}
7184+
7185+void au_dpri_dentry(struct dentry *dentry)
7186+{
7187+ struct au_dinfo *dinfo;
7188+ aufs_bindex_t bindex;
7189+ int err;
7190+
7191+ err = do_pri_dentry(-1, dentry);
7192+ if (err || !au_test_aufs(dentry->d_sb))
7193+ return;
7194+
7195+ dinfo = au_di(dentry);
7196+ if (!dinfo)
7197+ return;
5afbbe0d
AM
7198+ dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7199+ dinfo->di_btop, dinfo->di_bbot,
38d290e6
JR
7200+ dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7201+ dinfo->di_tmpfile);
5afbbe0d 7202+ if (dinfo->di_btop < 0)
1facf9fc 7203+ return;
5afbbe0d
AM
7204+ for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7205+ do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
1facf9fc 7206+}
7207+
7208+static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7209+{
7210+ char a[32];
7211+
7212+ if (!file || IS_ERR(file)) {
7213+ dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7214+ return -1;
7215+ }
7216+ a[0] = 0;
7217+ if (bindex < 0
b912730e 7218+ && !IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7219+ && au_test_aufs(file->f_path.dentry->d_sb)
1facf9fc 7220+ && au_fi(file))
e49829fe 7221+ snprintf(a, sizeof(a), ", gen %d, mmapped %d",
2cbb1c4b 7222+ au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
b752ccd1 7223+ dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
1facf9fc 7224+ bindex, file->f_mode, file->f_flags, (long)file_count(file),
b752ccd1 7225+ file->f_version, file->f_pos, a);
b912730e 7226+ if (!IS_ERR_OR_NULL(file->f_path.dentry))
2000de60 7227+ do_pri_dentry(bindex, file->f_path.dentry);
1facf9fc 7228+ return 0;
7229+}
7230+
7231+void au_dpri_file(struct file *file)
7232+{
7233+ struct au_finfo *finfo;
4a4d8108
AM
7234+ struct au_fidir *fidir;
7235+ struct au_hfile *hfile;
1facf9fc 7236+ aufs_bindex_t bindex;
7237+ int err;
7238+
7239+ err = do_pri_file(-1, file);
2000de60 7240+ if (err
b912730e 7241+ || IS_ERR_OR_NULL(file->f_path.dentry)
2000de60 7242+ || !au_test_aufs(file->f_path.dentry->d_sb))
1facf9fc 7243+ return;
7244+
7245+ finfo = au_fi(file);
7246+ if (!finfo)
7247+ return;
4a4d8108 7248+ if (finfo->fi_btop < 0)
1facf9fc 7249+ return;
4a4d8108
AM
7250+ fidir = finfo->fi_hdir;
7251+ if (!fidir)
7252+ do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7253+ else
e49829fe
JR
7254+ for (bindex = finfo->fi_btop;
7255+ bindex >= 0 && bindex <= fidir->fd_bbot;
4a4d8108
AM
7256+ bindex++) {
7257+ hfile = fidir->fd_hfile + bindex;
7258+ do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7259+ }
1facf9fc 7260+}
7261+
7262+static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7263+{
7264+ struct vfsmount *mnt;
7265+ struct super_block *sb;
7266+
7267+ if (!br || IS_ERR(br))
7268+ goto out;
86dc4139 7269+ mnt = au_br_mnt(br);
1facf9fc 7270+ if (!mnt || IS_ERR(mnt))
7271+ goto out;
7272+ sb = mnt->mnt_sb;
7273+ if (!sb || IS_ERR(sb))
7274+ goto out;
7275+
5afbbe0d 7276+ dpri("s%d: {perm 0x%x, id %d, cnt %lld, wbr %p}, "
b752ccd1 7277+ "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
1facf9fc 7278+ "xino %d\n",
5afbbe0d 7279+ bindex, br->br_perm, br->br_id, au_br_count(br),
1e00d052 7280+ br->br_wbr, au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
b752ccd1 7281+ sb->s_flags, sb->s_count,
1facf9fc 7282+ atomic_read(&sb->s_active), !!br->br_xino.xi_file);
7283+ return 0;
7284+
4f0767ce 7285+out:
1facf9fc 7286+ dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7287+ return -1;
7288+}
7289+
7290+void au_dpri_sb(struct super_block *sb)
7291+{
7292+ struct au_sbinfo *sbinfo;
7293+ aufs_bindex_t bindex;
7294+ int err;
7295+ /* to reuduce stack size */
7296+ struct {
7297+ struct vfsmount mnt;
7298+ struct au_branch fake;
7299+ } *a;
7300+
7301+ /* this function can be called from magic sysrq */
7302+ a = kzalloc(sizeof(*a), GFP_ATOMIC);
7303+ if (unlikely(!a)) {
7304+ dpri("no memory\n");
7305+ return;
7306+ }
7307+
7308+ a->mnt.mnt_sb = sb;
86dc4139 7309+ a->fake.br_path.mnt = &a->mnt;
5afbbe0d 7310+ au_br_count_init(&a->fake);
1facf9fc 7311+ err = do_pri_br(-1, &a->fake);
5afbbe0d 7312+ au_br_count_fin(&a->fake);
1facf9fc 7313+ kfree(a);
7314+ dpri("dev 0x%x\n", sb->s_dev);
7315+ if (err || !au_test_aufs(sb))
7316+ return;
7317+
7318+ sbinfo = au_sbi(sb);
7319+ if (!sbinfo)
7320+ return;
5afbbe0d
AM
7321+ dpri("nw %lld, gen %u, kobj %d\n",
7322+ percpu_counter_sum(&sbinfo->si_nowait.nw_len),
7323+ sbinfo->si_generation,
1facf9fc 7324+ atomic_read(&sbinfo->si_kobj.kref.refcount));
5afbbe0d 7325+ for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
1facf9fc 7326+ do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7327+}
7328+
7329+/* ---------------------------------------------------------------------- */
7330+
027c5e7a
AM
7331+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7332+{
5527c038 7333+ struct inode *h_inode, *inode = d_inode(dentry);
027c5e7a 7334+ struct dentry *h_dentry;
5afbbe0d 7335+ aufs_bindex_t bindex, bbot, bi;
027c5e7a
AM
7336+
7337+ if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7338+ return;
7339+
5afbbe0d
AM
7340+ bbot = au_dbbot(dentry);
7341+ bi = au_ibbot(inode);
7342+ if (bi < bbot)
7343+ bbot = bi;
7344+ bindex = au_dbtop(dentry);
7345+ bi = au_ibtop(inode);
027c5e7a
AM
7346+ if (bi > bindex)
7347+ bindex = bi;
7348+
5afbbe0d 7349+ for (; bindex <= bbot; bindex++) {
027c5e7a
AM
7350+ h_dentry = au_h_dptr(dentry, bindex);
7351+ if (!h_dentry)
7352+ continue;
7353+ h_inode = au_h_iptr(inode, bindex);
5527c038 7354+ if (unlikely(h_inode != d_inode(h_dentry))) {
392086de 7355+ au_debug_on();
027c5e7a
AM
7356+ AuDbg("b%d, %s:%d\n", bindex, func, line);
7357+ AuDbgDentry(dentry);
7358+ AuDbgInode(inode);
392086de 7359+ au_debug_off();
027c5e7a
AM
7360+ BUG();
7361+ }
7362+ }
7363+}
7364+
1facf9fc 7365+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7366+{
7367+ int err, i, j;
7368+ struct au_dcsub_pages dpages;
7369+ struct au_dpage *dpage;
7370+ struct dentry **dentries;
7371+
7372+ err = au_dpages_init(&dpages, GFP_NOFS);
7373+ AuDebugOn(err);
027c5e7a 7374+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
1facf9fc 7375+ AuDebugOn(err);
7376+ for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7377+ dpage = dpages.dpages + i;
7378+ dentries = dpage->dentries;
7379+ for (j = dpage->ndentry - 1; !err && j >= 0; j--)
027c5e7a 7380+ AuDebugOn(au_digen_test(dentries[j], sigen));
1facf9fc 7381+ }
7382+ au_dpages_free(&dpages);
7383+}
7384+
1facf9fc 7385+void au_dbg_verify_kthread(void)
7386+{
53392da6 7387+ if (au_wkq_test()) {
1facf9fc 7388+ au_dbg_blocked();
1e00d052
AM
7389+ /*
7390+ * It may be recursive, but udba=notify between two aufs mounts,
7391+ * where a single ro branch is shared, is not a problem.
7392+ */
7393+ /* WARN_ON(1); */
1facf9fc 7394+ }
7395+}
7396+
7397+/* ---------------------------------------------------------------------- */
7398+
1facf9fc 7399+int __init au_debug_init(void)
7400+{
7401+ aufs_bindex_t bindex;
7402+ struct au_vdir_destr destr;
7403+
7404+ bindex = -1;
7405+ AuDebugOn(bindex >= 0);
7406+
7407+ destr.len = -1;
7408+ AuDebugOn(destr.len < NAME_MAX);
7409+
7410+#ifdef CONFIG_4KSTACKS
0c3ec466 7411+ pr_warn("CONFIG_4KSTACKS is defined.\n");
1facf9fc 7412+#endif
7413+
1facf9fc 7414+ return 0;
7415+}
7f207e10
AM
7416diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7417--- /usr/share/empty/fs/aufs/debug.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 7418+++ linux/fs/aufs/debug.h 2016-07-25 19:05:34.811159821 +0200
5527c038 7419@@ -0,0 +1,225 @@
1facf9fc 7420+/*
8cdd5066 7421+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 7422+ *
7423+ * This program, aufs is free software; you can redistribute it and/or modify
7424+ * it under the terms of the GNU General Public License as published by
7425+ * the Free Software Foundation; either version 2 of the License, or
7426+ * (at your option) any later version.
dece6358
AM
7427+ *
7428+ * This program is distributed in the hope that it will be useful,
7429+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7430+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7431+ * GNU General Public License for more details.
7432+ *
7433+ * You should have received a copy of the GNU General Public License
523b37e3 7434+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7435+ */
7436+
7437+/*
7438+ * debug print functions
7439+ */
7440+
7441+#ifndef __AUFS_DEBUG_H__
7442+#define __AUFS_DEBUG_H__
7443+
7444+#ifdef __KERNEL__
7445+
392086de 7446+#include <linux/atomic.h>
4a4d8108
AM
7447+#include <linux/module.h>
7448+#include <linux/kallsyms.h>
1facf9fc 7449+#include <linux/sysrq.h>
4a4d8108 7450+
1facf9fc 7451+#ifdef CONFIG_AUFS_DEBUG
7452+#define AuDebugOn(a) BUG_ON(a)
7453+
7454+/* module parameter */
392086de
AM
7455+extern atomic_t aufs_debug;
7456+static inline void au_debug_on(void)
1facf9fc 7457+{
392086de
AM
7458+ atomic_inc(&aufs_debug);
7459+}
7460+static inline void au_debug_off(void)
7461+{
7462+ atomic_dec_if_positive(&aufs_debug);
1facf9fc 7463+}
7464+
7465+static inline int au_debug_test(void)
7466+{
392086de 7467+ return atomic_read(&aufs_debug) > 0;
1facf9fc 7468+}
7469+#else
7470+#define AuDebugOn(a) do {} while (0)
392086de
AM
7471+AuStubVoid(au_debug_on, void)
7472+AuStubVoid(au_debug_off, void)
4a4d8108 7473+AuStubInt0(au_debug_test, void)
1facf9fc 7474+#endif /* CONFIG_AUFS_DEBUG */
7475+
392086de
AM
7476+#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7477+
1facf9fc 7478+/* ---------------------------------------------------------------------- */
7479+
7480+/* debug print */
7481+
4a4d8108 7482+#define AuDbg(fmt, ...) do { \
1facf9fc 7483+ if (au_debug_test()) \
4a4d8108 7484+ pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
1facf9fc 7485+} while (0)
4a4d8108
AM
7486+#define AuLabel(l) AuDbg(#l "\n")
7487+#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7488+#define AuWarn1(fmt, ...) do { \
1facf9fc 7489+ static unsigned char _c; \
7490+ if (!_c++) \
0c3ec466 7491+ pr_warn(fmt, ##__VA_ARGS__); \
1facf9fc 7492+} while (0)
7493+
4a4d8108 7494+#define AuErr1(fmt, ...) do { \
1facf9fc 7495+ static unsigned char _c; \
7496+ if (!_c++) \
4a4d8108 7497+ pr_err(fmt, ##__VA_ARGS__); \
1facf9fc 7498+} while (0)
7499+
4a4d8108 7500+#define AuIOErr1(fmt, ...) do { \
1facf9fc 7501+ static unsigned char _c; \
7502+ if (!_c++) \
4a4d8108 7503+ AuIOErr(fmt, ##__VA_ARGS__); \
1facf9fc 7504+} while (0)
7505+
7506+#define AuUnsupportMsg "This operation is not supported." \
7507+ " Please report this application to aufs-users ML."
4a4d8108
AM
7508+#define AuUnsupport(fmt, ...) do { \
7509+ pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
1facf9fc 7510+ dump_stack(); \
7511+} while (0)
7512+
7513+#define AuTraceErr(e) do { \
7514+ if (unlikely((e) < 0)) \
7515+ AuDbg("err %d\n", (int)(e)); \
7516+} while (0)
7517+
7518+#define AuTraceErrPtr(p) do { \
7519+ if (IS_ERR(p)) \
7520+ AuDbg("err %ld\n", PTR_ERR(p)); \
7521+} while (0)
7522+
7523+/* dirty macros for debug print, use with "%.*s" and caution */
7524+#define AuLNPair(qstr) (qstr)->len, (qstr)->name
1facf9fc 7525+
7526+/* ---------------------------------------------------------------------- */
7527+
dece6358 7528+struct dentry;
1facf9fc 7529+#ifdef CONFIG_AUFS_DEBUG
c1595e42 7530+extern struct mutex au_dbg_mtx;
1facf9fc 7531+extern char *au_plevel;
7532+struct au_nhash;
7533+void au_dpri_whlist(struct au_nhash *whlist);
7534+struct au_vdir;
7535+void au_dpri_vdir(struct au_vdir *vdir);
dece6358 7536+struct inode;
1facf9fc 7537+void au_dpri_inode(struct inode *inode);
2cbb1c4b 7538+void au_dpri_dalias(struct inode *inode);
1facf9fc 7539+void au_dpri_dentry(struct dentry *dentry);
dece6358 7540+struct file;
1facf9fc 7541+void au_dpri_file(struct file *filp);
dece6358 7542+struct super_block;
1facf9fc 7543+void au_dpri_sb(struct super_block *sb);
7544+
027c5e7a
AM
7545+#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7546+void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
1facf9fc 7547+void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
1facf9fc 7548+void au_dbg_verify_kthread(void);
7549+
7550+int __init au_debug_init(void);
7e9cd9fe 7551+
1facf9fc 7552+#define AuDbgWhlist(w) do { \
c1595e42 7553+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7554+ AuDbg(#w "\n"); \
7555+ au_dpri_whlist(w); \
c1595e42 7556+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7557+} while (0)
7558+
7559+#define AuDbgVdir(v) do { \
c1595e42 7560+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7561+ AuDbg(#v "\n"); \
7562+ au_dpri_vdir(v); \
c1595e42 7563+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7564+} while (0)
7565+
7566+#define AuDbgInode(i) do { \
c1595e42 7567+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7568+ AuDbg(#i "\n"); \
7569+ au_dpri_inode(i); \
c1595e42 7570+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7571+} while (0)
7572+
2cbb1c4b 7573+#define AuDbgDAlias(i) do { \
c1595e42 7574+ mutex_lock(&au_dbg_mtx); \
2cbb1c4b
JR
7575+ AuDbg(#i "\n"); \
7576+ au_dpri_dalias(i); \
c1595e42 7577+ mutex_unlock(&au_dbg_mtx); \
2cbb1c4b
JR
7578+} while (0)
7579+
1facf9fc 7580+#define AuDbgDentry(d) do { \
c1595e42 7581+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7582+ AuDbg(#d "\n"); \
7583+ au_dpri_dentry(d); \
c1595e42 7584+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7585+} while (0)
7586+
7587+#define AuDbgFile(f) do { \
c1595e42 7588+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7589+ AuDbg(#f "\n"); \
7590+ au_dpri_file(f); \
c1595e42 7591+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7592+} while (0)
7593+
7594+#define AuDbgSb(sb) do { \
c1595e42 7595+ mutex_lock(&au_dbg_mtx); \
1facf9fc 7596+ AuDbg(#sb "\n"); \
7597+ au_dpri_sb(sb); \
c1595e42 7598+ mutex_unlock(&au_dbg_mtx); \
1facf9fc 7599+} while (0)
7600+
4a4d8108
AM
7601+#define AuDbgSym(addr) do { \
7602+ char sym[KSYM_SYMBOL_LEN]; \
7603+ sprint_symbol(sym, (unsigned long)addr); \
7604+ AuDbg("%s\n", sym); \
7605+} while (0)
1facf9fc 7606+#else
027c5e7a 7607+AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
4a4d8108
AM
7608+AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7609+AuStubVoid(au_dbg_verify_kthread, void)
7610+AuStubInt0(__init au_debug_init, void)
1facf9fc 7611+
1facf9fc 7612+#define AuDbgWhlist(w) do {} while (0)
7613+#define AuDbgVdir(v) do {} while (0)
7614+#define AuDbgInode(i) do {} while (0)
2cbb1c4b 7615+#define AuDbgDAlias(i) do {} while (0)
1facf9fc 7616+#define AuDbgDentry(d) do {} while (0)
7617+#define AuDbgFile(f) do {} while (0)
7618+#define AuDbgSb(sb) do {} while (0)
4a4d8108 7619+#define AuDbgSym(addr) do {} while (0)
1facf9fc 7620+#endif /* CONFIG_AUFS_DEBUG */
7621+
7622+/* ---------------------------------------------------------------------- */
7623+
7624+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7625+int __init au_sysrq_init(void);
7626+void au_sysrq_fin(void);
7627+
7628+#ifdef CONFIG_HW_CONSOLE
7629+#define au_dbg_blocked() do { \
7630+ WARN_ON(1); \
0c5527e5 7631+ handle_sysrq('w'); \
1facf9fc 7632+} while (0)
7633+#else
4a4d8108 7634+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7635+#endif
7636+
7637+#else
4a4d8108
AM
7638+AuStubInt0(__init au_sysrq_init, void)
7639+AuStubVoid(au_sysrq_fin, void)
7640+AuStubVoid(au_dbg_blocked, void)
1facf9fc 7641+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7642+
7643+#endif /* __KERNEL__ */
7644+#endif /* __AUFS_DEBUG_H__ */
7f207e10
AM
7645diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7646--- /usr/share/empty/fs/aufs/dentry.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
7647+++ linux/fs/aufs/dentry.c 2016-07-25 19:05:34.811159821 +0200
7648@@ -0,0 +1,1128 @@
1facf9fc 7649+/*
8cdd5066 7650+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 7651+ *
7652+ * This program, aufs is free software; you can redistribute it and/or modify
7653+ * it under the terms of the GNU General Public License as published by
7654+ * the Free Software Foundation; either version 2 of the License, or
7655+ * (at your option) any later version.
dece6358
AM
7656+ *
7657+ * This program is distributed in the hope that it will be useful,
7658+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
7659+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7660+ * GNU General Public License for more details.
7661+ *
7662+ * You should have received a copy of the GNU General Public License
523b37e3 7663+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 7664+ */
7665+
7666+/*
7667+ * lookup and dentry operations
7668+ */
7669+
dece6358 7670+#include <linux/namei.h>
1facf9fc 7671+#include "aufs.h"
7672+
1facf9fc 7673+struct au_do_lookup_args {
7674+ unsigned int flags;
7675+ mode_t type;
1facf9fc 7676+};
7677+
7678+/*
7679+ * returns positive/negative dentry, NULL or an error.
7680+ * NULL means whiteout-ed or not-found.
7681+ */
7682+static struct dentry*
7683+au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7684+ aufs_bindex_t bindex, struct qstr *wh_name,
7685+ struct au_do_lookup_args *args)
7686+{
7687+ struct dentry *h_dentry;
2000de60 7688+ struct inode *h_inode;
1facf9fc 7689+ struct au_branch *br;
7690+ int wh_found, opq;
7691+ unsigned char wh_able;
7692+ const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
076b876e
AM
7693+ const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7694+ IGNORE_PERM);
1facf9fc 7695+
1facf9fc 7696+ wh_found = 0;
7697+ br = au_sbr(dentry->d_sb, bindex);
7698+ wh_able = !!au_br_whable(br->br_perm);
7699+ if (wh_able)
076b876e 7700+ wh_found = au_wh_test(h_parent, wh_name, /*try_sio*/0);
1facf9fc 7701+ h_dentry = ERR_PTR(wh_found);
7702+ if (!wh_found)
7703+ goto real_lookup;
7704+ if (unlikely(wh_found < 0))
7705+ goto out;
7706+
7707+ /* We found a whiteout */
5afbbe0d 7708+ /* au_set_dbbot(dentry, bindex); */
1facf9fc 7709+ au_set_dbwh(dentry, bindex);
7710+ if (!allow_neg)
7711+ return NULL; /* success */
7712+
4f0767ce 7713+real_lookup:
076b876e
AM
7714+ if (!ignore_perm)
7715+ h_dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
7716+ else
7717+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
2000de60
JR
7718+ if (IS_ERR(h_dentry)) {
7719+ if (PTR_ERR(h_dentry) == -ENAMETOOLONG
7720+ && !allow_neg)
7721+ h_dentry = NULL;
1facf9fc 7722+ goto out;
2000de60 7723+ }
1facf9fc 7724+
5527c038
JR
7725+ h_inode = d_inode(h_dentry);
7726+ if (d_is_negative(h_dentry)) {
1facf9fc 7727+ if (!allow_neg)
7728+ goto out_neg;
7729+ } else if (wh_found
7730+ || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
7731+ goto out_neg;
7732+
5afbbe0d
AM
7733+ if (au_dbbot(dentry) <= bindex)
7734+ au_set_dbbot(dentry, bindex);
7735+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
7736+ au_set_dbtop(dentry, bindex);
1facf9fc 7737+ au_set_h_dptr(dentry, bindex, h_dentry);
7738+
2000de60
JR
7739+ if (!d_is_dir(h_dentry)
7740+ || !wh_able
5527c038 7741+ || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
1facf9fc 7742+ goto out; /* success */
7743+
febd17d6 7744+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
076b876e 7745+ opq = au_diropq_test(h_dentry);
febd17d6 7746+ inode_unlock(h_inode);
1facf9fc 7747+ if (opq > 0)
7748+ au_set_dbdiropq(dentry, bindex);
7749+ else if (unlikely(opq < 0)) {
7750+ au_set_h_dptr(dentry, bindex, NULL);
7751+ h_dentry = ERR_PTR(opq);
7752+ }
7753+ goto out;
7754+
4f0767ce 7755+out_neg:
1facf9fc 7756+ dput(h_dentry);
7757+ h_dentry = NULL;
4f0767ce 7758+out:
1facf9fc 7759+ return h_dentry;
7760+}
7761+
dece6358
AM
7762+static int au_test_shwh(struct super_block *sb, const struct qstr *name)
7763+{
7764+ if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
7765+ && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
7766+ return -EPERM;
7767+ return 0;
7768+}
7769+
1facf9fc 7770+/*
7771+ * returns the number of lower positive dentries,
7772+ * otherwise an error.
7773+ * can be called at unlinking with @type is zero.
7774+ */
5afbbe0d
AM
7775+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
7776+ unsigned int flags)
1facf9fc 7777+{
7778+ int npositive, err;
7779+ aufs_bindex_t bindex, btail, bdiropq;
076b876e 7780+ unsigned char isdir, dirperm1;
1facf9fc 7781+ struct qstr whname;
7782+ struct au_do_lookup_args args = {
5afbbe0d 7783+ .flags = flags
1facf9fc 7784+ };
7785+ const struct qstr *name = &dentry->d_name;
7786+ struct dentry *parent;
076b876e 7787+ struct super_block *sb;
1facf9fc 7788+
076b876e
AM
7789+ sb = dentry->d_sb;
7790+ err = au_test_shwh(sb, name);
dece6358 7791+ if (unlikely(err))
1facf9fc 7792+ goto out;
7793+
7794+ err = au_wh_name_alloc(&whname, name);
7795+ if (unlikely(err))
7796+ goto out;
7797+
2000de60 7798+ isdir = !!d_is_dir(dentry);
076b876e 7799+ dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
1facf9fc 7800+
7801+ npositive = 0;
4a4d8108 7802+ parent = dget_parent(dentry);
1facf9fc 7803+ btail = au_dbtaildir(parent);
5afbbe0d 7804+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 7805+ struct dentry *h_parent, *h_dentry;
7806+ struct inode *h_inode, *h_dir;
7807+
7808+ h_dentry = au_h_dptr(dentry, bindex);
7809+ if (h_dentry) {
5527c038 7810+ if (d_is_positive(h_dentry))
1facf9fc 7811+ npositive++;
5afbbe0d 7812+ break;
1facf9fc 7813+ }
7814+ h_parent = au_h_dptr(parent, bindex);
2000de60 7815+ if (!h_parent || !d_is_dir(h_parent))
1facf9fc 7816+ continue;
7817+
5527c038 7818+ h_dir = d_inode(h_parent);
febd17d6 7819+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 7820+ h_dentry = au_do_lookup(h_parent, dentry, bindex, &whname,
7821+ &args);
febd17d6 7822+ inode_unlock(h_dir);
1facf9fc 7823+ err = PTR_ERR(h_dentry);
7824+ if (IS_ERR(h_dentry))
4a4d8108 7825+ goto out_parent;
2000de60
JR
7826+ if (h_dentry)
7827+ au_fclr_lkup(args.flags, ALLOW_NEG);
076b876e
AM
7828+ if (dirperm1)
7829+ au_fset_lkup(args.flags, IGNORE_PERM);
1facf9fc 7830+
79b8bda9 7831+ if (au_dbwh(dentry) == bindex)
1facf9fc 7832+ break;
7833+ if (!h_dentry)
7834+ continue;
5527c038 7835+ if (d_is_negative(h_dentry))
1facf9fc 7836+ continue;
5527c038 7837+ h_inode = d_inode(h_dentry);
1facf9fc 7838+ npositive++;
7839+ if (!args.type)
7840+ args.type = h_inode->i_mode & S_IFMT;
7841+ if (args.type != S_IFDIR)
7842+ break;
7843+ else if (isdir) {
7844+ /* the type of lower may be different */
7845+ bdiropq = au_dbdiropq(dentry);
7846+ if (bdiropq >= 0 && bdiropq <= bindex)
7847+ break;
7848+ }
7849+ }
7850+
7851+ if (npositive) {
7852+ AuLabel(positive);
5afbbe0d 7853+ au_update_dbtop(dentry);
1facf9fc 7854+ }
7855+ err = npositive;
076b876e 7856+ if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
5afbbe0d 7857+ && au_dbtop(dentry) < 0)) {
1facf9fc 7858+ err = -EIO;
523b37e3
AM
7859+ AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
7860+ dentry, err);
027c5e7a 7861+ }
1facf9fc 7862+
4f0767ce 7863+out_parent:
4a4d8108 7864+ dput(parent);
1facf9fc 7865+ kfree(whname.name);
4f0767ce 7866+out:
1facf9fc 7867+ return err;
7868+}
7869+
076b876e 7870+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
1facf9fc 7871+{
7872+ struct dentry *dentry;
7873+ int wkq_err;
7874+
5527c038 7875+ if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
b4510431 7876+ dentry = vfsub_lkup_one(name, parent);
1facf9fc 7877+ else {
b4510431
AM
7878+ struct vfsub_lkup_one_args args = {
7879+ .errp = &dentry,
7880+ .name = name,
7881+ .parent = parent
1facf9fc 7882+ };
7883+
b4510431 7884+ wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
1facf9fc 7885+ if (unlikely(wkq_err))
7886+ dentry = ERR_PTR(wkq_err);
7887+ }
7888+
7889+ return dentry;
7890+}
7891+
7892+/*
7893+ * lookup @dentry on @bindex which should be negative.
7894+ */
86dc4139 7895+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
1facf9fc 7896+{
7897+ int err;
7898+ struct dentry *parent, *h_parent, *h_dentry;
86dc4139 7899+ struct au_branch *br;
1facf9fc 7900+
1facf9fc 7901+ parent = dget_parent(dentry);
7902+ h_parent = au_h_dptr(parent, bindex);
86dc4139
AM
7903+ br = au_sbr(dentry->d_sb, bindex);
7904+ if (wh)
7905+ h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
7906+ else
076b876e 7907+ h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
1facf9fc 7908+ err = PTR_ERR(h_dentry);
7909+ if (IS_ERR(h_dentry))
7910+ goto out;
5527c038 7911+ if (unlikely(d_is_positive(h_dentry))) {
1facf9fc 7912+ err = -EIO;
523b37e3 7913+ AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
1facf9fc 7914+ dput(h_dentry);
7915+ goto out;
7916+ }
7917+
4a4d8108 7918+ err = 0;
5afbbe0d
AM
7919+ if (bindex < au_dbtop(dentry))
7920+ au_set_dbtop(dentry, bindex);
7921+ if (au_dbbot(dentry) < bindex)
7922+ au_set_dbbot(dentry, bindex);
1facf9fc 7923+ au_set_h_dptr(dentry, bindex, h_dentry);
1facf9fc 7924+
4f0767ce 7925+out:
1facf9fc 7926+ dput(parent);
7927+ return err;
7928+}
7929+
7930+/* ---------------------------------------------------------------------- */
7931+
7932+/* subset of struct inode */
7933+struct au_iattr {
7934+ unsigned long i_ino;
7935+ /* unsigned int i_nlink; */
0c3ec466
AM
7936+ kuid_t i_uid;
7937+ kgid_t i_gid;
1facf9fc 7938+ u64 i_version;
7939+/*
7940+ loff_t i_size;
7941+ blkcnt_t i_blocks;
7942+*/
7943+ umode_t i_mode;
7944+};
7945+
7946+static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
7947+{
7948+ ia->i_ino = h_inode->i_ino;
7949+ /* ia->i_nlink = h_inode->i_nlink; */
7950+ ia->i_uid = h_inode->i_uid;
7951+ ia->i_gid = h_inode->i_gid;
7952+ ia->i_version = h_inode->i_version;
7953+/*
7954+ ia->i_size = h_inode->i_size;
7955+ ia->i_blocks = h_inode->i_blocks;
7956+*/
7957+ ia->i_mode = (h_inode->i_mode & S_IFMT);
7958+}
7959+
7960+static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
7961+{
7962+ return ia->i_ino != h_inode->i_ino
7963+ /* || ia->i_nlink != h_inode->i_nlink */
0c3ec466 7964+ || !uid_eq(ia->i_uid, h_inode->i_uid)
2dfbb274 7965+ || !gid_eq(ia->i_gid, h_inode->i_gid)
1facf9fc 7966+ || ia->i_version != h_inode->i_version
7967+/*
7968+ || ia->i_size != h_inode->i_size
7969+ || ia->i_blocks != h_inode->i_blocks
7970+*/
7971+ || ia->i_mode != (h_inode->i_mode & S_IFMT);
7972+}
7973+
7974+static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
7975+ struct au_branch *br)
7976+{
7977+ int err;
7978+ struct au_iattr ia;
7979+ struct inode *h_inode;
7980+ struct dentry *h_d;
7981+ struct super_block *h_sb;
7982+
7983+ err = 0;
7984+ memset(&ia, -1, sizeof(ia));
7985+ h_sb = h_dentry->d_sb;
5527c038
JR
7986+ h_inode = NULL;
7987+ if (d_is_positive(h_dentry)) {
7988+ h_inode = d_inode(h_dentry);
1facf9fc 7989+ au_iattr_save(&ia, h_inode);
5527c038 7990+ } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
1facf9fc 7991+ /* nfs d_revalidate may return 0 for negative dentry */
7992+ /* fuse d_revalidate always return 0 for negative dentry */
7993+ goto out;
7994+
7995+ /* main purpose is namei.c:cached_lookup() and d_revalidate */
b4510431 7996+ h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
1facf9fc 7997+ err = PTR_ERR(h_d);
7998+ if (IS_ERR(h_d))
7999+ goto out;
8000+
8001+ err = 0;
8002+ if (unlikely(h_d != h_dentry
5527c038 8003+ || d_inode(h_d) != h_inode
1facf9fc 8004+ || (h_inode && au_iattr_test(&ia, h_inode))))
8005+ err = au_busy_or_stale();
8006+ dput(h_d);
8007+
4f0767ce 8008+out:
1facf9fc 8009+ AuTraceErr(err);
8010+ return err;
8011+}
8012+
8013+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8014+ struct dentry *h_parent, struct au_branch *br)
8015+{
8016+ int err;
8017+
8018+ err = 0;
027c5e7a
AM
8019+ if (udba == AuOpt_UDBA_REVAL
8020+ && !au_test_fs_remote(h_dentry->d_sb)) {
1facf9fc 8021+ IMustLock(h_dir);
5527c038 8022+ err = (d_inode(h_dentry->d_parent) != h_dir);
027c5e7a 8023+ } else if (udba != AuOpt_UDBA_NONE)
1facf9fc 8024+ err = au_h_verify_dentry(h_dentry, h_parent, br);
8025+
8026+ return err;
8027+}
8028+
8029+/* ---------------------------------------------------------------------- */
8030+
027c5e7a 8031+static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
1facf9fc 8032+{
027c5e7a 8033+ int err;
5afbbe0d 8034+ aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
027c5e7a
AM
8035+ struct au_hdentry tmp, *p, *q;
8036+ struct au_dinfo *dinfo;
8037+ struct super_block *sb;
1facf9fc 8038+
027c5e7a 8039+ DiMustWriteLock(dentry);
1308ab2a 8040+
027c5e7a
AM
8041+ sb = dentry->d_sb;
8042+ dinfo = au_di(dentry);
5afbbe0d 8043+ bbot = dinfo->di_bbot;
1facf9fc 8044+ bwh = dinfo->di_bwh;
8045+ bdiropq = dinfo->di_bdiropq;
5afbbe0d
AM
8046+ bindex = dinfo->di_btop;
8047+ p = au_hdentry(dinfo, bindex);
8048+ for (; bindex <= bbot; bindex++, p++) {
027c5e7a 8049+ if (!p->hd_dentry)
1facf9fc 8050+ continue;
8051+
027c5e7a
AM
8052+ new_bindex = au_br_index(sb, p->hd_id);
8053+ if (new_bindex == bindex)
1facf9fc 8054+ continue;
1facf9fc 8055+
1facf9fc 8056+ if (dinfo->di_bwh == bindex)
8057+ bwh = new_bindex;
8058+ if (dinfo->di_bdiropq == bindex)
8059+ bdiropq = new_bindex;
8060+ if (new_bindex < 0) {
8061+ au_hdput(p);
8062+ p->hd_dentry = NULL;
8063+ continue;
8064+ }
8065+
8066+ /* swap two lower dentries, and loop again */
5afbbe0d 8067+ q = au_hdentry(dinfo, new_bindex);
1facf9fc 8068+ tmp = *q;
8069+ *q = *p;
8070+ *p = tmp;
8071+ if (tmp.hd_dentry) {
8072+ bindex--;
8073+ p--;
8074+ }
8075+ }
8076+
1facf9fc 8077+ dinfo->di_bwh = -1;
5afbbe0d 8078+ if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
1facf9fc 8079+ dinfo->di_bwh = bwh;
8080+
8081+ dinfo->di_bdiropq = -1;
8082+ if (bdiropq >= 0
5afbbe0d 8083+ && bdiropq <= au_sbbot(sb)
1facf9fc 8084+ && au_sbr_whable(sb, bdiropq))
8085+ dinfo->di_bdiropq = bdiropq;
8086+
027c5e7a 8087+ err = -EIO;
5afbbe0d
AM
8088+ dinfo->di_btop = -1;
8089+ dinfo->di_bbot = -1;
8090+ bbot = au_dbbot(parent);
8091+ bindex = 0;
8092+ p = au_hdentry(dinfo, bindex);
8093+ for (; bindex <= bbot; bindex++, p++)
1facf9fc 8094+ if (p->hd_dentry) {
5afbbe0d 8095+ dinfo->di_btop = bindex;
1facf9fc 8096+ break;
8097+ }
8098+
5afbbe0d
AM
8099+ if (dinfo->di_btop >= 0) {
8100+ bindex = bbot;
8101+ p = au_hdentry(dinfo, bindex);
8102+ for (; bindex >= 0; bindex--, p--)
027c5e7a 8103+ if (p->hd_dentry) {
5afbbe0d 8104+ dinfo->di_bbot = bindex;
027c5e7a
AM
8105+ err = 0;
8106+ break;
8107+ }
8108+ }
8109+
8110+ return err;
1facf9fc 8111+}
8112+
027c5e7a 8113+static void au_do_hide(struct dentry *dentry)
1facf9fc 8114+{
027c5e7a 8115+ struct inode *inode;
1facf9fc 8116+
5527c038
JR
8117+ if (d_really_is_positive(dentry)) {
8118+ inode = d_inode(dentry);
8119+ if (!d_is_dir(dentry)) {
027c5e7a
AM
8120+ if (inode->i_nlink && !d_unhashed(dentry))
8121+ drop_nlink(inode);
8122+ } else {
8123+ clear_nlink(inode);
8124+ /* stop next lookup */
8125+ inode->i_flags |= S_DEAD;
8126+ }
8127+ smp_mb(); /* necessary? */
8128+ }
8129+ d_drop(dentry);
8130+}
1308ab2a 8131+
027c5e7a
AM
8132+static int au_hide_children(struct dentry *parent)
8133+{
8134+ int err, i, j, ndentry;
8135+ struct au_dcsub_pages dpages;
8136+ struct au_dpage *dpage;
8137+ struct dentry *dentry;
1facf9fc 8138+
027c5e7a 8139+ err = au_dpages_init(&dpages, GFP_NOFS);
1facf9fc 8140+ if (unlikely(err))
8141+ goto out;
027c5e7a
AM
8142+ err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8143+ if (unlikely(err))
8144+ goto out_dpages;
1facf9fc 8145+
027c5e7a
AM
8146+ /* in reverse order */
8147+ for (i = dpages.ndpage - 1; i >= 0; i--) {
8148+ dpage = dpages.dpages + i;
8149+ ndentry = dpage->ndentry;
8150+ for (j = ndentry - 1; j >= 0; j--) {
8151+ dentry = dpage->dentries[j];
8152+ if (dentry != parent)
8153+ au_do_hide(dentry);
8154+ }
8155+ }
1facf9fc 8156+
027c5e7a
AM
8157+out_dpages:
8158+ au_dpages_free(&dpages);
4f0767ce 8159+out:
027c5e7a 8160+ return err;
1facf9fc 8161+}
8162+
027c5e7a 8163+static void au_hide(struct dentry *dentry)
1facf9fc 8164+{
027c5e7a 8165+ int err;
1facf9fc 8166+
027c5e7a 8167+ AuDbgDentry(dentry);
2000de60 8168+ if (d_is_dir(dentry)) {
027c5e7a
AM
8169+ /* shrink_dcache_parent(dentry); */
8170+ err = au_hide_children(dentry);
8171+ if (unlikely(err))
523b37e3
AM
8172+ AuIOErr("%pd, failed hiding children, ignored %d\n",
8173+ dentry, err);
027c5e7a
AM
8174+ }
8175+ au_do_hide(dentry);
8176+}
1facf9fc 8177+
027c5e7a
AM
8178+/*
8179+ * By adding a dirty branch, a cached dentry may be affected in various ways.
8180+ *
8181+ * a dirty branch is added
8182+ * - on the top of layers
8183+ * - in the middle of layers
8184+ * - to the bottom of layers
8185+ *
8186+ * on the added branch there exists
8187+ * - a whiteout
8188+ * - a diropq
8189+ * - a same named entry
8190+ * + exist
8191+ * * negative --> positive
8192+ * * positive --> positive
8193+ * - type is unchanged
8194+ * - type is changed
8195+ * + doesn't exist
8196+ * * negative --> negative
8197+ * * positive --> negative (rejected by au_br_del() for non-dir case)
8198+ * - none
8199+ */
8200+static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8201+ struct au_dinfo *tmp)
8202+{
8203+ int err;
5afbbe0d 8204+ aufs_bindex_t bindex, bbot;
027c5e7a
AM
8205+ struct {
8206+ struct dentry *dentry;
8207+ struct inode *inode;
8208+ mode_t mode;
be52b249
AM
8209+ } orig_h, tmp_h = {
8210+ .dentry = NULL
8211+ };
027c5e7a
AM
8212+ struct au_hdentry *hd;
8213+ struct inode *inode, *h_inode;
8214+ struct dentry *h_dentry;
8215+
8216+ err = 0;
5afbbe0d 8217+ AuDebugOn(dinfo->di_btop < 0);
027c5e7a 8218+ orig_h.mode = 0;
5afbbe0d 8219+ orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
5527c038
JR
8220+ orig_h.inode = NULL;
8221+ if (d_is_positive(orig_h.dentry)) {
8222+ orig_h.inode = d_inode(orig_h.dentry);
027c5e7a 8223+ orig_h.mode = orig_h.inode->i_mode & S_IFMT;
5527c038 8224+ }
5afbbe0d
AM
8225+ if (tmp->di_btop >= 0) {
8226+ tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
5527c038
JR
8227+ if (d_is_positive(tmp_h.dentry)) {
8228+ tmp_h.inode = d_inode(tmp_h.dentry);
027c5e7a 8229+ tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
5527c038 8230+ }
027c5e7a
AM
8231+ }
8232+
5527c038
JR
8233+ inode = NULL;
8234+ if (d_really_is_positive(dentry))
8235+ inode = d_inode(dentry);
027c5e7a
AM
8236+ if (!orig_h.inode) {
8237+ AuDbg("nagative originally\n");
8238+ if (inode) {
8239+ au_hide(dentry);
8240+ goto out;
8241+ }
8242+ AuDebugOn(inode);
5afbbe0d 8243+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
027c5e7a
AM
8244+ AuDebugOn(dinfo->di_bdiropq != -1);
8245+
8246+ if (!tmp_h.inode) {
8247+ AuDbg("negative --> negative\n");
8248+ /* should have only one negative lower */
5afbbe0d
AM
8249+ if (tmp->di_btop >= 0
8250+ && tmp->di_btop < dinfo->di_btop) {
8251+ AuDebugOn(tmp->di_btop != tmp->di_bbot);
8252+ AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8253+ au_set_h_dptr(dentry, dinfo->di_btop, NULL);
027c5e7a 8254+ au_di_cp(dinfo, tmp);
5afbbe0d
AM
8255+ hd = au_hdentry(tmp, tmp->di_btop);
8256+ au_set_h_dptr(dentry, tmp->di_btop,
027c5e7a
AM
8257+ dget(hd->hd_dentry));
8258+ }
8259+ au_dbg_verify_dinode(dentry);
8260+ } else {
8261+ AuDbg("negative --> positive\n");
8262+ /*
8263+ * similar to the behaviour of creating with bypassing
8264+ * aufs.
8265+ * unhash it in order to force an error in the
8266+ * succeeding create operation.
8267+ * we should not set S_DEAD here.
8268+ */
8269+ d_drop(dentry);
8270+ /* au_di_swap(tmp, dinfo); */
8271+ au_dbg_verify_dinode(dentry);
8272+ }
8273+ } else {
8274+ AuDbg("positive originally\n");
8275+ /* inode may be NULL */
8276+ AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8277+ if (!tmp_h.inode) {
8278+ AuDbg("positive --> negative\n");
8279+ /* or bypassing aufs */
8280+ au_hide(dentry);
5afbbe0d 8281+ if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
027c5e7a
AM
8282+ dinfo->di_bwh = tmp->di_bwh;
8283+ if (inode)
8284+ err = au_refresh_hinode_self(inode);
8285+ au_dbg_verify_dinode(dentry);
8286+ } else if (orig_h.mode == tmp_h.mode) {
8287+ AuDbg("positive --> positive, same type\n");
8288+ if (!S_ISDIR(orig_h.mode)
5afbbe0d 8289+ && dinfo->di_btop > tmp->di_btop) {
027c5e7a
AM
8290+ /*
8291+ * similar to the behaviour of removing and
8292+ * creating.
8293+ */
8294+ au_hide(dentry);
8295+ if (inode)
8296+ err = au_refresh_hinode_self(inode);
8297+ au_dbg_verify_dinode(dentry);
8298+ } else {
8299+ /* fill empty slots */
5afbbe0d
AM
8300+ if (dinfo->di_btop > tmp->di_btop)
8301+ dinfo->di_btop = tmp->di_btop;
8302+ if (dinfo->di_bbot < tmp->di_bbot)
8303+ dinfo->di_bbot = tmp->di_bbot;
027c5e7a
AM
8304+ dinfo->di_bwh = tmp->di_bwh;
8305+ dinfo->di_bdiropq = tmp->di_bdiropq;
5afbbe0d
AM
8306+ bbot = dinfo->di_bbot;
8307+ bindex = tmp->di_btop;
8308+ hd = au_hdentry(tmp, bindex);
8309+ for (; bindex <= bbot; bindex++, hd++) {
027c5e7a
AM
8310+ if (au_h_dptr(dentry, bindex))
8311+ continue;
5afbbe0d 8312+ h_dentry = hd->hd_dentry;
027c5e7a
AM
8313+ if (!h_dentry)
8314+ continue;
5527c038
JR
8315+ AuDebugOn(d_is_negative(h_dentry));
8316+ h_inode = d_inode(h_dentry);
027c5e7a
AM
8317+ AuDebugOn(orig_h.mode
8318+ != (h_inode->i_mode
8319+ & S_IFMT));
8320+ au_set_h_dptr(dentry, bindex,
8321+ dget(h_dentry));
8322+ }
5afbbe0d
AM
8323+ if (inode)
8324+ err = au_refresh_hinode(inode, dentry);
027c5e7a
AM
8325+ au_dbg_verify_dinode(dentry);
8326+ }
8327+ } else {
8328+ AuDbg("positive --> positive, different type\n");
8329+ /* similar to the behaviour of removing and creating */
8330+ au_hide(dentry);
8331+ if (inode)
8332+ err = au_refresh_hinode_self(inode);
8333+ au_dbg_verify_dinode(dentry);
8334+ }
8335+ }
8336+
8337+out:
8338+ return err;
8339+}
8340+
79b8bda9
AM
8341+void au_refresh_dop(struct dentry *dentry, int force_reval)
8342+{
8343+ const struct dentry_operations *dop
8344+ = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8345+ static const unsigned int mask
8346+ = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8347+
8348+ BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8349+
8350+ if (dentry->d_op == dop)
8351+ return;
8352+
8353+ AuDbg("%pd\n", dentry);
8354+ spin_lock(&dentry->d_lock);
8355+ if (dop == &aufs_dop)
8356+ dentry->d_flags |= mask;
8357+ else
8358+ dentry->d_flags &= ~mask;
8359+ dentry->d_op = dop;
8360+ spin_unlock(&dentry->d_lock);
8361+}
8362+
027c5e7a
AM
8363+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8364+{
8365+ int err, ebrange;
8366+ unsigned int sigen;
8367+ struct au_dinfo *dinfo, *tmp;
8368+ struct super_block *sb;
8369+ struct inode *inode;
8370+
8371+ DiMustWriteLock(dentry);
8372+ AuDebugOn(IS_ROOT(dentry));
5527c038 8373+ AuDebugOn(d_really_is_negative(parent));
027c5e7a
AM
8374+
8375+ sb = dentry->d_sb;
027c5e7a
AM
8376+ sigen = au_sigen(sb);
8377+ err = au_digen_test(parent, sigen);
8378+ if (unlikely(err))
8379+ goto out;
8380+
8381+ dinfo = au_di(dentry);
5afbbe0d 8382+ err = au_di_realloc(dinfo, au_sbbot(sb) + 1);
027c5e7a
AM
8383+ if (unlikely(err))
8384+ goto out;
8385+ ebrange = au_dbrange_test(dentry);
8386+ if (!ebrange)
8387+ ebrange = au_do_refresh_hdentry(dentry, parent);
8388+
38d290e6 8389+ if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
5afbbe0d 8390+ AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
5527c038
JR
8391+ if (d_really_is_positive(dentry)) {
8392+ inode = d_inode(dentry);
027c5e7a 8393+ err = au_refresh_hinode_self(inode);
5527c038 8394+ }
027c5e7a
AM
8395+ au_dbg_verify_dinode(dentry);
8396+ if (!err)
8397+ goto out_dgen; /* success */
8398+ goto out;
8399+ }
8400+
8401+ /* temporary dinfo */
8402+ AuDbgDentry(dentry);
8403+ err = -ENOMEM;
8404+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8405+ if (unlikely(!tmp))
8406+ goto out;
8407+ au_di_swap(tmp, dinfo);
8408+ /* returns the number of positive dentries */
8409+ /*
8410+ * if current working dir is removed, it returns an error.
8411+ * but the dentry is legal.
8412+ */
5afbbe0d 8413+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
027c5e7a
AM
8414+ AuDbgDentry(dentry);
8415+ au_di_swap(tmp, dinfo);
8416+ if (err == -ENOENT)
8417+ err = 0;
8418+ if (err >= 0) {
8419+ /* compare/refresh by dinfo */
8420+ AuDbgDentry(dentry);
8421+ err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8422+ au_dbg_verify_dinode(dentry);
8423+ AuTraceErr(err);
8424+ }
8425+ au_rw_write_unlock(&tmp->di_rwsem);
8426+ au_di_free(tmp);
8427+ if (unlikely(err))
8428+ goto out;
8429+
8430+out_dgen:
8431+ au_update_digen(dentry);
8432+out:
8433+ if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
523b37e3 8434+ AuIOErr("failed refreshing %pd, %d\n", dentry, err);
027c5e7a
AM
8435+ AuDbgDentry(dentry);
8436+ }
8437+ AuTraceErr(err);
8438+ return err;
8439+}
8440+
b4510431
AM
8441+static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8442+ struct dentry *dentry, aufs_bindex_t bindex)
027c5e7a
AM
8443+{
8444+ int err, valid;
027c5e7a
AM
8445+
8446+ err = 0;
8447+ if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8448+ goto out;
027c5e7a
AM
8449+
8450+ AuDbg("b%d\n", bindex);
b4510431
AM
8451+ /*
8452+ * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8453+ * due to whiteout and branch permission.
8454+ */
8455+ flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8456+ | LOOKUP_FOLLOW | LOOKUP_EXCL);
8457+ /* it may return tri-state */
8458+ valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
1facf9fc 8459+
8460+ if (unlikely(valid < 0))
8461+ err = valid;
8462+ else if (!valid)
8463+ err = -EINVAL;
8464+
4f0767ce 8465+out:
1facf9fc 8466+ AuTraceErr(err);
8467+ return err;
8468+}
8469+
8470+/* todo: remove this */
8471+static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
b4510431 8472+ unsigned int flags, int do_udba)
1facf9fc 8473+{
8474+ int err;
8475+ umode_t mode, h_mode;
5afbbe0d 8476+ aufs_bindex_t bindex, btail, btop, ibs, ibe;
38d290e6 8477+ unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
4a4d8108 8478+ struct inode *h_inode, *h_cached_inode;
1facf9fc 8479+ struct dentry *h_dentry;
8480+ struct qstr *name, *h_name;
8481+
8482+ err = 0;
8483+ plus = 0;
8484+ mode = 0;
1facf9fc 8485+ ibs = -1;
8486+ ibe = -1;
8487+ unhashed = !!d_unhashed(dentry);
8488+ is_root = !!IS_ROOT(dentry);
8489+ name = &dentry->d_name;
38d290e6 8490+ tmpfile = au_di(dentry)->di_tmpfile;
1facf9fc 8491+
8492+ /*
7f207e10
AM
8493+ * Theoretically, REVAL test should be unnecessary in case of
8494+ * {FS,I}NOTIFY.
8495+ * But {fs,i}notify doesn't fire some necessary events,
1facf9fc 8496+ * IN_ATTRIB for atime/nlink/pageio
1facf9fc 8497+ * Let's do REVAL test too.
8498+ */
8499+ if (do_udba && inode) {
8500+ mode = (inode->i_mode & S_IFMT);
8501+ plus = (inode->i_nlink > 0);
5afbbe0d
AM
8502+ ibs = au_ibtop(inode);
8503+ ibe = au_ibbot(inode);
1facf9fc 8504+ }
8505+
5afbbe0d
AM
8506+ btop = au_dbtop(dentry);
8507+ btail = btop;
1facf9fc 8508+ if (inode && S_ISDIR(inode->i_mode))
8509+ btail = au_dbtaildir(dentry);
5afbbe0d 8510+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 8511+ h_dentry = au_h_dptr(dentry, bindex);
8512+ if (!h_dentry)
8513+ continue;
8514+
523b37e3
AM
8515+ AuDbg("b%d, %pd\n", bindex, h_dentry);
8516+ h_nfs = !!au_test_nfs(h_dentry->d_sb);
027c5e7a 8517+ spin_lock(&h_dentry->d_lock);
1facf9fc 8518+ h_name = &h_dentry->d_name;
8519+ if (unlikely(do_udba
8520+ && !is_root
523b37e3
AM
8521+ && ((!h_nfs
8522+ && (unhashed != !!d_unhashed(h_dentry)
38d290e6
JR
8523+ || (!tmpfile
8524+ && !au_qstreq(name, h_name))
8525+ ))
523b37e3
AM
8526+ || (h_nfs
8527+ && !(flags & LOOKUP_OPEN)
8528+ && (h_dentry->d_flags
8529+ & DCACHE_NFSFS_RENAMED)))
1facf9fc 8530+ )) {
38d290e6
JR
8531+ int h_unhashed;
8532+
8533+ h_unhashed = d_unhashed(h_dentry);
027c5e7a 8534+ spin_unlock(&h_dentry->d_lock);
38d290e6
JR
8535+ AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8536+ unhashed, h_unhashed, dentry, h_dentry);
1facf9fc 8537+ goto err;
8538+ }
027c5e7a 8539+ spin_unlock(&h_dentry->d_lock);
1facf9fc 8540+
b4510431 8541+ err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
1facf9fc 8542+ if (unlikely(err))
8543+ /* do not goto err, to keep the errno */
8544+ break;
8545+
8546+ /* todo: plink too? */
8547+ if (!do_udba)
8548+ continue;
8549+
8550+ /* UDBA tests */
5527c038 8551+ if (unlikely(!!inode != d_is_positive(h_dentry)))
1facf9fc 8552+ goto err;
8553+
5527c038
JR
8554+ h_inode = NULL;
8555+ if (d_is_positive(h_dentry))
8556+ h_inode = d_inode(h_dentry);
1facf9fc 8557+ h_plus = plus;
8558+ h_mode = mode;
8559+ h_cached_inode = h_inode;
8560+ if (h_inode) {
8561+ h_mode = (h_inode->i_mode & S_IFMT);
8562+ h_plus = (h_inode->i_nlink > 0);
8563+ }
8564+ if (inode && ibs <= bindex && bindex <= ibe)
8565+ h_cached_inode = au_h_iptr(inode, bindex);
8566+
523b37e3 8567+ if (!h_nfs) {
38d290e6 8568+ if (unlikely(plus != h_plus && !tmpfile))
523b37e3
AM
8569+ goto err;
8570+ } else {
8571+ if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8572+ && !is_root
8573+ && !IS_ROOT(h_dentry)
8574+ && unhashed != d_unhashed(h_dentry)))
8575+ goto err;
8576+ }
8577+ if (unlikely(mode != h_mode
1facf9fc 8578+ || h_cached_inode != h_inode))
8579+ goto err;
8580+ continue;
8581+
f6b6e03d 8582+err:
1facf9fc 8583+ err = -EINVAL;
8584+ break;
8585+ }
8586+
523b37e3 8587+ AuTraceErr(err);
1facf9fc 8588+ return err;
8589+}
8590+
027c5e7a 8591+/* todo: consolidate with do_refresh() and au_reval_for_attr() */
1facf9fc 8592+static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8593+{
8594+ int err;
8595+ struct dentry *parent;
1facf9fc 8596+
027c5e7a 8597+ if (!au_digen_test(dentry, sigen))
1facf9fc 8598+ return 0;
8599+
8600+ parent = dget_parent(dentry);
8601+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8602+ AuDebugOn(au_digen_test(parent, sigen));
1facf9fc 8603+ au_dbg_verify_gen(parent, sigen);
027c5e7a 8604+ err = au_refresh_dentry(dentry, parent);
1facf9fc 8605+ di_read_unlock(parent, AuLock_IR);
8606+ dput(parent);
027c5e7a 8607+ AuTraceErr(err);
1facf9fc 8608+ return err;
8609+}
8610+
8611+int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8612+{
8613+ int err;
8614+ struct dentry *d, *parent;
1facf9fc 8615+
027c5e7a 8616+ if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
1facf9fc 8617+ return simple_reval_dpath(dentry, sigen);
8618+
8619+ /* slow loop, keep it simple and stupid */
8620+ /* cf: au_cpup_dirs() */
8621+ err = 0;
8622+ parent = NULL;
027c5e7a 8623+ while (au_digen_test(dentry, sigen)) {
1facf9fc 8624+ d = dentry;
8625+ while (1) {
8626+ dput(parent);
8627+ parent = dget_parent(d);
027c5e7a 8628+ if (!au_digen_test(parent, sigen))
1facf9fc 8629+ break;
8630+ d = parent;
8631+ }
8632+
1facf9fc 8633+ if (d != dentry)
027c5e7a 8634+ di_write_lock_child2(d);
1facf9fc 8635+
8636+ /* someone might update our dentry while we were sleeping */
027c5e7a
AM
8637+ if (au_digen_test(d, sigen)) {
8638+ /*
8639+ * todo: consolidate with simple_reval_dpath(),
8640+ * do_refresh() and au_reval_for_attr().
8641+ */
1facf9fc 8642+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 8643+ err = au_refresh_dentry(d, parent);
1facf9fc 8644+ di_read_unlock(parent, AuLock_IR);
8645+ }
8646+
8647+ if (d != dentry)
8648+ di_write_unlock(d);
8649+ dput(parent);
8650+ if (unlikely(err))
8651+ break;
8652+ }
8653+
8654+ return err;
8655+}
8656+
8657+/*
8658+ * if valid returns 1, otherwise 0.
8659+ */
b4510431 8660+static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
1facf9fc 8661+{
8662+ int valid, err;
8663+ unsigned int sigen;
8664+ unsigned char do_udba;
8665+ struct super_block *sb;
8666+ struct inode *inode;
8667+
027c5e7a 8668+ /* todo: support rcu-walk? */
b4510431 8669+ if (flags & LOOKUP_RCU)
027c5e7a
AM
8670+ return -ECHILD;
8671+
8672+ valid = 0;
8673+ if (unlikely(!au_di(dentry)))
8674+ goto out;
8675+
e49829fe 8676+ valid = 1;
1facf9fc 8677+ sb = dentry->d_sb;
e49829fe
JR
8678+ /*
8679+ * todo: very ugly
8680+ * i_mutex of parent dir may be held,
8681+ * but we should not return 'invalid' due to busy.
8682+ */
8683+ err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
8684+ if (unlikely(err)) {
8685+ valid = err;
027c5e7a 8686+ AuTraceErr(err);
e49829fe
JR
8687+ goto out;
8688+ }
5527c038
JR
8689+ inode = NULL;
8690+ if (d_really_is_positive(dentry))
8691+ inode = d_inode(dentry);
5afbbe0d 8692+ if (unlikely(inode && au_is_bad_inode(inode))) {
c1595e42
JR
8693+ err = -EINVAL;
8694+ AuTraceErr(err);
8695+ goto out_dgrade;
8696+ }
027c5e7a
AM
8697+ if (unlikely(au_dbrange_test(dentry))) {
8698+ err = -EINVAL;
8699+ AuTraceErr(err);
8700+ goto out_dgrade;
1facf9fc 8701+ }
027c5e7a
AM
8702+
8703+ sigen = au_sigen(sb);
8704+ if (au_digen_test(dentry, sigen)) {
1facf9fc 8705+ AuDebugOn(IS_ROOT(dentry));
027c5e7a
AM
8706+ err = au_reval_dpath(dentry, sigen);
8707+ if (unlikely(err)) {
8708+ AuTraceErr(err);
1facf9fc 8709+ goto out_dgrade;
027c5e7a 8710+ }
1facf9fc 8711+ }
8712+ di_downgrade_lock(dentry, AuLock_IR);
8713+
1facf9fc 8714+ err = -EINVAL;
c1595e42 8715+ if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
523b37e3 8716+ && inode
38d290e6 8717+ && !(inode->i_state && I_LINKABLE)
79b8bda9
AM
8718+ && (IS_DEADDIR(inode) || !inode->i_nlink)) {
8719+ AuTraceErr(err);
027c5e7a 8720+ goto out_inval;
79b8bda9 8721+ }
027c5e7a 8722+
1facf9fc 8723+ do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
8724+ if (do_udba && inode) {
5afbbe0d 8725+ aufs_bindex_t btop = au_ibtop(inode);
027c5e7a 8726+ struct inode *h_inode;
1facf9fc 8727+
5afbbe0d
AM
8728+ if (btop >= 0) {
8729+ h_inode = au_h_iptr(inode, btop);
79b8bda9
AM
8730+ if (h_inode && au_test_higen(inode, h_inode)) {
8731+ AuTraceErr(err);
027c5e7a 8732+ goto out_inval;
79b8bda9 8733+ }
027c5e7a 8734+ }
1facf9fc 8735+ }
8736+
b4510431 8737+ err = h_d_revalidate(dentry, inode, flags, do_udba);
5afbbe0d 8738+ if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
1facf9fc 8739+ err = -EIO;
523b37e3
AM
8740+ AuDbg("both of real entry and whiteout found, %p, err %d\n",
8741+ dentry, err);
027c5e7a 8742+ }
e49829fe 8743+ goto out_inval;
1facf9fc 8744+
4f0767ce 8745+out_dgrade:
1facf9fc 8746+ di_downgrade_lock(dentry, AuLock_IR);
e49829fe 8747+out_inval:
1facf9fc 8748+ aufs_read_unlock(dentry, AuLock_IR);
8749+ AuTraceErr(err);
8750+ valid = !err;
e49829fe 8751+out:
027c5e7a 8752+ if (!valid) {
523b37e3 8753+ AuDbg("%pd invalid, %d\n", dentry, valid);
027c5e7a
AM
8754+ d_drop(dentry);
8755+ }
1facf9fc 8756+ return valid;
8757+}
8758+
8759+static void aufs_d_release(struct dentry *dentry)
8760+{
027c5e7a 8761+ if (au_di(dentry)) {
4a4d8108
AM
8762+ au_di_fin(dentry);
8763+ au_hn_di_reinit(dentry);
1facf9fc 8764+ }
1facf9fc 8765+}
8766+
4a4d8108 8767+const struct dentry_operations aufs_dop = {
c06a8ce3
AM
8768+ .d_revalidate = aufs_d_revalidate,
8769+ .d_weak_revalidate = aufs_d_revalidate,
8770+ .d_release = aufs_d_release
1facf9fc 8771+};
79b8bda9
AM
8772+
8773+/* aufs_dop without d_revalidate */
8774+const struct dentry_operations aufs_dop_noreval = {
8775+ .d_release = aufs_d_release
8776+};
7f207e10
AM
8777diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
8778--- /usr/share/empty/fs/aufs/dentry.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
8779+++ linux/fs/aufs/dentry.h 2016-07-25 19:05:34.811159821 +0200
8780@@ -0,0 +1,252 @@
1facf9fc 8781+/*
8cdd5066 8782+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 8783+ *
8784+ * This program, aufs is free software; you can redistribute it and/or modify
8785+ * it under the terms of the GNU General Public License as published by
8786+ * the Free Software Foundation; either version 2 of the License, or
8787+ * (at your option) any later version.
dece6358
AM
8788+ *
8789+ * This program is distributed in the hope that it will be useful,
8790+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
8791+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8792+ * GNU General Public License for more details.
8793+ *
8794+ * You should have received a copy of the GNU General Public License
523b37e3 8795+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 8796+ */
8797+
8798+/*
8799+ * lookup and dentry operations
8800+ */
8801+
8802+#ifndef __AUFS_DENTRY_H__
8803+#define __AUFS_DENTRY_H__
8804+
8805+#ifdef __KERNEL__
8806+
dece6358 8807+#include <linux/dcache.h>
1facf9fc 8808+#include "rwsem.h"
8809+
1facf9fc 8810+struct au_hdentry {
8811+ struct dentry *hd_dentry;
027c5e7a 8812+ aufs_bindex_t hd_id;
1facf9fc 8813+};
8814+
8815+struct au_dinfo {
8816+ atomic_t di_generation;
8817+
dece6358 8818+ struct au_rwsem di_rwsem;
5afbbe0d 8819+ aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
38d290e6 8820+ unsigned char di_tmpfile; /* to allow the different name */
1facf9fc 8821+ struct au_hdentry *di_hdentry;
4a4d8108 8822+} ____cacheline_aligned_in_smp;
1facf9fc 8823+
8824+/* ---------------------------------------------------------------------- */
8825+
5afbbe0d
AM
8826+/* flags for au_lkup_dentry() */
8827+#define AuLkup_ALLOW_NEG 1
8828+#define AuLkup_IGNORE_PERM (1 << 1)
8829+#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
8830+#define au_fset_lkup(flags, name) \
8831+ do { (flags) |= AuLkup_##name; } while (0)
8832+#define au_fclr_lkup(flags, name) \
8833+ do { (flags) &= ~AuLkup_##name; } while (0)
8834+
8835+/* ---------------------------------------------------------------------- */
8836+
1facf9fc 8837+/* dentry.c */
79b8bda9 8838+extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
1facf9fc 8839+struct au_branch;
076b876e 8840+struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
1facf9fc 8841+int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8842+ struct dentry *h_parent, struct au_branch *br);
8843+
5afbbe0d
AM
8844+int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8845+ unsigned int flags);
86dc4139 8846+int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
027c5e7a 8847+int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
1facf9fc 8848+int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
79b8bda9 8849+void au_refresh_dop(struct dentry *dentry, int force_reval);
1facf9fc 8850+
8851+/* dinfo.c */
4a4d8108 8852+void au_di_init_once(void *_di);
027c5e7a
AM
8853+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
8854+void au_di_free(struct au_dinfo *dinfo);
8855+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
8856+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
4a4d8108
AM
8857+int au_di_init(struct dentry *dentry);
8858+void au_di_fin(struct dentry *dentry);
1facf9fc 8859+int au_di_realloc(struct au_dinfo *dinfo, int nbr);
8860+
8861+void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
8862+void di_read_unlock(struct dentry *d, int flags);
8863+void di_downgrade_lock(struct dentry *d, int flags);
8864+void di_write_lock(struct dentry *d, unsigned int lsc);
8865+void di_write_unlock(struct dentry *d);
8866+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
8867+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
8868+void di_write_unlock2(struct dentry *d1, struct dentry *d2);
8869+
8870+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
2cbb1c4b 8871+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
1facf9fc 8872+aufs_bindex_t au_dbtail(struct dentry *dentry);
8873+aufs_bindex_t au_dbtaildir(struct dentry *dentry);
8874+
8875+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
8876+ struct dentry *h_dentry);
027c5e7a
AM
8877+int au_digen_test(struct dentry *dentry, unsigned int sigen);
8878+int au_dbrange_test(struct dentry *dentry);
1facf9fc 8879+void au_update_digen(struct dentry *dentry);
8880+void au_update_dbrange(struct dentry *dentry, int do_put_zero);
5afbbe0d
AM
8881+void au_update_dbtop(struct dentry *dentry);
8882+void au_update_dbbot(struct dentry *dentry);
1facf9fc 8883+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
8884+
8885+/* ---------------------------------------------------------------------- */
8886+
8887+static inline struct au_dinfo *au_di(struct dentry *dentry)
8888+{
8889+ return dentry->d_fsdata;
8890+}
8891+
8892+/* ---------------------------------------------------------------------- */
8893+
8894+/* lock subclass for dinfo */
8895+enum {
8896+ AuLsc_DI_CHILD, /* child first */
4a4d8108 8897+ AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
1facf9fc 8898+ AuLsc_DI_CHILD3, /* copyup dirs */
8899+ AuLsc_DI_PARENT,
8900+ AuLsc_DI_PARENT2,
027c5e7a
AM
8901+ AuLsc_DI_PARENT3,
8902+ AuLsc_DI_TMP /* temp for replacing dinfo */
1facf9fc 8903+};
8904+
8905+/*
8906+ * di_read_lock_child, di_write_lock_child,
8907+ * di_read_lock_child2, di_write_lock_child2,
8908+ * di_read_lock_child3, di_write_lock_child3,
8909+ * di_read_lock_parent, di_write_lock_parent,
8910+ * di_read_lock_parent2, di_write_lock_parent2,
8911+ * di_read_lock_parent3, di_write_lock_parent3,
8912+ */
8913+#define AuReadLockFunc(name, lsc) \
8914+static inline void di_read_lock_##name(struct dentry *d, int flags) \
8915+{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
8916+
8917+#define AuWriteLockFunc(name, lsc) \
8918+static inline void di_write_lock_##name(struct dentry *d) \
8919+{ di_write_lock(d, AuLsc_DI_##lsc); }
8920+
8921+#define AuRWLockFuncs(name, lsc) \
8922+ AuReadLockFunc(name, lsc) \
8923+ AuWriteLockFunc(name, lsc)
8924+
8925+AuRWLockFuncs(child, CHILD);
8926+AuRWLockFuncs(child2, CHILD2);
8927+AuRWLockFuncs(child3, CHILD3);
8928+AuRWLockFuncs(parent, PARENT);
8929+AuRWLockFuncs(parent2, PARENT2);
8930+AuRWLockFuncs(parent3, PARENT3);
8931+
8932+#undef AuReadLockFunc
8933+#undef AuWriteLockFunc
8934+#undef AuRWLockFuncs
8935+
8936+#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
dece6358
AM
8937+#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
8938+#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
1facf9fc 8939+
8940+/* ---------------------------------------------------------------------- */
8941+
8942+/* todo: memory barrier? */
8943+static inline unsigned int au_digen(struct dentry *d)
8944+{
8945+ return atomic_read(&au_di(d)->di_generation);
8946+}
8947+
8948+static inline void au_h_dentry_init(struct au_hdentry *hdentry)
8949+{
8950+ hdentry->hd_dentry = NULL;
8951+}
8952+
5afbbe0d
AM
8953+static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
8954+ aufs_bindex_t bindex)
8955+{
8956+ return di->di_hdentry + bindex;
8957+}
8958+
1facf9fc 8959+static inline void au_hdput(struct au_hdentry *hd)
8960+{
4a4d8108
AM
8961+ if (hd)
8962+ dput(hd->hd_dentry);
1facf9fc 8963+}
8964+
5afbbe0d 8965+static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
1facf9fc 8966+{
1308ab2a 8967+ DiMustAnyLock(dentry);
5afbbe0d 8968+ return au_di(dentry)->di_btop;
1facf9fc 8969+}
8970+
5afbbe0d 8971+static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
1facf9fc 8972+{
1308ab2a 8973+ DiMustAnyLock(dentry);
5afbbe0d 8974+ return au_di(dentry)->di_bbot;
1facf9fc 8975+}
8976+
8977+static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
8978+{
1308ab2a 8979+ DiMustAnyLock(dentry);
1facf9fc 8980+ return au_di(dentry)->di_bwh;
8981+}
8982+
8983+static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
8984+{
1308ab2a 8985+ DiMustAnyLock(dentry);
1facf9fc 8986+ return au_di(dentry)->di_bdiropq;
8987+}
8988+
8989+/* todo: hard/soft set? */
5afbbe0d 8990+static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 8991+{
1308ab2a 8992+ DiMustWriteLock(dentry);
5afbbe0d 8993+ au_di(dentry)->di_btop = bindex;
1facf9fc 8994+}
8995+
5afbbe0d 8996+static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
1facf9fc 8997+{
1308ab2a 8998+ DiMustWriteLock(dentry);
5afbbe0d 8999+ au_di(dentry)->di_bbot = bindex;
1facf9fc 9000+}
9001+
9002+static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9003+{
1308ab2a 9004+ DiMustWriteLock(dentry);
5afbbe0d 9005+ /* dbwh can be outside of btop - bbot range */
1facf9fc 9006+ au_di(dentry)->di_bwh = bindex;
9007+}
9008+
9009+static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9010+{
1308ab2a 9011+ DiMustWriteLock(dentry);
1facf9fc 9012+ au_di(dentry)->di_bdiropq = bindex;
9013+}
9014+
9015+/* ---------------------------------------------------------------------- */
9016+
4a4d8108 9017+#ifdef CONFIG_AUFS_HNOTIFY
1facf9fc 9018+static inline void au_digen_dec(struct dentry *d)
9019+{
e49829fe 9020+ atomic_dec(&au_di(d)->di_generation);
1facf9fc 9021+}
9022+
4a4d8108 9023+static inline void au_hn_di_reinit(struct dentry *dentry)
1facf9fc 9024+{
9025+ dentry->d_fsdata = NULL;
9026+}
9027+#else
4a4d8108
AM
9028+AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9029+#endif /* CONFIG_AUFS_HNOTIFY */
1facf9fc 9030+
9031+#endif /* __KERNEL__ */
9032+#endif /* __AUFS_DENTRY_H__ */
7f207e10
AM
9033diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9034--- /usr/share/empty/fs/aufs/dinfo.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
9035+++ linux/fs/aufs/dinfo.c 2016-07-25 19:05:34.811159821 +0200
9036@@ -0,0 +1,552 @@
1facf9fc 9037+/*
8cdd5066 9038+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 9039+ *
9040+ * This program, aufs is free software; you can redistribute it and/or modify
9041+ * it under the terms of the GNU General Public License as published by
9042+ * the Free Software Foundation; either version 2 of the License, or
9043+ * (at your option) any later version.
dece6358
AM
9044+ *
9045+ * This program is distributed in the hope that it will be useful,
9046+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9047+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9048+ * GNU General Public License for more details.
9049+ *
9050+ * You should have received a copy of the GNU General Public License
523b37e3 9051+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9052+ */
9053+
9054+/*
9055+ * dentry private data
9056+ */
9057+
9058+#include "aufs.h"
9059+
e49829fe 9060+void au_di_init_once(void *_dinfo)
4a4d8108 9061+{
e49829fe 9062+ struct au_dinfo *dinfo = _dinfo;
4a4d8108 9063+
e49829fe 9064+ au_rw_init(&dinfo->di_rwsem);
4a4d8108
AM
9065+}
9066+
027c5e7a 9067+struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
1facf9fc 9068+{
9069+ struct au_dinfo *dinfo;
027c5e7a 9070+ int nbr, i;
1facf9fc 9071+
9072+ dinfo = au_cache_alloc_dinfo();
9073+ if (unlikely(!dinfo))
9074+ goto out;
9075+
5afbbe0d 9076+ nbr = au_sbbot(sb) + 1;
1facf9fc 9077+ if (nbr <= 0)
9078+ nbr = 1;
9079+ dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
027c5e7a
AM
9080+ if (dinfo->di_hdentry) {
9081+ au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
5afbbe0d
AM
9082+ dinfo->di_btop = -1;
9083+ dinfo->di_bbot = -1;
027c5e7a
AM
9084+ dinfo->di_bwh = -1;
9085+ dinfo->di_bdiropq = -1;
38d290e6 9086+ dinfo->di_tmpfile = 0;
027c5e7a
AM
9087+ for (i = 0; i < nbr; i++)
9088+ dinfo->di_hdentry[i].hd_id = -1;
9089+ goto out;
9090+ }
1facf9fc 9091+
1facf9fc 9092+ au_cache_free_dinfo(dinfo);
027c5e7a
AM
9093+ dinfo = NULL;
9094+
4f0767ce 9095+out:
027c5e7a 9096+ return dinfo;
1facf9fc 9097+}
9098+
027c5e7a 9099+void au_di_free(struct au_dinfo *dinfo)
4a4d8108 9100+{
4a4d8108 9101+ struct au_hdentry *p;
5afbbe0d 9102+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
9103+
9104+ /* dentry may not be revalidated */
5afbbe0d 9105+ bindex = dinfo->di_btop;
4a4d8108 9106+ if (bindex >= 0) {
5afbbe0d
AM
9107+ bbot = dinfo->di_bbot;
9108+ p = au_hdentry(dinfo, bindex);
9109+ while (bindex++ <= bbot)
4a4d8108
AM
9110+ au_hdput(p++);
9111+ }
027c5e7a
AM
9112+ kfree(dinfo->di_hdentry);
9113+ au_cache_free_dinfo(dinfo);
9114+}
9115+
9116+void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9117+{
9118+ struct au_hdentry *p;
9119+ aufs_bindex_t bi;
9120+
9121+ AuRwMustWriteLock(&a->di_rwsem);
9122+ AuRwMustWriteLock(&b->di_rwsem);
9123+
9124+#define DiSwap(v, name) \
9125+ do { \
9126+ v = a->di_##name; \
9127+ a->di_##name = b->di_##name; \
9128+ b->di_##name = v; \
9129+ } while (0)
9130+
9131+ DiSwap(p, hdentry);
5afbbe0d
AM
9132+ DiSwap(bi, btop);
9133+ DiSwap(bi, bbot);
027c5e7a
AM
9134+ DiSwap(bi, bwh);
9135+ DiSwap(bi, bdiropq);
9136+ /* smp_mb(); */
9137+
9138+#undef DiSwap
9139+}
9140+
9141+void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9142+{
9143+ AuRwMustWriteLock(&dst->di_rwsem);
9144+ AuRwMustWriteLock(&src->di_rwsem);
9145+
5afbbe0d
AM
9146+ dst->di_btop = src->di_btop;
9147+ dst->di_bbot = src->di_bbot;
027c5e7a
AM
9148+ dst->di_bwh = src->di_bwh;
9149+ dst->di_bdiropq = src->di_bdiropq;
9150+ /* smp_mb(); */
9151+}
9152+
9153+int au_di_init(struct dentry *dentry)
9154+{
9155+ int err;
9156+ struct super_block *sb;
9157+ struct au_dinfo *dinfo;
9158+
9159+ err = 0;
9160+ sb = dentry->d_sb;
9161+ dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9162+ if (dinfo) {
9163+ atomic_set(&dinfo->di_generation, au_sigen(sb));
9164+ /* smp_mb(); */ /* atomic_set */
9165+ dentry->d_fsdata = dinfo;
9166+ } else
9167+ err = -ENOMEM;
9168+
9169+ return err;
9170+}
9171+
9172+void au_di_fin(struct dentry *dentry)
9173+{
9174+ struct au_dinfo *dinfo;
9175+
9176+ dinfo = au_di(dentry);
9177+ AuRwDestroy(&dinfo->di_rwsem);
9178+ au_di_free(dinfo);
4a4d8108
AM
9179+}
9180+
1facf9fc 9181+int au_di_realloc(struct au_dinfo *dinfo, int nbr)
9182+{
9183+ int err, sz;
9184+ struct au_hdentry *hdp;
9185+
1308ab2a 9186+ AuRwMustWriteLock(&dinfo->di_rwsem);
9187+
1facf9fc 9188+ err = -ENOMEM;
5afbbe0d 9189+ sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
1facf9fc 9190+ if (!sz)
9191+ sz = sizeof(*hdp);
9192+ hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS);
9193+ if (hdp) {
9194+ dinfo->di_hdentry = hdp;
9195+ err = 0;
9196+ }
9197+
9198+ return err;
9199+}
9200+
9201+/* ---------------------------------------------------------------------- */
9202+
9203+static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9204+{
9205+ switch (lsc) {
9206+ case AuLsc_DI_CHILD:
9207+ ii_write_lock_child(inode);
9208+ break;
9209+ case AuLsc_DI_CHILD2:
9210+ ii_write_lock_child2(inode);
9211+ break;
9212+ case AuLsc_DI_CHILD3:
9213+ ii_write_lock_child3(inode);
9214+ break;
9215+ case AuLsc_DI_PARENT:
9216+ ii_write_lock_parent(inode);
9217+ break;
9218+ case AuLsc_DI_PARENT2:
9219+ ii_write_lock_parent2(inode);
9220+ break;
9221+ case AuLsc_DI_PARENT3:
9222+ ii_write_lock_parent3(inode);
9223+ break;
9224+ default:
9225+ BUG();
9226+ }
9227+}
9228+
9229+static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9230+{
9231+ switch (lsc) {
9232+ case AuLsc_DI_CHILD:
9233+ ii_read_lock_child(inode);
9234+ break;
9235+ case AuLsc_DI_CHILD2:
9236+ ii_read_lock_child2(inode);
9237+ break;
9238+ case AuLsc_DI_CHILD3:
9239+ ii_read_lock_child3(inode);
9240+ break;
9241+ case AuLsc_DI_PARENT:
9242+ ii_read_lock_parent(inode);
9243+ break;
9244+ case AuLsc_DI_PARENT2:
9245+ ii_read_lock_parent2(inode);
9246+ break;
9247+ case AuLsc_DI_PARENT3:
9248+ ii_read_lock_parent3(inode);
9249+ break;
9250+ default:
9251+ BUG();
9252+ }
9253+}
9254+
9255+void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9256+{
5527c038
JR
9257+ struct inode *inode;
9258+
dece6358 9259+ au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9260+ if (d_really_is_positive(d)) {
9261+ inode = d_inode(d);
1facf9fc 9262+ if (au_ftest_lock(flags, IW))
5527c038 9263+ do_ii_write_lock(inode, lsc);
1facf9fc 9264+ else if (au_ftest_lock(flags, IR))
5527c038 9265+ do_ii_read_lock(inode, lsc);
1facf9fc 9266+ }
9267+}
9268+
9269+void di_read_unlock(struct dentry *d, int flags)
9270+{
5527c038
JR
9271+ struct inode *inode;
9272+
9273+ if (d_really_is_positive(d)) {
9274+ inode = d_inode(d);
027c5e7a
AM
9275+ if (au_ftest_lock(flags, IW)) {
9276+ au_dbg_verify_dinode(d);
5527c038 9277+ ii_write_unlock(inode);
027c5e7a
AM
9278+ } else if (au_ftest_lock(flags, IR)) {
9279+ au_dbg_verify_dinode(d);
5527c038 9280+ ii_read_unlock(inode);
027c5e7a 9281+ }
1facf9fc 9282+ }
dece6358 9283+ au_rw_read_unlock(&au_di(d)->di_rwsem);
1facf9fc 9284+}
9285+
9286+void di_downgrade_lock(struct dentry *d, int flags)
9287+{
5527c038
JR
9288+ if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9289+ ii_downgrade_lock(d_inode(d));
dece6358 9290+ au_rw_dgrade_lock(&au_di(d)->di_rwsem);
1facf9fc 9291+}
9292+
9293+void di_write_lock(struct dentry *d, unsigned int lsc)
9294+{
dece6358 9295+ au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
5527c038
JR
9296+ if (d_really_is_positive(d))
9297+ do_ii_write_lock(d_inode(d), lsc);
1facf9fc 9298+}
9299+
9300+void di_write_unlock(struct dentry *d)
9301+{
027c5e7a 9302+ au_dbg_verify_dinode(d);
5527c038
JR
9303+ if (d_really_is_positive(d))
9304+ ii_write_unlock(d_inode(d));
dece6358 9305+ au_rw_write_unlock(&au_di(d)->di_rwsem);
1facf9fc 9306+}
9307+
9308+void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9309+{
9310+ AuDebugOn(d1 == d2
5527c038 9311+ || d_inode(d1) == d_inode(d2)
1facf9fc 9312+ || d1->d_sb != d2->d_sb);
9313+
9314+ if (isdir && au_test_subdir(d1, d2)) {
9315+ di_write_lock_child(d1);
9316+ di_write_lock_child2(d2);
9317+ } else {
9318+ /* there should be no races */
9319+ di_write_lock_child(d2);
9320+ di_write_lock_child2(d1);
9321+ }
9322+}
9323+
9324+void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9325+{
9326+ AuDebugOn(d1 == d2
5527c038 9327+ || d_inode(d1) == d_inode(d2)
1facf9fc 9328+ || d1->d_sb != d2->d_sb);
9329+
9330+ if (isdir && au_test_subdir(d1, d2)) {
9331+ di_write_lock_parent(d1);
9332+ di_write_lock_parent2(d2);
9333+ } else {
9334+ /* there should be no races */
9335+ di_write_lock_parent(d2);
9336+ di_write_lock_parent2(d1);
9337+ }
9338+}
9339+
9340+void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9341+{
9342+ di_write_unlock(d1);
5527c038 9343+ if (d_inode(d1) == d_inode(d2))
dece6358 9344+ au_rw_write_unlock(&au_di(d2)->di_rwsem);
1facf9fc 9345+ else
9346+ di_write_unlock(d2);
9347+}
9348+
9349+/* ---------------------------------------------------------------------- */
9350+
9351+struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9352+{
9353+ struct dentry *d;
9354+
1308ab2a 9355+ DiMustAnyLock(dentry);
9356+
5afbbe0d 9357+ if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
1facf9fc 9358+ return NULL;
9359+ AuDebugOn(bindex < 0);
5afbbe0d 9360+ d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
c1595e42 9361+ AuDebugOn(d && au_dcount(d) <= 0);
1facf9fc 9362+ return d;
9363+}
9364+
2cbb1c4b
JR
9365+/*
9366+ * extended version of au_h_dptr().
38d290e6
JR
9367+ * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9368+ * error.
2cbb1c4b
JR
9369+ */
9370+struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9371+{
9372+ struct dentry *h_dentry;
9373+ struct inode *inode, *h_inode;
9374+
5527c038 9375+ AuDebugOn(d_really_is_negative(dentry));
2cbb1c4b
JR
9376+
9377+ h_dentry = NULL;
5afbbe0d
AM
9378+ if (au_dbtop(dentry) <= bindex
9379+ && bindex <= au_dbbot(dentry))
2cbb1c4b 9380+ h_dentry = au_h_dptr(dentry, bindex);
38d290e6 9381+ if (h_dentry && !au_d_linkable(h_dentry)) {
2cbb1c4b
JR
9382+ dget(h_dentry);
9383+ goto out; /* success */
9384+ }
9385+
5527c038 9386+ inode = d_inode(dentry);
5afbbe0d
AM
9387+ AuDebugOn(bindex < au_ibtop(inode));
9388+ AuDebugOn(au_ibbot(inode) < bindex);
2cbb1c4b
JR
9389+ h_inode = au_h_iptr(inode, bindex);
9390+ h_dentry = d_find_alias(h_inode);
9391+ if (h_dentry) {
9392+ if (!IS_ERR(h_dentry)) {
38d290e6 9393+ if (!au_d_linkable(h_dentry))
2cbb1c4b
JR
9394+ goto out; /* success */
9395+ dput(h_dentry);
9396+ } else
9397+ goto out;
9398+ }
9399+
9400+ if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9401+ h_dentry = au_plink_lkup(inode, bindex);
9402+ AuDebugOn(!h_dentry);
9403+ if (!IS_ERR(h_dentry)) {
9404+ if (!au_d_hashed_positive(h_dentry))
9405+ goto out; /* success */
9406+ dput(h_dentry);
9407+ h_dentry = NULL;
9408+ }
9409+ }
9410+
9411+out:
9412+ AuDbgDentry(h_dentry);
9413+ return h_dentry;
9414+}
9415+
1facf9fc 9416+aufs_bindex_t au_dbtail(struct dentry *dentry)
9417+{
5afbbe0d 9418+ aufs_bindex_t bbot, bwh;
1facf9fc 9419+
5afbbe0d
AM
9420+ bbot = au_dbbot(dentry);
9421+ if (0 <= bbot) {
1facf9fc 9422+ bwh = au_dbwh(dentry);
9423+ if (!bwh)
9424+ return bwh;
5afbbe0d 9425+ if (0 < bwh && bwh < bbot)
1facf9fc 9426+ return bwh - 1;
9427+ }
5afbbe0d 9428+ return bbot;
1facf9fc 9429+}
9430+
9431+aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9432+{
5afbbe0d 9433+ aufs_bindex_t bbot, bopq;
1facf9fc 9434+
5afbbe0d
AM
9435+ bbot = au_dbtail(dentry);
9436+ if (0 <= bbot) {
1facf9fc 9437+ bopq = au_dbdiropq(dentry);
5afbbe0d
AM
9438+ if (0 <= bopq && bopq < bbot)
9439+ bbot = bopq;
1facf9fc 9440+ }
5afbbe0d 9441+ return bbot;
1facf9fc 9442+}
9443+
9444+/* ---------------------------------------------------------------------- */
9445+
9446+void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9447+ struct dentry *h_dentry)
9448+{
5afbbe0d
AM
9449+ struct au_dinfo *dinfo;
9450+ struct au_hdentry *hd;
027c5e7a 9451+ struct au_branch *br;
1facf9fc 9452+
1308ab2a 9453+ DiMustWriteLock(dentry);
9454+
5afbbe0d
AM
9455+ dinfo = au_di(dentry);
9456+ hd = au_hdentry(dinfo, bindex);
4a4d8108 9457+ au_hdput(hd);
1facf9fc 9458+ hd->hd_dentry = h_dentry;
027c5e7a
AM
9459+ if (h_dentry) {
9460+ br = au_sbr(dentry->d_sb, bindex);
9461+ hd->hd_id = br->br_id;
9462+ }
9463+}
9464+
9465+int au_dbrange_test(struct dentry *dentry)
9466+{
9467+ int err;
5afbbe0d 9468+ aufs_bindex_t btop, bbot;
027c5e7a
AM
9469+
9470+ err = 0;
5afbbe0d
AM
9471+ btop = au_dbtop(dentry);
9472+ bbot = au_dbbot(dentry);
9473+ if (btop >= 0)
9474+ AuDebugOn(bbot < 0 && btop > bbot);
027c5e7a
AM
9475+ else {
9476+ err = -EIO;
5afbbe0d 9477+ AuDebugOn(bbot >= 0);
027c5e7a
AM
9478+ }
9479+
9480+ return err;
9481+}
9482+
9483+int au_digen_test(struct dentry *dentry, unsigned int sigen)
9484+{
9485+ int err;
9486+
9487+ err = 0;
9488+ if (unlikely(au_digen(dentry) != sigen
5527c038 9489+ || au_iigen_test(d_inode(dentry), sigen)))
027c5e7a
AM
9490+ err = -EIO;
9491+
9492+ return err;
1facf9fc 9493+}
9494+
9495+void au_update_digen(struct dentry *dentry)
9496+{
9497+ atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9498+ /* smp_mb(); */ /* atomic_set */
9499+}
9500+
9501+void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9502+{
9503+ struct au_dinfo *dinfo;
9504+ struct dentry *h_d;
4a4d8108 9505+ struct au_hdentry *hdp;
5afbbe0d 9506+ aufs_bindex_t bindex, bbot;
1facf9fc 9507+
1308ab2a 9508+ DiMustWriteLock(dentry);
9509+
1facf9fc 9510+ dinfo = au_di(dentry);
5afbbe0d 9511+ if (!dinfo || dinfo->di_btop < 0)
1facf9fc 9512+ return;
9513+
9514+ if (do_put_zero) {
5afbbe0d
AM
9515+ bbot = dinfo->di_bbot;
9516+ bindex = dinfo->di_btop;
9517+ hdp = au_hdentry(dinfo, bindex);
9518+ for (; bindex <= bbot; bindex++, hdp++) {
9519+ h_d = hdp->hd_dentry;
5527c038 9520+ if (h_d && d_is_negative(h_d))
1facf9fc 9521+ au_set_h_dptr(dentry, bindex, NULL);
9522+ }
9523+ }
9524+
5afbbe0d
AM
9525+ dinfo->di_btop = 0;
9526+ hdp = au_hdentry(dinfo, dinfo->di_btop);
9527+ for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9528+ if (hdp->hd_dentry)
1facf9fc 9529+ break;
5afbbe0d
AM
9530+ if (dinfo->di_btop > dinfo->di_bbot) {
9531+ dinfo->di_btop = -1;
9532+ dinfo->di_bbot = -1;
1facf9fc 9533+ return;
9534+ }
9535+
5afbbe0d
AM
9536+ hdp = au_hdentry(dinfo, dinfo->di_bbot);
9537+ for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9538+ if (hdp->hd_dentry)
1facf9fc 9539+ break;
5afbbe0d 9540+ AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
1facf9fc 9541+}
9542+
5afbbe0d 9543+void au_update_dbtop(struct dentry *dentry)
1facf9fc 9544+{
5afbbe0d 9545+ aufs_bindex_t bindex, bbot;
1facf9fc 9546+ struct dentry *h_dentry;
9547+
5afbbe0d
AM
9548+ bbot = au_dbbot(dentry);
9549+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
1facf9fc 9550+ h_dentry = au_h_dptr(dentry, bindex);
9551+ if (!h_dentry)
9552+ continue;
5527c038 9553+ if (d_is_positive(h_dentry)) {
5afbbe0d 9554+ au_set_dbtop(dentry, bindex);
1facf9fc 9555+ return;
9556+ }
9557+ au_set_h_dptr(dentry, bindex, NULL);
9558+ }
9559+}
9560+
5afbbe0d 9561+void au_update_dbbot(struct dentry *dentry)
1facf9fc 9562+{
5afbbe0d 9563+ aufs_bindex_t bindex, btop;
1facf9fc 9564+ struct dentry *h_dentry;
9565+
5afbbe0d
AM
9566+ btop = au_dbtop(dentry);
9567+ for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
1facf9fc 9568+ h_dentry = au_h_dptr(dentry, bindex);
9569+ if (!h_dentry)
9570+ continue;
5527c038 9571+ if (d_is_positive(h_dentry)) {
5afbbe0d 9572+ au_set_dbbot(dentry, bindex);
1facf9fc 9573+ return;
9574+ }
9575+ au_set_h_dptr(dentry, bindex, NULL);
9576+ }
9577+}
9578+
9579+int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9580+{
5afbbe0d 9581+ aufs_bindex_t bindex, bbot;
1facf9fc 9582+
5afbbe0d
AM
9583+ bbot = au_dbbot(dentry);
9584+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
1facf9fc 9585+ if (au_h_dptr(dentry, bindex) == h_dentry)
9586+ return bindex;
9587+ return -1;
9588+}
7f207e10
AM
9589diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9590--- /usr/share/empty/fs/aufs/dir.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 9591+++ linux/fs/aufs/dir.c 2016-07-25 19:05:34.811159821 +0200
febd17d6 9592@@ -0,0 +1,756 @@
1facf9fc 9593+/*
8cdd5066 9594+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 9595+ *
9596+ * This program, aufs is free software; you can redistribute it and/or modify
9597+ * it under the terms of the GNU General Public License as published by
9598+ * the Free Software Foundation; either version 2 of the License, or
9599+ * (at your option) any later version.
dece6358
AM
9600+ *
9601+ * This program is distributed in the hope that it will be useful,
9602+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
9603+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9604+ * GNU General Public License for more details.
9605+ *
9606+ * You should have received a copy of the GNU General Public License
523b37e3 9607+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 9608+ */
9609+
9610+/*
9611+ * directory operations
9612+ */
9613+
9614+#include <linux/fs_stack.h>
9615+#include "aufs.h"
9616+
9617+void au_add_nlink(struct inode *dir, struct inode *h_dir)
9618+{
9dbd164d
AM
9619+ unsigned int nlink;
9620+
1facf9fc 9621+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9622+
9dbd164d
AM
9623+ nlink = dir->i_nlink;
9624+ nlink += h_dir->i_nlink - 2;
1facf9fc 9625+ if (h_dir->i_nlink < 2)
9dbd164d 9626+ nlink += 2;
f6b6e03d 9627+ smp_mb(); /* for i_nlink */
7eafdf33 9628+ /* 0 can happen in revaliding */
92d182d2 9629+ set_nlink(dir, nlink);
1facf9fc 9630+}
9631+
9632+void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9633+{
9dbd164d
AM
9634+ unsigned int nlink;
9635+
1facf9fc 9636+ AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9637+
9dbd164d
AM
9638+ nlink = dir->i_nlink;
9639+ nlink -= h_dir->i_nlink - 2;
1facf9fc 9640+ if (h_dir->i_nlink < 2)
9dbd164d 9641+ nlink -= 2;
f6b6e03d 9642+ smp_mb(); /* for i_nlink */
92d182d2 9643+ /* nlink == 0 means the branch-fs is broken */
9dbd164d 9644+ set_nlink(dir, nlink);
1facf9fc 9645+}
9646+
1308ab2a 9647+loff_t au_dir_size(struct file *file, struct dentry *dentry)
9648+{
9649+ loff_t sz;
5afbbe0d 9650+ aufs_bindex_t bindex, bbot;
1308ab2a 9651+ struct file *h_file;
9652+ struct dentry *h_dentry;
9653+
9654+ sz = 0;
9655+ if (file) {
2000de60 9656+ AuDebugOn(!d_is_dir(file->f_path.dentry));
1308ab2a 9657+
5afbbe0d
AM
9658+ bbot = au_fbbot_dir(file);
9659+ for (bindex = au_fbtop(file);
9660+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 9661+ bindex++) {
4a4d8108 9662+ h_file = au_hf_dir(file, bindex);
c06a8ce3
AM
9663+ if (h_file && file_inode(h_file))
9664+ sz += vfsub_f_size_read(h_file);
1308ab2a 9665+ }
9666+ } else {
9667+ AuDebugOn(!dentry);
2000de60 9668+ AuDebugOn(!d_is_dir(dentry));
1308ab2a 9669+
5afbbe0d
AM
9670+ bbot = au_dbtaildir(dentry);
9671+ for (bindex = au_dbtop(dentry);
9672+ bindex <= bbot && sz < KMALLOC_MAX_SIZE;
1308ab2a 9673+ bindex++) {
9674+ h_dentry = au_h_dptr(dentry, bindex);
5527c038
JR
9675+ if (h_dentry && d_is_positive(h_dentry))
9676+ sz += i_size_read(d_inode(h_dentry));
1308ab2a 9677+ }
9678+ }
9679+ if (sz < KMALLOC_MAX_SIZE)
9680+ sz = roundup_pow_of_two(sz);
9681+ if (sz > KMALLOC_MAX_SIZE)
9682+ sz = KMALLOC_MAX_SIZE;
9683+ else if (sz < NAME_MAX) {
9684+ BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
9685+ sz = AUFS_RDBLK_DEF;
9686+ }
9687+ return sz;
9688+}
9689+
b912730e
AM
9690+struct au_dir_ts_arg {
9691+ struct dentry *dentry;
9692+ aufs_bindex_t brid;
9693+};
9694+
9695+static void au_do_dir_ts(void *arg)
9696+{
9697+ struct au_dir_ts_arg *a = arg;
9698+ struct au_dtime dt;
9699+ struct path h_path;
9700+ struct inode *dir, *h_dir;
9701+ struct super_block *sb;
9702+ struct au_branch *br;
9703+ struct au_hinode *hdir;
9704+ int err;
5afbbe0d 9705+ aufs_bindex_t btop, bindex;
b912730e
AM
9706+
9707+ sb = a->dentry->d_sb;
5527c038 9708+ if (d_really_is_negative(a->dentry))
b912730e 9709+ goto out;
5527c038 9710+ /* no dir->i_mutex lock */
b95c5147
AM
9711+ aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
9712+
5527c038 9713+ dir = d_inode(a->dentry);
5afbbe0d 9714+ btop = au_ibtop(dir);
b912730e 9715+ bindex = au_br_index(sb, a->brid);
5afbbe0d 9716+ if (bindex < btop)
b912730e
AM
9717+ goto out_unlock;
9718+
9719+ br = au_sbr(sb, bindex);
9720+ h_path.dentry = au_h_dptr(a->dentry, bindex);
9721+ if (!h_path.dentry)
9722+ goto out_unlock;
9723+ h_path.mnt = au_br_mnt(br);
9724+ au_dtime_store(&dt, a->dentry, &h_path);
9725+
5afbbe0d 9726+ br = au_sbr(sb, btop);
b912730e
AM
9727+ if (!au_br_writable(br->br_perm))
9728+ goto out_unlock;
5afbbe0d 9729+ h_path.dentry = au_h_dptr(a->dentry, btop);
b912730e
AM
9730+ h_path.mnt = au_br_mnt(br);
9731+ err = vfsub_mnt_want_write(h_path.mnt);
9732+ if (err)
9733+ goto out_unlock;
5afbbe0d
AM
9734+ hdir = au_hi(dir, btop);
9735+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
9736+ h_dir = au_h_iptr(dir, btop);
b912730e
AM
9737+ if (h_dir->i_nlink
9738+ && timespec_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
9739+ dt.dt_h_path = h_path;
9740+ au_dtime_revert(&dt);
9741+ }
5afbbe0d 9742+ au_hn_inode_unlock(hdir);
b912730e
AM
9743+ vfsub_mnt_drop_write(h_path.mnt);
9744+ au_cpup_attr_timesizes(dir);
9745+
9746+out_unlock:
9747+ aufs_read_unlock(a->dentry, AuLock_DW);
9748+out:
9749+ dput(a->dentry);
9750+ au_nwt_done(&au_sbi(sb)->si_nowait);
9751+ kfree(arg);
9752+}
9753+
9754+void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
9755+{
9756+ int perm, wkq_err;
5afbbe0d 9757+ aufs_bindex_t btop;
b912730e
AM
9758+ struct au_dir_ts_arg *arg;
9759+ struct dentry *dentry;
9760+ struct super_block *sb;
9761+
9762+ IMustLock(dir);
9763+
9764+ dentry = d_find_any_alias(dir);
9765+ AuDebugOn(!dentry);
9766+ sb = dentry->d_sb;
5afbbe0d
AM
9767+ btop = au_ibtop(dir);
9768+ if (btop == bindex) {
b912730e
AM
9769+ au_cpup_attr_timesizes(dir);
9770+ goto out;
9771+ }
9772+
5afbbe0d 9773+ perm = au_sbr_perm(sb, btop);
b912730e
AM
9774+ if (!au_br_writable(perm))
9775+ goto out;
9776+
9777+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
9778+ if (!arg)
9779+ goto out;
9780+
9781+ arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
9782+ arg->brid = au_sbr_id(sb, bindex);
9783+ wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
9784+ if (unlikely(wkq_err)) {
9785+ pr_err("wkq %d\n", wkq_err);
9786+ dput(dentry);
9787+ kfree(arg);
9788+ }
9789+
9790+out:
9791+ dput(dentry);
9792+}
9793+
1facf9fc 9794+/* ---------------------------------------------------------------------- */
9795+
9796+static int reopen_dir(struct file *file)
9797+{
9798+ int err;
9799+ unsigned int flags;
5afbbe0d 9800+ aufs_bindex_t bindex, btail, btop;
1facf9fc 9801+ struct dentry *dentry, *h_dentry;
9802+ struct file *h_file;
9803+
9804+ /* open all lower dirs */
2000de60 9805+ dentry = file->f_path.dentry;
5afbbe0d
AM
9806+ btop = au_dbtop(dentry);
9807+ for (bindex = au_fbtop(file); bindex < btop; bindex++)
1facf9fc 9808+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 9809+ au_set_fbtop(file, btop);
1facf9fc 9810+
9811+ btail = au_dbtaildir(dentry);
5afbbe0d 9812+ for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
1facf9fc 9813+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d 9814+ au_set_fbbot_dir(file, btail);
1facf9fc 9815+
4a4d8108 9816+ flags = vfsub_file_flags(file);
5afbbe0d 9817+ for (bindex = btop; bindex <= btail; bindex++) {
1facf9fc 9818+ h_dentry = au_h_dptr(dentry, bindex);
9819+ if (!h_dentry)
9820+ continue;
4a4d8108 9821+ h_file = au_hf_dir(file, bindex);
1facf9fc 9822+ if (h_file)
9823+ continue;
9824+
392086de 9825+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 9826+ err = PTR_ERR(h_file);
9827+ if (IS_ERR(h_file))
9828+ goto out; /* close all? */
9829+ au_set_h_fptr(file, bindex, h_file);
9830+ }
9831+ au_update_figen(file);
9832+ /* todo: necessary? */
9833+ /* file->f_ra = h_file->f_ra; */
9834+ err = 0;
9835+
4f0767ce 9836+out:
1facf9fc 9837+ return err;
9838+}
9839+
b912730e 9840+static int do_open_dir(struct file *file, int flags, struct file *h_file)
1facf9fc 9841+{
9842+ int err;
9843+ aufs_bindex_t bindex, btail;
9844+ struct dentry *dentry, *h_dentry;
8cdd5066 9845+ struct vfsmount *mnt;
1facf9fc 9846+
1308ab2a 9847+ FiMustWriteLock(file);
b912730e 9848+ AuDebugOn(h_file);
1308ab2a 9849+
523b37e3 9850+ err = 0;
8cdd5066 9851+ mnt = file->f_path.mnt;
2000de60 9852+ dentry = file->f_path.dentry;
5527c038 9853+ file->f_version = d_inode(dentry)->i_version;
5afbbe0d
AM
9854+ bindex = au_dbtop(dentry);
9855+ au_set_fbtop(file, bindex);
1facf9fc 9856+ btail = au_dbtaildir(dentry);
5afbbe0d 9857+ au_set_fbbot_dir(file, btail);
1facf9fc 9858+ for (; !err && bindex <= btail; bindex++) {
9859+ h_dentry = au_h_dptr(dentry, bindex);
9860+ if (!h_dentry)
9861+ continue;
9862+
8cdd5066
JR
9863+ err = vfsub_test_mntns(mnt, h_dentry->d_sb);
9864+ if (unlikely(err))
9865+ break;
392086de 9866+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
1facf9fc 9867+ if (IS_ERR(h_file)) {
9868+ err = PTR_ERR(h_file);
9869+ break;
9870+ }
9871+ au_set_h_fptr(file, bindex, h_file);
9872+ }
9873+ au_update_figen(file);
9874+ /* todo: necessary? */
9875+ /* file->f_ra = h_file->f_ra; */
9876+ if (!err)
9877+ return 0; /* success */
9878+
9879+ /* close all */
5afbbe0d 9880+ for (bindex = au_fbtop(file); bindex <= btail; bindex++)
1facf9fc 9881+ au_set_h_fptr(file, bindex, NULL);
5afbbe0d
AM
9882+ au_set_fbtop(file, -1);
9883+ au_set_fbbot_dir(file, -1);
4a4d8108 9884+
1facf9fc 9885+ return err;
9886+}
9887+
9888+static int aufs_open_dir(struct inode *inode __maybe_unused,
9889+ struct file *file)
9890+{
4a4d8108
AM
9891+ int err;
9892+ struct super_block *sb;
9893+ struct au_fidir *fidir;
9894+
9895+ err = -ENOMEM;
2000de60 9896+ sb = file->f_path.dentry->d_sb;
4a4d8108 9897+ si_read_lock(sb, AuLock_FLUSH);
e49829fe 9898+ fidir = au_fidir_alloc(sb);
4a4d8108 9899+ if (fidir) {
b912730e
AM
9900+ struct au_do_open_args args = {
9901+ .open = do_open_dir,
9902+ .fidir = fidir
9903+ };
9904+ err = au_do_open(file, &args);
4a4d8108
AM
9905+ if (unlikely(err))
9906+ kfree(fidir);
9907+ }
9908+ si_read_unlock(sb);
9909+ return err;
1facf9fc 9910+}
9911+
9912+static int aufs_release_dir(struct inode *inode __maybe_unused,
9913+ struct file *file)
9914+{
9915+ struct au_vdir *vdir_cache;
4a4d8108
AM
9916+ struct au_finfo *finfo;
9917+ struct au_fidir *fidir;
5afbbe0d 9918+ aufs_bindex_t bindex, bbot;
1facf9fc 9919+
4a4d8108
AM
9920+ finfo = au_fi(file);
9921+ fidir = finfo->fi_hdir;
9922+ if (fidir) {
076b876e 9923+ au_sphl_del(&finfo->fi_hlist,
2000de60 9924+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108
AM
9925+ vdir_cache = fidir->fd_vdir_cache; /* lock-free */
9926+ if (vdir_cache)
9927+ au_vdir_free(vdir_cache);
9928+
9929+ bindex = finfo->fi_btop;
9930+ if (bindex >= 0) {
9931+ /*
9932+ * calls fput() instead of filp_close(),
9933+ * since no dnotify or lock for the lower file.
9934+ */
5afbbe0d
AM
9935+ bbot = fidir->fd_bbot;
9936+ for (; bindex <= bbot; bindex++)
4a4d8108
AM
9937+ au_set_h_fptr(file, bindex, NULL);
9938+ }
9939+ kfree(fidir);
9940+ finfo->fi_hdir = NULL;
1facf9fc 9941+ }
1facf9fc 9942+ au_finfo_fin(file);
1facf9fc 9943+ return 0;
9944+}
9945+
9946+/* ---------------------------------------------------------------------- */
9947+
4a4d8108
AM
9948+static int au_do_flush_dir(struct file *file, fl_owner_t id)
9949+{
9950+ int err;
5afbbe0d 9951+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
9952+ struct file *h_file;
9953+
9954+ err = 0;
5afbbe0d
AM
9955+ bbot = au_fbbot_dir(file);
9956+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
9957+ h_file = au_hf_dir(file, bindex);
9958+ if (h_file)
9959+ err = vfsub_flush(h_file, id);
9960+ }
9961+ return err;
9962+}
9963+
9964+static int aufs_flush_dir(struct file *file, fl_owner_t id)
9965+{
9966+ return au_do_flush(file, id, au_do_flush_dir);
9967+}
9968+
9969+/* ---------------------------------------------------------------------- */
9970+
1facf9fc 9971+static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
9972+{
9973+ int err;
5afbbe0d 9974+ aufs_bindex_t bbot, bindex;
1facf9fc 9975+ struct inode *inode;
9976+ struct super_block *sb;
9977+
9978+ err = 0;
9979+ sb = dentry->d_sb;
5527c038 9980+ inode = d_inode(dentry);
1facf9fc 9981+ IMustLock(inode);
5afbbe0d
AM
9982+ bbot = au_dbbot(dentry);
9983+ for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
1facf9fc 9984+ struct path h_path;
1facf9fc 9985+
9986+ if (au_test_ro(sb, bindex, inode))
9987+ continue;
9988+ h_path.dentry = au_h_dptr(dentry, bindex);
9989+ if (!h_path.dentry)
9990+ continue;
1facf9fc 9991+
1facf9fc 9992+ h_path.mnt = au_sbr_mnt(sb, bindex);
53392da6 9993+ err = vfsub_fsync(NULL, &h_path, datasync);
1facf9fc 9994+ }
9995+
9996+ return err;
9997+}
9998+
9999+static int au_do_fsync_dir(struct file *file, int datasync)
10000+{
10001+ int err;
5afbbe0d 10002+ aufs_bindex_t bbot, bindex;
1facf9fc 10003+ struct file *h_file;
10004+ struct super_block *sb;
10005+ struct inode *inode;
1facf9fc 10006+
10007+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
10008+ if (unlikely(err))
10009+ goto out;
10010+
c06a8ce3 10011+ inode = file_inode(file);
b912730e 10012+ sb = inode->i_sb;
5afbbe0d
AM
10013+ bbot = au_fbbot_dir(file);
10014+ for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
4a4d8108 10015+ h_file = au_hf_dir(file, bindex);
1facf9fc 10016+ if (!h_file || au_test_ro(sb, bindex, inode))
10017+ continue;
10018+
53392da6 10019+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
1facf9fc 10020+ }
10021+
4f0767ce 10022+out:
1facf9fc 10023+ return err;
10024+}
10025+
10026+/*
10027+ * @file may be NULL
10028+ */
1e00d052
AM
10029+static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10030+ int datasync)
1facf9fc 10031+{
10032+ int err;
b752ccd1 10033+ struct dentry *dentry;
5527c038 10034+ struct inode *inode;
1facf9fc 10035+ struct super_block *sb;
1facf9fc 10036+
10037+ err = 0;
2000de60 10038+ dentry = file->f_path.dentry;
5527c038 10039+ inode = d_inode(dentry);
febd17d6 10040+ inode_lock(inode);
1facf9fc 10041+ sb = dentry->d_sb;
10042+ si_noflush_read_lock(sb);
10043+ if (file)
10044+ err = au_do_fsync_dir(file, datasync);
10045+ else {
10046+ di_write_lock_child(dentry);
10047+ err = au_do_fsync_dir_no_file(dentry, datasync);
10048+ }
5527c038 10049+ au_cpup_attr_timesizes(inode);
1facf9fc 10050+ di_write_unlock(dentry);
10051+ if (file)
10052+ fi_write_unlock(file);
10053+
10054+ si_read_unlock(sb);
febd17d6 10055+ inode_unlock(inode);
1facf9fc 10056+ return err;
10057+}
10058+
10059+/* ---------------------------------------------------------------------- */
10060+
5afbbe0d 10061+static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
1facf9fc 10062+{
10063+ int err;
10064+ struct dentry *dentry;
9dbd164d 10065+ struct inode *inode, *h_inode;
1facf9fc 10066+ struct super_block *sb;
10067+
523b37e3 10068+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 10069+
2000de60 10070+ dentry = file->f_path.dentry;
5527c038 10071+ inode = d_inode(dentry);
1facf9fc 10072+ IMustLock(inode);
10073+
10074+ sb = dentry->d_sb;
10075+ si_read_lock(sb, AuLock_FLUSH);
10076+ err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1);
10077+ if (unlikely(err))
10078+ goto out;
027c5e7a
AM
10079+ err = au_alive_dir(dentry);
10080+ if (!err)
10081+ err = au_vdir_init(file);
1facf9fc 10082+ di_downgrade_lock(dentry, AuLock_IR);
10083+ if (unlikely(err))
10084+ goto out_unlock;
10085+
5afbbe0d 10086+ h_inode = au_h_iptr(inode, au_ibtop(inode));
b752ccd1 10087+ if (!au_test_nfsd()) {
392086de 10088+ err = au_vdir_fill_de(file, ctx);
9dbd164d 10089+ fsstack_copy_attr_atime(inode, h_inode);
1facf9fc 10090+ } else {
10091+ /*
10092+ * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10093+ * encode_fh() and others.
10094+ */
9dbd164d 10095+ atomic_inc(&h_inode->i_count);
1facf9fc 10096+ di_read_unlock(dentry, AuLock_IR);
10097+ si_read_unlock(sb);
392086de 10098+ err = au_vdir_fill_de(file, ctx);
1facf9fc 10099+ fsstack_copy_attr_atime(inode, h_inode);
10100+ fi_write_unlock(file);
9dbd164d 10101+ iput(h_inode);
1facf9fc 10102+
10103+ AuTraceErr(err);
10104+ return err;
10105+ }
10106+
4f0767ce 10107+out_unlock:
1facf9fc 10108+ di_read_unlock(dentry, AuLock_IR);
10109+ fi_write_unlock(file);
4f0767ce 10110+out:
1facf9fc 10111+ si_read_unlock(sb);
10112+ return err;
10113+}
10114+
10115+/* ---------------------------------------------------------------------- */
10116+
10117+#define AuTestEmpty_WHONLY 1
dece6358
AM
10118+#define AuTestEmpty_CALLED (1 << 1)
10119+#define AuTestEmpty_SHWH (1 << 2)
1facf9fc 10120+#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
7f207e10
AM
10121+#define au_fset_testempty(flags, name) \
10122+ do { (flags) |= AuTestEmpty_##name; } while (0)
10123+#define au_fclr_testempty(flags, name) \
10124+ do { (flags) &= ~AuTestEmpty_##name; } while (0)
1facf9fc 10125+
dece6358
AM
10126+#ifndef CONFIG_AUFS_SHWH
10127+#undef AuTestEmpty_SHWH
10128+#define AuTestEmpty_SHWH 0
10129+#endif
10130+
1facf9fc 10131+struct test_empty_arg {
392086de 10132+ struct dir_context ctx;
1308ab2a 10133+ struct au_nhash *whlist;
1facf9fc 10134+ unsigned int flags;
10135+ int err;
10136+ aufs_bindex_t bindex;
10137+};
10138+
392086de
AM
10139+static int test_empty_cb(struct dir_context *ctx, const char *__name,
10140+ int namelen, loff_t offset __maybe_unused, u64 ino,
dece6358 10141+ unsigned int d_type)
1facf9fc 10142+{
392086de
AM
10143+ struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10144+ ctx);
1facf9fc 10145+ char *name = (void *)__name;
10146+
10147+ arg->err = 0;
10148+ au_fset_testempty(arg->flags, CALLED);
10149+ /* smp_mb(); */
10150+ if (name[0] == '.'
10151+ && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10152+ goto out; /* success */
10153+
10154+ if (namelen <= AUFS_WH_PFX_LEN
10155+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10156+ if (au_ftest_testempty(arg->flags, WHONLY)
1308ab2a 10157+ && !au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10158+ arg->err = -ENOTEMPTY;
10159+ goto out;
10160+ }
10161+
10162+ name += AUFS_WH_PFX_LEN;
10163+ namelen -= AUFS_WH_PFX_LEN;
1308ab2a 10164+ if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
1facf9fc 10165+ arg->err = au_nhash_append_wh
1308ab2a 10166+ (arg->whlist, name, namelen, ino, d_type, arg->bindex,
dece6358 10167+ au_ftest_testempty(arg->flags, SHWH));
1facf9fc 10168+
4f0767ce 10169+out:
1facf9fc 10170+ /* smp_mb(); */
10171+ AuTraceErr(arg->err);
10172+ return arg->err;
10173+}
10174+
10175+static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10176+{
10177+ int err;
10178+ struct file *h_file;
10179+
10180+ h_file = au_h_open(dentry, arg->bindex,
10181+ O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
392086de 10182+ /*file*/NULL, /*force_wr*/0);
1facf9fc 10183+ err = PTR_ERR(h_file);
10184+ if (IS_ERR(h_file))
10185+ goto out;
10186+
10187+ err = 0;
10188+ if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
c06a8ce3 10189+ && !file_inode(h_file)->i_nlink)
1facf9fc 10190+ goto out_put;
10191+
10192+ do {
10193+ arg->err = 0;
10194+ au_fclr_testempty(arg->flags, CALLED);
10195+ /* smp_mb(); */
392086de 10196+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1facf9fc 10197+ if (err >= 0)
10198+ err = arg->err;
10199+ } while (!err && au_ftest_testempty(arg->flags, CALLED));
10200+
4f0767ce 10201+out_put:
1facf9fc 10202+ fput(h_file);
10203+ au_sbr_put(dentry->d_sb, arg->bindex);
4f0767ce 10204+out:
1facf9fc 10205+ return err;
10206+}
10207+
10208+struct do_test_empty_args {
10209+ int *errp;
10210+ struct dentry *dentry;
10211+ struct test_empty_arg *arg;
10212+};
10213+
10214+static void call_do_test_empty(void *args)
10215+{
10216+ struct do_test_empty_args *a = args;
10217+ *a->errp = do_test_empty(a->dentry, a->arg);
10218+}
10219+
10220+static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10221+{
10222+ int err, wkq_err;
10223+ struct dentry *h_dentry;
10224+ struct inode *h_inode;
10225+
10226+ h_dentry = au_h_dptr(dentry, arg->bindex);
5527c038 10227+ h_inode = d_inode(h_dentry);
53392da6 10228+ /* todo: i_mode changes anytime? */
febd17d6 10229+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 10230+ err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
febd17d6 10231+ inode_unlock(h_inode);
1facf9fc 10232+ if (!err)
10233+ err = do_test_empty(dentry, arg);
10234+ else {
10235+ struct do_test_empty_args args = {
10236+ .errp = &err,
10237+ .dentry = dentry,
10238+ .arg = arg
10239+ };
10240+ unsigned int flags = arg->flags;
10241+
10242+ wkq_err = au_wkq_wait(call_do_test_empty, &args);
10243+ if (unlikely(wkq_err))
10244+ err = wkq_err;
10245+ arg->flags = flags;
10246+ }
10247+
10248+ return err;
10249+}
10250+
10251+int au_test_empty_lower(struct dentry *dentry)
10252+{
10253+ int err;
1308ab2a 10254+ unsigned int rdhash;
5afbbe0d 10255+ aufs_bindex_t bindex, btop, btail;
1308ab2a 10256+ struct au_nhash whlist;
392086de
AM
10257+ struct test_empty_arg arg = {
10258+ .ctx = {
2000de60 10259+ .actor = test_empty_cb
392086de
AM
10260+ }
10261+ };
076b876e 10262+ int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
1facf9fc 10263+
dece6358
AM
10264+ SiMustAnyLock(dentry->d_sb);
10265+
1308ab2a 10266+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10267+ if (!rdhash)
10268+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10269+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
dece6358 10270+ if (unlikely(err))
1facf9fc 10271+ goto out;
10272+
1facf9fc 10273+ arg.flags = 0;
1308ab2a 10274+ arg.whlist = &whlist;
5afbbe0d 10275+ btop = au_dbtop(dentry);
dece6358
AM
10276+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10277+ au_fset_testempty(arg.flags, SHWH);
076b876e
AM
10278+ test_empty = do_test_empty;
10279+ if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10280+ test_empty = sio_test_empty;
5afbbe0d 10281+ arg.bindex = btop;
076b876e 10282+ err = test_empty(dentry, &arg);
1facf9fc 10283+ if (unlikely(err))
10284+ goto out_whlist;
10285+
10286+ au_fset_testempty(arg.flags, WHONLY);
10287+ btail = au_dbtaildir(dentry);
5afbbe0d 10288+ for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
1facf9fc 10289+ struct dentry *h_dentry;
10290+
10291+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10292+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10293+ arg.bindex = bindex;
076b876e 10294+ err = test_empty(dentry, &arg);
1facf9fc 10295+ }
10296+ }
10297+
4f0767ce 10298+out_whlist:
1308ab2a 10299+ au_nhash_wh_free(&whlist);
4f0767ce 10300+out:
1facf9fc 10301+ return err;
10302+}
10303+
10304+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10305+{
10306+ int err;
392086de
AM
10307+ struct test_empty_arg arg = {
10308+ .ctx = {
2000de60 10309+ .actor = test_empty_cb
392086de
AM
10310+ }
10311+ };
1facf9fc 10312+ aufs_bindex_t bindex, btail;
10313+
10314+ err = 0;
1308ab2a 10315+ arg.whlist = whlist;
1facf9fc 10316+ arg.flags = AuTestEmpty_WHONLY;
dece6358
AM
10317+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10318+ au_fset_testempty(arg.flags, SHWH);
1facf9fc 10319+ btail = au_dbtaildir(dentry);
5afbbe0d 10320+ for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
1facf9fc 10321+ struct dentry *h_dentry;
10322+
10323+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 10324+ if (h_dentry && d_is_positive(h_dentry)) {
1facf9fc 10325+ arg.bindex = bindex;
10326+ err = sio_test_empty(dentry, &arg);
10327+ }
10328+ }
10329+
10330+ return err;
10331+}
10332+
10333+/* ---------------------------------------------------------------------- */
10334+
10335+const struct file_operations aufs_dir_fop = {
4a4d8108 10336+ .owner = THIS_MODULE,
027c5e7a 10337+ .llseek = default_llseek,
1facf9fc 10338+ .read = generic_read_dir,
5afbbe0d 10339+ .iterate_shared = aufs_iterate_shared,
1facf9fc 10340+ .unlocked_ioctl = aufs_ioctl_dir,
b752ccd1
AM
10341+#ifdef CONFIG_COMPAT
10342+ .compat_ioctl = aufs_compat_ioctl_dir,
10343+#endif
1facf9fc 10344+ .open = aufs_open_dir,
10345+ .release = aufs_release_dir,
4a4d8108 10346+ .flush = aufs_flush_dir,
1facf9fc 10347+ .fsync = aufs_fsync_dir
10348+};
7f207e10
AM
10349diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10350--- /usr/share/empty/fs/aufs/dir.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 10351+++ linux/fs/aufs/dir.h 2016-07-25 19:05:34.811159821 +0200
b912730e 10352@@ -0,0 +1,131 @@
1facf9fc 10353+/*
8cdd5066 10354+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 10355+ *
10356+ * This program, aufs is free software; you can redistribute it and/or modify
10357+ * it under the terms of the GNU General Public License as published by
10358+ * the Free Software Foundation; either version 2 of the License, or
10359+ * (at your option) any later version.
dece6358
AM
10360+ *
10361+ * This program is distributed in the hope that it will be useful,
10362+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10363+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10364+ * GNU General Public License for more details.
10365+ *
10366+ * You should have received a copy of the GNU General Public License
523b37e3 10367+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10368+ */
10369+
10370+/*
10371+ * directory operations
10372+ */
10373+
10374+#ifndef __AUFS_DIR_H__
10375+#define __AUFS_DIR_H__
10376+
10377+#ifdef __KERNEL__
10378+
10379+#include <linux/fs.h>
1facf9fc 10380+
10381+/* ---------------------------------------------------------------------- */
10382+
10383+/* need to be faster and smaller */
10384+
10385+struct au_nhash {
dece6358
AM
10386+ unsigned int nh_num;
10387+ struct hlist_head *nh_head;
1facf9fc 10388+};
10389+
10390+struct au_vdir_destr {
10391+ unsigned char len;
10392+ unsigned char name[0];
10393+} __packed;
10394+
10395+struct au_vdir_dehstr {
10396+ struct hlist_node hash;
10397+ struct au_vdir_destr *str;
4a4d8108 10398+} ____cacheline_aligned_in_smp;
1facf9fc 10399+
10400+struct au_vdir_de {
10401+ ino_t de_ino;
10402+ unsigned char de_type;
10403+ /* caution: packed */
10404+ struct au_vdir_destr de_str;
10405+} __packed;
10406+
10407+struct au_vdir_wh {
10408+ struct hlist_node wh_hash;
dece6358
AM
10409+#ifdef CONFIG_AUFS_SHWH
10410+ ino_t wh_ino;
1facf9fc 10411+ aufs_bindex_t wh_bindex;
dece6358
AM
10412+ unsigned char wh_type;
10413+#else
10414+ aufs_bindex_t wh_bindex;
10415+#endif
10416+ /* caution: packed */
1facf9fc 10417+ struct au_vdir_destr wh_str;
10418+} __packed;
10419+
10420+union au_vdir_deblk_p {
10421+ unsigned char *deblk;
10422+ struct au_vdir_de *de;
10423+};
10424+
10425+struct au_vdir {
10426+ unsigned char **vd_deblk;
10427+ unsigned long vd_nblk;
1facf9fc 10428+ struct {
10429+ unsigned long ul;
10430+ union au_vdir_deblk_p p;
10431+ } vd_last;
10432+
10433+ unsigned long vd_version;
dece6358 10434+ unsigned int vd_deblk_sz;
1facf9fc 10435+ unsigned long vd_jiffy;
4a4d8108 10436+} ____cacheline_aligned_in_smp;
1facf9fc 10437+
10438+/* ---------------------------------------------------------------------- */
10439+
10440+/* dir.c */
10441+extern const struct file_operations aufs_dir_fop;
10442+void au_add_nlink(struct inode *dir, struct inode *h_dir);
10443+void au_sub_nlink(struct inode *dir, struct inode *h_dir);
1308ab2a 10444+loff_t au_dir_size(struct file *file, struct dentry *dentry);
b912730e 10445+void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
1facf9fc 10446+int au_test_empty_lower(struct dentry *dentry);
10447+int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10448+
10449+/* vdir.c */
1308ab2a 10450+unsigned int au_rdhash_est(loff_t sz);
dece6358
AM
10451+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10452+void au_nhash_wh_free(struct au_nhash *whlist);
1facf9fc 10453+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10454+ int limit);
dece6358
AM
10455+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10456+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10457+ unsigned int d_type, aufs_bindex_t bindex,
10458+ unsigned char shwh);
1facf9fc 10459+void au_vdir_free(struct au_vdir *vdir);
10460+int au_vdir_init(struct file *file);
392086de 10461+int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
1facf9fc 10462+
10463+/* ioctl.c */
10464+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10465+
1308ab2a 10466+#ifdef CONFIG_AUFS_RDU
10467+/* rdu.c */
10468+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
10469+#ifdef CONFIG_COMPAT
10470+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10471+ unsigned long arg);
10472+#endif
1308ab2a 10473+#else
c1595e42
JR
10474+AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10475+ unsigned int cmd, unsigned long arg)
b752ccd1 10476+#ifdef CONFIG_COMPAT
c1595e42
JR
10477+AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10478+ unsigned int cmd, unsigned long arg)
b752ccd1 10479+#endif
1308ab2a 10480+#endif
10481+
1facf9fc 10482+#endif /* __KERNEL__ */
10483+#endif /* __AUFS_DIR_H__ */
7f207e10
AM
10484diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
10485--- /usr/share/empty/fs/aufs/dynop.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 10486+++ linux/fs/aufs/dynop.c 2016-07-25 19:05:34.811159821 +0200
7e9cd9fe 10487@@ -0,0 +1,369 @@
1facf9fc 10488+/*
8cdd5066 10489+ * Copyright (C) 2010-2016 Junjiro R. Okajima
1facf9fc 10490+ *
10491+ * This program, aufs is free software; you can redistribute it and/or modify
10492+ * it under the terms of the GNU General Public License as published by
10493+ * the Free Software Foundation; either version 2 of the License, or
10494+ * (at your option) any later version.
dece6358
AM
10495+ *
10496+ * This program is distributed in the hope that it will be useful,
10497+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10498+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10499+ * GNU General Public License for more details.
10500+ *
10501+ * You should have received a copy of the GNU General Public License
523b37e3 10502+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 10503+ */
10504+
10505+/*
4a4d8108 10506+ * dynamically customizable operations for regular files
1facf9fc 10507+ */
10508+
1facf9fc 10509+#include "aufs.h"
10510+
4a4d8108 10511+#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
1facf9fc 10512+
4a4d8108
AM
10513+/*
10514+ * How large will these lists be?
10515+ * Usually just a few elements, 20-30 at most for each, I guess.
10516+ */
10517+static struct au_splhead dynop[AuDyLast];
10518+
10519+static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
1facf9fc 10520+{
4a4d8108
AM
10521+ struct au_dykey *key, *tmp;
10522+ struct list_head *head;
1facf9fc 10523+
4a4d8108
AM
10524+ key = NULL;
10525+ head = &spl->head;
10526+ rcu_read_lock();
10527+ list_for_each_entry_rcu(tmp, head, dk_list)
10528+ if (tmp->dk_op.dy_hop == h_op) {
10529+ key = tmp;
10530+ kref_get(&key->dk_kref);
10531+ break;
10532+ }
10533+ rcu_read_unlock();
10534+
10535+ return key;
1facf9fc 10536+}
10537+
4a4d8108 10538+static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
1facf9fc 10539+{
4a4d8108
AM
10540+ struct au_dykey **k, *found;
10541+ const void *h_op = key->dk_op.dy_hop;
10542+ int i;
1facf9fc 10543+
4a4d8108
AM
10544+ found = NULL;
10545+ k = br->br_dykey;
10546+ for (i = 0; i < AuBrDynOp; i++)
10547+ if (k[i]) {
10548+ if (k[i]->dk_op.dy_hop == h_op) {
10549+ found = k[i];
10550+ break;
10551+ }
10552+ } else
10553+ break;
10554+ if (!found) {
10555+ spin_lock(&br->br_dykey_lock);
10556+ for (; i < AuBrDynOp; i++)
10557+ if (k[i]) {
10558+ if (k[i]->dk_op.dy_hop == h_op) {
10559+ found = k[i];
10560+ break;
10561+ }
10562+ } else {
10563+ k[i] = key;
10564+ break;
10565+ }
10566+ spin_unlock(&br->br_dykey_lock);
10567+ BUG_ON(i == AuBrDynOp); /* expand the array */
10568+ }
10569+
10570+ return found;
1facf9fc 10571+}
10572+
4a4d8108
AM
10573+/* kref_get() if @key is already added */
10574+static struct au_dykey *dy_gadd(struct au_splhead *spl, struct au_dykey *key)
10575+{
10576+ struct au_dykey *tmp, *found;
10577+ struct list_head *head;
10578+ const void *h_op = key->dk_op.dy_hop;
1facf9fc 10579+
4a4d8108
AM
10580+ found = NULL;
10581+ head = &spl->head;
10582+ spin_lock(&spl->spin);
10583+ list_for_each_entry(tmp, head, dk_list)
10584+ if (tmp->dk_op.dy_hop == h_op) {
10585+ kref_get(&tmp->dk_kref);
10586+ found = tmp;
10587+ break;
10588+ }
10589+ if (!found)
10590+ list_add_rcu(&key->dk_list, head);
10591+ spin_unlock(&spl->spin);
1facf9fc 10592+
4a4d8108
AM
10593+ if (!found)
10594+ DyPrSym(key);
10595+ return found;
10596+}
10597+
10598+static void dy_free_rcu(struct rcu_head *rcu)
1facf9fc 10599+{
4a4d8108
AM
10600+ struct au_dykey *key;
10601+
10602+ key = container_of(rcu, struct au_dykey, dk_rcu);
10603+ DyPrSym(key);
10604+ kfree(key);
1facf9fc 10605+}
10606+
4a4d8108
AM
10607+static void dy_free(struct kref *kref)
10608+{
10609+ struct au_dykey *key;
10610+ struct au_splhead *spl;
1facf9fc 10611+
4a4d8108
AM
10612+ key = container_of(kref, struct au_dykey, dk_kref);
10613+ spl = dynop + key->dk_op.dy_type;
10614+ au_spl_del_rcu(&key->dk_list, spl);
10615+ call_rcu(&key->dk_rcu, dy_free_rcu);
10616+}
10617+
10618+void au_dy_put(struct au_dykey *key)
1facf9fc 10619+{
4a4d8108
AM
10620+ kref_put(&key->dk_kref, dy_free);
10621+}
1facf9fc 10622+
4a4d8108
AM
10623+/* ---------------------------------------------------------------------- */
10624+
10625+#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
10626+
10627+#ifdef CONFIG_AUFS_DEBUG
10628+#define DyDbgDeclare(cnt) unsigned int cnt = 0
4f0767ce 10629+#define DyDbgInc(cnt) do { cnt++; } while (0)
4a4d8108
AM
10630+#else
10631+#define DyDbgDeclare(cnt) do {} while (0)
10632+#define DyDbgInc(cnt) do {} while (0)
10633+#endif
10634+
10635+#define DySet(func, dst, src, h_op, h_sb) do { \
10636+ DyDbgInc(cnt); \
10637+ if (h_op->func) { \
10638+ if (src.func) \
10639+ dst.func = src.func; \
10640+ else \
10641+ AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
10642+ } \
10643+} while (0)
10644+
10645+#define DySetForce(func, dst, src) do { \
10646+ AuDebugOn(!src.func); \
10647+ DyDbgInc(cnt); \
10648+ dst.func = src.func; \
10649+} while (0)
10650+
10651+#define DySetAop(func) \
10652+ DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
10653+#define DySetAopForce(func) \
10654+ DySetForce(func, dyaop->da_op, aufs_aop)
10655+
10656+static void dy_aop(struct au_dykey *key, const void *h_op,
10657+ struct super_block *h_sb __maybe_unused)
10658+{
10659+ struct au_dyaop *dyaop = (void *)key;
10660+ const struct address_space_operations *h_aop = h_op;
10661+ DyDbgDeclare(cnt);
10662+
10663+ AuDbg("%s\n", au_sbtype(h_sb));
10664+
10665+ DySetAop(writepage);
10666+ DySetAopForce(readpage); /* force */
4a4d8108
AM
10667+ DySetAop(writepages);
10668+ DySetAop(set_page_dirty);
10669+ DySetAop(readpages);
10670+ DySetAop(write_begin);
10671+ DySetAop(write_end);
10672+ DySetAop(bmap);
10673+ DySetAop(invalidatepage);
10674+ DySetAop(releasepage);
027c5e7a 10675+ DySetAop(freepage);
7e9cd9fe 10676+ /* this one will be changed according to an aufs mount option */
4a4d8108 10677+ DySetAop(direct_IO);
4a4d8108
AM
10678+ DySetAop(migratepage);
10679+ DySetAop(launder_page);
10680+ DySetAop(is_partially_uptodate);
392086de 10681+ DySetAop(is_dirty_writeback);
4a4d8108 10682+ DySetAop(error_remove_page);
b4510431
AM
10683+ DySetAop(swap_activate);
10684+ DySetAop(swap_deactivate);
4a4d8108
AM
10685+
10686+ DyDbgSize(cnt, *h_aop);
4a4d8108
AM
10687+}
10688+
4a4d8108
AM
10689+/* ---------------------------------------------------------------------- */
10690+
10691+static void dy_bug(struct kref *kref)
10692+{
10693+ BUG();
10694+}
10695+
10696+static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
10697+{
10698+ struct au_dykey *key, *old;
10699+ struct au_splhead *spl;
b752ccd1 10700+ struct op {
4a4d8108 10701+ unsigned int sz;
b752ccd1
AM
10702+ void (*set)(struct au_dykey *key, const void *h_op,
10703+ struct super_block *h_sb __maybe_unused);
10704+ };
10705+ static const struct op a[] = {
4a4d8108
AM
10706+ [AuDy_AOP] = {
10707+ .sz = sizeof(struct au_dyaop),
b752ccd1 10708+ .set = dy_aop
4a4d8108 10709+ }
b752ccd1
AM
10710+ };
10711+ const struct op *p;
4a4d8108
AM
10712+
10713+ spl = dynop + op->dy_type;
10714+ key = dy_gfind_get(spl, op->dy_hop);
10715+ if (key)
10716+ goto out_add; /* success */
10717+
10718+ p = a + op->dy_type;
10719+ key = kzalloc(p->sz, GFP_NOFS);
10720+ if (unlikely(!key)) {
10721+ key = ERR_PTR(-ENOMEM);
10722+ goto out;
10723+ }
10724+
10725+ key->dk_op.dy_hop = op->dy_hop;
10726+ kref_init(&key->dk_kref);
86dc4139 10727+ p->set(key, op->dy_hop, au_br_sb(br));
4a4d8108
AM
10728+ old = dy_gadd(spl, key);
10729+ if (old) {
10730+ kfree(key);
10731+ key = old;
10732+ }
10733+
10734+out_add:
10735+ old = dy_bradd(br, key);
10736+ if (old)
10737+ /* its ref-count should never be zero here */
10738+ kref_put(&key->dk_kref, dy_bug);
10739+out:
10740+ return key;
10741+}
10742+
10743+/* ---------------------------------------------------------------------- */
10744+/*
10745+ * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
c1595e42 10746+ * This behaviour is necessary to return an error from open(O_DIRECT) instead
4a4d8108
AM
10747+ * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
10748+ * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
10749+ * See the aufs manual in detail.
4a4d8108
AM
10750+ */
10751+static void dy_adx(struct au_dyaop *dyaop, int do_dx)
10752+{
7e9cd9fe 10753+ if (!do_dx)
4a4d8108 10754+ dyaop->da_op.direct_IO = NULL;
7e9cd9fe 10755+ else
4a4d8108 10756+ dyaop->da_op.direct_IO = aufs_aop.direct_IO;
4a4d8108
AM
10757+}
10758+
10759+static struct au_dyaop *dy_aget(struct au_branch *br,
10760+ const struct address_space_operations *h_aop,
10761+ int do_dx)
10762+{
10763+ struct au_dyaop *dyaop;
10764+ struct au_dynop op;
10765+
10766+ op.dy_type = AuDy_AOP;
10767+ op.dy_haop = h_aop;
10768+ dyaop = (void *)dy_get(&op, br);
10769+ if (IS_ERR(dyaop))
10770+ goto out;
10771+ dy_adx(dyaop, do_dx);
10772+
10773+out:
10774+ return dyaop;
10775+}
10776+
10777+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10778+ struct inode *h_inode)
10779+{
10780+ int err, do_dx;
10781+ struct super_block *sb;
10782+ struct au_branch *br;
10783+ struct au_dyaop *dyaop;
10784+
10785+ AuDebugOn(!S_ISREG(h_inode->i_mode));
10786+ IiMustWriteLock(inode);
10787+
10788+ sb = inode->i_sb;
10789+ br = au_sbr(sb, bindex);
10790+ do_dx = !!au_opt_test(au_mntflags(sb), DIO);
10791+ dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
10792+ err = PTR_ERR(dyaop);
10793+ if (IS_ERR(dyaop))
10794+ /* unnecessary to call dy_fput() */
10795+ goto out;
10796+
10797+ err = 0;
10798+ inode->i_mapping->a_ops = &dyaop->da_op;
10799+
10800+out:
10801+ return err;
10802+}
10803+
b752ccd1
AM
10804+/*
10805+ * Is it safe to replace a_ops during the inode/file is in operation?
10806+ * Yes, I hope so.
10807+ */
10808+int au_dy_irefresh(struct inode *inode)
10809+{
10810+ int err;
5afbbe0d 10811+ aufs_bindex_t btop;
b752ccd1
AM
10812+ struct inode *h_inode;
10813+
10814+ err = 0;
10815+ if (S_ISREG(inode->i_mode)) {
5afbbe0d
AM
10816+ btop = au_ibtop(inode);
10817+ h_inode = au_h_iptr(inode, btop);
10818+ err = au_dy_iaop(inode, btop, h_inode);
b752ccd1
AM
10819+ }
10820+ return err;
10821+}
10822+
4a4d8108
AM
10823+void au_dy_arefresh(int do_dx)
10824+{
10825+ struct au_splhead *spl;
10826+ struct list_head *head;
10827+ struct au_dykey *key;
10828+
10829+ spl = dynop + AuDy_AOP;
10830+ head = &spl->head;
10831+ spin_lock(&spl->spin);
10832+ list_for_each_entry(key, head, dk_list)
10833+ dy_adx((void *)key, do_dx);
10834+ spin_unlock(&spl->spin);
10835+}
10836+
4a4d8108
AM
10837+/* ---------------------------------------------------------------------- */
10838+
10839+void __init au_dy_init(void)
10840+{
10841+ int i;
10842+
10843+ /* make sure that 'struct au_dykey *' can be any type */
10844+ BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
4a4d8108
AM
10845+
10846+ for (i = 0; i < AuDyLast; i++)
10847+ au_spl_init(dynop + i);
10848+}
10849+
10850+void au_dy_fin(void)
10851+{
10852+ int i;
10853+
10854+ for (i = 0; i < AuDyLast; i++)
10855+ WARN_ON(!list_empty(&dynop[i].head));
10856+}
7f207e10
AM
10857diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
10858--- /usr/share/empty/fs/aufs/dynop.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 10859+++ linux/fs/aufs/dynop.h 2016-07-25 19:05:34.811159821 +0200
7e9cd9fe 10860@@ -0,0 +1,74 @@
4a4d8108 10861+/*
8cdd5066 10862+ * Copyright (C) 2010-2016 Junjiro R. Okajima
4a4d8108
AM
10863+ *
10864+ * This program, aufs is free software; you can redistribute it and/or modify
10865+ * it under the terms of the GNU General Public License as published by
10866+ * the Free Software Foundation; either version 2 of the License, or
10867+ * (at your option) any later version.
10868+ *
10869+ * This program is distributed in the hope that it will be useful,
10870+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10871+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10872+ * GNU General Public License for more details.
10873+ *
10874+ * You should have received a copy of the GNU General Public License
523b37e3 10875+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
10876+ */
10877+
10878+/*
10879+ * dynamically customizable operations (for regular files only)
10880+ */
10881+
10882+#ifndef __AUFS_DYNOP_H__
10883+#define __AUFS_DYNOP_H__
10884+
10885+#ifdef __KERNEL__
10886+
7e9cd9fe
AM
10887+#include <linux/fs.h>
10888+#include <linux/kref.h>
4a4d8108 10889+
2cbb1c4b 10890+enum {AuDy_AOP, AuDyLast};
4a4d8108
AM
10891+
10892+struct au_dynop {
10893+ int dy_type;
10894+ union {
10895+ const void *dy_hop;
10896+ const struct address_space_operations *dy_haop;
4a4d8108
AM
10897+ };
10898+};
10899+
10900+struct au_dykey {
10901+ union {
10902+ struct list_head dk_list;
10903+ struct rcu_head dk_rcu;
10904+ };
10905+ struct au_dynop dk_op;
10906+
10907+ /*
10908+ * during I am in the branch local array, kref is gotten. when the
10909+ * branch is removed, kref is put.
10910+ */
10911+ struct kref dk_kref;
10912+};
10913+
10914+/* stop unioning since their sizes are very different from each other */
10915+struct au_dyaop {
10916+ struct au_dykey da_key;
10917+ struct address_space_operations da_op; /* not const */
4a4d8108
AM
10918+};
10919+
4a4d8108
AM
10920+/* ---------------------------------------------------------------------- */
10921+
10922+/* dynop.c */
10923+struct au_branch;
10924+void au_dy_put(struct au_dykey *key);
10925+int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
10926+ struct inode *h_inode);
b752ccd1 10927+int au_dy_irefresh(struct inode *inode);
4a4d8108 10928+void au_dy_arefresh(int do_dio);
4a4d8108
AM
10929+
10930+void __init au_dy_init(void);
10931+void au_dy_fin(void);
10932+
4a4d8108
AM
10933+#endif /* __KERNEL__ */
10934+#endif /* __AUFS_DYNOP_H__ */
7f207e10
AM
10935diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
10936--- /usr/share/empty/fs/aufs/export.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
10937+++ linux/fs/aufs/export.c 2016-07-25 19:05:34.811159821 +0200
10938@@ -0,0 +1,837 @@
4a4d8108 10939+/*
8cdd5066 10940+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
10941+ *
10942+ * This program, aufs is free software; you can redistribute it and/or modify
10943+ * it under the terms of the GNU General Public License as published by
10944+ * the Free Software Foundation; either version 2 of the License, or
10945+ * (at your option) any later version.
10946+ *
10947+ * This program is distributed in the hope that it will be useful,
10948+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10949+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10950+ * GNU General Public License for more details.
10951+ *
10952+ * You should have received a copy of the GNU General Public License
523b37e3 10953+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
10954+ */
10955+
10956+/*
10957+ * export via nfs
10958+ */
10959+
10960+#include <linux/exportfs.h>
7eafdf33 10961+#include <linux/fs_struct.h>
4a4d8108
AM
10962+#include <linux/namei.h>
10963+#include <linux/nsproxy.h>
10964+#include <linux/random.h>
10965+#include <linux/writeback.h>
7eafdf33 10966+#include "../fs/mount.h"
4a4d8108
AM
10967+#include "aufs.h"
10968+
10969+union conv {
10970+#ifdef CONFIG_AUFS_INO_T_64
10971+ __u32 a[2];
10972+#else
10973+ __u32 a[1];
10974+#endif
10975+ ino_t ino;
10976+};
10977+
10978+static ino_t decode_ino(__u32 *a)
10979+{
10980+ union conv u;
10981+
10982+ BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
10983+ u.a[0] = a[0];
10984+#ifdef CONFIG_AUFS_INO_T_64
10985+ u.a[1] = a[1];
10986+#endif
10987+ return u.ino;
10988+}
10989+
10990+static void encode_ino(__u32 *a, ino_t ino)
10991+{
10992+ union conv u;
10993+
10994+ u.ino = ino;
10995+ a[0] = u.a[0];
10996+#ifdef CONFIG_AUFS_INO_T_64
10997+ a[1] = u.a[1];
10998+#endif
10999+}
11000+
11001+/* NFS file handle */
11002+enum {
11003+ Fh_br_id,
11004+ Fh_sigen,
11005+#ifdef CONFIG_AUFS_INO_T_64
11006+ /* support 64bit inode number */
11007+ Fh_ino1,
11008+ Fh_ino2,
11009+ Fh_dir_ino1,
11010+ Fh_dir_ino2,
11011+#else
11012+ Fh_ino1,
11013+ Fh_dir_ino1,
11014+#endif
11015+ Fh_igen,
11016+ Fh_h_type,
11017+ Fh_tail,
11018+
11019+ Fh_ino = Fh_ino1,
11020+ Fh_dir_ino = Fh_dir_ino1
11021+};
11022+
11023+static int au_test_anon(struct dentry *dentry)
11024+{
027c5e7a 11025+ /* note: read d_flags without d_lock */
4a4d8108
AM
11026+ return !!(dentry->d_flags & DCACHE_DISCONNECTED);
11027+}
11028+
a2a7ad62
AM
11029+int au_test_nfsd(void)
11030+{
11031+ int ret;
11032+ struct task_struct *tsk = current;
11033+ char comm[sizeof(tsk->comm)];
11034+
11035+ ret = 0;
11036+ if (tsk->flags & PF_KTHREAD) {
11037+ get_task_comm(comm, tsk);
11038+ ret = !strcmp(comm, "nfsd");
11039+ }
11040+
11041+ return ret;
11042+}
11043+
4a4d8108
AM
11044+/* ---------------------------------------------------------------------- */
11045+/* inode generation external table */
11046+
b752ccd1 11047+void au_xigen_inc(struct inode *inode)
4a4d8108 11048+{
4a4d8108
AM
11049+ loff_t pos;
11050+ ssize_t sz;
11051+ __u32 igen;
11052+ struct super_block *sb;
11053+ struct au_sbinfo *sbinfo;
11054+
4a4d8108 11055+ sb = inode->i_sb;
b752ccd1 11056+ AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
1facf9fc 11057+
b752ccd1 11058+ sbinfo = au_sbi(sb);
1facf9fc 11059+ pos = inode->i_ino;
11060+ pos *= sizeof(igen);
11061+ igen = inode->i_generation + 1;
1facf9fc 11062+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
11063+ sizeof(igen), &pos);
11064+ if (sz == sizeof(igen))
b752ccd1 11065+ return; /* success */
1facf9fc 11066+
b752ccd1 11067+ if (unlikely(sz >= 0))
1facf9fc 11068+ AuIOErr("xigen error (%zd)\n", sz);
1facf9fc 11069+}
11070+
11071+int au_xigen_new(struct inode *inode)
11072+{
11073+ int err;
11074+ loff_t pos;
11075+ ssize_t sz;
11076+ struct super_block *sb;
11077+ struct au_sbinfo *sbinfo;
11078+ struct file *file;
11079+
11080+ err = 0;
11081+ /* todo: dirty, at mount time */
11082+ if (inode->i_ino == AUFS_ROOT_INO)
11083+ goto out;
11084+ sb = inode->i_sb;
dece6358 11085+ SiMustAnyLock(sb);
1facf9fc 11086+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11087+ goto out;
11088+
11089+ err = -EFBIG;
11090+ pos = inode->i_ino;
11091+ if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
11092+ AuIOErr1("too large i%lld\n", pos);
11093+ goto out;
11094+ }
11095+ pos *= sizeof(inode->i_generation);
11096+
11097+ err = 0;
11098+ sbinfo = au_sbi(sb);
11099+ file = sbinfo->si_xigen;
11100+ BUG_ON(!file);
11101+
c06a8ce3 11102+ if (vfsub_f_size_read(file)
1facf9fc 11103+ < pos + sizeof(inode->i_generation)) {
11104+ inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
11105+ sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
11106+ sizeof(inode->i_generation), &pos);
11107+ } else
11108+ sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
11109+ sizeof(inode->i_generation), &pos);
11110+ if (sz == sizeof(inode->i_generation))
11111+ goto out; /* success */
11112+
11113+ err = sz;
11114+ if (unlikely(sz >= 0)) {
11115+ err = -EIO;
11116+ AuIOErr("xigen error (%zd)\n", sz);
11117+ }
11118+
4f0767ce 11119+out:
1facf9fc 11120+ return err;
11121+}
11122+
11123+int au_xigen_set(struct super_block *sb, struct file *base)
11124+{
11125+ int err;
11126+ struct au_sbinfo *sbinfo;
11127+ struct file *file;
11128+
dece6358
AM
11129+ SiMustWriteLock(sb);
11130+
1facf9fc 11131+ sbinfo = au_sbi(sb);
11132+ file = au_xino_create2(base, sbinfo->si_xigen);
11133+ err = PTR_ERR(file);
11134+ if (IS_ERR(file))
11135+ goto out;
11136+ err = 0;
11137+ if (sbinfo->si_xigen)
11138+ fput(sbinfo->si_xigen);
11139+ sbinfo->si_xigen = file;
11140+
4f0767ce 11141+out:
1facf9fc 11142+ return err;
11143+}
11144+
11145+void au_xigen_clr(struct super_block *sb)
11146+{
11147+ struct au_sbinfo *sbinfo;
11148+
dece6358
AM
11149+ SiMustWriteLock(sb);
11150+
1facf9fc 11151+ sbinfo = au_sbi(sb);
11152+ if (sbinfo->si_xigen) {
11153+ fput(sbinfo->si_xigen);
11154+ sbinfo->si_xigen = NULL;
11155+ }
11156+}
11157+
11158+/* ---------------------------------------------------------------------- */
11159+
11160+static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
11161+ ino_t dir_ino)
11162+{
11163+ struct dentry *dentry, *d;
11164+ struct inode *inode;
11165+ unsigned int sigen;
11166+
11167+ dentry = NULL;
11168+ inode = ilookup(sb, ino);
11169+ if (!inode)
11170+ goto out;
11171+
11172+ dentry = ERR_PTR(-ESTALE);
11173+ sigen = au_sigen(sb);
5afbbe0d 11174+ if (unlikely(au_is_bad_inode(inode)
1facf9fc 11175+ || IS_DEADDIR(inode)
537831f9 11176+ || sigen != au_iigen(inode, NULL)))
1facf9fc 11177+ goto out_iput;
11178+
11179+ dentry = NULL;
11180+ if (!dir_ino || S_ISDIR(inode->i_mode))
11181+ dentry = d_find_alias(inode);
11182+ else {
027c5e7a 11183+ spin_lock(&inode->i_lock);
c1595e42 11184+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 11185+ spin_lock(&d->d_lock);
1facf9fc 11186+ if (!au_test_anon(d)
5527c038 11187+ && d_inode(d->d_parent)->i_ino == dir_ino) {
027c5e7a
AM
11188+ dentry = dget_dlock(d);
11189+ spin_unlock(&d->d_lock);
1facf9fc 11190+ break;
11191+ }
027c5e7a
AM
11192+ spin_unlock(&d->d_lock);
11193+ }
11194+ spin_unlock(&inode->i_lock);
1facf9fc 11195+ }
027c5e7a 11196+ if (unlikely(dentry && au_digen_test(dentry, sigen))) {
2cbb1c4b 11197+ /* need to refresh */
1facf9fc 11198+ dput(dentry);
2cbb1c4b 11199+ dentry = NULL;
1facf9fc 11200+ }
11201+
4f0767ce 11202+out_iput:
1facf9fc 11203+ iput(inode);
4f0767ce 11204+out:
2cbb1c4b 11205+ AuTraceErrPtr(dentry);
1facf9fc 11206+ return dentry;
11207+}
11208+
11209+/* ---------------------------------------------------------------------- */
11210+
11211+/* todo: dirty? */
11212+/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
4a4d8108
AM
11213+
11214+struct au_compare_mnt_args {
11215+ /* input */
11216+ struct super_block *sb;
11217+
11218+ /* output */
11219+ struct vfsmount *mnt;
11220+};
11221+
11222+static int au_compare_mnt(struct vfsmount *mnt, void *arg)
11223+{
11224+ struct au_compare_mnt_args *a = arg;
11225+
11226+ if (mnt->mnt_sb != a->sb)
11227+ return 0;
11228+ a->mnt = mntget(mnt);
11229+ return 1;
11230+}
11231+
1facf9fc 11232+static struct vfsmount *au_mnt_get(struct super_block *sb)
11233+{
4a4d8108 11234+ int err;
7eafdf33 11235+ struct path root;
4a4d8108
AM
11236+ struct au_compare_mnt_args args = {
11237+ .sb = sb
11238+ };
1facf9fc 11239+
7eafdf33 11240+ get_fs_root(current->fs, &root);
523b37e3 11241+ rcu_read_lock();
7eafdf33 11242+ err = iterate_mounts(au_compare_mnt, &args, root.mnt);
523b37e3 11243+ rcu_read_unlock();
7eafdf33 11244+ path_put(&root);
4a4d8108
AM
11245+ AuDebugOn(!err);
11246+ AuDebugOn(!args.mnt);
11247+ return args.mnt;
1facf9fc 11248+}
11249+
11250+struct au_nfsd_si_lock {
4a4d8108 11251+ unsigned int sigen;
027c5e7a 11252+ aufs_bindex_t bindex, br_id;
1facf9fc 11253+ unsigned char force_lock;
11254+};
11255+
027c5e7a
AM
11256+static int si_nfsd_read_lock(struct super_block *sb,
11257+ struct au_nfsd_si_lock *nsi_lock)
1facf9fc 11258+{
027c5e7a 11259+ int err;
1facf9fc 11260+ aufs_bindex_t bindex;
11261+
11262+ si_read_lock(sb, AuLock_FLUSH);
11263+
11264+ /* branch id may be wrapped around */
027c5e7a 11265+ err = 0;
1facf9fc 11266+ bindex = au_br_index(sb, nsi_lock->br_id);
11267+ if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
11268+ goto out; /* success */
11269+
027c5e7a
AM
11270+ err = -ESTALE;
11271+ bindex = -1;
1facf9fc 11272+ if (!nsi_lock->force_lock)
11273+ si_read_unlock(sb);
1facf9fc 11274+
4f0767ce 11275+out:
027c5e7a
AM
11276+ nsi_lock->bindex = bindex;
11277+ return err;
1facf9fc 11278+}
11279+
11280+struct find_name_by_ino {
392086de 11281+ struct dir_context ctx;
1facf9fc 11282+ int called, found;
11283+ ino_t ino;
11284+ char *name;
11285+ int namelen;
11286+};
11287+
11288+static int
392086de
AM
11289+find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
11290+ loff_t offset, u64 ino, unsigned int d_type)
1facf9fc 11291+{
392086de
AM
11292+ struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
11293+ ctx);
1facf9fc 11294+
11295+ a->called++;
11296+ if (a->ino != ino)
11297+ return 0;
11298+
11299+ memcpy(a->name, name, namelen);
11300+ a->namelen = namelen;
11301+ a->found = 1;
11302+ return 1;
11303+}
11304+
11305+static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
11306+ struct au_nfsd_si_lock *nsi_lock)
11307+{
11308+ struct dentry *dentry, *parent;
11309+ struct file *file;
11310+ struct inode *dir;
392086de
AM
11311+ struct find_name_by_ino arg = {
11312+ .ctx = {
2000de60 11313+ .actor = find_name_by_ino
392086de
AM
11314+ }
11315+ };
1facf9fc 11316+ int err;
11317+
11318+ parent = path->dentry;
11319+ if (nsi_lock)
11320+ si_read_unlock(parent->d_sb);
4a4d8108 11321+ file = vfsub_dentry_open(path, au_dir_roflags);
1facf9fc 11322+ dentry = (void *)file;
11323+ if (IS_ERR(file))
11324+ goto out;
11325+
11326+ dentry = ERR_PTR(-ENOMEM);
537831f9 11327+ arg.name = (void *)__get_free_page(GFP_NOFS);
1facf9fc 11328+ if (unlikely(!arg.name))
11329+ goto out_file;
11330+ arg.ino = ino;
11331+ arg.found = 0;
11332+ do {
11333+ arg.called = 0;
11334+ /* smp_mb(); */
392086de 11335+ err = vfsub_iterate_dir(file, &arg.ctx);
1facf9fc 11336+ } while (!err && !arg.found && arg.called);
11337+ dentry = ERR_PTR(err);
11338+ if (unlikely(err))
11339+ goto out_name;
1716fcea
AM
11340+ /* instead of ENOENT */
11341+ dentry = ERR_PTR(-ESTALE);
1facf9fc 11342+ if (!arg.found)
11343+ goto out_name;
11344+
b4510431 11345+ /* do not call vfsub_lkup_one() */
5527c038 11346+ dir = d_inode(parent);
febd17d6 11347+ dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
1facf9fc 11348+ AuTraceErrPtr(dentry);
11349+ if (IS_ERR(dentry))
11350+ goto out_name;
11351+ AuDebugOn(au_test_anon(dentry));
5527c038 11352+ if (unlikely(d_really_is_negative(dentry))) {
1facf9fc 11353+ dput(dentry);
11354+ dentry = ERR_PTR(-ENOENT);
11355+ }
11356+
4f0767ce 11357+out_name:
537831f9 11358+ free_page((unsigned long)arg.name);
4f0767ce 11359+out_file:
1facf9fc 11360+ fput(file);
4f0767ce 11361+out:
1facf9fc 11362+ if (unlikely(nsi_lock
11363+ && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
11364+ if (!IS_ERR(dentry)) {
11365+ dput(dentry);
11366+ dentry = ERR_PTR(-ESTALE);
11367+ }
11368+ AuTraceErrPtr(dentry);
11369+ return dentry;
11370+}
11371+
11372+static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
11373+ ino_t dir_ino,
11374+ struct au_nfsd_si_lock *nsi_lock)
11375+{
11376+ struct dentry *dentry;
11377+ struct path path;
11378+
11379+ if (dir_ino != AUFS_ROOT_INO) {
11380+ path.dentry = decode_by_ino(sb, dir_ino, 0);
11381+ dentry = path.dentry;
11382+ if (!path.dentry || IS_ERR(path.dentry))
11383+ goto out;
11384+ AuDebugOn(au_test_anon(path.dentry));
11385+ } else
11386+ path.dentry = dget(sb->s_root);
11387+
11388+ path.mnt = au_mnt_get(sb);
11389+ dentry = au_lkup_by_ino(&path, ino, nsi_lock);
11390+ path_put(&path);
11391+
4f0767ce 11392+out:
1facf9fc 11393+ AuTraceErrPtr(dentry);
11394+ return dentry;
11395+}
11396+
11397+/* ---------------------------------------------------------------------- */
11398+
11399+static int h_acceptable(void *expv, struct dentry *dentry)
11400+{
11401+ return 1;
11402+}
11403+
11404+static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
11405+ char *buf, int len, struct super_block *sb)
11406+{
11407+ char *p;
11408+ int n;
11409+ struct path path;
11410+
11411+ p = d_path(h_rootpath, buf, len);
11412+ if (IS_ERR(p))
11413+ goto out;
11414+ n = strlen(p);
11415+
11416+ path.mnt = h_rootpath->mnt;
11417+ path.dentry = h_parent;
11418+ p = d_path(&path, buf, len);
11419+ if (IS_ERR(p))
11420+ goto out;
11421+ if (n != 1)
11422+ p += n;
11423+
11424+ path.mnt = au_mnt_get(sb);
11425+ path.dentry = sb->s_root;
11426+ p = d_path(&path, buf, len - strlen(p));
11427+ mntput(path.mnt);
11428+ if (IS_ERR(p))
11429+ goto out;
11430+ if (n != 1)
11431+ p[strlen(p)] = '/';
11432+
4f0767ce 11433+out:
1facf9fc 11434+ AuTraceErrPtr(p);
11435+ return p;
11436+}
11437+
11438+static
027c5e7a
AM
11439+struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
11440+ int fh_len, struct au_nfsd_si_lock *nsi_lock)
1facf9fc 11441+{
11442+ struct dentry *dentry, *h_parent, *root;
11443+ struct super_block *h_sb;
11444+ char *pathname, *p;
11445+ struct vfsmount *h_mnt;
11446+ struct au_branch *br;
11447+ int err;
11448+ struct path path;
11449+
027c5e7a 11450+ br = au_sbr(sb, nsi_lock->bindex);
86dc4139 11451+ h_mnt = au_br_mnt(br);
1facf9fc 11452+ h_sb = h_mnt->mnt_sb;
11453+ /* todo: call lower fh_to_dentry()? fh_to_parent()? */
5afbbe0d 11454+ lockdep_off();
1facf9fc 11455+ h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
11456+ fh_len - Fh_tail, fh[Fh_h_type],
11457+ h_acceptable, /*context*/NULL);
5afbbe0d 11458+ lockdep_on();
1facf9fc 11459+ dentry = h_parent;
11460+ if (unlikely(!h_parent || IS_ERR(h_parent))) {
11461+ AuWarn1("%s decode_fh failed, %ld\n",
11462+ au_sbtype(h_sb), PTR_ERR(h_parent));
11463+ goto out;
11464+ }
11465+ dentry = NULL;
11466+ if (unlikely(au_test_anon(h_parent))) {
11467+ AuWarn1("%s decode_fh returned a disconnected dentry\n",
11468+ au_sbtype(h_sb));
11469+ goto out_h_parent;
11470+ }
11471+
11472+ dentry = ERR_PTR(-ENOMEM);
11473+ pathname = (void *)__get_free_page(GFP_NOFS);
11474+ if (unlikely(!pathname))
11475+ goto out_h_parent;
11476+
11477+ root = sb->s_root;
11478+ path.mnt = h_mnt;
11479+ di_read_lock_parent(root, !AuLock_IR);
027c5e7a 11480+ path.dentry = au_h_dptr(root, nsi_lock->bindex);
1facf9fc 11481+ di_read_unlock(root, !AuLock_IR);
11482+ p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
11483+ dentry = (void *)p;
11484+ if (IS_ERR(p))
11485+ goto out_pathname;
11486+
11487+ si_read_unlock(sb);
11488+ err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
11489+ dentry = ERR_PTR(err);
11490+ if (unlikely(err))
11491+ goto out_relock;
11492+
11493+ dentry = ERR_PTR(-ENOENT);
11494+ AuDebugOn(au_test_anon(path.dentry));
5527c038 11495+ if (unlikely(d_really_is_negative(path.dentry)))
1facf9fc 11496+ goto out_path;
11497+
5527c038 11498+ if (ino != d_inode(path.dentry)->i_ino)
1facf9fc 11499+ dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
11500+ else
11501+ dentry = dget(path.dentry);
11502+
4f0767ce 11503+out_path:
1facf9fc 11504+ path_put(&path);
4f0767ce 11505+out_relock:
1facf9fc 11506+ if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
11507+ if (!IS_ERR(dentry)) {
11508+ dput(dentry);
11509+ dentry = ERR_PTR(-ESTALE);
11510+ }
4f0767ce 11511+out_pathname:
1facf9fc 11512+ free_page((unsigned long)pathname);
4f0767ce 11513+out_h_parent:
1facf9fc 11514+ dput(h_parent);
4f0767ce 11515+out:
1facf9fc 11516+ AuTraceErrPtr(dentry);
11517+ return dentry;
11518+}
11519+
11520+/* ---------------------------------------------------------------------- */
11521+
11522+static struct dentry *
11523+aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
11524+ int fh_type)
11525+{
11526+ struct dentry *dentry;
11527+ __u32 *fh = fid->raw;
027c5e7a 11528+ struct au_branch *br;
1facf9fc 11529+ ino_t ino, dir_ino;
1facf9fc 11530+ struct au_nfsd_si_lock nsi_lock = {
1facf9fc 11531+ .force_lock = 0
11532+ };
11533+
1facf9fc 11534+ dentry = ERR_PTR(-ESTALE);
4a4d8108
AM
11535+ /* it should never happen, but the file handle is unreliable */
11536+ if (unlikely(fh_len < Fh_tail))
11537+ goto out;
11538+ nsi_lock.sigen = fh[Fh_sigen];
11539+ nsi_lock.br_id = fh[Fh_br_id];
11540+
1facf9fc 11541+ /* branch id may be wrapped around */
027c5e7a
AM
11542+ br = NULL;
11543+ if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
1facf9fc 11544+ goto out;
11545+ nsi_lock.force_lock = 1;
11546+
11547+ /* is this inode still cached? */
11548+ ino = decode_ino(fh + Fh_ino);
4a4d8108
AM
11549+ /* it should never happen */
11550+ if (unlikely(ino == AUFS_ROOT_INO))
8cdd5066 11551+ goto out_unlock;
4a4d8108 11552+
1facf9fc 11553+ dir_ino = decode_ino(fh + Fh_dir_ino);
11554+ dentry = decode_by_ino(sb, ino, dir_ino);
11555+ if (IS_ERR(dentry))
11556+ goto out_unlock;
11557+ if (dentry)
11558+ goto accept;
11559+
11560+ /* is the parent dir cached? */
027c5e7a 11561+ br = au_sbr(sb, nsi_lock.bindex);
5afbbe0d 11562+ au_br_get(br);
1facf9fc 11563+ dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
11564+ if (IS_ERR(dentry))
11565+ goto out_unlock;
11566+ if (dentry)
11567+ goto accept;
11568+
11569+ /* lookup path */
027c5e7a 11570+ dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
1facf9fc 11571+ if (IS_ERR(dentry))
11572+ goto out_unlock;
11573+ if (unlikely(!dentry))
11574+ /* todo?: make it ESTALE */
11575+ goto out_unlock;
11576+
4f0767ce 11577+accept:
027c5e7a 11578+ if (!au_digen_test(dentry, au_sigen(sb))
5527c038 11579+ && d_inode(dentry)->i_generation == fh[Fh_igen])
1facf9fc 11580+ goto out_unlock; /* success */
11581+
11582+ dput(dentry);
11583+ dentry = ERR_PTR(-ESTALE);
4f0767ce 11584+out_unlock:
027c5e7a 11585+ if (br)
5afbbe0d 11586+ au_br_put(br);
1facf9fc 11587+ si_read_unlock(sb);
4f0767ce 11588+out:
1facf9fc 11589+ AuTraceErrPtr(dentry);
11590+ return dentry;
11591+}
11592+
11593+#if 0 /* reserved for future use */
11594+/* support subtreecheck option */
11595+static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
11596+ int fh_len, int fh_type)
11597+{
11598+ struct dentry *parent;
11599+ __u32 *fh = fid->raw;
11600+ ino_t dir_ino;
11601+
11602+ dir_ino = decode_ino(fh + Fh_dir_ino);
11603+ parent = decode_by_ino(sb, dir_ino, 0);
11604+ if (IS_ERR(parent))
11605+ goto out;
11606+ if (!parent)
11607+ parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
11608+ dir_ino, fh, fh_len);
11609+
4f0767ce 11610+out:
1facf9fc 11611+ AuTraceErrPtr(parent);
11612+ return parent;
11613+}
11614+#endif
11615+
11616+/* ---------------------------------------------------------------------- */
11617+
0c3ec466
AM
11618+static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
11619+ struct inode *dir)
1facf9fc 11620+{
11621+ int err;
0c3ec466 11622+ aufs_bindex_t bindex;
1facf9fc 11623+ struct super_block *sb, *h_sb;
0c3ec466
AM
11624+ struct dentry *dentry, *parent, *h_parent;
11625+ struct inode *h_dir;
1facf9fc 11626+ struct au_branch *br;
11627+
1facf9fc 11628+ err = -ENOSPC;
11629+ if (unlikely(*max_len <= Fh_tail)) {
11630+ AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
11631+ goto out;
11632+ }
11633+
11634+ err = FILEID_ROOT;
0c3ec466
AM
11635+ if (inode->i_ino == AUFS_ROOT_INO) {
11636+ AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
1facf9fc 11637+ goto out;
11638+ }
11639+
1facf9fc 11640+ h_parent = NULL;
0c3ec466
AM
11641+ sb = inode->i_sb;
11642+ err = si_read_lock(sb, AuLock_FLUSH);
027c5e7a
AM
11643+ if (unlikely(err))
11644+ goto out;
11645+
1facf9fc 11646+#ifdef CONFIG_AUFS_DEBUG
11647+ if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
11648+ AuWarn1("NFS-exporting requires xino\n");
11649+#endif
027c5e7a 11650+ err = -EIO;
0c3ec466
AM
11651+ parent = NULL;
11652+ ii_read_lock_child(inode);
5afbbe0d 11653+ bindex = au_ibtop(inode);
0c3ec466 11654+ if (!dir) {
c1595e42 11655+ dentry = d_find_any_alias(inode);
0c3ec466
AM
11656+ if (unlikely(!dentry))
11657+ goto out_unlock;
11658+ AuDebugOn(au_test_anon(dentry));
11659+ parent = dget_parent(dentry);
11660+ dput(dentry);
11661+ if (unlikely(!parent))
11662+ goto out_unlock;
5527c038
JR
11663+ if (d_really_is_positive(parent))
11664+ dir = d_inode(parent);
1facf9fc 11665+ }
0c3ec466
AM
11666+
11667+ ii_read_lock_parent(dir);
11668+ h_dir = au_h_iptr(dir, bindex);
11669+ ii_read_unlock(dir);
11670+ if (unlikely(!h_dir))
11671+ goto out_parent;
c1595e42 11672+ h_parent = d_find_any_alias(h_dir);
1facf9fc 11673+ if (unlikely(!h_parent))
0c3ec466 11674+ goto out_hparent;
1facf9fc 11675+
11676+ err = -EPERM;
11677+ br = au_sbr(sb, bindex);
86dc4139 11678+ h_sb = au_br_sb(br);
1facf9fc 11679+ if (unlikely(!h_sb->s_export_op)) {
11680+ AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
0c3ec466 11681+ goto out_hparent;
1facf9fc 11682+ }
11683+
11684+ fh[Fh_br_id] = br->br_id;
11685+ fh[Fh_sigen] = au_sigen(sb);
11686+ encode_ino(fh + Fh_ino, inode->i_ino);
0c3ec466 11687+ encode_ino(fh + Fh_dir_ino, dir->i_ino);
1facf9fc 11688+ fh[Fh_igen] = inode->i_generation;
11689+
11690+ *max_len -= Fh_tail;
11691+ fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
11692+ max_len,
11693+ /*connectable or subtreecheck*/0);
11694+ err = fh[Fh_h_type];
11695+ *max_len += Fh_tail;
11696+ /* todo: macros? */
1716fcea 11697+ if (err != FILEID_INVALID)
1facf9fc 11698+ err = 99;
11699+ else
11700+ AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
11701+
0c3ec466 11702+out_hparent:
1facf9fc 11703+ dput(h_parent);
0c3ec466 11704+out_parent:
1facf9fc 11705+ dput(parent);
0c3ec466
AM
11706+out_unlock:
11707+ ii_read_unlock(inode);
11708+ si_read_unlock(sb);
4f0767ce 11709+out:
1facf9fc 11710+ if (unlikely(err < 0))
1716fcea 11711+ err = FILEID_INVALID;
1facf9fc 11712+ return err;
11713+}
11714+
11715+/* ---------------------------------------------------------------------- */
11716+
4a4d8108
AM
11717+static int aufs_commit_metadata(struct inode *inode)
11718+{
11719+ int err;
11720+ aufs_bindex_t bindex;
11721+ struct super_block *sb;
11722+ struct inode *h_inode;
11723+ int (*f)(struct inode *inode);
11724+
11725+ sb = inode->i_sb;
e49829fe 11726+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 11727+ ii_write_lock_child(inode);
5afbbe0d 11728+ bindex = au_ibtop(inode);
4a4d8108
AM
11729+ AuDebugOn(bindex < 0);
11730+ h_inode = au_h_iptr(inode, bindex);
11731+
11732+ f = h_inode->i_sb->s_export_op->commit_metadata;
11733+ if (f)
11734+ err = f(h_inode);
11735+ else {
11736+ struct writeback_control wbc = {
11737+ .sync_mode = WB_SYNC_ALL,
11738+ .nr_to_write = 0 /* metadata only */
11739+ };
11740+
11741+ err = sync_inode(h_inode, &wbc);
11742+ }
11743+
11744+ au_cpup_attr_timesizes(inode);
11745+ ii_write_unlock(inode);
11746+ si_read_unlock(sb);
11747+ return err;
11748+}
11749+
11750+/* ---------------------------------------------------------------------- */
11751+
1facf9fc 11752+static struct export_operations aufs_export_op = {
4a4d8108 11753+ .fh_to_dentry = aufs_fh_to_dentry,
1facf9fc 11754+ /* .fh_to_parent = aufs_fh_to_parent, */
4a4d8108
AM
11755+ .encode_fh = aufs_encode_fh,
11756+ .commit_metadata = aufs_commit_metadata
1facf9fc 11757+};
11758+
11759+void au_export_init(struct super_block *sb)
11760+{
11761+ struct au_sbinfo *sbinfo;
11762+ __u32 u;
11763+
5afbbe0d
AM
11764+ BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
11765+ && IS_MODULE(CONFIG_EXPORTFS),
11766+ AUFS_NAME ": unsupported configuration "
11767+ "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
11768+
1facf9fc 11769+ sb->s_export_op = &aufs_export_op;
11770+ sbinfo = au_sbi(sb);
11771+ sbinfo->si_xigen = NULL;
11772+ get_random_bytes(&u, sizeof(u));
11773+ BUILD_BUG_ON(sizeof(u) != sizeof(int));
11774+ atomic_set(&sbinfo->si_xigen_next, u);
11775+}
076b876e
AM
11776diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
11777--- /usr/share/empty/fs/aufs/fhsm.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 11778+++ linux/fs/aufs/fhsm.c 2016-07-25 19:05:34.814493242 +0200
c1595e42 11779@@ -0,0 +1,426 @@
076b876e 11780+/*
8cdd5066 11781+ * Copyright (C) 2011-2016 Junjiro R. Okajima
076b876e
AM
11782+ *
11783+ * This program, aufs is free software; you can redistribute it and/or modify
11784+ * it under the terms of the GNU General Public License as published by
11785+ * the Free Software Foundation; either version 2 of the License, or
11786+ * (at your option) any later version.
11787+ *
11788+ * This program is distributed in the hope that it will be useful,
11789+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11790+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11791+ * GNU General Public License for more details.
11792+ *
11793+ * You should have received a copy of the GNU General Public License
11794+ * along with this program; if not, write to the Free Software
11795+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
11796+ */
11797+
11798+/*
11799+ * File-based Hierarchy Storage Management
11800+ */
11801+
11802+#include <linux/anon_inodes.h>
11803+#include <linux/poll.h>
11804+#include <linux/seq_file.h>
11805+#include <linux/statfs.h>
11806+#include "aufs.h"
11807+
c1595e42
JR
11808+static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
11809+{
11810+ struct au_sbinfo *sbinfo;
11811+ struct au_fhsm *fhsm;
11812+
11813+ SiMustAnyLock(sb);
11814+
11815+ sbinfo = au_sbi(sb);
11816+ fhsm = &sbinfo->si_fhsm;
11817+ AuDebugOn(!fhsm);
11818+ return fhsm->fhsm_bottom;
11819+}
11820+
11821+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
11822+{
11823+ struct au_sbinfo *sbinfo;
11824+ struct au_fhsm *fhsm;
11825+
11826+ SiMustWriteLock(sb);
11827+
11828+ sbinfo = au_sbi(sb);
11829+ fhsm = &sbinfo->si_fhsm;
11830+ AuDebugOn(!fhsm);
11831+ fhsm->fhsm_bottom = bindex;
11832+}
11833+
11834+/* ---------------------------------------------------------------------- */
11835+
076b876e
AM
11836+static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
11837+{
11838+ struct au_br_fhsm *bf;
11839+
11840+ bf = br->br_fhsm;
11841+ MtxMustLock(&bf->bf_lock);
11842+
11843+ return !bf->bf_readable
11844+ || time_after(jiffies,
11845+ bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
11846+}
11847+
11848+/* ---------------------------------------------------------------------- */
11849+
11850+static void au_fhsm_notify(struct super_block *sb, int val)
11851+{
11852+ struct au_sbinfo *sbinfo;
11853+ struct au_fhsm *fhsm;
11854+
11855+ SiMustAnyLock(sb);
11856+
11857+ sbinfo = au_sbi(sb);
11858+ fhsm = &sbinfo->si_fhsm;
11859+ if (au_fhsm_pid(fhsm)
11860+ && atomic_read(&fhsm->fhsm_readable) != -1) {
11861+ atomic_set(&fhsm->fhsm_readable, val);
11862+ if (val)
11863+ wake_up(&fhsm->fhsm_wqh);
11864+ }
11865+}
11866+
11867+static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
11868+ struct aufs_stfs *rstfs, int do_lock, int do_notify)
11869+{
11870+ int err;
11871+ struct au_branch *br;
11872+ struct au_br_fhsm *bf;
11873+
11874+ br = au_sbr(sb, bindex);
11875+ AuDebugOn(au_br_rdonly(br));
11876+ bf = br->br_fhsm;
11877+ AuDebugOn(!bf);
11878+
11879+ if (do_lock)
11880+ mutex_lock(&bf->bf_lock);
11881+ else
11882+ MtxMustLock(&bf->bf_lock);
11883+
11884+ /* sb->s_root for NFS is unreliable */
11885+ err = au_br_stfs(br, &bf->bf_stfs);
11886+ if (unlikely(err)) {
11887+ AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
11888+ goto out;
11889+ }
11890+
11891+ bf->bf_jiffy = jiffies;
11892+ bf->bf_readable = 1;
11893+ if (do_notify)
11894+ au_fhsm_notify(sb, /*val*/1);
11895+ if (rstfs)
11896+ *rstfs = bf->bf_stfs;
11897+
11898+out:
11899+ if (do_lock)
11900+ mutex_unlock(&bf->bf_lock);
11901+ au_fhsm_notify(sb, /*val*/1);
11902+
11903+ return err;
11904+}
11905+
11906+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
11907+{
11908+ int err;
076b876e
AM
11909+ struct au_sbinfo *sbinfo;
11910+ struct au_fhsm *fhsm;
11911+ struct au_branch *br;
11912+ struct au_br_fhsm *bf;
11913+
11914+ AuDbg("b%d, force %d\n", bindex, force);
11915+ SiMustAnyLock(sb);
11916+
11917+ sbinfo = au_sbi(sb);
11918+ fhsm = &sbinfo->si_fhsm;
c1595e42
JR
11919+ if (!au_ftest_si(sbinfo, FHSM)
11920+ || fhsm->fhsm_bottom == bindex)
076b876e
AM
11921+ return;
11922+
11923+ br = au_sbr(sb, bindex);
11924+ bf = br->br_fhsm;
11925+ AuDebugOn(!bf);
11926+ mutex_lock(&bf->bf_lock);
11927+ if (force
11928+ || au_fhsm_pid(fhsm)
11929+ || au_fhsm_test_jiffy(sbinfo, br))
11930+ err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
11931+ /*do_notify*/1);
11932+ mutex_unlock(&bf->bf_lock);
11933+}
11934+
11935+void au_fhsm_wrote_all(struct super_block *sb, int force)
11936+{
5afbbe0d 11937+ aufs_bindex_t bindex, bbot;
076b876e
AM
11938+ struct au_branch *br;
11939+
11940+ /* exclude the bottom */
5afbbe0d
AM
11941+ bbot = au_fhsm_bottom(sb);
11942+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
11943+ br = au_sbr(sb, bindex);
11944+ if (au_br_fhsm(br->br_perm))
11945+ au_fhsm_wrote(sb, bindex, force);
11946+ }
11947+}
11948+
11949+/* ---------------------------------------------------------------------- */
11950+
11951+static unsigned int au_fhsm_poll(struct file *file,
11952+ struct poll_table_struct *wait)
11953+{
11954+ unsigned int mask;
11955+ struct au_sbinfo *sbinfo;
11956+ struct au_fhsm *fhsm;
11957+
11958+ mask = 0;
11959+ sbinfo = file->private_data;
11960+ fhsm = &sbinfo->si_fhsm;
11961+ poll_wait(file, &fhsm->fhsm_wqh, wait);
11962+ if (atomic_read(&fhsm->fhsm_readable))
11963+ mask = POLLIN /* | POLLRDNORM */;
11964+
11965+ AuTraceErr((int)mask);
11966+ return mask;
11967+}
11968+
11969+static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
11970+ struct aufs_stfs *stfs, __s16 brid)
11971+{
11972+ int err;
11973+
11974+ err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
11975+ if (!err)
11976+ err = __put_user(brid, &stbr->brid);
11977+ if (unlikely(err))
11978+ err = -EFAULT;
11979+
11980+ return err;
11981+}
11982+
11983+static ssize_t au_fhsm_do_read(struct super_block *sb,
11984+ struct aufs_stbr __user *stbr, size_t count)
11985+{
11986+ ssize_t err;
11987+ int nstbr;
5afbbe0d 11988+ aufs_bindex_t bindex, bbot;
076b876e
AM
11989+ struct au_branch *br;
11990+ struct au_br_fhsm *bf;
11991+
11992+ /* except the bottom branch */
11993+ err = 0;
11994+ nstbr = 0;
5afbbe0d
AM
11995+ bbot = au_fhsm_bottom(sb);
11996+ for (bindex = 0; !err && bindex < bbot; bindex++) {
076b876e
AM
11997+ br = au_sbr(sb, bindex);
11998+ if (!au_br_fhsm(br->br_perm))
11999+ continue;
12000+
12001+ bf = br->br_fhsm;
12002+ mutex_lock(&bf->bf_lock);
12003+ if (bf->bf_readable) {
12004+ err = -EFAULT;
12005+ if (count >= sizeof(*stbr))
12006+ err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
12007+ br->br_id);
12008+ if (!err) {
12009+ bf->bf_readable = 0;
12010+ count -= sizeof(*stbr);
12011+ nstbr++;
12012+ }
12013+ }
12014+ mutex_unlock(&bf->bf_lock);
12015+ }
12016+ if (!err)
12017+ err = sizeof(*stbr) * nstbr;
12018+
12019+ return err;
12020+}
12021+
12022+static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
12023+ loff_t *pos)
12024+{
12025+ ssize_t err;
12026+ int readable;
5afbbe0d 12027+ aufs_bindex_t nfhsm, bindex, bbot;
076b876e
AM
12028+ struct au_sbinfo *sbinfo;
12029+ struct au_fhsm *fhsm;
12030+ struct au_branch *br;
12031+ struct super_block *sb;
12032+
12033+ err = 0;
12034+ sbinfo = file->private_data;
12035+ fhsm = &sbinfo->si_fhsm;
12036+need_data:
12037+ spin_lock_irq(&fhsm->fhsm_wqh.lock);
12038+ if (!atomic_read(&fhsm->fhsm_readable)) {
12039+ if (vfsub_file_flags(file) & O_NONBLOCK)
12040+ err = -EAGAIN;
12041+ else
12042+ err = wait_event_interruptible_locked_irq
12043+ (fhsm->fhsm_wqh,
12044+ atomic_read(&fhsm->fhsm_readable));
12045+ }
12046+ spin_unlock_irq(&fhsm->fhsm_wqh.lock);
12047+ if (unlikely(err))
12048+ goto out;
12049+
12050+ /* sb may already be dead */
12051+ au_rw_read_lock(&sbinfo->si_rwsem);
12052+ readable = atomic_read(&fhsm->fhsm_readable);
12053+ if (readable > 0) {
12054+ sb = sbinfo->si_sb;
12055+ AuDebugOn(!sb);
12056+ /* exclude the bottom branch */
12057+ nfhsm = 0;
5afbbe0d
AM
12058+ bbot = au_fhsm_bottom(sb);
12059+ for (bindex = 0; bindex < bbot; bindex++) {
076b876e
AM
12060+ br = au_sbr(sb, bindex);
12061+ if (au_br_fhsm(br->br_perm))
12062+ nfhsm++;
12063+ }
12064+ err = -EMSGSIZE;
12065+ if (nfhsm * sizeof(struct aufs_stbr) <= count) {
12066+ atomic_set(&fhsm->fhsm_readable, 0);
12067+ err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
12068+ count);
12069+ }
12070+ }
12071+ au_rw_read_unlock(&sbinfo->si_rwsem);
12072+ if (!readable)
12073+ goto need_data;
12074+
12075+out:
12076+ return err;
12077+}
12078+
12079+static int au_fhsm_release(struct inode *inode, struct file *file)
12080+{
12081+ struct au_sbinfo *sbinfo;
12082+ struct au_fhsm *fhsm;
12083+
12084+ /* sb may already be dead */
12085+ sbinfo = file->private_data;
12086+ fhsm = &sbinfo->si_fhsm;
12087+ spin_lock(&fhsm->fhsm_spin);
12088+ fhsm->fhsm_pid = 0;
12089+ spin_unlock(&fhsm->fhsm_spin);
12090+ kobject_put(&sbinfo->si_kobj);
12091+
12092+ return 0;
12093+}
12094+
12095+static const struct file_operations au_fhsm_fops = {
12096+ .owner = THIS_MODULE,
12097+ .llseek = noop_llseek,
12098+ .read = au_fhsm_read,
12099+ .poll = au_fhsm_poll,
12100+ .release = au_fhsm_release
12101+};
12102+
12103+int au_fhsm_fd(struct super_block *sb, int oflags)
12104+{
12105+ int err, fd;
12106+ struct au_sbinfo *sbinfo;
12107+ struct au_fhsm *fhsm;
12108+
12109+ err = -EPERM;
12110+ if (unlikely(!capable(CAP_SYS_ADMIN)))
12111+ goto out;
12112+
12113+ err = -EINVAL;
12114+ if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
12115+ goto out;
12116+
12117+ err = 0;
12118+ sbinfo = au_sbi(sb);
12119+ fhsm = &sbinfo->si_fhsm;
12120+ spin_lock(&fhsm->fhsm_spin);
12121+ if (!fhsm->fhsm_pid)
12122+ fhsm->fhsm_pid = current->pid;
12123+ else
12124+ err = -EBUSY;
12125+ spin_unlock(&fhsm->fhsm_spin);
12126+ if (unlikely(err))
12127+ goto out;
12128+
12129+ oflags |= O_RDONLY;
12130+ /* oflags |= FMODE_NONOTIFY; */
12131+ fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
12132+ err = fd;
12133+ if (unlikely(fd < 0))
12134+ goto out_pid;
12135+
12136+ /* succeed reglardless 'fhsm' status */
12137+ kobject_get(&sbinfo->si_kobj);
12138+ si_noflush_read_lock(sb);
12139+ if (au_ftest_si(sbinfo, FHSM))
12140+ au_fhsm_wrote_all(sb, /*force*/0);
12141+ si_read_unlock(sb);
12142+ goto out; /* success */
12143+
12144+out_pid:
12145+ spin_lock(&fhsm->fhsm_spin);
12146+ fhsm->fhsm_pid = 0;
12147+ spin_unlock(&fhsm->fhsm_spin);
12148+out:
12149+ AuTraceErr(err);
12150+ return err;
12151+}
12152+
12153+/* ---------------------------------------------------------------------- */
12154+
12155+int au_fhsm_br_alloc(struct au_branch *br)
12156+{
12157+ int err;
12158+
12159+ err = 0;
12160+ br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
12161+ if (br->br_fhsm)
12162+ au_br_fhsm_init(br->br_fhsm);
12163+ else
12164+ err = -ENOMEM;
12165+
12166+ return err;
12167+}
12168+
12169+/* ---------------------------------------------------------------------- */
12170+
12171+void au_fhsm_fin(struct super_block *sb)
12172+{
12173+ au_fhsm_notify(sb, /*val*/-1);
12174+}
12175+
12176+void au_fhsm_init(struct au_sbinfo *sbinfo)
12177+{
12178+ struct au_fhsm *fhsm;
12179+
12180+ fhsm = &sbinfo->si_fhsm;
12181+ spin_lock_init(&fhsm->fhsm_spin);
12182+ init_waitqueue_head(&fhsm->fhsm_wqh);
12183+ atomic_set(&fhsm->fhsm_readable, 0);
12184+ fhsm->fhsm_expire
12185+ = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
c1595e42 12186+ fhsm->fhsm_bottom = -1;
076b876e
AM
12187+}
12188+
12189+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
12190+{
12191+ sbinfo->si_fhsm.fhsm_expire
12192+ = msecs_to_jiffies(sec * MSEC_PER_SEC);
12193+}
12194+
12195+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
12196+{
12197+ unsigned int u;
12198+
12199+ if (!au_ftest_si(sbinfo, FHSM))
12200+ return;
12201+
12202+ u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
12203+ if (u != AUFS_FHSM_CACHE_DEF_SEC)
12204+ seq_printf(seq, ",fhsm_sec=%u", u);
12205+}
7f207e10
AM
12206diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
12207--- /usr/share/empty/fs/aufs/file.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
12208+++ linux/fs/aufs/file.c 2016-07-25 19:05:34.814493242 +0200
12209@@ -0,0 +1,843 @@
1facf9fc 12210+/*
8cdd5066 12211+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 12212+ *
12213+ * This program, aufs is free software; you can redistribute it and/or modify
12214+ * it under the terms of the GNU General Public License as published by
12215+ * the Free Software Foundation; either version 2 of the License, or
12216+ * (at your option) any later version.
dece6358
AM
12217+ *
12218+ * This program is distributed in the hope that it will be useful,
12219+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12220+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12221+ * GNU General Public License for more details.
12222+ *
12223+ * You should have received a copy of the GNU General Public License
523b37e3 12224+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 12225+ */
12226+
12227+/*
4a4d8108 12228+ * handling file/dir, and address_space operation
1facf9fc 12229+ */
12230+
7eafdf33
AM
12231+#ifdef CONFIG_AUFS_DEBUG
12232+#include <linux/migrate.h>
12233+#endif
4a4d8108 12234+#include <linux/pagemap.h>
1facf9fc 12235+#include "aufs.h"
12236+
4a4d8108
AM
12237+/* drop flags for writing */
12238+unsigned int au_file_roflags(unsigned int flags)
12239+{
12240+ flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
12241+ flags |= O_RDONLY | O_NOATIME;
12242+ return flags;
12243+}
12244+
12245+/* common functions to regular file and dir */
12246+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 12247+ struct file *file, int force_wr)
1facf9fc 12248+{
1308ab2a 12249+ struct file *h_file;
4a4d8108
AM
12250+ struct dentry *h_dentry;
12251+ struct inode *h_inode;
12252+ struct super_block *sb;
12253+ struct au_branch *br;
12254+ struct path h_path;
b912730e 12255+ int err;
1facf9fc 12256+
4a4d8108
AM
12257+ /* a race condition can happen between open and unlink/rmdir */
12258+ h_file = ERR_PTR(-ENOENT);
12259+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 12260+ if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
4a4d8108 12261+ goto out;
5527c038 12262+ h_inode = d_inode(h_dentry);
027c5e7a
AM
12263+ spin_lock(&h_dentry->d_lock);
12264+ err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
5527c038 12265+ /* || !d_inode(dentry)->i_nlink */
027c5e7a
AM
12266+ ;
12267+ spin_unlock(&h_dentry->d_lock);
12268+ if (unlikely(err))
4a4d8108 12269+ goto out;
1facf9fc 12270+
4a4d8108
AM
12271+ sb = dentry->d_sb;
12272+ br = au_sbr(sb, bindex);
b912730e
AM
12273+ err = au_br_test_oflag(flags, br);
12274+ h_file = ERR_PTR(err);
12275+ if (unlikely(err))
027c5e7a 12276+ goto out;
1facf9fc 12277+
4a4d8108 12278+ /* drop flags for writing */
5527c038 12279+ if (au_test_ro(sb, bindex, d_inode(dentry))) {
392086de
AM
12280+ if (force_wr && !(flags & O_WRONLY))
12281+ force_wr = 0;
4a4d8108 12282+ flags = au_file_roflags(flags);
392086de
AM
12283+ if (force_wr) {
12284+ h_file = ERR_PTR(-EROFS);
12285+ flags = au_file_roflags(flags);
12286+ if (unlikely(vfsub_native_ro(h_inode)
12287+ || IS_APPEND(h_inode)))
12288+ goto out;
12289+ flags &= ~O_ACCMODE;
12290+ flags |= O_WRONLY;
12291+ }
12292+ }
4a4d8108 12293+ flags &= ~O_CREAT;
5afbbe0d 12294+ au_br_get(br);
4a4d8108 12295+ h_path.dentry = h_dentry;
86dc4139 12296+ h_path.mnt = au_br_mnt(br);
38d290e6 12297+ h_file = vfsub_dentry_open(&h_path, flags);
4a4d8108
AM
12298+ if (IS_ERR(h_file))
12299+ goto out_br;
dece6358 12300+
b912730e 12301+ if (flags & __FMODE_EXEC) {
4a4d8108
AM
12302+ err = deny_write_access(h_file);
12303+ if (unlikely(err)) {
12304+ fput(h_file);
12305+ h_file = ERR_PTR(err);
12306+ goto out_br;
12307+ }
12308+ }
953406b4 12309+ fsnotify_open(h_file);
4a4d8108 12310+ goto out; /* success */
1facf9fc 12311+
4f0767ce 12312+out_br:
5afbbe0d 12313+ au_br_put(br);
4f0767ce 12314+out:
4a4d8108
AM
12315+ return h_file;
12316+}
1308ab2a 12317+
076b876e
AM
12318+static int au_cmoo(struct dentry *dentry)
12319+{
12320+ int err, cmoo;
12321+ unsigned int udba;
12322+ struct path h_path;
12323+ struct au_pin pin;
12324+ struct au_cp_generic cpg = {
12325+ .dentry = dentry,
12326+ .bdst = -1,
12327+ .bsrc = -1,
12328+ .len = -1,
12329+ .pin = &pin,
12330+ .flags = AuCpup_DTIME | AuCpup_HOPEN
12331+ };
7e9cd9fe 12332+ struct inode *delegated;
076b876e
AM
12333+ struct super_block *sb;
12334+ struct au_sbinfo *sbinfo;
12335+ struct au_fhsm *fhsm;
12336+ pid_t pid;
12337+ struct au_branch *br;
12338+ struct dentry *parent;
12339+ struct au_hinode *hdir;
12340+
12341+ DiMustWriteLock(dentry);
5527c038 12342+ IiMustWriteLock(d_inode(dentry));
076b876e
AM
12343+
12344+ err = 0;
12345+ if (IS_ROOT(dentry))
12346+ goto out;
5afbbe0d 12347+ cpg.bsrc = au_dbtop(dentry);
076b876e
AM
12348+ if (!cpg.bsrc)
12349+ goto out;
12350+
12351+ sb = dentry->d_sb;
12352+ sbinfo = au_sbi(sb);
12353+ fhsm = &sbinfo->si_fhsm;
12354+ pid = au_fhsm_pid(fhsm);
12355+ if (pid
12356+ && (current->pid == pid
12357+ || current->real_parent->pid == pid))
12358+ goto out;
12359+
12360+ br = au_sbr(sb, cpg.bsrc);
12361+ cmoo = au_br_cmoo(br->br_perm);
12362+ if (!cmoo)
12363+ goto out;
7e9cd9fe 12364+ if (!d_is_reg(dentry))
076b876e
AM
12365+ cmoo &= AuBrAttr_COO_ALL;
12366+ if (!cmoo)
12367+ goto out;
12368+
12369+ parent = dget_parent(dentry);
12370+ di_write_lock_parent(parent);
12371+ err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
12372+ cpg.bdst = err;
12373+ if (unlikely(err < 0)) {
12374+ err = 0; /* there is no upper writable branch */
12375+ goto out_dgrade;
12376+ }
12377+ AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
12378+
12379+ /* do not respect the coo attrib for the target branch */
12380+ err = au_cpup_dirs(dentry, cpg.bdst);
12381+ if (unlikely(err))
12382+ goto out_dgrade;
12383+
12384+ di_downgrade_lock(parent, AuLock_IR);
12385+ udba = au_opt_udba(sb);
12386+ err = au_pin(&pin, dentry, cpg.bdst, udba,
12387+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12388+ if (unlikely(err))
12389+ goto out_parent;
12390+
12391+ err = au_sio_cpup_simple(&cpg);
12392+ au_unpin(&pin);
12393+ if (unlikely(err))
12394+ goto out_parent;
12395+ if (!(cmoo & AuBrWAttr_MOO))
12396+ goto out_parent; /* success */
12397+
12398+ err = au_pin(&pin, dentry, cpg.bsrc, udba,
12399+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12400+ if (unlikely(err))
12401+ goto out_parent;
12402+
12403+ h_path.mnt = au_br_mnt(br);
12404+ h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
5527c038 12405+ hdir = au_hi(d_inode(parent), cpg.bsrc);
076b876e
AM
12406+ delegated = NULL;
12407+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
12408+ au_unpin(&pin);
12409+ /* todo: keep h_dentry or not? */
12410+ if (unlikely(err == -EWOULDBLOCK)) {
12411+ pr_warn("cannot retry for NFSv4 delegation"
12412+ " for an internal unlink\n");
12413+ iput(delegated);
12414+ }
12415+ if (unlikely(err)) {
12416+ pr_err("unlink %pd after coo failed (%d), ignored\n",
12417+ dentry, err);
12418+ err = 0;
12419+ }
12420+ goto out_parent; /* success */
12421+
12422+out_dgrade:
12423+ di_downgrade_lock(parent, AuLock_IR);
12424+out_parent:
12425+ di_read_unlock(parent, AuLock_IR);
12426+ dput(parent);
12427+out:
12428+ AuTraceErr(err);
12429+ return err;
12430+}
12431+
b912730e 12432+int au_do_open(struct file *file, struct au_do_open_args *args)
1facf9fc 12433+{
b912730e 12434+ int err, no_lock = args->no_lock;
1facf9fc 12435+ struct dentry *dentry;
076b876e 12436+ struct au_finfo *finfo;
1308ab2a 12437+
b912730e
AM
12438+ if (!no_lock)
12439+ err = au_finfo_init(file, args->fidir);
12440+ else {
12441+ lockdep_off();
12442+ err = au_finfo_init(file, args->fidir);
12443+ lockdep_on();
12444+ }
4a4d8108
AM
12445+ if (unlikely(err))
12446+ goto out;
1facf9fc 12447+
2000de60 12448+ dentry = file->f_path.dentry;
b912730e
AM
12449+ AuDebugOn(IS_ERR_OR_NULL(dentry));
12450+ if (!no_lock) {
12451+ di_write_lock_child(dentry);
12452+ err = au_cmoo(dentry);
12453+ di_downgrade_lock(dentry, AuLock_IR);
12454+ if (!err)
12455+ err = args->open(file, vfsub_file_flags(file), NULL);
12456+ di_read_unlock(dentry, AuLock_IR);
12457+ } else {
12458+ err = au_cmoo(dentry);
12459+ if (!err)
12460+ err = args->open(file, vfsub_file_flags(file),
12461+ args->h_file);
5afbbe0d 12462+ if (!err && au_fbtop(file) != au_dbtop(dentry))
b912730e
AM
12463+ /*
12464+ * cmoo happens after h_file was opened.
12465+ * need to refresh file later.
12466+ */
12467+ atomic_dec(&au_fi(file)->fi_generation);
12468+ }
1facf9fc 12469+
076b876e
AM
12470+ finfo = au_fi(file);
12471+ if (!err) {
12472+ finfo->fi_file = file;
12473+ au_sphl_add(&finfo->fi_hlist,
2000de60 12474+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
076b876e 12475+ }
b912730e
AM
12476+ if (!no_lock)
12477+ fi_write_unlock(file);
12478+ else {
12479+ lockdep_off();
12480+ fi_write_unlock(file);
12481+ lockdep_on();
12482+ }
4a4d8108 12483+ if (unlikely(err)) {
076b876e 12484+ finfo->fi_hdir = NULL;
4a4d8108 12485+ au_finfo_fin(file);
1308ab2a 12486+ }
4a4d8108 12487+
4f0767ce 12488+out:
1308ab2a 12489+ return err;
12490+}
dece6358 12491+
4a4d8108 12492+int au_reopen_nondir(struct file *file)
1308ab2a 12493+{
4a4d8108 12494+ int err;
5afbbe0d 12495+ aufs_bindex_t btop;
4a4d8108
AM
12496+ struct dentry *dentry;
12497+ struct file *h_file, *h_file_tmp;
1308ab2a 12498+
2000de60 12499+ dentry = file->f_path.dentry;
5afbbe0d 12500+ btop = au_dbtop(dentry);
4a4d8108 12501+ h_file_tmp = NULL;
5afbbe0d 12502+ if (au_fbtop(file) == btop) {
4a4d8108
AM
12503+ h_file = au_hf_top(file);
12504+ if (file->f_mode == h_file->f_mode)
12505+ return 0; /* success */
12506+ h_file_tmp = h_file;
12507+ get_file(h_file_tmp);
5afbbe0d 12508+ au_set_h_fptr(file, btop, NULL);
4a4d8108
AM
12509+ }
12510+ AuDebugOn(au_fi(file)->fi_hdir);
86dc4139
AM
12511+ /*
12512+ * it can happen
12513+ * file exists on both of rw and ro
5afbbe0d 12514+ * open --> dbtop and fbtop are both 0
86dc4139
AM
12515+ * prepend a branch as rw, "rw" become ro
12516+ * remove rw/file
12517+ * delete the top branch, "rw" becomes rw again
5afbbe0d
AM
12518+ * --> dbtop is 1, fbtop is still 0
12519+ * write --> fbtop is 0 but dbtop is 1
86dc4139 12520+ */
5afbbe0d 12521+ /* AuDebugOn(au_fbtop(file) < btop); */
1308ab2a 12522+
5afbbe0d 12523+ h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
392086de 12524+ file, /*force_wr*/0);
4a4d8108 12525+ err = PTR_ERR(h_file);
86dc4139
AM
12526+ if (IS_ERR(h_file)) {
12527+ if (h_file_tmp) {
5afbbe0d
AM
12528+ au_sbr_get(dentry->d_sb, btop);
12529+ au_set_h_fptr(file, btop, h_file_tmp);
86dc4139
AM
12530+ h_file_tmp = NULL;
12531+ }
4a4d8108 12532+ goto out; /* todo: close all? */
86dc4139 12533+ }
4a4d8108
AM
12534+
12535+ err = 0;
5afbbe0d
AM
12536+ au_set_fbtop(file, btop);
12537+ au_set_h_fptr(file, btop, h_file);
4a4d8108
AM
12538+ au_update_figen(file);
12539+ /* todo: necessary? */
12540+ /* file->f_ra = h_file->f_ra; */
12541+
4f0767ce 12542+out:
4a4d8108
AM
12543+ if (h_file_tmp)
12544+ fput(h_file_tmp);
12545+ return err;
1facf9fc 12546+}
12547+
1308ab2a 12548+/* ---------------------------------------------------------------------- */
12549+
4a4d8108
AM
12550+static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
12551+ struct dentry *hi_wh)
1facf9fc 12552+{
4a4d8108 12553+ int err;
5afbbe0d 12554+ aufs_bindex_t btop;
4a4d8108
AM
12555+ struct au_dinfo *dinfo;
12556+ struct dentry *h_dentry;
12557+ struct au_hdentry *hdp;
1facf9fc 12558+
2000de60 12559+ dinfo = au_di(file->f_path.dentry);
4a4d8108 12560+ AuRwMustWriteLock(&dinfo->di_rwsem);
dece6358 12561+
5afbbe0d
AM
12562+ btop = dinfo->di_btop;
12563+ dinfo->di_btop = btgt;
12564+ hdp = au_hdentry(dinfo, btgt);
12565+ h_dentry = hdp->hd_dentry;
12566+ hdp->hd_dentry = hi_wh;
4a4d8108 12567+ err = au_reopen_nondir(file);
5afbbe0d
AM
12568+ hdp->hd_dentry = h_dentry;
12569+ dinfo->di_btop = btop;
1facf9fc 12570+
1facf9fc 12571+ return err;
12572+}
12573+
4a4d8108 12574+static int au_ready_to_write_wh(struct file *file, loff_t len,
86dc4139 12575+ aufs_bindex_t bcpup, struct au_pin *pin)
1facf9fc 12576+{
4a4d8108 12577+ int err;
027c5e7a 12578+ struct inode *inode, *h_inode;
c2b27bf2
AM
12579+ struct dentry *h_dentry, *hi_wh;
12580+ struct au_cp_generic cpg = {
2000de60 12581+ .dentry = file->f_path.dentry,
c2b27bf2
AM
12582+ .bdst = bcpup,
12583+ .bsrc = -1,
12584+ .len = len,
12585+ .pin = pin
12586+ };
1facf9fc 12587+
5afbbe0d 12588+ au_update_dbtop(cpg.dentry);
5527c038 12589+ inode = d_inode(cpg.dentry);
027c5e7a 12590+ h_inode = NULL;
5afbbe0d
AM
12591+ if (au_dbtop(cpg.dentry) <= bcpup
12592+ && au_dbbot(cpg.dentry) >= bcpup) {
c2b27bf2 12593+ h_dentry = au_h_dptr(cpg.dentry, bcpup);
5527c038
JR
12594+ if (h_dentry && d_is_positive(h_dentry))
12595+ h_inode = d_inode(h_dentry);
027c5e7a 12596+ }
4a4d8108 12597+ hi_wh = au_hi_wh(inode, bcpup);
027c5e7a 12598+ if (!hi_wh && !h_inode)
c2b27bf2 12599+ err = au_sio_cpup_wh(&cpg, file);
4a4d8108
AM
12600+ else
12601+ /* already copied-up after unlink */
12602+ err = au_reopen_wh(file, bcpup, hi_wh);
1facf9fc 12603+
4a4d8108 12604+ if (!err
38d290e6
JR
12605+ && (inode->i_nlink > 1
12606+ || (inode->i_state & I_LINKABLE))
c2b27bf2
AM
12607+ && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
12608+ au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
1308ab2a 12609+
dece6358 12610+ return err;
1facf9fc 12611+}
12612+
4a4d8108
AM
12613+/*
12614+ * prepare the @file for writing.
12615+ */
12616+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
1facf9fc 12617+{
4a4d8108 12618+ int err;
5afbbe0d 12619+ aufs_bindex_t dbtop;
c1595e42 12620+ struct dentry *parent;
86dc4139 12621+ struct inode *inode;
1facf9fc 12622+ struct super_block *sb;
4a4d8108 12623+ struct file *h_file;
c2b27bf2 12624+ struct au_cp_generic cpg = {
2000de60 12625+ .dentry = file->f_path.dentry,
c2b27bf2
AM
12626+ .bdst = -1,
12627+ .bsrc = -1,
12628+ .len = len,
12629+ .pin = pin,
12630+ .flags = AuCpup_DTIME
12631+ };
1facf9fc 12632+
c2b27bf2 12633+ sb = cpg.dentry->d_sb;
5527c038 12634+ inode = d_inode(cpg.dentry);
5afbbe0d 12635+ cpg.bsrc = au_fbtop(file);
c2b27bf2 12636+ err = au_test_ro(sb, cpg.bsrc, inode);
4a4d8108 12637+ if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
c2b27bf2
AM
12638+ err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
12639+ /*flags*/0);
1facf9fc 12640+ goto out;
4a4d8108 12641+ }
1facf9fc 12642+
027c5e7a 12643+ /* need to cpup or reopen */
c2b27bf2 12644+ parent = dget_parent(cpg.dentry);
4a4d8108 12645+ di_write_lock_parent(parent);
c2b27bf2
AM
12646+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12647+ cpg.bdst = err;
4a4d8108
AM
12648+ if (unlikely(err < 0))
12649+ goto out_dgrade;
12650+ err = 0;
12651+
c2b27bf2
AM
12652+ if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
12653+ err = au_cpup_dirs(cpg.dentry, cpg.bdst);
1facf9fc 12654+ if (unlikely(err))
4a4d8108
AM
12655+ goto out_dgrade;
12656+ }
12657+
c2b27bf2 12658+ err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108
AM
12659+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
12660+ if (unlikely(err))
12661+ goto out_dgrade;
12662+
5afbbe0d
AM
12663+ dbtop = au_dbtop(cpg.dentry);
12664+ if (dbtop <= cpg.bdst)
c2b27bf2 12665+ cpg.bsrc = cpg.bdst;
027c5e7a 12666+
5afbbe0d 12667+ if (dbtop <= cpg.bdst /* just reopen */
c2b27bf2 12668+ || !d_unhashed(cpg.dentry) /* copyup and reopen */
027c5e7a 12669+ ) {
392086de 12670+ h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
86dc4139 12671+ if (IS_ERR(h_file))
027c5e7a 12672+ err = PTR_ERR(h_file);
86dc4139 12673+ else {
027c5e7a 12674+ di_downgrade_lock(parent, AuLock_IR);
5afbbe0d 12675+ if (dbtop > cpg.bdst)
c2b27bf2 12676+ err = au_sio_cpup_simple(&cpg);
027c5e7a
AM
12677+ if (!err)
12678+ err = au_reopen_nondir(file);
c2b27bf2 12679+ au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
027c5e7a 12680+ }
027c5e7a
AM
12681+ } else { /* copyup as wh and reopen */
12682+ /*
12683+ * since writable hfsplus branch is not supported,
12684+ * h_open_pre/post() are unnecessary.
12685+ */
c2b27bf2 12686+ err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
4a4d8108 12687+ di_downgrade_lock(parent, AuLock_IR);
4a4d8108 12688+ }
4a4d8108
AM
12689+
12690+ if (!err) {
12691+ au_pin_set_parent_lflag(pin, /*lflag*/0);
12692+ goto out_dput; /* success */
12693+ }
12694+ au_unpin(pin);
12695+ goto out_unlock;
1facf9fc 12696+
4f0767ce 12697+out_dgrade:
4a4d8108 12698+ di_downgrade_lock(parent, AuLock_IR);
4f0767ce 12699+out_unlock:
4a4d8108 12700+ di_read_unlock(parent, AuLock_IR);
4f0767ce 12701+out_dput:
4a4d8108 12702+ dput(parent);
4f0767ce 12703+out:
1facf9fc 12704+ return err;
12705+}
12706+
4a4d8108
AM
12707+/* ---------------------------------------------------------------------- */
12708+
12709+int au_do_flush(struct file *file, fl_owner_t id,
12710+ int (*flush)(struct file *file, fl_owner_t id))
1facf9fc 12711+{
4a4d8108 12712+ int err;
1facf9fc 12713+ struct super_block *sb;
4a4d8108 12714+ struct inode *inode;
1facf9fc 12715+
c06a8ce3
AM
12716+ inode = file_inode(file);
12717+ sb = inode->i_sb;
4a4d8108
AM
12718+ si_noflush_read_lock(sb);
12719+ fi_read_lock(file);
b752ccd1 12720+ ii_read_lock_child(inode);
1facf9fc 12721+
4a4d8108
AM
12722+ err = flush(file, id);
12723+ au_cpup_attr_timesizes(inode);
1facf9fc 12724+
b752ccd1 12725+ ii_read_unlock(inode);
4a4d8108 12726+ fi_read_unlock(file);
1308ab2a 12727+ si_read_unlock(sb);
dece6358 12728+ return err;
1facf9fc 12729+}
12730+
4a4d8108
AM
12731+/* ---------------------------------------------------------------------- */
12732+
12733+static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
1facf9fc 12734+{
4a4d8108 12735+ int err;
4a4d8108
AM
12736+ struct au_pin pin;
12737+ struct au_finfo *finfo;
c2b27bf2 12738+ struct dentry *parent, *hi_wh;
4a4d8108 12739+ struct inode *inode;
1facf9fc 12740+ struct super_block *sb;
c2b27bf2 12741+ struct au_cp_generic cpg = {
2000de60 12742+ .dentry = file->f_path.dentry,
c2b27bf2
AM
12743+ .bdst = -1,
12744+ .bsrc = -1,
12745+ .len = -1,
12746+ .pin = &pin,
12747+ .flags = AuCpup_DTIME
12748+ };
1facf9fc 12749+
4a4d8108
AM
12750+ FiMustWriteLock(file);
12751+
12752+ err = 0;
12753+ finfo = au_fi(file);
c2b27bf2 12754+ sb = cpg.dentry->d_sb;
5527c038 12755+ inode = d_inode(cpg.dentry);
5afbbe0d 12756+ cpg.bdst = au_ibtop(inode);
c2b27bf2 12757+ if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
1308ab2a 12758+ goto out;
dece6358 12759+
c2b27bf2
AM
12760+ parent = dget_parent(cpg.dentry);
12761+ if (au_test_ro(sb, cpg.bdst, inode)) {
4a4d8108 12762+ di_read_lock_parent(parent, !AuLock_IR);
c2b27bf2
AM
12763+ err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
12764+ cpg.bdst = err;
4a4d8108
AM
12765+ di_read_unlock(parent, !AuLock_IR);
12766+ if (unlikely(err < 0))
12767+ goto out_parent;
12768+ err = 0;
1facf9fc 12769+ }
1facf9fc 12770+
4a4d8108 12771+ di_read_lock_parent(parent, AuLock_IR);
c2b27bf2 12772+ hi_wh = au_hi_wh(inode, cpg.bdst);
7f207e10
AM
12773+ if (!S_ISDIR(inode->i_mode)
12774+ && au_opt_test(au_mntflags(sb), PLINK)
4a4d8108 12775+ && au_plink_test(inode)
c2b27bf2 12776+ && !d_unhashed(cpg.dentry)
5afbbe0d 12777+ && cpg.bdst < au_dbtop(cpg.dentry)) {
c2b27bf2 12778+ err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
4a4d8108
AM
12779+ if (unlikely(err))
12780+ goto out_unlock;
12781+
12782+ /* always superio. */
c2b27bf2 12783+ err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
4a4d8108 12784+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 12785+ if (!err) {
c2b27bf2 12786+ err = au_sio_cpup_simple(&cpg);
367653fa
AM
12787+ au_unpin(&pin);
12788+ }
4a4d8108
AM
12789+ } else if (hi_wh) {
12790+ /* already copied-up after unlink */
c2b27bf2 12791+ err = au_reopen_wh(file, cpg.bdst, hi_wh);
4a4d8108
AM
12792+ *need_reopen = 0;
12793+ }
1facf9fc 12794+
4f0767ce 12795+out_unlock:
4a4d8108 12796+ di_read_unlock(parent, AuLock_IR);
4f0767ce 12797+out_parent:
4a4d8108 12798+ dput(parent);
4f0767ce 12799+out:
1308ab2a 12800+ return err;
dece6358 12801+}
1facf9fc 12802+
4a4d8108 12803+static void au_do_refresh_dir(struct file *file)
dece6358 12804+{
5afbbe0d 12805+ aufs_bindex_t bindex, bbot, new_bindex, brid;
4a4d8108
AM
12806+ struct au_hfile *p, tmp, *q;
12807+ struct au_finfo *finfo;
1308ab2a 12808+ struct super_block *sb;
4a4d8108 12809+ struct au_fidir *fidir;
1facf9fc 12810+
4a4d8108 12811+ FiMustWriteLock(file);
1facf9fc 12812+
2000de60 12813+ sb = file->f_path.dentry->d_sb;
4a4d8108
AM
12814+ finfo = au_fi(file);
12815+ fidir = finfo->fi_hdir;
12816+ AuDebugOn(!fidir);
12817+ p = fidir->fd_hfile + finfo->fi_btop;
12818+ brid = p->hf_br->br_id;
5afbbe0d
AM
12819+ bbot = fidir->fd_bbot;
12820+ for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
4a4d8108
AM
12821+ if (!p->hf_file)
12822+ continue;
1308ab2a 12823+
4a4d8108
AM
12824+ new_bindex = au_br_index(sb, p->hf_br->br_id);
12825+ if (new_bindex == bindex)
12826+ continue;
12827+ if (new_bindex < 0) {
12828+ au_set_h_fptr(file, bindex, NULL);
12829+ continue;
12830+ }
1308ab2a 12831+
4a4d8108
AM
12832+ /* swap two lower inode, and loop again */
12833+ q = fidir->fd_hfile + new_bindex;
12834+ tmp = *q;
12835+ *q = *p;
12836+ *p = tmp;
12837+ if (tmp.hf_file) {
12838+ bindex--;
12839+ p--;
12840+ }
12841+ }
1308ab2a 12842+
4a4d8108 12843+ p = fidir->fd_hfile;
2000de60 12844+ if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
5afbbe0d
AM
12845+ bbot = au_sbbot(sb);
12846+ for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
4a4d8108
AM
12847+ finfo->fi_btop++, p++)
12848+ if (p->hf_file) {
c06a8ce3 12849+ if (file_inode(p->hf_file))
4a4d8108 12850+ break;
c1595e42 12851+ au_hfput(p, file);
4a4d8108
AM
12852+ }
12853+ } else {
5afbbe0d
AM
12854+ bbot = au_br_index(sb, brid);
12855+ for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
4a4d8108
AM
12856+ finfo->fi_btop++, p++)
12857+ if (p->hf_file)
12858+ au_hfput(p, file);
5afbbe0d 12859+ bbot = au_sbbot(sb);
4a4d8108 12860+ }
1308ab2a 12861+
5afbbe0d
AM
12862+ p = fidir->fd_hfile + bbot;
12863+ for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
4a4d8108
AM
12864+ fidir->fd_bbot--, p--)
12865+ if (p->hf_file) {
c06a8ce3 12866+ if (file_inode(p->hf_file))
4a4d8108 12867+ break;
c1595e42 12868+ au_hfput(p, file);
4a4d8108
AM
12869+ }
12870+ AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
1308ab2a 12871+}
12872+
4a4d8108
AM
12873+/*
12874+ * after branch manipulating, refresh the file.
12875+ */
12876+static int refresh_file(struct file *file, int (*reopen)(struct file *file))
1facf9fc 12877+{
4a4d8108 12878+ int err, need_reopen;
5afbbe0d 12879+ aufs_bindex_t bbot, bindex;
4a4d8108 12880+ struct dentry *dentry;
1308ab2a 12881+ struct au_finfo *finfo;
4a4d8108 12882+ struct au_hfile *hfile;
1facf9fc 12883+
2000de60 12884+ dentry = file->f_path.dentry;
1308ab2a 12885+ finfo = au_fi(file);
4a4d8108
AM
12886+ if (!finfo->fi_hdir) {
12887+ hfile = &finfo->fi_htop;
12888+ AuDebugOn(!hfile->hf_file);
12889+ bindex = au_br_index(dentry->d_sb, hfile->hf_br->br_id);
12890+ AuDebugOn(bindex < 0);
12891+ if (bindex != finfo->fi_btop)
5afbbe0d 12892+ au_set_fbtop(file, bindex);
4a4d8108 12893+ } else {
5afbbe0d 12894+ err = au_fidir_realloc(finfo, au_sbbot(dentry->d_sb) + 1);
4a4d8108
AM
12895+ if (unlikely(err))
12896+ goto out;
12897+ au_do_refresh_dir(file);
12898+ }
1facf9fc 12899+
4a4d8108
AM
12900+ err = 0;
12901+ need_reopen = 1;
12902+ if (!au_test_mmapped(file))
12903+ err = au_file_refresh_by_inode(file, &need_reopen);
027c5e7a 12904+ if (!err && need_reopen && !d_unlinked(dentry))
4a4d8108
AM
12905+ err = reopen(file);
12906+ if (!err) {
12907+ au_update_figen(file);
12908+ goto out; /* success */
12909+ }
12910+
12911+ /* error, close all lower files */
12912+ if (finfo->fi_hdir) {
5afbbe0d
AM
12913+ bbot = au_fbbot_dir(file);
12914+ for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
4a4d8108
AM
12915+ au_set_h_fptr(file, bindex, NULL);
12916+ }
1facf9fc 12917+
4f0767ce 12918+out:
1facf9fc 12919+ return err;
12920+}
12921+
4a4d8108
AM
12922+/* common function to regular file and dir */
12923+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
12924+ int wlock)
dece6358 12925+{
1308ab2a 12926+ int err;
4a4d8108 12927+ unsigned int sigen, figen;
5afbbe0d 12928+ aufs_bindex_t btop;
4a4d8108
AM
12929+ unsigned char pseudo_link;
12930+ struct dentry *dentry;
12931+ struct inode *inode;
1facf9fc 12932+
4a4d8108 12933+ err = 0;
2000de60 12934+ dentry = file->f_path.dentry;
5527c038 12935+ inode = d_inode(dentry);
4a4d8108
AM
12936+ sigen = au_sigen(dentry->d_sb);
12937+ fi_write_lock(file);
12938+ figen = au_figen(file);
12939+ di_write_lock_child(dentry);
5afbbe0d
AM
12940+ btop = au_dbtop(dentry);
12941+ pseudo_link = (btop != au_ibtop(inode));
12942+ if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
4a4d8108
AM
12943+ if (!wlock) {
12944+ di_downgrade_lock(dentry, AuLock_IR);
12945+ fi_downgrade_lock(file);
12946+ }
12947+ goto out; /* success */
12948+ }
dece6358 12949+
4a4d8108 12950+ AuDbg("sigen %d, figen %d\n", sigen, figen);
027c5e7a 12951+ if (au_digen_test(dentry, sigen)) {
4a4d8108 12952+ err = au_reval_dpath(dentry, sigen);
027c5e7a 12953+ AuDebugOn(!err && au_digen_test(dentry, sigen));
4a4d8108 12954+ }
dece6358 12955+
027c5e7a
AM
12956+ if (!err)
12957+ err = refresh_file(file, reopen);
4a4d8108
AM
12958+ if (!err) {
12959+ if (!wlock) {
12960+ di_downgrade_lock(dentry, AuLock_IR);
12961+ fi_downgrade_lock(file);
12962+ }
12963+ } else {
12964+ di_write_unlock(dentry);
12965+ fi_write_unlock(file);
12966+ }
1facf9fc 12967+
4f0767ce 12968+out:
1308ab2a 12969+ return err;
12970+}
1facf9fc 12971+
4a4d8108
AM
12972+/* ---------------------------------------------------------------------- */
12973+
12974+/* cf. aufs_nopage() */
12975+/* for madvise(2) */
12976+static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
1308ab2a 12977+{
4a4d8108
AM
12978+ unlock_page(page);
12979+ return 0;
12980+}
1facf9fc 12981+
4a4d8108 12982+/* it will never be called, but necessary to support O_DIRECT */
5afbbe0d 12983+static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4a4d8108 12984+{ BUG(); return 0; }
1facf9fc 12985+
4a4d8108
AM
12986+/* they will never be called. */
12987+#ifdef CONFIG_AUFS_DEBUG
12988+static int aufs_write_begin(struct file *file, struct address_space *mapping,
12989+ loff_t pos, unsigned len, unsigned flags,
12990+ struct page **pagep, void **fsdata)
12991+{ AuUnsupport(); return 0; }
12992+static int aufs_write_end(struct file *file, struct address_space *mapping,
12993+ loff_t pos, unsigned len, unsigned copied,
12994+ struct page *page, void *fsdata)
12995+{ AuUnsupport(); return 0; }
12996+static int aufs_writepage(struct page *page, struct writeback_control *wbc)
12997+{ AuUnsupport(); return 0; }
1308ab2a 12998+
4a4d8108
AM
12999+static int aufs_set_page_dirty(struct page *page)
13000+{ AuUnsupport(); return 0; }
392086de
AM
13001+static void aufs_invalidatepage(struct page *page, unsigned int offset,
13002+ unsigned int length)
4a4d8108
AM
13003+{ AuUnsupport(); }
13004+static int aufs_releasepage(struct page *page, gfp_t gfp)
13005+{ AuUnsupport(); return 0; }
79b8bda9 13006+#if 0 /* called by memory compaction regardless file */
4a4d8108 13007+static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
7eafdf33 13008+ struct page *page, enum migrate_mode mode)
4a4d8108 13009+{ AuUnsupport(); return 0; }
79b8bda9 13010+#endif
4a4d8108
AM
13011+static int aufs_launder_page(struct page *page)
13012+{ AuUnsupport(); return 0; }
13013+static int aufs_is_partially_uptodate(struct page *page,
38d290e6
JR
13014+ unsigned long from,
13015+ unsigned long count)
4a4d8108 13016+{ AuUnsupport(); return 0; }
392086de
AM
13017+static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
13018+ bool *writeback)
13019+{ AuUnsupport(); }
4a4d8108
AM
13020+static int aufs_error_remove_page(struct address_space *mapping,
13021+ struct page *page)
13022+{ AuUnsupport(); return 0; }
b4510431
AM
13023+static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
13024+ sector_t *span)
13025+{ AuUnsupport(); return 0; }
13026+static void aufs_swap_deactivate(struct file *file)
13027+{ AuUnsupport(); }
4a4d8108
AM
13028+#endif /* CONFIG_AUFS_DEBUG */
13029+
13030+const struct address_space_operations aufs_aop = {
13031+ .readpage = aufs_readpage,
13032+ .direct_IO = aufs_direct_IO,
4a4d8108
AM
13033+#ifdef CONFIG_AUFS_DEBUG
13034+ .writepage = aufs_writepage,
4a4d8108
AM
13035+ /* no writepages, because of writepage */
13036+ .set_page_dirty = aufs_set_page_dirty,
13037+ /* no readpages, because of readpage */
13038+ .write_begin = aufs_write_begin,
13039+ .write_end = aufs_write_end,
13040+ /* no bmap, no block device */
13041+ .invalidatepage = aufs_invalidatepage,
13042+ .releasepage = aufs_releasepage,
79b8bda9
AM
13043+ /* is fallback_migrate_page ok? */
13044+ /* .migratepage = aufs_migratepage, */
4a4d8108
AM
13045+ .launder_page = aufs_launder_page,
13046+ .is_partially_uptodate = aufs_is_partially_uptodate,
392086de 13047+ .is_dirty_writeback = aufs_is_dirty_writeback,
b4510431
AM
13048+ .error_remove_page = aufs_error_remove_page,
13049+ .swap_activate = aufs_swap_activate,
13050+ .swap_deactivate = aufs_swap_deactivate
4a4d8108 13051+#endif /* CONFIG_AUFS_DEBUG */
dece6358 13052+};
7f207e10
AM
13053diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
13054--- /usr/share/empty/fs/aufs/file.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 13055+++ linux/fs/aufs/file.h 2016-07-25 19:05:34.814493242 +0200
b912730e 13056@@ -0,0 +1,291 @@
4a4d8108 13057+/*
8cdd5066 13058+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
13059+ *
13060+ * This program, aufs is free software; you can redistribute it and/or modify
13061+ * it under the terms of the GNU General Public License as published by
13062+ * the Free Software Foundation; either version 2 of the License, or
13063+ * (at your option) any later version.
13064+ *
13065+ * This program is distributed in the hope that it will be useful,
13066+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13067+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13068+ * GNU General Public License for more details.
13069+ *
13070+ * You should have received a copy of the GNU General Public License
523b37e3 13071+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 13072+ */
1facf9fc 13073+
4a4d8108
AM
13074+/*
13075+ * file operations
13076+ */
1facf9fc 13077+
4a4d8108
AM
13078+#ifndef __AUFS_FILE_H__
13079+#define __AUFS_FILE_H__
1facf9fc 13080+
4a4d8108 13081+#ifdef __KERNEL__
1facf9fc 13082+
2cbb1c4b 13083+#include <linux/file.h>
4a4d8108
AM
13084+#include <linux/fs.h>
13085+#include <linux/poll.h>
4a4d8108 13086+#include "rwsem.h"
1facf9fc 13087+
4a4d8108
AM
13088+struct au_branch;
13089+struct au_hfile {
13090+ struct file *hf_file;
13091+ struct au_branch *hf_br;
13092+};
1facf9fc 13093+
4a4d8108
AM
13094+struct au_vdir;
13095+struct au_fidir {
13096+ aufs_bindex_t fd_bbot;
13097+ aufs_bindex_t fd_nent;
13098+ struct au_vdir *fd_vdir_cache;
13099+ struct au_hfile fd_hfile[];
13100+};
1facf9fc 13101+
4a4d8108 13102+static inline int au_fidir_sz(int nent)
dece6358 13103+{
4f0767ce
JR
13104+ AuDebugOn(nent < 0);
13105+ return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
4a4d8108 13106+}
1facf9fc 13107+
4a4d8108
AM
13108+struct au_finfo {
13109+ atomic_t fi_generation;
dece6358 13110+
4a4d8108
AM
13111+ struct au_rwsem fi_rwsem;
13112+ aufs_bindex_t fi_btop;
13113+
13114+ /* do not union them */
13115+ struct { /* for non-dir */
13116+ struct au_hfile fi_htop;
2cbb1c4b 13117+ atomic_t fi_mmapped;
4a4d8108
AM
13118+ };
13119+ struct au_fidir *fi_hdir; /* for dir only */
523b37e3
AM
13120+
13121+ struct hlist_node fi_hlist;
13122+ struct file *fi_file; /* very ugly */
4a4d8108 13123+} ____cacheline_aligned_in_smp;
1facf9fc 13124+
4a4d8108 13125+/* ---------------------------------------------------------------------- */
1facf9fc 13126+
4a4d8108
AM
13127+/* file.c */
13128+extern const struct address_space_operations aufs_aop;
13129+unsigned int au_file_roflags(unsigned int flags);
13130+struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
392086de 13131+ struct file *file, int force_wr);
b912730e
AM
13132+struct au_do_open_args {
13133+ int no_lock;
13134+ int (*open)(struct file *file, int flags,
13135+ struct file *h_file);
13136+ struct au_fidir *fidir;
13137+ struct file *h_file;
13138+};
13139+int au_do_open(struct file *file, struct au_do_open_args *args);
4a4d8108
AM
13140+int au_reopen_nondir(struct file *file);
13141+struct au_pin;
13142+int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
13143+int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
13144+ int wlock);
13145+int au_do_flush(struct file *file, fl_owner_t id,
13146+ int (*flush)(struct file *file, fl_owner_t id));
1facf9fc 13147+
4a4d8108
AM
13148+/* poll.c */
13149+#ifdef CONFIG_AUFS_POLL
13150+unsigned int aufs_poll(struct file *file, poll_table *wait);
13151+#endif
1facf9fc 13152+
4a4d8108
AM
13153+#ifdef CONFIG_AUFS_BR_HFSPLUS
13154+/* hfsplus.c */
392086de
AM
13155+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
13156+ int force_wr);
4a4d8108
AM
13157+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
13158+ struct file *h_file);
13159+#else
c1595e42
JR
13160+AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
13161+ aufs_bindex_t bindex, int force_wr)
4a4d8108
AM
13162+AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
13163+ struct file *h_file);
13164+#endif
1facf9fc 13165+
4a4d8108
AM
13166+/* f_op.c */
13167+extern const struct file_operations aufs_file_fop;
b912730e 13168+int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
4a4d8108 13169+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
b912730e 13170+struct file *au_read_pre(struct file *file, int keep_fi);
4a4d8108 13171+
4a4d8108
AM
13172+/* finfo.c */
13173+void au_hfput(struct au_hfile *hf, struct file *file);
13174+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
13175+ struct file *h_file);
1facf9fc 13176+
4a4d8108 13177+void au_update_figen(struct file *file);
4a4d8108
AM
13178+struct au_fidir *au_fidir_alloc(struct super_block *sb);
13179+int au_fidir_realloc(struct au_finfo *finfo, int nbr);
1facf9fc 13180+
4a4d8108
AM
13181+void au_fi_init_once(void *_fi);
13182+void au_finfo_fin(struct file *file);
13183+int au_finfo_init(struct file *file, struct au_fidir *fidir);
1facf9fc 13184+
4a4d8108
AM
13185+/* ioctl.c */
13186+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
b752ccd1
AM
13187+#ifdef CONFIG_COMPAT
13188+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
13189+ unsigned long arg);
c2b27bf2
AM
13190+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
13191+ unsigned long arg);
b752ccd1 13192+#endif
1facf9fc 13193+
4a4d8108 13194+/* ---------------------------------------------------------------------- */
1facf9fc 13195+
4a4d8108
AM
13196+static inline struct au_finfo *au_fi(struct file *file)
13197+{
38d290e6 13198+ return file->private_data;
4a4d8108 13199+}
1facf9fc 13200+
4a4d8108 13201+/* ---------------------------------------------------------------------- */
1facf9fc 13202+
4a4d8108
AM
13203+/*
13204+ * fi_read_lock, fi_write_lock,
13205+ * fi_read_unlock, fi_write_unlock, fi_downgrade_lock
13206+ */
13207+AuSimpleRwsemFuncs(fi, struct file *f, &au_fi(f)->fi_rwsem);
1308ab2a 13208+
4a4d8108
AM
13209+#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
13210+#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
13211+#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
1facf9fc 13212+
1308ab2a 13213+/* ---------------------------------------------------------------------- */
13214+
4a4d8108 13215+/* todo: hard/soft set? */
5afbbe0d 13216+static inline aufs_bindex_t au_fbtop(struct file *file)
dece6358 13217+{
4a4d8108
AM
13218+ FiMustAnyLock(file);
13219+ return au_fi(file)->fi_btop;
13220+}
dece6358 13221+
5afbbe0d 13222+static inline aufs_bindex_t au_fbbot_dir(struct file *file)
4a4d8108
AM
13223+{
13224+ FiMustAnyLock(file);
13225+ AuDebugOn(!au_fi(file)->fi_hdir);
13226+ return au_fi(file)->fi_hdir->fd_bbot;
13227+}
1facf9fc 13228+
4a4d8108
AM
13229+static inline struct au_vdir *au_fvdir_cache(struct file *file)
13230+{
13231+ FiMustAnyLock(file);
13232+ AuDebugOn(!au_fi(file)->fi_hdir);
13233+ return au_fi(file)->fi_hdir->fd_vdir_cache;
13234+}
1facf9fc 13235+
5afbbe0d 13236+static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
13237+{
13238+ FiMustWriteLock(file);
13239+ au_fi(file)->fi_btop = bindex;
13240+}
1facf9fc 13241+
5afbbe0d 13242+static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
4a4d8108
AM
13243+{
13244+ FiMustWriteLock(file);
13245+ AuDebugOn(!au_fi(file)->fi_hdir);
13246+ au_fi(file)->fi_hdir->fd_bbot = bindex;
13247+}
1308ab2a 13248+
4a4d8108
AM
13249+static inline void au_set_fvdir_cache(struct file *file,
13250+ struct au_vdir *vdir_cache)
13251+{
13252+ FiMustWriteLock(file);
13253+ AuDebugOn(!au_fi(file)->fi_hdir);
13254+ au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
13255+}
dece6358 13256+
4a4d8108
AM
13257+static inline struct file *au_hf_top(struct file *file)
13258+{
13259+ FiMustAnyLock(file);
13260+ AuDebugOn(au_fi(file)->fi_hdir);
13261+ return au_fi(file)->fi_htop.hf_file;
13262+}
1facf9fc 13263+
4a4d8108
AM
13264+static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
13265+{
13266+ FiMustAnyLock(file);
13267+ AuDebugOn(!au_fi(file)->fi_hdir);
13268+ return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
dece6358
AM
13269+}
13270+
4a4d8108
AM
13271+/* todo: memory barrier? */
13272+static inline unsigned int au_figen(struct file *f)
dece6358 13273+{
4a4d8108
AM
13274+ return atomic_read(&au_fi(f)->fi_generation);
13275+}
dece6358 13276+
2cbb1c4b
JR
13277+static inline void au_set_mmapped(struct file *f)
13278+{
13279+ if (atomic_inc_return(&au_fi(f)->fi_mmapped))
13280+ return;
0c3ec466 13281+ pr_warn("fi_mmapped wrapped around\n");
2cbb1c4b
JR
13282+ while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
13283+ ;
13284+}
13285+
13286+static inline void au_unset_mmapped(struct file *f)
13287+{
13288+ atomic_dec(&au_fi(f)->fi_mmapped);
13289+}
13290+
4a4d8108
AM
13291+static inline int au_test_mmapped(struct file *f)
13292+{
2cbb1c4b
JR
13293+ return atomic_read(&au_fi(f)->fi_mmapped);
13294+}
13295+
13296+/* customize vma->vm_file */
13297+
13298+static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
13299+ struct file *file)
13300+{
53392da6
AM
13301+ struct file *f;
13302+
13303+ f = vma->vm_file;
2cbb1c4b
JR
13304+ get_file(file);
13305+ vma->vm_file = file;
53392da6 13306+ fput(f);
2cbb1c4b
JR
13307+}
13308+
13309+#ifdef CONFIG_MMU
13310+#define AuDbgVmRegion(file, vma) do {} while (0)
13311+
13312+static inline void au_vm_file_reset(struct vm_area_struct *vma,
13313+ struct file *file)
13314+{
13315+ au_do_vm_file_reset(vma, file);
13316+}
13317+#else
13318+#define AuDbgVmRegion(file, vma) \
13319+ AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
13320+
13321+static inline void au_vm_file_reset(struct vm_area_struct *vma,
13322+ struct file *file)
13323+{
53392da6
AM
13324+ struct file *f;
13325+
2cbb1c4b 13326+ au_do_vm_file_reset(vma, file);
53392da6 13327+ f = vma->vm_region->vm_file;
2cbb1c4b
JR
13328+ get_file(file);
13329+ vma->vm_region->vm_file = file;
53392da6 13330+ fput(f);
2cbb1c4b
JR
13331+}
13332+#endif /* CONFIG_MMU */
13333+
13334+/* handle vma->vm_prfile */
fb47a38f 13335+static inline void au_vm_prfile_set(struct vm_area_struct *vma,
2cbb1c4b
JR
13336+ struct file *file)
13337+{
2cbb1c4b
JR
13338+ get_file(file);
13339+ vma->vm_prfile = file;
13340+#ifndef CONFIG_MMU
13341+ get_file(file);
13342+ vma->vm_region->vm_prfile = file;
13343+#endif
fb47a38f 13344+}
1308ab2a 13345+
4a4d8108
AM
13346+#endif /* __KERNEL__ */
13347+#endif /* __AUFS_FILE_H__ */
7f207e10
AM
13348diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
13349--- /usr/share/empty/fs/aufs/finfo.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
13350+++ linux/fs/aufs/finfo.c 2016-07-25 19:05:34.814493242 +0200
13351@@ -0,0 +1,149 @@
4a4d8108 13352+/*
8cdd5066 13353+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
13354+ *
13355+ * This program, aufs is free software; you can redistribute it and/or modify
13356+ * it under the terms of the GNU General Public License as published by
13357+ * the Free Software Foundation; either version 2 of the License, or
13358+ * (at your option) any later version.
13359+ *
13360+ * This program is distributed in the hope that it will be useful,
13361+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13362+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13363+ * GNU General Public License for more details.
13364+ *
13365+ * You should have received a copy of the GNU General Public License
523b37e3 13366+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 13367+ */
1308ab2a 13368+
4a4d8108
AM
13369+/*
13370+ * file private data
13371+ */
1facf9fc 13372+
4a4d8108 13373+#include "aufs.h"
1facf9fc 13374+
4a4d8108
AM
13375+void au_hfput(struct au_hfile *hf, struct file *file)
13376+{
13377+ /* todo: direct access f_flags */
2cbb1c4b 13378+ if (vfsub_file_flags(file) & __FMODE_EXEC)
4a4d8108
AM
13379+ allow_write_access(hf->hf_file);
13380+ fput(hf->hf_file);
13381+ hf->hf_file = NULL;
5afbbe0d 13382+ au_br_put(hf->hf_br);
4a4d8108
AM
13383+ hf->hf_br = NULL;
13384+}
1facf9fc 13385+
4a4d8108
AM
13386+void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
13387+{
13388+ struct au_finfo *finfo = au_fi(file);
13389+ struct au_hfile *hf;
13390+ struct au_fidir *fidir;
13391+
13392+ fidir = finfo->fi_hdir;
13393+ if (!fidir) {
13394+ AuDebugOn(finfo->fi_btop != bindex);
13395+ hf = &finfo->fi_htop;
13396+ } else
13397+ hf = fidir->fd_hfile + bindex;
13398+
13399+ if (hf && hf->hf_file)
13400+ au_hfput(hf, file);
13401+ if (val) {
13402+ FiMustWriteLock(file);
b912730e 13403+ AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
4a4d8108 13404+ hf->hf_file = val;
2000de60 13405+ hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
1308ab2a 13406+ }
4a4d8108 13407+}
1facf9fc 13408+
4a4d8108
AM
13409+void au_update_figen(struct file *file)
13410+{
2000de60 13411+ atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
4a4d8108 13412+ /* smp_mb(); */ /* atomic_set */
1facf9fc 13413+}
13414+
4a4d8108
AM
13415+/* ---------------------------------------------------------------------- */
13416+
4a4d8108
AM
13417+struct au_fidir *au_fidir_alloc(struct super_block *sb)
13418+{
13419+ struct au_fidir *fidir;
13420+ int nbr;
13421+
5afbbe0d 13422+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
13423+ if (nbr < 2)
13424+ nbr = 2; /* initial allocate for 2 branches */
13425+ fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
13426+ if (fidir) {
13427+ fidir->fd_bbot = -1;
13428+ fidir->fd_nent = nbr;
4a4d8108
AM
13429+ }
13430+
13431+ return fidir;
13432+}
13433+
13434+int au_fidir_realloc(struct au_finfo *finfo, int nbr)
13435+{
13436+ int err;
13437+ struct au_fidir *fidir, *p;
13438+
13439+ AuRwMustWriteLock(&finfo->fi_rwsem);
13440+ fidir = finfo->fi_hdir;
13441+ AuDebugOn(!fidir);
13442+
13443+ err = -ENOMEM;
13444+ p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
13445+ GFP_NOFS);
13446+ if (p) {
13447+ p->fd_nent = nbr;
13448+ finfo->fi_hdir = p;
13449+ err = 0;
13450+ }
1facf9fc 13451+
dece6358 13452+ return err;
1facf9fc 13453+}
1308ab2a 13454+
13455+/* ---------------------------------------------------------------------- */
13456+
4a4d8108 13457+void au_finfo_fin(struct file *file)
1308ab2a 13458+{
4a4d8108
AM
13459+ struct au_finfo *finfo;
13460+
2000de60 13461+ au_nfiles_dec(file->f_path.dentry->d_sb);
7f207e10 13462+
4a4d8108
AM
13463+ finfo = au_fi(file);
13464+ AuDebugOn(finfo->fi_hdir);
13465+ AuRwDestroy(&finfo->fi_rwsem);
13466+ au_cache_free_finfo(finfo);
1308ab2a 13467+}
1308ab2a 13468+
e49829fe 13469+void au_fi_init_once(void *_finfo)
4a4d8108 13470+{
e49829fe 13471+ struct au_finfo *finfo = _finfo;
1308ab2a 13472+
e49829fe 13473+ au_rw_init(&finfo->fi_rwsem);
4a4d8108 13474+}
1308ab2a 13475+
4a4d8108
AM
13476+int au_finfo_init(struct file *file, struct au_fidir *fidir)
13477+{
1716fcea 13478+ int err;
4a4d8108
AM
13479+ struct au_finfo *finfo;
13480+ struct dentry *dentry;
13481+
13482+ err = -ENOMEM;
2000de60 13483+ dentry = file->f_path.dentry;
4a4d8108
AM
13484+ finfo = au_cache_alloc_finfo();
13485+ if (unlikely(!finfo))
13486+ goto out;
13487+
13488+ err = 0;
7f207e10 13489+ au_nfiles_inc(dentry->d_sb);
4a4d8108
AM
13490+ au_rw_write_lock(&finfo->fi_rwsem);
13491+ finfo->fi_btop = -1;
13492+ finfo->fi_hdir = fidir;
13493+ atomic_set(&finfo->fi_generation, au_digen(dentry));
13494+ /* smp_mb(); */ /* atomic_set */
13495+
13496+ file->private_data = finfo;
13497+
13498+out:
13499+ return err;
13500+}
7f207e10
AM
13501diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
13502--- /usr/share/empty/fs/aufs/f_op.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 13503+++ linux/fs/aufs/f_op.c 2016-07-25 19:05:34.811159821 +0200
febd17d6 13504@@ -0,0 +1,770 @@
dece6358 13505+/*
8cdd5066 13506+ * Copyright (C) 2005-2016 Junjiro R. Okajima
dece6358
AM
13507+ *
13508+ * This program, aufs is free software; you can redistribute it and/or modify
13509+ * it under the terms of the GNU General Public License as published by
13510+ * the Free Software Foundation; either version 2 of the License, or
13511+ * (at your option) any later version.
13512+ *
13513+ * This program is distributed in the hope that it will be useful,
13514+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13515+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13516+ * GNU General Public License for more details.
13517+ *
13518+ * You should have received a copy of the GNU General Public License
523b37e3 13519+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 13520+ */
1facf9fc 13521+
13522+/*
4a4d8108 13523+ * file and vm operations
1facf9fc 13524+ */
dece6358 13525+
86dc4139 13526+#include <linux/aio.h>
4a4d8108
AM
13527+#include <linux/fs_stack.h>
13528+#include <linux/mman.h>
4a4d8108 13529+#include <linux/security.h>
dece6358
AM
13530+#include "aufs.h"
13531+
b912730e 13532+int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
1facf9fc 13533+{
4a4d8108
AM
13534+ int err;
13535+ aufs_bindex_t bindex;
8cdd5066 13536+ struct dentry *dentry, *h_dentry;
4a4d8108 13537+ struct au_finfo *finfo;
38d290e6 13538+ struct inode *h_inode;
4a4d8108
AM
13539+
13540+ FiMustWriteLock(file);
13541+
523b37e3 13542+ err = 0;
2000de60 13543+ dentry = file->f_path.dentry;
b912730e 13544+ AuDebugOn(IS_ERR_OR_NULL(dentry));
4a4d8108
AM
13545+ finfo = au_fi(file);
13546+ memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
2cbb1c4b 13547+ atomic_set(&finfo->fi_mmapped, 0);
5afbbe0d 13548+ bindex = au_dbtop(dentry);
8cdd5066
JR
13549+ if (!h_file) {
13550+ h_dentry = au_h_dptr(dentry, bindex);
13551+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13552+ if (unlikely(err))
13553+ goto out;
b912730e 13554+ h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
8cdd5066
JR
13555+ } else {
13556+ h_dentry = h_file->f_path.dentry;
13557+ err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
13558+ if (unlikely(err))
13559+ goto out;
b912730e 13560+ get_file(h_file);
8cdd5066 13561+ }
4a4d8108
AM
13562+ if (IS_ERR(h_file))
13563+ err = PTR_ERR(h_file);
13564+ else {
38d290e6
JR
13565+ if ((flags & __O_TMPFILE)
13566+ && !(flags & O_EXCL)) {
13567+ h_inode = file_inode(h_file);
13568+ spin_lock(&h_inode->i_lock);
13569+ h_inode->i_state |= I_LINKABLE;
13570+ spin_unlock(&h_inode->i_lock);
13571+ }
5afbbe0d 13572+ au_set_fbtop(file, bindex);
4a4d8108
AM
13573+ au_set_h_fptr(file, bindex, h_file);
13574+ au_update_figen(file);
13575+ /* todo: necessary? */
13576+ /* file->f_ra = h_file->f_ra; */
13577+ }
027c5e7a 13578+
8cdd5066 13579+out:
4a4d8108 13580+ return err;
1facf9fc 13581+}
13582+
4a4d8108
AM
13583+static int aufs_open_nondir(struct inode *inode __maybe_unused,
13584+ struct file *file)
1facf9fc 13585+{
4a4d8108 13586+ int err;
1308ab2a 13587+ struct super_block *sb;
b912730e
AM
13588+ struct au_do_open_args args = {
13589+ .open = au_do_open_nondir
13590+ };
1facf9fc 13591+
523b37e3
AM
13592+ AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
13593+ file, vfsub_file_flags(file), file->f_mode);
1facf9fc 13594+
2000de60 13595+ sb = file->f_path.dentry->d_sb;
4a4d8108 13596+ si_read_lock(sb, AuLock_FLUSH);
b912730e 13597+ err = au_do_open(file, &args);
4a4d8108
AM
13598+ si_read_unlock(sb);
13599+ return err;
13600+}
1facf9fc 13601+
4a4d8108
AM
13602+int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
13603+{
13604+ struct au_finfo *finfo;
13605+ aufs_bindex_t bindex;
1facf9fc 13606+
4a4d8108 13607+ finfo = au_fi(file);
2000de60
JR
13608+ au_sphl_del(&finfo->fi_hlist,
13609+ &au_sbi(file->f_path.dentry->d_sb)->si_files);
4a4d8108 13610+ bindex = finfo->fi_btop;
b4510431 13611+ if (bindex >= 0)
4a4d8108 13612+ au_set_h_fptr(file, bindex, NULL);
7f207e10 13613+
4a4d8108
AM
13614+ au_finfo_fin(file);
13615+ return 0;
1facf9fc 13616+}
13617+
4a4d8108
AM
13618+/* ---------------------------------------------------------------------- */
13619+
13620+static int au_do_flush_nondir(struct file *file, fl_owner_t id)
dece6358 13621+{
1308ab2a 13622+ int err;
4a4d8108
AM
13623+ struct file *h_file;
13624+
13625+ err = 0;
13626+ h_file = au_hf_top(file);
13627+ if (h_file)
13628+ err = vfsub_flush(h_file, id);
13629+ return err;
13630+}
13631+
13632+static int aufs_flush_nondir(struct file *file, fl_owner_t id)
13633+{
13634+ return au_do_flush(file, id, au_do_flush_nondir);
13635+}
13636+
13637+/* ---------------------------------------------------------------------- */
9dbd164d
AM
13638+/*
13639+ * read and write functions acquire [fdi]_rwsem once, but release before
13640+ * mmap_sem. This is because to stop a race condition between mmap(2).
13641+ * Releasing these aufs-rwsem should be safe, no branch-mamagement (by keeping
13642+ * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
13643+ * read functions after [fdi]_rwsem are released, but it should be harmless.
13644+ */
4a4d8108 13645+
b912730e
AM
13646+/* Callers should call au_read_post() or fput() in the end */
13647+struct file *au_read_pre(struct file *file, int keep_fi)
4a4d8108 13648+{
4a4d8108 13649+ struct file *h_file;
b912730e 13650+ int err;
1facf9fc 13651+
4a4d8108 13652+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0);
b912730e
AM
13653+ if (!err) {
13654+ di_read_unlock(file->f_path.dentry, AuLock_IR);
13655+ h_file = au_hf_top(file);
13656+ get_file(h_file);
13657+ if (!keep_fi)
13658+ fi_read_unlock(file);
13659+ } else
13660+ h_file = ERR_PTR(err);
13661+
13662+ return h_file;
13663+}
13664+
13665+static void au_read_post(struct inode *inode, struct file *h_file)
13666+{
13667+ /* update without lock, I don't think it a problem */
13668+ fsstack_copy_attr_atime(inode, file_inode(h_file));
13669+ fput(h_file);
13670+}
13671+
13672+struct au_write_pre {
13673+ blkcnt_t blks;
5afbbe0d 13674+ aufs_bindex_t btop;
b912730e
AM
13675+};
13676+
13677+/*
13678+ * return with iinfo is write-locked
13679+ * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
13680+ * end
13681+ */
13682+static struct file *au_write_pre(struct file *file, int do_ready,
13683+ struct au_write_pre *wpre)
13684+{
13685+ struct file *h_file;
13686+ struct dentry *dentry;
13687+ int err;
13688+ struct au_pin pin;
13689+
13690+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
13691+ h_file = ERR_PTR(err);
dece6358
AM
13692+ if (unlikely(err))
13693+ goto out;
1facf9fc 13694+
b912730e
AM
13695+ dentry = file->f_path.dentry;
13696+ if (do_ready) {
13697+ err = au_ready_to_write(file, -1, &pin);
13698+ if (unlikely(err)) {
13699+ h_file = ERR_PTR(err);
13700+ di_write_unlock(dentry);
13701+ goto out_fi;
13702+ }
13703+ }
13704+
13705+ di_downgrade_lock(dentry, /*flags*/0);
13706+ if (wpre)
5afbbe0d 13707+ wpre->btop = au_fbtop(file);
4a4d8108 13708+ h_file = au_hf_top(file);
9dbd164d 13709+ get_file(h_file);
b912730e
AM
13710+ if (wpre)
13711+ wpre->blks = file_inode(h_file)->i_blocks;
13712+ if (do_ready)
13713+ au_unpin(&pin);
13714+ di_read_unlock(dentry, /*flags*/0);
13715+
13716+out_fi:
13717+ fi_write_unlock(file);
13718+out:
13719+ return h_file;
13720+}
13721+
13722+static void au_write_post(struct inode *inode, struct file *h_file,
13723+ struct au_write_pre *wpre, ssize_t written)
13724+{
13725+ struct inode *h_inode;
13726+
13727+ au_cpup_attr_timesizes(inode);
5afbbe0d 13728+ AuDebugOn(au_ibtop(inode) != wpre->btop);
b912730e
AM
13729+ h_inode = file_inode(h_file);
13730+ inode->i_mode = h_inode->i_mode;
13731+ ii_write_unlock(inode);
13732+ fput(h_file);
13733+
13734+ /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
13735+ if (written > 0)
5afbbe0d 13736+ au_fhsm_wrote(inode->i_sb, wpre->btop,
b912730e
AM
13737+ /*force*/h_inode->i_blocks > wpre->blks);
13738+}
13739+
13740+static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
13741+ loff_t *ppos)
13742+{
13743+ ssize_t err;
13744+ struct inode *inode;
13745+ struct file *h_file;
13746+ struct super_block *sb;
13747+
13748+ inode = file_inode(file);
13749+ sb = inode->i_sb;
13750+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13751+
13752+ h_file = au_read_pre(file, /*keep_fi*/0);
13753+ err = PTR_ERR(h_file);
13754+ if (IS_ERR(h_file))
13755+ goto out;
9dbd164d
AM
13756+
13757+ /* filedata may be obsoleted by concurrent copyup, but no problem */
4a4d8108
AM
13758+ err = vfsub_read_u(h_file, buf, count, ppos);
13759+ /* todo: necessary? */
13760+ /* file->f_ra = h_file->f_ra; */
b912730e 13761+ au_read_post(inode, h_file);
1308ab2a 13762+
4f0767ce 13763+out:
dece6358
AM
13764+ si_read_unlock(sb);
13765+ return err;
13766+}
1facf9fc 13767+
e49829fe
JR
13768+/*
13769+ * todo: very ugly
13770+ * it locks both of i_mutex and si_rwsem for read in safe.
13771+ * if the plink maintenance mode continues forever (that is the problem),
13772+ * may loop forever.
13773+ */
13774+static void au_mtx_and_read_lock(struct inode *inode)
13775+{
13776+ int err;
13777+ struct super_block *sb = inode->i_sb;
13778+
13779+ while (1) {
febd17d6 13780+ inode_lock(inode);
e49829fe
JR
13781+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
13782+ if (!err)
13783+ break;
febd17d6 13784+ inode_unlock(inode);
e49829fe
JR
13785+ si_read_lock(sb, AuLock_NOPLMW);
13786+ si_read_unlock(sb);
13787+ }
13788+}
13789+
4a4d8108
AM
13790+static ssize_t aufs_write(struct file *file, const char __user *ubuf,
13791+ size_t count, loff_t *ppos)
dece6358 13792+{
4a4d8108 13793+ ssize_t err;
b912730e
AM
13794+ struct au_write_pre wpre;
13795+ struct inode *inode;
4a4d8108
AM
13796+ struct file *h_file;
13797+ char __user *buf = (char __user *)ubuf;
1facf9fc 13798+
b912730e 13799+ inode = file_inode(file);
e49829fe 13800+ au_mtx_and_read_lock(inode);
1facf9fc 13801+
b912730e
AM
13802+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13803+ err = PTR_ERR(h_file);
13804+ if (IS_ERR(h_file))
9dbd164d 13805+ goto out;
9dbd164d 13806+
4a4d8108 13807+ err = vfsub_write_u(h_file, buf, count, ppos);
b912730e 13808+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 13809+
4f0767ce 13810+out:
b912730e 13811+ si_read_unlock(inode->i_sb);
febd17d6 13812+ inode_unlock(inode);
dece6358
AM
13813+ return err;
13814+}
1facf9fc 13815+
076b876e
AM
13816+static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
13817+ struct iov_iter *iov_iter)
dece6358 13818+{
4a4d8108
AM
13819+ ssize_t err;
13820+ struct file *file;
076b876e 13821+ ssize_t (*iter)(struct kiocb *, struct iov_iter *);
1facf9fc 13822+
4a4d8108
AM
13823+ err = security_file_permission(h_file, rw);
13824+ if (unlikely(err))
13825+ goto out;
1facf9fc 13826+
4a4d8108 13827+ err = -ENOSYS;
076b876e 13828+ iter = NULL;
5527c038 13829+ if (rw == MAY_READ)
076b876e 13830+ iter = h_file->f_op->read_iter;
5527c038 13831+ else if (rw == MAY_WRITE)
076b876e 13832+ iter = h_file->f_op->write_iter;
076b876e
AM
13833+
13834+ file = kio->ki_filp;
13835+ kio->ki_filp = h_file;
13836+ if (iter) {
2cbb1c4b 13837+ lockdep_off();
076b876e
AM
13838+ err = iter(kio, iov_iter);
13839+ lockdep_on();
4a4d8108
AM
13840+ } else
13841+ /* currently there is no such fs */
13842+ WARN_ON_ONCE(1);
076b876e 13843+ kio->ki_filp = file;
1facf9fc 13844+
4f0767ce 13845+out:
dece6358
AM
13846+ return err;
13847+}
1facf9fc 13848+
076b876e 13849+static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1facf9fc 13850+{
4a4d8108
AM
13851+ ssize_t err;
13852+ struct file *file, *h_file;
b912730e 13853+ struct inode *inode;
dece6358 13854+ struct super_block *sb;
1facf9fc 13855+
4a4d8108 13856+ file = kio->ki_filp;
b912730e
AM
13857+ inode = file_inode(file);
13858+ sb = inode->i_sb;
e49829fe 13859+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
4a4d8108 13860+
5afbbe0d 13861+ h_file = au_read_pre(file, /*keep_fi*/1);
b912730e
AM
13862+ err = PTR_ERR(h_file);
13863+ if (IS_ERR(h_file))
13864+ goto out;
9dbd164d 13865+
5afbbe0d
AM
13866+ if (au_test_loopback_kthread()) {
13867+ au_warn_loopback(h_file->f_path.dentry->d_sb);
13868+ if (file->f_mapping != h_file->f_mapping) {
13869+ file->f_mapping = h_file->f_mapping;
13870+ smp_mb(); /* unnecessary? */
13871+ }
13872+ }
13873+ fi_read_unlock(file);
13874+
076b876e 13875+ err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
4a4d8108
AM
13876+ /* todo: necessary? */
13877+ /* file->f_ra = h_file->f_ra; */
b912730e 13878+ au_read_post(inode, h_file);
1facf9fc 13879+
4f0767ce 13880+out:
4a4d8108 13881+ si_read_unlock(sb);
1308ab2a 13882+ return err;
13883+}
1facf9fc 13884+
076b876e 13885+static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
1308ab2a 13886+{
4a4d8108 13887+ ssize_t err;
b912730e
AM
13888+ struct au_write_pre wpre;
13889+ struct inode *inode;
4a4d8108 13890+ struct file *file, *h_file;
1308ab2a 13891+
4a4d8108 13892+ file = kio->ki_filp;
b912730e 13893+ inode = file_inode(file);
e49829fe
JR
13894+ au_mtx_and_read_lock(inode);
13895+
b912730e
AM
13896+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13897+ err = PTR_ERR(h_file);
13898+ if (IS_ERR(h_file))
9dbd164d 13899+ goto out;
9dbd164d 13900+
076b876e 13901+ err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
b912730e 13902+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 13903+
4f0767ce 13904+out:
b912730e 13905+ si_read_unlock(inode->i_sb);
febd17d6 13906+ inode_unlock(inode);
dece6358 13907+ return err;
1facf9fc 13908+}
13909+
4a4d8108
AM
13910+static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
13911+ struct pipe_inode_info *pipe, size_t len,
13912+ unsigned int flags)
1facf9fc 13913+{
4a4d8108
AM
13914+ ssize_t err;
13915+ struct file *h_file;
b912730e 13916+ struct inode *inode;
dece6358 13917+ struct super_block *sb;
1facf9fc 13918+
b912730e
AM
13919+ inode = file_inode(file);
13920+ sb = inode->i_sb;
e49829fe 13921+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e 13922+
5afbbe0d 13923+ h_file = au_read_pre(file, /*keep_fi*/0);
b912730e
AM
13924+ err = PTR_ERR(h_file);
13925+ if (IS_ERR(h_file))
dece6358 13926+ goto out;
1facf9fc 13927+
4a4d8108
AM
13928+ err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
13929+ /* todo: necessasry? */
13930+ /* file->f_ra = h_file->f_ra; */
b912730e 13931+ au_read_post(inode, h_file);
1facf9fc 13932+
4f0767ce 13933+out:
4a4d8108 13934+ si_read_unlock(sb);
dece6358 13935+ return err;
1facf9fc 13936+}
13937+
4a4d8108
AM
13938+static ssize_t
13939+aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
13940+ size_t len, unsigned int flags)
1facf9fc 13941+{
4a4d8108 13942+ ssize_t err;
b912730e
AM
13943+ struct au_write_pre wpre;
13944+ struct inode *inode;
076b876e 13945+ struct file *h_file;
1facf9fc 13946+
b912730e 13947+ inode = file_inode(file);
e49829fe 13948+ au_mtx_and_read_lock(inode);
9dbd164d 13949+
b912730e
AM
13950+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13951+ err = PTR_ERR(h_file);
13952+ if (IS_ERR(h_file))
9dbd164d 13953+ goto out;
9dbd164d 13954+
4a4d8108 13955+ err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
b912730e 13956+ au_write_post(inode, h_file, &wpre, err);
1facf9fc 13957+
4f0767ce 13958+out:
b912730e 13959+ si_read_unlock(inode->i_sb);
febd17d6 13960+ inode_unlock(inode);
4a4d8108
AM
13961+ return err;
13962+}
1facf9fc 13963+
38d290e6
JR
13964+static long aufs_fallocate(struct file *file, int mode, loff_t offset,
13965+ loff_t len)
13966+{
13967+ long err;
b912730e 13968+ struct au_write_pre wpre;
38d290e6
JR
13969+ struct inode *inode;
13970+ struct file *h_file;
13971+
b912730e 13972+ inode = file_inode(file);
38d290e6
JR
13973+ au_mtx_and_read_lock(inode);
13974+
b912730e
AM
13975+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
13976+ err = PTR_ERR(h_file);
13977+ if (IS_ERR(h_file))
38d290e6 13978+ goto out;
38d290e6
JR
13979+
13980+ lockdep_off();
03673fb0 13981+ err = vfs_fallocate(h_file, mode, offset, len);
38d290e6 13982+ lockdep_on();
b912730e 13983+ au_write_post(inode, h_file, &wpre, /*written*/1);
38d290e6
JR
13984+
13985+out:
b912730e 13986+ si_read_unlock(inode->i_sb);
febd17d6 13987+ inode_unlock(inode);
38d290e6
JR
13988+ return err;
13989+}
13990+
4a4d8108
AM
13991+/* ---------------------------------------------------------------------- */
13992+
9dbd164d
AM
13993+/*
13994+ * The locking order around current->mmap_sem.
13995+ * - in most and regular cases
13996+ * file I/O syscall -- aufs_read() or something
13997+ * -- si_rwsem for read -- mmap_sem
13998+ * (Note that [fdi]i_rwsem are released before mmap_sem).
13999+ * - in mmap case
14000+ * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
14001+ * This AB-BA order is definitly bad, but is not a problem since "si_rwsem for
14002+ * read" allows muliple processes to acquire it and [fdi]i_rwsem are not held in
14003+ * file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
14004+ * It means that when aufs acquires si_rwsem for write, the process should never
14005+ * acquire mmap_sem.
14006+ *
392086de 14007+ * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
9dbd164d
AM
14008+ * problem either since any directory is not able to be mmap-ed.
14009+ * The similar scenario is applied to aufs_readlink() too.
14010+ */
14011+
38d290e6 14012+#if 0 /* stop calling security_file_mmap() */
2dfbb274
AM
14013+/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
14014+#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
14015+
14016+static unsigned long au_arch_prot_conv(unsigned long flags)
14017+{
14018+ /* currently ppc64 only */
14019+#ifdef CONFIG_PPC64
14020+ /* cf. linux/arch/powerpc/include/asm/mman.h */
14021+ AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
14022+ return AuConv_VM_PROT(flags, SAO);
14023+#else
14024+ AuDebugOn(arch_calc_vm_prot_bits(-1));
14025+ return 0;
14026+#endif
14027+}
14028+
14029+static unsigned long au_prot_conv(unsigned long flags)
14030+{
14031+ return AuConv_VM_PROT(flags, READ)
14032+ | AuConv_VM_PROT(flags, WRITE)
14033+ | AuConv_VM_PROT(flags, EXEC)
14034+ | au_arch_prot_conv(flags);
14035+}
14036+
14037+/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
14038+#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
14039+
14040+static unsigned long au_flag_conv(unsigned long flags)
14041+{
14042+ return AuConv_VM_MAP(flags, GROWSDOWN)
14043+ | AuConv_VM_MAP(flags, DENYWRITE)
2dfbb274
AM
14044+ | AuConv_VM_MAP(flags, LOCKED);
14045+}
38d290e6 14046+#endif
2dfbb274 14047+
9dbd164d 14048+static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
dece6358 14049+{
4a4d8108 14050+ int err;
4a4d8108 14051+ const unsigned char wlock
9dbd164d 14052+ = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
4a4d8108 14053+ struct super_block *sb;
9dbd164d 14054+ struct file *h_file;
b912730e 14055+ struct inode *inode;
9dbd164d
AM
14056+
14057+ AuDbgVmRegion(file, vma);
1308ab2a 14058+
b912730e
AM
14059+ inode = file_inode(file);
14060+ sb = inode->i_sb;
9dbd164d 14061+ lockdep_off();
e49829fe 14062+ si_read_lock(sb, AuLock_NOPLMW);
4a4d8108 14063+
b912730e 14064+ h_file = au_write_pre(file, wlock, /*wpre*/NULL);
9dbd164d 14065+ lockdep_on();
b912730e
AM
14066+ err = PTR_ERR(h_file);
14067+ if (IS_ERR(h_file))
14068+ goto out;
1308ab2a 14069+
b912730e
AM
14070+ err = 0;
14071+ au_set_mmapped(file);
9dbd164d 14072+ au_vm_file_reset(vma, h_file);
38d290e6
JR
14073+ /*
14074+ * we cannot call security_mmap_file() here since it may acquire
14075+ * mmap_sem or i_mutex.
14076+ *
14077+ * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
14078+ * au_flag_conv(vma->vm_flags));
14079+ */
9dbd164d
AM
14080+ if (!err)
14081+ err = h_file->f_op->mmap(h_file, vma);
b912730e
AM
14082+ if (!err) {
14083+ au_vm_prfile_set(vma, file);
14084+ fsstack_copy_attr_atime(inode, file_inode(h_file));
14085+ goto out_fput; /* success */
14086+ }
2cbb1c4b
JR
14087+ au_unset_mmapped(file);
14088+ au_vm_file_reset(vma, file);
b912730e 14089+
2cbb1c4b 14090+out_fput:
9dbd164d 14091+ lockdep_off();
b912730e
AM
14092+ ii_write_unlock(inode);
14093+ lockdep_on();
14094+ fput(h_file);
4f0767ce 14095+out:
b912730e 14096+ lockdep_off();
9dbd164d
AM
14097+ si_read_unlock(sb);
14098+ lockdep_on();
14099+ AuTraceErr(err);
4a4d8108
AM
14100+ return err;
14101+}
14102+
14103+/* ---------------------------------------------------------------------- */
14104+
1e00d052
AM
14105+static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
14106+ int datasync)
4a4d8108
AM
14107+{
14108+ int err;
b912730e 14109+ struct au_write_pre wpre;
4a4d8108
AM
14110+ struct inode *inode;
14111+ struct file *h_file;
4a4d8108
AM
14112+
14113+ err = 0; /* -EBADF; */ /* posix? */
14114+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
b912730e 14115+ goto out;
4a4d8108 14116+
b912730e
AM
14117+ inode = file_inode(file);
14118+ au_mtx_and_read_lock(inode);
14119+
14120+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14121+ err = PTR_ERR(h_file);
14122+ if (IS_ERR(h_file))
4a4d8108 14123+ goto out_unlock;
4a4d8108 14124+
53392da6 14125+ err = vfsub_fsync(h_file, &h_file->f_path, datasync);
b912730e 14126+ au_write_post(inode, h_file, &wpre, /*written*/0);
4a4d8108 14127+
4f0767ce 14128+out_unlock:
b912730e 14129+ si_read_unlock(inode->i_sb);
febd17d6 14130+ inode_unlock(inode);
b912730e 14131+out:
4a4d8108 14132+ return err;
dece6358
AM
14133+}
14134+
4a4d8108
AM
14135+/* no one supports this operation, currently */
14136+#if 0
14137+static int aufs_aio_fsync_nondir(struct kiocb *kio, int datasync)
dece6358 14138+{
4a4d8108 14139+ int err;
b912730e 14140+ struct au_write_pre wpre;
febd17d6 14141+ struct inode *inode, *h_inode;
4a4d8108 14142+ struct file *file, *h_file;
1308ab2a 14143+
4a4d8108
AM
14144+ err = 0; /* -EBADF; */ /* posix? */
14145+ if (unlikely(!(file->f_mode & FMODE_WRITE)))
14146+ goto out;
1308ab2a 14147+
b912730e
AM
14148+ file = kio->ki_filp;
14149+ inode = file_inode(file);
14150+ au_mtx_and_read_lock(inode);
14151+
14152+ h_file = au_write_pre(file, /*do_ready*/1, &wpre);
14153+ err = PTR_ERR(h_file);
14154+ if (IS_ERR(h_file))
4a4d8108 14155+ goto out_unlock;
1308ab2a 14156+
4a4d8108
AM
14157+ err = -ENOSYS;
14158+ h_file = au_hf_top(file);
523b37e3 14159+ if (h_file->f_op->aio_fsync) {
febd17d6 14160+ h_inode = file_inode(h_file);
4a4d8108
AM
14161+ if (!is_sync_kiocb(kio)) {
14162+ get_file(h_file);
14163+ fput(file);
14164+ }
14165+ kio->ki_filp = h_file;
14166+ err = h_file->f_op->aio_fsync(kio, datasync);
febd17d6 14167+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108
AM
14168+ if (!err)
14169+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL);
14170+ /*ignore*/
febd17d6 14171+ inode_unlock(h_inode);
4a4d8108 14172+ }
b912730e 14173+ au_write_post(inode, h_file, &wpre, /*written*/0);
1308ab2a 14174+
4f0767ce 14175+out_unlock:
e49829fe 14176+ si_read_unlock(inode->sb);
febd17d6 14177+ inode_unlock(inode);
b912730e 14178+out:
4a4d8108 14179+ return err;
dece6358 14180+}
4a4d8108 14181+#endif
dece6358 14182+
4a4d8108 14183+static int aufs_fasync(int fd, struct file *file, int flag)
dece6358 14184+{
4a4d8108
AM
14185+ int err;
14186+ struct file *h_file;
4a4d8108 14187+ struct super_block *sb;
1308ab2a 14188+
b912730e 14189+ sb = file->f_path.dentry->d_sb;
e49829fe 14190+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e
AM
14191+
14192+ h_file = au_read_pre(file, /*keep_fi*/0);
14193+ err = PTR_ERR(h_file);
14194+ if (IS_ERR(h_file))
4a4d8108
AM
14195+ goto out;
14196+
523b37e3 14197+ if (h_file->f_op->fasync)
4a4d8108 14198+ err = h_file->f_op->fasync(fd, h_file, flag);
b912730e 14199+ fput(h_file); /* instead of au_read_post() */
1308ab2a 14200+
4f0767ce 14201+out:
4a4d8108 14202+ si_read_unlock(sb);
1308ab2a 14203+ return err;
dece6358 14204+}
4a4d8108 14205+
febd17d6
JR
14206+static int aufs_setfl(struct file *file, unsigned long arg)
14207+{
14208+ int err;
14209+ struct file *h_file;
14210+ struct super_block *sb;
14211+
14212+ sb = file->f_path.dentry->d_sb;
14213+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
14214+
14215+ h_file = au_read_pre(file, /*keep_fi*/0);
14216+ err = PTR_ERR(h_file);
14217+ if (IS_ERR(h_file))
14218+ goto out;
14219+
14220+ arg |= vfsub_file_flags(file) & FASYNC; /* stop calling h_file->fasync */
14221+ err = setfl(/*unused fd*/-1, h_file, arg);
14222+ fput(h_file); /* instead of au_read_post() */
14223+
14224+out:
14225+ si_read_unlock(sb);
14226+ return err;
14227+}
14228+
4a4d8108
AM
14229+/* ---------------------------------------------------------------------- */
14230+
14231+/* no one supports this operation, currently */
14232+#if 0
14233+static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
2000de60 14234+ size_t len, loff_t *pos, int more)
4a4d8108
AM
14235+{
14236+}
14237+#endif
14238+
14239+/* ---------------------------------------------------------------------- */
14240+
14241+const struct file_operations aufs_file_fop = {
14242+ .owner = THIS_MODULE,
2cbb1c4b 14243+
027c5e7a 14244+ .llseek = default_llseek,
4a4d8108
AM
14245+
14246+ .read = aufs_read,
14247+ .write = aufs_write,
076b876e
AM
14248+ .read_iter = aufs_read_iter,
14249+ .write_iter = aufs_write_iter,
14250+
4a4d8108
AM
14251+#ifdef CONFIG_AUFS_POLL
14252+ .poll = aufs_poll,
14253+#endif
14254+ .unlocked_ioctl = aufs_ioctl_nondir,
b752ccd1 14255+#ifdef CONFIG_COMPAT
c2b27bf2 14256+ .compat_ioctl = aufs_compat_ioctl_nondir,
b752ccd1 14257+#endif
4a4d8108
AM
14258+ .mmap = aufs_mmap,
14259+ .open = aufs_open_nondir,
14260+ .flush = aufs_flush_nondir,
14261+ .release = aufs_release_nondir,
14262+ .fsync = aufs_fsync_nondir,
14263+ /* .aio_fsync = aufs_aio_fsync_nondir, */
14264+ .fasync = aufs_fasync,
14265+ /* .sendpage = aufs_sendpage, */
febd17d6 14266+ .setfl = aufs_setfl,
4a4d8108
AM
14267+ .splice_write = aufs_splice_write,
14268+ .splice_read = aufs_splice_read,
14269+#if 0
14270+ .aio_splice_write = aufs_aio_splice_write,
38d290e6 14271+ .aio_splice_read = aufs_aio_splice_read,
4a4d8108 14272+#endif
38d290e6 14273+ .fallocate = aufs_fallocate
4a4d8108 14274+};
7f207e10
AM
14275diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
14276--- /usr/share/empty/fs/aufs/fstype.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 14277+++ linux/fs/aufs/fstype.h 2016-07-25 19:05:34.814493242 +0200
b912730e 14278@@ -0,0 +1,400 @@
4a4d8108 14279+/*
8cdd5066 14280+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
14281+ *
14282+ * This program, aufs is free software; you can redistribute it and/or modify
14283+ * it under the terms of the GNU General Public License as published by
14284+ * the Free Software Foundation; either version 2 of the License, or
14285+ * (at your option) any later version.
14286+ *
14287+ * This program is distributed in the hope that it will be useful,
14288+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14289+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14290+ * GNU General Public License for more details.
14291+ *
14292+ * You should have received a copy of the GNU General Public License
523b37e3 14293+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
14294+ */
14295+
14296+/*
14297+ * judging filesystem type
14298+ */
14299+
14300+#ifndef __AUFS_FSTYPE_H__
14301+#define __AUFS_FSTYPE_H__
14302+
14303+#ifdef __KERNEL__
14304+
14305+#include <linux/fs.h>
14306+#include <linux/magic.h>
b912730e 14307+#include <linux/nfs_fs.h>
b95c5147 14308+#include <linux/romfs_fs.h>
4a4d8108
AM
14309+
14310+static inline int au_test_aufs(struct super_block *sb)
14311+{
14312+ return sb->s_magic == AUFS_SUPER_MAGIC;
14313+}
14314+
14315+static inline const char *au_sbtype(struct super_block *sb)
14316+{
14317+ return sb->s_type->name;
14318+}
1308ab2a 14319+
14320+static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
14321+{
2000de60
JR
14322+#if defined(CONFIG_ISO9660_FS) || defined(CONFIG_ISO9660_FS_MODULE)
14323+ return sb->s_magic == ISOFS_SUPER_MAGIC;
dece6358
AM
14324+#else
14325+ return 0;
14326+#endif
14327+}
14328+
1308ab2a 14329+static inline int au_test_romfs(struct super_block *sb __maybe_unused)
dece6358 14330+{
2000de60
JR
14331+#if defined(CONFIG_ROMFS_FS) || defined(CONFIG_ROMFS_FS_MODULE)
14332+ return sb->s_magic == ROMFS_MAGIC;
dece6358
AM
14333+#else
14334+ return 0;
14335+#endif
14336+}
14337+
1308ab2a 14338+static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
dece6358 14339+{
1308ab2a 14340+#if defined(CONFIG_CRAMFS) || defined(CONFIG_CRAMFS_MODULE)
14341+ return sb->s_magic == CRAMFS_MAGIC;
14342+#endif
14343+ return 0;
14344+}
14345+
14346+static inline int au_test_nfs(struct super_block *sb __maybe_unused)
14347+{
14348+#if defined(CONFIG_NFS_FS) || defined(CONFIG_NFS_FS_MODULE)
14349+ return sb->s_magic == NFS_SUPER_MAGIC;
dece6358
AM
14350+#else
14351+ return 0;
14352+#endif
14353+}
14354+
1308ab2a 14355+static inline int au_test_fuse(struct super_block *sb __maybe_unused)
dece6358 14356+{
1308ab2a 14357+#if defined(CONFIG_FUSE_FS) || defined(CONFIG_FUSE_FS_MODULE)
14358+ return sb->s_magic == FUSE_SUPER_MAGIC;
dece6358
AM
14359+#else
14360+ return 0;
14361+#endif
14362+}
14363+
1308ab2a 14364+static inline int au_test_xfs(struct super_block *sb __maybe_unused)
dece6358 14365+{
1308ab2a 14366+#if defined(CONFIG_XFS_FS) || defined(CONFIG_XFS_FS_MODULE)
14367+ return sb->s_magic == XFS_SB_MAGIC;
dece6358
AM
14368+#else
14369+ return 0;
14370+#endif
14371+}
14372+
1308ab2a 14373+static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
dece6358 14374+{
1308ab2a 14375+#ifdef CONFIG_TMPFS
14376+ return sb->s_magic == TMPFS_MAGIC;
14377+#else
14378+ return 0;
dece6358 14379+#endif
dece6358
AM
14380+}
14381+
1308ab2a 14382+static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
1facf9fc 14383+{
1308ab2a 14384+#if defined(CONFIG_ECRYPT_FS) || defined(CONFIG_ECRYPT_FS_MODULE)
14385+ return !strcmp(au_sbtype(sb), "ecryptfs");
14386+#else
14387+ return 0;
14388+#endif
1facf9fc 14389+}
14390+
1308ab2a 14391+static inline int au_test_ramfs(struct super_block *sb)
14392+{
14393+ return sb->s_magic == RAMFS_MAGIC;
14394+}
14395+
14396+static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
14397+{
14398+#if defined(CONFIG_UBIFS_FS) || defined(CONFIG_UBIFS_FS_MODULE)
14399+ return sb->s_magic == UBIFS_SUPER_MAGIC;
14400+#else
14401+ return 0;
14402+#endif
14403+}
14404+
14405+static inline int au_test_procfs(struct super_block *sb __maybe_unused)
14406+{
14407+#ifdef CONFIG_PROC_FS
14408+ return sb->s_magic == PROC_SUPER_MAGIC;
14409+#else
14410+ return 0;
14411+#endif
14412+}
14413+
14414+static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
14415+{
14416+#ifdef CONFIG_SYSFS
14417+ return sb->s_magic == SYSFS_MAGIC;
14418+#else
14419+ return 0;
14420+#endif
14421+}
14422+
14423+static inline int au_test_configfs(struct super_block *sb __maybe_unused)
14424+{
14425+#if defined(CONFIG_CONFIGFS_FS) || defined(CONFIG_CONFIGFS_FS_MODULE)
14426+ return sb->s_magic == CONFIGFS_MAGIC;
14427+#else
14428+ return 0;
14429+#endif
14430+}
14431+
14432+static inline int au_test_minix(struct super_block *sb __maybe_unused)
14433+{
14434+#if defined(CONFIG_MINIX_FS) || defined(CONFIG_MINIX_FS_MODULE)
14435+ return sb->s_magic == MINIX3_SUPER_MAGIC
14436+ || sb->s_magic == MINIX2_SUPER_MAGIC
14437+ || sb->s_magic == MINIX2_SUPER_MAGIC2
14438+ || sb->s_magic == MINIX_SUPER_MAGIC
14439+ || sb->s_magic == MINIX_SUPER_MAGIC2;
14440+#else
14441+ return 0;
14442+#endif
14443+}
14444+
1308ab2a 14445+static inline int au_test_fat(struct super_block *sb __maybe_unused)
14446+{
14447+#if defined(CONFIG_FAT_FS) || defined(CONFIG_FAT_FS_MODULE)
14448+ return sb->s_magic == MSDOS_SUPER_MAGIC;
14449+#else
14450+ return 0;
14451+#endif
14452+}
14453+
14454+static inline int au_test_msdos(struct super_block *sb)
14455+{
14456+ return au_test_fat(sb);
14457+}
14458+
14459+static inline int au_test_vfat(struct super_block *sb)
14460+{
14461+ return au_test_fat(sb);
14462+}
14463+
14464+static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
14465+{
14466+#ifdef CONFIG_SECURITYFS
14467+ return sb->s_magic == SECURITYFS_MAGIC;
14468+#else
14469+ return 0;
14470+#endif
14471+}
14472+
14473+static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
14474+{
14475+#if defined(CONFIG_SQUASHFS) || defined(CONFIG_SQUASHFS_MODULE)
14476+ return sb->s_magic == SQUASHFS_MAGIC;
14477+#else
14478+ return 0;
14479+#endif
14480+}
14481+
14482+static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
14483+{
14484+#if defined(CONFIG_BTRFS_FS) || defined(CONFIG_BTRFS_FS_MODULE)
14485+ return sb->s_magic == BTRFS_SUPER_MAGIC;
14486+#else
14487+ return 0;
14488+#endif
14489+}
14490+
14491+static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
14492+{
14493+#if defined(CONFIG_XENFS) || defined(CONFIG_XENFS_MODULE)
14494+ return sb->s_magic == XENFS_SUPER_MAGIC;
14495+#else
14496+ return 0;
14497+#endif
14498+}
14499+
14500+static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
14501+{
14502+#ifdef CONFIG_DEBUG_FS
14503+ return sb->s_magic == DEBUGFS_MAGIC;
14504+#else
14505+ return 0;
14506+#endif
14507+}
14508+
14509+static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
14510+{
14511+#if defined(CONFIG_NILFS) || defined(CONFIG_NILFS_MODULE)
14512+ return sb->s_magic == NILFS_SUPER_MAGIC;
14513+#else
14514+ return 0;
14515+#endif
14516+}
14517+
4a4d8108
AM
14518+static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
14519+{
14520+#if defined(CONFIG_HFSPLUS_FS) || defined(CONFIG_HFSPLUS_FS_MODULE)
14521+ return sb->s_magic == HFSPLUS_SUPER_MAGIC;
14522+#else
14523+ return 0;
14524+#endif
14525+}
14526+
1308ab2a 14527+/* ---------------------------------------------------------------------- */
14528+/*
14529+ * they can't be an aufs branch.
14530+ */
14531+static inline int au_test_fs_unsuppoted(struct super_block *sb)
14532+{
14533+ return
14534+#ifndef CONFIG_AUFS_BR_RAMFS
14535+ au_test_ramfs(sb) ||
14536+#endif
14537+ au_test_procfs(sb)
14538+ || au_test_sysfs(sb)
14539+ || au_test_configfs(sb)
14540+ || au_test_debugfs(sb)
14541+ || au_test_securityfs(sb)
14542+ || au_test_xenfs(sb)
14543+ || au_test_ecryptfs(sb)
14544+ /* || !strcmp(au_sbtype(sb), "unionfs") */
14545+ || au_test_aufs(sb); /* will be supported in next version */
14546+}
14547+
1308ab2a 14548+static inline int au_test_fs_remote(struct super_block *sb)
14549+{
14550+ return !au_test_tmpfs(sb)
14551+#ifdef CONFIG_AUFS_BR_RAMFS
14552+ && !au_test_ramfs(sb)
14553+#endif
14554+ && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
14555+}
14556+
14557+/* ---------------------------------------------------------------------- */
14558+
14559+/*
14560+ * Note: these functions (below) are created after reading ->getattr() in all
14561+ * filesystems under linux/fs. it means we have to do so in every update...
14562+ */
14563+
14564+/*
14565+ * some filesystems require getattr to refresh the inode attributes before
14566+ * referencing.
14567+ * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
14568+ * and leave the work for d_revalidate()
14569+ */
14570+static inline int au_test_fs_refresh_iattr(struct super_block *sb)
14571+{
14572+ return au_test_nfs(sb)
14573+ || au_test_fuse(sb)
1308ab2a 14574+ /* || au_test_btrfs(sb) */ /* untested */
1308ab2a 14575+ ;
14576+}
14577+
14578+/*
14579+ * filesystems which don't maintain i_size or i_blocks.
14580+ */
14581+static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
14582+{
14583+ return au_test_xfs(sb)
4a4d8108
AM
14584+ || au_test_btrfs(sb)
14585+ || au_test_ubifs(sb)
14586+ || au_test_hfsplus(sb) /* maintained, but incorrect */
1308ab2a 14587+ /* || au_test_minix(sb) */ /* untested */
14588+ ;
14589+}
14590+
14591+/*
14592+ * filesystems which don't store the correct value in some of their inode
14593+ * attributes.
14594+ */
14595+static inline int au_test_fs_bad_iattr(struct super_block *sb)
14596+{
14597+ return au_test_fs_bad_iattr_size(sb)
1308ab2a 14598+ || au_test_fat(sb)
14599+ || au_test_msdos(sb)
14600+ || au_test_vfat(sb);
1facf9fc 14601+}
14602+
14603+/* they don't check i_nlink in link(2) */
14604+static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
14605+{
14606+ return au_test_tmpfs(sb)
14607+#ifdef CONFIG_AUFS_BR_RAMFS
14608+ || au_test_ramfs(sb)
14609+#endif
4a4d8108 14610+ || au_test_ubifs(sb)
4a4d8108 14611+ || au_test_hfsplus(sb);
1facf9fc 14612+}
14613+
14614+/*
14615+ * filesystems which sets S_NOATIME and S_NOCMTIME.
14616+ */
14617+static inline int au_test_fs_notime(struct super_block *sb)
14618+{
14619+ return au_test_nfs(sb)
14620+ || au_test_fuse(sb)
dece6358 14621+ || au_test_ubifs(sb)
1facf9fc 14622+ ;
14623+}
14624+
1facf9fc 14625+/* temporary support for i#1 in cramfs */
14626+static inline int au_test_fs_unique_ino(struct inode *inode)
14627+{
14628+ if (au_test_cramfs(inode->i_sb))
14629+ return inode->i_ino != 1;
14630+ return 1;
14631+}
14632+
14633+/* ---------------------------------------------------------------------- */
14634+
14635+/*
14636+ * the filesystem where the xino files placed must support i/o after unlink and
14637+ * maintain i_size and i_blocks.
14638+ */
14639+static inline int au_test_fs_bad_xino(struct super_block *sb)
14640+{
14641+ return au_test_fs_remote(sb)
14642+ || au_test_fs_bad_iattr_size(sb)
1facf9fc 14643+ /* don't want unnecessary work for xino */
14644+ || au_test_aufs(sb)
1308ab2a 14645+ || au_test_ecryptfs(sb)
14646+ || au_test_nilfs(sb);
1facf9fc 14647+}
14648+
14649+static inline int au_test_fs_trunc_xino(struct super_block *sb)
14650+{
14651+ return au_test_tmpfs(sb)
14652+ || au_test_ramfs(sb);
14653+}
14654+
14655+/*
14656+ * test if the @sb is real-readonly.
14657+ */
14658+static inline int au_test_fs_rr(struct super_block *sb)
14659+{
14660+ return au_test_squashfs(sb)
14661+ || au_test_iso9660(sb)
14662+ || au_test_cramfs(sb)
14663+ || au_test_romfs(sb);
14664+}
14665+
b912730e
AM
14666+/*
14667+ * test if the @inode is nfs with 'noacl' option
14668+ * NFS always sets MS_POSIXACL regardless its mount option 'noacl.'
14669+ */
14670+static inline int au_test_nfs_noacl(struct inode *inode)
14671+{
14672+ return au_test_nfs(inode->i_sb)
14673+ /* && IS_POSIXACL(inode) */
14674+ && !nfs_server_capable(inode, NFS_CAP_ACLS);
14675+}
14676+
1facf9fc 14677+#endif /* __KERNEL__ */
14678+#endif /* __AUFS_FSTYPE_H__ */
7f207e10
AM
14679diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
14680--- /usr/share/empty/fs/aufs/hfsnotify.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
14681+++ linux/fs/aufs/hfsnotify.c 2016-07-25 19:05:34.814493242 +0200
14682@@ -0,0 +1,287 @@
1facf9fc 14683+/*
8cdd5066 14684+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 14685+ *
14686+ * This program, aufs is free software; you can redistribute it and/or modify
14687+ * it under the terms of the GNU General Public License as published by
14688+ * the Free Software Foundation; either version 2 of the License, or
14689+ * (at your option) any later version.
dece6358
AM
14690+ *
14691+ * This program is distributed in the hope that it will be useful,
14692+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14693+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14694+ * GNU General Public License for more details.
14695+ *
14696+ * You should have received a copy of the GNU General Public License
523b37e3 14697+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 14698+ */
14699+
14700+/*
4a4d8108 14701+ * fsnotify for the lower directories
1facf9fc 14702+ */
14703+
14704+#include "aufs.h"
14705+
4a4d8108
AM
14706+/* FS_IN_IGNORED is unnecessary */
14707+static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
14708+ | FS_CREATE | FS_EVENT_ON_CHILD);
7f207e10 14709+static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
7eafdf33 14710+static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
1facf9fc 14711+
0c5527e5 14712+static void au_hfsn_free_mark(struct fsnotify_mark *mark)
1facf9fc 14713+{
0c5527e5
AM
14714+ struct au_hnotify *hn = container_of(mark, struct au_hnotify,
14715+ hn_mark);
5afbbe0d 14716+ /* AuDbg("here\n"); */
7eafdf33 14717+ au_cache_free_hnotify(hn);
076b876e 14718+ smp_mb__before_atomic();
1716fcea
AM
14719+ if (atomic64_dec_and_test(&au_hfsn_ifree))
14720+ wake_up(&au_hfsn_wq);
4a4d8108 14721+}
1facf9fc 14722+
027c5e7a 14723+static int au_hfsn_alloc(struct au_hinode *hinode)
4a4d8108 14724+{
1716fcea 14725+ int err;
027c5e7a
AM
14726+ struct au_hnotify *hn;
14727+ struct super_block *sb;
14728+ struct au_branch *br;
0c5527e5 14729+ struct fsnotify_mark *mark;
027c5e7a 14730+ aufs_bindex_t bindex;
1facf9fc 14731+
027c5e7a
AM
14732+ hn = hinode->hi_notify;
14733+ sb = hn->hn_aufs_inode->i_sb;
14734+ bindex = au_br_index(sb, hinode->hi_id);
14735+ br = au_sbr(sb, bindex);
1716fcea
AM
14736+ AuDebugOn(!br->br_hfsn);
14737+
0c5527e5
AM
14738+ mark = &hn->hn_mark;
14739+ fsnotify_init_mark(mark, au_hfsn_free_mark);
14740+ mark->mask = AuHfsnMask;
7f207e10
AM
14741+ /*
14742+ * by udba rename or rmdir, aufs assign a new inode to the known
14743+ * h_inode, so specify 1 to allow dups.
14744+ */
c1595e42 14745+ lockdep_off();
1716fcea 14746+ err = fsnotify_add_mark(mark, br->br_hfsn->hfsn_group, hinode->hi_inode,
027c5e7a 14747+ /*mnt*/NULL, /*allow_dups*/1);
c1595e42 14748+ lockdep_on();
1716fcea
AM
14749+
14750+ return err;
1facf9fc 14751+}
14752+
7eafdf33 14753+static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
1facf9fc 14754+{
0c5527e5 14755+ struct fsnotify_mark *mark;
7eafdf33 14756+ unsigned long long ull;
1716fcea 14757+ struct fsnotify_group *group;
7eafdf33
AM
14758+
14759+ ull = atomic64_inc_return(&au_hfsn_ifree);
14760+ BUG_ON(!ull);
953406b4 14761+
0c5527e5 14762+ mark = &hn->hn_mark;
1716fcea
AM
14763+ spin_lock(&mark->lock);
14764+ group = mark->group;
14765+ fsnotify_get_group(group);
14766+ spin_unlock(&mark->lock);
c1595e42 14767+ lockdep_off();
1716fcea 14768+ fsnotify_destroy_mark(mark, group);
5afbbe0d 14769+ fsnotify_put_mark(mark);
1716fcea 14770+ fsnotify_put_group(group);
c1595e42 14771+ lockdep_on();
7f207e10 14772+
7eafdf33
AM
14773+ /* free hn by myself */
14774+ return 0;
1facf9fc 14775+}
14776+
14777+/* ---------------------------------------------------------------------- */
14778+
4a4d8108 14779+static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
1facf9fc 14780+{
0c5527e5 14781+ struct fsnotify_mark *mark;
1facf9fc 14782+
0c5527e5
AM
14783+ mark = &hinode->hi_notify->hn_mark;
14784+ spin_lock(&mark->lock);
1facf9fc 14785+ if (do_set) {
0c5527e5
AM
14786+ AuDebugOn(mark->mask & AuHfsnMask);
14787+ mark->mask |= AuHfsnMask;
1facf9fc 14788+ } else {
0c5527e5
AM
14789+ AuDebugOn(!(mark->mask & AuHfsnMask));
14790+ mark->mask &= ~AuHfsnMask;
1facf9fc 14791+ }
0c5527e5 14792+ spin_unlock(&mark->lock);
4a4d8108 14793+ /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
1facf9fc 14794+}
14795+
4a4d8108 14796+/* ---------------------------------------------------------------------- */
1facf9fc 14797+
4a4d8108
AM
14798+/* #define AuDbgHnotify */
14799+#ifdef AuDbgHnotify
14800+static char *au_hfsn_name(u32 mask)
14801+{
14802+#ifdef CONFIG_AUFS_DEBUG
c06a8ce3
AM
14803+#define test_ret(flag) \
14804+ do { \
14805+ if (mask & flag) \
14806+ return #flag; \
14807+ } while (0)
4a4d8108
AM
14808+ test_ret(FS_ACCESS);
14809+ test_ret(FS_MODIFY);
14810+ test_ret(FS_ATTRIB);
14811+ test_ret(FS_CLOSE_WRITE);
14812+ test_ret(FS_CLOSE_NOWRITE);
14813+ test_ret(FS_OPEN);
14814+ test_ret(FS_MOVED_FROM);
14815+ test_ret(FS_MOVED_TO);
14816+ test_ret(FS_CREATE);
14817+ test_ret(FS_DELETE);
14818+ test_ret(FS_DELETE_SELF);
14819+ test_ret(FS_MOVE_SELF);
14820+ test_ret(FS_UNMOUNT);
14821+ test_ret(FS_Q_OVERFLOW);
14822+ test_ret(FS_IN_IGNORED);
b912730e 14823+ test_ret(FS_ISDIR);
4a4d8108
AM
14824+ test_ret(FS_IN_ONESHOT);
14825+ test_ret(FS_EVENT_ON_CHILD);
14826+ return "";
14827+#undef test_ret
14828+#else
14829+ return "??";
14830+#endif
1facf9fc 14831+}
4a4d8108 14832+#endif
1facf9fc 14833+
14834+/* ---------------------------------------------------------------------- */
14835+
1716fcea
AM
14836+static void au_hfsn_free_group(struct fsnotify_group *group)
14837+{
14838+ struct au_br_hfsnotify *hfsn = group->private;
14839+
5afbbe0d 14840+ /* AuDbg("here\n"); */
1716fcea
AM
14841+ kfree(hfsn);
14842+}
14843+
4a4d8108 14844+static int au_hfsn_handle_event(struct fsnotify_group *group,
fb47a38f 14845+ struct inode *inode,
0c5527e5
AM
14846+ struct fsnotify_mark *inode_mark,
14847+ struct fsnotify_mark *vfsmount_mark,
fb47a38f
JR
14848+ u32 mask, void *data, int data_type,
14849+ const unsigned char *file_name, u32 cookie)
1facf9fc 14850+{
14851+ int err;
4a4d8108
AM
14852+ struct au_hnotify *hnotify;
14853+ struct inode *h_dir, *h_inode;
fb47a38f 14854+ struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
4a4d8108 14855+
fb47a38f 14856+ AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
1facf9fc 14857+
14858+ err = 0;
0c5527e5 14859+ /* if FS_UNMOUNT happens, there must be another bug */
4a4d8108 14860+ AuDebugOn(mask & FS_UNMOUNT);
0c5527e5 14861+ if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
1facf9fc 14862+ goto out;
1facf9fc 14863+
fb47a38f
JR
14864+ h_dir = inode;
14865+ h_inode = NULL;
4a4d8108 14866+#ifdef AuDbgHnotify
392086de 14867+ au_debug_on();
4a4d8108
AM
14868+ if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
14869+ || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
14870+ AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
14871+ h_dir->i_ino, mask, au_hfsn_name(mask),
14872+ AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
14873+ /* WARN_ON(1); */
1facf9fc 14874+ }
392086de 14875+ au_debug_off();
1facf9fc 14876+#endif
4a4d8108 14877+
0c5527e5
AM
14878+ AuDebugOn(!inode_mark);
14879+ hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
14880+ err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
1facf9fc 14881+
4a4d8108
AM
14882+out:
14883+ return err;
14884+}
1facf9fc 14885+
4a4d8108 14886+static struct fsnotify_ops au_hfsn_ops = {
1716fcea
AM
14887+ .handle_event = au_hfsn_handle_event,
14888+ .free_group_priv = au_hfsn_free_group
4a4d8108
AM
14889+};
14890+
14891+/* ---------------------------------------------------------------------- */
14892+
027c5e7a
AM
14893+static void au_hfsn_fin_br(struct au_branch *br)
14894+{
1716fcea 14895+ struct au_br_hfsnotify *hfsn;
027c5e7a 14896+
1716fcea 14897+ hfsn = br->br_hfsn;
c1595e42
JR
14898+ if (hfsn) {
14899+ lockdep_off();
1716fcea 14900+ fsnotify_put_group(hfsn->hfsn_group);
c1595e42
JR
14901+ lockdep_on();
14902+ }
027c5e7a
AM
14903+}
14904+
1716fcea 14905+static int au_hfsn_init_br(struct au_branch *br, int perm)
4a4d8108
AM
14906+{
14907+ int err;
1716fcea
AM
14908+ struct fsnotify_group *group;
14909+ struct au_br_hfsnotify *hfsn;
1facf9fc 14910+
4a4d8108 14911+ err = 0;
1716fcea
AM
14912+ br->br_hfsn = NULL;
14913+ if (!au_br_hnotifyable(perm))
027c5e7a 14914+ goto out;
027c5e7a 14915+
1716fcea
AM
14916+ err = -ENOMEM;
14917+ hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
14918+ if (unlikely(!hfsn))
027c5e7a
AM
14919+ goto out;
14920+
1716fcea
AM
14921+ err = 0;
14922+ group = fsnotify_alloc_group(&au_hfsn_ops);
14923+ if (IS_ERR(group)) {
14924+ err = PTR_ERR(group);
0c5527e5 14925+ pr_err("fsnotify_alloc_group() failed, %d\n", err);
1716fcea 14926+ goto out_hfsn;
4a4d8108 14927+ }
1facf9fc 14928+
1716fcea
AM
14929+ group->private = hfsn;
14930+ hfsn->hfsn_group = group;
14931+ br->br_hfsn = hfsn;
14932+ goto out; /* success */
14933+
14934+out_hfsn:
14935+ kfree(hfsn);
027c5e7a 14936+out:
1716fcea
AM
14937+ return err;
14938+}
14939+
14940+static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
14941+{
14942+ int err;
14943+
14944+ err = 0;
14945+ if (!br->br_hfsn)
14946+ err = au_hfsn_init_br(br, perm);
14947+
1facf9fc 14948+ return err;
14949+}
14950+
7eafdf33
AM
14951+/* ---------------------------------------------------------------------- */
14952+
14953+static void au_hfsn_fin(void)
14954+{
14955+ AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
14956+ wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
14957+}
14958+
4a4d8108
AM
14959+const struct au_hnotify_op au_hnotify_op = {
14960+ .ctl = au_hfsn_ctl,
14961+ .alloc = au_hfsn_alloc,
14962+ .free = au_hfsn_free,
1facf9fc 14963+
7eafdf33
AM
14964+ .fin = au_hfsn_fin,
14965+
027c5e7a
AM
14966+ .reset_br = au_hfsn_reset_br,
14967+ .fin_br = au_hfsn_fin_br,
14968+ .init_br = au_hfsn_init_br
4a4d8108 14969+};
7f207e10
AM
14970diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
14971--- /usr/share/empty/fs/aufs/hfsplus.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 14972+++ linux/fs/aufs/hfsplus.c 2016-07-25 19:05:34.814493242 +0200
523b37e3 14973@@ -0,0 +1,56 @@
4a4d8108 14974+/*
8cdd5066 14975+ * Copyright (C) 2010-2016 Junjiro R. Okajima
4a4d8108
AM
14976+ *
14977+ * This program, aufs is free software; you can redistribute it and/or modify
14978+ * it under the terms of the GNU General Public License as published by
14979+ * the Free Software Foundation; either version 2 of the License, or
14980+ * (at your option) any later version.
14981+ *
14982+ * This program is distributed in the hope that it will be useful,
14983+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14984+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14985+ * GNU General Public License for more details.
14986+ *
14987+ * You should have received a copy of the GNU General Public License
523b37e3 14988+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 14989+ */
1facf9fc 14990+
4a4d8108
AM
14991+/*
14992+ * special support for filesystems which aqucires an inode mutex
14993+ * at final closing a file, eg, hfsplus.
14994+ *
14995+ * This trick is very simple and stupid, just to open the file before really
14996+ * neceeary open to tell hfsplus that this is not the final closing.
14997+ * The caller should call au_h_open_pre() after acquiring the inode mutex,
14998+ * and au_h_open_post() after releasing it.
14999+ */
1facf9fc 15000+
4a4d8108 15001+#include "aufs.h"
1facf9fc 15002+
392086de
AM
15003+struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15004+ int force_wr)
4a4d8108
AM
15005+{
15006+ struct file *h_file;
15007+ struct dentry *h_dentry;
1facf9fc 15008+
4a4d8108
AM
15009+ h_dentry = au_h_dptr(dentry, bindex);
15010+ AuDebugOn(!h_dentry);
5527c038 15011+ AuDebugOn(d_is_negative(h_dentry));
4a4d8108
AM
15012+
15013+ h_file = NULL;
15014+ if (au_test_hfsplus(h_dentry->d_sb)
7e9cd9fe 15015+ && d_is_reg(h_dentry))
4a4d8108
AM
15016+ h_file = au_h_open(dentry, bindex,
15017+ O_RDONLY | O_NOATIME | O_LARGEFILE,
392086de 15018+ /*file*/NULL, force_wr);
4a4d8108 15019+ return h_file;
1facf9fc 15020+}
15021+
4a4d8108
AM
15022+void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15023+ struct file *h_file)
15024+{
15025+ if (h_file) {
15026+ fput(h_file);
15027+ au_sbr_put(dentry->d_sb, bindex);
15028+ }
15029+}
7f207e10
AM
15030diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
15031--- /usr/share/empty/fs/aufs/hnotify.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 15032+++ linux/fs/aufs/hnotify.c 2016-07-25 19:05:34.814493242 +0200
5527c038 15033@@ -0,0 +1,710 @@
e49829fe 15034+/*
8cdd5066 15035+ * Copyright (C) 2005-2016 Junjiro R. Okajima
e49829fe
JR
15036+ *
15037+ * This program, aufs is free software; you can redistribute it and/or modify
15038+ * it under the terms of the GNU General Public License as published by
15039+ * the Free Software Foundation; either version 2 of the License, or
15040+ * (at your option) any later version.
15041+ *
15042+ * This program is distributed in the hope that it will be useful,
15043+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15044+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15045+ * GNU General Public License for more details.
15046+ *
15047+ * You should have received a copy of the GNU General Public License
523b37e3 15048+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
15049+ */
15050+
15051+/*
7f207e10 15052+ * abstraction to notify the direct changes on lower directories
e49829fe
JR
15053+ */
15054+
15055+#include "aufs.h"
15056+
027c5e7a 15057+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
e49829fe
JR
15058+{
15059+ int err;
7f207e10 15060+ struct au_hnotify *hn;
1facf9fc 15061+
4a4d8108
AM
15062+ err = -ENOMEM;
15063+ hn = au_cache_alloc_hnotify();
15064+ if (hn) {
15065+ hn->hn_aufs_inode = inode;
027c5e7a
AM
15066+ hinode->hi_notify = hn;
15067+ err = au_hnotify_op.alloc(hinode);
15068+ AuTraceErr(err);
15069+ if (unlikely(err)) {
15070+ hinode->hi_notify = NULL;
4a4d8108
AM
15071+ au_cache_free_hnotify(hn);
15072+ /*
15073+ * The upper dir was removed by udba, but the same named
15074+ * dir left. In this case, aufs assignes a new inode
15075+ * number and set the monitor again.
15076+ * For the lower dir, the old monitnor is still left.
15077+ */
15078+ if (err == -EEXIST)
15079+ err = 0;
15080+ }
1308ab2a 15081+ }
1308ab2a 15082+
027c5e7a 15083+ AuTraceErr(err);
1308ab2a 15084+ return err;
dece6358 15085+}
1facf9fc 15086+
4a4d8108 15087+void au_hn_free(struct au_hinode *hinode)
dece6358 15088+{
4a4d8108 15089+ struct au_hnotify *hn;
1facf9fc 15090+
4a4d8108
AM
15091+ hn = hinode->hi_notify;
15092+ if (hn) {
4a4d8108 15093+ hinode->hi_notify = NULL;
7eafdf33
AM
15094+ if (au_hnotify_op.free(hinode, hn))
15095+ au_cache_free_hnotify(hn);
4a4d8108
AM
15096+ }
15097+}
dece6358 15098+
4a4d8108 15099+/* ---------------------------------------------------------------------- */
dece6358 15100+
4a4d8108
AM
15101+void au_hn_ctl(struct au_hinode *hinode, int do_set)
15102+{
15103+ if (hinode->hi_notify)
15104+ au_hnotify_op.ctl(hinode, do_set);
15105+}
15106+
15107+void au_hn_reset(struct inode *inode, unsigned int flags)
15108+{
5afbbe0d 15109+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
15110+ struct inode *hi;
15111+ struct dentry *iwhdentry;
1facf9fc 15112+
5afbbe0d
AM
15113+ bbot = au_ibbot(inode);
15114+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108
AM
15115+ hi = au_h_iptr(inode, bindex);
15116+ if (!hi)
15117+ continue;
1308ab2a 15118+
febd17d6 15119+ /* inode_lock_nested(hi, AuLsc_I_CHILD); */
4a4d8108
AM
15120+ iwhdentry = au_hi_wh(inode, bindex);
15121+ if (iwhdentry)
15122+ dget(iwhdentry);
15123+ au_igrab(hi);
15124+ au_set_h_iptr(inode, bindex, NULL, 0);
15125+ au_set_h_iptr(inode, bindex, au_igrab(hi),
15126+ flags & ~AuHi_XINO);
15127+ iput(hi);
15128+ dput(iwhdentry);
febd17d6 15129+ /* inode_unlock(hi); */
1facf9fc 15130+ }
1facf9fc 15131+}
15132+
1308ab2a 15133+/* ---------------------------------------------------------------------- */
1facf9fc 15134+
4a4d8108 15135+static int hn_xino(struct inode *inode, struct inode *h_inode)
1facf9fc 15136+{
4a4d8108 15137+ int err;
5afbbe0d 15138+ aufs_bindex_t bindex, bbot, bfound, btop;
4a4d8108 15139+ struct inode *h_i;
1facf9fc 15140+
4a4d8108
AM
15141+ err = 0;
15142+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 15143+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
15144+ goto out;
15145+ }
1facf9fc 15146+
4a4d8108 15147+ bfound = -1;
5afbbe0d
AM
15148+ bbot = au_ibbot(inode);
15149+ btop = au_ibtop(inode);
4a4d8108 15150+#if 0 /* reserved for future use */
5afbbe0d 15151+ if (bindex == bbot) {
4a4d8108
AM
15152+ /* keep this ino in rename case */
15153+ goto out;
15154+ }
15155+#endif
5afbbe0d 15156+ for (bindex = btop; bindex <= bbot; bindex++)
4a4d8108
AM
15157+ if (au_h_iptr(inode, bindex) == h_inode) {
15158+ bfound = bindex;
15159+ break;
15160+ }
15161+ if (bfound < 0)
1308ab2a 15162+ goto out;
1facf9fc 15163+
5afbbe0d 15164+ for (bindex = btop; bindex <= bbot; bindex++) {
4a4d8108
AM
15165+ h_i = au_h_iptr(inode, bindex);
15166+ if (!h_i)
15167+ continue;
1facf9fc 15168+
4a4d8108
AM
15169+ err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
15170+ /* ignore this error */
15171+ /* bad action? */
1facf9fc 15172+ }
1facf9fc 15173+
4a4d8108 15174+ /* children inode number will be broken */
1facf9fc 15175+
4f0767ce 15176+out:
4a4d8108
AM
15177+ AuTraceErr(err);
15178+ return err;
1facf9fc 15179+}
15180+
4a4d8108 15181+static int hn_gen_tree(struct dentry *dentry)
1facf9fc 15182+{
4a4d8108
AM
15183+ int err, i, j, ndentry;
15184+ struct au_dcsub_pages dpages;
15185+ struct au_dpage *dpage;
15186+ struct dentry **dentries;
1facf9fc 15187+
4a4d8108
AM
15188+ err = au_dpages_init(&dpages, GFP_NOFS);
15189+ if (unlikely(err))
15190+ goto out;
15191+ err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
15192+ if (unlikely(err))
15193+ goto out_dpages;
1facf9fc 15194+
4a4d8108
AM
15195+ for (i = 0; i < dpages.ndpage; i++) {
15196+ dpage = dpages.dpages + i;
15197+ dentries = dpage->dentries;
15198+ ndentry = dpage->ndentry;
15199+ for (j = 0; j < ndentry; j++) {
15200+ struct dentry *d;
15201+
15202+ d = dentries[j];
15203+ if (IS_ROOT(d))
15204+ continue;
15205+
4a4d8108 15206+ au_digen_dec(d);
5527c038 15207+ if (d_really_is_positive(d))
4a4d8108
AM
15208+ /* todo: reset children xino?
15209+ cached children only? */
5527c038 15210+ au_iigen_dec(d_inode(d));
1308ab2a 15211+ }
dece6358 15212+ }
1facf9fc 15213+
4f0767ce 15214+out_dpages:
4a4d8108 15215+ au_dpages_free(&dpages);
dece6358 15216+
027c5e7a 15217+#if 0
4a4d8108
AM
15218+ /* discard children */
15219+ dentry_unhash(dentry);
15220+ dput(dentry);
027c5e7a 15221+#endif
4f0767ce 15222+out:
dece6358
AM
15223+ return err;
15224+}
15225+
1308ab2a 15226+/*
4a4d8108 15227+ * return 0 if processed.
1308ab2a 15228+ */
4a4d8108
AM
15229+static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
15230+ const unsigned int isdir)
dece6358 15231+{
1308ab2a 15232+ int err;
4a4d8108
AM
15233+ struct dentry *d;
15234+ struct qstr *dname;
1facf9fc 15235+
4a4d8108
AM
15236+ err = 1;
15237+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 15238+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
15239+ err = 0;
15240+ goto out;
15241+ }
dece6358 15242+
4a4d8108
AM
15243+ if (!isdir) {
15244+ AuDebugOn(!name);
15245+ au_iigen_dec(inode);
027c5e7a 15246+ spin_lock(&inode->i_lock);
c1595e42 15247+ hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
027c5e7a 15248+ spin_lock(&d->d_lock);
4a4d8108
AM
15249+ dname = &d->d_name;
15250+ if (dname->len != nlen
027c5e7a
AM
15251+ && memcmp(dname->name, name, nlen)) {
15252+ spin_unlock(&d->d_lock);
4a4d8108 15253+ continue;
027c5e7a 15254+ }
4a4d8108 15255+ err = 0;
4a4d8108
AM
15256+ au_digen_dec(d);
15257+ spin_unlock(&d->d_lock);
15258+ break;
1facf9fc 15259+ }
027c5e7a 15260+ spin_unlock(&inode->i_lock);
1308ab2a 15261+ } else {
027c5e7a 15262+ au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
c1595e42 15263+ d = d_find_any_alias(inode);
4a4d8108
AM
15264+ if (!d) {
15265+ au_iigen_dec(inode);
15266+ goto out;
15267+ }
1facf9fc 15268+
027c5e7a 15269+ spin_lock(&d->d_lock);
4a4d8108 15270+ dname = &d->d_name;
027c5e7a
AM
15271+ if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
15272+ spin_unlock(&d->d_lock);
4a4d8108 15273+ err = hn_gen_tree(d);
027c5e7a
AM
15274+ spin_lock(&d->d_lock);
15275+ }
15276+ spin_unlock(&d->d_lock);
4a4d8108
AM
15277+ dput(d);
15278+ }
1facf9fc 15279+
4f0767ce 15280+out:
4a4d8108 15281+ AuTraceErr(err);
1308ab2a 15282+ return err;
15283+}
dece6358 15284+
4a4d8108 15285+static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
1facf9fc 15286+{
4a4d8108 15287+ int err;
1facf9fc 15288+
5527c038 15289+ if (IS_ROOT(dentry)) {
0c3ec466 15290+ pr_warn("branch root dir was changed\n");
4a4d8108
AM
15291+ return 0;
15292+ }
1308ab2a 15293+
4a4d8108
AM
15294+ err = 0;
15295+ if (!isdir) {
4a4d8108 15296+ au_digen_dec(dentry);
5527c038
JR
15297+ if (d_really_is_positive(dentry))
15298+ au_iigen_dec(d_inode(dentry));
4a4d8108 15299+ } else {
027c5e7a 15300+ au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
5527c038 15301+ if (d_really_is_positive(dentry))
4a4d8108
AM
15302+ err = hn_gen_tree(dentry);
15303+ }
15304+
15305+ AuTraceErr(err);
15306+ return err;
1facf9fc 15307+}
15308+
4a4d8108 15309+/* ---------------------------------------------------------------------- */
1facf9fc 15310+
4a4d8108
AM
15311+/* hnotify job flags */
15312+#define AuHnJob_XINO0 1
15313+#define AuHnJob_GEN (1 << 1)
15314+#define AuHnJob_DIRENT (1 << 2)
15315+#define AuHnJob_ISDIR (1 << 3)
15316+#define AuHnJob_TRYXINO0 (1 << 4)
15317+#define AuHnJob_MNTPNT (1 << 5)
15318+#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
7f207e10
AM
15319+#define au_fset_hnjob(flags, name) \
15320+ do { (flags) |= AuHnJob_##name; } while (0)
15321+#define au_fclr_hnjob(flags, name) \
15322+ do { (flags) &= ~AuHnJob_##name; } while (0)
1facf9fc 15323+
4a4d8108
AM
15324+enum {
15325+ AuHn_CHILD,
15326+ AuHn_PARENT,
15327+ AuHnLast
15328+};
1facf9fc 15329+
4a4d8108
AM
15330+struct au_hnotify_args {
15331+ struct inode *h_dir, *dir, *h_child_inode;
15332+ u32 mask;
15333+ unsigned int flags[AuHnLast];
15334+ unsigned int h_child_nlen;
15335+ char h_child_name[];
15336+};
1facf9fc 15337+
4a4d8108
AM
15338+struct hn_job_args {
15339+ unsigned int flags;
15340+ struct inode *inode, *h_inode, *dir, *h_dir;
15341+ struct dentry *dentry;
15342+ char *h_name;
15343+ int h_nlen;
15344+};
1308ab2a 15345+
4a4d8108
AM
15346+static int hn_job(struct hn_job_args *a)
15347+{
15348+ const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
076b876e 15349+ int e;
1308ab2a 15350+
4a4d8108
AM
15351+ /* reset xino */
15352+ if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
15353+ hn_xino(a->inode, a->h_inode); /* ignore this error */
1308ab2a 15354+
4a4d8108
AM
15355+ if (au_ftest_hnjob(a->flags, TRYXINO0)
15356+ && a->inode
15357+ && a->h_inode) {
febd17d6 15358+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
38d290e6
JR
15359+ if (!a->h_inode->i_nlink
15360+ && !(a->h_inode->i_state & I_LINKABLE))
4a4d8108 15361+ hn_xino(a->inode, a->h_inode); /* ignore this error */
febd17d6 15362+ inode_unlock(a->h_inode);
1308ab2a 15363+ }
1facf9fc 15364+
4a4d8108
AM
15365+ /* make the generation obsolete */
15366+ if (au_ftest_hnjob(a->flags, GEN)) {
076b876e 15367+ e = -1;
4a4d8108 15368+ if (a->inode)
076b876e 15369+ e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
4a4d8108 15370+ isdir);
076b876e 15371+ if (e && a->dentry)
4a4d8108
AM
15372+ hn_gen_by_name(a->dentry, isdir);
15373+ /* ignore this error */
1facf9fc 15374+ }
1facf9fc 15375+
4a4d8108
AM
15376+ /* make dir entries obsolete */
15377+ if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
15378+ struct au_vdir *vdir;
1facf9fc 15379+
4a4d8108
AM
15380+ vdir = au_ivdir(a->inode);
15381+ if (vdir)
15382+ vdir->vd_jiffy = 0;
15383+ /* IMustLock(a->inode); */
15384+ /* a->inode->i_version++; */
15385+ }
1facf9fc 15386+
4a4d8108
AM
15387+ /* can do nothing but warn */
15388+ if (au_ftest_hnjob(a->flags, MNTPNT)
15389+ && a->dentry
15390+ && d_mountpoint(a->dentry))
523b37e3 15391+ pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
1facf9fc 15392+
4a4d8108 15393+ return 0;
1308ab2a 15394+}
1facf9fc 15395+
1308ab2a 15396+/* ---------------------------------------------------------------------- */
1facf9fc 15397+
4a4d8108
AM
15398+static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
15399+ struct inode *dir)
1308ab2a 15400+{
4a4d8108
AM
15401+ struct dentry *dentry, *d, *parent;
15402+ struct qstr *dname;
1308ab2a 15403+
c1595e42 15404+ parent = d_find_any_alias(dir);
4a4d8108
AM
15405+ if (!parent)
15406+ return NULL;
1308ab2a 15407+
4a4d8108 15408+ dentry = NULL;
027c5e7a 15409+ spin_lock(&parent->d_lock);
c1595e42 15410+ list_for_each_entry(d, &parent->d_subdirs, d_child) {
523b37e3 15411+ /* AuDbg("%pd\n", d); */
027c5e7a 15412+ spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
4a4d8108
AM
15413+ dname = &d->d_name;
15414+ if (dname->len != nlen || memcmp(dname->name, name, nlen))
027c5e7a
AM
15415+ goto cont_unlock;
15416+ if (au_di(d))
15417+ au_digen_dec(d);
15418+ else
15419+ goto cont_unlock;
c1595e42 15420+ if (au_dcount(d) > 0) {
027c5e7a 15421+ dentry = dget_dlock(d);
4a4d8108 15422+ spin_unlock(&d->d_lock);
027c5e7a 15423+ break;
dece6358 15424+ }
1facf9fc 15425+
f6b6e03d 15426+cont_unlock:
027c5e7a 15427+ spin_unlock(&d->d_lock);
1308ab2a 15428+ }
027c5e7a 15429+ spin_unlock(&parent->d_lock);
4a4d8108 15430+ dput(parent);
1facf9fc 15431+
4a4d8108
AM
15432+ if (dentry)
15433+ di_write_lock_child(dentry);
1308ab2a 15434+
4a4d8108
AM
15435+ return dentry;
15436+}
dece6358 15437+
4a4d8108
AM
15438+static struct inode *lookup_wlock_by_ino(struct super_block *sb,
15439+ aufs_bindex_t bindex, ino_t h_ino)
15440+{
15441+ struct inode *inode;
15442+ ino_t ino;
15443+ int err;
15444+
15445+ inode = NULL;
15446+ err = au_xino_read(sb, bindex, h_ino, &ino);
15447+ if (!err && ino)
15448+ inode = ilookup(sb, ino);
15449+ if (!inode)
15450+ goto out;
15451+
15452+ if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
0c3ec466 15453+ pr_warn("wrong root branch\n");
4a4d8108
AM
15454+ iput(inode);
15455+ inode = NULL;
15456+ goto out;
1308ab2a 15457+ }
15458+
4a4d8108 15459+ ii_write_lock_child(inode);
1308ab2a 15460+
4f0767ce 15461+out:
4a4d8108 15462+ return inode;
dece6358
AM
15463+}
15464+
4a4d8108 15465+static void au_hn_bh(void *_args)
1facf9fc 15466+{
4a4d8108
AM
15467+ struct au_hnotify_args *a = _args;
15468+ struct super_block *sb;
5afbbe0d 15469+ aufs_bindex_t bindex, bbot, bfound;
4a4d8108 15470+ unsigned char xino, try_iput;
1facf9fc 15471+ int err;
1308ab2a 15472+ struct inode *inode;
4a4d8108
AM
15473+ ino_t h_ino;
15474+ struct hn_job_args args;
15475+ struct dentry *dentry;
15476+ struct au_sbinfo *sbinfo;
1facf9fc 15477+
4a4d8108
AM
15478+ AuDebugOn(!_args);
15479+ AuDebugOn(!a->h_dir);
15480+ AuDebugOn(!a->dir);
15481+ AuDebugOn(!a->mask);
15482+ AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
15483+ a->mask, a->dir->i_ino, a->h_dir->i_ino,
15484+ a->h_child_inode ? a->h_child_inode->i_ino : 0);
1facf9fc 15485+
4a4d8108
AM
15486+ inode = NULL;
15487+ dentry = NULL;
15488+ /*
15489+ * do not lock a->dir->i_mutex here
15490+ * because of d_revalidate() may cause a deadlock.
15491+ */
15492+ sb = a->dir->i_sb;
15493+ AuDebugOn(!sb);
15494+ sbinfo = au_sbi(sb);
15495+ AuDebugOn(!sbinfo);
7f207e10 15496+ si_write_lock(sb, AuLock_NOPLMW);
1facf9fc 15497+
4a4d8108
AM
15498+ ii_read_lock_parent(a->dir);
15499+ bfound = -1;
5afbbe0d
AM
15500+ bbot = au_ibbot(a->dir);
15501+ for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
4a4d8108
AM
15502+ if (au_h_iptr(a->dir, bindex) == a->h_dir) {
15503+ bfound = bindex;
15504+ break;
15505+ }
15506+ ii_read_unlock(a->dir);
15507+ if (unlikely(bfound < 0))
15508+ goto out;
1facf9fc 15509+
4a4d8108
AM
15510+ xino = !!au_opt_test(au_mntflags(sb), XINO);
15511+ h_ino = 0;
15512+ if (a->h_child_inode)
15513+ h_ino = a->h_child_inode->i_ino;
1facf9fc 15514+
4a4d8108
AM
15515+ if (a->h_child_nlen
15516+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
15517+ || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
15518+ dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
15519+ a->dir);
15520+ try_iput = 0;
5527c038
JR
15521+ if (dentry && d_really_is_positive(dentry))
15522+ inode = d_inode(dentry);
4a4d8108
AM
15523+ if (xino && !inode && h_ino
15524+ && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
15525+ || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
15526+ || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
15527+ inode = lookup_wlock_by_ino(sb, bfound, h_ino);
15528+ try_iput = 1;
15529+ }
1facf9fc 15530+
4a4d8108
AM
15531+ args.flags = a->flags[AuHn_CHILD];
15532+ args.dentry = dentry;
15533+ args.inode = inode;
15534+ args.h_inode = a->h_child_inode;
15535+ args.dir = a->dir;
15536+ args.h_dir = a->h_dir;
15537+ args.h_name = a->h_child_name;
15538+ args.h_nlen = a->h_child_nlen;
15539+ err = hn_job(&args);
15540+ if (dentry) {
027c5e7a 15541+ if (au_di(dentry))
4a4d8108
AM
15542+ di_write_unlock(dentry);
15543+ dput(dentry);
15544+ }
15545+ if (inode && try_iput) {
15546+ ii_write_unlock(inode);
15547+ iput(inode);
15548+ }
1facf9fc 15549+
4a4d8108
AM
15550+ ii_write_lock_parent(a->dir);
15551+ args.flags = a->flags[AuHn_PARENT];
15552+ args.dentry = NULL;
15553+ args.inode = a->dir;
15554+ args.h_inode = a->h_dir;
15555+ args.dir = NULL;
15556+ args.h_dir = NULL;
15557+ args.h_name = NULL;
15558+ args.h_nlen = 0;
15559+ err = hn_job(&args);
15560+ ii_write_unlock(a->dir);
1facf9fc 15561+
4f0767ce 15562+out:
4a4d8108
AM
15563+ iput(a->h_child_inode);
15564+ iput(a->h_dir);
15565+ iput(a->dir);
027c5e7a
AM
15566+ si_write_unlock(sb);
15567+ au_nwt_done(&sbinfo->si_nowait);
1308ab2a 15568+ kfree(a);
dece6358 15569+}
1facf9fc 15570+
4a4d8108
AM
15571+/* ---------------------------------------------------------------------- */
15572+
15573+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
15574+ struct qstr *h_child_qstr, struct inode *h_child_inode)
dece6358 15575+{
4a4d8108 15576+ int err, len;
53392da6 15577+ unsigned int flags[AuHnLast], f;
4a4d8108
AM
15578+ unsigned char isdir, isroot, wh;
15579+ struct inode *dir;
15580+ struct au_hnotify_args *args;
15581+ char *p, *h_child_name;
dece6358 15582+
1308ab2a 15583+ err = 0;
4a4d8108
AM
15584+ AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
15585+ dir = igrab(hnotify->hn_aufs_inode);
15586+ if (!dir)
15587+ goto out;
1facf9fc 15588+
4a4d8108
AM
15589+ isroot = (dir->i_ino == AUFS_ROOT_INO);
15590+ wh = 0;
15591+ h_child_name = (void *)h_child_qstr->name;
15592+ len = h_child_qstr->len;
15593+ if (h_child_name) {
15594+ if (len > AUFS_WH_PFX_LEN
15595+ && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
15596+ h_child_name += AUFS_WH_PFX_LEN;
15597+ len -= AUFS_WH_PFX_LEN;
15598+ wh = 1;
15599+ }
1facf9fc 15600+ }
dece6358 15601+
4a4d8108
AM
15602+ isdir = 0;
15603+ if (h_child_inode)
15604+ isdir = !!S_ISDIR(h_child_inode->i_mode);
15605+ flags[AuHn_PARENT] = AuHnJob_ISDIR;
15606+ flags[AuHn_CHILD] = 0;
15607+ if (isdir)
15608+ flags[AuHn_CHILD] = AuHnJob_ISDIR;
15609+ au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
15610+ au_fset_hnjob(flags[AuHn_CHILD], GEN);
15611+ switch (mask & FS_EVENTS_POSS_ON_CHILD) {
15612+ case FS_MOVED_FROM:
15613+ case FS_MOVED_TO:
15614+ au_fset_hnjob(flags[AuHn_CHILD], XINO0);
15615+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15616+ /*FALLTHROUGH*/
15617+ case FS_CREATE:
fb47a38f 15618+ AuDebugOn(!h_child_name);
4a4d8108 15619+ break;
1facf9fc 15620+
4a4d8108
AM
15621+ case FS_DELETE:
15622+ /*
15623+ * aufs never be able to get this child inode.
15624+ * revalidation should be in d_revalidate()
15625+ * by checking i_nlink, i_generation or d_unhashed().
15626+ */
15627+ AuDebugOn(!h_child_name);
15628+ au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
15629+ au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
15630+ break;
dece6358 15631+
4a4d8108
AM
15632+ default:
15633+ AuDebugOn(1);
15634+ }
1308ab2a 15635+
4a4d8108
AM
15636+ if (wh)
15637+ h_child_inode = NULL;
1308ab2a 15638+
4a4d8108
AM
15639+ err = -ENOMEM;
15640+ /* iput() and kfree() will be called in au_hnotify() */
4a4d8108 15641+ args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
4a4d8108
AM
15642+ if (unlikely(!args)) {
15643+ AuErr1("no memory\n");
15644+ iput(dir);
15645+ goto out;
15646+ }
15647+ args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
15648+ args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
15649+ args->mask = mask;
15650+ args->dir = dir;
15651+ args->h_dir = igrab(h_dir);
15652+ if (h_child_inode)
15653+ h_child_inode = igrab(h_child_inode); /* can be NULL */
15654+ args->h_child_inode = h_child_inode;
15655+ args->h_child_nlen = len;
15656+ if (len) {
15657+ p = (void *)args;
15658+ p += sizeof(*args);
15659+ memcpy(p, h_child_name, len);
15660+ p[len] = 0;
1308ab2a 15661+ }
1308ab2a 15662+
38d290e6 15663+ /* NFS fires the event for silly-renamed one from kworker */
53392da6 15664+ f = 0;
38d290e6
JR
15665+ if (!dir->i_nlink
15666+ || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
53392da6
AM
15667+ f = AuWkq_NEST;
15668+ err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
4a4d8108
AM
15669+ if (unlikely(err)) {
15670+ pr_err("wkq %d\n", err);
15671+ iput(args->h_child_inode);
15672+ iput(args->h_dir);
15673+ iput(args->dir);
15674+ kfree(args);
1facf9fc 15675+ }
1facf9fc 15676+
4a4d8108 15677+out:
1facf9fc 15678+ return err;
15679+}
15680+
027c5e7a
AM
15681+/* ---------------------------------------------------------------------- */
15682+
15683+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
15684+{
15685+ int err;
15686+
15687+ AuDebugOn(!(udba & AuOptMask_UDBA));
15688+
15689+ err = 0;
15690+ if (au_hnotify_op.reset_br)
15691+ err = au_hnotify_op.reset_br(udba, br, perm);
15692+
15693+ return err;
15694+}
15695+
15696+int au_hnotify_init_br(struct au_branch *br, int perm)
15697+{
15698+ int err;
15699+
15700+ err = 0;
15701+ if (au_hnotify_op.init_br)
15702+ err = au_hnotify_op.init_br(br, perm);
15703+
15704+ return err;
15705+}
15706+
15707+void au_hnotify_fin_br(struct au_branch *br)
15708+{
15709+ if (au_hnotify_op.fin_br)
15710+ au_hnotify_op.fin_br(br);
15711+}
15712+
4a4d8108
AM
15713+static void au_hn_destroy_cache(void)
15714+{
15715+ kmem_cache_destroy(au_cachep[AuCache_HNOTIFY]);
15716+ au_cachep[AuCache_HNOTIFY] = NULL;
15717+}
1308ab2a 15718+
4a4d8108 15719+int __init au_hnotify_init(void)
1facf9fc 15720+{
1308ab2a 15721+ int err;
1308ab2a 15722+
4a4d8108
AM
15723+ err = -ENOMEM;
15724+ au_cachep[AuCache_HNOTIFY] = AuCache(au_hnotify);
15725+ if (au_cachep[AuCache_HNOTIFY]) {
027c5e7a
AM
15726+ err = 0;
15727+ if (au_hnotify_op.init)
15728+ err = au_hnotify_op.init();
4a4d8108
AM
15729+ if (unlikely(err))
15730+ au_hn_destroy_cache();
1308ab2a 15731+ }
1308ab2a 15732+ AuTraceErr(err);
4a4d8108 15733+ return err;
1308ab2a 15734+}
15735+
4a4d8108 15736+void au_hnotify_fin(void)
1308ab2a 15737+{
027c5e7a
AM
15738+ if (au_hnotify_op.fin)
15739+ au_hnotify_op.fin();
4a4d8108
AM
15740+ /* cf. au_cache_fin() */
15741+ if (au_cachep[AuCache_HNOTIFY])
15742+ au_hn_destroy_cache();
dece6358 15743+}
7f207e10
AM
15744diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
15745--- /usr/share/empty/fs/aufs/iinfo.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
15746+++ linux/fs/aufs/iinfo.c 2016-07-25 19:05:34.814493242 +0200
15747@@ -0,0 +1,284 @@
dece6358 15748+/*
8cdd5066 15749+ * Copyright (C) 2005-2016 Junjiro R. Okajima
dece6358
AM
15750+ *
15751+ * This program, aufs is free software; you can redistribute it and/or modify
15752+ * it under the terms of the GNU General Public License as published by
15753+ * the Free Software Foundation; either version 2 of the License, or
15754+ * (at your option) any later version.
15755+ *
15756+ * This program is distributed in the hope that it will be useful,
15757+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
15758+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15759+ * GNU General Public License for more details.
15760+ *
15761+ * You should have received a copy of the GNU General Public License
523b37e3 15762+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358 15763+ */
1facf9fc 15764+
dece6358 15765+/*
4a4d8108 15766+ * inode private data
dece6358 15767+ */
1facf9fc 15768+
1308ab2a 15769+#include "aufs.h"
1facf9fc 15770+
4a4d8108 15771+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 15772+{
4a4d8108 15773+ struct inode *h_inode;
5afbbe0d 15774+ struct au_hinode *hinode;
1facf9fc 15775+
4a4d8108 15776+ IiMustAnyLock(inode);
1facf9fc 15777+
5afbbe0d
AM
15778+ hinode = au_hinode(au_ii(inode), bindex);
15779+ h_inode = hinode->hi_inode;
4a4d8108
AM
15780+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15781+ return h_inode;
15782+}
1facf9fc 15783+
4a4d8108
AM
15784+/* todo: hard/soft set? */
15785+void au_hiput(struct au_hinode *hinode)
15786+{
15787+ au_hn_free(hinode);
15788+ dput(hinode->hi_whdentry);
15789+ iput(hinode->hi_inode);
15790+}
1facf9fc 15791+
4a4d8108
AM
15792+unsigned int au_hi_flags(struct inode *inode, int isdir)
15793+{
15794+ unsigned int flags;
15795+ const unsigned int mnt_flags = au_mntflags(inode->i_sb);
1facf9fc 15796+
4a4d8108
AM
15797+ flags = 0;
15798+ if (au_opt_test(mnt_flags, XINO))
15799+ au_fset_hi(flags, XINO);
15800+ if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
15801+ au_fset_hi(flags, HNOTIFY);
15802+ return flags;
1facf9fc 15803+}
15804+
4a4d8108
AM
15805+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
15806+ struct inode *h_inode, unsigned int flags)
1308ab2a 15807+{
4a4d8108
AM
15808+ struct au_hinode *hinode;
15809+ struct inode *hi;
15810+ struct au_iinfo *iinfo = au_ii(inode);
1facf9fc 15811+
4a4d8108 15812+ IiMustWriteLock(inode);
dece6358 15813+
5afbbe0d 15814+ hinode = au_hinode(iinfo, bindex);
4a4d8108
AM
15815+ hi = hinode->hi_inode;
15816+ AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
15817+
15818+ if (hi)
15819+ au_hiput(hinode);
15820+ hinode->hi_inode = h_inode;
15821+ if (h_inode) {
15822+ int err;
15823+ struct super_block *sb = inode->i_sb;
15824+ struct au_branch *br;
15825+
027c5e7a
AM
15826+ AuDebugOn(inode->i_mode
15827+ && (h_inode->i_mode & S_IFMT)
15828+ != (inode->i_mode & S_IFMT));
5afbbe0d 15829+ if (bindex == iinfo->ii_btop)
4a4d8108
AM
15830+ au_cpup_igen(inode, h_inode);
15831+ br = au_sbr(sb, bindex);
15832+ hinode->hi_id = br->br_id;
15833+ if (au_ftest_hi(flags, XINO)) {
15834+ err = au_xino_write(sb, bindex, h_inode->i_ino,
15835+ inode->i_ino);
15836+ if (unlikely(err))
15837+ AuIOErr1("failed au_xino_write() %d\n", err);
15838+ }
15839+
15840+ if (au_ftest_hi(flags, HNOTIFY)
15841+ && au_br_hnotifyable(br->br_perm)) {
027c5e7a 15842+ err = au_hn_alloc(hinode, inode);
4a4d8108
AM
15843+ if (unlikely(err))
15844+ AuIOErr1("au_hn_alloc() %d\n", err);
1308ab2a 15845+ }
15846+ }
4a4d8108 15847+}
dece6358 15848+
4a4d8108
AM
15849+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
15850+ struct dentry *h_wh)
15851+{
15852+ struct au_hinode *hinode;
dece6358 15853+
4a4d8108
AM
15854+ IiMustWriteLock(inode);
15855+
5afbbe0d 15856+ hinode = au_hinode(au_ii(inode), bindex);
4a4d8108
AM
15857+ AuDebugOn(hinode->hi_whdentry);
15858+ hinode->hi_whdentry = h_wh;
1facf9fc 15859+}
15860+
537831f9 15861+void au_update_iigen(struct inode *inode, int half)
1308ab2a 15862+{
537831f9
AM
15863+ struct au_iinfo *iinfo;
15864+ struct au_iigen *iigen;
15865+ unsigned int sigen;
15866+
15867+ sigen = au_sigen(inode->i_sb);
15868+ iinfo = au_ii(inode);
15869+ iigen = &iinfo->ii_generation;
be52b249 15870+ spin_lock(&iigen->ig_spin);
537831f9
AM
15871+ iigen->ig_generation = sigen;
15872+ if (half)
15873+ au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
15874+ else
15875+ au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
be52b249 15876+ spin_unlock(&iigen->ig_spin);
4a4d8108 15877+}
1facf9fc 15878+
4a4d8108
AM
15879+/* it may be called at remount time, too */
15880+void au_update_ibrange(struct inode *inode, int do_put_zero)
15881+{
15882+ struct au_iinfo *iinfo;
5afbbe0d 15883+ aufs_bindex_t bindex, bbot;
1facf9fc 15884+
5afbbe0d 15885+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 15886+ IiMustWriteLock(inode);
1facf9fc 15887+
5afbbe0d
AM
15888+ iinfo = au_ii(inode);
15889+ if (do_put_zero && iinfo->ii_btop >= 0) {
15890+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
15891+ bindex++) {
15892+ struct inode *h_i;
1facf9fc 15893+
5afbbe0d 15894+ h_i = au_hinode(iinfo, bindex)->hi_inode;
38d290e6
JR
15895+ if (h_i
15896+ && !h_i->i_nlink
15897+ && !(h_i->i_state & I_LINKABLE))
027c5e7a
AM
15898+ au_set_h_iptr(inode, bindex, NULL, 0);
15899+ }
4a4d8108
AM
15900+ }
15901+
5afbbe0d
AM
15902+ iinfo->ii_btop = -1;
15903+ iinfo->ii_bbot = -1;
15904+ bbot = au_sbbot(inode->i_sb);
15905+ for (bindex = 0; bindex <= bbot; bindex++)
15906+ if (au_hinode(iinfo, bindex)->hi_inode) {
15907+ iinfo->ii_btop = bindex;
4a4d8108 15908+ break;
027c5e7a 15909+ }
5afbbe0d
AM
15910+ if (iinfo->ii_btop >= 0)
15911+ for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
15912+ if (au_hinode(iinfo, bindex)->hi_inode) {
15913+ iinfo->ii_bbot = bindex;
027c5e7a
AM
15914+ break;
15915+ }
5afbbe0d 15916+ AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
1308ab2a 15917+}
1facf9fc 15918+
dece6358 15919+/* ---------------------------------------------------------------------- */
1facf9fc 15920+
4a4d8108 15921+void au_icntnr_init_once(void *_c)
dece6358 15922+{
4a4d8108
AM
15923+ struct au_icntnr *c = _c;
15924+ struct au_iinfo *iinfo = &c->iinfo;
1facf9fc 15925+
be52b249 15926+ spin_lock_init(&iinfo->ii_generation.ig_spin);
4a4d8108
AM
15927+ au_rw_init(&iinfo->ii_rwsem);
15928+ inode_init_once(&c->vfs_inode);
15929+}
1facf9fc 15930+
5afbbe0d
AM
15931+void au_hinode_init(struct au_hinode *hinode)
15932+{
15933+ hinode->hi_inode = NULL;
15934+ hinode->hi_id = -1;
15935+ au_hn_init(hinode);
15936+ hinode->hi_whdentry = NULL;
15937+}
15938+
4a4d8108
AM
15939+int au_iinfo_init(struct inode *inode)
15940+{
15941+ struct au_iinfo *iinfo;
15942+ struct super_block *sb;
5afbbe0d 15943+ struct au_hinode *hi;
4a4d8108 15944+ int nbr, i;
1facf9fc 15945+
4a4d8108
AM
15946+ sb = inode->i_sb;
15947+ iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
5afbbe0d 15948+ nbr = au_sbbot(sb) + 1;
4a4d8108
AM
15949+ if (unlikely(nbr <= 0))
15950+ nbr = 1;
5afbbe0d
AM
15951+ hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
15952+ if (hi) {
7f207e10 15953+ au_ninodes_inc(sb);
5afbbe0d
AM
15954+
15955+ iinfo->ii_hinode = hi;
15956+ for (i = 0; i < nbr; i++, hi++)
15957+ au_hinode_init(hi);
1facf9fc 15958+
537831f9 15959+ iinfo->ii_generation.ig_generation = au_sigen(sb);
5afbbe0d
AM
15960+ iinfo->ii_btop = -1;
15961+ iinfo->ii_bbot = -1;
4a4d8108
AM
15962+ iinfo->ii_vdir = NULL;
15963+ return 0;
1308ab2a 15964+ }
4a4d8108
AM
15965+ return -ENOMEM;
15966+}
1facf9fc 15967+
5afbbe0d 15968+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr)
4a4d8108 15969+{
5afbbe0d 15970+ int err, i;
4a4d8108 15971+ struct au_hinode *hip;
1facf9fc 15972+
4a4d8108
AM
15973+ AuRwMustWriteLock(&iinfo->ii_rwsem);
15974+
15975+ err = -ENOMEM;
5afbbe0d 15976+ hip = krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS);
4a4d8108
AM
15977+ if (hip) {
15978+ iinfo->ii_hinode = hip;
5afbbe0d
AM
15979+ i = iinfo->ii_bbot + 1;
15980+ hip += i;
15981+ for (; i < nbr; i++, hip++)
15982+ au_hinode_init(hip);
4a4d8108 15983+ err = 0;
1308ab2a 15984+ }
4a4d8108 15985+
1308ab2a 15986+ return err;
1facf9fc 15987+}
15988+
4a4d8108 15989+void au_iinfo_fin(struct inode *inode)
1facf9fc 15990+{
4a4d8108
AM
15991+ struct au_iinfo *iinfo;
15992+ struct au_hinode *hi;
15993+ struct super_block *sb;
5afbbe0d 15994+ aufs_bindex_t bindex, bbot;
b752ccd1 15995+ const unsigned char unlinked = !inode->i_nlink;
1308ab2a 15996+
5afbbe0d 15997+ AuDebugOn(au_is_bad_inode(inode));
1308ab2a 15998+
b752ccd1 15999+ sb = inode->i_sb;
7f207e10 16000+ au_ninodes_dec(sb);
b752ccd1
AM
16001+ if (si_pid_test(sb))
16002+ au_xino_delete_inode(inode, unlinked);
16003+ else {
16004+ /*
16005+ * it is safe to hide the dependency between sbinfo and
16006+ * sb->s_umount.
16007+ */
16008+ lockdep_off();
16009+ si_noflush_read_lock(sb);
16010+ au_xino_delete_inode(inode, unlinked);
16011+ si_read_unlock(sb);
16012+ lockdep_on();
16013+ }
16014+
5afbbe0d 16015+ iinfo = au_ii(inode);
4a4d8108
AM
16016+ if (iinfo->ii_vdir)
16017+ au_vdir_free(iinfo->ii_vdir);
1308ab2a 16018+
5afbbe0d 16019+ bindex = iinfo->ii_btop;
b752ccd1 16020+ if (bindex >= 0) {
5afbbe0d
AM
16021+ hi = au_hinode(iinfo, bindex);
16022+ bbot = iinfo->ii_bbot;
16023+ while (bindex++ <= bbot) {
b752ccd1 16024+ if (hi->hi_inode)
4a4d8108 16025+ au_hiput(hi);
4a4d8108
AM
16026+ hi++;
16027+ }
16028+ }
4a4d8108
AM
16029+ kfree(iinfo->ii_hinode);
16030+ AuRwDestroy(&iinfo->ii_rwsem);
dece6358 16031+}
7f207e10
AM
16032diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
16033--- /usr/share/empty/fs/aufs/inode.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
16034+++ linux/fs/aufs/inode.c 2016-07-25 19:05:34.814493242 +0200
16035@@ -0,0 +1,517 @@
4a4d8108 16036+/*
8cdd5066 16037+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
16038+ *
16039+ * This program, aufs is free software; you can redistribute it and/or modify
16040+ * it under the terms of the GNU General Public License as published by
16041+ * the Free Software Foundation; either version 2 of the License, or
16042+ * (at your option) any later version.
16043+ *
16044+ * This program is distributed in the hope that it will be useful,
16045+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16046+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16047+ * GNU General Public License for more details.
16048+ *
16049+ * You should have received a copy of the GNU General Public License
523b37e3 16050+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 16051+ */
1facf9fc 16052+
4a4d8108
AM
16053+/*
16054+ * inode functions
16055+ */
1facf9fc 16056+
4a4d8108 16057+#include "aufs.h"
1308ab2a 16058+
4a4d8108
AM
16059+struct inode *au_igrab(struct inode *inode)
16060+{
16061+ if (inode) {
16062+ AuDebugOn(!atomic_read(&inode->i_count));
027c5e7a 16063+ ihold(inode);
1facf9fc 16064+ }
4a4d8108
AM
16065+ return inode;
16066+}
1facf9fc 16067+
4a4d8108
AM
16068+static void au_refresh_hinode_attr(struct inode *inode, int do_version)
16069+{
16070+ au_cpup_attr_all(inode, /*force*/0);
537831f9 16071+ au_update_iigen(inode, /*half*/1);
4a4d8108
AM
16072+ if (do_version)
16073+ inode->i_version++;
dece6358 16074+}
1facf9fc 16075+
027c5e7a 16076+static int au_ii_refresh(struct inode *inode, int *update)
dece6358 16077+{
4a4d8108 16078+ int err, e;
027c5e7a 16079+ umode_t type;
4a4d8108 16080+ aufs_bindex_t bindex, new_bindex;
1308ab2a 16081+ struct super_block *sb;
4a4d8108 16082+ struct au_iinfo *iinfo;
027c5e7a 16083+ struct au_hinode *p, *q, tmp;
1facf9fc 16084+
5afbbe0d 16085+ AuDebugOn(au_is_bad_inode(inode));
4a4d8108 16086+ IiMustWriteLock(inode);
1facf9fc 16087+
027c5e7a 16088+ *update = 0;
4a4d8108 16089+ sb = inode->i_sb;
027c5e7a 16090+ type = inode->i_mode & S_IFMT;
4a4d8108 16091+ iinfo = au_ii(inode);
5afbbe0d 16092+ err = au_hinode_realloc(iinfo, au_sbbot(sb) + 1);
4a4d8108 16093+ if (unlikely(err))
1308ab2a 16094+ goto out;
1facf9fc 16095+
5afbbe0d
AM
16096+ AuDebugOn(iinfo->ii_btop < 0);
16097+ p = au_hinode(iinfo, iinfo->ii_btop);
16098+ for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
4a4d8108
AM
16099+ bindex++, p++) {
16100+ if (!p->hi_inode)
16101+ continue;
1facf9fc 16102+
027c5e7a 16103+ AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
4a4d8108
AM
16104+ new_bindex = au_br_index(sb, p->hi_id);
16105+ if (new_bindex == bindex)
16106+ continue;
1facf9fc 16107+
4a4d8108 16108+ if (new_bindex < 0) {
027c5e7a 16109+ *update = 1;
4a4d8108
AM
16110+ au_hiput(p);
16111+ p->hi_inode = NULL;
16112+ continue;
1308ab2a 16113+ }
4a4d8108 16114+
5afbbe0d
AM
16115+ if (new_bindex < iinfo->ii_btop)
16116+ iinfo->ii_btop = new_bindex;
16117+ if (iinfo->ii_bbot < new_bindex)
16118+ iinfo->ii_bbot = new_bindex;
4a4d8108 16119+ /* swap two lower inode, and loop again */
5afbbe0d 16120+ q = au_hinode(iinfo, new_bindex);
4a4d8108
AM
16121+ tmp = *q;
16122+ *q = *p;
16123+ *p = tmp;
16124+ if (tmp.hi_inode) {
16125+ bindex--;
16126+ p--;
1308ab2a 16127+ }
16128+ }
4a4d8108
AM
16129+ au_update_ibrange(inode, /*do_put_zero*/0);
16130+ e = au_dy_irefresh(inode);
16131+ if (unlikely(e && !err))
16132+ err = e;
1facf9fc 16133+
4f0767ce 16134+out:
027c5e7a
AM
16135+ AuTraceErr(err);
16136+ return err;
16137+}
16138+
b95c5147
AM
16139+void au_refresh_iop(struct inode *inode, int force_getattr)
16140+{
16141+ int type;
16142+ struct au_sbinfo *sbi = au_sbi(inode->i_sb);
16143+ const struct inode_operations *iop
16144+ = force_getattr ? aufs_iop : sbi->si_iop_array;
16145+
16146+ if (inode->i_op == iop)
16147+ return;
16148+
16149+ switch (inode->i_mode & S_IFMT) {
16150+ case S_IFDIR:
16151+ type = AuIop_DIR;
16152+ break;
16153+ case S_IFLNK:
16154+ type = AuIop_SYMLINK;
16155+ break;
16156+ default:
16157+ type = AuIop_OTHER;
16158+ break;
16159+ }
16160+
16161+ inode->i_op = iop + type;
16162+ /* unnecessary smp_wmb() */
16163+}
16164+
027c5e7a
AM
16165+int au_refresh_hinode_self(struct inode *inode)
16166+{
16167+ int err, update;
16168+
16169+ err = au_ii_refresh(inode, &update);
16170+ if (!err)
16171+ au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
16172+
16173+ AuTraceErr(err);
4a4d8108
AM
16174+ return err;
16175+}
1facf9fc 16176+
4a4d8108
AM
16177+int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
16178+{
027c5e7a 16179+ int err, e, update;
4a4d8108 16180+ unsigned int flags;
027c5e7a 16181+ umode_t mode;
5afbbe0d 16182+ aufs_bindex_t bindex, bbot;
027c5e7a 16183+ unsigned char isdir;
4a4d8108
AM
16184+ struct au_hinode *p;
16185+ struct au_iinfo *iinfo;
1facf9fc 16186+
027c5e7a 16187+ err = au_ii_refresh(inode, &update);
4a4d8108
AM
16188+ if (unlikely(err))
16189+ goto out;
16190+
16191+ update = 0;
16192+ iinfo = au_ii(inode);
5afbbe0d 16193+ p = au_hinode(iinfo, iinfo->ii_btop);
027c5e7a
AM
16194+ mode = (inode->i_mode & S_IFMT);
16195+ isdir = S_ISDIR(mode);
4a4d8108 16196+ flags = au_hi_flags(inode, isdir);
5afbbe0d
AM
16197+ bbot = au_dbbot(dentry);
16198+ for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
5527c038 16199+ struct inode *h_i, *h_inode;
4a4d8108
AM
16200+ struct dentry *h_d;
16201+
16202+ h_d = au_h_dptr(dentry, bindex);
5527c038 16203+ if (!h_d || d_is_negative(h_d))
4a4d8108
AM
16204+ continue;
16205+
5527c038
JR
16206+ h_inode = d_inode(h_d);
16207+ AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
5afbbe0d 16208+ if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
4a4d8108
AM
16209+ h_i = au_h_iptr(inode, bindex);
16210+ if (h_i) {
5527c038 16211+ if (h_i == h_inode)
4a4d8108
AM
16212+ continue;
16213+ err = -EIO;
16214+ break;
16215+ }
16216+ }
5afbbe0d
AM
16217+ if (bindex < iinfo->ii_btop)
16218+ iinfo->ii_btop = bindex;
16219+ if (iinfo->ii_bbot < bindex)
16220+ iinfo->ii_bbot = bindex;
5527c038 16221+ au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
4a4d8108 16222+ update = 1;
1308ab2a 16223+ }
4a4d8108
AM
16224+ au_update_ibrange(inode, /*do_put_zero*/0);
16225+ e = au_dy_irefresh(inode);
16226+ if (unlikely(e && !err))
16227+ err = e;
027c5e7a
AM
16228+ if (!err)
16229+ au_refresh_hinode_attr(inode, update && isdir);
4a4d8108 16230+
4f0767ce 16231+out:
4a4d8108 16232+ AuTraceErr(err);
1308ab2a 16233+ return err;
dece6358
AM
16234+}
16235+
4a4d8108 16236+static int set_inode(struct inode *inode, struct dentry *dentry)
dece6358 16237+{
4a4d8108
AM
16238+ int err;
16239+ unsigned int flags;
16240+ umode_t mode;
5afbbe0d 16241+ aufs_bindex_t bindex, btop, btail;
4a4d8108
AM
16242+ unsigned char isdir;
16243+ struct dentry *h_dentry;
16244+ struct inode *h_inode;
16245+ struct au_iinfo *iinfo;
b95c5147 16246+ struct inode_operations *iop;
dece6358 16247+
4a4d8108 16248+ IiMustWriteLock(inode);
dece6358 16249+
4a4d8108
AM
16250+ err = 0;
16251+ isdir = 0;
b95c5147 16252+ iop = au_sbi(inode->i_sb)->si_iop_array;
5afbbe0d
AM
16253+ btop = au_dbtop(dentry);
16254+ h_dentry = au_h_dptr(dentry, btop);
5527c038 16255+ h_inode = d_inode(h_dentry);
4a4d8108
AM
16256+ mode = h_inode->i_mode;
16257+ switch (mode & S_IFMT) {
16258+ case S_IFREG:
16259+ btail = au_dbtail(dentry);
b95c5147 16260+ inode->i_op = iop + AuIop_OTHER;
4a4d8108 16261+ inode->i_fop = &aufs_file_fop;
5afbbe0d 16262+ err = au_dy_iaop(inode, btop, h_inode);
4a4d8108
AM
16263+ if (unlikely(err))
16264+ goto out;
16265+ break;
16266+ case S_IFDIR:
16267+ isdir = 1;
16268+ btail = au_dbtaildir(dentry);
b95c5147 16269+ inode->i_op = iop + AuIop_DIR;
4a4d8108
AM
16270+ inode->i_fop = &aufs_dir_fop;
16271+ break;
16272+ case S_IFLNK:
16273+ btail = au_dbtail(dentry);
b95c5147 16274+ inode->i_op = iop + AuIop_SYMLINK;
4a4d8108
AM
16275+ break;
16276+ case S_IFBLK:
16277+ case S_IFCHR:
16278+ case S_IFIFO:
16279+ case S_IFSOCK:
16280+ btail = au_dbtail(dentry);
b95c5147 16281+ inode->i_op = iop + AuIop_OTHER;
38d290e6 16282+ init_special_inode(inode, mode, h_inode->i_rdev);
4a4d8108
AM
16283+ break;
16284+ default:
16285+ AuIOErr("Unknown file type 0%o\n", mode);
16286+ err = -EIO;
1308ab2a 16287+ goto out;
4a4d8108 16288+ }
dece6358 16289+
4a4d8108
AM
16290+ /* do not set hnotify for whiteouted dirs (SHWH mode) */
16291+ flags = au_hi_flags(inode, isdir);
16292+ if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
16293+ && au_ftest_hi(flags, HNOTIFY)
16294+ && dentry->d_name.len > AUFS_WH_PFX_LEN
16295+ && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
16296+ au_fclr_hi(flags, HNOTIFY);
16297+ iinfo = au_ii(inode);
5afbbe0d
AM
16298+ iinfo->ii_btop = btop;
16299+ iinfo->ii_bbot = btail;
16300+ for (bindex = btop; bindex <= btail; bindex++) {
4a4d8108
AM
16301+ h_dentry = au_h_dptr(dentry, bindex);
16302+ if (h_dentry)
16303+ au_set_h_iptr(inode, bindex,
5527c038 16304+ au_igrab(d_inode(h_dentry)), flags);
4a4d8108
AM
16305+ }
16306+ au_cpup_attr_all(inode, /*force*/1);
c1595e42
JR
16307+ /*
16308+ * to force calling aufs_get_acl() every time,
16309+ * do not call cache_no_acl() for aufs inode.
16310+ */
dece6358 16311+
4f0767ce 16312+out:
4a4d8108
AM
16313+ return err;
16314+}
dece6358 16315+
027c5e7a
AM
16316+/*
16317+ * successful returns with iinfo write_locked
16318+ * minus: errno
16319+ * zero: success, matched
16320+ * plus: no error, but unmatched
16321+ */
16322+static int reval_inode(struct inode *inode, struct dentry *dentry)
4a4d8108
AM
16323+{
16324+ int err;
cfc41e69 16325+ unsigned int gen, igflags;
5afbbe0d 16326+ aufs_bindex_t bindex, bbot;
4a4d8108 16327+ struct inode *h_inode, *h_dinode;
5527c038 16328+ struct dentry *h_dentry;
dece6358 16329+
4a4d8108
AM
16330+ /*
16331+ * before this function, if aufs got any iinfo lock, it must be only
16332+ * one, the parent dir.
16333+ * it can happen by UDBA and the obsoleted inode number.
16334+ */
16335+ err = -EIO;
16336+ if (unlikely(inode->i_ino == parent_ino(dentry)))
16337+ goto out;
16338+
027c5e7a 16339+ err = 1;
4a4d8108 16340+ ii_write_lock_new_child(inode);
5afbbe0d 16341+ h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
5527c038 16342+ h_dinode = d_inode(h_dentry);
5afbbe0d
AM
16343+ bbot = au_ibbot(inode);
16344+ for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
4a4d8108 16345+ h_inode = au_h_iptr(inode, bindex);
537831f9
AM
16346+ if (!h_inode || h_inode != h_dinode)
16347+ continue;
16348+
16349+ err = 0;
cfc41e69 16350+ gen = au_iigen(inode, &igflags);
537831f9 16351+ if (gen == au_digen(dentry)
cfc41e69 16352+ && !au_ig_ftest(igflags, HALF_REFRESHED))
4a4d8108 16353+ break;
537831f9
AM
16354+
16355+ /* fully refresh inode using dentry */
16356+ err = au_refresh_hinode(inode, dentry);
16357+ if (!err)
16358+ au_update_iigen(inode, /*half*/0);
16359+ break;
1facf9fc 16360+ }
dece6358 16361+
4a4d8108
AM
16362+ if (unlikely(err))
16363+ ii_write_unlock(inode);
4f0767ce 16364+out:
1facf9fc 16365+ return err;
16366+}
1facf9fc 16367+
4a4d8108
AM
16368+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16369+ unsigned int d_type, ino_t *ino)
1facf9fc 16370+{
4a4d8108
AM
16371+ int err;
16372+ struct mutex *mtx;
1facf9fc 16373+
b752ccd1 16374+ /* prevent hardlinked inode number from race condition */
4a4d8108 16375+ mtx = NULL;
b752ccd1 16376+ if (d_type != DT_DIR) {
4a4d8108
AM
16377+ mtx = &au_sbr(sb, bindex)->br_xino.xi_nondir_mtx;
16378+ mutex_lock(mtx);
16379+ }
16380+ err = au_xino_read(sb, bindex, h_ino, ino);
16381+ if (unlikely(err))
16382+ goto out;
1308ab2a 16383+
4a4d8108
AM
16384+ if (!*ino) {
16385+ err = -EIO;
16386+ *ino = au_xino_new_ino(sb);
16387+ if (unlikely(!*ino))
1facf9fc 16388+ goto out;
4a4d8108
AM
16389+ err = au_xino_write(sb, bindex, h_ino, *ino);
16390+ if (unlikely(err))
1308ab2a 16391+ goto out;
1308ab2a 16392+ }
1facf9fc 16393+
4f0767ce 16394+out:
b752ccd1 16395+ if (mtx)
4a4d8108 16396+ mutex_unlock(mtx);
1facf9fc 16397+ return err;
16398+}
16399+
4a4d8108
AM
16400+/* successful returns with iinfo write_locked */
16401+/* todo: return with unlocked? */
16402+struct inode *au_new_inode(struct dentry *dentry, int must_new)
1facf9fc 16403+{
5527c038 16404+ struct inode *inode, *h_inode;
4a4d8108
AM
16405+ struct dentry *h_dentry;
16406+ struct super_block *sb;
b752ccd1 16407+ struct mutex *mtx;
4a4d8108 16408+ ino_t h_ino, ino;
1716fcea 16409+ int err;
5afbbe0d 16410+ aufs_bindex_t btop;
1facf9fc 16411+
4a4d8108 16412+ sb = dentry->d_sb;
5afbbe0d
AM
16413+ btop = au_dbtop(dentry);
16414+ h_dentry = au_h_dptr(dentry, btop);
5527c038
JR
16415+ h_inode = d_inode(h_dentry);
16416+ h_ino = h_inode->i_ino;
b752ccd1
AM
16417+
16418+ /*
16419+ * stop 'race'-ing between hardlinks under different
16420+ * parents.
16421+ */
16422+ mtx = NULL;
2000de60 16423+ if (!d_is_dir(h_dentry))
5afbbe0d 16424+ mtx = &au_sbr(sb, btop)->br_xino.xi_nondir_mtx;
b752ccd1 16425+
4f0767ce 16426+new_ino:
b752ccd1
AM
16427+ if (mtx)
16428+ mutex_lock(mtx);
5afbbe0d 16429+ err = au_xino_read(sb, btop, h_ino, &ino);
4a4d8108
AM
16430+ inode = ERR_PTR(err);
16431+ if (unlikely(err))
16432+ goto out;
b752ccd1 16433+
4a4d8108
AM
16434+ if (!ino) {
16435+ ino = au_xino_new_ino(sb);
16436+ if (unlikely(!ino)) {
16437+ inode = ERR_PTR(-EIO);
dece6358
AM
16438+ goto out;
16439+ }
16440+ }
1facf9fc 16441+
4a4d8108
AM
16442+ AuDbg("i%lu\n", (unsigned long)ino);
16443+ inode = au_iget_locked(sb, ino);
16444+ err = PTR_ERR(inode);
16445+ if (IS_ERR(inode))
1facf9fc 16446+ goto out;
1facf9fc 16447+
4a4d8108
AM
16448+ AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
16449+ if (inode->i_state & I_NEW) {
16450+ ii_write_lock_new_child(inode);
16451+ err = set_inode(inode, dentry);
16452+ if (!err) {
16453+ unlock_new_inode(inode);
16454+ goto out; /* success */
16455+ }
1308ab2a 16456+
027c5e7a
AM
16457+ /*
16458+ * iget_failed() calls iput(), but we need to call
16459+ * ii_write_unlock() after iget_failed(). so dirty hack for
16460+ * i_count.
16461+ */
16462+ atomic_inc(&inode->i_count);
4a4d8108 16463+ iget_failed(inode);
027c5e7a 16464+ ii_write_unlock(inode);
5afbbe0d 16465+ au_xino_write(sb, btop, h_ino, /*ino*/0);
027c5e7a
AM
16466+ /* ignore this error */
16467+ goto out_iput;
16468+ } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
b752ccd1
AM
16469+ /*
16470+ * horrible race condition between lookup, readdir and copyup
16471+ * (or something).
16472+ */
16473+ if (mtx)
16474+ mutex_unlock(mtx);
027c5e7a
AM
16475+ err = reval_inode(inode, dentry);
16476+ if (unlikely(err < 0)) {
16477+ mtx = NULL;
16478+ goto out_iput;
16479+ }
16480+
b752ccd1
AM
16481+ if (!err) {
16482+ mtx = NULL;
4a4d8108 16483+ goto out; /* success */
b752ccd1
AM
16484+ } else if (mtx)
16485+ mutex_lock(mtx);
4a4d8108
AM
16486+ }
16487+
5527c038 16488+ if (unlikely(au_test_fs_unique_ino(h_inode)))
4a4d8108 16489+ AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
523b37e3 16490+ " b%d, %s, %pd, hi%lu, i%lu.\n",
5afbbe0d 16491+ btop, au_sbtype(h_dentry->d_sb), dentry,
4a4d8108
AM
16492+ (unsigned long)h_ino, (unsigned long)ino);
16493+ ino = 0;
5afbbe0d 16494+ err = au_xino_write(sb, btop, h_ino, /*ino*/0);
4a4d8108
AM
16495+ if (!err) {
16496+ iput(inode);
b752ccd1
AM
16497+ if (mtx)
16498+ mutex_unlock(mtx);
4a4d8108
AM
16499+ goto new_ino;
16500+ }
1308ab2a 16501+
4f0767ce 16502+out_iput:
4a4d8108 16503+ iput(inode);
4a4d8108 16504+ inode = ERR_PTR(err);
4f0767ce 16505+out:
b752ccd1
AM
16506+ if (mtx)
16507+ mutex_unlock(mtx);
4a4d8108 16508+ return inode;
1facf9fc 16509+}
16510+
4a4d8108 16511+/* ---------------------------------------------------------------------- */
1facf9fc 16512+
4a4d8108
AM
16513+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16514+ struct inode *inode)
16515+{
16516+ int err;
076b876e 16517+ struct inode *hi;
1facf9fc 16518+
4a4d8108 16519+ err = au_br_rdonly(au_sbr(sb, bindex));
1facf9fc 16520+
4a4d8108
AM
16521+ /* pseudo-link after flushed may happen out of bounds */
16522+ if (!err
16523+ && inode
5afbbe0d
AM
16524+ && au_ibtop(inode) <= bindex
16525+ && bindex <= au_ibbot(inode)) {
4a4d8108
AM
16526+ /*
16527+ * permission check is unnecessary since vfsub routine
16528+ * will be called later
16529+ */
076b876e 16530+ hi = au_h_iptr(inode, bindex);
4a4d8108
AM
16531+ if (hi)
16532+ err = IS_IMMUTABLE(hi) ? -EROFS : 0;
1facf9fc 16533+ }
16534+
4a4d8108
AM
16535+ return err;
16536+}
dece6358 16537+
4a4d8108
AM
16538+int au_test_h_perm(struct inode *h_inode, int mask)
16539+{
2dfbb274 16540+ if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
4a4d8108
AM
16541+ return 0;
16542+ return inode_permission(h_inode, mask);
16543+}
1facf9fc 16544+
4a4d8108
AM
16545+int au_test_h_perm_sio(struct inode *h_inode, int mask)
16546+{
16547+ if (au_test_nfs(h_inode->i_sb)
16548+ && (mask & MAY_WRITE)
16549+ && S_ISDIR(h_inode->i_mode))
16550+ mask |= MAY_READ; /* force permission check */
16551+ return au_test_h_perm(h_inode, mask);
1facf9fc 16552+}
7f207e10
AM
16553diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
16554--- /usr/share/empty/fs/aufs/inode.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
16555+++ linux/fs/aufs/inode.h 2016-07-25 19:05:34.814493242 +0200
16556@@ -0,0 +1,694 @@
4a4d8108 16557+/*
8cdd5066 16558+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
16559+ *
16560+ * This program, aufs is free software; you can redistribute it and/or modify
16561+ * it under the terms of the GNU General Public License as published by
16562+ * the Free Software Foundation; either version 2 of the License, or
16563+ * (at your option) any later version.
16564+ *
16565+ * This program is distributed in the hope that it will be useful,
16566+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16567+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16568+ * GNU General Public License for more details.
16569+ *
16570+ * You should have received a copy of the GNU General Public License
523b37e3 16571+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 16572+ */
1facf9fc 16573+
1308ab2a 16574+/*
4a4d8108 16575+ * inode operations
1308ab2a 16576+ */
dece6358 16577+
4a4d8108
AM
16578+#ifndef __AUFS_INODE_H__
16579+#define __AUFS_INODE_H__
dece6358 16580+
4a4d8108 16581+#ifdef __KERNEL__
1308ab2a 16582+
4a4d8108 16583+#include <linux/fsnotify.h>
4a4d8108 16584+#include "rwsem.h"
1308ab2a 16585+
4a4d8108 16586+struct vfsmount;
1facf9fc 16587+
4a4d8108
AM
16588+struct au_hnotify {
16589+#ifdef CONFIG_AUFS_HNOTIFY
16590+#ifdef CONFIG_AUFS_HFSNOTIFY
7f207e10 16591+ /* never use fsnotify_add_vfsmount_mark() */
0c5527e5 16592+ struct fsnotify_mark hn_mark;
4a4d8108 16593+#endif
7f207e10 16594+ struct inode *hn_aufs_inode; /* no get/put */
4a4d8108
AM
16595+#endif
16596+} ____cacheline_aligned_in_smp;
1facf9fc 16597+
4a4d8108
AM
16598+struct au_hinode {
16599+ struct inode *hi_inode;
16600+ aufs_bindex_t hi_id;
16601+#ifdef CONFIG_AUFS_HNOTIFY
16602+ struct au_hnotify *hi_notify;
16603+#endif
dece6358 16604+
4a4d8108
AM
16605+ /* reference to the copied-up whiteout with get/put */
16606+ struct dentry *hi_whdentry;
16607+};
dece6358 16608+
537831f9
AM
16609+/* ig_flags */
16610+#define AuIG_HALF_REFRESHED 1
16611+#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
16612+#define au_ig_fset(flags, name) \
16613+ do { (flags) |= AuIG_##name; } while (0)
16614+#define au_ig_fclr(flags, name) \
16615+ do { (flags) &= ~AuIG_##name; } while (0)
16616+
16617+struct au_iigen {
be52b249 16618+ spinlock_t ig_spin;
537831f9
AM
16619+ __u32 ig_generation, ig_flags;
16620+};
16621+
4a4d8108
AM
16622+struct au_vdir;
16623+struct au_iinfo {
7a9e40b8 16624+ struct au_iigen ii_generation;
4a4d8108 16625+ struct super_block *ii_hsb1; /* no get/put */
1facf9fc 16626+
4a4d8108 16627+ struct au_rwsem ii_rwsem;
5afbbe0d 16628+ aufs_bindex_t ii_btop, ii_bbot;
4a4d8108
AM
16629+ __u32 ii_higen;
16630+ struct au_hinode *ii_hinode;
16631+ struct au_vdir *ii_vdir;
16632+};
1facf9fc 16633+
4a4d8108
AM
16634+struct au_icntnr {
16635+ struct au_iinfo iinfo;
16636+ struct inode vfs_inode;
5afbbe0d 16637+ struct hlist_node plink;
4a4d8108 16638+} ____cacheline_aligned_in_smp;
1308ab2a 16639+
4a4d8108
AM
16640+/* au_pin flags */
16641+#define AuPin_DI_LOCKED 1
16642+#define AuPin_MNT_WRITE (1 << 1)
16643+#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
7f207e10
AM
16644+#define au_fset_pin(flags, name) \
16645+ do { (flags) |= AuPin_##name; } while (0)
16646+#define au_fclr_pin(flags, name) \
16647+ do { (flags) &= ~AuPin_##name; } while (0)
4a4d8108
AM
16648+
16649+struct au_pin {
16650+ /* input */
16651+ struct dentry *dentry;
16652+ unsigned int udba;
16653+ unsigned char lsc_di, lsc_hi, flags;
16654+ aufs_bindex_t bindex;
16655+
16656+ /* output */
16657+ struct dentry *parent;
16658+ struct au_hinode *hdir;
16659+ struct vfsmount *h_mnt;
86dc4139
AM
16660+
16661+ /* temporary unlock/relock for copyup */
16662+ struct dentry *h_dentry, *h_parent;
16663+ struct au_branch *br;
16664+ struct task_struct *task;
4a4d8108 16665+};
1facf9fc 16666+
86dc4139 16667+void au_pin_hdir_unlock(struct au_pin *p);
c1595e42 16668+int au_pin_hdir_lock(struct au_pin *p);
86dc4139 16669+int au_pin_hdir_relock(struct au_pin *p);
86dc4139
AM
16670+void au_pin_hdir_acquire_nest(struct au_pin *p);
16671+void au_pin_hdir_release(struct au_pin *p);
16672+
1308ab2a 16673+/* ---------------------------------------------------------------------- */
16674+
4a4d8108 16675+static inline struct au_iinfo *au_ii(struct inode *inode)
1facf9fc 16676+{
5afbbe0d
AM
16677+ BUG_ON(is_bad_inode(inode));
16678+ return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
4a4d8108 16679+}
1facf9fc 16680+
4a4d8108 16681+/* ---------------------------------------------------------------------- */
1facf9fc 16682+
4a4d8108
AM
16683+/* inode.c */
16684+struct inode *au_igrab(struct inode *inode);
b95c5147 16685+void au_refresh_iop(struct inode *inode, int force_getattr);
027c5e7a 16686+int au_refresh_hinode_self(struct inode *inode);
4a4d8108
AM
16687+int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
16688+int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
16689+ unsigned int d_type, ino_t *ino);
16690+struct inode *au_new_inode(struct dentry *dentry, int must_new);
16691+int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
16692+ struct inode *inode);
16693+int au_test_h_perm(struct inode *h_inode, int mask);
16694+int au_test_h_perm_sio(struct inode *h_inode, int mask);
1facf9fc 16695+
4a4d8108
AM
16696+static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
16697+ ino_t h_ino, unsigned int d_type, ino_t *ino)
16698+{
16699+#ifdef CONFIG_AUFS_SHWH
16700+ return au_ino(sb, bindex, h_ino, d_type, ino);
16701+#else
16702+ return 0;
16703+#endif
16704+}
1facf9fc 16705+
4a4d8108 16706+/* i_op.c */
b95c5147
AM
16707+enum {
16708+ AuIop_SYMLINK,
16709+ AuIop_DIR,
16710+ AuIop_OTHER,
16711+ AuIop_Last
16712+};
16713+extern struct inode_operations aufs_iop[AuIop_Last],
16714+ aufs_iop_nogetattr[AuIop_Last];
1308ab2a 16715+
4a4d8108
AM
16716+/* au_wr_dir flags */
16717+#define AuWrDir_ADD_ENTRY 1
7e9cd9fe
AM
16718+#define AuWrDir_ISDIR (1 << 1)
16719+#define AuWrDir_TMPFILE (1 << 2)
4a4d8108 16720+#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
7f207e10
AM
16721+#define au_fset_wrdir(flags, name) \
16722+ do { (flags) |= AuWrDir_##name; } while (0)
16723+#define au_fclr_wrdir(flags, name) \
16724+ do { (flags) &= ~AuWrDir_##name; } while (0)
1facf9fc 16725+
4a4d8108
AM
16726+struct au_wr_dir_args {
16727+ aufs_bindex_t force_btgt;
16728+ unsigned char flags;
16729+};
16730+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
16731+ struct au_wr_dir_args *args);
dece6358 16732+
4a4d8108
AM
16733+struct dentry *au_pinned_h_parent(struct au_pin *pin);
16734+void au_pin_init(struct au_pin *pin, struct dentry *dentry,
16735+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
16736+ unsigned int udba, unsigned char flags);
16737+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
16738+ unsigned int udba, unsigned char flags) __must_check;
16739+int au_do_pin(struct au_pin *pin) __must_check;
16740+void au_unpin(struct au_pin *pin);
c1595e42
JR
16741+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
16742+
16743+#define AuIcpup_DID_CPUP 1
16744+#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
16745+#define au_fset_icpup(flags, name) \
16746+ do { (flags) |= AuIcpup_##name; } while (0)
16747+#define au_fclr_icpup(flags, name) \
16748+ do { (flags) &= ~AuIcpup_##name; } while (0)
16749+
16750+struct au_icpup_args {
16751+ unsigned char flags;
16752+ unsigned char pin_flags;
16753+ aufs_bindex_t btgt;
16754+ unsigned int udba;
16755+ struct au_pin pin;
16756+ struct path h_path;
16757+ struct inode *h_inode;
16758+};
16759+
16760+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
16761+ struct au_icpup_args *a);
16762+
16763+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path);
1facf9fc 16764+
4a4d8108
AM
16765+/* i_op_add.c */
16766+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
16767+ struct dentry *h_parent, int isdir);
7eafdf33
AM
16768+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
16769+ dev_t dev);
4a4d8108 16770+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
7eafdf33 16771+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 16772+ bool want_excl);
b912730e
AM
16773+struct vfsub_aopen_args;
16774+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
16775+ struct vfsub_aopen_args *args);
38d290e6 16776+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
4a4d8108
AM
16777+int aufs_link(struct dentry *src_dentry, struct inode *dir,
16778+ struct dentry *dentry);
7eafdf33 16779+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
1facf9fc 16780+
4a4d8108
AM
16781+/* i_op_del.c */
16782+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
16783+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
16784+ struct dentry *h_parent, int isdir);
16785+int aufs_unlink(struct inode *dir, struct dentry *dentry);
16786+int aufs_rmdir(struct inode *dir, struct dentry *dentry);
1308ab2a 16787+
4a4d8108
AM
16788+/* i_op_ren.c */
16789+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
16790+int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
16791+ struct inode *dir, struct dentry *dentry);
1facf9fc 16792+
4a4d8108
AM
16793+/* iinfo.c */
16794+struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
16795+void au_hiput(struct au_hinode *hinode);
16796+void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
16797+ struct dentry *h_wh);
16798+unsigned int au_hi_flags(struct inode *inode, int isdir);
1308ab2a 16799+
4a4d8108
AM
16800+/* hinode flags */
16801+#define AuHi_XINO 1
16802+#define AuHi_HNOTIFY (1 << 1)
16803+#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
7f207e10
AM
16804+#define au_fset_hi(flags, name) \
16805+ do { (flags) |= AuHi_##name; } while (0)
16806+#define au_fclr_hi(flags, name) \
16807+ do { (flags) &= ~AuHi_##name; } while (0)
1facf9fc 16808+
4a4d8108
AM
16809+#ifndef CONFIG_AUFS_HNOTIFY
16810+#undef AuHi_HNOTIFY
16811+#define AuHi_HNOTIFY 0
16812+#endif
1facf9fc 16813+
4a4d8108
AM
16814+void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
16815+ struct inode *h_inode, unsigned int flags);
1facf9fc 16816+
537831f9 16817+void au_update_iigen(struct inode *inode, int half);
4a4d8108 16818+void au_update_ibrange(struct inode *inode, int do_put_zero);
1facf9fc 16819+
4a4d8108 16820+void au_icntnr_init_once(void *_c);
5afbbe0d 16821+void au_hinode_init(struct au_hinode *hinode);
4a4d8108
AM
16822+int au_iinfo_init(struct inode *inode);
16823+void au_iinfo_fin(struct inode *inode);
5afbbe0d 16824+int au_hinode_realloc(struct au_iinfo *iinfo, int nbr);
1308ab2a 16825+
e49829fe 16826+#ifdef CONFIG_PROC_FS
4a4d8108 16827+/* plink.c */
e49829fe 16828+int au_plink_maint(struct super_block *sb, int flags);
7e9cd9fe 16829+struct au_sbinfo;
e49829fe
JR
16830+void au_plink_maint_leave(struct au_sbinfo *sbinfo);
16831+int au_plink_maint_enter(struct super_block *sb);
4a4d8108
AM
16832+#ifdef CONFIG_AUFS_DEBUG
16833+void au_plink_list(struct super_block *sb);
16834+#else
16835+AuStubVoid(au_plink_list, struct super_block *sb)
16836+#endif
16837+int au_plink_test(struct inode *inode);
16838+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
16839+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
16840+ struct dentry *h_dentry);
e49829fe
JR
16841+void au_plink_put(struct super_block *sb, int verbose);
16842+void au_plink_clean(struct super_block *sb, int verbose);
4a4d8108 16843+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
e49829fe
JR
16844+#else
16845+AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
16846+AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
16847+AuStubInt0(au_plink_maint_enter, struct super_block *sb);
16848+AuStubVoid(au_plink_list, struct super_block *sb);
16849+AuStubInt0(au_plink_test, struct inode *inode);
16850+AuStub(struct dentry *, au_plink_lkup, return NULL,
16851+ struct inode *inode, aufs_bindex_t bindex);
16852+AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
16853+ struct dentry *h_dentry);
16854+AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
16855+AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
16856+AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
16857+#endif /* CONFIG_PROC_FS */
1facf9fc 16858+
c1595e42
JR
16859+#ifdef CONFIG_AUFS_XATTR
16860+/* xattr.c */
7e9cd9fe
AM
16861+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
16862+ unsigned int verbose);
c1595e42 16863+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
5afbbe0d
AM
16864+ssize_t aufs_getxattr(struct dentry *dentry, struct inode *inode,
16865+ const char *name, void *value, size_t size);
16866+int aufs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
16867+ const void *value, size_t size, int flags);
c1595e42
JR
16868+int aufs_removexattr(struct dentry *dentry, const char *name);
16869+
16870+/* void au_xattr_init(struct super_block *sb); */
16871+#else
16872+AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe 16873+ int ignore_flags, unsigned int verbose);
c1595e42
JR
16874+/* AuStubVoid(au_xattr_init, struct super_block *sb); */
16875+#endif
16876+
16877+#ifdef CONFIG_FS_POSIX_ACL
16878+struct posix_acl *aufs_get_acl(struct inode *inode, int type);
16879+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
16880+#endif
16881+
16882+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
16883+enum {
16884+ AU_XATTR_SET,
16885+ AU_XATTR_REMOVE,
16886+ AU_ACL_SET
16887+};
16888+
16889+struct au_srxattr {
16890+ int type;
16891+ union {
16892+ struct {
16893+ const char *name;
16894+ const void *value;
16895+ size_t size;
16896+ int flags;
16897+ } set;
16898+ struct {
16899+ const char *name;
16900+ } remove;
16901+ struct {
16902+ struct posix_acl *acl;
16903+ int type;
16904+ } acl_set;
16905+ } u;
16906+};
5afbbe0d
AM
16907+ssize_t au_srxattr(struct dentry *dentry, struct inode *inode,
16908+ struct au_srxattr *arg);
c1595e42
JR
16909+#endif
16910+
4a4d8108 16911+/* ---------------------------------------------------------------------- */
1308ab2a 16912+
4a4d8108
AM
16913+/* lock subclass for iinfo */
16914+enum {
16915+ AuLsc_II_CHILD, /* child first */
16916+ AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
16917+ AuLsc_II_CHILD3, /* copyup dirs */
16918+ AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
16919+ AuLsc_II_PARENT2,
16920+ AuLsc_II_PARENT3, /* copyup dirs */
16921+ AuLsc_II_NEW_CHILD
16922+};
1308ab2a 16923+
1facf9fc 16924+/*
4a4d8108
AM
16925+ * ii_read_lock_child, ii_write_lock_child,
16926+ * ii_read_lock_child2, ii_write_lock_child2,
16927+ * ii_read_lock_child3, ii_write_lock_child3,
16928+ * ii_read_lock_parent, ii_write_lock_parent,
16929+ * ii_read_lock_parent2, ii_write_lock_parent2,
16930+ * ii_read_lock_parent3, ii_write_lock_parent3,
16931+ * ii_read_lock_new_child, ii_write_lock_new_child,
1facf9fc 16932+ */
4a4d8108
AM
16933+#define AuReadLockFunc(name, lsc) \
16934+static inline void ii_read_lock_##name(struct inode *i) \
16935+{ \
16936+ au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16937+}
16938+
16939+#define AuWriteLockFunc(name, lsc) \
16940+static inline void ii_write_lock_##name(struct inode *i) \
16941+{ \
16942+ au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
16943+}
16944+
16945+#define AuRWLockFuncs(name, lsc) \
16946+ AuReadLockFunc(name, lsc) \
16947+ AuWriteLockFunc(name, lsc)
16948+
16949+AuRWLockFuncs(child, CHILD);
16950+AuRWLockFuncs(child2, CHILD2);
16951+AuRWLockFuncs(child3, CHILD3);
16952+AuRWLockFuncs(parent, PARENT);
16953+AuRWLockFuncs(parent2, PARENT2);
16954+AuRWLockFuncs(parent3, PARENT3);
16955+AuRWLockFuncs(new_child, NEW_CHILD);
16956+
16957+#undef AuReadLockFunc
16958+#undef AuWriteLockFunc
16959+#undef AuRWLockFuncs
1facf9fc 16960+
16961+/*
4a4d8108 16962+ * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
1facf9fc 16963+ */
4a4d8108 16964+AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
1facf9fc 16965+
4a4d8108
AM
16966+#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
16967+#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
16968+#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
1facf9fc 16969+
4a4d8108 16970+/* ---------------------------------------------------------------------- */
1308ab2a 16971+
027c5e7a
AM
16972+static inline void au_icntnr_init(struct au_icntnr *c)
16973+{
16974+#ifdef CONFIG_AUFS_DEBUG
16975+ c->vfs_inode.i_mode = 0;
16976+#endif
16977+}
16978+
cfc41e69 16979+static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
4a4d8108 16980+{
537831f9
AM
16981+ unsigned int gen;
16982+ struct au_iinfo *iinfo;
be52b249 16983+ struct au_iigen *iigen;
537831f9
AM
16984+
16985+ iinfo = au_ii(inode);
be52b249
AM
16986+ iigen = &iinfo->ii_generation;
16987+ spin_lock(&iigen->ig_spin);
cfc41e69
AM
16988+ if (igflags)
16989+ *igflags = iigen->ig_flags;
be52b249
AM
16990+ gen = iigen->ig_generation;
16991+ spin_unlock(&iigen->ig_spin);
537831f9
AM
16992+
16993+ return gen;
4a4d8108 16994+}
1308ab2a 16995+
4a4d8108
AM
16996+/* tiny test for inode number */
16997+/* tmpfs generation is too rough */
16998+static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
16999+{
17000+ struct au_iinfo *iinfo;
1308ab2a 17001+
4a4d8108
AM
17002+ iinfo = au_ii(inode);
17003+ AuRwMustAnyLock(&iinfo->ii_rwsem);
17004+ return !(iinfo->ii_hsb1 == h_inode->i_sb
17005+ && iinfo->ii_higen == h_inode->i_generation);
17006+}
1308ab2a 17007+
4a4d8108
AM
17008+static inline void au_iigen_dec(struct inode *inode)
17009+{
537831f9 17010+ struct au_iinfo *iinfo;
be52b249 17011+ struct au_iigen *iigen;
537831f9
AM
17012+
17013+ iinfo = au_ii(inode);
be52b249
AM
17014+ iigen = &iinfo->ii_generation;
17015+ spin_lock(&iigen->ig_spin);
17016+ iigen->ig_generation--;
17017+ spin_unlock(&iigen->ig_spin);
027c5e7a
AM
17018+}
17019+
17020+static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
17021+{
17022+ int err;
17023+
17024+ err = 0;
537831f9 17025+ if (unlikely(inode && au_iigen(inode, NULL) != sigen))
027c5e7a
AM
17026+ err = -EIO;
17027+
17028+ return err;
4a4d8108 17029+}
1308ab2a 17030+
4a4d8108 17031+/* ---------------------------------------------------------------------- */
1308ab2a 17032+
5afbbe0d
AM
17033+static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
17034+ aufs_bindex_t bindex)
17035+{
17036+ return iinfo->ii_hinode + bindex;
17037+}
17038+
17039+static inline int au_is_bad_inode(struct inode *inode)
17040+{
17041+ return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
17042+}
17043+
4a4d8108
AM
17044+static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
17045+ aufs_bindex_t bindex)
17046+{
17047+ IiMustAnyLock(inode);
5afbbe0d 17048+ return au_hinode(au_ii(inode), bindex)->hi_id;
4a4d8108 17049+}
1308ab2a 17050+
5afbbe0d 17051+static inline aufs_bindex_t au_ibtop(struct inode *inode)
4a4d8108
AM
17052+{
17053+ IiMustAnyLock(inode);
5afbbe0d 17054+ return au_ii(inode)->ii_btop;
4a4d8108 17055+}
1308ab2a 17056+
5afbbe0d 17057+static inline aufs_bindex_t au_ibbot(struct inode *inode)
4a4d8108
AM
17058+{
17059+ IiMustAnyLock(inode);
5afbbe0d 17060+ return au_ii(inode)->ii_bbot;
4a4d8108 17061+}
1308ab2a 17062+
4a4d8108
AM
17063+static inline struct au_vdir *au_ivdir(struct inode *inode)
17064+{
17065+ IiMustAnyLock(inode);
17066+ return au_ii(inode)->ii_vdir;
17067+}
1308ab2a 17068+
4a4d8108
AM
17069+static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
17070+{
17071+ IiMustAnyLock(inode);
5afbbe0d 17072+ return au_hinode(au_ii(inode), bindex)->hi_whdentry;
4a4d8108 17073+}
1308ab2a 17074+
5afbbe0d 17075+static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17076+{
4a4d8108 17077+ IiMustWriteLock(inode);
5afbbe0d 17078+ au_ii(inode)->ii_btop = bindex;
4a4d8108 17079+}
1308ab2a 17080+
5afbbe0d 17081+static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
4a4d8108
AM
17082+{
17083+ IiMustWriteLock(inode);
5afbbe0d 17084+ au_ii(inode)->ii_bbot = bindex;
1308ab2a 17085+}
17086+
4a4d8108
AM
17087+static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
17088+{
17089+ IiMustWriteLock(inode);
17090+ au_ii(inode)->ii_vdir = vdir;
17091+}
1facf9fc 17092+
4a4d8108 17093+static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
1308ab2a 17094+{
4a4d8108 17095+ IiMustAnyLock(inode);
5afbbe0d 17096+ return au_hinode(au_ii(inode), bindex);
4a4d8108 17097+}
dece6358 17098+
4a4d8108 17099+/* ---------------------------------------------------------------------- */
1facf9fc 17100+
4a4d8108
AM
17101+static inline struct dentry *au_pinned_parent(struct au_pin *pin)
17102+{
17103+ if (pin)
17104+ return pin->parent;
17105+ return NULL;
1facf9fc 17106+}
17107+
4a4d8108 17108+static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
1facf9fc 17109+{
4a4d8108
AM
17110+ if (pin && pin->hdir)
17111+ return pin->hdir->hi_inode;
17112+ return NULL;
1308ab2a 17113+}
1facf9fc 17114+
4a4d8108
AM
17115+static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
17116+{
17117+ if (pin)
17118+ return pin->hdir;
17119+ return NULL;
17120+}
1facf9fc 17121+
4a4d8108 17122+static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
1308ab2a 17123+{
4a4d8108
AM
17124+ if (pin)
17125+ pin->dentry = dentry;
17126+}
1308ab2a 17127+
4a4d8108
AM
17128+static inline void au_pin_set_parent_lflag(struct au_pin *pin,
17129+ unsigned char lflag)
17130+{
17131+ if (pin) {
7f207e10 17132+ if (lflag)
4a4d8108 17133+ au_fset_pin(pin->flags, DI_LOCKED);
7f207e10 17134+ else
4a4d8108 17135+ au_fclr_pin(pin->flags, DI_LOCKED);
1308ab2a 17136+ }
4a4d8108
AM
17137+}
17138+
7e9cd9fe 17139+#if 0 /* reserved */
4a4d8108
AM
17140+static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
17141+{
17142+ if (pin) {
17143+ dput(pin->parent);
17144+ pin->parent = dget(parent);
1facf9fc 17145+ }
4a4d8108 17146+}
7e9cd9fe 17147+#endif
1facf9fc 17148+
4a4d8108
AM
17149+/* ---------------------------------------------------------------------- */
17150+
027c5e7a 17151+struct au_branch;
4a4d8108
AM
17152+#ifdef CONFIG_AUFS_HNOTIFY
17153+struct au_hnotify_op {
17154+ void (*ctl)(struct au_hinode *hinode, int do_set);
027c5e7a 17155+ int (*alloc)(struct au_hinode *hinode);
7eafdf33
AM
17156+
17157+ /*
17158+ * if it returns true, the the caller should free hinode->hi_notify,
17159+ * otherwise ->free() frees it.
17160+ */
17161+ int (*free)(struct au_hinode *hinode,
17162+ struct au_hnotify *hn) __must_check;
4a4d8108
AM
17163+
17164+ void (*fin)(void);
17165+ int (*init)(void);
027c5e7a
AM
17166+
17167+ int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
17168+ void (*fin_br)(struct au_branch *br);
17169+ int (*init_br)(struct au_branch *br, int perm);
4a4d8108
AM
17170+};
17171+
17172+/* hnotify.c */
027c5e7a 17173+int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
4a4d8108
AM
17174+void au_hn_free(struct au_hinode *hinode);
17175+void au_hn_ctl(struct au_hinode *hinode, int do_set);
17176+void au_hn_reset(struct inode *inode, unsigned int flags);
17177+int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17178+ struct qstr *h_child_qstr, struct inode *h_child_inode);
027c5e7a
AM
17179+int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
17180+int au_hnotify_init_br(struct au_branch *br, int perm);
17181+void au_hnotify_fin_br(struct au_branch *br);
4a4d8108
AM
17182+int __init au_hnotify_init(void);
17183+void au_hnotify_fin(void);
17184+
7f207e10 17185+/* hfsnotify.c */
4a4d8108
AM
17186+extern const struct au_hnotify_op au_hnotify_op;
17187+
17188+static inline
17189+void au_hn_init(struct au_hinode *hinode)
17190+{
17191+ hinode->hi_notify = NULL;
1308ab2a 17192+}
17193+
53392da6
AM
17194+static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
17195+{
17196+ return hinode->hi_notify;
17197+}
17198+
4a4d8108 17199+#else
c1595e42
JR
17200+AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
17201+ struct au_hinode *hinode __maybe_unused,
17202+ struct inode *inode __maybe_unused)
17203+AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
4a4d8108
AM
17204+AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
17205+AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
17206+ int do_set __maybe_unused)
17207+AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
17208+ unsigned int flags __maybe_unused)
027c5e7a
AM
17209+AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
17210+ struct au_branch *br __maybe_unused,
17211+ int perm __maybe_unused)
17212+AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
17213+ int perm __maybe_unused)
17214+AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
4a4d8108
AM
17215+AuStubInt0(__init au_hnotify_init, void)
17216+AuStubVoid(au_hnotify_fin, void)
17217+AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
17218+#endif /* CONFIG_AUFS_HNOTIFY */
17219+
17220+static inline void au_hn_suspend(struct au_hinode *hdir)
17221+{
17222+ au_hn_ctl(hdir, /*do_set*/0);
1308ab2a 17223+}
17224+
4a4d8108 17225+static inline void au_hn_resume(struct au_hinode *hdir)
1308ab2a 17226+{
4a4d8108
AM
17227+ au_hn_ctl(hdir, /*do_set*/1);
17228+}
1308ab2a 17229+
5afbbe0d 17230+static inline void au_hn_inode_lock(struct au_hinode *hdir)
4a4d8108 17231+{
febd17d6 17232+ inode_lock(hdir->hi_inode);
4a4d8108
AM
17233+ au_hn_suspend(hdir);
17234+}
dece6358 17235+
5afbbe0d 17236+static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
4a4d8108
AM
17237+ unsigned int sc __maybe_unused)
17238+{
febd17d6 17239+ inode_lock_nested(hdir->hi_inode, sc);
4a4d8108 17240+ au_hn_suspend(hdir);
1facf9fc 17241+}
1facf9fc 17242+
5afbbe0d 17243+static inline void au_hn_inode_unlock(struct au_hinode *hdir)
4a4d8108
AM
17244+{
17245+ au_hn_resume(hdir);
febd17d6 17246+ inode_unlock(hdir->hi_inode);
4a4d8108
AM
17247+}
17248+
17249+#endif /* __KERNEL__ */
17250+#endif /* __AUFS_INODE_H__ */
7f207e10
AM
17251diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
17252--- /usr/share/empty/fs/aufs/ioctl.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 17253+++ linux/fs/aufs/ioctl.c 2016-07-25 19:05:34.814493242 +0200
c1595e42 17254@@ -0,0 +1,219 @@
4a4d8108 17255+/*
8cdd5066 17256+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
17257+ *
17258+ * This program, aufs is free software; you can redistribute it and/or modify
17259+ * it under the terms of the GNU General Public License as published by
17260+ * the Free Software Foundation; either version 2 of the License, or
17261+ * (at your option) any later version.
17262+ *
17263+ * This program is distributed in the hope that it will be useful,
17264+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17265+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17266+ * GNU General Public License for more details.
17267+ *
17268+ * You should have received a copy of the GNU General Public License
523b37e3 17269+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
17270+ */
17271+
17272+/*
17273+ * ioctl
17274+ * plink-management and readdir in userspace.
17275+ * assist the pathconf(3) wrapper library.
c2b27bf2 17276+ * move-down
076b876e 17277+ * File-based Hierarchical Storage Management.
4a4d8108
AM
17278+ */
17279+
c2b27bf2
AM
17280+#include <linux/compat.h>
17281+#include <linux/file.h>
4a4d8108
AM
17282+#include "aufs.h"
17283+
1e00d052 17284+static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
4a4d8108
AM
17285+{
17286+ int err, fd;
5afbbe0d 17287+ aufs_bindex_t wbi, bindex, bbot;
4a4d8108
AM
17288+ struct file *h_file;
17289+ struct super_block *sb;
17290+ struct dentry *root;
1e00d052
AM
17291+ struct au_branch *br;
17292+ struct aufs_wbr_fd wbrfd = {
17293+ .oflags = au_dir_roflags,
17294+ .brid = -1
17295+ };
17296+ const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
17297+ | O_NOATIME | O_CLOEXEC;
4a4d8108 17298+
1e00d052
AM
17299+ AuDebugOn(wbrfd.oflags & ~valid);
17300+
17301+ if (arg) {
17302+ err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
17303+ if (unlikely(err)) {
17304+ err = -EFAULT;
17305+ goto out;
17306+ }
17307+
17308+ err = -EINVAL;
17309+ AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
17310+ wbrfd.oflags |= au_dir_roflags;
17311+ AuDbg("0%o\n", wbrfd.oflags);
17312+ if (unlikely(wbrfd.oflags & ~valid))
17313+ goto out;
17314+ }
17315+
2000de60 17316+ fd = get_unused_fd_flags(0);
1e00d052
AM
17317+ err = fd;
17318+ if (unlikely(fd < 0))
4a4d8108 17319+ goto out;
4a4d8108 17320+
1e00d052 17321+ h_file = ERR_PTR(-EINVAL);
4a4d8108 17322+ wbi = 0;
1e00d052 17323+ br = NULL;
4a4d8108
AM
17324+ sb = path->dentry->d_sb;
17325+ root = sb->s_root;
17326+ aufs_read_lock(root, AuLock_IR);
5afbbe0d 17327+ bbot = au_sbbot(sb);
1e00d052
AM
17328+ if (wbrfd.brid >= 0) {
17329+ wbi = au_br_index(sb, wbrfd.brid);
5afbbe0d 17330+ if (unlikely(wbi < 0 || wbi > bbot))
1e00d052
AM
17331+ goto out_unlock;
17332+ }
17333+
17334+ h_file = ERR_PTR(-ENOENT);
17335+ br = au_sbr(sb, wbi);
17336+ if (!au_br_writable(br->br_perm)) {
17337+ if (arg)
17338+ goto out_unlock;
17339+
17340+ bindex = wbi + 1;
17341+ wbi = -1;
5afbbe0d 17342+ for (; bindex <= bbot; bindex++) {
1e00d052
AM
17343+ br = au_sbr(sb, bindex);
17344+ if (au_br_writable(br->br_perm)) {
4a4d8108 17345+ wbi = bindex;
1e00d052 17346+ br = au_sbr(sb, wbi);
4a4d8108
AM
17347+ break;
17348+ }
17349+ }
4a4d8108
AM
17350+ }
17351+ AuDbg("wbi %d\n", wbi);
1e00d052 17352+ if (wbi >= 0)
392086de
AM
17353+ h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
17354+ /*force_wr*/0);
1e00d052
AM
17355+
17356+out_unlock:
4a4d8108
AM
17357+ aufs_read_unlock(root, AuLock_IR);
17358+ err = PTR_ERR(h_file);
17359+ if (IS_ERR(h_file))
17360+ goto out_fd;
17361+
5afbbe0d 17362+ au_br_put(br); /* cf. au_h_open() */
4a4d8108
AM
17363+ fd_install(fd, h_file);
17364+ err = fd;
17365+ goto out; /* success */
17366+
4f0767ce 17367+out_fd:
4a4d8108 17368+ put_unused_fd(fd);
4f0767ce 17369+out:
1e00d052 17370+ AuTraceErr(err);
4a4d8108
AM
17371+ return err;
17372+}
17373+
17374+/* ---------------------------------------------------------------------- */
17375+
17376+long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
17377+{
17378+ long err;
c1595e42 17379+ struct dentry *dentry;
4a4d8108
AM
17380+
17381+ switch (cmd) {
4a4d8108
AM
17382+ case AUFS_CTL_RDU:
17383+ case AUFS_CTL_RDU_INO:
17384+ err = au_rdu_ioctl(file, cmd, arg);
17385+ break;
17386+
17387+ case AUFS_CTL_WBR_FD:
1e00d052 17388+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
17389+ break;
17390+
027c5e7a
AM
17391+ case AUFS_CTL_IBUSY:
17392+ err = au_ibusy_ioctl(file, arg);
17393+ break;
17394+
076b876e
AM
17395+ case AUFS_CTL_BRINFO:
17396+ err = au_brinfo_ioctl(file, arg);
17397+ break;
17398+
17399+ case AUFS_CTL_FHSM_FD:
2000de60 17400+ dentry = file->f_path.dentry;
c1595e42
JR
17401+ if (IS_ROOT(dentry))
17402+ err = au_fhsm_fd(dentry->d_sb, arg);
17403+ else
17404+ err = -ENOTTY;
076b876e
AM
17405+ break;
17406+
4a4d8108
AM
17407+ default:
17408+ /* do not call the lower */
17409+ AuDbg("0x%x\n", cmd);
17410+ err = -ENOTTY;
17411+ }
17412+
17413+ AuTraceErr(err);
17414+ return err;
17415+}
17416+
17417+long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
17418+{
17419+ long err;
17420+
17421+ switch (cmd) {
c2b27bf2 17422+ case AUFS_CTL_MVDOWN:
2000de60 17423+ err = au_mvdown(file->f_path.dentry, (void __user *)arg);
c2b27bf2
AM
17424+ break;
17425+
4a4d8108 17426+ case AUFS_CTL_WBR_FD:
1e00d052 17427+ err = au_wbr_fd(&file->f_path, (void __user *)arg);
4a4d8108
AM
17428+ break;
17429+
17430+ default:
17431+ /* do not call the lower */
17432+ AuDbg("0x%x\n", cmd);
17433+ err = -ENOTTY;
17434+ }
17435+
17436+ AuTraceErr(err);
17437+ return err;
17438+}
b752ccd1
AM
17439+
17440+#ifdef CONFIG_COMPAT
17441+long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
17442+ unsigned long arg)
17443+{
17444+ long err;
17445+
17446+ switch (cmd) {
17447+ case AUFS_CTL_RDU:
17448+ case AUFS_CTL_RDU_INO:
17449+ err = au_rdu_compat_ioctl(file, cmd, arg);
17450+ break;
17451+
027c5e7a
AM
17452+ case AUFS_CTL_IBUSY:
17453+ err = au_ibusy_compat_ioctl(file, arg);
17454+ break;
17455+
076b876e
AM
17456+ case AUFS_CTL_BRINFO:
17457+ err = au_brinfo_compat_ioctl(file, arg);
17458+ break;
17459+
b752ccd1
AM
17460+ default:
17461+ err = aufs_ioctl_dir(file, cmd, arg);
17462+ }
17463+
17464+ AuTraceErr(err);
17465+ return err;
17466+}
17467+
b752ccd1
AM
17468+long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
17469+ unsigned long arg)
17470+{
17471+ return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
17472+}
17473+#endif
7f207e10
AM
17474diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
17475--- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
17476+++ linux/fs/aufs/i_op_add.c 2016-07-25 19:05:34.814493242 +0200
17477@@ -0,0 +1,924 @@
4a4d8108 17478+/*
8cdd5066 17479+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
17480+ *
17481+ * This program, aufs is free software; you can redistribute it and/or modify
17482+ * it under the terms of the GNU General Public License as published by
17483+ * the Free Software Foundation; either version 2 of the License, or
17484+ * (at your option) any later version.
17485+ *
17486+ * This program is distributed in the hope that it will be useful,
17487+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
17488+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17489+ * GNU General Public License for more details.
17490+ *
17491+ * You should have received a copy of the GNU General Public License
523b37e3 17492+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108
AM
17493+ */
17494+
17495+/*
17496+ * inode operations (add entry)
17497+ */
17498+
17499+#include "aufs.h"
17500+
17501+/*
17502+ * final procedure of adding a new entry, except link(2).
17503+ * remove whiteout, instantiate, copyup the parent dir's times and size
17504+ * and update version.
17505+ * if it failed, re-create the removed whiteout.
17506+ */
17507+static int epilog(struct inode *dir, aufs_bindex_t bindex,
17508+ struct dentry *wh_dentry, struct dentry *dentry)
17509+{
17510+ int err, rerr;
17511+ aufs_bindex_t bwh;
17512+ struct path h_path;
076b876e 17513+ struct super_block *sb;
4a4d8108
AM
17514+ struct inode *inode, *h_dir;
17515+ struct dentry *wh;
17516+
17517+ bwh = -1;
076b876e 17518+ sb = dir->i_sb;
4a4d8108 17519+ if (wh_dentry) {
5527c038 17520+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
4a4d8108
AM
17521+ IMustLock(h_dir);
17522+ AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
17523+ bwh = au_dbwh(dentry);
17524+ h_path.dentry = wh_dentry;
076b876e 17525+ h_path.mnt = au_sbr_mnt(sb, bindex);
4a4d8108
AM
17526+ err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
17527+ dentry);
17528+ if (unlikely(err))
17529+ goto out;
17530+ }
17531+
17532+ inode = au_new_inode(dentry, /*must_new*/1);
17533+ if (!IS_ERR(inode)) {
17534+ d_instantiate(dentry, inode);
5527c038 17535+ dir = d_inode(dentry->d_parent); /* dir inode is locked */
4a4d8108 17536+ IMustLock(dir);
b912730e 17537+ au_dir_ts(dir, bindex);
4a4d8108 17538+ dir->i_version++;
076b876e 17539+ au_fhsm_wrote(sb, bindex, /*force*/0);
4a4d8108
AM
17540+ return 0; /* success */
17541+ }
17542+
17543+ err = PTR_ERR(inode);
17544+ if (!wh_dentry)
17545+ goto out;
17546+
17547+ /* revert */
17548+ /* dir inode is locked */
17549+ wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
17550+ rerr = PTR_ERR(wh);
17551+ if (IS_ERR(wh)) {
523b37e3
AM
17552+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
17553+ dentry, err, rerr);
4a4d8108
AM
17554+ err = -EIO;
17555+ } else
17556+ dput(wh);
17557+
4f0767ce 17558+out:
4a4d8108
AM
17559+ return err;
17560+}
17561+
027c5e7a
AM
17562+static int au_d_may_add(struct dentry *dentry)
17563+{
17564+ int err;
17565+
17566+ err = 0;
17567+ if (unlikely(d_unhashed(dentry)))
17568+ err = -ENOENT;
5527c038 17569+ if (unlikely(d_really_is_positive(dentry)))
027c5e7a
AM
17570+ err = -EEXIST;
17571+ return err;
17572+}
17573+
4a4d8108
AM
17574+/*
17575+ * simple tests for the adding inode operations.
17576+ * following the checks in vfs, plus the parent-child relationship.
17577+ */
17578+int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
17579+ struct dentry *h_parent, int isdir)
17580+{
17581+ int err;
17582+ umode_t h_mode;
17583+ struct dentry *h_dentry;
17584+ struct inode *h_inode;
17585+
17586+ err = -ENAMETOOLONG;
17587+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17588+ goto out;
17589+
17590+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 17591+ if (d_really_is_negative(dentry)) {
4a4d8108 17592+ err = -EEXIST;
5527c038 17593+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
17594+ goto out;
17595+ } else {
17596+ /* rename(2) case */
17597+ err = -EIO;
5527c038
JR
17598+ if (unlikely(d_is_negative(h_dentry)))
17599+ goto out;
17600+ h_inode = d_inode(h_dentry);
17601+ if (unlikely(!h_inode->i_nlink))
4a4d8108
AM
17602+ goto out;
17603+
17604+ h_mode = h_inode->i_mode;
17605+ if (!isdir) {
17606+ err = -EISDIR;
17607+ if (unlikely(S_ISDIR(h_mode)))
17608+ goto out;
17609+ } else if (unlikely(!S_ISDIR(h_mode))) {
17610+ err = -ENOTDIR;
17611+ goto out;
17612+ }
17613+ }
17614+
17615+ err = 0;
17616+ /* expected parent dir is locked */
17617+ if (unlikely(h_parent != h_dentry->d_parent))
17618+ err = -EIO;
17619+
4f0767ce 17620+out:
4a4d8108
AM
17621+ AuTraceErr(err);
17622+ return err;
17623+}
17624+
17625+/*
17626+ * initial procedure of adding a new entry.
17627+ * prepare writable branch and the parent dir, lock it,
17628+ * and lookup whiteout for the new entry.
17629+ */
17630+static struct dentry*
17631+lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
17632+ struct dentry *src_dentry, struct au_pin *pin,
17633+ struct au_wr_dir_args *wr_dir_args)
17634+{
17635+ struct dentry *wh_dentry, *h_parent;
17636+ struct super_block *sb;
17637+ struct au_branch *br;
17638+ int err;
17639+ unsigned int udba;
17640+ aufs_bindex_t bcpup;
17641+
523b37e3 17642+ AuDbg("%pd\n", dentry);
4a4d8108
AM
17643+
17644+ err = au_wr_dir(dentry, src_dentry, wr_dir_args);
17645+ bcpup = err;
17646+ wh_dentry = ERR_PTR(err);
17647+ if (unlikely(err < 0))
17648+ goto out;
17649+
17650+ sb = dentry->d_sb;
17651+ udba = au_opt_udba(sb);
17652+ err = au_pin(pin, dentry, bcpup, udba,
17653+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
17654+ wh_dentry = ERR_PTR(err);
17655+ if (unlikely(err))
17656+ goto out;
17657+
17658+ h_parent = au_pinned_h_parent(pin);
17659+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 17660+ && au_dbtop(dentry) == bcpup)
4a4d8108
AM
17661+ err = au_may_add(dentry, bcpup, h_parent,
17662+ au_ftest_wrdir(wr_dir_args->flags, ISDIR));
17663+ else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
17664+ err = -ENAMETOOLONG;
17665+ wh_dentry = ERR_PTR(err);
17666+ if (unlikely(err))
17667+ goto out_unpin;
17668+
17669+ br = au_sbr(sb, bcpup);
17670+ if (dt) {
17671+ struct path tmp = {
17672+ .dentry = h_parent,
86dc4139 17673+ .mnt = au_br_mnt(br)
4a4d8108
AM
17674+ };
17675+ au_dtime_store(dt, au_pinned_parent(pin), &tmp);
17676+ }
17677+
17678+ wh_dentry = NULL;
17679+ if (bcpup != au_dbwh(dentry))
17680+ goto out; /* success */
17681+
2000de60
JR
17682+ /*
17683+ * ENAMETOOLONG here means that if we allowed create such name, then it
17684+ * would not be able to removed in the future. So we don't allow such
17685+ * name here and we don't handle ENAMETOOLONG differently here.
17686+ */
4a4d8108
AM
17687+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
17688+
4f0767ce 17689+out_unpin:
4a4d8108
AM
17690+ if (IS_ERR(wh_dentry))
17691+ au_unpin(pin);
4f0767ce 17692+out:
4a4d8108
AM
17693+ return wh_dentry;
17694+}
17695+
17696+/* ---------------------------------------------------------------------- */
17697+
17698+enum { Mknod, Symlink, Creat };
17699+struct simple_arg {
17700+ int type;
17701+ union {
17702+ struct {
b912730e
AM
17703+ umode_t mode;
17704+ bool want_excl;
17705+ bool try_aopen;
17706+ struct vfsub_aopen_args *aopen;
4a4d8108
AM
17707+ } c;
17708+ struct {
17709+ const char *symname;
17710+ } s;
17711+ struct {
7eafdf33 17712+ umode_t mode;
4a4d8108
AM
17713+ dev_t dev;
17714+ } m;
17715+ } u;
17716+};
17717+
17718+static int add_simple(struct inode *dir, struct dentry *dentry,
17719+ struct simple_arg *arg)
17720+{
076b876e 17721+ int err, rerr;
5afbbe0d 17722+ aufs_bindex_t btop;
4a4d8108 17723+ unsigned char created;
b912730e
AM
17724+ const unsigned char try_aopen
17725+ = (arg->type == Creat && arg->u.c.try_aopen);
4a4d8108
AM
17726+ struct dentry *wh_dentry, *parent;
17727+ struct inode *h_dir;
b912730e
AM
17728+ struct super_block *sb;
17729+ struct au_branch *br;
c2b27bf2
AM
17730+ /* to reuduce stack size */
17731+ struct {
17732+ struct au_dtime dt;
17733+ struct au_pin pin;
17734+ struct path h_path;
17735+ struct au_wr_dir_args wr_dir_args;
17736+ } *a;
4a4d8108 17737+
523b37e3 17738+ AuDbg("%pd\n", dentry);
4a4d8108
AM
17739+ IMustLock(dir);
17740+
c2b27bf2
AM
17741+ err = -ENOMEM;
17742+ a = kmalloc(sizeof(*a), GFP_NOFS);
17743+ if (unlikely(!a))
17744+ goto out;
17745+ a->wr_dir_args.force_btgt = -1;
17746+ a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
17747+
4a4d8108 17748+ parent = dentry->d_parent; /* dir inode is locked */
b912730e
AM
17749+ if (!try_aopen) {
17750+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
17751+ if (unlikely(err))
17752+ goto out_free;
17753+ }
027c5e7a
AM
17754+ err = au_d_may_add(dentry);
17755+ if (unlikely(err))
17756+ goto out_unlock;
b912730e
AM
17757+ if (!try_aopen)
17758+ di_write_lock_parent(parent);
c2b27bf2
AM
17759+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
17760+ &a->pin, &a->wr_dir_args);
4a4d8108
AM
17761+ err = PTR_ERR(wh_dentry);
17762+ if (IS_ERR(wh_dentry))
027c5e7a 17763+ goto out_parent;
4a4d8108 17764+
5afbbe0d 17765+ btop = au_dbtop(dentry);
b912730e 17766+ sb = dentry->d_sb;
5afbbe0d
AM
17767+ br = au_sbr(sb, btop);
17768+ a->h_path.dentry = au_h_dptr(dentry, btop);
b912730e 17769+ a->h_path.mnt = au_br_mnt(br);
c2b27bf2 17770+ h_dir = au_pinned_h_dir(&a->pin);
4a4d8108
AM
17771+ switch (arg->type) {
17772+ case Creat:
b912730e
AM
17773+ err = 0;
17774+ if (!try_aopen || !h_dir->i_op->atomic_open)
17775+ err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
17776+ arg->u.c.want_excl);
17777+ else
17778+ err = vfsub_atomic_open(h_dir, a->h_path.dentry,
17779+ arg->u.c.aopen, br);
4a4d8108
AM
17780+ break;
17781+ case Symlink:
c2b27bf2 17782+ err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
4a4d8108
AM
17783+ break;
17784+ case Mknod:
c2b27bf2
AM
17785+ err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
17786+ arg->u.m.dev);
4a4d8108
AM
17787+ break;
17788+ default:
17789+ BUG();
17790+ }
17791+ created = !err;
17792+ if (!err)
5afbbe0d 17793+ err = epilog(dir, btop, wh_dentry, dentry);
4a4d8108
AM
17794+
17795+ /* revert */
5527c038 17796+ if (unlikely(created && err && d_is_positive(a->h_path.dentry))) {
523b37e3
AM
17797+ /* no delegation since it is just created */
17798+ rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
17799+ /*force*/0);
4a4d8108 17800+ if (rerr) {
523b37e3
AM
17801+ AuIOErr("%pd revert failure(%d, %d)\n",
17802+ dentry, err, rerr);
4a4d8108
AM
17803+ err = -EIO;
17804+ }
c2b27bf2 17805+ au_dtime_revert(&a->dt);
4a4d8108
AM
17806+ }
17807+
b912730e
AM
17808+ if (!err && try_aopen && !h_dir->i_op->atomic_open)
17809+ *arg->u.c.aopen->opened |= FILE_CREATED;
17810+
c2b27bf2 17811+ au_unpin(&a->pin);
4a4d8108
AM
17812+ dput(wh_dentry);
17813+
027c5e7a 17814+out_parent:
b912730e
AM
17815+ if (!try_aopen)
17816+ di_write_unlock(parent);
027c5e7a 17817+out_unlock:
4a4d8108 17818+ if (unlikely(err)) {
5afbbe0d 17819+ au_update_dbtop(dentry);
4a4d8108
AM
17820+ d_drop(dentry);
17821+ }
b912730e
AM
17822+ if (!try_aopen)
17823+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2
AM
17824+out_free:
17825+ kfree(a);
027c5e7a 17826+out:
4a4d8108
AM
17827+ return err;
17828+}
17829+
7eafdf33
AM
17830+int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
17831+ dev_t dev)
4a4d8108
AM
17832+{
17833+ struct simple_arg arg = {
17834+ .type = Mknod,
17835+ .u.m = {
17836+ .mode = mode,
17837+ .dev = dev
17838+ }
17839+ };
17840+ return add_simple(dir, dentry, &arg);
17841+}
17842+
17843+int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
17844+{
17845+ struct simple_arg arg = {
17846+ .type = Symlink,
17847+ .u.s.symname = symname
17848+ };
17849+ return add_simple(dir, dentry, &arg);
17850+}
17851+
7eafdf33 17852+int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
b4510431 17853+ bool want_excl)
4a4d8108
AM
17854+{
17855+ struct simple_arg arg = {
17856+ .type = Creat,
17857+ .u.c = {
b4510431
AM
17858+ .mode = mode,
17859+ .want_excl = want_excl
4a4d8108
AM
17860+ }
17861+ };
17862+ return add_simple(dir, dentry, &arg);
17863+}
17864+
b912730e
AM
17865+int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
17866+ struct vfsub_aopen_args *aopen_args)
17867+{
17868+ struct simple_arg arg = {
17869+ .type = Creat,
17870+ .u.c = {
17871+ .mode = aopen_args->create_mode,
17872+ .want_excl = aopen_args->open_flag & O_EXCL,
17873+ .try_aopen = true,
17874+ .aopen = aopen_args
17875+ }
17876+ };
17877+ return add_simple(dir, dentry, &arg);
17878+}
17879+
38d290e6
JR
17880+int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
17881+{
17882+ int err;
17883+ aufs_bindex_t bindex;
17884+ struct super_block *sb;
17885+ struct dentry *parent, *h_parent, *h_dentry;
17886+ struct inode *h_dir, *inode;
17887+ struct vfsmount *h_mnt;
17888+ struct au_wr_dir_args wr_dir_args = {
17889+ .force_btgt = -1,
17890+ .flags = AuWrDir_TMPFILE
17891+ };
17892+
17893+ /* copy-up may happen */
febd17d6 17894+ inode_lock(dir);
38d290e6
JR
17895+
17896+ sb = dir->i_sb;
17897+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
17898+ if (unlikely(err))
17899+ goto out;
17900+
17901+ err = au_di_init(dentry);
17902+ if (unlikely(err))
17903+ goto out_si;
17904+
17905+ err = -EBUSY;
17906+ parent = d_find_any_alias(dir);
17907+ AuDebugOn(!parent);
17908+ di_write_lock_parent(parent);
5527c038 17909+ if (unlikely(d_inode(parent) != dir))
38d290e6
JR
17910+ goto out_parent;
17911+
17912+ err = au_digen_test(parent, au_sigen(sb));
17913+ if (unlikely(err))
17914+ goto out_parent;
17915+
5afbbe0d
AM
17916+ bindex = au_dbtop(parent);
17917+ au_set_dbtop(dentry, bindex);
17918+ au_set_dbbot(dentry, bindex);
38d290e6
JR
17919+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
17920+ bindex = err;
17921+ if (unlikely(err < 0))
17922+ goto out_parent;
17923+
17924+ err = -EOPNOTSUPP;
17925+ h_dir = au_h_iptr(dir, bindex);
17926+ if (unlikely(!h_dir->i_op->tmpfile))
17927+ goto out_parent;
17928+
17929+ h_mnt = au_sbr_mnt(sb, bindex);
17930+ err = vfsub_mnt_want_write(h_mnt);
17931+ if (unlikely(err))
17932+ goto out_parent;
17933+
17934+ h_parent = au_h_dptr(parent, bindex);
5527c038 17935+ err = inode_permission(d_inode(h_parent), MAY_WRITE | MAY_EXEC);
38d290e6
JR
17936+ if (unlikely(err))
17937+ goto out_mnt;
17938+
17939+ err = -ENOMEM;
17940+ h_dentry = d_alloc(h_parent, &dentry->d_name);
17941+ if (unlikely(!h_dentry))
17942+ goto out_mnt;
17943+
17944+ err = h_dir->i_op->tmpfile(h_dir, h_dentry, mode);
17945+ if (unlikely(err))
17946+ goto out_dentry;
17947+
5afbbe0d
AM
17948+ au_set_dbtop(dentry, bindex);
17949+ au_set_dbbot(dentry, bindex);
38d290e6
JR
17950+ au_set_h_dptr(dentry, bindex, dget(h_dentry));
17951+ inode = au_new_inode(dentry, /*must_new*/1);
17952+ if (IS_ERR(inode)) {
17953+ err = PTR_ERR(inode);
17954+ au_set_h_dptr(dentry, bindex, NULL);
5afbbe0d
AM
17955+ au_set_dbtop(dentry, -1);
17956+ au_set_dbbot(dentry, -1);
38d290e6
JR
17957+ } else {
17958+ if (!inode->i_nlink)
17959+ set_nlink(inode, 1);
17960+ d_tmpfile(dentry, inode);
17961+ au_di(dentry)->di_tmpfile = 1;
17962+
17963+ /* update without i_mutex */
5afbbe0d 17964+ if (au_ibtop(dir) == au_dbtop(dentry))
38d290e6
JR
17965+ au_cpup_attr_timesizes(dir);
17966+ }
17967+
17968+out_dentry:
17969+ dput(h_dentry);
17970+out_mnt:
17971+ vfsub_mnt_drop_write(h_mnt);
17972+out_parent:
17973+ di_write_unlock(parent);
17974+ dput(parent);
17975+ di_write_unlock(dentry);
5afbbe0d 17976+ if (unlikely(err)) {
38d290e6
JR
17977+ au_di_fin(dentry);
17978+ dentry->d_fsdata = NULL;
17979+ }
17980+out_si:
17981+ si_read_unlock(sb);
17982+out:
febd17d6 17983+ inode_unlock(dir);
38d290e6
JR
17984+ return err;
17985+}
17986+
4a4d8108
AM
17987+/* ---------------------------------------------------------------------- */
17988+
17989+struct au_link_args {
17990+ aufs_bindex_t bdst, bsrc;
17991+ struct au_pin pin;
17992+ struct path h_path;
17993+ struct dentry *src_parent, *parent;
17994+};
17995+
17996+static int au_cpup_before_link(struct dentry *src_dentry,
17997+ struct au_link_args *a)
17998+{
17999+ int err;
18000+ struct dentry *h_src_dentry;
c2b27bf2
AM
18001+ struct au_cp_generic cpg = {
18002+ .dentry = src_dentry,
18003+ .bdst = a->bdst,
18004+ .bsrc = a->bsrc,
18005+ .len = -1,
18006+ .pin = &a->pin,
18007+ .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
18008+ };
4a4d8108
AM
18009+
18010+ di_read_lock_parent(a->src_parent, AuLock_IR);
18011+ err = au_test_and_cpup_dirs(src_dentry, a->bdst);
18012+ if (unlikely(err))
18013+ goto out;
18014+
18015+ h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
4a4d8108
AM
18016+ err = au_pin(&a->pin, src_dentry, a->bdst,
18017+ au_opt_udba(src_dentry->d_sb),
18018+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18019+ if (unlikely(err))
18020+ goto out;
367653fa 18021+
c2b27bf2 18022+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
18023+ au_unpin(&a->pin);
18024+
4f0767ce 18025+out:
4a4d8108
AM
18026+ di_read_unlock(a->src_parent, AuLock_IR);
18027+ return err;
18028+}
18029+
86dc4139
AM
18030+static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
18031+ struct au_link_args *a)
4a4d8108
AM
18032+{
18033+ int err;
18034+ unsigned char plink;
5afbbe0d 18035+ aufs_bindex_t bbot;
4a4d8108 18036+ struct dentry *h_src_dentry;
523b37e3 18037+ struct inode *h_inode, *inode, *delegated;
4a4d8108
AM
18038+ struct super_block *sb;
18039+ struct file *h_file;
18040+
18041+ plink = 0;
18042+ h_inode = NULL;
18043+ sb = src_dentry->d_sb;
5527c038 18044+ inode = d_inode(src_dentry);
5afbbe0d 18045+ if (au_ibtop(inode) <= a->bdst)
4a4d8108
AM
18046+ h_inode = au_h_iptr(inode, a->bdst);
18047+ if (!h_inode || !h_inode->i_nlink) {
18048+ /* copyup src_dentry as the name of dentry. */
5afbbe0d
AM
18049+ bbot = au_dbbot(dentry);
18050+ if (bbot < a->bsrc)
18051+ au_set_dbbot(dentry, a->bsrc);
86dc4139
AM
18052+ au_set_h_dptr(dentry, a->bsrc,
18053+ dget(au_h_dptr(src_dentry, a->bsrc)));
18054+ dget(a->h_path.dentry);
18055+ au_set_h_dptr(dentry, a->bdst, NULL);
c1595e42
JR
18056+ AuDbg("temporary d_inode...\n");
18057+ spin_lock(&dentry->d_lock);
5527c038 18058+ dentry->d_inode = d_inode(src_dentry); /* tmp */
c1595e42 18059+ spin_unlock(&dentry->d_lock);
392086de 18060+ h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
86dc4139 18061+ if (IS_ERR(h_file))
4a4d8108 18062+ err = PTR_ERR(h_file);
86dc4139 18063+ else {
c2b27bf2
AM
18064+ struct au_cp_generic cpg = {
18065+ .dentry = dentry,
18066+ .bdst = a->bdst,
18067+ .bsrc = -1,
18068+ .len = -1,
18069+ .pin = &a->pin,
18070+ .flags = AuCpup_KEEPLINO
18071+ };
18072+ err = au_sio_cpup_simple(&cpg);
86dc4139
AM
18073+ au_h_open_post(dentry, a->bsrc, h_file);
18074+ if (!err) {
18075+ dput(a->h_path.dentry);
18076+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18077+ } else
18078+ au_set_h_dptr(dentry, a->bdst,
18079+ a->h_path.dentry);
18080+ }
c1595e42 18081+ spin_lock(&dentry->d_lock);
86dc4139 18082+ dentry->d_inode = NULL; /* restore */
c1595e42
JR
18083+ spin_unlock(&dentry->d_lock);
18084+ AuDbg("temporary d_inode...done\n");
86dc4139 18085+ au_set_h_dptr(dentry, a->bsrc, NULL);
5afbbe0d 18086+ au_set_dbbot(dentry, bbot);
4a4d8108
AM
18087+ } else {
18088+ /* the inode of src_dentry already exists on a.bdst branch */
18089+ h_src_dentry = d_find_alias(h_inode);
18090+ if (!h_src_dentry && au_plink_test(inode)) {
18091+ plink = 1;
18092+ h_src_dentry = au_plink_lkup(inode, a->bdst);
18093+ err = PTR_ERR(h_src_dentry);
18094+ if (IS_ERR(h_src_dentry))
18095+ goto out;
18096+
5527c038 18097+ if (unlikely(d_is_negative(h_src_dentry))) {
4a4d8108
AM
18098+ dput(h_src_dentry);
18099+ h_src_dentry = NULL;
18100+ }
18101+
18102+ }
18103+ if (h_src_dentry) {
523b37e3 18104+ delegated = NULL;
4a4d8108 18105+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
18106+ &a->h_path, &delegated);
18107+ if (unlikely(err == -EWOULDBLOCK)) {
18108+ pr_warn("cannot retry for NFSv4 delegation"
18109+ " for an internal link\n");
18110+ iput(delegated);
18111+ }
4a4d8108
AM
18112+ dput(h_src_dentry);
18113+ } else {
18114+ AuIOErr("no dentry found for hi%lu on b%d\n",
18115+ h_inode->i_ino, a->bdst);
18116+ err = -EIO;
18117+ }
18118+ }
18119+
18120+ if (!err && !plink)
18121+ au_plink_append(inode, a->bdst, a->h_path.dentry);
18122+
18123+out:
2cbb1c4b 18124+ AuTraceErr(err);
4a4d8108
AM
18125+ return err;
18126+}
18127+
18128+int aufs_link(struct dentry *src_dentry, struct inode *dir,
18129+ struct dentry *dentry)
18130+{
18131+ int err, rerr;
18132+ struct au_dtime dt;
18133+ struct au_link_args *a;
18134+ struct dentry *wh_dentry, *h_src_dentry;
523b37e3 18135+ struct inode *inode, *delegated;
4a4d8108
AM
18136+ struct super_block *sb;
18137+ struct au_wr_dir_args wr_dir_args = {
18138+ /* .force_btgt = -1, */
18139+ .flags = AuWrDir_ADD_ENTRY
18140+ };
18141+
18142+ IMustLock(dir);
5527c038 18143+ inode = d_inode(src_dentry);
4a4d8108
AM
18144+ IMustLock(inode);
18145+
4a4d8108
AM
18146+ err = -ENOMEM;
18147+ a = kzalloc(sizeof(*a), GFP_NOFS);
18148+ if (unlikely(!a))
18149+ goto out;
18150+
18151+ a->parent = dentry->d_parent; /* dir inode is locked */
027c5e7a
AM
18152+ err = aufs_read_and_write_lock2(dentry, src_dentry,
18153+ AuLock_NOPLM | AuLock_GEN);
e49829fe
JR
18154+ if (unlikely(err))
18155+ goto out_kfree;
38d290e6 18156+ err = au_d_linkable(src_dentry);
027c5e7a
AM
18157+ if (unlikely(err))
18158+ goto out_unlock;
18159+ err = au_d_may_add(dentry);
18160+ if (unlikely(err))
18161+ goto out_unlock;
e49829fe 18162+
4a4d8108 18163+ a->src_parent = dget_parent(src_dentry);
5afbbe0d 18164+ wr_dir_args.force_btgt = au_ibtop(inode);
4a4d8108
AM
18165+
18166+ di_write_lock_parent(a->parent);
18167+ wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
18168+ wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
18169+ &wr_dir_args);
18170+ err = PTR_ERR(wh_dentry);
18171+ if (IS_ERR(wh_dentry))
027c5e7a 18172+ goto out_parent;
4a4d8108
AM
18173+
18174+ err = 0;
18175+ sb = dentry->d_sb;
5afbbe0d 18176+ a->bdst = au_dbtop(dentry);
4a4d8108
AM
18177+ a->h_path.dentry = au_h_dptr(dentry, a->bdst);
18178+ a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
5afbbe0d 18179+ a->bsrc = au_ibtop(inode);
2cbb1c4b 18180+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
38d290e6
JR
18181+ if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
18182+ h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
2cbb1c4b 18183+ if (!h_src_dentry) {
5afbbe0d 18184+ a->bsrc = au_dbtop(src_dentry);
2cbb1c4b
JR
18185+ h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
18186+ AuDebugOn(!h_src_dentry);
38d290e6
JR
18187+ } else if (IS_ERR(h_src_dentry)) {
18188+ err = PTR_ERR(h_src_dentry);
2cbb1c4b 18189+ goto out_parent;
38d290e6 18190+ }
2cbb1c4b 18191+
4a4d8108
AM
18192+ if (au_opt_test(au_mntflags(sb), PLINK)) {
18193+ if (a->bdst < a->bsrc
18194+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
86dc4139 18195+ err = au_cpup_or_link(src_dentry, dentry, a);
523b37e3
AM
18196+ else {
18197+ delegated = NULL;
4a4d8108 18198+ err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
523b37e3
AM
18199+ &a->h_path, &delegated);
18200+ if (unlikely(err == -EWOULDBLOCK)) {
18201+ pr_warn("cannot retry for NFSv4 delegation"
18202+ " for an internal link\n");
18203+ iput(delegated);
18204+ }
18205+ }
2cbb1c4b 18206+ dput(h_src_dentry);
4a4d8108
AM
18207+ } else {
18208+ /*
18209+ * copyup src_dentry to the branch we process,
18210+ * and then link(2) to it.
18211+ */
2cbb1c4b 18212+ dput(h_src_dentry);
4a4d8108
AM
18213+ if (a->bdst < a->bsrc
18214+ /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
18215+ au_unpin(&a->pin);
18216+ di_write_unlock(a->parent);
18217+ err = au_cpup_before_link(src_dentry, a);
18218+ di_write_lock_parent(a->parent);
18219+ if (!err)
18220+ err = au_pin(&a->pin, dentry, a->bdst,
18221+ au_opt_udba(sb),
18222+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
18223+ if (unlikely(err))
18224+ goto out_wh;
18225+ }
18226+ if (!err) {
18227+ h_src_dentry = au_h_dptr(src_dentry, a->bdst);
18228+ err = -ENOENT;
5527c038 18229+ if (h_src_dentry && d_is_positive(h_src_dentry)) {
523b37e3 18230+ delegated = NULL;
4a4d8108
AM
18231+ err = vfsub_link(h_src_dentry,
18232+ au_pinned_h_dir(&a->pin),
523b37e3
AM
18233+ &a->h_path, &delegated);
18234+ if (unlikely(err == -EWOULDBLOCK)) {
18235+ pr_warn("cannot retry"
18236+ " for NFSv4 delegation"
18237+ " for an internal link\n");
18238+ iput(delegated);
18239+ }
18240+ }
4a4d8108
AM
18241+ }
18242+ }
18243+ if (unlikely(err))
18244+ goto out_unpin;
18245+
18246+ if (wh_dentry) {
18247+ a->h_path.dentry = wh_dentry;
18248+ err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
18249+ dentry);
18250+ if (unlikely(err))
18251+ goto out_revert;
18252+ }
18253+
b912730e 18254+ au_dir_ts(dir, a->bdst);
4a4d8108 18255+ dir->i_version++;
4a4d8108
AM
18256+ inc_nlink(inode);
18257+ inode->i_ctime = dir->i_ctime;
027c5e7a
AM
18258+ d_instantiate(dentry, au_igrab(inode));
18259+ if (d_unhashed(a->h_path.dentry))
4a4d8108
AM
18260+ /* some filesystem calls d_drop() */
18261+ d_drop(dentry);
076b876e
AM
18262+ /* some filesystems consume an inode even hardlink */
18263+ au_fhsm_wrote(sb, a->bdst, /*force*/0);
4a4d8108
AM
18264+ goto out_unpin; /* success */
18265+
4f0767ce 18266+out_revert:
523b37e3
AM
18267+ /* no delegation since it is just created */
18268+ rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
18269+ /*delegated*/NULL, /*force*/0);
027c5e7a 18270+ if (unlikely(rerr)) {
523b37e3 18271+ AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
027c5e7a
AM
18272+ err = -EIO;
18273+ }
4a4d8108 18274+ au_dtime_revert(&dt);
4f0767ce 18275+out_unpin:
4a4d8108 18276+ au_unpin(&a->pin);
4f0767ce 18277+out_wh:
4a4d8108 18278+ dput(wh_dentry);
027c5e7a
AM
18279+out_parent:
18280+ di_write_unlock(a->parent);
18281+ dput(a->src_parent);
4f0767ce 18282+out_unlock:
4a4d8108 18283+ if (unlikely(err)) {
5afbbe0d 18284+ au_update_dbtop(dentry);
4a4d8108
AM
18285+ d_drop(dentry);
18286+ }
4a4d8108 18287+ aufs_read_and_write_unlock2(dentry, src_dentry);
e49829fe 18288+out_kfree:
4a4d8108 18289+ kfree(a);
4f0767ce 18290+out:
86dc4139 18291+ AuTraceErr(err);
4a4d8108
AM
18292+ return err;
18293+}
18294+
7eafdf33 18295+int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
4a4d8108
AM
18296+{
18297+ int err, rerr;
18298+ aufs_bindex_t bindex;
18299+ unsigned char diropq;
18300+ struct path h_path;
18301+ struct dentry *wh_dentry, *parent, *opq_dentry;
febd17d6 18302+ struct inode *h_inode;
4a4d8108
AM
18303+ struct super_block *sb;
18304+ struct {
18305+ struct au_pin pin;
18306+ struct au_dtime dt;
18307+ } *a; /* reduce the stack usage */
18308+ struct au_wr_dir_args wr_dir_args = {
18309+ .force_btgt = -1,
18310+ .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
18311+ };
18312+
18313+ IMustLock(dir);
18314+
18315+ err = -ENOMEM;
18316+ a = kmalloc(sizeof(*a), GFP_NOFS);
18317+ if (unlikely(!a))
18318+ goto out;
18319+
027c5e7a
AM
18320+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
18321+ if (unlikely(err))
18322+ goto out_free;
18323+ err = au_d_may_add(dentry);
18324+ if (unlikely(err))
18325+ goto out_unlock;
18326+
4a4d8108
AM
18327+ parent = dentry->d_parent; /* dir inode is locked */
18328+ di_write_lock_parent(parent);
18329+ wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
18330+ &a->pin, &wr_dir_args);
18331+ err = PTR_ERR(wh_dentry);
18332+ if (IS_ERR(wh_dentry))
027c5e7a 18333+ goto out_parent;
4a4d8108
AM
18334+
18335+ sb = dentry->d_sb;
5afbbe0d 18336+ bindex = au_dbtop(dentry);
4a4d8108
AM
18337+ h_path.dentry = au_h_dptr(dentry, bindex);
18338+ h_path.mnt = au_sbr_mnt(sb, bindex);
18339+ err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
18340+ if (unlikely(err))
027c5e7a 18341+ goto out_unpin;
4a4d8108
AM
18342+
18343+ /* make the dir opaque */
18344+ diropq = 0;
febd17d6 18345+ h_inode = d_inode(h_path.dentry);
4a4d8108
AM
18346+ if (wh_dentry
18347+ || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
febd17d6 18348+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 18349+ opq_dentry = au_diropq_create(dentry, bindex);
febd17d6 18350+ inode_unlock(h_inode);
4a4d8108
AM
18351+ err = PTR_ERR(opq_dentry);
18352+ if (IS_ERR(opq_dentry))
18353+ goto out_dir;
18354+ dput(opq_dentry);
18355+ diropq = 1;
18356+ }
18357+
18358+ err = epilog(dir, bindex, wh_dentry, dentry);
18359+ if (!err) {
18360+ inc_nlink(dir);
027c5e7a 18361+ goto out_unpin; /* success */
4a4d8108
AM
18362+ }
18363+
18364+ /* revert */
18365+ if (diropq) {
18366+ AuLabel(revert opq);
febd17d6 18367+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 18368+ rerr = au_diropq_remove(dentry, bindex);
febd17d6 18369+ inode_unlock(h_inode);
4a4d8108 18370+ if (rerr) {
523b37e3
AM
18371+ AuIOErr("%pd reverting diropq failed(%d, %d)\n",
18372+ dentry, err, rerr);
4a4d8108
AM
18373+ err = -EIO;
18374+ }
18375+ }
18376+
4f0767ce 18377+out_dir:
4a4d8108
AM
18378+ AuLabel(revert dir);
18379+ rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
18380+ if (rerr) {
523b37e3
AM
18381+ AuIOErr("%pd reverting dir failed(%d, %d)\n",
18382+ dentry, err, rerr);
4a4d8108
AM
18383+ err = -EIO;
18384+ }
4a4d8108 18385+ au_dtime_revert(&a->dt);
027c5e7a 18386+out_unpin:
4a4d8108
AM
18387+ au_unpin(&a->pin);
18388+ dput(wh_dentry);
027c5e7a
AM
18389+out_parent:
18390+ di_write_unlock(parent);
18391+out_unlock:
4a4d8108 18392+ if (unlikely(err)) {
5afbbe0d 18393+ au_update_dbtop(dentry);
4a4d8108
AM
18394+ d_drop(dentry);
18395+ }
4a4d8108 18396+ aufs_read_unlock(dentry, AuLock_DW);
027c5e7a 18397+out_free:
4a4d8108 18398+ kfree(a);
4f0767ce 18399+out:
4a4d8108
AM
18400+ return err;
18401+}
7f207e10
AM
18402diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
18403--- /usr/share/empty/fs/aufs/i_op.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
18404+++ linux/fs/aufs/i_op.c 2016-07-25 19:05:34.814493242 +0200
18405@@ -0,0 +1,1414 @@
4a4d8108 18406+/*
8cdd5066 18407+ * Copyright (C) 2005-2016 Junjiro R. Okajima
4a4d8108
AM
18408+ *
18409+ * This program, aufs is free software; you can redistribute it and/or modify
18410+ * it under the terms of the GNU General Public License as published by
18411+ * the Free Software Foundation; either version 2 of the License, or
18412+ * (at your option) any later version.
18413+ *
18414+ * This program is distributed in the hope that it will be useful,
18415+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
18416+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18417+ * GNU General Public License for more details.
18418+ *
18419+ * You should have received a copy of the GNU General Public License
523b37e3 18420+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a4d8108 18421+ */
1facf9fc 18422+
1308ab2a 18423+/*
4a4d8108 18424+ * inode operations (except add/del/rename)
1308ab2a 18425+ */
4a4d8108
AM
18426+
18427+#include <linux/device_cgroup.h>
18428+#include <linux/fs_stack.h>
4a4d8108
AM
18429+#include <linux/namei.h>
18430+#include <linux/security.h>
4a4d8108
AM
18431+#include "aufs.h"
18432+
1e00d052 18433+static int h_permission(struct inode *h_inode, int mask,
79b8bda9 18434+ struct path *h_path, int brperm)
1facf9fc 18435+{
1308ab2a 18436+ int err;
4a4d8108 18437+ const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
1facf9fc 18438+
4a4d8108
AM
18439+ err = -EACCES;
18440+ if ((write_mask && IS_IMMUTABLE(h_inode))
18441+ || ((mask & MAY_EXEC)
18442+ && S_ISREG(h_inode->i_mode)
79b8bda9 18443+ && (path_noexec(h_path)
4a4d8108
AM
18444+ || !(h_inode->i_mode & S_IXUGO))))
18445+ goto out;
18446+
18447+ /*
18448+ * - skip the lower fs test in the case of write to ro branch.
18449+ * - nfs dir permission write check is optimized, but a policy for
18450+ * link/rename requires a real check.
b912730e
AM
18451+ * - nfs always sets MS_POSIXACL regardless its mount option 'noacl.'
18452+ * in this case, generic_permission() returns -EOPNOTSUPP.
4a4d8108
AM
18453+ */
18454+ if ((write_mask && !au_br_writable(brperm))
18455+ || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
18456+ && write_mask && !(mask & MAY_READ))
18457+ || !h_inode->i_op->permission) {
18458+ /* AuLabel(generic_permission); */
b912730e 18459+ /* AuDbg("get_acl %pf\n", h_inode->i_op->get_acl); */
1e00d052 18460+ err = generic_permission(h_inode, mask);
b912730e
AM
18461+ if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
18462+ err = h_inode->i_op->permission(h_inode, mask);
18463+ AuTraceErr(err);
1308ab2a 18464+ } else {
4a4d8108 18465+ /* AuLabel(h_inode->permission); */
1e00d052 18466+ err = h_inode->i_op->permission(h_inode, mask);
4a4d8108
AM
18467+ AuTraceErr(err);
18468+ }
1facf9fc 18469+
4a4d8108
AM
18470+ if (!err)
18471+ err = devcgroup_inode_permission(h_inode, mask);
7f207e10 18472+ if (!err)
4a4d8108 18473+ err = security_inode_permission(h_inode, mask);
4a4d8108
AM
18474+
18475+#if 0
18476+ if (!err) {
18477+ /* todo: do we need to call ima_path_check()? */
18478+ struct path h_path = {
18479+ .dentry =
18480+ .mnt = h_mnt
18481+ };
18482+ err = ima_path_check(&h_path,
18483+ mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
18484+ IMA_COUNT_LEAVE);
1308ab2a 18485+ }
4a4d8108 18486+#endif
dece6358 18487+
4f0767ce 18488+out:
1308ab2a 18489+ return err;
18490+}
dece6358 18491+
1e00d052 18492+static int aufs_permission(struct inode *inode, int mask)
1308ab2a 18493+{
18494+ int err;
5afbbe0d 18495+ aufs_bindex_t bindex, bbot;
4a4d8108
AM
18496+ const unsigned char isdir = !!S_ISDIR(inode->i_mode),
18497+ write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
18498+ struct inode *h_inode;
18499+ struct super_block *sb;
18500+ struct au_branch *br;
1facf9fc 18501+
027c5e7a 18502+ /* todo: support rcu-walk? */
1e00d052 18503+ if (mask & MAY_NOT_BLOCK)
027c5e7a
AM
18504+ return -ECHILD;
18505+
4a4d8108
AM
18506+ sb = inode->i_sb;
18507+ si_read_lock(sb, AuLock_FLUSH);
18508+ ii_read_lock_child(inode);
027c5e7a
AM
18509+#if 0
18510+ err = au_iigen_test(inode, au_sigen(sb));
18511+ if (unlikely(err))
18512+ goto out;
18513+#endif
dece6358 18514+
076b876e
AM
18515+ if (!isdir
18516+ || write_mask
18517+ || au_opt_test(au_mntflags(sb), DIRPERM1)) {
4a4d8108 18518+ err = au_busy_or_stale();
5afbbe0d 18519+ h_inode = au_h_iptr(inode, au_ibtop(inode));
4a4d8108
AM
18520+ if (unlikely(!h_inode
18521+ || (h_inode->i_mode & S_IFMT)
18522+ != (inode->i_mode & S_IFMT)))
18523+ goto out;
1facf9fc 18524+
4a4d8108 18525+ err = 0;
5afbbe0d 18526+ bindex = au_ibtop(inode);
4a4d8108 18527+ br = au_sbr(sb, bindex);
79b8bda9 18528+ err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
4a4d8108
AM
18529+ if (write_mask
18530+ && !err
18531+ && !special_file(h_inode->i_mode)) {
18532+ /* test whether the upper writable branch exists */
18533+ err = -EROFS;
18534+ for (; bindex >= 0; bindex--)
18535+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
18536+ err = 0;
18537+ break;
18538+ }
18539+ }
18540+ goto out;
18541+ }
dece6358 18542+
4a4d8108 18543+ /* non-write to dir */
1308ab2a 18544+ err = 0;
5afbbe0d
AM
18545+ bbot = au_ibbot(inode);
18546+ for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
4a4d8108
AM
18547+ h_inode = au_h_iptr(inode, bindex);
18548+ if (h_inode) {
18549+ err = au_busy_or_stale();
18550+ if (unlikely(!S_ISDIR(h_inode->i_mode)))
18551+ break;
18552+
18553+ br = au_sbr(sb, bindex);
79b8bda9 18554+ err = h_permission(h_inode, mask, &br->br_path,
4a4d8108
AM
18555+ br->br_perm);
18556+ }
18557+ }
1308ab2a 18558+
4f0767ce 18559+out:
4a4d8108
AM
18560+ ii_read_unlock(inode);
18561+ si_read_unlock(sb);
1308ab2a 18562+ return err;
18563+}
18564+
4a4d8108 18565+/* ---------------------------------------------------------------------- */
1facf9fc 18566+
4a4d8108 18567+static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
b4510431 18568+ unsigned int flags)
4a4d8108
AM
18569+{
18570+ struct dentry *ret, *parent;
b752ccd1 18571+ struct inode *inode;
4a4d8108 18572+ struct super_block *sb;
1716fcea 18573+ int err, npositive;
dece6358 18574+
4a4d8108 18575+ IMustLock(dir);
1308ab2a 18576+
537831f9
AM
18577+ /* todo: support rcu-walk? */
18578+ ret = ERR_PTR(-ECHILD);
18579+ if (flags & LOOKUP_RCU)
18580+ goto out;
18581+
18582+ ret = ERR_PTR(-ENAMETOOLONG);
18583+ if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
18584+ goto out;
18585+
4a4d8108 18586+ sb = dir->i_sb;
7f207e10
AM
18587+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
18588+ ret = ERR_PTR(err);
18589+ if (unlikely(err))
18590+ goto out;
18591+
4a4d8108
AM
18592+ err = au_di_init(dentry);
18593+ ret = ERR_PTR(err);
18594+ if (unlikely(err))
7f207e10 18595+ goto out_si;
1308ab2a 18596+
9dbd164d 18597+ inode = NULL;
027c5e7a 18598+ npositive = 0; /* suppress a warning */
4a4d8108
AM
18599+ parent = dentry->d_parent; /* dir inode is locked */
18600+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
18601+ err = au_alive_dir(parent);
18602+ if (!err)
18603+ err = au_digen_test(parent, au_sigen(sb));
18604+ if (!err) {
5afbbe0d
AM
18605+ /* regardless LOOKUP_CREATE, always ALLOW_NEG */
18606+ npositive = au_lkup_dentry(dentry, au_dbtop(parent),
18607+ AuLkup_ALLOW_NEG);
027c5e7a
AM
18608+ err = npositive;
18609+ }
4a4d8108 18610+ di_read_unlock(parent, AuLock_IR);
4a4d8108
AM
18611+ ret = ERR_PTR(err);
18612+ if (unlikely(err < 0))
18613+ goto out_unlock;
1308ab2a 18614+
4a4d8108 18615+ if (npositive) {
b752ccd1 18616+ inode = au_new_inode(dentry, /*must_new*/0);
c1595e42
JR
18617+ if (IS_ERR(inode)) {
18618+ ret = (void *)inode;
18619+ inode = NULL;
18620+ goto out_unlock;
18621+ }
9dbd164d 18622+ }
4a4d8108 18623+
c1595e42
JR
18624+ if (inode)
18625+ atomic_inc(&inode->i_count);
4a4d8108 18626+ ret = d_splice_alias(inode, dentry);
537831f9
AM
18627+#if 0
18628+ if (unlikely(d_need_lookup(dentry))) {
18629+ spin_lock(&dentry->d_lock);
18630+ dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
18631+ spin_unlock(&dentry->d_lock);
18632+ } else
18633+#endif
c1595e42 18634+ if (inode) {
2000de60 18635+ if (!IS_ERR(ret)) {
c1595e42 18636+ iput(inode);
2000de60
JR
18637+ if (ret && ret != dentry)
18638+ ii_write_unlock(inode);
18639+ } else {
c1595e42
JR
18640+ ii_write_unlock(inode);
18641+ iput(inode);
18642+ inode = NULL;
18643+ }
7f207e10 18644+ }
1facf9fc 18645+
4f0767ce 18646+out_unlock:
4a4d8108 18647+ di_write_unlock(dentry);
7f207e10 18648+out_si:
4a4d8108 18649+ si_read_unlock(sb);
7f207e10 18650+out:
4a4d8108
AM
18651+ return ret;
18652+}
1facf9fc 18653+
4a4d8108 18654+/* ---------------------------------------------------------------------- */
1facf9fc 18655+
b912730e
AM
18656+struct aopen_node {
18657+ struct hlist_node hlist;
18658+ struct file *file, *h_file;
18659+};
18660+
18661+static int au_do_aopen(struct inode *inode, struct file *file)
18662+{
18663+ struct au_sphlhead *aopen;
18664+ struct aopen_node *node;
18665+ struct au_do_open_args args = {
18666+ .no_lock = 1,
18667+ .open = au_do_open_nondir
18668+ };
18669+
18670+ aopen = &au_sbi(inode->i_sb)->si_aopen;
18671+ spin_lock(&aopen->spin);
18672+ hlist_for_each_entry(node, &aopen->head, hlist)
18673+ if (node->file == file) {
18674+ args.h_file = node->h_file;
18675+ break;
18676+ }
18677+ spin_unlock(&aopen->spin);
18678+ /* AuDebugOn(!args.h_file); */
18679+
18680+ return au_do_open(file, &args);
18681+}
18682+
18683+static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
18684+ struct file *file, unsigned int open_flag,
18685+ umode_t create_mode, int *opened)
18686+{
18687+ int err, h_opened = *opened;
5afbbe0d 18688+ unsigned int lkup_flags;
b912730e
AM
18689+ struct dentry *parent;
18690+ struct dentry *d;
18691+ struct au_sphlhead *aopen;
18692+ struct vfsub_aopen_args args = {
18693+ .open_flag = open_flag,
18694+ .create_mode = create_mode,
18695+ .opened = &h_opened
18696+ };
18697+ struct aopen_node aopen_node = {
18698+ .file = file
18699+ };
18700+
18701+ IMustLock(dir);
5afbbe0d 18702+ AuDbg("open_flag 0%o\n", open_flag);
b912730e
AM
18703+ AuDbgDentry(dentry);
18704+
18705+ err = 0;
18706+ if (!au_di(dentry)) {
5afbbe0d
AM
18707+ lkup_flags = LOOKUP_OPEN;
18708+ if (open_flag & O_CREAT)
18709+ lkup_flags |= LOOKUP_CREATE;
18710+ d = aufs_lookup(dir, dentry, lkup_flags);
b912730e
AM
18711+ if (IS_ERR(d)) {
18712+ err = PTR_ERR(d);
5afbbe0d 18713+ AuTraceErr(err);
b912730e
AM
18714+ goto out;
18715+ } else if (d) {
18716+ /*
18717+ * obsoleted dentry found.
18718+ * another error will be returned later.
18719+ */
18720+ d_drop(d);
b912730e 18721+ AuDbgDentry(d);
5afbbe0d 18722+ dput(d);
b912730e
AM
18723+ }
18724+ AuDbgDentry(dentry);
18725+ }
18726+
18727+ if (d_is_positive(dentry)
18728+ || d_unhashed(dentry)
18729+ || d_unlinked(dentry)
18730+ || !(open_flag & O_CREAT))
18731+ goto out_no_open;
18732+
18733+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
18734+ if (unlikely(err))
18735+ goto out;
18736+
18737+ parent = dentry->d_parent; /* dir is locked */
18738+ di_write_lock_parent(parent);
5afbbe0d 18739+ err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
b912730e
AM
18740+ if (unlikely(err))
18741+ goto out_unlock;
18742+
18743+ AuDbgDentry(dentry);
18744+ if (d_is_positive(dentry))
18745+ goto out_unlock;
18746+
18747+ args.file = get_empty_filp();
18748+ err = PTR_ERR(args.file);
18749+ if (IS_ERR(args.file))
18750+ goto out_unlock;
18751+
18752+ args.file->f_flags = file->f_flags;
18753+ err = au_aopen_or_create(dir, dentry, &args);
18754+ AuTraceErr(err);
18755+ AuDbgFile(args.file);
18756+ if (unlikely(err < 0)) {
18757+ if (h_opened & FILE_OPENED)
18758+ fput(args.file);
18759+ else
18760+ put_filp(args.file);
18761+ goto out_unlock;
18762+ }
18763+
18764+ /* some filesystems don't set FILE_CREATED while succeeded? */
18765+ *opened |= FILE_CREATED;
18766+ if (h_opened & FILE_OPENED)
18767+ aopen_node.h_file = args.file;
18768+ else {
18769+ put_filp(args.file);
18770+ args.file = NULL;
18771+ }
18772+ aopen = &au_sbi(dir->i_sb)->si_aopen;
18773+ au_sphl_add(&aopen_node.hlist, aopen);
18774+ err = finish_open(file, dentry, au_do_aopen, opened);
18775+ au_sphl_del(&aopen_node.hlist, aopen);
18776+ AuTraceErr(err);
18777+ AuDbgFile(file);
18778+ if (aopen_node.h_file)
18779+ fput(aopen_node.h_file);
18780+
18781+out_unlock:
18782+ di_write_unlock(parent);
18783+ aufs_read_unlock(dentry, AuLock_DW);
18784+ AuDbgDentry(dentry);
18785+ if (unlikely(err))
18786+ goto out;
18787+out_no_open:
18788+ if (!err && !(*opened & FILE_CREATED)) {
18789+ AuLabel(out_no_open);
18790+ dget(dentry);
18791+ err = finish_no_open(file, dentry);
18792+ }
18793+out:
18794+ AuDbg("%pd%s%s\n", dentry,
18795+ (*opened & FILE_CREATED) ? " created" : "",
18796+ (*opened & FILE_OPENED) ? " opened" : "");
18797+ AuTraceErr(err);
18798+ return err;
18799+}
18800+
18801+
18802+/* ---------------------------------------------------------------------- */
18803+
4a4d8108
AM
18804+static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
18805+ const unsigned char add_entry, aufs_bindex_t bcpup,
5afbbe0d 18806+ aufs_bindex_t btop)
4a4d8108
AM
18807+{
18808+ int err;
18809+ struct dentry *h_parent;
18810+ struct inode *h_dir;
1facf9fc 18811+
027c5e7a 18812+ if (add_entry)
5527c038 18813+ IMustLock(d_inode(parent));
027c5e7a 18814+ else
4a4d8108
AM
18815+ di_write_lock_parent(parent);
18816+
18817+ err = 0;
18818+ if (!au_h_dptr(parent, bcpup)) {
5afbbe0d 18819+ if (btop > bcpup)
c2b27bf2 18820+ err = au_cpup_dirs(dentry, bcpup);
5afbbe0d 18821+ else if (btop < bcpup)
4a4d8108
AM
18822+ err = au_cpdown_dirs(dentry, bcpup);
18823+ else
c2b27bf2 18824+ BUG();
4a4d8108 18825+ }
38d290e6 18826+ if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
4a4d8108 18827+ h_parent = au_h_dptr(parent, bcpup);
5527c038 18828+ h_dir = d_inode(h_parent);
febd17d6 18829+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
7e9cd9fe 18830+ err = au_lkup_neg(dentry, bcpup, /*wh*/0);
4a4d8108 18831+ /* todo: no unlock here */
febd17d6 18832+ inode_unlock(h_dir);
027c5e7a
AM
18833+
18834+ AuDbg("bcpup %d\n", bcpup);
18835+ if (!err) {
5527c038 18836+ if (d_really_is_negative(dentry))
5afbbe0d 18837+ au_set_h_dptr(dentry, btop, NULL);
4a4d8108
AM
18838+ au_update_dbrange(dentry, /*do_put_zero*/0);
18839+ }
1308ab2a 18840+ }
1facf9fc 18841+
4a4d8108
AM
18842+ if (!add_entry)
18843+ di_write_unlock(parent);
18844+ if (!err)
18845+ err = bcpup; /* success */
1308ab2a 18846+
027c5e7a 18847+ AuTraceErr(err);
4a4d8108
AM
18848+ return err;
18849+}
1facf9fc 18850+
4a4d8108
AM
18851+/*
18852+ * decide the branch and the parent dir where we will create a new entry.
18853+ * returns new bindex or an error.
18854+ * copyup the parent dir if needed.
18855+ */
18856+int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18857+ struct au_wr_dir_args *args)
18858+{
18859+ int err;
392086de 18860+ unsigned int flags;
5afbbe0d 18861+ aufs_bindex_t bcpup, btop, src_btop;
86dc4139
AM
18862+ const unsigned char add_entry
18863+ = au_ftest_wrdir(args->flags, ADD_ENTRY)
38d290e6 18864+ | au_ftest_wrdir(args->flags, TMPFILE);
4a4d8108
AM
18865+ struct super_block *sb;
18866+ struct dentry *parent;
18867+ struct au_sbinfo *sbinfo;
1facf9fc 18868+
4a4d8108
AM
18869+ sb = dentry->d_sb;
18870+ sbinfo = au_sbi(sb);
18871+ parent = dget_parent(dentry);
5afbbe0d
AM
18872+ btop = au_dbtop(dentry);
18873+ bcpup = btop;
4a4d8108
AM
18874+ if (args->force_btgt < 0) {
18875+ if (src_dentry) {
5afbbe0d
AM
18876+ src_btop = au_dbtop(src_dentry);
18877+ if (src_btop < btop)
18878+ bcpup = src_btop;
4a4d8108 18879+ } else if (add_entry) {
392086de
AM
18880+ flags = 0;
18881+ if (au_ftest_wrdir(args->flags, ISDIR))
18882+ au_fset_wbr(flags, DIR);
18883+ err = AuWbrCreate(sbinfo, dentry, flags);
4a4d8108
AM
18884+ bcpup = err;
18885+ }
1facf9fc 18886+
5527c038 18887+ if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
4a4d8108
AM
18888+ if (add_entry)
18889+ err = AuWbrCopyup(sbinfo, dentry);
18890+ else {
18891+ if (!IS_ROOT(dentry)) {
18892+ di_read_lock_parent(parent, !AuLock_IR);
18893+ err = AuWbrCopyup(sbinfo, dentry);
18894+ di_read_unlock(parent, !AuLock_IR);
18895+ } else
18896+ err = AuWbrCopyup(sbinfo, dentry);
18897+ }
18898+ bcpup = err;
18899+ if (unlikely(err < 0))
18900+ goto out;
18901+ }
18902+ } else {
18903+ bcpup = args->force_btgt;
5527c038 18904+ AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
1308ab2a 18905+ }
027c5e7a 18906+
5afbbe0d 18907+ AuDbg("btop %d, bcpup %d\n", btop, bcpup);
4a4d8108 18908+ err = bcpup;
5afbbe0d 18909+ if (bcpup == btop)
4a4d8108 18910+ goto out; /* success */
4a4d8108
AM
18911+
18912+ /* copyup the new parent into the branch we process */
5afbbe0d 18913+ err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
027c5e7a 18914+ if (err >= 0) {
5527c038 18915+ if (d_really_is_negative(dentry)) {
5afbbe0d
AM
18916+ au_set_h_dptr(dentry, btop, NULL);
18917+ au_set_dbtop(dentry, bcpup);
18918+ au_set_dbbot(dentry, bcpup);
027c5e7a 18919+ }
38d290e6
JR
18920+ AuDebugOn(add_entry
18921+ && !au_ftest_wrdir(args->flags, TMPFILE)
18922+ && !au_h_dptr(dentry, bcpup));
027c5e7a 18923+ }
86dc4139
AM
18924+
18925+out:
18926+ dput(parent);
18927+ return err;
18928+}
18929+
18930+/* ---------------------------------------------------------------------- */
18931+
18932+void au_pin_hdir_unlock(struct au_pin *p)
18933+{
18934+ if (p->hdir)
5afbbe0d 18935+ au_hn_inode_unlock(p->hdir);
86dc4139
AM
18936+}
18937+
c1595e42 18938+int au_pin_hdir_lock(struct au_pin *p)
86dc4139
AM
18939+{
18940+ int err;
18941+
18942+ err = 0;
18943+ if (!p->hdir)
18944+ goto out;
18945+
18946+ /* even if an error happens later, keep this lock */
5afbbe0d 18947+ au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
86dc4139
AM
18948+
18949+ err = -EBUSY;
5527c038 18950+ if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
86dc4139
AM
18951+ goto out;
18952+
18953+ err = 0;
18954+ if (p->h_dentry)
18955+ err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
18956+ p->h_parent, p->br);
18957+
18958+out:
18959+ return err;
18960+}
18961+
18962+int au_pin_hdir_relock(struct au_pin *p)
18963+{
18964+ int err, i;
18965+ struct inode *h_i;
18966+ struct dentry *h_d[] = {
18967+ p->h_dentry,
18968+ p->h_parent
18969+ };
18970+
18971+ err = au_pin_hdir_lock(p);
18972+ if (unlikely(err))
18973+ goto out;
18974+
18975+ for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
18976+ if (!h_d[i])
18977+ continue;
5527c038
JR
18978+ if (d_is_positive(h_d[i])) {
18979+ h_i = d_inode(h_d[i]);
86dc4139 18980+ err = !h_i->i_nlink;
5527c038 18981+ }
86dc4139
AM
18982+ }
18983+
18984+out:
18985+ return err;
18986+}
18987+
5afbbe0d 18988+static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
86dc4139 18989+{
5afbbe0d
AM
18990+#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
18991+ p->hdir->hi_inode->i_rwsem.owner = task;
86dc4139
AM
18992+#endif
18993+}
18994+
18995+void au_pin_hdir_acquire_nest(struct au_pin *p)
18996+{
18997+ if (p->hdir) {
5afbbe0d 18998+ rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
86dc4139
AM
18999+ p->lsc_hi, 0, NULL, _RET_IP_);
19000+ au_pin_hdir_set_owner(p, current);
19001+ }
dece6358 19002+}
1facf9fc 19003+
86dc4139
AM
19004+void au_pin_hdir_release(struct au_pin *p)
19005+{
19006+ if (p->hdir) {
19007+ au_pin_hdir_set_owner(p, p->task);
5afbbe0d 19008+ rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
86dc4139
AM
19009+ }
19010+}
1308ab2a 19011+
4a4d8108 19012+struct dentry *au_pinned_h_parent(struct au_pin *pin)
1308ab2a 19013+{
4a4d8108
AM
19014+ if (pin && pin->parent)
19015+ return au_h_dptr(pin->parent, pin->bindex);
19016+ return NULL;
dece6358 19017+}
1facf9fc 19018+
4a4d8108 19019+void au_unpin(struct au_pin *p)
dece6358 19020+{
86dc4139
AM
19021+ if (p->hdir)
19022+ au_pin_hdir_unlock(p);
e49829fe 19023+ if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
b4510431 19024+ vfsub_mnt_drop_write(p->h_mnt);
4a4d8108
AM
19025+ if (!p->hdir)
19026+ return;
1facf9fc 19027+
4a4d8108
AM
19028+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19029+ di_read_unlock(p->parent, AuLock_IR);
19030+ iput(p->hdir->hi_inode);
19031+ dput(p->parent);
19032+ p->parent = NULL;
19033+ p->hdir = NULL;
19034+ p->h_mnt = NULL;
86dc4139 19035+ /* do not clear p->task */
4a4d8108 19036+}
1308ab2a 19037+
4a4d8108
AM
19038+int au_do_pin(struct au_pin *p)
19039+{
19040+ int err;
19041+ struct super_block *sb;
4a4d8108
AM
19042+ struct inode *h_dir;
19043+
19044+ err = 0;
19045+ sb = p->dentry->d_sb;
86dc4139 19046+ p->br = au_sbr(sb, p->bindex);
4a4d8108
AM
19047+ if (IS_ROOT(p->dentry)) {
19048+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 19049+ p->h_mnt = au_br_mnt(p->br);
b4510431 19050+ err = vfsub_mnt_want_write(p->h_mnt);
4a4d8108
AM
19051+ if (unlikely(err)) {
19052+ au_fclr_pin(p->flags, MNT_WRITE);
19053+ goto out_err;
19054+ }
19055+ }
dece6358 19056+ goto out;
1facf9fc 19057+ }
19058+
86dc4139 19059+ p->h_dentry = NULL;
5afbbe0d 19060+ if (p->bindex <= au_dbbot(p->dentry))
86dc4139 19061+ p->h_dentry = au_h_dptr(p->dentry, p->bindex);
dece6358 19062+
4a4d8108
AM
19063+ p->parent = dget_parent(p->dentry);
19064+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19065+ di_read_lock(p->parent, AuLock_IR, p->lsc_di);
dece6358 19066+
4a4d8108 19067+ h_dir = NULL;
86dc4139 19068+ p->h_parent = au_h_dptr(p->parent, p->bindex);
5527c038 19069+ p->hdir = au_hi(d_inode(p->parent), p->bindex);
4a4d8108
AM
19070+ if (p->hdir)
19071+ h_dir = p->hdir->hi_inode;
dece6358 19072+
b752ccd1
AM
19073+ /*
19074+ * udba case, or
19075+ * if DI_LOCKED is not set, then p->parent may be different
19076+ * and h_parent can be NULL.
19077+ */
86dc4139 19078+ if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
e49829fe 19079+ err = -EBUSY;
4a4d8108
AM
19080+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19081+ di_read_unlock(p->parent, AuLock_IR);
19082+ dput(p->parent);
19083+ p->parent = NULL;
19084+ goto out_err;
19085+ }
1308ab2a 19086+
4a4d8108 19087+ if (au_ftest_pin(p->flags, MNT_WRITE)) {
86dc4139 19088+ p->h_mnt = au_br_mnt(p->br);
b4510431 19089+ err = vfsub_mnt_want_write(p->h_mnt);
dece6358 19090+ if (unlikely(err)) {
4a4d8108 19091+ au_fclr_pin(p->flags, MNT_WRITE);
86dc4139
AM
19092+ if (!au_ftest_pin(p->flags, DI_LOCKED))
19093+ di_read_unlock(p->parent, AuLock_IR);
19094+ dput(p->parent);
19095+ p->parent = NULL;
19096+ goto out_err;
dece6358
AM
19097+ }
19098+ }
4a4d8108 19099+
86dc4139
AM
19100+ au_igrab(h_dir);
19101+ err = au_pin_hdir_lock(p);
19102+ if (!err)
19103+ goto out; /* success */
19104+
076b876e
AM
19105+ au_unpin(p);
19106+
4f0767ce 19107+out_err:
4a4d8108
AM
19108+ pr_err("err %d\n", err);
19109+ err = au_busy_or_stale();
4f0767ce 19110+out:
1facf9fc 19111+ return err;
19112+}
19113+
4a4d8108
AM
19114+void au_pin_init(struct au_pin *p, struct dentry *dentry,
19115+ aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19116+ unsigned int udba, unsigned char flags)
19117+{
19118+ p->dentry = dentry;
19119+ p->udba = udba;
19120+ p->lsc_di = lsc_di;
19121+ p->lsc_hi = lsc_hi;
19122+ p->flags = flags;
19123+ p->bindex = bindex;
19124+
19125+ p->parent = NULL;
19126+ p->hdir = NULL;
19127+ p->h_mnt = NULL;
86dc4139
AM
19128+
19129+ p->h_dentry = NULL;
19130+ p->h_parent = NULL;
19131+ p->br = NULL;
19132+ p->task = current;
4a4d8108
AM
19133+}
19134+
19135+int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19136+ unsigned int udba, unsigned char flags)
19137+{
19138+ au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
19139+ udba, flags);
19140+ return au_do_pin(pin);
19141+}
19142+
dece6358
AM
19143+/* ---------------------------------------------------------------------- */
19144+
1308ab2a 19145+/*
4a4d8108
AM
19146+ * ->setattr() and ->getattr() are called in various cases.
19147+ * chmod, stat: dentry is revalidated.
19148+ * fchmod, fstat: file and dentry are not revalidated, additionally they may be
19149+ * unhashed.
19150+ * for ->setattr(), ia->ia_file is passed from ftruncate only.
1308ab2a 19151+ */
027c5e7a 19152+/* todo: consolidate with do_refresh() and simple_reval_dpath() */
c1595e42 19153+int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
1facf9fc 19154+{
4a4d8108 19155+ int err;
4a4d8108 19156+ struct dentry *parent;
1facf9fc 19157+
1308ab2a 19158+ err = 0;
027c5e7a 19159+ if (au_digen_test(dentry, sigen)) {
4a4d8108
AM
19160+ parent = dget_parent(dentry);
19161+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a 19162+ err = au_refresh_dentry(dentry, parent);
4a4d8108
AM
19163+ di_read_unlock(parent, AuLock_IR);
19164+ dput(parent);
dece6358 19165+ }
1facf9fc 19166+
4a4d8108 19167+ AuTraceErr(err);
1308ab2a 19168+ return err;
19169+}
dece6358 19170+
c1595e42
JR
19171+int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19172+ struct au_icpup_args *a)
1308ab2a 19173+{
19174+ int err;
4a4d8108 19175+ loff_t sz;
5afbbe0d 19176+ aufs_bindex_t btop, ibtop;
4a4d8108
AM
19177+ struct dentry *hi_wh, *parent;
19178+ struct inode *inode;
4a4d8108
AM
19179+ struct au_wr_dir_args wr_dir_args = {
19180+ .force_btgt = -1,
19181+ .flags = 0
19182+ };
19183+
2000de60 19184+ if (d_is_dir(dentry))
4a4d8108
AM
19185+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
19186+ /* plink or hi_wh() case */
5afbbe0d 19187+ btop = au_dbtop(dentry);
5527c038 19188+ inode = d_inode(dentry);
5afbbe0d
AM
19189+ ibtop = au_ibtop(inode);
19190+ if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
19191+ wr_dir_args.force_btgt = ibtop;
4a4d8108
AM
19192+ err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19193+ if (unlikely(err < 0))
19194+ goto out;
19195+ a->btgt = err;
5afbbe0d 19196+ if (err != btop)
4a4d8108
AM
19197+ au_fset_icpup(a->flags, DID_CPUP);
19198+
19199+ err = 0;
19200+ a->pin_flags = AuPin_MNT_WRITE;
19201+ parent = NULL;
19202+ if (!IS_ROOT(dentry)) {
19203+ au_fset_pin(a->pin_flags, DI_LOCKED);
19204+ parent = dget_parent(dentry);
19205+ di_write_lock_parent(parent);
19206+ }
19207+
19208+ err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
19209+ if (unlikely(err))
19210+ goto out_parent;
19211+
4a4d8108 19212+ sz = -1;
5afbbe0d 19213+ a->h_path.dentry = au_h_dptr(dentry, btop);
5527c038 19214+ a->h_inode = d_inode(a->h_path.dentry);
c1595e42 19215+ if (ia && (ia->ia_valid & ATTR_SIZE)) {
febd17d6 19216+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
c1595e42
JR
19217+ if (ia->ia_size < i_size_read(a->h_inode))
19218+ sz = ia->ia_size;
febd17d6 19219+ inode_unlock(a->h_inode);
c1595e42 19220+ }
4a4d8108 19221+
4a4d8108 19222+ hi_wh = NULL;
027c5e7a 19223+ if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
4a4d8108
AM
19224+ hi_wh = au_hi_wh(inode, a->btgt);
19225+ if (!hi_wh) {
c2b27bf2
AM
19226+ struct au_cp_generic cpg = {
19227+ .dentry = dentry,
19228+ .bdst = a->btgt,
19229+ .bsrc = -1,
19230+ .len = sz,
19231+ .pin = &a->pin
19232+ };
19233+ err = au_sio_cpup_wh(&cpg, /*file*/NULL);
4a4d8108
AM
19234+ if (unlikely(err))
19235+ goto out_unlock;
19236+ hi_wh = au_hi_wh(inode, a->btgt);
19237+ /* todo: revalidate hi_wh? */
19238+ }
19239+ }
19240+
19241+ if (parent) {
19242+ au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
19243+ di_downgrade_lock(parent, AuLock_IR);
19244+ dput(parent);
19245+ parent = NULL;
19246+ }
19247+ if (!au_ftest_icpup(a->flags, DID_CPUP))
19248+ goto out; /* success */
19249+
19250+ if (!d_unhashed(dentry)) {
c2b27bf2
AM
19251+ struct au_cp_generic cpg = {
19252+ .dentry = dentry,
19253+ .bdst = a->btgt,
5afbbe0d 19254+ .bsrc = btop,
c2b27bf2
AM
19255+ .len = sz,
19256+ .pin = &a->pin,
19257+ .flags = AuCpup_DTIME | AuCpup_HOPEN
19258+ };
19259+ err = au_sio_cpup_simple(&cpg);
4a4d8108
AM
19260+ if (!err)
19261+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19262+ } else if (!hi_wh)
19263+ a->h_path.dentry = au_h_dptr(dentry, a->btgt);
19264+ else
19265+ a->h_path.dentry = hi_wh; /* do not dget here */
1308ab2a 19266+
4f0767ce 19267+out_unlock:
5527c038 19268+ a->h_inode = d_inode(a->h_path.dentry);
86dc4139 19269+ if (!err)
dece6358 19270+ goto out; /* success */
4a4d8108 19271+ au_unpin(&a->pin);
4f0767ce 19272+out_parent:
4a4d8108
AM
19273+ if (parent) {
19274+ di_write_unlock(parent);
19275+ dput(parent);
19276+ }
4f0767ce 19277+out:
86dc4139 19278+ if (!err)
febd17d6 19279+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
1facf9fc 19280+ return err;
19281+}
19282+
4a4d8108 19283+static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
1facf9fc 19284+{
4a4d8108 19285+ int err;
523b37e3 19286+ struct inode *inode, *delegated;
4a4d8108
AM
19287+ struct super_block *sb;
19288+ struct file *file;
19289+ struct au_icpup_args *a;
1facf9fc 19290+
5527c038 19291+ inode = d_inode(dentry);
4a4d8108 19292+ IMustLock(inode);
dece6358 19293+
4a4d8108
AM
19294+ err = -ENOMEM;
19295+ a = kzalloc(sizeof(*a), GFP_NOFS);
19296+ if (unlikely(!a))
19297+ goto out;
1facf9fc 19298+
4a4d8108
AM
19299+ if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
19300+ ia->ia_valid &= ~ATTR_MODE;
dece6358 19301+
4a4d8108
AM
19302+ file = NULL;
19303+ sb = dentry->d_sb;
e49829fe
JR
19304+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19305+ if (unlikely(err))
19306+ goto out_kfree;
19307+
4a4d8108
AM
19308+ if (ia->ia_valid & ATTR_FILE) {
19309+ /* currently ftruncate(2) only */
7e9cd9fe 19310+ AuDebugOn(!d_is_reg(dentry));
4a4d8108
AM
19311+ file = ia->ia_file;
19312+ err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1);
19313+ if (unlikely(err))
19314+ goto out_si;
19315+ ia->ia_file = au_hf_top(file);
19316+ a->udba = AuOpt_UDBA_NONE;
19317+ } else {
19318+ /* fchmod() doesn't pass ia_file */
19319+ a->udba = au_opt_udba(sb);
027c5e7a
AM
19320+ di_write_lock_child(dentry);
19321+ /* no d_unlinked(), to set UDBA_NONE for root */
4a4d8108
AM
19322+ if (d_unhashed(dentry))
19323+ a->udba = AuOpt_UDBA_NONE;
4a4d8108
AM
19324+ if (a->udba != AuOpt_UDBA_NONE) {
19325+ AuDebugOn(IS_ROOT(dentry));
19326+ err = au_reval_for_attr(dentry, au_sigen(sb));
19327+ if (unlikely(err))
19328+ goto out_dentry;
19329+ }
dece6358 19330+ }
dece6358 19331+
4a4d8108
AM
19332+ err = au_pin_and_icpup(dentry, ia, a);
19333+ if (unlikely(err < 0))
19334+ goto out_dentry;
19335+ if (au_ftest_icpup(a->flags, DID_CPUP)) {
19336+ ia->ia_file = NULL;
19337+ ia->ia_valid &= ~ATTR_FILE;
1308ab2a 19338+ }
dece6358 19339+
4a4d8108
AM
19340+ a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
19341+ if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
19342+ == (ATTR_MODE | ATTR_CTIME)) {
7eafdf33 19343+ err = security_path_chmod(&a->h_path, ia->ia_mode);
4a4d8108
AM
19344+ if (unlikely(err))
19345+ goto out_unlock;
19346+ } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
19347+ && (ia->ia_valid & ATTR_CTIME)) {
86dc4139 19348+ err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
4a4d8108
AM
19349+ if (unlikely(err))
19350+ goto out_unlock;
19351+ }
dece6358 19352+
4a4d8108
AM
19353+ if (ia->ia_valid & ATTR_SIZE) {
19354+ struct file *f;
1308ab2a 19355+
953406b4 19356+ if (ia->ia_size < i_size_read(inode))
4a4d8108 19357+ /* unmap only */
953406b4 19358+ truncate_setsize(inode, ia->ia_size);
1308ab2a 19359+
4a4d8108
AM
19360+ f = NULL;
19361+ if (ia->ia_valid & ATTR_FILE)
19362+ f = ia->ia_file;
febd17d6 19363+ inode_unlock(a->h_inode);
4a4d8108 19364+ err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
febd17d6 19365+ inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
523b37e3
AM
19366+ } else {
19367+ delegated = NULL;
19368+ while (1) {
19369+ err = vfsub_notify_change(&a->h_path, ia, &delegated);
19370+ if (delegated) {
19371+ err = break_deleg_wait(&delegated);
19372+ if (!err)
19373+ continue;
19374+ }
19375+ break;
19376+ }
19377+ }
8cdd5066
JR
19378+ /*
19379+ * regardless aufs 'acl' option setting.
19380+ * why don't all acl-aware fs call this func from their ->setattr()?
19381+ */
19382+ if (!err && (ia->ia_valid & ATTR_MODE))
19383+ err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
4a4d8108
AM
19384+ if (!err)
19385+ au_cpup_attr_changeable(inode);
1308ab2a 19386+
4f0767ce 19387+out_unlock:
febd17d6 19388+ inode_unlock(a->h_inode);
4a4d8108 19389+ au_unpin(&a->pin);
027c5e7a 19390+ if (unlikely(err))
5afbbe0d 19391+ au_update_dbtop(dentry);
4f0767ce 19392+out_dentry:
4a4d8108
AM
19393+ di_write_unlock(dentry);
19394+ if (file) {
19395+ fi_write_unlock(file);
19396+ ia->ia_file = file;
19397+ ia->ia_valid |= ATTR_FILE;
19398+ }
4f0767ce 19399+out_si:
4a4d8108 19400+ si_read_unlock(sb);
e49829fe 19401+out_kfree:
4a4d8108 19402+ kfree(a);
4f0767ce 19403+out:
4a4d8108
AM
19404+ AuTraceErr(err);
19405+ return err;
1facf9fc 19406+}
19407+
c1595e42
JR
19408+#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
19409+static int au_h_path_to_set_attr(struct dentry *dentry,
19410+ struct au_icpup_args *a, struct path *h_path)
19411+{
19412+ int err;
19413+ struct super_block *sb;
19414+
19415+ sb = dentry->d_sb;
19416+ a->udba = au_opt_udba(sb);
19417+ /* no d_unlinked(), to set UDBA_NONE for root */
19418+ if (d_unhashed(dentry))
19419+ a->udba = AuOpt_UDBA_NONE;
19420+ if (a->udba != AuOpt_UDBA_NONE) {
19421+ AuDebugOn(IS_ROOT(dentry));
19422+ err = au_reval_for_attr(dentry, au_sigen(sb));
19423+ if (unlikely(err))
19424+ goto out;
19425+ }
19426+ err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
19427+ if (unlikely(err < 0))
19428+ goto out;
19429+
19430+ h_path->dentry = a->h_path.dentry;
19431+ h_path->mnt = au_sbr_mnt(sb, a->btgt);
19432+
19433+out:
19434+ return err;
19435+}
19436+
5afbbe0d
AM
19437+ssize_t au_srxattr(struct dentry *dentry, struct inode *inode,
19438+ struct au_srxattr *arg)
c1595e42
JR
19439+{
19440+ int err;
19441+ struct path h_path;
19442+ struct super_block *sb;
19443+ struct au_icpup_args *a;
5afbbe0d 19444+ struct inode *h_inode;
c1595e42 19445+
c1595e42
JR
19446+ IMustLock(inode);
19447+
19448+ err = -ENOMEM;
19449+ a = kzalloc(sizeof(*a), GFP_NOFS);
19450+ if (unlikely(!a))
19451+ goto out;
19452+
19453+ sb = dentry->d_sb;
19454+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19455+ if (unlikely(err))
19456+ goto out_kfree;
19457+
19458+ h_path.dentry = NULL; /* silence gcc */
19459+ di_write_lock_child(dentry);
19460+ err = au_h_path_to_set_attr(dentry, a, &h_path);
19461+ if (unlikely(err))
19462+ goto out_di;
19463+
febd17d6 19464+ inode_unlock(a->h_inode);
c1595e42
JR
19465+ switch (arg->type) {
19466+ case AU_XATTR_SET:
5afbbe0d 19467+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
19468+ err = vfsub_setxattr(h_path.dentry,
19469+ arg->u.set.name, arg->u.set.value,
19470+ arg->u.set.size, arg->u.set.flags);
19471+ break;
19472+ case AU_XATTR_REMOVE:
19473+ err = vfsub_removexattr(h_path.dentry, arg->u.remove.name);
19474+ break;
19475+ case AU_ACL_SET:
19476+ err = -EOPNOTSUPP;
5527c038 19477+ h_inode = d_inode(h_path.dentry);
c1595e42
JR
19478+ if (h_inode->i_op->set_acl)
19479+ err = h_inode->i_op->set_acl(h_inode,
19480+ arg->u.acl_set.acl,
19481+ arg->u.acl_set.type);
19482+ break;
19483+ }
19484+ if (!err)
19485+ au_cpup_attr_timesizes(inode);
19486+
19487+ au_unpin(&a->pin);
19488+ if (unlikely(err))
5afbbe0d 19489+ au_update_dbtop(dentry);
c1595e42
JR
19490+
19491+out_di:
19492+ di_write_unlock(dentry);
19493+ si_read_unlock(sb);
19494+out_kfree:
19495+ kfree(a);
19496+out:
19497+ AuTraceErr(err);
19498+ return err;
19499+}
19500+#endif
19501+
4a4d8108
AM
19502+static void au_refresh_iattr(struct inode *inode, struct kstat *st,
19503+ unsigned int nlink)
1facf9fc 19504+{
9dbd164d
AM
19505+ unsigned int n;
19506+
4a4d8108 19507+ inode->i_mode = st->mode;
86dc4139
AM
19508+ /* don't i_[ug]id_write() here */
19509+ inode->i_uid = st->uid;
19510+ inode->i_gid = st->gid;
4a4d8108
AM
19511+ inode->i_atime = st->atime;
19512+ inode->i_mtime = st->mtime;
19513+ inode->i_ctime = st->ctime;
1facf9fc 19514+
4a4d8108
AM
19515+ au_cpup_attr_nlink(inode, /*force*/0);
19516+ if (S_ISDIR(inode->i_mode)) {
9dbd164d
AM
19517+ n = inode->i_nlink;
19518+ n -= nlink;
19519+ n += st->nlink;
f6b6e03d 19520+ smp_mb(); /* for i_nlink */
7eafdf33 19521+ /* 0 can happen */
92d182d2 19522+ set_nlink(inode, n);
4a4d8108 19523+ }
1facf9fc 19524+
4a4d8108
AM
19525+ spin_lock(&inode->i_lock);
19526+ inode->i_blocks = st->blocks;
19527+ i_size_write(inode, st->size);
19528+ spin_unlock(&inode->i_lock);
1facf9fc 19529+}
19530+
c1595e42
JR
19531+/*
19532+ * common routine for aufs_getattr() and aufs_getxattr().
19533+ * returns zero or negative (an error).
19534+ * @dentry will be read-locked in success.
19535+ */
19536+int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path)
1facf9fc 19537+{
4a4d8108 19538+ int err;
076b876e 19539+ unsigned int mnt_flags, sigen;
c1595e42 19540+ unsigned char udba_none;
4a4d8108 19541+ aufs_bindex_t bindex;
4a4d8108
AM
19542+ struct super_block *sb, *h_sb;
19543+ struct inode *inode;
1facf9fc 19544+
c1595e42
JR
19545+ h_path->mnt = NULL;
19546+ h_path->dentry = NULL;
19547+
19548+ err = 0;
4a4d8108 19549+ sb = dentry->d_sb;
4a4d8108
AM
19550+ mnt_flags = au_mntflags(sb);
19551+ udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
1facf9fc 19552+
4a4d8108 19553+ /* support fstat(2) */
027c5e7a 19554+ if (!d_unlinked(dentry) && !udba_none) {
076b876e 19555+ sigen = au_sigen(sb);
027c5e7a
AM
19556+ err = au_digen_test(dentry, sigen);
19557+ if (!err) {
4a4d8108 19558+ di_read_lock_child(dentry, AuLock_IR);
027c5e7a 19559+ err = au_dbrange_test(dentry);
c1595e42
JR
19560+ if (unlikely(err)) {
19561+ di_read_unlock(dentry, AuLock_IR);
19562+ goto out;
19563+ }
027c5e7a 19564+ } else {
4a4d8108
AM
19565+ AuDebugOn(IS_ROOT(dentry));
19566+ di_write_lock_child(dentry);
027c5e7a
AM
19567+ err = au_dbrange_test(dentry);
19568+ if (!err)
19569+ err = au_reval_for_attr(dentry, sigen);
c1595e42
JR
19570+ if (!err)
19571+ di_downgrade_lock(dentry, AuLock_IR);
19572+ else {
19573+ di_write_unlock(dentry);
19574+ goto out;
19575+ }
4a4d8108
AM
19576+ }
19577+ } else
19578+ di_read_lock_child(dentry, AuLock_IR);
1facf9fc 19579+
5527c038 19580+ inode = d_inode(dentry);
5afbbe0d 19581+ bindex = au_ibtop(inode);
c1595e42
JR
19582+ h_path->mnt = au_sbr_mnt(sb, bindex);
19583+ h_sb = h_path->mnt->mnt_sb;
19584+ if (!force
19585+ && !au_test_fs_bad_iattr(h_sb)
19586+ && udba_none)
19587+ goto out; /* success */
1facf9fc 19588+
5afbbe0d 19589+ if (au_dbtop(dentry) == bindex)
c1595e42 19590+ h_path->dentry = au_h_dptr(dentry, bindex);
4a4d8108 19591+ else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
c1595e42
JR
19592+ h_path->dentry = au_plink_lkup(inode, bindex);
19593+ if (IS_ERR(h_path->dentry))
19594+ /* pretending success */
19595+ h_path->dentry = NULL;
19596+ else
19597+ dput(h_path->dentry);
4a4d8108 19598+ }
c1595e42
JR
19599+
19600+out:
19601+ return err;
19602+}
19603+
19604+static int aufs_getattr(struct vfsmount *mnt __maybe_unused,
19605+ struct dentry *dentry, struct kstat *st)
19606+{
19607+ int err;
19608+ unsigned char positive;
19609+ struct path h_path;
19610+ struct inode *inode;
19611+ struct super_block *sb;
19612+
5527c038 19613+ inode = d_inode(dentry);
c1595e42
JR
19614+ sb = dentry->d_sb;
19615+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19616+ if (unlikely(err))
19617+ goto out;
19618+ err = au_h_path_getattr(dentry, /*force*/0, &h_path);
19619+ if (unlikely(err))
19620+ goto out_si;
c06a8ce3 19621+ if (unlikely(!h_path.dentry))
c1595e42 19622+ /* illegally overlapped or something */
4a4d8108
AM
19623+ goto out_fill; /* pretending success */
19624+
5527c038 19625+ positive = d_is_positive(h_path.dentry);
4a4d8108 19626+ if (positive)
c06a8ce3 19627+ err = vfs_getattr(&h_path, st);
4a4d8108
AM
19628+ if (!err) {
19629+ if (positive)
c06a8ce3 19630+ au_refresh_iattr(inode, st,
5527c038 19631+ d_inode(h_path.dentry)->i_nlink);
4a4d8108 19632+ goto out_fill; /* success */
1facf9fc 19633+ }
7f207e10 19634+ AuTraceErr(err);
c1595e42 19635+ goto out_di;
4a4d8108 19636+
4f0767ce 19637+out_fill:
4a4d8108 19638+ generic_fillattr(inode, st);
c1595e42 19639+out_di:
4a4d8108 19640+ di_read_unlock(dentry, AuLock_IR);
c1595e42 19641+out_si:
4a4d8108 19642+ si_read_unlock(sb);
7f207e10
AM
19643+out:
19644+ AuTraceErr(err);
4a4d8108 19645+ return err;
1facf9fc 19646+}
19647+
19648+/* ---------------------------------------------------------------------- */
19649+
febd17d6
JR
19650+static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
19651+ struct delayed_call *done)
4a4d8108 19652+{
c2c0f25c 19653+ const char *ret;
c2c0f25c 19654+ struct dentry *h_dentry;
febd17d6 19655+ struct inode *h_inode;
4a4d8108 19656+ int err;
c2c0f25c 19657+ aufs_bindex_t bindex;
1facf9fc 19658+
79b8bda9 19659+ ret = NULL; /* suppress a warning */
febd17d6
JR
19660+ err = -ECHILD;
19661+ if (!dentry)
19662+ goto out;
19663+
027c5e7a
AM
19664+ err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
19665+ if (unlikely(err))
c2c0f25c 19666+ goto out;
027c5e7a
AM
19667+
19668+ err = au_d_hashed_positive(dentry);
c2c0f25c
AM
19669+ if (unlikely(err))
19670+ goto out_unlock;
19671+
19672+ err = -EINVAL;
19673+ inode = d_inode(dentry);
5afbbe0d 19674+ bindex = au_ibtop(inode);
c2c0f25c 19675+ h_inode = au_h_iptr(inode, bindex);
febd17d6 19676+ if (unlikely(!h_inode->i_op->get_link))
c2c0f25c
AM
19677+ goto out_unlock;
19678+
19679+ err = -EBUSY;
19680+ h_dentry = NULL;
5afbbe0d 19681+ if (au_dbtop(dentry) <= bindex) {
c2c0f25c
AM
19682+ h_dentry = au_h_dptr(dentry, bindex);
19683+ if (h_dentry)
19684+ dget(h_dentry);
027c5e7a 19685+ }
c2c0f25c
AM
19686+ if (!h_dentry) {
19687+ h_dentry = d_find_any_alias(h_inode);
19688+ if (IS_ERR(h_dentry)) {
19689+ err = PTR_ERR(h_dentry);
febd17d6 19690+ goto out_unlock;
c2c0f25c
AM
19691+ }
19692+ }
19693+ if (unlikely(!h_dentry))
febd17d6 19694+ goto out_unlock;
1facf9fc 19695+
c2c0f25c 19696+ err = 0;
febd17d6 19697+ AuDbg("%pf\n", h_inode->i_op->get_link);
c2c0f25c 19698+ AuDbgDentry(h_dentry);
febd17d6 19699+ ret = h_inode->i_op->get_link(h_dentry, h_inode, done);
c2c0f25c 19700+ dput(h_dentry);
febd17d6
JR
19701+ if (IS_ERR(ret))
19702+ err = PTR_ERR(ret);
c2c0f25c 19703+
c2c0f25c
AM
19704+out_unlock:
19705+ aufs_read_unlock(dentry, AuLock_IR);
4f0767ce 19706+out:
c2c0f25c
AM
19707+ if (unlikely(err))
19708+ ret = ERR_PTR(err);
19709+ AuTraceErrPtr(ret);
19710+ return ret;
4a4d8108 19711+}
1facf9fc 19712+
4a4d8108 19713+/* ---------------------------------------------------------------------- */
1facf9fc 19714+
0c3ec466 19715+static int aufs_update_time(struct inode *inode, struct timespec *ts, int flags)
4a4d8108 19716+{
0c3ec466
AM
19717+ int err;
19718+ struct super_block *sb;
19719+ struct inode *h_inode;
19720+
19721+ sb = inode->i_sb;
19722+ /* mmap_sem might be acquired already, cf. aufs_mmap() */
19723+ lockdep_off();
19724+ si_read_lock(sb, AuLock_FLUSH);
19725+ ii_write_lock_child(inode);
19726+ lockdep_on();
5afbbe0d 19727+ h_inode = au_h_iptr(inode, au_ibtop(inode));
0c3ec466
AM
19728+ err = vfsub_update_time(h_inode, ts, flags);
19729+ lockdep_off();
38d290e6
JR
19730+ if (!err)
19731+ au_cpup_attr_timesizes(inode);
0c3ec466
AM
19732+ ii_write_unlock(inode);
19733+ si_read_unlock(sb);
19734+ lockdep_on();
38d290e6
JR
19735+
19736+ if (!err && (flags & S_VERSION))
19737+ inode_inc_iversion(inode);
19738+
0c3ec466 19739+ return err;
4a4d8108 19740+}
1facf9fc 19741+
4a4d8108 19742+/* ---------------------------------------------------------------------- */
1308ab2a 19743+
b95c5147
AM
19744+/* no getattr version will be set by module.c:aufs_init() */
19745+struct inode_operations aufs_iop_nogetattr[AuIop_Last],
19746+ aufs_iop[] = {
19747+ [AuIop_SYMLINK] = {
19748+ .permission = aufs_permission,
c1595e42 19749+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
19750+ .get_acl = aufs_get_acl,
19751+ .set_acl = aufs_set_acl, /* unsupport for symlink? */
c1595e42
JR
19752+#endif
19753+
b95c5147
AM
19754+ .setattr = aufs_setattr,
19755+ .getattr = aufs_getattr,
0c3ec466 19756+
c1595e42 19757+#ifdef CONFIG_AUFS_XATTR
b95c5147
AM
19758+ .setxattr = aufs_setxattr,
19759+ .getxattr = aufs_getxattr,
19760+ .listxattr = aufs_listxattr,
19761+ .removexattr = aufs_removexattr,
c1595e42
JR
19762+#endif
19763+
b95c5147 19764+ .readlink = generic_readlink,
febd17d6 19765+ .get_link = aufs_get_link,
0c3ec466 19766+
b95c5147
AM
19767+ /* .update_time = aufs_update_time */
19768+ },
19769+ [AuIop_DIR] = {
19770+ .create = aufs_create,
19771+ .lookup = aufs_lookup,
19772+ .link = aufs_link,
19773+ .unlink = aufs_unlink,
19774+ .symlink = aufs_symlink,
19775+ .mkdir = aufs_mkdir,
19776+ .rmdir = aufs_rmdir,
19777+ .mknod = aufs_mknod,
19778+ .rename = aufs_rename,
19779+
19780+ .permission = aufs_permission,
c1595e42 19781+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
19782+ .get_acl = aufs_get_acl,
19783+ .set_acl = aufs_set_acl,
c1595e42
JR
19784+#endif
19785+
b95c5147
AM
19786+ .setattr = aufs_setattr,
19787+ .getattr = aufs_getattr,
0c3ec466 19788+
c1595e42 19789+#ifdef CONFIG_AUFS_XATTR
b95c5147
AM
19790+ .setxattr = aufs_setxattr,
19791+ .getxattr = aufs_getxattr,
19792+ .listxattr = aufs_listxattr,
19793+ .removexattr = aufs_removexattr,
c1595e42
JR
19794+#endif
19795+
b95c5147
AM
19796+ .update_time = aufs_update_time,
19797+ .atomic_open = aufs_atomic_open,
19798+ .tmpfile = aufs_tmpfile
19799+ },
19800+ [AuIop_OTHER] = {
19801+ .permission = aufs_permission,
c1595e42 19802+#ifdef CONFIG_FS_POSIX_ACL
b95c5147
AM
19803+ .get_acl = aufs_get_acl,
19804+ .set_acl = aufs_set_acl,
c1595e42
JR
19805+#endif
19806+
b95c5147
AM
19807+ .setattr = aufs_setattr,
19808+ .getattr = aufs_getattr,
0c3ec466 19809+
c1595e42 19810+#ifdef CONFIG_AUFS_XATTR
b95c5147
AM
19811+ .setxattr = aufs_setxattr,
19812+ .getxattr = aufs_getxattr,
19813+ .listxattr = aufs_listxattr,
19814+ .removexattr = aufs_removexattr,
c1595e42
JR
19815+#endif
19816+
b95c5147
AM
19817+ .update_time = aufs_update_time
19818+ }
4a4d8108 19819+};
7f207e10
AM
19820diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
19821--- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
19822+++ linux/fs/aufs/i_op_del.c 2016-07-25 19:05:34.814493242 +0200
19823@@ -0,0 +1,511 @@
1facf9fc 19824+/*
8cdd5066 19825+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 19826+ *
19827+ * This program, aufs is free software; you can redistribute it and/or modify
19828+ * it under the terms of the GNU General Public License as published by
19829+ * the Free Software Foundation; either version 2 of the License, or
19830+ * (at your option) any later version.
dece6358
AM
19831+ *
19832+ * This program is distributed in the hope that it will be useful,
19833+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
19834+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19835+ * GNU General Public License for more details.
19836+ *
19837+ * You should have received a copy of the GNU General Public License
523b37e3 19838+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 19839+ */
19840+
19841+/*
4a4d8108 19842+ * inode operations (del entry)
1308ab2a 19843+ */
dece6358 19844+
1308ab2a 19845+#include "aufs.h"
dece6358 19846+
4a4d8108
AM
19847+/*
19848+ * decide if a new whiteout for @dentry is necessary or not.
19849+ * when it is necessary, prepare the parent dir for the upper branch whose
19850+ * branch index is @bcpup for creation. the actual creation of the whiteout will
19851+ * be done by caller.
19852+ * return value:
19853+ * 0: wh is unnecessary
19854+ * plus: wh is necessary
19855+ * minus: error
19856+ */
19857+int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
1308ab2a 19858+{
4a4d8108 19859+ int need_wh, err;
5afbbe0d 19860+ aufs_bindex_t btop;
4a4d8108 19861+ struct super_block *sb;
dece6358 19862+
4a4d8108 19863+ sb = dentry->d_sb;
5afbbe0d 19864+ btop = au_dbtop(dentry);
4a4d8108 19865+ if (*bcpup < 0) {
5afbbe0d
AM
19866+ *bcpup = btop;
19867+ if (au_test_ro(sb, btop, d_inode(dentry))) {
4a4d8108
AM
19868+ err = AuWbrCopyup(au_sbi(sb), dentry);
19869+ *bcpup = err;
19870+ if (unlikely(err < 0))
19871+ goto out;
19872+ }
19873+ } else
5afbbe0d 19874+ AuDebugOn(btop < *bcpup
5527c038 19875+ || au_test_ro(sb, *bcpup, d_inode(dentry)));
5afbbe0d 19876+ AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
1308ab2a 19877+
5afbbe0d 19878+ if (*bcpup != btop) {
4a4d8108
AM
19879+ err = au_cpup_dirs(dentry, *bcpup);
19880+ if (unlikely(err))
19881+ goto out;
19882+ need_wh = 1;
19883+ } else {
027c5e7a 19884+ struct au_dinfo *dinfo, *tmp;
4a4d8108 19885+
027c5e7a
AM
19886+ need_wh = -ENOMEM;
19887+ dinfo = au_di(dentry);
19888+ tmp = au_di_alloc(sb, AuLsc_DI_TMP);
19889+ if (tmp) {
19890+ au_di_cp(tmp, dinfo);
19891+ au_di_swap(tmp, dinfo);
19892+ /* returns the number of positive dentries */
5afbbe0d
AM
19893+ need_wh = au_lkup_dentry(dentry, btop + 1,
19894+ /* AuLkup_IGNORE_PERM */ 0);
027c5e7a
AM
19895+ au_di_swap(tmp, dinfo);
19896+ au_rw_write_unlock(&tmp->di_rwsem);
19897+ au_di_free(tmp);
4a4d8108
AM
19898+ }
19899+ }
19900+ AuDbg("need_wh %d\n", need_wh);
19901+ err = need_wh;
19902+
4f0767ce 19903+out:
4a4d8108 19904+ return err;
1facf9fc 19905+}
19906+
4a4d8108
AM
19907+/*
19908+ * simple tests for the del-entry operations.
19909+ * following the checks in vfs, plus the parent-child relationship.
19910+ */
19911+int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
19912+ struct dentry *h_parent, int isdir)
1facf9fc 19913+{
4a4d8108
AM
19914+ int err;
19915+ umode_t h_mode;
19916+ struct dentry *h_dentry, *h_latest;
1308ab2a 19917+ struct inode *h_inode;
1facf9fc 19918+
4a4d8108 19919+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 19920+ if (d_really_is_positive(dentry)) {
4a4d8108 19921+ err = -ENOENT;
5527c038
JR
19922+ if (unlikely(d_is_negative(h_dentry)))
19923+ goto out;
19924+ h_inode = d_inode(h_dentry);
19925+ if (unlikely(!h_inode->i_nlink))
4a4d8108 19926+ goto out;
1facf9fc 19927+
4a4d8108
AM
19928+ h_mode = h_inode->i_mode;
19929+ if (!isdir) {
19930+ err = -EISDIR;
19931+ if (unlikely(S_ISDIR(h_mode)))
19932+ goto out;
19933+ } else if (unlikely(!S_ISDIR(h_mode))) {
19934+ err = -ENOTDIR;
19935+ goto out;
19936+ }
19937+ } else {
19938+ /* rename(2) case */
19939+ err = -EIO;
5527c038 19940+ if (unlikely(d_is_positive(h_dentry)))
4a4d8108
AM
19941+ goto out;
19942+ }
1facf9fc 19943+
4a4d8108
AM
19944+ err = -ENOENT;
19945+ /* expected parent dir is locked */
19946+ if (unlikely(h_parent != h_dentry->d_parent))
19947+ goto out;
19948+ err = 0;
19949+
19950+ /*
19951+ * rmdir a dir may break the consistency on some filesystem.
19952+ * let's try heavy test.
19953+ */
19954+ err = -EACCES;
076b876e 19955+ if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
5527c038 19956+ && au_test_h_perm(d_inode(h_parent),
076b876e 19957+ MAY_EXEC | MAY_WRITE)))
4a4d8108
AM
19958+ goto out;
19959+
076b876e 19960+ h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
4a4d8108
AM
19961+ err = -EIO;
19962+ if (IS_ERR(h_latest))
19963+ goto out;
19964+ if (h_latest == h_dentry)
19965+ err = 0;
19966+ dput(h_latest);
19967+
4f0767ce 19968+out:
4a4d8108 19969+ return err;
1308ab2a 19970+}
1facf9fc 19971+
4a4d8108
AM
19972+/*
19973+ * decide the branch where we operate for @dentry. the branch index will be set
19974+ * @rbcpup. after diciding it, 'pin' it and store the timestamps of the parent
19975+ * dir for reverting.
19976+ * when a new whiteout is necessary, create it.
19977+ */
19978+static struct dentry*
19979+lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
19980+ struct au_dtime *dt, struct au_pin *pin)
1308ab2a 19981+{
4a4d8108
AM
19982+ struct dentry *wh_dentry;
19983+ struct super_block *sb;
19984+ struct path h_path;
19985+ int err, need_wh;
19986+ unsigned int udba;
19987+ aufs_bindex_t bcpup;
dece6358 19988+
4a4d8108
AM
19989+ need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
19990+ wh_dentry = ERR_PTR(need_wh);
19991+ if (unlikely(need_wh < 0))
19992+ goto out;
19993+
19994+ sb = dentry->d_sb;
19995+ udba = au_opt_udba(sb);
19996+ bcpup = *rbcpup;
19997+ err = au_pin(pin, dentry, bcpup, udba,
19998+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19999+ wh_dentry = ERR_PTR(err);
20000+ if (unlikely(err))
20001+ goto out;
20002+
20003+ h_path.dentry = au_pinned_h_parent(pin);
20004+ if (udba != AuOpt_UDBA_NONE
5afbbe0d 20005+ && au_dbtop(dentry) == bcpup) {
4a4d8108
AM
20006+ err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
20007+ wh_dentry = ERR_PTR(err);
20008+ if (unlikely(err))
20009+ goto out_unpin;
20010+ }
20011+
20012+ h_path.mnt = au_sbr_mnt(sb, bcpup);
20013+ au_dtime_store(dt, au_pinned_parent(pin), &h_path);
20014+ wh_dentry = NULL;
20015+ if (!need_wh)
20016+ goto out; /* success, no need to create whiteout */
20017+
20018+ wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
20019+ if (IS_ERR(wh_dentry))
20020+ goto out_unpin;
20021+
20022+ /* returns with the parent is locked and wh_dentry is dget-ed */
20023+ goto out; /* success */
20024+
4f0767ce 20025+out_unpin:
4a4d8108 20026+ au_unpin(pin);
4f0767ce 20027+out:
4a4d8108 20028+ return wh_dentry;
1facf9fc 20029+}
20030+
4a4d8108
AM
20031+/*
20032+ * when removing a dir, rename it to a unique temporary whiteout-ed name first
20033+ * in order to be revertible and save time for removing many child whiteouts
20034+ * under the dir.
20035+ * returns 1 when there are too many child whiteout and caller should remove
20036+ * them asynchronously. returns 0 when the number of children is enough small to
20037+ * remove now or the branch fs is a remote fs.
20038+ * otherwise return an error.
20039+ */
20040+static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
20041+ struct au_nhash *whlist, struct inode *dir)
1facf9fc 20042+{
4a4d8108
AM
20043+ int rmdir_later, err, dirwh;
20044+ struct dentry *h_dentry;
20045+ struct super_block *sb;
5527c038 20046+ struct inode *inode;
4a4d8108
AM
20047+
20048+ sb = dentry->d_sb;
20049+ SiMustAnyLock(sb);
20050+ h_dentry = au_h_dptr(dentry, bindex);
20051+ err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
20052+ if (unlikely(err))
20053+ goto out;
20054+
20055+ /* stop monitoring */
5527c038
JR
20056+ inode = d_inode(dentry);
20057+ au_hn_free(au_hi(inode, bindex));
4a4d8108
AM
20058+
20059+ if (!au_test_fs_remote(h_dentry->d_sb)) {
20060+ dirwh = au_sbi(sb)->si_dirwh;
20061+ rmdir_later = (dirwh <= 1);
20062+ if (!rmdir_later)
20063+ rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
20064+ dirwh);
20065+ if (rmdir_later)
20066+ return rmdir_later;
20067+ }
1facf9fc 20068+
4a4d8108
AM
20069+ err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
20070+ if (unlikely(err)) {
523b37e3
AM
20071+ AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
20072+ h_dentry, bindex, err);
4a4d8108
AM
20073+ err = 0;
20074+ }
dece6358 20075+
4f0767ce 20076+out:
4a4d8108
AM
20077+ AuTraceErr(err);
20078+ return err;
20079+}
1308ab2a 20080+
4a4d8108
AM
20081+/*
20082+ * final procedure for deleting a entry.
20083+ * maintain dentry and iattr.
20084+ */
20085+static void epilog(struct inode *dir, struct dentry *dentry,
20086+ aufs_bindex_t bindex)
20087+{
20088+ struct inode *inode;
1308ab2a 20089+
5527c038 20090+ inode = d_inode(dentry);
4a4d8108
AM
20091+ d_drop(dentry);
20092+ inode->i_ctime = dir->i_ctime;
1308ab2a 20093+
b912730e 20094+ au_dir_ts(dir, bindex);
4a4d8108 20095+ dir->i_version++;
1facf9fc 20096+}
20097+
4a4d8108
AM
20098+/*
20099+ * when an error happened, remove the created whiteout and revert everything.
20100+ */
7f207e10
AM
20101+static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
20102+ aufs_bindex_t bwh, struct dentry *wh_dentry,
20103+ struct dentry *dentry, struct au_dtime *dt)
1facf9fc 20104+{
4a4d8108
AM
20105+ int rerr;
20106+ struct path h_path = {
20107+ .dentry = wh_dentry,
7f207e10 20108+ .mnt = au_sbr_mnt(dir->i_sb, bindex)
4a4d8108 20109+ };
dece6358 20110+
7f207e10 20111+ rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
4a4d8108
AM
20112+ if (!rerr) {
20113+ au_set_dbwh(dentry, bwh);
20114+ au_dtime_revert(dt);
20115+ return 0;
20116+ }
dece6358 20117+
523b37e3 20118+ AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
4a4d8108 20119+ return -EIO;
1facf9fc 20120+}
20121+
4a4d8108 20122+/* ---------------------------------------------------------------------- */
1facf9fc 20123+
4a4d8108 20124+int aufs_unlink(struct inode *dir, struct dentry *dentry)
1308ab2a 20125+{
4a4d8108 20126+ int err;
5afbbe0d 20127+ aufs_bindex_t bwh, bindex, btop;
523b37e3 20128+ struct inode *inode, *h_dir, *delegated;
4a4d8108 20129+ struct dentry *parent, *wh_dentry;
c2b27bf2
AM
20130+ /* to reuduce stack size */
20131+ struct {
20132+ struct au_dtime dt;
20133+ struct au_pin pin;
20134+ struct path h_path;
20135+ } *a;
1facf9fc 20136+
4a4d8108 20137+ IMustLock(dir);
027c5e7a 20138+
c2b27bf2
AM
20139+ err = -ENOMEM;
20140+ a = kmalloc(sizeof(*a), GFP_NOFS);
20141+ if (unlikely(!a))
20142+ goto out;
20143+
027c5e7a
AM
20144+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20145+ if (unlikely(err))
c2b27bf2 20146+ goto out_free;
027c5e7a
AM
20147+ err = au_d_hashed_positive(dentry);
20148+ if (unlikely(err))
20149+ goto out_unlock;
5527c038 20150+ inode = d_inode(dentry);
4a4d8108 20151+ IMustLock(inode);
027c5e7a 20152+ err = -EISDIR;
2000de60 20153+ if (unlikely(d_is_dir(dentry)))
027c5e7a 20154+ goto out_unlock; /* possible? */
1facf9fc 20155+
5afbbe0d 20156+ btop = au_dbtop(dentry);
4a4d8108
AM
20157+ bwh = au_dbwh(dentry);
20158+ bindex = -1;
027c5e7a
AM
20159+ parent = dentry->d_parent; /* dir inode is locked */
20160+ di_write_lock_parent(parent);
c2b27bf2
AM
20161+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
20162+ &a->pin);
4a4d8108
AM
20163+ err = PTR_ERR(wh_dentry);
20164+ if (IS_ERR(wh_dentry))
027c5e7a 20165+ goto out_parent;
1facf9fc 20166+
5afbbe0d
AM
20167+ a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
20168+ a->h_path.dentry = au_h_dptr(dentry, btop);
c2b27bf2 20169+ dget(a->h_path.dentry);
5afbbe0d 20170+ if (bindex == btop) {
c2b27bf2 20171+ h_dir = au_pinned_h_dir(&a->pin);
523b37e3
AM
20172+ delegated = NULL;
20173+ err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
20174+ if (unlikely(err == -EWOULDBLOCK)) {
20175+ pr_warn("cannot retry for NFSv4 delegation"
20176+ " for an internal unlink\n");
20177+ iput(delegated);
20178+ }
4a4d8108
AM
20179+ } else {
20180+ /* dir inode is locked */
5527c038 20181+ h_dir = d_inode(wh_dentry->d_parent);
4a4d8108
AM
20182+ IMustLock(h_dir);
20183+ err = 0;
20184+ }
dece6358 20185+
4a4d8108 20186+ if (!err) {
7f207e10 20187+ vfsub_drop_nlink(inode);
4a4d8108
AM
20188+ epilog(dir, dentry, bindex);
20189+
20190+ /* update target timestamps */
5afbbe0d 20191+ if (bindex == btop) {
c2b27bf2
AM
20192+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
20193+ /*ignore*/
5527c038 20194+ inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
4a4d8108
AM
20195+ } else
20196+ /* todo: this timestamp may be reverted later */
20197+ inode->i_ctime = h_dir->i_ctime;
027c5e7a 20198+ goto out_unpin; /* success */
1facf9fc 20199+ }
20200+
4a4d8108
AM
20201+ /* revert */
20202+ if (wh_dentry) {
20203+ int rerr;
20204+
c2b27bf2
AM
20205+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20206+ &a->dt);
4a4d8108
AM
20207+ if (rerr)
20208+ err = rerr;
dece6358 20209+ }
1facf9fc 20210+
027c5e7a 20211+out_unpin:
c2b27bf2 20212+ au_unpin(&a->pin);
4a4d8108 20213+ dput(wh_dentry);
c2b27bf2 20214+ dput(a->h_path.dentry);
027c5e7a 20215+out_parent:
4a4d8108 20216+ di_write_unlock(parent);
027c5e7a 20217+out_unlock:
4a4d8108 20218+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2
AM
20219+out_free:
20220+ kfree(a);
027c5e7a 20221+out:
4a4d8108 20222+ return err;
dece6358
AM
20223+}
20224+
4a4d8108 20225+int aufs_rmdir(struct inode *dir, struct dentry *dentry)
1308ab2a 20226+{
4a4d8108 20227+ int err, rmdir_later;
5afbbe0d 20228+ aufs_bindex_t bwh, bindex, btop;
4a4d8108
AM
20229+ struct inode *inode;
20230+ struct dentry *parent, *wh_dentry, *h_dentry;
20231+ struct au_whtmp_rmdir *args;
c2b27bf2
AM
20232+ /* to reuduce stack size */
20233+ struct {
20234+ struct au_dtime dt;
20235+ struct au_pin pin;
20236+ } *a;
1facf9fc 20237+
4a4d8108 20238+ IMustLock(dir);
027c5e7a 20239+
c2b27bf2
AM
20240+ err = -ENOMEM;
20241+ a = kmalloc(sizeof(*a), GFP_NOFS);
20242+ if (unlikely(!a))
20243+ goto out;
20244+
027c5e7a
AM
20245+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20246+ if (unlikely(err))
c2b27bf2 20247+ goto out_free;
53392da6
AM
20248+ err = au_alive_dir(dentry);
20249+ if (unlikely(err))
027c5e7a 20250+ goto out_unlock;
5527c038 20251+ inode = d_inode(dentry);
4a4d8108 20252+ IMustLock(inode);
027c5e7a 20253+ err = -ENOTDIR;
2000de60 20254+ if (unlikely(!d_is_dir(dentry)))
027c5e7a 20255+ goto out_unlock; /* possible? */
dece6358 20256+
4a4d8108
AM
20257+ err = -ENOMEM;
20258+ args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
20259+ if (unlikely(!args))
20260+ goto out_unlock;
dece6358 20261+
4a4d8108
AM
20262+ parent = dentry->d_parent; /* dir inode is locked */
20263+ di_write_lock_parent(parent);
20264+ err = au_test_empty(dentry, &args->whlist);
20265+ if (unlikely(err))
027c5e7a 20266+ goto out_parent;
1facf9fc 20267+
5afbbe0d 20268+ btop = au_dbtop(dentry);
4a4d8108
AM
20269+ bwh = au_dbwh(dentry);
20270+ bindex = -1;
c2b27bf2
AM
20271+ wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
20272+ &a->pin);
4a4d8108
AM
20273+ err = PTR_ERR(wh_dentry);
20274+ if (IS_ERR(wh_dentry))
027c5e7a 20275+ goto out_parent;
1facf9fc 20276+
5afbbe0d 20277+ h_dentry = au_h_dptr(dentry, btop);
4a4d8108
AM
20278+ dget(h_dentry);
20279+ rmdir_later = 0;
5afbbe0d
AM
20280+ if (bindex == btop) {
20281+ err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
4a4d8108
AM
20282+ if (err > 0) {
20283+ rmdir_later = err;
20284+ err = 0;
20285+ }
20286+ } else {
20287+ /* stop monitoring */
5afbbe0d 20288+ au_hn_free(au_hi(inode, btop));
4a4d8108
AM
20289+
20290+ /* dir inode is locked */
5527c038 20291+ IMustLock(d_inode(wh_dentry->d_parent));
1facf9fc 20292+ err = 0;
20293+ }
20294+
4a4d8108 20295+ if (!err) {
027c5e7a 20296+ vfsub_dead_dir(inode);
4a4d8108
AM
20297+ au_set_dbdiropq(dentry, -1);
20298+ epilog(dir, dentry, bindex);
1308ab2a 20299+
4a4d8108 20300+ if (rmdir_later) {
5afbbe0d 20301+ au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
4a4d8108
AM
20302+ args = NULL;
20303+ }
1308ab2a 20304+
4a4d8108 20305+ goto out_unpin; /* success */
1facf9fc 20306+ }
20307+
4a4d8108
AM
20308+ /* revert */
20309+ AuLabel(revert);
20310+ if (wh_dentry) {
20311+ int rerr;
1308ab2a 20312+
c2b27bf2
AM
20313+ rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
20314+ &a->dt);
4a4d8108
AM
20315+ if (rerr)
20316+ err = rerr;
1facf9fc 20317+ }
20318+
4f0767ce 20319+out_unpin:
c2b27bf2 20320+ au_unpin(&a->pin);
4a4d8108
AM
20321+ dput(wh_dentry);
20322+ dput(h_dentry);
027c5e7a 20323+out_parent:
4a4d8108
AM
20324+ di_write_unlock(parent);
20325+ if (args)
20326+ au_whtmp_rmdir_free(args);
4f0767ce 20327+out_unlock:
4a4d8108 20328+ aufs_read_unlock(dentry, AuLock_DW);
c2b27bf2
AM
20329+out_free:
20330+ kfree(a);
4f0767ce 20331+out:
4a4d8108
AM
20332+ AuTraceErr(err);
20333+ return err;
dece6358 20334+}
7f207e10
AM
20335diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
20336--- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 20337+++ linux/fs/aufs/i_op_ren.c 2016-07-25 19:05:34.814493242 +0200
b95c5147 20338@@ -0,0 +1,1015 @@
1facf9fc 20339+/*
8cdd5066 20340+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 20341+ *
20342+ * This program, aufs is free software; you can redistribute it and/or modify
20343+ * it under the terms of the GNU General Public License as published by
20344+ * the Free Software Foundation; either version 2 of the License, or
20345+ * (at your option) any later version.
dece6358
AM
20346+ *
20347+ * This program is distributed in the hope that it will be useful,
20348+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20349+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20350+ * GNU General Public License for more details.
20351+ *
20352+ * You should have received a copy of the GNU General Public License
523b37e3 20353+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 20354+ */
20355+
20356+/*
4a4d8108
AM
20357+ * inode operation (rename entry)
20358+ * todo: this is crazy monster
1facf9fc 20359+ */
20360+
20361+#include "aufs.h"
20362+
4a4d8108
AM
20363+enum { AuSRC, AuDST, AuSrcDst };
20364+enum { AuPARENT, AuCHILD, AuParentChild };
1facf9fc 20365+
4a4d8108
AM
20366+#define AuRen_ISDIR 1
20367+#define AuRen_ISSAMEDIR (1 << 1)
20368+#define AuRen_WHSRC (1 << 2)
20369+#define AuRen_WHDST (1 << 3)
20370+#define AuRen_MNT_WRITE (1 << 4)
20371+#define AuRen_DT_DSTDIR (1 << 5)
20372+#define AuRen_DIROPQ (1 << 6)
4a4d8108 20373+#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
7f207e10
AM
20374+#define au_fset_ren(flags, name) \
20375+ do { (flags) |= AuRen_##name; } while (0)
20376+#define au_fclr_ren(flags, name) \
20377+ do { (flags) &= ~AuRen_##name; } while (0)
1facf9fc 20378+
4a4d8108
AM
20379+struct au_ren_args {
20380+ struct {
20381+ struct dentry *dentry, *h_dentry, *parent, *h_parent,
20382+ *wh_dentry;
20383+ struct inode *dir, *inode;
20384+ struct au_hinode *hdir;
20385+ struct au_dtime dt[AuParentChild];
5afbbe0d 20386+ aufs_bindex_t btop;
4a4d8108 20387+ } sd[AuSrcDst];
1facf9fc 20388+
4a4d8108
AM
20389+#define src_dentry sd[AuSRC].dentry
20390+#define src_dir sd[AuSRC].dir
20391+#define src_inode sd[AuSRC].inode
20392+#define src_h_dentry sd[AuSRC].h_dentry
20393+#define src_parent sd[AuSRC].parent
20394+#define src_h_parent sd[AuSRC].h_parent
20395+#define src_wh_dentry sd[AuSRC].wh_dentry
20396+#define src_hdir sd[AuSRC].hdir
20397+#define src_h_dir sd[AuSRC].hdir->hi_inode
20398+#define src_dt sd[AuSRC].dt
5afbbe0d 20399+#define src_btop sd[AuSRC].btop
1facf9fc 20400+
4a4d8108
AM
20401+#define dst_dentry sd[AuDST].dentry
20402+#define dst_dir sd[AuDST].dir
20403+#define dst_inode sd[AuDST].inode
20404+#define dst_h_dentry sd[AuDST].h_dentry
20405+#define dst_parent sd[AuDST].parent
20406+#define dst_h_parent sd[AuDST].h_parent
20407+#define dst_wh_dentry sd[AuDST].wh_dentry
20408+#define dst_hdir sd[AuDST].hdir
20409+#define dst_h_dir sd[AuDST].hdir->hi_inode
20410+#define dst_dt sd[AuDST].dt
5afbbe0d 20411+#define dst_btop sd[AuDST].btop
4a4d8108
AM
20412+
20413+ struct dentry *h_trap;
20414+ struct au_branch *br;
20415+ struct au_hinode *src_hinode;
20416+ struct path h_path;
20417+ struct au_nhash whlist;
027c5e7a 20418+ aufs_bindex_t btgt, src_bwh, src_bdiropq;
1facf9fc 20419+
1308ab2a 20420+ unsigned int flags;
1facf9fc 20421+
4a4d8108
AM
20422+ struct au_whtmp_rmdir *thargs;
20423+ struct dentry *h_dst;
20424+};
1308ab2a 20425+
4a4d8108 20426+/* ---------------------------------------------------------------------- */
1308ab2a 20427+
4a4d8108
AM
20428+/*
20429+ * functions for reverting.
20430+ * when an error happened in a single rename systemcall, we should revert
79b8bda9 20431+ * everything as if nothing happened.
4a4d8108
AM
20432+ * we don't need to revert the copied-up/down the parent dir since they are
20433+ * harmless.
20434+ */
1facf9fc 20435+
4a4d8108
AM
20436+#define RevertFailure(fmt, ...) do { \
20437+ AuIOErr("revert failure: " fmt " (%d, %d)\n", \
20438+ ##__VA_ARGS__, err, rerr); \
20439+ err = -EIO; \
20440+} while (0)
1facf9fc 20441+
4a4d8108 20442+static void au_ren_rev_diropq(int err, struct au_ren_args *a)
1facf9fc 20443+{
4a4d8108 20444+ int rerr;
1facf9fc 20445+
5afbbe0d 20446+ au_hn_inode_lock_nested(a->src_hinode, AuLsc_I_CHILD);
4a4d8108 20447+ rerr = au_diropq_remove(a->src_dentry, a->btgt);
5afbbe0d 20448+ au_hn_inode_unlock(a->src_hinode);
027c5e7a 20449+ au_set_dbdiropq(a->src_dentry, a->src_bdiropq);
4a4d8108 20450+ if (rerr)
523b37e3 20451+ RevertFailure("remove diropq %pd", a->src_dentry);
4a4d8108 20452+}
1facf9fc 20453+
4a4d8108
AM
20454+static void au_ren_rev_rename(int err, struct au_ren_args *a)
20455+{
20456+ int rerr;
523b37e3 20457+ struct inode *delegated;
1facf9fc 20458+
b4510431
AM
20459+ a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
20460+ a->src_h_parent);
4a4d8108
AM
20461+ rerr = PTR_ERR(a->h_path.dentry);
20462+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 20463+ RevertFailure("lkup one %pd", a->src_dentry);
4a4d8108 20464+ return;
1facf9fc 20465+ }
20466+
523b37e3 20467+ delegated = NULL;
4a4d8108
AM
20468+ rerr = vfsub_rename(a->dst_h_dir,
20469+ au_h_dptr(a->src_dentry, a->btgt),
523b37e3
AM
20470+ a->src_h_dir, &a->h_path, &delegated);
20471+ if (unlikely(rerr == -EWOULDBLOCK)) {
20472+ pr_warn("cannot retry for NFSv4 delegation"
20473+ " for an internal rename\n");
20474+ iput(delegated);
20475+ }
4a4d8108
AM
20476+ d_drop(a->h_path.dentry);
20477+ dput(a->h_path.dentry);
20478+ /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
20479+ if (rerr)
523b37e3 20480+ RevertFailure("rename %pd", a->src_dentry);
1facf9fc 20481+}
20482+
4a4d8108 20483+static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
1facf9fc 20484+{
4a4d8108 20485+ int rerr;
523b37e3 20486+ struct inode *delegated;
dece6358 20487+
b4510431
AM
20488+ a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
20489+ a->dst_h_parent);
4a4d8108
AM
20490+ rerr = PTR_ERR(a->h_path.dentry);
20491+ if (IS_ERR(a->h_path.dentry)) {
523b37e3 20492+ RevertFailure("lkup one %pd", a->dst_dentry);
4a4d8108
AM
20493+ return;
20494+ }
5527c038 20495+ if (d_is_positive(a->h_path.dentry)) {
4a4d8108
AM
20496+ d_drop(a->h_path.dentry);
20497+ dput(a->h_path.dentry);
20498+ return;
dece6358
AM
20499+ }
20500+
523b37e3
AM
20501+ delegated = NULL;
20502+ rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
20503+ &delegated);
20504+ if (unlikely(rerr == -EWOULDBLOCK)) {
20505+ pr_warn("cannot retry for NFSv4 delegation"
20506+ " for an internal rename\n");
20507+ iput(delegated);
20508+ }
4a4d8108
AM
20509+ d_drop(a->h_path.dentry);
20510+ dput(a->h_path.dentry);
20511+ if (!rerr)
20512+ au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
20513+ else
523b37e3 20514+ RevertFailure("rename %pd", a->h_dst);
4a4d8108 20515+}
1308ab2a 20516+
4a4d8108
AM
20517+static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
20518+{
20519+ int rerr;
1308ab2a 20520+
4a4d8108
AM
20521+ a->h_path.dentry = a->src_wh_dentry;
20522+ rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
027c5e7a 20523+ au_set_dbwh(a->src_dentry, a->src_bwh);
4a4d8108 20524+ if (rerr)
523b37e3 20525+ RevertFailure("unlink %pd", a->src_wh_dentry);
4a4d8108 20526+}
4a4d8108 20527+#undef RevertFailure
1facf9fc 20528+
1308ab2a 20529+/* ---------------------------------------------------------------------- */
20530+
4a4d8108
AM
20531+/*
20532+ * when we have to copyup the renaming entry, do it with the rename-target name
20533+ * in order to minimize the cost (the later actual rename is unnecessary).
20534+ * otherwise rename it on the target branch.
20535+ */
20536+static int au_ren_or_cpup(struct au_ren_args *a)
1facf9fc 20537+{
dece6358 20538+ int err;
4a4d8108 20539+ struct dentry *d;
523b37e3 20540+ struct inode *delegated;
1facf9fc 20541+
4a4d8108 20542+ d = a->src_dentry;
5afbbe0d 20543+ if (au_dbtop(d) == a->btgt) {
4a4d8108
AM
20544+ a->h_path.dentry = a->dst_h_dentry;
20545+ if (au_ftest_ren(a->flags, DIROPQ)
20546+ && au_dbdiropq(d) == a->btgt)
20547+ au_fclr_ren(a->flags, DIROPQ);
5afbbe0d 20548+ AuDebugOn(au_dbtop(d) != a->btgt);
523b37e3 20549+ delegated = NULL;
4a4d8108 20550+ err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
523b37e3
AM
20551+ a->dst_h_dir, &a->h_path, &delegated);
20552+ if (unlikely(err == -EWOULDBLOCK)) {
20553+ pr_warn("cannot retry for NFSv4 delegation"
20554+ " for an internal rename\n");
20555+ iput(delegated);
20556+ }
c2b27bf2 20557+ } else
86dc4139 20558+ BUG();
1308ab2a 20559+
027c5e7a
AM
20560+ if (!err && a->h_dst)
20561+ /* it will be set to dinfo later */
20562+ dget(a->h_dst);
1facf9fc 20563+
dece6358
AM
20564+ return err;
20565+}
1facf9fc 20566+
4a4d8108
AM
20567+/* cf. aufs_rmdir() */
20568+static int au_ren_del_whtmp(struct au_ren_args *a)
dece6358 20569+{
4a4d8108
AM
20570+ int err;
20571+ struct inode *dir;
1facf9fc 20572+
4a4d8108
AM
20573+ dir = a->dst_dir;
20574+ SiMustAnyLock(dir->i_sb);
20575+ if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
20576+ au_sbi(dir->i_sb)->si_dirwh)
20577+ || au_test_fs_remote(a->h_dst->d_sb)) {
20578+ err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
20579+ if (unlikely(err))
523b37e3
AM
20580+ pr_warn("failed removing whtmp dir %pd (%d), "
20581+ "ignored.\n", a->h_dst, err);
4a4d8108
AM
20582+ } else {
20583+ au_nhash_wh_free(&a->thargs->whlist);
20584+ a->thargs->whlist = a->whlist;
20585+ a->whlist.nh_num = 0;
20586+ au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
20587+ dput(a->h_dst);
20588+ a->thargs = NULL;
20589+ }
20590+
20591+ return 0;
1308ab2a 20592+}
1facf9fc 20593+
4a4d8108
AM
20594+/* make it 'opaque' dir. */
20595+static int au_ren_diropq(struct au_ren_args *a)
20596+{
20597+ int err;
20598+ struct dentry *diropq;
1facf9fc 20599+
4a4d8108 20600+ err = 0;
027c5e7a 20601+ a->src_bdiropq = au_dbdiropq(a->src_dentry);
4a4d8108 20602+ a->src_hinode = au_hi(a->src_inode, a->btgt);
5afbbe0d 20603+ au_hn_inode_lock_nested(a->src_hinode, AuLsc_I_CHILD);
4a4d8108 20604+ diropq = au_diropq_create(a->src_dentry, a->btgt);
5afbbe0d 20605+ au_hn_inode_unlock(a->src_hinode);
4a4d8108
AM
20606+ if (IS_ERR(diropq))
20607+ err = PTR_ERR(diropq);
076b876e
AM
20608+ else
20609+ dput(diropq);
1facf9fc 20610+
4a4d8108
AM
20611+ return err;
20612+}
1facf9fc 20613+
4a4d8108
AM
20614+static int do_rename(struct au_ren_args *a)
20615+{
20616+ int err;
20617+ struct dentry *d, *h_d;
1facf9fc 20618+
4a4d8108
AM
20619+ /* prepare workqueue args for asynchronous rmdir */
20620+ h_d = a->dst_h_dentry;
5527c038 20621+ if (au_ftest_ren(a->flags, ISDIR) && d_is_positive(h_d)) {
4a4d8108
AM
20622+ err = -ENOMEM;
20623+ a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb, GFP_NOFS);
20624+ if (unlikely(!a->thargs))
20625+ goto out;
20626+ a->h_dst = dget(h_d);
20627+ }
1facf9fc 20628+
4a4d8108
AM
20629+ /* create whiteout for src_dentry */
20630+ if (au_ftest_ren(a->flags, WHSRC)) {
027c5e7a
AM
20631+ a->src_bwh = au_dbwh(a->src_dentry);
20632+ AuDebugOn(a->src_bwh >= 0);
4a4d8108
AM
20633+ a->src_wh_dentry
20634+ = au_wh_create(a->src_dentry, a->btgt, a->src_h_parent);
20635+ err = PTR_ERR(a->src_wh_dentry);
20636+ if (IS_ERR(a->src_wh_dentry))
20637+ goto out_thargs;
20638+ }
1facf9fc 20639+
4a4d8108
AM
20640+ /* lookup whiteout for dentry */
20641+ if (au_ftest_ren(a->flags, WHDST)) {
20642+ h_d = au_wh_lkup(a->dst_h_parent, &a->dst_dentry->d_name,
20643+ a->br);
20644+ err = PTR_ERR(h_d);
20645+ if (IS_ERR(h_d))
20646+ goto out_whsrc;
5527c038 20647+ if (d_is_negative(h_d))
4a4d8108
AM
20648+ dput(h_d);
20649+ else
20650+ a->dst_wh_dentry = h_d;
20651+ }
1facf9fc 20652+
4a4d8108
AM
20653+ /* rename dentry to tmpwh */
20654+ if (a->thargs) {
20655+ err = au_whtmp_ren(a->dst_h_dentry, a->br);
20656+ if (unlikely(err))
20657+ goto out_whdst;
dece6358 20658+
4a4d8108
AM
20659+ d = a->dst_dentry;
20660+ au_set_h_dptr(d, a->btgt, NULL);
86dc4139 20661+ err = au_lkup_neg(d, a->btgt, /*wh*/0);
4a4d8108
AM
20662+ if (unlikely(err))
20663+ goto out_whtmp;
20664+ a->dst_h_dentry = au_h_dptr(d, a->btgt);
20665+ }
1facf9fc 20666+
5afbbe0d 20667+ BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
1facf9fc 20668+
4a4d8108
AM
20669+ /* rename by vfs_rename or cpup */
20670+ d = a->dst_dentry;
20671+ if (au_ftest_ren(a->flags, ISDIR)
20672+ && (a->dst_wh_dentry
20673+ || au_dbdiropq(d) == a->btgt
20674+ /* hide the lower to keep xino */
5afbbe0d 20675+ || a->btgt < au_dbbot(d)
4a4d8108
AM
20676+ || au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ)))
20677+ au_fset_ren(a->flags, DIROPQ);
20678+ err = au_ren_or_cpup(a);
20679+ if (unlikely(err))
20680+ /* leave the copied-up one */
20681+ goto out_whtmp;
1308ab2a 20682+
4a4d8108
AM
20683+ /* make dir opaque */
20684+ if (au_ftest_ren(a->flags, DIROPQ)) {
20685+ err = au_ren_diropq(a);
20686+ if (unlikely(err))
20687+ goto out_rename;
20688+ }
1308ab2a 20689+
4a4d8108 20690+ /* update target timestamps */
5afbbe0d 20691+ AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
4a4d8108
AM
20692+ a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
20693+ vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
5527c038 20694+ a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
1facf9fc 20695+
4a4d8108
AM
20696+ /* remove whiteout for dentry */
20697+ if (a->dst_wh_dentry) {
20698+ a->h_path.dentry = a->dst_wh_dentry;
20699+ err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
20700+ a->dst_dentry);
20701+ if (unlikely(err))
20702+ goto out_diropq;
20703+ }
1facf9fc 20704+
4a4d8108
AM
20705+ /* remove whtmp */
20706+ if (a->thargs)
20707+ au_ren_del_whtmp(a); /* ignore this error */
1308ab2a 20708+
076b876e 20709+ au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
4a4d8108
AM
20710+ err = 0;
20711+ goto out_success;
20712+
4f0767ce 20713+out_diropq:
4a4d8108
AM
20714+ if (au_ftest_ren(a->flags, DIROPQ))
20715+ au_ren_rev_diropq(err, a);
4f0767ce 20716+out_rename:
7e9cd9fe 20717+ au_ren_rev_rename(err, a);
027c5e7a 20718+ dput(a->h_dst);
4f0767ce 20719+out_whtmp:
4a4d8108
AM
20720+ if (a->thargs)
20721+ au_ren_rev_whtmp(err, a);
4f0767ce 20722+out_whdst:
4a4d8108
AM
20723+ dput(a->dst_wh_dentry);
20724+ a->dst_wh_dentry = NULL;
4f0767ce 20725+out_whsrc:
4a4d8108
AM
20726+ if (a->src_wh_dentry)
20727+ au_ren_rev_whsrc(err, a);
4f0767ce 20728+out_success:
4a4d8108
AM
20729+ dput(a->src_wh_dentry);
20730+ dput(a->dst_wh_dentry);
4f0767ce 20731+out_thargs:
4a4d8108
AM
20732+ if (a->thargs) {
20733+ dput(a->h_dst);
20734+ au_whtmp_rmdir_free(a->thargs);
20735+ a->thargs = NULL;
20736+ }
4f0767ce 20737+out:
4a4d8108 20738+ return err;
dece6358 20739+}
1facf9fc 20740+
1308ab2a 20741+/* ---------------------------------------------------------------------- */
1facf9fc 20742+
4a4d8108
AM
20743+/*
20744+ * test if @dentry dir can be rename destination or not.
20745+ * success means, it is a logically empty dir.
20746+ */
20747+static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
1308ab2a 20748+{
4a4d8108 20749+ return au_test_empty(dentry, whlist);
1308ab2a 20750+}
1facf9fc 20751+
4a4d8108
AM
20752+/*
20753+ * test if @dentry dir can be rename source or not.
20754+ * if it can, return 0 and @children is filled.
20755+ * success means,
20756+ * - it is a logically empty dir.
20757+ * - or, it exists on writable branch and has no children including whiteouts
20758+ * on the lower branch.
20759+ */
20760+static int may_rename_srcdir(struct dentry *dentry, aufs_bindex_t btgt)
20761+{
20762+ int err;
20763+ unsigned int rdhash;
5afbbe0d 20764+ aufs_bindex_t btop;
1facf9fc 20765+
5afbbe0d
AM
20766+ btop = au_dbtop(dentry);
20767+ if (btop != btgt) {
4a4d8108 20768+ struct au_nhash whlist;
dece6358 20769+
4a4d8108
AM
20770+ SiMustAnyLock(dentry->d_sb);
20771+ rdhash = au_sbi(dentry->d_sb)->si_rdhash;
20772+ if (!rdhash)
20773+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
20774+ dentry));
20775+ err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
20776+ if (unlikely(err))
20777+ goto out;
20778+ err = au_test_empty(dentry, &whlist);
20779+ au_nhash_wh_free(&whlist);
20780+ goto out;
20781+ }
dece6358 20782+
5afbbe0d 20783+ if (btop == au_dbtaildir(dentry))
4a4d8108 20784+ return 0; /* success */
dece6358 20785+
4a4d8108 20786+ err = au_test_empty_lower(dentry);
1facf9fc 20787+
4f0767ce 20788+out:
4a4d8108
AM
20789+ if (err == -ENOTEMPTY) {
20790+ AuWarn1("renaming dir who has child(ren) on multiple branches,"
20791+ " is not supported\n");
20792+ err = -EXDEV;
20793+ }
20794+ return err;
20795+}
1308ab2a 20796+
4a4d8108
AM
20797+/* side effect: sets whlist and h_dentry */
20798+static int au_ren_may_dir(struct au_ren_args *a)
1308ab2a 20799+{
4a4d8108
AM
20800+ int err;
20801+ unsigned int rdhash;
20802+ struct dentry *d;
1facf9fc 20803+
4a4d8108
AM
20804+ d = a->dst_dentry;
20805+ SiMustAnyLock(d->d_sb);
1facf9fc 20806+
4a4d8108
AM
20807+ err = 0;
20808+ if (au_ftest_ren(a->flags, ISDIR) && a->dst_inode) {
20809+ rdhash = au_sbi(d->d_sb)->si_rdhash;
20810+ if (!rdhash)
20811+ rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
20812+ err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
20813+ if (unlikely(err))
20814+ goto out;
1308ab2a 20815+
5afbbe0d 20816+ au_set_dbtop(d, a->dst_btop);
4a4d8108 20817+ err = may_rename_dstdir(d, &a->whlist);
5afbbe0d 20818+ au_set_dbtop(d, a->btgt);
4a4d8108 20819+ }
5afbbe0d 20820+ a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
20821+ if (unlikely(err))
20822+ goto out;
20823+
20824+ d = a->src_dentry;
5afbbe0d 20825+ a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
4a4d8108
AM
20826+ if (au_ftest_ren(a->flags, ISDIR)) {
20827+ err = may_rename_srcdir(d, a->btgt);
20828+ if (unlikely(err)) {
20829+ au_nhash_wh_free(&a->whlist);
20830+ a->whlist.nh_num = 0;
20831+ }
20832+ }
4f0767ce 20833+out:
4a4d8108 20834+ return err;
1facf9fc 20835+}
20836+
4a4d8108 20837+/* ---------------------------------------------------------------------- */
1facf9fc 20838+
4a4d8108
AM
20839+/*
20840+ * simple tests for rename.
20841+ * following the checks in vfs, plus the parent-child relationship.
20842+ */
20843+static int au_may_ren(struct au_ren_args *a)
20844+{
20845+ int err, isdir;
20846+ struct inode *h_inode;
1facf9fc 20847+
5afbbe0d 20848+ if (a->src_btop == a->btgt) {
4a4d8108
AM
20849+ err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
20850+ au_ftest_ren(a->flags, ISDIR));
20851+ if (unlikely(err))
20852+ goto out;
20853+ err = -EINVAL;
20854+ if (unlikely(a->src_h_dentry == a->h_trap))
20855+ goto out;
20856+ }
1facf9fc 20857+
4a4d8108 20858+ err = 0;
5afbbe0d 20859+ if (a->dst_btop != a->btgt)
4a4d8108 20860+ goto out;
1facf9fc 20861+
027c5e7a
AM
20862+ err = -ENOTEMPTY;
20863+ if (unlikely(a->dst_h_dentry == a->h_trap))
20864+ goto out;
20865+
4a4d8108 20866+ err = -EIO;
4a4d8108 20867+ isdir = !!au_ftest_ren(a->flags, ISDIR);
5527c038
JR
20868+ if (d_really_is_negative(a->dst_dentry)) {
20869+ if (d_is_negative(a->dst_h_dentry))
20870+ err = au_may_add(a->dst_dentry, a->btgt,
20871+ a->dst_h_parent, isdir);
4a4d8108 20872+ } else {
5527c038 20873+ if (unlikely(d_is_negative(a->dst_h_dentry)))
4a4d8108 20874+ goto out;
5527c038
JR
20875+ h_inode = d_inode(a->dst_h_dentry);
20876+ if (h_inode->i_nlink)
20877+ err = au_may_del(a->dst_dentry, a->btgt,
20878+ a->dst_h_parent, isdir);
4a4d8108 20879+ }
1facf9fc 20880+
4f0767ce 20881+out:
4a4d8108
AM
20882+ if (unlikely(err == -ENOENT || err == -EEXIST))
20883+ err = -EIO;
20884+ AuTraceErr(err);
20885+ return err;
20886+}
1facf9fc 20887+
1308ab2a 20888+/* ---------------------------------------------------------------------- */
1facf9fc 20889+
4a4d8108
AM
20890+/*
20891+ * locking order
20892+ * (VFS)
20893+ * - src_dir and dir by lock_rename()
20894+ * - inode if exitsts
20895+ * (aufs)
20896+ * - lock all
20897+ * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
20898+ * + si_read_lock
20899+ * + di_write_lock2_child()
20900+ * + di_write_lock_child()
20901+ * + ii_write_lock_child()
20902+ * + di_write_lock_child2()
20903+ * + ii_write_lock_child2()
20904+ * + src_parent and parent
20905+ * + di_write_lock_parent()
20906+ * + ii_write_lock_parent()
20907+ * + di_write_lock_parent2()
20908+ * + ii_write_lock_parent2()
20909+ * + lower src_dir and dir by vfsub_lock_rename()
20910+ * + verify the every relationships between child and parent. if any
20911+ * of them failed, unlock all and return -EBUSY.
20912+ */
20913+static void au_ren_unlock(struct au_ren_args *a)
1308ab2a 20914+{
4a4d8108
AM
20915+ vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
20916+ a->dst_h_parent, a->dst_hdir);
86dc4139
AM
20917+ if (au_ftest_ren(a->flags, MNT_WRITE))
20918+ vfsub_mnt_drop_write(au_br_mnt(a->br));
1308ab2a 20919+}
20920+
4a4d8108 20921+static int au_ren_lock(struct au_ren_args *a)
1308ab2a 20922+{
4a4d8108
AM
20923+ int err;
20924+ unsigned int udba;
1308ab2a 20925+
4a4d8108
AM
20926+ err = 0;
20927+ a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
20928+ a->src_hdir = au_hi(a->src_dir, a->btgt);
20929+ a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
20930+ a->dst_hdir = au_hi(a->dst_dir, a->btgt);
86dc4139
AM
20931+
20932+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
20933+ if (unlikely(err))
20934+ goto out;
20935+ au_fset_ren(a->flags, MNT_WRITE);
4a4d8108
AM
20936+ a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
20937+ a->dst_h_parent, a->dst_hdir);
20938+ udba = au_opt_udba(a->src_dentry->d_sb);
5527c038
JR
20939+ if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
20940+ || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
4a4d8108 20941+ err = au_busy_or_stale();
5afbbe0d 20942+ if (!err && au_dbtop(a->src_dentry) == a->btgt)
4a4d8108 20943+ err = au_h_verify(a->src_h_dentry, udba,
5527c038 20944+ d_inode(a->src_h_parent), a->src_h_parent,
4a4d8108 20945+ a->br);
5afbbe0d 20946+ if (!err && au_dbtop(a->dst_dentry) == a->btgt)
4a4d8108 20947+ err = au_h_verify(a->dst_h_dentry, udba,
5527c038 20948+ d_inode(a->dst_h_parent), a->dst_h_parent,
4a4d8108 20949+ a->br);
86dc4139 20950+ if (!err)
4a4d8108 20951+ goto out; /* success */
4a4d8108
AM
20952+
20953+ err = au_busy_or_stale();
4a4d8108 20954+ au_ren_unlock(a);
86dc4139 20955+
4f0767ce 20956+out:
4a4d8108 20957+ return err;
1facf9fc 20958+}
20959+
20960+/* ---------------------------------------------------------------------- */
20961+
4a4d8108 20962+static void au_ren_refresh_dir(struct au_ren_args *a)
1facf9fc 20963+{
4a4d8108 20964+ struct inode *dir;
dece6358 20965+
4a4d8108
AM
20966+ dir = a->dst_dir;
20967+ dir->i_version++;
20968+ if (au_ftest_ren(a->flags, ISDIR)) {
20969+ /* is this updating defined in POSIX? */
20970+ au_cpup_attr_timesizes(a->src_inode);
20971+ au_cpup_attr_nlink(dir, /*force*/1);
4a4d8108 20972+ }
027c5e7a 20973+
b912730e 20974+ au_dir_ts(dir, a->btgt);
dece6358 20975+
4a4d8108
AM
20976+ if (au_ftest_ren(a->flags, ISSAMEDIR))
20977+ return;
dece6358 20978+
4a4d8108
AM
20979+ dir = a->src_dir;
20980+ dir->i_version++;
20981+ if (au_ftest_ren(a->flags, ISDIR))
20982+ au_cpup_attr_nlink(dir, /*force*/1);
b912730e 20983+ au_dir_ts(dir, a->btgt);
1facf9fc 20984+}
20985+
4a4d8108 20986+static void au_ren_refresh(struct au_ren_args *a)
1facf9fc 20987+{
5afbbe0d 20988+ aufs_bindex_t bbot, bindex;
4a4d8108
AM
20989+ struct dentry *d, *h_d;
20990+ struct inode *i, *h_i;
20991+ struct super_block *sb;
dece6358 20992+
027c5e7a
AM
20993+ d = a->dst_dentry;
20994+ d_drop(d);
20995+ if (a->h_dst)
20996+ /* already dget-ed by au_ren_or_cpup() */
20997+ au_set_h_dptr(d, a->btgt, a->h_dst);
20998+
20999+ i = a->dst_inode;
21000+ if (i) {
21001+ if (!au_ftest_ren(a->flags, ISDIR))
21002+ vfsub_drop_nlink(i);
21003+ else {
21004+ vfsub_dead_dir(i);
21005+ au_cpup_attr_timesizes(i);
21006+ }
21007+ au_update_dbrange(d, /*do_put_zero*/1);
21008+ } else {
5afbbe0d
AM
21009+ bbot = a->btgt;
21010+ for (bindex = au_dbtop(d); bindex < bbot; bindex++)
027c5e7a 21011+ au_set_h_dptr(d, bindex, NULL);
5afbbe0d
AM
21012+ bbot = au_dbbot(d);
21013+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
027c5e7a
AM
21014+ au_set_h_dptr(d, bindex, NULL);
21015+ au_update_dbrange(d, /*do_put_zero*/0);
21016+ }
21017+
4a4d8108
AM
21018+ d = a->src_dentry;
21019+ au_set_dbwh(d, -1);
5afbbe0d
AM
21020+ bbot = au_dbbot(d);
21021+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
4a4d8108
AM
21022+ h_d = au_h_dptr(d, bindex);
21023+ if (h_d)
21024+ au_set_h_dptr(d, bindex, NULL);
21025+ }
5afbbe0d 21026+ au_set_dbbot(d, a->btgt);
4a4d8108
AM
21027+
21028+ sb = d->d_sb;
21029+ i = a->src_inode;
21030+ if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
21031+ return; /* success */
21032+
5afbbe0d
AM
21033+ bbot = au_ibbot(i);
21034+ for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
4a4d8108
AM
21035+ h_i = au_h_iptr(i, bindex);
21036+ if (h_i) {
21037+ au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
21038+ /* ignore this error */
21039+ au_set_h_iptr(i, bindex, NULL, 0);
21040+ }
21041+ }
5afbbe0d 21042+ au_set_ibbot(i, a->btgt);
1308ab2a 21043+}
dece6358 21044+
4a4d8108
AM
21045+/* ---------------------------------------------------------------------- */
21046+
21047+/* mainly for link(2) and rename(2) */
21048+int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
1308ab2a 21049+{
4a4d8108
AM
21050+ aufs_bindex_t bdiropq, bwh;
21051+ struct dentry *parent;
21052+ struct au_branch *br;
21053+
21054+ parent = dentry->d_parent;
5527c038 21055+ IMustLock(d_inode(parent)); /* dir is locked */
4a4d8108
AM
21056+
21057+ bdiropq = au_dbdiropq(parent);
21058+ bwh = au_dbwh(dentry);
21059+ br = au_sbr(dentry->d_sb, btgt);
21060+ if (au_br_rdonly(br)
21061+ || (0 <= bdiropq && bdiropq < btgt)
21062+ || (0 <= bwh && bwh < btgt))
21063+ btgt = -1;
21064+
21065+ AuDbg("btgt %d\n", btgt);
21066+ return btgt;
1facf9fc 21067+}
21068+
5afbbe0d 21069+/* sets src_btop, dst_btop and btgt */
4a4d8108 21070+static int au_ren_wbr(struct au_ren_args *a)
1facf9fc 21071+{
4a4d8108
AM
21072+ int err;
21073+ struct au_wr_dir_args wr_dir_args = {
21074+ /* .force_btgt = -1, */
21075+ .flags = AuWrDir_ADD_ENTRY
21076+ };
dece6358 21077+
5afbbe0d
AM
21078+ a->src_btop = au_dbtop(a->src_dentry);
21079+ a->dst_btop = au_dbtop(a->dst_dentry);
4a4d8108
AM
21080+ if (au_ftest_ren(a->flags, ISDIR))
21081+ au_fset_wrdir(wr_dir_args.flags, ISDIR);
5afbbe0d
AM
21082+ wr_dir_args.force_btgt = a->src_btop;
21083+ if (a->dst_inode && a->dst_btop < a->src_btop)
21084+ wr_dir_args.force_btgt = a->dst_btop;
4a4d8108
AM
21085+ wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
21086+ err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
21087+ a->btgt = err;
dece6358 21088+
4a4d8108 21089+ return err;
1facf9fc 21090+}
21091+
4a4d8108 21092+static void au_ren_dt(struct au_ren_args *a)
1facf9fc 21093+{
4a4d8108
AM
21094+ a->h_path.dentry = a->src_h_parent;
21095+ au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
21096+ if (!au_ftest_ren(a->flags, ISSAMEDIR)) {
21097+ a->h_path.dentry = a->dst_h_parent;
21098+ au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
21099+ }
1facf9fc 21100+
4a4d8108
AM
21101+ au_fclr_ren(a->flags, DT_DSTDIR);
21102+ if (!au_ftest_ren(a->flags, ISDIR))
21103+ return;
dece6358 21104+
4a4d8108
AM
21105+ a->h_path.dentry = a->src_h_dentry;
21106+ au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
5527c038 21107+ if (d_is_positive(a->dst_h_dentry)) {
4a4d8108
AM
21108+ au_fset_ren(a->flags, DT_DSTDIR);
21109+ a->h_path.dentry = a->dst_h_dentry;
21110+ au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
21111+ }
1308ab2a 21112+}
dece6358 21113+
4a4d8108 21114+static void au_ren_rev_dt(int err, struct au_ren_args *a)
1308ab2a 21115+{
4a4d8108 21116+ struct dentry *h_d;
febd17d6 21117+ struct inode *h_inode;
4a4d8108
AM
21118+
21119+ au_dtime_revert(a->src_dt + AuPARENT);
21120+ if (!au_ftest_ren(a->flags, ISSAMEDIR))
21121+ au_dtime_revert(a->dst_dt + AuPARENT);
21122+
21123+ if (au_ftest_ren(a->flags, ISDIR) && err != -EIO) {
21124+ h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
21125+ h_inode = d_inode(h_d);
21126+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21127+ au_dtime_revert(a->src_dt + AuCHILD);
febd17d6 21128+ inode_unlock(h_inode);
4a4d8108
AM
21129+
21130+ if (au_ftest_ren(a->flags, DT_DSTDIR)) {
21131+ h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
febd17d6
JR
21132+ h_inode = d_inode(h_d);
21133+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
4a4d8108 21134+ au_dtime_revert(a->dst_dt + AuCHILD);
febd17d6 21135+ inode_unlock(h_inode);
1facf9fc 21136+ }
21137+ }
21138+}
21139+
4a4d8108
AM
21140+/* ---------------------------------------------------------------------- */
21141+
21142+int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
21143+ struct inode *_dst_dir, struct dentry *_dst_dentry)
1facf9fc 21144+{
e49829fe 21145+ int err, flags;
4a4d8108
AM
21146+ /* reduce stack space */
21147+ struct au_ren_args *a;
21148+
523b37e3 21149+ AuDbg("%pd, %pd\n", _src_dentry, _dst_dentry);
4a4d8108
AM
21150+ IMustLock(_src_dir);
21151+ IMustLock(_dst_dir);
21152+
21153+ err = -ENOMEM;
21154+ BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
21155+ a = kzalloc(sizeof(*a), GFP_NOFS);
21156+ if (unlikely(!a))
21157+ goto out;
21158+
21159+ a->src_dir = _src_dir;
21160+ a->src_dentry = _src_dentry;
5527c038
JR
21161+ a->src_inode = NULL;
21162+ if (d_really_is_positive(a->src_dentry))
21163+ a->src_inode = d_inode(a->src_dentry);
4a4d8108
AM
21164+ a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
21165+ a->dst_dir = _dst_dir;
21166+ a->dst_dentry = _dst_dentry;
5527c038
JR
21167+ a->dst_inode = NULL;
21168+ if (d_really_is_positive(a->dst_dentry))
21169+ a->dst_inode = d_inode(a->dst_dentry);
4a4d8108
AM
21170+ a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
21171+ if (a->dst_inode) {
21172+ IMustLock(a->dst_inode);
21173+ au_igrab(a->dst_inode);
1facf9fc 21174+ }
1facf9fc 21175+
4a4d8108 21176+ err = -ENOTDIR;
027c5e7a 21177+ flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
2000de60 21178+ if (d_is_dir(a->src_dentry)) {
4a4d8108 21179+ au_fset_ren(a->flags, ISDIR);
5527c038 21180+ if (unlikely(d_really_is_positive(a->dst_dentry)
2000de60 21181+ && !d_is_dir(a->dst_dentry)))
4a4d8108 21182+ goto out_free;
b95c5147
AM
21183+ flags |= AuLock_DIRS;
21184+ }
21185+ err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry, flags);
e49829fe
JR
21186+ if (unlikely(err))
21187+ goto out_free;
1facf9fc 21188+
027c5e7a
AM
21189+ err = au_d_hashed_positive(a->src_dentry);
21190+ if (unlikely(err))
21191+ goto out_unlock;
21192+ err = -ENOENT;
21193+ if (a->dst_inode) {
21194+ /*
21195+ * If it is a dir, VFS unhash dst_dentry before this
21196+ * function. It means we cannot rely upon d_unhashed().
21197+ */
21198+ if (unlikely(!a->dst_inode->i_nlink))
21199+ goto out_unlock;
21200+ if (!S_ISDIR(a->dst_inode->i_mode)) {
21201+ err = au_d_hashed_positive(a->dst_dentry);
21202+ if (unlikely(err))
21203+ goto out_unlock;
21204+ } else if (unlikely(IS_DEADDIR(a->dst_inode)))
21205+ goto out_unlock;
21206+ } else if (unlikely(d_unhashed(a->dst_dentry)))
21207+ goto out_unlock;
21208+
7eafdf33
AM
21209+ /*
21210+ * is it possible?
79b8bda9 21211+ * yes, it happened (in linux-3.3-rcN) but I don't know why.
7eafdf33
AM
21212+ * there may exist a problem somewhere else.
21213+ */
21214+ err = -EINVAL;
5527c038 21215+ if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
7eafdf33
AM
21216+ goto out_unlock;
21217+
4a4d8108
AM
21218+ au_fset_ren(a->flags, ISSAMEDIR); /* temporary */
21219+ di_write_lock_parent(a->dst_parent);
1facf9fc 21220+
4a4d8108
AM
21221+ /* which branch we process */
21222+ err = au_ren_wbr(a);
21223+ if (unlikely(err < 0))
027c5e7a 21224+ goto out_parent;
4a4d8108 21225+ a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
86dc4139 21226+ a->h_path.mnt = au_br_mnt(a->br);
1facf9fc 21227+
4a4d8108
AM
21228+ /* are they available to be renamed */
21229+ err = au_ren_may_dir(a);
21230+ if (unlikely(err))
21231+ goto out_children;
1facf9fc 21232+
4a4d8108 21233+ /* prepare the writable parent dir on the same branch */
5afbbe0d 21234+ if (a->dst_btop == a->btgt) {
4a4d8108
AM
21235+ au_fset_ren(a->flags, WHDST);
21236+ } else {
21237+ err = au_cpup_dirs(a->dst_dentry, a->btgt);
21238+ if (unlikely(err))
21239+ goto out_children;
21240+ }
1facf9fc 21241+
4a4d8108
AM
21242+ if (a->src_dir != a->dst_dir) {
21243+ /*
21244+ * this temporary unlock is safe,
21245+ * because both dir->i_mutex are locked.
21246+ */
21247+ di_write_unlock(a->dst_parent);
21248+ di_write_lock_parent(a->src_parent);
21249+ err = au_wr_dir_need_wh(a->src_dentry,
21250+ au_ftest_ren(a->flags, ISDIR),
21251+ &a->btgt);
21252+ di_write_unlock(a->src_parent);
21253+ di_write_lock2_parent(a->src_parent, a->dst_parent, /*isdir*/1);
21254+ au_fclr_ren(a->flags, ISSAMEDIR);
21255+ } else
21256+ err = au_wr_dir_need_wh(a->src_dentry,
21257+ au_ftest_ren(a->flags, ISDIR),
21258+ &a->btgt);
21259+ if (unlikely(err < 0))
21260+ goto out_children;
21261+ if (err)
21262+ au_fset_ren(a->flags, WHSRC);
1facf9fc 21263+
86dc4139 21264+ /* cpup src */
5afbbe0d 21265+ if (a->src_btop != a->btgt) {
86dc4139
AM
21266+ struct au_pin pin;
21267+
21268+ err = au_pin(&pin, a->src_dentry, a->btgt,
21269+ au_opt_udba(a->src_dentry->d_sb),
21270+ AuPin_DI_LOCKED | AuPin_MNT_WRITE);
367653fa 21271+ if (!err) {
c2b27bf2
AM
21272+ struct au_cp_generic cpg = {
21273+ .dentry = a->src_dentry,
21274+ .bdst = a->btgt,
5afbbe0d 21275+ .bsrc = a->src_btop,
c2b27bf2
AM
21276+ .len = -1,
21277+ .pin = &pin,
21278+ .flags = AuCpup_DTIME | AuCpup_HOPEN
21279+ };
5afbbe0d 21280+ AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
c2b27bf2 21281+ err = au_sio_cpup_simple(&cpg);
367653fa 21282+ au_unpin(&pin);
86dc4139 21283+ }
86dc4139
AM
21284+ if (unlikely(err))
21285+ goto out_children;
5afbbe0d 21286+ a->src_btop = a->btgt;
86dc4139
AM
21287+ a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
21288+ au_fset_ren(a->flags, WHSRC);
21289+ }
21290+
4a4d8108
AM
21291+ /* lock them all */
21292+ err = au_ren_lock(a);
21293+ if (unlikely(err))
86dc4139 21294+ /* leave the copied-up one */
4a4d8108 21295+ goto out_children;
1facf9fc 21296+
4a4d8108
AM
21297+ if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
21298+ err = au_may_ren(a);
21299+ else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
21300+ err = -ENAMETOOLONG;
21301+ if (unlikely(err))
21302+ goto out_hdir;
1facf9fc 21303+
4a4d8108
AM
21304+ /* store timestamps to be revertible */
21305+ au_ren_dt(a);
1facf9fc 21306+
4a4d8108
AM
21307+ /* here we go */
21308+ err = do_rename(a);
21309+ if (unlikely(err))
21310+ goto out_dt;
21311+
21312+ /* update dir attributes */
21313+ au_ren_refresh_dir(a);
21314+
21315+ /* dput/iput all lower dentries */
21316+ au_ren_refresh(a);
21317+
21318+ goto out_hdir; /* success */
21319+
4f0767ce 21320+out_dt:
4a4d8108 21321+ au_ren_rev_dt(err, a);
4f0767ce 21322+out_hdir:
4a4d8108 21323+ au_ren_unlock(a);
4f0767ce 21324+out_children:
4a4d8108 21325+ au_nhash_wh_free(&a->whlist);
5afbbe0d
AM
21326+ if (err && a->dst_inode && a->dst_btop != a->btgt) {
21327+ AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
027c5e7a 21328+ au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
5afbbe0d 21329+ au_set_dbtop(a->dst_dentry, a->dst_btop);
4a4d8108 21330+ }
027c5e7a 21331+out_parent:
4a4d8108
AM
21332+ if (!err)
21333+ d_move(a->src_dentry, a->dst_dentry);
027c5e7a 21334+ else {
5afbbe0d 21335+ au_update_dbtop(a->dst_dentry);
027c5e7a
AM
21336+ if (!a->dst_inode)
21337+ d_drop(a->dst_dentry);
21338+ }
4a4d8108
AM
21339+ if (au_ftest_ren(a->flags, ISSAMEDIR))
21340+ di_write_unlock(a->dst_parent);
21341+ else
21342+ di_write_unlock2(a->src_parent, a->dst_parent);
027c5e7a 21343+out_unlock:
4a4d8108 21344+ aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
4f0767ce 21345+out_free:
4a4d8108
AM
21346+ iput(a->dst_inode);
21347+ if (a->thargs)
21348+ au_whtmp_rmdir_free(a->thargs);
21349+ kfree(a);
4f0767ce 21350+out:
4a4d8108
AM
21351+ AuTraceErr(err);
21352+ return err;
1308ab2a 21353+}
7f207e10
AM
21354diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
21355--- /usr/share/empty/fs/aufs/Kconfig 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21356+++ linux/fs/aufs/Kconfig 2015-09-24 10:47:58.248052907 +0200
c1595e42 21357@@ -0,0 +1,185 @@
4a4d8108
AM
21358+config AUFS_FS
21359+ tristate "Aufs (Advanced multi layered unification filesystem) support"
4a4d8108
AM
21360+ help
21361+ Aufs is a stackable unification filesystem such as Unionfs,
21362+ which unifies several directories and provides a merged single
21363+ directory.
21364+ In the early days, aufs was entirely re-designed and
21365+ re-implemented Unionfs Version 1.x series. Introducing many
21366+ original ideas, approaches and improvements, it becomes totally
21367+ different from Unionfs while keeping the basic features.
1facf9fc 21368+
4a4d8108
AM
21369+if AUFS_FS
21370+choice
21371+ prompt "Maximum number of branches"
21372+ default AUFS_BRANCH_MAX_127
21373+ help
21374+ Specifies the maximum number of branches (or member directories)
21375+ in a single aufs. The larger value consumes more system
21376+ resources and has a minor impact to performance.
21377+config AUFS_BRANCH_MAX_127
21378+ bool "127"
21379+ help
21380+ Specifies the maximum number of branches (or member directories)
21381+ in a single aufs. The larger value consumes more system
21382+ resources and has a minor impact to performance.
21383+config AUFS_BRANCH_MAX_511
21384+ bool "511"
21385+ help
21386+ Specifies the maximum number of branches (or member directories)
21387+ in a single aufs. The larger value consumes more system
21388+ resources and has a minor impact to performance.
21389+config AUFS_BRANCH_MAX_1023
21390+ bool "1023"
21391+ help
21392+ Specifies the maximum number of branches (or member directories)
21393+ in a single aufs. The larger value consumes more system
21394+ resources and has a minor impact to performance.
21395+config AUFS_BRANCH_MAX_32767
21396+ bool "32767"
21397+ help
21398+ Specifies the maximum number of branches (or member directories)
21399+ in a single aufs. The larger value consumes more system
21400+ resources and has a minor impact to performance.
21401+endchoice
1facf9fc 21402+
e49829fe
JR
21403+config AUFS_SBILIST
21404+ bool
21405+ depends on AUFS_MAGIC_SYSRQ || PROC_FS
21406+ default y
21407+ help
21408+ Automatic configuration for internal use.
21409+ When aufs supports Magic SysRq or /proc, enabled automatically.
21410+
4a4d8108
AM
21411+config AUFS_HNOTIFY
21412+ bool "Detect direct branch access (bypassing aufs)"
21413+ help
21414+ If you want to modify files on branches directly, eg. bypassing aufs,
21415+ and want aufs to detect the changes of them fully, then enable this
21416+ option and use 'udba=notify' mount option.
7f207e10 21417+ Currently there is only one available configuration, "fsnotify".
4a4d8108
AM
21418+ It will have a negative impact to the performance.
21419+ See detail in aufs.5.
dece6358 21420+
4a4d8108
AM
21421+choice
21422+ prompt "method" if AUFS_HNOTIFY
21423+ default AUFS_HFSNOTIFY
21424+config AUFS_HFSNOTIFY
21425+ bool "fsnotify"
21426+ select FSNOTIFY
4a4d8108 21427+endchoice
1facf9fc 21428+
4a4d8108
AM
21429+config AUFS_EXPORT
21430+ bool "NFS-exportable aufs"
2cbb1c4b 21431+ depends on EXPORTFS
4a4d8108
AM
21432+ help
21433+ If you want to export your mounted aufs via NFS, then enable this
21434+ option. There are several requirements for this configuration.
21435+ See detail in aufs.5.
1facf9fc 21436+
4a4d8108
AM
21437+config AUFS_INO_T_64
21438+ bool
21439+ depends on AUFS_EXPORT
21440+ depends on 64BIT && !(ALPHA || S390)
21441+ default y
21442+ help
21443+ Automatic configuration for internal use.
21444+ /* typedef unsigned long/int __kernel_ino_t */
21445+ /* alpha and s390x are int */
1facf9fc 21446+
c1595e42
JR
21447+config AUFS_XATTR
21448+ bool "support for XATTR/EA (including Security Labels)"
21449+ help
21450+ If your branch fs supports XATTR/EA and you want to make them
21451+ available in aufs too, then enable this opsion and specify the
21452+ branch attributes for EA.
21453+ See detail in aufs.5.
21454+
076b876e
AM
21455+config AUFS_FHSM
21456+ bool "File-based Hierarchical Storage Management"
21457+ help
21458+ Hierarchical Storage Management (or HSM) is a well-known feature
21459+ in the storage world. Aufs provides this feature as file-based.
21460+ with multiple branches.
21461+ These multiple branches are prioritized, ie. the topmost one
21462+ should be the fastest drive and be used heavily.
21463+
4a4d8108
AM
21464+config AUFS_RDU
21465+ bool "Readdir in userspace"
21466+ help
21467+ Aufs has two methods to provide a merged view for a directory,
21468+ by a user-space library and by kernel-space natively. The latter
21469+ is always enabled but sometimes large and slow.
21470+ If you enable this option, install the library in aufs2-util
21471+ package, and set some environment variables for your readdir(3),
21472+ then the work will be handled in user-space which generally
21473+ shows better performance in most cases.
21474+ See detail in aufs.5.
1facf9fc 21475+
4a4d8108
AM
21476+config AUFS_SHWH
21477+ bool "Show whiteouts"
21478+ help
21479+ If you want to make the whiteouts in aufs visible, then enable
21480+ this option and specify 'shwh' mount option. Although it may
21481+ sounds like philosophy or something, but in technically it
21482+ simply shows the name of whiteout with keeping its behaviour.
1facf9fc 21483+
4a4d8108
AM
21484+config AUFS_BR_RAMFS
21485+ bool "Ramfs (initramfs/rootfs) as an aufs branch"
21486+ help
21487+ If you want to use ramfs as an aufs branch fs, then enable this
21488+ option. Generally tmpfs is recommended.
21489+ Aufs prohibited them to be a branch fs by default, because
21490+ initramfs becomes unusable after switch_root or something
21491+ generally. If you sets initramfs as an aufs branch and boot your
21492+ system by switch_root, you will meet a problem easily since the
21493+ files in initramfs may be inaccessible.
21494+ Unless you are going to use ramfs as an aufs branch fs without
21495+ switch_root or something, leave it N.
1facf9fc 21496+
4a4d8108
AM
21497+config AUFS_BR_FUSE
21498+ bool "Fuse fs as an aufs branch"
21499+ depends on FUSE_FS
21500+ select AUFS_POLL
21501+ help
21502+ If you want to use fuse-based userspace filesystem as an aufs
21503+ branch fs, then enable this option.
21504+ It implements the internal poll(2) operation which is
21505+ implemented by fuse only (curretnly).
1facf9fc 21506+
4a4d8108
AM
21507+config AUFS_POLL
21508+ bool
21509+ help
21510+ Automatic configuration for internal use.
1facf9fc 21511+
4a4d8108
AM
21512+config AUFS_BR_HFSPLUS
21513+ bool "Hfsplus as an aufs branch"
21514+ depends on HFSPLUS_FS
21515+ default y
21516+ help
21517+ If you want to use hfsplus fs as an aufs branch fs, then enable
21518+ this option. This option introduces a small overhead at
21519+ copying-up a file on hfsplus.
1facf9fc 21520+
4a4d8108
AM
21521+config AUFS_BDEV_LOOP
21522+ bool
21523+ depends on BLK_DEV_LOOP
21524+ default y
21525+ help
21526+ Automatic configuration for internal use.
21527+ Convert =[ym] into =y.
1308ab2a 21528+
4a4d8108
AM
21529+config AUFS_DEBUG
21530+ bool "Debug aufs"
21531+ help
21532+ Enable this to compile aufs internal debug code.
21533+ It will have a negative impact to the performance.
21534+
21535+config AUFS_MAGIC_SYSRQ
21536+ bool
21537+ depends on AUFS_DEBUG && MAGIC_SYSRQ
21538+ default y
21539+ help
21540+ Automatic configuration for internal use.
21541+ When aufs supports Magic SysRq, enabled automatically.
21542+endif
7f207e10
AM
21543diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
21544--- /usr/share/empty/fs/aufs/loop.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21545+++ linux/fs/aufs/loop.c 2016-07-25 19:05:34.814493242 +0200
79b8bda9 21546@@ -0,0 +1,146 @@
1facf9fc 21547+/*
8cdd5066 21548+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 21549+ *
21550+ * This program, aufs is free software; you can redistribute it and/or modify
21551+ * it under the terms of the GNU General Public License as published by
21552+ * the Free Software Foundation; either version 2 of the License, or
21553+ * (at your option) any later version.
dece6358
AM
21554+ *
21555+ * This program is distributed in the hope that it will be useful,
21556+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21557+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21558+ * GNU General Public License for more details.
21559+ *
21560+ * You should have received a copy of the GNU General Public License
523b37e3 21561+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21562+ */
21563+
21564+/*
21565+ * support for loopback block device as a branch
21566+ */
21567+
1facf9fc 21568+#include "aufs.h"
21569+
392086de
AM
21570+/* added into drivers/block/loop.c */
21571+static struct file *(*backing_file_func)(struct super_block *sb);
21572+
1facf9fc 21573+/*
21574+ * test if two lower dentries have overlapping branches.
21575+ */
b752ccd1 21576+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
1facf9fc 21577+{
b752ccd1 21578+ struct super_block *h_sb;
392086de
AM
21579+ struct file *backing_file;
21580+
21581+ if (unlikely(!backing_file_func)) {
21582+ /* don't load "loop" module here */
21583+ backing_file_func = symbol_get(loop_backing_file);
21584+ if (unlikely(!backing_file_func))
21585+ /* "loop" module is not loaded */
21586+ return 0;
21587+ }
1facf9fc 21588+
b752ccd1 21589+ h_sb = h_adding->d_sb;
392086de
AM
21590+ backing_file = backing_file_func(h_sb);
21591+ if (!backing_file)
1facf9fc 21592+ return 0;
21593+
2000de60 21594+ h_adding = backing_file->f_path.dentry;
b752ccd1
AM
21595+ /*
21596+ * h_adding can be local NFS.
21597+ * in this case aufs cannot detect the loop.
21598+ */
21599+ if (unlikely(h_adding->d_sb == sb))
1facf9fc 21600+ return 1;
b752ccd1 21601+ return !!au_test_subdir(h_adding, sb->s_root);
1facf9fc 21602+}
21603+
21604+/* true if a kernel thread named 'loop[0-9].*' accesses a file */
21605+int au_test_loopback_kthread(void)
21606+{
b752ccd1
AM
21607+ int ret;
21608+ struct task_struct *tsk = current;
a2a7ad62 21609+ char c, comm[sizeof(tsk->comm)];
b752ccd1
AM
21610+
21611+ ret = 0;
21612+ if (tsk->flags & PF_KTHREAD) {
a2a7ad62
AM
21613+ get_task_comm(comm, tsk);
21614+ c = comm[4];
b752ccd1 21615+ ret = ('0' <= c && c <= '9'
a2a7ad62 21616+ && !strncmp(comm, "loop", 4));
b752ccd1 21617+ }
1facf9fc 21618+
b752ccd1 21619+ return ret;
1facf9fc 21620+}
87a755f4
AM
21621+
21622+/* ---------------------------------------------------------------------- */
21623+
21624+#define au_warn_loopback_step 16
21625+static int au_warn_loopback_nelem = au_warn_loopback_step;
21626+static unsigned long *au_warn_loopback_array;
21627+
21628+void au_warn_loopback(struct super_block *h_sb)
21629+{
21630+ int i, new_nelem;
21631+ unsigned long *a, magic;
21632+ static DEFINE_SPINLOCK(spin);
21633+
21634+ magic = h_sb->s_magic;
21635+ spin_lock(&spin);
21636+ a = au_warn_loopback_array;
21637+ for (i = 0; i < au_warn_loopback_nelem && *a; i++)
21638+ if (a[i] == magic) {
21639+ spin_unlock(&spin);
21640+ return;
21641+ }
21642+
21643+ /* h_sb is new to us, print it */
21644+ if (i < au_warn_loopback_nelem) {
21645+ a[i] = magic;
21646+ goto pr;
21647+ }
21648+
21649+ /* expand the array */
21650+ new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
21651+ a = au_kzrealloc(au_warn_loopback_array,
21652+ au_warn_loopback_nelem * sizeof(unsigned long),
21653+ new_nelem * sizeof(unsigned long), GFP_ATOMIC);
21654+ if (a) {
21655+ au_warn_loopback_nelem = new_nelem;
21656+ au_warn_loopback_array = a;
21657+ a[i] = magic;
21658+ goto pr;
21659+ }
21660+
21661+ spin_unlock(&spin);
21662+ AuWarn1("realloc failed, ignored\n");
21663+ return;
21664+
21665+pr:
21666+ spin_unlock(&spin);
0c3ec466
AM
21667+ pr_warn("you may want to try another patch for loopback file "
21668+ "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
87a755f4
AM
21669+}
21670+
21671+int au_loopback_init(void)
21672+{
21673+ int err;
21674+ struct super_block *sb __maybe_unused;
21675+
79b8bda9 21676+ BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
87a755f4
AM
21677+
21678+ err = 0;
21679+ au_warn_loopback_array = kcalloc(au_warn_loopback_step,
21680+ sizeof(unsigned long), GFP_NOFS);
21681+ if (unlikely(!au_warn_loopback_array))
21682+ err = -ENOMEM;
21683+
21684+ return err;
21685+}
21686+
21687+void au_loopback_fin(void)
21688+{
79b8bda9
AM
21689+ if (backing_file_func)
21690+ symbol_put(loop_backing_file);
87a755f4
AM
21691+ kfree(au_warn_loopback_array);
21692+}
7f207e10
AM
21693diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
21694--- /usr/share/empty/fs/aufs/loop.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21695+++ linux/fs/aufs/loop.h 2016-07-25 19:05:34.814493242 +0200
523b37e3 21696@@ -0,0 +1,52 @@
1facf9fc 21697+/*
8cdd5066 21698+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 21699+ *
21700+ * This program, aufs is free software; you can redistribute it and/or modify
21701+ * it under the terms of the GNU General Public License as published by
21702+ * the Free Software Foundation; either version 2 of the License, or
21703+ * (at your option) any later version.
dece6358
AM
21704+ *
21705+ * This program is distributed in the hope that it will be useful,
21706+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21707+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21708+ * GNU General Public License for more details.
21709+ *
21710+ * You should have received a copy of the GNU General Public License
523b37e3 21711+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21712+ */
21713+
21714+/*
21715+ * support for loopback mount as a branch
21716+ */
21717+
21718+#ifndef __AUFS_LOOP_H__
21719+#define __AUFS_LOOP_H__
21720+
21721+#ifdef __KERNEL__
21722+
dece6358
AM
21723+struct dentry;
21724+struct super_block;
1facf9fc 21725+
21726+#ifdef CONFIG_AUFS_BDEV_LOOP
392086de
AM
21727+/* drivers/block/loop.c */
21728+struct file *loop_backing_file(struct super_block *sb);
21729+
1facf9fc 21730+/* loop.c */
b752ccd1 21731+int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
1facf9fc 21732+int au_test_loopback_kthread(void);
87a755f4
AM
21733+void au_warn_loopback(struct super_block *h_sb);
21734+
21735+int au_loopback_init(void);
21736+void au_loopback_fin(void);
1facf9fc 21737+#else
4a4d8108 21738+AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
b752ccd1 21739+ struct dentry *h_adding)
4a4d8108 21740+AuStubInt0(au_test_loopback_kthread, void)
87a755f4
AM
21741+AuStubVoid(au_warn_loopback, struct super_block *h_sb)
21742+
21743+AuStubInt0(au_loopback_init, void)
21744+AuStubVoid(au_loopback_fin, void)
1facf9fc 21745+#endif /* BLK_DEV_LOOP */
21746+
21747+#endif /* __KERNEL__ */
21748+#endif /* __AUFS_LOOP_H__ */
7f207e10
AM
21749diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
21750--- /usr/share/empty/fs/aufs/magic.mk 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21751+++ linux/fs/aufs/magic.mk 2015-09-24 10:47:58.254719746 +0200
7e9cd9fe 21752@@ -0,0 +1,30 @@
1facf9fc 21753+
21754+# defined in ${srctree}/fs/fuse/inode.c
21755+# tristate
21756+ifdef CONFIG_FUSE_FS
21757+ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
21758+endif
21759+
1facf9fc 21760+# defined in ${srctree}/fs/xfs/xfs_sb.h
21761+# tristate
21762+ifdef CONFIG_XFS_FS
21763+ccflags-y += -DXFS_SB_MAGIC=0x58465342
21764+endif
21765+
21766+# defined in ${srctree}/fs/configfs/mount.c
21767+# tristate
21768+ifdef CONFIG_CONFIGFS_FS
21769+ccflags-y += -DCONFIGFS_MAGIC=0x62656570
21770+endif
21771+
1facf9fc 21772+# defined in ${srctree}/fs/ubifs/ubifs.h
21773+# tristate
21774+ifdef CONFIG_UBIFS_FS
21775+ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
21776+endif
4a4d8108
AM
21777+
21778+# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
21779+# tristate
21780+ifdef CONFIG_HFSPLUS_FS
21781+ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
21782+endif
7f207e10
AM
21783diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
21784--- /usr/share/empty/fs/aufs/Makefile 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 21785+++ linux/fs/aufs/Makefile 2015-09-24 10:47:58.248052907 +0200
c1595e42 21786@@ -0,0 +1,44 @@
4a4d8108
AM
21787+
21788+include ${src}/magic.mk
21789+ifeq (${CONFIG_AUFS_FS},m)
21790+include ${src}/conf.mk
21791+endif
21792+-include ${src}/priv_def.mk
21793+
21794+# cf. include/linux/kernel.h
21795+# enable pr_debug
21796+ccflags-y += -DDEBUG
f6c5ef8b
AM
21797+# sparse requires the full pathname
21798+ifdef M
523b37e3 21799+ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
f6c5ef8b 21800+else
523b37e3 21801+ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
f6c5ef8b 21802+endif
4a4d8108
AM
21803+
21804+obj-$(CONFIG_AUFS_FS) += aufs.o
21805+aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
21806+ wkq.o vfsub.o dcsub.o \
e49829fe 21807+ cpup.o whout.o wbr_policy.o \
4a4d8108
AM
21808+ dinfo.o dentry.o \
21809+ dynop.o \
21810+ finfo.o file.o f_op.o \
21811+ dir.o vdir.o \
21812+ iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
c2b27bf2 21813+ mvdown.o ioctl.o
4a4d8108
AM
21814+
21815+# all are boolean
e49829fe 21816+aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
4a4d8108
AM
21817+aufs-$(CONFIG_SYSFS) += sysfs.o
21818+aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
21819+aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
21820+aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
21821+aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
4a4d8108 21822+aufs-$(CONFIG_AUFS_EXPORT) += export.o
c1595e42
JR
21823+aufs-$(CONFIG_AUFS_XATTR) += xattr.o
21824+aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
076b876e 21825+aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
4a4d8108
AM
21826+aufs-$(CONFIG_AUFS_POLL) += poll.o
21827+aufs-$(CONFIG_AUFS_RDU) += rdu.o
4a4d8108
AM
21828+aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
21829+aufs-$(CONFIG_AUFS_DEBUG) += debug.o
21830+aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
7f207e10
AM
21831diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
21832--- /usr/share/empty/fs/aufs/module.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
21833+++ linux/fs/aufs/module.c 2016-07-25 19:05:34.814493242 +0200
21834@@ -0,0 +1,223 @@
1facf9fc 21835+/*
8cdd5066 21836+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 21837+ *
21838+ * This program, aufs is free software; you can redistribute it and/or modify
21839+ * it under the terms of the GNU General Public License as published by
21840+ * the Free Software Foundation; either version 2 of the License, or
21841+ * (at your option) any later version.
dece6358
AM
21842+ *
21843+ * This program is distributed in the hope that it will be useful,
21844+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
21845+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21846+ * GNU General Public License for more details.
21847+ *
21848+ * You should have received a copy of the GNU General Public License
523b37e3 21849+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 21850+ */
21851+
21852+/*
21853+ * module global variables and operations
21854+ */
21855+
21856+#include <linux/module.h>
21857+#include <linux/seq_file.h>
21858+#include "aufs.h"
21859+
21860+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp)
21861+{
21862+ if (new_sz <= nused)
21863+ return p;
21864+
21865+ p = krealloc(p, new_sz, gfp);
21866+ if (p)
21867+ memset(p + nused, 0, new_sz - nused);
21868+ return p;
21869+}
21870+
21871+/* ---------------------------------------------------------------------- */
21872+
21873+/*
21874+ * aufs caches
21875+ */
5afbbe0d
AM
21876+struct kmem_cache *au_cachep[AuCache_Last] = {
21877+ [0] = NULL
21878+};
21879+
21880+static void au_cache_fin(void)
21881+{
21882+ int i;
21883+
21884+ /*
21885+ * Make sure all delayed rcu free inodes are flushed before we
21886+ * destroy cache.
21887+ */
21888+ rcu_barrier();
21889+
21890+ /* excluding AuCache_HNOTIFY */
21891+ BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
21892+ for (i = 0; i < AuCache_HNOTIFY; i++) {
21893+ kmem_cache_destroy(au_cachep[i]);
21894+ au_cachep[i] = NULL;
21895+ }
21896+}
21897+
1facf9fc 21898+static int __init au_cache_init(void)
21899+{
4a4d8108 21900+ au_cachep[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
1facf9fc 21901+ if (au_cachep[AuCache_DINFO])
027c5e7a 21902+ /* SLAB_DESTROY_BY_RCU */
4a4d8108
AM
21903+ au_cachep[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
21904+ au_icntnr_init_once);
1facf9fc 21905+ if (au_cachep[AuCache_ICNTNR])
4a4d8108
AM
21906+ au_cachep[AuCache_FINFO] = AuCacheCtor(au_finfo,
21907+ au_fi_init_once);
1facf9fc 21908+ if (au_cachep[AuCache_FINFO])
21909+ au_cachep[AuCache_VDIR] = AuCache(au_vdir);
21910+ if (au_cachep[AuCache_VDIR])
21911+ au_cachep[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
21912+ if (au_cachep[AuCache_DEHSTR])
21913+ return 0;
21914+
5afbbe0d 21915+ au_cache_fin();
1facf9fc 21916+ return -ENOMEM;
21917+}
21918+
1facf9fc 21919+/* ---------------------------------------------------------------------- */
21920+
21921+int au_dir_roflags;
21922+
e49829fe 21923+#ifdef CONFIG_AUFS_SBILIST
1e00d052
AM
21924+/*
21925+ * iterate_supers_type() doesn't protect us from
21926+ * remounting (branch management)
21927+ */
5afbbe0d 21928+struct au_sphlhead au_sbilist;
e49829fe
JR
21929+#endif
21930+
1facf9fc 21931+/*
21932+ * functions for module interface.
21933+ */
21934+MODULE_LICENSE("GPL");
21935+/* MODULE_LICENSE("GPL v2"); */
dece6358 21936+MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
1facf9fc 21937+MODULE_DESCRIPTION(AUFS_NAME
21938+ " -- Advanced multi layered unification filesystem");
21939+MODULE_VERSION(AUFS_VERSION);
c06a8ce3 21940+MODULE_ALIAS_FS(AUFS_NAME);
1facf9fc 21941+
1facf9fc 21942+/* this module parameter has no meaning when SYSFS is disabled */
21943+int sysaufs_brs = 1;
21944+MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
21945+module_param_named(brs, sysaufs_brs, int, S_IRUGO);
21946+
076b876e 21947+/* this module parameter has no meaning when USER_NS is disabled */
8cdd5066 21948+bool au_userns;
076b876e
AM
21949+MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
21950+module_param_named(allow_userns, au_userns, bool, S_IRUGO);
21951+
1facf9fc 21952+/* ---------------------------------------------------------------------- */
21953+
21954+static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
21955+
21956+int au_seq_path(struct seq_file *seq, struct path *path)
21957+{
79b8bda9
AM
21958+ int err;
21959+
21960+ err = seq_path(seq, path, au_esc_chars);
21961+ if (err > 0)
21962+ err = 0;
21963+ else if (err < 0)
21964+ err = -ENOMEM;
21965+
21966+ return err;
1facf9fc 21967+}
21968+
21969+/* ---------------------------------------------------------------------- */
21970+
21971+static int __init aufs_init(void)
21972+{
21973+ int err, i;
21974+ char *p;
21975+
21976+ p = au_esc_chars;
21977+ for (i = 1; i <= ' '; i++)
21978+ *p++ = i;
21979+ *p++ = '\\';
21980+ *p++ = '\x7f';
21981+ *p = 0;
21982+
21983+ au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
21984+
b95c5147
AM
21985+ memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
21986+ for (i = 0; i < AuIop_Last; i++)
21987+ aufs_iop_nogetattr[i].getattr = NULL;
21988+
e49829fe 21989+ au_sbilist_init();
1facf9fc 21990+ sysaufs_brs_init();
21991+ au_debug_init();
4a4d8108 21992+ au_dy_init();
1facf9fc 21993+ err = sysaufs_init();
21994+ if (unlikely(err))
21995+ goto out;
e49829fe 21996+ err = au_procfs_init();
4f0767ce 21997+ if (unlikely(err))
953406b4 21998+ goto out_sysaufs;
e49829fe
JR
21999+ err = au_wkq_init();
22000+ if (unlikely(err))
22001+ goto out_procfs;
87a755f4 22002+ err = au_loopback_init();
1facf9fc 22003+ if (unlikely(err))
22004+ goto out_wkq;
87a755f4
AM
22005+ err = au_hnotify_init();
22006+ if (unlikely(err))
22007+ goto out_loopback;
1facf9fc 22008+ err = au_sysrq_init();
22009+ if (unlikely(err))
22010+ goto out_hin;
22011+ err = au_cache_init();
22012+ if (unlikely(err))
22013+ goto out_sysrq;
076b876e
AM
22014+
22015+ aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
1facf9fc 22016+ err = register_filesystem(&aufs_fs_type);
22017+ if (unlikely(err))
22018+ goto out_cache;
076b876e 22019+
4a4d8108
AM
22020+ /* since we define pr_fmt, call printk directly */
22021+ printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
1facf9fc 22022+ goto out; /* success */
22023+
4f0767ce 22024+out_cache:
1facf9fc 22025+ au_cache_fin();
4f0767ce 22026+out_sysrq:
1facf9fc 22027+ au_sysrq_fin();
4f0767ce 22028+out_hin:
4a4d8108 22029+ au_hnotify_fin();
87a755f4
AM
22030+out_loopback:
22031+ au_loopback_fin();
4f0767ce 22032+out_wkq:
1facf9fc 22033+ au_wkq_fin();
e49829fe
JR
22034+out_procfs:
22035+ au_procfs_fin();
4f0767ce 22036+out_sysaufs:
1facf9fc 22037+ sysaufs_fin();
4a4d8108 22038+ au_dy_fin();
4f0767ce 22039+out:
1facf9fc 22040+ return err;
22041+}
22042+
22043+static void __exit aufs_exit(void)
22044+{
22045+ unregister_filesystem(&aufs_fs_type);
22046+ au_cache_fin();
22047+ au_sysrq_fin();
4a4d8108 22048+ au_hnotify_fin();
87a755f4 22049+ au_loopback_fin();
1facf9fc 22050+ au_wkq_fin();
e49829fe 22051+ au_procfs_fin();
1facf9fc 22052+ sysaufs_fin();
4a4d8108 22053+ au_dy_fin();
1facf9fc 22054+}
22055+
22056+module_init(aufs_init);
22057+module_exit(aufs_exit);
7f207e10
AM
22058diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
22059--- /usr/share/empty/fs/aufs/module.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
22060+++ linux/fs/aufs/module.h 2016-07-25 19:05:34.814493242 +0200
22061@@ -0,0 +1,89 @@
1facf9fc 22062+/*
8cdd5066 22063+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 22064+ *
22065+ * This program, aufs is free software; you can redistribute it and/or modify
22066+ * it under the terms of the GNU General Public License as published by
22067+ * the Free Software Foundation; either version 2 of the License, or
22068+ * (at your option) any later version.
dece6358
AM
22069+ *
22070+ * This program is distributed in the hope that it will be useful,
22071+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22072+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22073+ * GNU General Public License for more details.
22074+ *
22075+ * You should have received a copy of the GNU General Public License
523b37e3 22076+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22077+ */
22078+
22079+/*
22080+ * module initialization and module-global
22081+ */
22082+
22083+#ifndef __AUFS_MODULE_H__
22084+#define __AUFS_MODULE_H__
22085+
22086+#ifdef __KERNEL__
22087+
22088+#include <linux/slab.h>
22089+
dece6358
AM
22090+struct path;
22091+struct seq_file;
22092+
1facf9fc 22093+/* module parameters */
1facf9fc 22094+extern int sysaufs_brs;
8cdd5066 22095+extern bool au_userns;
1facf9fc 22096+
22097+/* ---------------------------------------------------------------------- */
22098+
22099+extern int au_dir_roflags;
22100+
22101+void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp);
22102+int au_seq_path(struct seq_file *seq, struct path *path);
22103+
e49829fe
JR
22104+#ifdef CONFIG_PROC_FS
22105+/* procfs.c */
22106+int __init au_procfs_init(void);
22107+void au_procfs_fin(void);
22108+#else
22109+AuStubInt0(au_procfs_init, void);
22110+AuStubVoid(au_procfs_fin, void);
22111+#endif
22112+
4f0767ce
JR
22113+/* ---------------------------------------------------------------------- */
22114+
22115+/* kmem cache */
1facf9fc 22116+enum {
22117+ AuCache_DINFO,
22118+ AuCache_ICNTNR,
22119+ AuCache_FINFO,
22120+ AuCache_VDIR,
22121+ AuCache_DEHSTR,
7eafdf33 22122+ AuCache_HNOTIFY, /* must be last */
1facf9fc 22123+ AuCache_Last
22124+};
22125+
4a4d8108
AM
22126+#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
22127+#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
22128+#define AuCacheCtor(type, ctor) \
22129+ kmem_cache_create(#type, sizeof(struct type), \
22130+ __alignof__(struct type), AuCacheFlags, ctor)
1facf9fc 22131+
22132+extern struct kmem_cache *au_cachep[];
22133+
22134+#define AuCacheFuncs(name, index) \
4a4d8108 22135+static inline struct au_##name *au_cache_alloc_##name(void) \
1facf9fc 22136+{ return kmem_cache_alloc(au_cachep[AuCache_##index], GFP_NOFS); } \
4a4d8108 22137+static inline void au_cache_free_##name(struct au_##name *p) \
1facf9fc 22138+{ kmem_cache_free(au_cachep[AuCache_##index], p); }
22139+
22140+AuCacheFuncs(dinfo, DINFO);
22141+AuCacheFuncs(icntnr, ICNTNR);
22142+AuCacheFuncs(finfo, FINFO);
22143+AuCacheFuncs(vdir, VDIR);
4a4d8108
AM
22144+AuCacheFuncs(vdir_dehstr, DEHSTR);
22145+#ifdef CONFIG_AUFS_HNOTIFY
22146+AuCacheFuncs(hnotify, HNOTIFY);
22147+#endif
1facf9fc 22148+
4a4d8108
AM
22149+#endif /* __KERNEL__ */
22150+#endif /* __AUFS_MODULE_H__ */
c2b27bf2
AM
22151diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
22152--- /usr/share/empty/fs/aufs/mvdown.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
22153+++ linux/fs/aufs/mvdown.c 2016-07-25 19:05:34.814493242 +0200
22154@@ -0,0 +1,704 @@
c2b27bf2 22155+/*
8cdd5066 22156+ * Copyright (C) 2011-2016 Junjiro R. Okajima
c2b27bf2
AM
22157+ *
22158+ * This program, aufs is free software; you can redistribute it and/or modify
22159+ * it under the terms of the GNU General Public License as published by
22160+ * the Free Software Foundation; either version 2 of the License, or
22161+ * (at your option) any later version.
22162+ *
22163+ * This program is distributed in the hope that it will be useful,
22164+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22165+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22166+ * GNU General Public License for more details.
22167+ *
22168+ * You should have received a copy of the GNU General Public License
523b37e3
AM
22169+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
22170+ */
22171+
22172+/*
22173+ * move-down, opposite of copy-up
c2b27bf2
AM
22174+ */
22175+
22176+#include "aufs.h"
22177+
c2b27bf2
AM
22178+struct au_mvd_args {
22179+ struct {
c2b27bf2
AM
22180+ struct super_block *h_sb;
22181+ struct dentry *h_parent;
22182+ struct au_hinode *hdir;
392086de 22183+ struct inode *h_dir, *h_inode;
c1595e42 22184+ struct au_pin pin;
c2b27bf2
AM
22185+ } info[AUFS_MVDOWN_NARRAY];
22186+
22187+ struct aufs_mvdown mvdown;
22188+ struct dentry *dentry, *parent;
22189+ struct inode *inode, *dir;
22190+ struct super_block *sb;
22191+ aufs_bindex_t bopq, bwh, bfound;
22192+ unsigned char rename_lock;
c2b27bf2
AM
22193+};
22194+
392086de 22195+#define mvd_errno mvdown.au_errno
076b876e
AM
22196+#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
22197+#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
22198+#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
22199+#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
c2b27bf2 22200+
392086de
AM
22201+#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
22202+#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
22203+#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
22204+#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
22205+#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
c1595e42 22206+#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
392086de
AM
22207+
22208+#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
22209+#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
22210+#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
22211+#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
22212+#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
c1595e42 22213+#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
c2b27bf2
AM
22214+
22215+#define AU_MVD_PR(flag, ...) do { \
22216+ if (flag) \
22217+ pr_err(__VA_ARGS__); \
22218+ } while (0)
22219+
076b876e
AM
22220+static int find_lower_writable(struct au_mvd_args *a)
22221+{
22222+ struct super_block *sb;
5afbbe0d 22223+ aufs_bindex_t bindex, bbot;
076b876e
AM
22224+ struct au_branch *br;
22225+
22226+ sb = a->sb;
22227+ bindex = a->mvd_bsrc;
5afbbe0d 22228+ bbot = au_sbbot(sb);
076b876e 22229+ if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
5afbbe0d 22230+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
22231+ br = au_sbr(sb, bindex);
22232+ if (au_br_fhsm(br->br_perm)
22233+ && (!(au_br_sb(br)->s_flags & MS_RDONLY)))
22234+ return bindex;
22235+ }
22236+ else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
5afbbe0d 22237+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
22238+ br = au_sbr(sb, bindex);
22239+ if (!au_br_rdonly(br))
22240+ return bindex;
22241+ }
22242+ else
5afbbe0d 22243+ for (bindex++; bindex <= bbot; bindex++) {
076b876e
AM
22244+ br = au_sbr(sb, bindex);
22245+ if (!(au_br_sb(br)->s_flags & MS_RDONLY)) {
22246+ if (au_br_rdonly(br))
22247+ a->mvdown.flags
22248+ |= AUFS_MVDOWN_ROLOWER_R;
22249+ return bindex;
22250+ }
22251+ }
22252+
22253+ return -1;
22254+}
22255+
c2b27bf2 22256+/* make the parent dir on bdst */
392086de 22257+static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22258+{
22259+ int err;
22260+
22261+ err = 0;
22262+ a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
22263+ a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
22264+ a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
22265+ a->mvd_h_dst_parent = NULL;
5afbbe0d 22266+ if (au_dbbot(a->parent) >= a->mvd_bdst)
c2b27bf2
AM
22267+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22268+ if (!a->mvd_h_dst_parent) {
22269+ err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
22270+ if (unlikely(err)) {
392086de 22271+ AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
c2b27bf2
AM
22272+ goto out;
22273+ }
22274+ a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
22275+ }
22276+
22277+out:
22278+ AuTraceErr(err);
22279+ return err;
22280+}
22281+
22282+/* lock them all */
392086de 22283+static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22284+{
22285+ int err;
22286+ struct dentry *h_trap;
22287+
22288+ a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
22289+ a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
c1595e42
JR
22290+ err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
22291+ au_opt_udba(a->sb),
22292+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22293+ AuTraceErr(err);
22294+ if (unlikely(err)) {
22295+ AU_MVD_PR(dmsg, "pin_dst failed\n");
22296+ goto out;
22297+ }
22298+
c2b27bf2
AM
22299+ if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
22300+ a->rename_lock = 0;
c1595e42
JR
22301+ au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22302+ AuLsc_DI_PARENT, AuLsc_I_PARENT3,
22303+ au_opt_udba(a->sb),
22304+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22305+ err = au_do_pin(&a->mvd_pin_src);
22306+ AuTraceErr(err);
5527c038 22307+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
22308+ if (unlikely(err)) {
22309+ AU_MVD_PR(dmsg, "pin_src failed\n");
22310+ goto out_dst;
22311+ }
22312+ goto out; /* success */
c2b27bf2
AM
22313+ }
22314+
c2b27bf2 22315+ a->rename_lock = 1;
c1595e42
JR
22316+ au_pin_hdir_unlock(&a->mvd_pin_dst);
22317+ err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
22318+ au_opt_udba(a->sb),
22319+ AuPin_MNT_WRITE | AuPin_DI_LOCKED);
22320+ AuTraceErr(err);
5527c038 22321+ a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
c1595e42
JR
22322+ if (unlikely(err)) {
22323+ AU_MVD_PR(dmsg, "pin_src failed\n");
22324+ au_pin_hdir_lock(&a->mvd_pin_dst);
22325+ goto out_dst;
22326+ }
22327+ au_pin_hdir_unlock(&a->mvd_pin_src);
c2b27bf2
AM
22328+ h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22329+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
22330+ if (h_trap) {
22331+ err = (h_trap != a->mvd_h_src_parent);
22332+ if (err)
22333+ err = (h_trap != a->mvd_h_dst_parent);
22334+ }
22335+ BUG_ON(err); /* it should never happen */
c1595e42
JR
22336+ if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
22337+ err = -EBUSY;
22338+ AuTraceErr(err);
22339+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22340+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
22341+ au_pin_hdir_lock(&a->mvd_pin_src);
22342+ au_unpin(&a->mvd_pin_src);
22343+ au_pin_hdir_lock(&a->mvd_pin_dst);
22344+ goto out_dst;
22345+ }
22346+ goto out; /* success */
c2b27bf2 22347+
c1595e42
JR
22348+out_dst:
22349+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
22350+out:
22351+ AuTraceErr(err);
22352+ return err;
22353+}
22354+
392086de 22355+static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2 22356+{
c1595e42
JR
22357+ if (!a->rename_lock)
22358+ au_unpin(&a->mvd_pin_src);
22359+ else {
c2b27bf2
AM
22360+ vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
22361+ a->mvd_h_dst_parent, a->mvd_hdir_dst);
c1595e42
JR
22362+ au_pin_hdir_lock(&a->mvd_pin_src);
22363+ au_unpin(&a->mvd_pin_src);
22364+ au_pin_hdir_lock(&a->mvd_pin_dst);
22365+ }
22366+ au_unpin(&a->mvd_pin_dst);
c2b27bf2
AM
22367+}
22368+
22369+/* copy-down the file */
392086de 22370+static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22371+{
22372+ int err;
22373+ struct au_cp_generic cpg = {
22374+ .dentry = a->dentry,
22375+ .bdst = a->mvd_bdst,
22376+ .bsrc = a->mvd_bsrc,
22377+ .len = -1,
c1595e42 22378+ .pin = &a->mvd_pin_dst,
c2b27bf2
AM
22379+ .flags = AuCpup_DTIME | AuCpup_HOPEN
22380+ };
22381+
22382+ AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
392086de
AM
22383+ if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22384+ au_fset_cpup(cpg.flags, OVERWRITE);
22385+ if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
22386+ au_fset_cpup(cpg.flags, RWDST);
c2b27bf2
AM
22387+ err = au_sio_cpdown_simple(&cpg);
22388+ if (unlikely(err))
392086de 22389+ AU_MVD_PR(dmsg, "cpdown failed\n");
c2b27bf2
AM
22390+
22391+ AuTraceErr(err);
22392+ return err;
22393+}
22394+
22395+/*
22396+ * unlink the whiteout on bdst if exist which may be created by UDBA while we
22397+ * were sleeping
22398+ */
392086de 22399+static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22400+{
22401+ int err;
22402+ struct path h_path;
22403+ struct au_branch *br;
523b37e3 22404+ struct inode *delegated;
c2b27bf2
AM
22405+
22406+ br = au_sbr(a->sb, a->mvd_bdst);
22407+ h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
22408+ err = PTR_ERR(h_path.dentry);
22409+ if (IS_ERR(h_path.dentry)) {
392086de 22410+ AU_MVD_PR(dmsg, "wh_lkup failed\n");
c2b27bf2
AM
22411+ goto out;
22412+ }
22413+
22414+ err = 0;
5527c038 22415+ if (d_is_positive(h_path.dentry)) {
c2b27bf2 22416+ h_path.mnt = au_br_mnt(br);
523b37e3 22417+ delegated = NULL;
5527c038 22418+ err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
523b37e3
AM
22419+ &delegated, /*force*/0);
22420+ if (unlikely(err == -EWOULDBLOCK)) {
22421+ pr_warn("cannot retry for NFSv4 delegation"
22422+ " for an internal unlink\n");
22423+ iput(delegated);
22424+ }
c2b27bf2 22425+ if (unlikely(err))
392086de 22426+ AU_MVD_PR(dmsg, "wh_unlink failed\n");
c2b27bf2
AM
22427+ }
22428+ dput(h_path.dentry);
22429+
22430+out:
22431+ AuTraceErr(err);
22432+ return err;
22433+}
22434+
22435+/*
22436+ * unlink the topmost h_dentry
c2b27bf2 22437+ */
392086de 22438+static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22439+{
22440+ int err;
22441+ struct path h_path;
523b37e3 22442+ struct inode *delegated;
c2b27bf2
AM
22443+
22444+ h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
22445+ h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
523b37e3
AM
22446+ delegated = NULL;
22447+ err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
22448+ if (unlikely(err == -EWOULDBLOCK)) {
22449+ pr_warn("cannot retry for NFSv4 delegation"
22450+ " for an internal unlink\n");
22451+ iput(delegated);
22452+ }
c2b27bf2 22453+ if (unlikely(err))
392086de 22454+ AU_MVD_PR(dmsg, "unlink failed\n");
c2b27bf2
AM
22455+
22456+ AuTraceErr(err);
22457+ return err;
22458+}
22459+
076b876e
AM
22460+/* Since mvdown succeeded, we ignore an error of this function */
22461+static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
22462+{
22463+ int err;
22464+ struct au_branch *br;
22465+
22466+ a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
22467+ br = au_sbr(a->sb, a->mvd_bsrc);
22468+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
22469+ if (!err) {
22470+ br = au_sbr(a->sb, a->mvd_bdst);
22471+ a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
22472+ err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
22473+ }
22474+ if (!err)
22475+ a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
22476+ else
22477+ AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
22478+}
22479+
c2b27bf2
AM
22480+/*
22481+ * copy-down the file and unlink the bsrc file.
22482+ * - unlink the bdst whout if exist
22483+ * - copy-down the file (with whtmp name and rename)
22484+ * - unlink the bsrc file
22485+ */
392086de 22486+static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22487+{
22488+ int err;
22489+
392086de 22490+ err = au_do_mkdir(dmsg, a);
c2b27bf2 22491+ if (!err)
392086de 22492+ err = au_do_lock(dmsg, a);
c2b27bf2
AM
22493+ if (unlikely(err))
22494+ goto out;
22495+
22496+ /*
22497+ * do not revert the activities we made on bdst since they should be
22498+ * harmless in aufs.
22499+ */
22500+
392086de 22501+ err = au_do_cpdown(dmsg, a);
c2b27bf2 22502+ if (!err)
392086de
AM
22503+ err = au_do_unlink_wh(dmsg, a);
22504+ if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
22505+ err = au_do_unlink(dmsg, a);
c2b27bf2
AM
22506+ if (unlikely(err))
22507+ goto out_unlock;
22508+
c1595e42
JR
22509+ AuDbg("%pd2, 0x%x, %d --> %d\n",
22510+ a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
076b876e
AM
22511+ if (find_lower_writable(a) < 0)
22512+ a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
22513+
22514+ if (a->mvdown.flags & AUFS_MVDOWN_STFS)
22515+ au_do_stfs(dmsg, a);
22516+
c2b27bf2 22517+ /* maintain internal array */
392086de
AM
22518+ if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
22519+ au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
5afbbe0d 22520+ au_set_dbtop(a->dentry, a->mvd_bdst);
392086de 22521+ au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
5afbbe0d 22522+ au_set_ibtop(a->inode, a->mvd_bdst);
79b8bda9
AM
22523+ } else {
22524+ /* hide the lower */
22525+ au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
5afbbe0d 22526+ au_set_dbbot(a->dentry, a->mvd_bsrc);
79b8bda9 22527+ au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
5afbbe0d 22528+ au_set_ibbot(a->inode, a->mvd_bsrc);
392086de 22529+ }
5afbbe0d
AM
22530+ if (au_dbbot(a->dentry) < a->mvd_bdst)
22531+ au_set_dbbot(a->dentry, a->mvd_bdst);
22532+ if (au_ibbot(a->inode) < a->mvd_bdst)
22533+ au_set_ibbot(a->inode, a->mvd_bdst);
c2b27bf2
AM
22534+
22535+out_unlock:
392086de 22536+ au_do_unlock(dmsg, a);
c2b27bf2
AM
22537+out:
22538+ AuTraceErr(err);
22539+ return err;
22540+}
22541+
22542+/* ---------------------------------------------------------------------- */
22543+
c2b27bf2 22544+/* make sure the file is idle */
392086de 22545+static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22546+{
22547+ int err, plinked;
c2b27bf2
AM
22548+
22549+ err = 0;
c2b27bf2 22550+ plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
5afbbe0d 22551+ if (au_dbtop(a->dentry) == a->mvd_bsrc
c1595e42 22552+ && au_dcount(a->dentry) == 1
c2b27bf2 22553+ && atomic_read(&a->inode->i_count) == 1
392086de 22554+ /* && a->mvd_h_src_inode->i_nlink == 1 */
c2b27bf2
AM
22555+ && (!plinked || !au_plink_test(a->inode))
22556+ && a->inode->i_nlink == 1)
22557+ goto out;
22558+
22559+ err = -EBUSY;
392086de 22560+ AU_MVD_PR(dmsg,
c1595e42 22561+ "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
5afbbe0d 22562+ a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
c2b27bf2 22563+ atomic_read(&a->inode->i_count), a->inode->i_nlink,
392086de 22564+ a->mvd_h_src_inode->i_nlink,
c2b27bf2
AM
22565+ plinked, plinked ? au_plink_test(a->inode) : 0);
22566+
22567+out:
22568+ AuTraceErr(err);
22569+ return err;
22570+}
22571+
22572+/* make sure the parent dir is fine */
392086de 22573+static int au_mvd_args_parent(const unsigned char dmsg,
c2b27bf2
AM
22574+ struct au_mvd_args *a)
22575+{
22576+ int err;
22577+ aufs_bindex_t bindex;
22578+
22579+ err = 0;
22580+ if (unlikely(au_alive_dir(a->parent))) {
22581+ err = -ENOENT;
392086de 22582+ AU_MVD_PR(dmsg, "parent dir is dead\n");
c2b27bf2
AM
22583+ goto out;
22584+ }
22585+
22586+ a->bopq = au_dbdiropq(a->parent);
22587+ bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
22588+ AuDbg("b%d\n", bindex);
22589+ if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
22590+ || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
22591+ err = -EINVAL;
392086de
AM
22592+ a->mvd_errno = EAU_MVDOWN_OPAQUE;
22593+ AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
c2b27bf2
AM
22594+ a->bopq, a->mvd_bdst);
22595+ }
22596+
22597+out:
22598+ AuTraceErr(err);
22599+ return err;
22600+}
22601+
392086de 22602+static int au_mvd_args_intermediate(const unsigned char dmsg,
c2b27bf2
AM
22603+ struct au_mvd_args *a)
22604+{
22605+ int err;
22606+ struct au_dinfo *dinfo, *tmp;
22607+
22608+ /* lookup the next lower positive entry */
22609+ err = -ENOMEM;
22610+ tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
22611+ if (unlikely(!tmp))
22612+ goto out;
22613+
22614+ a->bfound = -1;
22615+ a->bwh = -1;
22616+ dinfo = au_di(a->dentry);
22617+ au_di_cp(tmp, dinfo);
22618+ au_di_swap(tmp, dinfo);
22619+
22620+ /* returns the number of positive dentries */
5afbbe0d
AM
22621+ err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
22622+ /* AuLkup_IGNORE_PERM */ 0);
c2b27bf2
AM
22623+ if (!err)
22624+ a->bwh = au_dbwh(a->dentry);
22625+ else if (err > 0)
5afbbe0d 22626+ a->bfound = au_dbtop(a->dentry);
c2b27bf2
AM
22627+
22628+ au_di_swap(tmp, dinfo);
22629+ au_rw_write_unlock(&tmp->di_rwsem);
22630+ au_di_free(tmp);
22631+ if (unlikely(err < 0))
392086de 22632+ AU_MVD_PR(dmsg, "failed look-up lower\n");
c2b27bf2
AM
22633+
22634+ /*
22635+ * here, we have these cases.
22636+ * bfound == -1
22637+ * no positive dentry under bsrc. there are more sub-cases.
22638+ * bwh < 0
22639+ * there no whiteout, we can safely move-down.
22640+ * bwh <= bsrc
22641+ * impossible
22642+ * bsrc < bwh && bwh < bdst
22643+ * there is a whiteout on RO branch. cannot proceed.
22644+ * bwh == bdst
22645+ * there is a whiteout on the RW target branch. it should
22646+ * be removed.
22647+ * bdst < bwh
22648+ * there is a whiteout somewhere unrelated branch.
22649+ * -1 < bfound && bfound <= bsrc
22650+ * impossible.
22651+ * bfound < bdst
22652+ * found, but it is on RO branch between bsrc and bdst. cannot
22653+ * proceed.
22654+ * bfound == bdst
22655+ * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
22656+ * error.
22657+ * bdst < bfound
22658+ * found, after we create the file on bdst, it will be hidden.
22659+ */
22660+
22661+ AuDebugOn(a->bfound == -1
22662+ && a->bwh != -1
22663+ && a->bwh <= a->mvd_bsrc);
22664+ AuDebugOn(-1 < a->bfound
22665+ && a->bfound <= a->mvd_bsrc);
22666+
22667+ err = -EINVAL;
22668+ if (a->bfound == -1
22669+ && a->mvd_bsrc < a->bwh
22670+ && a->bwh != -1
22671+ && a->bwh < a->mvd_bdst) {
392086de
AM
22672+ a->mvd_errno = EAU_MVDOWN_WHITEOUT;
22673+ AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
c2b27bf2
AM
22674+ a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
22675+ goto out;
22676+ } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
392086de
AM
22677+ a->mvd_errno = EAU_MVDOWN_UPPER;
22678+ AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
c2b27bf2
AM
22679+ a->mvd_bdst, a->bfound);
22680+ goto out;
22681+ }
22682+
22683+ err = 0; /* success */
22684+
22685+out:
22686+ AuTraceErr(err);
22687+ return err;
22688+}
22689+
392086de 22690+static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22691+{
22692+ int err;
22693+
392086de
AM
22694+ err = 0;
22695+ if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
22696+ && a->bfound == a->mvd_bdst)
22697+ err = -EEXIST;
c2b27bf2
AM
22698+ AuTraceErr(err);
22699+ return err;
22700+}
22701+
392086de 22702+static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
c2b27bf2
AM
22703+{
22704+ int err;
22705+ struct au_branch *br;
22706+
22707+ err = -EISDIR;
22708+ if (unlikely(S_ISDIR(a->inode->i_mode)))
22709+ goto out;
22710+
22711+ err = -EINVAL;
392086de 22712+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
5afbbe0d 22713+ a->mvd_bsrc = au_ibtop(a->inode);
392086de
AM
22714+ else {
22715+ a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
22716+ if (unlikely(a->mvd_bsrc < 0
5afbbe0d
AM
22717+ || (a->mvd_bsrc < au_dbtop(a->dentry)
22718+ || au_dbbot(a->dentry) < a->mvd_bsrc
392086de 22719+ || !au_h_dptr(a->dentry, a->mvd_bsrc))
5afbbe0d
AM
22720+ || (a->mvd_bsrc < au_ibtop(a->inode)
22721+ || au_ibbot(a->inode) < a->mvd_bsrc
392086de
AM
22722+ || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
22723+ a->mvd_errno = EAU_MVDOWN_NOUPPER;
22724+ AU_MVD_PR(dmsg, "no upper\n");
22725+ goto out;
22726+ }
22727+ }
5afbbe0d 22728+ if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
392086de
AM
22729+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
22730+ AU_MVD_PR(dmsg, "on the bottom\n");
c2b27bf2
AM
22731+ goto out;
22732+ }
392086de 22733+ a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
c2b27bf2
AM
22734+ br = au_sbr(a->sb, a->mvd_bsrc);
22735+ err = au_br_rdonly(br);
392086de
AM
22736+ if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
22737+ if (unlikely(err))
22738+ goto out;
22739+ } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
22740+ || IS_APPEND(a->mvd_h_src_inode))) {
22741+ if (err)
22742+ a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
22743+ /* go on */
22744+ } else
c2b27bf2
AM
22745+ goto out;
22746+
22747+ err = -EINVAL;
392086de
AM
22748+ if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
22749+ a->mvd_bdst = find_lower_writable(a);
22750+ if (unlikely(a->mvd_bdst < 0)) {
22751+ a->mvd_errno = EAU_MVDOWN_BOTTOM;
22752+ AU_MVD_PR(dmsg, "no writable lower branch\n");
22753+ goto out;
22754+ }
22755+ } else {
22756+ a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
22757+ if (unlikely(a->mvd_bdst < 0
5afbbe0d 22758+ || au_sbbot(a->sb) < a->mvd_bdst)) {
392086de
AM
22759+ a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
22760+ AU_MVD_PR(dmsg, "no lower brid\n");
22761+ goto out;
22762+ }
c2b27bf2
AM
22763+ }
22764+
392086de 22765+ err = au_mvd_args_busy(dmsg, a);
c2b27bf2 22766+ if (!err)
392086de 22767+ err = au_mvd_args_parent(dmsg, a);
c2b27bf2 22768+ if (!err)
392086de 22769+ err = au_mvd_args_intermediate(dmsg, a);
c2b27bf2 22770+ if (!err)
392086de 22771+ err = au_mvd_args_exist(dmsg, a);
c2b27bf2
AM
22772+ if (!err)
22773+ AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
22774+
22775+out:
22776+ AuTraceErr(err);
22777+ return err;
22778+}
22779+
22780+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
22781+{
392086de
AM
22782+ int err, e;
22783+ unsigned char dmsg;
22784+ struct au_mvd_args *args;
79b8bda9 22785+ struct inode *inode;
c2b27bf2 22786+
79b8bda9 22787+ inode = d_inode(dentry);
c2b27bf2
AM
22788+ err = -EPERM;
22789+ if (unlikely(!capable(CAP_SYS_ADMIN)))
22790+ goto out;
22791+
392086de
AM
22792+ err = -ENOMEM;
22793+ args = kmalloc(sizeof(*args), GFP_NOFS);
22794+ if (unlikely(!args))
22795+ goto out;
22796+
22797+ err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
22798+ if (!err)
22799+ err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
c2b27bf2
AM
22800+ if (unlikely(err)) {
22801+ err = -EFAULT;
392086de
AM
22802+ AuTraceErr(err);
22803+ goto out_free;
c2b27bf2 22804+ }
392086de
AM
22805+ AuDbg("flags 0x%x\n", args->mvdown.flags);
22806+ args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
22807+ args->mvdown.au_errno = 0;
22808+ args->dentry = dentry;
79b8bda9 22809+ args->inode = inode;
392086de 22810+ args->sb = dentry->d_sb;
c2b27bf2 22811+
392086de
AM
22812+ err = -ENOENT;
22813+ dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
22814+ args->parent = dget_parent(dentry);
5527c038 22815+ args->dir = d_inode(args->parent);
febd17d6 22816+ inode_lock_nested(args->dir, I_MUTEX_PARENT);
392086de
AM
22817+ dput(args->parent);
22818+ if (unlikely(args->parent != dentry->d_parent)) {
22819+ AU_MVD_PR(dmsg, "parent dir is moved\n");
c2b27bf2
AM
22820+ goto out_dir;
22821+ }
22822+
febd17d6 22823+ inode_lock_nested(inode, I_MUTEX_CHILD);
b95c5147 22824+ err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
c2b27bf2
AM
22825+ if (unlikely(err))
22826+ goto out_inode;
22827+
392086de
AM
22828+ di_write_lock_parent(args->parent);
22829+ err = au_mvd_args(dmsg, args);
c2b27bf2
AM
22830+ if (unlikely(err))
22831+ goto out_parent;
22832+
392086de 22833+ err = au_do_mvdown(dmsg, args);
c2b27bf2
AM
22834+ if (unlikely(err))
22835+ goto out_parent;
c2b27bf2 22836+
392086de 22837+ au_cpup_attr_timesizes(args->dir);
79b8bda9
AM
22838+ au_cpup_attr_timesizes(inode);
22839+ if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
22840+ au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
c2b27bf2
AM
22841+ /* au_digen_dec(dentry); */
22842+
22843+out_parent:
392086de 22844+ di_write_unlock(args->parent);
c2b27bf2
AM
22845+ aufs_read_unlock(dentry, AuLock_DW);
22846+out_inode:
febd17d6 22847+ inode_unlock(inode);
c2b27bf2 22848+out_dir:
febd17d6 22849+ inode_unlock(args->dir);
392086de
AM
22850+out_free:
22851+ e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
22852+ if (unlikely(e))
22853+ err = -EFAULT;
22854+ kfree(args);
c2b27bf2
AM
22855+out:
22856+ AuTraceErr(err);
22857+ return err;
22858+}
22859diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
22860--- /usr/share/empty/fs/aufs/opts.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 22861+++ linux/fs/aufs/opts.c 2016-07-25 19:05:34.814493242 +0200
79b8bda9 22862@@ -0,0 +1,1859 @@
1facf9fc 22863+/*
8cdd5066 22864+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 22865+ *
22866+ * This program, aufs is free software; you can redistribute it and/or modify
22867+ * it under the terms of the GNU General Public License as published by
22868+ * the Free Software Foundation; either version 2 of the License, or
22869+ * (at your option) any later version.
dece6358
AM
22870+ *
22871+ * This program is distributed in the hope that it will be useful,
22872+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22873+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22874+ * GNU General Public License for more details.
22875+ *
22876+ * You should have received a copy of the GNU General Public License
523b37e3 22877+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 22878+ */
22879+
22880+/*
22881+ * mount options/flags
22882+ */
22883+
dece6358 22884+#include <linux/namei.h>
1facf9fc 22885+#include <linux/types.h> /* a distribution requires */
22886+#include <linux/parser.h>
22887+#include "aufs.h"
22888+
22889+/* ---------------------------------------------------------------------- */
22890+
22891+enum {
22892+ Opt_br,
7e9cd9fe
AM
22893+ Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
22894+ Opt_idel, Opt_imod,
22895+ Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
dece6358 22896+ Opt_rdblk_def, Opt_rdhash_def,
7e9cd9fe 22897+ Opt_xino, Opt_noxino,
1facf9fc 22898+ Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
22899+ Opt_trunc_xino_path, Opt_itrunc_xino,
22900+ Opt_trunc_xib, Opt_notrunc_xib,
dece6358 22901+ Opt_shwh, Opt_noshwh,
1facf9fc 22902+ Opt_plink, Opt_noplink, Opt_list_plink,
22903+ Opt_udba,
4a4d8108 22904+ Opt_dio, Opt_nodio,
1facf9fc 22905+ Opt_diropq_a, Opt_diropq_w,
22906+ Opt_warn_perm, Opt_nowarn_perm,
22907+ Opt_wbr_copyup, Opt_wbr_create,
076b876e 22908+ Opt_fhsm_sec,
1facf9fc 22909+ Opt_verbose, Opt_noverbose,
22910+ Opt_sum, Opt_nosum, Opt_wsum,
076b876e 22911+ Opt_dirperm1, Opt_nodirperm1,
c1595e42 22912+ Opt_acl, Opt_noacl,
1facf9fc 22913+ Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
22914+};
22915+
22916+static match_table_t options = {
22917+ {Opt_br, "br=%s"},
22918+ {Opt_br, "br:%s"},
22919+
22920+ {Opt_add, "add=%d:%s"},
22921+ {Opt_add, "add:%d:%s"},
22922+ {Opt_add, "ins=%d:%s"},
22923+ {Opt_add, "ins:%d:%s"},
22924+ {Opt_append, "append=%s"},
22925+ {Opt_append, "append:%s"},
22926+ {Opt_prepend, "prepend=%s"},
22927+ {Opt_prepend, "prepend:%s"},
22928+
22929+ {Opt_del, "del=%s"},
22930+ {Opt_del, "del:%s"},
22931+ /* {Opt_idel, "idel:%d"}, */
22932+ {Opt_mod, "mod=%s"},
22933+ {Opt_mod, "mod:%s"},
22934+ /* {Opt_imod, "imod:%d:%s"}, */
22935+
22936+ {Opt_dirwh, "dirwh=%d"},
22937+
22938+ {Opt_xino, "xino=%s"},
22939+ {Opt_noxino, "noxino"},
22940+ {Opt_trunc_xino, "trunc_xino"},
22941+ {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
22942+ {Opt_notrunc_xino, "notrunc_xino"},
22943+ {Opt_trunc_xino_path, "trunc_xino=%s"},
22944+ {Opt_itrunc_xino, "itrunc_xino=%d"},
22945+ /* {Opt_zxino, "zxino=%s"}, */
22946+ {Opt_trunc_xib, "trunc_xib"},
22947+ {Opt_notrunc_xib, "notrunc_xib"},
22948+
e49829fe 22949+#ifdef CONFIG_PROC_FS
1facf9fc 22950+ {Opt_plink, "plink"},
e49829fe
JR
22951+#else
22952+ {Opt_ignore_silent, "plink"},
22953+#endif
22954+
1facf9fc 22955+ {Opt_noplink, "noplink"},
e49829fe 22956+
1facf9fc 22957+#ifdef CONFIG_AUFS_DEBUG
22958+ {Opt_list_plink, "list_plink"},
22959+#endif
22960+
22961+ {Opt_udba, "udba=%s"},
22962+
4a4d8108
AM
22963+ {Opt_dio, "dio"},
22964+ {Opt_nodio, "nodio"},
22965+
076b876e
AM
22966+#ifdef CONFIG_AUFS_FHSM
22967+ {Opt_fhsm_sec, "fhsm_sec=%d"},
22968+#else
22969+ {Opt_ignore_silent, "fhsm_sec=%d"},
22970+#endif
22971+
1facf9fc 22972+ {Opt_diropq_a, "diropq=always"},
22973+ {Opt_diropq_a, "diropq=a"},
22974+ {Opt_diropq_w, "diropq=whiteouted"},
22975+ {Opt_diropq_w, "diropq=w"},
22976+
22977+ {Opt_warn_perm, "warn_perm"},
22978+ {Opt_nowarn_perm, "nowarn_perm"},
22979+
22980+ /* keep them temporary */
1facf9fc 22981+ {Opt_ignore_silent, "nodlgt"},
1facf9fc 22982+ {Opt_ignore_silent, "clean_plink"},
22983+
dece6358
AM
22984+#ifdef CONFIG_AUFS_SHWH
22985+ {Opt_shwh, "shwh"},
22986+#endif
22987+ {Opt_noshwh, "noshwh"},
22988+
076b876e
AM
22989+ {Opt_dirperm1, "dirperm1"},
22990+ {Opt_nodirperm1, "nodirperm1"},
22991+
1facf9fc 22992+ {Opt_verbose, "verbose"},
22993+ {Opt_verbose, "v"},
22994+ {Opt_noverbose, "noverbose"},
22995+ {Opt_noverbose, "quiet"},
22996+ {Opt_noverbose, "q"},
22997+ {Opt_noverbose, "silent"},
22998+
22999+ {Opt_sum, "sum"},
23000+ {Opt_nosum, "nosum"},
23001+ {Opt_wsum, "wsum"},
23002+
23003+ {Opt_rdcache, "rdcache=%d"},
23004+ {Opt_rdblk, "rdblk=%d"},
dece6358 23005+ {Opt_rdblk_def, "rdblk=def"},
1facf9fc 23006+ {Opt_rdhash, "rdhash=%d"},
dece6358 23007+ {Opt_rdhash_def, "rdhash=def"},
1facf9fc 23008+
23009+ {Opt_wbr_create, "create=%s"},
23010+ {Opt_wbr_create, "create_policy=%s"},
23011+ {Opt_wbr_copyup, "cpup=%s"},
23012+ {Opt_wbr_copyup, "copyup=%s"},
23013+ {Opt_wbr_copyup, "copyup_policy=%s"},
23014+
c1595e42
JR
23015+ /* generic VFS flag */
23016+#ifdef CONFIG_FS_POSIX_ACL
23017+ {Opt_acl, "acl"},
23018+ {Opt_noacl, "noacl"},
23019+#else
23020+ {Opt_ignore_silent, "acl"},
23021+ {Opt_ignore_silent, "noacl"},
23022+#endif
23023+
1facf9fc 23024+ /* internal use for the scripts */
23025+ {Opt_ignore_silent, "si=%s"},
23026+
23027+ {Opt_br, "dirs=%s"},
23028+ {Opt_ignore, "debug=%d"},
23029+ {Opt_ignore, "delete=whiteout"},
23030+ {Opt_ignore, "delete=all"},
23031+ {Opt_ignore, "imap=%s"},
23032+
1308ab2a 23033+ /* temporary workaround, due to old mount(8)? */
23034+ {Opt_ignore_silent, "relatime"},
23035+
1facf9fc 23036+ {Opt_err, NULL}
23037+};
23038+
23039+/* ---------------------------------------------------------------------- */
23040+
076b876e 23041+static const char *au_parser_pattern(int val, match_table_t tbl)
1facf9fc 23042+{
076b876e
AM
23043+ struct match_token *p;
23044+
23045+ p = tbl;
23046+ while (p->pattern) {
23047+ if (p->token == val)
23048+ return p->pattern;
23049+ p++;
1facf9fc 23050+ }
23051+ BUG();
23052+ return "??";
23053+}
23054+
076b876e
AM
23055+static const char *au_optstr(int *val, match_table_t tbl)
23056+{
23057+ struct match_token *p;
23058+ int v;
23059+
23060+ v = *val;
2000de60
JR
23061+ if (!v)
23062+ goto out;
076b876e 23063+ p = tbl;
2000de60
JR
23064+ while (p->pattern) {
23065+ if (p->token
23066+ && (v & p->token) == p->token) {
076b876e
AM
23067+ *val &= ~p->token;
23068+ return p->pattern;
23069+ }
23070+ p++;
23071+ }
2000de60
JR
23072+
23073+out:
076b876e
AM
23074+ return NULL;
23075+}
23076+
1facf9fc 23077+/* ---------------------------------------------------------------------- */
23078+
1e00d052 23079+static match_table_t brperm = {
1facf9fc 23080+ {AuBrPerm_RO, AUFS_BRPERM_RO},
23081+ {AuBrPerm_RR, AUFS_BRPERM_RR},
23082+ {AuBrPerm_RW, AUFS_BRPERM_RW},
1e00d052
AM
23083+ {0, NULL}
23084+};
1facf9fc 23085+
86dc4139 23086+static match_table_t brattr = {
076b876e
AM
23087+ /* general */
23088+ {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
23089+ {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
c1595e42 23090+ /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
86dc4139 23091+ {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
2000de60 23092+#ifdef CONFIG_AUFS_FHSM
076b876e 23093+ {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
2000de60
JR
23094+#endif
23095+#ifdef CONFIG_AUFS_XATTR
c1595e42
JR
23096+ {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
23097+ {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
23098+ {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
23099+ {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
23100+ {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
23101+ {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
2000de60 23102+#endif
076b876e
AM
23103+
23104+ /* ro/rr branch */
1e00d052 23105+ {AuBrRAttr_WH, AUFS_BRRATTR_WH},
076b876e
AM
23106+
23107+ /* rw branch */
23108+ {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
1e00d052 23109+ {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
076b876e 23110+
1e00d052 23111+ {0, NULL}
1facf9fc 23112+};
23113+
1e00d052
AM
23114+static int br_attr_val(char *str, match_table_t table, substring_t args[])
23115+{
23116+ int attr, v;
23117+ char *p;
23118+
23119+ attr = 0;
23120+ do {
23121+ p = strchr(str, '+');
23122+ if (p)
23123+ *p = 0;
23124+ v = match_token(str, table, args);
076b876e
AM
23125+ if (v) {
23126+ if (v & AuBrAttr_CMOO_Mask)
23127+ attr &= ~AuBrAttr_CMOO_Mask;
1e00d052 23128+ attr |= v;
076b876e 23129+ } else {
1e00d052
AM
23130+ if (p)
23131+ *p = '+';
0c3ec466 23132+ pr_warn("ignored branch attribute %s\n", str);
1e00d052
AM
23133+ break;
23134+ }
23135+ if (p)
23136+ str = p + 1;
23137+ } while (p);
23138+
23139+ return attr;
23140+}
23141+
076b876e
AM
23142+static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
23143+{
23144+ int sz;
23145+ const char *p;
23146+ char *q;
23147+
076b876e
AM
23148+ q = str->a;
23149+ *q = 0;
23150+ p = au_optstr(&perm, brattr);
23151+ if (p) {
23152+ sz = strlen(p);
23153+ memcpy(q, p, sz + 1);
23154+ q += sz;
23155+ } else
23156+ goto out;
23157+
23158+ do {
23159+ p = au_optstr(&perm, brattr);
23160+ if (p) {
23161+ *q++ = '+';
23162+ sz = strlen(p);
23163+ memcpy(q, p, sz + 1);
23164+ q += sz;
23165+ }
23166+ } while (p);
23167+
23168+out:
c1595e42 23169+ return q - str->a;
076b876e
AM
23170+}
23171+
4a4d8108 23172+static int noinline_for_stack br_perm_val(char *perm)
1facf9fc 23173+{
076b876e
AM
23174+ int val, bad, sz;
23175+ char *p;
1facf9fc 23176+ substring_t args[MAX_OPT_ARGS];
076b876e 23177+ au_br_perm_str_t attr;
1facf9fc 23178+
1e00d052
AM
23179+ p = strchr(perm, '+');
23180+ if (p)
23181+ *p = 0;
23182+ val = match_token(perm, brperm, args);
23183+ if (!val) {
23184+ if (p)
23185+ *p = '+';
0c3ec466 23186+ pr_warn("ignored branch permission %s\n", perm);
1e00d052
AM
23187+ val = AuBrPerm_RO;
23188+ goto out;
23189+ }
23190+ if (!p)
23191+ goto out;
23192+
076b876e
AM
23193+ val |= br_attr_val(p + 1, brattr, args);
23194+
23195+ bad = 0;
86dc4139 23196+ switch (val & AuBrPerm_Mask) {
1e00d052
AM
23197+ case AuBrPerm_RO:
23198+ case AuBrPerm_RR:
076b876e
AM
23199+ bad = val & AuBrWAttr_Mask;
23200+ val &= ~AuBrWAttr_Mask;
1e00d052
AM
23201+ break;
23202+ case AuBrPerm_RW:
076b876e
AM
23203+ bad = val & AuBrRAttr_Mask;
23204+ val &= ~AuBrRAttr_Mask;
1e00d052
AM
23205+ break;
23206+ }
c1595e42
JR
23207+
23208+ /*
23209+ * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
23210+ * does not treat it as an error, just warning.
23211+ * this is a tiny guard for the user operation.
23212+ */
23213+ if (val & AuBrAttr_UNPIN) {
23214+ bad |= AuBrAttr_UNPIN;
23215+ val &= ~AuBrAttr_UNPIN;
23216+ }
23217+
076b876e
AM
23218+ if (unlikely(bad)) {
23219+ sz = au_do_optstr_br_attr(&attr, bad);
23220+ AuDebugOn(!sz);
23221+ pr_warn("ignored branch attribute %s\n", attr.a);
23222+ }
1e00d052
AM
23223+
23224+out:
1facf9fc 23225+ return val;
23226+}
23227+
076b876e 23228+void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
1facf9fc 23229+{
076b876e
AM
23230+ au_br_perm_str_t attr;
23231+ const char *p;
23232+ char *q;
1e00d052
AM
23233+ int sz;
23234+
076b876e
AM
23235+ q = str->a;
23236+ p = au_optstr(&perm, brperm);
23237+ AuDebugOn(!p || !*p);
23238+ sz = strlen(p);
23239+ memcpy(q, p, sz + 1);
23240+ q += sz;
1e00d052 23241+
076b876e
AM
23242+ sz = au_do_optstr_br_attr(&attr, perm);
23243+ if (sz) {
23244+ *q++ = '+';
23245+ memcpy(q, attr.a, sz + 1);
1e00d052
AM
23246+ }
23247+
076b876e 23248+ AuDebugOn(strlen(str->a) >= sizeof(str->a));
1facf9fc 23249+}
23250+
23251+/* ---------------------------------------------------------------------- */
23252+
23253+static match_table_t udbalevel = {
23254+ {AuOpt_UDBA_REVAL, "reval"},
23255+ {AuOpt_UDBA_NONE, "none"},
4a4d8108
AM
23256+#ifdef CONFIG_AUFS_HNOTIFY
23257+ {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
23258+#ifdef CONFIG_AUFS_HFSNOTIFY
23259+ {AuOpt_UDBA_HNOTIFY, "fsnotify"},
4a4d8108 23260+#endif
1facf9fc 23261+#endif
23262+ {-1, NULL}
23263+};
23264+
4a4d8108 23265+static int noinline_for_stack udba_val(char *str)
1facf9fc 23266+{
23267+ substring_t args[MAX_OPT_ARGS];
23268+
7f207e10 23269+ return match_token(str, udbalevel, args);
1facf9fc 23270+}
23271+
23272+const char *au_optstr_udba(int udba)
23273+{
076b876e 23274+ return au_parser_pattern(udba, udbalevel);
1facf9fc 23275+}
23276+
23277+/* ---------------------------------------------------------------------- */
23278+
23279+static match_table_t au_wbr_create_policy = {
23280+ {AuWbrCreate_TDP, "tdp"},
23281+ {AuWbrCreate_TDP, "top-down-parent"},
23282+ {AuWbrCreate_RR, "rr"},
23283+ {AuWbrCreate_RR, "round-robin"},
23284+ {AuWbrCreate_MFS, "mfs"},
23285+ {AuWbrCreate_MFS, "most-free-space"},
23286+ {AuWbrCreate_MFSV, "mfs:%d"},
23287+ {AuWbrCreate_MFSV, "most-free-space:%d"},
23288+
23289+ {AuWbrCreate_MFSRR, "mfsrr:%d"},
23290+ {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
23291+ {AuWbrCreate_PMFS, "pmfs"},
23292+ {AuWbrCreate_PMFSV, "pmfs:%d"},
392086de
AM
23293+ {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
23294+ {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
1facf9fc 23295+
23296+ {-1, NULL}
23297+};
23298+
dece6358
AM
23299+/*
23300+ * cf. linux/lib/parser.c and cmdline.c
23301+ * gave up calling memparse() since it uses simple_strtoull() instead of
9dbd164d 23302+ * kstrto...().
dece6358 23303+ */
4a4d8108
AM
23304+static int noinline_for_stack
23305+au_match_ull(substring_t *s, unsigned long long *result)
1facf9fc 23306+{
23307+ int err;
23308+ unsigned int len;
23309+ char a[32];
23310+
23311+ err = -ERANGE;
23312+ len = s->to - s->from;
23313+ if (len + 1 <= sizeof(a)) {
23314+ memcpy(a, s->from, len);
23315+ a[len] = '\0';
9dbd164d 23316+ err = kstrtoull(a, 0, result);
1facf9fc 23317+ }
23318+ return err;
23319+}
23320+
23321+static int au_wbr_mfs_wmark(substring_t *arg, char *str,
23322+ struct au_opt_wbr_create *create)
23323+{
23324+ int err;
23325+ unsigned long long ull;
23326+
23327+ err = 0;
23328+ if (!au_match_ull(arg, &ull))
23329+ create->mfsrr_watermark = ull;
23330+ else {
4a4d8108 23331+ pr_err("bad integer in %s\n", str);
1facf9fc 23332+ err = -EINVAL;
23333+ }
23334+
23335+ return err;
23336+}
23337+
23338+static int au_wbr_mfs_sec(substring_t *arg, char *str,
23339+ struct au_opt_wbr_create *create)
23340+{
23341+ int n, err;
23342+
23343+ err = 0;
027c5e7a 23344+ if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
1facf9fc 23345+ create->mfs_second = n;
23346+ else {
4a4d8108 23347+ pr_err("bad integer in %s\n", str);
1facf9fc 23348+ err = -EINVAL;
23349+ }
23350+
23351+ return err;
23352+}
23353+
4a4d8108
AM
23354+static int noinline_for_stack
23355+au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
1facf9fc 23356+{
23357+ int err, e;
23358+ substring_t args[MAX_OPT_ARGS];
23359+
23360+ err = match_token(str, au_wbr_create_policy, args);
23361+ create->wbr_create = err;
23362+ switch (err) {
23363+ case AuWbrCreate_MFSRRV:
392086de 23364+ case AuWbrCreate_PMFSRRV:
1facf9fc 23365+ e = au_wbr_mfs_wmark(&args[0], str, create);
23366+ if (!e)
23367+ e = au_wbr_mfs_sec(&args[1], str, create);
23368+ if (unlikely(e))
23369+ err = e;
23370+ break;
23371+ case AuWbrCreate_MFSRR:
392086de 23372+ case AuWbrCreate_PMFSRR:
1facf9fc 23373+ e = au_wbr_mfs_wmark(&args[0], str, create);
23374+ if (unlikely(e)) {
23375+ err = e;
23376+ break;
23377+ }
23378+ /*FALLTHROUGH*/
23379+ case AuWbrCreate_MFS:
23380+ case AuWbrCreate_PMFS:
027c5e7a 23381+ create->mfs_second = AUFS_MFS_DEF_SEC;
1facf9fc 23382+ break;
23383+ case AuWbrCreate_MFSV:
23384+ case AuWbrCreate_PMFSV:
23385+ e = au_wbr_mfs_sec(&args[0], str, create);
23386+ if (unlikely(e))
23387+ err = e;
23388+ break;
23389+ }
23390+
23391+ return err;
23392+}
23393+
23394+const char *au_optstr_wbr_create(int wbr_create)
23395+{
076b876e 23396+ return au_parser_pattern(wbr_create, au_wbr_create_policy);
1facf9fc 23397+}
23398+
23399+static match_table_t au_wbr_copyup_policy = {
23400+ {AuWbrCopyup_TDP, "tdp"},
23401+ {AuWbrCopyup_TDP, "top-down-parent"},
23402+ {AuWbrCopyup_BUP, "bup"},
23403+ {AuWbrCopyup_BUP, "bottom-up-parent"},
23404+ {AuWbrCopyup_BU, "bu"},
23405+ {AuWbrCopyup_BU, "bottom-up"},
23406+ {-1, NULL}
23407+};
23408+
4a4d8108 23409+static int noinline_for_stack au_wbr_copyup_val(char *str)
1facf9fc 23410+{
23411+ substring_t args[MAX_OPT_ARGS];
23412+
23413+ return match_token(str, au_wbr_copyup_policy, args);
23414+}
23415+
23416+const char *au_optstr_wbr_copyup(int wbr_copyup)
23417+{
076b876e 23418+ return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
1facf9fc 23419+}
23420+
23421+/* ---------------------------------------------------------------------- */
23422+
23423+static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
23424+
23425+static void dump_opts(struct au_opts *opts)
23426+{
23427+#ifdef CONFIG_AUFS_DEBUG
23428+ /* reduce stack space */
23429+ union {
23430+ struct au_opt_add *add;
23431+ struct au_opt_del *del;
23432+ struct au_opt_mod *mod;
23433+ struct au_opt_xino *xino;
23434+ struct au_opt_xino_itrunc *xino_itrunc;
23435+ struct au_opt_wbr_create *create;
23436+ } u;
23437+ struct au_opt *opt;
23438+
23439+ opt = opts->opt;
23440+ while (opt->type != Opt_tail) {
23441+ switch (opt->type) {
23442+ case Opt_add:
23443+ u.add = &opt->add;
23444+ AuDbg("add {b%d, %s, 0x%x, %p}\n",
23445+ u.add->bindex, u.add->pathname, u.add->perm,
23446+ u.add->path.dentry);
23447+ break;
23448+ case Opt_del:
23449+ case Opt_idel:
23450+ u.del = &opt->del;
23451+ AuDbg("del {%s, %p}\n",
23452+ u.del->pathname, u.del->h_path.dentry);
23453+ break;
23454+ case Opt_mod:
23455+ case Opt_imod:
23456+ u.mod = &opt->mod;
23457+ AuDbg("mod {%s, 0x%x, %p}\n",
23458+ u.mod->path, u.mod->perm, u.mod->h_root);
23459+ break;
23460+ case Opt_append:
23461+ u.add = &opt->add;
23462+ AuDbg("append {b%d, %s, 0x%x, %p}\n",
23463+ u.add->bindex, u.add->pathname, u.add->perm,
23464+ u.add->path.dentry);
23465+ break;
23466+ case Opt_prepend:
23467+ u.add = &opt->add;
23468+ AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
23469+ u.add->bindex, u.add->pathname, u.add->perm,
23470+ u.add->path.dentry);
23471+ break;
23472+ case Opt_dirwh:
23473+ AuDbg("dirwh %d\n", opt->dirwh);
23474+ break;
23475+ case Opt_rdcache:
23476+ AuDbg("rdcache %d\n", opt->rdcache);
23477+ break;
23478+ case Opt_rdblk:
23479+ AuDbg("rdblk %u\n", opt->rdblk);
23480+ break;
dece6358
AM
23481+ case Opt_rdblk_def:
23482+ AuDbg("rdblk_def\n");
23483+ break;
1facf9fc 23484+ case Opt_rdhash:
23485+ AuDbg("rdhash %u\n", opt->rdhash);
23486+ break;
dece6358
AM
23487+ case Opt_rdhash_def:
23488+ AuDbg("rdhash_def\n");
23489+ break;
1facf9fc 23490+ case Opt_xino:
23491+ u.xino = &opt->xino;
523b37e3 23492+ AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
1facf9fc 23493+ break;
23494+ case Opt_trunc_xino:
23495+ AuLabel(trunc_xino);
23496+ break;
23497+ case Opt_notrunc_xino:
23498+ AuLabel(notrunc_xino);
23499+ break;
23500+ case Opt_trunc_xino_path:
23501+ case Opt_itrunc_xino:
23502+ u.xino_itrunc = &opt->xino_itrunc;
23503+ AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
23504+ break;
1facf9fc 23505+ case Opt_noxino:
23506+ AuLabel(noxino);
23507+ break;
23508+ case Opt_trunc_xib:
23509+ AuLabel(trunc_xib);
23510+ break;
23511+ case Opt_notrunc_xib:
23512+ AuLabel(notrunc_xib);
23513+ break;
dece6358
AM
23514+ case Opt_shwh:
23515+ AuLabel(shwh);
23516+ break;
23517+ case Opt_noshwh:
23518+ AuLabel(noshwh);
23519+ break;
076b876e
AM
23520+ case Opt_dirperm1:
23521+ AuLabel(dirperm1);
23522+ break;
23523+ case Opt_nodirperm1:
23524+ AuLabel(nodirperm1);
23525+ break;
1facf9fc 23526+ case Opt_plink:
23527+ AuLabel(plink);
23528+ break;
23529+ case Opt_noplink:
23530+ AuLabel(noplink);
23531+ break;
23532+ case Opt_list_plink:
23533+ AuLabel(list_plink);
23534+ break;
23535+ case Opt_udba:
23536+ AuDbg("udba %d, %s\n",
23537+ opt->udba, au_optstr_udba(opt->udba));
23538+ break;
4a4d8108
AM
23539+ case Opt_dio:
23540+ AuLabel(dio);
23541+ break;
23542+ case Opt_nodio:
23543+ AuLabel(nodio);
23544+ break;
1facf9fc 23545+ case Opt_diropq_a:
23546+ AuLabel(diropq_a);
23547+ break;
23548+ case Opt_diropq_w:
23549+ AuLabel(diropq_w);
23550+ break;
23551+ case Opt_warn_perm:
23552+ AuLabel(warn_perm);
23553+ break;
23554+ case Opt_nowarn_perm:
23555+ AuLabel(nowarn_perm);
23556+ break;
1facf9fc 23557+ case Opt_verbose:
23558+ AuLabel(verbose);
23559+ break;
23560+ case Opt_noverbose:
23561+ AuLabel(noverbose);
23562+ break;
23563+ case Opt_sum:
23564+ AuLabel(sum);
23565+ break;
23566+ case Opt_nosum:
23567+ AuLabel(nosum);
23568+ break;
23569+ case Opt_wsum:
23570+ AuLabel(wsum);
23571+ break;
23572+ case Opt_wbr_create:
23573+ u.create = &opt->wbr_create;
23574+ AuDbg("create %d, %s\n", u.create->wbr_create,
23575+ au_optstr_wbr_create(u.create->wbr_create));
23576+ switch (u.create->wbr_create) {
23577+ case AuWbrCreate_MFSV:
23578+ case AuWbrCreate_PMFSV:
23579+ AuDbg("%d sec\n", u.create->mfs_second);
23580+ break;
23581+ case AuWbrCreate_MFSRR:
23582+ AuDbg("%llu watermark\n",
23583+ u.create->mfsrr_watermark);
23584+ break;
23585+ case AuWbrCreate_MFSRRV:
392086de 23586+ case AuWbrCreate_PMFSRRV:
1facf9fc 23587+ AuDbg("%llu watermark, %d sec\n",
23588+ u.create->mfsrr_watermark,
23589+ u.create->mfs_second);
23590+ break;
23591+ }
23592+ break;
23593+ case Opt_wbr_copyup:
23594+ AuDbg("copyup %d, %s\n", opt->wbr_copyup,
23595+ au_optstr_wbr_copyup(opt->wbr_copyup));
23596+ break;
076b876e
AM
23597+ case Opt_fhsm_sec:
23598+ AuDbg("fhsm_sec %u\n", opt->fhsm_second);
23599+ break;
c1595e42
JR
23600+ case Opt_acl:
23601+ AuLabel(acl);
23602+ break;
23603+ case Opt_noacl:
23604+ AuLabel(noacl);
23605+ break;
1facf9fc 23606+ default:
23607+ BUG();
23608+ }
23609+ opt++;
23610+ }
23611+#endif
23612+}
23613+
23614+void au_opts_free(struct au_opts *opts)
23615+{
23616+ struct au_opt *opt;
23617+
23618+ opt = opts->opt;
23619+ while (opt->type != Opt_tail) {
23620+ switch (opt->type) {
23621+ case Opt_add:
23622+ case Opt_append:
23623+ case Opt_prepend:
23624+ path_put(&opt->add.path);
23625+ break;
23626+ case Opt_del:
23627+ case Opt_idel:
23628+ path_put(&opt->del.h_path);
23629+ break;
23630+ case Opt_mod:
23631+ case Opt_imod:
23632+ dput(opt->mod.h_root);
23633+ break;
23634+ case Opt_xino:
23635+ fput(opt->xino.file);
23636+ break;
23637+ }
23638+ opt++;
23639+ }
23640+}
23641+
23642+static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
23643+ aufs_bindex_t bindex)
23644+{
23645+ int err;
23646+ struct au_opt_add *add = &opt->add;
23647+ char *p;
23648+
23649+ add->bindex = bindex;
1e00d052 23650+ add->perm = AuBrPerm_RO;
1facf9fc 23651+ add->pathname = opt_str;
23652+ p = strchr(opt_str, '=');
23653+ if (p) {
23654+ *p++ = 0;
23655+ if (*p)
23656+ add->perm = br_perm_val(p);
23657+ }
23658+
23659+ err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
23660+ if (!err) {
23661+ if (!p) {
23662+ add->perm = AuBrPerm_RO;
23663+ if (au_test_fs_rr(add->path.dentry->d_sb))
23664+ add->perm = AuBrPerm_RR;
23665+ else if (!bindex && !(sb_flags & MS_RDONLY))
23666+ add->perm = AuBrPerm_RW;
23667+ }
23668+ opt->type = Opt_add;
23669+ goto out;
23670+ }
4a4d8108 23671+ pr_err("lookup failed %s (%d)\n", add->pathname, err);
1facf9fc 23672+ err = -EINVAL;
23673+
4f0767ce 23674+out:
1facf9fc 23675+ return err;
23676+}
23677+
23678+static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
23679+{
23680+ int err;
23681+
23682+ del->pathname = args[0].from;
23683+ AuDbg("del path %s\n", del->pathname);
23684+
23685+ err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
23686+ if (unlikely(err))
4a4d8108 23687+ pr_err("lookup failed %s (%d)\n", del->pathname, err);
1facf9fc 23688+
23689+ return err;
23690+}
23691+
23692+#if 0 /* reserved for future use */
23693+static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
23694+ struct au_opt_del *del, substring_t args[])
23695+{
23696+ int err;
23697+ struct dentry *root;
23698+
23699+ err = -EINVAL;
23700+ root = sb->s_root;
23701+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 23702+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 23703+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 23704+ goto out;
23705+ }
23706+
23707+ err = 0;
23708+ del->h_path.dentry = dget(au_h_dptr(root, bindex));
23709+ del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
23710+
4f0767ce 23711+out:
1facf9fc 23712+ aufs_read_unlock(root, !AuLock_IR);
23713+ return err;
23714+}
23715+#endif
23716+
4a4d8108
AM
23717+static int noinline_for_stack
23718+au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
1facf9fc 23719+{
23720+ int err;
23721+ struct path path;
23722+ char *p;
23723+
23724+ err = -EINVAL;
23725+ mod->path = args[0].from;
23726+ p = strchr(mod->path, '=');
23727+ if (unlikely(!p)) {
4a4d8108 23728+ pr_err("no permssion %s\n", args[0].from);
1facf9fc 23729+ goto out;
23730+ }
23731+
23732+ *p++ = 0;
23733+ err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
23734+ if (unlikely(err)) {
4a4d8108 23735+ pr_err("lookup failed %s (%d)\n", mod->path, err);
1facf9fc 23736+ goto out;
23737+ }
23738+
23739+ mod->perm = br_perm_val(p);
23740+ AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
23741+ mod->h_root = dget(path.dentry);
23742+ path_put(&path);
23743+
4f0767ce 23744+out:
1facf9fc 23745+ return err;
23746+}
23747+
23748+#if 0 /* reserved for future use */
23749+static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
23750+ struct au_opt_mod *mod, substring_t args[])
23751+{
23752+ int err;
23753+ struct dentry *root;
23754+
23755+ err = -EINVAL;
23756+ root = sb->s_root;
23757+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 23758+ if (bindex < 0 || au_sbbot(sb) < bindex) {
4a4d8108 23759+ pr_err("out of bounds, %d\n", bindex);
1facf9fc 23760+ goto out;
23761+ }
23762+
23763+ err = 0;
23764+ mod->perm = br_perm_val(args[1].from);
23765+ AuDbg("mod path %s, perm 0x%x, %s\n",
23766+ mod->path, mod->perm, args[1].from);
23767+ mod->h_root = dget(au_h_dptr(root, bindex));
23768+
4f0767ce 23769+out:
1facf9fc 23770+ aufs_read_unlock(root, !AuLock_IR);
23771+ return err;
23772+}
23773+#endif
23774+
23775+static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
23776+ substring_t args[])
23777+{
23778+ int err;
23779+ struct file *file;
23780+
23781+ file = au_xino_create(sb, args[0].from, /*silent*/0);
23782+ err = PTR_ERR(file);
23783+ if (IS_ERR(file))
23784+ goto out;
23785+
23786+ err = -EINVAL;
2000de60 23787+ if (unlikely(file->f_path.dentry->d_sb == sb)) {
1facf9fc 23788+ fput(file);
4a4d8108 23789+ pr_err("%s must be outside\n", args[0].from);
1facf9fc 23790+ goto out;
23791+ }
23792+
23793+ err = 0;
23794+ xino->file = file;
23795+ xino->path = args[0].from;
23796+
4f0767ce 23797+out:
1facf9fc 23798+ return err;
23799+}
23800+
4a4d8108
AM
23801+static int noinline_for_stack
23802+au_opts_parse_xino_itrunc_path(struct super_block *sb,
23803+ struct au_opt_xino_itrunc *xino_itrunc,
23804+ substring_t args[])
1facf9fc 23805+{
23806+ int err;
5afbbe0d 23807+ aufs_bindex_t bbot, bindex;
1facf9fc 23808+ struct path path;
23809+ struct dentry *root;
23810+
23811+ err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
23812+ if (unlikely(err)) {
4a4d8108 23813+ pr_err("lookup failed %s (%d)\n", args[0].from, err);
1facf9fc 23814+ goto out;
23815+ }
23816+
23817+ xino_itrunc->bindex = -1;
23818+ root = sb->s_root;
23819+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d
AM
23820+ bbot = au_sbbot(sb);
23821+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 23822+ if (au_h_dptr(root, bindex) == path.dentry) {
23823+ xino_itrunc->bindex = bindex;
23824+ break;
23825+ }
23826+ }
23827+ aufs_read_unlock(root, !AuLock_IR);
23828+ path_put(&path);
23829+
23830+ if (unlikely(xino_itrunc->bindex < 0)) {
4a4d8108 23831+ pr_err("no such branch %s\n", args[0].from);
1facf9fc 23832+ err = -EINVAL;
23833+ }
23834+
4f0767ce 23835+out:
1facf9fc 23836+ return err;
23837+}
23838+
23839+/* called without aufs lock */
23840+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
23841+{
23842+ int err, n, token;
23843+ aufs_bindex_t bindex;
23844+ unsigned char skipped;
23845+ struct dentry *root;
23846+ struct au_opt *opt, *opt_tail;
23847+ char *opt_str;
23848+ /* reduce the stack space */
23849+ union {
23850+ struct au_opt_xino_itrunc *xino_itrunc;
23851+ struct au_opt_wbr_create *create;
23852+ } u;
23853+ struct {
23854+ substring_t args[MAX_OPT_ARGS];
23855+ } *a;
23856+
23857+ err = -ENOMEM;
23858+ a = kmalloc(sizeof(*a), GFP_NOFS);
23859+ if (unlikely(!a))
23860+ goto out;
23861+
23862+ root = sb->s_root;
23863+ err = 0;
23864+ bindex = 0;
23865+ opt = opts->opt;
23866+ opt_tail = opt + opts->max_opt - 1;
23867+ opt->type = Opt_tail;
23868+ while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
23869+ err = -EINVAL;
23870+ skipped = 0;
23871+ token = match_token(opt_str, options, a->args);
23872+ switch (token) {
23873+ case Opt_br:
23874+ err = 0;
23875+ while (!err && (opt_str = strsep(&a->args[0].from, ":"))
23876+ && *opt_str) {
23877+ err = opt_add(opt, opt_str, opts->sb_flags,
23878+ bindex++);
23879+ if (unlikely(!err && ++opt > opt_tail)) {
23880+ err = -E2BIG;
23881+ break;
23882+ }
23883+ opt->type = Opt_tail;
23884+ skipped = 1;
23885+ }
23886+ break;
23887+ case Opt_add:
23888+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 23889+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 23890+ break;
23891+ }
23892+ bindex = n;
23893+ err = opt_add(opt, a->args[1].from, opts->sb_flags,
23894+ bindex);
23895+ if (!err)
23896+ opt->type = token;
23897+ break;
23898+ case Opt_append:
23899+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
23900+ /*dummy bindex*/1);
23901+ if (!err)
23902+ opt->type = token;
23903+ break;
23904+ case Opt_prepend:
23905+ err = opt_add(opt, a->args[0].from, opts->sb_flags,
23906+ /*bindex*/0);
23907+ if (!err)
23908+ opt->type = token;
23909+ break;
23910+ case Opt_del:
23911+ err = au_opts_parse_del(&opt->del, a->args);
23912+ if (!err)
23913+ opt->type = token;
23914+ break;
23915+#if 0 /* reserved for future use */
23916+ case Opt_idel:
23917+ del->pathname = "(indexed)";
23918+ if (unlikely(match_int(&args[0], &n))) {
4a4d8108 23919+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 23920+ break;
23921+ }
23922+ err = au_opts_parse_idel(sb, n, &opt->del, a->args);
23923+ if (!err)
23924+ opt->type = token;
23925+ break;
23926+#endif
23927+ case Opt_mod:
23928+ err = au_opts_parse_mod(&opt->mod, a->args);
23929+ if (!err)
23930+ opt->type = token;
23931+ break;
23932+#ifdef IMOD /* reserved for future use */
23933+ case Opt_imod:
23934+ u.mod->path = "(indexed)";
23935+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 23936+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 23937+ break;
23938+ }
23939+ err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
23940+ if (!err)
23941+ opt->type = token;
23942+ break;
23943+#endif
23944+ case Opt_xino:
23945+ err = au_opts_parse_xino(sb, &opt->xino, a->args);
23946+ if (!err)
23947+ opt->type = token;
23948+ break;
23949+
23950+ case Opt_trunc_xino_path:
23951+ err = au_opts_parse_xino_itrunc_path
23952+ (sb, &opt->xino_itrunc, a->args);
23953+ if (!err)
23954+ opt->type = token;
23955+ break;
23956+
23957+ case Opt_itrunc_xino:
23958+ u.xino_itrunc = &opt->xino_itrunc;
23959+ if (unlikely(match_int(&a->args[0], &n))) {
4a4d8108 23960+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 23961+ break;
23962+ }
23963+ u.xino_itrunc->bindex = n;
23964+ aufs_read_lock(root, AuLock_FLUSH);
5afbbe0d 23965+ if (n < 0 || au_sbbot(sb) < n) {
4a4d8108 23966+ pr_err("out of bounds, %d\n", n);
1facf9fc 23967+ aufs_read_unlock(root, !AuLock_IR);
23968+ break;
23969+ }
23970+ aufs_read_unlock(root, !AuLock_IR);
23971+ err = 0;
23972+ opt->type = token;
23973+ break;
23974+
23975+ case Opt_dirwh:
23976+ if (unlikely(match_int(&a->args[0], &opt->dirwh)))
23977+ break;
23978+ err = 0;
23979+ opt->type = token;
23980+ break;
23981+
23982+ case Opt_rdcache:
027c5e7a
AM
23983+ if (unlikely(match_int(&a->args[0], &n))) {
23984+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 23985+ break;
027c5e7a
AM
23986+ }
23987+ if (unlikely(n > AUFS_RDCACHE_MAX)) {
23988+ pr_err("rdcache must be smaller than %d\n",
23989+ AUFS_RDCACHE_MAX);
23990+ break;
23991+ }
23992+ opt->rdcache = n;
1facf9fc 23993+ err = 0;
23994+ opt->type = token;
23995+ break;
23996+ case Opt_rdblk:
23997+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 23998+ || n < 0
1facf9fc 23999+ || n > KMALLOC_MAX_SIZE)) {
4a4d8108 24000+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24001+ break;
24002+ }
1308ab2a 24003+ if (unlikely(n && n < NAME_MAX)) {
4a4d8108
AM
24004+ pr_err("rdblk must be larger than %d\n",
24005+ NAME_MAX);
1facf9fc 24006+ break;
24007+ }
24008+ opt->rdblk = n;
24009+ err = 0;
24010+ opt->type = token;
24011+ break;
24012+ case Opt_rdhash:
24013+ if (unlikely(match_int(&a->args[0], &n)
1308ab2a 24014+ || n < 0
1facf9fc 24015+ || n * sizeof(struct hlist_head)
24016+ > KMALLOC_MAX_SIZE)) {
4a4d8108 24017+ pr_err("bad integer in %s\n", opt_str);
1facf9fc 24018+ break;
24019+ }
24020+ opt->rdhash = n;
24021+ err = 0;
24022+ opt->type = token;
24023+ break;
24024+
24025+ case Opt_trunc_xino:
24026+ case Opt_notrunc_xino:
24027+ case Opt_noxino:
24028+ case Opt_trunc_xib:
24029+ case Opt_notrunc_xib:
dece6358
AM
24030+ case Opt_shwh:
24031+ case Opt_noshwh:
076b876e
AM
24032+ case Opt_dirperm1:
24033+ case Opt_nodirperm1:
1facf9fc 24034+ case Opt_plink:
24035+ case Opt_noplink:
24036+ case Opt_list_plink:
4a4d8108
AM
24037+ case Opt_dio:
24038+ case Opt_nodio:
1facf9fc 24039+ case Opt_diropq_a:
24040+ case Opt_diropq_w:
24041+ case Opt_warn_perm:
24042+ case Opt_nowarn_perm:
1facf9fc 24043+ case Opt_verbose:
24044+ case Opt_noverbose:
24045+ case Opt_sum:
24046+ case Opt_nosum:
24047+ case Opt_wsum:
dece6358
AM
24048+ case Opt_rdblk_def:
24049+ case Opt_rdhash_def:
c1595e42
JR
24050+ case Opt_acl:
24051+ case Opt_noacl:
1facf9fc 24052+ err = 0;
24053+ opt->type = token;
24054+ break;
24055+
24056+ case Opt_udba:
24057+ opt->udba = udba_val(a->args[0].from);
24058+ if (opt->udba >= 0) {
24059+ err = 0;
24060+ opt->type = token;
24061+ } else
4a4d8108 24062+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 24063+ break;
24064+
24065+ case Opt_wbr_create:
24066+ u.create = &opt->wbr_create;
24067+ u.create->wbr_create
24068+ = au_wbr_create_val(a->args[0].from, u.create);
24069+ if (u.create->wbr_create >= 0) {
24070+ err = 0;
24071+ opt->type = token;
24072+ } else
4a4d8108 24073+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 24074+ break;
24075+ case Opt_wbr_copyup:
24076+ opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
24077+ if (opt->wbr_copyup >= 0) {
24078+ err = 0;
24079+ opt->type = token;
24080+ } else
4a4d8108 24081+ pr_err("wrong value, %s\n", opt_str);
1facf9fc 24082+ break;
24083+
076b876e
AM
24084+ case Opt_fhsm_sec:
24085+ if (unlikely(match_int(&a->args[0], &n)
24086+ || n < 0)) {
24087+ pr_err("bad integer in %s\n", opt_str);
24088+ break;
24089+ }
24090+ if (sysaufs_brs) {
24091+ opt->fhsm_second = n;
24092+ opt->type = token;
24093+ } else
24094+ pr_warn("ignored %s\n", opt_str);
24095+ err = 0;
24096+ break;
24097+
1facf9fc 24098+ case Opt_ignore:
0c3ec466 24099+ pr_warn("ignored %s\n", opt_str);
1facf9fc 24100+ /*FALLTHROUGH*/
24101+ case Opt_ignore_silent:
24102+ skipped = 1;
24103+ err = 0;
24104+ break;
24105+ case Opt_err:
4a4d8108 24106+ pr_err("unknown option %s\n", opt_str);
1facf9fc 24107+ break;
24108+ }
24109+
24110+ if (!err && !skipped) {
24111+ if (unlikely(++opt > opt_tail)) {
24112+ err = -E2BIG;
24113+ opt--;
24114+ opt->type = Opt_tail;
24115+ break;
24116+ }
24117+ opt->type = Opt_tail;
24118+ }
24119+ }
24120+
24121+ kfree(a);
24122+ dump_opts(opts);
24123+ if (unlikely(err))
24124+ au_opts_free(opts);
24125+
4f0767ce 24126+out:
1facf9fc 24127+ return err;
24128+}
24129+
24130+static int au_opt_wbr_create(struct super_block *sb,
24131+ struct au_opt_wbr_create *create)
24132+{
24133+ int err;
24134+ struct au_sbinfo *sbinfo;
24135+
dece6358
AM
24136+ SiMustWriteLock(sb);
24137+
1facf9fc 24138+ err = 1; /* handled */
24139+ sbinfo = au_sbi(sb);
24140+ if (sbinfo->si_wbr_create_ops->fin) {
24141+ err = sbinfo->si_wbr_create_ops->fin(sb);
24142+ if (!err)
24143+ err = 1;
24144+ }
24145+
24146+ sbinfo->si_wbr_create = create->wbr_create;
24147+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
24148+ switch (create->wbr_create) {
24149+ case AuWbrCreate_MFSRRV:
24150+ case AuWbrCreate_MFSRR:
392086de
AM
24151+ case AuWbrCreate_PMFSRR:
24152+ case AuWbrCreate_PMFSRRV:
1facf9fc 24153+ sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
24154+ /*FALLTHROUGH*/
24155+ case AuWbrCreate_MFS:
24156+ case AuWbrCreate_MFSV:
24157+ case AuWbrCreate_PMFS:
24158+ case AuWbrCreate_PMFSV:
e49829fe
JR
24159+ sbinfo->si_wbr_mfs.mfs_expire
24160+ = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
1facf9fc 24161+ break;
24162+ }
24163+
24164+ if (sbinfo->si_wbr_create_ops->init)
24165+ sbinfo->si_wbr_create_ops->init(sb); /* ignore */
24166+
24167+ return err;
24168+}
24169+
24170+/*
24171+ * returns,
24172+ * plus: processed without an error
24173+ * zero: unprocessed
24174+ */
24175+static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
24176+ struct au_opts *opts)
24177+{
24178+ int err;
24179+ struct au_sbinfo *sbinfo;
24180+
dece6358
AM
24181+ SiMustWriteLock(sb);
24182+
1facf9fc 24183+ err = 1; /* handled */
24184+ sbinfo = au_sbi(sb);
24185+ switch (opt->type) {
24186+ case Opt_udba:
24187+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
24188+ sbinfo->si_mntflags |= opt->udba;
24189+ opts->given_udba |= opt->udba;
24190+ break;
24191+
24192+ case Opt_plink:
24193+ au_opt_set(sbinfo->si_mntflags, PLINK);
24194+ break;
24195+ case Opt_noplink:
24196+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
e49829fe 24197+ au_plink_put(sb, /*verbose*/1);
1facf9fc 24198+ au_opt_clr(sbinfo->si_mntflags, PLINK);
24199+ break;
24200+ case Opt_list_plink:
24201+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
24202+ au_plink_list(sb);
24203+ break;
24204+
4a4d8108
AM
24205+ case Opt_dio:
24206+ au_opt_set(sbinfo->si_mntflags, DIO);
24207+ au_fset_opts(opts->flags, REFRESH_DYAOP);
24208+ break;
24209+ case Opt_nodio:
24210+ au_opt_clr(sbinfo->si_mntflags, DIO);
24211+ au_fset_opts(opts->flags, REFRESH_DYAOP);
24212+ break;
24213+
076b876e
AM
24214+ case Opt_fhsm_sec:
24215+ au_fhsm_set(sbinfo, opt->fhsm_second);
24216+ break;
24217+
1facf9fc 24218+ case Opt_diropq_a:
24219+ au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24220+ break;
24221+ case Opt_diropq_w:
24222+ au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
24223+ break;
24224+
24225+ case Opt_warn_perm:
24226+ au_opt_set(sbinfo->si_mntflags, WARN_PERM);
24227+ break;
24228+ case Opt_nowarn_perm:
24229+ au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
24230+ break;
24231+
1facf9fc 24232+ case Opt_verbose:
24233+ au_opt_set(sbinfo->si_mntflags, VERBOSE);
24234+ break;
24235+ case Opt_noverbose:
24236+ au_opt_clr(sbinfo->si_mntflags, VERBOSE);
24237+ break;
24238+
24239+ case Opt_sum:
24240+ au_opt_set(sbinfo->si_mntflags, SUM);
24241+ break;
24242+ case Opt_wsum:
24243+ au_opt_clr(sbinfo->si_mntflags, SUM);
24244+ au_opt_set(sbinfo->si_mntflags, SUM_W);
24245+ case Opt_nosum:
24246+ au_opt_clr(sbinfo->si_mntflags, SUM);
24247+ au_opt_clr(sbinfo->si_mntflags, SUM_W);
24248+ break;
24249+
24250+ case Opt_wbr_create:
24251+ err = au_opt_wbr_create(sb, &opt->wbr_create);
24252+ break;
24253+ case Opt_wbr_copyup:
24254+ sbinfo->si_wbr_copyup = opt->wbr_copyup;
24255+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
24256+ break;
24257+
24258+ case Opt_dirwh:
24259+ sbinfo->si_dirwh = opt->dirwh;
24260+ break;
24261+
24262+ case Opt_rdcache:
e49829fe
JR
24263+ sbinfo->si_rdcache
24264+ = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
1facf9fc 24265+ break;
24266+ case Opt_rdblk:
24267+ sbinfo->si_rdblk = opt->rdblk;
24268+ break;
dece6358
AM
24269+ case Opt_rdblk_def:
24270+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
24271+ break;
1facf9fc 24272+ case Opt_rdhash:
24273+ sbinfo->si_rdhash = opt->rdhash;
24274+ break;
dece6358
AM
24275+ case Opt_rdhash_def:
24276+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
24277+ break;
24278+
24279+ case Opt_shwh:
24280+ au_opt_set(sbinfo->si_mntflags, SHWH);
24281+ break;
24282+ case Opt_noshwh:
24283+ au_opt_clr(sbinfo->si_mntflags, SHWH);
24284+ break;
1facf9fc 24285+
076b876e
AM
24286+ case Opt_dirperm1:
24287+ au_opt_set(sbinfo->si_mntflags, DIRPERM1);
24288+ break;
24289+ case Opt_nodirperm1:
24290+ au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
24291+ break;
24292+
1facf9fc 24293+ case Opt_trunc_xino:
24294+ au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
24295+ break;
24296+ case Opt_notrunc_xino:
24297+ au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
24298+ break;
24299+
24300+ case Opt_trunc_xino_path:
24301+ case Opt_itrunc_xino:
24302+ err = au_xino_trunc(sb, opt->xino_itrunc.bindex);
24303+ if (!err)
24304+ err = 1;
24305+ break;
24306+
24307+ case Opt_trunc_xib:
24308+ au_fset_opts(opts->flags, TRUNC_XIB);
24309+ break;
24310+ case Opt_notrunc_xib:
24311+ au_fclr_opts(opts->flags, TRUNC_XIB);
24312+ break;
24313+
c1595e42
JR
24314+ case Opt_acl:
24315+ sb->s_flags |= MS_POSIXACL;
24316+ break;
24317+ case Opt_noacl:
24318+ sb->s_flags &= ~MS_POSIXACL;
24319+ break;
24320+
1facf9fc 24321+ default:
24322+ err = 0;
24323+ break;
24324+ }
24325+
24326+ return err;
24327+}
24328+
24329+/*
24330+ * returns tri-state.
24331+ * plus: processed without an error
24332+ * zero: unprocessed
24333+ * minus: error
24334+ */
24335+static int au_opt_br(struct super_block *sb, struct au_opt *opt,
24336+ struct au_opts *opts)
24337+{
24338+ int err, do_refresh;
24339+
24340+ err = 0;
24341+ switch (opt->type) {
24342+ case Opt_append:
5afbbe0d 24343+ opt->add.bindex = au_sbbot(sb) + 1;
1facf9fc 24344+ if (opt->add.bindex < 0)
24345+ opt->add.bindex = 0;
24346+ goto add;
24347+ case Opt_prepend:
24348+ opt->add.bindex = 0;
f6b6e03d 24349+ add: /* indented label */
1facf9fc 24350+ case Opt_add:
24351+ err = au_br_add(sb, &opt->add,
24352+ au_ftest_opts(opts->flags, REMOUNT));
24353+ if (!err) {
24354+ err = 1;
027c5e7a 24355+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24356+ }
24357+ break;
24358+
24359+ case Opt_del:
24360+ case Opt_idel:
24361+ err = au_br_del(sb, &opt->del,
24362+ au_ftest_opts(opts->flags, REMOUNT));
24363+ if (!err) {
24364+ err = 1;
24365+ au_fset_opts(opts->flags, TRUNC_XIB);
027c5e7a 24366+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24367+ }
24368+ break;
24369+
24370+ case Opt_mod:
24371+ case Opt_imod:
24372+ err = au_br_mod(sb, &opt->mod,
24373+ au_ftest_opts(opts->flags, REMOUNT),
24374+ &do_refresh);
24375+ if (!err) {
24376+ err = 1;
027c5e7a
AM
24377+ if (do_refresh)
24378+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24379+ }
24380+ break;
24381+ }
24382+
24383+ return err;
24384+}
24385+
24386+static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
24387+ struct au_opt_xino **opt_xino,
24388+ struct au_opts *opts)
24389+{
24390+ int err;
5afbbe0d 24391+ aufs_bindex_t bbot, bindex;
1facf9fc 24392+ struct dentry *root, *parent, *h_root;
24393+
24394+ err = 0;
24395+ switch (opt->type) {
24396+ case Opt_xino:
24397+ err = au_xino_set(sb, &opt->xino,
24398+ !!au_ftest_opts(opts->flags, REMOUNT));
24399+ if (unlikely(err))
24400+ break;
24401+
24402+ *opt_xino = &opt->xino;
24403+ au_xino_brid_set(sb, -1);
24404+
24405+ /* safe d_parent access */
2000de60 24406+ parent = opt->xino.file->f_path.dentry->d_parent;
1facf9fc 24407+ root = sb->s_root;
5afbbe0d
AM
24408+ bbot = au_sbbot(sb);
24409+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 24410+ h_root = au_h_dptr(root, bindex);
24411+ if (h_root == parent) {
24412+ au_xino_brid_set(sb, au_sbr_id(sb, bindex));
24413+ break;
24414+ }
24415+ }
24416+ break;
24417+
24418+ case Opt_noxino:
24419+ au_xino_clr(sb);
24420+ au_xino_brid_set(sb, -1);
24421+ *opt_xino = (void *)-1;
24422+ break;
24423+ }
24424+
24425+ return err;
24426+}
24427+
24428+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24429+ unsigned int pending)
24430+{
076b876e 24431+ int err, fhsm;
5afbbe0d 24432+ aufs_bindex_t bindex, bbot;
79b8bda9 24433+ unsigned char do_plink, skip, do_free, can_no_dreval;
1facf9fc 24434+ struct au_branch *br;
24435+ struct au_wbr *wbr;
79b8bda9 24436+ struct dentry *root, *dentry;
1facf9fc 24437+ struct inode *dir, *h_dir;
24438+ struct au_sbinfo *sbinfo;
24439+ struct au_hinode *hdir;
24440+
dece6358
AM
24441+ SiMustAnyLock(sb);
24442+
1facf9fc 24443+ sbinfo = au_sbi(sb);
24444+ AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
24445+
dece6358
AM
24446+ if (!(sb_flags & MS_RDONLY)) {
24447+ if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
0c3ec466 24448+ pr_warn("first branch should be rw\n");
dece6358 24449+ if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
febd17d6 24450+ pr_warn_once("shwh should be used with ro\n");
dece6358 24451+ }
1facf9fc 24452+
4a4d8108 24453+ if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
1facf9fc 24454+ && !au_opt_test(sbinfo->si_mntflags, XINO))
febd17d6 24455+ pr_warn_once("udba=*notify requires xino\n");
1facf9fc 24456+
076b876e 24457+ if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
febd17d6
JR
24458+ pr_warn_once("dirperm1 breaks the protection"
24459+ " by the permission bits on the lower branch\n");
076b876e 24460+
1facf9fc 24461+ err = 0;
076b876e 24462+ fhsm = 0;
1facf9fc 24463+ root = sb->s_root;
5527c038 24464+ dir = d_inode(root);
1facf9fc 24465+ do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
79b8bda9
AM
24466+ can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
24467+ UDBA_NONE);
5afbbe0d
AM
24468+ bbot = au_sbbot(sb);
24469+ for (bindex = 0; !err && bindex <= bbot; bindex++) {
1facf9fc 24470+ skip = 0;
24471+ h_dir = au_h_iptr(dir, bindex);
24472+ br = au_sbr(sb, bindex);
1facf9fc 24473+
c1595e42
JR
24474+ if ((br->br_perm & AuBrAttr_ICEX)
24475+ && !h_dir->i_op->listxattr)
24476+ br->br_perm &= ~AuBrAttr_ICEX;
24477+#if 0
24478+ if ((br->br_perm & AuBrAttr_ICEX_SEC)
24479+ && (au_br_sb(br)->s_flags & MS_NOSEC))
24480+ br->br_perm &= ~AuBrAttr_ICEX_SEC;
24481+#endif
24482+
24483+ do_free = 0;
1facf9fc 24484+ wbr = br->br_wbr;
24485+ if (wbr)
24486+ wbr_wh_read_lock(wbr);
24487+
1e00d052 24488+ if (!au_br_writable(br->br_perm)) {
1facf9fc 24489+ do_free = !!wbr;
24490+ skip = (!wbr
24491+ || (!wbr->wbr_whbase
24492+ && !wbr->wbr_plink
24493+ && !wbr->wbr_orph));
1e00d052 24494+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 24495+ /* skip = (!br->br_whbase && !br->br_orph); */
24496+ skip = (!wbr || !wbr->wbr_whbase);
24497+ if (skip && wbr) {
24498+ if (do_plink)
24499+ skip = !!wbr->wbr_plink;
24500+ else
24501+ skip = !wbr->wbr_plink;
24502+ }
1e00d052 24503+ } else {
1facf9fc 24504+ /* skip = (br->br_whbase && br->br_ohph); */
24505+ skip = (wbr && wbr->wbr_whbase);
24506+ if (skip) {
24507+ if (do_plink)
24508+ skip = !!wbr->wbr_plink;
24509+ else
24510+ skip = !wbr->wbr_plink;
24511+ }
1facf9fc 24512+ }
24513+ if (wbr)
24514+ wbr_wh_read_unlock(wbr);
24515+
79b8bda9
AM
24516+ if (can_no_dreval) {
24517+ dentry = br->br_path.dentry;
24518+ spin_lock(&dentry->d_lock);
24519+ if (dentry->d_flags &
24520+ (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
24521+ can_no_dreval = 0;
24522+ spin_unlock(&dentry->d_lock);
24523+ }
24524+
076b876e
AM
24525+ if (au_br_fhsm(br->br_perm)) {
24526+ fhsm++;
24527+ AuDebugOn(!br->br_fhsm);
24528+ }
24529+
1facf9fc 24530+ if (skip)
24531+ continue;
24532+
24533+ hdir = au_hi(dir, bindex);
5afbbe0d 24534+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 24535+ if (wbr)
24536+ wbr_wh_write_lock(wbr);
86dc4139 24537+ err = au_wh_init(br, sb);
1facf9fc 24538+ if (wbr)
24539+ wbr_wh_write_unlock(wbr);
5afbbe0d 24540+ au_hn_inode_unlock(hdir);
1facf9fc 24541+
24542+ if (!err && do_free) {
24543+ kfree(wbr);
24544+ br->br_wbr = NULL;
24545+ }
24546+ }
24547+
79b8bda9
AM
24548+ if (can_no_dreval)
24549+ au_fset_si(sbinfo, NO_DREVAL);
24550+ else
24551+ au_fclr_si(sbinfo, NO_DREVAL);
24552+
c1595e42 24553+ if (fhsm >= 2) {
076b876e 24554+ au_fset_si(sbinfo, FHSM);
5afbbe0d 24555+ for (bindex = bbot; bindex >= 0; bindex--) {
c1595e42
JR
24556+ br = au_sbr(sb, bindex);
24557+ if (au_br_fhsm(br->br_perm)) {
24558+ au_fhsm_set_bottom(sb, bindex);
24559+ break;
24560+ }
24561+ }
24562+ } else {
076b876e 24563+ au_fclr_si(sbinfo, FHSM);
c1595e42
JR
24564+ au_fhsm_set_bottom(sb, -1);
24565+ }
076b876e 24566+
1facf9fc 24567+ return err;
24568+}
24569+
24570+int au_opts_mount(struct super_block *sb, struct au_opts *opts)
24571+{
24572+ int err;
24573+ unsigned int tmp;
5afbbe0d 24574+ aufs_bindex_t bindex, bbot;
1facf9fc 24575+ struct au_opt *opt;
24576+ struct au_opt_xino *opt_xino, xino;
24577+ struct au_sbinfo *sbinfo;
027c5e7a 24578+ struct au_branch *br;
076b876e 24579+ struct inode *dir;
1facf9fc 24580+
dece6358
AM
24581+ SiMustWriteLock(sb);
24582+
1facf9fc 24583+ err = 0;
24584+ opt_xino = NULL;
24585+ opt = opts->opt;
24586+ while (err >= 0 && opt->type != Opt_tail)
24587+ err = au_opt_simple(sb, opt++, opts);
24588+ if (err > 0)
24589+ err = 0;
24590+ else if (unlikely(err < 0))
24591+ goto out;
24592+
24593+ /* disable xino and udba temporary */
24594+ sbinfo = au_sbi(sb);
24595+ tmp = sbinfo->si_mntflags;
24596+ au_opt_clr(sbinfo->si_mntflags, XINO);
24597+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
24598+
24599+ opt = opts->opt;
24600+ while (err >= 0 && opt->type != Opt_tail)
24601+ err = au_opt_br(sb, opt++, opts);
24602+ if (err > 0)
24603+ err = 0;
24604+ else if (unlikely(err < 0))
24605+ goto out;
24606+
5afbbe0d
AM
24607+ bbot = au_sbbot(sb);
24608+ if (unlikely(bbot < 0)) {
1facf9fc 24609+ err = -EINVAL;
4a4d8108 24610+ pr_err("no branches\n");
1facf9fc 24611+ goto out;
24612+ }
24613+
24614+ if (au_opt_test(tmp, XINO))
24615+ au_opt_set(sbinfo->si_mntflags, XINO);
24616+ opt = opts->opt;
24617+ while (!err && opt->type != Opt_tail)
24618+ err = au_opt_xino(sb, opt++, &opt_xino, opts);
24619+ if (unlikely(err))
24620+ goto out;
24621+
24622+ err = au_opts_verify(sb, sb->s_flags, tmp);
24623+ if (unlikely(err))
24624+ goto out;
24625+
24626+ /* restore xino */
24627+ if (au_opt_test(tmp, XINO) && !opt_xino) {
24628+ xino.file = au_xino_def(sb);
24629+ err = PTR_ERR(xino.file);
24630+ if (IS_ERR(xino.file))
24631+ goto out;
24632+
24633+ err = au_xino_set(sb, &xino, /*remount*/0);
24634+ fput(xino.file);
24635+ if (unlikely(err))
24636+ goto out;
24637+ }
24638+
24639+ /* restore udba */
027c5e7a 24640+ tmp &= AuOptMask_UDBA;
1facf9fc 24641+ sbinfo->si_mntflags &= ~AuOptMask_UDBA;
027c5e7a 24642+ sbinfo->si_mntflags |= tmp;
5afbbe0d
AM
24643+ bbot = au_sbbot(sb);
24644+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
24645+ br = au_sbr(sb, bindex);
24646+ err = au_hnotify_reset_br(tmp, br, br->br_perm);
24647+ if (unlikely(err))
24648+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
24649+ bindex, err);
24650+ /* go on even if err */
24651+ }
4a4d8108 24652+ if (au_opt_test(tmp, UDBA_HNOTIFY)) {
5527c038 24653+ dir = d_inode(sb->s_root);
4a4d8108 24654+ au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
1facf9fc 24655+ }
24656+
4f0767ce 24657+out:
1facf9fc 24658+ return err;
24659+}
24660+
24661+int au_opts_remount(struct super_block *sb, struct au_opts *opts)
24662+{
24663+ int err, rerr;
79b8bda9 24664+ unsigned char no_dreval;
1facf9fc 24665+ struct inode *dir;
24666+ struct au_opt_xino *opt_xino;
24667+ struct au_opt *opt;
24668+ struct au_sbinfo *sbinfo;
24669+
dece6358
AM
24670+ SiMustWriteLock(sb);
24671+
79b8bda9 24672+ err = 0;
5527c038 24673+ dir = d_inode(sb->s_root);
1facf9fc 24674+ sbinfo = au_sbi(sb);
1facf9fc 24675+ opt_xino = NULL;
24676+ opt = opts->opt;
24677+ while (err >= 0 && opt->type != Opt_tail) {
24678+ err = au_opt_simple(sb, opt, opts);
24679+ if (!err)
24680+ err = au_opt_br(sb, opt, opts);
24681+ if (!err)
24682+ err = au_opt_xino(sb, opt, &opt_xino, opts);
24683+ opt++;
24684+ }
24685+ if (err > 0)
24686+ err = 0;
24687+ AuTraceErr(err);
24688+ /* go on even err */
24689+
79b8bda9 24690+ no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
1facf9fc 24691+ rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
24692+ if (unlikely(rerr && !err))
24693+ err = rerr;
24694+
79b8bda9 24695+ if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
b95c5147 24696+ au_fset_opts(opts->flags, REFRESH_IDOP);
79b8bda9 24697+
1facf9fc 24698+ if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
24699+ rerr = au_xib_trunc(sb);
24700+ if (unlikely(rerr && !err))
24701+ err = rerr;
24702+ }
24703+
24704+ /* will be handled by the caller */
027c5e7a 24705+ if (!au_ftest_opts(opts->flags, REFRESH)
79b8bda9
AM
24706+ && (opts->given_udba
24707+ || au_opt_test(sbinfo->si_mntflags, XINO)
b95c5147 24708+ || au_ftest_opts(opts->flags, REFRESH_IDOP)
79b8bda9 24709+ ))
027c5e7a 24710+ au_fset_opts(opts->flags, REFRESH);
1facf9fc 24711+
24712+ AuDbg("status 0x%x\n", opts->flags);
24713+ return err;
24714+}
24715+
24716+/* ---------------------------------------------------------------------- */
24717+
24718+unsigned int au_opt_udba(struct super_block *sb)
24719+{
24720+ return au_mntflags(sb) & AuOptMask_UDBA;
24721+}
7f207e10
AM
24722diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
24723--- /usr/share/empty/fs/aufs/opts.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 24724+++ linux/fs/aufs/opts.h 2016-07-25 19:05:34.814493242 +0200
79b8bda9 24725@@ -0,0 +1,211 @@
1facf9fc 24726+/*
8cdd5066 24727+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 24728+ *
24729+ * This program, aufs is free software; you can redistribute it and/or modify
24730+ * it under the terms of the GNU General Public License as published by
24731+ * the Free Software Foundation; either version 2 of the License, or
24732+ * (at your option) any later version.
dece6358
AM
24733+ *
24734+ * This program is distributed in the hope that it will be useful,
24735+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24736+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24737+ * GNU General Public License for more details.
24738+ *
24739+ * You should have received a copy of the GNU General Public License
523b37e3 24740+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24741+ */
24742+
24743+/*
24744+ * mount options/flags
24745+ */
24746+
24747+#ifndef __AUFS_OPTS_H__
24748+#define __AUFS_OPTS_H__
24749+
24750+#ifdef __KERNEL__
24751+
dece6358 24752+#include <linux/path.h>
1facf9fc 24753+
dece6358
AM
24754+struct file;
24755+struct super_block;
24756+
1facf9fc 24757+/* ---------------------------------------------------------------------- */
24758+
24759+/* mount flags */
24760+#define AuOpt_XINO 1 /* external inode number bitmap
24761+ and translation table */
24762+#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
24763+#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
24764+#define AuOpt_UDBA_REVAL (1 << 3)
4a4d8108 24765+#define AuOpt_UDBA_HNOTIFY (1 << 4)
dece6358
AM
24766+#define AuOpt_SHWH (1 << 5) /* show whiteout */
24767+#define AuOpt_PLINK (1 << 6) /* pseudo-link */
076b876e
AM
24768+#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
24769+ bits */
dece6358
AM
24770+#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
24771+#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
24772+#define AuOpt_SUM_W (1 << 11) /* unimplemented */
24773+#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
24774+#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
4a4d8108 24775+#define AuOpt_DIO (1 << 14) /* direct io */
1facf9fc 24776+
4a4d8108
AM
24777+#ifndef CONFIG_AUFS_HNOTIFY
24778+#undef AuOpt_UDBA_HNOTIFY
24779+#define AuOpt_UDBA_HNOTIFY 0
1facf9fc 24780+#endif
dece6358
AM
24781+#ifndef CONFIG_AUFS_SHWH
24782+#undef AuOpt_SHWH
24783+#define AuOpt_SHWH 0
24784+#endif
1facf9fc 24785+
24786+#define AuOpt_Def (AuOpt_XINO \
24787+ | AuOpt_UDBA_REVAL \
24788+ | AuOpt_PLINK \
24789+ /* | AuOpt_DIRPERM1 */ \
24790+ | AuOpt_WARN_PERM)
24791+#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
24792+ | AuOpt_UDBA_REVAL \
4a4d8108 24793+ | AuOpt_UDBA_HNOTIFY)
1facf9fc 24794+
24795+#define au_opt_test(flags, name) (flags & AuOpt_##name)
24796+#define au_opt_set(flags, name) do { \
24797+ BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
24798+ ((flags) |= AuOpt_##name); \
24799+} while (0)
24800+#define au_opt_set_udba(flags, name) do { \
24801+ (flags) &= ~AuOptMask_UDBA; \
24802+ ((flags) |= AuOpt_##name); \
24803+} while (0)
7f207e10
AM
24804+#define au_opt_clr(flags, name) do { \
24805+ ((flags) &= ~AuOpt_##name); \
24806+} while (0)
1facf9fc 24807+
e49829fe
JR
24808+static inline unsigned int au_opts_plink(unsigned int mntflags)
24809+{
24810+#ifdef CONFIG_PROC_FS
24811+ return mntflags;
24812+#else
24813+ return mntflags & ~AuOpt_PLINK;
24814+#endif
24815+}
24816+
1facf9fc 24817+/* ---------------------------------------------------------------------- */
24818+
24819+/* policies to select one among multiple writable branches */
24820+enum {
24821+ AuWbrCreate_TDP, /* top down parent */
24822+ AuWbrCreate_RR, /* round robin */
24823+ AuWbrCreate_MFS, /* most free space */
24824+ AuWbrCreate_MFSV, /* mfs with seconds */
24825+ AuWbrCreate_MFSRR, /* mfs then rr */
24826+ AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
24827+ AuWbrCreate_PMFS, /* parent and mfs */
24828+ AuWbrCreate_PMFSV, /* parent and mfs with seconds */
392086de
AM
24829+ AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
24830+ AuWbrCreate_PMFSRRV, /* plus seconds */
1facf9fc 24831+
24832+ AuWbrCreate_Def = AuWbrCreate_TDP
24833+};
24834+
24835+enum {
24836+ AuWbrCopyup_TDP, /* top down parent */
24837+ AuWbrCopyup_BUP, /* bottom up parent */
24838+ AuWbrCopyup_BU, /* bottom up */
24839+
24840+ AuWbrCopyup_Def = AuWbrCopyup_TDP
24841+};
24842+
24843+/* ---------------------------------------------------------------------- */
24844+
24845+struct au_opt_add {
24846+ aufs_bindex_t bindex;
24847+ char *pathname;
24848+ int perm;
24849+ struct path path;
24850+};
24851+
24852+struct au_opt_del {
24853+ char *pathname;
24854+ struct path h_path;
24855+};
24856+
24857+struct au_opt_mod {
24858+ char *path;
24859+ int perm;
24860+ struct dentry *h_root;
24861+};
24862+
24863+struct au_opt_xino {
24864+ char *path;
24865+ struct file *file;
24866+};
24867+
24868+struct au_opt_xino_itrunc {
24869+ aufs_bindex_t bindex;
24870+};
24871+
24872+struct au_opt_wbr_create {
24873+ int wbr_create;
24874+ int mfs_second;
24875+ unsigned long long mfsrr_watermark;
24876+};
24877+
24878+struct au_opt {
24879+ int type;
24880+ union {
24881+ struct au_opt_xino xino;
24882+ struct au_opt_xino_itrunc xino_itrunc;
24883+ struct au_opt_add add;
24884+ struct au_opt_del del;
24885+ struct au_opt_mod mod;
24886+ int dirwh;
24887+ int rdcache;
24888+ unsigned int rdblk;
24889+ unsigned int rdhash;
24890+ int udba;
24891+ struct au_opt_wbr_create wbr_create;
24892+ int wbr_copyup;
076b876e 24893+ unsigned int fhsm_second;
1facf9fc 24894+ };
24895+};
24896+
24897+/* opts flags */
24898+#define AuOpts_REMOUNT 1
027c5e7a
AM
24899+#define AuOpts_REFRESH (1 << 1)
24900+#define AuOpts_TRUNC_XIB (1 << 2)
24901+#define AuOpts_REFRESH_DYAOP (1 << 3)
b95c5147 24902+#define AuOpts_REFRESH_IDOP (1 << 4)
1facf9fc 24903+#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
7f207e10
AM
24904+#define au_fset_opts(flags, name) \
24905+ do { (flags) |= AuOpts_##name; } while (0)
24906+#define au_fclr_opts(flags, name) \
24907+ do { (flags) &= ~AuOpts_##name; } while (0)
1facf9fc 24908+
24909+struct au_opts {
24910+ struct au_opt *opt;
24911+ int max_opt;
24912+
24913+ unsigned int given_udba;
24914+ unsigned int flags;
24915+ unsigned long sb_flags;
24916+};
24917+
24918+/* ---------------------------------------------------------------------- */
24919+
7e9cd9fe 24920+/* opts.c */
076b876e 24921+void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
1facf9fc 24922+const char *au_optstr_udba(int udba);
24923+const char *au_optstr_wbr_copyup(int wbr_copyup);
24924+const char *au_optstr_wbr_create(int wbr_create);
24925+
24926+void au_opts_free(struct au_opts *opts);
24927+int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
24928+int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
24929+ unsigned int pending);
24930+int au_opts_mount(struct super_block *sb, struct au_opts *opts);
24931+int au_opts_remount(struct super_block *sb, struct au_opts *opts);
24932+
24933+unsigned int au_opt_udba(struct super_block *sb);
24934+
1facf9fc 24935+#endif /* __KERNEL__ */
24936+#endif /* __AUFS_OPTS_H__ */
7f207e10
AM
24937diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
24938--- /usr/share/empty/fs/aufs/plink.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
24939+++ linux/fs/aufs/plink.c 2016-07-25 19:05:34.817826663 +0200
24940@@ -0,0 +1,502 @@
1facf9fc 24941+/*
8cdd5066 24942+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 24943+ *
24944+ * This program, aufs is free software; you can redistribute it and/or modify
24945+ * it under the terms of the GNU General Public License as published by
24946+ * the Free Software Foundation; either version 2 of the License, or
24947+ * (at your option) any later version.
dece6358
AM
24948+ *
24949+ * This program is distributed in the hope that it will be useful,
24950+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
24951+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24952+ * GNU General Public License for more details.
24953+ *
24954+ * You should have received a copy of the GNU General Public License
523b37e3 24955+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 24956+ */
24957+
24958+/*
24959+ * pseudo-link
24960+ */
24961+
24962+#include "aufs.h"
24963+
24964+/*
e49829fe 24965+ * the pseudo-link maintenance mode.
1facf9fc 24966+ * during a user process maintains the pseudo-links,
24967+ * prohibit adding a new plink and branch manipulation.
e49829fe
JR
24968+ *
24969+ * Flags
24970+ * NOPLM:
24971+ * For entry functions which will handle plink, and i_mutex is already held
24972+ * in VFS.
24973+ * They cannot wait and should return an error at once.
24974+ * Callers has to check the error.
24975+ * NOPLMW:
24976+ * For entry functions which will handle plink, but i_mutex is not held
24977+ * in VFS.
24978+ * They can wait the plink maintenance mode to finish.
24979+ *
24980+ * They behave like F_SETLK and F_SETLKW.
24981+ * If the caller never handle plink, then both flags are unnecessary.
1facf9fc 24982+ */
e49829fe
JR
24983+
24984+int au_plink_maint(struct super_block *sb, int flags)
1facf9fc 24985+{
e49829fe
JR
24986+ int err;
24987+ pid_t pid, ppid;
24988+ struct au_sbinfo *sbi;
dece6358
AM
24989+
24990+ SiMustAnyLock(sb);
24991+
e49829fe
JR
24992+ err = 0;
24993+ if (!au_opt_test(au_mntflags(sb), PLINK))
24994+ goto out;
24995+
24996+ sbi = au_sbi(sb);
24997+ pid = sbi->si_plink_maint_pid;
24998+ if (!pid || pid == current->pid)
24999+ goto out;
25000+
25001+ /* todo: it highly depends upon /sbin/mount.aufs */
25002+ rcu_read_lock();
25003+ ppid = task_pid_vnr(rcu_dereference(current->real_parent));
25004+ rcu_read_unlock();
25005+ if (pid == ppid)
25006+ goto out;
25007+
25008+ if (au_ftest_lock(flags, NOPLMW)) {
027c5e7a
AM
25009+ /* if there is no i_mutex lock in VFS, we don't need to wait */
25010+ /* AuDebugOn(!lockdep_depth(current)); */
e49829fe
JR
25011+ while (sbi->si_plink_maint_pid) {
25012+ si_read_unlock(sb);
25013+ /* gave up wake_up_bit() */
25014+ wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
25015+
25016+ if (au_ftest_lock(flags, FLUSH))
25017+ au_nwt_flush(&sbi->si_nowait);
25018+ si_noflush_read_lock(sb);
25019+ }
25020+ } else if (au_ftest_lock(flags, NOPLM)) {
25021+ AuDbg("ppid %d, pid %d\n", ppid, pid);
25022+ err = -EAGAIN;
25023+ }
25024+
25025+out:
25026+ return err;
4a4d8108
AM
25027+}
25028+
e49829fe 25029+void au_plink_maint_leave(struct au_sbinfo *sbinfo)
4a4d8108 25030+{
4a4d8108 25031+ spin_lock(&sbinfo->si_plink_maint_lock);
027c5e7a 25032+ sbinfo->si_plink_maint_pid = 0;
4a4d8108 25033+ spin_unlock(&sbinfo->si_plink_maint_lock);
027c5e7a 25034+ wake_up_all(&sbinfo->si_plink_wq);
4a4d8108
AM
25035+}
25036+
e49829fe 25037+int au_plink_maint_enter(struct super_block *sb)
4a4d8108
AM
25038+{
25039+ int err;
4a4d8108
AM
25040+ struct au_sbinfo *sbinfo;
25041+
25042+ err = 0;
4a4d8108
AM
25043+ sbinfo = au_sbi(sb);
25044+ /* make sure i am the only one in this fs */
e49829fe
JR
25045+ si_write_lock(sb, AuLock_FLUSH);
25046+ if (au_opt_test(au_mntflags(sb), PLINK)) {
25047+ spin_lock(&sbinfo->si_plink_maint_lock);
25048+ if (!sbinfo->si_plink_maint_pid)
25049+ sbinfo->si_plink_maint_pid = current->pid;
25050+ else
25051+ err = -EBUSY;
25052+ spin_unlock(&sbinfo->si_plink_maint_lock);
25053+ }
4a4d8108
AM
25054+ si_write_unlock(sb);
25055+
25056+ return err;
1facf9fc 25057+}
25058+
25059+/* ---------------------------------------------------------------------- */
25060+
1facf9fc 25061+#ifdef CONFIG_AUFS_DEBUG
25062+void au_plink_list(struct super_block *sb)
25063+{
86dc4139 25064+ int i;
1facf9fc 25065+ struct au_sbinfo *sbinfo;
86dc4139 25066+ struct hlist_head *plink_hlist;
5afbbe0d 25067+ struct au_icntnr *icntnr;
1facf9fc 25068+
dece6358
AM
25069+ SiMustAnyLock(sb);
25070+
1facf9fc 25071+ sbinfo = au_sbi(sb);
25072+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25073+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25074+
86dc4139
AM
25075+ for (i = 0; i < AuPlink_NHASH; i++) {
25076+ plink_hlist = &sbinfo->si_plink[i].head;
25077+ rcu_read_lock();
5afbbe0d
AM
25078+ hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25079+ AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
86dc4139
AM
25080+ rcu_read_unlock();
25081+ }
1facf9fc 25082+}
25083+#endif
25084+
25085+/* is the inode pseudo-linked? */
25086+int au_plink_test(struct inode *inode)
25087+{
86dc4139 25088+ int found, i;
1facf9fc 25089+ struct au_sbinfo *sbinfo;
86dc4139 25090+ struct hlist_head *plink_hlist;
5afbbe0d 25091+ struct au_icntnr *icntnr;
1facf9fc 25092+
25093+ sbinfo = au_sbi(inode->i_sb);
dece6358 25094+ AuRwMustAnyLock(&sbinfo->si_rwsem);
1facf9fc 25095+ AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
e49829fe 25096+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
1facf9fc 25097+
25098+ found = 0;
86dc4139
AM
25099+ i = au_plink_hash(inode->i_ino);
25100+ plink_hlist = &sbinfo->si_plink[i].head;
4a4d8108 25101+ rcu_read_lock();
5afbbe0d
AM
25102+ hlist_for_each_entry_rcu(icntnr, plink_hlist, plink)
25103+ if (&icntnr->vfs_inode == inode) {
1facf9fc 25104+ found = 1;
25105+ break;
25106+ }
4a4d8108 25107+ rcu_read_unlock();
1facf9fc 25108+ return found;
25109+}
25110+
25111+/* ---------------------------------------------------------------------- */
25112+
25113+/*
25114+ * generate a name for plink.
25115+ * the file will be stored under AUFS_WH_PLINKDIR.
25116+ */
25117+/* 20 is max digits length of ulong 64 */
25118+#define PLINK_NAME_LEN ((20 + 1) * 2)
25119+
25120+static int plink_name(char *name, int len, struct inode *inode,
25121+ aufs_bindex_t bindex)
25122+{
25123+ int rlen;
25124+ struct inode *h_inode;
25125+
25126+ h_inode = au_h_iptr(inode, bindex);
25127+ rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
25128+ return rlen;
25129+}
25130+
7f207e10
AM
25131+struct au_do_plink_lkup_args {
25132+ struct dentry **errp;
25133+ struct qstr *tgtname;
25134+ struct dentry *h_parent;
25135+ struct au_branch *br;
25136+};
25137+
25138+static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
25139+ struct dentry *h_parent,
25140+ struct au_branch *br)
25141+{
25142+ struct dentry *h_dentry;
febd17d6 25143+ struct inode *h_inode;
7f207e10 25144+
febd17d6
JR
25145+ h_inode = d_inode(h_parent);
25146+ inode_lock_nested(h_inode, AuLsc_I_CHILD2);
b4510431 25147+ h_dentry = vfsub_lkup_one(tgtname, h_parent);
febd17d6 25148+ inode_unlock(h_inode);
7f207e10
AM
25149+ return h_dentry;
25150+}
25151+
25152+static void au_call_do_plink_lkup(void *args)
25153+{
25154+ struct au_do_plink_lkup_args *a = args;
25155+ *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
25156+}
25157+
1facf9fc 25158+/* lookup the plink-ed @inode under the branch at @bindex */
25159+struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
25160+{
25161+ struct dentry *h_dentry, *h_parent;
25162+ struct au_branch *br;
7f207e10 25163+ int wkq_err;
1facf9fc 25164+ char a[PLINK_NAME_LEN];
0c3ec466 25165+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 25166+
e49829fe
JR
25167+ AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
25168+
1facf9fc 25169+ br = au_sbr(inode->i_sb, bindex);
25170+ h_parent = br->br_wbr->wbr_plink;
1facf9fc 25171+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25172+
2dfbb274 25173+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
7f207e10
AM
25174+ struct au_do_plink_lkup_args args = {
25175+ .errp = &h_dentry,
25176+ .tgtname = &tgtname,
25177+ .h_parent = h_parent,
25178+ .br = br
25179+ };
25180+
25181+ wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
25182+ if (unlikely(wkq_err))
25183+ h_dentry = ERR_PTR(wkq_err);
25184+ } else
25185+ h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
25186+
1facf9fc 25187+ return h_dentry;
25188+}
25189+
25190+/* create a pseudo-link */
25191+static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
25192+ struct dentry *h_dentry, struct au_branch *br)
25193+{
25194+ int err;
25195+ struct path h_path = {
86dc4139 25196+ .mnt = au_br_mnt(br)
1facf9fc 25197+ };
523b37e3 25198+ struct inode *h_dir, *delegated;
1facf9fc 25199+
5527c038 25200+ h_dir = d_inode(h_parent);
febd17d6 25201+ inode_lock_nested(h_dir, AuLsc_I_CHILD2);
4f0767ce 25202+again:
b4510431 25203+ h_path.dentry = vfsub_lkup_one(tgt, h_parent);
1facf9fc 25204+ err = PTR_ERR(h_path.dentry);
25205+ if (IS_ERR(h_path.dentry))
25206+ goto out;
25207+
25208+ err = 0;
25209+ /* wh.plink dir is not monitored */
7f207e10 25210+ /* todo: is it really safe? */
5527c038
JR
25211+ if (d_is_positive(h_path.dentry)
25212+ && d_inode(h_path.dentry) != d_inode(h_dentry)) {
523b37e3
AM
25213+ delegated = NULL;
25214+ err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
25215+ if (unlikely(err == -EWOULDBLOCK)) {
25216+ pr_warn("cannot retry for NFSv4 delegation"
25217+ " for an internal unlink\n");
25218+ iput(delegated);
25219+ }
1facf9fc 25220+ dput(h_path.dentry);
25221+ h_path.dentry = NULL;
25222+ if (!err)
25223+ goto again;
25224+ }
5527c038 25225+ if (!err && d_is_negative(h_path.dentry)) {
523b37e3
AM
25226+ delegated = NULL;
25227+ err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
25228+ if (unlikely(err == -EWOULDBLOCK)) {
25229+ pr_warn("cannot retry for NFSv4 delegation"
25230+ " for an internal link\n");
25231+ iput(delegated);
25232+ }
25233+ }
1facf9fc 25234+ dput(h_path.dentry);
25235+
4f0767ce 25236+out:
febd17d6 25237+ inode_unlock(h_dir);
1facf9fc 25238+ return err;
25239+}
25240+
25241+struct do_whplink_args {
25242+ int *errp;
25243+ struct qstr *tgt;
25244+ struct dentry *h_parent;
25245+ struct dentry *h_dentry;
25246+ struct au_branch *br;
25247+};
25248+
25249+static void call_do_whplink(void *args)
25250+{
25251+ struct do_whplink_args *a = args;
25252+ *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
25253+}
25254+
25255+static int whplink(struct dentry *h_dentry, struct inode *inode,
25256+ aufs_bindex_t bindex, struct au_branch *br)
25257+{
25258+ int err, wkq_err;
25259+ struct au_wbr *wbr;
25260+ struct dentry *h_parent;
1facf9fc 25261+ char a[PLINK_NAME_LEN];
0c3ec466 25262+ struct qstr tgtname = QSTR_INIT(a, 0);
1facf9fc 25263+
25264+ wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
25265+ h_parent = wbr->wbr_plink;
1facf9fc 25266+ tgtname.len = plink_name(a, sizeof(a), inode, bindex);
25267+
25268+ /* always superio. */
2dfbb274 25269+ if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
1facf9fc 25270+ struct do_whplink_args args = {
25271+ .errp = &err,
25272+ .tgt = &tgtname,
25273+ .h_parent = h_parent,
25274+ .h_dentry = h_dentry,
25275+ .br = br
25276+ };
25277+ wkq_err = au_wkq_wait(call_do_whplink, &args);
25278+ if (unlikely(wkq_err))
25279+ err = wkq_err;
25280+ } else
25281+ err = do_whplink(&tgtname, h_parent, h_dentry, br);
1facf9fc 25282+
25283+ return err;
25284+}
25285+
1facf9fc 25286+/*
25287+ * create a new pseudo-link for @h_dentry on @bindex.
25288+ * the linked inode is held in aufs @inode.
25289+ */
25290+void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
25291+ struct dentry *h_dentry)
25292+{
25293+ struct super_block *sb;
25294+ struct au_sbinfo *sbinfo;
86dc4139 25295+ struct hlist_head *plink_hlist;
5afbbe0d 25296+ struct au_icntnr *icntnr;
86dc4139
AM
25297+ struct au_sphlhead *sphl;
25298+ int found, err, cnt, i;
1facf9fc 25299+
25300+ sb = inode->i_sb;
25301+ sbinfo = au_sbi(sb);
25302+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25303+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25304+
86dc4139 25305+ found = au_plink_test(inode);
4a4d8108 25306+ if (found)
1facf9fc 25307+ return;
4a4d8108 25308+
86dc4139
AM
25309+ i = au_plink_hash(inode->i_ino);
25310+ sphl = sbinfo->si_plink + i;
25311+ plink_hlist = &sphl->head;
5afbbe0d 25312+ au_igrab(inode);
1facf9fc 25313+
86dc4139 25314+ spin_lock(&sphl->spin);
5afbbe0d
AM
25315+ hlist_for_each_entry(icntnr, plink_hlist, plink) {
25316+ if (&icntnr->vfs_inode == inode) {
4a4d8108
AM
25317+ found = 1;
25318+ break;
25319+ }
1facf9fc 25320+ }
5afbbe0d
AM
25321+ if (!found) {
25322+ icntnr = container_of(inode, struct au_icntnr, vfs_inode);
25323+ hlist_add_head_rcu(&icntnr->plink, plink_hlist);
25324+ }
86dc4139 25325+ spin_unlock(&sphl->spin);
4a4d8108 25326+ if (!found) {
86dc4139
AM
25327+ cnt = au_sphl_count(sphl);
25328+#define msg "unexpectedly unblanced or too many pseudo-links"
25329+ if (cnt > AUFS_PLINK_WARN)
25330+ AuWarn1(msg ", %d\n", cnt);
25331+#undef msg
1facf9fc 25332+ err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
5afbbe0d
AM
25333+ if (unlikely(err)) {
25334+ pr_warn("err %d, damaged pseudo link.\n", err);
25335+ au_sphl_del_rcu(&icntnr->plink, sphl);
25336+ iput(&icntnr->vfs_inode);
4a4d8108 25337+ }
5afbbe0d
AM
25338+ } else
25339+ iput(&icntnr->vfs_inode);
1facf9fc 25340+}
25341+
25342+/* free all plinks */
e49829fe 25343+void au_plink_put(struct super_block *sb, int verbose)
1facf9fc 25344+{
86dc4139 25345+ int i, warned;
1facf9fc 25346+ struct au_sbinfo *sbinfo;
86dc4139
AM
25347+ struct hlist_head *plink_hlist;
25348+ struct hlist_node *tmp;
5afbbe0d 25349+ struct au_icntnr *icntnr;
1facf9fc 25350+
dece6358
AM
25351+ SiMustWriteLock(sb);
25352+
1facf9fc 25353+ sbinfo = au_sbi(sb);
25354+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25355+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25356+
1facf9fc 25357+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
25358+ warned = 0;
25359+ for (i = 0; i < AuPlink_NHASH; i++) {
25360+ plink_hlist = &sbinfo->si_plink[i].head;
25361+ if (!warned && verbose && !hlist_empty(plink_hlist)) {
25362+ pr_warn("pseudo-link is not flushed");
25363+ warned = 1;
25364+ }
5afbbe0d
AM
25365+ hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink)
25366+ iput(&icntnr->vfs_inode);
86dc4139
AM
25367+ INIT_HLIST_HEAD(plink_hlist);
25368+ }
1facf9fc 25369+}
25370+
e49829fe
JR
25371+void au_plink_clean(struct super_block *sb, int verbose)
25372+{
25373+ struct dentry *root;
25374+
25375+ root = sb->s_root;
25376+ aufs_write_lock(root);
25377+ if (au_opt_test(au_mntflags(sb), PLINK))
25378+ au_plink_put(sb, verbose);
25379+ aufs_write_unlock(root);
25380+}
25381+
86dc4139
AM
25382+static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
25383+{
25384+ int do_put;
5afbbe0d 25385+ aufs_bindex_t btop, bbot, bindex;
86dc4139
AM
25386+
25387+ do_put = 0;
5afbbe0d
AM
25388+ btop = au_ibtop(inode);
25389+ bbot = au_ibbot(inode);
25390+ if (btop >= 0) {
25391+ for (bindex = btop; bindex <= bbot; bindex++) {
86dc4139
AM
25392+ if (!au_h_iptr(inode, bindex)
25393+ || au_ii_br_id(inode, bindex) != br_id)
25394+ continue;
25395+ au_set_h_iptr(inode, bindex, NULL, 0);
25396+ do_put = 1;
25397+ break;
25398+ }
25399+ if (do_put)
5afbbe0d 25400+ for (bindex = btop; bindex <= bbot; bindex++)
86dc4139
AM
25401+ if (au_h_iptr(inode, bindex)) {
25402+ do_put = 0;
25403+ break;
25404+ }
25405+ } else
25406+ do_put = 1;
25407+
25408+ return do_put;
25409+}
25410+
1facf9fc 25411+/* free the plinks on a branch specified by @br_id */
25412+void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
25413+{
25414+ struct au_sbinfo *sbinfo;
86dc4139
AM
25415+ struct hlist_head *plink_hlist;
25416+ struct hlist_node *tmp;
5afbbe0d 25417+ struct au_icntnr *icntnr;
1facf9fc 25418+ struct inode *inode;
86dc4139 25419+ int i, do_put;
1facf9fc 25420+
dece6358
AM
25421+ SiMustWriteLock(sb);
25422+
1facf9fc 25423+ sbinfo = au_sbi(sb);
25424+ AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
e49829fe 25425+ AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
1facf9fc 25426+
1facf9fc 25427+ /* no spin_lock since sbinfo is write-locked */
86dc4139
AM
25428+ for (i = 0; i < AuPlink_NHASH; i++) {
25429+ plink_hlist = &sbinfo->si_plink[i].head;
5afbbe0d
AM
25430+ hlist_for_each_entry_safe(icntnr, tmp, plink_hlist, plink) {
25431+ inode = au_igrab(&icntnr->vfs_inode);
86dc4139
AM
25432+ ii_write_lock_child(inode);
25433+ do_put = au_plink_do_half_refresh(inode, br_id);
5afbbe0d
AM
25434+ if (do_put) {
25435+ hlist_del(&icntnr->plink);
25436+ iput(inode);
25437+ }
86dc4139
AM
25438+ ii_write_unlock(inode);
25439+ iput(inode);
dece6358 25440+ }
dece6358
AM
25441+ }
25442+}
7f207e10
AM
25443diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
25444--- /usr/share/empty/fs/aufs/poll.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 25445+++ linux/fs/aufs/poll.c 2016-07-25 19:05:34.817826663 +0200
b912730e 25446@@ -0,0 +1,52 @@
dece6358 25447+/*
8cdd5066 25448+ * Copyright (C) 2005-2016 Junjiro R. Okajima
dece6358
AM
25449+ *
25450+ * This program, aufs is free software; you can redistribute it and/or modify
25451+ * it under the terms of the GNU General Public License as published by
25452+ * the Free Software Foundation; either version 2 of the License, or
25453+ * (at your option) any later version.
25454+ *
25455+ * This program is distributed in the hope that it will be useful,
25456+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25457+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25458+ * GNU General Public License for more details.
25459+ *
25460+ * You should have received a copy of the GNU General Public License
523b37e3 25461+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
dece6358
AM
25462+ */
25463+
1308ab2a 25464+/*
25465+ * poll operation
25466+ * There is only one filesystem which implements ->poll operation, currently.
25467+ */
25468+
25469+#include "aufs.h"
25470+
25471+unsigned int aufs_poll(struct file *file, poll_table *wait)
25472+{
25473+ unsigned int mask;
25474+ int err;
25475+ struct file *h_file;
1308ab2a 25476+ struct super_block *sb;
25477+
25478+ /* We should pretend an error happened. */
25479+ mask = POLLERR /* | POLLIN | POLLOUT */;
b912730e 25480+ sb = file->f_path.dentry->d_sb;
e49829fe 25481+ si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
b912730e
AM
25482+
25483+ h_file = au_read_pre(file, /*keep_fi*/0);
25484+ err = PTR_ERR(h_file);
25485+ if (IS_ERR(h_file))
1308ab2a 25486+ goto out;
25487+
25488+ /* it is not an error if h_file has no operation */
25489+ mask = DEFAULT_POLLMASK;
523b37e3 25490+ if (h_file->f_op->poll)
1308ab2a 25491+ mask = h_file->f_op->poll(h_file, wait);
b912730e 25492+ fput(h_file); /* instead of au_read_post() */
1308ab2a 25493+
4f0767ce 25494+out:
1308ab2a 25495+ si_read_unlock(sb);
25496+ AuTraceErr((int)mask);
25497+ return mask;
25498+}
c1595e42
JR
25499diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
25500--- /usr/share/empty/fs/aufs/posix_acl.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 25501+++ linux/fs/aufs/posix_acl.c 2016-07-25 19:05:34.817826663 +0200
8cdd5066 25502@@ -0,0 +1,98 @@
c1595e42 25503+/*
8cdd5066 25504+ * Copyright (C) 2014-2016 Junjiro R. Okajima
c1595e42
JR
25505+ *
25506+ * This program, aufs is free software; you can redistribute it and/or modify
25507+ * it under the terms of the GNU General Public License as published by
25508+ * the Free Software Foundation; either version 2 of the License, or
25509+ * (at your option) any later version.
25510+ *
25511+ * This program is distributed in the hope that it will be useful,
25512+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25513+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25514+ * GNU General Public License for more details.
25515+ *
25516+ * You should have received a copy of the GNU General Public License
25517+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
25518+ */
25519+
25520+/*
25521+ * posix acl operations
25522+ */
25523+
25524+#include <linux/fs.h>
c1595e42
JR
25525+#include "aufs.h"
25526+
25527+struct posix_acl *aufs_get_acl(struct inode *inode, int type)
25528+{
25529+ struct posix_acl *acl;
25530+ int err;
25531+ aufs_bindex_t bindex;
25532+ struct inode *h_inode;
25533+ struct super_block *sb;
25534+
25535+ acl = NULL;
25536+ sb = inode->i_sb;
25537+ si_read_lock(sb, AuLock_FLUSH);
25538+ ii_read_lock_child(inode);
25539+ if (!(sb->s_flags & MS_POSIXACL))
25540+ goto out;
25541+
5afbbe0d 25542+ bindex = au_ibtop(inode);
c1595e42
JR
25543+ h_inode = au_h_iptr(inode, bindex);
25544+ if (unlikely(!h_inode
25545+ || ((h_inode->i_mode & S_IFMT)
25546+ != (inode->i_mode & S_IFMT)))) {
25547+ err = au_busy_or_stale();
25548+ acl = ERR_PTR(err);
25549+ goto out;
25550+ }
25551+
25552+ /* always topmost only */
25553+ acl = get_acl(h_inode, type);
25554+
25555+out:
25556+ ii_read_unlock(inode);
25557+ si_read_unlock(sb);
25558+
25559+ AuTraceErrPtr(acl);
25560+ return acl;
25561+}
25562+
25563+int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
25564+{
25565+ int err;
25566+ ssize_t ssz;
25567+ struct dentry *dentry;
25568+ struct au_srxattr arg = {
25569+ .type = AU_ACL_SET,
25570+ .u.acl_set = {
25571+ .acl = acl,
25572+ .type = type
25573+ },
25574+ };
25575+
5afbbe0d
AM
25576+ IMustLock(inode);
25577+
c1595e42
JR
25578+ if (inode->i_ino == AUFS_ROOT_INO)
25579+ dentry = dget(inode->i_sb->s_root);
25580+ else {
25581+ dentry = d_find_alias(inode);
25582+ if (!dentry)
25583+ dentry = d_find_any_alias(inode);
25584+ if (!dentry) {
25585+ pr_warn("cannot handle this inode, "
25586+ "please report to aufs-users ML\n");
25587+ err = -ENOENT;
25588+ goto out;
25589+ }
25590+ }
25591+
5afbbe0d 25592+ ssz = au_srxattr(dentry, inode, &arg);
c1595e42
JR
25593+ dput(dentry);
25594+ err = ssz;
25595+ if (ssz >= 0)
25596+ err = 0;
25597+
25598+out:
c1595e42
JR
25599+ return err;
25600+}
7f207e10
AM
25601diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
25602--- /usr/share/empty/fs/aufs/procfs.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 25603+++ linux/fs/aufs/procfs.c 2016-07-25 19:05:34.817826663 +0200
523b37e3 25604@@ -0,0 +1,169 @@
e49829fe 25605+/*
8cdd5066 25606+ * Copyright (C) 2010-2016 Junjiro R. Okajima
e49829fe
JR
25607+ *
25608+ * This program, aufs is free software; you can redistribute it and/or modify
25609+ * it under the terms of the GNU General Public License as published by
25610+ * the Free Software Foundation; either version 2 of the License, or
25611+ * (at your option) any later version.
25612+ *
25613+ * This program is distributed in the hope that it will be useful,
25614+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25615+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25616+ * GNU General Public License for more details.
25617+ *
25618+ * You should have received a copy of the GNU General Public License
523b37e3 25619+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
e49829fe
JR
25620+ */
25621+
25622+/*
25623+ * procfs interfaces
25624+ */
25625+
25626+#include <linux/proc_fs.h>
25627+#include "aufs.h"
25628+
25629+static int au_procfs_plm_release(struct inode *inode, struct file *file)
25630+{
25631+ struct au_sbinfo *sbinfo;
25632+
25633+ sbinfo = file->private_data;
25634+ if (sbinfo) {
25635+ au_plink_maint_leave(sbinfo);
25636+ kobject_put(&sbinfo->si_kobj);
25637+ }
25638+
25639+ return 0;
25640+}
25641+
25642+static void au_procfs_plm_write_clean(struct file *file)
25643+{
25644+ struct au_sbinfo *sbinfo;
25645+
25646+ sbinfo = file->private_data;
25647+ if (sbinfo)
25648+ au_plink_clean(sbinfo->si_sb, /*verbose*/0);
25649+}
25650+
25651+static int au_procfs_plm_write_si(struct file *file, unsigned long id)
25652+{
25653+ int err;
25654+ struct super_block *sb;
25655+ struct au_sbinfo *sbinfo;
25656+
25657+ err = -EBUSY;
25658+ if (unlikely(file->private_data))
25659+ goto out;
25660+
25661+ sb = NULL;
53392da6 25662+ /* don't use au_sbilist_lock() here */
e49829fe 25663+ spin_lock(&au_sbilist.spin);
5afbbe0d 25664+ hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
e49829fe
JR
25665+ if (id == sysaufs_si_id(sbinfo)) {
25666+ kobject_get(&sbinfo->si_kobj);
25667+ sb = sbinfo->si_sb;
25668+ break;
25669+ }
25670+ spin_unlock(&au_sbilist.spin);
25671+
25672+ err = -EINVAL;
25673+ if (unlikely(!sb))
25674+ goto out;
25675+
25676+ err = au_plink_maint_enter(sb);
25677+ if (!err)
25678+ /* keep kobject_get() */
25679+ file->private_data = sbinfo;
25680+ else
25681+ kobject_put(&sbinfo->si_kobj);
25682+out:
25683+ return err;
25684+}
25685+
25686+/*
25687+ * Accept a valid "si=xxxx" only.
25688+ * Once it is accepted successfully, accept "clean" too.
25689+ */
25690+static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
25691+ size_t count, loff_t *ppos)
25692+{
25693+ ssize_t err;
25694+ unsigned long id;
25695+ /* last newline is allowed */
25696+ char buf[3 + sizeof(unsigned long) * 2 + 1];
25697+
25698+ err = -EACCES;
25699+ if (unlikely(!capable(CAP_SYS_ADMIN)))
25700+ goto out;
25701+
25702+ err = -EINVAL;
25703+ if (unlikely(count > sizeof(buf)))
25704+ goto out;
25705+
25706+ err = copy_from_user(buf, ubuf, count);
25707+ if (unlikely(err)) {
25708+ err = -EFAULT;
25709+ goto out;
25710+ }
25711+ buf[count] = 0;
25712+
25713+ err = -EINVAL;
25714+ if (!strcmp("clean", buf)) {
25715+ au_procfs_plm_write_clean(file);
25716+ goto out_success;
25717+ } else if (unlikely(strncmp("si=", buf, 3)))
25718+ goto out;
25719+
9dbd164d 25720+ err = kstrtoul(buf + 3, 16, &id);
e49829fe
JR
25721+ if (unlikely(err))
25722+ goto out;
25723+
25724+ err = au_procfs_plm_write_si(file, id);
25725+ if (unlikely(err))
25726+ goto out;
25727+
25728+out_success:
25729+ err = count; /* success */
25730+out:
25731+ return err;
25732+}
25733+
25734+static const struct file_operations au_procfs_plm_fop = {
25735+ .write = au_procfs_plm_write,
25736+ .release = au_procfs_plm_release,
25737+ .owner = THIS_MODULE
25738+};
25739+
25740+/* ---------------------------------------------------------------------- */
25741+
25742+static struct proc_dir_entry *au_procfs_dir;
25743+
25744+void au_procfs_fin(void)
25745+{
25746+ remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
25747+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25748+}
25749+
25750+int __init au_procfs_init(void)
25751+{
25752+ int err;
25753+ struct proc_dir_entry *entry;
25754+
25755+ err = -ENOMEM;
25756+ au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
25757+ if (unlikely(!au_procfs_dir))
25758+ goto out;
25759+
25760+ entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | S_IWUSR,
25761+ au_procfs_dir, &au_procfs_plm_fop);
25762+ if (unlikely(!entry))
25763+ goto out_dir;
25764+
25765+ err = 0;
25766+ goto out; /* success */
25767+
25768+
25769+out_dir:
25770+ remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
25771+out:
25772+ return err;
25773+}
7f207e10
AM
25774diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
25775--- /usr/share/empty/fs/aufs/rdu.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
25776+++ linux/fs/aufs/rdu.c 2016-07-25 19:05:34.817826663 +0200
25777@@ -0,0 +1,381 @@
1308ab2a 25778+/*
8cdd5066 25779+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1308ab2a 25780+ *
25781+ * This program, aufs is free software; you can redistribute it and/or modify
25782+ * it under the terms of the GNU General Public License as published by
25783+ * the Free Software Foundation; either version 2 of the License, or
25784+ * (at your option) any later version.
25785+ *
25786+ * This program is distributed in the hope that it will be useful,
25787+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25788+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25789+ * GNU General Public License for more details.
25790+ *
25791+ * You should have received a copy of the GNU General Public License
523b37e3 25792+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1308ab2a 25793+ */
25794+
25795+/*
25796+ * readdir in userspace.
25797+ */
25798+
b752ccd1 25799+#include <linux/compat.h>
4a4d8108 25800+#include <linux/fs_stack.h>
1308ab2a 25801+#include <linux/security.h>
1308ab2a 25802+#include "aufs.h"
25803+
25804+/* bits for struct aufs_rdu.flags */
25805+#define AuRdu_CALLED 1
25806+#define AuRdu_CONT (1 << 1)
25807+#define AuRdu_FULL (1 << 2)
25808+#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
7f207e10
AM
25809+#define au_fset_rdu(flags, name) \
25810+ do { (flags) |= AuRdu_##name; } while (0)
25811+#define au_fclr_rdu(flags, name) \
25812+ do { (flags) &= ~AuRdu_##name; } while (0)
1308ab2a 25813+
25814+struct au_rdu_arg {
392086de 25815+ struct dir_context ctx;
1308ab2a 25816+ struct aufs_rdu *rdu;
25817+ union au_rdu_ent_ul ent;
25818+ unsigned long end;
25819+
25820+ struct super_block *sb;
25821+ int err;
25822+};
25823+
392086de 25824+static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
1308ab2a 25825+ loff_t offset, u64 h_ino, unsigned int d_type)
25826+{
25827+ int err, len;
392086de 25828+ struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
1308ab2a 25829+ struct aufs_rdu *rdu = arg->rdu;
25830+ struct au_rdu_ent ent;
25831+
25832+ err = 0;
25833+ arg->err = 0;
25834+ au_fset_rdu(rdu->cookie.flags, CALLED);
25835+ len = au_rdu_len(nlen);
25836+ if (arg->ent.ul + len < arg->end) {
25837+ ent.ino = h_ino;
25838+ ent.bindex = rdu->cookie.bindex;
25839+ ent.type = d_type;
25840+ ent.nlen = nlen;
4a4d8108
AM
25841+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
25842+ ent.type = DT_UNKNOWN;
1308ab2a 25843+
9dbd164d 25844+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 25845+ err = -EFAULT;
25846+ if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
25847+ goto out;
25848+ if (copy_to_user(arg->ent.e->name, name, nlen))
25849+ goto out;
25850+ /* the terminating NULL */
25851+ if (__put_user(0, arg->ent.e->name + nlen))
25852+ goto out;
25853+ err = 0;
25854+ /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
25855+ arg->ent.ul += len;
25856+ rdu->rent++;
25857+ } else {
25858+ err = -EFAULT;
25859+ au_fset_rdu(rdu->cookie.flags, FULL);
25860+ rdu->full = 1;
25861+ rdu->tail = arg->ent;
25862+ }
25863+
4f0767ce 25864+out:
1308ab2a 25865+ /* AuTraceErr(err); */
25866+ return err;
25867+}
25868+
25869+static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
25870+{
25871+ int err;
25872+ loff_t offset;
25873+ struct au_rdu_cookie *cookie = &arg->rdu->cookie;
25874+
92d182d2 25875+ /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
1308ab2a 25876+ offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
25877+ err = offset;
25878+ if (unlikely(offset != cookie->h_pos))
25879+ goto out;
25880+
25881+ err = 0;
25882+ do {
25883+ arg->err = 0;
25884+ au_fclr_rdu(cookie->flags, CALLED);
25885+ /* smp_mb(); */
392086de 25886+ err = vfsub_iterate_dir(h_file, &arg->ctx);
1308ab2a 25887+ if (err >= 0)
25888+ err = arg->err;
25889+ } while (!err
25890+ && au_ftest_rdu(cookie->flags, CALLED)
25891+ && !au_ftest_rdu(cookie->flags, FULL));
25892+ cookie->h_pos = h_file->f_pos;
25893+
4f0767ce 25894+out:
1308ab2a 25895+ AuTraceErr(err);
25896+ return err;
25897+}
25898+
25899+static int au_rdu(struct file *file, struct aufs_rdu *rdu)
25900+{
25901+ int err;
5afbbe0d 25902+ aufs_bindex_t bbot;
392086de
AM
25903+ struct au_rdu_arg arg = {
25904+ .ctx = {
2000de60 25905+ .actor = au_rdu_fill
392086de
AM
25906+ }
25907+ };
1308ab2a 25908+ struct dentry *dentry;
25909+ struct inode *inode;
25910+ struct file *h_file;
25911+ struct au_rdu_cookie *cookie = &rdu->cookie;
25912+
25913+ err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
25914+ if (unlikely(err)) {
25915+ err = -EFAULT;
25916+ AuTraceErr(err);
25917+ goto out;
25918+ }
25919+ rdu->rent = 0;
25920+ rdu->tail = rdu->ent;
25921+ rdu->full = 0;
25922+ arg.rdu = rdu;
25923+ arg.ent = rdu->ent;
25924+ arg.end = arg.ent.ul;
25925+ arg.end += rdu->sz;
25926+
25927+ err = -ENOTDIR;
5afbbe0d 25928+ if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
1308ab2a 25929+ goto out;
25930+
25931+ err = security_file_permission(file, MAY_READ);
25932+ AuTraceErr(err);
25933+ if (unlikely(err))
25934+ goto out;
25935+
2000de60 25936+ dentry = file->f_path.dentry;
5527c038 25937+ inode = d_inode(dentry);
5afbbe0d 25938+ inode_lock_shared(inode);
1308ab2a 25939+
25940+ arg.sb = inode->i_sb;
e49829fe
JR
25941+ err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
25942+ if (unlikely(err))
25943+ goto out_mtx;
027c5e7a
AM
25944+ err = au_alive_dir(dentry);
25945+ if (unlikely(err))
25946+ goto out_si;
e49829fe 25947+ /* todo: reval? */
1308ab2a 25948+ fi_read_lock(file);
25949+
25950+ err = -EAGAIN;
25951+ if (unlikely(au_ftest_rdu(cookie->flags, CONT)
25952+ && cookie->generation != au_figen(file)))
25953+ goto out_unlock;
25954+
25955+ err = 0;
25956+ if (!rdu->blk) {
25957+ rdu->blk = au_sbi(arg.sb)->si_rdblk;
25958+ if (!rdu->blk)
25959+ rdu->blk = au_dir_size(file, /*dentry*/NULL);
25960+ }
5afbbe0d
AM
25961+ bbot = au_fbtop(file);
25962+ if (cookie->bindex < bbot)
25963+ cookie->bindex = bbot;
25964+ bbot = au_fbbot_dir(file);
25965+ /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
25966+ for (; !err && cookie->bindex <= bbot;
1308ab2a 25967+ cookie->bindex++, cookie->h_pos = 0) {
4a4d8108 25968+ h_file = au_hf_dir(file, cookie->bindex);
1308ab2a 25969+ if (!h_file)
25970+ continue;
25971+
25972+ au_fclr_rdu(cookie->flags, FULL);
25973+ err = au_rdu_do(h_file, &arg);
25974+ AuTraceErr(err);
25975+ if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
25976+ break;
25977+ }
25978+ AuDbg("rent %llu\n", rdu->rent);
25979+
25980+ if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
25981+ rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
25982+ au_fset_rdu(cookie->flags, CONT);
25983+ cookie->generation = au_figen(file);
25984+ }
25985+
25986+ ii_read_lock_child(inode);
5afbbe0d 25987+ fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
1308ab2a 25988+ ii_read_unlock(inode);
25989+
4f0767ce 25990+out_unlock:
1308ab2a 25991+ fi_read_unlock(file);
027c5e7a 25992+out_si:
1308ab2a 25993+ si_read_unlock(arg.sb);
4f0767ce 25994+out_mtx:
5afbbe0d 25995+ inode_unlock_shared(inode);
4f0767ce 25996+out:
1308ab2a 25997+ AuTraceErr(err);
25998+ return err;
25999+}
26000+
26001+static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
26002+{
26003+ int err;
26004+ ino_t ino;
26005+ unsigned long long nent;
26006+ union au_rdu_ent_ul *u;
26007+ struct au_rdu_ent ent;
26008+ struct super_block *sb;
26009+
26010+ err = 0;
26011+ nent = rdu->nent;
26012+ u = &rdu->ent;
2000de60 26013+ sb = file->f_path.dentry->d_sb;
1308ab2a 26014+ si_read_lock(sb, AuLock_FLUSH);
26015+ while (nent-- > 0) {
9dbd164d 26016+ /* unnecessary to support mmap_sem since this is a dir */
1308ab2a 26017+ err = copy_from_user(&ent, u->e, sizeof(ent));
4a4d8108
AM
26018+ if (!err)
26019+ err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
1308ab2a 26020+ if (unlikely(err)) {
26021+ err = -EFAULT;
26022+ AuTraceErr(err);
26023+ break;
26024+ }
26025+
26026+ /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
26027+ if (!ent.wh)
26028+ err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
26029+ else
26030+ err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
26031+ &ino);
26032+ if (unlikely(err)) {
26033+ AuTraceErr(err);
26034+ break;
26035+ }
26036+
26037+ err = __put_user(ino, &u->e->ino);
26038+ if (unlikely(err)) {
26039+ err = -EFAULT;
26040+ AuTraceErr(err);
26041+ break;
26042+ }
26043+ u->ul += au_rdu_len(ent.nlen);
26044+ }
26045+ si_read_unlock(sb);
26046+
26047+ return err;
26048+}
26049+
26050+/* ---------------------------------------------------------------------- */
26051+
26052+static int au_rdu_verify(struct aufs_rdu *rdu)
26053+{
b752ccd1 26054+ AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
1308ab2a 26055+ "%llu, b%d, 0x%x, g%u}\n",
b752ccd1 26056+ rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
1308ab2a 26057+ rdu->blk,
26058+ rdu->rent, rdu->shwh, rdu->full,
26059+ rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
26060+ rdu->cookie.generation);
dece6358 26061+
b752ccd1 26062+ if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
1308ab2a 26063+ return 0;
dece6358 26064+
b752ccd1
AM
26065+ AuDbg("%u:%u\n",
26066+ rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
1308ab2a 26067+ return -EINVAL;
26068+}
26069+
26070+long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
dece6358 26071+{
1308ab2a 26072+ long err, e;
26073+ struct aufs_rdu rdu;
26074+ void __user *p = (void __user *)arg;
dece6358 26075+
1308ab2a 26076+ err = copy_from_user(&rdu, p, sizeof(rdu));
26077+ if (unlikely(err)) {
26078+ err = -EFAULT;
26079+ AuTraceErr(err);
26080+ goto out;
26081+ }
26082+ err = au_rdu_verify(&rdu);
dece6358
AM
26083+ if (unlikely(err))
26084+ goto out;
26085+
1308ab2a 26086+ switch (cmd) {
26087+ case AUFS_CTL_RDU:
26088+ err = au_rdu(file, &rdu);
26089+ if (unlikely(err))
26090+ break;
dece6358 26091+
1308ab2a 26092+ e = copy_to_user(p, &rdu, sizeof(rdu));
26093+ if (unlikely(e)) {
26094+ err = -EFAULT;
26095+ AuTraceErr(err);
26096+ }
26097+ break;
26098+ case AUFS_CTL_RDU_INO:
26099+ err = au_rdu_ino(file, &rdu);
26100+ break;
26101+
26102+ default:
4a4d8108 26103+ /* err = -ENOTTY; */
1308ab2a 26104+ err = -EINVAL;
26105+ }
dece6358 26106+
4f0767ce 26107+out:
1308ab2a 26108+ AuTraceErr(err);
26109+ return err;
1facf9fc 26110+}
b752ccd1
AM
26111+
26112+#ifdef CONFIG_COMPAT
26113+long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
26114+{
26115+ long err, e;
26116+ struct aufs_rdu rdu;
26117+ void __user *p = compat_ptr(arg);
26118+
26119+ /* todo: get_user()? */
26120+ err = copy_from_user(&rdu, p, sizeof(rdu));
26121+ if (unlikely(err)) {
26122+ err = -EFAULT;
26123+ AuTraceErr(err);
26124+ goto out;
26125+ }
26126+ rdu.ent.e = compat_ptr(rdu.ent.ul);
26127+ err = au_rdu_verify(&rdu);
26128+ if (unlikely(err))
26129+ goto out;
26130+
26131+ switch (cmd) {
26132+ case AUFS_CTL_RDU:
26133+ err = au_rdu(file, &rdu);
26134+ if (unlikely(err))
26135+ break;
26136+
26137+ rdu.ent.ul = ptr_to_compat(rdu.ent.e);
26138+ rdu.tail.ul = ptr_to_compat(rdu.tail.e);
26139+ e = copy_to_user(p, &rdu, sizeof(rdu));
26140+ if (unlikely(e)) {
26141+ err = -EFAULT;
26142+ AuTraceErr(err);
26143+ }
26144+ break;
26145+ case AUFS_CTL_RDU_INO:
26146+ err = au_rdu_ino(file, &rdu);
26147+ break;
26148+
26149+ default:
26150+ /* err = -ENOTTY; */
26151+ err = -EINVAL;
26152+ }
26153+
4f0767ce 26154+out:
b752ccd1
AM
26155+ AuTraceErr(err);
26156+ return err;
26157+}
26158+#endif
7f207e10
AM
26159diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
26160--- /usr/share/empty/fs/aufs/rwsem.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
26161+++ linux/fs/aufs/rwsem.h 2016-07-25 19:05:34.817826663 +0200
26162@@ -0,0 +1,198 @@
1facf9fc 26163+/*
8cdd5066 26164+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26165+ *
26166+ * This program, aufs is free software; you can redistribute it and/or modify
26167+ * it under the terms of the GNU General Public License as published by
26168+ * the Free Software Foundation; either version 2 of the License, or
26169+ * (at your option) any later version.
dece6358
AM
26170+ *
26171+ * This program is distributed in the hope that it will be useful,
26172+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26173+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26174+ * GNU General Public License for more details.
26175+ *
26176+ * You should have received a copy of the GNU General Public License
523b37e3 26177+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26178+ */
26179+
26180+/*
26181+ * simple read-write semaphore wrappers
26182+ */
26183+
26184+#ifndef __AUFS_RWSEM_H__
26185+#define __AUFS_RWSEM_H__
26186+
26187+#ifdef __KERNEL__
26188+
4a4d8108 26189+#include "debug.h"
dece6358
AM
26190+
26191+struct au_rwsem {
26192+ struct rw_semaphore rwsem;
26193+#ifdef CONFIG_AUFS_DEBUG
26194+ /* just for debugging, not almighty counter */
26195+ atomic_t rcnt, wcnt;
26196+#endif
26197+};
26198+
5afbbe0d
AM
26199+#ifdef CONFIG_LOCKDEP
26200+#define au_lockdep_set_name(rw) \
26201+ lockdep_set_class_and_name(&(rw)->rwsem, \
26202+ /*original key*/(rw)->rwsem.dep_map.key, \
26203+ /*name*/#rw)
26204+#else
26205+#define au_lockdep_set_name(rw) do {} while (0)
26206+#endif
26207+
dece6358
AM
26208+#ifdef CONFIG_AUFS_DEBUG
26209+#define AuDbgCntInit(rw) do { \
26210+ atomic_set(&(rw)->rcnt, 0); \
26211+ atomic_set(&(rw)->wcnt, 0); \
26212+ smp_mb(); /* atomic set */ \
26213+} while (0)
26214+
5afbbe0d
AM
26215+#define AuDbgCnt(rw, cnt) atomic_read(&(rw)->cnt)
26216+#define AuDbgCntInc(rw, cnt) atomic_inc(&(rw)->cnt)
26217+#define AuDbgCntDec(rw, cnt) WARN_ON(atomic_dec_return(&(rw)->cnt) < 0)
26218+#define AuDbgRcntInc(rw) AuDbgCntInc(rw, rcnt)
26219+#define AuDbgRcntDec(rw) AuDbgCntDec(rw, rcnt)
26220+#define AuDbgWcntInc(rw) AuDbgCntInc(rw, wcnt)
26221+#define AuDbgWcntDec(rw) AuDbgCntDec(rw, wcnt)
dece6358 26222+#else
5afbbe0d 26223+#define AuDbgCnt(rw, cnt) 0
dece6358
AM
26224+#define AuDbgCntInit(rw) do {} while (0)
26225+#define AuDbgRcntInc(rw) do {} while (0)
26226+#define AuDbgRcntDec(rw) do {} while (0)
26227+#define AuDbgWcntInc(rw) do {} while (0)
26228+#define AuDbgWcntDec(rw) do {} while (0)
26229+#endif /* CONFIG_AUFS_DEBUG */
26230+
26231+/* to debug easier, do not make them inlined functions */
5afbbe0d 26232+#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(&(rw)->rwsem))
dece6358 26233+/* rwsem_is_locked() is unusable */
5afbbe0d
AM
26234+#define AuRwMustReadLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0)
26235+#define AuRwMustWriteLock(rw) AuDebugOn(AuDbgCnt(rw, wcnt) <= 0)
26236+#define AuRwMustAnyLock(rw) AuDebugOn(AuDbgCnt(rw, rcnt) <= 0 \
26237+ && AuDbgCnt(rw, wcnt) <= 0)
26238+#define AuRwDestroy(rw) AuDebugOn(AuDbgCnt(rw, rcnt) \
26239+ || AuDbgCnt(rw, wcnt))
26240+
26241+#define au_rw_init(rw) do { \
26242+ AuDbgCntInit(rw); \
26243+ init_rwsem(&(rw)->rwsem); \
26244+ au_lockdep_set_name(rw); \
26245+ } while (0)
dece6358 26246+
5afbbe0d
AM
26247+#define au_rw_init_wlock(rw) do { \
26248+ au_rw_init(rw); \
26249+ down_write(&(rw)->rwsem); \
26250+ AuDbgWcntInc(rw); \
26251+ } while (0)
dece6358 26252+
5afbbe0d
AM
26253+#define au_rw_init_wlock_nested(rw, lsc) do { \
26254+ au_rw_init(rw); \
26255+ down_write_nested(&(rw)->rwsem, lsc); \
26256+ AuDbgWcntInc(rw); \
26257+ } while (0)
dece6358
AM
26258+
26259+static inline void au_rw_read_lock(struct au_rwsem *rw)
26260+{
26261+ down_read(&rw->rwsem);
26262+ AuDbgRcntInc(rw);
26263+}
26264+
26265+static inline void au_rw_read_lock_nested(struct au_rwsem *rw, unsigned int lsc)
26266+{
26267+ down_read_nested(&rw->rwsem, lsc);
26268+ AuDbgRcntInc(rw);
26269+}
26270+
26271+static inline void au_rw_read_unlock(struct au_rwsem *rw)
26272+{
26273+ AuRwMustReadLock(rw);
26274+ AuDbgRcntDec(rw);
26275+ up_read(&rw->rwsem);
26276+}
26277+
26278+static inline void au_rw_dgrade_lock(struct au_rwsem *rw)
26279+{
26280+ AuRwMustWriteLock(rw);
26281+ AuDbgRcntInc(rw);
26282+ AuDbgWcntDec(rw);
26283+ downgrade_write(&rw->rwsem);
26284+}
26285+
26286+static inline void au_rw_write_lock(struct au_rwsem *rw)
26287+{
26288+ down_write(&rw->rwsem);
26289+ AuDbgWcntInc(rw);
26290+}
26291+
26292+static inline void au_rw_write_lock_nested(struct au_rwsem *rw,
26293+ unsigned int lsc)
26294+{
26295+ down_write_nested(&rw->rwsem, lsc);
26296+ AuDbgWcntInc(rw);
26297+}
1facf9fc 26298+
dece6358
AM
26299+static inline void au_rw_write_unlock(struct au_rwsem *rw)
26300+{
26301+ AuRwMustWriteLock(rw);
26302+ AuDbgWcntDec(rw);
26303+ up_write(&rw->rwsem);
26304+}
26305+
26306+/* why is not _nested version defined */
26307+static inline int au_rw_read_trylock(struct au_rwsem *rw)
26308+{
076b876e
AM
26309+ int ret;
26310+
26311+ ret = down_read_trylock(&rw->rwsem);
dece6358
AM
26312+ if (ret)
26313+ AuDbgRcntInc(rw);
26314+ return ret;
26315+}
26316+
26317+static inline int au_rw_write_trylock(struct au_rwsem *rw)
26318+{
076b876e
AM
26319+ int ret;
26320+
26321+ ret = down_write_trylock(&rw->rwsem);
dece6358
AM
26322+ if (ret)
26323+ AuDbgWcntInc(rw);
26324+ return ret;
26325+}
26326+
5afbbe0d 26327+#undef AuDbgCntDec
dece6358
AM
26328+#undef AuDbgRcntInc
26329+#undef AuDbgRcntDec
dece6358 26330+#undef AuDbgWcntDec
1facf9fc 26331+
26332+#define AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26333+static inline void prefix##_read_lock(param) \
dece6358 26334+{ au_rw_read_lock(rwsem); } \
1facf9fc 26335+static inline void prefix##_write_lock(param) \
dece6358 26336+{ au_rw_write_lock(rwsem); } \
1facf9fc 26337+static inline int prefix##_read_trylock(param) \
dece6358 26338+{ return au_rw_read_trylock(rwsem); } \
1facf9fc 26339+static inline int prefix##_write_trylock(param) \
dece6358 26340+{ return au_rw_write_trylock(rwsem); }
1facf9fc 26341+/* why is not _nested version defined */
26342+/* static inline void prefix##_read_trylock_nested(param, lsc)
dece6358 26343+{ au_rw_read_trylock_nested(rwsem, lsc)); }
1facf9fc 26344+static inline void prefix##_write_trylock_nestd(param, lsc)
dece6358 26345+{ au_rw_write_trylock_nested(rwsem, lsc); } */
1facf9fc 26346+
26347+#define AuSimpleUnlockRwsemFuncs(prefix, param, rwsem) \
26348+static inline void prefix##_read_unlock(param) \
dece6358 26349+{ au_rw_read_unlock(rwsem); } \
1facf9fc 26350+static inline void prefix##_write_unlock(param) \
dece6358 26351+{ au_rw_write_unlock(rwsem); } \
1facf9fc 26352+static inline void prefix##_downgrade_lock(param) \
dece6358 26353+{ au_rw_dgrade_lock(rwsem); }
1facf9fc 26354+
26355+#define AuSimpleRwsemFuncs(prefix, param, rwsem) \
26356+ AuSimpleLockRwsemFuncs(prefix, param, rwsem) \
26357+ AuSimpleUnlockRwsemFuncs(prefix, param, rwsem)
26358+
26359+#endif /* __KERNEL__ */
26360+#endif /* __AUFS_RWSEM_H__ */
7f207e10
AM
26361diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
26362--- /usr/share/empty/fs/aufs/sbinfo.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
26363+++ linux/fs/aufs/sbinfo.c 2016-07-25 19:05:34.817826663 +0200
26364@@ -0,0 +1,353 @@
1facf9fc 26365+/*
8cdd5066 26366+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26367+ *
26368+ * This program, aufs is free software; you can redistribute it and/or modify
26369+ * it under the terms of the GNU General Public License as published by
26370+ * the Free Software Foundation; either version 2 of the License, or
26371+ * (at your option) any later version.
dece6358
AM
26372+ *
26373+ * This program is distributed in the hope that it will be useful,
26374+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26375+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26376+ * GNU General Public License for more details.
26377+ *
26378+ * You should have received a copy of the GNU General Public License
523b37e3 26379+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26380+ */
26381+
26382+/*
26383+ * superblock private data
26384+ */
26385+
26386+#include "aufs.h"
26387+
26388+/*
26389+ * they are necessary regardless sysfs is disabled.
26390+ */
26391+void au_si_free(struct kobject *kobj)
26392+{
86dc4139 26393+ int i;
1facf9fc 26394+ struct au_sbinfo *sbinfo;
b752ccd1 26395+ char *locked __maybe_unused; /* debug only */
1facf9fc 26396+
26397+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
86dc4139
AM
26398+ for (i = 0; i < AuPlink_NHASH; i++)
26399+ AuDebugOn(!hlist_empty(&sbinfo->si_plink[i].head));
5afbbe0d
AM
26400+ au_nwt_fin(&sbinfo->si_nowait);
26401+
26402+ AuDebugOn(percpu_counter_sum(&sbinfo->si_ninodes));
26403+ percpu_counter_destroy(&sbinfo->si_ninodes);
26404+ AuDebugOn(percpu_counter_sum(&sbinfo->si_nfiles));
26405+ percpu_counter_destroy(&sbinfo->si_nfiles);
1facf9fc 26406+
e49829fe 26407+ au_rw_write_lock(&sbinfo->si_rwsem);
1facf9fc 26408+ au_br_free(sbinfo);
e49829fe 26409+ au_rw_write_unlock(&sbinfo->si_rwsem);
b752ccd1 26410+
1facf9fc 26411+ kfree(sbinfo->si_branch);
febd17d6
JR
26412+ for (i = 0; i < AU_NPIDMAP; i++)
26413+ kfree(sbinfo->au_si_pid.pid_bitmap[i]);
26414+ mutex_destroy(&sbinfo->au_si_pid.pid_mtx);
1facf9fc 26415+ mutex_destroy(&sbinfo->si_xib_mtx);
dece6358 26416+ AuRwDestroy(&sbinfo->si_rwsem);
1facf9fc 26417+
26418+ kfree(sbinfo);
26419+}
26420+
26421+int au_si_alloc(struct super_block *sb)
26422+{
86dc4139 26423+ int err, i;
1facf9fc 26424+ struct au_sbinfo *sbinfo;
26425+
26426+ err = -ENOMEM;
4a4d8108 26427+ sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
1facf9fc 26428+ if (unlikely(!sbinfo))
26429+ goto out;
26430+
26431+ /* will be reallocated separately */
26432+ sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
26433+ if (unlikely(!sbinfo->si_branch))
febd17d6 26434+ goto out_sbinfo;
1facf9fc 26435+
1facf9fc 26436+ err = sysaufs_si_init(sbinfo);
26437+ if (unlikely(err))
26438+ goto out_br;
26439+
26440+ au_nwt_init(&sbinfo->si_nowait);
dece6358 26441+ au_rw_init_wlock(&sbinfo->si_rwsem);
febd17d6 26442+ mutex_init(&sbinfo->au_si_pid.pid_mtx);
b752ccd1 26443+
5afbbe0d
AM
26444+ percpu_counter_init(&sbinfo->si_ninodes, 0, GFP_NOFS);
26445+ percpu_counter_init(&sbinfo->si_nfiles, 0, GFP_NOFS);
7f207e10 26446+
5afbbe0d 26447+ sbinfo->si_bbot = -1;
392086de 26448+ sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
1facf9fc 26449+
26450+ sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
26451+ sbinfo->si_wbr_create = AuWbrCreate_Def;
4a4d8108
AM
26452+ sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
26453+ sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
1facf9fc 26454+
076b876e
AM
26455+ au_fhsm_init(sbinfo);
26456+
e49829fe 26457+ sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
1facf9fc 26458+
392086de
AM
26459+ sbinfo->si_xino_jiffy = jiffies;
26460+ sbinfo->si_xino_expire
26461+ = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
1facf9fc 26462+ mutex_init(&sbinfo->si_xib_mtx);
1facf9fc 26463+ sbinfo->si_xino_brid = -1;
26464+ /* leave si_xib_last_pindex and si_xib_next_bit */
26465+
b912730e
AM
26466+ au_sphl_init(&sbinfo->si_aopen);
26467+
e49829fe 26468+ sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
1facf9fc 26469+ sbinfo->si_rdblk = AUFS_RDBLK_DEF;
26470+ sbinfo->si_rdhash = AUFS_RDHASH_DEF;
26471+ sbinfo->si_dirwh = AUFS_DIRWH_DEF;
26472+
86dc4139
AM
26473+ for (i = 0; i < AuPlink_NHASH; i++)
26474+ au_sphl_init(sbinfo->si_plink + i);
1facf9fc 26475+ init_waitqueue_head(&sbinfo->si_plink_wq);
4a4d8108 26476+ spin_lock_init(&sbinfo->si_plink_maint_lock);
1facf9fc 26477+
523b37e3
AM
26478+ au_sphl_init(&sbinfo->si_files);
26479+
b95c5147
AM
26480+ /* with getattr by default */
26481+ sbinfo->si_iop_array = aufs_iop;
26482+
1facf9fc 26483+ /* leave other members for sysaufs and si_mnt. */
26484+ sbinfo->si_sb = sb;
26485+ sb->s_fs_info = sbinfo;
b752ccd1 26486+ si_pid_set(sb);
1facf9fc 26487+ return 0; /* success */
26488+
4f0767ce 26489+out_br:
1facf9fc 26490+ kfree(sbinfo->si_branch);
4f0767ce 26491+out_sbinfo:
1facf9fc 26492+ kfree(sbinfo);
4f0767ce 26493+out:
1facf9fc 26494+ return err;
26495+}
26496+
26497+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr)
26498+{
26499+ int err, sz;
26500+ struct au_branch **brp;
26501+
dece6358
AM
26502+ AuRwMustWriteLock(&sbinfo->si_rwsem);
26503+
1facf9fc 26504+ err = -ENOMEM;
5afbbe0d 26505+ sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
1facf9fc 26506+ if (unlikely(!sz))
26507+ sz = sizeof(*brp);
26508+ brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS);
26509+ if (brp) {
26510+ sbinfo->si_branch = brp;
26511+ err = 0;
26512+ }
26513+
26514+ return err;
26515+}
26516+
26517+/* ---------------------------------------------------------------------- */
26518+
26519+unsigned int au_sigen_inc(struct super_block *sb)
26520+{
26521+ unsigned int gen;
5527c038 26522+ struct inode *inode;
1facf9fc 26523+
dece6358
AM
26524+ SiMustWriteLock(sb);
26525+
1facf9fc 26526+ gen = ++au_sbi(sb)->si_generation;
26527+ au_update_digen(sb->s_root);
5527c038
JR
26528+ inode = d_inode(sb->s_root);
26529+ au_update_iigen(inode, /*half*/0);
26530+ inode->i_version++;
1facf9fc 26531+ return gen;
26532+}
26533+
26534+aufs_bindex_t au_new_br_id(struct super_block *sb)
26535+{
26536+ aufs_bindex_t br_id;
26537+ int i;
26538+ struct au_sbinfo *sbinfo;
26539+
dece6358
AM
26540+ SiMustWriteLock(sb);
26541+
1facf9fc 26542+ sbinfo = au_sbi(sb);
26543+ for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
26544+ br_id = ++sbinfo->si_last_br_id;
7f207e10 26545+ AuDebugOn(br_id < 0);
1facf9fc 26546+ if (br_id && au_br_index(sb, br_id) < 0)
26547+ return br_id;
26548+ }
26549+
26550+ return -1;
26551+}
26552+
26553+/* ---------------------------------------------------------------------- */
26554+
e49829fe
JR
26555+/* it is ok that new 'nwt' tasks are appended while we are sleeping */
26556+int si_read_lock(struct super_block *sb, int flags)
26557+{
26558+ int err;
26559+
26560+ err = 0;
26561+ if (au_ftest_lock(flags, FLUSH))
26562+ au_nwt_flush(&au_sbi(sb)->si_nowait);
26563+
26564+ si_noflush_read_lock(sb);
26565+ err = au_plink_maint(sb, flags);
26566+ if (unlikely(err))
26567+ si_read_unlock(sb);
26568+
26569+ return err;
26570+}
26571+
26572+int si_write_lock(struct super_block *sb, int flags)
26573+{
26574+ int err;
26575+
26576+ if (au_ftest_lock(flags, FLUSH))
26577+ au_nwt_flush(&au_sbi(sb)->si_nowait);
26578+
26579+ si_noflush_write_lock(sb);
26580+ err = au_plink_maint(sb, flags);
26581+ if (unlikely(err))
26582+ si_write_unlock(sb);
26583+
26584+ return err;
26585+}
26586+
1facf9fc 26587+/* dentry and super_block lock. call at entry point */
e49829fe 26588+int aufs_read_lock(struct dentry *dentry, int flags)
1facf9fc 26589+{
e49829fe 26590+ int err;
027c5e7a 26591+ struct super_block *sb;
e49829fe 26592+
027c5e7a
AM
26593+ sb = dentry->d_sb;
26594+ err = si_read_lock(sb, flags);
26595+ if (unlikely(err))
26596+ goto out;
26597+
26598+ if (au_ftest_lock(flags, DW))
26599+ di_write_lock_child(dentry);
26600+ else
26601+ di_read_lock_child(dentry, flags);
26602+
26603+ if (au_ftest_lock(flags, GEN)) {
26604+ err = au_digen_test(dentry, au_sigen(sb));
79b8bda9
AM
26605+ if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
26606+ AuDebugOn(!err && au_dbrange_test(dentry));
26607+ else if (!err)
26608+ err = au_dbrange_test(dentry);
027c5e7a
AM
26609+ if (unlikely(err))
26610+ aufs_read_unlock(dentry, flags);
e49829fe
JR
26611+ }
26612+
027c5e7a 26613+out:
e49829fe 26614+ return err;
1facf9fc 26615+}
26616+
26617+void aufs_read_unlock(struct dentry *dentry, int flags)
26618+{
26619+ if (au_ftest_lock(flags, DW))
26620+ di_write_unlock(dentry);
26621+ else
26622+ di_read_unlock(dentry, flags);
26623+ si_read_unlock(dentry->d_sb);
26624+}
26625+
26626+void aufs_write_lock(struct dentry *dentry)
26627+{
e49829fe 26628+ si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
1facf9fc 26629+ di_write_lock_child(dentry);
26630+}
26631+
26632+void aufs_write_unlock(struct dentry *dentry)
26633+{
26634+ di_write_unlock(dentry);
26635+ si_write_unlock(dentry->d_sb);
26636+}
26637+
e49829fe 26638+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
1facf9fc 26639+{
e49829fe 26640+ int err;
027c5e7a
AM
26641+ unsigned int sigen;
26642+ struct super_block *sb;
e49829fe 26643+
027c5e7a
AM
26644+ sb = d1->d_sb;
26645+ err = si_read_lock(sb, flags);
26646+ if (unlikely(err))
26647+ goto out;
26648+
b95c5147 26649+ di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
027c5e7a
AM
26650+
26651+ if (au_ftest_lock(flags, GEN)) {
26652+ sigen = au_sigen(sb);
26653+ err = au_digen_test(d1, sigen);
26654+ AuDebugOn(!err && au_dbrange_test(d1));
26655+ if (!err) {
26656+ err = au_digen_test(d2, sigen);
26657+ AuDebugOn(!err && au_dbrange_test(d2));
26658+ }
26659+ if (unlikely(err))
26660+ aufs_read_and_write_unlock2(d1, d2);
26661+ }
26662+
26663+out:
e49829fe 26664+ return err;
1facf9fc 26665+}
26666+
26667+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
26668+{
26669+ di_write_unlock2(d1, d2);
26670+ si_read_unlock(d1->d_sb);
26671+}
b752ccd1
AM
26672+
26673+/* ---------------------------------------------------------------------- */
26674+
febd17d6 26675+static void si_pid_alloc(struct au_si_pid *au_si_pid, int idx)
b752ccd1 26676+{
febd17d6 26677+ unsigned long *p;
b752ccd1 26678+
febd17d6
JR
26679+ BUILD_BUG_ON(sizeof(unsigned long) !=
26680+ sizeof(*au_si_pid->pid_bitmap));
b752ccd1 26681+
febd17d6
JR
26682+ mutex_lock(&au_si_pid->pid_mtx);
26683+ p = au_si_pid->pid_bitmap[idx];
26684+ while (!p) {
26685+ /*
26686+ * bad approach.
26687+ * but keeping 'si_pid_set()' void is more important.
26688+ */
26689+ p = kcalloc(BITS_TO_LONGS(AU_PIDSTEP),
26690+ sizeof(*au_si_pid->pid_bitmap),
26691+ GFP_NOFS);
26692+ if (p)
26693+ break;
26694+ cond_resched();
26695+ }
26696+ au_si_pid->pid_bitmap[idx] = p;
26697+ mutex_unlock(&au_si_pid->pid_mtx);
b752ccd1
AM
26698+}
26699+
febd17d6 26700+void si_pid_set(struct super_block *sb)
b752ccd1 26701+{
febd17d6
JR
26702+ pid_t bit;
26703+ int idx;
26704+ unsigned long *bitmap;
26705+ struct au_si_pid *au_si_pid;
26706+
26707+ si_pid_idx_bit(&idx, &bit);
26708+ au_si_pid = &au_sbi(sb)->au_si_pid;
26709+ bitmap = au_si_pid->pid_bitmap[idx];
26710+ if (!bitmap) {
26711+ si_pid_alloc(au_si_pid, idx);
26712+ bitmap = au_si_pid->pid_bitmap[idx];
26713+ }
26714+ AuDebugOn(test_bit(bit, bitmap));
26715+ set_bit(bit, bitmap);
26716+ /* smp_mb(); */
b752ccd1 26717+}
7f207e10
AM
26718diff -urN /usr/share/empty/fs/aufs/spl.h linux/fs/aufs/spl.h
26719--- /usr/share/empty/fs/aufs/spl.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 26720+++ linux/fs/aufs/spl.h 2016-07-25 19:05:34.817826663 +0200
523b37e3 26721@@ -0,0 +1,111 @@
1facf9fc 26722+/*
8cdd5066 26723+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26724+ *
26725+ * This program, aufs is free software; you can redistribute it and/or modify
26726+ * it under the terms of the GNU General Public License as published by
26727+ * the Free Software Foundation; either version 2 of the License, or
26728+ * (at your option) any later version.
dece6358
AM
26729+ *
26730+ * This program is distributed in the hope that it will be useful,
26731+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26732+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26733+ * GNU General Public License for more details.
26734+ *
26735+ * You should have received a copy of the GNU General Public License
523b37e3 26736+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26737+ */
26738+
26739+/*
26740+ * simple list protected by a spinlock
26741+ */
26742+
26743+#ifndef __AUFS_SPL_H__
26744+#define __AUFS_SPL_H__
26745+
26746+#ifdef __KERNEL__
26747+
1facf9fc 26748+struct au_splhead {
26749+ spinlock_t spin;
26750+ struct list_head head;
26751+};
26752+
26753+static inline void au_spl_init(struct au_splhead *spl)
26754+{
26755+ spin_lock_init(&spl->spin);
26756+ INIT_LIST_HEAD(&spl->head);
26757+}
26758+
26759+static inline void au_spl_add(struct list_head *list, struct au_splhead *spl)
26760+{
26761+ spin_lock(&spl->spin);
26762+ list_add(list, &spl->head);
26763+ spin_unlock(&spl->spin);
26764+}
26765+
26766+static inline void au_spl_del(struct list_head *list, struct au_splhead *spl)
26767+{
26768+ spin_lock(&spl->spin);
26769+ list_del(list);
26770+ spin_unlock(&spl->spin);
26771+}
26772+
4a4d8108
AM
26773+static inline void au_spl_del_rcu(struct list_head *list,
26774+ struct au_splhead *spl)
26775+{
26776+ spin_lock(&spl->spin);
26777+ list_del_rcu(list);
26778+ spin_unlock(&spl->spin);
26779+}
26780+
86dc4139
AM
26781+/* ---------------------------------------------------------------------- */
26782+
26783+struct au_sphlhead {
26784+ spinlock_t spin;
26785+ struct hlist_head head;
26786+};
26787+
26788+static inline void au_sphl_init(struct au_sphlhead *sphl)
26789+{
26790+ spin_lock_init(&sphl->spin);
26791+ INIT_HLIST_HEAD(&sphl->head);
26792+}
26793+
26794+static inline void au_sphl_add(struct hlist_node *hlist,
26795+ struct au_sphlhead *sphl)
26796+{
26797+ spin_lock(&sphl->spin);
26798+ hlist_add_head(hlist, &sphl->head);
26799+ spin_unlock(&sphl->spin);
26800+}
26801+
26802+static inline void au_sphl_del(struct hlist_node *hlist,
26803+ struct au_sphlhead *sphl)
26804+{
26805+ spin_lock(&sphl->spin);
26806+ hlist_del(hlist);
26807+ spin_unlock(&sphl->spin);
26808+}
26809+
26810+static inline void au_sphl_del_rcu(struct hlist_node *hlist,
26811+ struct au_sphlhead *sphl)
26812+{
26813+ spin_lock(&sphl->spin);
26814+ hlist_del_rcu(hlist);
26815+ spin_unlock(&sphl->spin);
26816+}
26817+
26818+static inline unsigned long au_sphl_count(struct au_sphlhead *sphl)
26819+{
26820+ unsigned long cnt;
26821+ struct hlist_node *pos;
26822+
26823+ cnt = 0;
26824+ spin_lock(&sphl->spin);
26825+ hlist_for_each(pos, &sphl->head)
26826+ cnt++;
26827+ spin_unlock(&sphl->spin);
26828+ return cnt;
26829+}
26830+
1facf9fc 26831+#endif /* __KERNEL__ */
26832+#endif /* __AUFS_SPL_H__ */
7f207e10
AM
26833diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
26834--- /usr/share/empty/fs/aufs/super.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 26835+++ linux/fs/aufs/super.c 2016-07-25 19:05:34.817826663 +0200
be52b249 26836@@ -0,0 +1,1039 @@
1facf9fc 26837+/*
8cdd5066 26838+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 26839+ *
26840+ * This program, aufs is free software; you can redistribute it and/or modify
26841+ * it under the terms of the GNU General Public License as published by
26842+ * the Free Software Foundation; either version 2 of the License, or
26843+ * (at your option) any later version.
dece6358
AM
26844+ *
26845+ * This program is distributed in the hope that it will be useful,
26846+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
26847+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26848+ * GNU General Public License for more details.
26849+ *
26850+ * You should have received a copy of the GNU General Public License
523b37e3 26851+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 26852+ */
26853+
26854+/*
26855+ * mount and super_block operations
26856+ */
26857+
f6c5ef8b 26858+#include <linux/mm.h>
1facf9fc 26859+#include <linux/seq_file.h>
26860+#include <linux/statfs.h>
7f207e10 26861+#include <linux/vmalloc.h>
1facf9fc 26862+#include "aufs.h"
26863+
26864+/*
26865+ * super_operations
26866+ */
26867+static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
26868+{
26869+ struct au_icntnr *c;
26870+
26871+ c = au_cache_alloc_icntnr();
26872+ if (c) {
027c5e7a 26873+ au_icntnr_init(c);
1facf9fc 26874+ c->vfs_inode.i_version = 1; /* sigen(sb); */
26875+ c->iinfo.ii_hinode = NULL;
26876+ return &c->vfs_inode;
26877+ }
26878+ return NULL;
26879+}
26880+
027c5e7a
AM
26881+static void aufs_destroy_inode_cb(struct rcu_head *head)
26882+{
26883+ struct inode *inode = container_of(head, struct inode, i_rcu);
26884+
b4510431 26885+ INIT_HLIST_HEAD(&inode->i_dentry);
027c5e7a
AM
26886+ au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
26887+}
26888+
1facf9fc 26889+static void aufs_destroy_inode(struct inode *inode)
26890+{
5afbbe0d
AM
26891+ if (!au_is_bad_inode(inode))
26892+ au_iinfo_fin(inode);
027c5e7a 26893+ call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
1facf9fc 26894+}
26895+
26896+struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
26897+{
26898+ struct inode *inode;
26899+ int err;
26900+
26901+ inode = iget_locked(sb, ino);
26902+ if (unlikely(!inode)) {
26903+ inode = ERR_PTR(-ENOMEM);
26904+ goto out;
26905+ }
26906+ if (!(inode->i_state & I_NEW))
26907+ goto out;
26908+
26909+ err = au_xigen_new(inode);
26910+ if (!err)
26911+ err = au_iinfo_init(inode);
26912+ if (!err)
26913+ inode->i_version++;
26914+ else {
26915+ iget_failed(inode);
26916+ inode = ERR_PTR(err);
26917+ }
26918+
4f0767ce 26919+out:
1facf9fc 26920+ /* never return NULL */
26921+ AuDebugOn(!inode);
26922+ AuTraceErrPtr(inode);
26923+ return inode;
26924+}
26925+
26926+/* lock free root dinfo */
26927+static int au_show_brs(struct seq_file *seq, struct super_block *sb)
26928+{
26929+ int err;
5afbbe0d 26930+ aufs_bindex_t bindex, bbot;
1facf9fc 26931+ struct path path;
4a4d8108 26932+ struct au_hdentry *hdp;
1facf9fc 26933+ struct au_branch *br;
076b876e 26934+ au_br_perm_str_t perm;
1facf9fc 26935+
26936+ err = 0;
5afbbe0d
AM
26937+ bbot = au_sbbot(sb);
26938+ bindex = 0;
26939+ hdp = au_hdentry(au_di(sb->s_root), bindex);
26940+ for (; !err && bindex <= bbot; bindex++, hdp++) {
1facf9fc 26941+ br = au_sbr(sb, bindex);
86dc4139 26942+ path.mnt = au_br_mnt(br);
5afbbe0d 26943+ path.dentry = hdp->hd_dentry;
1facf9fc 26944+ err = au_seq_path(seq, &path);
79b8bda9 26945+ if (!err) {
076b876e 26946+ au_optstr_br_perm(&perm, br->br_perm);
79b8bda9 26947+ seq_printf(seq, "=%s", perm.a);
5afbbe0d 26948+ if (bindex != bbot)
79b8bda9 26949+ seq_putc(seq, ':');
1e00d052 26950+ }
1facf9fc 26951+ }
79b8bda9
AM
26952+ if (unlikely(err || seq_has_overflowed(seq)))
26953+ err = -E2BIG;
1facf9fc 26954+
26955+ return err;
26956+}
26957+
26958+static void au_show_wbr_create(struct seq_file *m, int v,
26959+ struct au_sbinfo *sbinfo)
26960+{
26961+ const char *pat;
26962+
dece6358
AM
26963+ AuRwMustAnyLock(&sbinfo->si_rwsem);
26964+
c2b27bf2 26965+ seq_puts(m, ",create=");
1facf9fc 26966+ pat = au_optstr_wbr_create(v);
26967+ switch (v) {
26968+ case AuWbrCreate_TDP:
26969+ case AuWbrCreate_RR:
26970+ case AuWbrCreate_MFS:
26971+ case AuWbrCreate_PMFS:
c2b27bf2 26972+ seq_puts(m, pat);
1facf9fc 26973+ break;
26974+ case AuWbrCreate_MFSV:
26975+ seq_printf(m, /*pat*/"mfs:%lu",
e49829fe
JR
26976+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26977+ / MSEC_PER_SEC);
1facf9fc 26978+ break;
26979+ case AuWbrCreate_PMFSV:
26980+ seq_printf(m, /*pat*/"pmfs:%lu",
e49829fe
JR
26981+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26982+ / MSEC_PER_SEC);
1facf9fc 26983+ break;
26984+ case AuWbrCreate_MFSRR:
26985+ seq_printf(m, /*pat*/"mfsrr:%llu",
26986+ sbinfo->si_wbr_mfs.mfsrr_watermark);
26987+ break;
26988+ case AuWbrCreate_MFSRRV:
26989+ seq_printf(m, /*pat*/"mfsrr:%llu:%lu",
26990+ sbinfo->si_wbr_mfs.mfsrr_watermark,
e49829fe
JR
26991+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
26992+ / MSEC_PER_SEC);
1facf9fc 26993+ break;
392086de
AM
26994+ case AuWbrCreate_PMFSRR:
26995+ seq_printf(m, /*pat*/"pmfsrr:%llu",
26996+ sbinfo->si_wbr_mfs.mfsrr_watermark);
26997+ break;
26998+ case AuWbrCreate_PMFSRRV:
26999+ seq_printf(m, /*pat*/"pmfsrr:%llu:%lu",
27000+ sbinfo->si_wbr_mfs.mfsrr_watermark,
27001+ jiffies_to_msecs(sbinfo->si_wbr_mfs.mfs_expire)
27002+ / MSEC_PER_SEC);
27003+ break;
1facf9fc 27004+ }
27005+}
27006+
7eafdf33 27007+static int au_show_xino(struct seq_file *seq, struct super_block *sb)
1facf9fc 27008+{
27009+#ifdef CONFIG_SYSFS
27010+ return 0;
27011+#else
27012+ int err;
27013+ const int len = sizeof(AUFS_XINO_FNAME) - 1;
27014+ aufs_bindex_t bindex, brid;
1facf9fc 27015+ struct qstr *name;
27016+ struct file *f;
27017+ struct dentry *d, *h_root;
27018+
dece6358
AM
27019+ AuRwMustAnyLock(&sbinfo->si_rwsem);
27020+
1facf9fc 27021+ err = 0;
1facf9fc 27022+ f = au_sbi(sb)->si_xib;
27023+ if (!f)
27024+ goto out;
27025+
27026+ /* stop printing the default xino path on the first writable branch */
27027+ h_root = NULL;
27028+ brid = au_xino_brid(sb);
27029+ if (brid >= 0) {
27030+ bindex = au_br_index(sb, brid);
5afbbe0d 27031+ h_root = au_hdentry(au_di(sb->s_root), bindex)->hd_dentry;
1facf9fc 27032+ }
2000de60 27033+ d = f->f_path.dentry;
1facf9fc 27034+ name = &d->d_name;
27035+ /* safe ->d_parent because the file is unlinked */
27036+ if (d->d_parent == h_root
27037+ && name->len == len
27038+ && !memcmp(name->name, AUFS_XINO_FNAME, len))
27039+ goto out;
27040+
27041+ seq_puts(seq, ",xino=");
27042+ err = au_xino_path(seq, f);
27043+
4f0767ce 27044+out:
1facf9fc 27045+ return err;
27046+#endif
27047+}
27048+
27049+/* seq_file will re-call me in case of too long string */
7eafdf33 27050+static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
1facf9fc 27051+{
027c5e7a 27052+ int err;
1facf9fc 27053+ unsigned int mnt_flags, v;
27054+ struct super_block *sb;
27055+ struct au_sbinfo *sbinfo;
27056+
27057+#define AuBool(name, str) do { \
27058+ v = au_opt_test(mnt_flags, name); \
27059+ if (v != au_opt_test(AuOpt_Def, name)) \
27060+ seq_printf(m, ",%s" #str, v ? "" : "no"); \
27061+} while (0)
27062+
27063+#define AuStr(name, str) do { \
27064+ v = mnt_flags & AuOptMask_##name; \
27065+ if (v != (AuOpt_Def & AuOptMask_##name)) \
27066+ seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
27067+} while (0)
27068+
27069+#define AuUInt(name, str, val) do { \
27070+ if (val != AUFS_##name##_DEF) \
27071+ seq_printf(m, "," #str "=%u", val); \
27072+} while (0)
27073+
7eafdf33 27074+ sb = dentry->d_sb;
c1595e42
JR
27075+ if (sb->s_flags & MS_POSIXACL)
27076+ seq_puts(m, ",acl");
27077+
27078+ /* lock free root dinfo */
1facf9fc 27079+ si_noflush_read_lock(sb);
27080+ sbinfo = au_sbi(sb);
27081+ seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
27082+
27083+ mnt_flags = au_mntflags(sb);
27084+ if (au_opt_test(mnt_flags, XINO)) {
7eafdf33 27085+ err = au_show_xino(m, sb);
1facf9fc 27086+ if (unlikely(err))
27087+ goto out;
27088+ } else
27089+ seq_puts(m, ",noxino");
27090+
27091+ AuBool(TRUNC_XINO, trunc_xino);
27092+ AuStr(UDBA, udba);
dece6358 27093+ AuBool(SHWH, shwh);
1facf9fc 27094+ AuBool(PLINK, plink);
4a4d8108 27095+ AuBool(DIO, dio);
076b876e 27096+ AuBool(DIRPERM1, dirperm1);
1facf9fc 27097+
27098+ v = sbinfo->si_wbr_create;
27099+ if (v != AuWbrCreate_Def)
27100+ au_show_wbr_create(m, v, sbinfo);
27101+
27102+ v = sbinfo->si_wbr_copyup;
27103+ if (v != AuWbrCopyup_Def)
27104+ seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
27105+
27106+ v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
27107+ if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
27108+ seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
27109+
27110+ AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
27111+
027c5e7a
AM
27112+ v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
27113+ AuUInt(RDCACHE, rdcache, v);
1facf9fc 27114+
27115+ AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
27116+ AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
27117+
076b876e
AM
27118+ au_fhsm_show(m, sbinfo);
27119+
1facf9fc 27120+ AuBool(SUM, sum);
27121+ /* AuBool(SUM_W, wsum); */
27122+ AuBool(WARN_PERM, warn_perm);
27123+ AuBool(VERBOSE, verbose);
27124+
4f0767ce 27125+out:
1facf9fc 27126+ /* be sure to print "br:" last */
27127+ if (!sysaufs_brs) {
27128+ seq_puts(m, ",br:");
27129+ au_show_brs(m, sb);
27130+ }
27131+ si_read_unlock(sb);
27132+ return 0;
27133+
1facf9fc 27134+#undef AuBool
27135+#undef AuStr
4a4d8108 27136+#undef AuUInt
1facf9fc 27137+}
27138+
27139+/* ---------------------------------------------------------------------- */
27140+
27141+/* sum mode which returns the summation for statfs(2) */
27142+
27143+static u64 au_add_till_max(u64 a, u64 b)
27144+{
27145+ u64 old;
27146+
27147+ old = a;
27148+ a += b;
92d182d2
AM
27149+ if (old <= a)
27150+ return a;
27151+ return ULLONG_MAX;
27152+}
27153+
27154+static u64 au_mul_till_max(u64 a, long mul)
27155+{
27156+ u64 old;
27157+
27158+ old = a;
27159+ a *= mul;
27160+ if (old <= a)
1facf9fc 27161+ return a;
27162+ return ULLONG_MAX;
27163+}
27164+
27165+static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
27166+{
27167+ int err;
92d182d2 27168+ long bsize, factor;
1facf9fc 27169+ u64 blocks, bfree, bavail, files, ffree;
5afbbe0d 27170+ aufs_bindex_t bbot, bindex, i;
1facf9fc 27171+ unsigned char shared;
7f207e10 27172+ struct path h_path;
1facf9fc 27173+ struct super_block *h_sb;
27174+
92d182d2
AM
27175+ err = 0;
27176+ bsize = LONG_MAX;
27177+ files = 0;
27178+ ffree = 0;
1facf9fc 27179+ blocks = 0;
27180+ bfree = 0;
27181+ bavail = 0;
5afbbe0d
AM
27182+ bbot = au_sbbot(sb);
27183+ for (bindex = 0; bindex <= bbot; bindex++) {
7f207e10
AM
27184+ h_path.mnt = au_sbr_mnt(sb, bindex);
27185+ h_sb = h_path.mnt->mnt_sb;
1facf9fc 27186+ shared = 0;
92d182d2 27187+ for (i = 0; !shared && i < bindex; i++)
1facf9fc 27188+ shared = (au_sbr_sb(sb, i) == h_sb);
27189+ if (shared)
27190+ continue;
27191+
27192+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
27193+ h_path.dentry = h_path.mnt->mnt_root;
27194+ err = vfs_statfs(&h_path, buf);
1facf9fc 27195+ if (unlikely(err))
27196+ goto out;
27197+
92d182d2
AM
27198+ if (bsize > buf->f_bsize) {
27199+ /*
27200+ * we will reduce bsize, so we have to expand blocks
27201+ * etc. to match them again
27202+ */
27203+ factor = (bsize / buf->f_bsize);
27204+ blocks = au_mul_till_max(blocks, factor);
27205+ bfree = au_mul_till_max(bfree, factor);
27206+ bavail = au_mul_till_max(bavail, factor);
27207+ bsize = buf->f_bsize;
27208+ }
27209+
27210+ factor = (buf->f_bsize / bsize);
27211+ blocks = au_add_till_max(blocks,
27212+ au_mul_till_max(buf->f_blocks, factor));
27213+ bfree = au_add_till_max(bfree,
27214+ au_mul_till_max(buf->f_bfree, factor));
27215+ bavail = au_add_till_max(bavail,
27216+ au_mul_till_max(buf->f_bavail, factor));
1facf9fc 27217+ files = au_add_till_max(files, buf->f_files);
27218+ ffree = au_add_till_max(ffree, buf->f_ffree);
27219+ }
27220+
92d182d2 27221+ buf->f_bsize = bsize;
1facf9fc 27222+ buf->f_blocks = blocks;
27223+ buf->f_bfree = bfree;
27224+ buf->f_bavail = bavail;
27225+ buf->f_files = files;
27226+ buf->f_ffree = ffree;
92d182d2 27227+ buf->f_frsize = 0;
1facf9fc 27228+
4f0767ce 27229+out:
1facf9fc 27230+ return err;
27231+}
27232+
27233+static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
27234+{
27235+ int err;
7f207e10 27236+ struct path h_path;
1facf9fc 27237+ struct super_block *sb;
27238+
27239+ /* lock free root dinfo */
27240+ sb = dentry->d_sb;
27241+ si_noflush_read_lock(sb);
7f207e10 27242+ if (!au_opt_test(au_mntflags(sb), SUM)) {
1facf9fc 27243+ /* sb->s_root for NFS is unreliable */
7f207e10
AM
27244+ h_path.mnt = au_sbr_mnt(sb, 0);
27245+ h_path.dentry = h_path.mnt->mnt_root;
27246+ err = vfs_statfs(&h_path, buf);
27247+ } else
1facf9fc 27248+ err = au_statfs_sum(sb, buf);
27249+ si_read_unlock(sb);
27250+
27251+ if (!err) {
27252+ buf->f_type = AUFS_SUPER_MAGIC;
4a4d8108 27253+ buf->f_namelen = AUFS_MAX_NAMELEN;
1facf9fc 27254+ memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
27255+ }
27256+ /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
27257+
27258+ return err;
27259+}
27260+
27261+/* ---------------------------------------------------------------------- */
27262+
537831f9
AM
27263+static int aufs_sync_fs(struct super_block *sb, int wait)
27264+{
27265+ int err, e;
5afbbe0d 27266+ aufs_bindex_t bbot, bindex;
537831f9
AM
27267+ struct au_branch *br;
27268+ struct super_block *h_sb;
27269+
27270+ err = 0;
27271+ si_noflush_read_lock(sb);
5afbbe0d
AM
27272+ bbot = au_sbbot(sb);
27273+ for (bindex = 0; bindex <= bbot; bindex++) {
537831f9
AM
27274+ br = au_sbr(sb, bindex);
27275+ if (!au_br_writable(br->br_perm))
27276+ continue;
27277+
27278+ h_sb = au_sbr_sb(sb, bindex);
27279+ if (h_sb->s_op->sync_fs) {
27280+ e = h_sb->s_op->sync_fs(h_sb, wait);
27281+ if (unlikely(e && !err))
27282+ err = e;
27283+ /* go on even if an error happens */
27284+ }
27285+ }
27286+ si_read_unlock(sb);
27287+
27288+ return err;
27289+}
27290+
27291+/* ---------------------------------------------------------------------- */
27292+
1facf9fc 27293+/* final actions when unmounting a file system */
27294+static void aufs_put_super(struct super_block *sb)
27295+{
27296+ struct au_sbinfo *sbinfo;
27297+
27298+ sbinfo = au_sbi(sb);
27299+ if (!sbinfo)
27300+ return;
27301+
1facf9fc 27302+ dbgaufs_si_fin(sbinfo);
27303+ kobject_put(&sbinfo->si_kobj);
27304+}
27305+
27306+/* ---------------------------------------------------------------------- */
27307+
79b8bda9
AM
27308+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
27309+ struct super_block *sb, void *arg)
7f207e10
AM
27310+{
27311+ void *array;
076b876e 27312+ unsigned long long n, sz;
7f207e10
AM
27313+
27314+ array = NULL;
27315+ n = 0;
27316+ if (!*hint)
27317+ goto out;
27318+
27319+ if (*hint > ULLONG_MAX / sizeof(array)) {
27320+ array = ERR_PTR(-EMFILE);
27321+ pr_err("hint %llu\n", *hint);
27322+ goto out;
27323+ }
27324+
076b876e
AM
27325+ sz = sizeof(array) * *hint;
27326+ array = kzalloc(sz, GFP_NOFS);
7f207e10 27327+ if (unlikely(!array))
076b876e 27328+ array = vzalloc(sz);
7f207e10
AM
27329+ if (unlikely(!array)) {
27330+ array = ERR_PTR(-ENOMEM);
27331+ goto out;
27332+ }
27333+
79b8bda9 27334+ n = cb(sb, array, *hint, arg);
7f207e10
AM
27335+ AuDebugOn(n > *hint);
27336+
27337+out:
27338+ *hint = n;
27339+ return array;
27340+}
27341+
79b8bda9 27342+static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
7f207e10
AM
27343+ unsigned long long max __maybe_unused,
27344+ void *arg)
27345+{
27346+ unsigned long long n;
27347+ struct inode **p, *inode;
27348+ struct list_head *head;
27349+
27350+ n = 0;
27351+ p = a;
27352+ head = arg;
79b8bda9 27353+ spin_lock(&sb->s_inode_list_lock);
7f207e10 27354+ list_for_each_entry(inode, head, i_sb_list) {
5afbbe0d
AM
27355+ if (!au_is_bad_inode(inode)
27356+ && au_ii(inode)->ii_btop >= 0) {
2cbb1c4b
JR
27357+ spin_lock(&inode->i_lock);
27358+ if (atomic_read(&inode->i_count)) {
27359+ au_igrab(inode);
27360+ *p++ = inode;
27361+ n++;
27362+ AuDebugOn(n > max);
27363+ }
27364+ spin_unlock(&inode->i_lock);
7f207e10
AM
27365+ }
27366+ }
79b8bda9 27367+ spin_unlock(&sb->s_inode_list_lock);
7f207e10
AM
27368+
27369+ return n;
27370+}
27371+
27372+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
27373+{
5afbbe0d 27374+ *max = au_ninodes(sb);
79b8bda9 27375+ return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
7f207e10
AM
27376+}
27377+
27378+void au_iarray_free(struct inode **a, unsigned long long max)
27379+{
27380+ unsigned long long ull;
27381+
27382+ for (ull = 0; ull < max; ull++)
27383+ iput(a[ull]);
be52b249 27384+ kvfree(a);
7f207e10
AM
27385+}
27386+
27387+/* ---------------------------------------------------------------------- */
27388+
1facf9fc 27389+/*
27390+ * refresh dentry and inode at remount time.
27391+ */
027c5e7a
AM
27392+/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
27393+static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
27394+ struct dentry *parent)
1facf9fc 27395+{
27396+ int err;
1facf9fc 27397+
27398+ di_write_lock_child(dentry);
1facf9fc 27399+ di_read_lock_parent(parent, AuLock_IR);
027c5e7a
AM
27400+ err = au_refresh_dentry(dentry, parent);
27401+ if (!err && dir_flags)
5527c038 27402+ au_hn_reset(d_inode(dentry), dir_flags);
1facf9fc 27403+ di_read_unlock(parent, AuLock_IR);
1facf9fc 27404+ di_write_unlock(dentry);
27405+
27406+ return err;
27407+}
27408+
027c5e7a
AM
27409+static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
27410+ struct au_sbinfo *sbinfo,
b95c5147 27411+ const unsigned int dir_flags, unsigned int do_idop)
1facf9fc 27412+{
027c5e7a
AM
27413+ int err;
27414+ struct dentry *parent;
027c5e7a
AM
27415+
27416+ err = 0;
27417+ parent = dget_parent(dentry);
27418+ if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
5527c038
JR
27419+ if (d_really_is_positive(dentry)) {
27420+ if (!d_is_dir(dentry))
027c5e7a
AM
27421+ err = au_do_refresh(dentry, /*dir_flags*/0,
27422+ parent);
27423+ else {
27424+ err = au_do_refresh(dentry, dir_flags, parent);
27425+ if (unlikely(err))
27426+ au_fset_si(sbinfo, FAILED_REFRESH_DIR);
27427+ }
27428+ } else
27429+ err = au_do_refresh(dentry, /*dir_flags*/0, parent);
27430+ AuDbgDentry(dentry);
27431+ }
27432+ dput(parent);
27433+
79b8bda9 27434+ if (!err) {
b95c5147 27435+ if (do_idop)
79b8bda9
AM
27436+ au_refresh_dop(dentry, /*force_reval*/0);
27437+ } else
27438+ au_refresh_dop(dentry, /*force_reval*/1);
27439+
027c5e7a
AM
27440+ AuTraceErr(err);
27441+ return err;
1facf9fc 27442+}
27443+
b95c5147 27444+static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
1facf9fc 27445+{
27446+ int err, i, j, ndentry, e;
027c5e7a 27447+ unsigned int sigen;
1facf9fc 27448+ struct au_dcsub_pages dpages;
27449+ struct au_dpage *dpage;
027c5e7a
AM
27450+ struct dentry **dentries, *d;
27451+ struct au_sbinfo *sbinfo;
27452+ struct dentry *root = sb->s_root;
5527c038 27453+ const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
1facf9fc 27454+
b95c5147 27455+ if (do_idop)
79b8bda9
AM
27456+ au_refresh_dop(root, /*force_reval*/0);
27457+
027c5e7a
AM
27458+ err = au_dpages_init(&dpages, GFP_NOFS);
27459+ if (unlikely(err))
1facf9fc 27460+ goto out;
027c5e7a
AM
27461+ err = au_dcsub_pages(&dpages, root, NULL, NULL);
27462+ if (unlikely(err))
1facf9fc 27463+ goto out_dpages;
1facf9fc 27464+
027c5e7a
AM
27465+ sigen = au_sigen(sb);
27466+ sbinfo = au_sbi(sb);
27467+ for (i = 0; i < dpages.ndpage; i++) {
1facf9fc 27468+ dpage = dpages.dpages + i;
27469+ dentries = dpage->dentries;
27470+ ndentry = dpage->ndentry;
027c5e7a 27471+ for (j = 0; j < ndentry; j++) {
1facf9fc 27472+ d = dentries[j];
79b8bda9 27473+ e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
b95c5147 27474+ do_idop);
027c5e7a
AM
27475+ if (unlikely(e && !err))
27476+ err = e;
27477+ /* go on even err */
1facf9fc 27478+ }
27479+ }
27480+
4f0767ce 27481+out_dpages:
1facf9fc 27482+ au_dpages_free(&dpages);
4f0767ce 27483+out:
1facf9fc 27484+ return err;
27485+}
27486+
b95c5147 27487+static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
1facf9fc 27488+{
027c5e7a
AM
27489+ int err, e;
27490+ unsigned int sigen;
27491+ unsigned long long max, ull;
27492+ struct inode *inode, **array;
1facf9fc 27493+
027c5e7a
AM
27494+ array = au_iarray_alloc(sb, &max);
27495+ err = PTR_ERR(array);
27496+ if (IS_ERR(array))
27497+ goto out;
1facf9fc 27498+
27499+ err = 0;
027c5e7a
AM
27500+ sigen = au_sigen(sb);
27501+ for (ull = 0; ull < max; ull++) {
27502+ inode = array[ull];
076b876e
AM
27503+ if (unlikely(!inode))
27504+ break;
b95c5147
AM
27505+
27506+ e = 0;
27507+ ii_write_lock_child(inode);
537831f9 27508+ if (au_iigen(inode, NULL) != sigen) {
027c5e7a 27509+ e = au_refresh_hinode_self(inode);
1facf9fc 27510+ if (unlikely(e)) {
b95c5147 27511+ au_refresh_iop(inode, /*force_getattr*/1);
027c5e7a 27512+ pr_err("error %d, i%lu\n", e, inode->i_ino);
1facf9fc 27513+ if (!err)
27514+ err = e;
27515+ /* go on even if err */
27516+ }
27517+ }
b95c5147
AM
27518+ if (!e && do_idop)
27519+ au_refresh_iop(inode, /*force_getattr*/0);
27520+ ii_write_unlock(inode);
1facf9fc 27521+ }
27522+
027c5e7a 27523+ au_iarray_free(array, max);
1facf9fc 27524+
4f0767ce 27525+out:
1facf9fc 27526+ return err;
27527+}
27528+
b95c5147 27529+static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
1facf9fc 27530+{
027c5e7a
AM
27531+ int err, e;
27532+ unsigned int udba;
5afbbe0d 27533+ aufs_bindex_t bindex, bbot;
1facf9fc 27534+ struct dentry *root;
27535+ struct inode *inode;
027c5e7a 27536+ struct au_branch *br;
79b8bda9 27537+ struct au_sbinfo *sbi;
1facf9fc 27538+
27539+ au_sigen_inc(sb);
79b8bda9
AM
27540+ sbi = au_sbi(sb);
27541+ au_fclr_si(sbi, FAILED_REFRESH_DIR);
1facf9fc 27542+
27543+ root = sb->s_root;
27544+ DiMustNoWaiters(root);
5527c038 27545+ inode = d_inode(root);
1facf9fc 27546+ IiMustNoWaiters(inode);
1facf9fc 27547+
027c5e7a 27548+ udba = au_opt_udba(sb);
5afbbe0d
AM
27549+ bbot = au_sbbot(sb);
27550+ for (bindex = 0; bindex <= bbot; bindex++) {
027c5e7a
AM
27551+ br = au_sbr(sb, bindex);
27552+ err = au_hnotify_reset_br(udba, br, br->br_perm);
1facf9fc 27553+ if (unlikely(err))
027c5e7a
AM
27554+ AuIOErr("hnotify failed on br %d, %d, ignored\n",
27555+ bindex, err);
27556+ /* go on even if err */
1facf9fc 27557+ }
027c5e7a 27558+ au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
1facf9fc 27559+
b95c5147 27560+ if (do_idop) {
79b8bda9
AM
27561+ if (au_ftest_si(sbi, NO_DREVAL)) {
27562+ AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
27563+ sb->s_d_op = &aufs_dop_noreval;
b95c5147
AM
27564+ AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
27565+ sbi->si_iop_array = aufs_iop_nogetattr;
79b8bda9
AM
27566+ } else {
27567+ AuDebugOn(sb->s_d_op == &aufs_dop);
27568+ sb->s_d_op = &aufs_dop;
b95c5147
AM
27569+ AuDebugOn(sbi->si_iop_array == aufs_iop);
27570+ sbi->si_iop_array = aufs_iop;
79b8bda9 27571+ }
b95c5147
AM
27572+ pr_info("reset to %pf and %pf\n",
27573+ sb->s_d_op, sbi->si_iop_array);
79b8bda9
AM
27574+ }
27575+
027c5e7a 27576+ di_write_unlock(root);
b95c5147
AM
27577+ err = au_refresh_d(sb, do_idop);
27578+ e = au_refresh_i(sb, do_idop);
027c5e7a
AM
27579+ if (unlikely(e && !err))
27580+ err = e;
1facf9fc 27581+ /* aufs_write_lock() calls ..._child() */
27582+ di_write_lock_child(root);
027c5e7a
AM
27583+
27584+ au_cpup_attr_all(inode, /*force*/1);
27585+
27586+ if (unlikely(err))
27587+ AuIOErr("refresh failed, ignored, %d\n", err);
1facf9fc 27588+}
27589+
27590+/* stop extra interpretation of errno in mount(8), and strange error messages */
27591+static int cvt_err(int err)
27592+{
27593+ AuTraceErr(err);
27594+
27595+ switch (err) {
27596+ case -ENOENT:
27597+ case -ENOTDIR:
27598+ case -EEXIST:
27599+ case -EIO:
27600+ err = -EINVAL;
27601+ }
27602+ return err;
27603+}
27604+
27605+static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
27606+{
4a4d8108
AM
27607+ int err, do_dx;
27608+ unsigned int mntflags;
be52b249
AM
27609+ struct au_opts opts = {
27610+ .opt = NULL
27611+ };
1facf9fc 27612+ struct dentry *root;
27613+ struct inode *inode;
27614+ struct au_sbinfo *sbinfo;
27615+
27616+ err = 0;
27617+ root = sb->s_root;
27618+ if (!data || !*data) {
e49829fe
JR
27619+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27620+ if (!err) {
27621+ di_write_lock_child(root);
27622+ err = au_opts_verify(sb, *flags, /*pending*/0);
27623+ aufs_write_unlock(root);
27624+ }
1facf9fc 27625+ goto out;
27626+ }
27627+
27628+ err = -ENOMEM;
1facf9fc 27629+ opts.opt = (void *)__get_free_page(GFP_NOFS);
27630+ if (unlikely(!opts.opt))
27631+ goto out;
27632+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27633+ opts.flags = AuOpts_REMOUNT;
27634+ opts.sb_flags = *flags;
27635+
27636+ /* parse it before aufs lock */
27637+ err = au_opts_parse(sb, data, &opts);
27638+ if (unlikely(err))
27639+ goto out_opts;
27640+
27641+ sbinfo = au_sbi(sb);
5527c038 27642+ inode = d_inode(root);
febd17d6 27643+ inode_lock(inode);
e49829fe
JR
27644+ err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
27645+ if (unlikely(err))
27646+ goto out_mtx;
27647+ di_write_lock_child(root);
1facf9fc 27648+
27649+ /* au_opts_remount() may return an error */
27650+ err = au_opts_remount(sb, &opts);
27651+ au_opts_free(&opts);
27652+
027c5e7a 27653+ if (au_ftest_opts(opts.flags, REFRESH))
b95c5147 27654+ au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
1facf9fc 27655+
4a4d8108
AM
27656+ if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
27657+ mntflags = au_mntflags(sb);
27658+ do_dx = !!au_opt_test(mntflags, DIO);
27659+ au_dy_arefresh(do_dx);
27660+ }
27661+
076b876e 27662+ au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
1facf9fc 27663+ aufs_write_unlock(root);
953406b4 27664+
e49829fe 27665+out_mtx:
febd17d6 27666+ inode_unlock(inode);
4f0767ce 27667+out_opts:
1facf9fc 27668+ free_page((unsigned long)opts.opt);
4f0767ce 27669+out:
1facf9fc 27670+ err = cvt_err(err);
27671+ AuTraceErr(err);
27672+ return err;
27673+}
27674+
4a4d8108 27675+static const struct super_operations aufs_sop = {
1facf9fc 27676+ .alloc_inode = aufs_alloc_inode,
27677+ .destroy_inode = aufs_destroy_inode,
b752ccd1 27678+ /* always deleting, no clearing */
1facf9fc 27679+ .drop_inode = generic_delete_inode,
27680+ .show_options = aufs_show_options,
27681+ .statfs = aufs_statfs,
27682+ .put_super = aufs_put_super,
537831f9 27683+ .sync_fs = aufs_sync_fs,
1facf9fc 27684+ .remount_fs = aufs_remount_fs
27685+};
27686+
27687+/* ---------------------------------------------------------------------- */
27688+
27689+static int alloc_root(struct super_block *sb)
27690+{
27691+ int err;
27692+ struct inode *inode;
27693+ struct dentry *root;
27694+
27695+ err = -ENOMEM;
27696+ inode = au_iget_locked(sb, AUFS_ROOT_INO);
27697+ err = PTR_ERR(inode);
27698+ if (IS_ERR(inode))
27699+ goto out;
27700+
b95c5147 27701+ inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
1facf9fc 27702+ inode->i_fop = &aufs_dir_fop;
27703+ inode->i_mode = S_IFDIR;
9dbd164d 27704+ set_nlink(inode, 2);
1facf9fc 27705+ unlock_new_inode(inode);
27706+
92d182d2 27707+ root = d_make_root(inode);
1facf9fc 27708+ if (unlikely(!root))
92d182d2 27709+ goto out;
1facf9fc 27710+ err = PTR_ERR(root);
27711+ if (IS_ERR(root))
92d182d2 27712+ goto out;
1facf9fc 27713+
4a4d8108 27714+ err = au_di_init(root);
1facf9fc 27715+ if (!err) {
27716+ sb->s_root = root;
27717+ return 0; /* success */
27718+ }
27719+ dput(root);
1facf9fc 27720+
4f0767ce 27721+out:
1facf9fc 27722+ return err;
1facf9fc 27723+}
27724+
27725+static int aufs_fill_super(struct super_block *sb, void *raw_data,
27726+ int silent __maybe_unused)
27727+{
27728+ int err;
be52b249
AM
27729+ struct au_opts opts = {
27730+ .opt = NULL
27731+ };
79b8bda9 27732+ struct au_sbinfo *sbinfo;
1facf9fc 27733+ struct dentry *root;
27734+ struct inode *inode;
27735+ char *arg = raw_data;
27736+
27737+ if (unlikely(!arg || !*arg)) {
27738+ err = -EINVAL;
4a4d8108 27739+ pr_err("no arg\n");
1facf9fc 27740+ goto out;
27741+ }
27742+
27743+ err = -ENOMEM;
1facf9fc 27744+ opts.opt = (void *)__get_free_page(GFP_NOFS);
27745+ if (unlikely(!opts.opt))
27746+ goto out;
27747+ opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
27748+ opts.sb_flags = sb->s_flags;
27749+
27750+ err = au_si_alloc(sb);
27751+ if (unlikely(err))
27752+ goto out_opts;
79b8bda9 27753+ sbinfo = au_sbi(sb);
1facf9fc 27754+
27755+ /* all timestamps always follow the ones on the branch */
27756+ sb->s_flags |= MS_NOATIME | MS_NODIRATIME;
27757+ sb->s_op = &aufs_sop;
027c5e7a 27758+ sb->s_d_op = &aufs_dop;
1facf9fc 27759+ sb->s_magic = AUFS_SUPER_MAGIC;
27760+ sb->s_maxbytes = 0;
c1595e42 27761+ sb->s_stack_depth = 1;
1facf9fc 27762+ au_export_init(sb);
c1595e42 27763+ /* au_xattr_init(sb); */
1facf9fc 27764+
27765+ err = alloc_root(sb);
27766+ if (unlikely(err)) {
27767+ si_write_unlock(sb);
27768+ goto out_info;
27769+ }
27770+ root = sb->s_root;
5527c038 27771+ inode = d_inode(root);
1facf9fc 27772+
27773+ /*
27774+ * actually we can parse options regardless aufs lock here.
27775+ * but at remount time, parsing must be done before aufs lock.
27776+ * so we follow the same rule.
27777+ */
27778+ ii_write_lock_parent(inode);
27779+ aufs_write_unlock(root);
27780+ err = au_opts_parse(sb, arg, &opts);
27781+ if (unlikely(err))
27782+ goto out_root;
27783+
27784+ /* lock vfs_inode first, then aufs. */
febd17d6 27785+ inode_lock(inode);
1facf9fc 27786+ aufs_write_lock(root);
27787+ err = au_opts_mount(sb, &opts);
27788+ au_opts_free(&opts);
79b8bda9
AM
27789+ if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
27790+ sb->s_d_op = &aufs_dop_noreval;
27791+ pr_info("%pf\n", sb->s_d_op);
27792+ au_refresh_dop(root, /*force_reval*/0);
b95c5147
AM
27793+ sbinfo->si_iop_array = aufs_iop_nogetattr;
27794+ au_refresh_iop(inode, /*force_getattr*/0);
79b8bda9 27795+ }
1facf9fc 27796+ aufs_write_unlock(root);
febd17d6 27797+ inode_unlock(inode);
4a4d8108
AM
27798+ if (!err)
27799+ goto out_opts; /* success */
1facf9fc 27800+
4f0767ce 27801+out_root:
1facf9fc 27802+ dput(root);
27803+ sb->s_root = NULL;
4f0767ce 27804+out_info:
79b8bda9
AM
27805+ dbgaufs_si_fin(sbinfo);
27806+ kobject_put(&sbinfo->si_kobj);
1facf9fc 27807+ sb->s_fs_info = NULL;
4f0767ce 27808+out_opts:
1facf9fc 27809+ free_page((unsigned long)opts.opt);
4f0767ce 27810+out:
1facf9fc 27811+ AuTraceErr(err);
27812+ err = cvt_err(err);
27813+ AuTraceErr(err);
27814+ return err;
27815+}
27816+
27817+/* ---------------------------------------------------------------------- */
27818+
027c5e7a
AM
27819+static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
27820+ const char *dev_name __maybe_unused,
27821+ void *raw_data)
1facf9fc 27822+{
027c5e7a 27823+ struct dentry *root;
1facf9fc 27824+ struct super_block *sb;
27825+
27826+ /* all timestamps always follow the ones on the branch */
27827+ /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
027c5e7a
AM
27828+ root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
27829+ if (IS_ERR(root))
27830+ goto out;
27831+
27832+ sb = root->d_sb;
27833+ si_write_lock(sb, !AuLock_FLUSH);
27834+ sysaufs_brs_add(sb, 0);
27835+ si_write_unlock(sb);
27836+ au_sbilist_add(sb);
27837+
27838+out:
27839+ return root;
1facf9fc 27840+}
27841+
e49829fe
JR
27842+static void aufs_kill_sb(struct super_block *sb)
27843+{
27844+ struct au_sbinfo *sbinfo;
27845+
27846+ sbinfo = au_sbi(sb);
27847+ if (sbinfo) {
27848+ au_sbilist_del(sb);
27849+ aufs_write_lock(sb->s_root);
076b876e 27850+ au_fhsm_fin(sb);
e49829fe
JR
27851+ if (sbinfo->si_wbr_create_ops->fin)
27852+ sbinfo->si_wbr_create_ops->fin(sb);
27853+ if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
27854+ au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
b95c5147 27855+ au_remount_refresh(sb, /*do_idop*/0);
e49829fe
JR
27856+ }
27857+ if (au_opt_test(sbinfo->si_mntflags, PLINK))
27858+ au_plink_put(sb, /*verbose*/1);
27859+ au_xino_clr(sb);
1e00d052 27860+ sbinfo->si_sb = NULL;
e49829fe 27861+ aufs_write_unlock(sb->s_root);
e49829fe
JR
27862+ au_nwt_flush(&sbinfo->si_nowait);
27863+ }
98d9a5b1 27864+ kill_anon_super(sb);
e49829fe
JR
27865+}
27866+
1facf9fc 27867+struct file_system_type aufs_fs_type = {
27868+ .name = AUFS_FSTYPE,
c06a8ce3
AM
27869+ /* a race between rename and others */
27870+ .fs_flags = FS_RENAME_DOES_D_MOVE,
027c5e7a 27871+ .mount = aufs_mount,
e49829fe 27872+ .kill_sb = aufs_kill_sb,
1facf9fc 27873+ /* no need to __module_get() and module_put(). */
27874+ .owner = THIS_MODULE,
27875+};
7f207e10
AM
27876diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
27877--- /usr/share/empty/fs/aufs/super.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
27878+++ linux/fs/aufs/super.h 2016-07-25 19:05:34.817826663 +0200
27879@@ -0,0 +1,638 @@
1facf9fc 27880+/*
8cdd5066 27881+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 27882+ *
27883+ * This program, aufs is free software; you can redistribute it and/or modify
27884+ * it under the terms of the GNU General Public License as published by
27885+ * the Free Software Foundation; either version 2 of the License, or
27886+ * (at your option) any later version.
dece6358
AM
27887+ *
27888+ * This program is distributed in the hope that it will be useful,
27889+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
27890+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27891+ * GNU General Public License for more details.
27892+ *
27893+ * You should have received a copy of the GNU General Public License
523b37e3 27894+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 27895+ */
27896+
27897+/*
27898+ * super_block operations
27899+ */
27900+
27901+#ifndef __AUFS_SUPER_H__
27902+#define __AUFS_SUPER_H__
27903+
27904+#ifdef __KERNEL__
27905+
27906+#include <linux/fs.h>
5527c038 27907+#include <linux/kobject.h>
1facf9fc 27908+#include "rwsem.h"
27909+#include "spl.h"
27910+#include "wkq.h"
27911+
1facf9fc 27912+/* policies to select one among multiple writable branches */
27913+struct au_wbr_copyup_operations {
27914+ int (*copyup)(struct dentry *dentry);
27915+};
27916+
392086de
AM
27917+#define AuWbr_DIR 1 /* target is a dir */
27918+#define AuWbr_PARENT (1 << 1) /* always require a parent */
27919+
27920+#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
27921+#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
27922+#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
27923+
1facf9fc 27924+struct au_wbr_create_operations {
392086de 27925+ int (*create)(struct dentry *dentry, unsigned int flags);
1facf9fc 27926+ int (*init)(struct super_block *sb);
27927+ int (*fin)(struct super_block *sb);
27928+};
27929+
27930+struct au_wbr_mfs {
27931+ struct mutex mfs_lock; /* protect this structure */
27932+ unsigned long mfs_jiffy;
27933+ unsigned long mfs_expire;
27934+ aufs_bindex_t mfs_bindex;
27935+
27936+ unsigned long long mfsrr_bytes;
27937+ unsigned long long mfsrr_watermark;
27938+};
27939+
86dc4139
AM
27940+#define AuPlink_NHASH 100
27941+static inline int au_plink_hash(ino_t ino)
27942+{
27943+ return ino % AuPlink_NHASH;
27944+}
27945+
076b876e
AM
27946+/* File-based Hierarchical Storage Management */
27947+struct au_fhsm {
27948+#ifdef CONFIG_AUFS_FHSM
27949+ /* allow only one process who can receive the notification */
27950+ spinlock_t fhsm_spin;
27951+ pid_t fhsm_pid;
27952+ wait_queue_head_t fhsm_wqh;
27953+ atomic_t fhsm_readable;
27954+
c1595e42 27955+ /* these are protected by si_rwsem */
076b876e 27956+ unsigned long fhsm_expire;
c1595e42 27957+ aufs_bindex_t fhsm_bottom;
076b876e
AM
27958+#endif
27959+};
27960+
febd17d6
JR
27961+#define AU_PIDSTEP (int)(BITS_TO_LONGS(PID_MAX_DEFAULT) * BITS_PER_LONG)
27962+#define AU_NPIDMAP (int)DIV_ROUND_UP(PID_MAX_LIMIT, AU_PIDSTEP)
27963+struct au_si_pid {
27964+ unsigned long *pid_bitmap[AU_NPIDMAP];
27965+ struct mutex pid_mtx;
27966+};
27967+
1facf9fc 27968+struct au_branch;
27969+struct au_sbinfo {
27970+ /* nowait tasks in the system-wide workqueue */
27971+ struct au_nowait_tasks si_nowait;
27972+
b752ccd1
AM
27973+ /*
27974+ * tried sb->s_umount, but failed due to the dependecy between i_mutex.
27975+ * rwsem for au_sbinfo is necessary.
27976+ */
dece6358 27977+ struct au_rwsem si_rwsem;
1facf9fc 27978+
b752ccd1 27979+ /* prevent recursive locking in deleting inode */
febd17d6 27980+ struct au_si_pid au_si_pid;
b752ccd1 27981+
7f207e10 27982+ /*
523b37e3
AM
27983+ * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
27984+ * remount.
7f207e10 27985+ */
5afbbe0d 27986+ struct percpu_counter si_ninodes, si_nfiles;
7f207e10 27987+
1facf9fc 27988+ /* branch management */
27989+ unsigned int si_generation;
27990+
2000de60 27991+ /* see AuSi_ flags */
1facf9fc 27992+ unsigned char au_si_status;
27993+
5afbbe0d 27994+ aufs_bindex_t si_bbot;
7f207e10
AM
27995+
27996+ /* dirty trick to keep br_id plus */
27997+ unsigned int si_last_br_id :
27998+ sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
1facf9fc 27999+ struct au_branch **si_branch;
28000+
28001+ /* policy to select a writable branch */
28002+ unsigned char si_wbr_copyup;
28003+ unsigned char si_wbr_create;
28004+ struct au_wbr_copyup_operations *si_wbr_copyup_ops;
28005+ struct au_wbr_create_operations *si_wbr_create_ops;
28006+
28007+ /* round robin */
28008+ atomic_t si_wbr_rr_next;
28009+
28010+ /* most free space */
28011+ struct au_wbr_mfs si_wbr_mfs;
28012+
076b876e
AM
28013+ /* File-based Hierarchical Storage Management */
28014+ struct au_fhsm si_fhsm;
28015+
1facf9fc 28016+ /* mount flags */
28017+ /* include/asm-ia64/siginfo.h defines a macro named si_flags */
28018+ unsigned int si_mntflags;
28019+
28020+ /* external inode number (bitmap and translation table) */
5527c038
JR
28021+ vfs_readf_t si_xread;
28022+ vfs_writef_t si_xwrite;
1facf9fc 28023+ struct file *si_xib;
28024+ struct mutex si_xib_mtx; /* protect xib members */
28025+ unsigned long *si_xib_buf;
28026+ unsigned long si_xib_last_pindex;
28027+ int si_xib_next_bit;
28028+ aufs_bindex_t si_xino_brid;
392086de
AM
28029+ unsigned long si_xino_jiffy;
28030+ unsigned long si_xino_expire;
1facf9fc 28031+ /* reserved for future use */
28032+ /* unsigned long long si_xib_limit; */ /* Max xib file size */
28033+
28034+#ifdef CONFIG_AUFS_EXPORT
28035+ /* i_generation */
28036+ struct file *si_xigen;
28037+ atomic_t si_xigen_next;
28038+#endif
28039+
b912730e
AM
28040+ /* dirty trick to suppoer atomic_open */
28041+ struct au_sphlhead si_aopen;
28042+
1facf9fc 28043+ /* vdir parameters */
e49829fe 28044+ unsigned long si_rdcache; /* max cache time in jiffies */
1facf9fc 28045+ unsigned int si_rdblk; /* deblk size */
28046+ unsigned int si_rdhash; /* hash size */
28047+
28048+ /*
28049+ * If the number of whiteouts are larger than si_dirwh, leave all of
28050+ * them after au_whtmp_ren to reduce the cost of rmdir(2).
28051+ * future fsck.aufs or kernel thread will remove them later.
28052+ * Otherwise, remove all whiteouts and the dir in rmdir(2).
28053+ */
28054+ unsigned int si_dirwh;
28055+
1facf9fc 28056+ /* pseudo_link list */
86dc4139 28057+ struct au_sphlhead si_plink[AuPlink_NHASH];
1facf9fc 28058+ wait_queue_head_t si_plink_wq;
4a4d8108 28059+ spinlock_t si_plink_maint_lock;
e49829fe 28060+ pid_t si_plink_maint_pid;
1facf9fc 28061+
523b37e3
AM
28062+ /* file list */
28063+ struct au_sphlhead si_files;
28064+
b95c5147
AM
28065+ /* with/without getattr, brother of sb->s_d_op */
28066+ struct inode_operations *si_iop_array;
28067+
1facf9fc 28068+ /*
28069+ * sysfs and lifetime management.
28070+ * this is not a small structure and it may be a waste of memory in case
28071+ * of sysfs is disabled, particulary when many aufs-es are mounted.
28072+ * but using sysfs is majority.
28073+ */
28074+ struct kobject si_kobj;
28075+#ifdef CONFIG_DEBUG_FS
86dc4139
AM
28076+ struct dentry *si_dbgaufs;
28077+ struct dentry *si_dbgaufs_plink;
28078+ struct dentry *si_dbgaufs_xib;
1facf9fc 28079+#ifdef CONFIG_AUFS_EXPORT
28080+ struct dentry *si_dbgaufs_xigen;
28081+#endif
28082+#endif
28083+
e49829fe 28084+#ifdef CONFIG_AUFS_SBILIST
5afbbe0d 28085+ struct hlist_node si_list;
e49829fe
JR
28086+#endif
28087+
1facf9fc 28088+ /* dirty, necessary for unmounting, sysfs and sysrq */
28089+ struct super_block *si_sb;
28090+};
28091+
dece6358
AM
28092+/* sbinfo status flags */
28093+/*
28094+ * set true when refresh_dirs() failed at remount time.
28095+ * then try refreshing dirs at access time again.
28096+ * if it is false, refreshing dirs at access time is unnecesary
28097+ */
027c5e7a 28098+#define AuSi_FAILED_REFRESH_DIR 1
076b876e 28099+#define AuSi_FHSM (1 << 1) /* fhsm is active now */
79b8bda9 28100+#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
076b876e
AM
28101+
28102+#ifndef CONFIG_AUFS_FHSM
28103+#undef AuSi_FHSM
28104+#define AuSi_FHSM 0
28105+#endif
28106+
dece6358
AM
28107+static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
28108+ unsigned int flag)
28109+{
28110+ AuRwMustAnyLock(&sbi->si_rwsem);
28111+ return sbi->au_si_status & flag;
28112+}
28113+#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
28114+#define au_fset_si(sbinfo, name) do { \
28115+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28116+ (sbinfo)->au_si_status |= AuSi_##name; \
28117+} while (0)
28118+#define au_fclr_si(sbinfo, name) do { \
28119+ AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
28120+ (sbinfo)->au_si_status &= ~AuSi_##name; \
28121+} while (0)
28122+
1facf9fc 28123+/* ---------------------------------------------------------------------- */
28124+
28125+/* policy to select one among writable branches */
4a4d8108
AM
28126+#define AuWbrCopyup(sbinfo, ...) \
28127+ ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
28128+#define AuWbrCreate(sbinfo, ...) \
28129+ ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
1facf9fc 28130+
28131+/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
28132+#define AuLock_DW 1 /* write-lock dentry */
28133+#define AuLock_IR (1 << 1) /* read-lock inode */
28134+#define AuLock_IW (1 << 2) /* write-lock inode */
28135+#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
b95c5147 28136+#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
e49829fe
JR
28137+#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
28138+#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
027c5e7a 28139+#define AuLock_GEN (1 << 7) /* test digen/iigen */
1facf9fc 28140+#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
7f207e10
AM
28141+#define au_fset_lock(flags, name) \
28142+ do { (flags) |= AuLock_##name; } while (0)
28143+#define au_fclr_lock(flags, name) \
28144+ do { (flags) &= ~AuLock_##name; } while (0)
1facf9fc 28145+
28146+/* ---------------------------------------------------------------------- */
28147+
28148+/* super.c */
28149+extern struct file_system_type aufs_fs_type;
28150+struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
79b8bda9
AM
28151+typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
28152+ unsigned long long max, void *arg);
79b8bda9
AM
28153+void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
28154+ struct super_block *sb, void *arg);
7f207e10
AM
28155+struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
28156+void au_iarray_free(struct inode **a, unsigned long long max);
1facf9fc 28157+
28158+/* sbinfo.c */
28159+void au_si_free(struct kobject *kobj);
28160+int au_si_alloc(struct super_block *sb);
28161+int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
28162+
28163+unsigned int au_sigen_inc(struct super_block *sb);
28164+aufs_bindex_t au_new_br_id(struct super_block *sb);
28165+
e49829fe
JR
28166+int si_read_lock(struct super_block *sb, int flags);
28167+int si_write_lock(struct super_block *sb, int flags);
28168+int aufs_read_lock(struct dentry *dentry, int flags);
1facf9fc 28169+void aufs_read_unlock(struct dentry *dentry, int flags);
28170+void aufs_write_lock(struct dentry *dentry);
28171+void aufs_write_unlock(struct dentry *dentry);
e49829fe 28172+int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
1facf9fc 28173+void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
28174+
28175+/* wbr_policy.c */
28176+extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
28177+extern struct au_wbr_create_operations au_wbr_create_ops[];
28178+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
c2b27bf2 28179+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
5afbbe0d 28180+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
c2b27bf2
AM
28181+
28182+/* mvdown.c */
28183+int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
1facf9fc 28184+
076b876e
AM
28185+#ifdef CONFIG_AUFS_FHSM
28186+/* fhsm.c */
28187+
28188+static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
28189+{
28190+ pid_t pid;
28191+
28192+ spin_lock(&fhsm->fhsm_spin);
28193+ pid = fhsm->fhsm_pid;
28194+ spin_unlock(&fhsm->fhsm_spin);
28195+
28196+ return pid;
28197+}
28198+
28199+void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
28200+void au_fhsm_wrote_all(struct super_block *sb, int force);
28201+int au_fhsm_fd(struct super_block *sb, int oflags);
28202+int au_fhsm_br_alloc(struct au_branch *br);
c1595e42 28203+void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
076b876e
AM
28204+void au_fhsm_fin(struct super_block *sb);
28205+void au_fhsm_init(struct au_sbinfo *sbinfo);
28206+void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
28207+void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
28208+#else
28209+AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
28210+ int force)
28211+AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
28212+AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
c1595e42
JR
28213+AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
28214+AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
28215+AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
076b876e
AM
28216+AuStubVoid(au_fhsm_fin, struct super_block *sb)
28217+AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
28218+AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
28219+AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
28220+#endif
28221+
1facf9fc 28222+/* ---------------------------------------------------------------------- */
28223+
28224+static inline struct au_sbinfo *au_sbi(struct super_block *sb)
28225+{
28226+ return sb->s_fs_info;
28227+}
28228+
28229+/* ---------------------------------------------------------------------- */
28230+
28231+#ifdef CONFIG_AUFS_EXPORT
a2a7ad62 28232+int au_test_nfsd(void);
1facf9fc 28233+void au_export_init(struct super_block *sb);
b752ccd1 28234+void au_xigen_inc(struct inode *inode);
1facf9fc 28235+int au_xigen_new(struct inode *inode);
28236+int au_xigen_set(struct super_block *sb, struct file *base);
28237+void au_xigen_clr(struct super_block *sb);
28238+
28239+static inline int au_busy_or_stale(void)
28240+{
b752ccd1 28241+ if (!au_test_nfsd())
1facf9fc 28242+ return -EBUSY;
28243+ return -ESTALE;
28244+}
28245+#else
b752ccd1 28246+AuStubInt0(au_test_nfsd, void)
a2a7ad62 28247+AuStubVoid(au_export_init, struct super_block *sb)
b752ccd1 28248+AuStubVoid(au_xigen_inc, struct inode *inode)
4a4d8108
AM
28249+AuStubInt0(au_xigen_new, struct inode *inode)
28250+AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
28251+AuStubVoid(au_xigen_clr, struct super_block *sb)
c1595e42 28252+AuStub(int, au_busy_or_stale, return -EBUSY, void)
1facf9fc 28253+#endif /* CONFIG_AUFS_EXPORT */
28254+
28255+/* ---------------------------------------------------------------------- */
28256+
e49829fe
JR
28257+#ifdef CONFIG_AUFS_SBILIST
28258+/* module.c */
5afbbe0d 28259+extern struct au_sphlhead au_sbilist;
e49829fe
JR
28260+
28261+static inline void au_sbilist_init(void)
28262+{
5afbbe0d 28263+ au_sphl_init(&au_sbilist);
e49829fe
JR
28264+}
28265+
28266+static inline void au_sbilist_add(struct super_block *sb)
28267+{
5afbbe0d 28268+ au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe
JR
28269+}
28270+
28271+static inline void au_sbilist_del(struct super_block *sb)
28272+{
5afbbe0d 28273+ au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
e49829fe 28274+}
53392da6
AM
28275+
28276+#ifdef CONFIG_AUFS_MAGIC_SYSRQ
28277+static inline void au_sbilist_lock(void)
28278+{
28279+ spin_lock(&au_sbilist.spin);
28280+}
28281+
28282+static inline void au_sbilist_unlock(void)
28283+{
28284+ spin_unlock(&au_sbilist.spin);
28285+}
28286+#define AuGFP_SBILIST GFP_ATOMIC
28287+#else
28288+AuStubVoid(au_sbilist_lock, void)
28289+AuStubVoid(au_sbilist_unlock, void)
28290+#define AuGFP_SBILIST GFP_NOFS
28291+#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
e49829fe
JR
28292+#else
28293+AuStubVoid(au_sbilist_init, void)
c1595e42
JR
28294+AuStubVoid(au_sbilist_add, struct super_block *sb)
28295+AuStubVoid(au_sbilist_del, struct super_block *sb)
53392da6
AM
28296+AuStubVoid(au_sbilist_lock, void)
28297+AuStubVoid(au_sbilist_unlock, void)
28298+#define AuGFP_SBILIST GFP_NOFS
e49829fe
JR
28299+#endif
28300+
28301+/* ---------------------------------------------------------------------- */
28302+
1facf9fc 28303+static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
28304+{
dece6358 28305+ /*
c1595e42 28306+ * This function is a dynamic '__init' function actually,
dece6358
AM
28307+ * so the tiny check for si_rwsem is unnecessary.
28308+ */
28309+ /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
1facf9fc 28310+#ifdef CONFIG_DEBUG_FS
28311+ sbinfo->si_dbgaufs = NULL;
86dc4139 28312+ sbinfo->si_dbgaufs_plink = NULL;
1facf9fc 28313+ sbinfo->si_dbgaufs_xib = NULL;
28314+#ifdef CONFIG_AUFS_EXPORT
28315+ sbinfo->si_dbgaufs_xigen = NULL;
28316+#endif
28317+#endif
28318+}
28319+
28320+/* ---------------------------------------------------------------------- */
28321+
febd17d6 28322+static inline void si_pid_idx_bit(int *idx, pid_t *bit)
b752ccd1
AM
28323+{
28324+ /* the origin of pid is 1, but the bitmap's is 0 */
febd17d6
JR
28325+ *bit = current->pid - 1;
28326+ *idx = *bit / AU_PIDSTEP;
28327+ *bit %= AU_PIDSTEP;
b752ccd1
AM
28328+}
28329+
28330+static inline int si_pid_test(struct super_block *sb)
28331+{
076b876e 28332+ pid_t bit;
febd17d6
JR
28333+ int idx;
28334+ unsigned long *bitmap;
076b876e 28335+
febd17d6
JR
28336+ si_pid_idx_bit(&idx, &bit);
28337+ bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
28338+ if (bitmap)
28339+ return test_bit(bit, bitmap);
28340+ return 0;
b752ccd1
AM
28341+}
28342+
28343+static inline void si_pid_clr(struct super_block *sb)
28344+{
076b876e 28345+ pid_t bit;
febd17d6
JR
28346+ int idx;
28347+ unsigned long *bitmap;
076b876e 28348+
febd17d6
JR
28349+ si_pid_idx_bit(&idx, &bit);
28350+ bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
28351+ BUG_ON(!bitmap);
28352+ AuDebugOn(!test_bit(bit, bitmap));
28353+ clear_bit(bit, bitmap);
28354+ /* smp_mb(); */
b752ccd1
AM
28355+}
28356+
febd17d6
JR
28357+void si_pid_set(struct super_block *sb);
28358+
b752ccd1
AM
28359+/* ---------------------------------------------------------------------- */
28360+
1facf9fc 28361+/* lock superblock. mainly for entry point functions */
28362+/*
b752ccd1
AM
28363+ * __si_read_lock, __si_write_lock,
28364+ * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
1facf9fc 28365+ */
b752ccd1 28366+AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
1facf9fc 28367+
dece6358
AM
28368+#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
28369+#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
28370+#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
28371+
b752ccd1
AM
28372+static inline void si_noflush_read_lock(struct super_block *sb)
28373+{
28374+ __si_read_lock(sb);
28375+ si_pid_set(sb);
28376+}
28377+
28378+static inline int si_noflush_read_trylock(struct super_block *sb)
28379+{
076b876e
AM
28380+ int locked;
28381+
28382+ locked = __si_read_trylock(sb);
b752ccd1
AM
28383+ if (locked)
28384+ si_pid_set(sb);
28385+ return locked;
28386+}
28387+
28388+static inline void si_noflush_write_lock(struct super_block *sb)
28389+{
28390+ __si_write_lock(sb);
28391+ si_pid_set(sb);
28392+}
28393+
28394+static inline int si_noflush_write_trylock(struct super_block *sb)
28395+{
076b876e
AM
28396+ int locked;
28397+
28398+ locked = __si_write_trylock(sb);
b752ccd1
AM
28399+ if (locked)
28400+ si_pid_set(sb);
28401+ return locked;
28402+}
28403+
7e9cd9fe 28404+#if 0 /* reserved */
1facf9fc 28405+static inline int si_read_trylock(struct super_block *sb, int flags)
28406+{
28407+ if (au_ftest_lock(flags, FLUSH))
28408+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28409+ return si_noflush_read_trylock(sb);
28410+}
e49829fe 28411+#endif
1facf9fc 28412+
b752ccd1
AM
28413+static inline void si_read_unlock(struct super_block *sb)
28414+{
28415+ si_pid_clr(sb);
28416+ __si_read_unlock(sb);
28417+}
28418+
7e9cd9fe 28419+#if 0 /* reserved */
1facf9fc 28420+static inline int si_write_trylock(struct super_block *sb, int flags)
28421+{
28422+ if (au_ftest_lock(flags, FLUSH))
28423+ au_nwt_flush(&au_sbi(sb)->si_nowait);
28424+ return si_noflush_write_trylock(sb);
28425+}
b752ccd1
AM
28426+#endif
28427+
28428+static inline void si_write_unlock(struct super_block *sb)
28429+{
28430+ si_pid_clr(sb);
28431+ __si_write_unlock(sb);
28432+}
28433+
7e9cd9fe 28434+#if 0 /* reserved */
b752ccd1
AM
28435+static inline void si_downgrade_lock(struct super_block *sb)
28436+{
28437+ __si_downgrade_lock(sb);
28438+}
28439+#endif
1facf9fc 28440+
28441+/* ---------------------------------------------------------------------- */
28442+
5afbbe0d 28443+static inline aufs_bindex_t au_sbbot(struct super_block *sb)
1facf9fc 28444+{
dece6358 28445+ SiMustAnyLock(sb);
5afbbe0d 28446+ return au_sbi(sb)->si_bbot;
1facf9fc 28447+}
28448+
28449+static inline unsigned int au_mntflags(struct super_block *sb)
28450+{
dece6358 28451+ SiMustAnyLock(sb);
1facf9fc 28452+ return au_sbi(sb)->si_mntflags;
28453+}
28454+
28455+static inline unsigned int au_sigen(struct super_block *sb)
28456+{
dece6358 28457+ SiMustAnyLock(sb);
1facf9fc 28458+ return au_sbi(sb)->si_generation;
28459+}
28460+
5afbbe0d
AM
28461+static inline unsigned long long au_ninodes(struct super_block *sb)
28462+{
28463+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
28464+
28465+ BUG_ON(n < 0);
28466+ return n;
28467+}
28468+
7f207e10
AM
28469+static inline void au_ninodes_inc(struct super_block *sb)
28470+{
5afbbe0d 28471+ percpu_counter_inc(&au_sbi(sb)->si_ninodes);
7f207e10
AM
28472+}
28473+
28474+static inline void au_ninodes_dec(struct super_block *sb)
28475+{
5afbbe0d
AM
28476+ percpu_counter_dec(&au_sbi(sb)->si_ninodes);
28477+}
28478+
28479+static inline unsigned long long au_nfiles(struct super_block *sb)
28480+{
28481+ s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
28482+
28483+ BUG_ON(n < 0);
28484+ return n;
7f207e10
AM
28485+}
28486+
28487+static inline void au_nfiles_inc(struct super_block *sb)
28488+{
5afbbe0d 28489+ percpu_counter_inc(&au_sbi(sb)->si_nfiles);
7f207e10
AM
28490+}
28491+
28492+static inline void au_nfiles_dec(struct super_block *sb)
28493+{
5afbbe0d 28494+ percpu_counter_dec(&au_sbi(sb)->si_nfiles);
7f207e10
AM
28495+}
28496+
1facf9fc 28497+static inline struct au_branch *au_sbr(struct super_block *sb,
28498+ aufs_bindex_t bindex)
28499+{
dece6358 28500+ SiMustAnyLock(sb);
1facf9fc 28501+ return au_sbi(sb)->si_branch[0 + bindex];
28502+}
28503+
28504+static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
28505+{
dece6358 28506+ SiMustWriteLock(sb);
1facf9fc 28507+ au_sbi(sb)->si_xino_brid = brid;
28508+}
28509+
28510+static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
28511+{
dece6358 28512+ SiMustAnyLock(sb);
1facf9fc 28513+ return au_sbi(sb)->si_xino_brid;
28514+}
28515+
28516+#endif /* __KERNEL__ */
28517+#endif /* __AUFS_SUPER_H__ */
7f207e10
AM
28518diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
28519--- /usr/share/empty/fs/aufs/sysaufs.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 28520+++ linux/fs/aufs/sysaufs.c 2016-07-25 19:05:34.817826663 +0200
523b37e3 28521@@ -0,0 +1,104 @@
1facf9fc 28522+/*
8cdd5066 28523+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28524+ *
28525+ * This program, aufs is free software; you can redistribute it and/or modify
28526+ * it under the terms of the GNU General Public License as published by
28527+ * the Free Software Foundation; either version 2 of the License, or
28528+ * (at your option) any later version.
dece6358
AM
28529+ *
28530+ * This program is distributed in the hope that it will be useful,
28531+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28532+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28533+ * GNU General Public License for more details.
28534+ *
28535+ * You should have received a copy of the GNU General Public License
523b37e3 28536+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28537+ */
28538+
28539+/*
28540+ * sysfs interface and lifetime management
28541+ * they are necessary regardless sysfs is disabled.
28542+ */
28543+
1facf9fc 28544+#include <linux/random.h>
1facf9fc 28545+#include "aufs.h"
28546+
28547+unsigned long sysaufs_si_mask;
e49829fe 28548+struct kset *sysaufs_kset;
1facf9fc 28549+
28550+#define AuSiAttr(_name) { \
28551+ .attr = { .name = __stringify(_name), .mode = 0444 }, \
28552+ .show = sysaufs_si_##_name, \
28553+}
28554+
28555+static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
28556+struct attribute *sysaufs_si_attrs[] = {
28557+ &sysaufs_si_attr_xi_path.attr,
28558+ NULL,
28559+};
28560+
4a4d8108 28561+static const struct sysfs_ops au_sbi_ops = {
1facf9fc 28562+ .show = sysaufs_si_show
28563+};
28564+
28565+static struct kobj_type au_sbi_ktype = {
28566+ .release = au_si_free,
28567+ .sysfs_ops = &au_sbi_ops,
28568+ .default_attrs = sysaufs_si_attrs
28569+};
28570+
28571+/* ---------------------------------------------------------------------- */
28572+
28573+int sysaufs_si_init(struct au_sbinfo *sbinfo)
28574+{
28575+ int err;
28576+
e49829fe 28577+ sbinfo->si_kobj.kset = sysaufs_kset;
1facf9fc 28578+ /* cf. sysaufs_name() */
28579+ err = kobject_init_and_add
e49829fe 28580+ (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
1facf9fc 28581+ SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
28582+
28583+ dbgaufs_si_null(sbinfo);
28584+ if (!err) {
28585+ err = dbgaufs_si_init(sbinfo);
28586+ if (unlikely(err))
28587+ kobject_put(&sbinfo->si_kobj);
28588+ }
28589+ return err;
28590+}
28591+
28592+void sysaufs_fin(void)
28593+{
28594+ dbgaufs_fin();
e49829fe
JR
28595+ sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
28596+ kset_unregister(sysaufs_kset);
1facf9fc 28597+}
28598+
28599+int __init sysaufs_init(void)
28600+{
28601+ int err;
28602+
28603+ do {
28604+ get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
28605+ } while (!sysaufs_si_mask);
28606+
4a4d8108 28607+ err = -EINVAL;
e49829fe
JR
28608+ sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
28609+ if (unlikely(!sysaufs_kset))
4a4d8108 28610+ goto out;
e49829fe
JR
28611+ err = PTR_ERR(sysaufs_kset);
28612+ if (IS_ERR(sysaufs_kset))
1facf9fc 28613+ goto out;
e49829fe 28614+ err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
1facf9fc 28615+ if (unlikely(err)) {
e49829fe 28616+ kset_unregister(sysaufs_kset);
1facf9fc 28617+ goto out;
28618+ }
28619+
28620+ err = dbgaufs_init();
28621+ if (unlikely(err))
28622+ sysaufs_fin();
4f0767ce 28623+out:
1facf9fc 28624+ return err;
28625+}
7f207e10
AM
28626diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
28627--- /usr/share/empty/fs/aufs/sysaufs.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 28628+++ linux/fs/aufs/sysaufs.h 2016-07-25 19:05:34.817826663 +0200
c1595e42 28629@@ -0,0 +1,101 @@
1facf9fc 28630+/*
8cdd5066 28631+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28632+ *
28633+ * This program, aufs is free software; you can redistribute it and/or modify
28634+ * it under the terms of the GNU General Public License as published by
28635+ * the Free Software Foundation; either version 2 of the License, or
28636+ * (at your option) any later version.
dece6358
AM
28637+ *
28638+ * This program is distributed in the hope that it will be useful,
28639+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28640+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28641+ * GNU General Public License for more details.
28642+ *
28643+ * You should have received a copy of the GNU General Public License
523b37e3 28644+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28645+ */
28646+
28647+/*
28648+ * sysfs interface and mount lifetime management
28649+ */
28650+
28651+#ifndef __SYSAUFS_H__
28652+#define __SYSAUFS_H__
28653+
28654+#ifdef __KERNEL__
28655+
1facf9fc 28656+#include <linux/sysfs.h>
1facf9fc 28657+#include "module.h"
28658+
dece6358
AM
28659+struct super_block;
28660+struct au_sbinfo;
28661+
1facf9fc 28662+struct sysaufs_si_attr {
28663+ struct attribute attr;
28664+ int (*show)(struct seq_file *seq, struct super_block *sb);
28665+};
28666+
28667+/* ---------------------------------------------------------------------- */
28668+
28669+/* sysaufs.c */
28670+extern unsigned long sysaufs_si_mask;
e49829fe 28671+extern struct kset *sysaufs_kset;
1facf9fc 28672+extern struct attribute *sysaufs_si_attrs[];
28673+int sysaufs_si_init(struct au_sbinfo *sbinfo);
28674+int __init sysaufs_init(void);
28675+void sysaufs_fin(void);
28676+
28677+/* ---------------------------------------------------------------------- */
28678+
28679+/* some people doesn't like to show a pointer in kernel */
28680+static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
28681+{
28682+ return sysaufs_si_mask ^ (unsigned long)sbinfo;
28683+}
28684+
28685+#define SysaufsSiNamePrefix "si_"
28686+#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
28687+static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
28688+{
28689+ snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
28690+ sysaufs_si_id(sbinfo));
28691+}
28692+
28693+struct au_branch;
28694+#ifdef CONFIG_SYSFS
28695+/* sysfs.c */
28696+extern struct attribute_group *sysaufs_attr_group;
28697+
28698+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
28699+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
28700+ char *buf);
076b876e
AM
28701+long au_brinfo_ioctl(struct file *file, unsigned long arg);
28702+#ifdef CONFIG_COMPAT
28703+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
28704+#endif
1facf9fc 28705+
28706+void sysaufs_br_init(struct au_branch *br);
28707+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
28708+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
28709+
28710+#define sysaufs_brs_init() do {} while (0)
28711+
28712+#else
28713+#define sysaufs_attr_group NULL
28714+
4a4d8108 28715+AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
c1595e42
JR
28716+AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
28717+ struct attribute *attr, char *buf)
4a4d8108
AM
28718+AuStubVoid(sysaufs_br_init, struct au_branch *br)
28719+AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
28720+AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
1facf9fc 28721+
28722+static inline void sysaufs_brs_init(void)
28723+{
28724+ sysaufs_brs = 0;
28725+}
28726+
28727+#endif /* CONFIG_SYSFS */
28728+
28729+#endif /* __KERNEL__ */
28730+#endif /* __SYSAUFS_H__ */
7f207e10
AM
28731diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
28732--- /usr/share/empty/fs/aufs/sysfs.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 28733+++ linux/fs/aufs/sysfs.c 2016-07-25 19:05:34.817826663 +0200
79b8bda9 28734@@ -0,0 +1,376 @@
1facf9fc 28735+/*
8cdd5066 28736+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 28737+ *
28738+ * This program, aufs is free software; you can redistribute it and/or modify
28739+ * it under the terms of the GNU General Public License as published by
28740+ * the Free Software Foundation; either version 2 of the License, or
28741+ * (at your option) any later version.
dece6358
AM
28742+ *
28743+ * This program is distributed in the hope that it will be useful,
28744+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28745+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28746+ * GNU General Public License for more details.
28747+ *
28748+ * You should have received a copy of the GNU General Public License
523b37e3 28749+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 28750+ */
28751+
28752+/*
28753+ * sysfs interface
28754+ */
28755+
076b876e 28756+#include <linux/compat.h>
1facf9fc 28757+#include <linux/seq_file.h>
1facf9fc 28758+#include "aufs.h"
28759+
4a4d8108
AM
28760+#ifdef CONFIG_AUFS_FS_MODULE
28761+/* this entry violates the "one line per file" policy of sysfs */
28762+static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
28763+ char *buf)
28764+{
28765+ ssize_t err;
28766+ static char *conf =
28767+/* this file is generated at compiling */
28768+#include "conf.str"
28769+ ;
28770+
28771+ err = snprintf(buf, PAGE_SIZE, conf);
28772+ if (unlikely(err >= PAGE_SIZE))
28773+ err = -EFBIG;
28774+ return err;
28775+}
28776+
28777+static struct kobj_attribute au_config_attr = __ATTR_RO(config);
28778+#endif
28779+
1facf9fc 28780+static struct attribute *au_attr[] = {
4a4d8108
AM
28781+#ifdef CONFIG_AUFS_FS_MODULE
28782+ &au_config_attr.attr,
28783+#endif
1facf9fc 28784+ NULL, /* need to NULL terminate the list of attributes */
28785+};
28786+
28787+static struct attribute_group sysaufs_attr_group_body = {
28788+ .attrs = au_attr
28789+};
28790+
28791+struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
28792+
28793+/* ---------------------------------------------------------------------- */
28794+
28795+int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
28796+{
28797+ int err;
28798+
dece6358
AM
28799+ SiMustAnyLock(sb);
28800+
1facf9fc 28801+ err = 0;
28802+ if (au_opt_test(au_mntflags(sb), XINO)) {
28803+ err = au_xino_path(seq, au_sbi(sb)->si_xib);
28804+ seq_putc(seq, '\n');
28805+ }
28806+ return err;
28807+}
28808+
28809+/*
28810+ * the lifetime of branch is independent from the entry under sysfs.
28811+ * sysfs handles the lifetime of the entry, and never call ->show() after it is
28812+ * unlinked.
28813+ */
28814+static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
392086de 28815+ aufs_bindex_t bindex, int idx)
1facf9fc 28816+{
1e00d052 28817+ int err;
1facf9fc 28818+ struct path path;
28819+ struct dentry *root;
28820+ struct au_branch *br;
076b876e 28821+ au_br_perm_str_t perm;
1facf9fc 28822+
28823+ AuDbg("b%d\n", bindex);
28824+
1e00d052 28825+ err = 0;
1facf9fc 28826+ root = sb->s_root;
28827+ di_read_lock_parent(root, !AuLock_IR);
28828+ br = au_sbr(sb, bindex);
392086de
AM
28829+
28830+ switch (idx) {
28831+ case AuBrSysfs_BR:
28832+ path.mnt = au_br_mnt(br);
28833+ path.dentry = au_h_dptr(root, bindex);
79b8bda9
AM
28834+ err = au_seq_path(seq, &path);
28835+ if (!err) {
28836+ au_optstr_br_perm(&perm, br->br_perm);
28837+ seq_printf(seq, "=%s\n", perm.a);
28838+ }
392086de
AM
28839+ break;
28840+ case AuBrSysfs_BRID:
79b8bda9 28841+ seq_printf(seq, "%d\n", br->br_id);
392086de
AM
28842+ break;
28843+ }
076b876e 28844+ di_read_unlock(root, !AuLock_IR);
79b8bda9 28845+ if (unlikely(err || seq_has_overflowed(seq)))
076b876e 28846+ err = -E2BIG;
392086de 28847+
1e00d052 28848+ return err;
1facf9fc 28849+}
28850+
28851+/* ---------------------------------------------------------------------- */
28852+
28853+static struct seq_file *au_seq(char *p, ssize_t len)
28854+{
28855+ struct seq_file *seq;
28856+
28857+ seq = kzalloc(sizeof(*seq), GFP_NOFS);
28858+ if (seq) {
28859+ /* mutex_init(&seq.lock); */
28860+ seq->buf = p;
28861+ seq->size = len;
28862+ return seq; /* success */
28863+ }
28864+
28865+ seq = ERR_PTR(-ENOMEM);
28866+ return seq;
28867+}
28868+
392086de
AM
28869+#define SysaufsBr_PREFIX "br"
28870+#define SysaufsBrid_PREFIX "brid"
1facf9fc 28871+
28872+/* todo: file size may exceed PAGE_SIZE */
28873+ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
1308ab2a 28874+ char *buf)
1facf9fc 28875+{
28876+ ssize_t err;
392086de 28877+ int idx;
1facf9fc 28878+ long l;
5afbbe0d 28879+ aufs_bindex_t bbot;
1facf9fc 28880+ struct au_sbinfo *sbinfo;
28881+ struct super_block *sb;
28882+ struct seq_file *seq;
28883+ char *name;
28884+ struct attribute **cattr;
28885+
28886+ sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
28887+ sb = sbinfo->si_sb;
1308ab2a 28888+
28889+ /*
28890+ * prevent a race condition between sysfs and aufs.
28891+ * for instance, sysfs_file_read() calls sysfs_get_active_two() which
28892+ * prohibits maintaining the sysfs entries.
28893+ * hew we acquire read lock after sysfs_get_active_two().
28894+ * on the other hand, the remount process may maintain the sysfs/aufs
28895+ * entries after acquiring write lock.
28896+ * it can cause a deadlock.
28897+ * simply we gave up processing read here.
28898+ */
28899+ err = -EBUSY;
28900+ if (unlikely(!si_noflush_read_trylock(sb)))
28901+ goto out;
1facf9fc 28902+
28903+ seq = au_seq(buf, PAGE_SIZE);
28904+ err = PTR_ERR(seq);
28905+ if (IS_ERR(seq))
1308ab2a 28906+ goto out_unlock;
1facf9fc 28907+
28908+ name = (void *)attr->name;
28909+ cattr = sysaufs_si_attrs;
28910+ while (*cattr) {
28911+ if (!strcmp(name, (*cattr)->name)) {
28912+ err = container_of(*cattr, struct sysaufs_si_attr, attr)
28913+ ->show(seq, sb);
28914+ goto out_seq;
28915+ }
28916+ cattr++;
28917+ }
28918+
392086de
AM
28919+ if (!strncmp(name, SysaufsBrid_PREFIX,
28920+ sizeof(SysaufsBrid_PREFIX) - 1)) {
28921+ idx = AuBrSysfs_BRID;
28922+ name += sizeof(SysaufsBrid_PREFIX) - 1;
28923+ } else if (!strncmp(name, SysaufsBr_PREFIX,
28924+ sizeof(SysaufsBr_PREFIX) - 1)) {
28925+ idx = AuBrSysfs_BR;
1facf9fc 28926+ name += sizeof(SysaufsBr_PREFIX) - 1;
392086de
AM
28927+ } else
28928+ BUG();
28929+
28930+ err = kstrtol(name, 10, &l);
28931+ if (!err) {
5afbbe0d
AM
28932+ bbot = au_sbbot(sb);
28933+ if (l <= bbot)
392086de
AM
28934+ err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
28935+ else
28936+ err = -ENOENT;
1facf9fc 28937+ }
1facf9fc 28938+
4f0767ce 28939+out_seq:
1facf9fc 28940+ if (!err) {
28941+ err = seq->count;
28942+ /* sysfs limit */
28943+ if (unlikely(err == PAGE_SIZE))
28944+ err = -EFBIG;
28945+ }
28946+ kfree(seq);
4f0767ce 28947+out_unlock:
1facf9fc 28948+ si_read_unlock(sb);
4f0767ce 28949+out:
1facf9fc 28950+ return err;
28951+}
28952+
28953+/* ---------------------------------------------------------------------- */
28954+
076b876e
AM
28955+static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
28956+{
28957+ int err;
28958+ int16_t brid;
5afbbe0d 28959+ aufs_bindex_t bindex, bbot;
076b876e
AM
28960+ size_t sz;
28961+ char *buf;
28962+ struct seq_file *seq;
28963+ struct au_branch *br;
28964+
28965+ si_read_lock(sb, AuLock_FLUSH);
5afbbe0d
AM
28966+ bbot = au_sbbot(sb);
28967+ err = bbot + 1;
076b876e
AM
28968+ if (!arg)
28969+ goto out;
28970+
28971+ err = -ENOMEM;
28972+ buf = (void *)__get_free_page(GFP_NOFS);
28973+ if (unlikely(!buf))
28974+ goto out;
28975+
28976+ seq = au_seq(buf, PAGE_SIZE);
28977+ err = PTR_ERR(seq);
28978+ if (IS_ERR(seq))
28979+ goto out_buf;
28980+
28981+ sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
5afbbe0d 28982+ for (bindex = 0; bindex <= bbot; bindex++, arg++) {
076b876e
AM
28983+ err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
28984+ if (unlikely(err))
28985+ break;
28986+
28987+ br = au_sbr(sb, bindex);
28988+ brid = br->br_id;
28989+ BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
28990+ err = __put_user(brid, &arg->id);
28991+ if (unlikely(err))
28992+ break;
28993+
28994+ BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
28995+ err = __put_user(br->br_perm, &arg->perm);
28996+ if (unlikely(err))
28997+ break;
28998+
79b8bda9
AM
28999+ err = au_seq_path(seq, &br->br_path);
29000+ if (unlikely(err))
29001+ break;
29002+ seq_putc(seq, '\0');
29003+ if (!seq_has_overflowed(seq)) {
076b876e
AM
29004+ err = copy_to_user(arg->path, seq->buf, seq->count);
29005+ seq->count = 0;
29006+ if (unlikely(err))
29007+ break;
29008+ } else {
29009+ err = -E2BIG;
29010+ goto out_seq;
29011+ }
29012+ }
29013+ if (unlikely(err))
29014+ err = -EFAULT;
29015+
29016+out_seq:
29017+ kfree(seq);
29018+out_buf:
29019+ free_page((unsigned long)buf);
29020+out:
29021+ si_read_unlock(sb);
29022+ return err;
29023+}
29024+
29025+long au_brinfo_ioctl(struct file *file, unsigned long arg)
29026+{
2000de60 29027+ return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
076b876e
AM
29028+}
29029+
29030+#ifdef CONFIG_COMPAT
29031+long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
29032+{
2000de60 29033+ return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
076b876e
AM
29034+}
29035+#endif
29036+
29037+/* ---------------------------------------------------------------------- */
29038+
1facf9fc 29039+void sysaufs_br_init(struct au_branch *br)
29040+{
392086de
AM
29041+ int i;
29042+ struct au_brsysfs *br_sysfs;
29043+ struct attribute *attr;
4a4d8108 29044+
392086de
AM
29045+ br_sysfs = br->br_sysfs;
29046+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29047+ attr = &br_sysfs->attr;
29048+ sysfs_attr_init(attr);
29049+ attr->name = br_sysfs->name;
29050+ attr->mode = S_IRUGO;
29051+ br_sysfs++;
29052+ }
1facf9fc 29053+}
29054+
29055+void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
29056+{
29057+ struct au_branch *br;
29058+ struct kobject *kobj;
392086de
AM
29059+ struct au_brsysfs *br_sysfs;
29060+ int i;
5afbbe0d 29061+ aufs_bindex_t bbot;
1facf9fc 29062+
29063+ dbgaufs_brs_del(sb, bindex);
29064+
29065+ if (!sysaufs_brs)
29066+ return;
29067+
29068+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
29069+ bbot = au_sbbot(sb);
29070+ for (; bindex <= bbot; bindex++) {
1facf9fc 29071+ br = au_sbr(sb, bindex);
392086de
AM
29072+ br_sysfs = br->br_sysfs;
29073+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29074+ sysfs_remove_file(kobj, &br_sysfs->attr);
29075+ br_sysfs++;
29076+ }
1facf9fc 29077+ }
29078+}
29079+
29080+void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
29081+{
392086de 29082+ int err, i;
5afbbe0d 29083+ aufs_bindex_t bbot;
1facf9fc 29084+ struct kobject *kobj;
29085+ struct au_branch *br;
392086de 29086+ struct au_brsysfs *br_sysfs;
1facf9fc 29087+
29088+ dbgaufs_brs_add(sb, bindex);
29089+
29090+ if (!sysaufs_brs)
29091+ return;
29092+
29093+ kobj = &au_sbi(sb)->si_kobj;
5afbbe0d
AM
29094+ bbot = au_sbbot(sb);
29095+ for (; bindex <= bbot; bindex++) {
1facf9fc 29096+ br = au_sbr(sb, bindex);
392086de
AM
29097+ br_sysfs = br->br_sysfs;
29098+ snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
29099+ SysaufsBr_PREFIX "%d", bindex);
29100+ snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
29101+ SysaufsBrid_PREFIX "%d", bindex);
29102+ for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
29103+ err = sysfs_create_file(kobj, &br_sysfs->attr);
29104+ if (unlikely(err))
29105+ pr_warn("failed %s under sysfs(%d)\n",
29106+ br_sysfs->name, err);
29107+ br_sysfs++;
29108+ }
1facf9fc 29109+ }
29110+}
7f207e10
AM
29111diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
29112--- /usr/share/empty/fs/aufs/sysrq.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 29113+++ linux/fs/aufs/sysrq.c 2016-07-25 19:05:34.817826663 +0200
076b876e 29114@@ -0,0 +1,157 @@
1facf9fc 29115+/*
8cdd5066 29116+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 29117+ *
29118+ * This program, aufs is free software; you can redistribute it and/or modify
29119+ * it under the terms of the GNU General Public License as published by
29120+ * the Free Software Foundation; either version 2 of the License, or
29121+ * (at your option) any later version.
dece6358
AM
29122+ *
29123+ * This program is distributed in the hope that it will be useful,
29124+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29125+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29126+ * GNU General Public License for more details.
29127+ *
29128+ * You should have received a copy of the GNU General Public License
523b37e3 29129+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29130+ */
29131+
29132+/*
29133+ * magic sysrq hanlder
29134+ */
29135+
1facf9fc 29136+/* #include <linux/sysrq.h> */
027c5e7a 29137+#include <linux/writeback.h>
1facf9fc 29138+#include "aufs.h"
29139+
29140+/* ---------------------------------------------------------------------- */
29141+
29142+static void sysrq_sb(struct super_block *sb)
29143+{
29144+ char *plevel;
29145+ struct au_sbinfo *sbinfo;
29146+ struct file *file;
523b37e3
AM
29147+ struct au_sphlhead *files;
29148+ struct au_finfo *finfo;
1facf9fc 29149+
29150+ plevel = au_plevel;
29151+ au_plevel = KERN_WARNING;
1facf9fc 29152+
4a4d8108 29153+ /* since we define pr_fmt, call printk directly */
c06a8ce3
AM
29154+#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
29155+
29156+ sbinfo = au_sbi(sb);
4a4d8108 29157+ printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
c06a8ce3 29158+ pr("superblock\n");
1facf9fc 29159+ au_dpri_sb(sb);
027c5e7a
AM
29160+
29161+#if 0
c06a8ce3 29162+ pr("root dentry\n");
1facf9fc 29163+ au_dpri_dentry(sb->s_root);
c06a8ce3 29164+ pr("root inode\n");
5527c038 29165+ au_dpri_inode(d_inode(sb->s_root));
027c5e7a
AM
29166+#endif
29167+
1facf9fc 29168+#if 0
027c5e7a
AM
29169+ do {
29170+ int err, i, j, ndentry;
29171+ struct au_dcsub_pages dpages;
29172+ struct au_dpage *dpage;
29173+
29174+ err = au_dpages_init(&dpages, GFP_ATOMIC);
29175+ if (unlikely(err))
29176+ break;
29177+ err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
29178+ if (!err)
29179+ for (i = 0; i < dpages.ndpage; i++) {
29180+ dpage = dpages.dpages + i;
29181+ ndentry = dpage->ndentry;
29182+ for (j = 0; j < ndentry; j++)
29183+ au_dpri_dentry(dpage->dentries[j]);
29184+ }
29185+ au_dpages_free(&dpages);
29186+ } while (0);
29187+#endif
29188+
29189+#if 1
29190+ {
29191+ struct inode *i;
076b876e 29192+
c06a8ce3 29193+ pr("isolated inode\n");
79b8bda9 29194+ spin_lock(&sb->s_inode_list_lock);
2cbb1c4b
JR
29195+ list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
29196+ spin_lock(&i->i_lock);
b4510431 29197+ if (1 || hlist_empty(&i->i_dentry))
027c5e7a 29198+ au_dpri_inode(i);
2cbb1c4b
JR
29199+ spin_unlock(&i->i_lock);
29200+ }
79b8bda9 29201+ spin_unlock(&sb->s_inode_list_lock);
027c5e7a 29202+ }
1facf9fc 29203+#endif
c06a8ce3 29204+ pr("files\n");
523b37e3
AM
29205+ files = &au_sbi(sb)->si_files;
29206+ spin_lock(&files->spin);
29207+ hlist_for_each_entry(finfo, &files->head, fi_hlist) {
4a4d8108 29208+ umode_t mode;
076b876e 29209+
523b37e3 29210+ file = finfo->fi_file;
c06a8ce3 29211+ mode = file_inode(file)->i_mode;
38d290e6 29212+ if (!special_file(mode))
1facf9fc 29213+ au_dpri_file(file);
523b37e3
AM
29214+ }
29215+ spin_unlock(&files->spin);
c06a8ce3 29216+ pr("done\n");
1facf9fc 29217+
c06a8ce3 29218+#undef pr
1facf9fc 29219+ au_plevel = plevel;
1facf9fc 29220+}
29221+
29222+/* ---------------------------------------------------------------------- */
29223+
29224+/* module parameter */
29225+static char *aufs_sysrq_key = "a";
29226+module_param_named(sysrq, aufs_sysrq_key, charp, S_IRUGO);
29227+MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
29228+
0c5527e5 29229+static void au_sysrq(int key __maybe_unused)
1facf9fc 29230+{
1facf9fc 29231+ struct au_sbinfo *sbinfo;
29232+
027c5e7a 29233+ lockdep_off();
53392da6 29234+ au_sbilist_lock();
5afbbe0d 29235+ hlist_for_each_entry(sbinfo, &au_sbilist.head, si_list)
1facf9fc 29236+ sysrq_sb(sbinfo->si_sb);
53392da6 29237+ au_sbilist_unlock();
027c5e7a 29238+ lockdep_on();
1facf9fc 29239+}
29240+
29241+static struct sysrq_key_op au_sysrq_op = {
29242+ .handler = au_sysrq,
29243+ .help_msg = "Aufs",
29244+ .action_msg = "Aufs",
29245+ .enable_mask = SYSRQ_ENABLE_DUMP
29246+};
29247+
29248+/* ---------------------------------------------------------------------- */
29249+
29250+int __init au_sysrq_init(void)
29251+{
29252+ int err;
29253+ char key;
29254+
29255+ err = -1;
29256+ key = *aufs_sysrq_key;
29257+ if ('a' <= key && key <= 'z')
29258+ err = register_sysrq_key(key, &au_sysrq_op);
29259+ if (unlikely(err))
4a4d8108 29260+ pr_err("err %d, sysrq=%c\n", err, key);
1facf9fc 29261+ return err;
29262+}
29263+
29264+void au_sysrq_fin(void)
29265+{
29266+ int err;
076b876e 29267+
1facf9fc 29268+ err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
29269+ if (unlikely(err))
4a4d8108 29270+ pr_err("err %d (ignored)\n", err);
1facf9fc 29271+}
7f207e10
AM
29272diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
29273--- /usr/share/empty/fs/aufs/vdir.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
29274+++ linux/fs/aufs/vdir.c 2016-07-25 19:05:34.817826663 +0200
29275@@ -0,0 +1,889 @@
1facf9fc 29276+/*
8cdd5066 29277+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 29278+ *
29279+ * This program, aufs is free software; you can redistribute it and/or modify
29280+ * it under the terms of the GNU General Public License as published by
29281+ * the Free Software Foundation; either version 2 of the License, or
29282+ * (at your option) any later version.
dece6358
AM
29283+ *
29284+ * This program is distributed in the hope that it will be useful,
29285+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
29286+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29287+ * GNU General Public License for more details.
29288+ *
29289+ * You should have received a copy of the GNU General Public License
523b37e3 29290+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 29291+ */
29292+
29293+/*
29294+ * virtual or vertical directory
29295+ */
29296+
29297+#include "aufs.h"
29298+
dece6358 29299+static unsigned int calc_size(int nlen)
1facf9fc 29300+{
dece6358 29301+ return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
1facf9fc 29302+}
29303+
29304+static int set_deblk_end(union au_vdir_deblk_p *p,
29305+ union au_vdir_deblk_p *deblk_end)
29306+{
29307+ if (calc_size(0) <= deblk_end->deblk - p->deblk) {
29308+ p->de->de_str.len = 0;
29309+ /* smp_mb(); */
29310+ return 0;
29311+ }
29312+ return -1; /* error */
29313+}
29314+
29315+/* returns true or false */
29316+static int is_deblk_end(union au_vdir_deblk_p *p,
29317+ union au_vdir_deblk_p *deblk_end)
29318+{
29319+ if (calc_size(0) <= deblk_end->deblk - p->deblk)
29320+ return !p->de->de_str.len;
29321+ return 1;
29322+}
29323+
29324+static unsigned char *last_deblk(struct au_vdir *vdir)
29325+{
29326+ return vdir->vd_deblk[vdir->vd_nblk - 1];
29327+}
29328+
29329+/* ---------------------------------------------------------------------- */
29330+
79b8bda9 29331+/* estimate the appropriate size for name hash table */
1308ab2a 29332+unsigned int au_rdhash_est(loff_t sz)
29333+{
29334+ unsigned int n;
29335+
29336+ n = UINT_MAX;
29337+ sz >>= 10;
29338+ if (sz < n)
29339+ n = sz;
29340+ if (sz < AUFS_RDHASH_DEF)
29341+ n = AUFS_RDHASH_DEF;
4a4d8108 29342+ /* pr_info("n %u\n", n); */
1308ab2a 29343+ return n;
29344+}
29345+
1facf9fc 29346+/*
29347+ * the allocated memory has to be freed by
dece6358 29348+ * au_nhash_wh_free() or au_nhash_de_free().
1facf9fc 29349+ */
dece6358 29350+int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
1facf9fc 29351+{
1facf9fc 29352+ struct hlist_head *head;
dece6358 29353+ unsigned int u;
076b876e 29354+ size_t sz;
1facf9fc 29355+
076b876e
AM
29356+ sz = sizeof(*nhash->nh_head) * num_hash;
29357+ head = kmalloc(sz, gfp);
dece6358
AM
29358+ if (head) {
29359+ nhash->nh_num = num_hash;
29360+ nhash->nh_head = head;
29361+ for (u = 0; u < num_hash; u++)
1facf9fc 29362+ INIT_HLIST_HEAD(head++);
dece6358 29363+ return 0; /* success */
1facf9fc 29364+ }
1facf9fc 29365+
dece6358 29366+ return -ENOMEM;
1facf9fc 29367+}
29368+
dece6358
AM
29369+static void nhash_count(struct hlist_head *head)
29370+{
29371+#if 0
29372+ unsigned long n;
29373+ struct hlist_node *pos;
29374+
29375+ n = 0;
29376+ hlist_for_each(pos, head)
29377+ n++;
4a4d8108 29378+ pr_info("%lu\n", n);
dece6358
AM
29379+#endif
29380+}
29381+
29382+static void au_nhash_wh_do_free(struct hlist_head *head)
1facf9fc 29383+{
c06a8ce3
AM
29384+ struct au_vdir_wh *pos;
29385+ struct hlist_node *node;
1facf9fc 29386+
c06a8ce3
AM
29387+ hlist_for_each_entry_safe(pos, node, head, wh_hash)
29388+ kfree(pos);
1facf9fc 29389+}
29390+
dece6358 29391+static void au_nhash_de_do_free(struct hlist_head *head)
1facf9fc 29392+{
c06a8ce3
AM
29393+ struct au_vdir_dehstr *pos;
29394+ struct hlist_node *node;
1facf9fc 29395+
c06a8ce3
AM
29396+ hlist_for_each_entry_safe(pos, node, head, hash)
29397+ au_cache_free_vdir_dehstr(pos);
1facf9fc 29398+}
29399+
dece6358
AM
29400+static void au_nhash_do_free(struct au_nhash *nhash,
29401+ void (*free)(struct hlist_head *head))
1facf9fc 29402+{
1308ab2a 29403+ unsigned int n;
1facf9fc 29404+ struct hlist_head *head;
1facf9fc 29405+
dece6358 29406+ n = nhash->nh_num;
1308ab2a 29407+ if (!n)
29408+ return;
29409+
dece6358 29410+ head = nhash->nh_head;
1308ab2a 29411+ while (n-- > 0) {
dece6358
AM
29412+ nhash_count(head);
29413+ free(head++);
1facf9fc 29414+ }
dece6358 29415+ kfree(nhash->nh_head);
1facf9fc 29416+}
29417+
dece6358 29418+void au_nhash_wh_free(struct au_nhash *whlist)
1facf9fc 29419+{
dece6358
AM
29420+ au_nhash_do_free(whlist, au_nhash_wh_do_free);
29421+}
1facf9fc 29422+
dece6358
AM
29423+static void au_nhash_de_free(struct au_nhash *delist)
29424+{
29425+ au_nhash_do_free(delist, au_nhash_de_do_free);
1facf9fc 29426+}
29427+
29428+/* ---------------------------------------------------------------------- */
29429+
29430+int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
29431+ int limit)
29432+{
29433+ int num;
29434+ unsigned int u, n;
29435+ struct hlist_head *head;
c06a8ce3 29436+ struct au_vdir_wh *pos;
1facf9fc 29437+
29438+ num = 0;
29439+ n = whlist->nh_num;
29440+ head = whlist->nh_head;
1308ab2a 29441+ for (u = 0; u < n; u++, head++)
c06a8ce3
AM
29442+ hlist_for_each_entry(pos, head, wh_hash)
29443+ if (pos->wh_bindex == btgt && ++num > limit)
1facf9fc 29444+ return 1;
1facf9fc 29445+ return 0;
29446+}
29447+
29448+static struct hlist_head *au_name_hash(struct au_nhash *nhash,
dece6358 29449+ unsigned char *name,
1facf9fc 29450+ unsigned int len)
29451+{
dece6358
AM
29452+ unsigned int v;
29453+ /* const unsigned int magic_bit = 12; */
29454+
1308ab2a 29455+ AuDebugOn(!nhash->nh_num || !nhash->nh_head);
29456+
dece6358
AM
29457+ v = 0;
29458+ while (len--)
29459+ v += *name++;
29460+ /* v = hash_long(v, magic_bit); */
29461+ v %= nhash->nh_num;
29462+ return nhash->nh_head + v;
29463+}
29464+
29465+static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
29466+ int nlen)
29467+{
29468+ return str->len == nlen && !memcmp(str->name, name, nlen);
1facf9fc 29469+}
29470+
29471+/* returns found or not */
dece6358 29472+int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
1facf9fc 29473+{
29474+ struct hlist_head *head;
c06a8ce3 29475+ struct au_vdir_wh *pos;
1facf9fc 29476+ struct au_vdir_destr *str;
29477+
dece6358 29478+ head = au_name_hash(whlist, name, nlen);
c06a8ce3
AM
29479+ hlist_for_each_entry(pos, head, wh_hash) {
29480+ str = &pos->wh_str;
1facf9fc 29481+ AuDbg("%.*s\n", str->len, str->name);
dece6358
AM
29482+ if (au_nhash_test_name(str, name, nlen))
29483+ return 1;
29484+ }
29485+ return 0;
29486+}
29487+
29488+/* returns found(true) or not */
29489+static int test_known(struct au_nhash *delist, char *name, int nlen)
29490+{
29491+ struct hlist_head *head;
c06a8ce3 29492+ struct au_vdir_dehstr *pos;
dece6358
AM
29493+ struct au_vdir_destr *str;
29494+
29495+ head = au_name_hash(delist, name, nlen);
c06a8ce3
AM
29496+ hlist_for_each_entry(pos, head, hash) {
29497+ str = pos->str;
dece6358
AM
29498+ AuDbg("%.*s\n", str->len, str->name);
29499+ if (au_nhash_test_name(str, name, nlen))
1facf9fc 29500+ return 1;
29501+ }
29502+ return 0;
29503+}
29504+
dece6358
AM
29505+static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
29506+ unsigned char d_type)
29507+{
29508+#ifdef CONFIG_AUFS_SHWH
29509+ wh->wh_ino = ino;
29510+ wh->wh_type = d_type;
29511+#endif
29512+}
29513+
29514+/* ---------------------------------------------------------------------- */
29515+
29516+int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
29517+ unsigned int d_type, aufs_bindex_t bindex,
29518+ unsigned char shwh)
1facf9fc 29519+{
29520+ int err;
29521+ struct au_vdir_destr *str;
29522+ struct au_vdir_wh *wh;
29523+
dece6358 29524+ AuDbg("%.*s\n", nlen, name);
1308ab2a 29525+ AuDebugOn(!whlist->nh_num || !whlist->nh_head);
29526+
1facf9fc 29527+ err = -ENOMEM;
dece6358 29528+ wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
1facf9fc 29529+ if (unlikely(!wh))
29530+ goto out;
29531+
29532+ err = 0;
29533+ wh->wh_bindex = bindex;
dece6358
AM
29534+ if (shwh)
29535+ au_shwh_init_wh(wh, ino, d_type);
1facf9fc 29536+ str = &wh->wh_str;
dece6358
AM
29537+ str->len = nlen;
29538+ memcpy(str->name, name, nlen);
29539+ hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
1facf9fc 29540+ /* smp_mb(); */
29541+
4f0767ce 29542+out:
1facf9fc 29543+ return err;
29544+}
29545+
1facf9fc 29546+static int append_deblk(struct au_vdir *vdir)
29547+{
29548+ int err;
dece6358 29549+ unsigned long ul;
1facf9fc 29550+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
29551+ union au_vdir_deblk_p p, deblk_end;
29552+ unsigned char **o;
29553+
29554+ err = -ENOMEM;
dece6358
AM
29555+ o = krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
29556+ GFP_NOFS);
1facf9fc 29557+ if (unlikely(!o))
29558+ goto out;
29559+
29560+ vdir->vd_deblk = o;
29561+ p.deblk = kmalloc(deblk_sz, GFP_NOFS);
29562+ if (p.deblk) {
29563+ ul = vdir->vd_nblk++;
29564+ vdir->vd_deblk[ul] = p.deblk;
29565+ vdir->vd_last.ul = ul;
29566+ vdir->vd_last.p.deblk = p.deblk;
29567+ deblk_end.deblk = p.deblk + deblk_sz;
29568+ err = set_deblk_end(&p, &deblk_end);
29569+ }
29570+
4f0767ce 29571+out:
1facf9fc 29572+ return err;
29573+}
29574+
dece6358
AM
29575+static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
29576+ unsigned int d_type, struct au_nhash *delist)
29577+{
29578+ int err;
29579+ unsigned int sz;
29580+ const unsigned int deblk_sz = vdir->vd_deblk_sz;
29581+ union au_vdir_deblk_p p, *room, deblk_end;
29582+ struct au_vdir_dehstr *dehstr;
29583+
29584+ p.deblk = last_deblk(vdir);
29585+ deblk_end.deblk = p.deblk + deblk_sz;
29586+ room = &vdir->vd_last.p;
29587+ AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
29588+ || !is_deblk_end(room, &deblk_end));
29589+
29590+ sz = calc_size(nlen);
29591+ if (unlikely(sz > deblk_end.deblk - room->deblk)) {
29592+ err = append_deblk(vdir);
29593+ if (unlikely(err))
29594+ goto out;
29595+
29596+ p.deblk = last_deblk(vdir);
29597+ deblk_end.deblk = p.deblk + deblk_sz;
29598+ /* smp_mb(); */
29599+ AuDebugOn(room->deblk != p.deblk);
29600+ }
29601+
29602+ err = -ENOMEM;
4a4d8108 29603+ dehstr = au_cache_alloc_vdir_dehstr();
dece6358
AM
29604+ if (unlikely(!dehstr))
29605+ goto out;
29606+
29607+ dehstr->str = &room->de->de_str;
29608+ hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
29609+ room->de->de_ino = ino;
29610+ room->de->de_type = d_type;
29611+ room->de->de_str.len = nlen;
29612+ memcpy(room->de->de_str.name, name, nlen);
29613+
29614+ err = 0;
29615+ room->deblk += sz;
29616+ if (unlikely(set_deblk_end(room, &deblk_end)))
29617+ err = append_deblk(vdir);
29618+ /* smp_mb(); */
29619+
4f0767ce 29620+out:
dece6358
AM
29621+ return err;
29622+}
29623+
29624+/* ---------------------------------------------------------------------- */
29625+
29626+void au_vdir_free(struct au_vdir *vdir)
29627+{
29628+ unsigned char **deblk;
29629+
29630+ deblk = vdir->vd_deblk;
29631+ while (vdir->vd_nblk--)
29632+ kfree(*deblk++);
29633+ kfree(vdir->vd_deblk);
29634+ au_cache_free_vdir(vdir);
29635+}
29636+
1308ab2a 29637+static struct au_vdir *alloc_vdir(struct file *file)
1facf9fc 29638+{
29639+ struct au_vdir *vdir;
1308ab2a 29640+ struct super_block *sb;
1facf9fc 29641+ int err;
29642+
2000de60 29643+ sb = file->f_path.dentry->d_sb;
dece6358
AM
29644+ SiMustAnyLock(sb);
29645+
1facf9fc 29646+ err = -ENOMEM;
29647+ vdir = au_cache_alloc_vdir();
29648+ if (unlikely(!vdir))
29649+ goto out;
29650+
29651+ vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
29652+ if (unlikely(!vdir->vd_deblk))
29653+ goto out_free;
29654+
29655+ vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
1308ab2a 29656+ if (!vdir->vd_deblk_sz) {
79b8bda9 29657+ /* estimate the appropriate size for deblk */
1308ab2a 29658+ vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
4a4d8108 29659+ /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
1308ab2a 29660+ }
1facf9fc 29661+ vdir->vd_nblk = 0;
29662+ vdir->vd_version = 0;
29663+ vdir->vd_jiffy = 0;
29664+ err = append_deblk(vdir);
29665+ if (!err)
29666+ return vdir; /* success */
29667+
29668+ kfree(vdir->vd_deblk);
29669+
4f0767ce 29670+out_free:
1facf9fc 29671+ au_cache_free_vdir(vdir);
4f0767ce 29672+out:
1facf9fc 29673+ vdir = ERR_PTR(err);
29674+ return vdir;
29675+}
29676+
29677+static int reinit_vdir(struct au_vdir *vdir)
29678+{
29679+ int err;
29680+ union au_vdir_deblk_p p, deblk_end;
29681+
29682+ while (vdir->vd_nblk > 1) {
29683+ kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
29684+ /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
29685+ vdir->vd_nblk--;
29686+ }
29687+ p.deblk = vdir->vd_deblk[0];
29688+ deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
29689+ err = set_deblk_end(&p, &deblk_end);
29690+ /* keep vd_dblk_sz */
29691+ vdir->vd_last.ul = 0;
29692+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29693+ vdir->vd_version = 0;
29694+ vdir->vd_jiffy = 0;
29695+ /* smp_mb(); */
29696+ return err;
29697+}
29698+
29699+/* ---------------------------------------------------------------------- */
29700+
1facf9fc 29701+#define AuFillVdir_CALLED 1
29702+#define AuFillVdir_WHABLE (1 << 1)
dece6358 29703+#define AuFillVdir_SHWH (1 << 2)
1facf9fc 29704+#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
7f207e10
AM
29705+#define au_fset_fillvdir(flags, name) \
29706+ do { (flags) |= AuFillVdir_##name; } while (0)
29707+#define au_fclr_fillvdir(flags, name) \
29708+ do { (flags) &= ~AuFillVdir_##name; } while (0)
1facf9fc 29709+
dece6358
AM
29710+#ifndef CONFIG_AUFS_SHWH
29711+#undef AuFillVdir_SHWH
29712+#define AuFillVdir_SHWH 0
29713+#endif
29714+
1facf9fc 29715+struct fillvdir_arg {
392086de 29716+ struct dir_context ctx;
1facf9fc 29717+ struct file *file;
29718+ struct au_vdir *vdir;
dece6358
AM
29719+ struct au_nhash delist;
29720+ struct au_nhash whlist;
1facf9fc 29721+ aufs_bindex_t bindex;
29722+ unsigned int flags;
29723+ int err;
29724+};
29725+
392086de 29726+static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
1facf9fc 29727+ loff_t offset __maybe_unused, u64 h_ino,
29728+ unsigned int d_type)
29729+{
392086de 29730+ struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
1facf9fc 29731+ char *name = (void *)__name;
29732+ struct super_block *sb;
1facf9fc 29733+ ino_t ino;
dece6358 29734+ const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
1facf9fc 29735+
1facf9fc 29736+ arg->err = 0;
2000de60 29737+ sb = arg->file->f_path.dentry->d_sb;
1facf9fc 29738+ au_fset_fillvdir(arg->flags, CALLED);
29739+ /* smp_mb(); */
dece6358 29740+ if (nlen <= AUFS_WH_PFX_LEN
1facf9fc 29741+ || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
dece6358
AM
29742+ if (test_known(&arg->delist, name, nlen)
29743+ || au_nhash_test_known_wh(&arg->whlist, name, nlen))
29744+ goto out; /* already exists or whiteouted */
1facf9fc 29745+
dece6358 29746+ arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
4a4d8108
AM
29747+ if (!arg->err) {
29748+ if (unlikely(nlen > AUFS_MAX_NAMELEN))
29749+ d_type = DT_UNKNOWN;
dece6358
AM
29750+ arg->err = append_de(arg->vdir, name, nlen, ino,
29751+ d_type, &arg->delist);
4a4d8108 29752+ }
1facf9fc 29753+ } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
29754+ name += AUFS_WH_PFX_LEN;
dece6358
AM
29755+ nlen -= AUFS_WH_PFX_LEN;
29756+ if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
29757+ goto out; /* already whiteouted */
1facf9fc 29758+
dece6358
AM
29759+ if (shwh)
29760+ arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
29761+ &ino);
4a4d8108
AM
29762+ if (!arg->err) {
29763+ if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
29764+ d_type = DT_UNKNOWN;
1facf9fc 29765+ arg->err = au_nhash_append_wh
dece6358
AM
29766+ (&arg->whlist, name, nlen, ino, d_type,
29767+ arg->bindex, shwh);
4a4d8108 29768+ }
1facf9fc 29769+ }
29770+
4f0767ce 29771+out:
1facf9fc 29772+ if (!arg->err)
29773+ arg->vdir->vd_jiffy = jiffies;
29774+ /* smp_mb(); */
29775+ AuTraceErr(arg->err);
29776+ return arg->err;
29777+}
29778+
dece6358
AM
29779+static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
29780+ struct au_nhash *whlist, struct au_nhash *delist)
29781+{
29782+#ifdef CONFIG_AUFS_SHWH
29783+ int err;
29784+ unsigned int nh, u;
29785+ struct hlist_head *head;
c06a8ce3
AM
29786+ struct au_vdir_wh *pos;
29787+ struct hlist_node *n;
dece6358
AM
29788+ char *p, *o;
29789+ struct au_vdir_destr *destr;
29790+
29791+ AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
29792+
29793+ err = -ENOMEM;
537831f9 29794+ o = p = (void *)__get_free_page(GFP_NOFS);
dece6358
AM
29795+ if (unlikely(!p))
29796+ goto out;
29797+
29798+ err = 0;
29799+ nh = whlist->nh_num;
29800+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
29801+ p += AUFS_WH_PFX_LEN;
29802+ for (u = 0; u < nh; u++) {
29803+ head = whlist->nh_head + u;
c06a8ce3
AM
29804+ hlist_for_each_entry_safe(pos, n, head, wh_hash) {
29805+ destr = &pos->wh_str;
dece6358
AM
29806+ memcpy(p, destr->name, destr->len);
29807+ err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
c06a8ce3 29808+ pos->wh_ino, pos->wh_type, delist);
dece6358
AM
29809+ if (unlikely(err))
29810+ break;
29811+ }
29812+ }
29813+
537831f9 29814+ free_page((unsigned long)o);
dece6358 29815+
4f0767ce 29816+out:
dece6358
AM
29817+ AuTraceErr(err);
29818+ return err;
29819+#else
29820+ return 0;
29821+#endif
29822+}
29823+
1facf9fc 29824+static int au_do_read_vdir(struct fillvdir_arg *arg)
29825+{
29826+ int err;
dece6358 29827+ unsigned int rdhash;
1facf9fc 29828+ loff_t offset;
5afbbe0d 29829+ aufs_bindex_t bbot, bindex, btop;
dece6358 29830+ unsigned char shwh;
1facf9fc 29831+ struct file *hf, *file;
29832+ struct super_block *sb;
29833+
1facf9fc 29834+ file = arg->file;
2000de60 29835+ sb = file->f_path.dentry->d_sb;
dece6358
AM
29836+ SiMustAnyLock(sb);
29837+
29838+ rdhash = au_sbi(sb)->si_rdhash;
1308ab2a 29839+ if (!rdhash)
29840+ rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
dece6358
AM
29841+ err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
29842+ if (unlikely(err))
1facf9fc 29843+ goto out;
dece6358
AM
29844+ err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
29845+ if (unlikely(err))
1facf9fc 29846+ goto out_delist;
29847+
29848+ err = 0;
29849+ arg->flags = 0;
dece6358
AM
29850+ shwh = 0;
29851+ if (au_opt_test(au_mntflags(sb), SHWH)) {
29852+ shwh = 1;
29853+ au_fset_fillvdir(arg->flags, SHWH);
29854+ }
5afbbe0d
AM
29855+ btop = au_fbtop(file);
29856+ bbot = au_fbbot_dir(file);
29857+ for (bindex = btop; !err && bindex <= bbot; bindex++) {
4a4d8108 29858+ hf = au_hf_dir(file, bindex);
1facf9fc 29859+ if (!hf)
29860+ continue;
29861+
29862+ offset = vfsub_llseek(hf, 0, SEEK_SET);
29863+ err = offset;
29864+ if (unlikely(offset))
29865+ break;
29866+
29867+ arg->bindex = bindex;
29868+ au_fclr_fillvdir(arg->flags, WHABLE);
dece6358 29869+ if (shwh
5afbbe0d 29870+ || (bindex != bbot
dece6358 29871+ && au_br_whable(au_sbr_perm(sb, bindex))))
1facf9fc 29872+ au_fset_fillvdir(arg->flags, WHABLE);
29873+ do {
29874+ arg->err = 0;
29875+ au_fclr_fillvdir(arg->flags, CALLED);
29876+ /* smp_mb(); */
392086de 29877+ err = vfsub_iterate_dir(hf, &arg->ctx);
1facf9fc 29878+ if (err >= 0)
29879+ err = arg->err;
29880+ } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
392086de
AM
29881+
29882+ /*
29883+ * dir_relax() may be good for concurrency, but aufs should not
29884+ * use it since it will cause a lockdep problem.
29885+ */
1facf9fc 29886+ }
dece6358
AM
29887+
29888+ if (!err && shwh)
29889+ err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
29890+
29891+ au_nhash_wh_free(&arg->whlist);
1facf9fc 29892+
4f0767ce 29893+out_delist:
dece6358 29894+ au_nhash_de_free(&arg->delist);
4f0767ce 29895+out:
1facf9fc 29896+ return err;
29897+}
29898+
29899+static int read_vdir(struct file *file, int may_read)
29900+{
29901+ int err;
29902+ unsigned long expire;
29903+ unsigned char do_read;
392086de
AM
29904+ struct fillvdir_arg arg = {
29905+ .ctx = {
2000de60 29906+ .actor = fillvdir
392086de
AM
29907+ }
29908+ };
1facf9fc 29909+ struct inode *inode;
29910+ struct au_vdir *vdir, *allocated;
29911+
29912+ err = 0;
c06a8ce3 29913+ inode = file_inode(file);
1facf9fc 29914+ IMustLock(inode);
5afbbe0d 29915+ IiMustWriteLock(inode);
dece6358
AM
29916+ SiMustAnyLock(inode->i_sb);
29917+
1facf9fc 29918+ allocated = NULL;
29919+ do_read = 0;
29920+ expire = au_sbi(inode->i_sb)->si_rdcache;
29921+ vdir = au_ivdir(inode);
29922+ if (!vdir) {
29923+ do_read = 1;
1308ab2a 29924+ vdir = alloc_vdir(file);
1facf9fc 29925+ err = PTR_ERR(vdir);
29926+ if (IS_ERR(vdir))
29927+ goto out;
29928+ err = 0;
29929+ allocated = vdir;
29930+ } else if (may_read
29931+ && (inode->i_version != vdir->vd_version
29932+ || time_after(jiffies, vdir->vd_jiffy + expire))) {
29933+ do_read = 1;
29934+ err = reinit_vdir(vdir);
29935+ if (unlikely(err))
29936+ goto out;
29937+ }
29938+
29939+ if (!do_read)
29940+ return 0; /* success */
29941+
29942+ arg.file = file;
29943+ arg.vdir = vdir;
29944+ err = au_do_read_vdir(&arg);
29945+ if (!err) {
392086de 29946+ /* file->f_pos = 0; */ /* todo: ctx->pos? */
1facf9fc 29947+ vdir->vd_version = inode->i_version;
29948+ vdir->vd_last.ul = 0;
29949+ vdir->vd_last.p.deblk = vdir->vd_deblk[0];
29950+ if (allocated)
29951+ au_set_ivdir(inode, allocated);
29952+ } else if (allocated)
29953+ au_vdir_free(allocated);
29954+
4f0767ce 29955+out:
1facf9fc 29956+ return err;
29957+}
29958+
29959+static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
29960+{
29961+ int err, rerr;
29962+ unsigned long ul, n;
29963+ const unsigned int deblk_sz = src->vd_deblk_sz;
29964+
29965+ AuDebugOn(tgt->vd_nblk != 1);
29966+
29967+ err = -ENOMEM;
29968+ if (tgt->vd_nblk < src->vd_nblk) {
29969+ unsigned char **p;
29970+
dece6358
AM
29971+ p = krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
29972+ GFP_NOFS);
1facf9fc 29973+ if (unlikely(!p))
29974+ goto out;
29975+ tgt->vd_deblk = p;
29976+ }
29977+
1308ab2a 29978+ if (tgt->vd_deblk_sz != deblk_sz) {
29979+ unsigned char *p;
29980+
29981+ tgt->vd_deblk_sz = deblk_sz;
29982+ p = krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS);
29983+ if (unlikely(!p))
29984+ goto out;
29985+ tgt->vd_deblk[0] = p;
29986+ }
1facf9fc 29987+ memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
1facf9fc 29988+ tgt->vd_version = src->vd_version;
29989+ tgt->vd_jiffy = src->vd_jiffy;
29990+
29991+ n = src->vd_nblk;
29992+ for (ul = 1; ul < n; ul++) {
dece6358
AM
29993+ tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
29994+ GFP_NOFS);
29995+ if (unlikely(!tgt->vd_deblk[ul]))
1facf9fc 29996+ goto out;
1308ab2a 29997+ tgt->vd_nblk++;
1facf9fc 29998+ }
1308ab2a 29999+ tgt->vd_nblk = n;
30000+ tgt->vd_last.ul = tgt->vd_last.ul;
30001+ tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
30002+ tgt->vd_last.p.deblk += src->vd_last.p.deblk
30003+ - src->vd_deblk[src->vd_last.ul];
1facf9fc 30004+ /* smp_mb(); */
30005+ return 0; /* success */
30006+
4f0767ce 30007+out:
1facf9fc 30008+ rerr = reinit_vdir(tgt);
30009+ BUG_ON(rerr);
30010+ return err;
30011+}
30012+
30013+int au_vdir_init(struct file *file)
30014+{
30015+ int err;
30016+ struct inode *inode;
30017+ struct au_vdir *vdir_cache, *allocated;
30018+
392086de 30019+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 30020+ err = read_vdir(file, !file->f_pos);
30021+ if (unlikely(err))
30022+ goto out;
30023+
30024+ allocated = NULL;
30025+ vdir_cache = au_fvdir_cache(file);
30026+ if (!vdir_cache) {
1308ab2a 30027+ vdir_cache = alloc_vdir(file);
1facf9fc 30028+ err = PTR_ERR(vdir_cache);
30029+ if (IS_ERR(vdir_cache))
30030+ goto out;
30031+ allocated = vdir_cache;
30032+ } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
392086de 30033+ /* test file->f_pos here instead of ctx->pos */
1facf9fc 30034+ err = reinit_vdir(vdir_cache);
30035+ if (unlikely(err))
30036+ goto out;
30037+ } else
30038+ return 0; /* success */
30039+
c06a8ce3 30040+ inode = file_inode(file);
1facf9fc 30041+ err = copy_vdir(vdir_cache, au_ivdir(inode));
30042+ if (!err) {
30043+ file->f_version = inode->i_version;
30044+ if (allocated)
30045+ au_set_fvdir_cache(file, allocated);
30046+ } else if (allocated)
30047+ au_vdir_free(allocated);
30048+
4f0767ce 30049+out:
1facf9fc 30050+ return err;
30051+}
30052+
30053+static loff_t calc_offset(struct au_vdir *vdir)
30054+{
30055+ loff_t offset;
30056+ union au_vdir_deblk_p p;
30057+
30058+ p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
30059+ offset = vdir->vd_last.p.deblk - p.deblk;
30060+ offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
30061+ return offset;
30062+}
30063+
30064+/* returns true or false */
392086de 30065+static int seek_vdir(struct file *file, struct dir_context *ctx)
1facf9fc 30066+{
30067+ int valid;
30068+ unsigned int deblk_sz;
30069+ unsigned long ul, n;
30070+ loff_t offset;
30071+ union au_vdir_deblk_p p, deblk_end;
30072+ struct au_vdir *vdir_cache;
30073+
30074+ valid = 1;
30075+ vdir_cache = au_fvdir_cache(file);
30076+ offset = calc_offset(vdir_cache);
30077+ AuDbg("offset %lld\n", offset);
392086de 30078+ if (ctx->pos == offset)
1facf9fc 30079+ goto out;
30080+
30081+ vdir_cache->vd_last.ul = 0;
30082+ vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
392086de 30083+ if (!ctx->pos)
1facf9fc 30084+ goto out;
30085+
30086+ valid = 0;
30087+ deblk_sz = vdir_cache->vd_deblk_sz;
392086de 30088+ ul = div64_u64(ctx->pos, deblk_sz);
1facf9fc 30089+ AuDbg("ul %lu\n", ul);
30090+ if (ul >= vdir_cache->vd_nblk)
30091+ goto out;
30092+
30093+ n = vdir_cache->vd_nblk;
30094+ for (; ul < n; ul++) {
30095+ p.deblk = vdir_cache->vd_deblk[ul];
30096+ deblk_end.deblk = p.deblk + deblk_sz;
30097+ offset = ul;
30098+ offset *= deblk_sz;
392086de 30099+ while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
1facf9fc 30100+ unsigned int l;
30101+
30102+ l = calc_size(p.de->de_str.len);
30103+ offset += l;
30104+ p.deblk += l;
30105+ }
30106+ if (!is_deblk_end(&p, &deblk_end)) {
30107+ valid = 1;
30108+ vdir_cache->vd_last.ul = ul;
30109+ vdir_cache->vd_last.p = p;
30110+ break;
30111+ }
30112+ }
30113+
4f0767ce 30114+out:
1facf9fc 30115+ /* smp_mb(); */
30116+ AuTraceErr(!valid);
30117+ return valid;
30118+}
30119+
392086de 30120+int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
1facf9fc 30121+{
1facf9fc 30122+ unsigned int l, deblk_sz;
30123+ union au_vdir_deblk_p deblk_end;
30124+ struct au_vdir *vdir_cache;
30125+ struct au_vdir_de *de;
30126+
30127+ vdir_cache = au_fvdir_cache(file);
392086de 30128+ if (!seek_vdir(file, ctx))
1facf9fc 30129+ return 0;
30130+
30131+ deblk_sz = vdir_cache->vd_deblk_sz;
30132+ while (1) {
30133+ deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
30134+ deblk_end.deblk += deblk_sz;
30135+ while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
30136+ de = vdir_cache->vd_last.p.de;
30137+ AuDbg("%.*s, off%lld, i%lu, dt%d\n",
392086de 30138+ de->de_str.len, de->de_str.name, ctx->pos,
1facf9fc 30139+ (unsigned long)de->de_ino, de->de_type);
392086de
AM
30140+ if (unlikely(!dir_emit(ctx, de->de_str.name,
30141+ de->de_str.len, de->de_ino,
30142+ de->de_type))) {
1facf9fc 30143+ /* todo: ignore the error caused by udba? */
30144+ /* return err; */
30145+ return 0;
30146+ }
30147+
30148+ l = calc_size(de->de_str.len);
30149+ vdir_cache->vd_last.p.deblk += l;
392086de 30150+ ctx->pos += l;
1facf9fc 30151+ }
30152+ if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
30153+ vdir_cache->vd_last.ul++;
30154+ vdir_cache->vd_last.p.deblk
30155+ = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
392086de 30156+ ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
1facf9fc 30157+ continue;
30158+ }
30159+ break;
30160+ }
30161+
30162+ /* smp_mb(); */
30163+ return 0;
30164+}
7f207e10
AM
30165diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
30166--- /usr/share/empty/fs/aufs/vfsub.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 30167+++ linux/fs/aufs/vfsub.c 2016-07-25 19:05:34.817826663 +0200
febd17d6 30168@@ -0,0 +1,884 @@
1facf9fc 30169+/*
8cdd5066 30170+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 30171+ *
30172+ * This program, aufs is free software; you can redistribute it and/or modify
30173+ * it under the terms of the GNU General Public License as published by
30174+ * the Free Software Foundation; either version 2 of the License, or
30175+ * (at your option) any later version.
dece6358
AM
30176+ *
30177+ * This program is distributed in the hope that it will be useful,
30178+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30179+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30180+ * GNU General Public License for more details.
30181+ *
30182+ * You should have received a copy of the GNU General Public License
523b37e3 30183+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 30184+ */
30185+
30186+/*
30187+ * sub-routines for VFS
30188+ */
30189+
dece6358 30190+#include <linux/namei.h>
8cdd5066 30191+#include <linux/nsproxy.h>
dece6358
AM
30192+#include <linux/security.h>
30193+#include <linux/splice.h>
8cdd5066 30194+#include "../fs/mount.h"
1facf9fc 30195+#include "aufs.h"
30196+
8cdd5066
JR
30197+#ifdef CONFIG_AUFS_BR_FUSE
30198+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
30199+{
30200+ struct nsproxy *ns;
30201+
30202+ if (!au_test_fuse(h_sb) || !au_userns)
30203+ return 0;
30204+
30205+ ns = current->nsproxy;
30206+ /* no {get,put}_nsproxy(ns) */
30207+ return real_mount(mnt)->mnt_ns == ns->mnt_ns ? 0 : -EACCES;
30208+}
30209+#endif
30210+
30211+/* ---------------------------------------------------------------------- */
30212+
1facf9fc 30213+int vfsub_update_h_iattr(struct path *h_path, int *did)
30214+{
30215+ int err;
30216+ struct kstat st;
30217+ struct super_block *h_sb;
30218+
30219+ /* for remote fs, leave work for its getattr or d_revalidate */
30220+ /* for bad i_attr fs, handle them in aufs_getattr() */
30221+ /* still some fs may acquire i_mutex. we need to skip them */
30222+ err = 0;
30223+ if (!did)
30224+ did = &err;
30225+ h_sb = h_path->dentry->d_sb;
30226+ *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
30227+ if (*did)
c06a8ce3 30228+ err = vfs_getattr(h_path, &st);
1facf9fc 30229+
30230+ return err;
30231+}
30232+
30233+/* ---------------------------------------------------------------------- */
30234+
4a4d8108 30235+struct file *vfsub_dentry_open(struct path *path, int flags)
1308ab2a 30236+{
30237+ struct file *file;
30238+
b4510431 30239+ file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
7f207e10 30240+ current_cred());
2cbb1c4b
JR
30241+ if (!IS_ERR_OR_NULL(file)
30242+ && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
5527c038 30243+ i_readcount_inc(d_inode(path->dentry));
4a4d8108 30244+
1308ab2a 30245+ return file;
30246+}
30247+
1facf9fc 30248+struct file *vfsub_filp_open(const char *path, int oflags, int mode)
30249+{
30250+ struct file *file;
30251+
2cbb1c4b 30252+ lockdep_off();
7f207e10 30253+ file = filp_open(path,
2cbb1c4b 30254+ oflags /* | __FMODE_NONOTIFY */,
7f207e10 30255+ mode);
2cbb1c4b 30256+ lockdep_on();
1facf9fc 30257+ if (IS_ERR(file))
30258+ goto out;
30259+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30260+
4f0767ce 30261+out:
1facf9fc 30262+ return file;
30263+}
30264+
b912730e
AM
30265+/*
30266+ * Ideally this function should call VFS:do_last() in order to keep all its
30267+ * checkings. But it is very hard for aufs to regenerate several VFS internal
30268+ * structure such as nameidata. This is a second (or third) best approach.
30269+ * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
30270+ */
30271+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
30272+ struct vfsub_aopen_args *args, struct au_branch *br)
30273+{
30274+ int err;
30275+ struct file *file = args->file;
30276+ /* copied from linux/fs/namei.c:atomic_open() */
30277+ struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
30278+
30279+ IMustLock(dir);
30280+ AuDebugOn(!dir->i_op->atomic_open);
30281+
30282+ err = au_br_test_oflag(args->open_flag, br);
30283+ if (unlikely(err))
30284+ goto out;
30285+
30286+ args->file->f_path.dentry = DENTRY_NOT_SET;
30287+ args->file->f_path.mnt = au_br_mnt(br);
30288+ err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
30289+ args->create_mode, args->opened);
30290+ if (err >= 0) {
30291+ /* some filesystems don't set FILE_CREATED while succeeded? */
30292+ if (*args->opened & FILE_CREATED)
30293+ fsnotify_create(dir, dentry);
30294+ } else
30295+ goto out;
30296+
30297+
30298+ if (!err) {
30299+ /* todo: call VFS:may_open() here */
30300+ err = open_check_o_direct(file);
30301+ /* todo: ima_file_check() too? */
30302+ if (!err && (args->open_flag & __FMODE_EXEC))
30303+ err = deny_write_access(file);
30304+ if (unlikely(err))
30305+ /* note that the file is created and still opened */
30306+ goto out;
30307+ }
30308+
5afbbe0d 30309+ au_br_get(br);
b912730e
AM
30310+ fsnotify_open(file);
30311+
30312+out:
30313+ return err;
30314+}
30315+
1facf9fc 30316+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
30317+{
30318+ int err;
30319+
1facf9fc 30320+ err = kern_path(name, flags, path);
5527c038 30321+ if (!err && d_is_positive(path->dentry))
1facf9fc 30322+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30323+ return err;
30324+}
30325+
febd17d6
JR
30326+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
30327+ struct dentry *parent, int len)
30328+{
30329+ struct path path = {
30330+ .mnt = NULL
30331+ };
30332+
30333+ path.dentry = lookup_one_len_unlocked(name, parent, len);
30334+ if (IS_ERR(path.dentry))
30335+ goto out;
30336+ if (d_is_positive(path.dentry))
30337+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30338+
30339+out:
30340+ AuTraceErrPtr(path.dentry);
30341+ return path.dentry;
30342+}
30343+
1facf9fc 30344+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
30345+ int len)
30346+{
30347+ struct path path = {
30348+ .mnt = NULL
30349+ };
30350+
1308ab2a 30351+ /* VFS checks it too, but by WARN_ON_ONCE() */
5527c038 30352+ IMustLock(d_inode(parent));
1facf9fc 30353+
30354+ path.dentry = lookup_one_len(name, parent, len);
30355+ if (IS_ERR(path.dentry))
30356+ goto out;
5527c038 30357+ if (d_is_positive(path.dentry))
1facf9fc 30358+ vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
30359+
4f0767ce 30360+out:
4a4d8108 30361+ AuTraceErrPtr(path.dentry);
1facf9fc 30362+ return path.dentry;
30363+}
30364+
b4510431 30365+void vfsub_call_lkup_one(void *args)
2cbb1c4b 30366+{
b4510431
AM
30367+ struct vfsub_lkup_one_args *a = args;
30368+ *a->errp = vfsub_lkup_one(a->name, a->parent);
2cbb1c4b
JR
30369+}
30370+
1facf9fc 30371+/* ---------------------------------------------------------------------- */
30372+
30373+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
30374+ struct dentry *d2, struct au_hinode *hdir2)
30375+{
30376+ struct dentry *d;
30377+
2cbb1c4b 30378+ lockdep_off();
1facf9fc 30379+ d = lock_rename(d1, d2);
2cbb1c4b 30380+ lockdep_on();
4a4d8108 30381+ au_hn_suspend(hdir1);
1facf9fc 30382+ if (hdir1 != hdir2)
4a4d8108 30383+ au_hn_suspend(hdir2);
1facf9fc 30384+
30385+ return d;
30386+}
30387+
30388+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
30389+ struct dentry *d2, struct au_hinode *hdir2)
30390+{
4a4d8108 30391+ au_hn_resume(hdir1);
1facf9fc 30392+ if (hdir1 != hdir2)
4a4d8108 30393+ au_hn_resume(hdir2);
2cbb1c4b 30394+ lockdep_off();
1facf9fc 30395+ unlock_rename(d1, d2);
2cbb1c4b 30396+ lockdep_on();
1facf9fc 30397+}
30398+
30399+/* ---------------------------------------------------------------------- */
30400+
b4510431 30401+int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
1facf9fc 30402+{
30403+ int err;
30404+ struct dentry *d;
30405+
30406+ IMustLock(dir);
30407+
30408+ d = path->dentry;
30409+ path->dentry = d->d_parent;
b752ccd1 30410+ err = security_path_mknod(path, d, mode, 0);
1facf9fc 30411+ path->dentry = d;
30412+ if (unlikely(err))
30413+ goto out;
30414+
c1595e42 30415+ lockdep_off();
b4510431 30416+ err = vfs_create(dir, path->dentry, mode, want_excl);
c1595e42 30417+ lockdep_on();
1facf9fc 30418+ if (!err) {
30419+ struct path tmp = *path;
30420+ int did;
30421+
30422+ vfsub_update_h_iattr(&tmp, &did);
30423+ if (did) {
30424+ tmp.dentry = path->dentry->d_parent;
30425+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30426+ }
30427+ /*ignore*/
30428+ }
30429+
4f0767ce 30430+out:
1facf9fc 30431+ return err;
30432+}
30433+
30434+int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
30435+{
30436+ int err;
30437+ struct dentry *d;
30438+
30439+ IMustLock(dir);
30440+
30441+ d = path->dentry;
30442+ path->dentry = d->d_parent;
b752ccd1 30443+ err = security_path_symlink(path, d, symname);
1facf9fc 30444+ path->dentry = d;
30445+ if (unlikely(err))
30446+ goto out;
30447+
c1595e42 30448+ lockdep_off();
1facf9fc 30449+ err = vfs_symlink(dir, path->dentry, symname);
c1595e42 30450+ lockdep_on();
1facf9fc 30451+ if (!err) {
30452+ struct path tmp = *path;
30453+ int did;
30454+
30455+ vfsub_update_h_iattr(&tmp, &did);
30456+ if (did) {
30457+ tmp.dentry = path->dentry->d_parent;
30458+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30459+ }
30460+ /*ignore*/
30461+ }
30462+
4f0767ce 30463+out:
1facf9fc 30464+ return err;
30465+}
30466+
30467+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
30468+{
30469+ int err;
30470+ struct dentry *d;
30471+
30472+ IMustLock(dir);
30473+
30474+ d = path->dentry;
30475+ path->dentry = d->d_parent;
027c5e7a 30476+ err = security_path_mknod(path, d, mode, new_encode_dev(dev));
1facf9fc 30477+ path->dentry = d;
30478+ if (unlikely(err))
30479+ goto out;
30480+
c1595e42 30481+ lockdep_off();
1facf9fc 30482+ err = vfs_mknod(dir, path->dentry, mode, dev);
c1595e42 30483+ lockdep_on();
1facf9fc 30484+ if (!err) {
30485+ struct path tmp = *path;
30486+ int did;
30487+
30488+ vfsub_update_h_iattr(&tmp, &did);
30489+ if (did) {
30490+ tmp.dentry = path->dentry->d_parent;
30491+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30492+ }
30493+ /*ignore*/
30494+ }
30495+
4f0767ce 30496+out:
1facf9fc 30497+ return err;
30498+}
30499+
30500+static int au_test_nlink(struct inode *inode)
30501+{
30502+ const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
30503+
30504+ if (!au_test_fs_no_limit_nlink(inode->i_sb)
30505+ || inode->i_nlink < link_max)
30506+ return 0;
30507+ return -EMLINK;
30508+}
30509+
523b37e3
AM
30510+int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
30511+ struct inode **delegated_inode)
1facf9fc 30512+{
30513+ int err;
30514+ struct dentry *d;
30515+
30516+ IMustLock(dir);
30517+
5527c038 30518+ err = au_test_nlink(d_inode(src_dentry));
1facf9fc 30519+ if (unlikely(err))
30520+ return err;
30521+
b4510431 30522+ /* we don't call may_linkat() */
1facf9fc 30523+ d = path->dentry;
30524+ path->dentry = d->d_parent;
b752ccd1 30525+ err = security_path_link(src_dentry, path, d);
1facf9fc 30526+ path->dentry = d;
30527+ if (unlikely(err))
30528+ goto out;
30529+
2cbb1c4b 30530+ lockdep_off();
523b37e3 30531+ err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
2cbb1c4b 30532+ lockdep_on();
1facf9fc 30533+ if (!err) {
30534+ struct path tmp = *path;
30535+ int did;
30536+
30537+ /* fuse has different memory inode for the same inumber */
30538+ vfsub_update_h_iattr(&tmp, &did);
30539+ if (did) {
30540+ tmp.dentry = path->dentry->d_parent;
30541+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30542+ tmp.dentry = src_dentry;
30543+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30544+ }
30545+ /*ignore*/
30546+ }
30547+
4f0767ce 30548+out:
1facf9fc 30549+ return err;
30550+}
30551+
30552+int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
523b37e3
AM
30553+ struct inode *dir, struct path *path,
30554+ struct inode **delegated_inode)
1facf9fc 30555+{
30556+ int err;
30557+ struct path tmp = {
30558+ .mnt = path->mnt
30559+ };
30560+ struct dentry *d;
30561+
30562+ IMustLock(dir);
30563+ IMustLock(src_dir);
30564+
30565+ d = path->dentry;
30566+ path->dentry = d->d_parent;
30567+ tmp.dentry = src_dentry->d_parent;
38d290e6 30568+ err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
1facf9fc 30569+ path->dentry = d;
30570+ if (unlikely(err))
30571+ goto out;
30572+
2cbb1c4b 30573+ lockdep_off();
523b37e3 30574+ err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
38d290e6 30575+ delegated_inode, /*flags*/0);
2cbb1c4b 30576+ lockdep_on();
1facf9fc 30577+ if (!err) {
30578+ int did;
30579+
30580+ tmp.dentry = d->d_parent;
30581+ vfsub_update_h_iattr(&tmp, &did);
30582+ if (did) {
30583+ tmp.dentry = src_dentry;
30584+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30585+ tmp.dentry = src_dentry->d_parent;
30586+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30587+ }
30588+ /*ignore*/
30589+ }
30590+
4f0767ce 30591+out:
1facf9fc 30592+ return err;
30593+}
30594+
30595+int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
30596+{
30597+ int err;
30598+ struct dentry *d;
30599+
30600+ IMustLock(dir);
30601+
30602+ d = path->dentry;
30603+ path->dentry = d->d_parent;
b752ccd1 30604+ err = security_path_mkdir(path, d, mode);
1facf9fc 30605+ path->dentry = d;
30606+ if (unlikely(err))
30607+ goto out;
30608+
c1595e42 30609+ lockdep_off();
1facf9fc 30610+ err = vfs_mkdir(dir, path->dentry, mode);
c1595e42 30611+ lockdep_on();
1facf9fc 30612+ if (!err) {
30613+ struct path tmp = *path;
30614+ int did;
30615+
30616+ vfsub_update_h_iattr(&tmp, &did);
30617+ if (did) {
30618+ tmp.dentry = path->dentry->d_parent;
30619+ vfsub_update_h_iattr(&tmp, /*did*/NULL);
30620+ }
30621+ /*ignore*/
30622+ }
30623+
4f0767ce 30624+out:
1facf9fc 30625+ return err;
30626+}
30627+
30628+int vfsub_rmdir(struct inode *dir, struct path *path)
30629+{
30630+ int err;
30631+ struct dentry *d;
30632+
30633+ IMustLock(dir);
30634+
30635+ d = path->dentry;
30636+ path->dentry = d->d_parent;
b752ccd1 30637+ err = security_path_rmdir(path, d);
1facf9fc 30638+ path->dentry = d;
30639+ if (unlikely(err))
30640+ goto out;
30641+
2cbb1c4b 30642+ lockdep_off();
1facf9fc 30643+ err = vfs_rmdir(dir, path->dentry);
2cbb1c4b 30644+ lockdep_on();
1facf9fc 30645+ if (!err) {
30646+ struct path tmp = {
30647+ .dentry = path->dentry->d_parent,
30648+ .mnt = path->mnt
30649+ };
30650+
30651+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
30652+ }
30653+
4f0767ce 30654+out:
1facf9fc 30655+ return err;
30656+}
30657+
30658+/* ---------------------------------------------------------------------- */
30659+
9dbd164d 30660+/* todo: support mmap_sem? */
1facf9fc 30661+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
30662+ loff_t *ppos)
30663+{
30664+ ssize_t err;
30665+
2cbb1c4b 30666+ lockdep_off();
1facf9fc 30667+ err = vfs_read(file, ubuf, count, ppos);
2cbb1c4b 30668+ lockdep_on();
1facf9fc 30669+ if (err >= 0)
30670+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30671+ return err;
30672+}
30673+
30674+/* todo: kernel_read()? */
30675+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
30676+ loff_t *ppos)
30677+{
30678+ ssize_t err;
30679+ mm_segment_t oldfs;
b752ccd1
AM
30680+ union {
30681+ void *k;
30682+ char __user *u;
30683+ } buf;
1facf9fc 30684+
b752ccd1 30685+ buf.k = kbuf;
1facf9fc 30686+ oldfs = get_fs();
30687+ set_fs(KERNEL_DS);
b752ccd1 30688+ err = vfsub_read_u(file, buf.u, count, ppos);
1facf9fc 30689+ set_fs(oldfs);
30690+ return err;
30691+}
30692+
30693+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
30694+ loff_t *ppos)
30695+{
30696+ ssize_t err;
30697+
2cbb1c4b 30698+ lockdep_off();
1facf9fc 30699+ err = vfs_write(file, ubuf, count, ppos);
2cbb1c4b 30700+ lockdep_on();
1facf9fc 30701+ if (err >= 0)
30702+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30703+ return err;
30704+}
30705+
30706+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
30707+{
30708+ ssize_t err;
30709+ mm_segment_t oldfs;
b752ccd1
AM
30710+ union {
30711+ void *k;
30712+ const char __user *u;
30713+ } buf;
1facf9fc 30714+
b752ccd1 30715+ buf.k = kbuf;
1facf9fc 30716+ oldfs = get_fs();
30717+ set_fs(KERNEL_DS);
b752ccd1 30718+ err = vfsub_write_u(file, buf.u, count, ppos);
1facf9fc 30719+ set_fs(oldfs);
30720+ return err;
30721+}
30722+
4a4d8108
AM
30723+int vfsub_flush(struct file *file, fl_owner_t id)
30724+{
30725+ int err;
30726+
30727+ err = 0;
523b37e3 30728+ if (file->f_op->flush) {
2000de60 30729+ if (!au_test_nfs(file->f_path.dentry->d_sb))
2cbb1c4b
JR
30730+ err = file->f_op->flush(file, id);
30731+ else {
30732+ lockdep_off();
30733+ err = file->f_op->flush(file, id);
30734+ lockdep_on();
30735+ }
4a4d8108
AM
30736+ if (!err)
30737+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
30738+ /*ignore*/
30739+ }
30740+ return err;
30741+}
30742+
392086de 30743+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
1facf9fc 30744+{
30745+ int err;
30746+
523b37e3 30747+ AuDbg("%pD, ctx{%pf, %llu}\n", file, ctx->actor, ctx->pos);
392086de 30748+
2cbb1c4b 30749+ lockdep_off();
392086de 30750+ err = iterate_dir(file, ctx);
2cbb1c4b 30751+ lockdep_on();
1facf9fc 30752+ if (err >= 0)
30753+ vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
30754+ return err;
30755+}
30756+
30757+long vfsub_splice_to(struct file *in, loff_t *ppos,
30758+ struct pipe_inode_info *pipe, size_t len,
30759+ unsigned int flags)
30760+{
30761+ long err;
30762+
2cbb1c4b 30763+ lockdep_off();
0fc653ad 30764+ err = do_splice_to(in, ppos, pipe, len, flags);
2cbb1c4b 30765+ lockdep_on();
4a4d8108 30766+ file_accessed(in);
1facf9fc 30767+ if (err >= 0)
30768+ vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
30769+ return err;
30770+}
30771+
30772+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
30773+ loff_t *ppos, size_t len, unsigned int flags)
30774+{
30775+ long err;
30776+
2cbb1c4b 30777+ lockdep_off();
0fc653ad 30778+ err = do_splice_from(pipe, out, ppos, len, flags);
2cbb1c4b 30779+ lockdep_on();
1facf9fc 30780+ if (err >= 0)
30781+ vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
30782+ return err;
30783+}
30784+
53392da6
AM
30785+int vfsub_fsync(struct file *file, struct path *path, int datasync)
30786+{
30787+ int err;
30788+
30789+ /* file can be NULL */
30790+ lockdep_off();
30791+ err = vfs_fsync(file, datasync);
30792+ lockdep_on();
30793+ if (!err) {
30794+ if (!path) {
30795+ AuDebugOn(!file);
30796+ path = &file->f_path;
30797+ }
30798+ vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
30799+ }
30800+ return err;
30801+}
30802+
1facf9fc 30803+/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
30804+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
30805+ struct file *h_file)
30806+{
30807+ int err;
30808+ struct inode *h_inode;
c06a8ce3 30809+ struct super_block *h_sb;
1facf9fc 30810+
1facf9fc 30811+ if (!h_file) {
c06a8ce3
AM
30812+ err = vfsub_truncate(h_path, length);
30813+ goto out;
1facf9fc 30814+ }
30815+
5527c038 30816+ h_inode = d_inode(h_path->dentry);
c06a8ce3
AM
30817+ h_sb = h_inode->i_sb;
30818+ lockdep_off();
30819+ sb_start_write(h_sb);
30820+ lockdep_on();
1facf9fc 30821+ err = locks_verify_truncate(h_inode, h_file, length);
30822+ if (!err)
953406b4 30823+ err = security_path_truncate(h_path);
2cbb1c4b
JR
30824+ if (!err) {
30825+ lockdep_off();
1facf9fc 30826+ err = do_truncate(h_path->dentry, length, attr, h_file);
2cbb1c4b
JR
30827+ lockdep_on();
30828+ }
c06a8ce3
AM
30829+ lockdep_off();
30830+ sb_end_write(h_sb);
30831+ lockdep_on();
1facf9fc 30832+
4f0767ce 30833+out:
1facf9fc 30834+ return err;
30835+}
30836+
30837+/* ---------------------------------------------------------------------- */
30838+
30839+struct au_vfsub_mkdir_args {
30840+ int *errp;
30841+ struct inode *dir;
30842+ struct path *path;
30843+ int mode;
30844+};
30845+
30846+static void au_call_vfsub_mkdir(void *args)
30847+{
30848+ struct au_vfsub_mkdir_args *a = args;
30849+ *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
30850+}
30851+
30852+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
30853+{
30854+ int err, do_sio, wkq_err;
30855+
30856+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
30857+ if (!do_sio) {
30858+ lockdep_off();
1facf9fc 30859+ err = vfsub_mkdir(dir, path, mode);
c1595e42
JR
30860+ lockdep_on();
30861+ } else {
1facf9fc 30862+ struct au_vfsub_mkdir_args args = {
30863+ .errp = &err,
30864+ .dir = dir,
30865+ .path = path,
30866+ .mode = mode
30867+ };
30868+ wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
30869+ if (unlikely(wkq_err))
30870+ err = wkq_err;
30871+ }
30872+
30873+ return err;
30874+}
30875+
30876+struct au_vfsub_rmdir_args {
30877+ int *errp;
30878+ struct inode *dir;
30879+ struct path *path;
30880+};
30881+
30882+static void au_call_vfsub_rmdir(void *args)
30883+{
30884+ struct au_vfsub_rmdir_args *a = args;
30885+ *a->errp = vfsub_rmdir(a->dir, a->path);
30886+}
30887+
30888+int vfsub_sio_rmdir(struct inode *dir, struct path *path)
30889+{
30890+ int err, do_sio, wkq_err;
30891+
30892+ do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
c1595e42
JR
30893+ if (!do_sio) {
30894+ lockdep_off();
1facf9fc 30895+ err = vfsub_rmdir(dir, path);
c1595e42
JR
30896+ lockdep_on();
30897+ } else {
1facf9fc 30898+ struct au_vfsub_rmdir_args args = {
30899+ .errp = &err,
30900+ .dir = dir,
30901+ .path = path
30902+ };
30903+ wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
30904+ if (unlikely(wkq_err))
30905+ err = wkq_err;
30906+ }
30907+
30908+ return err;
30909+}
30910+
30911+/* ---------------------------------------------------------------------- */
30912+
30913+struct notify_change_args {
30914+ int *errp;
30915+ struct path *path;
30916+ struct iattr *ia;
523b37e3 30917+ struct inode **delegated_inode;
1facf9fc 30918+};
30919+
30920+static void call_notify_change(void *args)
30921+{
30922+ struct notify_change_args *a = args;
30923+ struct inode *h_inode;
30924+
5527c038 30925+ h_inode = d_inode(a->path->dentry);
1facf9fc 30926+ IMustLock(h_inode);
30927+
30928+ *a->errp = -EPERM;
30929+ if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
c1595e42 30930+ lockdep_off();
523b37e3
AM
30931+ *a->errp = notify_change(a->path->dentry, a->ia,
30932+ a->delegated_inode);
c1595e42 30933+ lockdep_on();
1facf9fc 30934+ if (!*a->errp)
30935+ vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
30936+ }
30937+ AuTraceErr(*a->errp);
30938+}
30939+
523b37e3
AM
30940+int vfsub_notify_change(struct path *path, struct iattr *ia,
30941+ struct inode **delegated_inode)
1facf9fc 30942+{
30943+ int err;
30944+ struct notify_change_args args = {
523b37e3
AM
30945+ .errp = &err,
30946+ .path = path,
30947+ .ia = ia,
30948+ .delegated_inode = delegated_inode
1facf9fc 30949+ };
30950+
30951+ call_notify_change(&args);
30952+
30953+ return err;
30954+}
30955+
523b37e3
AM
30956+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
30957+ struct inode **delegated_inode)
1facf9fc 30958+{
30959+ int err, wkq_err;
30960+ struct notify_change_args args = {
523b37e3
AM
30961+ .errp = &err,
30962+ .path = path,
30963+ .ia = ia,
30964+ .delegated_inode = delegated_inode
1facf9fc 30965+ };
30966+
30967+ wkq_err = au_wkq_wait(call_notify_change, &args);
30968+ if (unlikely(wkq_err))
30969+ err = wkq_err;
30970+
30971+ return err;
30972+}
30973+
30974+/* ---------------------------------------------------------------------- */
30975+
30976+struct unlink_args {
30977+ int *errp;
30978+ struct inode *dir;
30979+ struct path *path;
523b37e3 30980+ struct inode **delegated_inode;
1facf9fc 30981+};
30982+
30983+static void call_unlink(void *args)
30984+{
30985+ struct unlink_args *a = args;
30986+ struct dentry *d = a->path->dentry;
30987+ struct inode *h_inode;
30988+ const int stop_sillyrename = (au_test_nfs(d->d_sb)
c1595e42 30989+ && au_dcount(d) == 1);
1facf9fc 30990+
30991+ IMustLock(a->dir);
30992+
30993+ a->path->dentry = d->d_parent;
30994+ *a->errp = security_path_unlink(a->path, d);
30995+ a->path->dentry = d;
30996+ if (unlikely(*a->errp))
30997+ return;
30998+
30999+ if (!stop_sillyrename)
31000+ dget(d);
5527c038
JR
31001+ h_inode = NULL;
31002+ if (d_is_positive(d)) {
31003+ h_inode = d_inode(d);
027c5e7a 31004+ ihold(h_inode);
5527c038 31005+ }
1facf9fc 31006+
2cbb1c4b 31007+ lockdep_off();
523b37e3 31008+ *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
2cbb1c4b 31009+ lockdep_on();
1facf9fc 31010+ if (!*a->errp) {
31011+ struct path tmp = {
31012+ .dentry = d->d_parent,
31013+ .mnt = a->path->mnt
31014+ };
31015+ vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
31016+ }
31017+
31018+ if (!stop_sillyrename)
31019+ dput(d);
31020+ if (h_inode)
31021+ iput(h_inode);
31022+
31023+ AuTraceErr(*a->errp);
31024+}
31025+
31026+/*
31027+ * @dir: must be locked.
31028+ * @dentry: target dentry.
31029+ */
523b37e3
AM
31030+int vfsub_unlink(struct inode *dir, struct path *path,
31031+ struct inode **delegated_inode, int force)
1facf9fc 31032+{
31033+ int err;
31034+ struct unlink_args args = {
523b37e3
AM
31035+ .errp = &err,
31036+ .dir = dir,
31037+ .path = path,
31038+ .delegated_inode = delegated_inode
1facf9fc 31039+ };
31040+
31041+ if (!force)
31042+ call_unlink(&args);
31043+ else {
31044+ int wkq_err;
31045+
31046+ wkq_err = au_wkq_wait(call_unlink, &args);
31047+ if (unlikely(wkq_err))
31048+ err = wkq_err;
31049+ }
31050+
31051+ return err;
31052+}
7f207e10
AM
31053diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
31054--- /usr/share/empty/fs/aufs/vfsub.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 31055+++ linux/fs/aufs/vfsub.h 2016-07-25 19:05:34.817826663 +0200
febd17d6 31056@@ -0,0 +1,310 @@
1facf9fc 31057+/*
8cdd5066 31058+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 31059+ *
31060+ * This program, aufs is free software; you can redistribute it and/or modify
31061+ * it under the terms of the GNU General Public License as published by
31062+ * the Free Software Foundation; either version 2 of the License, or
31063+ * (at your option) any later version.
dece6358
AM
31064+ *
31065+ * This program is distributed in the hope that it will be useful,
31066+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31067+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31068+ * GNU General Public License for more details.
31069+ *
31070+ * You should have received a copy of the GNU General Public License
523b37e3 31071+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31072+ */
31073+
31074+/*
31075+ * sub-routines for VFS
31076+ */
31077+
31078+#ifndef __AUFS_VFSUB_H__
31079+#define __AUFS_VFSUB_H__
31080+
31081+#ifdef __KERNEL__
31082+
31083+#include <linux/fs.h>
b4510431 31084+#include <linux/mount.h>
8cdd5066 31085+#include <linux/posix_acl.h>
c1595e42 31086+#include <linux/xattr.h>
7f207e10 31087+#include "debug.h"
1facf9fc 31088+
7f207e10 31089+/* copied from linux/fs/internal.h */
2cbb1c4b 31090+/* todo: BAD approach!! */
c06a8ce3 31091+extern void __mnt_drop_write(struct vfsmount *);
b912730e 31092+extern int open_check_o_direct(struct file *f);
7f207e10
AM
31093+
31094+/* ---------------------------------------------------------------------- */
1facf9fc 31095+
31096+/* lock subclass for lower inode */
31097+/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
31098+/* reduce? gave up. */
31099+enum {
c1595e42 31100+ AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
1facf9fc 31101+ AuLsc_I_PARENT, /* lower inode, parent first */
31102+ AuLsc_I_PARENT2, /* copyup dirs */
dece6358 31103+ AuLsc_I_PARENT3, /* copyup wh */
1facf9fc 31104+ AuLsc_I_CHILD,
31105+ AuLsc_I_CHILD2,
31106+ AuLsc_I_End
31107+};
31108+
31109+/* to debug easier, do not make them inlined functions */
31110+#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
febd17d6 31111+#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
1facf9fc 31112+
31113+/* ---------------------------------------------------------------------- */
31114+
7f207e10
AM
31115+static inline void vfsub_drop_nlink(struct inode *inode)
31116+{
31117+ AuDebugOn(!inode->i_nlink);
31118+ drop_nlink(inode);
31119+}
31120+
027c5e7a
AM
31121+static inline void vfsub_dead_dir(struct inode *inode)
31122+{
31123+ AuDebugOn(!S_ISDIR(inode->i_mode));
31124+ inode->i_flags |= S_DEAD;
31125+ clear_nlink(inode);
31126+}
31127+
392086de
AM
31128+static inline int vfsub_native_ro(struct inode *inode)
31129+{
31130+ return (inode->i_sb->s_flags & MS_RDONLY)
31131+ || IS_RDONLY(inode)
31132+ /* || IS_APPEND(inode) */
31133+ || IS_IMMUTABLE(inode);
31134+}
31135+
8cdd5066
JR
31136+#ifdef CONFIG_AUFS_BR_FUSE
31137+int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
31138+#else
31139+AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
31140+#endif
31141+
7f207e10
AM
31142+/* ---------------------------------------------------------------------- */
31143+
31144+int vfsub_update_h_iattr(struct path *h_path, int *did);
31145+struct file *vfsub_dentry_open(struct path *path, int flags);
31146+struct file *vfsub_filp_open(const char *path, int oflags, int mode);
b912730e
AM
31147+struct vfsub_aopen_args {
31148+ struct file *file;
31149+ unsigned int open_flag;
31150+ umode_t create_mode;
31151+ int *opened;
31152+};
31153+struct au_branch;
31154+int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
31155+ struct vfsub_aopen_args *args, struct au_branch *br);
1facf9fc 31156+int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
b4510431 31157+
febd17d6
JR
31158+struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
31159+ struct dentry *parent, int len);
1facf9fc 31160+struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
31161+ int len);
b4510431
AM
31162+
31163+struct vfsub_lkup_one_args {
31164+ struct dentry **errp;
31165+ struct qstr *name;
31166+ struct dentry *parent;
31167+};
31168+
31169+static inline struct dentry *vfsub_lkup_one(struct qstr *name,
31170+ struct dentry *parent)
31171+{
31172+ return vfsub_lookup_one_len(name->name, parent, name->len);
31173+}
31174+
31175+void vfsub_call_lkup_one(void *args);
31176+
31177+/* ---------------------------------------------------------------------- */
31178+
31179+static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
31180+{
31181+ int err;
076b876e 31182+
b4510431
AM
31183+ lockdep_off();
31184+ err = mnt_want_write(mnt);
31185+ lockdep_on();
31186+ return err;
31187+}
31188+
31189+static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
31190+{
31191+ lockdep_off();
31192+ mnt_drop_write(mnt);
31193+ lockdep_on();
31194+}
1facf9fc 31195+
7e9cd9fe 31196+#if 0 /* reserved */
c06a8ce3
AM
31197+static inline void vfsub_mnt_drop_write_file(struct file *file)
31198+{
31199+ lockdep_off();
31200+ mnt_drop_write_file(file);
31201+ lockdep_on();
31202+}
7e9cd9fe 31203+#endif
c06a8ce3 31204+
1facf9fc 31205+/* ---------------------------------------------------------------------- */
31206+
31207+struct au_hinode;
31208+struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
31209+ struct dentry *d2, struct au_hinode *hdir2);
31210+void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
31211+ struct dentry *d2, struct au_hinode *hdir2);
31212+
537831f9
AM
31213+int vfsub_create(struct inode *dir, struct path *path, int mode,
31214+ bool want_excl);
1facf9fc 31215+int vfsub_symlink(struct inode *dir, struct path *path,
31216+ const char *symname);
31217+int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
31218+int vfsub_link(struct dentry *src_dentry, struct inode *dir,
523b37e3 31219+ struct path *path, struct inode **delegated_inode);
1facf9fc 31220+int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
523b37e3
AM
31221+ struct inode *hdir, struct path *path,
31222+ struct inode **delegated_inode);
1facf9fc 31223+int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
31224+int vfsub_rmdir(struct inode *dir, struct path *path);
31225+
31226+/* ---------------------------------------------------------------------- */
31227+
31228+ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
31229+ loff_t *ppos);
31230+ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
31231+ loff_t *ppos);
31232+ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
31233+ loff_t *ppos);
31234+ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
31235+ loff_t *ppos);
4a4d8108 31236+int vfsub_flush(struct file *file, fl_owner_t id);
392086de
AM
31237+int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
31238+
c06a8ce3
AM
31239+static inline loff_t vfsub_f_size_read(struct file *file)
31240+{
31241+ return i_size_read(file_inode(file));
31242+}
31243+
4a4d8108
AM
31244+static inline unsigned int vfsub_file_flags(struct file *file)
31245+{
31246+ unsigned int flags;
31247+
31248+ spin_lock(&file->f_lock);
31249+ flags = file->f_flags;
31250+ spin_unlock(&file->f_lock);
31251+
31252+ return flags;
31253+}
1308ab2a 31254+
7e9cd9fe 31255+#if 0 /* reserved */
1facf9fc 31256+static inline void vfsub_file_accessed(struct file *h_file)
31257+{
31258+ file_accessed(h_file);
31259+ vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
31260+}
7e9cd9fe 31261+#endif
1facf9fc 31262+
79b8bda9 31263+#if 0 /* reserved */
1facf9fc 31264+static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
31265+ struct dentry *h_dentry)
31266+{
31267+ struct path h_path = {
31268+ .dentry = h_dentry,
31269+ .mnt = h_mnt
31270+ };
92d182d2 31271+ touch_atime(&h_path);
1facf9fc 31272+ vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
31273+}
79b8bda9 31274+#endif
1facf9fc 31275+
0c3ec466
AM
31276+static inline int vfsub_update_time(struct inode *h_inode, struct timespec *ts,
31277+ int flags)
31278+{
5afbbe0d 31279+ return update_time(h_inode, ts, flags);
0c3ec466
AM
31280+ /* no vfsub_update_h_iattr() since we don't have struct path */
31281+}
31282+
8cdd5066
JR
31283+#ifdef CONFIG_FS_POSIX_ACL
31284+static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
31285+{
31286+ int err;
31287+
31288+ err = posix_acl_chmod(h_inode, h_mode);
31289+ if (err == -EOPNOTSUPP)
31290+ err = 0;
31291+ return err;
31292+}
31293+#else
31294+AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
31295+#endif
31296+
4a4d8108
AM
31297+long vfsub_splice_to(struct file *in, loff_t *ppos,
31298+ struct pipe_inode_info *pipe, size_t len,
31299+ unsigned int flags);
31300+long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
31301+ loff_t *ppos, size_t len, unsigned int flags);
c06a8ce3
AM
31302+
31303+static inline long vfsub_truncate(struct path *path, loff_t length)
31304+{
31305+ long err;
076b876e 31306+
c06a8ce3
AM
31307+ lockdep_off();
31308+ err = vfs_truncate(path, length);
31309+ lockdep_on();
31310+ return err;
31311+}
31312+
4a4d8108
AM
31313+int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
31314+ struct file *h_file);
53392da6 31315+int vfsub_fsync(struct file *file, struct path *path, int datasync);
4a4d8108 31316+
1facf9fc 31317+/* ---------------------------------------------------------------------- */
31318+
31319+static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
31320+{
31321+ loff_t err;
31322+
2cbb1c4b 31323+ lockdep_off();
1facf9fc 31324+ err = vfs_llseek(file, offset, origin);
2cbb1c4b 31325+ lockdep_on();
1facf9fc 31326+ return err;
31327+}
31328+
31329+/* ---------------------------------------------------------------------- */
31330+
4a4d8108
AM
31331+int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
31332+int vfsub_sio_rmdir(struct inode *dir, struct path *path);
523b37e3
AM
31333+int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
31334+ struct inode **delegated_inode);
31335+int vfsub_notify_change(struct path *path, struct iattr *ia,
31336+ struct inode **delegated_inode);
31337+int vfsub_unlink(struct inode *dir, struct path *path,
31338+ struct inode **delegated_inode, int force);
4a4d8108 31339+
c1595e42
JR
31340+/* ---------------------------------------------------------------------- */
31341+
31342+static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
31343+ const void *value, size_t size, int flags)
31344+{
31345+ int err;
31346+
31347+ lockdep_off();
31348+ err = vfs_setxattr(dentry, name, value, size, flags);
31349+ lockdep_on();
31350+
31351+ return err;
31352+}
31353+
31354+static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
31355+{
31356+ int err;
31357+
31358+ lockdep_off();
31359+ err = vfs_removexattr(dentry, name);
31360+ lockdep_on();
31361+
31362+ return err;
31363+}
31364+
1facf9fc 31365+#endif /* __KERNEL__ */
31366+#endif /* __AUFS_VFSUB_H__ */
7f207e10
AM
31367diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
31368--- /usr/share/empty/fs/aufs/wbr_policy.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 31369+++ linux/fs/aufs/wbr_policy.c 2016-07-25 19:05:34.817826663 +0200
076b876e 31370@@ -0,0 +1,765 @@
1facf9fc 31371+/*
8cdd5066 31372+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 31373+ *
31374+ * This program, aufs is free software; you can redistribute it and/or modify
31375+ * it under the terms of the GNU General Public License as published by
31376+ * the Free Software Foundation; either version 2 of the License, or
31377+ * (at your option) any later version.
dece6358
AM
31378+ *
31379+ * This program is distributed in the hope that it will be useful,
31380+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
31381+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31382+ * GNU General Public License for more details.
31383+ *
31384+ * You should have received a copy of the GNU General Public License
523b37e3 31385+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 31386+ */
31387+
31388+/*
31389+ * policies for selecting one among multiple writable branches
31390+ */
31391+
31392+#include <linux/statfs.h>
31393+#include "aufs.h"
31394+
31395+/* subset of cpup_attr() */
31396+static noinline_for_stack
31397+int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
31398+{
31399+ int err, sbits;
31400+ struct iattr ia;
31401+ struct inode *h_isrc;
31402+
5527c038 31403+ h_isrc = d_inode(h_src);
1facf9fc 31404+ ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
31405+ ia.ia_mode = h_isrc->i_mode;
31406+ ia.ia_uid = h_isrc->i_uid;
31407+ ia.ia_gid = h_isrc->i_gid;
31408+ sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
5527c038 31409+ au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
523b37e3
AM
31410+ /* no delegation since it is just created */
31411+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 31412+
31413+ /* is this nfs only? */
31414+ if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
31415+ ia.ia_valid = ATTR_FORCE | ATTR_MODE;
31416+ ia.ia_mode = h_isrc->i_mode;
523b37e3 31417+ err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
1facf9fc 31418+ }
31419+
31420+ return err;
31421+}
31422+
31423+#define AuCpdown_PARENT_OPQ 1
31424+#define AuCpdown_WHED (1 << 1)
31425+#define AuCpdown_MADE_DIR (1 << 2)
31426+#define AuCpdown_DIROPQ (1 << 3)
31427+#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
7f207e10
AM
31428+#define au_fset_cpdown(flags, name) \
31429+ do { (flags) |= AuCpdown_##name; } while (0)
31430+#define au_fclr_cpdown(flags, name) \
31431+ do { (flags) &= ~AuCpdown_##name; } while (0)
1facf9fc 31432+
1facf9fc 31433+static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
c2b27bf2 31434+ unsigned int *flags)
1facf9fc 31435+{
31436+ int err;
31437+ struct dentry *opq_dentry;
31438+
31439+ opq_dentry = au_diropq_create(dentry, bdst);
31440+ err = PTR_ERR(opq_dentry);
31441+ if (IS_ERR(opq_dentry))
31442+ goto out;
31443+ dput(opq_dentry);
c2b27bf2 31444+ au_fset_cpdown(*flags, DIROPQ);
1facf9fc 31445+
4f0767ce 31446+out:
1facf9fc 31447+ return err;
31448+}
31449+
31450+static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
31451+ struct inode *dir, aufs_bindex_t bdst)
31452+{
31453+ int err;
31454+ struct path h_path;
31455+ struct au_branch *br;
31456+
31457+ br = au_sbr(dentry->d_sb, bdst);
31458+ h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
31459+ err = PTR_ERR(h_path.dentry);
31460+ if (IS_ERR(h_path.dentry))
31461+ goto out;
31462+
31463+ err = 0;
5527c038 31464+ if (d_is_positive(h_path.dentry)) {
86dc4139 31465+ h_path.mnt = au_br_mnt(br);
1facf9fc 31466+ err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
31467+ dentry);
31468+ }
31469+ dput(h_path.dentry);
31470+
4f0767ce 31471+out:
1facf9fc 31472+ return err;
31473+}
31474+
31475+static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
86dc4139 31476+ struct au_pin *pin,
1facf9fc 31477+ struct dentry *h_parent, void *arg)
31478+{
31479+ int err, rerr;
5afbbe0d 31480+ aufs_bindex_t bopq, btop;
1facf9fc 31481+ struct path h_path;
31482+ struct dentry *parent;
31483+ struct inode *h_dir, *h_inode, *inode, *dir;
c2b27bf2 31484+ unsigned int *flags = arg;
1facf9fc 31485+
5afbbe0d 31486+ btop = au_dbtop(dentry);
1facf9fc 31487+ /* dentry is di-locked */
31488+ parent = dget_parent(dentry);
5527c038
JR
31489+ dir = d_inode(parent);
31490+ h_dir = d_inode(h_parent);
1facf9fc 31491+ AuDebugOn(h_dir != au_h_iptr(dir, bdst));
31492+ IMustLock(h_dir);
31493+
86dc4139 31494+ err = au_lkup_neg(dentry, bdst, /*wh*/0);
1facf9fc 31495+ if (unlikely(err < 0))
31496+ goto out;
31497+ h_path.dentry = au_h_dptr(dentry, bdst);
31498+ h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
31499+ err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path,
31500+ S_IRWXU | S_IRUGO | S_IXUGO);
31501+ if (unlikely(err))
31502+ goto out_put;
c2b27bf2 31503+ au_fset_cpdown(*flags, MADE_DIR);
1facf9fc 31504+
1facf9fc 31505+ bopq = au_dbdiropq(dentry);
c2b27bf2
AM
31506+ au_fclr_cpdown(*flags, WHED);
31507+ au_fclr_cpdown(*flags, DIROPQ);
1facf9fc 31508+ if (au_dbwh(dentry) == bdst)
c2b27bf2
AM
31509+ au_fset_cpdown(*flags, WHED);
31510+ if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
31511+ au_fset_cpdown(*flags, PARENT_OPQ);
5527c038 31512+ h_inode = d_inode(h_path.dentry);
febd17d6 31513+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
c2b27bf2
AM
31514+ if (au_ftest_cpdown(*flags, WHED)) {
31515+ err = au_cpdown_dir_opq(dentry, bdst, flags);
1facf9fc 31516+ if (unlikely(err)) {
febd17d6 31517+ inode_unlock(h_inode);
1facf9fc 31518+ goto out_dir;
31519+ }
31520+ }
31521+
5afbbe0d 31522+ err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
febd17d6 31523+ inode_unlock(h_inode);
1facf9fc 31524+ if (unlikely(err))
31525+ goto out_opq;
31526+
c2b27bf2 31527+ if (au_ftest_cpdown(*flags, WHED)) {
1facf9fc 31528+ err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
31529+ if (unlikely(err))
31530+ goto out_opq;
31531+ }
31532+
5527c038 31533+ inode = d_inode(dentry);
5afbbe0d
AM
31534+ if (au_ibbot(inode) < bdst)
31535+ au_set_ibbot(inode, bdst);
1facf9fc 31536+ au_set_h_iptr(inode, bdst, au_igrab(h_inode),
31537+ au_hi_flags(inode, /*isdir*/1));
076b876e 31538+ au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
1facf9fc 31539+ goto out; /* success */
31540+
31541+ /* revert */
4f0767ce 31542+out_opq:
c2b27bf2 31543+ if (au_ftest_cpdown(*flags, DIROPQ)) {
febd17d6 31544+ inode_lock_nested(h_inode, AuLsc_I_CHILD);
1facf9fc 31545+ rerr = au_diropq_remove(dentry, bdst);
febd17d6 31546+ inode_unlock(h_inode);
1facf9fc 31547+ if (unlikely(rerr)) {
523b37e3
AM
31548+ AuIOErr("failed removing diropq for %pd b%d (%d)\n",
31549+ dentry, bdst, rerr);
1facf9fc 31550+ err = -EIO;
31551+ goto out;
31552+ }
31553+ }
4f0767ce 31554+out_dir:
c2b27bf2 31555+ if (au_ftest_cpdown(*flags, MADE_DIR)) {
1facf9fc 31556+ rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
31557+ if (unlikely(rerr)) {
523b37e3
AM
31558+ AuIOErr("failed removing %pd b%d (%d)\n",
31559+ dentry, bdst, rerr);
1facf9fc 31560+ err = -EIO;
31561+ }
31562+ }
4f0767ce 31563+out_put:
1facf9fc 31564+ au_set_h_dptr(dentry, bdst, NULL);
5afbbe0d
AM
31565+ if (au_dbbot(dentry) == bdst)
31566+ au_update_dbbot(dentry);
4f0767ce 31567+out:
1facf9fc 31568+ dput(parent);
31569+ return err;
31570+}
31571+
31572+int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
31573+{
31574+ int err;
c2b27bf2 31575+ unsigned int flags;
1facf9fc 31576+
c2b27bf2
AM
31577+ flags = 0;
31578+ err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
1facf9fc 31579+
31580+ return err;
31581+}
31582+
31583+/* ---------------------------------------------------------------------- */
31584+
31585+/* policies for create */
31586+
c2b27bf2 31587+int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
4a4d8108
AM
31588+{
31589+ int err, i, j, ndentry;
31590+ aufs_bindex_t bopq;
31591+ struct au_dcsub_pages dpages;
31592+ struct au_dpage *dpage;
31593+ struct dentry **dentries, *parent, *d;
31594+
31595+ err = au_dpages_init(&dpages, GFP_NOFS);
31596+ if (unlikely(err))
31597+ goto out;
31598+ parent = dget_parent(dentry);
027c5e7a 31599+ err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
4a4d8108
AM
31600+ if (unlikely(err))
31601+ goto out_free;
31602+
31603+ err = bindex;
31604+ for (i = 0; i < dpages.ndpage; i++) {
31605+ dpage = dpages.dpages + i;
31606+ dentries = dpage->dentries;
31607+ ndentry = dpage->ndentry;
31608+ for (j = 0; j < ndentry; j++) {
31609+ d = dentries[j];
31610+ di_read_lock_parent2(d, !AuLock_IR);
31611+ bopq = au_dbdiropq(d);
31612+ di_read_unlock(d, !AuLock_IR);
31613+ if (bopq >= 0 && bopq < err)
31614+ err = bopq;
31615+ }
31616+ }
31617+
31618+out_free:
31619+ dput(parent);
31620+ au_dpages_free(&dpages);
31621+out:
31622+ return err;
31623+}
31624+
1facf9fc 31625+static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
31626+{
31627+ for (; bindex >= 0; bindex--)
31628+ if (!au_br_rdonly(au_sbr(sb, bindex)))
31629+ return bindex;
31630+ return -EROFS;
31631+}
31632+
31633+/* top down parent */
392086de
AM
31634+static int au_wbr_create_tdp(struct dentry *dentry,
31635+ unsigned int flags __maybe_unused)
1facf9fc 31636+{
31637+ int err;
5afbbe0d 31638+ aufs_bindex_t btop, bindex;
1facf9fc 31639+ struct super_block *sb;
31640+ struct dentry *parent, *h_parent;
31641+
31642+ sb = dentry->d_sb;
5afbbe0d
AM
31643+ btop = au_dbtop(dentry);
31644+ err = btop;
31645+ if (!au_br_rdonly(au_sbr(sb, btop)))
1facf9fc 31646+ goto out;
31647+
31648+ err = -EROFS;
31649+ parent = dget_parent(dentry);
5afbbe0d 31650+ for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
1facf9fc 31651+ h_parent = au_h_dptr(parent, bindex);
5527c038 31652+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 31653+ continue;
31654+
31655+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
31656+ err = bindex;
31657+ break;
31658+ }
31659+ }
31660+ dput(parent);
31661+
31662+ /* bottom up here */
4a4d8108 31663+ if (unlikely(err < 0)) {
5afbbe0d 31664+ err = au_wbr_bu(sb, btop - 1);
4a4d8108
AM
31665+ if (err >= 0)
31666+ err = au_wbr_nonopq(dentry, err);
31667+ }
1facf9fc 31668+
4f0767ce 31669+out:
1facf9fc 31670+ AuDbg("b%d\n", err);
31671+ return err;
31672+}
31673+
31674+/* ---------------------------------------------------------------------- */
31675+
31676+/* an exception for the policy other than tdp */
31677+static int au_wbr_create_exp(struct dentry *dentry)
31678+{
31679+ int err;
31680+ aufs_bindex_t bwh, bdiropq;
31681+ struct dentry *parent;
31682+
31683+ err = -1;
31684+ bwh = au_dbwh(dentry);
31685+ parent = dget_parent(dentry);
31686+ bdiropq = au_dbdiropq(parent);
31687+ if (bwh >= 0) {
31688+ if (bdiropq >= 0)
31689+ err = min(bdiropq, bwh);
31690+ else
31691+ err = bwh;
31692+ AuDbg("%d\n", err);
31693+ } else if (bdiropq >= 0) {
31694+ err = bdiropq;
31695+ AuDbg("%d\n", err);
31696+ }
31697+ dput(parent);
31698+
4a4d8108
AM
31699+ if (err >= 0)
31700+ err = au_wbr_nonopq(dentry, err);
31701+
1facf9fc 31702+ if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
31703+ err = -1;
31704+
31705+ AuDbg("%d\n", err);
31706+ return err;
31707+}
31708+
31709+/* ---------------------------------------------------------------------- */
31710+
31711+/* round robin */
31712+static int au_wbr_create_init_rr(struct super_block *sb)
31713+{
31714+ int err;
31715+
5afbbe0d 31716+ err = au_wbr_bu(sb, au_sbbot(sb));
1facf9fc 31717+ atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
dece6358 31718+ /* smp_mb(); */
1facf9fc 31719+
31720+ AuDbg("b%d\n", err);
31721+ return err;
31722+}
31723+
392086de 31724+static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
1facf9fc 31725+{
31726+ int err, nbr;
31727+ unsigned int u;
5afbbe0d 31728+ aufs_bindex_t bindex, bbot;
1facf9fc 31729+ struct super_block *sb;
31730+ atomic_t *next;
31731+
31732+ err = au_wbr_create_exp(dentry);
31733+ if (err >= 0)
31734+ goto out;
31735+
31736+ sb = dentry->d_sb;
31737+ next = &au_sbi(sb)->si_wbr_rr_next;
5afbbe0d
AM
31738+ bbot = au_sbbot(sb);
31739+ nbr = bbot + 1;
31740+ for (bindex = 0; bindex <= bbot; bindex++) {
392086de 31741+ if (!au_ftest_wbr(flags, DIR)) {
1facf9fc 31742+ err = atomic_dec_return(next) + 1;
31743+ /* modulo for 0 is meaningless */
31744+ if (unlikely(!err))
31745+ err = atomic_dec_return(next) + 1;
31746+ } else
31747+ err = atomic_read(next);
31748+ AuDbg("%d\n", err);
31749+ u = err;
31750+ err = u % nbr;
31751+ AuDbg("%d\n", err);
31752+ if (!au_br_rdonly(au_sbr(sb, err)))
31753+ break;
31754+ err = -EROFS;
31755+ }
31756+
4a4d8108
AM
31757+ if (err >= 0)
31758+ err = au_wbr_nonopq(dentry, err);
31759+
4f0767ce 31760+out:
1facf9fc 31761+ AuDbg("%d\n", err);
31762+ return err;
31763+}
31764+
31765+/* ---------------------------------------------------------------------- */
31766+
31767+/* most free space */
392086de 31768+static void au_mfs(struct dentry *dentry, struct dentry *parent)
1facf9fc 31769+{
31770+ struct super_block *sb;
31771+ struct au_branch *br;
31772+ struct au_wbr_mfs *mfs;
392086de 31773+ struct dentry *h_parent;
5afbbe0d 31774+ aufs_bindex_t bindex, bbot;
1facf9fc 31775+ int err;
31776+ unsigned long long b, bavail;
7f207e10 31777+ struct path h_path;
1facf9fc 31778+ /* reduce the stack usage */
31779+ struct kstatfs *st;
31780+
31781+ st = kmalloc(sizeof(*st), GFP_NOFS);
31782+ if (unlikely(!st)) {
31783+ AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
31784+ return;
31785+ }
31786+
31787+ bavail = 0;
31788+ sb = dentry->d_sb;
31789+ mfs = &au_sbi(sb)->si_wbr_mfs;
dece6358 31790+ MtxMustLock(&mfs->mfs_lock);
1facf9fc 31791+ mfs->mfs_bindex = -EROFS;
31792+ mfs->mfsrr_bytes = 0;
392086de
AM
31793+ if (!parent) {
31794+ bindex = 0;
5afbbe0d 31795+ bbot = au_sbbot(sb);
392086de 31796+ } else {
5afbbe0d
AM
31797+ bindex = au_dbtop(parent);
31798+ bbot = au_dbtaildir(parent);
392086de
AM
31799+ }
31800+
5afbbe0d 31801+ for (; bindex <= bbot; bindex++) {
392086de
AM
31802+ if (parent) {
31803+ h_parent = au_h_dptr(parent, bindex);
5527c038 31804+ if (!h_parent || d_is_negative(h_parent))
392086de
AM
31805+ continue;
31806+ }
1facf9fc 31807+ br = au_sbr(sb, bindex);
31808+ if (au_br_rdonly(br))
31809+ continue;
31810+
31811+ /* sb->s_root for NFS is unreliable */
86dc4139 31812+ h_path.mnt = au_br_mnt(br);
7f207e10
AM
31813+ h_path.dentry = h_path.mnt->mnt_root;
31814+ err = vfs_statfs(&h_path, st);
1facf9fc 31815+ if (unlikely(err)) {
31816+ AuWarn1("failed statfs, b%d, %d\n", bindex, err);
31817+ continue;
31818+ }
31819+
31820+ /* when the available size is equal, select the lower one */
31821+ BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
31822+ || sizeof(b) < sizeof(st->f_bsize));
31823+ b = st->f_bavail * st->f_bsize;
31824+ br->br_wbr->wbr_bytes = b;
31825+ if (b >= bavail) {
31826+ bavail = b;
31827+ mfs->mfs_bindex = bindex;
31828+ mfs->mfs_jiffy = jiffies;
31829+ }
31830+ }
31831+
31832+ mfs->mfsrr_bytes = bavail;
31833+ AuDbg("b%d\n", mfs->mfs_bindex);
31834+ kfree(st);
31835+}
31836+
392086de 31837+static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
1facf9fc 31838+{
31839+ int err;
392086de 31840+ struct dentry *parent;
1facf9fc 31841+ struct super_block *sb;
31842+ struct au_wbr_mfs *mfs;
31843+
31844+ err = au_wbr_create_exp(dentry);
31845+ if (err >= 0)
31846+ goto out;
31847+
31848+ sb = dentry->d_sb;
392086de
AM
31849+ parent = NULL;
31850+ if (au_ftest_wbr(flags, PARENT))
31851+ parent = dget_parent(dentry);
1facf9fc 31852+ mfs = &au_sbi(sb)->si_wbr_mfs;
31853+ mutex_lock(&mfs->mfs_lock);
31854+ if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
31855+ || mfs->mfs_bindex < 0
31856+ || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
392086de 31857+ au_mfs(dentry, parent);
1facf9fc 31858+ mutex_unlock(&mfs->mfs_lock);
31859+ err = mfs->mfs_bindex;
392086de 31860+ dput(parent);
1facf9fc 31861+
4a4d8108
AM
31862+ if (err >= 0)
31863+ err = au_wbr_nonopq(dentry, err);
31864+
4f0767ce 31865+out:
1facf9fc 31866+ AuDbg("b%d\n", err);
31867+ return err;
31868+}
31869+
31870+static int au_wbr_create_init_mfs(struct super_block *sb)
31871+{
31872+ struct au_wbr_mfs *mfs;
31873+
31874+ mfs = &au_sbi(sb)->si_wbr_mfs;
31875+ mutex_init(&mfs->mfs_lock);
31876+ mfs->mfs_jiffy = 0;
31877+ mfs->mfs_bindex = -EROFS;
31878+
31879+ return 0;
31880+}
31881+
31882+static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
31883+{
31884+ mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
31885+ return 0;
31886+}
31887+
31888+/* ---------------------------------------------------------------------- */
31889+
31890+/* most free space and then round robin */
392086de 31891+static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
1facf9fc 31892+{
31893+ int err;
31894+ struct au_wbr_mfs *mfs;
31895+
392086de 31896+ err = au_wbr_create_mfs(dentry, flags);
1facf9fc 31897+ if (err >= 0) {
31898+ mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
dece6358 31899+ mutex_lock(&mfs->mfs_lock);
1facf9fc 31900+ if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
392086de 31901+ err = au_wbr_create_rr(dentry, flags);
dece6358 31902+ mutex_unlock(&mfs->mfs_lock);
1facf9fc 31903+ }
31904+
31905+ AuDbg("b%d\n", err);
31906+ return err;
31907+}
31908+
31909+static int au_wbr_create_init_mfsrr(struct super_block *sb)
31910+{
31911+ int err;
31912+
31913+ au_wbr_create_init_mfs(sb); /* ignore */
31914+ err = au_wbr_create_init_rr(sb);
31915+
31916+ return err;
31917+}
31918+
31919+/* ---------------------------------------------------------------------- */
31920+
31921+/* top down parent and most free space */
392086de 31922+static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
1facf9fc 31923+{
31924+ int err, e2;
31925+ unsigned long long b;
5afbbe0d 31926+ aufs_bindex_t bindex, btop, bbot;
1facf9fc 31927+ struct super_block *sb;
31928+ struct dentry *parent, *h_parent;
31929+ struct au_branch *br;
31930+
392086de 31931+ err = au_wbr_create_tdp(dentry, flags);
1facf9fc 31932+ if (unlikely(err < 0))
31933+ goto out;
31934+ parent = dget_parent(dentry);
5afbbe0d
AM
31935+ btop = au_dbtop(parent);
31936+ bbot = au_dbtaildir(parent);
31937+ if (btop == bbot)
1facf9fc 31938+ goto out_parent; /* success */
31939+
392086de 31940+ e2 = au_wbr_create_mfs(dentry, flags);
1facf9fc 31941+ if (e2 < 0)
31942+ goto out_parent; /* success */
31943+
31944+ /* when the available size is equal, select upper one */
31945+ sb = dentry->d_sb;
31946+ br = au_sbr(sb, err);
31947+ b = br->br_wbr->wbr_bytes;
31948+ AuDbg("b%d, %llu\n", err, b);
31949+
5afbbe0d 31950+ for (bindex = btop; bindex <= bbot; bindex++) {
1facf9fc 31951+ h_parent = au_h_dptr(parent, bindex);
5527c038 31952+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 31953+ continue;
31954+
31955+ br = au_sbr(sb, bindex);
31956+ if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
31957+ b = br->br_wbr->wbr_bytes;
31958+ err = bindex;
31959+ AuDbg("b%d, %llu\n", err, b);
31960+ }
31961+ }
31962+
4a4d8108
AM
31963+ if (err >= 0)
31964+ err = au_wbr_nonopq(dentry, err);
31965+
4f0767ce 31966+out_parent:
1facf9fc 31967+ dput(parent);
4f0767ce 31968+out:
1facf9fc 31969+ AuDbg("b%d\n", err);
31970+ return err;
31971+}
31972+
31973+/* ---------------------------------------------------------------------- */
31974+
392086de
AM
31975+/*
31976+ * - top down parent
31977+ * - most free space with parent
31978+ * - most free space round-robin regardless parent
31979+ */
31980+static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
31981+{
31982+ int err;
31983+ unsigned long long watermark;
31984+ struct super_block *sb;
31985+ struct au_branch *br;
31986+ struct au_wbr_mfs *mfs;
31987+
31988+ err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
31989+ if (unlikely(err < 0))
31990+ goto out;
31991+
31992+ sb = dentry->d_sb;
31993+ br = au_sbr(sb, err);
31994+ mfs = &au_sbi(sb)->si_wbr_mfs;
31995+ mutex_lock(&mfs->mfs_lock);
31996+ watermark = mfs->mfsrr_watermark;
31997+ mutex_unlock(&mfs->mfs_lock);
31998+ if (br->br_wbr->wbr_bytes < watermark)
31999+ /* regardless the parent dir */
32000+ err = au_wbr_create_mfsrr(dentry, flags);
32001+
32002+out:
32003+ AuDbg("b%d\n", err);
32004+ return err;
32005+}
32006+
32007+/* ---------------------------------------------------------------------- */
32008+
1facf9fc 32009+/* policies for copyup */
32010+
32011+/* top down parent */
32012+static int au_wbr_copyup_tdp(struct dentry *dentry)
32013+{
392086de 32014+ return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
1facf9fc 32015+}
32016+
32017+/* bottom up parent */
32018+static int au_wbr_copyup_bup(struct dentry *dentry)
32019+{
32020+ int err;
5afbbe0d 32021+ aufs_bindex_t bindex, btop;
1facf9fc 32022+ struct dentry *parent, *h_parent;
32023+ struct super_block *sb;
32024+
32025+ err = -EROFS;
32026+ sb = dentry->d_sb;
32027+ parent = dget_parent(dentry);
5afbbe0d
AM
32028+ btop = au_dbtop(parent);
32029+ for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
1facf9fc 32030+ h_parent = au_h_dptr(parent, bindex);
5527c038 32031+ if (!h_parent || d_is_negative(h_parent))
1facf9fc 32032+ continue;
32033+
32034+ if (!au_br_rdonly(au_sbr(sb, bindex))) {
32035+ err = bindex;
32036+ break;
32037+ }
32038+ }
32039+ dput(parent);
32040+
32041+ /* bottom up here */
32042+ if (unlikely(err < 0))
5afbbe0d 32043+ err = au_wbr_bu(sb, btop - 1);
1facf9fc 32044+
32045+ AuDbg("b%d\n", err);
32046+ return err;
32047+}
32048+
32049+/* bottom up */
5afbbe0d 32050+int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
1facf9fc 32051+{
32052+ int err;
32053+
5afbbe0d 32054+ err = au_wbr_bu(dentry->d_sb, btop);
4a4d8108 32055+ AuDbg("b%d\n", err);
5afbbe0d 32056+ if (err > btop)
4a4d8108 32057+ err = au_wbr_nonopq(dentry, err);
1facf9fc 32058+
32059+ AuDbg("b%d\n", err);
32060+ return err;
32061+}
32062+
076b876e
AM
32063+static int au_wbr_copyup_bu(struct dentry *dentry)
32064+{
32065+ int err;
5afbbe0d 32066+ aufs_bindex_t btop;
076b876e 32067+
5afbbe0d
AM
32068+ btop = au_dbtop(dentry);
32069+ err = au_wbr_do_copyup_bu(dentry, btop);
076b876e
AM
32070+ return err;
32071+}
32072+
1facf9fc 32073+/* ---------------------------------------------------------------------- */
32074+
32075+struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
32076+ [AuWbrCopyup_TDP] = {
32077+ .copyup = au_wbr_copyup_tdp
32078+ },
32079+ [AuWbrCopyup_BUP] = {
32080+ .copyup = au_wbr_copyup_bup
32081+ },
32082+ [AuWbrCopyup_BU] = {
32083+ .copyup = au_wbr_copyup_bu
32084+ }
32085+};
32086+
32087+struct au_wbr_create_operations au_wbr_create_ops[] = {
32088+ [AuWbrCreate_TDP] = {
32089+ .create = au_wbr_create_tdp
32090+ },
32091+ [AuWbrCreate_RR] = {
32092+ .create = au_wbr_create_rr,
32093+ .init = au_wbr_create_init_rr
32094+ },
32095+ [AuWbrCreate_MFS] = {
32096+ .create = au_wbr_create_mfs,
32097+ .init = au_wbr_create_init_mfs,
32098+ .fin = au_wbr_create_fin_mfs
32099+ },
32100+ [AuWbrCreate_MFSV] = {
32101+ .create = au_wbr_create_mfs,
32102+ .init = au_wbr_create_init_mfs,
32103+ .fin = au_wbr_create_fin_mfs
32104+ },
32105+ [AuWbrCreate_MFSRR] = {
32106+ .create = au_wbr_create_mfsrr,
32107+ .init = au_wbr_create_init_mfsrr,
32108+ .fin = au_wbr_create_fin_mfs
32109+ },
32110+ [AuWbrCreate_MFSRRV] = {
32111+ .create = au_wbr_create_mfsrr,
32112+ .init = au_wbr_create_init_mfsrr,
32113+ .fin = au_wbr_create_fin_mfs
32114+ },
32115+ [AuWbrCreate_PMFS] = {
32116+ .create = au_wbr_create_pmfs,
32117+ .init = au_wbr_create_init_mfs,
32118+ .fin = au_wbr_create_fin_mfs
32119+ },
32120+ [AuWbrCreate_PMFSV] = {
32121+ .create = au_wbr_create_pmfs,
32122+ .init = au_wbr_create_init_mfs,
32123+ .fin = au_wbr_create_fin_mfs
392086de
AM
32124+ },
32125+ [AuWbrCreate_PMFSRR] = {
32126+ .create = au_wbr_create_pmfsrr,
32127+ .init = au_wbr_create_init_mfsrr,
32128+ .fin = au_wbr_create_fin_mfs
32129+ },
32130+ [AuWbrCreate_PMFSRRV] = {
32131+ .create = au_wbr_create_pmfsrr,
32132+ .init = au_wbr_create_init_mfsrr,
32133+ .fin = au_wbr_create_fin_mfs
1facf9fc 32134+ }
32135+};
7f207e10
AM
32136diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
32137--- /usr/share/empty/fs/aufs/whout.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 32138+++ linux/fs/aufs/whout.c 2016-07-25 19:05:34.817826663 +0200
be52b249 32139@@ -0,0 +1,1060 @@
1facf9fc 32140+/*
8cdd5066 32141+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 32142+ *
32143+ * This program, aufs is free software; you can redistribute it and/or modify
32144+ * it under the terms of the GNU General Public License as published by
32145+ * the Free Software Foundation; either version 2 of the License, or
32146+ * (at your option) any later version.
dece6358
AM
32147+ *
32148+ * This program is distributed in the hope that it will be useful,
32149+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
32150+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32151+ * GNU General Public License for more details.
32152+ *
32153+ * You should have received a copy of the GNU General Public License
523b37e3 32154+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 32155+ */
32156+
32157+/*
32158+ * whiteout for logical deletion and opaque directory
32159+ */
32160+
1facf9fc 32161+#include "aufs.h"
32162+
32163+#define WH_MASK S_IRUGO
32164+
32165+/*
32166+ * If a directory contains this file, then it is opaque. We start with the
32167+ * .wh. flag so that it is blocked by lookup.
32168+ */
0c3ec466
AM
32169+static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
32170+ sizeof(AUFS_WH_DIROPQ) - 1);
1facf9fc 32171+
32172+/*
32173+ * generate whiteout name, which is NOT terminated by NULL.
32174+ * @name: original d_name.name
32175+ * @len: original d_name.len
32176+ * @wh: whiteout qstr
32177+ * returns zero when succeeds, otherwise error.
32178+ * succeeded value as wh->name should be freed by kfree().
32179+ */
32180+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
32181+{
32182+ char *p;
32183+
32184+ if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
32185+ return -ENAMETOOLONG;
32186+
32187+ wh->len = name->len + AUFS_WH_PFX_LEN;
32188+ p = kmalloc(wh->len, GFP_NOFS);
32189+ wh->name = p;
32190+ if (p) {
32191+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32192+ memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
32193+ /* smp_mb(); */
32194+ return 0;
32195+ }
32196+ return -ENOMEM;
32197+}
32198+
32199+/* ---------------------------------------------------------------------- */
32200+
32201+/*
32202+ * test if the @wh_name exists under @h_parent.
32203+ * @try_sio specifies the necessary of super-io.
32204+ */
076b876e 32205+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
1facf9fc 32206+{
32207+ int err;
32208+ struct dentry *wh_dentry;
1facf9fc 32209+
1facf9fc 32210+ if (!try_sio)
b4510431 32211+ wh_dentry = vfsub_lkup_one(wh_name, h_parent);
1facf9fc 32212+ else
076b876e 32213+ wh_dentry = au_sio_lkup_one(wh_name, h_parent);
1facf9fc 32214+ err = PTR_ERR(wh_dentry);
2000de60
JR
32215+ if (IS_ERR(wh_dentry)) {
32216+ if (err == -ENAMETOOLONG)
32217+ err = 0;
1facf9fc 32218+ goto out;
2000de60 32219+ }
1facf9fc 32220+
32221+ err = 0;
5527c038 32222+ if (d_is_negative(wh_dentry))
1facf9fc 32223+ goto out_wh; /* success */
32224+
32225+ err = 1;
7e9cd9fe 32226+ if (d_is_reg(wh_dentry))
1facf9fc 32227+ goto out_wh; /* success */
32228+
32229+ err = -EIO;
523b37e3 32230+ AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
5527c038 32231+ wh_dentry, d_inode(wh_dentry)->i_mode);
1facf9fc 32232+
4f0767ce 32233+out_wh:
1facf9fc 32234+ dput(wh_dentry);
4f0767ce 32235+out:
1facf9fc 32236+ return err;
32237+}
32238+
32239+/*
32240+ * test if the @h_dentry sets opaque or not.
32241+ */
076b876e 32242+int au_diropq_test(struct dentry *h_dentry)
1facf9fc 32243+{
32244+ int err;
32245+ struct inode *h_dir;
32246+
5527c038 32247+ h_dir = d_inode(h_dentry);
076b876e 32248+ err = au_wh_test(h_dentry, &diropq_name,
1facf9fc 32249+ au_test_h_perm_sio(h_dir, MAY_EXEC));
32250+ return err;
32251+}
32252+
32253+/*
32254+ * returns a negative dentry whose name is unique and temporary.
32255+ */
32256+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
32257+ struct qstr *prefix)
32258+{
1facf9fc 32259+ struct dentry *dentry;
32260+ int i;
027c5e7a 32261+ char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
4a4d8108 32262+ *name, *p;
027c5e7a 32263+ /* strict atomic_t is unnecessary here */
1facf9fc 32264+ static unsigned short cnt;
32265+ struct qstr qs;
32266+
4a4d8108
AM
32267+ BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
32268+
1facf9fc 32269+ name = defname;
027c5e7a
AM
32270+ qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
32271+ if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
1facf9fc 32272+ dentry = ERR_PTR(-ENAMETOOLONG);
4a4d8108 32273+ if (unlikely(qs.len > NAME_MAX))
1facf9fc 32274+ goto out;
32275+ dentry = ERR_PTR(-ENOMEM);
32276+ name = kmalloc(qs.len + 1, GFP_NOFS);
32277+ if (unlikely(!name))
32278+ goto out;
32279+ }
32280+
32281+ /* doubly whiteout-ed */
32282+ memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
32283+ p = name + AUFS_WH_PFX_LEN * 2;
32284+ memcpy(p, prefix->name, prefix->len);
32285+ p += prefix->len;
32286+ *p++ = '.';
4a4d8108 32287+ AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
1facf9fc 32288+
32289+ qs.name = name;
32290+ for (i = 0; i < 3; i++) {
b752ccd1 32291+ sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
076b876e 32292+ dentry = au_sio_lkup_one(&qs, h_parent);
5527c038 32293+ if (IS_ERR(dentry) || d_is_negative(dentry))
1facf9fc 32294+ goto out_name;
32295+ dput(dentry);
32296+ }
0c3ec466 32297+ /* pr_warn("could not get random name\n"); */
1facf9fc 32298+ dentry = ERR_PTR(-EEXIST);
32299+ AuDbg("%.*s\n", AuLNPair(&qs));
32300+ BUG();
32301+
4f0767ce 32302+out_name:
1facf9fc 32303+ if (name != defname)
32304+ kfree(name);
4f0767ce 32305+out:
4a4d8108 32306+ AuTraceErrPtr(dentry);
1facf9fc 32307+ return dentry;
1facf9fc 32308+}
32309+
32310+/*
32311+ * rename the @h_dentry on @br to the whiteouted temporary name.
32312+ */
32313+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
32314+{
32315+ int err;
32316+ struct path h_path = {
86dc4139 32317+ .mnt = au_br_mnt(br)
1facf9fc 32318+ };
523b37e3 32319+ struct inode *h_dir, *delegated;
1facf9fc 32320+ struct dentry *h_parent;
32321+
32322+ h_parent = h_dentry->d_parent; /* dir inode is locked */
5527c038 32323+ h_dir = d_inode(h_parent);
1facf9fc 32324+ IMustLock(h_dir);
32325+
32326+ h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
32327+ err = PTR_ERR(h_path.dentry);
32328+ if (IS_ERR(h_path.dentry))
32329+ goto out;
32330+
32331+ /* under the same dir, no need to lock_rename() */
523b37e3
AM
32332+ delegated = NULL;
32333+ err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated);
1facf9fc 32334+ AuTraceErr(err);
523b37e3
AM
32335+ if (unlikely(err == -EWOULDBLOCK)) {
32336+ pr_warn("cannot retry for NFSv4 delegation"
32337+ " for an internal rename\n");
32338+ iput(delegated);
32339+ }
1facf9fc 32340+ dput(h_path.dentry);
32341+
4f0767ce 32342+out:
4a4d8108 32343+ AuTraceErr(err);
1facf9fc 32344+ return err;
32345+}
32346+
32347+/* ---------------------------------------------------------------------- */
32348+/*
32349+ * functions for removing a whiteout
32350+ */
32351+
32352+static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
32353+{
523b37e3
AM
32354+ int err, force;
32355+ struct inode *delegated;
1facf9fc 32356+
32357+ /*
32358+ * forces superio when the dir has a sticky bit.
32359+ * this may be a violation of unix fs semantics.
32360+ */
32361+ force = (h_dir->i_mode & S_ISVTX)
5527c038 32362+ && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
523b37e3
AM
32363+ delegated = NULL;
32364+ err = vfsub_unlink(h_dir, h_path, &delegated, force);
32365+ if (unlikely(err == -EWOULDBLOCK)) {
32366+ pr_warn("cannot retry for NFSv4 delegation"
32367+ " for an internal unlink\n");
32368+ iput(delegated);
32369+ }
32370+ return err;
1facf9fc 32371+}
32372+
32373+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
32374+ struct dentry *dentry)
32375+{
32376+ int err;
32377+
32378+ err = do_unlink_wh(h_dir, h_path);
32379+ if (!err && dentry)
32380+ au_set_dbwh(dentry, -1);
32381+
32382+ return err;
32383+}
32384+
32385+static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
32386+ struct au_branch *br)
32387+{
32388+ int err;
32389+ struct path h_path = {
86dc4139 32390+ .mnt = au_br_mnt(br)
1facf9fc 32391+ };
32392+
32393+ err = 0;
b4510431 32394+ h_path.dentry = vfsub_lkup_one(wh, h_parent);
1facf9fc 32395+ if (IS_ERR(h_path.dentry))
32396+ err = PTR_ERR(h_path.dentry);
32397+ else {
5527c038
JR
32398+ if (d_is_reg(h_path.dentry))
32399+ err = do_unlink_wh(d_inode(h_parent), &h_path);
1facf9fc 32400+ dput(h_path.dentry);
32401+ }
32402+
32403+ return err;
32404+}
32405+
32406+/* ---------------------------------------------------------------------- */
32407+/*
32408+ * initialize/clean whiteout for a branch
32409+ */
32410+
32411+static void au_wh_clean(struct inode *h_dir, struct path *whpath,
32412+ const int isdir)
32413+{
32414+ int err;
523b37e3 32415+ struct inode *delegated;
1facf9fc 32416+
5527c038 32417+ if (d_is_negative(whpath->dentry))
1facf9fc 32418+ return;
32419+
86dc4139
AM
32420+ if (isdir)
32421+ err = vfsub_rmdir(h_dir, whpath);
523b37e3
AM
32422+ else {
32423+ delegated = NULL;
32424+ err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
32425+ if (unlikely(err == -EWOULDBLOCK)) {
32426+ pr_warn("cannot retry for NFSv4 delegation"
32427+ " for an internal unlink\n");
32428+ iput(delegated);
32429+ }
32430+ }
1facf9fc 32431+ if (unlikely(err))
523b37e3
AM
32432+ pr_warn("failed removing %pd (%d), ignored.\n",
32433+ whpath->dentry, err);
1facf9fc 32434+}
32435+
32436+static int test_linkable(struct dentry *h_root)
32437+{
5527c038 32438+ struct inode *h_dir = d_inode(h_root);
1facf9fc 32439+
32440+ if (h_dir->i_op->link)
32441+ return 0;
32442+
523b37e3
AM
32443+ pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
32444+ h_root, au_sbtype(h_root->d_sb));
1facf9fc 32445+ return -ENOSYS;
32446+}
32447+
32448+/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
32449+static int au_whdir(struct inode *h_dir, struct path *path)
32450+{
32451+ int err;
32452+
32453+ err = -EEXIST;
5527c038 32454+ if (d_is_negative(path->dentry)) {
1facf9fc 32455+ int mode = S_IRWXU;
32456+
32457+ if (au_test_nfs(path->dentry->d_sb))
32458+ mode |= S_IXUGO;
86dc4139 32459+ err = vfsub_mkdir(h_dir, path, mode);
2000de60 32460+ } else if (d_is_dir(path->dentry))
1facf9fc 32461+ err = 0;
32462+ else
523b37e3 32463+ pr_err("unknown %pd exists\n", path->dentry);
1facf9fc 32464+
32465+ return err;
32466+}
32467+
32468+struct au_wh_base {
32469+ const struct qstr *name;
32470+ struct dentry *dentry;
32471+};
32472+
32473+static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
32474+ struct path *h_path)
32475+{
32476+ h_path->dentry = base[AuBrWh_BASE].dentry;
32477+ au_wh_clean(h_dir, h_path, /*isdir*/0);
32478+ h_path->dentry = base[AuBrWh_PLINK].dentry;
32479+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32480+ h_path->dentry = base[AuBrWh_ORPH].dentry;
32481+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32482+}
32483+
32484+/*
32485+ * returns tri-state,
c1595e42 32486+ * minus: error, caller should print the message
1facf9fc 32487+ * zero: succuess
c1595e42 32488+ * plus: error, caller should NOT print the message
1facf9fc 32489+ */
32490+static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
32491+ int do_plink, struct au_wh_base base[],
32492+ struct path *h_path)
32493+{
32494+ int err;
32495+ struct inode *h_dir;
32496+
5527c038 32497+ h_dir = d_inode(h_root);
1facf9fc 32498+ h_path->dentry = base[AuBrWh_BASE].dentry;
32499+ au_wh_clean(h_dir, h_path, /*isdir*/0);
32500+ h_path->dentry = base[AuBrWh_PLINK].dentry;
32501+ if (do_plink) {
32502+ err = test_linkable(h_root);
32503+ if (unlikely(err)) {
32504+ err = 1;
32505+ goto out;
32506+ }
32507+
32508+ err = au_whdir(h_dir, h_path);
32509+ if (unlikely(err))
32510+ goto out;
32511+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32512+ } else
32513+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32514+ h_path->dentry = base[AuBrWh_ORPH].dentry;
32515+ err = au_whdir(h_dir, h_path);
32516+ if (unlikely(err))
32517+ goto out;
32518+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32519+
4f0767ce 32520+out:
1facf9fc 32521+ return err;
32522+}
32523+
32524+/*
32525+ * for the moment, aufs supports the branch filesystem which does not support
32526+ * link(2). testing on FAT which does not support i_op->setattr() fully either,
32527+ * copyup failed. finally, such filesystem will not be used as the writable
32528+ * branch.
32529+ *
32530+ * returns tri-state, see above.
32531+ */
32532+static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
32533+ int do_plink, struct au_wh_base base[],
32534+ struct path *h_path)
32535+{
32536+ int err;
32537+ struct inode *h_dir;
32538+
1308ab2a 32539+ WbrWhMustWriteLock(wbr);
32540+
1facf9fc 32541+ err = test_linkable(h_root);
32542+ if (unlikely(err)) {
32543+ err = 1;
32544+ goto out;
32545+ }
32546+
32547+ /*
32548+ * todo: should this create be done in /sbin/mount.aufs helper?
32549+ */
32550+ err = -EEXIST;
5527c038
JR
32551+ h_dir = d_inode(h_root);
32552+ if (d_is_negative(base[AuBrWh_BASE].dentry)) {
86dc4139
AM
32553+ h_path->dentry = base[AuBrWh_BASE].dentry;
32554+ err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
7e9cd9fe 32555+ } else if (d_is_reg(base[AuBrWh_BASE].dentry))
1facf9fc 32556+ err = 0;
32557+ else
523b37e3 32558+ pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
1facf9fc 32559+ if (unlikely(err))
32560+ goto out;
32561+
32562+ h_path->dentry = base[AuBrWh_PLINK].dentry;
32563+ if (do_plink) {
32564+ err = au_whdir(h_dir, h_path);
32565+ if (unlikely(err))
32566+ goto out;
32567+ wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
32568+ } else
32569+ au_wh_clean(h_dir, h_path, /*isdir*/1);
32570+ wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
32571+
32572+ h_path->dentry = base[AuBrWh_ORPH].dentry;
32573+ err = au_whdir(h_dir, h_path);
32574+ if (unlikely(err))
32575+ goto out;
32576+ wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
32577+
4f0767ce 32578+out:
1facf9fc 32579+ return err;
32580+}
32581+
32582+/*
32583+ * initialize the whiteout base file/dir for @br.
32584+ */
86dc4139 32585+int au_wh_init(struct au_branch *br, struct super_block *sb)
1facf9fc 32586+{
32587+ int err, i;
32588+ const unsigned char do_plink
32589+ = !!au_opt_test(au_mntflags(sb), PLINK);
1facf9fc 32590+ struct inode *h_dir;
86dc4139
AM
32591+ struct path path = br->br_path;
32592+ struct dentry *h_root = path.dentry;
1facf9fc 32593+ struct au_wbr *wbr = br->br_wbr;
32594+ static const struct qstr base_name[] = {
0c3ec466
AM
32595+ [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
32596+ sizeof(AUFS_BASE_NAME) - 1),
32597+ [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
32598+ sizeof(AUFS_PLINKDIR_NAME) - 1),
32599+ [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
32600+ sizeof(AUFS_ORPHDIR_NAME) - 1)
1facf9fc 32601+ };
32602+ struct au_wh_base base[] = {
32603+ [AuBrWh_BASE] = {
32604+ .name = base_name + AuBrWh_BASE,
32605+ .dentry = NULL
32606+ },
32607+ [AuBrWh_PLINK] = {
32608+ .name = base_name + AuBrWh_PLINK,
32609+ .dentry = NULL
32610+ },
32611+ [AuBrWh_ORPH] = {
32612+ .name = base_name + AuBrWh_ORPH,
32613+ .dentry = NULL
32614+ }
32615+ };
32616+
1308ab2a 32617+ if (wbr)
32618+ WbrWhMustWriteLock(wbr);
1facf9fc 32619+
1facf9fc 32620+ for (i = 0; i < AuBrWh_Last; i++) {
32621+ /* doubly whiteouted */
32622+ struct dentry *d;
32623+
32624+ d = au_wh_lkup(h_root, (void *)base[i].name, br);
32625+ err = PTR_ERR(d);
32626+ if (IS_ERR(d))
32627+ goto out;
32628+
32629+ base[i].dentry = d;
32630+ AuDebugOn(wbr
32631+ && wbr->wbr_wh[i]
32632+ && wbr->wbr_wh[i] != base[i].dentry);
32633+ }
32634+
32635+ if (wbr)
32636+ for (i = 0; i < AuBrWh_Last; i++) {
32637+ dput(wbr->wbr_wh[i]);
32638+ wbr->wbr_wh[i] = NULL;
32639+ }
32640+
32641+ err = 0;
1e00d052 32642+ if (!au_br_writable(br->br_perm)) {
5527c038 32643+ h_dir = d_inode(h_root);
1facf9fc 32644+ au_wh_init_ro(h_dir, base, &path);
1e00d052 32645+ } else if (!au_br_wh_linkable(br->br_perm)) {
1facf9fc 32646+ err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
32647+ if (err > 0)
32648+ goto out;
32649+ else if (err)
32650+ goto out_err;
1e00d052 32651+ } else {
1facf9fc 32652+ err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
32653+ if (err > 0)
32654+ goto out;
32655+ else if (err)
32656+ goto out_err;
1facf9fc 32657+ }
32658+ goto out; /* success */
32659+
4f0767ce 32660+out_err:
523b37e3
AM
32661+ pr_err("an error(%d) on the writable branch %pd(%s)\n",
32662+ err, h_root, au_sbtype(h_root->d_sb));
4f0767ce 32663+out:
1facf9fc 32664+ for (i = 0; i < AuBrWh_Last; i++)
32665+ dput(base[i].dentry);
32666+ return err;
32667+}
32668+
32669+/* ---------------------------------------------------------------------- */
32670+/*
32671+ * whiteouts are all hard-linked usually.
32672+ * when its link count reaches a ceiling, we create a new whiteout base
32673+ * asynchronously.
32674+ */
32675+
32676+struct reinit_br_wh {
32677+ struct super_block *sb;
32678+ struct au_branch *br;
32679+};
32680+
32681+static void reinit_br_wh(void *arg)
32682+{
32683+ int err;
32684+ aufs_bindex_t bindex;
32685+ struct path h_path;
32686+ struct reinit_br_wh *a = arg;
32687+ struct au_wbr *wbr;
523b37e3 32688+ struct inode *dir, *delegated;
1facf9fc 32689+ struct dentry *h_root;
32690+ struct au_hinode *hdir;
32691+
32692+ err = 0;
32693+ wbr = a->br->br_wbr;
32694+ /* big aufs lock */
32695+ si_noflush_write_lock(a->sb);
32696+ if (!au_br_writable(a->br->br_perm))
32697+ goto out;
32698+ bindex = au_br_index(a->sb, a->br->br_id);
32699+ if (unlikely(bindex < 0))
32700+ goto out;
32701+
1308ab2a 32702+ di_read_lock_parent(a->sb->s_root, AuLock_IR);
5527c038 32703+ dir = d_inode(a->sb->s_root);
1facf9fc 32704+ hdir = au_hi(dir, bindex);
32705+ h_root = au_h_dptr(a->sb->s_root, bindex);
86dc4139 32706+ AuDebugOn(h_root != au_br_dentry(a->br));
1facf9fc 32707+
5afbbe0d 32708+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
1facf9fc 32709+ wbr_wh_write_lock(wbr);
32710+ err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
32711+ h_root, a->br);
32712+ if (!err) {
86dc4139
AM
32713+ h_path.dentry = wbr->wbr_whbase;
32714+ h_path.mnt = au_br_mnt(a->br);
523b37e3
AM
32715+ delegated = NULL;
32716+ err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
32717+ /*force*/0);
32718+ if (unlikely(err == -EWOULDBLOCK)) {
32719+ pr_warn("cannot retry for NFSv4 delegation"
32720+ " for an internal unlink\n");
32721+ iput(delegated);
32722+ }
1facf9fc 32723+ } else {
523b37e3 32724+ pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
1facf9fc 32725+ err = 0;
32726+ }
32727+ dput(wbr->wbr_whbase);
32728+ wbr->wbr_whbase = NULL;
32729+ if (!err)
86dc4139 32730+ err = au_wh_init(a->br, a->sb);
1facf9fc 32731+ wbr_wh_write_unlock(wbr);
5afbbe0d 32732+ au_hn_inode_unlock(hdir);
1308ab2a 32733+ di_read_unlock(a->sb->s_root, AuLock_IR);
076b876e
AM
32734+ if (!err)
32735+ au_fhsm_wrote(a->sb, bindex, /*force*/0);
1facf9fc 32736+
4f0767ce 32737+out:
1facf9fc 32738+ if (wbr)
32739+ atomic_dec(&wbr->wbr_wh_running);
5afbbe0d 32740+ au_br_put(a->br);
1facf9fc 32741+ si_write_unlock(a->sb);
027c5e7a 32742+ au_nwt_done(&au_sbi(a->sb)->si_nowait);
1facf9fc 32743+ kfree(arg);
32744+ if (unlikely(err))
32745+ AuIOErr("err %d\n", err);
32746+}
32747+
32748+static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
32749+{
32750+ int do_dec, wkq_err;
32751+ struct reinit_br_wh *arg;
32752+
32753+ do_dec = 1;
32754+ if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
32755+ goto out;
32756+
32757+ /* ignore ENOMEM */
32758+ arg = kmalloc(sizeof(*arg), GFP_NOFS);
32759+ if (arg) {
32760+ /*
32761+ * dec(wh_running), kfree(arg) and dec(br_count)
32762+ * in reinit function
32763+ */
32764+ arg->sb = sb;
32765+ arg->br = br;
5afbbe0d 32766+ au_br_get(br);
53392da6 32767+ wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
1facf9fc 32768+ if (unlikely(wkq_err)) {
32769+ atomic_dec(&br->br_wbr->wbr_wh_running);
5afbbe0d 32770+ au_br_put(br);
1facf9fc 32771+ kfree(arg);
32772+ }
32773+ do_dec = 0;
32774+ }
32775+
4f0767ce 32776+out:
1facf9fc 32777+ if (do_dec)
32778+ atomic_dec(&br->br_wbr->wbr_wh_running);
32779+}
32780+
32781+/* ---------------------------------------------------------------------- */
32782+
32783+/*
32784+ * create the whiteout @wh.
32785+ */
32786+static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
32787+ struct dentry *wh)
32788+{
32789+ int err;
32790+ struct path h_path = {
32791+ .dentry = wh
32792+ };
32793+ struct au_branch *br;
32794+ struct au_wbr *wbr;
32795+ struct dentry *h_parent;
523b37e3 32796+ struct inode *h_dir, *delegated;
1facf9fc 32797+
32798+ h_parent = wh->d_parent; /* dir inode is locked */
5527c038 32799+ h_dir = d_inode(h_parent);
1facf9fc 32800+ IMustLock(h_dir);
32801+
32802+ br = au_sbr(sb, bindex);
86dc4139 32803+ h_path.mnt = au_br_mnt(br);
1facf9fc 32804+ wbr = br->br_wbr;
32805+ wbr_wh_read_lock(wbr);
32806+ if (wbr->wbr_whbase) {
523b37e3
AM
32807+ delegated = NULL;
32808+ err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
32809+ if (unlikely(err == -EWOULDBLOCK)) {
32810+ pr_warn("cannot retry for NFSv4 delegation"
32811+ " for an internal link\n");
32812+ iput(delegated);
32813+ }
1facf9fc 32814+ if (!err || err != -EMLINK)
32815+ goto out;
32816+
32817+ /* link count full. re-initialize br_whbase. */
32818+ kick_reinit_br_wh(sb, br);
32819+ }
32820+
32821+ /* return this error in this context */
b4510431 32822+ err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
076b876e
AM
32823+ if (!err)
32824+ au_fhsm_wrote(sb, bindex, /*force*/0);
1facf9fc 32825+
4f0767ce 32826+out:
1facf9fc 32827+ wbr_wh_read_unlock(wbr);
32828+ return err;
32829+}
32830+
32831+/* ---------------------------------------------------------------------- */
32832+
32833+/*
32834+ * create or remove the diropq.
32835+ */
32836+static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
32837+ unsigned int flags)
32838+{
32839+ struct dentry *opq_dentry, *h_dentry;
32840+ struct super_block *sb;
32841+ struct au_branch *br;
32842+ int err;
32843+
32844+ sb = dentry->d_sb;
32845+ br = au_sbr(sb, bindex);
32846+ h_dentry = au_h_dptr(dentry, bindex);
b4510431 32847+ opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
1facf9fc 32848+ if (IS_ERR(opq_dentry))
32849+ goto out;
32850+
32851+ if (au_ftest_diropq(flags, CREATE)) {
32852+ err = link_or_create_wh(sb, bindex, opq_dentry);
32853+ if (!err) {
32854+ au_set_dbdiropq(dentry, bindex);
32855+ goto out; /* success */
32856+ }
32857+ } else {
32858+ struct path tmp = {
32859+ .dentry = opq_dentry,
86dc4139 32860+ .mnt = au_br_mnt(br)
1facf9fc 32861+ };
5527c038 32862+ err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
1facf9fc 32863+ if (!err)
32864+ au_set_dbdiropq(dentry, -1);
32865+ }
32866+ dput(opq_dentry);
32867+ opq_dentry = ERR_PTR(err);
32868+
4f0767ce 32869+out:
1facf9fc 32870+ return opq_dentry;
32871+}
32872+
32873+struct do_diropq_args {
32874+ struct dentry **errp;
32875+ struct dentry *dentry;
32876+ aufs_bindex_t bindex;
32877+ unsigned int flags;
32878+};
32879+
32880+static void call_do_diropq(void *args)
32881+{
32882+ struct do_diropq_args *a = args;
32883+ *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
32884+}
32885+
32886+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
32887+ unsigned int flags)
32888+{
32889+ struct dentry *diropq, *h_dentry;
32890+
32891+ h_dentry = au_h_dptr(dentry, bindex);
5527c038 32892+ if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
1facf9fc 32893+ diropq = do_diropq(dentry, bindex, flags);
32894+ else {
32895+ int wkq_err;
32896+ struct do_diropq_args args = {
32897+ .errp = &diropq,
32898+ .dentry = dentry,
32899+ .bindex = bindex,
32900+ .flags = flags
32901+ };
32902+
32903+ wkq_err = au_wkq_wait(call_do_diropq, &args);
32904+ if (unlikely(wkq_err))
32905+ diropq = ERR_PTR(wkq_err);
32906+ }
32907+
32908+ return diropq;
32909+}
32910+
32911+/* ---------------------------------------------------------------------- */
32912+
32913+/*
32914+ * lookup whiteout dentry.
32915+ * @h_parent: lower parent dentry which must exist and be locked
32916+ * @base_name: name of dentry which will be whiteouted
32917+ * returns dentry for whiteout.
32918+ */
32919+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
32920+ struct au_branch *br)
32921+{
32922+ int err;
32923+ struct qstr wh_name;
32924+ struct dentry *wh_dentry;
32925+
32926+ err = au_wh_name_alloc(&wh_name, base_name);
32927+ wh_dentry = ERR_PTR(err);
32928+ if (!err) {
b4510431 32929+ wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
1facf9fc 32930+ kfree(wh_name.name);
32931+ }
32932+ return wh_dentry;
32933+}
32934+
32935+/*
32936+ * link/create a whiteout for @dentry on @bindex.
32937+ */
32938+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
32939+ struct dentry *h_parent)
32940+{
32941+ struct dentry *wh_dentry;
32942+ struct super_block *sb;
32943+ int err;
32944+
32945+ sb = dentry->d_sb;
32946+ wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
5527c038 32947+ if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
1facf9fc 32948+ err = link_or_create_wh(sb, bindex, wh_dentry);
076b876e 32949+ if (!err) {
1facf9fc 32950+ au_set_dbwh(dentry, bindex);
076b876e
AM
32951+ au_fhsm_wrote(sb, bindex, /*force*/0);
32952+ } else {
1facf9fc 32953+ dput(wh_dentry);
32954+ wh_dentry = ERR_PTR(err);
32955+ }
32956+ }
32957+
32958+ return wh_dentry;
32959+}
32960+
32961+/* ---------------------------------------------------------------------- */
32962+
32963+/* Delete all whiteouts in this directory on branch bindex. */
32964+static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
32965+ aufs_bindex_t bindex, struct au_branch *br)
32966+{
32967+ int err;
32968+ unsigned long ul, n;
32969+ struct qstr wh_name;
32970+ char *p;
32971+ struct hlist_head *head;
c06a8ce3 32972+ struct au_vdir_wh *pos;
1facf9fc 32973+ struct au_vdir_destr *str;
32974+
32975+ err = -ENOMEM;
537831f9 32976+ p = (void *)__get_free_page(GFP_NOFS);
1facf9fc 32977+ wh_name.name = p;
32978+ if (unlikely(!wh_name.name))
32979+ goto out;
32980+
32981+ err = 0;
32982+ memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32983+ p += AUFS_WH_PFX_LEN;
32984+ n = whlist->nh_num;
32985+ head = whlist->nh_head;
32986+ for (ul = 0; !err && ul < n; ul++, head++) {
c06a8ce3
AM
32987+ hlist_for_each_entry(pos, head, wh_hash) {
32988+ if (pos->wh_bindex != bindex)
1facf9fc 32989+ continue;
32990+
c06a8ce3 32991+ str = &pos->wh_str;
1facf9fc 32992+ if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
32993+ memcpy(p, str->name, str->len);
32994+ wh_name.len = AUFS_WH_PFX_LEN + str->len;
32995+ err = unlink_wh_name(h_dentry, &wh_name, br);
32996+ if (!err)
32997+ continue;
32998+ break;
32999+ }
33000+ AuIOErr("whiteout name too long %.*s\n",
33001+ str->len, str->name);
33002+ err = -EIO;
33003+ break;
33004+ }
33005+ }
537831f9 33006+ free_page((unsigned long)wh_name.name);
1facf9fc 33007+
4f0767ce 33008+out:
1facf9fc 33009+ return err;
33010+}
33011+
33012+struct del_wh_children_args {
33013+ int *errp;
33014+ struct dentry *h_dentry;
1308ab2a 33015+ struct au_nhash *whlist;
1facf9fc 33016+ aufs_bindex_t bindex;
33017+ struct au_branch *br;
33018+};
33019+
33020+static void call_del_wh_children(void *args)
33021+{
33022+ struct del_wh_children_args *a = args;
1308ab2a 33023+ *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
1facf9fc 33024+}
33025+
33026+/* ---------------------------------------------------------------------- */
33027+
33028+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
33029+{
33030+ struct au_whtmp_rmdir *whtmp;
dece6358 33031+ int err;
1308ab2a 33032+ unsigned int rdhash;
dece6358
AM
33033+
33034+ SiMustAnyLock(sb);
1facf9fc 33035+
be52b249 33036+ whtmp = kzalloc(sizeof(*whtmp), gfp);
dece6358
AM
33037+ if (unlikely(!whtmp)) {
33038+ whtmp = ERR_PTR(-ENOMEM);
1facf9fc 33039+ goto out;
dece6358 33040+ }
1facf9fc 33041+
1308ab2a 33042+ /* no estimation for dir size */
33043+ rdhash = au_sbi(sb)->si_rdhash;
33044+ if (!rdhash)
33045+ rdhash = AUFS_RDHASH_DEF;
33046+ err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
33047+ if (unlikely(err)) {
33048+ kfree(whtmp);
33049+ whtmp = ERR_PTR(err);
33050+ }
dece6358 33051+
4f0767ce 33052+out:
dece6358 33053+ return whtmp;
1facf9fc 33054+}
33055+
33056+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
33057+{
027c5e7a 33058+ if (whtmp->br)
5afbbe0d 33059+ au_br_put(whtmp->br);
1facf9fc 33060+ dput(whtmp->wh_dentry);
33061+ iput(whtmp->dir);
dece6358 33062+ au_nhash_wh_free(&whtmp->whlist);
1facf9fc 33063+ kfree(whtmp);
33064+}
33065+
33066+/*
33067+ * rmdir the whiteouted temporary named dir @h_dentry.
33068+ * @whlist: whiteouted children.
33069+ */
33070+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33071+ struct dentry *wh_dentry, struct au_nhash *whlist)
33072+{
33073+ int err;
2000de60 33074+ unsigned int h_nlink;
1facf9fc 33075+ struct path h_tmp;
33076+ struct inode *wh_inode, *h_dir;
33077+ struct au_branch *br;
33078+
5527c038 33079+ h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
1facf9fc 33080+ IMustLock(h_dir);
33081+
33082+ br = au_sbr(dir->i_sb, bindex);
5527c038 33083+ wh_inode = d_inode(wh_dentry);
febd17d6 33084+ inode_lock_nested(wh_inode, AuLsc_I_CHILD);
1facf9fc 33085+
33086+ /*
33087+ * someone else might change some whiteouts while we were sleeping.
33088+ * it means this whlist may have an obsoleted entry.
33089+ */
33090+ if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
33091+ err = del_wh_children(wh_dentry, whlist, bindex, br);
33092+ else {
33093+ int wkq_err;
33094+ struct del_wh_children_args args = {
33095+ .errp = &err,
33096+ .h_dentry = wh_dentry,
1308ab2a 33097+ .whlist = whlist,
1facf9fc 33098+ .bindex = bindex,
33099+ .br = br
33100+ };
33101+
33102+ wkq_err = au_wkq_wait(call_del_wh_children, &args);
33103+ if (unlikely(wkq_err))
33104+ err = wkq_err;
33105+ }
febd17d6 33106+ inode_unlock(wh_inode);
1facf9fc 33107+
33108+ if (!err) {
33109+ h_tmp.dentry = wh_dentry;
86dc4139 33110+ h_tmp.mnt = au_br_mnt(br);
2000de60 33111+ h_nlink = h_dir->i_nlink;
1facf9fc 33112+ err = vfsub_rmdir(h_dir, &h_tmp);
2000de60
JR
33113+ /* some fs doesn't change the parent nlink in some cases */
33114+ h_nlink -= h_dir->i_nlink;
1facf9fc 33115+ }
33116+
33117+ if (!err) {
5afbbe0d 33118+ if (au_ibtop(dir) == bindex) {
7f207e10 33119+ /* todo: dir->i_mutex is necessary */
1facf9fc 33120+ au_cpup_attr_timesizes(dir);
2000de60
JR
33121+ if (h_nlink)
33122+ vfsub_drop_nlink(dir);
1facf9fc 33123+ }
33124+ return 0; /* success */
33125+ }
33126+
523b37e3 33127+ pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
1facf9fc 33128+ return err;
33129+}
33130+
33131+static void call_rmdir_whtmp(void *args)
33132+{
33133+ int err;
e49829fe 33134+ aufs_bindex_t bindex;
1facf9fc 33135+ struct au_whtmp_rmdir *a = args;
33136+ struct super_block *sb;
33137+ struct dentry *h_parent;
33138+ struct inode *h_dir;
1facf9fc 33139+ struct au_hinode *hdir;
33140+
33141+ /* rmdir by nfsd may cause deadlock with this i_mutex */
febd17d6 33142+ /* inode_lock(a->dir); */
e49829fe 33143+ err = -EROFS;
1facf9fc 33144+ sb = a->dir->i_sb;
e49829fe
JR
33145+ si_read_lock(sb, !AuLock_FLUSH);
33146+ if (!au_br_writable(a->br->br_perm))
33147+ goto out;
33148+ bindex = au_br_index(sb, a->br->br_id);
33149+ if (unlikely(bindex < 0))
1facf9fc 33150+ goto out;
33151+
33152+ err = -EIO;
1facf9fc 33153+ ii_write_lock_parent(a->dir);
33154+ h_parent = dget_parent(a->wh_dentry);
5527c038 33155+ h_dir = d_inode(h_parent);
e49829fe 33156+ hdir = au_hi(a->dir, bindex);
86dc4139
AM
33157+ err = vfsub_mnt_want_write(au_br_mnt(a->br));
33158+ if (unlikely(err))
33159+ goto out_mnt;
5afbbe0d 33160+ au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
e49829fe
JR
33161+ err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
33162+ a->br);
86dc4139
AM
33163+ if (!err)
33164+ err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
5afbbe0d 33165+ au_hn_inode_unlock(hdir);
86dc4139
AM
33166+ vfsub_mnt_drop_write(au_br_mnt(a->br));
33167+
33168+out_mnt:
1facf9fc 33169+ dput(h_parent);
33170+ ii_write_unlock(a->dir);
4f0767ce 33171+out:
febd17d6 33172+ /* inode_unlock(a->dir); */
1facf9fc 33173+ au_whtmp_rmdir_free(a);
027c5e7a
AM
33174+ si_read_unlock(sb);
33175+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 33176+ if (unlikely(err))
33177+ AuIOErr("err %d\n", err);
33178+}
33179+
33180+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33181+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
33182+{
33183+ int wkq_err;
e49829fe 33184+ struct super_block *sb;
1facf9fc 33185+
33186+ IMustLock(dir);
33187+
33188+ /* all post-process will be done in do_rmdir_whtmp(). */
e49829fe 33189+ sb = dir->i_sb;
1facf9fc 33190+ args->dir = au_igrab(dir);
e49829fe 33191+ args->br = au_sbr(sb, bindex);
5afbbe0d 33192+ au_br_get(args->br);
1facf9fc 33193+ args->wh_dentry = dget(wh_dentry);
53392da6 33194+ wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
1facf9fc 33195+ if (unlikely(wkq_err)) {
523b37e3 33196+ pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
1facf9fc 33197+ au_whtmp_rmdir_free(args);
33198+ }
33199+}
7f207e10
AM
33200diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
33201--- /usr/share/empty/fs/aufs/whout.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 33202+++ linux/fs/aufs/whout.h 2016-07-25 19:05:34.817826663 +0200
076b876e 33203@@ -0,0 +1,85 @@
1facf9fc 33204+/*
8cdd5066 33205+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33206+ *
33207+ * This program, aufs is free software; you can redistribute it and/or modify
33208+ * it under the terms of the GNU General Public License as published by
33209+ * the Free Software Foundation; either version 2 of the License, or
33210+ * (at your option) any later version.
dece6358
AM
33211+ *
33212+ * This program is distributed in the hope that it will be useful,
33213+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33214+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33215+ * GNU General Public License for more details.
33216+ *
33217+ * You should have received a copy of the GNU General Public License
523b37e3 33218+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33219+ */
33220+
33221+/*
33222+ * whiteout for logical deletion and opaque directory
33223+ */
33224+
33225+#ifndef __AUFS_WHOUT_H__
33226+#define __AUFS_WHOUT_H__
33227+
33228+#ifdef __KERNEL__
33229+
1facf9fc 33230+#include "dir.h"
33231+
33232+/* whout.c */
33233+int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
076b876e
AM
33234+int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
33235+int au_diropq_test(struct dentry *h_dentry);
7e9cd9fe 33236+struct au_branch;
1facf9fc 33237+struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
33238+ struct qstr *prefix);
33239+int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
33240+int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
33241+ struct dentry *dentry);
86dc4139 33242+int au_wh_init(struct au_branch *br, struct super_block *sb);
1facf9fc 33243+
33244+/* diropq flags */
33245+#define AuDiropq_CREATE 1
33246+#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
7f207e10
AM
33247+#define au_fset_diropq(flags, name) \
33248+ do { (flags) |= AuDiropq_##name; } while (0)
33249+#define au_fclr_diropq(flags, name) \
33250+ do { (flags) &= ~AuDiropq_##name; } while (0)
1facf9fc 33251+
33252+struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
33253+ unsigned int flags);
33254+struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
33255+ struct au_branch *br);
33256+struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
33257+ struct dentry *h_parent);
33258+
33259+/* real rmdir for the whiteout-ed dir */
33260+struct au_whtmp_rmdir {
33261+ struct inode *dir;
e49829fe 33262+ struct au_branch *br;
1facf9fc 33263+ struct dentry *wh_dentry;
dece6358 33264+ struct au_nhash whlist;
1facf9fc 33265+};
33266+
33267+struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
33268+void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
33269+int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
33270+ struct dentry *wh_dentry, struct au_nhash *whlist);
33271+void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
33272+ struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
33273+
33274+/* ---------------------------------------------------------------------- */
33275+
33276+static inline struct dentry *au_diropq_create(struct dentry *dentry,
33277+ aufs_bindex_t bindex)
33278+{
33279+ return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
33280+}
33281+
33282+static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
33283+{
33284+ return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
33285+}
33286+
33287+#endif /* __KERNEL__ */
33288+#endif /* __AUFS_WHOUT_H__ */
7f207e10
AM
33289diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
33290--- /usr/share/empty/fs/aufs/wkq.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
33291+++ linux/fs/aufs/wkq.c 2016-07-25 19:05:34.817826663 +0200
33292@@ -0,0 +1,218 @@
1facf9fc 33293+/*
8cdd5066 33294+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33295+ *
33296+ * This program, aufs is free software; you can redistribute it and/or modify
33297+ * it under the terms of the GNU General Public License as published by
33298+ * the Free Software Foundation; either version 2 of the License, or
33299+ * (at your option) any later version.
dece6358
AM
33300+ *
33301+ * This program is distributed in the hope that it will be useful,
33302+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33303+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33304+ * GNU General Public License for more details.
33305+ *
33306+ * You should have received a copy of the GNU General Public License
523b37e3 33307+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33308+ */
33309+
33310+/*
33311+ * workqueue for asynchronous/super-io operations
33312+ * todo: try new dredential scheme
33313+ */
33314+
dece6358 33315+#include <linux/module.h>
1facf9fc 33316+#include "aufs.h"
33317+
9dbd164d 33318+/* internal workqueue named AUFS_WKQ_NAME */
b752ccd1 33319+
9dbd164d 33320+static struct workqueue_struct *au_wkq;
1facf9fc 33321+
33322+struct au_wkinfo {
33323+ struct work_struct wk;
7f207e10 33324+ struct kobject *kobj;
1facf9fc 33325+
33326+ unsigned int flags; /* see wkq.h */
33327+
33328+ au_wkq_func_t func;
33329+ void *args;
33330+
1facf9fc 33331+ struct completion *comp;
33332+};
33333+
33334+/* ---------------------------------------------------------------------- */
33335+
1facf9fc 33336+static void wkq_func(struct work_struct *wk)
33337+{
33338+ struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
33339+
2dfbb274 33340+ AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
7f207e10
AM
33341+ AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
33342+
1facf9fc 33343+ wkinfo->func(wkinfo->args);
1facf9fc 33344+ if (au_ftest_wkq(wkinfo->flags, WAIT))
33345+ complete(wkinfo->comp);
33346+ else {
7f207e10 33347+ kobject_put(wkinfo->kobj);
9dbd164d 33348+ module_put(THIS_MODULE); /* todo: ?? */
1facf9fc 33349+ kfree(wkinfo);
33350+ }
33351+}
33352+
33353+/*
33354+ * Since struct completion is large, try allocating it dynamically.
33355+ */
c2b27bf2 33356+#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
1facf9fc 33357+#define AuWkqCompDeclare(name) struct completion *comp = NULL
33358+
33359+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33360+{
33361+ *comp = kmalloc(sizeof(**comp), GFP_NOFS);
33362+ if (*comp) {
33363+ init_completion(*comp);
33364+ wkinfo->comp = *comp;
33365+ return 0;
33366+ }
33367+ return -ENOMEM;
33368+}
33369+
33370+static void au_wkq_comp_free(struct completion *comp)
33371+{
33372+ kfree(comp);
33373+}
33374+
33375+#else
33376+
33377+/* no braces */
33378+#define AuWkqCompDeclare(name) \
33379+ DECLARE_COMPLETION_ONSTACK(_ ## name); \
33380+ struct completion *comp = &_ ## name
33381+
33382+static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
33383+{
33384+ wkinfo->comp = *comp;
33385+ return 0;
33386+}
33387+
33388+static void au_wkq_comp_free(struct completion *comp __maybe_unused)
33389+{
33390+ /* empty */
33391+}
33392+#endif /* 4KSTACKS */
33393+
53392da6 33394+static void au_wkq_run(struct au_wkinfo *wkinfo)
1facf9fc 33395+{
53392da6
AM
33396+ if (au_ftest_wkq(wkinfo->flags, NEST)) {
33397+ if (au_wkq_test()) {
38d290e6
JR
33398+ AuWarn1("wkq from wkq, unless silly-rename on NFS,"
33399+ " due to a dead dir by UDBA?\n");
53392da6
AM
33400+ AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
33401+ }
33402+ } else
33403+ au_dbg_verify_kthread();
33404+
33405+ if (au_ftest_wkq(wkinfo->flags, WAIT)) {
a1f66529 33406+ INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
9dbd164d 33407+ queue_work(au_wkq, &wkinfo->wk);
4a4d8108
AM
33408+ } else {
33409+ INIT_WORK(&wkinfo->wk, wkq_func);
33410+ schedule_work(&wkinfo->wk);
33411+ }
1facf9fc 33412+}
33413+
7f207e10
AM
33414+/*
33415+ * Be careful. It is easy to make deadlock happen.
33416+ * processA: lock, wkq and wait
33417+ * processB: wkq and wait, lock in wkq
33418+ * --> deadlock
33419+ */
b752ccd1 33420+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
1facf9fc 33421+{
33422+ int err;
33423+ AuWkqCompDeclare(comp);
33424+ struct au_wkinfo wkinfo = {
b752ccd1 33425+ .flags = flags,
1facf9fc 33426+ .func = func,
33427+ .args = args
33428+ };
33429+
33430+ err = au_wkq_comp_alloc(&wkinfo, &comp);
33431+ if (!err) {
53392da6 33432+ au_wkq_run(&wkinfo);
1facf9fc 33433+ /* no timeout, no interrupt */
33434+ wait_for_completion(wkinfo.comp);
33435+ au_wkq_comp_free(comp);
4a4d8108 33436+ destroy_work_on_stack(&wkinfo.wk);
1facf9fc 33437+ }
33438+
33439+ return err;
33440+
33441+}
33442+
027c5e7a
AM
33443+/*
33444+ * Note: dget/dput() in func for aufs dentries are not supported. It will be a
33445+ * problem in a concurrent umounting.
33446+ */
53392da6
AM
33447+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33448+ unsigned int flags)
1facf9fc 33449+{
33450+ int err;
33451+ struct au_wkinfo *wkinfo;
33452+
5afbbe0d 33453+ percpu_counter_inc(&au_sbi(sb)->si_nowait.nw_len);
1facf9fc 33454+
33455+ /*
33456+ * wkq_func() must free this wkinfo.
33457+ * it highly depends upon the implementation of workqueue.
33458+ */
33459+ err = 0;
33460+ wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
33461+ if (wkinfo) {
7f207e10 33462+ wkinfo->kobj = &au_sbi(sb)->si_kobj;
53392da6 33463+ wkinfo->flags = flags & ~AuWkq_WAIT;
1facf9fc 33464+ wkinfo->func = func;
33465+ wkinfo->args = args;
33466+ wkinfo->comp = NULL;
7f207e10 33467+ kobject_get(wkinfo->kobj);
9dbd164d 33468+ __module_get(THIS_MODULE); /* todo: ?? */
1facf9fc 33469+
53392da6 33470+ au_wkq_run(wkinfo);
1facf9fc 33471+ } else {
33472+ err = -ENOMEM;
e49829fe 33473+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 33474+ }
33475+
33476+ return err;
33477+}
33478+
33479+/* ---------------------------------------------------------------------- */
33480+
33481+void au_nwt_init(struct au_nowait_tasks *nwt)
33482+{
5afbbe0d 33483+ percpu_counter_init(&nwt->nw_len, 0, GFP_NOFS);
1facf9fc 33484+ init_waitqueue_head(&nwt->nw_wq);
33485+}
33486+
5afbbe0d
AM
33487+void au_nwt_fin(struct au_nowait_tasks *nwt)
33488+{
33489+ AuDebugOn(percpu_counter_sum(&nwt->nw_len));
33490+ percpu_counter_destroy(&nwt->nw_len);
33491+}
33492+
1facf9fc 33493+void au_wkq_fin(void)
33494+{
9dbd164d 33495+ destroy_workqueue(au_wkq);
1facf9fc 33496+}
33497+
33498+int __init au_wkq_init(void)
33499+{
9dbd164d 33500+ int err;
b752ccd1
AM
33501+
33502+ err = 0;
86dc4139 33503+ au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
9dbd164d
AM
33504+ if (IS_ERR(au_wkq))
33505+ err = PTR_ERR(au_wkq);
33506+ else if (!au_wkq)
33507+ err = -ENOMEM;
b752ccd1
AM
33508+
33509+ return err;
1facf9fc 33510+}
7f207e10
AM
33511diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
33512--- /usr/share/empty/fs/aufs/wkq.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
33513+++ linux/fs/aufs/wkq.h 2016-07-25 19:05:34.817826663 +0200
33514@@ -0,0 +1,95 @@
1facf9fc 33515+/*
8cdd5066 33516+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33517+ *
33518+ * This program, aufs is free software; you can redistribute it and/or modify
33519+ * it under the terms of the GNU General Public License as published by
33520+ * the Free Software Foundation; either version 2 of the License, or
33521+ * (at your option) any later version.
dece6358
AM
33522+ *
33523+ * This program is distributed in the hope that it will be useful,
33524+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33525+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33526+ * GNU General Public License for more details.
33527+ *
33528+ * You should have received a copy of the GNU General Public License
523b37e3 33529+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33530+ */
33531+
33532+/*
33533+ * workqueue for asynchronous/super-io operations
33534+ * todo: try new credentials management scheme
33535+ */
33536+
33537+#ifndef __AUFS_WKQ_H__
33538+#define __AUFS_WKQ_H__
33539+
33540+#ifdef __KERNEL__
33541+
5afbbe0d
AM
33542+#include <linux/percpu_counter.h>
33543+
dece6358
AM
33544+struct super_block;
33545+
1facf9fc 33546+/* ---------------------------------------------------------------------- */
33547+
33548+/*
33549+ * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
33550+ */
33551+struct au_nowait_tasks {
5afbbe0d 33552+ struct percpu_counter nw_len;
1facf9fc 33553+ wait_queue_head_t nw_wq;
33554+};
33555+
33556+/* ---------------------------------------------------------------------- */
33557+
33558+typedef void (*au_wkq_func_t)(void *args);
33559+
33560+/* wkq flags */
33561+#define AuWkq_WAIT 1
9dbd164d 33562+#define AuWkq_NEST (1 << 1)
1facf9fc 33563+#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
7f207e10
AM
33564+#define au_fset_wkq(flags, name) \
33565+ do { (flags) |= AuWkq_##name; } while (0)
33566+#define au_fclr_wkq(flags, name) \
33567+ do { (flags) &= ~AuWkq_##name; } while (0)
1facf9fc 33568+
9dbd164d
AM
33569+#ifndef CONFIG_AUFS_HNOTIFY
33570+#undef AuWkq_NEST
33571+#define AuWkq_NEST 0
33572+#endif
33573+
1facf9fc 33574+/* wkq.c */
b752ccd1 33575+int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
53392da6
AM
33576+int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
33577+ unsigned int flags);
1facf9fc 33578+void au_nwt_init(struct au_nowait_tasks *nwt);
5afbbe0d 33579+void au_nwt_fin(struct au_nowait_tasks *nwt);
1facf9fc 33580+int __init au_wkq_init(void);
33581+void au_wkq_fin(void);
33582+
33583+/* ---------------------------------------------------------------------- */
33584+
53392da6
AM
33585+static inline int au_wkq_test(void)
33586+{
33587+ return current->flags & PF_WQ_WORKER;
33588+}
33589+
b752ccd1 33590+static inline int au_wkq_wait(au_wkq_func_t func, void *args)
1facf9fc 33591+{
b752ccd1 33592+ return au_wkq_do_wait(AuWkq_WAIT, func, args);
1facf9fc 33593+}
33594+
33595+static inline void au_nwt_done(struct au_nowait_tasks *nwt)
33596+{
5afbbe0d
AM
33597+ percpu_counter_dec(&nwt->nw_len);
33598+ if (!percpu_counter_sum(&nwt->nw_len))
1facf9fc 33599+ wake_up_all(&nwt->nw_wq);
33600+}
33601+
33602+static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
33603+{
5afbbe0d 33604+ wait_event(nwt->nw_wq, !percpu_counter_sum(&nwt->nw_len));
1facf9fc 33605+ return 0;
33606+}
33607+
33608+#endif /* __KERNEL__ */
33609+#endif /* __AUFS_WKQ_H__ */
c1595e42
JR
33610diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
33611--- /usr/share/empty/fs/aufs/xattr.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
33612+++ linux/fs/aufs/xattr.c 2016-07-25 19:05:34.817826663 +0200
33613@@ -0,0 +1,345 @@
c1595e42 33614+/*
8cdd5066 33615+ * Copyright (C) 2014-2016 Junjiro R. Okajima
c1595e42
JR
33616+ *
33617+ * This program, aufs is free software; you can redistribute it and/or modify
33618+ * it under the terms of the GNU General Public License as published by
33619+ * the Free Software Foundation; either version 2 of the License, or
33620+ * (at your option) any later version.
33621+ *
33622+ * This program is distributed in the hope that it will be useful,
33623+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33624+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33625+ * GNU General Public License for more details.
33626+ *
33627+ * You should have received a copy of the GNU General Public License
33628+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
33629+ */
33630+
33631+/*
33632+ * handling xattr functions
33633+ */
33634+
33635+#include <linux/xattr.h>
33636+#include "aufs.h"
33637+
33638+static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
33639+{
33640+ if (!ignore_flags)
33641+ goto out;
33642+ switch (err) {
33643+ case -ENOMEM:
33644+ case -EDQUOT:
33645+ goto out;
33646+ }
33647+
33648+ if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
33649+ err = 0;
33650+ goto out;
33651+ }
33652+
33653+#define cmp(brattr, prefix) do { \
33654+ if (!strncmp(name, XATTR_##prefix##_PREFIX, \
33655+ XATTR_##prefix##_PREFIX_LEN)) { \
33656+ if (ignore_flags & AuBrAttr_ICEX_##brattr) \
33657+ err = 0; \
33658+ goto out; \
33659+ } \
33660+ } while (0)
33661+
33662+ cmp(SEC, SECURITY);
33663+ cmp(SYS, SYSTEM);
33664+ cmp(TR, TRUSTED);
33665+ cmp(USR, USER);
33666+#undef cmp
33667+
33668+ if (ignore_flags & AuBrAttr_ICEX_OTH)
33669+ err = 0;
33670+
33671+out:
33672+ return err;
33673+}
33674+
33675+static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
33676+
33677+static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
7e9cd9fe
AM
33678+ char *name, char **buf, unsigned int ignore_flags,
33679+ unsigned int verbose)
c1595e42
JR
33680+{
33681+ int err;
33682+ ssize_t ssz;
33683+ struct inode *h_idst;
33684+
33685+ ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
33686+ err = ssz;
33687+ if (unlikely(err <= 0)) {
c1595e42
JR
33688+ if (err == -ENODATA
33689+ || (err == -EOPNOTSUPP
b912730e 33690+ && ((ignore_flags & au_xattr_out_of_list)
5527c038 33691+ || (au_test_nfs_noacl(d_inode(h_src))
b912730e
AM
33692+ && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
33693+ || !strcmp(name,
33694+ XATTR_NAME_POSIX_ACL_DEFAULT))))
33695+ ))
c1595e42 33696+ err = 0;
b912730e
AM
33697+ if (err && (verbose || au_debug_test()))
33698+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
33699+ goto out;
33700+ }
33701+
33702+ /* unlock it temporary */
5527c038 33703+ h_idst = d_inode(h_dst);
febd17d6 33704+ inode_unlock(h_idst);
c1595e42 33705+ err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
febd17d6 33706+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42 33707+ if (unlikely(err)) {
7e9cd9fe
AM
33708+ if (verbose || au_debug_test())
33709+ pr_err("%s, err %d\n", name, err);
c1595e42
JR
33710+ err = au_xattr_ignore(err, name, ignore_flags);
33711+ }
33712+
33713+out:
33714+ return err;
33715+}
33716+
7e9cd9fe
AM
33717+int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
33718+ unsigned int verbose)
c1595e42
JR
33719+{
33720+ int err, unlocked, acl_access, acl_default;
33721+ ssize_t ssz;
33722+ struct inode *h_isrc, *h_idst;
33723+ char *value, *p, *o, *e;
33724+
33725+ /* try stopping to update the source inode while we are referencing */
7e9cd9fe 33726+ /* there should not be the parent-child relationship between them */
5527c038
JR
33727+ h_isrc = d_inode(h_src);
33728+ h_idst = d_inode(h_dst);
febd17d6
JR
33729+ inode_unlock(h_idst);
33730+ inode_lock_nested(h_isrc, AuLsc_I_CHILD);
33731+ inode_lock_nested(h_idst, AuLsc_I_CHILD2);
c1595e42
JR
33732+ unlocked = 0;
33733+
33734+ /* some filesystems don't list POSIX ACL, for example tmpfs */
33735+ ssz = vfs_listxattr(h_src, NULL, 0);
33736+ err = ssz;
33737+ if (unlikely(err < 0)) {
33738+ AuTraceErr(err);
33739+ if (err == -ENODATA
33740+ || err == -EOPNOTSUPP)
33741+ err = 0; /* ignore */
33742+ goto out;
33743+ }
33744+
33745+ err = 0;
33746+ p = NULL;
33747+ o = NULL;
33748+ if (ssz) {
33749+ err = -ENOMEM;
33750+ p = kmalloc(ssz, GFP_NOFS);
33751+ o = p;
33752+ if (unlikely(!p))
33753+ goto out;
33754+ err = vfs_listxattr(h_src, p, ssz);
33755+ }
febd17d6 33756+ inode_unlock(h_isrc);
c1595e42
JR
33757+ unlocked = 1;
33758+ AuDbg("err %d, ssz %zd\n", err, ssz);
33759+ if (unlikely(err < 0))
33760+ goto out_free;
33761+
33762+ err = 0;
33763+ e = p + ssz;
33764+ value = NULL;
33765+ acl_access = 0;
33766+ acl_default = 0;
33767+ while (!err && p < e) {
33768+ acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
33769+ sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
33770+ acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
33771+ sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
33772+ - 1);
7e9cd9fe
AM
33773+ err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
33774+ verbose);
c1595e42
JR
33775+ p += strlen(p) + 1;
33776+ }
33777+ AuTraceErr(err);
33778+ ignore_flags |= au_xattr_out_of_list;
33779+ if (!err && !acl_access) {
33780+ err = au_do_cpup_xattr(h_dst, h_src,
33781+ XATTR_NAME_POSIX_ACL_ACCESS, &value,
7e9cd9fe 33782+ ignore_flags, verbose);
c1595e42
JR
33783+ AuTraceErr(err);
33784+ }
33785+ if (!err && !acl_default) {
33786+ err = au_do_cpup_xattr(h_dst, h_src,
33787+ XATTR_NAME_POSIX_ACL_DEFAULT, &value,
7e9cd9fe 33788+ ignore_flags, verbose);
c1595e42
JR
33789+ AuTraceErr(err);
33790+ }
33791+
33792+ kfree(value);
33793+
33794+out_free:
33795+ kfree(o);
33796+out:
33797+ if (!unlocked)
febd17d6 33798+ inode_unlock(h_isrc);
c1595e42
JR
33799+ AuTraceErr(err);
33800+ return err;
33801+}
33802+
33803+/* ---------------------------------------------------------------------- */
33804+
33805+enum {
33806+ AU_XATTR_LIST,
33807+ AU_XATTR_GET
33808+};
33809+
33810+struct au_lgxattr {
33811+ int type;
33812+ union {
33813+ struct {
33814+ char *list;
33815+ size_t size;
33816+ } list;
33817+ struct {
33818+ const char *name;
33819+ void *value;
33820+ size_t size;
33821+ } get;
33822+ } u;
33823+};
33824+
33825+static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
33826+{
33827+ ssize_t err;
33828+ struct path h_path;
33829+ struct super_block *sb;
33830+
33831+ sb = dentry->d_sb;
33832+ err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
33833+ if (unlikely(err))
33834+ goto out;
33835+ err = au_h_path_getattr(dentry, /*force*/1, &h_path);
33836+ if (unlikely(err))
33837+ goto out_si;
33838+ if (unlikely(!h_path.dentry))
33839+ /* illegally overlapped or something */
33840+ goto out_di; /* pretending success */
33841+
33842+ /* always topmost entry only */
33843+ switch (arg->type) {
33844+ case AU_XATTR_LIST:
33845+ err = vfs_listxattr(h_path.dentry,
33846+ arg->u.list.list, arg->u.list.size);
33847+ break;
33848+ case AU_XATTR_GET:
5afbbe0d 33849+ AuDebugOn(d_is_negative(h_path.dentry));
c1595e42
JR
33850+ err = vfs_getxattr(h_path.dentry,
33851+ arg->u.get.name, arg->u.get.value,
33852+ arg->u.get.size);
33853+ break;
33854+ }
33855+
33856+out_di:
33857+ di_read_unlock(dentry, AuLock_IR);
33858+out_si:
33859+ si_read_unlock(sb);
33860+out:
33861+ AuTraceErr(err);
33862+ return err;
33863+}
33864+
33865+ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
33866+{
33867+ struct au_lgxattr arg = {
33868+ .type = AU_XATTR_LIST,
33869+ .u.list = {
33870+ .list = list,
33871+ .size = size
33872+ },
33873+ };
33874+
33875+ return au_lgxattr(dentry, &arg);
33876+}
33877+
5afbbe0d
AM
33878+ssize_t aufs_getxattr(struct dentry *dentry, struct inode *inode __maybe_unused,
33879+ const char *name, void *value, size_t size)
c1595e42
JR
33880+{
33881+ struct au_lgxattr arg = {
33882+ .type = AU_XATTR_GET,
33883+ .u.get = {
33884+ .name = name,
33885+ .value = value,
33886+ .size = size
33887+ },
33888+ };
33889+
33890+ return au_lgxattr(dentry, &arg);
33891+}
33892+
5afbbe0d
AM
33893+int aufs_setxattr(struct dentry *dentry, struct inode *inode, const char *name,
33894+ const void *value, size_t size, int flags)
c1595e42
JR
33895+{
33896+ struct au_srxattr arg = {
33897+ .type = AU_XATTR_SET,
33898+ .u.set = {
33899+ .name = name,
33900+ .value = value,
33901+ .size = size,
33902+ .flags = flags
33903+ },
33904+ };
33905+
5afbbe0d 33906+ return au_srxattr(dentry, inode, &arg);
c1595e42
JR
33907+}
33908+
33909+int aufs_removexattr(struct dentry *dentry, const char *name)
33910+{
33911+ struct au_srxattr arg = {
33912+ .type = AU_XATTR_REMOVE,
33913+ .u.remove = {
33914+ .name = name
33915+ },
33916+ };
33917+
5afbbe0d 33918+ return au_srxattr(dentry, d_inode(dentry), &arg);
c1595e42
JR
33919+}
33920+
33921+/* ---------------------------------------------------------------------- */
33922+
33923+#if 0
33924+static size_t au_xattr_list(struct dentry *dentry, char *list, size_t list_size,
33925+ const char *name, size_t name_len, int type)
33926+{
33927+ return aufs_listxattr(dentry, list, list_size);
33928+}
33929+
33930+static int au_xattr_get(struct dentry *dentry, const char *name, void *buffer,
33931+ size_t size, int type)
33932+{
33933+ return aufs_getxattr(dentry, name, buffer, size);
33934+}
33935+
33936+static int au_xattr_set(struct dentry *dentry, const char *name,
33937+ const void *value, size_t size, int flags, int type)
33938+{
33939+ return aufs_setxattr(dentry, name, value, size, flags);
33940+}
33941+
33942+static const struct xattr_handler au_xattr_handler = {
33943+ /* no prefix, no flags */
33944+ .list = au_xattr_list,
33945+ .get = au_xattr_get,
33946+ .set = au_xattr_set
33947+ /* why no remove? */
33948+};
33949+
33950+static const struct xattr_handler *au_xattr_handlers[] = {
33951+ &au_xattr_handler
33952+};
33953+
33954+void au_xattr_init(struct super_block *sb)
33955+{
33956+ /* sb->s_xattr = au_xattr_handlers; */
33957+}
33958+#endif
7f207e10
AM
33959diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
33960--- /usr/share/empty/fs/aufs/xino.c 1970-01-01 01:00:00.000000000 +0100
5afbbe0d
AM
33961+++ linux/fs/aufs/xino.c 2016-07-25 19:05:34.817826663 +0200
33962@@ -0,0 +1,1317 @@
1facf9fc 33963+/*
8cdd5066 33964+ * Copyright (C) 2005-2016 Junjiro R. Okajima
1facf9fc 33965+ *
33966+ * This program, aufs is free software; you can redistribute it and/or modify
33967+ * it under the terms of the GNU General Public License as published by
33968+ * the Free Software Foundation; either version 2 of the License, or
33969+ * (at your option) any later version.
dece6358
AM
33970+ *
33971+ * This program is distributed in the hope that it will be useful,
33972+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
33973+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33974+ * GNU General Public License for more details.
33975+ *
33976+ * You should have received a copy of the GNU General Public License
523b37e3 33977+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
1facf9fc 33978+ */
33979+
33980+/*
33981+ * external inode number translation table and bitmap
33982+ */
33983+
33984+#include <linux/seq_file.h>
392086de 33985+#include <linux/statfs.h>
1facf9fc 33986+#include "aufs.h"
33987+
9dbd164d 33988+/* todo: unnecessary to support mmap_sem since kernel-space? */
5527c038 33989+ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
1facf9fc 33990+ loff_t *pos)
33991+{
33992+ ssize_t err;
33993+ mm_segment_t oldfs;
b752ccd1
AM
33994+ union {
33995+ void *k;
33996+ char __user *u;
33997+ } buf;
1facf9fc 33998+
b752ccd1 33999+ buf.k = kbuf;
1facf9fc 34000+ oldfs = get_fs();
34001+ set_fs(KERNEL_DS);
34002+ do {
34003+ /* todo: signal_pending? */
b752ccd1 34004+ err = func(file, buf.u, size, pos);
1facf9fc 34005+ } while (err == -EAGAIN || err == -EINTR);
34006+ set_fs(oldfs);
34007+
34008+#if 0 /* reserved for future use */
34009+ if (err > 0)
2000de60 34010+ fsnotify_access(file->f_path.dentry);
1facf9fc 34011+#endif
34012+
34013+ return err;
34014+}
34015+
34016+/* ---------------------------------------------------------------------- */
34017+
be52b249
AM
34018+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34019+ size_t size, loff_t *pos);
34020+
5527c038 34021+static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
1facf9fc 34022+ size_t size, loff_t *pos)
34023+{
34024+ ssize_t err;
34025+ mm_segment_t oldfs;
b752ccd1
AM
34026+ union {
34027+ void *k;
34028+ const char __user *u;
34029+ } buf;
be52b249
AM
34030+ int i;
34031+ const int prevent_endless = 10;
1facf9fc 34032+
be52b249 34033+ i = 0;
b752ccd1 34034+ buf.k = kbuf;
1facf9fc 34035+ oldfs = get_fs();
34036+ set_fs(KERNEL_DS);
1facf9fc 34037+ do {
b752ccd1 34038+ err = func(file, buf.u, size, pos);
be52b249
AM
34039+ if (err == -EINTR
34040+ && !au_wkq_test()
34041+ && fatal_signal_pending(current)) {
34042+ set_fs(oldfs);
34043+ err = xino_fwrite_wkq(func, file, kbuf, size, pos);
34044+ BUG_ON(err == -EINTR);
34045+ oldfs = get_fs();
34046+ set_fs(KERNEL_DS);
34047+ }
34048+ } while (i++ < prevent_endless
34049+ && (err == -EAGAIN || err == -EINTR));
1facf9fc 34050+ set_fs(oldfs);
34051+
34052+#if 0 /* reserved for future use */
34053+ if (err > 0)
2000de60 34054+ fsnotify_modify(file->f_path.dentry);
1facf9fc 34055+#endif
34056+
34057+ return err;
34058+}
34059+
34060+struct do_xino_fwrite_args {
34061+ ssize_t *errp;
5527c038 34062+ vfs_writef_t func;
1facf9fc 34063+ struct file *file;
34064+ void *buf;
34065+ size_t size;
34066+ loff_t *pos;
34067+};
34068+
34069+static void call_do_xino_fwrite(void *args)
34070+{
34071+ struct do_xino_fwrite_args *a = args;
34072+ *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
34073+}
34074+
be52b249
AM
34075+static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
34076+ size_t size, loff_t *pos)
34077+{
34078+ ssize_t err;
34079+ int wkq_err;
34080+ struct do_xino_fwrite_args args = {
34081+ .errp = &err,
34082+ .func = func,
34083+ .file = file,
34084+ .buf = buf,
34085+ .size = size,
34086+ .pos = pos
34087+ };
34088+
34089+ /*
34090+ * it breaks RLIMIT_FSIZE and normal user's limit,
34091+ * users should care about quota and real 'filesystem full.'
34092+ */
34093+ wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
34094+ if (unlikely(wkq_err))
34095+ err = wkq_err;
34096+
34097+ return err;
34098+}
34099+
5527c038
JR
34100+ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
34101+ size_t size, loff_t *pos)
1facf9fc 34102+{
34103+ ssize_t err;
34104+
b752ccd1
AM
34105+ if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
34106+ lockdep_off();
34107+ err = do_xino_fwrite(func, file, buf, size, pos);
34108+ lockdep_on();
be52b249
AM
34109+ } else
34110+ err = xino_fwrite_wkq(func, file, buf, size, pos);
1facf9fc 34111+
34112+ return err;
34113+}
34114+
34115+/* ---------------------------------------------------------------------- */
34116+
34117+/*
34118+ * create a new xinofile at the same place/path as @base_file.
34119+ */
34120+struct file *au_xino_create2(struct file *base_file, struct file *copy_src)
34121+{
34122+ struct file *file;
4a4d8108 34123+ struct dentry *base, *parent;
523b37e3 34124+ struct inode *dir, *delegated;
1facf9fc 34125+ struct qstr *name;
1308ab2a 34126+ struct path path;
4a4d8108 34127+ int err;
1facf9fc 34128+
2000de60 34129+ base = base_file->f_path.dentry;
1facf9fc 34130+ parent = base->d_parent; /* dir inode is locked */
5527c038 34131+ dir = d_inode(parent);
1facf9fc 34132+ IMustLock(dir);
34133+
34134+ file = ERR_PTR(-EINVAL);
34135+ name = &base->d_name;
4a4d8108
AM
34136+ path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
34137+ if (IS_ERR(path.dentry)) {
34138+ file = (void *)path.dentry;
523b37e3
AM
34139+ pr_err("%pd lookup err %ld\n",
34140+ base, PTR_ERR(path.dentry));
1facf9fc 34141+ goto out;
34142+ }
34143+
34144+ /* no need to mnt_want_write() since we call dentry_open() later */
4a4d8108 34145+ err = vfs_create(dir, path.dentry, S_IRUGO | S_IWUGO, NULL);
1facf9fc 34146+ if (unlikely(err)) {
34147+ file = ERR_PTR(err);
523b37e3 34148+ pr_err("%pd create err %d\n", base, err);
1facf9fc 34149+ goto out_dput;
34150+ }
34151+
c06a8ce3 34152+ path.mnt = base_file->f_path.mnt;
4a4d8108 34153+ file = vfsub_dentry_open(&path,
7f207e10 34154+ O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 34155+ /* | __FMODE_NONOTIFY */);
1facf9fc 34156+ if (IS_ERR(file)) {
523b37e3 34157+ pr_err("%pd open err %ld\n", base, PTR_ERR(file));
1facf9fc 34158+ goto out_dput;
34159+ }
34160+
523b37e3
AM
34161+ delegated = NULL;
34162+ err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
34163+ if (unlikely(err == -EWOULDBLOCK)) {
34164+ pr_warn("cannot retry for NFSv4 delegation"
34165+ " for an internal unlink\n");
34166+ iput(delegated);
34167+ }
1facf9fc 34168+ if (unlikely(err)) {
523b37e3 34169+ pr_err("%pd unlink err %d\n", base, err);
1facf9fc 34170+ goto out_fput;
34171+ }
34172+
34173+ if (copy_src) {
34174+ /* no one can touch copy_src xino */
c06a8ce3 34175+ err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
1facf9fc 34176+ if (unlikely(err)) {
523b37e3 34177+ pr_err("%pd copy err %d\n", base, err);
1facf9fc 34178+ goto out_fput;
34179+ }
34180+ }
34181+ goto out_dput; /* success */
34182+
4f0767ce 34183+out_fput:
1facf9fc 34184+ fput(file);
34185+ file = ERR_PTR(err);
4f0767ce 34186+out_dput:
4a4d8108 34187+ dput(path.dentry);
4f0767ce 34188+out:
1facf9fc 34189+ return file;
34190+}
34191+
34192+struct au_xino_lock_dir {
34193+ struct au_hinode *hdir;
34194+ struct dentry *parent;
febd17d6 34195+ struct inode *dir;
1facf9fc 34196+};
34197+
34198+static void au_xino_lock_dir(struct super_block *sb, struct file *xino,
34199+ struct au_xino_lock_dir *ldir)
34200+{
34201+ aufs_bindex_t brid, bindex;
34202+
34203+ ldir->hdir = NULL;
34204+ bindex = -1;
34205+ brid = au_xino_brid(sb);
34206+ if (brid >= 0)
34207+ bindex = au_br_index(sb, brid);
34208+ if (bindex >= 0) {
5527c038 34209+ ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
5afbbe0d 34210+ au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
1facf9fc 34211+ } else {
2000de60 34212+ ldir->parent = dget_parent(xino->f_path.dentry);
febd17d6
JR
34213+ ldir->dir = d_inode(ldir->parent);
34214+ inode_lock_nested(ldir->dir, AuLsc_I_PARENT);
1facf9fc 34215+ }
34216+}
34217+
34218+static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
34219+{
34220+ if (ldir->hdir)
5afbbe0d 34221+ au_hn_inode_unlock(ldir->hdir);
1facf9fc 34222+ else {
febd17d6 34223+ inode_unlock(ldir->dir);
1facf9fc 34224+ dput(ldir->parent);
34225+ }
34226+}
34227+
34228+/* ---------------------------------------------------------------------- */
34229+
34230+/* trucate xino files asynchronously */
34231+
34232+int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex)
34233+{
34234+ int err;
392086de
AM
34235+ unsigned long jiffy;
34236+ blkcnt_t blocks;
5afbbe0d 34237+ aufs_bindex_t bi, bbot;
392086de 34238+ struct kstatfs *st;
1facf9fc 34239+ struct au_branch *br;
34240+ struct file *new_xino, *file;
34241+ struct super_block *h_sb;
34242+ struct au_xino_lock_dir ldir;
34243+
392086de 34244+ err = -ENOMEM;
be52b249 34245+ st = kmalloc(sizeof(*st), GFP_NOFS);
392086de
AM
34246+ if (unlikely(!st))
34247+ goto out;
34248+
1facf9fc 34249+ err = -EINVAL;
5afbbe0d
AM
34250+ bbot = au_sbbot(sb);
34251+ if (unlikely(bindex < 0 || bbot < bindex))
392086de 34252+ goto out_st;
1facf9fc 34253+ br = au_sbr(sb, bindex);
34254+ file = br->br_xino.xi_file;
34255+ if (!file)
392086de
AM
34256+ goto out_st;
34257+
34258+ err = vfs_statfs(&file->f_path, st);
34259+ if (unlikely(err))
34260+ AuErr1("statfs err %d, ignored\n", err);
34261+ jiffy = jiffies;
34262+ blocks = file_inode(file)->i_blocks;
34263+ pr_info("begin truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34264+ bindex, (u64)blocks, st->f_bfree, st->f_blocks);
1facf9fc 34265+
34266+ au_xino_lock_dir(sb, file, &ldir);
34267+ /* mnt_want_write() is unnecessary here */
34268+ new_xino = au_xino_create2(file, file);
34269+ au_xino_unlock_dir(&ldir);
34270+ err = PTR_ERR(new_xino);
392086de
AM
34271+ if (IS_ERR(new_xino)) {
34272+ pr_err("err %d, ignored\n", err);
34273+ goto out_st;
34274+ }
1facf9fc 34275+ err = 0;
34276+ fput(file);
34277+ br->br_xino.xi_file = new_xino;
34278+
86dc4139 34279+ h_sb = au_br_sb(br);
5afbbe0d 34280+ for (bi = 0; bi <= bbot; bi++) {
1facf9fc 34281+ if (unlikely(bi == bindex))
34282+ continue;
34283+ br = au_sbr(sb, bi);
86dc4139 34284+ if (au_br_sb(br) != h_sb)
1facf9fc 34285+ continue;
34286+
34287+ fput(br->br_xino.xi_file);
34288+ br->br_xino.xi_file = new_xino;
34289+ get_file(new_xino);
34290+ }
34291+
392086de
AM
34292+ err = vfs_statfs(&new_xino->f_path, st);
34293+ if (!err) {
34294+ pr_info("end truncating xino(b%d), ib%llu, %llu/%llu free blks\n",
34295+ bindex, (u64)file_inode(new_xino)->i_blocks,
34296+ st->f_bfree, st->f_blocks);
34297+ if (file_inode(new_xino)->i_blocks < blocks)
34298+ au_sbi(sb)->si_xino_jiffy = jiffy;
34299+ } else
34300+ AuErr1("statfs err %d, ignored\n", err);
34301+
34302+out_st:
34303+ kfree(st);
4f0767ce 34304+out:
1facf9fc 34305+ return err;
34306+}
34307+
34308+struct xino_do_trunc_args {
34309+ struct super_block *sb;
34310+ struct au_branch *br;
34311+};
34312+
34313+static void xino_do_trunc(void *_args)
34314+{
34315+ struct xino_do_trunc_args *args = _args;
34316+ struct super_block *sb;
34317+ struct au_branch *br;
34318+ struct inode *dir;
34319+ int err;
34320+ aufs_bindex_t bindex;
34321+
34322+ err = 0;
34323+ sb = args->sb;
5527c038 34324+ dir = d_inode(sb->s_root);
1facf9fc 34325+ br = args->br;
34326+
34327+ si_noflush_write_lock(sb);
34328+ ii_read_lock_parent(dir);
34329+ bindex = au_br_index(sb, br->br_id);
34330+ err = au_xino_trunc(sb, bindex);
1facf9fc 34331+ ii_read_unlock(dir);
34332+ if (unlikely(err))
392086de 34333+ pr_warn("err b%d, (%d)\n", bindex, err);
1facf9fc 34334+ atomic_dec(&br->br_xino_running);
5afbbe0d 34335+ au_br_put(br);
1facf9fc 34336+ si_write_unlock(sb);
027c5e7a 34337+ au_nwt_done(&au_sbi(sb)->si_nowait);
1facf9fc 34338+ kfree(args);
34339+}
34340+
392086de
AM
34341+static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
34342+{
34343+ int err;
34344+ struct kstatfs st;
34345+ struct au_sbinfo *sbinfo;
34346+
34347+ /* todo: si_xino_expire and the ratio should be customizable */
34348+ sbinfo = au_sbi(sb);
34349+ if (time_before(jiffies,
34350+ sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
34351+ return 0;
34352+
34353+ /* truncation border */
34354+ err = vfs_statfs(&br->br_xino.xi_file->f_path, &st);
34355+ if (unlikely(err)) {
34356+ AuErr1("statfs err %d, ignored\n", err);
34357+ return 0;
34358+ }
34359+ if (div64_u64(st.f_bfree * 100, st.f_blocks) >= AUFS_XINO_DEF_TRUNC)
34360+ return 0;
34361+
34362+ return 1;
34363+}
34364+
1facf9fc 34365+static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
34366+{
34367+ struct xino_do_trunc_args *args;
34368+ int wkq_err;
34369+
392086de 34370+ if (!xino_trunc_test(sb, br))
1facf9fc 34371+ return;
34372+
34373+ if (atomic_inc_return(&br->br_xino_running) > 1)
34374+ goto out;
34375+
34376+ /* lock and kfree() will be called in trunc_xino() */
34377+ args = kmalloc(sizeof(*args), GFP_NOFS);
34378+ if (unlikely(!args)) {
34379+ AuErr1("no memory\n");
34380+ goto out_args;
34381+ }
34382+
5afbbe0d 34383+ au_br_get(br);
1facf9fc 34384+ args->sb = sb;
34385+ args->br = br;
53392da6 34386+ wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
1facf9fc 34387+ if (!wkq_err)
34388+ return; /* success */
34389+
4a4d8108 34390+ pr_err("wkq %d\n", wkq_err);
5afbbe0d 34391+ au_br_put(br);
1facf9fc 34392+
4f0767ce 34393+out_args:
1facf9fc 34394+ kfree(args);
4f0767ce 34395+out:
e49829fe 34396+ atomic_dec(&br->br_xino_running);
1facf9fc 34397+}
34398+
34399+/* ---------------------------------------------------------------------- */
34400+
5527c038 34401+static int au_xino_do_write(vfs_writef_t write, struct file *file,
1facf9fc 34402+ ino_t h_ino, ino_t ino)
34403+{
34404+ loff_t pos;
34405+ ssize_t sz;
34406+
34407+ pos = h_ino;
34408+ if (unlikely(au_loff_max / sizeof(ino) - 1 < pos)) {
34409+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34410+ return -EFBIG;
34411+ }
34412+ pos *= sizeof(ino);
34413+ sz = xino_fwrite(write, file, &ino, sizeof(ino), &pos);
34414+ if (sz == sizeof(ino))
34415+ return 0; /* success */
34416+
34417+ AuIOErr("write failed (%zd)\n", sz);
34418+ return -EIO;
34419+}
34420+
34421+/*
34422+ * write @ino to the xinofile for the specified branch{@sb, @bindex}
34423+ * at the position of @h_ino.
34424+ * even if @ino is zero, it is written to the xinofile and means no entry.
34425+ * if the size of the xino file on a specific filesystem exceeds the watermark,
34426+ * try truncating it.
34427+ */
34428+int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34429+ ino_t ino)
34430+{
34431+ int err;
34432+ unsigned int mnt_flags;
34433+ struct au_branch *br;
34434+
34435+ BUILD_BUG_ON(sizeof(long long) != sizeof(au_loff_max)
34436+ || ((loff_t)-1) > 0);
dece6358 34437+ SiMustAnyLock(sb);
1facf9fc 34438+
34439+ mnt_flags = au_mntflags(sb);
34440+ if (!au_opt_test(mnt_flags, XINO))
34441+ return 0;
34442+
34443+ br = au_sbr(sb, bindex);
34444+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34445+ h_ino, ino);
34446+ if (!err) {
34447+ if (au_opt_test(mnt_flags, TRUNC_XINO)
86dc4139 34448+ && au_test_fs_trunc_xino(au_br_sb(br)))
1facf9fc 34449+ xino_try_trunc(sb, br);
34450+ return 0; /* success */
34451+ }
34452+
34453+ AuIOErr("write failed (%d)\n", err);
34454+ return -EIO;
34455+}
34456+
34457+/* ---------------------------------------------------------------------- */
34458+
34459+/* aufs inode number bitmap */
34460+
34461+static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
34462+static ino_t xib_calc_ino(unsigned long pindex, int bit)
34463+{
34464+ ino_t ino;
34465+
34466+ AuDebugOn(bit < 0 || page_bits <= bit);
34467+ ino = AUFS_FIRST_INO + pindex * page_bits + bit;
34468+ return ino;
34469+}
34470+
34471+static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
34472+{
34473+ AuDebugOn(ino < AUFS_FIRST_INO);
34474+ ino -= AUFS_FIRST_INO;
34475+ *pindex = ino / page_bits;
34476+ *bit = ino % page_bits;
34477+}
34478+
34479+static int xib_pindex(struct super_block *sb, unsigned long pindex)
34480+{
34481+ int err;
34482+ loff_t pos;
34483+ ssize_t sz;
34484+ struct au_sbinfo *sbinfo;
34485+ struct file *xib;
34486+ unsigned long *p;
34487+
34488+ sbinfo = au_sbi(sb);
34489+ MtxMustLock(&sbinfo->si_xib_mtx);
34490+ AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
34491+ || !au_opt_test(sbinfo->si_mntflags, XINO));
34492+
34493+ if (pindex == sbinfo->si_xib_last_pindex)
34494+ return 0;
34495+
34496+ xib = sbinfo->si_xib;
34497+ p = sbinfo->si_xib_buf;
34498+ pos = sbinfo->si_xib_last_pindex;
34499+ pos *= PAGE_SIZE;
34500+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34501+ if (unlikely(sz != PAGE_SIZE))
34502+ goto out;
34503+
34504+ pos = pindex;
34505+ pos *= PAGE_SIZE;
c06a8ce3 34506+ if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
1facf9fc 34507+ sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
34508+ else {
34509+ memset(p, 0, PAGE_SIZE);
34510+ sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
34511+ }
34512+ if (sz == PAGE_SIZE) {
34513+ sbinfo->si_xib_last_pindex = pindex;
34514+ return 0; /* success */
34515+ }
34516+
4f0767ce 34517+out:
b752ccd1
AM
34518+ AuIOErr1("write failed (%zd)\n", sz);
34519+ err = sz;
34520+ if (sz >= 0)
34521+ err = -EIO;
34522+ return err;
34523+}
34524+
34525+/* ---------------------------------------------------------------------- */
34526+
34527+static void au_xib_clear_bit(struct inode *inode)
34528+{
34529+ int err, bit;
34530+ unsigned long pindex;
34531+ struct super_block *sb;
34532+ struct au_sbinfo *sbinfo;
34533+
34534+ AuDebugOn(inode->i_nlink);
34535+
34536+ sb = inode->i_sb;
34537+ xib_calc_bit(inode->i_ino, &pindex, &bit);
34538+ AuDebugOn(page_bits <= bit);
34539+ sbinfo = au_sbi(sb);
34540+ mutex_lock(&sbinfo->si_xib_mtx);
34541+ err = xib_pindex(sb, pindex);
34542+ if (!err) {
34543+ clear_bit(bit, sbinfo->si_xib_buf);
34544+ sbinfo->si_xib_next_bit = bit;
34545+ }
34546+ mutex_unlock(&sbinfo->si_xib_mtx);
34547+}
34548+
34549+/* for s_op->delete_inode() */
34550+void au_xino_delete_inode(struct inode *inode, const int unlinked)
34551+{
34552+ int err;
34553+ unsigned int mnt_flags;
5afbbe0d 34554+ aufs_bindex_t bindex, bbot, bi;
b752ccd1
AM
34555+ unsigned char try_trunc;
34556+ struct au_iinfo *iinfo;
34557+ struct super_block *sb;
34558+ struct au_hinode *hi;
34559+ struct inode *h_inode;
34560+ struct au_branch *br;
5527c038 34561+ vfs_writef_t xwrite;
b752ccd1 34562+
5afbbe0d
AM
34563+ AuDebugOn(au_is_bad_inode(inode));
34564+
b752ccd1
AM
34565+ sb = inode->i_sb;
34566+ mnt_flags = au_mntflags(sb);
34567+ if (!au_opt_test(mnt_flags, XINO)
34568+ || inode->i_ino == AUFS_ROOT_INO)
34569+ return;
34570+
34571+ if (unlinked) {
34572+ au_xigen_inc(inode);
34573+ au_xib_clear_bit(inode);
34574+ }
34575+
34576+ iinfo = au_ii(inode);
5afbbe0d 34577+ bindex = iinfo->ii_btop;
b752ccd1
AM
34578+ if (bindex < 0)
34579+ return;
1facf9fc 34580+
b752ccd1
AM
34581+ xwrite = au_sbi(sb)->si_xwrite;
34582+ try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
5afbbe0d
AM
34583+ hi = au_hinode(iinfo, bindex);
34584+ bbot = iinfo->ii_bbot;
34585+ for (; bindex <= bbot; bindex++, hi++) {
b752ccd1
AM
34586+ h_inode = hi->hi_inode;
34587+ if (!h_inode
34588+ || (!unlinked && h_inode->i_nlink))
34589+ continue;
1facf9fc 34590+
b752ccd1
AM
34591+ /* inode may not be revalidated */
34592+ bi = au_br_index(sb, hi->hi_id);
34593+ if (bi < 0)
34594+ continue;
1facf9fc 34595+
b752ccd1
AM
34596+ br = au_sbr(sb, bi);
34597+ err = au_xino_do_write(xwrite, br->br_xino.xi_file,
34598+ h_inode->i_ino, /*ino*/0);
34599+ if (!err && try_trunc
86dc4139 34600+ && au_test_fs_trunc_xino(au_br_sb(br)))
b752ccd1 34601+ xino_try_trunc(sb, br);
1facf9fc 34602+ }
1facf9fc 34603+}
34604+
34605+/* get an unused inode number from bitmap */
34606+ino_t au_xino_new_ino(struct super_block *sb)
34607+{
34608+ ino_t ino;
34609+ unsigned long *p, pindex, ul, pend;
34610+ struct au_sbinfo *sbinfo;
34611+ struct file *file;
34612+ int free_bit, err;
34613+
34614+ if (!au_opt_test(au_mntflags(sb), XINO))
34615+ return iunique(sb, AUFS_FIRST_INO);
34616+
34617+ sbinfo = au_sbi(sb);
34618+ mutex_lock(&sbinfo->si_xib_mtx);
34619+ p = sbinfo->si_xib_buf;
34620+ free_bit = sbinfo->si_xib_next_bit;
34621+ if (free_bit < page_bits && !test_bit(free_bit, p))
34622+ goto out; /* success */
34623+ free_bit = find_first_zero_bit(p, page_bits);
34624+ if (free_bit < page_bits)
34625+ goto out; /* success */
34626+
34627+ pindex = sbinfo->si_xib_last_pindex;
34628+ for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
34629+ err = xib_pindex(sb, ul);
34630+ if (unlikely(err))
34631+ goto out_err;
34632+ free_bit = find_first_zero_bit(p, page_bits);
34633+ if (free_bit < page_bits)
34634+ goto out; /* success */
34635+ }
34636+
34637+ file = sbinfo->si_xib;
c06a8ce3 34638+ pend = vfsub_f_size_read(file) / PAGE_SIZE;
1facf9fc 34639+ for (ul = pindex + 1; ul <= pend; ul++) {
34640+ err = xib_pindex(sb, ul);
34641+ if (unlikely(err))
34642+ goto out_err;
34643+ free_bit = find_first_zero_bit(p, page_bits);
34644+ if (free_bit < page_bits)
34645+ goto out; /* success */
34646+ }
34647+ BUG();
34648+
4f0767ce 34649+out:
1facf9fc 34650+ set_bit(free_bit, p);
7f207e10 34651+ sbinfo->si_xib_next_bit = free_bit + 1;
1facf9fc 34652+ pindex = sbinfo->si_xib_last_pindex;
34653+ mutex_unlock(&sbinfo->si_xib_mtx);
34654+ ino = xib_calc_ino(pindex, free_bit);
34655+ AuDbg("i%lu\n", (unsigned long)ino);
34656+ return ino;
4f0767ce 34657+out_err:
1facf9fc 34658+ mutex_unlock(&sbinfo->si_xib_mtx);
34659+ AuDbg("i0\n");
34660+ return 0;
34661+}
34662+
34663+/*
34664+ * read @ino from xinofile for the specified branch{@sb, @bindex}
34665+ * at the position of @h_ino.
34666+ * if @ino does not exist and @do_new is true, get new one.
34667+ */
34668+int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
34669+ ino_t *ino)
34670+{
34671+ int err;
34672+ ssize_t sz;
34673+ loff_t pos;
34674+ struct file *file;
34675+ struct au_sbinfo *sbinfo;
34676+
34677+ *ino = 0;
34678+ if (!au_opt_test(au_mntflags(sb), XINO))
34679+ return 0; /* no xino */
34680+
34681+ err = 0;
34682+ sbinfo = au_sbi(sb);
34683+ pos = h_ino;
34684+ if (unlikely(au_loff_max / sizeof(*ino) - 1 < pos)) {
34685+ AuIOErr1("too large hi%lu\n", (unsigned long)h_ino);
34686+ return -EFBIG;
34687+ }
34688+ pos *= sizeof(*ino);
34689+
34690+ file = au_sbr(sb, bindex)->br_xino.xi_file;
c06a8ce3 34691+ if (vfsub_f_size_read(file) < pos + sizeof(*ino))
1facf9fc 34692+ return 0; /* no ino */
34693+
34694+ sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &pos);
34695+ if (sz == sizeof(*ino))
34696+ return 0; /* success */
34697+
34698+ err = sz;
34699+ if (unlikely(sz >= 0)) {
34700+ err = -EIO;
34701+ AuIOErr("xino read error (%zd)\n", sz);
34702+ }
34703+
34704+ return err;
34705+}
34706+
34707+/* ---------------------------------------------------------------------- */
34708+
34709+/* create and set a new xino file */
34710+
34711+struct file *au_xino_create(struct super_block *sb, char *fname, int silent)
34712+{
34713+ struct file *file;
34714+ struct dentry *h_parent, *d;
b912730e 34715+ struct inode *h_dir, *inode;
1facf9fc 34716+ int err;
34717+
34718+ /*
34719+ * at mount-time, and the xino file is the default path,
4a4d8108 34720+ * hnotify is disabled so we have no notify events to ignore.
1facf9fc 34721+ * when a user specified the xino, we cannot get au_hdir to be ignored.
34722+ */
7f207e10 34723+ file = vfsub_filp_open(fname, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
2cbb1c4b 34724+ /* | __FMODE_NONOTIFY */,
1facf9fc 34725+ S_IRUGO | S_IWUGO);
34726+ if (IS_ERR(file)) {
34727+ if (!silent)
4a4d8108 34728+ pr_err("open %s(%ld)\n", fname, PTR_ERR(file));
1facf9fc 34729+ return file;
34730+ }
34731+
34732+ /* keep file count */
b912730e
AM
34733+ err = 0;
34734+ inode = file_inode(file);
2000de60 34735+ h_parent = dget_parent(file->f_path.dentry);
5527c038 34736+ h_dir = d_inode(h_parent);
febd17d6 34737+ inode_lock_nested(h_dir, AuLsc_I_PARENT);
1facf9fc 34738+ /* mnt_want_write() is unnecessary here */
523b37e3 34739+ /* no delegation since it is just created */
b912730e
AM
34740+ if (inode->i_nlink)
34741+ err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
34742+ /*force*/0);
febd17d6 34743+ inode_unlock(h_dir);
1facf9fc 34744+ dput(h_parent);
34745+ if (unlikely(err)) {
34746+ if (!silent)
4a4d8108 34747+ pr_err("unlink %s(%d)\n", fname, err);
1facf9fc 34748+ goto out;
34749+ }
34750+
34751+ err = -EINVAL;
2000de60 34752+ d = file->f_path.dentry;
1facf9fc 34753+ if (unlikely(sb == d->d_sb)) {
34754+ if (!silent)
4a4d8108 34755+ pr_err("%s must be outside\n", fname);
1facf9fc 34756+ goto out;
34757+ }
34758+ if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
34759+ if (!silent)
4a4d8108
AM
34760+ pr_err("xino doesn't support %s(%s)\n",
34761+ fname, au_sbtype(d->d_sb));
1facf9fc 34762+ goto out;
34763+ }
34764+ return file; /* success */
34765+
4f0767ce 34766+out:
1facf9fc 34767+ fput(file);
34768+ file = ERR_PTR(err);
34769+ return file;
34770+}
34771+
34772+/*
34773+ * find another branch who is on the same filesystem of the specified
5afbbe0d 34774+ * branch{@btgt}. search until @bbot.
1facf9fc 34775+ */
34776+static int is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
5afbbe0d 34777+ aufs_bindex_t bbot)
1facf9fc 34778+{
34779+ aufs_bindex_t bindex;
34780+ struct super_block *tgt_sb = au_sbr_sb(sb, btgt);
34781+
34782+ for (bindex = 0; bindex < btgt; bindex++)
34783+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34784+ return bindex;
5afbbe0d 34785+ for (bindex++; bindex <= bbot; bindex++)
1facf9fc 34786+ if (unlikely(tgt_sb == au_sbr_sb(sb, bindex)))
34787+ return bindex;
34788+ return -1;
34789+}
34790+
34791+/* ---------------------------------------------------------------------- */
34792+
34793+/*
34794+ * initialize the xinofile for the specified branch @br
34795+ * at the place/path where @base_file indicates.
34796+ * test whether another branch is on the same filesystem or not,
34797+ * if @do_test is true.
34798+ */
34799+int au_xino_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
34800+ struct file *base_file, int do_test)
34801+{
34802+ int err;
34803+ ino_t ino;
5afbbe0d 34804+ aufs_bindex_t bbot, bindex;
1facf9fc 34805+ struct au_branch *shared_br, *b;
34806+ struct file *file;
34807+ struct super_block *tgt_sb;
34808+
34809+ shared_br = NULL;
5afbbe0d 34810+ bbot = au_sbbot(sb);
1facf9fc 34811+ if (do_test) {
86dc4139 34812+ tgt_sb = au_br_sb(br);
5afbbe0d 34813+ for (bindex = 0; bindex <= bbot; bindex++) {
1facf9fc 34814+ b = au_sbr(sb, bindex);
86dc4139 34815+ if (tgt_sb == au_br_sb(b)) {
1facf9fc 34816+ shared_br = b;
34817+ break;
34818+ }
34819+ }
34820+ }
34821+
34822+ if (!shared_br || !shared_br->br_xino.xi_file) {
34823+ struct au_xino_lock_dir ldir;
34824+
34825+ au_xino_lock_dir(sb, base_file, &ldir);
34826+ /* mnt_want_write() is unnecessary here */
34827+ file = au_xino_create2(base_file, NULL);
34828+ au_xino_unlock_dir(&ldir);
34829+ err = PTR_ERR(file);
34830+ if (IS_ERR(file))
34831+ goto out;
34832+ br->br_xino.xi_file = file;
34833+ } else {
34834+ br->br_xino.xi_file = shared_br->br_xino.xi_file;
34835+ get_file(br->br_xino.xi_file);
34836+ }
34837+
34838+ ino = AUFS_ROOT_INO;
34839+ err = au_xino_do_write(au_sbi(sb)->si_xwrite, br->br_xino.xi_file,
34840+ h_ino, ino);
b752ccd1
AM
34841+ if (unlikely(err)) {
34842+ fput(br->br_xino.xi_file);
34843+ br->br_xino.xi_file = NULL;
34844+ }
1facf9fc 34845+
4f0767ce 34846+out:
1facf9fc 34847+ return err;
34848+}
34849+
34850+/* ---------------------------------------------------------------------- */
34851+
34852+/* trucate a xino bitmap file */
34853+
34854+/* todo: slow */
34855+static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
34856+{
34857+ int err, bit;
34858+ ssize_t sz;
34859+ unsigned long pindex;
34860+ loff_t pos, pend;
34861+ struct au_sbinfo *sbinfo;
5527c038 34862+ vfs_readf_t func;
1facf9fc 34863+ ino_t *ino;
34864+ unsigned long *p;
34865+
34866+ err = 0;
34867+ sbinfo = au_sbi(sb);
dece6358 34868+ MtxMustLock(&sbinfo->si_xib_mtx);
1facf9fc 34869+ p = sbinfo->si_xib_buf;
34870+ func = sbinfo->si_xread;
c06a8ce3 34871+ pend = vfsub_f_size_read(file);
1facf9fc 34872+ pos = 0;
34873+ while (pos < pend) {
34874+ sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
34875+ err = sz;
34876+ if (unlikely(sz <= 0))
34877+ goto out;
34878+
34879+ err = 0;
34880+ for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
34881+ if (unlikely(*ino < AUFS_FIRST_INO))
34882+ continue;
34883+
34884+ xib_calc_bit(*ino, &pindex, &bit);
34885+ AuDebugOn(page_bits <= bit);
34886+ err = xib_pindex(sb, pindex);
34887+ if (!err)
34888+ set_bit(bit, p);
34889+ else
34890+ goto out;
34891+ }
34892+ }
34893+
4f0767ce 34894+out:
1facf9fc 34895+ return err;
34896+}
34897+
34898+static int xib_restore(struct super_block *sb)
34899+{
34900+ int err;
5afbbe0d 34901+ aufs_bindex_t bindex, bbot;
1facf9fc 34902+ void *page;
34903+
34904+ err = -ENOMEM;
34905+ page = (void *)__get_free_page(GFP_NOFS);
34906+ if (unlikely(!page))
34907+ goto out;
34908+
34909+ err = 0;
5afbbe0d
AM
34910+ bbot = au_sbbot(sb);
34911+ for (bindex = 0; !err && bindex <= bbot; bindex++)
1facf9fc 34912+ if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0)
34913+ err = do_xib_restore
34914+ (sb, au_sbr(sb, bindex)->br_xino.xi_file, page);
34915+ else
34916+ AuDbg("b%d\n", bindex);
34917+ free_page((unsigned long)page);
34918+
4f0767ce 34919+out:
1facf9fc 34920+ return err;
34921+}
34922+
34923+int au_xib_trunc(struct super_block *sb)
34924+{
34925+ int err;
34926+ ssize_t sz;
34927+ loff_t pos;
34928+ struct au_xino_lock_dir ldir;
34929+ struct au_sbinfo *sbinfo;
34930+ unsigned long *p;
34931+ struct file *file;
34932+
dece6358
AM
34933+ SiMustWriteLock(sb);
34934+
1facf9fc 34935+ err = 0;
34936+ sbinfo = au_sbi(sb);
34937+ if (!au_opt_test(sbinfo->si_mntflags, XINO))
34938+ goto out;
34939+
34940+ file = sbinfo->si_xib;
c06a8ce3 34941+ if (vfsub_f_size_read(file) <= PAGE_SIZE)
1facf9fc 34942+ goto out;
34943+
34944+ au_xino_lock_dir(sb, file, &ldir);
34945+ /* mnt_want_write() is unnecessary here */
34946+ file = au_xino_create2(sbinfo->si_xib, NULL);
34947+ au_xino_unlock_dir(&ldir);
34948+ err = PTR_ERR(file);
34949+ if (IS_ERR(file))
34950+ goto out;
34951+ fput(sbinfo->si_xib);
34952+ sbinfo->si_xib = file;
34953+
34954+ p = sbinfo->si_xib_buf;
34955+ memset(p, 0, PAGE_SIZE);
34956+ pos = 0;
34957+ sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
34958+ if (unlikely(sz != PAGE_SIZE)) {
34959+ err = sz;
34960+ AuIOErr("err %d\n", err);
34961+ if (sz >= 0)
34962+ err = -EIO;
34963+ goto out;
34964+ }
34965+
34966+ mutex_lock(&sbinfo->si_xib_mtx);
34967+ /* mnt_want_write() is unnecessary here */
34968+ err = xib_restore(sb);
34969+ mutex_unlock(&sbinfo->si_xib_mtx);
34970+
34971+out:
34972+ return err;
34973+}
34974+
34975+/* ---------------------------------------------------------------------- */
34976+
34977+/*
34978+ * xino mount option handlers
34979+ */
1facf9fc 34980+
34981+/* xino bitmap */
34982+static void xino_clear_xib(struct super_block *sb)
34983+{
34984+ struct au_sbinfo *sbinfo;
34985+
dece6358
AM
34986+ SiMustWriteLock(sb);
34987+
1facf9fc 34988+ sbinfo = au_sbi(sb);
34989+ sbinfo->si_xread = NULL;
34990+ sbinfo->si_xwrite = NULL;
34991+ if (sbinfo->si_xib)
34992+ fput(sbinfo->si_xib);
34993+ sbinfo->si_xib = NULL;
34994+ free_page((unsigned long)sbinfo->si_xib_buf);
34995+ sbinfo->si_xib_buf = NULL;
34996+}
34997+
34998+static int au_xino_set_xib(struct super_block *sb, struct file *base)
34999+{
35000+ int err;
35001+ loff_t pos;
35002+ struct au_sbinfo *sbinfo;
35003+ struct file *file;
35004+
dece6358
AM
35005+ SiMustWriteLock(sb);
35006+
1facf9fc 35007+ sbinfo = au_sbi(sb);
35008+ file = au_xino_create2(base, sbinfo->si_xib);
35009+ err = PTR_ERR(file);
35010+ if (IS_ERR(file))
35011+ goto out;
35012+ if (sbinfo->si_xib)
35013+ fput(sbinfo->si_xib);
35014+ sbinfo->si_xib = file;
5527c038
JR
35015+ sbinfo->si_xread = vfs_readf(file);
35016+ sbinfo->si_xwrite = vfs_writef(file);
1facf9fc 35017+
35018+ err = -ENOMEM;
35019+ if (!sbinfo->si_xib_buf)
35020+ sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
35021+ if (unlikely(!sbinfo->si_xib_buf))
35022+ goto out_unset;
35023+
35024+ sbinfo->si_xib_last_pindex = 0;
35025+ sbinfo->si_xib_next_bit = 0;
c06a8ce3 35026+ if (vfsub_f_size_read(file) < PAGE_SIZE) {
1facf9fc 35027+ pos = 0;
35028+ err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
35029+ PAGE_SIZE, &pos);
35030+ if (unlikely(err != PAGE_SIZE))
35031+ goto out_free;
35032+ }
35033+ err = 0;
35034+ goto out; /* success */
35035+
4f0767ce 35036+out_free:
1facf9fc 35037+ free_page((unsigned long)sbinfo->si_xib_buf);
b752ccd1
AM
35038+ sbinfo->si_xib_buf = NULL;
35039+ if (err >= 0)
35040+ err = -EIO;
4f0767ce 35041+out_unset:
b752ccd1
AM
35042+ fput(sbinfo->si_xib);
35043+ sbinfo->si_xib = NULL;
35044+ sbinfo->si_xread = NULL;
35045+ sbinfo->si_xwrite = NULL;
4f0767ce 35046+out:
b752ccd1 35047+ return err;
1facf9fc 35048+}
35049+
b752ccd1
AM
35050+/* xino for each branch */
35051+static void xino_clear_br(struct super_block *sb)
35052+{
5afbbe0d 35053+ aufs_bindex_t bindex, bbot;
b752ccd1 35054+ struct au_branch *br;
1facf9fc 35055+
5afbbe0d
AM
35056+ bbot = au_sbbot(sb);
35057+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
35058+ br = au_sbr(sb, bindex);
35059+ if (!br || !br->br_xino.xi_file)
35060+ continue;
35061+
35062+ fput(br->br_xino.xi_file);
35063+ br->br_xino.xi_file = NULL;
35064+ }
35065+}
35066+
35067+static int au_xino_set_br(struct super_block *sb, struct file *base)
1facf9fc 35068+{
35069+ int err;
b752ccd1 35070+ ino_t ino;
5afbbe0d 35071+ aufs_bindex_t bindex, bbot, bshared;
b752ccd1
AM
35072+ struct {
35073+ struct file *old, *new;
35074+ } *fpair, *p;
35075+ struct au_branch *br;
35076+ struct inode *inode;
5527c038 35077+ vfs_writef_t writef;
1facf9fc 35078+
b752ccd1
AM
35079+ SiMustWriteLock(sb);
35080+
35081+ err = -ENOMEM;
5afbbe0d
AM
35082+ bbot = au_sbbot(sb);
35083+ fpair = kcalloc(bbot + 1, sizeof(*fpair), GFP_NOFS);
b752ccd1 35084+ if (unlikely(!fpair))
1facf9fc 35085+ goto out;
35086+
5527c038 35087+ inode = d_inode(sb->s_root);
b752ccd1
AM
35088+ ino = AUFS_ROOT_INO;
35089+ writef = au_sbi(sb)->si_xwrite;
5afbbe0d 35090+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
35091+ bshared = is_sb_shared(sb, bindex, bindex - 1);
35092+ if (bshared >= 0) {
35093+ /* shared xino */
35094+ *p = fpair[bshared];
35095+ get_file(p->new);
35096+ }
35097+
35098+ if (!p->new) {
35099+ /* new xino */
5afbbe0d 35100+ br = au_sbr(sb, bindex);
b752ccd1
AM
35101+ p->old = br->br_xino.xi_file;
35102+ p->new = au_xino_create2(base, br->br_xino.xi_file);
35103+ err = PTR_ERR(p->new);
35104+ if (IS_ERR(p->new)) {
35105+ p->new = NULL;
35106+ goto out_pair;
35107+ }
35108+ }
35109+
35110+ err = au_xino_do_write(writef, p->new,
35111+ au_h_iptr(inode, bindex)->i_ino, ino);
35112+ if (unlikely(err))
35113+ goto out_pair;
35114+ }
35115+
5afbbe0d 35116+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++) {
b752ccd1
AM
35117+ br = au_sbr(sb, bindex);
35118+ if (br->br_xino.xi_file)
35119+ fput(br->br_xino.xi_file);
35120+ get_file(p->new);
35121+ br->br_xino.xi_file = p->new;
35122+ }
1facf9fc 35123+
4f0767ce 35124+out_pair:
5afbbe0d 35125+ for (bindex = 0, p = fpair; bindex <= bbot; bindex++, p++)
b752ccd1
AM
35126+ if (p->new)
35127+ fput(p->new);
35128+ else
35129+ break;
35130+ kfree(fpair);
4f0767ce 35131+out:
1facf9fc 35132+ return err;
35133+}
b752ccd1
AM
35134+
35135+void au_xino_clr(struct super_block *sb)
35136+{
35137+ struct au_sbinfo *sbinfo;
35138+
35139+ au_xigen_clr(sb);
35140+ xino_clear_xib(sb);
35141+ xino_clear_br(sb);
35142+ sbinfo = au_sbi(sb);
35143+ /* lvalue, do not call au_mntflags() */
35144+ au_opt_clr(sbinfo->si_mntflags, XINO);
35145+}
35146+
35147+int au_xino_set(struct super_block *sb, struct au_opt_xino *xino, int remount)
35148+{
35149+ int err, skip;
35150+ struct dentry *parent, *cur_parent;
35151+ struct qstr *dname, *cur_name;
35152+ struct file *cur_xino;
35153+ struct inode *dir;
35154+ struct au_sbinfo *sbinfo;
35155+
35156+ SiMustWriteLock(sb);
35157+
35158+ err = 0;
35159+ sbinfo = au_sbi(sb);
2000de60 35160+ parent = dget_parent(xino->file->f_path.dentry);
b752ccd1
AM
35161+ if (remount) {
35162+ skip = 0;
2000de60 35163+ dname = &xino->file->f_path.dentry->d_name;
b752ccd1
AM
35164+ cur_xino = sbinfo->si_xib;
35165+ if (cur_xino) {
2000de60
JR
35166+ cur_parent = dget_parent(cur_xino->f_path.dentry);
35167+ cur_name = &cur_xino->f_path.dentry->d_name;
b752ccd1 35168+ skip = (cur_parent == parent
38d290e6 35169+ && au_qstreq(dname, cur_name));
b752ccd1
AM
35170+ dput(cur_parent);
35171+ }
35172+ if (skip)
35173+ goto out;
35174+ }
35175+
35176+ au_opt_set(sbinfo->si_mntflags, XINO);
5527c038 35177+ dir = d_inode(parent);
febd17d6 35178+ inode_lock_nested(dir, AuLsc_I_PARENT);
b752ccd1
AM
35179+ /* mnt_want_write() is unnecessary here */
35180+ err = au_xino_set_xib(sb, xino->file);
35181+ if (!err)
35182+ err = au_xigen_set(sb, xino->file);
35183+ if (!err)
35184+ err = au_xino_set_br(sb, xino->file);
febd17d6 35185+ inode_unlock(dir);
b752ccd1
AM
35186+ if (!err)
35187+ goto out; /* success */
35188+
35189+ /* reset all */
35190+ AuIOErr("failed creating xino(%d).\n", err);
c1595e42
JR
35191+ au_xigen_clr(sb);
35192+ xino_clear_xib(sb);
b752ccd1 35193+
4f0767ce 35194+out:
b752ccd1
AM
35195+ dput(parent);
35196+ return err;
35197+}
35198+
35199+/* ---------------------------------------------------------------------- */
35200+
35201+/*
35202+ * create a xinofile at the default place/path.
35203+ */
35204+struct file *au_xino_def(struct super_block *sb)
35205+{
35206+ struct file *file;
35207+ char *page, *p;
35208+ struct au_branch *br;
35209+ struct super_block *h_sb;
35210+ struct path path;
5afbbe0d 35211+ aufs_bindex_t bbot, bindex, bwr;
b752ccd1
AM
35212+
35213+ br = NULL;
5afbbe0d 35214+ bbot = au_sbbot(sb);
b752ccd1 35215+ bwr = -1;
5afbbe0d 35216+ for (bindex = 0; bindex <= bbot; bindex++) {
b752ccd1
AM
35217+ br = au_sbr(sb, bindex);
35218+ if (au_br_writable(br->br_perm)
86dc4139 35219+ && !au_test_fs_bad_xino(au_br_sb(br))) {
b752ccd1
AM
35220+ bwr = bindex;
35221+ break;
35222+ }
35223+ }
35224+
7f207e10
AM
35225+ if (bwr >= 0) {
35226+ file = ERR_PTR(-ENOMEM);
537831f9 35227+ page = (void *)__get_free_page(GFP_NOFS);
7f207e10
AM
35228+ if (unlikely(!page))
35229+ goto out;
86dc4139 35230+ path.mnt = au_br_mnt(br);
7f207e10
AM
35231+ path.dentry = au_h_dptr(sb->s_root, bwr);
35232+ p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
35233+ file = (void *)p;
35234+ if (!IS_ERR(p)) {
35235+ strcat(p, "/" AUFS_XINO_FNAME);
35236+ AuDbg("%s\n", p);
35237+ file = au_xino_create(sb, p, /*silent*/0);
35238+ if (!IS_ERR(file))
35239+ au_xino_brid_set(sb, br->br_id);
35240+ }
537831f9 35241+ free_page((unsigned long)page);
7f207e10
AM
35242+ } else {
35243+ file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
35244+ if (IS_ERR(file))
35245+ goto out;
2000de60 35246+ h_sb = file->f_path.dentry->d_sb;
7f207e10
AM
35247+ if (unlikely(au_test_fs_bad_xino(h_sb))) {
35248+ pr_err("xino doesn't support %s(%s)\n",
35249+ AUFS_XINO_DEFPATH, au_sbtype(h_sb));
35250+ fput(file);
35251+ file = ERR_PTR(-EINVAL);
35252+ }
35253+ if (!IS_ERR(file))
35254+ au_xino_brid_set(sb, -1);
35255+ }
0c5527e5 35256+
7f207e10
AM
35257+out:
35258+ return file;
35259+}
35260+
35261+/* ---------------------------------------------------------------------- */
35262+
35263+int au_xino_path(struct seq_file *seq, struct file *file)
35264+{
35265+ int err;
35266+
35267+ err = au_seq_path(seq, &file->f_path);
79b8bda9 35268+ if (unlikely(err))
7f207e10
AM
35269+ goto out;
35270+
7f207e10
AM
35271+#define Deleted "\\040(deleted)"
35272+ seq->count -= sizeof(Deleted) - 1;
35273+ AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
35274+ sizeof(Deleted) - 1));
35275+#undef Deleted
35276+
35277+out:
35278+ return err;
35279+}
537831f9
AM
35280diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
35281--- /usr/share/empty/include/uapi/linux/aufs_type.h 1970-01-01 01:00:00.000000000 +0100
5afbbe0d 35282+++ linux/include/uapi/linux/aufs_type.h 2016-07-25 19:05:34.817826663 +0200
c1595e42 35283@@ -0,0 +1,419 @@
7f207e10 35284+/*
8cdd5066 35285+ * Copyright (C) 2005-2016 Junjiro R. Okajima
7f207e10
AM
35286+ *
35287+ * This program, aufs is free software; you can redistribute it and/or modify
35288+ * it under the terms of the GNU General Public License as published by
35289+ * the Free Software Foundation; either version 2 of the License, or
35290+ * (at your option) any later version.
35291+ *
35292+ * This program is distributed in the hope that it will be useful,
35293+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
35294+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35295+ * GNU General Public License for more details.
35296+ *
35297+ * You should have received a copy of the GNU General Public License
523b37e3 35298+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
7f207e10
AM
35299+ */
35300+
35301+#ifndef __AUFS_TYPE_H__
35302+#define __AUFS_TYPE_H__
35303+
f6c5ef8b
AM
35304+#define AUFS_NAME "aufs"
35305+
9dbd164d 35306+#ifdef __KERNEL__
f6c5ef8b
AM
35307+/*
35308+ * define it before including all other headers.
35309+ * sched.h may use pr_* macros before defining "current", so define the
35310+ * no-current version first, and re-define later.
35311+ */
35312+#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
35313+#include <linux/sched.h>
35314+#undef pr_fmt
a2a7ad62
AM
35315+#define pr_fmt(fmt) \
35316+ AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
35317+ (int)sizeof(current->comm), current->comm, current->pid
9dbd164d
AM
35318+#else
35319+#include <stdint.h>
35320+#include <sys/types.h>
f6c5ef8b 35321+#endif /* __KERNEL__ */
7f207e10 35322+
f6c5ef8b
AM
35323+#include <linux/limits.h>
35324+
5afbbe0d 35325+#define AUFS_VERSION "4.x-rcN-20160704"
7f207e10
AM
35326+
35327+/* todo? move this to linux-2.6.19/include/magic.h */
35328+#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
35329+
35330+/* ---------------------------------------------------------------------- */
35331+
35332+#ifdef CONFIG_AUFS_BRANCH_MAX_127
9dbd164d 35333+typedef int8_t aufs_bindex_t;
7f207e10
AM
35334+#define AUFS_BRANCH_MAX 127
35335+#else
9dbd164d 35336+typedef int16_t aufs_bindex_t;
7f207e10
AM
35337+#ifdef CONFIG_AUFS_BRANCH_MAX_511
35338+#define AUFS_BRANCH_MAX 511
35339+#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
35340+#define AUFS_BRANCH_MAX 1023
35341+#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
35342+#define AUFS_BRANCH_MAX 32767
35343+#endif
35344+#endif
35345+
35346+#ifdef __KERNEL__
35347+#ifndef AUFS_BRANCH_MAX
35348+#error unknown CONFIG_AUFS_BRANCH_MAX value
35349+#endif
35350+#endif /* __KERNEL__ */
35351+
35352+/* ---------------------------------------------------------------------- */
35353+
7f207e10
AM
35354+#define AUFS_FSTYPE AUFS_NAME
35355+
35356+#define AUFS_ROOT_INO 2
35357+#define AUFS_FIRST_INO 11
35358+
35359+#define AUFS_WH_PFX ".wh."
35360+#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
35361+#define AUFS_WH_TMP_LEN 4
86dc4139 35362+/* a limit for rmdir/rename a dir and copyup */
7f207e10
AM
35363+#define AUFS_MAX_NAMELEN (NAME_MAX \
35364+ - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
35365+ - 1 /* dot */\
35366+ - AUFS_WH_TMP_LEN) /* hex */
35367+#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
35368+#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
392086de
AM
35369+#define AUFS_XINO_DEF_SEC 30 /* seconds */
35370+#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
7f207e10
AM
35371+#define AUFS_DIRWH_DEF 3
35372+#define AUFS_RDCACHE_DEF 10 /* seconds */
027c5e7a 35373+#define AUFS_RDCACHE_MAX 3600 /* seconds */
7f207e10
AM
35374+#define AUFS_RDBLK_DEF 512 /* bytes */
35375+#define AUFS_RDHASH_DEF 32
35376+#define AUFS_WKQ_NAME AUFS_NAME "d"
027c5e7a
AM
35377+#define AUFS_MFS_DEF_SEC 30 /* seconds */
35378+#define AUFS_MFS_MAX_SEC 3600 /* seconds */
076b876e 35379+#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
86dc4139 35380+#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
7f207e10
AM
35381+
35382+/* pseudo-link maintenace under /proc */
35383+#define AUFS_PLINK_MAINT_NAME "plink_maint"
35384+#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
35385+#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
35386+
35387+#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
35388+#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
35389+
35390+#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
35391+#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
35392+#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
35393+
35394+/* doubly whiteouted */
35395+#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
35396+#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
35397+#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
35398+
1e00d052 35399+/* branch permissions and attributes */
7f207e10
AM
35400+#define AUFS_BRPERM_RW "rw"
35401+#define AUFS_BRPERM_RO "ro"
35402+#define AUFS_BRPERM_RR "rr"
076b876e
AM
35403+#define AUFS_BRATTR_COO_REG "coo_reg"
35404+#define AUFS_BRATTR_COO_ALL "coo_all"
35405+#define AUFS_BRATTR_FHSM "fhsm"
35406+#define AUFS_BRATTR_UNPIN "unpin"
c1595e42
JR
35407+#define AUFS_BRATTR_ICEX "icex"
35408+#define AUFS_BRATTR_ICEX_SEC "icexsec"
35409+#define AUFS_BRATTR_ICEX_SYS "icexsys"
35410+#define AUFS_BRATTR_ICEX_TR "icextr"
35411+#define AUFS_BRATTR_ICEX_USR "icexusr"
35412+#define AUFS_BRATTR_ICEX_OTH "icexoth"
1e00d052
AM
35413+#define AUFS_BRRATTR_WH "wh"
35414+#define AUFS_BRWATTR_NLWH "nolwh"
076b876e
AM
35415+#define AUFS_BRWATTR_MOO "moo"
35416+
35417+#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
35418+#define AuBrPerm_RO (1 << 1) /* readonly */
35419+#define AuBrPerm_RR (1 << 2) /* natively readonly */
35420+#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
35421+
35422+#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
35423+#define AuBrAttr_COO_ALL (1 << 4)
35424+#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
35425+
35426+#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
35427+#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
c1595e42
JR
35428+ branch. meaningless since
35429+ linux-3.18-rc1 */
35430+
35431+/* ignore error in copying XATTR */
35432+#define AuBrAttr_ICEX_SEC (1 << 7)
35433+#define AuBrAttr_ICEX_SYS (1 << 8)
35434+#define AuBrAttr_ICEX_TR (1 << 9)
35435+#define AuBrAttr_ICEX_USR (1 << 10)
35436+#define AuBrAttr_ICEX_OTH (1 << 11)
35437+#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
35438+ | AuBrAttr_ICEX_SYS \
35439+ | AuBrAttr_ICEX_TR \
35440+ | AuBrAttr_ICEX_USR \
35441+ | AuBrAttr_ICEX_OTH)
35442+
35443+#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
076b876e
AM
35444+#define AuBrRAttr_Mask AuBrRAttr_WH
35445+
c1595e42
JR
35446+#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
35447+#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
076b876e
AM
35448+#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
35449+
35450+#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
35451+
c1595e42 35452+/* #warning test userspace */
076b876e
AM
35453+#ifdef __KERNEL__
35454+#ifndef CONFIG_AUFS_FHSM
35455+#undef AuBrAttr_FHSM
35456+#define AuBrAttr_FHSM 0
35457+#endif
c1595e42
JR
35458+#ifndef CONFIG_AUFS_XATTR
35459+#undef AuBrAttr_ICEX
35460+#define AuBrAttr_ICEX 0
35461+#undef AuBrAttr_ICEX_SEC
35462+#define AuBrAttr_ICEX_SEC 0
35463+#undef AuBrAttr_ICEX_SYS
35464+#define AuBrAttr_ICEX_SYS 0
35465+#undef AuBrAttr_ICEX_TR
35466+#define AuBrAttr_ICEX_TR 0
35467+#undef AuBrAttr_ICEX_USR
35468+#define AuBrAttr_ICEX_USR 0
35469+#undef AuBrAttr_ICEX_OTH
35470+#define AuBrAttr_ICEX_OTH 0
35471+#endif
076b876e
AM
35472+#endif
35473+
35474+/* the longest combination */
c1595e42
JR
35475+/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
35476+#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
35477+ "+" AUFS_BRATTR_COO_REG \
35478+ "+" AUFS_BRATTR_FHSM \
35479+ "+" AUFS_BRATTR_UNPIN \
7e9cd9fe
AM
35480+ "+" AUFS_BRATTR_ICEX_SEC \
35481+ "+" AUFS_BRATTR_ICEX_SYS \
35482+ "+" AUFS_BRATTR_ICEX_USR \
35483+ "+" AUFS_BRATTR_ICEX_OTH \
076b876e
AM
35484+ "+" AUFS_BRWATTR_NLWH)
35485+
35486+typedef struct {
35487+ char a[AuBrPermStrSz];
35488+} au_br_perm_str_t;
35489+
35490+static inline int au_br_writable(int brperm)
35491+{
35492+ return brperm & AuBrPerm_RW;
35493+}
35494+
35495+static inline int au_br_whable(int brperm)
35496+{
35497+ return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
35498+}
35499+
35500+static inline int au_br_wh_linkable(int brperm)
35501+{
35502+ return !(brperm & AuBrWAttr_NoLinkWH);
35503+}
35504+
35505+static inline int au_br_cmoo(int brperm)
35506+{
35507+ return brperm & AuBrAttr_CMOO_Mask;
35508+}
35509+
35510+static inline int au_br_fhsm(int brperm)
35511+{
35512+ return brperm & AuBrAttr_FHSM;
35513+}
7f207e10
AM
35514+
35515+/* ---------------------------------------------------------------------- */
35516+
35517+/* ioctl */
35518+enum {
35519+ /* readdir in userspace */
35520+ AuCtl_RDU,
35521+ AuCtl_RDU_INO,
35522+
076b876e
AM
35523+ AuCtl_WBR_FD, /* pathconf wrapper */
35524+ AuCtl_IBUSY, /* busy inode */
35525+ AuCtl_MVDOWN, /* move-down */
35526+ AuCtl_BR, /* info about branches */
35527+ AuCtl_FHSM_FD /* connection for fhsm */
7f207e10
AM
35528+};
35529+
35530+/* borrowed from linux/include/linux/kernel.h */
35531+#ifndef ALIGN
35532+#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
35533+#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
35534+#endif
35535+
35536+/* borrowed from linux/include/linux/compiler-gcc3.h */
35537+#ifndef __aligned
35538+#define __aligned(x) __attribute__((aligned(x)))
53392da6
AM
35539+#endif
35540+
35541+#ifdef __KERNEL__
35542+#ifndef __packed
7f207e10
AM
35543+#define __packed __attribute__((packed))
35544+#endif
53392da6 35545+#endif
7f207e10
AM
35546+
35547+struct au_rdu_cookie {
9dbd164d
AM
35548+ uint64_t h_pos;
35549+ int16_t bindex;
35550+ uint8_t flags;
35551+ uint8_t pad;
35552+ uint32_t generation;
7f207e10
AM
35553+} __aligned(8);
35554+
35555+struct au_rdu_ent {
9dbd164d
AM
35556+ uint64_t ino;
35557+ int16_t bindex;
35558+ uint8_t type;
35559+ uint8_t nlen;
35560+ uint8_t wh;
7f207e10
AM
35561+ char name[0];
35562+} __aligned(8);
35563+
35564+static inline int au_rdu_len(int nlen)
35565+{
35566+ /* include the terminating NULL */
35567+ return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
9dbd164d 35568+ sizeof(uint64_t));
7f207e10
AM
35569+}
35570+
35571+union au_rdu_ent_ul {
35572+ struct au_rdu_ent __user *e;
9dbd164d 35573+ uint64_t ul;
7f207e10
AM
35574+};
35575+
35576+enum {
35577+ AufsCtlRduV_SZ,
35578+ AufsCtlRduV_End
35579+};
35580+
35581+struct aufs_rdu {
35582+ /* input */
35583+ union {
9dbd164d
AM
35584+ uint64_t sz; /* AuCtl_RDU */
35585+ uint64_t nent; /* AuCtl_RDU_INO */
7f207e10
AM
35586+ };
35587+ union au_rdu_ent_ul ent;
9dbd164d 35588+ uint16_t verify[AufsCtlRduV_End];
7f207e10
AM
35589+
35590+ /* input/output */
9dbd164d 35591+ uint32_t blk;
7f207e10
AM
35592+
35593+ /* output */
35594+ union au_rdu_ent_ul tail;
35595+ /* number of entries which were added in a single call */
9dbd164d
AM
35596+ uint64_t rent;
35597+ uint8_t full;
35598+ uint8_t shwh;
7f207e10
AM
35599+
35600+ struct au_rdu_cookie cookie;
35601+} __aligned(8);
35602+
1e00d052
AM
35603+/* ---------------------------------------------------------------------- */
35604+
35605+struct aufs_wbr_fd {
9dbd164d
AM
35606+ uint32_t oflags;
35607+ int16_t brid;
1e00d052
AM
35608+} __aligned(8);
35609+
35610+/* ---------------------------------------------------------------------- */
35611+
027c5e7a 35612+struct aufs_ibusy {
9dbd164d
AM
35613+ uint64_t ino, h_ino;
35614+ int16_t bindex;
027c5e7a
AM
35615+} __aligned(8);
35616+
1e00d052
AM
35617+/* ---------------------------------------------------------------------- */
35618+
392086de
AM
35619+/* error code for move-down */
35620+/* the actual message strings are implemented in aufs-util.git */
35621+enum {
35622+ EAU_MVDOWN_OPAQUE = 1,
35623+ EAU_MVDOWN_WHITEOUT,
35624+ EAU_MVDOWN_UPPER,
35625+ EAU_MVDOWN_BOTTOM,
35626+ EAU_MVDOWN_NOUPPER,
35627+ EAU_MVDOWN_NOLOWERBR,
35628+ EAU_Last
35629+};
35630+
c2b27bf2 35631+/* flags for move-down */
392086de
AM
35632+#define AUFS_MVDOWN_DMSG 1
35633+#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
35634+#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
35635+#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
35636+#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
35637+#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
35638+#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
35639+#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
35640+#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
076b876e
AM
35641+#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
35642+#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
35643+#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
35644+#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
c2b27bf2 35645+
076b876e 35646+/* index for move-down */
392086de
AM
35647+enum {
35648+ AUFS_MVDOWN_UPPER,
35649+ AUFS_MVDOWN_LOWER,
35650+ AUFS_MVDOWN_NARRAY
35651+};
35652+
076b876e
AM
35653+/*
35654+ * additional info of move-down
35655+ * number of free blocks and inodes.
35656+ * subset of struct kstatfs, but smaller and always 64bit.
35657+ */
35658+struct aufs_stfs {
35659+ uint64_t f_blocks;
35660+ uint64_t f_bavail;
35661+ uint64_t f_files;
35662+ uint64_t f_ffree;
35663+};
35664+
35665+struct aufs_stbr {
35666+ int16_t brid; /* optional input */
35667+ int16_t bindex; /* output */
35668+ struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
35669+} __aligned(8);
35670+
c2b27bf2 35671+struct aufs_mvdown {
076b876e
AM
35672+ uint32_t flags; /* input/output */
35673+ struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
35674+ int8_t au_errno; /* output */
35675+} __aligned(8);
35676+
35677+/* ---------------------------------------------------------------------- */
35678+
35679+union aufs_brinfo {
35680+ /* PATH_MAX may differ between kernel-space and user-space */
35681+ char _spacer[4096];
392086de 35682+ struct {
076b876e
AM
35683+ int16_t id;
35684+ int perm;
35685+ char path[0];
35686+ };
c2b27bf2
AM
35687+} __aligned(8);
35688+
35689+/* ---------------------------------------------------------------------- */
35690+
7f207e10
AM
35691+#define AuCtlType 'A'
35692+#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
35693+#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
1e00d052
AM
35694+#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
35695+ struct aufs_wbr_fd)
027c5e7a 35696+#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
392086de
AM
35697+#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
35698+ struct aufs_mvdown)
076b876e
AM
35699+#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
35700+#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
7f207e10
AM
35701+
35702+#endif /* __AUFS_TYPE_H__ */
5afbbe0d 35703aufs4.x-rcN loopback patch
5527c038
JR
35704
35705diff --git a/drivers/block/loop.c b/drivers/block/loop.c
5afbbe0d 35706index 7339e65..76e5da4 100644
5527c038
JR
35707--- a/drivers/block/loop.c
35708+++ b/drivers/block/loop.c
5afbbe0d 35709@@ -562,7 +562,7 @@ static int do_req_filebacked(struct loop_device *lo, struct request *rq)
5527c038
JR
35710 }
35711
35712 struct switch_request {
35713- struct file *file;
35714+ struct file *file, *virt_file;
35715 struct completion wait;
35716 };
35717
5afbbe0d 35718@@ -588,6 +588,7 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
5527c038
JR
35719 mapping = file->f_mapping;
35720 mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
35721 lo->lo_backing_file = file;
35722+ lo->lo_backing_virt_file = p->virt_file;
35723 lo->lo_blocksize = S_ISBLK(mapping->host->i_mode) ?
35724 mapping->host->i_bdev->bd_block_size : PAGE_SIZE;
35725 lo->old_gfp_mask = mapping_gfp_mask(mapping);
5afbbe0d 35726@@ -600,11 +601,13 @@ static void do_loop_switch(struct loop_device *lo, struct switch_request *p)
5527c038
JR
35727 * First it needs to flush existing IO, it does this by sending a magic
35728 * BIO down the pipe. The completion of this BIO does the actual switch.
35729 */
35730-static int loop_switch(struct loop_device *lo, struct file *file)
35731+static int loop_switch(struct loop_device *lo, struct file *file,
35732+ struct file *virt_file)
35733 {
35734 struct switch_request w;
35735
35736 w.file = file;
35737+ w.virt_file = virt_file;
35738
35739 /* freeze queue and wait for completion of scheduled requests */
35740 blk_mq_freeze_queue(lo->lo_queue);
5afbbe0d 35741@@ -623,7 +626,16 @@ static int loop_switch(struct loop_device *lo, struct file *file)
5527c038
JR
35742 */
35743 static int loop_flush(struct loop_device *lo)
35744 {
35745- return loop_switch(lo, NULL);
35746+ return loop_switch(lo, NULL, NULL);
35747+}
35748+
35749+static struct file *loop_real_file(struct file *file)
35750+{
35751+ struct file *f = NULL;
35752+
35753+ if (file->f_path.dentry->d_sb->s_op->real_loop)
35754+ f = file->f_path.dentry->d_sb->s_op->real_loop(file);
35755+ return f;
35756 }
35757
c2c0f25c 35758 static void loop_reread_partitions(struct loop_device *lo,
5afbbe0d 35759@@ -660,6 +672,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
35760 unsigned int arg)
35761 {
35762 struct file *file, *old_file;
35763+ struct file *f, *virt_file = NULL, *old_virt_file;
35764 struct inode *inode;
35765 int error;
35766
5afbbe0d 35767@@ -676,9 +689,16 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
35768 file = fget(arg);
35769 if (!file)
35770 goto out;
35771+ f = loop_real_file(file);
35772+ if (f) {
35773+ virt_file = file;
35774+ file = f;
35775+ get_file(file);
35776+ }
35777
35778 inode = file->f_mapping->host;
35779 old_file = lo->lo_backing_file;
35780+ old_virt_file = lo->lo_backing_virt_file;
35781
35782 error = -EINVAL;
35783
5afbbe0d 35784@@ -690,17 +710,21 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
5527c038
JR
35785 goto out_putf;
35786
35787 /* and ... switch */
35788- error = loop_switch(lo, file);
35789+ error = loop_switch(lo, file, virt_file);
35790 if (error)
35791 goto out_putf;
35792
35793 fput(old_file);
35794+ if (old_virt_file)
35795+ fput(old_virt_file);
35796 if (lo->lo_flags & LO_FLAGS_PARTSCAN)
c2c0f25c 35797 loop_reread_partitions(lo, bdev);
5527c038
JR
35798 return 0;
35799
35800 out_putf:
35801 fput(file);
35802+ if (virt_file)
35803+ fput(virt_file);
35804 out:
35805 return error;
35806 }
5afbbe0d 35807@@ -887,7 +911,7 @@ static int loop_prepare_queue(struct loop_device *lo)
5527c038
JR
35808 static int loop_set_fd(struct loop_device *lo, fmode_t mode,
35809 struct block_device *bdev, unsigned int arg)
35810 {
35811- struct file *file, *f;
35812+ struct file *file, *f, *virt_file = NULL;
35813 struct inode *inode;
35814 struct address_space *mapping;
35815 unsigned lo_blocksize;
5afbbe0d 35816@@ -902,6 +926,12 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
35817 file = fget(arg);
35818 if (!file)
35819 goto out;
35820+ f = loop_real_file(file);
35821+ if (f) {
35822+ virt_file = file;
35823+ file = f;
35824+ get_file(file);
35825+ }
35826
35827 error = -EBUSY;
35828 if (lo->lo_state != Lo_unbound)
5afbbe0d 35829@@ -954,6 +984,7 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
35830 lo->lo_device = bdev;
35831 lo->lo_flags = lo_flags;
35832 lo->lo_backing_file = file;
35833+ lo->lo_backing_virt_file = virt_file;
35834 lo->transfer = NULL;
35835 lo->ioctl = NULL;
35836 lo->lo_sizelimit = 0;
5afbbe0d 35837@@ -986,6 +1017,8 @@ static int loop_set_fd(struct loop_device *lo, fmode_t mode,
5527c038
JR
35838
35839 out_putf:
35840 fput(file);
35841+ if (virt_file)
35842+ fput(virt_file);
35843 out:
35844 /* This is safe: open() is still holding a reference. */
35845 module_put(THIS_MODULE);
5afbbe0d 35846@@ -1032,6 +1065,7 @@ loop_init_xfer(struct loop_device *lo, struct loop_func_table *xfer,
5527c038
JR
35847 static int loop_clr_fd(struct loop_device *lo)
35848 {
35849 struct file *filp = lo->lo_backing_file;
35850+ struct file *virt_filp = lo->lo_backing_virt_file;
35851 gfp_t gfp = lo->old_gfp_mask;
35852 struct block_device *bdev = lo->lo_device;
35853
5afbbe0d 35854@@ -1063,6 +1097,7 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
35855 spin_lock_irq(&lo->lo_lock);
35856 lo->lo_state = Lo_rundown;
35857 lo->lo_backing_file = NULL;
35858+ lo->lo_backing_virt_file = NULL;
35859 spin_unlock_irq(&lo->lo_lock);
35860
35861 loop_release_xfer(lo);
5afbbe0d 35862@@ -1107,6 +1142,8 @@ static int loop_clr_fd(struct loop_device *lo)
5527c038
JR
35863 * bd_mutex which is usually taken before lo_ctl_mutex.
35864 */
35865 fput(filp);
35866+ if (virt_filp)
35867+ fput(virt_filp);
35868 return 0;
35869 }
35870
35871diff --git a/drivers/block/loop.h b/drivers/block/loop.h
be52b249 35872index fb2237c..c3888c5 100644
5527c038
JR
35873--- a/drivers/block/loop.h
35874+++ b/drivers/block/loop.h
35875@@ -46,7 +46,7 @@ struct loop_device {
35876 int (*ioctl)(struct loop_device *, int cmd,
35877 unsigned long arg);
35878
35879- struct file * lo_backing_file;
35880+ struct file * lo_backing_file, *lo_backing_virt_file;
35881 struct block_device *lo_device;
35882 unsigned lo_blocksize;
35883 void *key_data;
35884diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
5afbbe0d 35885index 504b767..09426ad 100644
5527c038
JR
35886--- a/fs/aufs/f_op.c
35887+++ b/fs/aufs/f_op.c
5afbbe0d 35888@@ -346,7 +346,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
5527c038
JR
35889 if (IS_ERR(h_file))
35890 goto out;
35891
35892- if (au_test_loopback_kthread()) {
35893+ if (0 && au_test_loopback_kthread()) {
35894 au_warn_loopback(h_file->f_path.dentry->d_sb);
35895 if (file->f_mapping != h_file->f_mapping) {
35896 file->f_mapping = h_file->f_mapping;
35897diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
8cdd5066 35898index 5711e7a..9df5d16 100644
5527c038
JR
35899--- a/fs/aufs/loop.c
35900+++ b/fs/aufs/loop.c
79b8bda9
AM
35901@@ -131,3 +131,19 @@ void au_loopback_fin(void)
35902 symbol_put(loop_backing_file);
5527c038
JR
35903 kfree(au_warn_loopback_array);
35904 }
35905+
35906+/* ---------------------------------------------------------------------- */
35907+
35908+/* support the loopback block device insude aufs */
35909+
35910+struct file *aufs_real_loop(struct file *file)
35911+{
35912+ struct file *f;
35913+
35914+ BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
35915+ fi_read_lock(file);
35916+ f = au_hf_top(file);
35917+ fi_read_unlock(file);
35918+ AuDebugOn(!f);
35919+ return f;
35920+}
35921diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
8cdd5066 35922index 48bf070..66afec7 100644
5527c038
JR
35923--- a/fs/aufs/loop.h
35924+++ b/fs/aufs/loop.h
35925@@ -25,7 +25,11 @@ void au_warn_loopback(struct super_block *h_sb);
35926
35927 int au_loopback_init(void);
35928 void au_loopback_fin(void);
35929+
35930+struct file *aufs_real_loop(struct file *file);
35931 #else
35932+AuStub(struct file *, loop_backing_file, return NULL)
35933+
35934 AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
35935 struct dentry *h_adding)
35936 AuStubInt0(au_test_loopback_kthread, void)
35937@@ -33,6 +37,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
35938
35939 AuStubInt0(au_loopback_init, void)
35940 AuStubVoid(au_loopback_fin, void)
35941+
35942+AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
35943 #endif /* BLK_DEV_LOOP */
35944
35945 #endif /* __KERNEL__ */
35946diff --git a/fs/aufs/super.c b/fs/aufs/super.c
5afbbe0d 35947index 8bd2d9c..26581c0 100644
5527c038
JR
35948--- a/fs/aufs/super.c
35949+++ b/fs/aufs/super.c
be52b249 35950@@ -832,7 +832,10 @@ static const struct super_operations aufs_sop = {
5527c038
JR
35951 .statfs = aufs_statfs,
35952 .put_super = aufs_put_super,
35953 .sync_fs = aufs_sync_fs,
35954- .remount_fs = aufs_remount_fs
35955+ .remount_fs = aufs_remount_fs,
35956+#ifdef CONFIG_AUFS_BDEV_LOOP
35957+ .real_loop = aufs_real_loop
35958+#endif
35959 };
35960
35961 /* ---------------------------------------------------------------------- */
35962diff --git a/include/linux/fs.h b/include/linux/fs.h
5afbbe0d 35963index 33c6953..f9b06a2 100644
5527c038
JR
35964--- a/include/linux/fs.h
35965+++ b/include/linux/fs.h
5afbbe0d 35966@@ -1805,6 +1805,10 @@ struct super_operations {
5527c038
JR
35967 struct shrink_control *);
35968 long (*free_cached_objects)(struct super_block *,
35969 struct shrink_control *);
35970+#if defined(CONFIG_BLK_DEV_LOOP) || defined(CONFIG_BLK_DEV_LOOP_MODULE)
35971+ /* and aufs */
35972+ struct file *(*real_loop)(struct file *);
35973+#endif
35974 };
35975
35976 /*
This page took 5.977813 seconds and 4 git commands to generate.